blob: c0eed2d5f46a40d4df4f8587a34a64d808f91d57 [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 Moolenaarcef9dcc2005-12-06 19:50:41 +0000207#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100208static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209#else
210# define ex_popup ex_ni
211#endif
212#ifndef FEAT_GUI_MSWIN
213# define ex_simalt ex_ni
214#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000215#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216# define gui_mch_find_dialog ex_ni
217# define gui_mch_replace_dialog ex_ni
218#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000219#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220# define ex_helpfind ex_ni
221#endif
222#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100223# define ex_cscope ex_ni
224# define ex_scscope ex_ni
225# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#endif
227#ifndef FEAT_SYN_HL
228# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200229# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000230#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100231#ifndef FEAT_EVAL
232# define ex_packadd ex_ni
233# define ex_packloadall ex_ni
234#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200235#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200236# define ex_syntime ex_ni
237#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000238#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000239# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000240# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000241# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000242# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000243# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000244#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200245#ifndef FEAT_PERSISTENT_UNDO
246# define ex_rundo ex_ni
247# define ex_wundo ex_ni
248#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200249#ifndef FEAT_LUA
250# define ex_lua ex_script_ni
251# define ex_luado ex_ni
252# define ex_luafile ex_ni
253#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000254#ifndef FEAT_MZSCHEME
255# define ex_mzscheme ex_script_ni
256# define ex_mzfile ex_ni
257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258#ifndef FEAT_PERL
259# define ex_perl ex_script_ni
260# define ex_perldo ex_ni
261#endif
262#ifndef FEAT_PYTHON
263# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200264# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265# define ex_pyfile ex_ni
266#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200267#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200268# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200269# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200270# define ex_py3file ex_ni
271#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100272#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
273# define ex_pyx ex_script_ni
274# define ex_pyxdo ex_ni
275# define ex_pyxfile ex_ni
276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#ifndef FEAT_TCL
278# define ex_tcl ex_script_ni
279# define ex_tcldo ex_ni
280# define ex_tclfile ex_ni
281#endif
282#ifndef FEAT_RUBY
283# define ex_ruby ex_script_ni
284# define ex_rubydo ex_ni
285# define ex_rubyfile ex_ni
286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287#ifndef FEAT_KEYMAP
288# define ex_loadkeymap ex_ni
289#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100290static void ex_swapname(exarg_T *eap);
291static void ex_syncbind(exarg_T *eap);
292static void ex_read(exarg_T *eap);
293static void ex_pwd(exarg_T *eap);
294static void ex_equal(exarg_T *eap);
295static void ex_sleep(exarg_T *eap);
296static void do_exmap(exarg_T *eap, int isabbrev);
297static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100298static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000299#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100300static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301#else
302# define ex_winpos ex_ni
303#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100304static void ex_operators(exarg_T *eap);
305static void ex_put(exarg_T *eap);
306static void ex_copymove(exarg_T *eap);
307static void ex_submagic(exarg_T *eap);
308static void ex_join(exarg_T *eap);
309static void ex_at(exarg_T *eap);
310static void ex_bang(exarg_T *eap);
311static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200312#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100313static void ex_wundo(exarg_T *eap);
314static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200315#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100316static void ex_redo(exarg_T *eap);
317static void ex_later(exarg_T *eap);
318static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100319static void ex_redrawstatus(exarg_T *eap);
320static void close_redir(void);
321static void ex_mkrc(exarg_T *eap);
322static void ex_mark(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323#ifdef FEAT_USR_CMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100324static char_u *uc_fun_cmd(void);
325static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100327static void ex_startinsert(exarg_T *eap);
328static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100330static void ex_checkpath(exarg_T *eap);
331static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332#else
333# define ex_findpat ex_ni
334# define ex_checkpath ex_ni
335#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200336#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100337static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338#else
339# define ex_psearch ex_ni
340#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100341static void ex_tag(exarg_T *eap);
342static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343#ifndef FEAT_EVAL
344# define ex_scriptnames ex_ni
345# define ex_finish ex_ni
346# define ex_echo ex_ni
347# define ex_echohl ex_ni
348# define ex_execute ex_ni
349# define ex_call ex_ni
350# define ex_if ex_ni
351# define ex_endif ex_ni
352# define ex_else ex_ni
353# define ex_while ex_ni
354# define ex_continue ex_ni
355# define ex_break ex_ni
356# define ex_endwhile ex_ni
357# define ex_throw ex_ni
358# define ex_try ex_ni
359# define ex_catch ex_ni
360# define ex_finally ex_ni
361# define ex_endtry ex_ni
362# define ex_endfunction ex_ni
363# define ex_let ex_ni
364# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000365# define ex_lockvar ex_ni
366# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367# define ex_function ex_ni
368# define ex_delfunction ex_ni
369# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000370# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100372static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100374static int makeopens(FILE *fd, char_u *dirnow);
375static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
376static void ex_loadview(exarg_T *eap);
377static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000378static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379#else
380# define ex_loadview ex_ni
381#endif
382#ifndef FEAT_EVAL
383# define ex_compiler ex_ni
384#endif
385#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100386static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387#else
388# define ex_viminfo ex_ni
389#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100390static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100391static void ex_filetype(exarg_T *eap);
392static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000394# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395# define ex_diffpatch ex_ni
396# define ex_diffgetput ex_ni
397# define ex_diffsplit ex_ni
398# define ex_diffthis ex_ni
399# define ex_diffupdate ex_ni
400#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100401static void ex_digraphs(exarg_T *eap);
402static void ex_set(exarg_T *eap);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100403#if !defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404# define ex_options ex_ni
405#endif
406#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100407static void ex_nohlsearch(exarg_T *eap);
408static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409#else
410# define ex_nohlsearch ex_ni
411# define ex_match ex_ni
412#endif
413#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100414static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415#else
416# define ex_X ex_ni
417#endif
418#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100419static void ex_fold(exarg_T *eap);
420static void ex_foldopen(exarg_T *eap);
421static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422#else
423# define ex_fold ex_ni
424# define ex_foldopen ex_ni
425# define ex_folddo ex_ni
426#endif
427#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
428 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
429# define ex_language ex_ni
430#endif
431#ifndef FEAT_SIGNS
432# define ex_sign ex_ni
433#endif
434#ifndef FEAT_SUN_WORKSHOP
435# define ex_wsverb ex_ni
436#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000437#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200438# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000439# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200440# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442
443#ifndef FEAT_EVAL
444# define ex_debug ex_ni
445# define ex_breakadd ex_ni
446# define ex_debuggreedy ex_ni
447# define ex_breakdel ex_ni
448# define ex_breaklist ex_ni
449#endif
450
451#ifndef FEAT_CMDHIST
452# define ex_history ex_ni
453#endif
454#ifndef FEAT_JUMPLIST
455# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200456# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457# define ex_changes ex_ni
458#endif
459
Bram Moolenaar05159a02005-02-26 23:04:13 +0000460#ifndef FEAT_PROFILE
461# define ex_profile ex_ni
462#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200463#ifndef FEAT_TERMINAL
464# define ex_terminal ex_ni
465#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000466
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467/*
468 * Declare cmdnames[].
469 */
470#define DO_DECLARE_EXCMD
471#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200472#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100473
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474static char_u dollar_command[2] = {'$', 0};
475
476
477#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000478/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479typedef struct
480{
481 char_u *line; /* command line */
482 linenr_T lnum; /* sourcing_lnum of the line */
483} wcmd_T;
484
485/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000486 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000488 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000490struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491{
492 garray_T *lines_gap; /* growarray with line info */
493 int current_line; /* last read line from growarray */
494 int repeating; /* TRUE when looping a second time */
495 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100496 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 void *cookie;
498};
499
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100500static char_u *get_loop_line(int c, void *cookie, int indent);
501static int store_loop_line(garray_T *gap, char_u *line);
502static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000503
504/* Struct to save a few things while debugging. Used in do_cmdline() only. */
505struct dbg_stuff
506{
507 int trylevel;
508 int force_abort;
509 except_T *caught_stack;
510 char_u *vv_exception;
511 char_u *vv_throwpoint;
512 int did_emsg;
513 int got_int;
514 int did_throw;
515 int need_rethrow;
516 int check_cstack;
517 except_T *current_exception;
518};
519
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100520static void save_dbg_stuff(struct dbg_stuff *dsp);
521static void restore_dbg_stuff(struct dbg_stuff *dsp);
Bram Moolenaared203462004-06-16 11:19:22 +0000522
523 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100524save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000525{
526 dsp->trylevel = trylevel; trylevel = 0;
527 dsp->force_abort = force_abort; force_abort = FALSE;
528 dsp->caught_stack = caught_stack; caught_stack = NULL;
529 dsp->vv_exception = v_exception(NULL);
530 dsp->vv_throwpoint = v_throwpoint(NULL);
531
532 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
533 dsp->did_emsg = did_emsg; did_emsg = FALSE;
534 dsp->got_int = got_int; got_int = FALSE;
535 dsp->did_throw = did_throw; did_throw = FALSE;
536 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
537 dsp->check_cstack = check_cstack; check_cstack = FALSE;
538 dsp->current_exception = current_exception; current_exception = NULL;
539}
540
541 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100542restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000543{
544 suppress_errthrow = FALSE;
545 trylevel = dsp->trylevel;
546 force_abort = dsp->force_abort;
547 caught_stack = dsp->caught_stack;
548 (void)v_exception(dsp->vv_exception);
549 (void)v_throwpoint(dsp->vv_throwpoint);
550 did_emsg = dsp->did_emsg;
551 got_int = dsp->got_int;
552 did_throw = dsp->did_throw;
553 need_rethrow = dsp->need_rethrow;
554 check_cstack = dsp->check_cstack;
555 current_exception = dsp->current_exception;
556}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557#endif
558
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559/*
560 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
561 * command is given.
562 */
563 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100564do_exmode(
565 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566{
567 int save_msg_scroll;
568 int prev_msg_row;
569 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100570 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000571
572 if (improved)
573 exmode_active = EXMODE_VIM;
574 else
575 exmode_active = EXMODE_NORMAL;
576 State = NORMAL;
577
578 /* When using ":global /pat/ visual" and then "Q" we return to continue
579 * the :global command. */
580 if (global_busy)
581 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582
583 save_msg_scroll = msg_scroll;
584 ++RedrawingDisabled; /* don't redisplay the window */
585 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586#ifdef FEAT_GUI
587 /* Ignore scrollbar and mouse events in Ex mode */
588 ++hold_gui_events;
589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590
591 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
592 while (exmode_active)
593 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000594 /* Check for a ":normal" command and no more characters left. */
595 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
596 {
597 exmode_active = FALSE;
598 break;
599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 msg_scroll = TRUE;
601 need_wait_return = FALSE;
602 ex_pressedreturn = FALSE;
603 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100604 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 prev_msg_row = msg_row;
606 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 if (improved)
608 {
609 cmdline_row = msg_row;
610 do_cmdline(NULL, getexline, NULL, 0);
611 }
612 else
613 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
614 lines_left = Rows - 1;
615
Bram Moolenaardf177f62005-02-22 08:39:57 +0000616 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100617 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000619 if (curbuf->b_ml.ml_flags & ML_EMPTY)
620 EMSG(_(e_emptybuf));
621 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000623 if (ex_pressedreturn)
624 {
625 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000626 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000627 msg_row = prev_msg_row;
628 if (prev_msg_row == Rows - 1)
629 msg_row--;
630 }
631 msg_col = 0;
632 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
633 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000636 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
637 {
638 if (curbuf->b_ml.ml_flags & ML_EMPTY)
639 EMSG(_(e_emptybuf));
640 else
641 EMSG(_("E501: At end-of-file"));
642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 }
644
645#ifdef FEAT_GUI
646 --hold_gui_events;
647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 --RedrawingDisabled;
649 --no_wait_return;
650 update_screen(CLEAR);
651 need_wait_return = FALSE;
652 msg_scroll = save_msg_scroll;
653}
654
655/*
656 * Execute a simple command line. Used for translated commands like "*".
657 */
658 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100659do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660{
661 return do_cmdline(cmd, NULL, NULL,
662 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
663}
664
665/*
666 * do_cmdline(): execute one Ex command line
667 *
668 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100669 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 * 2. Split up in parts separated with '|'.
671 *
672 * This function can be called recursively!
673 *
674 * flags:
675 * DOCMD_VERBOSE - The command will be included in the error message.
676 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100677 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 * DOCMD_KEYTYPED - Don't reset KeyTyped.
679 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
680 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
681 *
682 * return FAIL if cmdline could not be executed, OK otherwise
683 */
684 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100685do_cmdline(
686 char_u *cmdline,
687 char_u *(*fgetline)(int, void *, int),
688 void *cookie, /* argument for fgetline() */
689 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690{
691 char_u *next_cmdline; /* next cmd to execute */
692 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100693 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 static int recursive = 0; /* recursive depth */
695 int msg_didout_before_start = 0;
696 int count = 0; /* line number count */
697 int did_inc = FALSE; /* incremented RedrawingDisabled */
698 int retval = OK;
699#ifdef FEAT_EVAL
700 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000701 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 int current_line = 0; /* active line in lines_ga */
703 char_u *fname = NULL; /* function or script name */
704 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
705 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000706 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 int initial_trylevel;
708 struct msglist **saved_msg_list = NULL;
709 struct msglist *private_msg_list;
710
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100711 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100712 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000714 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000716 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100718# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719# define cmd_cookie cookie
720#endif
721 static int call_depth = 0; /* recursiveness */
722
723#ifdef FEAT_EVAL
724 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
725 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000726 * This ensures that the do_errthrow() call in do_one_cmd() does not
727 * combine the messages stored by an earlier invocation of do_one_cmd()
728 * with the command name of the later one. This would happen when
729 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 saved_msg_list = msg_list;
731 msg_list = &private_msg_list;
732 private_msg_list = NULL;
733#endif
734
735 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100736 * here. The value of 200 allows nested function calls, ":source", etc.
737 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100738 if (call_depth >= 200
739#ifdef FEAT_EVAL
740 && call_depth >= p_mfd
741#endif
742 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 {
744 EMSG(_("E169: Command too recursive"));
745#ifdef FEAT_EVAL
746 /* When converting to an exception, we do not include the command name
747 * since this is not an error of the specific command. */
748 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
749 msg_list = saved_msg_list;
750#endif
751 return FAIL;
752 }
753 ++call_depth;
754
755#ifdef FEAT_EVAL
756 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000757 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 cstack.cs_trylevel = 0;
759 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000760 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
762
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100763 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764
765 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100766 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000767 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 ++ex_nesting_level;
769
770 /* Get the function or script name and the address where the next breakpoint
771 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000772 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 {
774 fname = func_name(real_cookie);
775 breakpoint = func_breakpoint(real_cookie);
776 dbg_tick = func_dbg_tick(real_cookie);
777 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100778 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 {
780 fname = sourcing_name;
781 breakpoint = source_breakpoint(real_cookie);
782 dbg_tick = source_dbg_tick(real_cookie);
783 }
784
785 /*
786 * Initialize "force_abort" and "suppress_errthrow" at the top level.
787 */
788 if (!recursive)
789 {
790 force_abort = FALSE;
791 suppress_errthrow = FALSE;
792 }
793
794 /*
795 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000796 * exception handling (used when debugging). Otherwise clear it to avoid
797 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000799 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000800 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000801 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200802 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803
804 initial_trylevel = trylevel;
805
806 /*
807 * "did_throw" will be set to TRUE when an exception is being thrown.
808 */
809 did_throw = FALSE;
810#endif
811 /*
812 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000813 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 * If force_abort is set, we cancel everything.
815 */
816 did_emsg = FALSE;
817
818 /*
819 * KeyTyped is only set when calling vgetc(). Reset it here when not
820 * calling vgetc() (sourced command lines).
821 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100822 if (!(flags & DOCMD_KEYTYPED)
823 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 KeyTyped = FALSE;
825
826 /*
827 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000828 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 * - for multiple commands on one line, separated with '|'
830 * - when repeating until there are no more lines (for ":source")
831 */
832 next_cmdline = cmdline;
833 do
834 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000835#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100836 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000837#endif
838
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000839 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 if (next_cmdline == NULL
841#ifdef FEAT_EVAL
842 && !force_abort
843 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000844 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845#endif
846 )
847 did_emsg = FALSE;
848
849 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000850 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100851 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 * 3. If a line is given: Make a copy, so we can mess with it.
853 */
854
855#ifdef FEAT_EVAL
856 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000857 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 {
859 /* Each '|' separated command is stored separately in lines_ga, to
860 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100861 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862
863 /* Check if a function has returned or, unless it has an unclosed
864 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000865 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000867# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000868 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000869 func_line_end(real_cookie);
870# endif
871 if (func_has_ended(real_cookie))
872 {
873 retval = FAIL;
874 break;
875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000877#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000878 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100879 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000880 script_line_end();
881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882
883 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100884 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 {
886 retval = FAIL;
887 break;
888 }
889
890 /* If breakpoints have been added/deleted need to check for it. */
891 if (breakpoint != NULL && dbg_tick != NULL
892 && *dbg_tick != debug_tick)
893 {
894 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100895 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 fname, sourcing_lnum);
897 *dbg_tick = debug_tick;
898 }
899
900 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
901 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
902
903 /* Did we encounter a breakpoint? */
904 if (breakpoint != NULL && *breakpoint != 0
905 && *breakpoint <= sourcing_lnum)
906 {
907 dbg_breakpoint(fname, sourcing_lnum);
908 /* Find next breakpoint. */
909 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100910 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 fname, sourcing_lnum);
912 *dbg_tick = debug_tick;
913 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000914# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000915 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000916 {
917 if (getline_is_func)
918 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100919 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000920 script_line_start();
921 }
922# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 }
924
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000925 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000927 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100928 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 * below, so that it stores lines in or reads them from
930 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000931 * while/for loop. */
932 cmd_getline = get_loop_line;
933 cmd_cookie = (void *)&cmd_loop_cookie;
934 cmd_loop_cookie.lines_gap = &lines_ga;
935 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100936 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000937 cmd_loop_cookie.cookie = cookie;
938 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 }
940 else
941 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100942 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 cmd_cookie = cookie;
944 }
945#endif
946
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100947 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 if (next_cmdline == NULL)
949 {
950 /*
951 * Need to set msg_didout for the first line after an ":if",
952 * otherwise the ":if" will be overwritten.
953 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100954 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100956 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957#ifdef FEAT_EVAL
958 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
959#else
960 0
961#endif
962 )) == NULL)
963 {
964 /* Don't call wait_return for aborted command line. The NULL
965 * returned for the end of a sourced file or executed function
966 * doesn't do this. */
967 if (KeyTyped && !(flags & DOCMD_REPEAT))
968 need_wait_return = FALSE;
969 retval = FAIL;
970 break;
971 }
972 used_getline = TRUE;
973
974 /*
975 * Keep the first typed line. Clear it when more lines are typed.
976 */
977 if (flags & DOCMD_KEEPLINE)
978 {
979 vim_free(repeat_cmdline);
980 if (count == 0)
981 repeat_cmdline = vim_strsave(next_cmdline);
982 else
983 repeat_cmdline = NULL;
984 }
985 }
986
987 /* 3. Make a copy of the command so we can mess with it. */
988 else if (cmdline_copy == NULL)
989 {
990 next_cmdline = vim_strsave(next_cmdline);
991 if (next_cmdline == NULL)
992 {
993 EMSG(_(e_outofmem));
994 retval = FAIL;
995 break;
996 }
997 }
998 cmdline_copy = next_cmdline;
999
1000#ifdef FEAT_EVAL
1001 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001002 * Save the current line when inside a ":while" or ":for", and when
1003 * the command looks like a ":while" or ":for", because we may need it
1004 * later. When there is a '|' and another command, it is stored
1005 * separately, because we need to be able to jump back to it from an
1006 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001008 if (current_line == lines_ga.ga_len
1009 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001011 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 {
1013 retval = FAIL;
1014 break;
1015 }
1016 }
1017 did_endif = FALSE;
1018#endif
1019
1020 if (count++ == 0)
1021 {
1022 /*
1023 * All output from the commands is put below each other, without
1024 * waiting for a return. Don't do this when executing commands
1025 * from a script or when being called recursive (e.g. for ":e
1026 * +command file").
1027 */
1028 if (!(flags & DOCMD_NOWAIT) && !recursive)
1029 {
1030 msg_didout_before_start = msg_didout;
1031 msg_didany = FALSE; /* no output yet */
1032 msg_start();
1033 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001034 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 ++RedrawingDisabled;
1036 did_inc = TRUE;
1037 }
1038 }
1039
1040 if (p_verbose >= 15 && sourcing_name != NULL)
1041 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001043 verbose_enter_scroll();
1044
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 smsg((char_u *)_("line %ld: %s"),
1046 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001047 if (msg_silent == 0)
1048 msg_puts((char_u *)"\n"); /* don't overwrite this */
1049
1050 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 --no_wait_return;
1052 }
1053
1054 /*
1055 * 2. Execute one '|' separated command.
1056 * do_one_cmd() will return NULL if there is no trailing '|'.
1057 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1058 */
1059 ++recursive;
1060 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1061#ifdef FEAT_EVAL
1062 &cstack,
1063#endif
1064 cmd_getline, cmd_cookie);
1065 --recursive;
1066
1067#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001068 if (cmd_cookie == (void *)&cmd_loop_cookie)
1069 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001071 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072#endif
1073
1074 if (next_cmdline == NULL)
1075 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001076 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077#ifdef FEAT_CMDHIST
1078 /*
1079 * If the command was typed, remember it for the ':' register.
1080 * Do this AFTER executing the command to make :@: work.
1081 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001082 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 && new_last_cmdline != NULL)
1084 {
1085 vim_free(last_cmdline);
1086 last_cmdline = new_last_cmdline;
1087 new_last_cmdline = NULL;
1088 }
1089#endif
1090 }
1091 else
1092 {
1093 /* need to copy the command after the '|' to cmdline_copy, for the
1094 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001095 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 next_cmdline = cmdline_copy;
1097 }
1098
1099
1100#ifdef FEAT_EVAL
1101 /* reset did_emsg for a function that is not aborted by an error */
1102 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001103 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 && !func_has_abort(real_cookie))
1105 did_emsg = FALSE;
1106
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001107 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 {
1109 ++current_line;
1110
1111 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001112 * An ":endwhile", ":endfor" and ":continue" is handled here.
1113 * If we were executing commands, jump back to the ":while" or
1114 * ":for".
1115 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001117 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001119 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001121 /* Jump back to the matching ":while" or ":for". Be careful
1122 * not to use a cs_line[] from an entry that isn't a ":while"
1123 * or ":for": It would make "current_line" invalid and can
1124 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 if (!did_emsg && !got_int && !did_throw
1126 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001127 && (cstack.cs_flags[cstack.cs_idx]
1128 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 && cstack.cs_line[cstack.cs_idx] >= 0
1130 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1131 {
1132 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001133 /* remember we jumped there */
1134 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 line_breakcheck(); /* check if CTRL-C typed */
1136
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001137 /* Check for the next breakpoint at or after the ":while"
1138 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 if (breakpoint != NULL)
1140 {
1141 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001142 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 fname,
1144 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1145 *dbg_tick = debug_tick;
1146 }
1147 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001148 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001150 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001152 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1153 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 }
1155 }
1156
1157 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001158 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001160 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001162 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1164 }
1165 }
1166
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001167 /* Check for the next breakpoint after a watchexpression */
1168 if (breakpoint != NULL && has_watchexpr())
1169 {
1170 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1171 *dbg_tick = debug_tick;
1172 }
1173
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 /*
1175 * When not inside any ":while" loop, clear remembered lines.
1176 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001177 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 {
1179 if (lines_ga.ga_len > 0)
1180 {
1181 sourcing_lnum =
1182 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1183 free_cmdlines(&lines_ga);
1184 }
1185 current_line = 0;
1186 }
1187
1188 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001189 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1190 * being restored at the ":endtry". Reset them here and set the
1191 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1192 * This includes the case where a missing ":endif", ":endwhile" or
1193 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001195 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001197 cstack.cs_lflags &= ~CSL_HAD_FINA;
1198 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1199 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 did_throw ? (void *)current_exception : NULL);
1201 did_emsg = got_int = did_throw = FALSE;
1202 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1203 }
1204
1205 /* Update global "trylevel" for recursive calls to do_cmdline() from
1206 * within this loop. */
1207 trylevel = initial_trylevel + cstack.cs_trylevel;
1208
1209 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001210 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 * files) is aborted because of an error, an interrupt, or an uncaught
1212 * exception, cancel everything. If it is left normally, reset
1213 * force_abort to get the non-EH compatible abortion behavior for
1214 * the rest of the script.
1215 */
1216 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1217 force_abort = FALSE;
1218
1219 /* Convert an interrupt to an exception if appropriate. */
1220 (void)do_intthrow(&cstack);
1221#endif /* FEAT_EVAL */
1222
1223 }
1224 /*
1225 * Continue executing command lines when:
1226 * - no CTRL-C typed, no aborting error, no exception thrown or try
1227 * conditionals need to be checked for executing finally clauses or
1228 * catching an interrupt exception
1229 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001230 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 * looping for ":source" command or function call.
1232 */
1233 while (!((got_int
1234#ifdef FEAT_EVAL
1235 || (did_emsg && force_abort) || did_throw
1236#endif
1237 )
1238#ifdef FEAT_EVAL
1239 && cstack.cs_trylevel == 0
1240#endif
1241 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001242 && !(did_emsg
1243#ifdef FEAT_EVAL
1244 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001245 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001246 * the :endtry to be missed. */
1247 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1248#endif
1249 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001250 && (getline_equal(fgetline, cookie, getexmodeline)
1251 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 && (next_cmdline != NULL
1253#ifdef FEAT_EVAL
1254 || cstack.cs_idx >= 0
1255#endif
1256 || (flags & DOCMD_REPEAT)));
1257
1258 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001259 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260#ifdef FEAT_EVAL
1261 free_cmdlines(&lines_ga);
1262 ga_clear(&lines_ga);
1263
1264 if (cstack.cs_idx >= 0)
1265 {
1266 /*
1267 * If a sourced file or executed function ran to its end, report the
1268 * unclosed conditional.
1269 */
1270 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001271 && ((getline_equal(fgetline, cookie, getsourceline)
1272 && !source_finished(fgetline, cookie))
1273 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 && !func_has_ended(real_cookie))))
1275 {
1276 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1277 EMSG(_(e_endtry));
1278 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1279 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001280 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1281 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 else
1283 EMSG(_(e_endif));
1284 }
1285
1286 /*
1287 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1288 * ":endtry" in a sourced file or executed function. If the try
1289 * conditional is in its finally clause, ignore anything pending.
1290 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001291 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 */
1293 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001294 {
1295 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1296
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001297 if (idx >= 0)
1298 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001299 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1300 &cstack.cs_looplevel);
1301 }
1302 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 trylevel = initial_trylevel;
1304 }
1305
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001306 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1307 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001309 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 ? (char_u *)"endfunction" : (char_u *)NULL);
1311
1312 if (trylevel == 0)
1313 {
1314 /*
1315 * When an exception is being thrown out of the outermost try
1316 * conditional, discard the uncaught exception, disable the conversion
1317 * of interrupts or errors to exceptions, and ensure that no more
1318 * commands are executed.
1319 */
1320 if (did_throw)
1321 {
1322 void *p = NULL;
1323 char_u *saved_sourcing_name;
1324 int saved_sourcing_lnum;
1325 struct msglist *messages = NULL, *next;
1326
1327 /*
1328 * If the uncaught exception is a user exception, report it as an
1329 * error. If it is an error exception, display the saved error
1330 * message now. For an interrupt exception, do nothing; the
1331 * interrupt message is given elsewhere.
1332 */
1333 switch (current_exception->type)
1334 {
1335 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001336 vim_snprintf((char *)IObuff, IOSIZE,
1337 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 current_exception->value);
1339 p = vim_strsave(IObuff);
1340 break;
1341 case ET_ERROR:
1342 messages = current_exception->messages;
1343 current_exception->messages = NULL;
1344 break;
1345 case ET_INTERRUPT:
1346 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 }
1348
1349 saved_sourcing_name = sourcing_name;
1350 saved_sourcing_lnum = sourcing_lnum;
1351 sourcing_name = current_exception->throw_name;
1352 sourcing_lnum = current_exception->throw_lnum;
1353 current_exception->throw_name = NULL;
1354
1355 discard_current_exception(); /* uses IObuff if 'verbose' */
1356 suppress_errthrow = TRUE;
1357 force_abort = TRUE;
1358
1359 if (messages != NULL)
1360 {
1361 do
1362 {
1363 next = messages->next;
1364 emsg(messages->msg);
1365 vim_free(messages->msg);
1366 vim_free(messages);
1367 messages = next;
1368 }
1369 while (messages != NULL);
1370 }
1371 else if (p != NULL)
1372 {
1373 emsg(p);
1374 vim_free(p);
1375 }
1376 vim_free(sourcing_name);
1377 sourcing_name = saved_sourcing_name;
1378 sourcing_lnum = saved_sourcing_lnum;
1379 }
1380
1381 /*
1382 * On an interrupt or an aborting error not converted to an exception,
1383 * disable the conversion of errors to exceptions. (Interrupts are not
1384 * converted any more, here.) This enables also the interrupt message
1385 * when force_abort is set and did_emsg unset in case of an interrupt
1386 * from a finally clause after an error.
1387 */
1388 else if (got_int || (did_emsg && force_abort))
1389 suppress_errthrow = TRUE;
1390 }
1391
1392 /*
1393 * The current cstack will be freed when do_cmdline() returns. An uncaught
1394 * exception will have to be rethrown in the previous cstack. If a function
1395 * has just returned or a script file was just finished and the previous
1396 * cstack belongs to the same function or, respectively, script file, it
1397 * will have to be checked for finally clauses to be executed due to the
1398 * ":return" or ":finish". This is done in do_one_cmd().
1399 */
1400 if (did_throw)
1401 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001402 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001404 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 && ex_nesting_level > func_level(real_cookie) + 1))
1406 {
1407 if (!did_throw)
1408 check_cstack = TRUE;
1409 }
1410 else
1411 {
1412 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001413 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 --ex_nesting_level;
1415 /*
1416 * Go to debug mode when returning from a function in which we are
1417 * single-stepping.
1418 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001419 if ((getline_equal(fgetline, cookie, getsourceline)
1420 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001422 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 ? (char_u *)_("End of sourced file")
1424 : (char_u *)_("End of function"));
1425 }
1426
1427 /*
1428 * Restore the exception environment (done after returning from the
1429 * debugger).
1430 */
1431 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001432 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433
1434 msg_list = saved_msg_list;
1435#endif /* FEAT_EVAL */
1436
1437 /*
1438 * If there was too much output to fit on the command line, ask the user to
1439 * hit return before redrawing the screen. With the ":global" command we do
1440 * this only once after the command is finished.
1441 */
1442 if (did_inc)
1443 {
1444 --RedrawingDisabled;
1445 --no_wait_return;
1446 msg_scroll = FALSE;
1447
1448 /*
1449 * When just finished an ":if"-":else" which was typed, no need to
1450 * wait for hit-return. Also for an error situation.
1451 */
1452 if (retval == FAIL
1453#ifdef FEAT_EVAL
1454 || (did_endif && KeyTyped && !did_emsg)
1455#endif
1456 )
1457 {
1458 need_wait_return = FALSE;
1459 msg_didany = FALSE; /* don't wait when restarting edit */
1460 }
1461 else if (need_wait_return)
1462 {
1463 /*
1464 * The msg_start() above clears msg_didout. The wait_return we do
1465 * here should not overwrite the command that may be shown before
1466 * doing that.
1467 */
1468 msg_didout |= msg_didout_before_start;
1469 wait_return(FALSE);
1470 }
1471 }
1472
Bram Moolenaar2a942252012-11-28 23:03:07 +01001473#ifdef FEAT_EVAL
1474 did_endif = FALSE; /* in case do_cmdline used recursively */
1475#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 /*
1477 * Reset if_level, in case a sourced script file contains more ":if" than
1478 * ":endif" (could be ":if x | foo | endif").
1479 */
1480 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001481#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001482
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 --call_depth;
1484 return retval;
1485}
1486
1487#ifdef FEAT_EVAL
1488/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001489 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 */
1491 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001492get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001494 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 wcmd_T *wp;
1496 char_u *line;
1497
1498 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1499 {
1500 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001501 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001503 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 if (cp->getline == NULL)
1505 line = getcmdline(c, 0L, indent);
1506 else
1507 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001508 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 ++cp->current_line;
1510
1511 return line;
1512 }
1513
1514 KeyTyped = FALSE;
1515 ++cp->current_line;
1516 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1517 sourcing_lnum = wp->lnum;
1518 return vim_strsave(wp->line);
1519}
1520
1521/*
1522 * Store a line in "gap" so that a ":while" loop can execute it again.
1523 */
1524 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001525store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526{
1527 if (ga_grow(gap, 1) == FAIL)
1528 return FAIL;
1529 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1530 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1531 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 return OK;
1533}
1534
1535/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001536 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 */
1538 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001539free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540{
1541 while (gap->ga_len > 0)
1542 {
1543 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1544 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 }
1546}
1547#endif
1548
1549/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001550 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1551 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001554getline_equal(
1555 char_u *(*fgetline)(int, void *, int),
1556 void *cookie UNUSED, /* argument for fgetline() */
1557 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558{
1559#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001560 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001561 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562
Bram Moolenaar89d40322006-08-29 15:30:07 +00001563 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001564 * function that's originally used to obtain the lines. This may be
1565 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001566 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001567 cp = (struct loop_cookie *)cookie;
1568 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 {
1570 gp = cp->getline;
1571 cp = cp->cookie;
1572 }
1573 return gp == func;
1574#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001575 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576#endif
1577}
1578
1579#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1580/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001581 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 * getline function. Otherwise return "cookie".
1583 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001585getline_cookie(
1586 char_u *(*fgetline)(int, void *, int) UNUSED,
1587 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588{
1589# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001590 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001591 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592
Bram Moolenaar89d40322006-08-29 15:30:07 +00001593 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001594 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001596 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001597 cp = (struct loop_cookie *)cookie;
1598 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 {
1600 gp = cp->getline;
1601 cp = cp->cookie;
1602 }
1603 return cp;
1604# else
1605 return cookie;
1606# endif
1607}
1608#endif
1609
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001610
1611/*
1612 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1613 * ":bwipeout", etc.
1614 * Returns the buffer number.
1615 */
1616 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001617compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001618{
1619 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001620 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001621 int count = offset;
1622
1623 buf = firstbuf;
1624 while (buf->b_next != NULL && buf->b_fnum < lnum)
1625 buf = buf->b_next;
1626 while (count != 0)
1627 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001628 count += (offset < 0) ? 1 : -1;
1629 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1630 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001631 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001632 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001633 if (addr_type == ADDR_LOADED_BUFFERS)
1634 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001635 while (buf->b_ml.ml_mfp == NULL)
1636 {
1637 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1638 if (nextbuf == NULL)
1639 break;
1640 buf = nextbuf;
1641 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001642 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001643 /* we might have gone too far, last buffer is not loadedd */
1644 if (addr_type == ADDR_LOADED_BUFFERS)
1645 while (buf->b_ml.ml_mfp == NULL)
1646 {
1647 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1648 if (nextbuf == NULL)
1649 break;
1650 buf = nextbuf;
1651 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001652 return buf->b_fnum;
1653}
1654
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001655static int current_win_nr(win_T *win);
1656static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001657
1658 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001659current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001660{
1661 win_T *wp;
1662 int nr = 0;
1663
Bram Moolenaar29323592016-07-24 22:04:11 +02001664 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001665 {
1666 ++nr;
1667 if (wp == win)
1668 break;
1669 }
1670 return nr;
1671}
1672
1673 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001674current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001675{
1676 tabpage_T *tp;
1677 int nr = 0;
1678
Bram Moolenaar29323592016-07-24 22:04:11 +02001679 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001680 {
1681 ++nr;
1682 if (tp == tab)
1683 break;
1684 }
1685 return nr;
1686}
1687
1688# define CURRENT_WIN_NR current_win_nr(curwin)
1689# define LAST_WIN_NR current_win_nr(NULL)
1690# define CURRENT_TAB_NR current_tab_nr(curtab)
1691# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001692
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693/*
1694 * Execute one Ex command.
1695 *
1696 * If 'sourcing' is TRUE, the command will be included in the error message.
1697 *
1698 * 1. skip comment lines and leading space
1699 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001700 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001701 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001702 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001703 * 6. parse arguments
1704 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001706 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 *
1708 * This function may be called recursively!
1709 */
1710#if (_MSC_VER == 1200)
1711/*
Bram Moolenaared203462004-06-16 11:19:22 +00001712 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001714 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715#endif
1716 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001717do_one_cmd(
1718 char_u **cmdlinep,
1719 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001721 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001723 char_u *(*fgetline)(int, void *, int),
1724 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725{
1726 char_u *p;
1727 linenr_T lnum;
1728 long n;
1729 char_u *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001730 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 exarg_T ea; /* Ex command arguments */
1732 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001733 int save_msg_scroll = msg_scroll;
1734 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001736#ifdef HAVE_SANDBOX
1737 int did_sandbox = FALSE;
1738#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 cmdmod_T save_cmdmod;
1740 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001741 char_u *cmd;
Bram Moolenaarded27822017-01-02 14:27:34 +01001742 int address_count = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743
1744 vim_memset(&ea, 0, sizeof(ea));
1745 ea.line1 = 1;
1746 ea.line2 = 1;
1747#ifdef FEAT_EVAL
1748 ++ex_nesting_level;
1749#endif
1750
Bram Moolenaar21691f82012-12-05 19:13:18 +01001751 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 if (quitmore
1753#ifdef FEAT_EVAL
1754 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001755 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001756#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001757 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001758 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 --quitmore;
1760
1761 /*
1762 * Reset browse, confirm, etc.. They are restored when returning, for
1763 * recursive calls.
1764 */
1765 save_cmdmod = cmdmod;
1766 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1767
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001768 /* "#!anything" is handled like a comment. */
1769 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1770 goto doend;
1771
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 /*
1773 * Repeat until no more command modifiers are found.
1774 */
1775 ea.cmd = *cmdlinep;
1776 for (;;)
1777 {
1778/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001779 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 */
1781 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1782 ++ea.cmd;
1783
1784 /* in ex mode, an empty line works like :+ */
1785 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001786 && (getline_equal(fgetline, cookie, getexmodeline)
1787 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001788 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 {
1790 ea.cmd = (char_u *)"+";
1791 ex_pressedreturn = TRUE;
1792 }
1793
1794 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001795 if (*ea.cmd == '"')
1796 goto doend;
1797 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001798 {
1799 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802
1803/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001804 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 */
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001806 p = skip_range(ea.cmd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 switch (*p)
1808 {
1809 /* When adding an entry, also modify cmd_exists(). */
1810 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1811 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 cmdmod.split |= WSP_ABOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 continue;
1814
1815 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1816 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 cmdmod.split |= WSP_BELOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 continue;
1819 }
1820 if (checkforcmd(&ea.cmd, "browse", 3))
1821 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001822#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 cmdmod.browse = TRUE;
1824#endif
1825 continue;
1826 }
1827 if (!checkforcmd(&ea.cmd, "botright", 2))
1828 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 cmdmod.split |= WSP_BOT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 continue;
1831
1832 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1833 break;
1834#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1835 cmdmod.confirm = TRUE;
1836#endif
1837 continue;
1838
1839 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1840 {
1841 cmdmod.keepmarks = TRUE;
1842 continue;
1843 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001844 if (checkforcmd(&ea.cmd, "keepalt", 5))
1845 {
1846 cmdmod.keepalt = TRUE;
1847 continue;
1848 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001849 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1850 {
1851 cmdmod.keeppatterns = TRUE;
1852 continue;
1853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1855 break;
1856 cmdmod.keepjumps = TRUE;
1857 continue;
1858
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001859 case 'f': /* only accept ":filter {pat} cmd" */
1860 {
1861 char_u *reg_pat;
1862
1863 if (!checkforcmd(&p, "filter", 4)
1864 || *p == NUL || ends_excmd(*p))
1865 break;
Bram Moolenaard29459b2016-08-26 22:29:11 +02001866 if (*p == '!')
1867 {
1868 cmdmod.filter_force = TRUE;
1869 p = skipwhite(p + 1);
1870 if (*p == NUL || ends_excmd(*p))
1871 break;
1872 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001873 p = skip_vimgrep_pat(p, &reg_pat, NULL);
1874 if (p == NULL || *p == NUL)
1875 break;
1876 cmdmod.filter_regmatch.regprog =
1877 vim_regcomp(reg_pat, RE_MAGIC);
1878 if (cmdmod.filter_regmatch.regprog == NULL)
1879 break;
1880 ea.cmd = p;
1881 continue;
1882 }
1883
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 /* ":hide" and ":hide | cmd" are not modifiers */
1885 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1886 || *p == NUL || ends_excmd(*p))
1887 break;
1888 ea.cmd = p;
1889 cmdmod.hide = TRUE;
1890 continue;
1891
1892 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1893 {
1894 cmdmod.lockmarks = TRUE;
1895 continue;
1896 }
1897
1898 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1899 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 cmdmod.split |= WSP_ABOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 continue;
1902
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001903 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001904 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001905 if (cmdmod.save_ei == NULL)
1906 {
1907 /* Set 'eventignore' to "all". Restore the
1908 * existing option value later. */
1909 cmdmod.save_ei = vim_strsave(p_ei);
1910 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001911 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001912 }
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001913 continue;
1914 }
Bram Moolenaar3bcfca32016-07-30 19:39:29 +02001915 if (!checkforcmd(&ea.cmd, "noswapfile", 3))
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001916 break;
1917 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001918 continue;
1919
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1921 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 cmdmod.split |= WSP_BELOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 continue;
1924
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001925 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1926 {
1927#ifdef HAVE_SANDBOX
1928 if (!did_sandbox)
1929 ++sandbox;
1930 did_sandbox = TRUE;
1931#endif
1932 continue;
1933 }
1934 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001936 if (save_msg_silent == -1)
1937 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 ++msg_silent;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001939 if (*ea.cmd == '!' && !VIM_ISWHITE(ea.cmd[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 {
1941 /* ":silent!", but not "silent !cmd" */
1942 ea.cmd = skipwhite(ea.cmd + 1);
1943 ++emsg_silent;
1944 ++did_esilent;
1945 }
1946 continue;
1947
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001948 case 't': if (checkforcmd(&p, "tab", 3))
1949 {
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001950 long tabnr = get_address(&ea, &ea.cmd, ADDR_TABS,
Bram Moolenaarded27822017-01-02 14:27:34 +01001951 ea.skip, FALSE, 1);
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001952 if (tabnr == MAXLNUM)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001953 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001954 else
1955 {
1956 if (tabnr < 0 || tabnr > LAST_TAB_NR)
1957 {
1958 errormsg = (char_u *)_(e_invrange);
1959 goto doend;
1960 }
1961 cmdmod.tab = tabnr + 1;
1962 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001963 ea.cmd = p;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001964 continue;
1965 }
1966 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 cmdmod.split |= WSP_TOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 continue;
1970
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001971 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1972 break;
1973 if (save_msg_silent == -1)
1974 save_msg_silent = msg_silent;
1975 msg_silent = 0;
1976 continue;
1977
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1979 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 cmdmod.split |= WSP_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 continue;
1982 }
1983 if (!checkforcmd(&p, "verbose", 4))
1984 break;
1985 if (verbose_save < 0)
1986 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001987 if (vim_isdigit(*ea.cmd))
1988 p_verbose = atoi((char *)ea.cmd);
1989 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 p_verbose = 1;
1991 ea.cmd = p;
1992 continue;
1993 }
1994 break;
1995 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001996 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997
1998#ifdef FEAT_EVAL
1999 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2000 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2001#else
2002 ea.skip = (if_level > 0);
2003#endif
2004
2005#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002006# ifdef FEAT_PROFILE
2007 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002008 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002009 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002010 if (getline_equal(fgetline, cookie, get_func_line))
2011 func_line_exec(getline_cookie(fgetline, cookie));
2012 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002013 script_line_exec();
2014 }
2015#endif
2016
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 /* May go to debug mode. If this happens and the ">quit" debug command is
2018 * used, throw an interrupt exception and skip the next command. */
2019 dbg_check_breakpoint(&ea);
2020 if (!ea.skip && got_int)
2021 {
2022 ea.skip = TRUE;
2023 (void)do_intthrow(cstack);
2024 }
2025#endif
2026
2027/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002028 * 3. Skip over the range to find the command. Let "p" point to after it.
2029 *
2030 * We need the command to know what kind of range it uses.
2031 */
2032 cmd = ea.cmd;
2033 ea.cmd = skip_range(ea.cmd, NULL);
2034 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2035 ea.cmd = skipwhite(ea.cmd + 1);
2036 p = find_command(&ea, NULL);
2037
2038/*
2039 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 *
2041 * where 'addr' is:
2042 *
2043 * % (entire file)
2044 * $ [+-NUM]
2045 * 'x [+-NUM] (where x denotes a currently defined mark)
2046 * . [+-NUM]
2047 * [+-NUM]..
2048 * NUM
2049 *
2050 * The ea.cmd pointer is updated to point to the first character following the
2051 * range spec. If an initial address is found, but no second, the upper bound
2052 * is equal to the lower.
2053 */
2054
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002055 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002056 if (!IS_USER_CMDIDX(ea.cmdidx))
2057 {
2058 if (ea.cmdidx != CMD_SIZE)
2059 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2060 else
2061 ea.addr_type = ADDR_LINES;
2062
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002063 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002064 if (ea.cmdidx == CMD_wincmd && p != NULL)
2065 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002066 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002067
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002069 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 for (;;)
2071 {
2072 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002073 switch (ea.addr_type)
2074 {
2075 case ADDR_LINES:
2076 /* default is current line number */
2077 ea.line2 = curwin->w_cursor.lnum;
2078 break;
2079 case ADDR_WINDOWS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002080 ea.line2 = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002081 break;
2082 case ADDR_ARGUMENTS:
2083 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002084 if (ea.line2 > ARGCOUNT)
2085 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002086 break;
2087 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002088 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002089 ea.line2 = curbuf->b_fnum;
2090 break;
2091 case ADDR_TABS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002092 ea.line2 = CURRENT_TAB_NR;
2093 break;
2094 case ADDR_TABS_RELATIVE:
2095 ea.line2 = 1;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002096 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002097#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002098 case ADDR_QUICKFIX:
2099 ea.line2 = qf_get_cur_valid_idx(&ea);
2100 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002101#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002104 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip,
Bram Moolenaarded27822017-01-02 14:27:34 +01002105 ea.addr_count == 0, address_count++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 if (ea.cmd == NULL) /* error detected */
2107 goto doend;
2108 if (lnum == MAXLNUM)
2109 {
2110 if (*ea.cmd == '%') /* '%' - all lines */
2111 {
2112 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002113 switch (ea.addr_type)
2114 {
2115 case ADDR_LINES:
2116 ea.line1 = 1;
2117 ea.line2 = curbuf->b_ml.ml_line_count;
2118 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002119 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002120 {
2121 buf_T *buf = firstbuf;
2122
2123 while (buf->b_next != NULL
2124 && buf->b_ml.ml_mfp == NULL)
2125 buf = buf->b_next;
2126 ea.line1 = buf->b_fnum;
2127 buf = lastbuf;
2128 while (buf->b_prev != NULL
2129 && buf->b_ml.ml_mfp == NULL)
2130 buf = buf->b_prev;
2131 ea.line2 = buf->b_fnum;
2132 break;
2133 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002134 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002135 ea.line1 = firstbuf->b_fnum;
2136 ea.line2 = lastbuf->b_fnum;
2137 break;
2138 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002139 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002140 if (IS_USER_CMDIDX(ea.cmdidx))
2141 {
2142 ea.line1 = 1;
2143 ea.line2 = ea.addr_type == ADDR_WINDOWS
2144 ? LAST_WIN_NR : LAST_TAB_NR;
2145 }
2146 else
2147 {
2148 /* there is no Vim command which uses '%' and
2149 * ADDR_WINDOWS or ADDR_TABS */
2150 errormsg = (char_u *)_(e_invrange);
2151 goto doend;
2152 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002153 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002154 case ADDR_TABS_RELATIVE:
2155 errormsg = (char_u *)_(e_invrange);
2156 goto doend;
2157 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002158 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002159 if (ARGCOUNT == 0)
2160 ea.line1 = ea.line2 = 0;
2161 else
2162 {
2163 ea.line1 = 1;
2164 ea.line2 = ARGCOUNT;
2165 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002166 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002167#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002168 case ADDR_QUICKFIX:
2169 ea.line1 = 1;
2170 ea.line2 = qf_get_size(&ea);
2171 if (ea.line2 == 0)
2172 ea.line2 = 1;
2173 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002174#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 ++ea.addr_count;
2177 }
2178 /* '*' - visual area */
2179 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2180 {
2181 pos_T *fp;
2182
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002183 if (ea.addr_type != ADDR_LINES)
2184 {
2185 errormsg = (char_u *)_(e_invrange);
2186 goto doend;
2187 }
2188
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 ++ea.cmd;
2190 if (!ea.skip)
2191 {
2192 fp = getmark('<', FALSE);
2193 if (check_mark(fp) == FAIL)
2194 goto doend;
2195 ea.line1 = fp->lnum;
2196 fp = getmark('>', FALSE);
2197 if (check_mark(fp) == FAIL)
2198 goto doend;
2199 ea.line2 = fp->lnum;
2200 ++ea.addr_count;
2201 }
2202 }
2203 }
2204 else
2205 ea.line2 = lnum;
2206 ea.addr_count++;
2207
2208 if (*ea.cmd == ';')
2209 {
2210 if (!ea.skip)
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002211 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 curwin->w_cursor.lnum = ea.line2;
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +01002213 /* don't leave the cursor on an illegal line or column */
2214 check_cursor();
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 }
2217 else if (*ea.cmd != ',')
2218 break;
2219 ++ea.cmd;
2220 }
2221
2222 /* One address given: set start and end lines */
2223 if (ea.addr_count == 1)
2224 {
2225 ea.line1 = ea.line2;
2226 /* ... but only implicit: really no address given */
2227 if (lnum == MAXLNUM)
2228 ea.addr_count = 0;
2229 }
2230
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002232 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 */
2234
2235 /*
2236 * Skip ':' and any white space
2237 */
2238 ea.cmd = skipwhite(ea.cmd);
2239 while (*ea.cmd == ':')
2240 ea.cmd = skipwhite(ea.cmd + 1);
2241
2242 /*
2243 * If we got a line, but no command, then go to the line.
2244 * If we find a '|' or '\n' we set ea.nextcmd.
2245 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002246 if (*ea.cmd == NUL || *ea.cmd == '"'
2247 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 {
2249 /*
2250 * strange vi behaviour:
2251 * ":3" jumps to line 3
2252 * ":3|..." prints line 3
2253 * ":|" prints current line
2254 */
2255 if (ea.skip) /* skip this if inside :if */
2256 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002257 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 {
2259 ea.cmdidx = CMD_print;
2260 ea.argt = RANGE+COUNT+TRLBAR;
2261 if ((errormsg = invalid_range(&ea)) == NULL)
2262 {
2263 correct_range(&ea);
2264 ex_print(&ea);
2265 }
2266 }
2267 else if (ea.addr_count != 0)
2268 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002269 if (ea.line2 > curbuf->b_ml.ml_line_count)
2270 {
2271 /* With '-' in 'cpoptions' a line number past the file is an
2272 * error, otherwise put it at the end of the file. */
2273 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2274 ea.line2 = -1;
2275 else
2276 ea.line2 = curbuf->b_ml.ml_line_count;
2277 }
2278
2279 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002280 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 else
2282 {
2283 if (ea.line2 == 0)
2284 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 else
2286 curwin->w_cursor.lnum = ea.line2;
2287 beginline(BL_SOL | BL_FIX);
2288 }
2289 }
2290 goto doend;
2291 }
2292
Bram Moolenaard5005162014-08-22 23:05:54 +02002293 /* If this looks like an undefined user command and there are CmdUndefined
2294 * autocommands defined, trigger the matching autocommands. */
2295 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2296 && ASCII_ISUPPER(*ea.cmd)
2297 && has_cmdundefined())
2298 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002299 int ret;
2300
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002301 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002302 while (ASCII_ISALNUM(*p))
2303 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002304 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002305 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2306 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002307 /* If the autocommands did something and didn't cause an error, try
2308 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002309 p = (ret
2310#ifdef FEAT_EVAL
2311 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02002312#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002313 ) ? find_command(&ea, NULL) : ea.cmd;
2314 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002315
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316#ifdef FEAT_USR_CMDS
2317 if (p == NULL)
2318 {
2319 if (!ea.skip)
2320 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2321 goto doend;
2322 }
2323 /* Check for wrong commands. */
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002324 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2325 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 {
2327 errormsg = uc_fun_cmd();
2328 goto doend;
2329 }
2330#endif
2331 if (ea.cmdidx == CMD_SIZE)
2332 {
2333 if (!ea.skip)
2334 {
2335 STRCPY(IObuff, _("E492: Not an editor command"));
2336 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002337 {
2338 /* If the modifier was parsed OK the error must be in the
2339 * following command */
2340 if (after_modifier != NULL)
2341 append_command(after_modifier);
2342 else
2343 append_command(*cmdlinep);
2344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002346 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 }
2348 goto doend;
2349 }
2350
Bram Moolenaar958636c2014-10-21 20:01:58 +02002351 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2352 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002353#ifdef HAVE_EX_SCRIPT_NI
2354 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2355#endif
2356 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357
2358#ifndef FEAT_EVAL
2359 /*
2360 * When the expression evaluation is disabled, recognize the ":if" and
2361 * ":endif" commands and ignore everything in between it.
2362 */
2363 if (ea.cmdidx == CMD_if)
2364 ++if_level;
2365 if (if_level)
2366 {
2367 if (ea.cmdidx == CMD_endif)
2368 --if_level;
2369 goto doend;
2370 }
2371
2372#endif
2373
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002374 /* forced commands */
2375 if (*p == '!' && ea.cmdidx != CMD_substitute
2376 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 {
2378 ++p;
2379 ea.forceit = TRUE;
2380 }
2381 else
2382 ea.forceit = FALSE;
2383
2384/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002385 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002387 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002388 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389
2390 if (!ea.skip)
2391 {
2392#ifdef HAVE_SANDBOX
2393 if (sandbox != 0 && !(ea.argt & SBOXOK))
2394 {
2395 /* Command not allowed in sandbox. */
2396 errormsg = (char_u *)_(e_sandbox);
2397 goto doend;
2398 }
2399#endif
2400 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2401 {
2402 /* Command not allowed in non-'modifiable' buffer */
2403 errormsg = (char_u *)_(e_modifiable);
2404 goto doend;
2405 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002406
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002407 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002408 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002410 /* Command not allowed when editing the command line. */
Bram Moolenaar75c19462017-02-12 18:34:05 +01002411 errormsg = (char_u *)_(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 goto doend;
2413 }
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002414 /* Disallow editing another buffer when "curbuf_lock" is set.
2415 * Do allow ":edit" (check for argument later).
2416 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002417 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002418 && ea.cmdidx != CMD_edit
2419 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002420 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002421 && curbuf_locked())
2422 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423
2424 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2425 {
2426 /* no range allowed */
2427 errormsg = (char_u *)_(e_norange);
2428 goto doend;
2429 }
2430 }
2431
2432 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2433 {
2434 errormsg = (char_u *)_(e_nobang);
2435 goto doend;
2436 }
2437
2438 /*
2439 * Don't complain about the range if it is not used
2440 * (could happen if line_count is accidentally set to 0).
2441 */
2442 if (!ea.skip && !ni)
2443 {
2444 /*
2445 * If the range is backwards, ask for confirmation and, if given, swap
2446 * ea.line1 & ea.line2 so it's forwards again.
2447 * When global command is busy, don't ask, will fail below.
2448 */
2449 if (!global_busy && ea.line1 > ea.line2)
2450 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002451 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002453 if (sourcing || exmode_active)
2454 {
2455 errormsg = (char_u *)_("E493: Backwards range given");
2456 goto doend;
2457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 if (ask_yesno((char_u *)
2459 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002460 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 }
2462 lnum = ea.line1;
2463 ea.line1 = ea.line2;
2464 ea.line2 = lnum;
2465 }
2466 if ((errormsg = invalid_range(&ea)) != NULL)
2467 goto doend;
2468 }
2469
2470 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2471 ea.line2 = 1;
2472
2473 correct_range(&ea);
2474
2475#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002476 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2477 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 {
2479 /* Put the first line at the start of a closed fold, put the last line
2480 * at the end of a closed fold. */
2481 (void)hasFolding(ea.line1, &ea.line1, NULL);
2482 (void)hasFolding(ea.line2, NULL, &ea.line2);
2483 }
2484#endif
2485
2486#ifdef FEAT_QUICKFIX
2487 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002488 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 * option here, so things like % get expanded.
2490 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002491 p = replace_makeprg(&ea, p, cmdlinep);
2492 if (p == NULL)
2493 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494#endif
2495
2496 /*
2497 * Skip to start of argument.
2498 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2499 */
2500 if (ea.cmdidx == CMD_bang)
2501 ea.arg = p;
2502 else
2503 ea.arg = skipwhite(p);
2504
2505 /*
2506 * Check for "++opt=val" argument.
2507 * Must be first, allow ":w ++enc=utf8 !cmd"
2508 */
2509 if (ea.argt & ARGOPT)
2510 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2511 if (getargopt(&ea) == FAIL && !ni)
2512 {
2513 errormsg = (char_u *)_(e_invarg);
2514 goto doend;
2515 }
2516
2517 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2518 {
2519 if (*ea.arg == '>') /* append */
2520 {
2521 if (*++ea.arg != '>') /* typed wrong */
2522 {
2523 errormsg = (char_u *)_("E494: Use w or w>>");
2524 goto doend;
2525 }
2526 ea.arg = skipwhite(ea.arg + 1);
2527 ea.append = TRUE;
2528 }
2529 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2530 {
2531 ++ea.arg;
2532 ea.usefilter = TRUE;
2533 }
2534 }
2535
2536 if (ea.cmdidx == CMD_read)
2537 {
2538 if (ea.forceit)
2539 {
2540 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2541 ea.forceit = FALSE;
2542 }
2543 else if (*ea.arg == '!') /* :r !filter */
2544 {
2545 ++ea.arg;
2546 ea.usefilter = TRUE;
2547 }
2548 }
2549
2550 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2551 {
2552 ea.amount = 1;
2553 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2554 {
2555 ++ea.arg;
2556 ++ea.amount;
2557 }
2558 ea.arg = skipwhite(ea.arg);
2559 }
2560
2561 /*
2562 * Check for "+command" argument, before checking for next command.
2563 * Don't do this for ":read !cmd" and ":write !cmd".
2564 */
2565 if ((ea.argt & EDITCMD) && !ea.usefilter)
2566 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2567
2568 /*
2569 * Check for '|' to separate commands and '"' to start comments.
2570 * Don't do this for ":read !cmd" and ":write !cmd".
2571 */
2572 if ((ea.argt & TRLBAR) && !ea.usefilter)
2573 separate_nextcmd(&ea);
2574
2575 /*
2576 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002577 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2578 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002580 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002581 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002582 || ea.cmdidx == CMD_global
2583 || ea.cmdidx == CMD_vglobal
2584 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 {
2586 for (p = ea.arg; *p; ++p)
2587 {
2588 /* Remove one backslash before a newline, so that it's possible to
2589 * pass a newline to the shell and also a newline that is preceded
2590 * with a backslash. This makes it impossible to end a shell
2591 * command in a backslash, but that doesn't appear useful.
2592 * Halving the number of backslashes is incompatible with previous
2593 * versions. */
2594 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002595 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 else if (*p == '\n')
2597 {
2598 ea.nextcmd = p + 1;
2599 *p = NUL;
2600 break;
2601 }
2602 }
2603 }
2604
2605 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2606 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002607 buf_T *buf;
2608
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002610 switch (ea.addr_type)
2611 {
2612 case ADDR_LINES:
2613 ea.line2 = curbuf->b_ml.ml_line_count;
2614 break;
2615 case ADDR_LOADED_BUFFERS:
2616 buf = firstbuf;
2617 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2618 buf = buf->b_next;
2619 ea.line1 = buf->b_fnum;
2620 buf = lastbuf;
2621 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2622 buf = buf->b_prev;
2623 ea.line2 = buf->b_fnum;
2624 break;
2625 case ADDR_BUFFERS:
2626 ea.line1 = firstbuf->b_fnum;
2627 ea.line2 = lastbuf->b_fnum;
2628 break;
2629 case ADDR_WINDOWS:
2630 ea.line2 = LAST_WIN_NR;
2631 break;
2632 case ADDR_TABS:
2633 ea.line2 = LAST_TAB_NR;
2634 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002635 case ADDR_TABS_RELATIVE:
2636 ea.line2 = 1;
2637 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002638 case ADDR_ARGUMENTS:
2639 if (ARGCOUNT == 0)
2640 ea.line1 = ea.line2 = 0;
2641 else
2642 ea.line2 = ARGCOUNT;
2643 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002644#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002645 case ADDR_QUICKFIX:
2646 ea.line2 = qf_get_size(&ea);
2647 if (ea.line2 == 0)
2648 ea.line2 = 1;
2649 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002650#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 }
2653
2654 /* accept numbered register only when no count allowed (:put) */
2655 if ( (ea.argt & REGSTR)
2656 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002657 /* Do not allow register = for user commands */
2658 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2660 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002661#ifndef FEAT_CLIPBOARD
2662 /* check these explicitly for a more specific error message */
2663 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002665 errormsg = (char_u *)_(e_invalidreg);
2666 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 }
2668#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002669 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2670 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002671 {
2672 ea.regname = *ea.arg++;
2673#ifdef FEAT_EVAL
2674 /* for '=' register: accept the rest of the line as an expression */
2675 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2676 {
2677 set_expr_line(vim_strsave(ea.arg));
2678 ea.arg += STRLEN(ea.arg);
2679 }
2680#endif
2681 ea.arg = skipwhite(ea.arg);
2682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 }
2684
2685 /*
2686 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2687 * count, it's a buffer name.
2688 */
2689 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2690 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002691 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 {
2693 n = getdigits(&ea.arg);
2694 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002695 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 {
2697 errormsg = (char_u *)_(e_zerocount);
2698 goto doend;
2699 }
2700 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2701 {
2702 ea.line2 = n;
2703 if (ea.addr_count == 0)
2704 ea.addr_count = 1;
2705 }
2706 else
2707 {
2708 ea.line1 = ea.line2;
2709 ea.line2 += n - 1;
2710 ++ea.addr_count;
2711 /*
2712 * Be vi compatible: no error message for out of range.
2713 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002714 if (ea.addr_type == ADDR_LINES
2715 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 ea.line2 = curbuf->b_ml.ml_line_count;
2717 }
2718 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002719
2720 /*
2721 * Check for flags: 'l', 'p' and '#'.
2722 */
2723 if (ea.argt & EXFLAGS)
2724 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002726 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002727 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 {
2729 errormsg = (char_u *)_(e_trailing);
2730 goto doend;
2731 }
2732
2733 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2734 {
2735 errormsg = (char_u *)_(e_argreq);
2736 goto doend;
2737 }
2738
2739#ifdef FEAT_EVAL
2740 /*
2741 * Skip the command when it's not going to be executed.
2742 * The commands like :if, :endif, etc. always need to be executed.
2743 * Also make an exception for commands that handle a trailing command
2744 * themselves.
2745 */
2746 if (ea.skip)
2747 {
2748 switch (ea.cmdidx)
2749 {
2750 /* commands that need evaluation */
2751 case CMD_while:
2752 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002753 case CMD_for:
2754 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 case CMD_if:
2756 case CMD_elseif:
2757 case CMD_else:
2758 case CMD_endif:
2759 case CMD_try:
2760 case CMD_catch:
2761 case CMD_finally:
2762 case CMD_endtry:
2763 case CMD_function:
2764 break;
2765
2766 /* Commands that handle '|' themselves. Check: A command should
2767 * either have the TRLBAR flag, appear in this list or appear in
2768 * the list at ":help :bar". */
2769 case CMD_aboveleft:
2770 case CMD_and:
2771 case CMD_belowright:
2772 case CMD_botright:
2773 case CMD_browse:
2774 case CMD_call:
2775 case CMD_confirm:
2776 case CMD_delfunction:
2777 case CMD_djump:
2778 case CMD_dlist:
2779 case CMD_dsearch:
2780 case CMD_dsplit:
2781 case CMD_echo:
2782 case CMD_echoerr:
2783 case CMD_echomsg:
2784 case CMD_echon:
2785 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002786 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 case CMD_help:
2788 case CMD_hide:
2789 case CMD_ijump:
2790 case CMD_ilist:
2791 case CMD_isearch:
2792 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002793 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 case CMD_keepjumps:
2795 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002796 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 case CMD_leftabove:
2798 case CMD_let:
2799 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002800 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002802 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002803 case CMD_noautocmd:
2804 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 case CMD_perl:
2806 case CMD_psearch:
2807 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002808 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002809 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 case CMD_return:
2811 case CMD_rightbelow:
2812 case CMD_ruby:
2813 case CMD_silent:
2814 case CMD_smagic:
2815 case CMD_snomagic:
2816 case CMD_substitute:
2817 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002818 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 case CMD_tcl:
2820 case CMD_throw:
2821 case CMD_tilde:
2822 case CMD_topleft:
2823 case CMD_unlet:
2824 case CMD_verbose:
2825 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002826 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 break;
2828
2829 default: goto doend;
2830 }
2831 }
2832#endif
2833
2834 if (ea.argt & XFILE)
2835 {
2836 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2837 goto doend;
2838 }
2839
2840#ifdef FEAT_LISTCMDS
2841 /*
2842 * Accept buffer name. Cannot be used at the same time with a buffer
2843 * number. Don't do this for a user command.
2844 */
2845 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002846 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 {
2848 /*
2849 * :bdelete, :bwipeout and :bunload take several arguments, separated
2850 * by spaces: find next space (skipping over escaped characters).
2851 * The others take one argument: ignore trailing spaces.
2852 */
2853 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2854 || ea.cmdidx == CMD_bunload)
2855 p = skiptowhite_esc(ea.arg);
2856 else
2857 {
2858 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002859 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 --p;
2861 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002862 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2863 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 if (ea.line2 < 0) /* failed */
2865 goto doend;
2866 ea.addr_count = 1;
2867 ea.arg = skipwhite(p);
2868 }
2869#endif
2870
Bram Moolenaar2be57332018-02-13 18:05:18 +01002871 /* The :try command saves the emsg_silent flag, reset it here when
2872 * ":silent! try" was used, it should only apply to :try itself. */
2873 if (ea.cmdidx == CMD_try && did_esilent > 0)
2874 {
2875 emsg_silent -= did_esilent;
2876 if (emsg_silent < 0)
2877 emsg_silent = 0;
2878 did_esilent = 0;
2879 }
2880
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002882 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 *
2884 * The "ea" structure holds the arguments that can be used.
2885 */
2886 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002887 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 ea.cookie = cookie;
2889#ifdef FEAT_EVAL
2890 ea.cstack = cstack;
2891#endif
2892
2893#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002894 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 {
2896 /*
2897 * Execute a user-defined command.
2898 */
2899 do_ucmd(&ea);
2900 }
2901 else
2902#endif
2903 {
2904 /*
2905 * Call the function to execute the command.
2906 */
2907 ea.errmsg = NULL;
2908 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2909 if (ea.errmsg != NULL)
2910 errormsg = (char_u *)_(ea.errmsg);
2911 }
2912
2913#ifdef FEAT_EVAL
2914 /*
2915 * If the command just executed called do_cmdline(), any throw or ":return"
2916 * or ":finish" encountered there must also check the cstack of the still
2917 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2918 * exception, or reanimate a returned function or finished script file and
2919 * return or finish it again.
2920 */
2921 if (need_rethrow)
2922 do_throw(cstack);
2923 else if (check_cstack)
2924 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002925 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002927 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 && current_func_returned())
2929 do_return(&ea, TRUE, FALSE, NULL);
2930 }
2931 need_rethrow = check_cstack = FALSE;
2932#endif
2933
2934doend:
2935 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002936 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002938 curwin->w_cursor.col = 0;
2939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940
2941 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2942 {
2943 if (sourcing)
2944 {
2945 if (errormsg != IObuff)
2946 {
2947 STRCPY(IObuff, errormsg);
2948 errormsg = IObuff;
2949 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002950 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 }
2952 emsg(errormsg);
2953 }
2954#ifdef FEAT_EVAL
2955 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002956 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2957 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958#endif
2959
2960 if (verbose_save >= 0)
2961 p_verbose = verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002962
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002963 if (cmdmod.save_ei != NULL)
2964 {
2965 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002966 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2967 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002968 free_string_option(cmdmod.save_ei);
2969 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002970
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002971 if (cmdmod.filter_regmatch.regprog != NULL)
2972 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973
2974 cmdmod = save_cmdmod;
2975
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002976 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 {
2978 /* messages could be enabled for a serious error, need to check if the
2979 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002980 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002981 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 emsg_silent -= did_esilent;
2983 if (emsg_silent < 0)
2984 emsg_silent = 0;
2985 /* Restore msg_scroll, it's set by file I/O commands, even when no
2986 * message is actually displayed. */
2987 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002988
2989 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2990 * somewhere in the line. Put it back in the first column. */
2991 if (redirecting())
2992 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 }
2994
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002995#ifdef HAVE_SANDBOX
2996 if (did_sandbox)
2997 --sandbox;
2998#endif
2999
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3001 ea.nextcmd = NULL;
3002
3003#ifdef FEAT_EVAL
3004 --ex_nesting_level;
3005#endif
3006
3007 return ea.nextcmd;
3008}
3009#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003010 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011#endif
3012
3013/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003014 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 * If there is a match advance "pp" to the argument and return TRUE.
3016 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003017 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003018checkforcmd(
3019 char_u **pp, /* start of command */
3020 char *cmd, /* name of command */
3021 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022{
3023 int i;
3024
3025 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003026 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 break;
3028 if (i >= len && !isalpha((*pp)[i]))
3029 {
3030 *pp = skipwhite(*pp + i);
3031 return TRUE;
3032 }
3033 return FALSE;
3034}
3035
3036/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003037 * Append "cmd" to the error message in IObuff.
3038 * Takes care of limiting the length and handling 0xa0, which would be
3039 * invisible otherwise.
3040 */
3041 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003042append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003043{
3044 char_u *s = cmd;
3045 char_u *d;
3046
3047 STRCAT(IObuff, ": ");
3048 d = IObuff + STRLEN(IObuff);
3049 while (*s != NUL && d - IObuff < IOSIZE - 7)
3050 {
3051 if (
3052#ifdef FEAT_MBYTE
3053 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3054#endif
3055 *s == 0xa0)
3056 {
3057 s +=
3058#ifdef FEAT_MBYTE
3059 enc_utf8 ? 2 :
3060#endif
3061 1;
3062 STRCPY(d, "<a0>");
3063 d += 4;
3064 }
3065 else
3066 MB_COPY_CHAR(s, d);
3067 }
3068 *d = NUL;
3069}
3070
3071/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003073 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003075 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 * Returns NULL for an ambiguous user command.
3077 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003079find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080{
3081 int len;
3082 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003083 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084
3085 /*
3086 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003087 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 * - the 'k' command can directly be followed by any character.
3089 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003090 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003092 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 */
3094 p = eap->cmd;
3095 if (*p == 'k')
3096 {
3097 eap->cmdidx = CMD_k;
3098 ++p;
3099 }
3100 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003101 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3102 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 || p[1] == 'g'
3104 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3105 || p[1] == 'I'
3106 || (p[1] == 'r' && p[2] != 'e')))
3107 {
3108 eap->cmdidx = CMD_substitute;
3109 ++p;
3110 }
3111 else
3112 {
3113 while (ASCII_ISALPHA(*p))
3114 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003115 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003116 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003117 while (ASCII_ISALNUM(*p))
3118 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003119
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 /* check for non-alpha command */
3121 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3122 ++p;
3123 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003124 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3125 {
3126 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3127 * :delete with the 'l' flag. Same for 'p'. */
3128 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003129 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003130 break;
3131 if (i == len - 1)
3132 {
3133 --len;
3134 if (p[-1] == 'l')
3135 eap->flags |= EXFLAG_LIST;
3136 else
3137 eap->flags |= EXFLAG_PRINT;
3138 }
3139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003141 if (ASCII_ISLOWER(eap->cmd[0]))
3142 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003143 int c1 = eap->cmd[0];
3144 int c2 = eap->cmd[1];
3145
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003146 if (command_count != (int)CMD_SIZE)
3147 {
3148 iemsg((char_u *)_("E943: Command table needs to be updated, run 'make cmdidxs'"));
3149 getout(1);
3150 }
3151
3152 /* Use a precomputed index for fast look-up in cmdnames[]
3153 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003154 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3155 if (ASCII_ISLOWER(c2))
3156 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003159 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160
3161 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3162 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3163 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3164 (size_t)len) == 0)
3165 {
3166#ifdef FEAT_EVAL
3167 if (full != NULL
3168 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3169 *full = TRUE;
3170#endif
3171 break;
3172 }
3173
3174#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003175 /* Look for a user defined command as a last resort. Let ":Print" be
3176 * overruled by a user defined command. */
3177 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3178 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003180 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 while (ASCII_ISALNUM(*p))
3182 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003183 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 }
3185#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003186 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 eap->cmdidx = CMD_SIZE;
3188 }
3189
3190 return p;
3191}
3192
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003193#ifdef FEAT_USR_CMDS
3194/*
3195 * Search for a user command that matches "eap->cmd".
3196 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3197 * Return a pointer to just after the command.
3198 * Return NULL if there is no matching command.
3199 */
3200 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003201find_ucmd(
3202 exarg_T *eap,
3203 char_u *p, /* end of the command (possibly including count) */
3204 int *full, /* set to TRUE for a full match */
3205 expand_T *xp, /* used for completion, NULL otherwise */
3206 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003207{
3208 int len = (int)(p - eap->cmd);
3209 int j, k, matchlen = 0;
3210 ucmd_T *uc;
3211 int found = FALSE;
3212 int possible = FALSE;
3213 char_u *cp, *np; /* Point into typed cmd and test name */
3214 garray_T *gap;
3215 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3216 only full match global is accepted. */
3217
3218 /*
3219 * Look for buffer-local user commands first, then global ones.
3220 */
3221 gap = &curbuf->b_ucmds;
3222 for (;;)
3223 {
3224 for (j = 0; j < gap->ga_len; ++j)
3225 {
3226 uc = USER_CMD_GA(gap, j);
3227 cp = eap->cmd;
3228 np = uc->uc_name;
3229 k = 0;
3230 while (k < len && *np != NUL && *cp++ == *np++)
3231 k++;
3232 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3233 {
3234 /* If finding a second match, the command is ambiguous. But
3235 * not if a buffer-local command wasn't a full match and a
3236 * global command is a full match. */
3237 if (k == len && found && *np != NUL)
3238 {
3239 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003240 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003241 amb_local = TRUE;
3242 }
3243
3244 if (!found || (k == len && *np == NUL))
3245 {
3246 /* If we matched up to a digit, then there could
3247 * be another command including the digit that we
3248 * should use instead.
3249 */
3250 if (k == len)
3251 found = TRUE;
3252 else
3253 possible = TRUE;
3254
3255 if (gap == &ucmds)
3256 eap->cmdidx = CMD_USER;
3257 else
3258 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003259 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003260 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003261 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003262
3263# ifdef FEAT_CMDL_COMPL
3264 if (compl != NULL)
3265 *compl = uc->uc_compl;
3266# ifdef FEAT_EVAL
3267 if (xp != NULL)
3268 {
3269 xp->xp_arg = uc->uc_compl_arg;
3270 xp->xp_scriptID = uc->uc_scriptID;
3271 }
3272# endif
3273# endif
3274 /* Do not search for further abbreviations
3275 * if this is an exact match. */
3276 matchlen = k;
3277 if (k == len && *np == NUL)
3278 {
3279 if (full != NULL)
3280 *full = TRUE;
3281 amb_local = FALSE;
3282 break;
3283 }
3284 }
3285 }
3286 }
3287
3288 /* Stop if we found a full match or searched all. */
3289 if (j < gap->ga_len || gap == &ucmds)
3290 break;
3291 gap = &ucmds;
3292 }
3293
3294 /* Only found ambiguous matches. */
3295 if (amb_local)
3296 {
3297 if (xp != NULL)
3298 xp->xp_context = EXPAND_UNSUCCESSFUL;
3299 return NULL;
3300 }
3301
3302 /* The match we found may be followed immediately by a number. Move "p"
3303 * back to point to it. */
3304 if (found || possible)
3305 return p + (matchlen - len);
3306 return p;
3307}
3308#endif
3309
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003311static struct cmdmod
3312{
3313 char *name;
3314 int minlen;
3315 int has_count; /* :123verbose :3tab */
3316} cmdmods[] = {
3317 {"aboveleft", 3, FALSE},
3318 {"belowright", 3, FALSE},
3319 {"botright", 2, FALSE},
3320 {"browse", 3, FALSE},
3321 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003322 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003323 {"hide", 3, FALSE},
3324 {"keepalt", 5, FALSE},
3325 {"keepjumps", 5, FALSE},
3326 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003327 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003328 {"leftabove", 5, FALSE},
3329 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003330 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003331 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003332 {"rightbelow", 6, FALSE},
3333 {"sandbox", 3, FALSE},
3334 {"silent", 3, FALSE},
3335 {"tab", 3, TRUE},
3336 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003337 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003338 {"verbose", 4, TRUE},
3339 {"vertical", 4, FALSE},
3340};
3341
3342/*
3343 * Return length of a command modifier (including optional count).
3344 * Return zero when it's not a modifier.
3345 */
3346 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003347modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003348{
3349 int i, j;
3350 char_u *p = cmd;
3351
3352 if (VIM_ISDIGIT(*cmd))
3353 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003354 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003355 {
3356 for (j = 0; p[j] != NUL; ++j)
3357 if (p[j] != cmdmods[i].name[j])
3358 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003359 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003360 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003361 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003362 }
3363 return 0;
3364}
3365
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366/*
3367 * Return > 0 if an Ex command "name" exists.
3368 * Return 2 if there is an exact match.
3369 * Return 3 if there is an ambiguous match.
3370 */
3371 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003372cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373{
3374 exarg_T ea;
3375 int full = FALSE;
3376 int i;
3377 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003378 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379
3380 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003381 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 {
3383 for (j = 0; name[j] != NUL; ++j)
3384 if (name[j] != cmdmods[i].name[j])
3385 break;
3386 if (name[j] == NUL && j >= cmdmods[i].minlen)
3387 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3388 }
3389
Bram Moolenaara9587612006-05-04 21:47:50 +00003390 /* Check built-in commands and user defined commands.
3391 * For ":2match" and ":3match" we need to skip the number. */
3392 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003394 p = find_command(&ea, &full);
3395 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003397 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3398 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003399 if (*skipwhite(p) != NUL)
3400 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3402}
3403#endif
3404
3405/*
3406 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3407 * we don't need/want deleted. Maybe this could be done better if we didn't
3408 * repeat all this stuff. The only problem is that they may not stay
3409 * perfectly compatible with each other, but then the command line syntax
3410 * probably won't change that much -- webb.
3411 */
3412 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003413set_one_cmd_context(
3414 expand_T *xp,
3415 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416{
3417 char_u *p;
3418 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003419 int len = 0;
3420 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3422 int compl = EXPAND_NOTHING;
3423#endif
3424#ifdef FEAT_CMDL_COMPL
3425 int delim;
3426#endif
3427 int forceit = FALSE;
3428 int usefilter = FALSE; /* filter instead of file name */
3429
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003430 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 xp->xp_pattern = buff;
3432 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003433 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434
3435/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003436 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 */
3438 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3439 ;
3440 xp->xp_pattern = cmd;
3441
3442 if (*cmd == NUL)
3443 return NULL;
3444 if (*cmd == '"') /* ignore comment lines */
3445 {
3446 xp->xp_context = EXPAND_NOTHING;
3447 return NULL;
3448 }
3449
3450/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003451 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 */
3453 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 xp->xp_pattern = cmd;
3455 if (*cmd == NUL)
3456 return NULL;
3457 if (*cmd == '"')
3458 {
3459 xp->xp_context = EXPAND_NOTHING;
3460 return NULL;
3461 }
3462
3463 if (*cmd == '|' || *cmd == '\n')
3464 return cmd + 1; /* There's another command */
3465
3466 /*
3467 * Isolate the command and search for it in the command table.
3468 * Exceptions:
3469 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003470 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3472 */
3473 if (*cmd == 'k' && cmd[1] != 'e')
3474 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003475 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 p = cmd + 1;
3477 }
3478 else
3479 {
3480 p = cmd;
3481 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3482 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003483 /* a user command may contain digits */
3484 if (ASCII_ISUPPER(cmd[0]))
3485 while (ASCII_ISALNUM(*p) || *p == '*')
3486 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003487 /* for python 3.x: ":py3*" commands completion */
3488 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003489 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003490 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003491 while (ASCII_ISALPHA(*p) || *p == '*')
3492 ++p;
3493 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003494 /* check for non-alpha command */
3495 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3496 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003497 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003499 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 {
3501 xp->xp_context = EXPAND_UNSUCCESSFUL;
3502 return NULL;
3503 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003504 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003505 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3506 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3507 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 break;
3509
3510#ifdef FEAT_USR_CMDS
3511 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3513 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514#endif
3515 }
3516
3517 /*
3518 * If the cursor is touching the command, and it ends in an alpha-numeric
3519 * character, complete the command name.
3520 */
3521 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3522 return NULL;
3523
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003524 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 {
3526 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3527 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003528 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 p = cmd + 1;
3530 }
3531#ifdef FEAT_USR_CMDS
3532 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3533 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003534 ea.cmd = cmd;
3535 p = find_ucmd(&ea, p, NULL, xp,
3536# if defined(FEAT_CMDL_COMPL)
3537 &compl
3538# else
3539 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003541 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003542 if (p == NULL)
3543 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 }
3545#endif
3546 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003547 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 {
3549 /* Not still touching the command and it was an illegal one */
3550 xp->xp_context = EXPAND_UNSUCCESSFUL;
3551 return NULL;
3552 }
3553
3554 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3555
3556 if (*p == '!') /* forced commands */
3557 {
3558 forceit = TRUE;
3559 ++p;
3560 }
3561
3562/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003563 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003565 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003566 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567
3568 arg = skipwhite(p);
3569
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003570 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
3572 if (*arg == '>') /* append */
3573 {
3574 if (*++arg == '>')
3575 ++arg;
3576 arg = skipwhite(arg);
3577 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003578 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 {
3580 ++arg;
3581 usefilter = TRUE;
3582 }
3583 }
3584
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003585 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 {
3587 usefilter = forceit; /* :r! filter if forced */
3588 if (*arg == '!') /* :r !filter */
3589 {
3590 ++arg;
3591 usefilter = TRUE;
3592 }
3593 }
3594
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003595 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 {
3597 while (*arg == *cmd) /* allow any number of '>' or '<' */
3598 ++arg;
3599 arg = skipwhite(arg);
3600 }
3601
3602 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003603 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 {
3605 /* Check if we're in the +command */
3606 p = arg + 1;
3607 arg = skip_cmd_arg(arg, FALSE);
3608
3609 /* Still touching the command after '+'? */
3610 if (*arg == NUL)
3611 return p;
3612
3613 /* Skip space(s) after +command to get to the real argument */
3614 arg = skipwhite(arg);
3615 }
3616
3617 /*
3618 * Check for '|' to separate commands and '"' to start comments.
3619 * Don't do this for ":read !cmd" and ":write !cmd".
3620 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003621 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 {
3623 p = arg;
3624 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003625 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 p += 2;
3627 while (*p)
3628 {
3629 if (*p == Ctrl_V)
3630 {
3631 if (p[1] != NUL)
3632 ++p;
3633 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003634 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 || *p == '|' || *p == '\n')
3636 {
3637 if (*(p - 1) != '\\')
3638 {
3639 if (*p == '|' || *p == '\n')
3640 return p + 1;
3641 return NULL; /* It's a comment */
3642 }
3643 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003644 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 }
3646 }
3647
3648 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003649 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 vim_strchr((char_u *)"|\"", *arg) == NULL)
3651 return NULL;
3652
3653 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003654 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003656 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003657 while (*p && p < buff + len)
3658 {
3659 if (*p == ' ' || *p == TAB)
3660 {
3661 /* argument starts after a space */
3662 xp->xp_pattern = ++p;
3663 }
3664 else
3665 {
3666 if (*p == '\\' && *(p + 1) != NUL)
3667 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003668 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003669 }
3670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003672 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003674 int c;
3675 int in_quote = FALSE;
3676 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677
3678 /*
3679 * Allow spaces within back-quotes to count as part of the argument
3680 * being expanded.
3681 */
3682 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003683 p = xp->xp_pattern;
3684 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003686#ifdef FEAT_MBYTE
3687 if (has_mbyte)
3688 c = mb_ptr2char(p);
3689 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003691 c = *p;
3692 if (c == '\\' && p[1] != NUL)
3693 ++p;
3694 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 {
3696 if (!in_quote)
3697 {
3698 xp->xp_pattern = p;
3699 bow = p + 1;
3700 }
3701 in_quote = !in_quote;
3702 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003703 /* An argument can contain just about everything, except
3704 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003705 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003706#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003707 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003708#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003709 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003710 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003711 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003712 while (*p != NUL)
3713 {
3714#ifdef FEAT_MBYTE
3715 if (has_mbyte)
3716 c = mb_ptr2char(p);
3717 else
3718#endif
3719 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003720 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003721 break;
3722#ifdef FEAT_MBYTE
3723 if (has_mbyte)
3724 len = (*mb_ptr2len)(p);
3725 else
3726#endif
3727 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003728 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003729 }
3730 if (in_quote)
3731 bow = p;
3732 else
3733 xp->xp_pattern = p;
3734 p -= len;
3735 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003736 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 }
3738
3739 /*
3740 * If we are still inside the quotes, and we passed a space, just
3741 * expand from there.
3742 */
3743 if (bow != NULL && in_quote)
3744 xp->xp_pattern = bow;
3745 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003746
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003747 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003748 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003749 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003750#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003751 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003752#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003753 /* When still after the command name expand executables. */
3754 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003755 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757
3758 /* Check for environment variable */
3759 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003760#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 || *xp->xp_pattern == '%'
3762#endif
3763 )
3764 {
3765 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3766 if (!vim_isIDc(*p))
3767 break;
3768 if (*p == NUL)
3769 {
3770 xp->xp_context = EXPAND_ENV_VARS;
3771 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003772#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3773 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003774 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003775 compl = EXPAND_ENV_VARS;
3776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 }
3778 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003779#if defined(FEAT_CMDL_COMPL)
3780 /* Check for user names */
3781 if (*xp->xp_pattern == '~')
3782 {
3783 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3784 ;
3785 /* Complete ~user only if it partially matches a user name.
3786 * A full match ~user<Tab> will be replaced by user's home
3787 * directory i.e. something like ~user<Tab> -> /home/user/ */
3788 if (*p == NUL && p > xp->xp_pattern + 1
3789 && match_user(xp->xp_pattern + 1) == 1)
3790 {
3791 xp->xp_context = EXPAND_USER;
3792 ++xp->xp_pattern;
3793 }
3794 }
3795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 }
3797
3798/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003799 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003801 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003803 case CMD_find:
3804 case CMD_sfind:
3805 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003806 if (xp->xp_context == EXPAND_FILES)
3807 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003808 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 case CMD_cd:
3810 case CMD_chdir:
3811 case CMD_lcd:
3812 case CMD_lchdir:
3813 if (xp->xp_context == EXPAND_FILES)
3814 xp->xp_context = EXPAND_DIRECTORIES;
3815 break;
3816 case CMD_help:
3817 xp->xp_context = EXPAND_HELP;
3818 xp->xp_pattern = arg;
3819 break;
3820
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003821 /* Command modifiers: return the argument.
3822 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003824 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 case CMD_belowright:
3826 case CMD_botright:
3827 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003828 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003829 case CMD_cdo:
3830 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003832 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 case CMD_folddoclosed:
3834 case CMD_folddoopen:
3835 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003836 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 case CMD_keepjumps:
3838 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003839 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003840 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003842 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003844 case CMD_noautocmd:
3845 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003847 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003849 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003850 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 case CMD_topleft:
3852 case CMD_verbose:
3853 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003854 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 return arg;
3856
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003857 case CMD_filter:
3858 if (*arg != NUL)
3859 arg = skip_vimgrep_pat(arg, NULL, NULL);
3860 if (arg == NULL || *arg == NUL)
3861 {
3862 xp->xp_context = EXPAND_NOTHING;
3863 return NULL;
3864 }
3865 return skipwhite(arg);
3866
Bram Moolenaar4f688582007-07-24 12:34:30 +00003867#ifdef FEAT_CMDL_COMPL
3868# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 case CMD_match:
3870 if (*arg == NUL || !ends_excmd(*arg))
3871 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003872 /* also complete "None" */
3873 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 arg = skipwhite(skiptowhite(arg));
3875 if (*arg != NUL)
3876 {
3877 xp->xp_context = EXPAND_NOTHING;
3878 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3879 }
3880 }
3881 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003882# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884/*
3885 * All completion for the +cmdline_compl feature goes here.
3886 */
3887
3888# ifdef FEAT_USR_CMDS
3889 case CMD_command:
3890 /* Check for attributes */
3891 while (*arg == '-')
3892 {
3893 arg++; /* Skip "-" */
3894 p = skiptowhite(arg);
3895 if (*p == NUL)
3896 {
3897 /* Cursor is still in the attribute */
3898 p = vim_strchr(arg, '=');
3899 if (p == NULL)
3900 {
3901 /* No "=", so complete attribute names */
3902 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3903 xp->xp_pattern = arg;
3904 return NULL;
3905 }
3906
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003907 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 * their arguments as well.
3909 */
3910 if (STRNICMP(arg, "complete", p - arg) == 0)
3911 {
3912 xp->xp_context = EXPAND_USER_COMPLETE;
3913 xp->xp_pattern = p + 1;
3914 return NULL;
3915 }
3916 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3917 {
3918 xp->xp_context = EXPAND_USER_NARGS;
3919 xp->xp_pattern = p + 1;
3920 return NULL;
3921 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003922 else if (STRNICMP(arg, "addr", p - arg) == 0)
3923 {
3924 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3925 xp->xp_pattern = p + 1;
3926 return NULL;
3927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 return NULL;
3929 }
3930 arg = skipwhite(p);
3931 }
3932
3933 /* After the attributes comes the new command name */
3934 p = skiptowhite(arg);
3935 if (*p == NUL)
3936 {
3937 xp->xp_context = EXPAND_USER_COMMANDS;
3938 xp->xp_pattern = arg;
3939 break;
3940 }
3941
3942 /* And finally comes a normal command */
3943 return skipwhite(p);
3944
3945 case CMD_delcommand:
3946 xp->xp_context = EXPAND_USER_COMMANDS;
3947 xp->xp_pattern = arg;
3948 break;
3949# endif
3950
3951 case CMD_global:
3952 case CMD_vglobal:
3953 delim = *arg; /* get the delimiter */
3954 if (delim)
3955 ++arg; /* skip delimiter if there is one */
3956
3957 while (arg[0] != NUL && arg[0] != delim)
3958 {
3959 if (arg[0] == '\\' && arg[1] != NUL)
3960 ++arg;
3961 ++arg;
3962 }
3963 if (arg[0] != NUL)
3964 return arg + 1;
3965 break;
3966 case CMD_and:
3967 case CMD_substitute:
3968 delim = *arg;
3969 if (delim)
3970 {
3971 /* skip "from" part */
3972 ++arg;
3973 arg = skip_regexp(arg, delim, p_magic, NULL);
3974 }
3975 /* skip "to" part */
3976 while (arg[0] != NUL && arg[0] != delim)
3977 {
3978 if (arg[0] == '\\' && arg[1] != NUL)
3979 ++arg;
3980 ++arg;
3981 }
3982 if (arg[0] != NUL) /* skip delimiter */
3983 ++arg;
3984 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3985 ++arg;
3986 if (arg[0] != NUL)
3987 return arg;
3988 break;
3989 case CMD_isearch:
3990 case CMD_dsearch:
3991 case CMD_ilist:
3992 case CMD_dlist:
3993 case CMD_ijump:
3994 case CMD_psearch:
3995 case CMD_djump:
3996 case CMD_isplit:
3997 case CMD_dsplit:
3998 arg = skipwhite(skipdigits(arg)); /* skip count */
3999 if (*arg == '/') /* Match regexp, not just whole words */
4000 {
4001 for (++arg; *arg && *arg != '/'; arg++)
4002 if (*arg == '\\' && arg[1] != NUL)
4003 arg++;
4004 if (*arg)
4005 {
4006 arg = skipwhite(arg + 1);
4007
4008 /* Check for trailing illegal characters */
4009 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4010 xp->xp_context = EXPAND_NOTHING;
4011 else
4012 return arg;
4013 }
4014 }
4015 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004016
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 case CMD_autocmd:
4018 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004020 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 case CMD_set:
4023 set_context_in_set_cmd(xp, arg, 0);
4024 break;
4025 case CMD_setglobal:
4026 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4027 break;
4028 case CMD_setlocal:
4029 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4030 break;
4031 case CMD_tag:
4032 case CMD_stag:
4033 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004034 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 case CMD_tselect:
4036 case CMD_stselect:
4037 case CMD_ptselect:
4038 case CMD_tjump:
4039 case CMD_stjump:
4040 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004041 if (*p_wop != NUL)
4042 xp->xp_context = EXPAND_TAGS_LISTFILES;
4043 else
4044 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 xp->xp_pattern = arg;
4046 break;
4047 case CMD_augroup:
4048 xp->xp_context = EXPAND_AUGROUP;
4049 xp->xp_pattern = arg;
4050 break;
4051#ifdef FEAT_SYN_HL
4052 case CMD_syntax:
4053 set_context_in_syntax_cmd(xp, arg);
4054 break;
4055#endif
4056#ifdef FEAT_EVAL
4057 case CMD_let:
4058 case CMD_if:
4059 case CMD_elseif:
4060 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004061 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 case CMD_echo:
4063 case CMD_echon:
4064 case CMD_execute:
4065 case CMD_echomsg:
4066 case CMD_echoerr:
4067 case CMD_call:
4068 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004069 case CMD_cexpr:
4070 case CMD_caddexpr:
4071 case CMD_cgetexpr:
4072 case CMD_lexpr:
4073 case CMD_laddexpr:
4074 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004075 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 break;
4077
4078 case CMD_unlet:
4079 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4080 arg = xp->xp_pattern + 1;
4081 xp->xp_context = EXPAND_USER_VARS;
4082 xp->xp_pattern = arg;
4083 break;
4084
4085 case CMD_function:
4086 case CMD_delfunction:
4087 xp->xp_context = EXPAND_USER_FUNC;
4088 xp->xp_pattern = arg;
4089 break;
4090
4091 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004092 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 break;
4094#endif
4095 case CMD_highlight:
4096 set_context_in_highlight_cmd(xp, arg);
4097 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004098#ifdef FEAT_CSCOPE
4099 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004100 case CMD_lcscope:
4101 case CMD_scscope:
4102 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004103 break;
4104#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004105#ifdef FEAT_SIGNS
4106 case CMD_sign:
4107 set_context_in_sign_cmd(xp, arg);
4108 break;
4109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110#ifdef FEAT_LISTCMDS
4111 case CMD_bdelete:
4112 case CMD_bwipeout:
4113 case CMD_bunload:
4114 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4115 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004116 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 case CMD_buffer:
4118 case CMD_sbuffer:
4119 case CMD_checktime:
4120 xp->xp_context = EXPAND_BUFFERS;
4121 xp->xp_pattern = arg;
4122 break;
4123#endif
4124#ifdef FEAT_USR_CMDS
4125 case CMD_USER:
4126 case CMD_USER_BUF:
4127 if (compl != EXPAND_NOTHING)
4128 {
4129 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004130 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 {
4132# ifdef FEAT_MENU
4133 if (compl == EXPAND_MENUS)
4134 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4135# endif
4136 if (compl == EXPAND_COMMANDS)
4137 return arg;
4138 if (compl == EXPAND_MAPPINGS)
4139 return set_context_in_map_cmd(xp, (char_u *)"map",
4140 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004141 /* Find start of last argument. */
4142 p = arg;
4143 while (*p)
4144 {
4145 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004146 /* argument starts after a space */
4147 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004148 else if (*p == '\\' && *(p + 1) != NUL)
4149 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004150 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 xp->xp_pattern = arg;
4153 }
4154 xp->xp_context = compl;
4155 }
4156 break;
4157#endif
4158 case CMD_map: case CMD_noremap:
4159 case CMD_nmap: case CMD_nnoremap:
4160 case CMD_vmap: case CMD_vnoremap:
4161 case CMD_omap: case CMD_onoremap:
4162 case CMD_imap: case CMD_inoremap:
4163 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004164 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004165 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004166 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004167 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004169 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 case CMD_unmap:
4171 case CMD_nunmap:
4172 case CMD_vunmap:
4173 case CMD_ounmap:
4174 case CMD_iunmap:
4175 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004176 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004177 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004178 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004179 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004181 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004182 case CMD_mapclear:
4183 case CMD_nmapclear:
4184 case CMD_vmapclear:
4185 case CMD_omapclear:
4186 case CMD_imapclear:
4187 case CMD_cmapclear:
4188 case CMD_lmapclear:
4189 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004190 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004191 case CMD_xmapclear:
4192 xp->xp_context = EXPAND_MAPCLEAR;
4193 xp->xp_pattern = arg;
4194 break;
4195
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 case CMD_abbreviate: case CMD_noreabbrev:
4197 case CMD_cabbrev: case CMD_cnoreabbrev:
4198 case CMD_iabbrev: case CMD_inoreabbrev:
4199 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004200 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 case CMD_unabbreviate:
4202 case CMD_cunabbrev:
4203 case CMD_iunabbrev:
4204 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004205 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206#ifdef FEAT_MENU
4207 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4208 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4209 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4210 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4211 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4212 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4213 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4214 case CMD_tmenu: case CMD_tunmenu:
4215 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4216 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4217#endif
4218
4219 case CMD_colorscheme:
4220 xp->xp_context = EXPAND_COLORS;
4221 xp->xp_pattern = arg;
4222 break;
4223
4224 case CMD_compiler:
4225 xp->xp_context = EXPAND_COMPILER;
4226 xp->xp_pattern = arg;
4227 break;
4228
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004229 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004230 xp->xp_context = EXPAND_OWNSYNTAX;
4231 xp->xp_pattern = arg;
4232 break;
4233
4234 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004235 xp->xp_context = EXPAND_FILETYPE;
4236 xp->xp_pattern = arg;
4237 break;
4238
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004239 case CMD_packadd:
4240 xp->xp_context = EXPAND_PACKADD;
4241 xp->xp_pattern = arg;
4242 break;
4243
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4245 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4246 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004247 p = skiptowhite(arg);
4248 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 {
4250 xp->xp_context = EXPAND_LANGUAGE;
4251 xp->xp_pattern = arg;
4252 }
4253 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004254 {
4255 if ( STRNCMP(arg, "messages", p - arg) == 0
4256 || STRNCMP(arg, "ctype", p - arg) == 0
4257 || STRNCMP(arg, "time", p - arg) == 0)
4258 {
4259 xp->xp_context = EXPAND_LOCALES;
4260 xp->xp_pattern = skipwhite(p);
4261 }
4262 else
4263 xp->xp_context = EXPAND_NOTHING;
4264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 break;
4266#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004267#if defined(FEAT_PROFILE)
4268 case CMD_profile:
4269 set_context_in_profile_cmd(xp, arg);
4270 break;
4271#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004272 case CMD_behave:
4273 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004274 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004275 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004277 case CMD_messages:
4278 xp->xp_context = EXPAND_MESSAGES;
4279 xp->xp_pattern = arg;
4280 break;
4281
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004282#if defined(FEAT_CMDHIST)
4283 case CMD_history:
4284 xp->xp_context = EXPAND_HISTORY;
4285 xp->xp_pattern = arg;
4286 break;
4287#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004288#if defined(FEAT_PROFILE)
4289 case CMD_syntime:
4290 xp->xp_context = EXPAND_SYNTIME;
4291 xp->xp_pattern = arg;
4292 break;
4293#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004294
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295#endif /* FEAT_CMDL_COMPL */
4296
4297 default:
4298 break;
4299 }
4300 return NULL;
4301}
4302
4303/*
4304 * skip a range specifier of the form: addr [,addr] [;addr] ..
4305 *
4306 * Backslashed delimiters after / or ? will be skipped, and commands will
4307 * not be expanded between /'s and ?'s or after "'".
4308 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004309 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 * Returns the "cmd" pointer advanced to beyond the range.
4311 */
4312 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004313skip_range(
4314 char_u *cmd,
4315 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004317 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004319 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004321 if (*cmd == '\\')
4322 {
4323 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4324 ++cmd;
4325 else
4326 break;
4327 }
4328 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 {
4330 if (*++cmd == NUL && ctx != NULL)
4331 *ctx = EXPAND_NOTHING;
4332 }
4333 else if (*cmd == '/' || *cmd == '?')
4334 {
4335 delim = *cmd++;
4336 while (*cmd != NUL && *cmd != delim)
4337 if (*cmd++ == '\\' && *cmd != NUL)
4338 ++cmd;
4339 if (*cmd == NUL && ctx != NULL)
4340 *ctx = EXPAND_NOTHING;
4341 }
4342 if (*cmd != NUL)
4343 ++cmd;
4344 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004345
4346 /* Skip ":" and white space. */
4347 while (*cmd == ':')
4348 cmd = skipwhite(cmd + 1);
4349
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 return cmd;
4351}
4352
4353/*
4354 * get a single EX address
4355 *
4356 * Set ptr to the next character after the part that was interpreted.
4357 * Set ptr to NULL when an error is encountered.
4358 *
4359 * Return MAXLNUM when no Ex address was found.
4360 */
4361 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004362get_address(
4363 exarg_T *eap UNUSED,
4364 char_u **ptr,
4365 int addr_type, /* flag: one of ADDR_LINES, ... */
4366 int skip, /* only skip the address, don't use it */
Bram Moolenaarded27822017-01-02 14:27:34 +01004367 int to_other_file, /* flag: may jump to other file */
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01004368 int address_count UNUSED) /* 1 for first address, >1 after comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369{
4370 int c;
4371 int i;
4372 long n;
4373 char_u *cmd;
4374 pos_T pos;
4375 pos_T *fp;
4376 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004377 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378
4379 cmd = skipwhite(*ptr);
4380 lnum = MAXLNUM;
4381 do
4382 {
4383 switch (*cmd)
4384 {
4385 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004386 ++cmd;
4387 switch (addr_type)
4388 {
4389 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 lnum = curwin->w_cursor.lnum;
4391 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004392 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004393 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004394 break;
4395 case ADDR_ARGUMENTS:
4396 lnum = curwin->w_arg_idx + 1;
4397 break;
4398 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004399 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004400 lnum = curbuf->b_fnum;
4401 break;
4402 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004403 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004404 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004405 case ADDR_TABS_RELATIVE:
4406 EMSG(_(e_invrange));
4407 cmd = NULL;
4408 goto error;
4409 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004410#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004411 case ADDR_QUICKFIX:
4412 lnum = qf_get_cur_valid_idx(eap);
4413 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004414#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004415 }
4416 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417
4418 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004419 ++cmd;
4420 switch (addr_type)
4421 {
4422 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 lnum = curbuf->b_ml.ml_line_count;
4424 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004425 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004426 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004427 break;
4428 case ADDR_ARGUMENTS:
4429 lnum = ARGCOUNT;
4430 break;
4431 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004432 buf = lastbuf;
4433 while (buf->b_ml.ml_mfp == NULL)
4434 {
4435 if (buf->b_prev == NULL)
4436 break;
4437 buf = buf->b_prev;
4438 }
4439 lnum = buf->b_fnum;
4440 break;
4441 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004442 lnum = lastbuf->b_fnum;
4443 break;
4444 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004445 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004446 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004447 case ADDR_TABS_RELATIVE:
4448 EMSG(_(e_invrange));
4449 cmd = NULL;
4450 goto error;
4451 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004452#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004453 case ADDR_QUICKFIX:
4454 lnum = qf_get_size(eap);
4455 if (lnum == 0)
4456 lnum = 1;
4457 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004458#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004459 }
4460 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461
4462 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004463 if (*++cmd == NUL)
4464 {
4465 cmd = NULL;
4466 goto error;
4467 }
4468 if (addr_type != ADDR_LINES)
4469 {
4470 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004471 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004472 goto error;
4473 }
4474 if (skip)
4475 ++cmd;
4476 else
4477 {
4478 /* Only accept a mark in another file when it is
4479 * used by itself: ":'M". */
4480 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4481 ++cmd;
4482 if (fp == (pos_T *)-1)
4483 /* Jumped to another file. */
4484 lnum = curwin->w_cursor.lnum;
4485 else
4486 {
4487 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 {
4489 cmd = NULL;
4490 goto error;
4491 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004492 lnum = fp->lnum;
4493 }
4494 }
4495 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496
4497 case '/':
4498 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004499 c = *cmd++;
4500 if (addr_type != ADDR_LINES)
4501 {
4502 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004503 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004504 goto error;
4505 }
4506 if (skip) /* skip "/pat/" */
4507 {
4508 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4509 if (*cmd == c)
4510 ++cmd;
4511 }
4512 else
4513 {
4514 pos = curwin->w_cursor; /* save curwin->w_cursor */
4515 /*
4516 * When '/' or '?' follows another address, start
4517 * from there.
4518 */
4519 if (lnum != MAXLNUM)
4520 curwin->w_cursor.lnum = lnum;
4521 /*
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004522 * Start a forward search at the end of the line (unless
4523 * before the first line).
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004524 * Start a backward search at the start of the line.
4525 * This makes sure we never match in the current
4526 * line, and can match anywhere in the
4527 * next/previous line.
4528 */
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004529 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004530 curwin->w_cursor.col = MAXCOL;
4531 else
4532 curwin->w_cursor.col = 0;
4533 searchcmdlen = 0;
4534 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004535 SEARCH_HIS | SEARCH_MSG, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004536 {
4537 curwin->w_cursor = pos;
4538 cmd = NULL;
4539 goto error;
4540 }
4541 lnum = curwin->w_cursor.lnum;
4542 curwin->w_cursor = pos;
4543 /* adjust command string pointer */
4544 cmd += searchcmdlen;
4545 }
4546 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547
4548 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004549 ++cmd;
4550 if (addr_type != ADDR_LINES)
4551 {
4552 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004553 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004554 goto error;
4555 }
4556 if (*cmd == '&')
4557 i = RE_SUBST;
4558 else if (*cmd == '?' || *cmd == '/')
4559 i = RE_SEARCH;
4560 else
4561 {
4562 EMSG(_(e_backslash));
4563 cmd = NULL;
4564 goto error;
4565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004567 if (!skip)
4568 {
4569 /*
4570 * When search follows another address, start from
4571 * there.
4572 */
4573 if (lnum != MAXLNUM)
4574 pos.lnum = lnum;
4575 else
4576 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004578 /*
4579 * Start the search just like for the above
4580 * do_search().
4581 */
4582 if (*cmd != '?')
4583 pos.col = MAXCOL;
4584 else
4585 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004586#ifdef FEAT_VIRTUALEDIT
4587 pos.coladd = 0;
4588#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004589 if (searchit(curwin, curbuf, &pos,
4590 *cmd == '?' ? BACKWARD : FORWARD,
4591 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004592 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004593 lnum = pos.lnum;
4594 else
4595 {
4596 cmd = NULL;
4597 goto error;
4598 }
4599 }
4600 ++cmd;
4601 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602
4603 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004604 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4605 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 }
4607
4608 for (;;)
4609 {
4610 cmd = skipwhite(cmd);
4611 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4612 break;
4613
4614 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004615 {
4616 switch (addr_type)
4617 {
4618 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004619 /* "+1" is same as ".+1" */
4620 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004621 break;
4622 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004623 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004624 break;
4625 case ADDR_ARGUMENTS:
4626 lnum = curwin->w_arg_idx + 1;
4627 break;
4628 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004629 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004630 lnum = curbuf->b_fnum;
4631 break;
4632 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004633 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004634 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004635 case ADDR_TABS_RELATIVE:
4636 lnum = 1;
4637 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004638#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004639 case ADDR_QUICKFIX:
4640 lnum = qf_get_cur_valid_idx(eap);
4641 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004642#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004643 }
4644 }
4645
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 if (VIM_ISDIGIT(*cmd))
4647 i = '+'; /* "number" is same as "+number" */
4648 else
4649 i = *cmd++;
4650 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4651 n = 1;
4652 else
4653 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004654
4655 if (addr_type == ADDR_TABS_RELATIVE)
4656 {
4657 EMSG(_(e_invrange));
4658 cmd = NULL;
4659 goto error;
4660 }
4661 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004662 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004663 lnum = compute_buffer_local_count(
4664 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004666 {
4667#ifdef FEAT_FOLDING
4668 /* Relative line addressing, need to adjust for folded lines
4669 * now, but only do it after the first address. */
4670 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4671 && address_count >= 2)
4672 (void)hasFolding(lnum, NULL, &lnum);
4673#endif
4674 if (i == '-')
4675 lnum -= n;
4676 else
4677 lnum += n;
4678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 }
4680 } while (*cmd == '/' || *cmd == '?');
4681
4682error:
4683 *ptr = cmd;
4684 return lnum;
4685}
4686
4687/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004688 * Get flags from an Ex command argument.
4689 */
4690 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004691get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004692{
4693 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4694 {
4695 if (*eap->arg == 'l')
4696 eap->flags |= EXFLAG_LIST;
4697 else if (*eap->arg == 'p')
4698 eap->flags |= EXFLAG_PRINT;
4699 else
4700 eap->flags |= EXFLAG_NR;
4701 eap->arg = skipwhite(eap->arg + 1);
4702 }
4703}
4704
4705/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 * Function called for command which is Not Implemented. NI!
4707 */
4708 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004709ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710{
4711 if (!eap->skip)
4712 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4713}
4714
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004715#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716/*
4717 * Function called for script command which is Not Implemented. NI!
4718 * Skips over ":perl <<EOF" constructs.
4719 */
4720 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004721ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722{
4723 if (!eap->skip)
4724 ex_ni(eap);
4725 else
4726 vim_free(script_get(eap, eap->arg));
4727}
4728#endif
4729
4730/*
4731 * Check range in Ex command for validity.
4732 * Return NULL when valid, error message when invalid.
4733 */
4734 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004735invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004737 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 if ( eap->line1 < 0
4739 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004740 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004742
4743 if (eap->argt & RANGE)
4744 {
4745 switch(eap->addr_type)
4746 {
4747 case ADDR_LINES:
4748 if (!(eap->argt & NOTADR)
4749 && eap->line2 > curbuf->b_ml.ml_line_count
4750#ifdef FEAT_DIFF
4751 + (eap->cmdidx == CMD_diffget)
4752#endif
4753 )
4754 return (char_u *)_(e_invrange);
4755 break;
4756 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004757 /* add 1 if ARGCOUNT is 0 */
4758 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004759 return (char_u *)_(e_invrange);
4760 break;
4761 case ADDR_BUFFERS:
4762 if (eap->line1 < firstbuf->b_fnum
4763 || eap->line2 > lastbuf->b_fnum)
4764 return (char_u *)_(e_invrange);
4765 break;
4766 case ADDR_LOADED_BUFFERS:
4767 buf = firstbuf;
4768 while (buf->b_ml.ml_mfp == NULL)
4769 {
4770 if (buf->b_next == NULL)
4771 return (char_u *)_(e_invrange);
4772 buf = buf->b_next;
4773 }
4774 if (eap->line1 < buf->b_fnum)
4775 return (char_u *)_(e_invrange);
4776 buf = lastbuf;
4777 while (buf->b_ml.ml_mfp == NULL)
4778 {
4779 if (buf->b_prev == NULL)
4780 return (char_u *)_(e_invrange);
4781 buf = buf->b_prev;
4782 }
4783 if (eap->line2 > buf->b_fnum)
4784 return (char_u *)_(e_invrange);
4785 break;
4786 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004787 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004788 return (char_u *)_(e_invrange);
4789 break;
4790 case ADDR_TABS:
4791 if (eap->line2 > LAST_TAB_NR)
4792 return (char_u *)_(e_invrange);
4793 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004794 case ADDR_TABS_RELATIVE:
4795 /* Do nothing */
4796 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004797#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004798 case ADDR_QUICKFIX:
4799 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4800 return (char_u *)_(e_invrange);
4801 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004802#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004803 }
4804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 return NULL;
4806}
4807
4808/*
4809 * Correct the range for zero line number, if required.
4810 */
4811 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004812correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813{
4814 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4815 {
4816 if (eap->line1 == 0)
4817 eap->line1 = 1;
4818 if (eap->line2 == 0)
4819 eap->line2 = 1;
4820 }
4821}
4822
Bram Moolenaar748bf032005-02-02 23:04:36 +00004823#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004824static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004825
4826/*
4827 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4828 * pattern. Otherwise return eap->arg.
4829 */
4830 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004831skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004832{
4833 char_u *p = eap->arg;
4834
Bram Moolenaara37420f2006-02-04 22:37:47 +00004835 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4836 || eap->cmdidx == CMD_vimgrepadd
4837 || eap->cmdidx == CMD_lvimgrepadd
4838 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004839 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004840 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004841 if (p == NULL)
4842 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004843 }
4844 return p;
4845}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004846
4847/*
4848 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4849 * in the command line, so that things like % get expanded.
4850 */
4851 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004852replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004853{
4854 char_u *new_cmdline;
4855 char_u *program;
4856 char_u *pos;
4857 char_u *ptr;
4858 int len;
4859 int i;
4860
4861 /*
4862 * Don't do it when ":vimgrep" is used for ":grep".
4863 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004864 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4865 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4866 || eap->cmdidx == CMD_grepadd
4867 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004868 && !grep_internal(eap->cmdidx))
4869 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004870 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4871 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004872 {
4873 if (*curbuf->b_p_gp == NUL)
4874 program = p_gp;
4875 else
4876 program = curbuf->b_p_gp;
4877 }
4878 else
4879 {
4880 if (*curbuf->b_p_mp == NUL)
4881 program = p_mp;
4882 else
4883 program = curbuf->b_p_mp;
4884 }
4885
4886 p = skipwhite(p);
4887
4888 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4889 {
4890 /* replace $* by given arguments */
4891 i = 1;
4892 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4893 ++i;
4894 len = (int)STRLEN(p);
4895 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4896 if (new_cmdline == NULL)
4897 return NULL; /* out of memory */
4898 ptr = new_cmdline;
4899 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4900 {
4901 i = (int)(pos - program);
4902 STRNCPY(ptr, program, i);
4903 STRCPY(ptr += i, p);
4904 ptr += len;
4905 program = pos + 2;
4906 }
4907 STRCPY(ptr, program);
4908 }
4909 else
4910 {
4911 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4912 if (new_cmdline == NULL)
4913 return NULL; /* out of memory */
4914 STRCPY(new_cmdline, program);
4915 STRCAT(new_cmdline, " ");
4916 STRCAT(new_cmdline, p);
4917 }
4918 msg_make(p);
4919
4920 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4921 vim_free(*cmdlinep);
4922 *cmdlinep = new_cmdline;
4923 p = new_cmdline;
4924 }
4925 return p;
4926}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004927#endif
4928
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929/*
4930 * Expand file name in Ex command argument.
4931 * Return FAIL for failure, OK otherwise.
4932 */
4933 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004934expand_filename(
4935 exarg_T *eap,
4936 char_u **cmdlinep,
4937 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938{
4939 int has_wildcards; /* need to expand wildcards */
4940 char_u *repl;
4941 int srclen;
4942 char_u *p;
4943 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004944 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945
Bram Moolenaar748bf032005-02-02 23:04:36 +00004946#ifdef FEAT_QUICKFIX
4947 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4948 p = skip_grep_pat(eap);
4949#else
4950 p = eap->arg;
4951#endif
4952
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 /*
4954 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4955 * the file name contains a wildcard it should not cause expanding.
4956 * (it will be expanded anyway if there is a wildcard before replacing).
4957 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004958 has_wildcards = mch_has_wildcard(p);
4959 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004961#ifdef FEAT_EVAL
4962 /* Skip over `=expr`, wildcards in it are not expanded. */
4963 if (p[0] == '`' && p[1] == '=')
4964 {
4965 p += 2;
4966 (void)skip_expr(&p);
4967 if (*p == '`')
4968 ++p;
4969 continue;
4970 }
4971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 /*
4973 * Quick check if this cannot be the start of a special string.
4974 * Also removes backslash before '%', '#' and '<'.
4975 */
4976 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4977 {
4978 ++p;
4979 continue;
4980 }
4981
4982 /*
4983 * Try to find a match at this position.
4984 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004985 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4986 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 if (*errormsgp != NULL) /* error detected */
4988 return FAIL;
4989 if (repl == NULL) /* no match found */
4990 {
4991 p += srclen;
4992 continue;
4993 }
4994
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004995 /* Wildcards won't be expanded below, the replacement is taken
4996 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4997 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4998 {
4999 char_u *l = repl;
5000
5001 repl = expand_env_save(repl);
5002 vim_free(l);
5003 }
5004
Bram Moolenaar63b92542007-03-27 14:57:09 +00005005 /* Need to escape white space et al. with a backslash.
5006 * Don't do this for:
5007 * - replacement that already has been escaped: "##"
5008 * - shell commands (may have to use quotes instead).
5009 * - non-unix systems when there is a single argument (spaces don't
5010 * separate arguments then).
5011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005013 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 && eap->cmdidx != CMD_grep
5016 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005017 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005018 && eap->cmdidx != CMD_lgrep
5019 && eap->cmdidx != CMD_lgrepadd
5020 && eap->cmdidx != CMD_lmake
5021 && eap->cmdidx != CMD_make
5022 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023#ifndef UNIX
5024 && !(eap->argt & NOSPC)
5025#endif
5026 )
5027 {
5028 char_u *l;
5029#ifdef BACKSLASH_IN_FILENAME
5030 /* Don't escape a backslash here, because rem_backslash() doesn't
5031 * remove it later. */
5032 static char_u *nobslash = (char_u *)" \t\"|";
5033# define ESCAPE_CHARS nobslash
5034#else
5035# define ESCAPE_CHARS escape_chars
5036#endif
5037
5038 for (l = repl; *l; ++l)
5039 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5040 {
5041 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5042 if (l != NULL)
5043 {
5044 vim_free(repl);
5045 repl = l;
5046 }
5047 break;
5048 }
5049 }
5050
5051 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02005052 if ((eap->usefilter || eap->cmdidx == CMD_bang
5053 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005054 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 {
5056 char_u *l;
5057
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005058 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 if (l != NULL)
5060 {
5061 vim_free(repl);
5062 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 }
5064 }
5065
5066 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5067 vim_free(repl);
5068 if (p == NULL)
5069 return FAIL;
5070 }
5071
5072 /*
5073 * One file argument: Expand wildcards.
5074 * Don't do this with ":r !command" or ":w !command".
5075 */
5076 if ((eap->argt & NOSPC) && !eap->usefilter)
5077 {
5078 /*
5079 * May do this twice:
5080 * 1. Replace environment variables.
5081 * 2. Replace any other wildcards, remove backslashes.
5082 */
5083 for (n = 1; n <= 2; ++n)
5084 {
5085 if (n == 2)
5086 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 /*
5088 * Halve the number of backslashes (this is Vi compatible).
5089 * For Unix and OS/2, when wildcards are expanded, this is
5090 * done by ExpandOne() below.
5091 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005092#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 if (!has_wildcards)
5094#endif
5095 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 }
5097
5098 if (has_wildcards)
5099 {
5100 if (n == 1)
5101 {
5102 /*
5103 * First loop: May expand environment variables. This
5104 * can be done much faster with expand_env() than with
5105 * something else (e.g., calling a shell).
5106 * After expanding environment variables, check again
5107 * if there are still wildcards present.
5108 */
5109 if (vim_strchr(eap->arg, '$') != NULL
5110 || vim_strchr(eap->arg, '~') != NULL)
5111 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005112 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005113 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 has_wildcards = mch_has_wildcard(NameBuff);
5115 p = NameBuff;
5116 }
5117 else
5118 p = NULL;
5119 }
5120 else /* n == 2 */
5121 {
5122 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005123 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124
5125 ExpandInit(&xpc);
5126 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005127 if (p_wic)
5128 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005130 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 if (p == NULL)
5132 return FAIL;
5133 }
5134 if (p != NULL)
5135 {
5136 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5137 p, cmdlinep);
5138 if (n == 2) /* p came from ExpandOne() */
5139 vim_free(p);
5140 }
5141 }
5142 }
5143 }
5144 return OK;
5145}
5146
5147/*
5148 * Replace part of the command line, keeping eap->cmd, eap->arg and
5149 * eap->nextcmd correct.
5150 * "src" points to the part that is to be replaced, of length "srclen".
5151 * "repl" is the replacement string.
5152 * Returns a pointer to the character after the replaced string.
5153 * Returns NULL for failure.
5154 */
5155 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005156repl_cmdline(
5157 exarg_T *eap,
5158 char_u *src,
5159 int srclen,
5160 char_u *repl,
5161 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162{
5163 int len;
5164 int i;
5165 char_u *new_cmdline;
5166
5167 /*
5168 * The new command line is build in new_cmdline[].
5169 * First allocate it.
5170 * Careful: a "+cmd" argument may have been NUL terminated.
5171 */
5172 len = (int)STRLEN(repl);
5173 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005174 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5176 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5177 return NULL; /* out of memory! */
5178
5179 /*
5180 * Copy the stuff before the expanded part.
5181 * Copy the expanded stuff.
5182 * Copy what came after the expanded part.
5183 * Copy the next commands, if there are any.
5184 */
5185 i = (int)(src - *cmdlinep); /* length of part before match */
5186 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005187
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 mch_memmove(new_cmdline + i, repl, (size_t)len);
5189 i += len; /* remember the end of the string */
5190 STRCPY(new_cmdline + i, src + srclen);
5191 src = new_cmdline + i; /* remember where to continue */
5192
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005193 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 {
5195 i = (int)STRLEN(new_cmdline) + 1;
5196 STRCPY(new_cmdline + i, eap->nextcmd);
5197 eap->nextcmd = new_cmdline + i;
5198 }
5199 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5200 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5201 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5202 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5203 vim_free(*cmdlinep);
5204 *cmdlinep = new_cmdline;
5205
5206 return src;
5207}
5208
5209/*
5210 * Check for '|' to separate commands and '"' to start comments.
5211 */
5212 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005213separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214{
5215 char_u *p;
5216
Bram Moolenaar86b68352004-12-27 21:59:20 +00005217#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005218 p = skip_grep_pat(eap);
5219#else
5220 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005221#endif
5222
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005223 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 {
5225 if (*p == Ctrl_V)
5226 {
5227 if (eap->argt & (USECTRLV | XFILE))
5228 ++p; /* skip CTRL-V and next char */
5229 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005230 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005231 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 if (*p == NUL) /* stop at NUL after CTRL-V */
5233 break;
5234 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005235
5236#ifdef FEAT_EVAL
5237 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005238 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005239 {
5240 p += 2;
5241 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005242 }
5243#endif
5244
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 /* Check for '"': start of comment or '|': next command */
5246 /* :@" and :*" do not start a comment!
5247 * :redir @" doesn't either. */
5248 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5249 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5250 || p != eap->arg)
5251 && (eap->cmdidx != CMD_redir
5252 || p != eap->arg + 1 || p[-1] != '@'))
5253 || *p == '|' || *p == '\n')
5254 {
5255 /*
5256 * We remove the '\' before the '|', unless USECTRLV is used
5257 * AND 'b' is present in 'cpoptions'.
5258 */
5259 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5260 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5261 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005262 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 --p;
5264 }
5265 else
5266 {
5267 eap->nextcmd = check_nextcmd(p);
5268 *p = NUL;
5269 break;
5270 }
5271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005273
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5275 del_trailing_spaces(eap->arg);
5276}
5277
5278/*
5279 * get + command from ex argument
5280 */
5281 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005282getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283{
5284 char_u *arg = *argp;
5285 char_u *command = NULL;
5286
5287 if (*arg == '+') /* +[command] */
5288 {
5289 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005290 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 command = dollar_command;
5292 else
5293 {
5294 command = arg;
5295 arg = skip_cmd_arg(command, TRUE);
5296 if (*arg != NUL)
5297 *arg++ = NUL; /* terminate command with NUL */
5298 }
5299
5300 arg = skipwhite(arg); /* skip over spaces */
5301 *argp = arg;
5302 }
5303 return command;
5304}
5305
5306/*
5307 * Find end of "+command" argument. Skip over "\ " and "\\".
5308 */
5309 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005310skip_cmd_arg(
5311 char_u *p,
5312 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313{
5314 while (*p && !vim_isspace(*p))
5315 {
5316 if (*p == '\\' && p[1] != NUL)
5317 {
5318 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005319 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 else
5321 ++p;
5322 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005323 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 }
5325 return p;
5326}
5327
5328/*
5329 * Get "++opt=arg" argument.
5330 * Return FAIL or OK.
5331 */
5332 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005333getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334{
5335 char_u *arg = eap->arg + 2;
5336 int *pp = NULL;
5337#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005338 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 char_u *p;
5340#endif
5341
5342 /* ":edit ++[no]bin[ary] file" */
5343 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5344 {
5345 if (*arg == 'n')
5346 {
5347 arg += 2;
5348 eap->force_bin = FORCE_NOBIN;
5349 }
5350 else
5351 eap->force_bin = FORCE_BIN;
5352 if (!checkforcmd(&arg, "binary", 3))
5353 return FAIL;
5354 eap->arg = skipwhite(arg);
5355 return OK;
5356 }
5357
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005358 /* ":read ++edit file" */
5359 if (STRNCMP(arg, "edit", 4) == 0)
5360 {
5361 eap->read_edit = TRUE;
5362 eap->arg = skipwhite(arg + 4);
5363 return OK;
5364 }
5365
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 if (STRNCMP(arg, "ff", 2) == 0)
5367 {
5368 arg += 2;
5369 pp = &eap->force_ff;
5370 }
5371 else if (STRNCMP(arg, "fileformat", 10) == 0)
5372 {
5373 arg += 10;
5374 pp = &eap->force_ff;
5375 }
5376#ifdef FEAT_MBYTE
5377 else if (STRNCMP(arg, "enc", 3) == 0)
5378 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005379 if (STRNCMP(arg, "encoding", 8) == 0)
5380 arg += 8;
5381 else
5382 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 pp = &eap->force_enc;
5384 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005385 else if (STRNCMP(arg, "bad", 3) == 0)
5386 {
5387 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005388 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390#endif
5391
5392 if (pp == NULL || *arg != '=')
5393 return FAIL;
5394
5395 ++arg;
5396 *pp = (int)(arg - eap->cmd);
5397 arg = skip_cmd_arg(arg, FALSE);
5398 eap->arg = skipwhite(arg);
5399 *arg = NUL;
5400
5401#ifdef FEAT_MBYTE
5402 if (pp == &eap->force_ff)
5403 {
5404#endif
5405 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5406 return FAIL;
5407#ifdef FEAT_MBYTE
5408 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005409 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 {
5411 /* Make 'fileencoding' lower case. */
5412 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5413 *p = TOLOWER_ASC(*p);
5414 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005415 else
5416 {
5417 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5418 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005419 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005420 if (STRICMP(p, "keep") == 0)
5421 eap->bad_char = BAD_KEEP;
5422 else if (STRICMP(p, "drop") == 0)
5423 eap->bad_char = BAD_DROP;
5424 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5425 eap->bad_char = *p;
5426 else
5427 return FAIL;
5428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429#endif
5430
5431 return OK;
5432}
5433
5434/*
5435 * ":abbreviate" and friends.
5436 */
5437 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005438ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439{
5440 do_exmap(eap, TRUE); /* almost the same as mapping */
5441}
5442
5443/*
5444 * ":map" and friends.
5445 */
5446 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005447ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448{
5449 /*
5450 * If we are sourcing .exrc or .vimrc in current directory we
5451 * print the mappings for security reasons.
5452 */
5453 if (secure)
5454 {
5455 secure = 2;
5456 msg_outtrans(eap->cmd);
5457 msg_putchar('\n');
5458 }
5459 do_exmap(eap, FALSE);
5460}
5461
5462/*
5463 * ":unmap" and friends.
5464 */
5465 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005466ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467{
5468 do_exmap(eap, FALSE);
5469}
5470
5471/*
5472 * ":mapclear" and friends.
5473 */
5474 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005475ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476{
5477 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5478}
5479
5480/*
5481 * ":abclear" and friends.
5482 */
5483 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005484ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485{
5486 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5487}
5488
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005490ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491{
5492 /*
5493 * Disallow auto commands from .exrc and .vimrc in current
5494 * directory for security reasons.
5495 */
5496 if (secure)
5497 {
5498 secure = 2;
5499 eap->errmsg = e_curdir;
5500 }
5501 else if (eap->cmdidx == CMD_autocmd)
5502 do_autocmd(eap->arg, eap->forceit);
5503 else
5504 do_augroup(eap->arg, eap->forceit);
5505}
5506
5507/*
5508 * ":doautocmd": Apply the automatic commands to the current buffer.
5509 */
5510 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005511ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005513 char_u *arg = eap->arg;
5514 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005515 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005516
Bram Moolenaar1610d052016-06-09 22:53:01 +02005517 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5518 /* Only when there is no <nomodeline>. */
5519 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005520 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522
5523#ifdef FEAT_LISTCMDS
5524/*
5525 * :[N]bunload[!] [N] [bufname] unload buffer
5526 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5527 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5528 */
5529 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005530ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531{
5532 eap->errmsg = do_bufdel(
5533 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5534 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5535 : DOBUF_UNLOAD, eap->arg,
5536 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5537}
5538
5539/*
5540 * :[N]buffer [N] to buffer N
5541 * :[N]sbuffer [N] to buffer N
5542 */
5543 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005544ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545{
5546 if (*eap->arg)
5547 eap->errmsg = e_trailing;
5548 else
5549 {
5550 if (eap->addr_count == 0) /* default is current buffer */
5551 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5552 else
5553 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005554 if (eap->do_ecmd_cmd != NULL)
5555 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 }
5557}
5558
5559/*
5560 * :[N]bmodified [N] to next mod. buffer
5561 * :[N]sbmodified [N] to next mod. buffer
5562 */
5563 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005564ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565{
5566 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005567 if (eap->do_ecmd_cmd != NULL)
5568 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569}
5570
5571/*
5572 * :[N]bnext [N] to next buffer
5573 * :[N]sbnext [N] split and to next buffer
5574 */
5575 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005576ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577{
5578 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005579 if (eap->do_ecmd_cmd != NULL)
5580 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581}
5582
5583/*
5584 * :[N]bNext [N] to previous buffer
5585 * :[N]bprevious [N] to previous buffer
5586 * :[N]sbNext [N] split and to previous buffer
5587 * :[N]sbprevious [N] split and to previous buffer
5588 */
5589 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005590ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591{
5592 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005593 if (eap->do_ecmd_cmd != NULL)
5594 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595}
5596
5597/*
5598 * :brewind to first buffer
5599 * :bfirst to first buffer
5600 * :sbrewind split and to first buffer
5601 * :sbfirst split and to first buffer
5602 */
5603 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005604ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605{
5606 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005607 if (eap->do_ecmd_cmd != NULL)
5608 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609}
5610
5611/*
5612 * :blast to last buffer
5613 * :sblast split and to last buffer
5614 */
5615 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005616ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617{
5618 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005619 if (eap->do_ecmd_cmd != NULL)
5620 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621}
5622#endif
5623
5624 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005625ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626{
5627 return (c == NUL || c == '|' || c == '"' || c == '\n');
5628}
5629
5630#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5631 || defined(PROTO)
5632/*
5633 * Return the next command, after the first '|' or '\n'.
5634 * Return NULL if not found.
5635 */
5636 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005637find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638{
5639 while (*p != '|' && *p != '\n')
5640 {
5641 if (*p == NUL)
5642 return NULL;
5643 ++p;
5644 }
5645 return (p + 1);
5646}
5647#endif
5648
5649/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005650 * Check if *p is a separator between Ex commands, skipping over white space.
5651 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 */
5653 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005654check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005656 char_u *s = skipwhite(p);
5657
5658 if (*s == '|' || *s == '\n')
5659 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 else
5661 return NULL;
5662}
5663
5664/*
5665 * - if there are more files to edit
5666 * - and this is the last window
5667 * - and forceit not used
5668 * - and not repeated twice on a row
5669 * return FAIL and give error message if 'message' TRUE
5670 * return OK otherwise
5671 */
5672 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005673check_more(
5674 int message, /* when FALSE check only, no messages */
5675 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676{
5677 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5678
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005679 if (!forceit && only_one_window()
5680 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 {
5682 if (message)
5683 {
5684#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5685 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5686 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005687 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688
5689 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005690 vim_strncpy(buff,
5691 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005692 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005694 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 _("%d more files to edit. Quit anyway?"), n);
5696 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5697 return OK;
5698 return FAIL;
5699 }
5700#endif
5701 if (n == 1)
5702 EMSG(_("E173: 1 more file to edit"));
5703 else
5704 EMSGN(_("E173: %ld more files to edit"), n);
5705 quitmore = 2; /* next try to quit is allowed */
5706 }
5707 return FAIL;
5708 }
5709 return OK;
5710}
5711
5712#ifdef FEAT_CMDL_COMPL
5713/*
5714 * Function given to ExpandGeneric() to obtain the list of command names.
5715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005717get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718{
5719 if (idx >= (int)CMD_SIZE)
5720# ifdef FEAT_USR_CMDS
5721 return get_user_command_name(idx);
5722# else
5723 return NULL;
5724# endif
5725 return cmdnames[idx].cmd_name;
5726}
5727#endif
5728
5729#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005730static 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);
5731static void uc_list(char_u *name, size_t name_len);
5732static 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);
5733static char_u *uc_split_args(char_u *arg, size_t *lenp);
5734static 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 +00005735
5736 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005737uc_add_command(
5738 char_u *name,
5739 size_t name_len,
5740 char_u *rep,
5741 long argt,
5742 long def,
5743 int flags,
5744 int compl,
5745 char_u *compl_arg,
5746 int addr_type,
5747 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748{
5749 ucmd_T *cmd = NULL;
5750 char_u *p;
5751 int i;
5752 int cmp = 1;
5753 char_u *rep_buf = NULL;
5754 garray_T *gap;
5755
Bram Moolenaar9c102382006-05-03 21:26:49 +00005756 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 if (rep_buf == NULL)
5758 {
5759 /* Can't replace termcodes - try using the string as is */
5760 rep_buf = vim_strsave(rep);
5761
5762 /* Give up if out of memory */
5763 if (rep_buf == NULL)
5764 return FAIL;
5765 }
5766
5767 /* get address of growarray: global or in curbuf */
5768 if (flags & UC_BUFFER)
5769 {
5770 gap = &curbuf->b_ucmds;
5771 if (gap->ga_itemsize == 0)
5772 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5773 }
5774 else
5775 gap = &ucmds;
5776
5777 /* Search for the command in the already defined commands. */
5778 for (i = 0; i < gap->ga_len; ++i)
5779 {
5780 size_t len;
5781
5782 cmd = USER_CMD_GA(gap, i);
5783 len = STRLEN(cmd->uc_name);
5784 cmp = STRNCMP(name, cmd->uc_name, name_len);
5785 if (cmp == 0)
5786 {
5787 if (name_len < len)
5788 cmp = -1;
5789 else if (name_len > len)
5790 cmp = 1;
5791 }
5792
5793 if (cmp == 0)
5794 {
5795 if (!force)
5796 {
5797 EMSG(_("E174: Command already exists: add ! to replace it"));
5798 goto fail;
5799 }
5800
Bram Moolenaard23a8232018-02-10 18:45:26 +01005801 VIM_CLEAR(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005802#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005803 VIM_CLEAR(cmd->uc_compl_arg);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 break;
5806 }
5807
5808 /* Stop as soon as we pass the name to add */
5809 if (cmp < 0)
5810 break;
5811 }
5812
5813 /* Extend the array unless we're replacing an existing command */
5814 if (cmp != 0)
5815 {
5816 if (ga_grow(gap, 1) != OK)
5817 goto fail;
5818 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5819 goto fail;
5820
5821 cmd = USER_CMD_GA(gap, i);
5822 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5823
5824 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825
5826 cmd->uc_name = p;
5827 }
5828
5829 cmd->uc_rep = rep_buf;
5830 cmd->uc_argt = argt;
5831 cmd->uc_def = def;
5832 cmd->uc_compl = compl;
5833#ifdef FEAT_EVAL
5834 cmd->uc_scriptID = current_SID;
5835# ifdef FEAT_CMDL_COMPL
5836 cmd->uc_compl_arg = compl_arg;
5837# endif
5838#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005839 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840
5841 return OK;
5842
5843fail:
5844 vim_free(rep_buf);
5845#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5846 vim_free(compl_arg);
5847#endif
5848 return FAIL;
5849}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005852#if defined(FEAT_USR_CMDS)
5853static struct
5854{
5855 int expand;
5856 char *name;
5857} addr_type_complete[] =
5858{
5859 {ADDR_ARGUMENTS, "arguments"},
5860 {ADDR_LINES, "lines"},
5861 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5862 {ADDR_TABS, "tabs"},
5863 {ADDR_BUFFERS, "buffers"},
5864 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005865 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005866 {-1, NULL}
5867};
5868#endif
5869
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005870#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871/*
5872 * List of names for completion for ":command" with the EXPAND_ flag.
5873 * Must be alphabetical for completion.
5874 */
5875static struct
5876{
5877 int expand;
5878 char *name;
5879} command_complete[] =
5880{
5881 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005882 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005884 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005886 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005887#if defined(FEAT_CSCOPE)
5888 {EXPAND_CSCOPE, "cscope"},
5889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5891 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005892 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893#endif
5894 {EXPAND_DIRECTORIES, "dir"},
5895 {EXPAND_ENV_VARS, "environment"},
5896 {EXPAND_EVENTS, "event"},
5897 {EXPAND_EXPRESSION, "expression"},
5898 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005899 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005900 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 {EXPAND_FUNCTIONS, "function"},
5902 {EXPAND_HELP, "help"},
5903 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005904#if defined(FEAT_CMDHIST)
5905 {EXPAND_HISTORY, "history"},
5906#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005907#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005908 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005909 {EXPAND_LOCALES, "locale"},
5910#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005911 {EXPAND_MAPCLEAR, "mapclear"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 {EXPAND_MAPPINGS, "mapping"},
5913 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005914 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005915 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005916#if defined(FEAT_PROFILE)
5917 {EXPAND_SYNTIME, "syntime"},
5918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005920 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005921 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005922#if defined(FEAT_SIGNS)
5923 {EXPAND_SIGN, "sign"},
5924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 {EXPAND_TAGS, "tag"},
5926 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005927 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 {EXPAND_USER_VARS, "var"},
5929 {0, NULL}
5930};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005933#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005935uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936{
5937 int i, j;
5938 int found = FALSE;
5939 ucmd_T *cmd;
5940 int len;
5941 long a;
5942 garray_T *gap;
5943
5944 gap = &curbuf->b_ucmds;
5945 for (;;)
5946 {
5947 for (i = 0; i < gap->ga_len; ++i)
5948 {
5949 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005950 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951
Bram Moolenaard29459b2016-08-26 22:29:11 +02005952 /* Skip commands which don't match the requested prefix and
5953 * commands filtered out. */
5954 if (STRNCMP(name, cmd->uc_name, name_len) != 0
5955 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 continue;
5957
5958 /* Put out the title first time */
5959 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005960 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 found = TRUE;
5962 msg_putchar('\n');
5963 if (got_int)
5964 break;
5965
5966 /* Special cases */
5967 msg_putchar(a & BANG ? '!' : ' ');
5968 msg_putchar(a & REGSTR ? '"' : ' ');
5969 msg_putchar(gap != &ucmds ? 'b' : ' ');
5970 msg_putchar(' ');
5971
Bram Moolenaar8820b482017-03-16 17:23:31 +01005972 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 len = (int)STRLEN(cmd->uc_name) + 4;
5974
5975 do {
5976 msg_putchar(' ');
5977 ++len;
5978 } while (len < 16);
5979
5980 len = 0;
5981
5982 /* Arguments */
5983 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5984 {
5985 case 0: IObuff[len++] = '0'; break;
5986 case (EXTRA): IObuff[len++] = '*'; break;
5987 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5988 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5989 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5990 }
5991
5992 do {
5993 IObuff[len++] = ' ';
5994 } while (len < 5);
5995
5996 /* Range */
5997 if (a & (RANGE|COUNT))
5998 {
5999 if (a & COUNT)
6000 {
6001 /* -count=N */
6002 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6003 len += (int)STRLEN(IObuff + len);
6004 }
6005 else if (a & DFLALL)
6006 IObuff[len++] = '%';
6007 else if (cmd->uc_def >= 0)
6008 {
6009 /* -range=N */
6010 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6011 len += (int)STRLEN(IObuff + len);
6012 }
6013 else
6014 IObuff[len++] = '.';
6015 }
6016
6017 do {
6018 IObuff[len++] = ' ';
6019 } while (len < 11);
6020
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006021 /* Address Type */
6022 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6023 if (addr_type_complete[j].expand != ADDR_LINES
6024 && addr_type_complete[j].expand == cmd->uc_addr_type)
6025 {
6026 STRCPY(IObuff + len, addr_type_complete[j].name);
6027 len += (int)STRLEN(IObuff + len);
6028 break;
6029 }
6030
6031 do {
6032 IObuff[len++] = ' ';
6033 } while (len < 21);
6034
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 /* Completion */
6036 for (j = 0; command_complete[j].expand != 0; ++j)
6037 if (command_complete[j].expand == cmd->uc_compl)
6038 {
6039 STRCPY(IObuff + len, command_complete[j].name);
6040 len += (int)STRLEN(IObuff + len);
6041 break;
6042 }
6043
6044 do {
6045 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006046 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047
6048 IObuff[len] = '\0';
6049 msg_outtrans(IObuff);
6050
6051 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006052#ifdef FEAT_EVAL
6053 if (p_verbose > 0)
6054 last_set_msg(cmd->uc_scriptID);
6055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 out_flush();
6057 ui_breakcheck();
6058 if (got_int)
6059 break;
6060 }
6061 if (gap == &ucmds || i < gap->ga_len)
6062 break;
6063 gap = &ucmds;
6064 }
6065
6066 if (!found)
6067 MSG(_("No user-defined commands found"));
6068}
6069
6070 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006071uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072{
6073 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6074 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6075 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6076 0xb9, 0x7f, 0};
6077 int i;
6078
6079 for (i = 0; fcmd[i]; ++i)
6080 IObuff[i] = fcmd[i] - 0x40;
6081 IObuff[i] = 0;
6082 return IObuff;
6083}
6084
6085 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006086uc_scan_attr(
6087 char_u *attr,
6088 size_t len,
6089 long *argt,
6090 long *def,
6091 int *flags,
6092 int *compl,
6093 char_u **compl_arg,
6094 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095{
6096 char_u *p;
6097
6098 if (len == 0)
6099 {
6100 EMSG(_("E175: No attribute specified"));
6101 return FAIL;
6102 }
6103
6104 /* First, try the simple attributes (no arguments) */
6105 if (STRNICMP(attr, "bang", len) == 0)
6106 *argt |= BANG;
6107 else if (STRNICMP(attr, "buffer", len) == 0)
6108 *flags |= UC_BUFFER;
6109 else if (STRNICMP(attr, "register", len) == 0)
6110 *argt |= REGSTR;
6111 else if (STRNICMP(attr, "bar", len) == 0)
6112 *argt |= TRLBAR;
6113 else
6114 {
6115 int i;
6116 char_u *val = NULL;
6117 size_t vallen = 0;
6118 size_t attrlen = len;
6119
6120 /* Look for the attribute name - which is the part before any '=' */
6121 for (i = 0; i < (int)len; ++i)
6122 {
6123 if (attr[i] == '=')
6124 {
6125 val = &attr[i + 1];
6126 vallen = len - i - 1;
6127 attrlen = i;
6128 break;
6129 }
6130 }
6131
6132 if (STRNICMP(attr, "nargs", attrlen) == 0)
6133 {
6134 if (vallen == 1)
6135 {
6136 if (*val == '0')
6137 /* Do nothing - this is the default */;
6138 else if (*val == '1')
6139 *argt |= (EXTRA | NOSPC | NEEDARG);
6140 else if (*val == '*')
6141 *argt |= EXTRA;
6142 else if (*val == '?')
6143 *argt |= (EXTRA | NOSPC);
6144 else if (*val == '+')
6145 *argt |= (EXTRA | NEEDARG);
6146 else
6147 goto wrong_nargs;
6148 }
6149 else
6150 {
6151wrong_nargs:
6152 EMSG(_("E176: Invalid number of arguments"));
6153 return FAIL;
6154 }
6155 }
6156 else if (STRNICMP(attr, "range", attrlen) == 0)
6157 {
6158 *argt |= RANGE;
6159 if (vallen == 1 && *val == '%')
6160 *argt |= DFLALL;
6161 else if (val != NULL)
6162 {
6163 p = val;
6164 if (*def >= 0)
6165 {
6166two_count:
6167 EMSG(_("E177: Count cannot be specified twice"));
6168 return FAIL;
6169 }
6170
6171 *def = getdigits(&p);
6172 *argt |= (ZEROR | NOTADR);
6173
6174 if (p != val + vallen || vallen == 0)
6175 {
6176invalid_count:
6177 EMSG(_("E178: Invalid default value for count"));
6178 return FAIL;
6179 }
6180 }
6181 }
6182 else if (STRNICMP(attr, "count", attrlen) == 0)
6183 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006184 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185
6186 if (val != NULL)
6187 {
6188 p = val;
6189 if (*def >= 0)
6190 goto two_count;
6191
6192 *def = getdigits(&p);
6193
6194 if (p != val + vallen)
6195 goto invalid_count;
6196 }
6197
6198 if (*def < 0)
6199 *def = 0;
6200 }
6201 else if (STRNICMP(attr, "complete", attrlen) == 0)
6202 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 if (val == NULL)
6204 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006205 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 return FAIL;
6207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006209 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6210 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006213 else if (STRNICMP(attr, "addr", attrlen) == 0)
6214 {
6215 *argt |= RANGE;
6216 if (val == NULL)
6217 {
6218 EMSG(_("E179: argument required for -addr"));
6219 return FAIL;
6220 }
6221 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6222 == FAIL)
6223 return FAIL;
6224 if (addr_type_arg != ADDR_LINES)
6225 *argt |= (ZEROR | NOTADR) ;
6226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 else
6228 {
6229 char_u ch = attr[len];
6230 attr[len] = '\0';
6231 EMSG2(_("E181: Invalid attribute: %s"), attr);
6232 attr[len] = ch;
6233 return FAIL;
6234 }
6235 }
6236
6237 return OK;
6238}
6239
Bram Moolenaara850a712009-01-28 14:42:59 +00006240/*
6241 * ":command ..."
6242 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006244ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245{
6246 char_u *name;
6247 char_u *end;
6248 char_u *p;
6249 long argt = 0;
6250 long def = -1;
6251 int flags = 0;
6252 int compl = EXPAND_NOTHING;
6253 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006254 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006256 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257
6258 p = eap->arg;
6259
6260 /* Check for attributes */
6261 while (*p == '-')
6262 {
6263 ++p;
6264 end = skiptowhite(p);
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006265 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
6266 &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 == FAIL)
6268 return;
6269 p = skipwhite(end);
6270 }
6271
6272 /* Get the name (if any) and skip to the following argument */
6273 name = p;
6274 if (ASCII_ISALPHA(*p))
6275 while (ASCII_ISALNUM(*p))
6276 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006277 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 {
6279 EMSG(_("E182: Invalid command name"));
6280 return;
6281 }
6282 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006283 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284
6285 /* If there is nothing after the name, and no attributes were specified,
6286 * we are listing commands
6287 */
6288 p = skipwhite(end);
6289 if (!has_attr && ends_excmd(*p))
6290 {
6291 uc_list(name, end - name);
6292 }
6293 else if (!ASCII_ISUPPER(*name))
6294 {
6295 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6296 return;
6297 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006298 else if ((name_len == 1 && *name == 'X')
6299 || (name_len <= 4
6300 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6301 {
6302 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6303 return;
6304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 else
6306 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006307 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308}
6309
6310/*
6311 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 * Clear all user commands, global and for current buffer.
6313 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006314 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006315ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316{
6317 uc_clear(&ucmds);
6318 uc_clear(&curbuf->b_ucmds);
6319}
6320
6321/*
6322 * Clear all user commands for "gap".
6323 */
6324 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006325uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326{
6327 int i;
6328 ucmd_T *cmd;
6329
6330 for (i = 0; i < gap->ga_len; ++i)
6331 {
6332 cmd = USER_CMD_GA(gap, i);
6333 vim_free(cmd->uc_name);
6334 vim_free(cmd->uc_rep);
6335# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6336 vim_free(cmd->uc_compl_arg);
6337# endif
6338 }
6339 ga_clear(gap);
6340}
6341
6342 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006343ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344{
6345 int i = 0;
6346 ucmd_T *cmd = NULL;
6347 int cmp = -1;
6348 garray_T *gap;
6349
6350 gap = &curbuf->b_ucmds;
6351 for (;;)
6352 {
6353 for (i = 0; i < gap->ga_len; ++i)
6354 {
6355 cmd = USER_CMD_GA(gap, i);
6356 cmp = STRCMP(eap->arg, cmd->uc_name);
6357 if (cmp <= 0)
6358 break;
6359 }
6360 if (gap == &ucmds || cmp == 0)
6361 break;
6362 gap = &ucmds;
6363 }
6364
6365 if (cmp != 0)
6366 {
6367 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6368 return;
6369 }
6370
6371 vim_free(cmd->uc_name);
6372 vim_free(cmd->uc_rep);
6373# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6374 vim_free(cmd->uc_compl_arg);
6375# endif
6376
6377 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378
6379 if (i < gap->ga_len)
6380 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6381}
6382
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006383/*
6384 * split and quote args for <f-args>
6385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006387uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388{
6389 char_u *buf;
6390 char_u *p;
6391 char_u *q;
6392 int len;
6393
6394 /* Precalculate length */
6395 p = arg;
6396 len = 2; /* Initial and final quotes */
6397
6398 while (*p)
6399 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006400 if (p[0] == '\\' && p[1] == '\\')
6401 {
6402 len += 2;
6403 p += 2;
6404 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006405 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 {
6407 len += 1;
6408 p += 2;
6409 }
6410 else if (*p == '\\' || *p == '"')
6411 {
6412 len += 2;
6413 p += 1;
6414 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006415 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 {
6417 p = skipwhite(p);
6418 if (*p == NUL)
6419 break;
6420 len += 3; /* "," */
6421 }
6422 else
6423 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006424#ifdef FEAT_MBYTE
6425 int charlen = (*mb_ptr2len)(p);
6426 len += charlen;
6427 p += charlen;
6428#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 ++len;
6430 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 }
6433 }
6434
6435 buf = alloc(len + 1);
6436 if (buf == NULL)
6437 {
6438 *lenp = 0;
6439 return buf;
6440 }
6441
6442 p = arg;
6443 q = buf;
6444 *q++ = '"';
6445 while (*p)
6446 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006447 if (p[0] == '\\' && p[1] == '\\')
6448 {
6449 *q++ = '\\';
6450 *q++ = '\\';
6451 p += 2;
6452 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006453 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 {
6455 *q++ = p[1];
6456 p += 2;
6457 }
6458 else if (*p == '\\' || *p == '"')
6459 {
6460 *q++ = '\\';
6461 *q++ = *p++;
6462 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006463 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 {
6465 p = skipwhite(p);
6466 if (*p == NUL)
6467 break;
6468 *q++ = '"';
6469 *q++ = ',';
6470 *q++ = '"';
6471 }
6472 else
6473 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006474 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 }
6476 }
6477 *q++ = '"';
6478 *q = 0;
6479
6480 *lenp = len;
6481 return buf;
6482}
6483
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006484 static size_t
6485add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6486{
6487 size_t result;
6488
6489 result = STRLEN(mod_str);
6490 if (*multi_mods)
6491 result += 1;
6492 if (buf != NULL)
6493 {
6494 if (*multi_mods)
6495 STRCAT(buf, " ");
6496 STRCAT(buf, mod_str);
6497 }
6498
6499 *multi_mods = 1;
6500
6501 return result;
6502}
6503
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504/*
6505 * Check for a <> code in a user command.
6506 * "code" points to the '<'. "len" the length of the <> (inclusive).
6507 * "buf" is where the result is to be added.
6508 * "split_buf" points to a buffer used for splitting, caller should free it.
6509 * "split_len" is the length of what "split_buf" contains.
6510 * Returns the length of the replacement, which has been added to "buf".
6511 * Returns -1 if there was no match, and only the "<" has been copied.
6512 */
6513 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006514uc_check_code(
6515 char_u *code,
6516 size_t len,
6517 char_u *buf,
6518 ucmd_T *cmd, /* the user command we're expanding */
6519 exarg_T *eap, /* ex arguments */
6520 char_u **split_buf,
6521 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522{
6523 size_t result = 0;
6524 char_u *p = code + 1;
6525 size_t l = len - 2;
6526 int quote = 0;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006527 enum {
6528 ct_ARGS,
6529 ct_BANG,
6530 ct_COUNT,
6531 ct_LINE1,
6532 ct_LINE2,
6533 ct_RANGE,
6534 ct_MODS,
6535 ct_REGISTER,
6536 ct_LT,
6537 ct_NONE
6538 } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539
6540 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6541 {
6542 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6543 p += 2;
6544 l -= 2;
6545 }
6546
Bram Moolenaar371d5402006-03-20 21:47:49 +00006547 ++l;
6548 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006550 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006552 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006554 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006556 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006558 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 type = ct_LINE2;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006560 else if (STRNICMP(p, "range>", l) == 0)
6561 type = ct_RANGE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006562 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006564 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006566 else if (STRNICMP(p, "mods>", l) == 0)
6567 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568
6569 switch (type)
6570 {
6571 case ct_ARGS:
6572 /* Simple case first */
6573 if (*eap->arg == NUL)
6574 {
6575 if (quote == 1)
6576 {
6577 result = 2;
6578 if (buf != NULL)
6579 STRCPY(buf, "''");
6580 }
6581 else
6582 result = 0;
6583 break;
6584 }
6585
6586 /* When specified there is a single argument don't split it.
6587 * Works for ":Cmd %" when % is "a b c". */
6588 if ((eap->argt & NOSPC) && quote == 2)
6589 quote = 1;
6590
6591 switch (quote)
6592 {
6593 case 0: /* No quoting, no splitting */
6594 result = STRLEN(eap->arg);
6595 if (buf != NULL)
6596 STRCPY(buf, eap->arg);
6597 break;
6598 case 1: /* Quote, but don't split */
6599 result = STRLEN(eap->arg) + 2;
6600 for (p = eap->arg; *p; ++p)
6601 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006602#ifdef FEAT_MBYTE
6603 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6604 /* DBCS can contain \ in a trail byte, skip the
6605 * double-byte character. */
6606 ++p;
6607 else
6608#endif
6609 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 ++result;
6611 }
6612
6613 if (buf != NULL)
6614 {
6615 *buf++ = '"';
6616 for (p = eap->arg; *p; ++p)
6617 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006618#ifdef FEAT_MBYTE
6619 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6620 /* DBCS can contain \ in a trail byte, copy the
6621 * double-byte character to avoid escaping. */
6622 *buf++ = *p++;
6623 else
6624#endif
6625 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 *buf++ = '\\';
6627 *buf++ = *p;
6628 }
6629 *buf = '"';
6630 }
6631
6632 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006633 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 /* This is hard, so only do it once, and cache the result */
6635 if (*split_buf == NULL)
6636 *split_buf = uc_split_args(eap->arg, split_len);
6637
6638 result = *split_len;
6639 if (buf != NULL && result != 0)
6640 STRCPY(buf, *split_buf);
6641
6642 break;
6643 }
6644 break;
6645
6646 case ct_BANG:
6647 result = eap->forceit ? 1 : 0;
6648 if (quote)
6649 result += 2;
6650 if (buf != NULL)
6651 {
6652 if (quote)
6653 *buf++ = '"';
6654 if (eap->forceit)
6655 *buf++ = '!';
6656 if (quote)
6657 *buf = '"';
6658 }
6659 break;
6660
6661 case ct_LINE1:
6662 case ct_LINE2:
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006663 case ct_RANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 case ct_COUNT:
6665 {
6666 char num_buf[20];
6667 long num = (type == ct_LINE1) ? eap->line1 :
6668 (type == ct_LINE2) ? eap->line2 :
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006669 (type == ct_RANGE) ? eap->addr_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6671 size_t num_len;
6672
6673 sprintf(num_buf, "%ld", num);
6674 num_len = STRLEN(num_buf);
6675 result = num_len;
6676
6677 if (quote)
6678 result += 2;
6679
6680 if (buf != NULL)
6681 {
6682 if (quote)
6683 *buf++ = '"';
6684 STRCPY(buf, num_buf);
6685 buf += num_len;
6686 if (quote)
6687 *buf = '"';
6688 }
6689
6690 break;
6691 }
6692
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006693 case ct_MODS:
6694 {
6695 int multi_mods = 0;
6696 typedef struct {
6697 int *varp;
6698 char *name;
6699 } mod_entry_T;
6700 static mod_entry_T mod_entries[] = {
6701#ifdef FEAT_BROWSE_CMD
6702 {&cmdmod.browse, "browse"},
6703#endif
6704#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6705 {&cmdmod.confirm, "confirm"},
6706#endif
6707 {&cmdmod.hide, "hide"},
6708 {&cmdmod.keepalt, "keepalt"},
6709 {&cmdmod.keepjumps, "keepjumps"},
6710 {&cmdmod.keepmarks, "keepmarks"},
6711 {&cmdmod.keeppatterns, "keeppatterns"},
6712 {&cmdmod.lockmarks, "lockmarks"},
6713 {&cmdmod.noswapfile, "noswapfile"},
6714 {NULL, NULL}
6715 };
6716 int i;
6717
6718 result = quote ? 2 : 0;
6719 if (buf != NULL)
6720 {
6721 if (quote)
6722 *buf++ = '"';
6723 *buf = '\0';
6724 }
6725
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006726 /* :aboveleft and :leftabove */
6727 if (cmdmod.split & WSP_ABOVE)
6728 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6729 /* :belowright and :rightbelow */
6730 if (cmdmod.split & WSP_BELOW)
6731 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6732 /* :botright */
6733 if (cmdmod.split & WSP_BOT)
6734 result += add_cmd_modifier(buf, "botright", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006735
6736 /* the modifiers that are simple flags */
6737 for (i = 0; mod_entries[i].varp != NULL; ++i)
6738 if (*mod_entries[i].varp)
6739 result += add_cmd_modifier(buf, mod_entries[i].name,
6740 &multi_mods);
6741
6742 /* TODO: How to support :noautocmd? */
6743#ifdef HAVE_SANDBOX
6744 /* TODO: How to support :sandbox?*/
6745#endif
6746 /* :silent */
6747 if (msg_silent > 0)
6748 result += add_cmd_modifier(buf,
6749 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006750 /* :tab */
6751 if (cmdmod.tab > 0)
6752 result += add_cmd_modifier(buf, "tab", &multi_mods);
6753 /* :topleft */
6754 if (cmdmod.split & WSP_TOP)
6755 result += add_cmd_modifier(buf, "topleft", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006756 /* TODO: How to support :unsilent?*/
6757 /* :verbose */
6758 if (p_verbose > 0)
6759 result += add_cmd_modifier(buf, "verbose", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006760 /* :vertical */
6761 if (cmdmod.split & WSP_VERT)
6762 result += add_cmd_modifier(buf, "vertical", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006763 if (quote && buf != NULL)
6764 {
6765 buf += result - 2;
6766 *buf = '"';
6767 }
6768 break;
6769 }
6770
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 case ct_REGISTER:
6772 result = eap->regname ? 1 : 0;
6773 if (quote)
6774 result += 2;
6775 if (buf != NULL)
6776 {
6777 if (quote)
6778 *buf++ = '\'';
6779 if (eap->regname)
6780 *buf++ = eap->regname;
6781 if (quote)
6782 *buf = '\'';
6783 }
6784 break;
6785
6786 case ct_LT:
6787 result = 1;
6788 if (buf != NULL)
6789 *buf = '<';
6790 break;
6791
6792 default:
6793 /* Not recognized: just copy the '<' and return -1. */
6794 result = (size_t)-1;
6795 if (buf != NULL)
6796 *buf = '<';
6797 break;
6798 }
6799
6800 return result;
6801}
6802
6803 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006804do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805{
6806 char_u *buf;
6807 char_u *p;
6808 char_u *q;
6809
6810 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006811 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006812 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 size_t len, totlen;
6814
6815 size_t split_len = 0;
6816 char_u *split_buf = NULL;
6817 ucmd_T *cmd;
6818#ifdef FEAT_EVAL
6819 scid_T save_current_SID = current_SID;
6820#endif
6821
6822 if (eap->cmdidx == CMD_USER)
6823 cmd = USER_CMD(eap->useridx);
6824 else
6825 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6826
6827 /*
6828 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006829 * First round: "buf" is NULL, compute length, allocate "buf".
6830 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 */
6832 buf = NULL;
6833 for (;;)
6834 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006835 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006836 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006838
6839 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006841 start = vim_strchr(p, '<');
6842 if (start != NULL)
6843 end = vim_strchr(start + 1, '>');
6844 if (buf != NULL)
6845 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006846 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6847 ;
6848 if (*ksp == K_SPECIAL
6849 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006850 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6851# ifdef FEAT_GUI
6852 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6853# endif
6854 ))
6855 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006856 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006857 * KS_SPECIAL KE_FILLER, like for mappings, but
6858 * do_cmdline() doesn't handle that, so convert it back.
6859 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6860 len = ksp - p;
6861 if (len > 0)
6862 {
6863 mch_memmove(q, p, len);
6864 q += len;
6865 }
6866 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6867 p = ksp + 3;
6868 continue;
6869 }
6870 }
6871
6872 /* break if there no <item> is found */
6873 if (start == NULL || end == NULL)
6874 break;
6875
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 /* Include the '>' */
6877 ++end;
6878
6879 /* Take everything up to the '<' */
6880 len = start - p;
6881 if (buf == NULL)
6882 totlen += len;
6883 else
6884 {
6885 mch_memmove(q, p, len);
6886 q += len;
6887 }
6888
6889 len = uc_check_code(start, end - start, q, cmd, eap,
6890 &split_buf, &split_len);
6891 if (len == (size_t)-1)
6892 {
6893 /* no match, continue after '<' */
6894 p = start + 1;
6895 len = 1;
6896 }
6897 else
6898 p = end;
6899 if (buf == NULL)
6900 totlen += len;
6901 else
6902 q += len;
6903 }
6904 if (buf != NULL) /* second time here, finished */
6905 {
6906 STRCPY(q, p);
6907 break;
6908 }
6909
6910 totlen += STRLEN(p); /* Add on the trailing characters */
6911 buf = alloc((unsigned)(totlen + 1));
6912 if (buf == NULL)
6913 {
6914 vim_free(split_buf);
6915 return;
6916 }
6917 }
6918
6919#ifdef FEAT_EVAL
6920 current_SID = cmd->uc_scriptID;
6921#endif
6922 (void)do_cmdline(buf, eap->getline, eap->cookie,
6923 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6924#ifdef FEAT_EVAL
6925 current_SID = save_current_SID;
6926#endif
6927 vim_free(buf);
6928 vim_free(split_buf);
6929}
6930
6931# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6932 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006933get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934{
6935 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6936}
6937
6938/*
6939 * Function given to ExpandGeneric() to obtain the list of user command names.
6940 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006942get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943{
6944 if (idx < curbuf->b_ucmds.ga_len)
6945 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6946 idx -= curbuf->b_ucmds.ga_len;
6947 if (idx < ucmds.ga_len)
6948 return USER_CMD(idx)->uc_name;
6949 return NULL;
6950}
6951
6952/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006953 * Function given to ExpandGeneric() to obtain the list of user address type names.
6954 */
6955 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006956get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006957{
6958 return (char_u *)addr_type_complete[idx].name;
6959}
6960
6961/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 * Function given to ExpandGeneric() to obtain the list of user command
6963 * attributes.
6964 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006966get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967{
6968 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006969 {"addr", "bang", "bar", "buffer", "complete",
6970 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006972 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 return NULL;
6974 return (char_u *)user_cmd_flags[idx];
6975}
6976
6977/*
6978 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6979 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006981get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982{
6983 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6984
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006985 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 return NULL;
6987 return (char_u *)user_cmd_nargs[idx];
6988}
6989
6990/*
6991 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6992 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006994get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995{
6996 return (char_u *)command_complete[idx].name;
6997}
6998# endif /* FEAT_CMDL_COMPL */
6999
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007000/*
7001 * Parse address type argument
7002 */
7003 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007004parse_addr_type_arg(
7005 char_u *value,
7006 int vallen,
7007 long *argt,
7008 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007009{
7010 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007011
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007012 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7013 {
7014 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7015 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7016 if (a && b)
7017 {
7018 *addr_type_arg = addr_type_complete[i].expand;
7019 break;
7020 }
7021 }
7022
7023 if (addr_type_complete[i].expand == -1)
7024 {
7025 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007026
Bram Moolenaar1c465442017-03-12 20:10:05 +01007027 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007028 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007029 err[i] = NUL;
7030 EMSG2(_("E180: Invalid address type value: %s"), err);
7031 return FAIL;
7032 }
7033
7034 if (*addr_type_arg != ADDR_LINES)
7035 *argt |= NOTADR;
7036
7037 return OK;
7038}
7039
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040#endif /* FEAT_USR_CMDS */
7041
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007042#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7043/*
7044 * Parse a completion argument "value[vallen]".
7045 * The detected completion goes in "*complp", argument type in "*argt".
7046 * When there is an argument, for function and user defined completion, it's
7047 * copied to allocated memory and stored in "*compl_arg".
7048 * Returns FAIL if something is wrong.
7049 */
7050 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007051parse_compl_arg(
7052 char_u *value,
7053 int vallen,
7054 int *complp,
7055 long *argt,
7056 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007057{
7058 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007059# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007060 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007061# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007062 int i;
7063 int valend = vallen;
7064
7065 /* Look for any argument part - which is the part after any ',' */
7066 for (i = 0; i < vallen; ++i)
7067 {
7068 if (value[i] == ',')
7069 {
7070 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007071# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007072 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007073# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007074 valend = i;
7075 break;
7076 }
7077 }
7078
7079 for (i = 0; command_complete[i].expand != 0; ++i)
7080 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007081 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007082 && STRNCMP(value, command_complete[i].name, valend) == 0)
7083 {
7084 *complp = command_complete[i].expand;
7085 if (command_complete[i].expand == EXPAND_BUFFERS)
7086 *argt |= BUFNAME;
7087 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7088 || command_complete[i].expand == EXPAND_FILES)
7089 *argt |= XFILE;
7090 break;
7091 }
7092 }
7093
7094 if (command_complete[i].expand == 0)
7095 {
7096 EMSG2(_("E180: Invalid complete value: %s"), value);
7097 return FAIL;
7098 }
7099
7100# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7101 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7102 && arg != NULL)
7103# else
7104 if (arg != NULL)
7105# endif
7106 {
7107 EMSG(_("E468: Completion argument only allowed for custom completion"));
7108 return FAIL;
7109 }
7110
7111# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7112 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7113 && arg == NULL)
7114 {
7115 EMSG(_("E467: Custom completion requires a function argument"));
7116 return FAIL;
7117 }
7118
7119 if (arg != NULL)
7120 *compl_arg = vim_strnsave(arg, (int)arglen);
7121# endif
7122 return OK;
7123}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007124
7125 int
7126cmdcomplete_str_to_type(char_u *complete_str)
7127{
7128 int i;
7129
7130 for (i = 0; command_complete[i].expand != 0; ++i)
7131 if (STRCMP(complete_str, command_complete[i].name) == 0)
7132 return command_complete[i].expand;
7133
7134 return EXPAND_NOTHING;
7135}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007136#endif
7137
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007139ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140{
Bram Moolenaare6850792010-05-14 15:28:44 +02007141 if (*eap->arg == NUL)
7142 {
7143#ifdef FEAT_EVAL
7144 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7145 char_u *p = NULL;
7146
7147 if (expr != NULL)
7148 {
7149 ++emsg_off;
7150 p = eval_to_string(expr, NULL, FALSE);
7151 --emsg_off;
7152 vim_free(expr);
7153 }
7154 if (p != NULL)
7155 {
7156 MSG(p);
7157 vim_free(p);
7158 }
7159 else
7160 MSG("default");
7161#else
7162 MSG(_("unknown"));
7163#endif
7164 }
7165 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007166 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167}
7168
7169 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007170ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171{
7172 if (*eap->arg == NUL && eap->cmd[2] == '!')
7173 MSG(_("Greetings, Vim user!"));
7174 do_highlight(eap->arg, eap->forceit, FALSE);
7175}
7176
7177
7178/*
7179 * Call this function if we thought we were going to exit, but we won't
7180 * (because of an error). May need to restore the terminal mode.
7181 */
7182 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007183not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184{
7185 exiting = FALSE;
7186 settmode(TMODE_RAW);
7187}
7188
7189/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007190 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 */
7192 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007193ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007195 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007196
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197#ifdef FEAT_CMDWIN
7198 if (cmdwin_type != 0)
7199 {
7200 cmdwin_result = Ctrl_C;
7201 return;
7202 }
7203#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007204 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007205 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007206 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007207 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007208 return;
7209 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007210 if (eap->addr_count > 0)
7211 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007212 int wnr = eap->line2;
7213
7214 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7215 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007216 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007217 }
7218 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007219 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007220
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02007221 /* Refuse to quit when locked. */
7222 if (curbuf_locked())
7223 return;
7224 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
7225 /* Bail out when autocommands closed the window.
7226 * Refuse to quit when the buffer in the last window is being closed (can
7227 * only happen in autocommands). */
7228 if (!win_valid(wp)
Bram Moolenaar0ea50702017-07-08 14:44:50 +02007229 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007230 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231
7232#ifdef FEAT_NETBEANS_INTG
7233 netbeansForcedQuit = eap->forceit;
7234#endif
7235
7236 /*
7237 * If there are more files or windows we won't exit.
7238 */
7239 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7240 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02007241 if ((!buf_hide(wp->w_buffer)
7242 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007243 | (eap->forceit ? CCGD_FORCEIT : 0)
7244 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007246 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 {
7248 not_exiting();
7249 }
7250 else
7251 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007252 /* quit last window
7253 * Note: only_one_window() returns true, even so a help window is
7254 * still open. In that case only quit, if no address has been
7255 * specified. Example:
7256 * :h|wincmd w|1q - don't quit
7257 * :h|wincmd w|q - quit
7258 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007259 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007261 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02007262#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007266 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 }
7268}
7269
7270/*
7271 * ":cquit".
7272 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007274ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275{
7276 getout(1); /* this does not always pass on the exit code to the Manx
7277 compiler. why? */
7278}
7279
7280/*
7281 * ":qall": try to quit all windows
7282 */
7283 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007284ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285{
7286# ifdef FEAT_CMDWIN
7287 if (cmdwin_type != 0)
7288 {
7289 if (eap->forceit)
7290 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7291 else
7292 cmdwin_result = K_XF2;
7293 return;
7294 }
7295# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007296
7297 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007298 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007299 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007300 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007301 return;
7302 }
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007303 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7304 /* Refuse to quit when locked or when the buffer in the last window is
7305 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007306 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007307 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007308
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007310 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 getout(0);
7312 not_exiting();
7313}
7314
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315/*
7316 * ":close": close current window, unless it is the last one
7317 */
7318 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007319ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007321 win_T *win;
7322 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007323#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007325 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007327#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007328 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007329 {
7330 if (eap->addr_count == 0)
7331 ex_win_close(eap->forceit, curwin, NULL);
7332 else {
Bram Moolenaar29323592016-07-24 22:04:11 +02007333 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007334 {
7335 winnr++;
7336 if (winnr == eap->line2)
7337 break;
7338 }
7339 if (win == NULL)
7340 win = lastwin;
7341 ex_win_close(eap->forceit, win, NULL);
7342 }
7343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344}
7345
Bram Moolenaar4033c552017-09-16 20:54:51 +02007346#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007347/*
7348 * ":pclose": Close any preview window.
7349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007351ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007352{
7353 win_T *win;
7354
Bram Moolenaar29323592016-07-24 22:04:11 +02007355 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007356 if (win->w_p_pvw)
7357 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007358 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007359 break;
7360 }
7361}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007362#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007363
Bram Moolenaarf740b292006-02-16 22:11:02 +00007364/*
7365 * Close window "win" and take care of handling closing the last window for a
7366 * modified buffer.
7367 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007368 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007369ex_win_close(
7370 int forceit,
7371 win_T *win,
7372 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373{
7374 int need_hide;
7375 buf_T *buf = win->w_buffer;
7376
7377 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007378 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007380#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 if ((p_confirm || cmdmod.confirm) && p_write)
7382 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007383 bufref_T bufref;
7384
7385 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007387 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 return;
7389 need_hide = FALSE;
7390 }
7391 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02007394 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 return;
7396 }
7397 }
7398
Bram Moolenaar4033c552017-09-16 20:54:51 +02007399#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007401#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007402
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007404 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02007405 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007406 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02007407 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408}
7409
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007411 * Handle the argument for a tabpage related ex command.
7412 * Returns a tabpage number.
7413 * When an error is encountered then eap->errmsg is set.
7414 */
7415 static int
7416get_tabpage_arg(exarg_T *eap)
7417{
7418 int tab_number;
7419 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7420
7421 if (eap->arg && *eap->arg != NUL)
7422 {
7423 char_u *p = eap->arg;
7424 char_u *p_save;
7425 int relative = 0; /* argument +N/-N means: go to N places to the
7426 * right/left relative to the current position. */
7427
7428 if (*p == '-')
7429 {
7430 relative = -1;
7431 p++;
7432 }
7433 else if (*p == '+')
7434 {
7435 relative = 1;
7436 p++;
7437 }
7438
7439 p_save = p;
7440 tab_number = getdigits(&p);
7441
7442 if (relative == 0)
7443 {
7444 if (STRCMP(p, "$") == 0)
7445 tab_number = LAST_TAB_NR;
7446 else if (p == p_save || *p_save == '-' || *p != NUL
7447 || tab_number > LAST_TAB_NR)
7448 {
7449 /* No numbers as argument. */
7450 eap->errmsg = e_invarg;
7451 goto theend;
7452 }
7453 }
7454 else
7455 {
7456 if (*p_save == NUL)
7457 tab_number = 1;
7458 else if (p == p_save || *p_save == '-' || *p != NUL
7459 || tab_number == 0)
7460 {
7461 /* No numbers as argument. */
7462 eap->errmsg = e_invarg;
7463 goto theend;
7464 }
7465 tab_number = tab_number * relative + tabpage_index(curtab);
7466 if (!unaccept_arg0 && relative == -1)
7467 --tab_number;
7468 }
7469 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7470 eap->errmsg = e_invarg;
7471 }
7472 else if (eap->addr_count > 0)
7473 {
7474 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007475 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007476 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007477 tab_number = 0;
7478 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007479 else
7480 {
7481 tab_number = eap->line2;
7482 if (!unaccept_arg0 && **eap->cmdlinep == '-')
7483 {
7484 --tab_number;
7485 if (tab_number < unaccept_arg0)
7486 eap->errmsg = e_invarg;
7487 }
7488 }
7489 }
7490 else
7491 {
7492 switch (eap->cmdidx)
7493 {
7494 case CMD_tabnext:
7495 tab_number = tabpage_index(curtab) + 1;
7496 if (tab_number > LAST_TAB_NR)
7497 tab_number = 1;
7498 break;
7499 case CMD_tabmove:
7500 tab_number = LAST_TAB_NR;
7501 break;
7502 default:
7503 tab_number = tabpage_index(curtab);
7504 }
7505 }
7506
7507theend:
7508 return tab_number;
7509}
7510
7511/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007512 * ":tabclose": close current tab page, unless it is the last one.
7513 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 */
7515 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007516ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007518 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007519 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007520
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007521# ifdef FEAT_CMDWIN
7522 if (cmdwin_type != 0)
7523 cmdwin_result = K_IGNORE;
7524 else
7525# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007526 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007527 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007528 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007530 tab_number = get_tabpage_arg(eap);
7531 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007532 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007533 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007534 if (tp == NULL)
7535 {
7536 beep_flush();
7537 return;
7538 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007539 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007540 {
7541 tabpage_close_other(tp, eap->forceit);
7542 return;
7543 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007544 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007545 tabpage_close(eap->forceit);
7546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007548}
7549
7550/*
7551 * ":tabonly": close all tab pages except the current one
7552 */
7553 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007554ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007555{
7556 tabpage_T *tp;
7557 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007558 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007559
7560# ifdef FEAT_CMDWIN
7561 if (cmdwin_type != 0)
7562 cmdwin_result = K_IGNORE;
7563 else
7564# endif
7565 if (first_tabpage->tp_next == NULL)
7566 MSG(_("Already only one tab page"));
7567 else
7568 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007569 tab_number = get_tabpage_arg(eap);
7570 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007571 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007572 goto_tabpage(tab_number);
7573 /* Repeat this up to a 1000 times, because autocommands may
7574 * mess up the lists. */
7575 for (done = 0; done < 1000; ++done)
7576 {
7577 FOR_ALL_TABPAGES(tp)
7578 if (tp->tp_topframe != topframe)
7579 {
7580 tabpage_close_other(tp, eap->forceit);
7581 /* if we failed to close it quit */
7582 if (valid_tabpage(tp))
7583 done = 1000;
7584 /* start over, "tp" is now invalid */
7585 break;
7586 }
7587 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007588 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007589 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007590 }
7591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593
7594/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007595 * Close the current tab page.
7596 */
7597 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007598tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007599{
7600 /* First close all the windows but the current one. If that worked then
7601 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007602 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007603 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007604 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007605 ex_win_close(forceit, curwin, NULL);
7606# ifdef FEAT_GUI
7607 need_mouse_correct = TRUE;
7608# endif
7609}
7610
7611/*
7612 * Close tab page "tp", which is not the current tab page.
7613 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007614 * Also takes care of the tab pages line disappearing when closing the
7615 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007616 */
7617 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007618tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007619{
7620 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007621 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007622 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007623
7624 /* Limit to 1000 windows, autocommands may add a window while we close
7625 * one. OK, so I'm paranoid... */
7626 while (++done < 1000)
7627 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007628 wp = tp->tp_firstwin;
7629 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007630
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007631 /* Autocommands may delete the tab page under our fingers and we may
7632 * fail to close a window with a modified buffer. */
7633 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007634 break;
7635 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007636
Bram Moolenaar29323592016-07-24 22:04:11 +02007637 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02007638
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007639 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007640 if (h != tabline_height())
7641 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007642}
7643
7644/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 * ":only".
7646 */
7647 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007648ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007650 win_T *wp;
7651 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652# ifdef FEAT_GUI
7653 need_mouse_correct = TRUE;
7654# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007655 if (eap->addr_count > 0)
7656 {
7657 wnr = eap->line2;
7658 for (wp = firstwin; --wnr > 0; )
7659 {
7660 if (wp->w_next == NULL)
7661 break;
7662 else
7663 wp = wp->w_next;
7664 }
7665 win_goto(wp);
7666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 close_others(TRUE, eap->forceit);
7668}
7669
7670/*
7671 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007672 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007674 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007675ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676{
7677 if (eap->addr_count == 0)
7678 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007679 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681
7682 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007683ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007685 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01007686 if (!eap->skip)
7687 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007688#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007689 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007690#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007691 if (eap->addr_count == 0)
7692 win_close(curwin, FALSE); /* don't free buffer */
7693 else
7694 {
7695 int winnr = 0;
7696 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007697
Bram Moolenaar2256c992016-11-15 21:17:07 +01007698 FOR_ALL_WINDOWS(win)
7699 {
7700 winnr++;
7701 if (winnr == eap->line2)
7702 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007703 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007704 if (win == NULL)
7705 win = lastwin;
7706 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 }
7709}
7710
7711/*
7712 * ":stop" and ":suspend": Suspend Vim.
7713 */
7714 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007715ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716{
7717 /*
7718 * Disallow suspending for "rvim".
7719 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007720 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 {
7722 if (!eap->forceit)
7723 autowrite_all();
7724 windgoto((int)Rows - 1, 0);
7725 out_char('\n');
7726 out_flush();
7727 stoptermcap();
7728 out_flush(); /* needed for SUN to restore xterm buffer */
7729#ifdef FEAT_TITLE
7730 mch_restore_title(3); /* restore window titles */
7731#endif
7732 ui_suspend(); /* call machine specific function */
7733#ifdef FEAT_TITLE
7734 maketitle();
7735 resettitle(); /* force updating the title */
7736#endif
7737 starttermcap();
7738 scroll_start(); /* scroll screen before redrawing */
7739 redraw_later_clear();
7740 shell_resized(); /* may have resized window */
7741 }
7742}
7743
7744/*
7745 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7746 */
7747 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007748ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749{
7750#ifdef FEAT_CMDWIN
7751 if (cmdwin_type != 0)
7752 {
7753 cmdwin_result = Ctrl_C;
7754 return;
7755 }
7756#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007757 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007758 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007759 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007760 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007761 return;
7762 }
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007763 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7764 /* Refuse to quit when locked or when the buffer in the last window is
7765 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007766 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007767 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768
7769 /*
7770 * if more files or windows we won't exit
7771 */
7772 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7773 exiting = TRUE;
7774 if ( ((eap->cmdidx == CMD_wq
7775 || curbufIsChanged())
7776 && do_write(eap) == FAIL)
7777 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007778 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 {
7780 not_exiting();
7781 }
7782 else
7783 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007786 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787# ifdef FEAT_GUI
7788 need_mouse_correct = TRUE;
7789# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007790 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007791 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 }
7793}
7794
7795/*
7796 * ":print", ":list", ":number".
7797 */
7798 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007799ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007801 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7802 EMSG(_(e_emptybuf));
7803 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007805 for ( ;!got_int; ui_breakcheck())
7806 {
7807 print_line(eap->line1,
7808 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7809 || (eap->flags & EXFLAG_NR)),
7810 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7811 if (++eap->line1 > eap->line2)
7812 break;
7813 out_flush(); /* show one line at a time */
7814 }
7815 setpcmark();
7816 /* put cursor at last line */
7817 curwin->w_cursor.lnum = eap->line2;
7818 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 }
7820
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822}
7823
7824#ifdef FEAT_BYTEOFF
7825 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007826ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827{
7828 goto_byte(eap->line2);
7829}
7830#endif
7831
7832/*
7833 * ":shell".
7834 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007836ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837{
7838 do_shell(NULL, 0);
7839}
7840
Bram Moolenaar4033c552017-09-16 20:54:51 +02007841#if defined(HAVE_DROP_FILE) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007843 || defined(FEAT_GUI_MSWIN) \
7844 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 || defined(PROTO)
7846
7847/*
7848 * Handle a file drop. The code is here because a drop is *nearly* like an
7849 * :args command, but not quite (we have a list of exact filenames, so we
7850 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7851 * code is very similar to :args and hence needs access to a lot of the static
7852 * functions in this file.
7853 *
7854 * The list should be allocated using alloc(), as should each item in the
7855 * list. This function takes over responsibility for freeing the list.
7856 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007857 * XXX The list is made into the argument list. This is freed using
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007858 * FreeWild(), which does a series of vim_free() calls.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 */
7860 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007861handle_drop(
7862 int filec, /* the number of files dropped */
7863 char_u **filev, /* the list of files dropped */
7864 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865{
7866 exarg_T ea;
7867 int save_msg_scroll = msg_scroll;
7868
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007869 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007870 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007872 if (curbuf_locked())
7873 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007874
Bram Moolenaarc236c162008-07-13 17:41:49 +00007875 /* When the screen is being updated we should not change buffers and
7876 * windows structures, it may cause freed memory to be used. */
7877 if (updating_screen)
7878 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879
7880 /* Check whether the current buffer is changed. If so, we will need
7881 * to split the current window or data could be lost.
7882 * We don't need to check if the 'hidden' option is set, as in this
7883 * case the buffer won't be lost.
7884 */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007885 if (!buf_hide(curbuf) && !split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 {
7887 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007888 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 --emsg_off;
7890 }
7891 if (split)
7892 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 if (win_split(0, 0) == FAIL)
7894 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007895 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896
7897 /* When splitting the window, create a new alist. Otherwise the
7898 * existing one is overwritten. */
7899 alist_unlink(curwin->w_alist);
7900 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 }
7902
7903 /*
7904 * Set up the new argument list.
7905 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007906 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907
7908 /*
7909 * Move to the first file.
7910 */
7911 /* Fake up a minimal "next" command for do_argfile() */
7912 vim_memset(&ea, 0, sizeof(ea));
7913 ea.cmd = (char_u *)"next";
7914 do_argfile(&ea, 0);
7915
7916 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7917 * mode that is not needed here. */
7918 need_start_insertmode = FALSE;
7919
7920 /* Restore msg_scroll, otherwise a following command may cause scrolling
7921 * unexpectedly. The screen will be redrawn by the caller, thus
7922 * msg_scroll being set by displaying a message is irrelevant. */
7923 msg_scroll = save_msg_scroll;
7924}
7925#endif
7926
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927/*
7928 * Clear an argument list: free all file names and reset it to zero entries.
7929 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007930 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007931alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932{
7933 while (--al->al_ga.ga_len >= 0)
7934 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7935 ga_clear(&al->al_ga);
7936}
7937
7938/*
7939 * Init an argument list.
7940 */
7941 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007942alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943{
7944 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7945}
7946
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947/*
7948 * Remove a reference from an argument list.
7949 * Ignored when the argument list is the global one.
7950 * If the argument list is no longer used by any window, free it.
7951 */
7952 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007953alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954{
7955 if (al != &global_alist && --al->al_refcount <= 0)
7956 {
7957 alist_clear(al);
7958 vim_free(al);
7959 }
7960}
7961
Bram Moolenaar4033c552017-09-16 20:54:51 +02007962#if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963/*
7964 * Create a new argument list and use it for the current window.
7965 */
7966 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007967alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968{
7969 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7970 if (curwin->w_alist == NULL)
7971 {
7972 curwin->w_alist = &global_alist;
7973 ++global_alist.al_refcount;
7974 }
7975 else
7976 {
7977 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007978 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 alist_init(curwin->w_alist);
7980 }
7981}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982#endif
7983
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007984#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985/*
7986 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007987 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7988 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 */
7990 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007991alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992{
7993 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007994 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 char_u **new_arg_files;
7996 int new_arg_file_count;
7997 char_u *save_p_su = p_su;
7998 int i;
7999
8000 /* Don't use 'suffixes' here. This should work like the shell did the
8001 * expansion. Also, the vimrc file isn't read yet, thus the user
8002 * can't set the options. */
8003 p_su = empty_option;
8004 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8005 if (old_arg_files != NULL)
8006 {
8007 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008008 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8009 old_arg_count = GARGCOUNT;
8010 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008012 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 && new_arg_file_count > 0)
8014 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008015 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8016 TRUE, fnum_list, fnum_len);
8017 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 }
8020 p_su = save_p_su;
8021}
8022#endif
8023
8024/*
8025 * Set the argument list for the current window.
8026 * Takes over the allocated files[] and the allocated fnames in it.
8027 */
8028 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008029alist_set(
8030 alist_T *al,
8031 int count,
8032 char_u **files,
8033 int use_curbuf,
8034 int *fnum_list,
8035 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036{
8037 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008038 static int recursive = 0;
8039
8040 if (recursive)
8041 {
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008042 EMSG(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008043 return;
8044 }
8045 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046
8047 alist_clear(al);
8048 if (ga_grow(&al->al_ga, count) == OK)
8049 {
8050 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008051 {
8052 if (got_int)
8053 {
8054 /* When adding many buffers this can take a long time. Allow
8055 * interrupting here. */
8056 while (i < count)
8057 vim_free(files[i++]);
8058 break;
8059 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008060
8061 /* May set buffer name of a buffer previously used for the
8062 * argument list, so that it's re-used by alist_add. */
8063 if (fnum_list != NULL && i < fnum_len)
8064 buf_set_name(fnum_list[i], files[i]);
8065
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008067 ui_breakcheck();
8068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 vim_free(files);
8070 }
8071 else
8072 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008075
8076 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077}
8078
8079/*
8080 * Add file "fname" to argument list "al".
8081 * "fname" must have been allocated and "al" must have been checked for room.
8082 */
8083 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008084alist_add(
8085 alist_T *al,
8086 char_u *fname,
8087 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088{
8089 if (fname == NULL) /* don't add NULL file names */
8090 return;
8091#ifdef BACKSLASH_IN_FILENAME
8092 slash_adjust(fname);
8093#endif
8094 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8095 if (set_fnum > 0)
8096 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8097 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8098 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099}
8100
8101#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8102/*
8103 * Adjust slashes in file names. Called after 'shellslash' was set.
8104 */
8105 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008106alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107{
8108 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008110 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111
8112 for (i = 0; i < GARGCOUNT; ++i)
8113 if (GARGLIST[i].ae_fname != NULL)
8114 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008115 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 if (wp->w_alist != &global_alist)
8117 for (i = 0; i < WARGCOUNT(wp); ++i)
8118 if (WARGLIST(wp)[i].ae_fname != NULL)
8119 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120}
8121#endif
8122
8123/*
8124 * ":preserve".
8125 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008127ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008129 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 ml_preserve(curbuf, TRUE);
8131}
8132
8133/*
8134 * ":recover".
8135 */
8136 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008137ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138{
8139 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8140 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008141 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8142 | CCGD_MULTWIN
8143 | (eap->forceit ? CCGD_FORCEIT : 0)
8144 | CCGD_EXCMD)
8145
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 && (*eap->arg == NUL
8147 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8148 ml_recover();
8149 recoverymode = FALSE;
8150}
8151
8152/*
8153 * Command modifier used in a wrong way.
8154 */
8155 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008156ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157{
8158 eap->errmsg = e_invcmd;
8159}
8160
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161/*
8162 * :sview [+command] file split window with new file, read-only
8163 * :split [[+command] file] split window with current or new file
8164 * :vsplit [[+command] file] split window vertically with current or new file
8165 * :new [[+command] file] split window with no or new file
8166 * :vnew [[+command] file] split vertically window with no or new file
8167 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008168 *
8169 * :tabedit open new Tab page with empty window
8170 * :tabedit [+command] file open new Tab page and edit "file"
8171 * :tabnew [[+command] file] just like :tabedit
8172 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 */
8174 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008175ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008177 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008178#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008180#endif
8181#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184
Bram Moolenaar4033c552017-09-16 20:54:51 +02008185#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188
Bram Moolenaar4033c552017-09-16 20:54:51 +02008189#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008191 * quickfix windows. But it's OK when doing ":tab split". */
8192 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 {
8194 if (eap->cmdidx == CMD_split)
8195 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 if (eap->cmdidx == CMD_vsplit)
8197 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200
Bram Moolenaar4033c552017-09-16 20:54:51 +02008201#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008202 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 {
8204 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8205 FNAME_MESS, TRUE, curbuf->b_ffname);
8206 if (fname == NULL)
8207 goto theend;
8208 eap->arg = fname;
8209 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008210# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02008211 else
8212# endif
8213#endif
8214#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 && eap->cmdidx != CMD_new)
8218 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008219 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008220# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008221 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008222# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008223 au_has_group((char_u *)"FileExplorer"))
8224 {
8225 /* No browsing supported but we do have the file explorer:
8226 * Edit the directory. */
8227 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8228 eap->arg = (char_u *)".";
8229 }
8230 else
8231 {
8232 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008234 if (fname == NULL)
8235 goto theend;
8236 eap->arg = fname;
8237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 }
8239 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02008240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008242 /*
8243 * Either open new tab page or split the window.
8244 */
8245 if (eap->cmdidx == CMD_tabedit
8246 || eap->cmdidx == CMD_tabfind
8247 || eap->cmdidx == CMD_tabnew)
8248 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008249 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8250 : eap->addr_count == 0 ? 0
8251 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008252 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008253 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008254
8255 /* set the alternate buffer for the window we came from */
8256 if (curwin != old_curwin
8257 && win_valid(old_curwin)
8258 && old_curwin->w_buffer != curbuf
8259 && !cmdmod.keepalt)
8260 old_curwin->w_alt_fnum = curbuf->b_fnum;
8261 }
8262 }
8263 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8265 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008266# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 /* Reset 'scrollbind' when editing another file, but keep it when
8268 * doing ":split" without arguments. */
8269 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008270# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008272# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008274 {
8275 RESET_BINDING(curwin);
8276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 else
8278 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008279# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 do_exedit(eap, old_curwin);
8281 }
8282
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008283# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008285# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008287# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288theend:
8289 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008290# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008292
8293/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008294 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008295 */
8296 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008297tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008298{
8299 exarg_T ea;
8300
8301 vim_memset(&ea, 0, sizeof(ea));
8302 ea.cmdidx = CMD_tabnew;
8303 ea.cmd = (char_u *)"tabn";
8304 ea.arg = (char_u *)"";
8305 ex_splitview(&ea);
8306}
8307
8308/*
8309 * :tabnext command
8310 */
8311 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008312ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008313{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008314 int tab_number;
8315
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008316 switch (eap->cmdidx)
8317 {
8318 case CMD_tabfirst:
8319 case CMD_tabrewind:
8320 goto_tabpage(1);
8321 break;
8322 case CMD_tablast:
8323 goto_tabpage(9999);
8324 break;
8325 case CMD_tabprevious:
8326 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008327 if (eap->arg && *eap->arg != NUL)
8328 {
8329 char_u *p = eap->arg;
8330 char_u *p_save = p;
8331
8332 tab_number = getdigits(&p);
8333 if (p == p_save || *p_save == '-' || *p != NUL
8334 || tab_number == 0)
8335 {
8336 /* No numbers as argument. */
8337 eap->errmsg = e_invarg;
8338 return;
8339 }
8340 }
8341 else
8342 {
8343 if (eap->addr_count == 0)
8344 tab_number = 1;
8345 else
8346 {
8347 tab_number = eap->line2;
8348 if (tab_number < 1)
8349 {
8350 eap->errmsg = e_invrange;
8351 return;
8352 }
8353 }
8354 }
8355 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008356 break;
8357 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008358 tab_number = get_tabpage_arg(eap);
8359 if (eap->errmsg == NULL)
8360 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008361 break;
8362 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008363}
8364
8365/*
8366 * :tabmove command
8367 */
8368 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008369ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008370{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008371 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008372
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008373 tab_number = get_tabpage_arg(eap);
8374 if (eap->errmsg == NULL)
8375 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008376}
8377
8378/*
8379 * :tabs command: List tabs and their contents.
8380 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008381 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008382ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008383{
8384 tabpage_T *tp;
8385 win_T *wp;
8386 int tabcount = 1;
8387
8388 msg_start();
8389 msg_scroll = TRUE;
8390 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8391 {
8392 msg_putchar('\n');
8393 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008394 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008395 out_flush(); /* output one line at a time */
8396 ui_breakcheck();
8397
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008398 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008399 wp = firstwin;
8400 else
8401 wp = tp->tp_firstwin;
8402 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8403 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008404 msg_putchar('\n');
8405 msg_putchar(wp == curwin ? '>' : ' ');
8406 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008407 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8408 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008409 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008410 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008411 else
8412 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8413 IObuff, IOSIZE, TRUE);
8414 msg_outtrans(IObuff);
8415 out_flush(); /* output one line at a time */
8416 ui_breakcheck();
8417 }
8418 }
8419}
8420
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421/*
8422 * ":mode": Set screen mode.
8423 * If no argument given, just get the screen size and redraw.
8424 */
8425 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008426ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427{
8428 if (*eap->arg == NUL)
8429 shell_resized();
8430 else
8431 mch_screenmode(eap->arg);
8432}
8433
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434/*
8435 * ":resize".
8436 * set, increment or decrement current window height
8437 */
8438 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008439ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440{
8441 int n;
8442 win_T *wp = curwin;
8443
8444 if (eap->addr_count > 0)
8445 {
8446 n = eap->line2;
8447 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8448 ;
8449 }
8450
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008451# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008453# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 if (cmdmod.split & WSP_VERT)
8456 {
8457 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02008458 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8460 n = 9999;
8461 win_setwidth_win((int)n, wp);
8462 }
8463 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 {
8465 if (*eap->arg == '-' || *eap->arg == '+')
8466 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02008467 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 n = 9999;
8469 win_setheight_win((int)n, wp);
8470 }
8471}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472
8473/*
8474 * ":find [+command] <file>" command.
8475 */
8476 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008477ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478{
8479#ifdef FEAT_SEARCHPATH
8480 char_u *fname;
8481 int count;
8482
8483 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8484 TRUE, curbuf->b_ffname);
8485 if (eap->addr_count > 0)
8486 {
8487 /* Repeat finding the file "count" times. This matters when it
8488 * appears several times in the path. */
8489 count = eap->line2;
8490 while (fname != NULL && --count > 0)
8491 {
8492 vim_free(fname);
8493 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8494 FALSE, curbuf->b_ffname);
8495 }
8496 }
8497
8498 if (fname != NULL)
8499 {
8500 eap->arg = fname;
8501#endif
8502 do_exedit(eap, NULL);
8503#ifdef FEAT_SEARCHPATH
8504 vim_free(fname);
8505 }
8506#endif
8507}
8508
8509/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008510 * ":open" simulation: for now just work like ":visual".
8511 */
8512 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008513ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008514{
8515 regmatch_T regmatch;
8516 char_u *p;
8517
8518 curwin->w_cursor.lnum = eap->line2;
8519 beginline(BL_SOL | BL_FIX);
8520 if (*eap->arg == '/')
8521 {
8522 /* ":open /pattern/": put cursor in column found with pattern */
8523 ++eap->arg;
8524 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8525 *p = NUL;
8526 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8527 if (regmatch.regprog != NULL)
8528 {
8529 regmatch.rm_ic = p_ic;
8530 p = ml_get_curline();
8531 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008532 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008533 else
8534 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008535 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008536 }
8537 /* Move to the NUL, ignore any other arguments. */
8538 eap->arg += STRLEN(eap->arg);
8539 }
8540 check_cursor();
8541
8542 eap->cmdidx = CMD_visual;
8543 do_exedit(eap, NULL);
8544}
8545
8546/*
8547 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 */
8549 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008550ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551{
8552 do_exedit(eap, NULL);
8553}
8554
8555/*
8556 * ":edit <file>" command and alikes.
8557 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008559do_exedit(
8560 exarg_T *eap,
8561 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562{
8563 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008565 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566
8567 /*
8568 * ":vi" command ends Ex mode.
8569 */
8570 if (exmode_active && (eap->cmdidx == CMD_visual
8571 || eap->cmdidx == CMD_view))
8572 {
8573 exmode_active = FALSE;
8574 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008575 {
8576 /* Special case: ":global/pat/visual\NLvi-commands" */
8577 if (global_busy)
8578 {
8579 int rd = RedrawingDisabled;
8580 int nwr = no_wait_return;
8581 int ms = msg_scroll;
8582#ifdef FEAT_GUI
8583 int he = hold_gui_events;
8584#endif
8585
8586 if (eap->nextcmd != NULL)
8587 {
8588 stuffReadbuff(eap->nextcmd);
8589 eap->nextcmd = NULL;
8590 }
8591
8592 if (exmode_was != EXMODE_VIM)
8593 settmode(TMODE_RAW);
8594 RedrawingDisabled = 0;
8595 no_wait_return = 0;
8596 need_wait_return = FALSE;
8597 msg_scroll = 0;
8598#ifdef FEAT_GUI
8599 hold_gui_events = 0;
8600#endif
8601 must_redraw = CLEAR;
8602
8603 main_loop(FALSE, TRUE);
8604
8605 RedrawingDisabled = rd;
8606 no_wait_return = nwr;
8607 msg_scroll = ms;
8608#ifdef FEAT_GUI
8609 hold_gui_events = he;
8610#endif
8611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 }
8615
8616 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008617 || eap->cmdidx == CMD_tabnew
8618 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02008619 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008621 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 setpcmark();
8623 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008624 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8625 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008627 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 || *eap->arg != NUL
8629#ifdef FEAT_BROWSE
8630 || cmdmod.browse
8631#endif
8632 )
8633 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008634 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8635 * can bring us here, others are stopped earlier. */
8636 if (*eap->arg != NUL && curbuf_locked())
8637 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008638
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 n = readonlymode;
8640 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8641 readonlymode = TRUE;
8642 else if (eap->cmdidx == CMD_enew)
8643 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8644 empty buffer */
8645 setpcmark();
8646 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8647 NULL, eap,
8648 /* ":edit" goes to first line if Vi compatible */
8649 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8650 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8651 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02008652 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008654 /* after a split we can use an existing buffer */
8655 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656#ifdef FEAT_LISTCMDS
8657 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8658#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008659 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 {
8661 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 if (old_curwin != NULL)
8663 {
8664 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02008665 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008667#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008668 cleanup_T cs;
8669
8670 /* Reset the error/interrupt/exception state here so that
8671 * aborting() returns FALSE when closing a window. */
8672 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008673#endif
8674#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008676#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02008677 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008678
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008679#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008680 /* Restore the error/interrupt/exception state if not
8681 * discarded by a new aborting error, interrupt, or
8682 * uncaught exception. */
8683 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008684#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 }
8686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 }
8688 else if (readonlymode && curbuf->b_nwindows == 1)
8689 {
8690 /* When editing an already visited buffer, 'readonly' won't be set
8691 * but the previous value is kept. With ":view" and ":sview" we
8692 * want the file to be readonly, except when another window is
8693 * editing the same buffer. */
8694 curbuf->b_p_ro = TRUE;
8695 }
8696 readonlymode = n;
8697 }
8698 else
8699 {
8700 if (eap->do_ecmd_cmd != NULL)
8701 do_cmdline_cmd(eap->do_ecmd_cmd);
8702#ifdef FEAT_TITLE
8703 n = curwin->w_arg_idx_invalid;
8704#endif
8705 check_arg_idx(curwin);
8706#ifdef FEAT_TITLE
8707 if (n != curwin->w_arg_idx_invalid)
8708 maketitle();
8709#endif
8710 }
8711
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 /*
8713 * if ":split file" worked, set alternate file name in old window to new
8714 * file
8715 */
8716 if (old_curwin != NULL
8717 && *eap->arg != NUL
8718 && curwin != old_curwin
8719 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008720 && old_curwin->w_buffer != curbuf
8721 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723
8724 ex_no_reprint = TRUE;
8725}
8726
8727#ifndef FEAT_GUI
8728/*
8729 * ":gui" and ":gvim" when there is no GUI.
8730 */
8731 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008732ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733{
8734 eap->errmsg = e_nogvim;
8735}
8736#endif
8737
8738#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8739 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008740ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741{
8742 gui_make_tearoff(eap->arg);
8743}
8744#endif
8745
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008746#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008748ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008750 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751}
8752#endif
8753
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008755ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756{
8757 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8758 MSG(_("No swap file"));
8759 else
8760 msg(curbuf->b_ml.ml_mfp->mf_fname);
8761}
8762
8763/*
8764 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8765 * offset.
8766 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8767 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008769ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770{
8771#ifdef FEAT_SCROLLBIND
8772 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008773 win_T *save_curwin = curwin;
8774 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 long topline;
8776 long y;
8777 linenr_T old_linenr = curwin->w_cursor.lnum;
8778
8779 setpcmark();
8780
8781 /*
8782 * determine max topline
8783 */
8784 if (curwin->w_p_scb)
8785 {
8786 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008787 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 {
8789 if (wp->w_p_scb && wp->w_buffer)
8790 {
8791 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8792 if (topline > y)
8793 topline = y;
8794 }
8795 }
8796 if (topline < 1)
8797 topline = 1;
8798 }
8799 else
8800 {
8801 topline = 1;
8802 }
8803
8804
8805 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008806 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008808 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 {
8810 if (curwin->w_p_scb)
8811 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008812 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 y = topline - curwin->w_topline;
8814 if (y > 0)
8815 scrollup(y, TRUE);
8816 else
8817 scrolldown(-y, TRUE);
8818 curwin->w_scbind_pos = topline;
8819 redraw_later(VALID);
8820 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 }
8823 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008824 curwin = save_curwin;
8825 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 if (curwin->w_p_scb)
8827 {
8828 did_syncbind = TRUE;
8829 checkpcmark();
8830 if (old_linenr != curwin->w_cursor.lnum)
8831 {
8832 char_u ctrl_o[2];
8833
8834 ctrl_o[0] = Ctrl_O;
8835 ctrl_o[1] = 0;
8836 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8837 }
8838 }
8839#endif
8840}
8841
8842
8843 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008844ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008846 int i;
8847 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8848 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849
8850 if (eap->usefilter) /* :r!cmd */
8851 do_bang(1, eap, FALSE, FALSE, TRUE);
8852 else
8853 {
8854 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8855 return;
8856
8857#ifdef FEAT_BROWSE
8858 if (cmdmod.browse)
8859 {
8860 char_u *browseFile;
8861
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008862 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 NULL, NULL, NULL, curbuf);
8864 if (browseFile != NULL)
8865 {
8866 i = readfile(browseFile, NULL,
8867 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8868 vim_free(browseFile);
8869 }
8870 else
8871 i = OK;
8872 }
8873 else
8874#endif
8875 if (*eap->arg == NUL)
8876 {
8877 if (check_fname() == FAIL) /* check for no file name */
8878 return;
8879 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8880 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8881 }
8882 else
8883 {
8884 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8885 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8886 i = readfile(eap->arg, NULL,
8887 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8888
8889 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01008890 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008892#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 if (!aborting())
8894#endif
8895 EMSG2(_(e_notopen), eap->arg);
8896 }
8897 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008898 {
8899 if (empty && exmode_active)
8900 {
8901 /* Delete the empty line that remains. Historically ex does
8902 * this but vi doesn't. */
8903 if (eap->line2 == 0)
8904 lnum = curbuf->b_ml.ml_line_count;
8905 else
8906 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008907 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008908 {
8909 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008910 if (curwin->w_cursor.lnum > 1
8911 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008912 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008913 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008914 }
8915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 }
8919}
8920
Bram Moolenaarea408852005-06-25 22:49:46 +00008921static char_u *prev_dir = NULL;
8922
8923#if defined(EXITFREE) || defined(PROTO)
8924 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008925free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00008926{
Bram Moolenaard23a8232018-02-10 18:45:26 +01008927 VIM_CLEAR(prev_dir);
8928 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00008929}
8930#endif
8931
Bram Moolenaarf4258302013-06-02 18:20:17 +02008932/*
8933 * Deal with the side effects of changing the current directory.
8934 * When "local" is TRUE then this was after an ":lcd" command.
8935 */
8936 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008937post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008938{
Bram Moolenaard23a8232018-02-10 18:45:26 +01008939 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02008940 if (local)
8941 {
8942 /* If still in global directory, need to remember current
8943 * directory as global directory. */
8944 if (globaldir == NULL && prev_dir != NULL)
8945 globaldir = vim_strsave(prev_dir);
8946 /* Remember this local directory for the window. */
8947 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8948 curwin->w_localdir = vim_strsave(NameBuff);
8949 }
8950 else
8951 {
8952 /* We are now in the global directory, no need to remember its
8953 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01008954 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02008955 }
8956
8957 shorten_fnames(TRUE);
8958}
8959
Bram Moolenaarea408852005-06-25 22:49:46 +00008960
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961/*
8962 * ":cd", ":lcd", ":chdir" and ":lchdir".
8963 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008964 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008965ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 char_u *new_dir;
8968 char_u *tofree;
8969
8970 new_dir = eap->arg;
8971#if !defined(UNIX) && !defined(VMS)
8972 /* for non-UNIX ":cd" means: print current directory */
8973 if (*new_dir == NUL)
8974 ex_pwd(NULL);
8975 else
8976#endif
8977 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008978 if (allbuf_locked())
8979 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008980 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8981 && !eap->forceit)
8982 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008983 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008984 return;
8985 }
8986
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 /* ":cd -": Change to previous directory */
8988 if (STRCMP(new_dir, "-") == 0)
8989 {
8990 if (prev_dir == NULL)
8991 {
8992 EMSG(_("E186: No previous directory"));
8993 return;
8994 }
8995 new_dir = prev_dir;
8996 }
8997
8998 /* Save current directory for next ":cd -" */
8999 tofree = prev_dir;
9000 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9001 prev_dir = vim_strsave(NameBuff);
9002 else
9003 prev_dir = NULL;
9004
9005#if defined(UNIX) || defined(VMS)
9006 /* for UNIX ":cd" means: go to home directory */
9007 if (*new_dir == NUL)
9008 {
9009 /* use NameBuff for home directory name */
9010# ifdef VMS
9011 char_u *p;
9012
9013 p = mch_getenv((char_u *)"SYS$LOGIN");
9014 if (p == NULL || *p == NUL) /* empty is the same as not set */
9015 NameBuff[0] = NUL;
9016 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009017 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018# else
9019 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9020# endif
9021 new_dir = NameBuff;
9022 }
9023#endif
9024 if (new_dir == NULL || vim_chdir(new_dir))
9025 EMSG(_(e_failed));
9026 else
9027 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009028 int is_local_chdir = eap->cmdidx == CMD_lcd
9029 || eap->cmdidx == CMD_lchdir;
9030
9031 post_chdir(is_local_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032
9033 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009034 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 ex_pwd(eap);
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009036 apply_autocmds(EVENT_DIRCHANGED,
9037 is_local_chdir ? (char_u *)"window" : (char_u *)"global",
9038 new_dir, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039 }
9040 vim_free(tofree);
9041 }
9042}
9043
9044/*
9045 * ":pwd".
9046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009048ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049{
9050 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9051 {
9052#ifdef BACKSLASH_IN_FILENAME
9053 slash_adjust(NameBuff);
9054#endif
9055 msg(NameBuff);
9056 }
9057 else
9058 EMSG(_("E187: Unknown"));
9059}
9060
9061/*
9062 * ":=".
9063 */
9064 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009065ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066{
9067 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009068 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069}
9070
9071 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009072ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009074 int n;
9075 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076
9077 if (cursor_valid())
9078 {
9079 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9080 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02009081 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009083
9084 len = eap->line2;
9085 switch (*eap->arg)
9086 {
9087 case 'm': break;
9088 case NUL: len *= 1000L; break;
9089 default: EMSG2(_(e_invarg2), eap->arg); return;
9090 }
9091 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092}
9093
9094/*
9095 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9096 */
9097 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009098do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099{
9100 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009101 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102
9103 cursor_on();
9104 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009105 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009107 wait_now = msec - done > 1000L ? 1000L : msec - done;
9108#ifdef FEAT_TIMERS
9109 {
9110 long due_time = check_due_timer();
9111
9112 if (due_time > 0 && due_time < wait_now)
9113 wait_now = due_time;
9114 }
9115#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009116#ifdef FEAT_JOB_CHANNEL
9117 if (has_any_channel() && wait_now > 100L)
9118 wait_now = 100L;
9119#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009120 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009121#ifdef FEAT_JOB_CHANNEL
9122 if (has_any_channel())
9123 ui_breakcheck_force(TRUE);
9124 else
9125#endif
9126 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009127#ifdef MESSAGE_QUEUE
9128 /* Process the netbeans and clientserver messages that may have been
9129 * received in the call to ui_breakcheck() when the GUI is in use. This
9130 * may occur when running a test case. */
9131 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 }
9134}
9135
9136 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009137do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138{
9139 int mode;
9140 char_u *cmdp;
9141
9142 cmdp = eap->cmd;
9143 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9144
9145 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9146 eap->arg, mode, isabbrev))
9147 {
9148 case 1: EMSG(_(e_invarg));
9149 break;
9150 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9151 break;
9152 }
9153}
9154
9155/*
9156 * ":winsize" command (obsolete).
9157 */
9158 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009159ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160{
9161 int w, h;
9162 char_u *arg = eap->arg;
9163 char_u *p;
9164
9165 w = getdigits(&arg);
9166 arg = skipwhite(arg);
9167 p = arg;
9168 h = getdigits(&arg);
9169 if (*p != NUL && *arg == NUL)
9170 set_shellsize(w, h, TRUE);
9171 else
9172 EMSG(_("E465: :winsize requires two number arguments"));
9173}
9174
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009176ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177{
9178 int xchar = NUL;
9179 char_u *p;
9180
9181 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9182 {
9183 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9184 if (eap->arg[1] == NUL)
9185 {
9186 EMSG(_(e_invarg));
9187 return;
9188 }
9189 xchar = eap->arg[1];
9190 p = eap->arg + 2;
9191 }
9192 else
9193 p = eap->arg + 1;
9194
9195 eap->nextcmd = check_nextcmd(p);
9196 p = skipwhite(p);
9197 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9198 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009199 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 {
9201 /* Pass flags on for ":vertical wincmd ]". */
9202 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009203 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9205 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009206 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 }
9208}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209
Bram Moolenaar843ee412004-06-30 16:16:41 +00009210#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211/*
9212 * ":winpos".
9213 */
9214 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009215ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216{
9217 int x, y;
9218 char_u *arg = eap->arg;
9219 char_u *p;
9220
9221 if (*arg == NUL)
9222 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009223# if defined(FEAT_GUI) || defined(MSWIN)
9224# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009226# else
9227 if (mch_get_winpos(&x, &y) != FAIL)
9228# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229 {
9230 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9231 msg(IObuff);
9232 }
9233 else
9234# endif
9235 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9236 }
9237 else
9238 {
9239 x = getdigits(&arg);
9240 arg = skipwhite(arg);
9241 p = arg;
9242 y = getdigits(&arg);
9243 if (*p == NUL || *arg != NUL)
9244 {
9245 EMSG(_("E466: :winpos requires two number arguments"));
9246 return;
9247 }
9248# ifdef FEAT_GUI
9249 if (gui.in_use)
9250 gui_mch_set_winpos(x, y);
9251 else if (gui.starting)
9252 {
9253 /* Remember the coordinates for when the window is opened. */
9254 gui_win_x = x;
9255 gui_win_y = y;
9256 }
9257# ifdef HAVE_TGETENT
9258 else
9259# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009260# else
9261# ifdef MSWIN
9262 mch_set_winpos(x, y);
9263# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264# endif
9265# ifdef HAVE_TGETENT
9266 if (*T_CWP)
9267 term_set_winpos(x, y);
9268# endif
9269 }
9270}
9271#endif
9272
9273/*
9274 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9275 */
9276 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009277ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278{
9279 oparg_T oa;
9280
9281 clear_oparg(&oa);
9282 oa.regname = eap->regname;
9283 oa.start.lnum = eap->line1;
9284 oa.end.lnum = eap->line2;
9285 oa.line_count = eap->line2 - eap->line1 + 1;
9286 oa.motion_type = MLINE;
9287#ifdef FEAT_VIRTUALEDIT
9288 virtual_op = FALSE;
9289#endif
9290 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9291 {
9292 setpcmark();
9293 curwin->w_cursor.lnum = eap->line1;
9294 beginline(BL_SOL | BL_FIX);
9295 }
9296
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009297 if (VIsual_active)
9298 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009299
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 switch (eap->cmdidx)
9301 {
9302 case CMD_delete:
9303 oa.op_type = OP_DELETE;
9304 op_delete(&oa);
9305 break;
9306
9307 case CMD_yank:
9308 oa.op_type = OP_YANK;
9309 (void)op_yank(&oa, FALSE, TRUE);
9310 break;
9311
9312 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009313 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009315 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9316#else
9317 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009319 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320 oa.op_type = OP_RSHIFT;
9321 else
9322 oa.op_type = OP_LSHIFT;
9323 op_shift(&oa, FALSE, eap->amount);
9324 break;
9325 }
9326#ifdef FEAT_VIRTUALEDIT
9327 virtual_op = MAYBE;
9328#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009329 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330}
9331
9332/*
9333 * ":put".
9334 */
9335 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009336ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337{
9338 /* ":0put" works like ":1put!". */
9339 if (eap->line2 == 0)
9340 {
9341 eap->line2 = 1;
9342 eap->forceit = TRUE;
9343 }
9344 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009345 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9346 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347}
9348
9349/*
9350 * Handle ":copy" and ":move".
9351 */
9352 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009353ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354{
9355 long n;
9356
Bram Moolenaarded27822017-01-02 14:27:34 +01009357 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 if (eap->arg == NULL) /* error detected */
9359 {
9360 eap->nextcmd = NULL;
9361 return;
9362 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009363 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364
9365 /*
9366 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9367 */
9368 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9369 {
9370 EMSG(_(e_invaddr));
9371 return;
9372 }
9373
9374 if (eap->cmdidx == CMD_move)
9375 {
9376 if (do_move(eap->line1, eap->line2, n) == FAIL)
9377 return;
9378 }
9379 else
9380 ex_copy(eap->line1, eap->line2, n);
9381 u_clearline();
9382 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009383 ex_may_print(eap);
9384}
9385
9386/*
9387 * Print the current line if flags were given to the Ex command.
9388 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009389 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009390ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009391{
9392 if (eap->flags != 0)
9393 {
9394 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9395 (eap->flags & EXFLAG_LIST));
9396 ex_no_reprint = TRUE;
9397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398}
9399
9400/*
9401 * ":smagic" and ":snomagic".
9402 */
9403 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009404ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405{
9406 int magic_save = p_magic;
9407
9408 p_magic = (eap->cmdidx == CMD_smagic);
9409 do_sub(eap);
9410 p_magic = magic_save;
9411}
9412
9413/*
9414 * ":join".
9415 */
9416 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009417ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418{
9419 curwin->w_cursor.lnum = eap->line1;
9420 if (eap->line1 == eap->line2)
9421 {
9422 if (eap->addr_count >= 2) /* :2,2join does nothing */
9423 return;
9424 if (eap->line2 == curbuf->b_ml.ml_line_count)
9425 {
9426 beep_flush();
9427 return;
9428 }
9429 ++eap->line2;
9430 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009431 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009433 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434}
9435
9436/*
9437 * ":[addr]@r" or ":[addr]*r": execute register
9438 */
9439 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009440ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441{
9442 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009443 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444
9445 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009446 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447
9448#ifdef USE_ON_FLY_SCROLL
9449 dont_scroll = TRUE; /* disallow scrolling here */
9450#endif
9451
9452 /* get the register name. No name means to use the previous one */
9453 c = *eap->arg;
9454 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9455 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009456 /* Put the register in the typeahead buffer with the "silent" flag. */
9457 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9458 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009459 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 else
9463 {
9464 int save_efr = exec_from_reg;
9465
9466 exec_from_reg = TRUE;
9467
9468 /*
9469 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009470 * Continue until the stuff buffer is empty and all added characters
9471 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009473 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9475
9476 exec_from_reg = save_efr;
9477 }
9478}
9479
9480/*
9481 * ":!".
9482 */
9483 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009484ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485{
9486 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9487}
9488
9489/*
9490 * ":undo".
9491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009493ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009495 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009496 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009497 else
9498 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499}
9500
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009501#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009502 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009503ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009504{
9505 char_u hash[UNDO_HASH_SIZE];
9506
9507 u_compute_hash(hash);
9508 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9509}
9510
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009511 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009512ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009513{
9514 char_u hash[UNDO_HASH_SIZE];
9515
9516 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009517 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009518}
9519#endif
9520
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521/*
9522 * ":redo".
9523 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009525ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526{
9527 u_redo(1);
9528}
9529
9530/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009531 * ":earlier" and ":later".
9532 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009533 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009534ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009535{
9536 long count = 0;
9537 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009538 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009539 char_u *p = eap->arg;
9540
9541 if (*p == NUL)
9542 count = 1;
9543 else if (isdigit(*p))
9544 {
9545 count = getdigits(&p);
9546 switch (*p)
9547 {
9548 case 's': ++p; sec = TRUE; break;
9549 case 'm': ++p; sec = TRUE; count *= 60; break;
9550 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009551 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9552 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009553 }
9554 }
9555
9556 if (*p != NUL)
9557 EMSG2(_(e_invarg2), eap->arg);
9558 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009559 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9560 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009561}
9562
9563/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 * ":redir": start/stop redirection.
9565 */
9566 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009567ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568{
9569 char *mode;
9570 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009571 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572
Bram Moolenaarba768492016-07-08 15:32:54 +02009573#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009574 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009575 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009576 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009577 return;
9578 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009579#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009580
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 if (STRICMP(eap->arg, "END") == 0)
9582 close_redir();
9583 else
9584 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009585 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009587 ++arg;
9588 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009590 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 mode = "a";
9592 }
9593 else
9594 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009595 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596
9597 close_redir();
9598
9599 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009600 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 if (fname == NULL)
9602 return;
9603#ifdef FEAT_BROWSE
9604 if (cmdmod.browse)
9605 {
9606 char_u *browseFile;
9607
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009608 browseFile = do_browse(BROWSE_SAVE,
9609 (char_u *)_("Save Redirection"),
9610 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 if (browseFile == NULL)
9612 return; /* operation cancelled */
9613 vim_free(fname);
9614 fname = browseFile;
9615 eap->forceit = TRUE; /* since dialog already asked */
9616 }
9617#endif
9618
9619 redir_fd = open_exfile(fname, eap->forceit, mode);
9620 vim_free(fname);
9621 }
9622#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009623 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 {
9625 /* redirect to a register a-z (resp. A-Z for appending) */
9626 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009627 ++arg;
9628 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009630 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009631 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009633 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009635 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009636 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009637 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009638 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009640 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009641 if (*arg == '>')
9642 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009643 /* Make register empty when not using @A-@Z and the
9644 * command is valid. */
9645 if (*arg == NUL && !isupper(redir_reg))
9646 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009648 }
9649 if (*arg != NUL)
9650 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009651 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009652 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009653 }
9654 }
9655 else if (*arg == '=' && arg[1] == '>')
9656 {
9657 int append;
9658
9659 /* redirect to a variable */
9660 close_redir();
9661 arg += 2;
9662
9663 if (*arg == '>')
9664 {
9665 ++arg;
9666 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 }
9668 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009669 append = FALSE;
9670
9671 if (var_redir_start(skipwhite(arg), append) == OK)
9672 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 }
9674#endif
9675
9676 /* TODO: redirect to a buffer */
9677
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009679 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009681
9682 /* Make sure redirection is not off. Can happen for cmdline completion
9683 * that indirectly invokes a command to catch its output. */
9684 if (redir_fd != NULL
9685#ifdef FEAT_EVAL
9686 || redir_reg || redir_vname
9687#endif
9688 )
9689 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690}
9691
9692/*
9693 * ":redraw": force redraw
9694 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009695 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009696ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697{
9698 int r = RedrawingDisabled;
9699 int p = p_lz;
9700
9701 RedrawingDisabled = 0;
9702 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009703 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009705 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706#ifdef FEAT_TITLE
9707 if (need_maketitle)
9708 maketitle();
9709#endif
9710 RedrawingDisabled = r;
9711 p_lz = p;
9712
9713 /* Reset msg_didout, so that a message that's there is overwritten. */
9714 msg_didout = FALSE;
9715 msg_col = 0;
9716
Bram Moolenaar56667a52013-07-17 11:54:28 +02009717 /* No need to wait after an intentional redraw. */
9718 need_wait_return = FALSE;
9719
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 out_flush();
9721}
9722
9723/*
9724 * ":redrawstatus": force redraw of status line(s)
9725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009727ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729 int r = RedrawingDisabled;
9730 int p = p_lz;
9731
9732 RedrawingDisabled = 0;
9733 p_lz = FALSE;
9734 if (eap->forceit)
9735 status_redraw_all();
9736 else
9737 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009738 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 RedrawingDisabled = r;
9740 p_lz = p;
9741 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742}
9743
9744 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009745close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746{
9747 if (redir_fd != NULL)
9748 {
9749 fclose(redir_fd);
9750 redir_fd = NULL;
9751 }
9752#ifdef FEAT_EVAL
9753 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009754 if (redir_vname)
9755 {
9756 var_redir_stop();
9757 redir_vname = 0;
9758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759#endif
9760}
9761
9762#if defined(FEAT_SESSION) && defined(USE_CRNL)
9763# define MKSESSION_NL
9764static int mksession_nl = FALSE; /* use NL only in put_eol() */
9765#endif
9766
9767/*
9768 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9769 */
9770 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009771ex_mkrc(
9772 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773{
9774 FILE *fd;
9775 int failed = FALSE;
9776 char_u *fname;
9777#ifdef FEAT_BROWSE
9778 char_u *browseFile = NULL;
9779#endif
9780#ifdef FEAT_SESSION
9781 int view_session = FALSE;
9782 int using_vdir = FALSE; /* using 'viewdir'? */
9783 char_u *viewFile = NULL;
9784 unsigned *flagp;
9785#endif
9786
9787 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9788 {
9789#ifdef FEAT_SESSION
9790 view_session = TRUE;
9791#else
9792 ex_ni(eap);
9793 return;
9794#endif
9795 }
9796
9797#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009798 /* Use the short file name until ":lcd" is used. We also don't use the
9799 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009800 did_lcd = FALSE;
9801
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9803 if (eap->cmdidx == CMD_mkview
9804 && (*eap->arg == NUL
9805 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9806 {
9807 eap->forceit = TRUE;
9808 fname = get_view_file(*eap->arg);
9809 if (fname == NULL)
9810 return;
9811 viewFile = fname;
9812 using_vdir = TRUE;
9813 }
9814 else
9815#endif
9816 if (*eap->arg != NUL)
9817 fname = eap->arg;
9818 else if (eap->cmdidx == CMD_mkvimrc)
9819 fname = (char_u *)VIMRC_FILE;
9820#ifdef FEAT_SESSION
9821 else if (eap->cmdidx == CMD_mksession)
9822 fname = (char_u *)SESSION_FILE;
9823#endif
9824 else
9825 fname = (char_u *)EXRC_FILE;
9826
9827#ifdef FEAT_BROWSE
9828 if (cmdmod.browse)
9829 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009830 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831# ifdef FEAT_SESSION
9832 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9833 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9834# endif
9835 (char_u *)_("Save Setup"),
9836 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9837 if (browseFile == NULL)
9838 goto theend;
9839 fname = browseFile;
9840 eap->forceit = TRUE; /* since dialog already asked */
9841 }
9842#endif
9843
9844#if defined(FEAT_SESSION) && defined(vim_mkdir)
9845 /* When using 'viewdir' may have to create the directory. */
9846 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009847 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848#endif
9849
9850 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9851 if (fd != NULL)
9852 {
9853#ifdef FEAT_SESSION
9854 if (eap->cmdidx == CMD_mkview)
9855 flagp = &vop_flags;
9856 else
9857 flagp = &ssop_flags;
9858#endif
9859
9860#ifdef MKSESSION_NL
9861 /* "unix" in 'sessionoptions': use NL line separator */
9862 if (view_session && (*flagp & SSOP_UNIX))
9863 mksession_nl = TRUE;
9864#endif
9865
9866 /* Write the version command for :mkvimrc */
9867 if (eap->cmdidx == CMD_mkvimrc)
9868 (void)put_line(fd, "version 6.0");
9869
9870#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009871 if (eap->cmdidx == CMD_mksession)
9872 {
9873 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9874 failed = TRUE;
9875 }
9876
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 if (eap->cmdidx != CMD_mkview)
9878#endif
9879 {
9880 /* Write setting 'compatible' first, because it has side effects.
9881 * For that same reason only do it when needed. */
9882 if (p_cp)
9883 (void)put_line(fd, "if !&cp | set cp | endif");
9884 else
9885 (void)put_line(fd, "if &cp | set nocp | endif");
9886 }
9887
9888#ifdef FEAT_SESSION
9889 if (!view_session
9890 || (eap->cmdidx == CMD_mksession
9891 && (*flagp & SSOP_OPTIONS)))
9892#endif
9893 failed |= (makemap(fd, NULL) == FAIL
9894 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9895
9896#ifdef FEAT_SESSION
9897 if (!failed && view_session)
9898 {
9899 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9900 failed = TRUE;
9901 if (eap->cmdidx == CMD_mksession)
9902 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009903 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904
Bram Moolenaard9462e32011-04-11 21:35:11 +02009905 dirnow = alloc(MAXPATHL);
9906 if (dirnow == NULL)
9907 failed = TRUE;
9908 else
9909 {
9910 /*
9911 * Change to session file's dir.
9912 */
9913 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009915 *dirnow = NUL;
9916 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9917 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009918 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009919 shorten_fnames(TRUE);
9920 }
9921 else if (*dirnow != NUL
9922 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9923 {
9924 if (mch_chdir((char *)globaldir) == 0)
9925 shorten_fnames(TRUE);
9926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927
Bram Moolenaard9462e32011-04-11 21:35:11 +02009928 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929
Bram Moolenaard9462e32011-04-11 21:35:11 +02009930 /* restore original dir */
9931 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009933 {
9934 if (mch_chdir((char *)dirnow) != 0)
9935 EMSG(_(e_prev_dir));
9936 shorten_fnames(TRUE);
9937 }
9938 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 }
9940 }
9941 else
9942 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009943 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9944 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 }
9946 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9947 == FAIL)
9948 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009949 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9950 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009951 if (eap->cmdidx == CMD_mksession)
9952 {
9953 if (put_line(fd, "unlet SessionLoad") == FAIL)
9954 failed = TRUE;
9955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 }
9957#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009958 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9959 failed = TRUE;
9960
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961 failed |= fclose(fd);
9962
9963 if (failed)
9964 EMSG(_(e_write));
9965#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9966 else if (eap->cmdidx == CMD_mksession)
9967 {
9968 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009969 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970
Bram Moolenaard9462e32011-04-11 21:35:11 +02009971 tbuf = alloc(MAXPATHL);
9972 if (tbuf != NULL)
9973 {
9974 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9975 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9976 vim_free(tbuf);
9977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978 }
9979#endif
9980#ifdef MKSESSION_NL
9981 mksession_nl = FALSE;
9982#endif
9983 }
9984
9985#ifdef FEAT_BROWSE
9986theend:
9987 vim_free(browseFile);
9988#endif
9989#ifdef FEAT_SESSION
9990 vim_free(viewFile);
9991#endif
9992}
9993
Bram Moolenaardf177f62005-02-22 08:39:57 +00009994#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9995 || defined(PROTO)
9996 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009997vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009998{
9999 if (vim_mkdir(name, prot) != 0)
10000 {
10001 EMSG2(_("E739: Cannot create directory: %s"), name);
10002 return FAIL;
10003 }
10004 return OK;
10005}
10006#endif
10007
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008/*
10009 * Open a file for writing for an Ex command, with some checks.
10010 * Return file descriptor, or NULL on failure.
10011 */
10012 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010013open_exfile(
10014 char_u *fname,
10015 int forceit,
10016 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017{
10018 FILE *fd;
10019
10020#ifdef UNIX
10021 /* with Unix it is possible to open a directory */
10022 if (mch_isdir(fname))
10023 {
10024 EMSG2(_(e_isadir2), fname);
10025 return NULL;
10026 }
10027#endif
10028 if (!forceit && *mode != 'a' && vim_fexists(fname))
10029 {
10030 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
10031 return NULL;
10032 }
10033
10034 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
10035 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
10036
10037 return fd;
10038}
10039
10040/*
10041 * ":mark" and ":k".
10042 */
10043 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010044ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045{
10046 pos_T pos;
10047
10048 if (*eap->arg == NUL) /* No argument? */
10049 EMSG(_(e_argreq));
10050 else if (eap->arg[1] != NUL) /* more than one character? */
10051 EMSG(_(e_trailing));
10052 else
10053 {
10054 pos = curwin->w_cursor; /* save curwin->w_cursor */
10055 curwin->w_cursor.lnum = eap->line2;
10056 beginline(BL_WHITE | BL_FIX);
10057 if (setmark(*eap->arg) == FAIL) /* set mark */
10058 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10059 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10060 }
10061}
10062
10063/*
10064 * Update w_topline, w_leftcol and the cursor position.
10065 */
10066 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010067update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068{
10069 check_cursor(); /* put cursor on valid line */
10070 update_topline();
10071 if (!curwin->w_p_wrap)
10072 validate_cursor();
10073 update_curswant();
10074}
10075
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010077 * Save the current State and go to Normal mode.
10078 * Return TRUE if the typeahead could be saved.
10079 */
10080 int
10081save_current_state(save_state_T *sst)
10082{
10083 sst->save_msg_scroll = msg_scroll;
10084 sst->save_restart_edit = restart_edit;
10085 sst->save_msg_didout = msg_didout;
10086 sst->save_State = State;
10087 sst->save_insertmode = p_im;
10088 sst->save_finish_op = finish_op;
10089 sst->save_opcount = opcount;
10090
10091 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10092 restart_edit = 0; /* don't go to Insert mode */
10093 p_im = FALSE; /* don't use 'insertmode' */
10094
10095 /*
10096 * Save the current typeahead. This is required to allow using ":normal"
10097 * from an event handler and makes sure we don't hang when the argument
10098 * ends with half a command.
10099 */
10100 save_typeahead(&sst->tabuf);
10101 return sst->tabuf.typebuf_valid;
10102}
10103
10104 void
10105restore_current_state(save_state_T *sst)
10106{
10107 /* Restore the previous typeahead. */
10108 restore_typeahead(&sst->tabuf);
10109
10110 msg_scroll = sst->save_msg_scroll;
10111 restart_edit = sst->save_restart_edit;
10112 p_im = sst->save_insertmode;
10113 finish_op = sst->save_finish_op;
10114 opcount = sst->save_opcount;
10115 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
10116
10117 /* Restore the state (needed when called from a function executed for
10118 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
10119 State = sst->save_State;
10120#ifdef CURSOR_SHAPE
10121 ui_cursor_shape(); /* may show different cursor shape */
10122#endif
10123}
10124
10125/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 * ":normal[!] {commands}": Execute normal mode commands.
10127 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010128 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010129ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130{
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010131 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132#ifdef FEAT_MBYTE
10133 char_u *arg = NULL;
10134 int l;
10135 char_u *p;
10136#endif
10137
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010138 if (ex_normal_lock > 0)
10139 {
10140 EMSG(_(e_secure));
10141 return;
10142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 if (ex_normal_busy >= p_mmd)
10144 {
10145 EMSG(_("E192: Recursive use of :normal too deep"));
10146 return;
10147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148
10149#ifdef FEAT_MBYTE
10150 /*
10151 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10152 * this for the K_SPECIAL leading byte, otherwise special keys will not
10153 * work.
10154 */
10155 if (has_mbyte)
10156 {
10157 int len = 0;
10158
10159 /* Count the number of characters to be escaped. */
10160 for (p = eap->arg; *p != NUL; ++p)
10161 {
10162# ifdef FEAT_GUI
10163 if (*p == CSI) /* leadbyte CSI */
10164 len += 2;
10165# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010166 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10168# ifdef FEAT_GUI
10169 || *p == CSI
10170# endif
10171 )
10172 len += 2;
10173 }
10174 if (len > 0)
10175 {
10176 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10177 if (arg != NULL)
10178 {
10179 len = 0;
10180 for (p = eap->arg; *p != NUL; ++p)
10181 {
10182 arg[len++] = *p;
10183# ifdef FEAT_GUI
10184 if (*p == CSI)
10185 {
10186 arg[len++] = KS_EXTRA;
10187 arg[len++] = (int)KE_CSI;
10188 }
10189# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010190 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191 {
10192 arg[len++] = *++p;
10193 if (*p == K_SPECIAL)
10194 {
10195 arg[len++] = KS_SPECIAL;
10196 arg[len++] = KE_FILLER;
10197 }
10198# ifdef FEAT_GUI
10199 else if (*p == CSI)
10200 {
10201 arg[len++] = KS_EXTRA;
10202 arg[len++] = (int)KE_CSI;
10203 }
10204# endif
10205 }
10206 arg[len] = NUL;
10207 }
10208 }
10209 }
10210 }
10211#endif
10212
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010213 ++ex_normal_busy;
10214 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215 {
10216 /*
10217 * Repeat the :normal command for each line in the range. When no
10218 * range given, execute it just once, without positioning the cursor
10219 * first.
10220 */
10221 do
10222 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223 if (eap->addr_count != 0)
10224 {
10225 curwin->w_cursor.lnum = eap->line1++;
10226 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010227 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228 }
10229
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010230 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231#ifdef FEAT_MBYTE
10232 arg != NULL ? arg :
10233#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010234 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 }
10236 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10237 }
10238
10239 /* Might not return to the main loop when in an event handler. */
10240 update_topline_cursor();
10241
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010242 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010243 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +010010244#ifdef FEAT_MOUSE
10245 setmouse();
10246#endif
10247#ifdef CURSOR_SHAPE
10248 ui_cursor_shape(); /* may show different cursor shape */
10249#endif
10250
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251#ifdef FEAT_MBYTE
10252 vim_free(arg);
10253#endif
10254}
10255
10256/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010257 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 */
10259 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010260ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010262 if (eap->forceit)
10263 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +020010264 /* cursor line can be zero on startup */
10265 if (!curwin->w_cursor.lnum)
10266 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +000010267 coladvance((colnr_T)MAXCOL);
10268 curwin->w_curswant = MAXCOL;
10269 curwin->w_set_curswant = FALSE;
10270 }
10271
Bram Moolenaara40c5002005-01-09 21:16:21 +000010272 /* Ignore the command when already in Insert mode. Inserting an
10273 * expression register that invokes a function can do this. */
10274 if (State & INSERT)
10275 return;
10276
Bram Moolenaar64969662005-12-14 21:59:55 +000010277 if (eap->cmdidx == CMD_startinsert)
10278 restart_edit = 'a';
10279 else if (eap->cmdidx == CMD_startreplace)
10280 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010282 restart_edit = 'V';
10283
10284 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010286 if (eap->cmdidx == CMD_startinsert)
10287 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 curwin->w_curswant = 0; /* avoid MAXCOL */
10289 }
10290}
10291
10292/*
10293 * ":stopinsert"
10294 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010296ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297{
10298 restart_edit = 0;
10299 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010300 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010303/*
10304 * Execute normal mode command "cmd".
10305 * "remap" can be REMAP_NONE or REMAP_YES.
10306 */
10307 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010308exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010309{
Bram Moolenaar25281632016-01-21 23:32:32 +010010310 /* Stuff the argument into the typeahead buffer. */
10311 ins_typebuf(cmd, remap, 0, TRUE, silent);
10312 exec_normal(FALSE);
10313}
Bram Moolenaar25281632016-01-21 23:32:32 +010010314
Bram Moolenaar25281632016-01-21 23:32:32 +010010315/*
10316 * Execute normal_cmd() until there is no typeahead left.
10317 */
10318 void
10319exec_normal(int was_typed)
10320{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010321 oparg_T oa;
10322
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010323 clear_oparg(&oa);
10324 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010325 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10326 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010327 {
10328 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010329 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010330 }
10331}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010332
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333#ifdef FEAT_FIND_ID
10334 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010335ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336{
10337 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10338 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10339 (linenr_T)1, (linenr_T)MAXLNUM);
10340}
10341
Bram Moolenaar4033c552017-09-16 20:54:51 +020010342#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343/*
10344 * ":psearch"
10345 */
10346 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010347ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348{
10349 g_do_tagpreview = p_pvh;
10350 ex_findpat(eap);
10351 g_do_tagpreview = 0;
10352}
10353#endif
10354
10355 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010356ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357{
10358 int whole = TRUE;
10359 long n;
10360 char_u *p;
10361 int action;
10362
10363 switch (cmdnames[eap->cmdidx].cmd_name[2])
10364 {
10365 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10366 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10367 action = ACTION_GOTO;
10368 else
10369 action = ACTION_SHOW;
10370 break;
10371 case 'i': /* ":ilist" and ":dlist" */
10372 action = ACTION_SHOW_ALL;
10373 break;
10374 case 'u': /* ":ijump" and ":djump" */
10375 action = ACTION_GOTO;
10376 break;
10377 default: /* ":isplit" and ":dsplit" */
10378 action = ACTION_SPLIT;
10379 break;
10380 }
10381
10382 n = 1;
10383 if (vim_isdigit(*eap->arg)) /* get count */
10384 {
10385 n = getdigits(&eap->arg);
10386 eap->arg = skipwhite(eap->arg);
10387 }
10388 if (*eap->arg == '/') /* Match regexp, not just whole words */
10389 {
10390 whole = FALSE;
10391 ++eap->arg;
10392 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10393 if (*p)
10394 {
10395 *p++ = NUL;
10396 p = skipwhite(p);
10397
10398 /* Check for trailing illegal characters */
10399 if (!ends_excmd(*p))
10400 eap->errmsg = e_trailing;
10401 else
10402 eap->nextcmd = check_nextcmd(p);
10403 }
10404 }
10405 if (!eap->skip)
10406 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10407 whole, !eap->forceit,
10408 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10409 n, action, eap->line1, eap->line2);
10410}
10411#endif
10412
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413
Bram Moolenaar4033c552017-09-16 20:54:51 +020010414#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415/*
10416 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10417 */
10418 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010419ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010421 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10423}
10424
10425/*
10426 * ":pedit"
10427 */
10428 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010429ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430{
10431 win_T *curwin_save = curwin;
10432
10433 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010434 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010435 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436 do_exedit(eap, NULL);
10437 keep_help_flag = FALSE;
10438 if (curwin != curwin_save && win_valid(curwin_save))
10439 {
10440 /* Return cursor to where we were */
10441 validate_cursor();
10442 redraw_later(VALID);
10443 win_enter(curwin_save, TRUE);
10444 }
10445 g_do_tagpreview = 0;
10446}
Bram Moolenaar4033c552017-09-16 20:54:51 +020010447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448
10449/*
10450 * ":stag", ":stselect" and ":stjump".
10451 */
10452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010453ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454{
10455 postponed_split = -1;
10456 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010457 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10459 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010460 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462
10463/*
10464 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10465 */
10466 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010467ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468{
10469 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10470}
10471
10472 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010473ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474{
10475 int cmd;
10476
10477 switch (name[1])
10478 {
10479 case 'j': cmd = DT_JUMP; /* ":tjump" */
10480 break;
10481 case 's': cmd = DT_SELECT; /* ":tselect" */
10482 break;
10483 case 'p': cmd = DT_PREV; /* ":tprevious" */
10484 break;
10485 case 'N': cmd = DT_PREV; /* ":tNext" */
10486 break;
10487 case 'n': cmd = DT_NEXT; /* ":tnext" */
10488 break;
10489 case 'o': cmd = DT_POP; /* ":pop" */
10490 break;
10491 case 'f': /* ":tfirst" */
10492 case 'r': cmd = DT_FIRST; /* ":trewind" */
10493 break;
10494 case 'l': cmd = DT_LAST; /* ":tlast" */
10495 break;
10496 default: /* ":tag" */
10497#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010498 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010500 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501 return;
10502 }
10503#endif
10504 cmd = DT_TAG;
10505 break;
10506 }
10507
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010508 if (name[0] == 'l')
10509 {
10510#ifndef FEAT_QUICKFIX
10511 ex_ni(eap);
10512 return;
10513#else
10514 cmd = DT_LTAG;
10515#endif
10516 }
10517
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10519 eap->forceit, TRUE);
10520}
10521
10522/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010523 * Check "str" for starting with a special cmdline variable.
10524 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10525 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10526 */
10527 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010528find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010529{
10530 int len;
10531 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010532 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010533 "%",
10534#define SPEC_PERC 0
10535 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +020010536#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010537 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010538#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010539 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010540#define SPEC_CCWORD (SPEC_CWORD + 1)
10541 "<cexpr>", /* expr under cursor */
10542#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010543 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010544#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010545 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010546#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010547 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010548#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010549 "<afile>", /* autocommand file name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010550#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010551 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010552#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010553 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010554#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010555#ifdef FEAT_CLIENTSERVER
10556 "<client>"
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010557# define SPEC_CLIENT (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010558#endif
10559 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010560
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010561 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010562 {
10563 len = (int)STRLEN(spec_str[i]);
10564 if (STRNCMP(src, spec_str[i], len) == 0)
10565 {
10566 *usedlen = len;
10567 return i;
10568 }
10569 }
10570 return -1;
10571}
10572
10573/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574 * Evaluate cmdline variables.
10575 *
10576 * change '%' to curbuf->b_ffname
10577 * '#' to curwin->w_altfile
10578 * '<cword>' to word under the cursor
10579 * '<cWORD>' to WORD under the cursor
10580 * '<cfile>' to path name under the cursor
10581 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010582 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 * '<afile>' to file name for autocommand
10584 * '<abuf>' to buffer number for autocommand
10585 * '<amatch>' to matching name for autocommand
10586 *
10587 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10588 * "" for error without a message) and NULL is returned.
10589 * Returns an allocated string if a valid match was found.
10590 * Returns NULL if no match was found. "usedlen" then still contains the
10591 * number of characters to skip.
10592 */
10593 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010594eval_vars(
10595 char_u *src, /* pointer into commandline */
10596 char_u *srcstart, /* beginning of valid memory for src */
10597 int *usedlen, /* characters after src that are used */
10598 linenr_T *lnump, /* line number for :e command, or NULL */
10599 char_u **errormsg, /* pointer to error message */
10600 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010601 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602{
10603 int i;
10604 char_u *s;
10605 char_u *result;
10606 char_u *resultbuf = NULL;
10607 int resultlen;
10608 buf_T *buf;
10609 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10610 int spec_idx;
10611#ifdef FEAT_MODIFY_FNAME
10612 int skip_mod = FALSE;
10613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615
10616 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010617 if (escaped != NULL)
10618 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619
10620 /*
10621 * Check if there is something to do.
10622 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010623 spec_idx = find_cmdline_var(src, usedlen);
10624 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625 {
10626 *usedlen = 1;
10627 return NULL;
10628 }
10629
10630 /*
10631 * Skip when preceded with a backslash "\%" and "\#".
10632 * Note: In "\\%" the % is also not recognized!
10633 */
10634 if (src > srcstart && src[-1] == '\\')
10635 {
10636 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010637 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638 return NULL;
10639 }
10640
10641 /*
10642 * word or WORD under cursor
10643 */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010644 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
10645 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646 {
Bram Moolenaar65f08472017-09-10 18:16:20 +020010647 resultlen = find_ident_under_cursor(&result,
10648 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
10649 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
10650 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010651 if (resultlen == 0)
10652 {
10653 *errormsg = (char_u *)"";
10654 return NULL;
10655 }
10656 }
10657
10658 /*
10659 * '#': Alternate file name
10660 * '%': Current file name
10661 * File name under the cursor
10662 * File name for autocommand
10663 * and following modifiers
10664 */
10665 else
10666 {
10667 switch (spec_idx)
10668 {
10669 case SPEC_PERC: /* '%': current file */
10670 if (curbuf->b_fname == NULL)
10671 {
10672 result = (char_u *)"";
10673 valid = 0; /* Must have ":p:h" to be valid */
10674 }
10675 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010676 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 break;
10678
10679 case SPEC_HASH: /* '#' or "#99": alternate file */
10680 if (src[1] == '#') /* "##": the argument list */
10681 {
10682 result = arg_all();
10683 resultbuf = result;
10684 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010685 if (escaped != NULL)
10686 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687#ifdef FEAT_MODIFY_FNAME
10688 skip_mod = TRUE;
10689#endif
10690 break;
10691 }
10692 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010693 if (*s == '<') /* "#<99" uses v:oldfiles */
10694 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010696 if (s == src + 2 && src[1] == '-')
10697 /* just a minus sign, don't skip over it */
10698 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699 *usedlen = (int)(s - src); /* length of what we expand */
10700
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010701 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010703 if (*usedlen < 2)
10704 {
10705 /* Should we give an error message for #<text? */
10706 *usedlen = 1;
10707 return NULL;
10708 }
10709#ifdef FEAT_EVAL
10710 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10711 (long)i);
10712 if (result == NULL)
10713 {
10714 *errormsg = (char_u *)"";
10715 return NULL;
10716 }
10717#else
10718 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721 }
10722 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010723 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010724 if (i == 0 && src[1] == '<' && *usedlen > 1)
10725 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010726 buf = buflist_findnr(i);
10727 if (buf == NULL)
10728 {
10729 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10730 return NULL;
10731 }
10732 if (lnump != NULL)
10733 *lnump = ECMD_LAST;
10734 if (buf->b_fname == NULL)
10735 {
10736 result = (char_u *)"";
10737 valid = 0; /* Must have ":p:h" to be valid */
10738 }
10739 else
10740 result = buf->b_fname;
10741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742 break;
10743
10744#ifdef FEAT_SEARCHPATH
10745 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010746 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747 if (result == NULL)
10748 {
10749 *errormsg = (char_u *)"";
10750 return NULL;
10751 }
10752 resultbuf = result; /* remember allocated string */
10753 break;
10754#endif
10755
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756 case SPEC_AFILE: /* file name for autocommand */
10757 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010758 if (result != NULL && !autocmd_fname_full)
10759 {
10760 /* Still need to turn the fname into a full path. It is
10761 * postponed to avoid a delay when <afile> is not used. */
10762 autocmd_fname_full = TRUE;
10763 result = FullName_save(autocmd_fname, FALSE);
10764 vim_free(autocmd_fname);
10765 autocmd_fname = result;
10766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767 if (result == NULL)
10768 {
10769 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10770 return NULL;
10771 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010772 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 break;
10774
10775 case SPEC_ABUF: /* buffer number for autocommand */
10776 if (autocmd_bufnr <= 0)
10777 {
10778 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10779 return NULL;
10780 }
10781 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10782 result = strbuf;
10783 break;
10784
10785 case SPEC_AMATCH: /* match name for autocommand */
10786 result = autocmd_match;
10787 if (result == NULL)
10788 {
10789 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10790 return NULL;
10791 }
10792 break;
10793
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794 case SPEC_SFILE: /* file name for ":so" command */
10795 result = sourcing_name;
10796 if (result == NULL)
10797 {
10798 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10799 return NULL;
10800 }
10801 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010802 case SPEC_SLNUM: /* line in file for ":so" command */
10803 if (sourcing_name == NULL || sourcing_lnum == 0)
10804 {
10805 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10806 return NULL;
10807 }
10808 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10809 result = strbuf;
10810 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811#if defined(FEAT_CLIENTSERVER)
10812 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010813 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10814 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815 result = strbuf;
10816 break;
10817#endif
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020010818 default:
10819 result = (char_u *)""; /* avoid gcc warning */
10820 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010821 }
10822
10823 resultlen = (int)STRLEN(result); /* length of new string */
10824 if (src[*usedlen] == '<') /* remove the file name extension */
10825 {
10826 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828 resultlen = (int)(s - result);
10829 }
10830#ifdef FEAT_MODIFY_FNAME
10831 else if (!skip_mod)
10832 {
10833 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10834 &resultlen);
10835 if (result == NULL)
10836 {
10837 *errormsg = (char_u *)"";
10838 return NULL;
10839 }
10840 }
10841#endif
10842 }
10843
10844 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10845 {
10846 if (valid != VALID_HEAD + VALID_PATH)
10847 /* xgettext:no-c-format */
10848 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10849 else
10850 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10851 result = NULL;
10852 }
10853 else
10854 result = vim_strnsave(result, resultlen);
10855 vim_free(resultbuf);
10856 return result;
10857}
10858
10859/*
10860 * Concatenate all files in the argument list, separated by spaces, and return
10861 * it in one allocated string.
10862 * Spaces and backslashes in the file names are escaped with a backslash.
10863 * Returns NULL when out of memory.
10864 */
10865 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010866arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867{
10868 int len;
10869 int idx;
10870 char_u *retval = NULL;
10871 char_u *p;
10872
10873 /*
10874 * Do this loop two times:
10875 * first time: compute the total length
10876 * second time: concatenate the names
10877 */
10878 for (;;)
10879 {
10880 len = 0;
10881 for (idx = 0; idx < ARGCOUNT; ++idx)
10882 {
10883 p = alist_name(&ARGLIST[idx]);
10884 if (p != NULL)
10885 {
10886 if (len > 0)
10887 {
10888 /* insert a space in between names */
10889 if (retval != NULL)
10890 retval[len] = ' ';
10891 ++len;
10892 }
10893 for ( ; *p != NUL; ++p)
10894 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010895 if (*p == ' '
10896#ifndef BACKSLASH_IN_FILENAME
10897 || *p == '\\'
10898#endif
10899 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 {
10901 /* insert a backslash */
10902 if (retval != NULL)
10903 retval[len] = '\\';
10904 ++len;
10905 }
10906 if (retval != NULL)
10907 retval[len] = *p;
10908 ++len;
10909 }
10910 }
10911 }
10912
10913 /* second time: break here */
10914 if (retval != NULL)
10915 {
10916 retval[len] = NUL;
10917 break;
10918 }
10919
10920 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010921 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922 if (retval == NULL)
10923 break;
10924 }
10925
10926 return retval;
10927}
10928
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929/*
10930 * Expand the <sfile> string in "arg".
10931 *
10932 * Returns an allocated string, or NULL for any error.
10933 */
10934 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010935expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936{
10937 char_u *errormsg;
10938 int len;
10939 char_u *result;
10940 char_u *newres;
10941 char_u *repl;
10942 int srclen;
10943 char_u *p;
10944
10945 result = vim_strsave(arg);
10946 if (result == NULL)
10947 return NULL;
10948
10949 for (p = result; *p; )
10950 {
10951 if (STRNCMP(p, "<sfile>", 7) != 0)
10952 ++p;
10953 else
10954 {
10955 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010956 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957 if (errormsg != NULL)
10958 {
10959 if (*errormsg)
10960 emsg(errormsg);
10961 vim_free(result);
10962 return NULL;
10963 }
10964 if (repl == NULL) /* no match (cannot happen) */
10965 {
10966 p += srclen;
10967 continue;
10968 }
10969 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10970 newres = alloc(len);
10971 if (newres == NULL)
10972 {
10973 vim_free(repl);
10974 vim_free(result);
10975 return NULL;
10976 }
10977 mch_memmove(newres, result, (size_t)(p - result));
10978 STRCPY(newres + (p - result), repl);
10979 len = (int)STRLEN(newres);
10980 STRCAT(newres, p + srclen);
10981 vim_free(repl);
10982 vim_free(result);
10983 result = newres;
10984 p = newres + len; /* continue after the match */
10985 }
10986 }
10987
10988 return result;
10989}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990
10991#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010010992static int ses_winsizes(FILE *fd, int restore_size,
10993 win_T *tab_firstwin);
10994static int ses_win_rec(FILE *fd, frame_T *fr);
10995static frame_T *ses_skipframe(frame_T *fr);
10996static int ses_do_frame(frame_T *fr);
10997static int ses_do_win(win_T *wp);
10998static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
10999static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011000static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001
11002/*
11003 * Write openfile commands for the current buffers to an .exrc file.
11004 * Return FAIL on error, OK otherwise.
11005 */
11006 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011007makeopens(
11008 FILE *fd,
11009 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010{
11011 buf_T *buf;
11012 int only_save_windows = TRUE;
11013 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014 int restore_size = TRUE;
11015 win_T *wp;
11016 char_u *sname;
11017 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011018 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011019 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011020 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011021 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011022 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011023 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024
11025 if (ssop_flags & SSOP_BUFFERS)
11026 only_save_windows = FALSE; /* Save ALL buffers */
11027
11028 /*
11029 * Begin by setting the this_session variable, and then other
11030 * sessionable variables.
11031 */
11032#ifdef FEAT_EVAL
11033 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11034 return FAIL;
11035 if (ssop_flags & SSOP_GLOBALS)
11036 if (store_session_globals(fd) == FAIL)
11037 return FAIL;
11038#endif
11039
11040 /*
11041 * Close all windows but one.
11042 */
11043 if (put_line(fd, "silent only") == FAIL)
11044 return FAIL;
11045
11046 /*
11047 * Now a :cd command to the session directory or the current directory
11048 */
11049 if (ssop_flags & SSOP_SESDIR)
11050 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011051 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11052 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053 return FAIL;
11054 }
11055 else if (ssop_flags & SSOP_CURDIR)
11056 {
11057 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11058 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011059 || fputs("cd ", fd) < 0
11060 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11061 || put_eol(fd) == FAIL)
11062 {
11063 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066 vim_free(sname);
11067 }
11068
11069 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011070 * If there is an empty, unnamed buffer we will wipe it out later.
11071 * Remember the buffer number.
11072 */
11073 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11074 return FAIL;
11075 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11076 return FAIL;
11077 if (put_line(fd, "endif") == FAIL)
11078 return FAIL;
11079
11080 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011081 * Now save the current files, current buffer first.
11082 */
11083 if (put_line(fd, "set shortmess=aoO") == FAIL)
11084 return FAIL;
11085
11086 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011087 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088 {
11089 if (!(only_save_windows && buf->b_nwindows == 0)
11090 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
11091 && buf->b_fname != NULL
11092 && buf->b_p_bl)
11093 {
11094 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11095 : buf->b_wininfo->wi_fpos.lnum) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011096 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097 return FAIL;
11098 }
11099 }
11100
11101 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011102 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11104 return FAIL;
11105
11106 if (ssop_flags & SSOP_RESIZE)
11107 {
11108 /* Note: after the restore we still check it worked!*/
11109 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11110 || put_eol(fd) == FAIL)
11111 return FAIL;
11112 }
11113
11114#ifdef FEAT_GUI
11115 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11116 {
11117 int x, y;
11118
11119 if (gui_mch_get_winpos(&x, &y) == OK)
11120 {
11121 /* Note: after the restore we still check it worked!*/
11122 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11123 return FAIL;
11124 }
11125 }
11126#endif
11127
11128 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011129 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11130 * will be displayed when creating the next tab. That resizes the windows
11131 * in the first tab, which may cause problems. Set 'showtabline' to 2
11132 * temporarily to avoid that.
11133 */
11134 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11135 {
11136 if (put_line(fd, "set stal=2") == FAIL)
11137 return FAIL;
11138 restore_stal = TRUE;
11139 }
11140
11141 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011142 * May repeat putting Windows for each tab, when "tabpages" is in
11143 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011144 * Don't use goto_tabpage(), it may change directory and trigger
11145 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011147 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011148 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011149 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011150 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011151 int need_tabnew = FALSE;
11152 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011153
Bram Moolenaar18144c82006-04-12 21:52:12 +000011154 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011156 tabpage_T *tp = find_tabpage(tabnr);
11157
11158 if (tp == NULL)
11159 break; /* done all tab pages */
11160 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011161 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011162 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011163 tab_topframe = topframe;
11164 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011165 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011166 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011167 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011168 tab_topframe = tp->tp_topframe;
11169 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011170 if (tabnr > 1)
11171 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173
Bram Moolenaar18144c82006-04-12 21:52:12 +000011174 /*
11175 * Before creating the window layout, try loading one file. If this
11176 * is aborted we don't end up with a number of useless windows.
11177 * This may have side effects! (e.g., compressed or network file).
11178 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011179 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011180 {
11181 if (ses_do_win(wp)
11182 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011183 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011184#ifdef FEAT_QUICKFIX
11185 && !bt_nofile(wp->w_buffer)
11186#endif
11187 )
11188 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011189 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011190 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
11191 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011192 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011193 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011194 if (!wp->w_arg_idx_invalid)
11195 edited_win = wp;
11196 break;
11197 }
11198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011200 /* If no file got edited create an empty tab page. */
11201 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11202 return FAIL;
11203
Bram Moolenaar18144c82006-04-12 21:52:12 +000011204 /*
11205 * Save current window layout.
11206 */
11207 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011209 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011211 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11212 return FAIL;
11213 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11214 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215
Bram Moolenaar18144c82006-04-12 21:52:12 +000011216 /*
11217 * Check if window sizes can be restored (no windows omitted).
11218 * Remember the window number of the current window after restoring.
11219 */
11220 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011221 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011222 {
11223 if (ses_do_win(wp))
11224 ++nr;
11225 else
11226 restore_size = FALSE;
11227 if (curwin == wp)
11228 cnr = nr;
11229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230
Bram Moolenaar18144c82006-04-12 21:52:12 +000011231 /* Go to the first window. */
11232 if (put_line(fd, "wincmd t") == FAIL)
11233 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011234
Bram Moolenaar18144c82006-04-12 21:52:12 +000011235 /*
11236 * If more than one window, see if sizes can be restored.
11237 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11238 * resized when moving between windows.
11239 * Do this before restoring the view, so that the topline and the
11240 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011241 * winminheight and winminwidth need to be set to avoid an error if the
11242 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011243 */
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011244 if (put_line(fd, "set winminheight=1 winheight=1 winminwidth=1 winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011245 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011246 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011247 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248
Bram Moolenaar18144c82006-04-12 21:52:12 +000011249 /*
11250 * Restore the view of the window (options, file, cursor, etc.).
11251 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011252 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011253 {
11254 if (!ses_do_win(wp))
11255 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011256 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11257 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011258 return FAIL;
11259 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11260 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011261 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011262 }
11263
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011264 /* The argument index in the first tab page is zero, need to set it in
11265 * each window. For further tab pages it's the window where we do
11266 * "tabedit". */
11267 cur_arg_idx = next_arg_idx;
11268
Bram Moolenaar18144c82006-04-12 21:52:12 +000011269 /*
11270 * Restore cursor to the current window if it's not the first one.
11271 */
11272 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11273 || put_eol(fd) == FAIL))
11274 return FAIL;
11275
11276 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011277 * Restore window sizes again after jumping around in windows, because
11278 * the current window has a minimum size while others may not.
11279 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011280 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011281 return FAIL;
11282
Bram Moolenaar18144c82006-04-12 21:52:12 +000011283 /* Don't continue in another tab page when doing only the current one
11284 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011285 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011286 break;
11287 }
11288
11289 if (ssop_flags & SSOP_TABPAGES)
11290 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011291 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11292 || put_eol(fd) == FAIL)
11293 return FAIL;
11294 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011295 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11296 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011297
Bram Moolenaar9c102382006-05-03 21:26:49 +000011298 /*
11299 * Wipe out an empty unnamed buffer we started in.
11300 */
11301 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11302 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011303 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011304 return FAIL;
11305 if (put_line(fd, "endif") == FAIL)
11306 return FAIL;
11307 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11308 return FAIL;
11309
11310 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11311 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11312 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11313 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011314 /* Re-apply 'winminheight' and 'winminwidth'. */
11315 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11316 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11317 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011318
11319 /*
11320 * Lastly, execute the x.vim file if it exists.
11321 */
11322 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11323 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011324 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325 || put_line(fd, "endif") == FAIL)
11326 return FAIL;
11327
11328 return OK;
11329}
11330
11331 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011332ses_winsizes(
11333 FILE *fd,
11334 int restore_size,
11335 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336{
11337 int n = 0;
11338 win_T *wp;
11339
11340 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11341 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011342 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343 {
11344 if (!ses_do_win(wp))
11345 continue;
11346 ++n;
11347
11348 /* restore height when not full height */
11349 if (wp->w_height + wp->w_status_height < topframe->fr_height
11350 && (fprintf(fd,
11351 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11352 n, (long)wp->w_height, Rows / 2, Rows) < 0
11353 || put_eol(fd) == FAIL))
11354 return FAIL;
11355
11356 /* restore width when not full width */
11357 if (wp->w_width < Columns && (fprintf(fd,
11358 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11359 n, (long)wp->w_width, Columns / 2, Columns) < 0
11360 || put_eol(fd) == FAIL))
11361 return FAIL;
11362 }
11363 }
11364 else
11365 {
11366 /* Just equalise window sizes */
11367 if (put_line(fd, "wincmd =") == FAIL)
11368 return FAIL;
11369 }
11370 return OK;
11371}
11372
11373/*
11374 * Write commands to "fd" to recursively create windows for frame "fr",
11375 * horizontally and vertically split.
11376 * After the commands the last window in the frame is the current window.
11377 * Returns FAIL when writing the commands to "fd" fails.
11378 */
11379 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011380ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381{
11382 frame_T *frc;
11383 int count = 0;
11384
11385 if (fr->fr_layout != FR_LEAF)
11386 {
11387 /* Find first frame that's not skipped and then create a window for
11388 * each following one (first frame is already there). */
11389 frc = ses_skipframe(fr->fr_child);
11390 if (frc != NULL)
11391 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11392 {
11393 /* Make window as big as possible so that we have lots of room
11394 * to split. */
11395 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11396 || put_line(fd, fr->fr_layout == FR_COL
11397 ? "split" : "vsplit") == FAIL)
11398 return FAIL;
11399 ++count;
11400 }
11401
11402 /* Go back to the first window. */
11403 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11404 ? "%dwincmd k" : "%dwincmd h", count) < 0
11405 || put_eol(fd) == FAIL))
11406 return FAIL;
11407
11408 /* Recursively create frames/windows in each window of this column or
11409 * row. */
11410 frc = ses_skipframe(fr->fr_child);
11411 while (frc != NULL)
11412 {
11413 ses_win_rec(fd, frc);
11414 frc = ses_skipframe(frc->fr_next);
11415 /* Go to next window. */
11416 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11417 return FAIL;
11418 }
11419 }
11420 return OK;
11421}
11422
11423/*
11424 * Skip frames that don't contain windows we want to save in the Session.
11425 * Returns NULL when there none.
11426 */
11427 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011428ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429{
11430 frame_T *frc;
11431
11432 for (frc = fr; frc != NULL; frc = frc->fr_next)
11433 if (ses_do_frame(frc))
11434 break;
11435 return frc;
11436}
11437
11438/*
11439 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11440 * the Session.
11441 */
11442 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011443ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444{
11445 frame_T *frc;
11446
11447 if (fr->fr_layout == FR_LEAF)
11448 return ses_do_win(fr->fr_win);
11449 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11450 if (ses_do_frame(frc))
11451 return TRUE;
11452 return FALSE;
11453}
11454
11455/*
11456 * Return non-zero if window "wp" is to be stored in the Session.
11457 */
11458 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011459ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460{
11461 if (wp->w_buffer->b_fname == NULL
11462#ifdef FEAT_QUICKFIX
11463 /* When 'buftype' is "nofile" can't restore the window contents. */
11464 || bt_nofile(wp->w_buffer)
11465#endif
11466 )
11467 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020011468 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469 return (ssop_flags & SSOP_HELP);
11470 return TRUE;
11471}
11472
11473/*
11474 * Write commands to "fd" to restore the view of a window.
11475 * Caller must make sure 'scrolloff' is zero.
11476 */
11477 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011478put_view(
11479 FILE *fd,
11480 win_T *wp,
11481 int add_edit, /* add ":edit" command to view */
11482 unsigned *flagp, /* vop_flags or ssop_flags */
11483 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011484 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485{
11486 win_T *save_curwin;
11487 int f;
11488 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011489 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011490
11491 /* Always restore cursor position for ":mksession". For ":mkview" only
11492 * when 'viewoptions' contains "cursor". */
11493 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11494
11495 /*
11496 * Local argument list.
11497 */
11498 if (wp->w_alist == &global_alist)
11499 {
11500 if (put_line(fd, "argglobal") == FAIL)
11501 return FAIL;
11502 }
11503 else
11504 {
11505 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11506 flagp == &vop_flags
11507 || !(*flagp & SSOP_CURDIR)
11508 || wp->w_localdir != NULL, flagp) == FAIL)
11509 return FAIL;
11510 }
11511
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011512 /* Only when part of a session: restore the argument index. Some
11513 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011514 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011515 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011516 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011517 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518 || put_eol(fd) == FAIL)
11519 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011520 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521 }
11522
11523 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011524 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011525 {
11526 /*
11527 * Load the file.
11528 */
11529 if (wp->w_buffer->b_ffname != NULL
11530#ifdef FEAT_QUICKFIX
11531 && !bt_nofile(wp->w_buffer)
11532#endif
11533 )
11534 {
11535 /*
11536 * Editing a file in this buffer: use ":edit file".
11537 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011538 *
11539 * Note, if a buffer for that file already exists, use :badd to
11540 * edit that buffer, to not lose folding information (:edit resets
11541 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542 */
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011543 if (fputs("if bufexists('", fd) < 0
11544 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11545 || fputs("') | buffer ", fd) < 0
11546 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11547 || fputs(" | else | edit ", fd) < 0
11548 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11549 || fputs(" | endif", fd) < 0
11550 ||
11551 put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011552 return FAIL;
11553 }
11554 else
11555 {
11556 /* No file in this buffer, just make it empty. */
11557 if (put_line(fd, "enew") == FAIL)
11558 return FAIL;
11559#ifdef FEAT_QUICKFIX
11560 if (wp->w_buffer->b_ffname != NULL)
11561 {
11562 /* The buffer does have a name, but it's not a file name. */
11563 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011564 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011565 return FAIL;
11566 }
11567#endif
11568 do_cursor = FALSE;
11569 }
11570 }
11571
11572 /*
11573 * Local mappings and abbreviations.
11574 */
11575 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11576 && makemap(fd, wp->w_buffer) == FAIL)
11577 return FAIL;
11578
11579 /*
11580 * Local options. Need to go to the window temporarily.
11581 * Store only local values when using ":mkview" and when ":mksession" is
11582 * used and 'sessionoptions' doesn't include "options".
11583 * Some folding options are always stored when "folds" is included,
11584 * otherwise the folds would not be restored correctly.
11585 */
11586 save_curwin = curwin;
11587 curwin = wp;
11588 curbuf = curwin->w_buffer;
11589 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11590 f = makeset(fd, OPT_LOCAL,
11591 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11592#ifdef FEAT_FOLDING
11593 else if (*flagp & SSOP_FOLDS)
11594 f = makefoldset(fd);
11595#endif
11596 else
11597 f = OK;
11598 curwin = save_curwin;
11599 curbuf = curwin->w_buffer;
11600 if (f == FAIL)
11601 return FAIL;
11602
11603#ifdef FEAT_FOLDING
11604 /*
11605 * Save Folds when 'buftype' is empty and for help files.
11606 */
11607 if ((*flagp & SSOP_FOLDS)
11608 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011609 && (*wp->w_buffer->b_p_bt == NUL || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011610 {
11611 if (put_folds(fd, wp) == FAIL)
11612 return FAIL;
11613 }
11614#endif
11615
11616 /*
11617 * Set the cursor after creating folds, since that moves the cursor.
11618 */
11619 if (do_cursor)
11620 {
11621
11622 /* Restore the cursor line in the file and relatively in the
11623 * window. Don't use "G", it changes the jumplist. */
11624 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11625 (long)wp->w_cursor.lnum,
11626 (long)(wp->w_cursor.lnum - wp->w_topline),
11627 (long)wp->w_height / 2, (long)wp->w_height) < 0
11628 || put_eol(fd) == FAIL
11629 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11630 || put_line(fd, "exe s:l") == FAIL
11631 || put_line(fd, "normal! zt") == FAIL
11632 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11633 || put_eol(fd) == FAIL)
11634 return FAIL;
11635 /* Restore the cursor column and left offset when not wrapping. */
11636 if (wp->w_cursor.col == 0)
11637 {
11638 if (put_line(fd, "normal! 0") == FAIL)
11639 return FAIL;
11640 }
11641 else
11642 {
11643 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11644 {
11645 if (fprintf(fd,
11646 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011647 (long)wp->w_virtcol + 1,
11648 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011649 (long)wp->w_width / 2, (long)wp->w_width) < 0
11650 || put_eol(fd) == FAIL
11651 || put_line(fd, "if s:c > 0") == FAIL
11652 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011653 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11654 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011655 || put_eol(fd) == FAIL
11656 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011657 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011658 || put_eol(fd) == FAIL
11659 || put_line(fd, "endif") == FAIL)
11660 return FAIL;
11661 }
11662 else
11663 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011664 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011665 || put_eol(fd) == FAIL)
11666 return FAIL;
11667 }
11668 }
11669 }
11670
11671 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010011672 * Local directory, if the current flag is not view options or the "curdir"
11673 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010011675 if (wp->w_localdir != NULL
11676 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677 {
11678 if (fputs("lcd ", fd) < 0
11679 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11680 || put_eol(fd) == FAIL)
11681 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011682 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011683 }
11684
11685 return OK;
11686}
11687
11688/*
11689 * Write an argument list to the session file.
11690 * Returns FAIL if writing fails.
11691 */
11692 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011693ses_arglist(
11694 FILE *fd,
11695 char *cmd,
11696 garray_T *gap,
11697 int fullname, /* TRUE: use full path name */
11698 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011699{
11700 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011701 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702 char_u *s;
11703
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011704 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11705 return FAIL;
11706 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011707 return FAIL;
11708 for (i = 0; i < gap->ga_len; ++i)
11709 {
11710 /* NULL file names are skipped (only happens when out of memory). */
11711 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11712 if (s != NULL)
11713 {
11714 if (fullname)
11715 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011716 buf = alloc(MAXPATHL);
11717 if (buf != NULL)
11718 {
11719 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11720 s = buf;
11721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011722 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010011723 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011724 || ses_put_fname(fd, s, flagp) == FAIL
11725 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011726 {
11727 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011728 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011729 }
11730 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731 }
11732 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011733 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011734}
11735
11736/*
11737 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011738 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011739 * Returns FAIL if writing fails.
11740 */
11741 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011742ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011743{
11744 char_u *name;
11745
11746 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011747 * the session file will be sourced.
11748 * Don't do this for ":mkview", we don't know the current directory.
11749 * Don't do this after ":lcd", we don't keep track of what the current
11750 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751 if (buf->b_sfname != NULL
11752 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011753 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011754#ifdef FEAT_AUTOCHDIR
11755 && !p_acd
11756#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011757 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011758 name = buf->b_sfname;
11759 else
11760 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011761 if (ses_put_fname(fd, name, flagp) == FAIL
11762 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011763 return FAIL;
11764 return OK;
11765}
11766
11767/*
11768 * Write a file name to the session file.
11769 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11770 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011771 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772 */
11773 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011774ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011775{
11776 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011777 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011778 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011779
11780 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011781 if (sname == NULL)
11782 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011783
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011784 if (*flagp & SSOP_SLASH)
11785 {
11786 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011787 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011788 if (*p == '\\')
11789 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011790 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011791
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011792 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011793 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011794 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011795 if (p == NULL)
11796 return FAIL;
11797
11798 /* write the result */
11799 if (fputs((char *)p, fd) < 0)
11800 retval = FAIL;
11801
11802 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803 return retval;
11804}
11805
11806/*
11807 * ":loadview [nr]"
11808 */
11809 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011810ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811{
11812 char_u *fname;
11813
11814 fname = get_view_file(*eap->arg);
11815 if (fname != NULL)
11816 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011817 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818 vim_free(fname);
11819 }
11820}
11821
11822/*
11823 * Get the name of the view file for the current buffer.
11824 */
11825 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011826get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011827{
11828 int len = 0;
11829 char_u *p, *s;
11830 char_u *retval;
11831 char_u *sname;
11832
11833 if (curbuf->b_ffname == NULL)
11834 {
11835 EMSG(_(e_noname));
11836 return NULL;
11837 }
11838 sname = home_replace_save(NULL, curbuf->b_ffname);
11839 if (sname == NULL)
11840 return NULL;
11841
11842 /*
11843 * We want a file name without separators, because we're not going to make
11844 * a directory.
11845 * "normal" path separator -> "=+"
11846 * "=" -> "=="
11847 * ":" path separator -> "=-"
11848 */
11849 for (p = sname; *p; ++p)
11850 if (*p == '=' || vim_ispathsep(*p))
11851 ++len;
11852 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11853 if (retval != NULL)
11854 {
11855 STRCPY(retval, p_vdir);
11856 add_pathsep(retval);
11857 s = retval + STRLEN(retval);
11858 for (p = sname; *p; ++p)
11859 {
11860 if (*p == '=')
11861 {
11862 *s++ = '=';
11863 *s++ = '=';
11864 }
11865 else if (vim_ispathsep(*p))
11866 {
11867 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011868#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011869 if (*p == ':')
11870 *s++ = '-';
11871 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011873 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011874 }
11875 else
11876 *s++ = *p;
11877 }
11878 *s++ = '=';
11879 *s++ = c;
11880 STRCPY(s, ".vim");
11881 }
11882
11883 vim_free(sname);
11884 return retval;
11885}
11886
11887#endif /* FEAT_SESSION */
11888
11889/*
11890 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11891 * Return FAIL for a write error.
11892 */
11893 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011894put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011895{
11896 if (
11897#ifdef USE_CRNL
11898 (
11899# ifdef MKSESSION_NL
11900 !mksession_nl &&
11901# endif
11902 (putc('\r', fd) < 0)) ||
11903#endif
11904 (putc('\n', fd) < 0))
11905 return FAIL;
11906 return OK;
11907}
11908
11909/*
11910 * Write a line to "fd".
11911 * Return FAIL for a write error.
11912 */
11913 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011914put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011915{
11916 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11917 return FAIL;
11918 return OK;
11919}
11920
11921#ifdef FEAT_VIMINFO
11922/*
11923 * ":rviminfo" and ":wviminfo".
11924 */
11925 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011926ex_viminfo(
11927 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928{
11929 char_u *save_viminfo;
11930
11931 save_viminfo = p_viminfo;
11932 if (*p_viminfo == NUL)
11933 p_viminfo = (char_u *)"'100";
11934 if (eap->cmdidx == CMD_rviminfo)
11935 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011936 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11937 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938 EMSG(_("E195: Cannot open viminfo file for reading"));
11939 }
11940 else
11941 write_viminfo(eap->arg, eap->forceit);
11942 p_viminfo = save_viminfo;
11943}
11944#endif
11945
11946#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011947/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011948 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011949 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011950 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011951 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011952dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011953{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954 if (fname == NULL)
11955 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011956 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011957}
11958#endif
11959
11960/*
11961 * ":behave {mswin,xterm}"
11962 */
11963 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011964ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011965{
11966 if (STRCMP(eap->arg, "mswin") == 0)
11967 {
11968 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11969 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11970 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11971 set_option_value((char_u *)"keymodel", 0L,
11972 (char_u *)"startsel,stopsel", 0);
11973 }
11974 else if (STRCMP(eap->arg, "xterm") == 0)
11975 {
11976 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11977 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11978 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11979 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11980 }
11981 else
11982 EMSG2(_(e_invarg2), eap->arg);
11983}
11984
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011985#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11986/*
11987 * Function given to ExpandGeneric() to obtain the possible arguments of the
11988 * ":behave {mswin,xterm}" command.
11989 */
11990 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011991get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011992{
11993 if (idx == 0)
11994 return (char_u *)"mswin";
11995 if (idx == 1)
11996 return (char_u *)"xterm";
11997 return NULL;
11998}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020011999
12000/*
12001 * Function given to ExpandGeneric() to obtain the possible arguments of the
12002 * ":messages {clear}" command.
12003 */
12004 char_u *
12005get_messages_arg(expand_T *xp UNUSED, int idx)
12006{
12007 if (idx == 0)
12008 return (char_u *)"clear";
12009 return NULL;
12010}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012011#endif
12012
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012013 char_u *
12014get_mapclear_arg(expand_T *xp UNUSED, int idx)
12015{
12016 if (idx == 0)
12017 return (char_u *)"<buffer>";
12018 return NULL;
12019}
12020
Bram Moolenaar071d4272004-06-13 20:20:40 +000012021static int filetype_detect = FALSE;
12022static int filetype_plugin = FALSE;
12023static int filetype_indent = FALSE;
12024
12025/*
12026 * ":filetype [plugin] [indent] {on,off,detect}"
12027 * on: Load the filetype.vim file to install autocommands for file types.
12028 * off: Load the ftoff.vim file to remove all autocommands for file types.
12029 * plugin on: load filetype.vim and ftplugin.vim
12030 * plugin off: load ftplugof.vim
12031 * indent on: load filetype.vim and indent.vim
12032 * indent off: load indoff.vim
12033 */
12034 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012035ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012036{
12037 char_u *arg = eap->arg;
12038 int plugin = FALSE;
12039 int indent = FALSE;
12040
12041 if (*eap->arg == NUL)
12042 {
12043 /* Print current status. */
12044 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
12045 filetype_detect ? "ON" : "OFF",
12046 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12047 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12048 return;
12049 }
12050
12051 /* Accept "plugin" and "indent" in any order. */
12052 for (;;)
12053 {
12054 if (STRNCMP(arg, "plugin", 6) == 0)
12055 {
12056 plugin = TRUE;
12057 arg = skipwhite(arg + 6);
12058 continue;
12059 }
12060 if (STRNCMP(arg, "indent", 6) == 0)
12061 {
12062 indent = TRUE;
12063 arg = skipwhite(arg + 6);
12064 continue;
12065 }
12066 break;
12067 }
12068 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12069 {
12070 if (*arg == 'o' || !filetype_detect)
12071 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012072 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073 filetype_detect = TRUE;
12074 if (plugin)
12075 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012076 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012077 filetype_plugin = TRUE;
12078 }
12079 if (indent)
12080 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012081 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082 filetype_indent = TRUE;
12083 }
12084 }
12085 if (*arg == 'd')
12086 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012087 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012088 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089 }
12090 }
12091 else if (STRCMP(arg, "off") == 0)
12092 {
12093 if (plugin || indent)
12094 {
12095 if (plugin)
12096 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012097 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098 filetype_plugin = FALSE;
12099 }
12100 if (indent)
12101 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012102 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012103 filetype_indent = FALSE;
12104 }
12105 }
12106 else
12107 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012108 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012109 filetype_detect = FALSE;
12110 }
12111 }
12112 else
12113 EMSG2(_(e_invarg2), arg);
12114}
12115
12116/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012117 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012118 */
12119 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012120ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012121{
12122 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012123 {
12124 char_u *arg = eap->arg;
12125
12126 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12127 arg += 9;
12128
12129 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12130 if (arg != eap->arg)
12131 did_filetype = FALSE;
12132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012133}
Bram Moolenaar071d4272004-06-13 20:20:40 +000012134
Bram Moolenaar071d4272004-06-13 20:20:40 +000012135 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012136ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012137{
12138#ifdef FEAT_DIGRAPHS
12139 if (*eap->arg != NUL)
12140 putdigraph(eap->arg);
12141 else
12142 listdigraphs();
12143#else
12144 EMSG(_("E196: No digraphs in this version"));
12145#endif
12146}
12147
12148 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012149ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012150{
12151 int flags = 0;
12152
12153 if (eap->cmdidx == CMD_setlocal)
12154 flags = OPT_LOCAL;
12155 else if (eap->cmdidx == CMD_setglobal)
12156 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010012157#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158 if (cmdmod.browse && flags == 0)
12159 ex_options(eap);
12160 else
12161#endif
12162 (void)do_set(eap->arg, flags);
12163}
12164
12165#ifdef FEAT_SEARCH_EXTRA
12166/*
12167 * ":nohlsearch"
12168 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012169 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012170ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012171{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010012172 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012173 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012174}
12175
12176/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012177 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012178 * Sets nextcmd to the start of the next command, if any. Also called when
12179 * skipping commands to find the next command.
12180 */
12181 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012182ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012183{
12184 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012185 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186 char_u *end;
12187 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012188 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012189
12190 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012191 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012192 else
12193 {
12194 EMSG(e_invcmd);
12195 return;
12196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012197
12198 /* First clear any old pattern. */
12199 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012200 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012201
12202 if (ends_excmd(*eap->arg))
12203 end = eap->arg;
12204 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012205 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206 end = eap->arg + 4;
12207 else
12208 {
12209 p = skiptowhite(eap->arg);
12210 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012211 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212 p = skipwhite(p);
12213 if (*p == NUL)
12214 {
12215 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012216 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012217 EMSG2(_(e_invarg2), eap->arg);
12218 return;
12219 }
12220 end = skip_regexp(p + 1, *p, TRUE, NULL);
12221 if (!eap->skip)
12222 {
12223 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12224 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012225 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012226 eap->errmsg = e_trailing;
12227 return;
12228 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012229 if (*end != *p)
12230 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012231 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012232 EMSG2(_(e_invarg2), p);
12233 return;
12234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235
12236 c = *end;
12237 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012238 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012239 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012240 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012241 }
12242 }
12243 eap->nextcmd = find_nextcmd(end);
12244}
12245#endif
12246
12247#ifdef FEAT_CRYPT
12248/*
12249 * ":X": Get crypt key
12250 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012251 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012252ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012254 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012255 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256}
12257#endif
12258
12259#ifdef FEAT_FOLDING
12260 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012261ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262{
12263 if (foldManualAllowed(TRUE))
12264 foldCreate(eap->line1, eap->line2);
12265}
12266
12267 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012268ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012269{
12270 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12271 eap->forceit, FALSE);
12272}
12273
12274 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012275ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012276{
12277 linenr_T lnum;
12278
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012279#ifdef FEAT_CLIPBOARD
12280 start_global_changes();
12281#endif
12282
Bram Moolenaar071d4272004-06-13 20:20:40 +000012283 /* First set the marks for all lines closed/open. */
12284 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12285 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12286 ml_setmarked(lnum);
12287
12288 /* Execute the command on the marked lines. */
12289 global_exe(eap->arg);
12290 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012291#ifdef FEAT_CLIPBOARD
12292 end_global_changes();
12293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294}
12295#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012296
12297# if defined(FEAT_TIMERS) || defined(PROTO)
12298 int
12299get_pressedreturn(void)
12300{
12301 return ex_pressedreturn;
12302}
12303
12304 void
12305set_pressedreturn(int val)
12306{
12307 ex_pressedreturn = val;
12308}
12309#endif