blob: 439467cf112853db4c651bc6e64d79d11fd53622 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
22#ifdef FEAT_USR_CMDS
23typedef struct ucmd
24{
25 char_u *uc_name; /* The command name */
26 long_u uc_argt; /* The argument type */
27 char_u *uc_rep; /* The command's replacement string */
28 long uc_def; /* The default value for a range/count */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int uc_compl; /* completion type */
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +010030 int uc_addr_type; /* The command's address type */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010031# ifdef FEAT_EVAL
32 scid_T uc_scriptID; /* SID where the command was defined */
33# ifdef FEAT_CMDL_COMPL
Bram Moolenaar071d4272004-06-13 20:20:40 +000034 char_u *uc_compl_arg; /* completion argument if any */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010035# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000036# endif
37} ucmd_T;
38
39#define UC_BUFFER 1 /* -buffer: local to current buffer */
40
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000041static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
43#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
44#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
45
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010046static void do_ucmd(exarg_T *eap);
47static void ex_command(exarg_T *eap);
48static void ex_delcommand(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# ifdef FEAT_CMDL_COMPL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010050static char_u *get_user_command_name(int idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051# endif
52
Bram Moolenaar958636c2014-10-21 20:01:58 +020053/* Wether a command index indicates a user command. */
54# define IS_USER_CMDIDX(idx) ((int)(idx) < 0)
55
Bram Moolenaar071d4272004-06-13 20:20:40 +000056#else
57# define ex_command ex_ni
58# define ex_comclear ex_ni
59# define ex_delcommand ex_ni
Bram Moolenaar958636c2014-10-21 20:01:58 +020060/* Wether a command index indicates a user command. */
61# define IS_USER_CMDIDX(idx) (FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000062#endif
63
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010064static int compute_buffer_local_count(int addr_type, int lnum, int local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000065#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010066static char_u *do_one_cmd(char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000067#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010068static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000069static int if_level = 0; /* depth in :if */
70#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010071static void append_command(char_u *cmd);
72static char_u *find_command(exarg_T *eap, int *full);
Bram Moolenaar071d4272004-06-13 20:20:40 +000073
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010074static void ex_abbreviate(exarg_T *eap);
75static void ex_map(exarg_T *eap);
76static void ex_unmap(exarg_T *eap);
77static void ex_mapclear(exarg_T *eap);
78static void ex_abclear(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#ifndef FEAT_MENU
80# define ex_emenu ex_ni
81# define ex_menu ex_ni
82# define ex_menutranslate ex_ni
83#endif
84#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static void ex_autocmd(exarg_T *eap);
86static void ex_doautocmd(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#else
88# define ex_autocmd ex_ni
89# define ex_doautocmd ex_ni
90# define ex_doautoall ex_ni
91#endif
92#ifdef FEAT_LISTCMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010093static void ex_bunload(exarg_T *eap);
94static void ex_buffer(exarg_T *eap);
95static void ex_bmodified(exarg_T *eap);
96static void ex_bnext(exarg_T *eap);
97static void ex_bprevious(exarg_T *eap);
98static void ex_brewind(exarg_T *eap);
99static void ex_blast(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#else
101# define ex_bunload ex_ni
102# define ex_buffer ex_ni
103# define ex_bmodified ex_ni
104# define ex_bnext ex_ni
105# define ex_bprevious ex_ni
106# define ex_brewind ex_ni
107# define ex_blast ex_ni
108# define buflist_list ex_ni
109# define ex_checktime ex_ni
110#endif
111#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
112# define ex_buffer_all ex_ni
113#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100114static char_u *getargcmd(char_u **);
115static char_u *skip_cmd_arg(char_u *p, int rembs);
116static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#ifndef FEAT_QUICKFIX
118# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000119# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120# define ex_cc ex_ni
121# define ex_cnext ex_ni
122# define ex_cfile ex_ni
123# define qf_list ex_ni
124# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200125# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000127# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#endif
129#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
130# define ex_cclose ex_ni
131# define ex_copen ex_ni
132# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +0200133# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000135#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
136# define ex_cexpr ex_ni
137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100139static int check_more(int, int);
140static linenr_T get_address(exarg_T *, char_u **, int addr_type, int skip, int to_other_file);
141static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200142#if !defined(FEAT_PERL) \
143 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
144 || !defined(FEAT_TCL) \
145 || !defined(FEAT_RUBY) \
146 || !defined(FEAT_LUA) \
147 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000148# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100149static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100151static char_u *invalid_range(exarg_T *eap);
152static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000153#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100154static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000155#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100156static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
157static void ex_highlight(exarg_T *eap);
158static void ex_colorscheme(exarg_T *eap);
159static void ex_quit(exarg_T *eap);
160static void ex_cquit(exarg_T *eap);
161static void ex_quit_all(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100163static void ex_close(exarg_T *eap);
164static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
165static void ex_only(exarg_T *eap);
166static void ex_resize(exarg_T *eap);
167static void ex_stag(exarg_T *eap);
168static void ex_tabclose(exarg_T *eap);
169static void ex_tabonly(exarg_T *eap);
170static void ex_tabnext(exarg_T *eap);
171static void ex_tabmove(exarg_T *eap);
172static void ex_tabs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173#else
174# define ex_close ex_ni
175# define ex_only ex_ni
176# define ex_all ex_ni
177# define ex_resize ex_ni
178# define ex_splitview ex_ni
179# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000180# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000181# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000182# define ex_tabs ex_ni
183# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000184# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185#endif
186#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100187static void ex_pclose(exarg_T *eap);
188static void ex_ptag(exarg_T *eap);
189static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190#else
191# define ex_pclose ex_ni
192# define ex_ptag ex_ni
193# define ex_pedit ex_ni
194#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100195static void ex_hide(exarg_T *eap);
196static void ex_stop(exarg_T *eap);
197static void ex_exit(exarg_T *eap);
198static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100200static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#else
202# define ex_goto ex_ni
203#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100204static void ex_shell(exarg_T *eap);
205static void ex_preserve(exarg_T *eap);
206static void ex_recover(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207#ifndef FEAT_LISTCMDS
208# define ex_argedit ex_ni
209# define ex_argadd ex_ni
210# define ex_argdelete ex_ni
211# define ex_listdo ex_ni
212#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100213static void ex_mode(exarg_T *eap);
214static void ex_wrongmodifier(exarg_T *eap);
215static void ex_find(exarg_T *eap);
216static void ex_open(exarg_T *eap);
217static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
219# define ex_drop ex_ni
220#endif
221#ifndef FEAT_GUI
222# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#endif
225#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100226static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227#else
228# define ex_tearoff ex_ni
229#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000230#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100231static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232#else
233# define ex_popup ex_ni
234#endif
235#ifndef FEAT_GUI_MSWIN
236# define ex_simalt ex_ni
237#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000238#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239# define gui_mch_find_dialog ex_ni
240# define gui_mch_replace_dialog ex_ni
241#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000242#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243# define ex_helpfind ex_ni
244#endif
245#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100246# define ex_cscope ex_ni
247# define ex_scscope ex_ni
248# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249#endif
250#ifndef FEAT_SYN_HL
251# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200252# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000253#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200254#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200255# define ex_syntime ex_ni
256#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000257#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000258# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000259# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000260# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000261# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000262# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000263#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200264#ifndef FEAT_PERSISTENT_UNDO
265# define ex_rundo ex_ni
266# define ex_wundo ex_ni
267#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200268#ifndef FEAT_LUA
269# define ex_lua ex_script_ni
270# define ex_luado ex_ni
271# define ex_luafile ex_ni
272#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000273#ifndef FEAT_MZSCHEME
274# define ex_mzscheme ex_script_ni
275# define ex_mzfile ex_ni
276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#ifndef FEAT_PERL
278# define ex_perl ex_script_ni
279# define ex_perldo ex_ni
280#endif
281#ifndef FEAT_PYTHON
282# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200283# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284# define ex_pyfile ex_ni
285#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200286#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200287# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200288# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200289# define ex_py3file ex_ni
290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291#ifndef FEAT_TCL
292# define ex_tcl ex_script_ni
293# define ex_tcldo ex_ni
294# define ex_tclfile ex_ni
295#endif
296#ifndef FEAT_RUBY
297# define ex_ruby ex_script_ni
298# define ex_rubydo ex_ni
299# define ex_rubyfile ex_ni
300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301#ifndef FEAT_KEYMAP
302# define ex_loadkeymap ex_ni
303#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100304static void ex_swapname(exarg_T *eap);
305static void ex_syncbind(exarg_T *eap);
306static void ex_read(exarg_T *eap);
307static void ex_pwd(exarg_T *eap);
308static void ex_equal(exarg_T *eap);
309static void ex_sleep(exarg_T *eap);
310static void do_exmap(exarg_T *eap, int isabbrev);
311static void ex_winsize(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100313static void ex_wincmd(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314#else
315# define ex_wincmd ex_ni
316#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000317#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319#else
320# define ex_winpos ex_ni
321#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100322static void ex_operators(exarg_T *eap);
323static void ex_put(exarg_T *eap);
324static void ex_copymove(exarg_T *eap);
325static void ex_submagic(exarg_T *eap);
326static void ex_join(exarg_T *eap);
327static void ex_at(exarg_T *eap);
328static void ex_bang(exarg_T *eap);
329static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200330#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100331static void ex_wundo(exarg_T *eap);
332static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200333#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100334static void ex_redo(exarg_T *eap);
335static void ex_later(exarg_T *eap);
336static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100337static void ex_redrawstatus(exarg_T *eap);
338static void close_redir(void);
339static void ex_mkrc(exarg_T *eap);
340static void ex_mark(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341#ifdef FEAT_USR_CMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100342static char_u *uc_fun_cmd(void);
343static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100345static void ex_startinsert(exarg_T *eap);
346static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100348static void ex_checkpath(exarg_T *eap);
349static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350#else
351# define ex_findpat ex_ni
352# define ex_checkpath ex_ni
353#endif
354#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100355static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356#else
357# define ex_psearch ex_ni
358#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100359static void ex_tag(exarg_T *eap);
360static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361#ifndef FEAT_EVAL
362# define ex_scriptnames ex_ni
363# define ex_finish ex_ni
364# define ex_echo ex_ni
365# define ex_echohl ex_ni
366# define ex_execute ex_ni
367# define ex_call ex_ni
368# define ex_if ex_ni
369# define ex_endif ex_ni
370# define ex_else ex_ni
371# define ex_while ex_ni
372# define ex_continue ex_ni
373# define ex_break ex_ni
374# define ex_endwhile ex_ni
375# define ex_throw ex_ni
376# define ex_try ex_ni
377# define ex_catch ex_ni
378# define ex_finally ex_ni
379# define ex_endtry ex_ni
380# define ex_endfunction ex_ni
381# define ex_let ex_ni
382# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000383# define ex_lockvar ex_ni
384# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385# define ex_function ex_ni
386# define ex_delfunction ex_ni
387# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000388# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100390static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100392static int makeopens(FILE *fd, char_u *dirnow);
393static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
394static void ex_loadview(exarg_T *eap);
395static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000396static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397#else
398# define ex_loadview ex_ni
399#endif
400#ifndef FEAT_EVAL
401# define ex_compiler ex_ni
402#endif
403#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100404static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405#else
406# define ex_viminfo ex_ni
407#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100408static void ex_behave(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100410static void ex_filetype(exarg_T *eap);
411static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412#else
413# define ex_filetype ex_ni
414# define ex_setfiletype ex_ni
415#endif
416#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000417# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418# define ex_diffpatch ex_ni
419# define ex_diffgetput ex_ni
420# define ex_diffsplit ex_ni
421# define ex_diffthis ex_ni
422# define ex_diffupdate ex_ni
423#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100424static void ex_digraphs(exarg_T *eap);
425static void ex_set(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
427# define ex_options ex_ni
428#endif
429#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100430static void ex_nohlsearch(exarg_T *eap);
431static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432#else
433# define ex_nohlsearch ex_ni
434# define ex_match ex_ni
435#endif
436#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100437static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438#else
439# define ex_X ex_ni
440#endif
441#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100442static void ex_fold(exarg_T *eap);
443static void ex_foldopen(exarg_T *eap);
444static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445#else
446# define ex_fold ex_ni
447# define ex_foldopen ex_ni
448# define ex_folddo ex_ni
449#endif
450#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
451 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
452# define ex_language ex_ni
453#endif
454#ifndef FEAT_SIGNS
455# define ex_sign ex_ni
456#endif
457#ifndef FEAT_SUN_WORKSHOP
458# define ex_wsverb ex_ni
459#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000460#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200461# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000462# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200463# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465
466#ifndef FEAT_EVAL
467# define ex_debug ex_ni
468# define ex_breakadd ex_ni
469# define ex_debuggreedy ex_ni
470# define ex_breakdel ex_ni
471# define ex_breaklist ex_ni
472#endif
473
474#ifndef FEAT_CMDHIST
475# define ex_history ex_ni
476#endif
477#ifndef FEAT_JUMPLIST
478# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200479# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480# define ex_changes ex_ni
481#endif
482
Bram Moolenaar05159a02005-02-26 23:04:13 +0000483#ifndef FEAT_PROFILE
484# define ex_profile ex_ni
485#endif
486
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487/*
488 * Declare cmdnames[].
489 */
490#define DO_DECLARE_EXCMD
491#include "ex_cmds.h"
492
493/*
494 * Table used to quickly search for a command, based on its first character.
495 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000496static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497{
498 CMD_append,
499 CMD_buffer,
500 CMD_change,
501 CMD_delete,
502 CMD_edit,
503 CMD_file,
504 CMD_global,
505 CMD_help,
506 CMD_insert,
507 CMD_join,
508 CMD_k,
509 CMD_list,
510 CMD_move,
511 CMD_next,
512 CMD_open,
513 CMD_print,
514 CMD_quit,
515 CMD_read,
516 CMD_substitute,
517 CMD_t,
518 CMD_undo,
519 CMD_vglobal,
520 CMD_write,
521 CMD_xit,
522 CMD_yank,
523 CMD_z,
524 CMD_bang
525};
526
527static char_u dollar_command[2] = {'$', 0};
528
529
530#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000531/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532typedef struct
533{
534 char_u *line; /* command line */
535 linenr_T lnum; /* sourcing_lnum of the line */
536} wcmd_T;
537
538/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000539 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000541 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000543struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544{
545 garray_T *lines_gap; /* growarray with line info */
546 int current_line; /* last read line from growarray */
547 int repeating; /* TRUE when looping a second time */
548 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100549 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 void *cookie;
551};
552
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100553static char_u *get_loop_line(int c, void *cookie, int indent);
554static int store_loop_line(garray_T *gap, char_u *line);
555static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000556
557/* Struct to save a few things while debugging. Used in do_cmdline() only. */
558struct dbg_stuff
559{
560 int trylevel;
561 int force_abort;
562 except_T *caught_stack;
563 char_u *vv_exception;
564 char_u *vv_throwpoint;
565 int did_emsg;
566 int got_int;
567 int did_throw;
568 int need_rethrow;
569 int check_cstack;
570 except_T *current_exception;
571};
572
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100573static void save_dbg_stuff(struct dbg_stuff *dsp);
574static void restore_dbg_stuff(struct dbg_stuff *dsp);
Bram Moolenaared203462004-06-16 11:19:22 +0000575
576 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100577save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000578{
579 dsp->trylevel = trylevel; trylevel = 0;
580 dsp->force_abort = force_abort; force_abort = FALSE;
581 dsp->caught_stack = caught_stack; caught_stack = NULL;
582 dsp->vv_exception = v_exception(NULL);
583 dsp->vv_throwpoint = v_throwpoint(NULL);
584
585 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
586 dsp->did_emsg = did_emsg; did_emsg = FALSE;
587 dsp->got_int = got_int; got_int = FALSE;
588 dsp->did_throw = did_throw; did_throw = FALSE;
589 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
590 dsp->check_cstack = check_cstack; check_cstack = FALSE;
591 dsp->current_exception = current_exception; current_exception = NULL;
592}
593
594 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100595restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000596{
597 suppress_errthrow = FALSE;
598 trylevel = dsp->trylevel;
599 force_abort = dsp->force_abort;
600 caught_stack = dsp->caught_stack;
601 (void)v_exception(dsp->vv_exception);
602 (void)v_throwpoint(dsp->vv_throwpoint);
603 did_emsg = dsp->did_emsg;
604 got_int = dsp->got_int;
605 did_throw = dsp->did_throw;
606 need_rethrow = dsp->need_rethrow;
607 check_cstack = dsp->check_cstack;
608 current_exception = dsp->current_exception;
609}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610#endif
611
612
613/*
614 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
615 * command is given.
616 */
617 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100618do_exmode(
619 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620{
621 int save_msg_scroll;
622 int prev_msg_row;
623 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000624 int changedtick;
625
626 if (improved)
627 exmode_active = EXMODE_VIM;
628 else
629 exmode_active = EXMODE_NORMAL;
630 State = NORMAL;
631
632 /* When using ":global /pat/ visual" and then "Q" we return to continue
633 * the :global command. */
634 if (global_busy)
635 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636
637 save_msg_scroll = msg_scroll;
638 ++RedrawingDisabled; /* don't redisplay the window */
639 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640#ifdef FEAT_GUI
641 /* Ignore scrollbar and mouse events in Ex mode */
642 ++hold_gui_events;
643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644
645 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
646 while (exmode_active)
647 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000648 /* Check for a ":normal" command and no more characters left. */
649 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
650 {
651 exmode_active = FALSE;
652 break;
653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 msg_scroll = TRUE;
655 need_wait_return = FALSE;
656 ex_pressedreturn = FALSE;
657 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000658 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 prev_msg_row = msg_row;
660 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 if (improved)
662 {
663 cmdline_row = msg_row;
664 do_cmdline(NULL, getexline, NULL, 0);
665 }
666 else
667 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
668 lines_left = Rows - 1;
669
Bram Moolenaardf177f62005-02-22 08:39:57 +0000670 if ((prev_line != curwin->w_cursor.lnum
671 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000673 if (curbuf->b_ml.ml_flags & ML_EMPTY)
674 EMSG(_(e_emptybuf));
675 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000677 if (ex_pressedreturn)
678 {
679 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000680 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000681 msg_row = prev_msg_row;
682 if (prev_msg_row == Rows - 1)
683 msg_row--;
684 }
685 msg_col = 0;
686 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
687 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000690 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
691 {
692 if (curbuf->b_ml.ml_flags & ML_EMPTY)
693 EMSG(_(e_emptybuf));
694 else
695 EMSG(_("E501: At end-of-file"));
696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 }
698
699#ifdef FEAT_GUI
700 --hold_gui_events;
701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 --RedrawingDisabled;
703 --no_wait_return;
704 update_screen(CLEAR);
705 need_wait_return = FALSE;
706 msg_scroll = save_msg_scroll;
707}
708
709/*
710 * Execute a simple command line. Used for translated commands like "*".
711 */
712 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100713do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714{
715 return do_cmdline(cmd, NULL, NULL,
716 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
717}
718
719/*
720 * do_cmdline(): execute one Ex command line
721 *
722 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100723 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 * 2. Split up in parts separated with '|'.
725 *
726 * This function can be called recursively!
727 *
728 * flags:
729 * DOCMD_VERBOSE - The command will be included in the error message.
730 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100731 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 * DOCMD_KEYTYPED - Don't reset KeyTyped.
733 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
734 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
735 *
736 * return FAIL if cmdline could not be executed, OK otherwise
737 */
738 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100739do_cmdline(
740 char_u *cmdline,
741 char_u *(*fgetline)(int, void *, int),
742 void *cookie, /* argument for fgetline() */
743 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744{
745 char_u *next_cmdline; /* next cmd to execute */
746 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100747 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 static int recursive = 0; /* recursive depth */
749 int msg_didout_before_start = 0;
750 int count = 0; /* line number count */
751 int did_inc = FALSE; /* incremented RedrawingDisabled */
752 int retval = OK;
753#ifdef FEAT_EVAL
754 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000755 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 int current_line = 0; /* active line in lines_ga */
757 char_u *fname = NULL; /* function or script name */
758 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
759 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000760 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 int initial_trylevel;
762 struct msglist **saved_msg_list = NULL;
763 struct msglist *private_msg_list;
764
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100765 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100766 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000768 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000770 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100772# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773# define cmd_cookie cookie
774#endif
775 static int call_depth = 0; /* recursiveness */
776
777#ifdef FEAT_EVAL
778 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
779 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000780 * This ensures that the do_errthrow() call in do_one_cmd() does not
781 * combine the messages stored by an earlier invocation of do_one_cmd()
782 * with the command name of the later one. This would happen when
783 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 saved_msg_list = msg_list;
785 msg_list = &private_msg_list;
786 private_msg_list = NULL;
787#endif
788
789 /* It's possible to create an endless loop with ":execute", catch that
790 * here. The value of 200 allows nested function calls, ":source", etc. */
791 if (call_depth == 200)
792 {
793 EMSG(_("E169: Command too recursive"));
794#ifdef FEAT_EVAL
795 /* When converting to an exception, we do not include the command name
796 * since this is not an error of the specific command. */
797 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
798 msg_list = saved_msg_list;
799#endif
800 return FAIL;
801 }
802 ++call_depth;
803
804#ifdef FEAT_EVAL
805 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000806 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 cstack.cs_trylevel = 0;
808 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000809 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
811
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100812 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
814 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100815 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000816 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 ++ex_nesting_level;
818
819 /* Get the function or script name and the address where the next breakpoint
820 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000821 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 {
823 fname = func_name(real_cookie);
824 breakpoint = func_breakpoint(real_cookie);
825 dbg_tick = func_dbg_tick(real_cookie);
826 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100827 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 {
829 fname = sourcing_name;
830 breakpoint = source_breakpoint(real_cookie);
831 dbg_tick = source_dbg_tick(real_cookie);
832 }
833
834 /*
835 * Initialize "force_abort" and "suppress_errthrow" at the top level.
836 */
837 if (!recursive)
838 {
839 force_abort = FALSE;
840 suppress_errthrow = FALSE;
841 }
842
843 /*
844 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000845 * exception handling (used when debugging). Otherwise clear it to avoid
846 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000848 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000849 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000850 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200851 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852
853 initial_trylevel = trylevel;
854
855 /*
856 * "did_throw" will be set to TRUE when an exception is being thrown.
857 */
858 did_throw = FALSE;
859#endif
860 /*
861 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000862 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 * If force_abort is set, we cancel everything.
864 */
865 did_emsg = FALSE;
866
867 /*
868 * KeyTyped is only set when calling vgetc(). Reset it here when not
869 * calling vgetc() (sourced command lines).
870 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100871 if (!(flags & DOCMD_KEYTYPED)
872 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 KeyTyped = FALSE;
874
875 /*
876 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000877 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 * - for multiple commands on one line, separated with '|'
879 * - when repeating until there are no more lines (for ":source")
880 */
881 next_cmdline = cmdline;
882 do
883 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000884#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100885 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000886#endif
887
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000888 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (next_cmdline == NULL
890#ifdef FEAT_EVAL
891 && !force_abort
892 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000893 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894#endif
895 )
896 did_emsg = FALSE;
897
898 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000899 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100900 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 * 3. If a line is given: Make a copy, so we can mess with it.
902 */
903
904#ifdef FEAT_EVAL
905 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000906 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 {
908 /* Each '|' separated command is stored separately in lines_ga, to
909 * be able to jump to it. Don't use next_cmdline now. */
910 vim_free(cmdline_copy);
911 cmdline_copy = NULL;
912
913 /* Check if a function has returned or, unless it has an unclosed
914 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000915 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000917# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000918 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000919 func_line_end(real_cookie);
920# endif
921 if (func_has_ended(real_cookie))
922 {
923 retval = FAIL;
924 break;
925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000927#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000928 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100929 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000930 script_line_end();
931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932
933 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100934 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 {
936 retval = FAIL;
937 break;
938 }
939
940 /* If breakpoints have been added/deleted need to check for it. */
941 if (breakpoint != NULL && dbg_tick != NULL
942 && *dbg_tick != debug_tick)
943 {
944 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100945 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 fname, sourcing_lnum);
947 *dbg_tick = debug_tick;
948 }
949
950 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
951 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
952
953 /* Did we encounter a breakpoint? */
954 if (breakpoint != NULL && *breakpoint != 0
955 && *breakpoint <= sourcing_lnum)
956 {
957 dbg_breakpoint(fname, sourcing_lnum);
958 /* Find next breakpoint. */
959 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100960 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 fname, sourcing_lnum);
962 *dbg_tick = debug_tick;
963 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000964# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000965 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000966 {
967 if (getline_is_func)
968 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100969 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000970 script_line_start();
971 }
972# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 }
974
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000975 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000977 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100978 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 * below, so that it stores lines in or reads them from
980 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000981 * while/for loop. */
982 cmd_getline = get_loop_line;
983 cmd_cookie = (void *)&cmd_loop_cookie;
984 cmd_loop_cookie.lines_gap = &lines_ga;
985 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100986 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000987 cmd_loop_cookie.cookie = cookie;
988 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 }
990 else
991 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100992 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 cmd_cookie = cookie;
994 }
995#endif
996
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100997 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 if (next_cmdline == NULL)
999 {
1000 /*
1001 * Need to set msg_didout for the first line after an ":if",
1002 * otherwise the ":if" will be overwritten.
1003 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001004 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001006 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007#ifdef FEAT_EVAL
1008 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1009#else
1010 0
1011#endif
1012 )) == NULL)
1013 {
1014 /* Don't call wait_return for aborted command line. The NULL
1015 * returned for the end of a sourced file or executed function
1016 * doesn't do this. */
1017 if (KeyTyped && !(flags & DOCMD_REPEAT))
1018 need_wait_return = FALSE;
1019 retval = FAIL;
1020 break;
1021 }
1022 used_getline = TRUE;
1023
1024 /*
1025 * Keep the first typed line. Clear it when more lines are typed.
1026 */
1027 if (flags & DOCMD_KEEPLINE)
1028 {
1029 vim_free(repeat_cmdline);
1030 if (count == 0)
1031 repeat_cmdline = vim_strsave(next_cmdline);
1032 else
1033 repeat_cmdline = NULL;
1034 }
1035 }
1036
1037 /* 3. Make a copy of the command so we can mess with it. */
1038 else if (cmdline_copy == NULL)
1039 {
1040 next_cmdline = vim_strsave(next_cmdline);
1041 if (next_cmdline == NULL)
1042 {
1043 EMSG(_(e_outofmem));
1044 retval = FAIL;
1045 break;
1046 }
1047 }
1048 cmdline_copy = next_cmdline;
1049
1050#ifdef FEAT_EVAL
1051 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001052 * Save the current line when inside a ":while" or ":for", and when
1053 * the command looks like a ":while" or ":for", because we may need it
1054 * later. When there is a '|' and another command, it is stored
1055 * separately, because we need to be able to jump back to it from an
1056 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001058 if (current_line == lines_ga.ga_len
1059 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001061 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 {
1063 retval = FAIL;
1064 break;
1065 }
1066 }
1067 did_endif = FALSE;
1068#endif
1069
1070 if (count++ == 0)
1071 {
1072 /*
1073 * All output from the commands is put below each other, without
1074 * waiting for a return. Don't do this when executing commands
1075 * from a script or when being called recursive (e.g. for ":e
1076 * +command file").
1077 */
1078 if (!(flags & DOCMD_NOWAIT) && !recursive)
1079 {
1080 msg_didout_before_start = msg_didout;
1081 msg_didany = FALSE; /* no output yet */
1082 msg_start();
1083 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001084 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 ++RedrawingDisabled;
1086 did_inc = TRUE;
1087 }
1088 }
1089
1090 if (p_verbose >= 15 && sourcing_name != NULL)
1091 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001093 verbose_enter_scroll();
1094
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 smsg((char_u *)_("line %ld: %s"),
1096 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001097 if (msg_silent == 0)
1098 msg_puts((char_u *)"\n"); /* don't overwrite this */
1099
1100 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 --no_wait_return;
1102 }
1103
1104 /*
1105 * 2. Execute one '|' separated command.
1106 * do_one_cmd() will return NULL if there is no trailing '|'.
1107 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1108 */
1109 ++recursive;
1110 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1111#ifdef FEAT_EVAL
1112 &cstack,
1113#endif
1114 cmd_getline, cmd_cookie);
1115 --recursive;
1116
1117#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001118 if (cmd_cookie == (void *)&cmd_loop_cookie)
1119 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001121 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122#endif
1123
1124 if (next_cmdline == NULL)
1125 {
1126 vim_free(cmdline_copy);
1127 cmdline_copy = NULL;
1128#ifdef FEAT_CMDHIST
1129 /*
1130 * If the command was typed, remember it for the ':' register.
1131 * Do this AFTER executing the command to make :@: work.
1132 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001133 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 && new_last_cmdline != NULL)
1135 {
1136 vim_free(last_cmdline);
1137 last_cmdline = new_last_cmdline;
1138 new_last_cmdline = NULL;
1139 }
1140#endif
1141 }
1142 else
1143 {
1144 /* need to copy the command after the '|' to cmdline_copy, for the
1145 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001146 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 next_cmdline = cmdline_copy;
1148 }
1149
1150
1151#ifdef FEAT_EVAL
1152 /* reset did_emsg for a function that is not aborted by an error */
1153 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001154 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 && !func_has_abort(real_cookie))
1156 did_emsg = FALSE;
1157
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001158 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 {
1160 ++current_line;
1161
1162 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001163 * An ":endwhile", ":endfor" and ":continue" is handled here.
1164 * If we were executing commands, jump back to the ":while" or
1165 * ":for".
1166 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001168 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001170 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001172 /* Jump back to the matching ":while" or ":for". Be careful
1173 * not to use a cs_line[] from an entry that isn't a ":while"
1174 * or ":for": It would make "current_line" invalid and can
1175 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 if (!did_emsg && !got_int && !did_throw
1177 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001178 && (cstack.cs_flags[cstack.cs_idx]
1179 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 && cstack.cs_line[cstack.cs_idx] >= 0
1181 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1182 {
1183 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001184 /* remember we jumped there */
1185 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 line_breakcheck(); /* check if CTRL-C typed */
1187
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001188 /* Check for the next breakpoint at or after the ":while"
1189 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 if (breakpoint != NULL)
1191 {
1192 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001193 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 fname,
1195 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1196 *dbg_tick = debug_tick;
1197 }
1198 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001199 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001201 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001203 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1204 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 }
1206 }
1207
1208 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001209 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001211 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001213 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1215 }
1216 }
1217
1218 /*
1219 * When not inside any ":while" loop, clear remembered lines.
1220 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001221 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 {
1223 if (lines_ga.ga_len > 0)
1224 {
1225 sourcing_lnum =
1226 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1227 free_cmdlines(&lines_ga);
1228 }
1229 current_line = 0;
1230 }
1231
1232 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001233 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1234 * being restored at the ":endtry". Reset them here and set the
1235 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1236 * This includes the case where a missing ":endif", ":endwhile" or
1237 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001239 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001241 cstack.cs_lflags &= ~CSL_HAD_FINA;
1242 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1243 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 did_throw ? (void *)current_exception : NULL);
1245 did_emsg = got_int = did_throw = FALSE;
1246 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1247 }
1248
1249 /* Update global "trylevel" for recursive calls to do_cmdline() from
1250 * within this loop. */
1251 trylevel = initial_trylevel + cstack.cs_trylevel;
1252
1253 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001254 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 * files) is aborted because of an error, an interrupt, or an uncaught
1256 * exception, cancel everything. If it is left normally, reset
1257 * force_abort to get the non-EH compatible abortion behavior for
1258 * the rest of the script.
1259 */
1260 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1261 force_abort = FALSE;
1262
1263 /* Convert an interrupt to an exception if appropriate. */
1264 (void)do_intthrow(&cstack);
1265#endif /* FEAT_EVAL */
1266
1267 }
1268 /*
1269 * Continue executing command lines when:
1270 * - no CTRL-C typed, no aborting error, no exception thrown or try
1271 * conditionals need to be checked for executing finally clauses or
1272 * catching an interrupt exception
1273 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001274 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 * looping for ":source" command or function call.
1276 */
1277 while (!((got_int
1278#ifdef FEAT_EVAL
1279 || (did_emsg && force_abort) || did_throw
1280#endif
1281 )
1282#ifdef FEAT_EVAL
1283 && cstack.cs_trylevel == 0
1284#endif
1285 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001286 && !(did_emsg
1287#ifdef FEAT_EVAL
1288 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001289 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001290 * the :endtry to be missed. */
1291 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1292#endif
1293 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001294 && (getline_equal(fgetline, cookie, getexmodeline)
1295 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 && (next_cmdline != NULL
1297#ifdef FEAT_EVAL
1298 || cstack.cs_idx >= 0
1299#endif
1300 || (flags & DOCMD_REPEAT)));
1301
1302 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001303 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304#ifdef FEAT_EVAL
1305 free_cmdlines(&lines_ga);
1306 ga_clear(&lines_ga);
1307
1308 if (cstack.cs_idx >= 0)
1309 {
1310 /*
1311 * If a sourced file or executed function ran to its end, report the
1312 * unclosed conditional.
1313 */
1314 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001315 && ((getline_equal(fgetline, cookie, getsourceline)
1316 && !source_finished(fgetline, cookie))
1317 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 && !func_has_ended(real_cookie))))
1319 {
1320 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1321 EMSG(_(e_endtry));
1322 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1323 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001324 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1325 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 else
1327 EMSG(_(e_endif));
1328 }
1329
1330 /*
1331 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1332 * ":endtry" in a sourced file or executed function. If the try
1333 * conditional is in its finally clause, ignore anything pending.
1334 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001335 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 */
1337 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001338 {
1339 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1340
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001341 if (idx >= 0)
1342 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001343 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1344 &cstack.cs_looplevel);
1345 }
1346 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 trylevel = initial_trylevel;
1348 }
1349
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001350 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1351 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001353 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 ? (char_u *)"endfunction" : (char_u *)NULL);
1355
1356 if (trylevel == 0)
1357 {
1358 /*
1359 * When an exception is being thrown out of the outermost try
1360 * conditional, discard the uncaught exception, disable the conversion
1361 * of interrupts or errors to exceptions, and ensure that no more
1362 * commands are executed.
1363 */
1364 if (did_throw)
1365 {
1366 void *p = NULL;
1367 char_u *saved_sourcing_name;
1368 int saved_sourcing_lnum;
1369 struct msglist *messages = NULL, *next;
1370
1371 /*
1372 * If the uncaught exception is a user exception, report it as an
1373 * error. If it is an error exception, display the saved error
1374 * message now. For an interrupt exception, do nothing; the
1375 * interrupt message is given elsewhere.
1376 */
1377 switch (current_exception->type)
1378 {
1379 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001380 vim_snprintf((char *)IObuff, IOSIZE,
1381 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 current_exception->value);
1383 p = vim_strsave(IObuff);
1384 break;
1385 case ET_ERROR:
1386 messages = current_exception->messages;
1387 current_exception->messages = NULL;
1388 break;
1389 case ET_INTERRUPT:
1390 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 }
1392
1393 saved_sourcing_name = sourcing_name;
1394 saved_sourcing_lnum = sourcing_lnum;
1395 sourcing_name = current_exception->throw_name;
1396 sourcing_lnum = current_exception->throw_lnum;
1397 current_exception->throw_name = NULL;
1398
1399 discard_current_exception(); /* uses IObuff if 'verbose' */
1400 suppress_errthrow = TRUE;
1401 force_abort = TRUE;
1402
1403 if (messages != NULL)
1404 {
1405 do
1406 {
1407 next = messages->next;
1408 emsg(messages->msg);
1409 vim_free(messages->msg);
1410 vim_free(messages);
1411 messages = next;
1412 }
1413 while (messages != NULL);
1414 }
1415 else if (p != NULL)
1416 {
1417 emsg(p);
1418 vim_free(p);
1419 }
1420 vim_free(sourcing_name);
1421 sourcing_name = saved_sourcing_name;
1422 sourcing_lnum = saved_sourcing_lnum;
1423 }
1424
1425 /*
1426 * On an interrupt or an aborting error not converted to an exception,
1427 * disable the conversion of errors to exceptions. (Interrupts are not
1428 * converted any more, here.) This enables also the interrupt message
1429 * when force_abort is set and did_emsg unset in case of an interrupt
1430 * from a finally clause after an error.
1431 */
1432 else if (got_int || (did_emsg && force_abort))
1433 suppress_errthrow = TRUE;
1434 }
1435
1436 /*
1437 * The current cstack will be freed when do_cmdline() returns. An uncaught
1438 * exception will have to be rethrown in the previous cstack. If a function
1439 * has just returned or a script file was just finished and the previous
1440 * cstack belongs to the same function or, respectively, script file, it
1441 * will have to be checked for finally clauses to be executed due to the
1442 * ":return" or ":finish". This is done in do_one_cmd().
1443 */
1444 if (did_throw)
1445 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001446 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001448 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 && ex_nesting_level > func_level(real_cookie) + 1))
1450 {
1451 if (!did_throw)
1452 check_cstack = TRUE;
1453 }
1454 else
1455 {
1456 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001457 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 --ex_nesting_level;
1459 /*
1460 * Go to debug mode when returning from a function in which we are
1461 * single-stepping.
1462 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001463 if ((getline_equal(fgetline, cookie, getsourceline)
1464 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001466 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 ? (char_u *)_("End of sourced file")
1468 : (char_u *)_("End of function"));
1469 }
1470
1471 /*
1472 * Restore the exception environment (done after returning from the
1473 * debugger).
1474 */
1475 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001476 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477
1478 msg_list = saved_msg_list;
1479#endif /* FEAT_EVAL */
1480
1481 /*
1482 * If there was too much output to fit on the command line, ask the user to
1483 * hit return before redrawing the screen. With the ":global" command we do
1484 * this only once after the command is finished.
1485 */
1486 if (did_inc)
1487 {
1488 --RedrawingDisabled;
1489 --no_wait_return;
1490 msg_scroll = FALSE;
1491
1492 /*
1493 * When just finished an ":if"-":else" which was typed, no need to
1494 * wait for hit-return. Also for an error situation.
1495 */
1496 if (retval == FAIL
1497#ifdef FEAT_EVAL
1498 || (did_endif && KeyTyped && !did_emsg)
1499#endif
1500 )
1501 {
1502 need_wait_return = FALSE;
1503 msg_didany = FALSE; /* don't wait when restarting edit */
1504 }
1505 else if (need_wait_return)
1506 {
1507 /*
1508 * The msg_start() above clears msg_didout. The wait_return we do
1509 * here should not overwrite the command that may be shown before
1510 * doing that.
1511 */
1512 msg_didout |= msg_didout_before_start;
1513 wait_return(FALSE);
1514 }
1515 }
1516
Bram Moolenaar2a942252012-11-28 23:03:07 +01001517#ifdef FEAT_EVAL
1518 did_endif = FALSE; /* in case do_cmdline used recursively */
1519#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 /*
1521 * Reset if_level, in case a sourced script file contains more ":if" than
1522 * ":endif" (could be ":if x | foo | endif").
1523 */
1524 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001525#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001526
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 --call_depth;
1528 return retval;
1529}
1530
1531#ifdef FEAT_EVAL
1532/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001533 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 */
1535 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001536get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001538 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 wcmd_T *wp;
1540 char_u *line;
1541
1542 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1543 {
1544 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001545 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001547 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 if (cp->getline == NULL)
1549 line = getcmdline(c, 0L, indent);
1550 else
1551 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001552 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 ++cp->current_line;
1554
1555 return line;
1556 }
1557
1558 KeyTyped = FALSE;
1559 ++cp->current_line;
1560 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1561 sourcing_lnum = wp->lnum;
1562 return vim_strsave(wp->line);
1563}
1564
1565/*
1566 * Store a line in "gap" so that a ":while" loop can execute it again.
1567 */
1568 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001569store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570{
1571 if (ga_grow(gap, 1) == FAIL)
1572 return FAIL;
1573 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1574 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1575 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 return OK;
1577}
1578
1579/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001580 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 */
1582 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001583free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584{
1585 while (gap->ga_len > 0)
1586 {
1587 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1588 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 }
1590}
1591#endif
1592
1593/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001594 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1595 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001598getline_equal(
1599 char_u *(*fgetline)(int, void *, int),
1600 void *cookie UNUSED, /* argument for fgetline() */
1601 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602{
1603#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001604 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001605 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606
Bram Moolenaar89d40322006-08-29 15:30:07 +00001607 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001608 * function that's originally used to obtain the lines. This may be
1609 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001610 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001611 cp = (struct loop_cookie *)cookie;
1612 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 {
1614 gp = cp->getline;
1615 cp = cp->cookie;
1616 }
1617 return gp == func;
1618#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001619 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620#endif
1621}
1622
1623#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1624/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001625 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 * getline function. Otherwise return "cookie".
1627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001629getline_cookie(
1630 char_u *(*fgetline)(int, void *, int) UNUSED,
1631 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632{
1633# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001634 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001635 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
Bram Moolenaar89d40322006-08-29 15:30:07 +00001637 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001638 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001640 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001641 cp = (struct loop_cookie *)cookie;
1642 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 {
1644 gp = cp->getline;
1645 cp = cp->cookie;
1646 }
1647 return cp;
1648# else
1649 return cookie;
1650# endif
1651}
1652#endif
1653
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001654
1655/*
1656 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1657 * ":bwipeout", etc.
1658 * Returns the buffer number.
1659 */
1660 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001661compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001662{
1663 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001664 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001665 int count = offset;
1666
1667 buf = firstbuf;
1668 while (buf->b_next != NULL && buf->b_fnum < lnum)
1669 buf = buf->b_next;
1670 while (count != 0)
1671 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001672 count += (offset < 0) ? 1 : -1;
1673 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1674 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001675 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001676 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001677 if (addr_type == ADDR_LOADED_BUFFERS)
1678 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001679 while (buf->b_ml.ml_mfp == NULL)
1680 {
1681 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1682 if (nextbuf == NULL)
1683 break;
1684 buf = nextbuf;
1685 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001686 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001687 /* we might have gone too far, last buffer is not loadedd */
1688 if (addr_type == ADDR_LOADED_BUFFERS)
1689 while (buf->b_ml.ml_mfp == NULL)
1690 {
1691 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1692 if (nextbuf == NULL)
1693 break;
1694 buf = nextbuf;
1695 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001696 return buf->b_fnum;
1697}
1698
Bram Moolenaarf240e182014-11-27 18:33:02 +01001699#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001700static int current_win_nr(win_T *win);
1701static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001702
1703 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001704current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001705{
1706 win_T *wp;
1707 int nr = 0;
1708
Bram Moolenaar29323592016-07-24 22:04:11 +02001709 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001710 {
1711 ++nr;
1712 if (wp == win)
1713 break;
1714 }
1715 return nr;
1716}
1717
1718 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001719current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001720{
1721 tabpage_T *tp;
1722 int nr = 0;
1723
Bram Moolenaar29323592016-07-24 22:04:11 +02001724 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001725 {
1726 ++nr;
1727 if (tp == tab)
1728 break;
1729 }
1730 return nr;
1731}
1732
1733# define CURRENT_WIN_NR current_win_nr(curwin)
1734# define LAST_WIN_NR current_win_nr(NULL)
1735# define CURRENT_TAB_NR current_tab_nr(curtab)
1736# define LAST_TAB_NR current_tab_nr(NULL)
1737#else
1738# define CURRENT_WIN_NR 1
1739# define LAST_WIN_NR 1
1740# define CURRENT_TAB_NR 1
1741# define LAST_TAB_NR 1
1742#endif
1743
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001744
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745/*
1746 * Execute one Ex command.
1747 *
1748 * If 'sourcing' is TRUE, the command will be included in the error message.
1749 *
1750 * 1. skip comment lines and leading space
1751 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001752 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001753 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001754 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001755 * 6. parse arguments
1756 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001758 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 *
1760 * This function may be called recursively!
1761 */
1762#if (_MSC_VER == 1200)
1763/*
Bram Moolenaared203462004-06-16 11:19:22 +00001764 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001766 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767#endif
1768 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001769do_one_cmd(
1770 char_u **cmdlinep,
1771 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001773 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001775 char_u *(*fgetline)(int, void *, int),
1776 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777{
1778 char_u *p;
1779 linenr_T lnum;
1780 long n;
1781 char_u *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001782 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 exarg_T ea; /* Ex command arguments */
1784 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001785 int save_msg_scroll = msg_scroll;
1786 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001788#ifdef HAVE_SANDBOX
1789 int did_sandbox = FALSE;
1790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 cmdmod_T save_cmdmod;
1792 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001793 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794
1795 vim_memset(&ea, 0, sizeof(ea));
1796 ea.line1 = 1;
1797 ea.line2 = 1;
1798#ifdef FEAT_EVAL
1799 ++ex_nesting_level;
1800#endif
1801
Bram Moolenaar21691f82012-12-05 19:13:18 +01001802 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 if (quitmore
1804#ifdef FEAT_EVAL
1805 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001806 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001807#endif
1808#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001809 /* avoid that an autocommand, e.g. QuitPre, does this */
1810 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811#endif
1812 )
1813 --quitmore;
1814
1815 /*
1816 * Reset browse, confirm, etc.. They are restored when returning, for
1817 * recursive calls.
1818 */
1819 save_cmdmod = cmdmod;
1820 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1821
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001822 /* "#!anything" is handled like a comment. */
1823 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1824 goto doend;
1825
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 /*
1827 * Repeat until no more command modifiers are found.
1828 */
1829 ea.cmd = *cmdlinep;
1830 for (;;)
1831 {
1832/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001833 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 */
1835 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1836 ++ea.cmd;
1837
1838 /* in ex mode, an empty line works like :+ */
1839 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001840 && (getline_equal(fgetline, cookie, getexmodeline)
1841 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001842 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 {
1844 ea.cmd = (char_u *)"+";
1845 ex_pressedreturn = TRUE;
1846 }
1847
1848 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001849 if (*ea.cmd == '"')
1850 goto doend;
1851 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001852 {
1853 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856
1857/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001858 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 */
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001860 p = skip_range(ea.cmd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 switch (*p)
1862 {
1863 /* When adding an entry, also modify cmd_exists(). */
1864 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1865 break;
1866#ifdef FEAT_WINDOWS
1867 cmdmod.split |= WSP_ABOVE;
1868#endif
1869 continue;
1870
1871 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1872 {
1873#ifdef FEAT_WINDOWS
1874 cmdmod.split |= WSP_BELOW;
1875#endif
1876 continue;
1877 }
1878 if (checkforcmd(&ea.cmd, "browse", 3))
1879 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001880#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 cmdmod.browse = TRUE;
1882#endif
1883 continue;
1884 }
1885 if (!checkforcmd(&ea.cmd, "botright", 2))
1886 break;
1887#ifdef FEAT_WINDOWS
1888 cmdmod.split |= WSP_BOT;
1889#endif
1890 continue;
1891
1892 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1893 break;
1894#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1895 cmdmod.confirm = TRUE;
1896#endif
1897 continue;
1898
1899 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1900 {
1901 cmdmod.keepmarks = TRUE;
1902 continue;
1903 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001904 if (checkforcmd(&ea.cmd, "keepalt", 5))
1905 {
1906 cmdmod.keepalt = TRUE;
1907 continue;
1908 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001909 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1910 {
1911 cmdmod.keeppatterns = TRUE;
1912 continue;
1913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1915 break;
1916 cmdmod.keepjumps = TRUE;
1917 continue;
1918
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001919 case 'f': /* only accept ":filter {pat} cmd" */
1920 {
1921 char_u *reg_pat;
1922
1923 if (!checkforcmd(&p, "filter", 4)
1924 || *p == NUL || ends_excmd(*p))
1925 break;
Bram Moolenaard29459b2016-08-26 22:29:11 +02001926 if (*p == '!')
1927 {
1928 cmdmod.filter_force = TRUE;
1929 p = skipwhite(p + 1);
1930 if (*p == NUL || ends_excmd(*p))
1931 break;
1932 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001933 p = skip_vimgrep_pat(p, &reg_pat, NULL);
1934 if (p == NULL || *p == NUL)
1935 break;
1936 cmdmod.filter_regmatch.regprog =
1937 vim_regcomp(reg_pat, RE_MAGIC);
1938 if (cmdmod.filter_regmatch.regprog == NULL)
1939 break;
1940 ea.cmd = p;
1941 continue;
1942 }
1943
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 /* ":hide" and ":hide | cmd" are not modifiers */
1945 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1946 || *p == NUL || ends_excmd(*p))
1947 break;
1948 ea.cmd = p;
1949 cmdmod.hide = TRUE;
1950 continue;
1951
1952 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1953 {
1954 cmdmod.lockmarks = TRUE;
1955 continue;
1956 }
1957
1958 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1959 break;
1960#ifdef FEAT_WINDOWS
1961 cmdmod.split |= WSP_ABOVE;
1962#endif
1963 continue;
1964
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001965 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001966 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001967#ifdef FEAT_AUTOCMD
1968 if (cmdmod.save_ei == NULL)
1969 {
1970 /* Set 'eventignore' to "all". Restore the
1971 * existing option value later. */
1972 cmdmod.save_ei = vim_strsave(p_ei);
1973 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001974 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001975 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001976#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001977 continue;
1978 }
Bram Moolenaar3bcfca32016-07-30 19:39:29 +02001979 if (!checkforcmd(&ea.cmd, "noswapfile", 3))
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001980 break;
1981 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001982 continue;
1983
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1985 break;
1986#ifdef FEAT_WINDOWS
1987 cmdmod.split |= WSP_BELOW;
1988#endif
1989 continue;
1990
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001991 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1992 {
1993#ifdef HAVE_SANDBOX
1994 if (!did_sandbox)
1995 ++sandbox;
1996 did_sandbox = TRUE;
1997#endif
1998 continue;
1999 }
2000 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002002 if (save_msg_silent == -1)
2003 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
2006 {
2007 /* ":silent!", but not "silent !cmd" */
2008 ea.cmd = skipwhite(ea.cmd + 1);
2009 ++emsg_silent;
2010 ++did_esilent;
2011 }
2012 continue;
2013
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002014 case 't': if (checkforcmd(&p, "tab", 3))
2015 {
2016#ifdef FEAT_WINDOWS
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02002017 long tabnr = get_address(&ea, &ea.cmd, ADDR_TABS,
2018 ea.skip, FALSE);
2019 if (tabnr == MAXLNUM)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002020 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02002021 else
2022 {
2023 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2024 {
2025 errormsg = (char_u *)_(e_invrange);
2026 goto doend;
2027 }
2028 cmdmod.tab = tabnr + 1;
2029 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002030 ea.cmd = p;
2031#endif
2032 continue;
2033 }
2034 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 break;
2036#ifdef FEAT_WINDOWS
2037 cmdmod.split |= WSP_TOP;
2038#endif
2039 continue;
2040
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002041 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
2042 break;
2043 if (save_msg_silent == -1)
2044 save_msg_silent = msg_silent;
2045 msg_silent = 0;
2046 continue;
2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
2049 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002050#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 cmdmod.split |= WSP_VERT;
2052#endif
2053 continue;
2054 }
2055 if (!checkforcmd(&p, "verbose", 4))
2056 break;
2057 if (verbose_save < 0)
2058 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00002059 if (vim_isdigit(*ea.cmd))
2060 p_verbose = atoi((char *)ea.cmd);
2061 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 p_verbose = 1;
2063 ea.cmd = p;
2064 continue;
2065 }
2066 break;
2067 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002068 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069
2070#ifdef FEAT_EVAL
2071 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2072 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2073#else
2074 ea.skip = (if_level > 0);
2075#endif
2076
2077#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002078# ifdef FEAT_PROFILE
2079 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002080 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002081 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002082 if (getline_equal(fgetline, cookie, get_func_line))
2083 func_line_exec(getline_cookie(fgetline, cookie));
2084 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002085 script_line_exec();
2086 }
2087#endif
2088
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 /* May go to debug mode. If this happens and the ">quit" debug command is
2090 * used, throw an interrupt exception and skip the next command. */
2091 dbg_check_breakpoint(&ea);
2092 if (!ea.skip && got_int)
2093 {
2094 ea.skip = TRUE;
2095 (void)do_intthrow(cstack);
2096 }
2097#endif
2098
2099/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002100 * 3. Skip over the range to find the command. Let "p" point to after it.
2101 *
2102 * We need the command to know what kind of range it uses.
2103 */
2104 cmd = ea.cmd;
2105 ea.cmd = skip_range(ea.cmd, NULL);
2106 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2107 ea.cmd = skipwhite(ea.cmd + 1);
2108 p = find_command(&ea, NULL);
2109
2110/*
2111 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 *
2113 * where 'addr' is:
2114 *
2115 * % (entire file)
2116 * $ [+-NUM]
2117 * 'x [+-NUM] (where x denotes a currently defined mark)
2118 * . [+-NUM]
2119 * [+-NUM]..
2120 * NUM
2121 *
2122 * The ea.cmd pointer is updated to point to the first character following the
2123 * range spec. If an initial address is found, but no second, the upper bound
2124 * is equal to the lower.
2125 */
2126
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002127 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002128 if (!IS_USER_CMDIDX(ea.cmdidx))
2129 {
2130 if (ea.cmdidx != CMD_SIZE)
2131 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2132 else
2133 ea.addr_type = ADDR_LINES;
2134
2135#ifdef FEAT_WINDOWS
2136 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002137 if (ea.cmdidx == CMD_wincmd && p != NULL)
2138 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002139#endif
2140 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002141
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002143 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 for (;;)
2145 {
2146 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002147 switch (ea.addr_type)
2148 {
2149 case ADDR_LINES:
2150 /* default is current line number */
2151 ea.line2 = curwin->w_cursor.lnum;
2152 break;
2153 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002154 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002155 ea.line2 = lnum;
2156 break;
2157 case ADDR_ARGUMENTS:
2158 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002159 if (ea.line2 > ARGCOUNT)
2160 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002161 break;
2162 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002163 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002164 ea.line2 = curbuf->b_fnum;
2165 break;
2166 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002167 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002168 ea.line2 = lnum;
2169 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002170#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002171 case ADDR_QUICKFIX:
2172 ea.line2 = qf_get_cur_valid_idx(&ea);
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.cmd = skipwhite(ea.cmd);
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002177 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip,
2178 ea.addr_count == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 if (ea.cmd == NULL) /* error detected */
2180 goto doend;
2181 if (lnum == MAXLNUM)
2182 {
2183 if (*ea.cmd == '%') /* '%' - all lines */
2184 {
2185 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002186 switch (ea.addr_type)
2187 {
2188 case ADDR_LINES:
2189 ea.line1 = 1;
2190 ea.line2 = curbuf->b_ml.ml_line_count;
2191 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002192 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002193 {
2194 buf_T *buf = firstbuf;
2195
2196 while (buf->b_next != NULL
2197 && buf->b_ml.ml_mfp == NULL)
2198 buf = buf->b_next;
2199 ea.line1 = buf->b_fnum;
2200 buf = lastbuf;
2201 while (buf->b_prev != NULL
2202 && buf->b_ml.ml_mfp == NULL)
2203 buf = buf->b_prev;
2204 ea.line2 = buf->b_fnum;
2205 break;
2206 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002207 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002208 ea.line1 = firstbuf->b_fnum;
2209 ea.line2 = lastbuf->b_fnum;
2210 break;
2211 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002212 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002213 if (IS_USER_CMDIDX(ea.cmdidx))
2214 {
2215 ea.line1 = 1;
2216 ea.line2 = ea.addr_type == ADDR_WINDOWS
2217 ? LAST_WIN_NR : LAST_TAB_NR;
2218 }
2219 else
2220 {
2221 /* there is no Vim command which uses '%' and
2222 * ADDR_WINDOWS or ADDR_TABS */
2223 errormsg = (char_u *)_(e_invrange);
2224 goto doend;
2225 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002226 break;
2227 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002228 if (ARGCOUNT == 0)
2229 ea.line1 = ea.line2 = 0;
2230 else
2231 {
2232 ea.line1 = 1;
2233 ea.line2 = ARGCOUNT;
2234 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002235 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002236#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002237 case ADDR_QUICKFIX:
2238 ea.line1 = 1;
2239 ea.line2 = qf_get_size(&ea);
2240 if (ea.line2 == 0)
2241 ea.line2 = 1;
2242 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002243#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 ++ea.addr_count;
2246 }
2247 /* '*' - visual area */
2248 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2249 {
2250 pos_T *fp;
2251
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002252 if (ea.addr_type != ADDR_LINES)
2253 {
2254 errormsg = (char_u *)_(e_invrange);
2255 goto doend;
2256 }
2257
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 ++ea.cmd;
2259 if (!ea.skip)
2260 {
2261 fp = getmark('<', FALSE);
2262 if (check_mark(fp) == FAIL)
2263 goto doend;
2264 ea.line1 = fp->lnum;
2265 fp = getmark('>', FALSE);
2266 if (check_mark(fp) == FAIL)
2267 goto doend;
2268 ea.line2 = fp->lnum;
2269 ++ea.addr_count;
2270 }
2271 }
2272 }
2273 else
2274 ea.line2 = lnum;
2275 ea.addr_count++;
2276
2277 if (*ea.cmd == ';')
2278 {
2279 if (!ea.skip)
2280 curwin->w_cursor.lnum = ea.line2;
2281 }
2282 else if (*ea.cmd != ',')
2283 break;
2284 ++ea.cmd;
2285 }
2286
2287 /* One address given: set start and end lines */
2288 if (ea.addr_count == 1)
2289 {
2290 ea.line1 = ea.line2;
2291 /* ... but only implicit: really no address given */
2292 if (lnum == MAXLNUM)
2293 ea.addr_count = 0;
2294 }
2295
2296 /* Don't leave the cursor on an illegal line (caused by ';') */
2297 check_cursor_lnum();
2298
2299/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002300 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 */
2302
2303 /*
2304 * Skip ':' and any white space
2305 */
2306 ea.cmd = skipwhite(ea.cmd);
2307 while (*ea.cmd == ':')
2308 ea.cmd = skipwhite(ea.cmd + 1);
2309
2310 /*
2311 * If we got a line, but no command, then go to the line.
2312 * If we find a '|' or '\n' we set ea.nextcmd.
2313 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002314 if (*ea.cmd == NUL || *ea.cmd == '"'
2315 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 {
2317 /*
2318 * strange vi behaviour:
2319 * ":3" jumps to line 3
2320 * ":3|..." prints line 3
2321 * ":|" prints current line
2322 */
2323 if (ea.skip) /* skip this if inside :if */
2324 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002325 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 {
2327 ea.cmdidx = CMD_print;
2328 ea.argt = RANGE+COUNT+TRLBAR;
2329 if ((errormsg = invalid_range(&ea)) == NULL)
2330 {
2331 correct_range(&ea);
2332 ex_print(&ea);
2333 }
2334 }
2335 else if (ea.addr_count != 0)
2336 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002337 if (ea.line2 > curbuf->b_ml.ml_line_count)
2338 {
2339 /* With '-' in 'cpoptions' a line number past the file is an
2340 * error, otherwise put it at the end of the file. */
2341 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2342 ea.line2 = -1;
2343 else
2344 ea.line2 = curbuf->b_ml.ml_line_count;
2345 }
2346
2347 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002348 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 else
2350 {
2351 if (ea.line2 == 0)
2352 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 else
2354 curwin->w_cursor.lnum = ea.line2;
2355 beginline(BL_SOL | BL_FIX);
2356 }
2357 }
2358 goto doend;
2359 }
2360
Bram Moolenaard5005162014-08-22 23:05:54 +02002361#ifdef FEAT_AUTOCMD
2362 /* If this looks like an undefined user command and there are CmdUndefined
2363 * autocommands defined, trigger the matching autocommands. */
2364 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2365 && ASCII_ISUPPER(*ea.cmd)
2366 && has_cmdundefined())
2367 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002368 int ret;
2369
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002370 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002371 while (ASCII_ISALNUM(*p))
2372 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002373 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002374 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2375 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002376 /* If the autocommands did something and didn't cause an error, try
2377 * finding the command again. */
Bram Moolenaareac784e2016-07-28 22:08:24 +02002378 p = (ret && !aborting()) ? find_command(&ea, NULL) : ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002379 }
2380#endif
2381
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382#ifdef FEAT_USR_CMDS
2383 if (p == NULL)
2384 {
2385 if (!ea.skip)
2386 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2387 goto doend;
2388 }
2389 /* Check for wrong commands. */
2390 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2391 {
2392 errormsg = uc_fun_cmd();
2393 goto doend;
2394 }
2395#endif
2396 if (ea.cmdidx == CMD_SIZE)
2397 {
2398 if (!ea.skip)
2399 {
2400 STRCPY(IObuff, _("E492: Not an editor command"));
2401 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002402 {
2403 /* If the modifier was parsed OK the error must be in the
2404 * following command */
2405 if (after_modifier != NULL)
2406 append_command(after_modifier);
2407 else
2408 append_command(*cmdlinep);
2409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002411 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 }
2413 goto doend;
2414 }
2415
Bram Moolenaar958636c2014-10-21 20:01:58 +02002416 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2417 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002418#ifdef HAVE_EX_SCRIPT_NI
2419 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2420#endif
2421 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422
2423#ifndef FEAT_EVAL
2424 /*
2425 * When the expression evaluation is disabled, recognize the ":if" and
2426 * ":endif" commands and ignore everything in between it.
2427 */
2428 if (ea.cmdidx == CMD_if)
2429 ++if_level;
2430 if (if_level)
2431 {
2432 if (ea.cmdidx == CMD_endif)
2433 --if_level;
2434 goto doend;
2435 }
2436
2437#endif
2438
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002439 /* forced commands */
2440 if (*p == '!' && ea.cmdidx != CMD_substitute
2441 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 {
2443 ++p;
2444 ea.forceit = TRUE;
2445 }
2446 else
2447 ea.forceit = FALSE;
2448
2449/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002450 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002452 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002453 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454
2455 if (!ea.skip)
2456 {
2457#ifdef HAVE_SANDBOX
2458 if (sandbox != 0 && !(ea.argt & SBOXOK))
2459 {
2460 /* Command not allowed in sandbox. */
2461 errormsg = (char_u *)_(e_sandbox);
2462 goto doend;
2463 }
2464#endif
2465 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2466 {
2467 /* Command not allowed in non-'modifiable' buffer */
2468 errormsg = (char_u *)_(e_modifiable);
2469 goto doend;
2470 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002471
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002472 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002473 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002475 /* Command not allowed when editing the command line. */
Bram Moolenaar5a497892016-09-03 16:29:04 +02002476 errormsg = get_text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 goto doend;
2478 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002479#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002480 /* Disallow editing another buffer when "curbuf_lock" is set.
2481 * Do allow ":edit" (check for argument later).
2482 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002483 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002484 && ea.cmdidx != CMD_edit
2485 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002486 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002487 && curbuf_locked())
2488 goto doend;
2489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490
2491 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2492 {
2493 /* no range allowed */
2494 errormsg = (char_u *)_(e_norange);
2495 goto doend;
2496 }
2497 }
2498
2499 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2500 {
2501 errormsg = (char_u *)_(e_nobang);
2502 goto doend;
2503 }
2504
2505 /*
2506 * Don't complain about the range if it is not used
2507 * (could happen if line_count is accidentally set to 0).
2508 */
2509 if (!ea.skip && !ni)
2510 {
2511 /*
2512 * If the range is backwards, ask for confirmation and, if given, swap
2513 * ea.line1 & ea.line2 so it's forwards again.
2514 * When global command is busy, don't ask, will fail below.
2515 */
2516 if (!global_busy && ea.line1 > ea.line2)
2517 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002518 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002520 if (sourcing || exmode_active)
2521 {
2522 errormsg = (char_u *)_("E493: Backwards range given");
2523 goto doend;
2524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 if (ask_yesno((char_u *)
2526 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002527 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 }
2529 lnum = ea.line1;
2530 ea.line1 = ea.line2;
2531 ea.line2 = lnum;
2532 }
2533 if ((errormsg = invalid_range(&ea)) != NULL)
2534 goto doend;
2535 }
2536
2537 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2538 ea.line2 = 1;
2539
2540 correct_range(&ea);
2541
2542#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002543 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2544 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 {
2546 /* Put the first line at the start of a closed fold, put the last line
2547 * at the end of a closed fold. */
2548 (void)hasFolding(ea.line1, &ea.line1, NULL);
2549 (void)hasFolding(ea.line2, NULL, &ea.line2);
2550 }
2551#endif
2552
2553#ifdef FEAT_QUICKFIX
2554 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002555 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 * option here, so things like % get expanded.
2557 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002558 p = replace_makeprg(&ea, p, cmdlinep);
2559 if (p == NULL)
2560 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561#endif
2562
2563 /*
2564 * Skip to start of argument.
2565 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2566 */
2567 if (ea.cmdidx == CMD_bang)
2568 ea.arg = p;
2569 else
2570 ea.arg = skipwhite(p);
2571
2572 /*
2573 * Check for "++opt=val" argument.
2574 * Must be first, allow ":w ++enc=utf8 !cmd"
2575 */
2576 if (ea.argt & ARGOPT)
2577 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2578 if (getargopt(&ea) == FAIL && !ni)
2579 {
2580 errormsg = (char_u *)_(e_invarg);
2581 goto doend;
2582 }
2583
2584 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2585 {
2586 if (*ea.arg == '>') /* append */
2587 {
2588 if (*++ea.arg != '>') /* typed wrong */
2589 {
2590 errormsg = (char_u *)_("E494: Use w or w>>");
2591 goto doend;
2592 }
2593 ea.arg = skipwhite(ea.arg + 1);
2594 ea.append = TRUE;
2595 }
2596 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2597 {
2598 ++ea.arg;
2599 ea.usefilter = TRUE;
2600 }
2601 }
2602
2603 if (ea.cmdidx == CMD_read)
2604 {
2605 if (ea.forceit)
2606 {
2607 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2608 ea.forceit = FALSE;
2609 }
2610 else if (*ea.arg == '!') /* :r !filter */
2611 {
2612 ++ea.arg;
2613 ea.usefilter = TRUE;
2614 }
2615 }
2616
2617 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2618 {
2619 ea.amount = 1;
2620 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2621 {
2622 ++ea.arg;
2623 ++ea.amount;
2624 }
2625 ea.arg = skipwhite(ea.arg);
2626 }
2627
2628 /*
2629 * Check for "+command" argument, before checking for next command.
2630 * Don't do this for ":read !cmd" and ":write !cmd".
2631 */
2632 if ((ea.argt & EDITCMD) && !ea.usefilter)
2633 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2634
2635 /*
2636 * Check for '|' to separate commands and '"' to start comments.
2637 * Don't do this for ":read !cmd" and ":write !cmd".
2638 */
2639 if ((ea.argt & TRLBAR) && !ea.usefilter)
2640 separate_nextcmd(&ea);
2641
2642 /*
2643 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002644 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2645 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002647 else if (ea.cmdidx == CMD_bang
2648 || ea.cmdidx == CMD_global
2649 || ea.cmdidx == CMD_vglobal
2650 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 {
2652 for (p = ea.arg; *p; ++p)
2653 {
2654 /* Remove one backslash before a newline, so that it's possible to
2655 * pass a newline to the shell and also a newline that is preceded
2656 * with a backslash. This makes it impossible to end a shell
2657 * command in a backslash, but that doesn't appear useful.
2658 * Halving the number of backslashes is incompatible with previous
2659 * versions. */
2660 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002661 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 else if (*p == '\n')
2663 {
2664 ea.nextcmd = p + 1;
2665 *p = NUL;
2666 break;
2667 }
2668 }
2669 }
2670
2671 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2672 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002673 buf_T *buf;
2674
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002676 switch (ea.addr_type)
2677 {
2678 case ADDR_LINES:
2679 ea.line2 = curbuf->b_ml.ml_line_count;
2680 break;
2681 case ADDR_LOADED_BUFFERS:
2682 buf = firstbuf;
2683 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2684 buf = buf->b_next;
2685 ea.line1 = buf->b_fnum;
2686 buf = lastbuf;
2687 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2688 buf = buf->b_prev;
2689 ea.line2 = buf->b_fnum;
2690 break;
2691 case ADDR_BUFFERS:
2692 ea.line1 = firstbuf->b_fnum;
2693 ea.line2 = lastbuf->b_fnum;
2694 break;
2695 case ADDR_WINDOWS:
2696 ea.line2 = LAST_WIN_NR;
2697 break;
2698 case ADDR_TABS:
2699 ea.line2 = LAST_TAB_NR;
2700 break;
2701 case ADDR_ARGUMENTS:
2702 if (ARGCOUNT == 0)
2703 ea.line1 = ea.line2 = 0;
2704 else
2705 ea.line2 = ARGCOUNT;
2706 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002707#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002708 case ADDR_QUICKFIX:
2709 ea.line2 = qf_get_size(&ea);
2710 if (ea.line2 == 0)
2711 ea.line2 = 1;
2712 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002713#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 }
2716
2717 /* accept numbered register only when no count allowed (:put) */
2718 if ( (ea.argt & REGSTR)
2719 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002720 /* Do not allow register = for user commands */
2721 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2723 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002724#ifndef FEAT_CLIPBOARD
2725 /* check these explicitly for a more specific error message */
2726 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002728 errormsg = (char_u *)_(e_invalidreg);
2729 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 }
2731#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002732 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2733 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002734 {
2735 ea.regname = *ea.arg++;
2736#ifdef FEAT_EVAL
2737 /* for '=' register: accept the rest of the line as an expression */
2738 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2739 {
2740 set_expr_line(vim_strsave(ea.arg));
2741 ea.arg += STRLEN(ea.arg);
2742 }
2743#endif
2744 ea.arg = skipwhite(ea.arg);
2745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 }
2747
2748 /*
2749 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2750 * count, it's a buffer name.
2751 */
2752 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2753 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2754 || vim_iswhite(*p)))
2755 {
2756 n = getdigits(&ea.arg);
2757 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002758 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 {
2760 errormsg = (char_u *)_(e_zerocount);
2761 goto doend;
2762 }
2763 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2764 {
2765 ea.line2 = n;
2766 if (ea.addr_count == 0)
2767 ea.addr_count = 1;
2768 }
2769 else
2770 {
2771 ea.line1 = ea.line2;
2772 ea.line2 += n - 1;
2773 ++ea.addr_count;
2774 /*
2775 * Be vi compatible: no error message for out of range.
2776 */
2777 if (ea.line2 > curbuf->b_ml.ml_line_count)
2778 ea.line2 = curbuf->b_ml.ml_line_count;
2779 }
2780 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002781
2782 /*
2783 * Check for flags: 'l', 'p' and '#'.
2784 */
2785 if (ea.argt & EXFLAGS)
2786 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002788 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002789 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 {
2791 errormsg = (char_u *)_(e_trailing);
2792 goto doend;
2793 }
2794
2795 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2796 {
2797 errormsg = (char_u *)_(e_argreq);
2798 goto doend;
2799 }
2800
2801#ifdef FEAT_EVAL
2802 /*
2803 * Skip the command when it's not going to be executed.
2804 * The commands like :if, :endif, etc. always need to be executed.
2805 * Also make an exception for commands that handle a trailing command
2806 * themselves.
2807 */
2808 if (ea.skip)
2809 {
2810 switch (ea.cmdidx)
2811 {
2812 /* commands that need evaluation */
2813 case CMD_while:
2814 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002815 case CMD_for:
2816 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 case CMD_if:
2818 case CMD_elseif:
2819 case CMD_else:
2820 case CMD_endif:
2821 case CMD_try:
2822 case CMD_catch:
2823 case CMD_finally:
2824 case CMD_endtry:
2825 case CMD_function:
2826 break;
2827
2828 /* Commands that handle '|' themselves. Check: A command should
2829 * either have the TRLBAR flag, appear in this list or appear in
2830 * the list at ":help :bar". */
2831 case CMD_aboveleft:
2832 case CMD_and:
2833 case CMD_belowright:
2834 case CMD_botright:
2835 case CMD_browse:
2836 case CMD_call:
2837 case CMD_confirm:
2838 case CMD_delfunction:
2839 case CMD_djump:
2840 case CMD_dlist:
2841 case CMD_dsearch:
2842 case CMD_dsplit:
2843 case CMD_echo:
2844 case CMD_echoerr:
2845 case CMD_echomsg:
2846 case CMD_echon:
2847 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002848 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 case CMD_help:
2850 case CMD_hide:
2851 case CMD_ijump:
2852 case CMD_ilist:
2853 case CMD_isearch:
2854 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002855 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 case CMD_keepjumps:
2857 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002858 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 case CMD_leftabove:
2860 case CMD_let:
2861 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002862 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002864 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002865 case CMD_noautocmd:
2866 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 case CMD_perl:
2868 case CMD_psearch:
2869 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002870 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002871 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 case CMD_return:
2873 case CMD_rightbelow:
2874 case CMD_ruby:
2875 case CMD_silent:
2876 case CMD_smagic:
2877 case CMD_snomagic:
2878 case CMD_substitute:
2879 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002880 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 case CMD_tcl:
2882 case CMD_throw:
2883 case CMD_tilde:
2884 case CMD_topleft:
2885 case CMD_unlet:
2886 case CMD_verbose:
2887 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002888 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 break;
2890
2891 default: goto doend;
2892 }
2893 }
2894#endif
2895
2896 if (ea.argt & XFILE)
2897 {
2898 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2899 goto doend;
2900 }
2901
2902#ifdef FEAT_LISTCMDS
2903 /*
2904 * Accept buffer name. Cannot be used at the same time with a buffer
2905 * number. Don't do this for a user command.
2906 */
2907 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002908 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 {
2910 /*
2911 * :bdelete, :bwipeout and :bunload take several arguments, separated
2912 * by spaces: find next space (skipping over escaped characters).
2913 * The others take one argument: ignore trailing spaces.
2914 */
2915 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2916 || ea.cmdidx == CMD_bunload)
2917 p = skiptowhite_esc(ea.arg);
2918 else
2919 {
2920 p = ea.arg + STRLEN(ea.arg);
2921 while (p > ea.arg && vim_iswhite(p[-1]))
2922 --p;
2923 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002924 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2925 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 if (ea.line2 < 0) /* failed */
2927 goto doend;
2928 ea.addr_count = 1;
2929 ea.arg = skipwhite(p);
2930 }
2931#endif
2932
2933/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002934 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 *
2936 * The "ea" structure holds the arguments that can be used.
2937 */
2938 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002939 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 ea.cookie = cookie;
2941#ifdef FEAT_EVAL
2942 ea.cstack = cstack;
2943#endif
2944
2945#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002946 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 {
2948 /*
2949 * Execute a user-defined command.
2950 */
2951 do_ucmd(&ea);
2952 }
2953 else
2954#endif
2955 {
2956 /*
2957 * Call the function to execute the command.
2958 */
2959 ea.errmsg = NULL;
2960 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2961 if (ea.errmsg != NULL)
2962 errormsg = (char_u *)_(ea.errmsg);
2963 }
2964
2965#ifdef FEAT_EVAL
2966 /*
2967 * If the command just executed called do_cmdline(), any throw or ":return"
2968 * or ":finish" encountered there must also check the cstack of the still
2969 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2970 * exception, or reanimate a returned function or finished script file and
2971 * return or finish it again.
2972 */
2973 if (need_rethrow)
2974 do_throw(cstack);
2975 else if (check_cstack)
2976 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002977 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002979 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 && current_func_returned())
2981 do_return(&ea, TRUE, FALSE, NULL);
2982 }
2983 need_rethrow = check_cstack = FALSE;
2984#endif
2985
2986doend:
2987 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2988 curwin->w_cursor.lnum = 1;
2989
2990 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2991 {
2992 if (sourcing)
2993 {
2994 if (errormsg != IObuff)
2995 {
2996 STRCPY(IObuff, errormsg);
2997 errormsg = IObuff;
2998 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002999 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 }
3001 emsg(errormsg);
3002 }
3003#ifdef FEAT_EVAL
3004 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02003005 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
3006 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007#endif
3008
3009 if (verbose_save >= 0)
3010 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00003011#ifdef FEAT_AUTOCMD
3012 if (cmdmod.save_ei != NULL)
3013 {
3014 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003015 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
3016 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00003017 free_string_option(cmdmod.save_ei);
3018 }
3019#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003020 if (cmdmod.filter_regmatch.regprog != NULL)
3021 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022
3023 cmdmod = save_cmdmod;
3024
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003025 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 {
3027 /* messages could be enabled for a serious error, need to check if the
3028 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00003029 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003030 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 emsg_silent -= did_esilent;
3032 if (emsg_silent < 0)
3033 emsg_silent = 0;
3034 /* Restore msg_scroll, it's set by file I/O commands, even when no
3035 * message is actually displayed. */
3036 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003037
3038 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
3039 * somewhere in the line. Put it back in the first column. */
3040 if (redirecting())
3041 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 }
3043
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003044#ifdef HAVE_SANDBOX
3045 if (did_sandbox)
3046 --sandbox;
3047#endif
3048
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3050 ea.nextcmd = NULL;
3051
3052#ifdef FEAT_EVAL
3053 --ex_nesting_level;
3054#endif
3055
3056 return ea.nextcmd;
3057}
3058#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003059 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060#endif
3061
3062/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003063 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 * If there is a match advance "pp" to the argument and return TRUE.
3065 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003066 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003067checkforcmd(
3068 char_u **pp, /* start of command */
3069 char *cmd, /* name of command */
3070 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071{
3072 int i;
3073
3074 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003075 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 break;
3077 if (i >= len && !isalpha((*pp)[i]))
3078 {
3079 *pp = skipwhite(*pp + i);
3080 return TRUE;
3081 }
3082 return FALSE;
3083}
3084
3085/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003086 * Append "cmd" to the error message in IObuff.
3087 * Takes care of limiting the length and handling 0xa0, which would be
3088 * invisible otherwise.
3089 */
3090 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003091append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003092{
3093 char_u *s = cmd;
3094 char_u *d;
3095
3096 STRCAT(IObuff, ": ");
3097 d = IObuff + STRLEN(IObuff);
3098 while (*s != NUL && d - IObuff < IOSIZE - 7)
3099 {
3100 if (
3101#ifdef FEAT_MBYTE
3102 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3103#endif
3104 *s == 0xa0)
3105 {
3106 s +=
3107#ifdef FEAT_MBYTE
3108 enc_utf8 ? 2 :
3109#endif
3110 1;
3111 STRCPY(d, "<a0>");
3112 d += 4;
3113 }
3114 else
3115 MB_COPY_CHAR(s, d);
3116 }
3117 *d = NUL;
3118}
3119
3120/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003122 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003124 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 * Returns NULL for an ambiguous user command.
3126 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003128find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129{
3130 int len;
3131 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003132 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133
3134 /*
3135 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003136 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 * - the 'k' command can directly be followed by any character.
3138 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003139 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003141 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 */
3143 p = eap->cmd;
3144 if (*p == 'k')
3145 {
3146 eap->cmdidx = CMD_k;
3147 ++p;
3148 }
3149 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003150 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3151 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 || p[1] == 'g'
3153 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3154 || p[1] == 'I'
3155 || (p[1] == 'r' && p[2] != 'e')))
3156 {
3157 eap->cmdidx = CMD_substitute;
3158 ++p;
3159 }
3160 else
3161 {
3162 while (ASCII_ISALPHA(*p))
3163 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003164 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003165 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003166 while (ASCII_ISALNUM(*p))
3167 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003168
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 /* check for non-alpha command */
3170 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3171 ++p;
3172 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003173 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3174 {
3175 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3176 * :delete with the 'l' flag. Same for 'p'. */
3177 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003178 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003179 break;
3180 if (i == len - 1)
3181 {
3182 --len;
3183 if (p[-1] == 'l')
3184 eap->flags |= EXFLAG_LIST;
3185 else
3186 eap->flags |= EXFLAG_PRINT;
3187 }
3188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189
3190 if (ASCII_ISLOWER(*eap->cmd))
3191 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
3192 else
3193 eap->cmdidx = cmdidxs[26];
3194
3195 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3196 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3197 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3198 (size_t)len) == 0)
3199 {
3200#ifdef FEAT_EVAL
3201 if (full != NULL
3202 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3203 *full = TRUE;
3204#endif
3205 break;
3206 }
3207
3208#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003209 /* Look for a user defined command as a last resort. Let ":Print" be
3210 * overruled by a user defined command. */
3211 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3212 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003214 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 while (ASCII_ISALNUM(*p))
3216 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003217 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 }
3219#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003220 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 eap->cmdidx = CMD_SIZE;
3222 }
3223
3224 return p;
3225}
3226
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003227#ifdef FEAT_USR_CMDS
3228/*
3229 * Search for a user command that matches "eap->cmd".
3230 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3231 * Return a pointer to just after the command.
3232 * Return NULL if there is no matching command.
3233 */
3234 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003235find_ucmd(
3236 exarg_T *eap,
3237 char_u *p, /* end of the command (possibly including count) */
3238 int *full, /* set to TRUE for a full match */
3239 expand_T *xp, /* used for completion, NULL otherwise */
3240 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003241{
3242 int len = (int)(p - eap->cmd);
3243 int j, k, matchlen = 0;
3244 ucmd_T *uc;
3245 int found = FALSE;
3246 int possible = FALSE;
3247 char_u *cp, *np; /* Point into typed cmd and test name */
3248 garray_T *gap;
3249 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3250 only full match global is accepted. */
3251
3252 /*
3253 * Look for buffer-local user commands first, then global ones.
3254 */
3255 gap = &curbuf->b_ucmds;
3256 for (;;)
3257 {
3258 for (j = 0; j < gap->ga_len; ++j)
3259 {
3260 uc = USER_CMD_GA(gap, j);
3261 cp = eap->cmd;
3262 np = uc->uc_name;
3263 k = 0;
3264 while (k < len && *np != NUL && *cp++ == *np++)
3265 k++;
3266 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3267 {
3268 /* If finding a second match, the command is ambiguous. But
3269 * not if a buffer-local command wasn't a full match and a
3270 * global command is a full match. */
3271 if (k == len && found && *np != NUL)
3272 {
3273 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003274 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003275 amb_local = TRUE;
3276 }
3277
3278 if (!found || (k == len && *np == NUL))
3279 {
3280 /* If we matched up to a digit, then there could
3281 * be another command including the digit that we
3282 * should use instead.
3283 */
3284 if (k == len)
3285 found = TRUE;
3286 else
3287 possible = TRUE;
3288
3289 if (gap == &ucmds)
3290 eap->cmdidx = CMD_USER;
3291 else
3292 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003293 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003294 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003295 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003296
3297# ifdef FEAT_CMDL_COMPL
3298 if (compl != NULL)
3299 *compl = uc->uc_compl;
3300# ifdef FEAT_EVAL
3301 if (xp != NULL)
3302 {
3303 xp->xp_arg = uc->uc_compl_arg;
3304 xp->xp_scriptID = uc->uc_scriptID;
3305 }
3306# endif
3307# endif
3308 /* Do not search for further abbreviations
3309 * if this is an exact match. */
3310 matchlen = k;
3311 if (k == len && *np == NUL)
3312 {
3313 if (full != NULL)
3314 *full = TRUE;
3315 amb_local = FALSE;
3316 break;
3317 }
3318 }
3319 }
3320 }
3321
3322 /* Stop if we found a full match or searched all. */
3323 if (j < gap->ga_len || gap == &ucmds)
3324 break;
3325 gap = &ucmds;
3326 }
3327
3328 /* Only found ambiguous matches. */
3329 if (amb_local)
3330 {
3331 if (xp != NULL)
3332 xp->xp_context = EXPAND_UNSUCCESSFUL;
3333 return NULL;
3334 }
3335
3336 /* The match we found may be followed immediately by a number. Move "p"
3337 * back to point to it. */
3338 if (found || possible)
3339 return p + (matchlen - len);
3340 return p;
3341}
3342#endif
3343
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003345static struct cmdmod
3346{
3347 char *name;
3348 int minlen;
3349 int has_count; /* :123verbose :3tab */
3350} cmdmods[] = {
3351 {"aboveleft", 3, FALSE},
3352 {"belowright", 3, FALSE},
3353 {"botright", 2, FALSE},
3354 {"browse", 3, FALSE},
3355 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003356 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003357 {"hide", 3, FALSE},
3358 {"keepalt", 5, FALSE},
3359 {"keepjumps", 5, FALSE},
3360 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003361 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003362 {"leftabove", 5, FALSE},
3363 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003364 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003365 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003366 {"rightbelow", 6, FALSE},
3367 {"sandbox", 3, FALSE},
3368 {"silent", 3, FALSE},
3369 {"tab", 3, TRUE},
3370 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003371 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003372 {"verbose", 4, TRUE},
3373 {"vertical", 4, FALSE},
3374};
3375
3376/*
3377 * Return length of a command modifier (including optional count).
3378 * Return zero when it's not a modifier.
3379 */
3380 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003381modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003382{
3383 int i, j;
3384 char_u *p = cmd;
3385
3386 if (VIM_ISDIGIT(*cmd))
3387 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003388 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003389 {
3390 for (j = 0; p[j] != NUL; ++j)
3391 if (p[j] != cmdmods[i].name[j])
3392 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003393 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003394 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003395 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003396 }
3397 return 0;
3398}
3399
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400/*
3401 * Return > 0 if an Ex command "name" exists.
3402 * Return 2 if there is an exact match.
3403 * Return 3 if there is an ambiguous match.
3404 */
3405 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003406cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407{
3408 exarg_T ea;
3409 int full = FALSE;
3410 int i;
3411 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003412 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413
3414 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003415 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 {
3417 for (j = 0; name[j] != NUL; ++j)
3418 if (name[j] != cmdmods[i].name[j])
3419 break;
3420 if (name[j] == NUL && j >= cmdmods[i].minlen)
3421 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3422 }
3423
Bram Moolenaara9587612006-05-04 21:47:50 +00003424 /* Check built-in commands and user defined commands.
3425 * For ":2match" and ":3match" we need to skip the number. */
3426 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003428 p = find_command(&ea, &full);
3429 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003431 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3432 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003433 if (*skipwhite(p) != NUL)
3434 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3436}
3437#endif
3438
3439/*
3440 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3441 * we don't need/want deleted. Maybe this could be done better if we didn't
3442 * repeat all this stuff. The only problem is that they may not stay
3443 * perfectly compatible with each other, but then the command line syntax
3444 * probably won't change that much -- webb.
3445 */
3446 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003447set_one_cmd_context(
3448 expand_T *xp,
3449 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450{
3451 char_u *p;
3452 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003453 int len = 0;
3454 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3456 int compl = EXPAND_NOTHING;
3457#endif
3458#ifdef FEAT_CMDL_COMPL
3459 int delim;
3460#endif
3461 int forceit = FALSE;
3462 int usefilter = FALSE; /* filter instead of file name */
3463
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003464 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 xp->xp_pattern = buff;
3466 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003467 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468
3469/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003470 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 */
3472 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3473 ;
3474 xp->xp_pattern = cmd;
3475
3476 if (*cmd == NUL)
3477 return NULL;
3478 if (*cmd == '"') /* ignore comment lines */
3479 {
3480 xp->xp_context = EXPAND_NOTHING;
3481 return NULL;
3482 }
3483
3484/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003485 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 */
3487 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 xp->xp_pattern = cmd;
3489 if (*cmd == NUL)
3490 return NULL;
3491 if (*cmd == '"')
3492 {
3493 xp->xp_context = EXPAND_NOTHING;
3494 return NULL;
3495 }
3496
3497 if (*cmd == '|' || *cmd == '\n')
3498 return cmd + 1; /* There's another command */
3499
3500 /*
3501 * Isolate the command and search for it in the command table.
3502 * Exceptions:
3503 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003504 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3506 */
3507 if (*cmd == 'k' && cmd[1] != 'e')
3508 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003509 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 p = cmd + 1;
3511 }
3512 else
3513 {
3514 p = cmd;
3515 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3516 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003517 /* a user command may contain digits */
3518 if (ASCII_ISUPPER(cmd[0]))
3519 while (ASCII_ISALNUM(*p) || *p == '*')
3520 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003521 /* for python 3.x: ":py3*" commands completion */
3522 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003523 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003524 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003525 while (ASCII_ISALPHA(*p) || *p == '*')
3526 ++p;
3527 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003528 /* check for non-alpha command */
3529 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3530 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003531 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003533 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 {
3535 xp->xp_context = EXPAND_UNSUCCESSFUL;
3536 return NULL;
3537 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003538 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003539 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3540 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3541 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 break;
3543
3544#ifdef FEAT_USR_CMDS
3545 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3547 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548#endif
3549 }
3550
3551 /*
3552 * If the cursor is touching the command, and it ends in an alpha-numeric
3553 * character, complete the command name.
3554 */
3555 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3556 return NULL;
3557
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003558 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 {
3560 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3561 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003562 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 p = cmd + 1;
3564 }
3565#ifdef FEAT_USR_CMDS
3566 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3567 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003568 ea.cmd = cmd;
3569 p = find_ucmd(&ea, p, NULL, xp,
3570# if defined(FEAT_CMDL_COMPL)
3571 &compl
3572# else
3573 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003575 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003576 if (p == NULL)
3577 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 }
3579#endif
3580 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003581 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 {
3583 /* Not still touching the command and it was an illegal one */
3584 xp->xp_context = EXPAND_UNSUCCESSFUL;
3585 return NULL;
3586 }
3587
3588 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3589
3590 if (*p == '!') /* forced commands */
3591 {
3592 forceit = TRUE;
3593 ++p;
3594 }
3595
3596/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003597 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003599 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003600 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601
3602 arg = skipwhite(p);
3603
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003604 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 {
3606 if (*arg == '>') /* append */
3607 {
3608 if (*++arg == '>')
3609 ++arg;
3610 arg = skipwhite(arg);
3611 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003612 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 {
3614 ++arg;
3615 usefilter = TRUE;
3616 }
3617 }
3618
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003619 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 {
3621 usefilter = forceit; /* :r! filter if forced */
3622 if (*arg == '!') /* :r !filter */
3623 {
3624 ++arg;
3625 usefilter = TRUE;
3626 }
3627 }
3628
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003629 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 {
3631 while (*arg == *cmd) /* allow any number of '>' or '<' */
3632 ++arg;
3633 arg = skipwhite(arg);
3634 }
3635
3636 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003637 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 {
3639 /* Check if we're in the +command */
3640 p = arg + 1;
3641 arg = skip_cmd_arg(arg, FALSE);
3642
3643 /* Still touching the command after '+'? */
3644 if (*arg == NUL)
3645 return p;
3646
3647 /* Skip space(s) after +command to get to the real argument */
3648 arg = skipwhite(arg);
3649 }
3650
3651 /*
3652 * Check for '|' to separate commands and '"' to start comments.
3653 * Don't do this for ":read !cmd" and ":write !cmd".
3654 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003655 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 {
3657 p = arg;
3658 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003659 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 p += 2;
3661 while (*p)
3662 {
3663 if (*p == Ctrl_V)
3664 {
3665 if (p[1] != NUL)
3666 ++p;
3667 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003668 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 || *p == '|' || *p == '\n')
3670 {
3671 if (*(p - 1) != '\\')
3672 {
3673 if (*p == '|' || *p == '\n')
3674 return p + 1;
3675 return NULL; /* It's a comment */
3676 }
3677 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003678 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 }
3680 }
3681
3682 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003683 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 vim_strchr((char_u *)"|\"", *arg) == NULL)
3685 return NULL;
3686
3687 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003688 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003690 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003691 while (*p && p < buff + len)
3692 {
3693 if (*p == ' ' || *p == TAB)
3694 {
3695 /* argument starts after a space */
3696 xp->xp_pattern = ++p;
3697 }
3698 else
3699 {
3700 if (*p == '\\' && *(p + 1) != NUL)
3701 ++p; /* skip over escaped character */
3702 mb_ptr_adv(p);
3703 }
3704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003706 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003708 int c;
3709 int in_quote = FALSE;
3710 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711
3712 /*
3713 * Allow spaces within back-quotes to count as part of the argument
3714 * being expanded.
3715 */
3716 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003717 p = xp->xp_pattern;
3718 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003720#ifdef FEAT_MBYTE
3721 if (has_mbyte)
3722 c = mb_ptr2char(p);
3723 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003725 c = *p;
3726 if (c == '\\' && p[1] != NUL)
3727 ++p;
3728 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 {
3730 if (!in_quote)
3731 {
3732 xp->xp_pattern = p;
3733 bow = p + 1;
3734 }
3735 in_quote = !in_quote;
3736 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003737 /* An argument can contain just about everything, except
3738 * characters that end the command and white space. */
3739 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003740#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003741 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003742#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003743 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003744 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003745 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003746 while (*p != NUL)
3747 {
3748#ifdef FEAT_MBYTE
3749 if (has_mbyte)
3750 c = mb_ptr2char(p);
3751 else
3752#endif
3753 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003754 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003755 break;
3756#ifdef FEAT_MBYTE
3757 if (has_mbyte)
3758 len = (*mb_ptr2len)(p);
3759 else
3760#endif
3761 len = 1;
3762 mb_ptr_adv(p);
3763 }
3764 if (in_quote)
3765 bow = p;
3766 else
3767 xp->xp_pattern = p;
3768 p -= len;
3769 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003770 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 }
3772
3773 /*
3774 * If we are still inside the quotes, and we passed a space, just
3775 * expand from there.
3776 */
3777 if (bow != NULL && in_quote)
3778 xp->xp_pattern = bow;
3779 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003780
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003781 /* For a shell command more chars need to be escaped. */
3782 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003783 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003784#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003785 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003786#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003787 /* When still after the command name expand executables. */
3788 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003789 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791
3792 /* Check for environment variable */
3793 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003794#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 || *xp->xp_pattern == '%'
3796#endif
3797 )
3798 {
3799 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3800 if (!vim_isIDc(*p))
3801 break;
3802 if (*p == NUL)
3803 {
3804 xp->xp_context = EXPAND_ENV_VARS;
3805 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003806#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3807 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003808 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003809 compl = EXPAND_ENV_VARS;
3810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 }
3812 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003813#if defined(FEAT_CMDL_COMPL)
3814 /* Check for user names */
3815 if (*xp->xp_pattern == '~')
3816 {
3817 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3818 ;
3819 /* Complete ~user only if it partially matches a user name.
3820 * A full match ~user<Tab> will be replaced by user's home
3821 * directory i.e. something like ~user<Tab> -> /home/user/ */
3822 if (*p == NUL && p > xp->xp_pattern + 1
3823 && match_user(xp->xp_pattern + 1) == 1)
3824 {
3825 xp->xp_context = EXPAND_USER;
3826 ++xp->xp_pattern;
3827 }
3828 }
3829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 }
3831
3832/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003833 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003835 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003837 case CMD_find:
3838 case CMD_sfind:
3839 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003840 if (xp->xp_context == EXPAND_FILES)
3841 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003842 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 case CMD_cd:
3844 case CMD_chdir:
3845 case CMD_lcd:
3846 case CMD_lchdir:
3847 if (xp->xp_context == EXPAND_FILES)
3848 xp->xp_context = EXPAND_DIRECTORIES;
3849 break;
3850 case CMD_help:
3851 xp->xp_context = EXPAND_HELP;
3852 xp->xp_pattern = arg;
3853 break;
3854
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003855 /* Command modifiers: return the argument.
3856 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003858 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 case CMD_belowright:
3860 case CMD_botright:
3861 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003862 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003863 case CMD_cdo:
3864 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003866 case CMD_debug:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003867 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 case CMD_folddoclosed:
3869 case CMD_folddoopen:
3870 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003871 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 case CMD_keepjumps:
3873 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003874 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003875 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003877 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003879 case CMD_noautocmd:
3880 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003882 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003884 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003885 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 case CMD_topleft:
3887 case CMD_verbose:
3888 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003889 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 return arg;
3891
Bram Moolenaar4f688582007-07-24 12:34:30 +00003892#ifdef FEAT_CMDL_COMPL
3893# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 case CMD_match:
3895 if (*arg == NUL || !ends_excmd(*arg))
3896 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003897 /* also complete "None" */
3898 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 arg = skipwhite(skiptowhite(arg));
3900 if (*arg != NUL)
3901 {
3902 xp->xp_context = EXPAND_NOTHING;
3903 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3904 }
3905 }
3906 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003907# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909/*
3910 * All completion for the +cmdline_compl feature goes here.
3911 */
3912
3913# ifdef FEAT_USR_CMDS
3914 case CMD_command:
3915 /* Check for attributes */
3916 while (*arg == '-')
3917 {
3918 arg++; /* Skip "-" */
3919 p = skiptowhite(arg);
3920 if (*p == NUL)
3921 {
3922 /* Cursor is still in the attribute */
3923 p = vim_strchr(arg, '=');
3924 if (p == NULL)
3925 {
3926 /* No "=", so complete attribute names */
3927 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3928 xp->xp_pattern = arg;
3929 return NULL;
3930 }
3931
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003932 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 * their arguments as well.
3934 */
3935 if (STRNICMP(arg, "complete", p - arg) == 0)
3936 {
3937 xp->xp_context = EXPAND_USER_COMPLETE;
3938 xp->xp_pattern = p + 1;
3939 return NULL;
3940 }
3941 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3942 {
3943 xp->xp_context = EXPAND_USER_NARGS;
3944 xp->xp_pattern = p + 1;
3945 return NULL;
3946 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003947 else if (STRNICMP(arg, "addr", p - arg) == 0)
3948 {
3949 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3950 xp->xp_pattern = p + 1;
3951 return NULL;
3952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 return NULL;
3954 }
3955 arg = skipwhite(p);
3956 }
3957
3958 /* After the attributes comes the new command name */
3959 p = skiptowhite(arg);
3960 if (*p == NUL)
3961 {
3962 xp->xp_context = EXPAND_USER_COMMANDS;
3963 xp->xp_pattern = arg;
3964 break;
3965 }
3966
3967 /* And finally comes a normal command */
3968 return skipwhite(p);
3969
3970 case CMD_delcommand:
3971 xp->xp_context = EXPAND_USER_COMMANDS;
3972 xp->xp_pattern = arg;
3973 break;
3974# endif
3975
3976 case CMD_global:
3977 case CMD_vglobal:
3978 delim = *arg; /* get the delimiter */
3979 if (delim)
3980 ++arg; /* skip delimiter if there is one */
3981
3982 while (arg[0] != NUL && arg[0] != delim)
3983 {
3984 if (arg[0] == '\\' && arg[1] != NUL)
3985 ++arg;
3986 ++arg;
3987 }
3988 if (arg[0] != NUL)
3989 return arg + 1;
3990 break;
3991 case CMD_and:
3992 case CMD_substitute:
3993 delim = *arg;
3994 if (delim)
3995 {
3996 /* skip "from" part */
3997 ++arg;
3998 arg = skip_regexp(arg, delim, p_magic, NULL);
3999 }
4000 /* skip "to" part */
4001 while (arg[0] != NUL && arg[0] != delim)
4002 {
4003 if (arg[0] == '\\' && arg[1] != NUL)
4004 ++arg;
4005 ++arg;
4006 }
4007 if (arg[0] != NUL) /* skip delimiter */
4008 ++arg;
4009 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
4010 ++arg;
4011 if (arg[0] != NUL)
4012 return arg;
4013 break;
4014 case CMD_isearch:
4015 case CMD_dsearch:
4016 case CMD_ilist:
4017 case CMD_dlist:
4018 case CMD_ijump:
4019 case CMD_psearch:
4020 case CMD_djump:
4021 case CMD_isplit:
4022 case CMD_dsplit:
4023 arg = skipwhite(skipdigits(arg)); /* skip count */
4024 if (*arg == '/') /* Match regexp, not just whole words */
4025 {
4026 for (++arg; *arg && *arg != '/'; arg++)
4027 if (*arg == '\\' && arg[1] != NUL)
4028 arg++;
4029 if (*arg)
4030 {
4031 arg = skipwhite(arg + 1);
4032
4033 /* Check for trailing illegal characters */
4034 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4035 xp->xp_context = EXPAND_NOTHING;
4036 else
4037 return arg;
4038 }
4039 }
4040 break;
4041#ifdef FEAT_AUTOCMD
4042 case CMD_autocmd:
4043 return set_context_in_autocmd(xp, arg, FALSE);
4044
4045 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004046 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 return set_context_in_autocmd(xp, arg, TRUE);
4048#endif
4049 case CMD_set:
4050 set_context_in_set_cmd(xp, arg, 0);
4051 break;
4052 case CMD_setglobal:
4053 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4054 break;
4055 case CMD_setlocal:
4056 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4057 break;
4058 case CMD_tag:
4059 case CMD_stag:
4060 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004061 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 case CMD_tselect:
4063 case CMD_stselect:
4064 case CMD_ptselect:
4065 case CMD_tjump:
4066 case CMD_stjump:
4067 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004068 if (*p_wop != NUL)
4069 xp->xp_context = EXPAND_TAGS_LISTFILES;
4070 else
4071 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 xp->xp_pattern = arg;
4073 break;
4074 case CMD_augroup:
4075 xp->xp_context = EXPAND_AUGROUP;
4076 xp->xp_pattern = arg;
4077 break;
4078#ifdef FEAT_SYN_HL
4079 case CMD_syntax:
4080 set_context_in_syntax_cmd(xp, arg);
4081 break;
4082#endif
4083#ifdef FEAT_EVAL
4084 case CMD_let:
4085 case CMD_if:
4086 case CMD_elseif:
4087 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004088 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 case CMD_echo:
4090 case CMD_echon:
4091 case CMD_execute:
4092 case CMD_echomsg:
4093 case CMD_echoerr:
4094 case CMD_call:
4095 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004096 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 break;
4098
4099 case CMD_unlet:
4100 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4101 arg = xp->xp_pattern + 1;
4102 xp->xp_context = EXPAND_USER_VARS;
4103 xp->xp_pattern = arg;
4104 break;
4105
4106 case CMD_function:
4107 case CMD_delfunction:
4108 xp->xp_context = EXPAND_USER_FUNC;
4109 xp->xp_pattern = arg;
4110 break;
4111
4112 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004113 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 break;
4115#endif
4116 case CMD_highlight:
4117 set_context_in_highlight_cmd(xp, arg);
4118 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004119#ifdef FEAT_CSCOPE
4120 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004121 case CMD_lcscope:
4122 case CMD_scscope:
4123 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004124 break;
4125#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004126#ifdef FEAT_SIGNS
4127 case CMD_sign:
4128 set_context_in_sign_cmd(xp, arg);
4129 break;
4130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131#ifdef FEAT_LISTCMDS
4132 case CMD_bdelete:
4133 case CMD_bwipeout:
4134 case CMD_bunload:
4135 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4136 arg = xp->xp_pattern + 1;
4137 /*FALLTHROUGH*/
4138 case CMD_buffer:
4139 case CMD_sbuffer:
4140 case CMD_checktime:
4141 xp->xp_context = EXPAND_BUFFERS;
4142 xp->xp_pattern = arg;
4143 break;
4144#endif
4145#ifdef FEAT_USR_CMDS
4146 case CMD_USER:
4147 case CMD_USER_BUF:
4148 if (compl != EXPAND_NOTHING)
4149 {
4150 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004151 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 {
4153# ifdef FEAT_MENU
4154 if (compl == EXPAND_MENUS)
4155 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4156# endif
4157 if (compl == EXPAND_COMMANDS)
4158 return arg;
4159 if (compl == EXPAND_MAPPINGS)
4160 return set_context_in_map_cmd(xp, (char_u *)"map",
4161 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004162 /* Find start of last argument. */
4163 p = arg;
4164 while (*p)
4165 {
4166 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004167 /* argument starts after a space */
4168 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004169 else if (*p == '\\' && *(p + 1) != NUL)
4170 ++p; /* skip over escaped character */
4171 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 xp->xp_pattern = arg;
4174 }
4175 xp->xp_context = compl;
4176 }
4177 break;
4178#endif
4179 case CMD_map: case CMD_noremap:
4180 case CMD_nmap: case CMD_nnoremap:
4181 case CMD_vmap: case CMD_vnoremap:
4182 case CMD_omap: case CMD_onoremap:
4183 case CMD_imap: case CMD_inoremap:
4184 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004185 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004186 case CMD_smap: case CMD_snoremap:
4187 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004189 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 case CMD_unmap:
4191 case CMD_nunmap:
4192 case CMD_vunmap:
4193 case CMD_ounmap:
4194 case CMD_iunmap:
4195 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004196 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004197 case CMD_sunmap:
4198 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004200 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 case CMD_abbreviate: case CMD_noreabbrev:
4202 case CMD_cabbrev: case CMD_cnoreabbrev:
4203 case CMD_iabbrev: case CMD_inoreabbrev:
4204 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004205 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 case CMD_unabbreviate:
4207 case CMD_cunabbrev:
4208 case CMD_iunabbrev:
4209 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004210 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211#ifdef FEAT_MENU
4212 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4213 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4214 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4215 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4216 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4217 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4218 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4219 case CMD_tmenu: case CMD_tunmenu:
4220 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4221 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4222#endif
4223
4224 case CMD_colorscheme:
4225 xp->xp_context = EXPAND_COLORS;
4226 xp->xp_pattern = arg;
4227 break;
4228
4229 case CMD_compiler:
4230 xp->xp_context = EXPAND_COMPILER;
4231 xp->xp_pattern = arg;
4232 break;
4233
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004234 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004235 xp->xp_context = EXPAND_OWNSYNTAX;
4236 xp->xp_pattern = arg;
4237 break;
4238
4239 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004240 xp->xp_context = EXPAND_FILETYPE;
4241 xp->xp_pattern = arg;
4242 break;
4243
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004244 case CMD_packadd:
4245 xp->xp_context = EXPAND_PACKADD;
4246 xp->xp_pattern = arg;
4247 break;
4248
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4250 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4251 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004252 p = skiptowhite(arg);
4253 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 {
4255 xp->xp_context = EXPAND_LANGUAGE;
4256 xp->xp_pattern = arg;
4257 }
4258 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004259 {
4260 if ( STRNCMP(arg, "messages", p - arg) == 0
4261 || STRNCMP(arg, "ctype", p - arg) == 0
4262 || STRNCMP(arg, "time", p - arg) == 0)
4263 {
4264 xp->xp_context = EXPAND_LOCALES;
4265 xp->xp_pattern = skipwhite(p);
4266 }
4267 else
4268 xp->xp_context = EXPAND_NOTHING;
4269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 break;
4271#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004272#if defined(FEAT_PROFILE)
4273 case CMD_profile:
4274 set_context_in_profile_cmd(xp, arg);
4275 break;
4276#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004277 case CMD_behave:
4278 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004279 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004280 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004282 case CMD_messages:
4283 xp->xp_context = EXPAND_MESSAGES;
4284 xp->xp_pattern = arg;
4285 break;
4286
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004287#if defined(FEAT_CMDHIST)
4288 case CMD_history:
4289 xp->xp_context = EXPAND_HISTORY;
4290 xp->xp_pattern = arg;
4291 break;
4292#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004293#if defined(FEAT_PROFILE)
4294 case CMD_syntime:
4295 xp->xp_context = EXPAND_SYNTIME;
4296 xp->xp_pattern = arg;
4297 break;
4298#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004299
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300#endif /* FEAT_CMDL_COMPL */
4301
4302 default:
4303 break;
4304 }
4305 return NULL;
4306}
4307
4308/*
4309 * skip a range specifier of the form: addr [,addr] [;addr] ..
4310 *
4311 * Backslashed delimiters after / or ? will be skipped, and commands will
4312 * not be expanded between /'s and ?'s or after "'".
4313 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004314 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 * Returns the "cmd" pointer advanced to beyond the range.
4316 */
4317 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004318skip_range(
4319 char_u *cmd,
4320 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004322 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323
Bram Moolenaardf177f62005-02-22 08:39:57 +00004324 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 {
4326 if (*cmd == '\'')
4327 {
4328 if (*++cmd == NUL && ctx != NULL)
4329 *ctx = EXPAND_NOTHING;
4330 }
4331 else if (*cmd == '/' || *cmd == '?')
4332 {
4333 delim = *cmd++;
4334 while (*cmd != NUL && *cmd != delim)
4335 if (*cmd++ == '\\' && *cmd != NUL)
4336 ++cmd;
4337 if (*cmd == NUL && ctx != NULL)
4338 *ctx = EXPAND_NOTHING;
4339 }
4340 if (*cmd != NUL)
4341 ++cmd;
4342 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004343
4344 /* Skip ":" and white space. */
4345 while (*cmd == ':')
4346 cmd = skipwhite(cmd + 1);
4347
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 return cmd;
4349}
4350
4351/*
4352 * get a single EX address
4353 *
4354 * Set ptr to the next character after the part that was interpreted.
4355 * Set ptr to NULL when an error is encountered.
4356 *
4357 * Return MAXLNUM when no Ex address was found.
4358 */
4359 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004360get_address(
4361 exarg_T *eap UNUSED,
4362 char_u **ptr,
4363 int addr_type, /* flag: one of ADDR_LINES, ... */
4364 int skip, /* only skip the address, don't use it */
4365 int to_other_file) /* flag: may jump to other file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366{
4367 int c;
4368 int i;
4369 long n;
4370 char_u *cmd;
4371 pos_T pos;
4372 pos_T *fp;
4373 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004374 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375
4376 cmd = skipwhite(*ptr);
4377 lnum = MAXLNUM;
4378 do
4379 {
4380 switch (*cmd)
4381 {
4382 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004383 ++cmd;
4384 switch (addr_type)
4385 {
4386 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 lnum = curwin->w_cursor.lnum;
4388 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004389 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004390 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004391 break;
4392 case ADDR_ARGUMENTS:
4393 lnum = curwin->w_arg_idx + 1;
4394 break;
4395 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004396 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004397 lnum = curbuf->b_fnum;
4398 break;
4399 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004400 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004401 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004402#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004403 case ADDR_QUICKFIX:
4404 lnum = qf_get_cur_valid_idx(eap);
4405 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004406#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004407 }
4408 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409
4410 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004411 ++cmd;
4412 switch (addr_type)
4413 {
4414 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 lnum = curbuf->b_ml.ml_line_count;
4416 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004417 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004418 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004419 break;
4420 case ADDR_ARGUMENTS:
4421 lnum = ARGCOUNT;
4422 break;
4423 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004424 buf = lastbuf;
4425 while (buf->b_ml.ml_mfp == NULL)
4426 {
4427 if (buf->b_prev == NULL)
4428 break;
4429 buf = buf->b_prev;
4430 }
4431 lnum = buf->b_fnum;
4432 break;
4433 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004434 lnum = lastbuf->b_fnum;
4435 break;
4436 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004437 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004438 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004439#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004440 case ADDR_QUICKFIX:
4441 lnum = qf_get_size(eap);
4442 if (lnum == 0)
4443 lnum = 1;
4444 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004445#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004446 }
4447 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448
4449 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004450 if (*++cmd == NUL)
4451 {
4452 cmd = NULL;
4453 goto error;
4454 }
4455 if (addr_type != ADDR_LINES)
4456 {
4457 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004458 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004459 goto error;
4460 }
4461 if (skip)
4462 ++cmd;
4463 else
4464 {
4465 /* Only accept a mark in another file when it is
4466 * used by itself: ":'M". */
4467 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4468 ++cmd;
4469 if (fp == (pos_T *)-1)
4470 /* Jumped to another file. */
4471 lnum = curwin->w_cursor.lnum;
4472 else
4473 {
4474 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 {
4476 cmd = NULL;
4477 goto error;
4478 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004479 lnum = fp->lnum;
4480 }
4481 }
4482 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483
4484 case '/':
4485 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004486 c = *cmd++;
4487 if (addr_type != ADDR_LINES)
4488 {
4489 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004490 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004491 goto error;
4492 }
4493 if (skip) /* skip "/pat/" */
4494 {
4495 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4496 if (*cmd == c)
4497 ++cmd;
4498 }
4499 else
4500 {
4501 pos = curwin->w_cursor; /* save curwin->w_cursor */
4502 /*
4503 * When '/' or '?' follows another address, start
4504 * from there.
4505 */
4506 if (lnum != MAXLNUM)
4507 curwin->w_cursor.lnum = lnum;
4508 /*
4509 * Start a forward search at the end of the line.
4510 * Start a backward search at the start of the line.
4511 * This makes sure we never match in the current
4512 * line, and can match anywhere in the
4513 * next/previous line.
4514 */
4515 if (c == '/')
4516 curwin->w_cursor.col = MAXCOL;
4517 else
4518 curwin->w_cursor.col = 0;
4519 searchcmdlen = 0;
4520 if (!do_search(NULL, c, cmd, 1L,
4521 SEARCH_HIS | SEARCH_MSG, NULL))
4522 {
4523 curwin->w_cursor = pos;
4524 cmd = NULL;
4525 goto error;
4526 }
4527 lnum = curwin->w_cursor.lnum;
4528 curwin->w_cursor = pos;
4529 /* adjust command string pointer */
4530 cmd += searchcmdlen;
4531 }
4532 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533
4534 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004535 ++cmd;
4536 if (addr_type != ADDR_LINES)
4537 {
4538 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004539 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004540 goto error;
4541 }
4542 if (*cmd == '&')
4543 i = RE_SUBST;
4544 else if (*cmd == '?' || *cmd == '/')
4545 i = RE_SEARCH;
4546 else
4547 {
4548 EMSG(_(e_backslash));
4549 cmd = NULL;
4550 goto error;
4551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004553 if (!skip)
4554 {
4555 /*
4556 * When search follows another address, start from
4557 * there.
4558 */
4559 if (lnum != MAXLNUM)
4560 pos.lnum = lnum;
4561 else
4562 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004564 /*
4565 * Start the search just like for the above
4566 * do_search().
4567 */
4568 if (*cmd != '?')
4569 pos.col = MAXCOL;
4570 else
4571 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004572#ifdef FEAT_VIRTUALEDIT
4573 pos.coladd = 0;
4574#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004575 if (searchit(curwin, curbuf, &pos,
4576 *cmd == '?' ? BACKWARD : FORWARD,
4577 (char_u *)"", 1L, SEARCH_MSG,
4578 i, (linenr_T)0, NULL) != FAIL)
4579 lnum = pos.lnum;
4580 else
4581 {
4582 cmd = NULL;
4583 goto error;
4584 }
4585 }
4586 ++cmd;
4587 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588
4589 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004590 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4591 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 }
4593
4594 for (;;)
4595 {
4596 cmd = skipwhite(cmd);
4597 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4598 break;
4599
4600 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004601 {
4602 switch (addr_type)
4603 {
4604 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004605 /* "+1" is same as ".+1" */
4606 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004607 break;
4608 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004609 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004610 break;
4611 case ADDR_ARGUMENTS:
4612 lnum = curwin->w_arg_idx + 1;
4613 break;
4614 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004615 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004616 lnum = curbuf->b_fnum;
4617 break;
4618 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004619 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004620 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004621#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004622 case ADDR_QUICKFIX:
4623 lnum = qf_get_cur_valid_idx(eap);
4624 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004625#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004626 }
4627 }
4628
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 if (VIM_ISDIGIT(*cmd))
4630 i = '+'; /* "number" is same as "+number" */
4631 else
4632 i = *cmd++;
4633 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4634 n = 1;
4635 else
4636 n = getdigits(&cmd);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004637 if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004638 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004639 lnum = compute_buffer_local_count(
4640 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004641 else if (i == '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 lnum -= n;
4643 else
4644 lnum += n;
4645 }
4646 } while (*cmd == '/' || *cmd == '?');
4647
4648error:
4649 *ptr = cmd;
4650 return lnum;
4651}
4652
4653/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004654 * Get flags from an Ex command argument.
4655 */
4656 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004657get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004658{
4659 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4660 {
4661 if (*eap->arg == 'l')
4662 eap->flags |= EXFLAG_LIST;
4663 else if (*eap->arg == 'p')
4664 eap->flags |= EXFLAG_PRINT;
4665 else
4666 eap->flags |= EXFLAG_NR;
4667 eap->arg = skipwhite(eap->arg + 1);
4668 }
4669}
4670
4671/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 * Function called for command which is Not Implemented. NI!
4673 */
4674 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004675ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676{
4677 if (!eap->skip)
4678 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4679}
4680
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004681#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682/*
4683 * Function called for script command which is Not Implemented. NI!
4684 * Skips over ":perl <<EOF" constructs.
4685 */
4686 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004687ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688{
4689 if (!eap->skip)
4690 ex_ni(eap);
4691 else
4692 vim_free(script_get(eap, eap->arg));
4693}
4694#endif
4695
4696/*
4697 * Check range in Ex command for validity.
4698 * Return NULL when valid, error message when invalid.
4699 */
4700 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004701invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004703 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 if ( eap->line1 < 0
4705 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004706 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004708
4709 if (eap->argt & RANGE)
4710 {
4711 switch(eap->addr_type)
4712 {
4713 case ADDR_LINES:
4714 if (!(eap->argt & NOTADR)
4715 && eap->line2 > curbuf->b_ml.ml_line_count
4716#ifdef FEAT_DIFF
4717 + (eap->cmdidx == CMD_diffget)
4718#endif
4719 )
4720 return (char_u *)_(e_invrange);
4721 break;
4722 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004723 /* add 1 if ARGCOUNT is 0 */
4724 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004725 return (char_u *)_(e_invrange);
4726 break;
4727 case ADDR_BUFFERS:
4728 if (eap->line1 < firstbuf->b_fnum
4729 || eap->line2 > lastbuf->b_fnum)
4730 return (char_u *)_(e_invrange);
4731 break;
4732 case ADDR_LOADED_BUFFERS:
4733 buf = firstbuf;
4734 while (buf->b_ml.ml_mfp == NULL)
4735 {
4736 if (buf->b_next == NULL)
4737 return (char_u *)_(e_invrange);
4738 buf = buf->b_next;
4739 }
4740 if (eap->line1 < buf->b_fnum)
4741 return (char_u *)_(e_invrange);
4742 buf = lastbuf;
4743 while (buf->b_ml.ml_mfp == NULL)
4744 {
4745 if (buf->b_prev == NULL)
4746 return (char_u *)_(e_invrange);
4747 buf = buf->b_prev;
4748 }
4749 if (eap->line2 > buf->b_fnum)
4750 return (char_u *)_(e_invrange);
4751 break;
4752 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004753 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004754 return (char_u *)_(e_invrange);
4755 break;
4756 case ADDR_TABS:
4757 if (eap->line2 > LAST_TAB_NR)
4758 return (char_u *)_(e_invrange);
4759 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004760#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004761 case ADDR_QUICKFIX:
4762 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4763 return (char_u *)_(e_invrange);
4764 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004765#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004766 }
4767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 return NULL;
4769}
4770
4771/*
4772 * Correct the range for zero line number, if required.
4773 */
4774 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004775correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776{
4777 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4778 {
4779 if (eap->line1 == 0)
4780 eap->line1 = 1;
4781 if (eap->line2 == 0)
4782 eap->line2 = 1;
4783 }
4784}
4785
Bram Moolenaar748bf032005-02-02 23:04:36 +00004786#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004787static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004788
4789/*
4790 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4791 * pattern. Otherwise return eap->arg.
4792 */
4793 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004794skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004795{
4796 char_u *p = eap->arg;
4797
Bram Moolenaara37420f2006-02-04 22:37:47 +00004798 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4799 || eap->cmdidx == CMD_vimgrepadd
4800 || eap->cmdidx == CMD_lvimgrepadd
4801 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004802 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004803 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004804 if (p == NULL)
4805 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004806 }
4807 return p;
4808}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004809
4810/*
4811 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4812 * in the command line, so that things like % get expanded.
4813 */
4814 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004815replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004816{
4817 char_u *new_cmdline;
4818 char_u *program;
4819 char_u *pos;
4820 char_u *ptr;
4821 int len;
4822 int i;
4823
4824 /*
4825 * Don't do it when ":vimgrep" is used for ":grep".
4826 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004827 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4828 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4829 || eap->cmdidx == CMD_grepadd
4830 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004831 && !grep_internal(eap->cmdidx))
4832 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004833 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4834 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004835 {
4836 if (*curbuf->b_p_gp == NUL)
4837 program = p_gp;
4838 else
4839 program = curbuf->b_p_gp;
4840 }
4841 else
4842 {
4843 if (*curbuf->b_p_mp == NUL)
4844 program = p_mp;
4845 else
4846 program = curbuf->b_p_mp;
4847 }
4848
4849 p = skipwhite(p);
4850
4851 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4852 {
4853 /* replace $* by given arguments */
4854 i = 1;
4855 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4856 ++i;
4857 len = (int)STRLEN(p);
4858 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4859 if (new_cmdline == NULL)
4860 return NULL; /* out of memory */
4861 ptr = new_cmdline;
4862 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4863 {
4864 i = (int)(pos - program);
4865 STRNCPY(ptr, program, i);
4866 STRCPY(ptr += i, p);
4867 ptr += len;
4868 program = pos + 2;
4869 }
4870 STRCPY(ptr, program);
4871 }
4872 else
4873 {
4874 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4875 if (new_cmdline == NULL)
4876 return NULL; /* out of memory */
4877 STRCPY(new_cmdline, program);
4878 STRCAT(new_cmdline, " ");
4879 STRCAT(new_cmdline, p);
4880 }
4881 msg_make(p);
4882
4883 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4884 vim_free(*cmdlinep);
4885 *cmdlinep = new_cmdline;
4886 p = new_cmdline;
4887 }
4888 return p;
4889}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004890#endif
4891
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892/*
4893 * Expand file name in Ex command argument.
4894 * Return FAIL for failure, OK otherwise.
4895 */
4896 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004897expand_filename(
4898 exarg_T *eap,
4899 char_u **cmdlinep,
4900 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901{
4902 int has_wildcards; /* need to expand wildcards */
4903 char_u *repl;
4904 int srclen;
4905 char_u *p;
4906 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004907 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908
Bram Moolenaar748bf032005-02-02 23:04:36 +00004909#ifdef FEAT_QUICKFIX
4910 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4911 p = skip_grep_pat(eap);
4912#else
4913 p = eap->arg;
4914#endif
4915
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 /*
4917 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4918 * the file name contains a wildcard it should not cause expanding.
4919 * (it will be expanded anyway if there is a wildcard before replacing).
4920 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004921 has_wildcards = mch_has_wildcard(p);
4922 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004924#ifdef FEAT_EVAL
4925 /* Skip over `=expr`, wildcards in it are not expanded. */
4926 if (p[0] == '`' && p[1] == '=')
4927 {
4928 p += 2;
4929 (void)skip_expr(&p);
4930 if (*p == '`')
4931 ++p;
4932 continue;
4933 }
4934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 /*
4936 * Quick check if this cannot be the start of a special string.
4937 * Also removes backslash before '%', '#' and '<'.
4938 */
4939 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4940 {
4941 ++p;
4942 continue;
4943 }
4944
4945 /*
4946 * Try to find a match at this position.
4947 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004948 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4949 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 if (*errormsgp != NULL) /* error detected */
4951 return FAIL;
4952 if (repl == NULL) /* no match found */
4953 {
4954 p += srclen;
4955 continue;
4956 }
4957
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004958 /* Wildcards won't be expanded below, the replacement is taken
4959 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4960 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4961 {
4962 char_u *l = repl;
4963
4964 repl = expand_env_save(repl);
4965 vim_free(l);
4966 }
4967
Bram Moolenaar63b92542007-03-27 14:57:09 +00004968 /* Need to escape white space et al. with a backslash.
4969 * Don't do this for:
4970 * - replacement that already has been escaped: "##"
4971 * - shell commands (may have to use quotes instead).
4972 * - non-unix systems when there is a single argument (spaces don't
4973 * separate arguments then).
4974 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004976 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 && eap->cmdidx != CMD_bang
4978 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004979 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004981 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004983 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984#ifndef UNIX
4985 && !(eap->argt & NOSPC)
4986#endif
4987 )
4988 {
4989 char_u *l;
4990#ifdef BACKSLASH_IN_FILENAME
4991 /* Don't escape a backslash here, because rem_backslash() doesn't
4992 * remove it later. */
4993 static char_u *nobslash = (char_u *)" \t\"|";
4994# define ESCAPE_CHARS nobslash
4995#else
4996# define ESCAPE_CHARS escape_chars
4997#endif
4998
4999 for (l = repl; *l; ++l)
5000 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5001 {
5002 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5003 if (l != NULL)
5004 {
5005 vim_free(repl);
5006 repl = l;
5007 }
5008 break;
5009 }
5010 }
5011
5012 /* For a shell command a '!' must be escaped. */
5013 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005014 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 {
5016 char_u *l;
5017
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005018 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 if (l != NULL)
5020 {
5021 vim_free(repl);
5022 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 }
5024 }
5025
5026 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5027 vim_free(repl);
5028 if (p == NULL)
5029 return FAIL;
5030 }
5031
5032 /*
5033 * One file argument: Expand wildcards.
5034 * Don't do this with ":r !command" or ":w !command".
5035 */
5036 if ((eap->argt & NOSPC) && !eap->usefilter)
5037 {
5038 /*
5039 * May do this twice:
5040 * 1. Replace environment variables.
5041 * 2. Replace any other wildcards, remove backslashes.
5042 */
5043 for (n = 1; n <= 2; ++n)
5044 {
5045 if (n == 2)
5046 {
5047#ifdef UNIX
5048 /*
5049 * Only for Unix we check for more than one file name.
5050 * For other systems spaces are considered to be part
5051 * of the file name.
5052 * Only check here if there is no wildcard, otherwise
5053 * ExpandOne() will check for errors. This allows
5054 * ":e `ls ve*.c`" on Unix.
5055 */
5056 if (!has_wildcards)
5057 for (p = eap->arg; *p; ++p)
5058 {
5059 /* skip escaped characters */
5060 if (p[1] && (*p == '\\' || *p == Ctrl_V))
5061 ++p;
5062 else if (vim_iswhite(*p))
5063 {
5064 *errormsgp = (char_u *)_("E172: Only one file name allowed");
5065 return FAIL;
5066 }
5067 }
5068#endif
5069
5070 /*
5071 * Halve the number of backslashes (this is Vi compatible).
5072 * For Unix and OS/2, when wildcards are expanded, this is
5073 * done by ExpandOne() below.
5074 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005075#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 if (!has_wildcards)
5077#endif
5078 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 }
5080
5081 if (has_wildcards)
5082 {
5083 if (n == 1)
5084 {
5085 /*
5086 * First loop: May expand environment variables. This
5087 * can be done much faster with expand_env() than with
5088 * something else (e.g., calling a shell).
5089 * After expanding environment variables, check again
5090 * if there are still wildcards present.
5091 */
5092 if (vim_strchr(eap->arg, '$') != NULL
5093 || vim_strchr(eap->arg, '~') != NULL)
5094 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005095 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005096 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 has_wildcards = mch_has_wildcard(NameBuff);
5098 p = NameBuff;
5099 }
5100 else
5101 p = NULL;
5102 }
5103 else /* n == 2 */
5104 {
5105 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005106 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107
5108 ExpandInit(&xpc);
5109 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005110 if (p_wic)
5111 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005113 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 if (p == NULL)
5115 return FAIL;
5116 }
5117 if (p != NULL)
5118 {
5119 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5120 p, cmdlinep);
5121 if (n == 2) /* p came from ExpandOne() */
5122 vim_free(p);
5123 }
5124 }
5125 }
5126 }
5127 return OK;
5128}
5129
5130/*
5131 * Replace part of the command line, keeping eap->cmd, eap->arg and
5132 * eap->nextcmd correct.
5133 * "src" points to the part that is to be replaced, of length "srclen".
5134 * "repl" is the replacement string.
5135 * Returns a pointer to the character after the replaced string.
5136 * Returns NULL for failure.
5137 */
5138 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005139repl_cmdline(
5140 exarg_T *eap,
5141 char_u *src,
5142 int srclen,
5143 char_u *repl,
5144 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145{
5146 int len;
5147 int i;
5148 char_u *new_cmdline;
5149
5150 /*
5151 * The new command line is build in new_cmdline[].
5152 * First allocate it.
5153 * Careful: a "+cmd" argument may have been NUL terminated.
5154 */
5155 len = (int)STRLEN(repl);
5156 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005157 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5159 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5160 return NULL; /* out of memory! */
5161
5162 /*
5163 * Copy the stuff before the expanded part.
5164 * Copy the expanded stuff.
5165 * Copy what came after the expanded part.
5166 * Copy the next commands, if there are any.
5167 */
5168 i = (int)(src - *cmdlinep); /* length of part before match */
5169 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005170
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 mch_memmove(new_cmdline + i, repl, (size_t)len);
5172 i += len; /* remember the end of the string */
5173 STRCPY(new_cmdline + i, src + srclen);
5174 src = new_cmdline + i; /* remember where to continue */
5175
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005176 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 {
5178 i = (int)STRLEN(new_cmdline) + 1;
5179 STRCPY(new_cmdline + i, eap->nextcmd);
5180 eap->nextcmd = new_cmdline + i;
5181 }
5182 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5183 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5184 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5185 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5186 vim_free(*cmdlinep);
5187 *cmdlinep = new_cmdline;
5188
5189 return src;
5190}
5191
5192/*
5193 * Check for '|' to separate commands and '"' to start comments.
5194 */
5195 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005196separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197{
5198 char_u *p;
5199
Bram Moolenaar86b68352004-12-27 21:59:20 +00005200#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005201 p = skip_grep_pat(eap);
5202#else
5203 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005204#endif
5205
5206 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 {
5208 if (*p == Ctrl_V)
5209 {
5210 if (eap->argt & (USECTRLV | XFILE))
5211 ++p; /* skip CTRL-V and next char */
5212 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005213 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005214 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 if (*p == NUL) /* stop at NUL after CTRL-V */
5216 break;
5217 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005218
5219#ifdef FEAT_EVAL
5220 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005221 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005222 {
5223 p += 2;
5224 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005225 }
5226#endif
5227
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 /* Check for '"': start of comment or '|': next command */
5229 /* :@" and :*" do not start a comment!
5230 * :redir @" doesn't either. */
5231 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5232 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5233 || p != eap->arg)
5234 && (eap->cmdidx != CMD_redir
5235 || p != eap->arg + 1 || p[-1] != '@'))
5236 || *p == '|' || *p == '\n')
5237 {
5238 /*
5239 * We remove the '\' before the '|', unless USECTRLV is used
5240 * AND 'b' is present in 'cpoptions'.
5241 */
5242 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5243 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5244 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005245 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 --p;
5247 }
5248 else
5249 {
5250 eap->nextcmd = check_nextcmd(p);
5251 *p = NUL;
5252 break;
5253 }
5254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005256
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5258 del_trailing_spaces(eap->arg);
5259}
5260
5261/*
5262 * get + command from ex argument
5263 */
5264 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005265getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266{
5267 char_u *arg = *argp;
5268 char_u *command = NULL;
5269
5270 if (*arg == '+') /* +[command] */
5271 {
5272 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005273 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 command = dollar_command;
5275 else
5276 {
5277 command = arg;
5278 arg = skip_cmd_arg(command, TRUE);
5279 if (*arg != NUL)
5280 *arg++ = NUL; /* terminate command with NUL */
5281 }
5282
5283 arg = skipwhite(arg); /* skip over spaces */
5284 *argp = arg;
5285 }
5286 return command;
5287}
5288
5289/*
5290 * Find end of "+command" argument. Skip over "\ " and "\\".
5291 */
5292 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005293skip_cmd_arg(
5294 char_u *p,
5295 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296{
5297 while (*p && !vim_isspace(*p))
5298 {
5299 if (*p == '\\' && p[1] != NUL)
5300 {
5301 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005302 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 else
5304 ++p;
5305 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005306 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 }
5308 return p;
5309}
5310
5311/*
5312 * Get "++opt=arg" argument.
5313 * Return FAIL or OK.
5314 */
5315 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005316getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317{
5318 char_u *arg = eap->arg + 2;
5319 int *pp = NULL;
5320#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005321 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 char_u *p;
5323#endif
5324
5325 /* ":edit ++[no]bin[ary] file" */
5326 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5327 {
5328 if (*arg == 'n')
5329 {
5330 arg += 2;
5331 eap->force_bin = FORCE_NOBIN;
5332 }
5333 else
5334 eap->force_bin = FORCE_BIN;
5335 if (!checkforcmd(&arg, "binary", 3))
5336 return FAIL;
5337 eap->arg = skipwhite(arg);
5338 return OK;
5339 }
5340
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005341 /* ":read ++edit file" */
5342 if (STRNCMP(arg, "edit", 4) == 0)
5343 {
5344 eap->read_edit = TRUE;
5345 eap->arg = skipwhite(arg + 4);
5346 return OK;
5347 }
5348
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 if (STRNCMP(arg, "ff", 2) == 0)
5350 {
5351 arg += 2;
5352 pp = &eap->force_ff;
5353 }
5354 else if (STRNCMP(arg, "fileformat", 10) == 0)
5355 {
5356 arg += 10;
5357 pp = &eap->force_ff;
5358 }
5359#ifdef FEAT_MBYTE
5360 else if (STRNCMP(arg, "enc", 3) == 0)
5361 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005362 if (STRNCMP(arg, "encoding", 8) == 0)
5363 arg += 8;
5364 else
5365 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 pp = &eap->force_enc;
5367 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005368 else if (STRNCMP(arg, "bad", 3) == 0)
5369 {
5370 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005371 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373#endif
5374
5375 if (pp == NULL || *arg != '=')
5376 return FAIL;
5377
5378 ++arg;
5379 *pp = (int)(arg - eap->cmd);
5380 arg = skip_cmd_arg(arg, FALSE);
5381 eap->arg = skipwhite(arg);
5382 *arg = NUL;
5383
5384#ifdef FEAT_MBYTE
5385 if (pp == &eap->force_ff)
5386 {
5387#endif
5388 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5389 return FAIL;
5390#ifdef FEAT_MBYTE
5391 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005392 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 {
5394 /* Make 'fileencoding' lower case. */
5395 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5396 *p = TOLOWER_ASC(*p);
5397 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005398 else
5399 {
5400 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5401 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005402 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005403 if (STRICMP(p, "keep") == 0)
5404 eap->bad_char = BAD_KEEP;
5405 else if (STRICMP(p, "drop") == 0)
5406 eap->bad_char = BAD_DROP;
5407 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5408 eap->bad_char = *p;
5409 else
5410 return FAIL;
5411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412#endif
5413
5414 return OK;
5415}
5416
5417/*
5418 * ":abbreviate" and friends.
5419 */
5420 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005421ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422{
5423 do_exmap(eap, TRUE); /* almost the same as mapping */
5424}
5425
5426/*
5427 * ":map" and friends.
5428 */
5429 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005430ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431{
5432 /*
5433 * If we are sourcing .exrc or .vimrc in current directory we
5434 * print the mappings for security reasons.
5435 */
5436 if (secure)
5437 {
5438 secure = 2;
5439 msg_outtrans(eap->cmd);
5440 msg_putchar('\n');
5441 }
5442 do_exmap(eap, FALSE);
5443}
5444
5445/*
5446 * ":unmap" and friends.
5447 */
5448 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005449ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450{
5451 do_exmap(eap, FALSE);
5452}
5453
5454/*
5455 * ":mapclear" and friends.
5456 */
5457 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005458ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459{
5460 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5461}
5462
5463/*
5464 * ":abclear" and friends.
5465 */
5466 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005467ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468{
5469 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5470}
5471
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005472#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005474ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475{
5476 /*
5477 * Disallow auto commands from .exrc and .vimrc in current
5478 * directory for security reasons.
5479 */
5480 if (secure)
5481 {
5482 secure = 2;
5483 eap->errmsg = e_curdir;
5484 }
5485 else if (eap->cmdidx == CMD_autocmd)
5486 do_autocmd(eap->arg, eap->forceit);
5487 else
5488 do_augroup(eap->arg, eap->forceit);
5489}
5490
5491/*
5492 * ":doautocmd": Apply the automatic commands to the current buffer.
5493 */
5494 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005495ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005497 char_u *arg = eap->arg;
5498 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005499 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005500
Bram Moolenaar1610d052016-06-09 22:53:01 +02005501 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5502 /* Only when there is no <nomodeline>. */
5503 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005504 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505}
5506#endif
5507
5508#ifdef FEAT_LISTCMDS
5509/*
5510 * :[N]bunload[!] [N] [bufname] unload buffer
5511 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5512 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5513 */
5514 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005515ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516{
5517 eap->errmsg = do_bufdel(
5518 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5519 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5520 : DOBUF_UNLOAD, eap->arg,
5521 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5522}
5523
5524/*
5525 * :[N]buffer [N] to buffer N
5526 * :[N]sbuffer [N] to buffer N
5527 */
5528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005529ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530{
5531 if (*eap->arg)
5532 eap->errmsg = e_trailing;
5533 else
5534 {
5535 if (eap->addr_count == 0) /* default is current buffer */
5536 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5537 else
5538 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005539 if (eap->do_ecmd_cmd != NULL)
5540 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 }
5542}
5543
5544/*
5545 * :[N]bmodified [N] to next mod. buffer
5546 * :[N]sbmodified [N] to next mod. buffer
5547 */
5548 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005549ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550{
5551 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005552 if (eap->do_ecmd_cmd != NULL)
5553 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554}
5555
5556/*
5557 * :[N]bnext [N] to next buffer
5558 * :[N]sbnext [N] split and to next buffer
5559 */
5560 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005561ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562{
5563 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005564 if (eap->do_ecmd_cmd != NULL)
5565 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566}
5567
5568/*
5569 * :[N]bNext [N] to previous buffer
5570 * :[N]bprevious [N] to previous buffer
5571 * :[N]sbNext [N] split and to previous buffer
5572 * :[N]sbprevious [N] split and to previous buffer
5573 */
5574 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005575ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576{
5577 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005578 if (eap->do_ecmd_cmd != NULL)
5579 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580}
5581
5582/*
5583 * :brewind to first buffer
5584 * :bfirst to first buffer
5585 * :sbrewind split and to first buffer
5586 * :sbfirst split and to first buffer
5587 */
5588 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005589ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590{
5591 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005592 if (eap->do_ecmd_cmd != NULL)
5593 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594}
5595
5596/*
5597 * :blast to last buffer
5598 * :sblast split and to last buffer
5599 */
5600 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005601ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602{
5603 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005604 if (eap->do_ecmd_cmd != NULL)
5605 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606}
5607#endif
5608
5609 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005610ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611{
5612 return (c == NUL || c == '|' || c == '"' || c == '\n');
5613}
5614
5615#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5616 || defined(PROTO)
5617/*
5618 * Return the next command, after the first '|' or '\n'.
5619 * Return NULL if not found.
5620 */
5621 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005622find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623{
5624 while (*p != '|' && *p != '\n')
5625 {
5626 if (*p == NUL)
5627 return NULL;
5628 ++p;
5629 }
5630 return (p + 1);
5631}
5632#endif
5633
5634/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005635 * Check if *p is a separator between Ex commands, skipping over white space.
5636 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 */
5638 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005639check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005641 char_u *s = skipwhite(p);
5642
5643 if (*s == '|' || *s == '\n')
5644 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 else
5646 return NULL;
5647}
5648
5649/*
5650 * - if there are more files to edit
5651 * - and this is the last window
5652 * - and forceit not used
5653 * - and not repeated twice on a row
5654 * return FAIL and give error message if 'message' TRUE
5655 * return OK otherwise
5656 */
5657 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005658check_more(
5659 int message, /* when FALSE check only, no messages */
5660 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661{
5662 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5663
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005664 if (!forceit && only_one_window()
5665 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 {
5667 if (message)
5668 {
5669#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5670 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5671 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005672 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673
5674 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005675 vim_strncpy(buff,
5676 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005677 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005679 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 _("%d more files to edit. Quit anyway?"), n);
5681 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5682 return OK;
5683 return FAIL;
5684 }
5685#endif
5686 if (n == 1)
5687 EMSG(_("E173: 1 more file to edit"));
5688 else
5689 EMSGN(_("E173: %ld more files to edit"), n);
5690 quitmore = 2; /* next try to quit is allowed */
5691 }
5692 return FAIL;
5693 }
5694 return OK;
5695}
5696
5697#ifdef FEAT_CMDL_COMPL
5698/*
5699 * Function given to ExpandGeneric() to obtain the list of command names.
5700 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005702get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703{
5704 if (idx >= (int)CMD_SIZE)
5705# ifdef FEAT_USR_CMDS
5706 return get_user_command_name(idx);
5707# else
5708 return NULL;
5709# endif
5710 return cmdnames[idx].cmd_name;
5711}
5712#endif
5713
5714#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005715static 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);
5716static void uc_list(char_u *name, size_t name_len);
5717static 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);
5718static char_u *uc_split_args(char_u *arg, size_t *lenp);
5719static 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 +00005720
5721 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005722uc_add_command(
5723 char_u *name,
5724 size_t name_len,
5725 char_u *rep,
5726 long argt,
5727 long def,
5728 int flags,
5729 int compl,
5730 char_u *compl_arg,
5731 int addr_type,
5732 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733{
5734 ucmd_T *cmd = NULL;
5735 char_u *p;
5736 int i;
5737 int cmp = 1;
5738 char_u *rep_buf = NULL;
5739 garray_T *gap;
5740
Bram Moolenaar9c102382006-05-03 21:26:49 +00005741 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 if (rep_buf == NULL)
5743 {
5744 /* Can't replace termcodes - try using the string as is */
5745 rep_buf = vim_strsave(rep);
5746
5747 /* Give up if out of memory */
5748 if (rep_buf == NULL)
5749 return FAIL;
5750 }
5751
5752 /* get address of growarray: global or in curbuf */
5753 if (flags & UC_BUFFER)
5754 {
5755 gap = &curbuf->b_ucmds;
5756 if (gap->ga_itemsize == 0)
5757 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5758 }
5759 else
5760 gap = &ucmds;
5761
5762 /* Search for the command in the already defined commands. */
5763 for (i = 0; i < gap->ga_len; ++i)
5764 {
5765 size_t len;
5766
5767 cmd = USER_CMD_GA(gap, i);
5768 len = STRLEN(cmd->uc_name);
5769 cmp = STRNCMP(name, cmd->uc_name, name_len);
5770 if (cmp == 0)
5771 {
5772 if (name_len < len)
5773 cmp = -1;
5774 else if (name_len > len)
5775 cmp = 1;
5776 }
5777
5778 if (cmp == 0)
5779 {
5780 if (!force)
5781 {
5782 EMSG(_("E174: Command already exists: add ! to replace it"));
5783 goto fail;
5784 }
5785
5786 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005787 cmd->uc_rep = NULL;
5788#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5789 vim_free(cmd->uc_compl_arg);
5790 cmd->uc_compl_arg = NULL;
5791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 break;
5793 }
5794
5795 /* Stop as soon as we pass the name to add */
5796 if (cmp < 0)
5797 break;
5798 }
5799
5800 /* Extend the array unless we're replacing an existing command */
5801 if (cmp != 0)
5802 {
5803 if (ga_grow(gap, 1) != OK)
5804 goto fail;
5805 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5806 goto fail;
5807
5808 cmd = USER_CMD_GA(gap, i);
5809 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5810
5811 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812
5813 cmd->uc_name = p;
5814 }
5815
5816 cmd->uc_rep = rep_buf;
5817 cmd->uc_argt = argt;
5818 cmd->uc_def = def;
5819 cmd->uc_compl = compl;
5820#ifdef FEAT_EVAL
5821 cmd->uc_scriptID = current_SID;
5822# ifdef FEAT_CMDL_COMPL
5823 cmd->uc_compl_arg = compl_arg;
5824# endif
5825#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005826 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827
5828 return OK;
5829
5830fail:
5831 vim_free(rep_buf);
5832#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5833 vim_free(compl_arg);
5834#endif
5835 return FAIL;
5836}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005839#if defined(FEAT_USR_CMDS)
5840static struct
5841{
5842 int expand;
5843 char *name;
5844} addr_type_complete[] =
5845{
5846 {ADDR_ARGUMENTS, "arguments"},
5847 {ADDR_LINES, "lines"},
5848 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5849 {ADDR_TABS, "tabs"},
5850 {ADDR_BUFFERS, "buffers"},
5851 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005852 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005853 {-1, NULL}
5854};
5855#endif
5856
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005857#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858/*
5859 * List of names for completion for ":command" with the EXPAND_ flag.
5860 * Must be alphabetical for completion.
5861 */
5862static struct
5863{
5864 int expand;
5865 char *name;
5866} command_complete[] =
5867{
5868 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005869 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005871 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005873 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005874#if defined(FEAT_CSCOPE)
5875 {EXPAND_CSCOPE, "cscope"},
5876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5878 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005879 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880#endif
5881 {EXPAND_DIRECTORIES, "dir"},
5882 {EXPAND_ENV_VARS, "environment"},
5883 {EXPAND_EVENTS, "event"},
5884 {EXPAND_EXPRESSION, "expression"},
5885 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005886 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005887 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 {EXPAND_FUNCTIONS, "function"},
5889 {EXPAND_HELP, "help"},
5890 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005891#if defined(FEAT_CMDHIST)
5892 {EXPAND_HISTORY, "history"},
5893#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005894#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005895 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005896 {EXPAND_LOCALES, "locale"},
5897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 {EXPAND_MAPPINGS, "mapping"},
5899 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005900 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005901 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005902#if defined(FEAT_PROFILE)
5903 {EXPAND_SYNTIME, "syntime"},
5904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005906 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005907 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005908#if defined(FEAT_SIGNS)
5909 {EXPAND_SIGN, "sign"},
5910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 {EXPAND_TAGS, "tag"},
5912 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005913 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 {EXPAND_USER_VARS, "var"},
5915 {0, NULL}
5916};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005919#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005921uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922{
5923 int i, j;
5924 int found = FALSE;
5925 ucmd_T *cmd;
5926 int len;
5927 long a;
5928 garray_T *gap;
5929
5930 gap = &curbuf->b_ucmds;
5931 for (;;)
5932 {
5933 for (i = 0; i < gap->ga_len; ++i)
5934 {
5935 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005936 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937
Bram Moolenaard29459b2016-08-26 22:29:11 +02005938 /* Skip commands which don't match the requested prefix and
5939 * commands filtered out. */
5940 if (STRNCMP(name, cmd->uc_name, name_len) != 0
5941 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 continue;
5943
5944 /* Put out the title first time */
5945 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005946 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 found = TRUE;
5948 msg_putchar('\n');
5949 if (got_int)
5950 break;
5951
5952 /* Special cases */
5953 msg_putchar(a & BANG ? '!' : ' ');
5954 msg_putchar(a & REGSTR ? '"' : ' ');
5955 msg_putchar(gap != &ucmds ? 'b' : ' ');
5956 msg_putchar(' ');
5957
5958 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5959 len = (int)STRLEN(cmd->uc_name) + 4;
5960
5961 do {
5962 msg_putchar(' ');
5963 ++len;
5964 } while (len < 16);
5965
5966 len = 0;
5967
5968 /* Arguments */
5969 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5970 {
5971 case 0: IObuff[len++] = '0'; break;
5972 case (EXTRA): IObuff[len++] = '*'; break;
5973 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5974 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5975 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5976 }
5977
5978 do {
5979 IObuff[len++] = ' ';
5980 } while (len < 5);
5981
5982 /* Range */
5983 if (a & (RANGE|COUNT))
5984 {
5985 if (a & COUNT)
5986 {
5987 /* -count=N */
5988 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5989 len += (int)STRLEN(IObuff + len);
5990 }
5991 else if (a & DFLALL)
5992 IObuff[len++] = '%';
5993 else if (cmd->uc_def >= 0)
5994 {
5995 /* -range=N */
5996 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5997 len += (int)STRLEN(IObuff + len);
5998 }
5999 else
6000 IObuff[len++] = '.';
6001 }
6002
6003 do {
6004 IObuff[len++] = ' ';
6005 } while (len < 11);
6006
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006007 /* Address Type */
6008 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6009 if (addr_type_complete[j].expand != ADDR_LINES
6010 && addr_type_complete[j].expand == cmd->uc_addr_type)
6011 {
6012 STRCPY(IObuff + len, addr_type_complete[j].name);
6013 len += (int)STRLEN(IObuff + len);
6014 break;
6015 }
6016
6017 do {
6018 IObuff[len++] = ' ';
6019 } while (len < 21);
6020
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 /* Completion */
6022 for (j = 0; command_complete[j].expand != 0; ++j)
6023 if (command_complete[j].expand == cmd->uc_compl)
6024 {
6025 STRCPY(IObuff + len, command_complete[j].name);
6026 len += (int)STRLEN(IObuff + len);
6027 break;
6028 }
6029
6030 do {
6031 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006032 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033
6034 IObuff[len] = '\0';
6035 msg_outtrans(IObuff);
6036
6037 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006038#ifdef FEAT_EVAL
6039 if (p_verbose > 0)
6040 last_set_msg(cmd->uc_scriptID);
6041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 out_flush();
6043 ui_breakcheck();
6044 if (got_int)
6045 break;
6046 }
6047 if (gap == &ucmds || i < gap->ga_len)
6048 break;
6049 gap = &ucmds;
6050 }
6051
6052 if (!found)
6053 MSG(_("No user-defined commands found"));
6054}
6055
6056 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006057uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058{
6059 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6060 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6061 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6062 0xb9, 0x7f, 0};
6063 int i;
6064
6065 for (i = 0; fcmd[i]; ++i)
6066 IObuff[i] = fcmd[i] - 0x40;
6067 IObuff[i] = 0;
6068 return IObuff;
6069}
6070
6071 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006072uc_scan_attr(
6073 char_u *attr,
6074 size_t len,
6075 long *argt,
6076 long *def,
6077 int *flags,
6078 int *compl,
6079 char_u **compl_arg,
6080 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081{
6082 char_u *p;
6083
6084 if (len == 0)
6085 {
6086 EMSG(_("E175: No attribute specified"));
6087 return FAIL;
6088 }
6089
6090 /* First, try the simple attributes (no arguments) */
6091 if (STRNICMP(attr, "bang", len) == 0)
6092 *argt |= BANG;
6093 else if (STRNICMP(attr, "buffer", len) == 0)
6094 *flags |= UC_BUFFER;
6095 else if (STRNICMP(attr, "register", len) == 0)
6096 *argt |= REGSTR;
6097 else if (STRNICMP(attr, "bar", len) == 0)
6098 *argt |= TRLBAR;
6099 else
6100 {
6101 int i;
6102 char_u *val = NULL;
6103 size_t vallen = 0;
6104 size_t attrlen = len;
6105
6106 /* Look for the attribute name - which is the part before any '=' */
6107 for (i = 0; i < (int)len; ++i)
6108 {
6109 if (attr[i] == '=')
6110 {
6111 val = &attr[i + 1];
6112 vallen = len - i - 1;
6113 attrlen = i;
6114 break;
6115 }
6116 }
6117
6118 if (STRNICMP(attr, "nargs", attrlen) == 0)
6119 {
6120 if (vallen == 1)
6121 {
6122 if (*val == '0')
6123 /* Do nothing - this is the default */;
6124 else if (*val == '1')
6125 *argt |= (EXTRA | NOSPC | NEEDARG);
6126 else if (*val == '*')
6127 *argt |= EXTRA;
6128 else if (*val == '?')
6129 *argt |= (EXTRA | NOSPC);
6130 else if (*val == '+')
6131 *argt |= (EXTRA | NEEDARG);
6132 else
6133 goto wrong_nargs;
6134 }
6135 else
6136 {
6137wrong_nargs:
6138 EMSG(_("E176: Invalid number of arguments"));
6139 return FAIL;
6140 }
6141 }
6142 else if (STRNICMP(attr, "range", attrlen) == 0)
6143 {
6144 *argt |= RANGE;
6145 if (vallen == 1 && *val == '%')
6146 *argt |= DFLALL;
6147 else if (val != NULL)
6148 {
6149 p = val;
6150 if (*def >= 0)
6151 {
6152two_count:
6153 EMSG(_("E177: Count cannot be specified twice"));
6154 return FAIL;
6155 }
6156
6157 *def = getdigits(&p);
6158 *argt |= (ZEROR | NOTADR);
6159
6160 if (p != val + vallen || vallen == 0)
6161 {
6162invalid_count:
6163 EMSG(_("E178: Invalid default value for count"));
6164 return FAIL;
6165 }
6166 }
6167 }
6168 else if (STRNICMP(attr, "count", attrlen) == 0)
6169 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006170 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171
6172 if (val != NULL)
6173 {
6174 p = val;
6175 if (*def >= 0)
6176 goto two_count;
6177
6178 *def = getdigits(&p);
6179
6180 if (p != val + vallen)
6181 goto invalid_count;
6182 }
6183
6184 if (*def < 0)
6185 *def = 0;
6186 }
6187 else if (STRNICMP(attr, "complete", attrlen) == 0)
6188 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 if (val == NULL)
6190 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006191 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 return FAIL;
6193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006195 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6196 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006199 else if (STRNICMP(attr, "addr", attrlen) == 0)
6200 {
6201 *argt |= RANGE;
6202 if (val == NULL)
6203 {
6204 EMSG(_("E179: argument required for -addr"));
6205 return FAIL;
6206 }
6207 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6208 == FAIL)
6209 return FAIL;
6210 if (addr_type_arg != ADDR_LINES)
6211 *argt |= (ZEROR | NOTADR) ;
6212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 else
6214 {
6215 char_u ch = attr[len];
6216 attr[len] = '\0';
6217 EMSG2(_("E181: Invalid attribute: %s"), attr);
6218 attr[len] = ch;
6219 return FAIL;
6220 }
6221 }
6222
6223 return OK;
6224}
6225
Bram Moolenaara850a712009-01-28 14:42:59 +00006226/*
6227 * ":command ..."
6228 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006230ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231{
6232 char_u *name;
6233 char_u *end;
6234 char_u *p;
6235 long argt = 0;
6236 long def = -1;
6237 int flags = 0;
6238 int compl = EXPAND_NOTHING;
6239 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006240 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006242 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243
6244 p = eap->arg;
6245
6246 /* Check for attributes */
6247 while (*p == '-')
6248 {
6249 ++p;
6250 end = skiptowhite(p);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006251 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 == FAIL)
6253 return;
6254 p = skipwhite(end);
6255 }
6256
6257 /* Get the name (if any) and skip to the following argument */
6258 name = p;
6259 if (ASCII_ISALPHA(*p))
6260 while (ASCII_ISALNUM(*p))
6261 ++p;
6262 if (!ends_excmd(*p) && !vim_iswhite(*p))
6263 {
6264 EMSG(_("E182: Invalid command name"));
6265 return;
6266 }
6267 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006268 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269
6270 /* If there is nothing after the name, and no attributes were specified,
6271 * we are listing commands
6272 */
6273 p = skipwhite(end);
6274 if (!has_attr && ends_excmd(*p))
6275 {
6276 uc_list(name, end - name);
6277 }
6278 else if (!ASCII_ISUPPER(*name))
6279 {
6280 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6281 return;
6282 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006283 else if ((name_len == 1 && *name == 'X')
6284 || (name_len <= 4
6285 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6286 {
6287 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6288 return;
6289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 else
6291 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006292 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293}
6294
6295/*
6296 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 * Clear all user commands, global and for current buffer.
6298 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006299 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006300ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301{
6302 uc_clear(&ucmds);
6303 uc_clear(&curbuf->b_ucmds);
6304}
6305
6306/*
6307 * Clear all user commands for "gap".
6308 */
6309 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006310uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311{
6312 int i;
6313 ucmd_T *cmd;
6314
6315 for (i = 0; i < gap->ga_len; ++i)
6316 {
6317 cmd = USER_CMD_GA(gap, i);
6318 vim_free(cmd->uc_name);
6319 vim_free(cmd->uc_rep);
6320# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6321 vim_free(cmd->uc_compl_arg);
6322# endif
6323 }
6324 ga_clear(gap);
6325}
6326
6327 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006328ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329{
6330 int i = 0;
6331 ucmd_T *cmd = NULL;
6332 int cmp = -1;
6333 garray_T *gap;
6334
6335 gap = &curbuf->b_ucmds;
6336 for (;;)
6337 {
6338 for (i = 0; i < gap->ga_len; ++i)
6339 {
6340 cmd = USER_CMD_GA(gap, i);
6341 cmp = STRCMP(eap->arg, cmd->uc_name);
6342 if (cmp <= 0)
6343 break;
6344 }
6345 if (gap == &ucmds || cmp == 0)
6346 break;
6347 gap = &ucmds;
6348 }
6349
6350 if (cmp != 0)
6351 {
6352 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6353 return;
6354 }
6355
6356 vim_free(cmd->uc_name);
6357 vim_free(cmd->uc_rep);
6358# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6359 vim_free(cmd->uc_compl_arg);
6360# endif
6361
6362 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363
6364 if (i < gap->ga_len)
6365 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6366}
6367
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006368/*
6369 * split and quote args for <f-args>
6370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006372uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373{
6374 char_u *buf;
6375 char_u *p;
6376 char_u *q;
6377 int len;
6378
6379 /* Precalculate length */
6380 p = arg;
6381 len = 2; /* Initial and final quotes */
6382
6383 while (*p)
6384 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006385 if (p[0] == '\\' && p[1] == '\\')
6386 {
6387 len += 2;
6388 p += 2;
6389 }
6390 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 {
6392 len += 1;
6393 p += 2;
6394 }
6395 else if (*p == '\\' || *p == '"')
6396 {
6397 len += 2;
6398 p += 1;
6399 }
6400 else if (vim_iswhite(*p))
6401 {
6402 p = skipwhite(p);
6403 if (*p == NUL)
6404 break;
6405 len += 3; /* "," */
6406 }
6407 else
6408 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006409#ifdef FEAT_MBYTE
6410 int charlen = (*mb_ptr2len)(p);
6411 len += charlen;
6412 p += charlen;
6413#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 ++len;
6415 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 }
6418 }
6419
6420 buf = alloc(len + 1);
6421 if (buf == NULL)
6422 {
6423 *lenp = 0;
6424 return buf;
6425 }
6426
6427 p = arg;
6428 q = buf;
6429 *q++ = '"';
6430 while (*p)
6431 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006432 if (p[0] == '\\' && p[1] == '\\')
6433 {
6434 *q++ = '\\';
6435 *q++ = '\\';
6436 p += 2;
6437 }
6438 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 {
6440 *q++ = p[1];
6441 p += 2;
6442 }
6443 else if (*p == '\\' || *p == '"')
6444 {
6445 *q++ = '\\';
6446 *q++ = *p++;
6447 }
6448 else if (vim_iswhite(*p))
6449 {
6450 p = skipwhite(p);
6451 if (*p == NUL)
6452 break;
6453 *q++ = '"';
6454 *q++ = ',';
6455 *q++ = '"';
6456 }
6457 else
6458 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006459 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 }
6461 }
6462 *q++ = '"';
6463 *q = 0;
6464
6465 *lenp = len;
6466 return buf;
6467}
6468
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006469 static size_t
6470add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6471{
6472 size_t result;
6473
6474 result = STRLEN(mod_str);
6475 if (*multi_mods)
6476 result += 1;
6477 if (buf != NULL)
6478 {
6479 if (*multi_mods)
6480 STRCAT(buf, " ");
6481 STRCAT(buf, mod_str);
6482 }
6483
6484 *multi_mods = 1;
6485
6486 return result;
6487}
6488
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489/*
6490 * Check for a <> code in a user command.
6491 * "code" points to the '<'. "len" the length of the <> (inclusive).
6492 * "buf" is where the result is to be added.
6493 * "split_buf" points to a buffer used for splitting, caller should free it.
6494 * "split_len" is the length of what "split_buf" contains.
6495 * Returns the length of the replacement, which has been added to "buf".
6496 * Returns -1 if there was no match, and only the "<" has been copied.
6497 */
6498 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006499uc_check_code(
6500 char_u *code,
6501 size_t len,
6502 char_u *buf,
6503 ucmd_T *cmd, /* the user command we're expanding */
6504 exarg_T *eap, /* ex arguments */
6505 char_u **split_buf,
6506 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507{
6508 size_t result = 0;
6509 char_u *p = code + 1;
6510 size_t l = len - 2;
6511 int quote = 0;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006512 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_MODS,
6513 ct_REGISTER, ct_LT, ct_NONE } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514
6515 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6516 {
6517 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6518 p += 2;
6519 l -= 2;
6520 }
6521
Bram Moolenaar371d5402006-03-20 21:47:49 +00006522 ++l;
6523 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006525 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006527 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006529 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006531 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006533 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006535 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006537 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006539 else if (STRNICMP(p, "mods>", l) == 0)
6540 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541
6542 switch (type)
6543 {
6544 case ct_ARGS:
6545 /* Simple case first */
6546 if (*eap->arg == NUL)
6547 {
6548 if (quote == 1)
6549 {
6550 result = 2;
6551 if (buf != NULL)
6552 STRCPY(buf, "''");
6553 }
6554 else
6555 result = 0;
6556 break;
6557 }
6558
6559 /* When specified there is a single argument don't split it.
6560 * Works for ":Cmd %" when % is "a b c". */
6561 if ((eap->argt & NOSPC) && quote == 2)
6562 quote = 1;
6563
6564 switch (quote)
6565 {
6566 case 0: /* No quoting, no splitting */
6567 result = STRLEN(eap->arg);
6568 if (buf != NULL)
6569 STRCPY(buf, eap->arg);
6570 break;
6571 case 1: /* Quote, but don't split */
6572 result = STRLEN(eap->arg) + 2;
6573 for (p = eap->arg; *p; ++p)
6574 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006575#ifdef FEAT_MBYTE
6576 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6577 /* DBCS can contain \ in a trail byte, skip the
6578 * double-byte character. */
6579 ++p;
6580 else
6581#endif
6582 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 ++result;
6584 }
6585
6586 if (buf != NULL)
6587 {
6588 *buf++ = '"';
6589 for (p = eap->arg; *p; ++p)
6590 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006591#ifdef FEAT_MBYTE
6592 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6593 /* DBCS can contain \ in a trail byte, copy the
6594 * double-byte character to avoid escaping. */
6595 *buf++ = *p++;
6596 else
6597#endif
6598 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 *buf++ = '\\';
6600 *buf++ = *p;
6601 }
6602 *buf = '"';
6603 }
6604
6605 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006606 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 /* This is hard, so only do it once, and cache the result */
6608 if (*split_buf == NULL)
6609 *split_buf = uc_split_args(eap->arg, split_len);
6610
6611 result = *split_len;
6612 if (buf != NULL && result != 0)
6613 STRCPY(buf, *split_buf);
6614
6615 break;
6616 }
6617 break;
6618
6619 case ct_BANG:
6620 result = eap->forceit ? 1 : 0;
6621 if (quote)
6622 result += 2;
6623 if (buf != NULL)
6624 {
6625 if (quote)
6626 *buf++ = '"';
6627 if (eap->forceit)
6628 *buf++ = '!';
6629 if (quote)
6630 *buf = '"';
6631 }
6632 break;
6633
6634 case ct_LINE1:
6635 case ct_LINE2:
6636 case ct_COUNT:
6637 {
6638 char num_buf[20];
6639 long num = (type == ct_LINE1) ? eap->line1 :
6640 (type == ct_LINE2) ? eap->line2 :
6641 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6642 size_t num_len;
6643
6644 sprintf(num_buf, "%ld", num);
6645 num_len = STRLEN(num_buf);
6646 result = num_len;
6647
6648 if (quote)
6649 result += 2;
6650
6651 if (buf != NULL)
6652 {
6653 if (quote)
6654 *buf++ = '"';
6655 STRCPY(buf, num_buf);
6656 buf += num_len;
6657 if (quote)
6658 *buf = '"';
6659 }
6660
6661 break;
6662 }
6663
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006664 case ct_MODS:
6665 {
6666 int multi_mods = 0;
6667 typedef struct {
6668 int *varp;
6669 char *name;
6670 } mod_entry_T;
6671 static mod_entry_T mod_entries[] = {
6672#ifdef FEAT_BROWSE_CMD
6673 {&cmdmod.browse, "browse"},
6674#endif
6675#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6676 {&cmdmod.confirm, "confirm"},
6677#endif
6678 {&cmdmod.hide, "hide"},
6679 {&cmdmod.keepalt, "keepalt"},
6680 {&cmdmod.keepjumps, "keepjumps"},
6681 {&cmdmod.keepmarks, "keepmarks"},
6682 {&cmdmod.keeppatterns, "keeppatterns"},
6683 {&cmdmod.lockmarks, "lockmarks"},
6684 {&cmdmod.noswapfile, "noswapfile"},
6685 {NULL, NULL}
6686 };
6687 int i;
6688
6689 result = quote ? 2 : 0;
6690 if (buf != NULL)
6691 {
6692 if (quote)
6693 *buf++ = '"';
6694 *buf = '\0';
6695 }
6696
6697#ifdef FEAT_WINDOWS
6698 /* :aboveleft and :leftabove */
6699 if (cmdmod.split & WSP_ABOVE)
6700 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6701 /* :belowright and :rightbelow */
6702 if (cmdmod.split & WSP_BELOW)
6703 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6704 /* :botright */
6705 if (cmdmod.split & WSP_BOT)
6706 result += add_cmd_modifier(buf, "botright", &multi_mods);
6707#endif
6708
6709 /* the modifiers that are simple flags */
6710 for (i = 0; mod_entries[i].varp != NULL; ++i)
6711 if (*mod_entries[i].varp)
6712 result += add_cmd_modifier(buf, mod_entries[i].name,
6713 &multi_mods);
6714
6715 /* TODO: How to support :noautocmd? */
6716#ifdef HAVE_SANDBOX
6717 /* TODO: How to support :sandbox?*/
6718#endif
6719 /* :silent */
6720 if (msg_silent > 0)
6721 result += add_cmd_modifier(buf,
6722 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
6723#ifdef FEAT_WINDOWS
6724 /* :tab */
6725 if (cmdmod.tab > 0)
6726 result += add_cmd_modifier(buf, "tab", &multi_mods);
6727 /* :topleft */
6728 if (cmdmod.split & WSP_TOP)
6729 result += add_cmd_modifier(buf, "topleft", &multi_mods);
6730#endif
6731 /* TODO: How to support :unsilent?*/
6732 /* :verbose */
6733 if (p_verbose > 0)
6734 result += add_cmd_modifier(buf, "verbose", &multi_mods);
6735#ifdef FEAT_WINDOWS
6736 /* :vertical */
6737 if (cmdmod.split & WSP_VERT)
6738 result += add_cmd_modifier(buf, "vertical", &multi_mods);
6739#endif
6740 if (quote && buf != NULL)
6741 {
6742 buf += result - 2;
6743 *buf = '"';
6744 }
6745 break;
6746 }
6747
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 case ct_REGISTER:
6749 result = eap->regname ? 1 : 0;
6750 if (quote)
6751 result += 2;
6752 if (buf != NULL)
6753 {
6754 if (quote)
6755 *buf++ = '\'';
6756 if (eap->regname)
6757 *buf++ = eap->regname;
6758 if (quote)
6759 *buf = '\'';
6760 }
6761 break;
6762
6763 case ct_LT:
6764 result = 1;
6765 if (buf != NULL)
6766 *buf = '<';
6767 break;
6768
6769 default:
6770 /* Not recognized: just copy the '<' and return -1. */
6771 result = (size_t)-1;
6772 if (buf != NULL)
6773 *buf = '<';
6774 break;
6775 }
6776
6777 return result;
6778}
6779
6780 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006781do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782{
6783 char_u *buf;
6784 char_u *p;
6785 char_u *q;
6786
6787 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006788 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006789 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 size_t len, totlen;
6791
6792 size_t split_len = 0;
6793 char_u *split_buf = NULL;
6794 ucmd_T *cmd;
6795#ifdef FEAT_EVAL
6796 scid_T save_current_SID = current_SID;
6797#endif
6798
6799 if (eap->cmdidx == CMD_USER)
6800 cmd = USER_CMD(eap->useridx);
6801 else
6802 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6803
6804 /*
6805 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006806 * First round: "buf" is NULL, compute length, allocate "buf".
6807 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 */
6809 buf = NULL;
6810 for (;;)
6811 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006812 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006813 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006815
6816 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006818 start = vim_strchr(p, '<');
6819 if (start != NULL)
6820 end = vim_strchr(start + 1, '>');
6821 if (buf != NULL)
6822 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006823 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6824 ;
6825 if (*ksp == K_SPECIAL
6826 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006827 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6828# ifdef FEAT_GUI
6829 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6830# endif
6831 ))
6832 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006833 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006834 * KS_SPECIAL KE_FILLER, like for mappings, but
6835 * do_cmdline() doesn't handle that, so convert it back.
6836 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6837 len = ksp - p;
6838 if (len > 0)
6839 {
6840 mch_memmove(q, p, len);
6841 q += len;
6842 }
6843 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6844 p = ksp + 3;
6845 continue;
6846 }
6847 }
6848
6849 /* break if there no <item> is found */
6850 if (start == NULL || end == NULL)
6851 break;
6852
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 /* Include the '>' */
6854 ++end;
6855
6856 /* Take everything up to the '<' */
6857 len = start - p;
6858 if (buf == NULL)
6859 totlen += len;
6860 else
6861 {
6862 mch_memmove(q, p, len);
6863 q += len;
6864 }
6865
6866 len = uc_check_code(start, end - start, q, cmd, eap,
6867 &split_buf, &split_len);
6868 if (len == (size_t)-1)
6869 {
6870 /* no match, continue after '<' */
6871 p = start + 1;
6872 len = 1;
6873 }
6874 else
6875 p = end;
6876 if (buf == NULL)
6877 totlen += len;
6878 else
6879 q += len;
6880 }
6881 if (buf != NULL) /* second time here, finished */
6882 {
6883 STRCPY(q, p);
6884 break;
6885 }
6886
6887 totlen += STRLEN(p); /* Add on the trailing characters */
6888 buf = alloc((unsigned)(totlen + 1));
6889 if (buf == NULL)
6890 {
6891 vim_free(split_buf);
6892 return;
6893 }
6894 }
6895
6896#ifdef FEAT_EVAL
6897 current_SID = cmd->uc_scriptID;
6898#endif
6899 (void)do_cmdline(buf, eap->getline, eap->cookie,
6900 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6901#ifdef FEAT_EVAL
6902 current_SID = save_current_SID;
6903#endif
6904 vim_free(buf);
6905 vim_free(split_buf);
6906}
6907
6908# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6909 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006910get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911{
6912 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6913}
6914
6915/*
6916 * Function given to ExpandGeneric() to obtain the list of user command names.
6917 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006919get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920{
6921 if (idx < curbuf->b_ucmds.ga_len)
6922 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6923 idx -= curbuf->b_ucmds.ga_len;
6924 if (idx < ucmds.ga_len)
6925 return USER_CMD(idx)->uc_name;
6926 return NULL;
6927}
6928
6929/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006930 * Function given to ExpandGeneric() to obtain the list of user address type names.
6931 */
6932 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006933get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006934{
6935 return (char_u *)addr_type_complete[idx].name;
6936}
6937
6938/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 * Function given to ExpandGeneric() to obtain the list of user command
6940 * attributes.
6941 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006943get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944{
6945 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006946 {"addr", "bang", "bar", "buffer", "complete",
6947 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006949 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 return NULL;
6951 return (char_u *)user_cmd_flags[idx];
6952}
6953
6954/*
6955 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6956 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006958get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959{
6960 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6961
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006962 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 return NULL;
6964 return (char_u *)user_cmd_nargs[idx];
6965}
6966
6967/*
6968 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6969 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006971get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972{
6973 return (char_u *)command_complete[idx].name;
6974}
6975# endif /* FEAT_CMDL_COMPL */
6976
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006977/*
6978 * Parse address type argument
6979 */
6980 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006981parse_addr_type_arg(
6982 char_u *value,
6983 int vallen,
6984 long *argt,
6985 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006986{
6987 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01006988
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006989 for (i = 0; addr_type_complete[i].expand != -1; ++i)
6990 {
6991 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
6992 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
6993 if (a && b)
6994 {
6995 *addr_type_arg = addr_type_complete[i].expand;
6996 break;
6997 }
6998 }
6999
7000 if (addr_type_complete[i].expand == -1)
7001 {
7002 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007003
7004 for (i = 0; err[i] != NUL && !vim_iswhite(err[i]); i++)
7005 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007006 err[i] = NUL;
7007 EMSG2(_("E180: Invalid address type value: %s"), err);
7008 return FAIL;
7009 }
7010
7011 if (*addr_type_arg != ADDR_LINES)
7012 *argt |= NOTADR;
7013
7014 return OK;
7015}
7016
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017#endif /* FEAT_USR_CMDS */
7018
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007019#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7020/*
7021 * Parse a completion argument "value[vallen]".
7022 * The detected completion goes in "*complp", argument type in "*argt".
7023 * When there is an argument, for function and user defined completion, it's
7024 * copied to allocated memory and stored in "*compl_arg".
7025 * Returns FAIL if something is wrong.
7026 */
7027 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007028parse_compl_arg(
7029 char_u *value,
7030 int vallen,
7031 int *complp,
7032 long *argt,
7033 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007034{
7035 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007036# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007037 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007038# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007039 int i;
7040 int valend = vallen;
7041
7042 /* Look for any argument part - which is the part after any ',' */
7043 for (i = 0; i < vallen; ++i)
7044 {
7045 if (value[i] == ',')
7046 {
7047 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007048# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007049 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007050# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007051 valend = i;
7052 break;
7053 }
7054 }
7055
7056 for (i = 0; command_complete[i].expand != 0; ++i)
7057 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007058 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007059 && STRNCMP(value, command_complete[i].name, valend) == 0)
7060 {
7061 *complp = command_complete[i].expand;
7062 if (command_complete[i].expand == EXPAND_BUFFERS)
7063 *argt |= BUFNAME;
7064 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7065 || command_complete[i].expand == EXPAND_FILES)
7066 *argt |= XFILE;
7067 break;
7068 }
7069 }
7070
7071 if (command_complete[i].expand == 0)
7072 {
7073 EMSG2(_("E180: Invalid complete value: %s"), value);
7074 return FAIL;
7075 }
7076
7077# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7078 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7079 && arg != NULL)
7080# else
7081 if (arg != NULL)
7082# endif
7083 {
7084 EMSG(_("E468: Completion argument only allowed for custom completion"));
7085 return FAIL;
7086 }
7087
7088# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7089 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7090 && arg == NULL)
7091 {
7092 EMSG(_("E467: Custom completion requires a function argument"));
7093 return FAIL;
7094 }
7095
7096 if (arg != NULL)
7097 *compl_arg = vim_strnsave(arg, (int)arglen);
7098# endif
7099 return OK;
7100}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007101
7102 int
7103cmdcomplete_str_to_type(char_u *complete_str)
7104{
7105 int i;
7106
7107 for (i = 0; command_complete[i].expand != 0; ++i)
7108 if (STRCMP(complete_str, command_complete[i].name) == 0)
7109 return command_complete[i].expand;
7110
7111 return EXPAND_NOTHING;
7112}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007113#endif
7114
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007116ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117{
Bram Moolenaare6850792010-05-14 15:28:44 +02007118 if (*eap->arg == NUL)
7119 {
7120#ifdef FEAT_EVAL
7121 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7122 char_u *p = NULL;
7123
7124 if (expr != NULL)
7125 {
7126 ++emsg_off;
7127 p = eval_to_string(expr, NULL, FALSE);
7128 --emsg_off;
7129 vim_free(expr);
7130 }
7131 if (p != NULL)
7132 {
7133 MSG(p);
7134 vim_free(p);
7135 }
7136 else
7137 MSG("default");
7138#else
7139 MSG(_("unknown"));
7140#endif
7141 }
7142 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007143 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144}
7145
7146 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007147ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148{
7149 if (*eap->arg == NUL && eap->cmd[2] == '!')
7150 MSG(_("Greetings, Vim user!"));
7151 do_highlight(eap->arg, eap->forceit, FALSE);
7152}
7153
7154
7155/*
7156 * Call this function if we thought we were going to exit, but we won't
7157 * (because of an error). May need to restore the terminal mode.
7158 */
7159 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007160not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161{
7162 exiting = FALSE;
7163 settmode(TMODE_RAW);
7164}
7165
7166/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007167 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 */
7169 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007170ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171{
Bram Moolenaarf240e182014-11-27 18:33:02 +01007172#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007173 win_T *wp;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007174#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007175
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176#ifdef FEAT_CMDWIN
7177 if (cmdwin_type != 0)
7178 {
7179 cmdwin_result = Ctrl_C;
7180 return;
7181 }
7182#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007183 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007184 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007185 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007186 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007187 return;
7188 }
Bram Moolenaarf240e182014-11-27 18:33:02 +01007189#ifdef FEAT_WINDOWS
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007190 if (eap->addr_count > 0)
7191 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007192 int wnr = eap->line2;
7193
7194 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7195 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007196 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007197 }
7198 else
Bram Moolenaarf240e182014-11-27 18:33:02 +01007199#endif
7200#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007201 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007202#endif
7203
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007204#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02007205 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007206 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02007207 * being closed (can only happen in autocommands). */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007208 if (curbuf_locked() || (wp->w_buffer->b_nwindows == 1
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007209 && wp->w_buffer->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007210 return;
7211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212
7213#ifdef FEAT_NETBEANS_INTG
7214 netbeansForcedQuit = eap->forceit;
7215#endif
7216
7217 /*
7218 * If there are more files or windows we won't exit.
7219 */
7220 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7221 exiting = TRUE;
7222 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007223 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7224 | (eap->forceit ? CCGD_FORCEIT : 0)
7225 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007227 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 {
7229 not_exiting();
7230 }
7231 else
7232 {
7233#ifdef FEAT_WINDOWS
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007234 /* quit last window
7235 * Note: only_one_window() returns true, even so a help window is
7236 * still open. In that case only quit, if no address has been
7237 * specified. Example:
7238 * :h|wincmd w|1q - don't quit
7239 * :h|wincmd w|q - quit
7240 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007241 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242#endif
7243 getout(0);
7244#ifdef FEAT_WINDOWS
7245# ifdef FEAT_GUI
7246 need_mouse_correct = TRUE;
7247# endif
7248 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007249 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250#endif
7251 }
7252}
7253
7254/*
7255 * ":cquit".
7256 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007258ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259{
7260 getout(1); /* this does not always pass on the exit code to the Manx
7261 compiler. why? */
7262}
7263
7264/*
7265 * ":qall": try to quit all windows
7266 */
7267 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007268ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269{
7270# ifdef FEAT_CMDWIN
7271 if (cmdwin_type != 0)
7272 {
7273 if (eap->forceit)
7274 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7275 else
7276 cmdwin_result = K_XF2;
7277 return;
7278 }
7279# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007280
7281 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007282 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007283 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007284 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007285 return;
7286 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007287#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007288 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7289 /* Refuse to quit when locked or when the buffer in the last window is
7290 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007291 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007292 return;
7293#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007294
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007296 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 getout(0);
7298 not_exiting();
7299}
7300
Bram Moolenaard9967712006-03-11 21:18:15 +00007301#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302/*
7303 * ":close": close current window, unless it is the last one
7304 */
7305 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007306ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007308 win_T *win;
7309 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310# ifdef FEAT_CMDWIN
7311 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007312 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 else
7314# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007315 if (!text_locked()
7316#ifdef FEAT_AUTOCMD
7317 && !curbuf_locked()
7318#endif
7319 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007320 {
7321 if (eap->addr_count == 0)
7322 ex_win_close(eap->forceit, curwin, NULL);
7323 else {
Bram Moolenaar29323592016-07-24 22:04:11 +02007324 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007325 {
7326 winnr++;
7327 if (winnr == eap->line2)
7328 break;
7329 }
7330 if (win == NULL)
7331 win = lastwin;
7332 ex_win_close(eap->forceit, win, NULL);
7333 }
7334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335}
7336
Bram Moolenaard9967712006-03-11 21:18:15 +00007337# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007338/*
7339 * ":pclose": Close any preview window.
7340 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007342ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007343{
7344 win_T *win;
7345
Bram Moolenaar29323592016-07-24 22:04:11 +02007346 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007347 if (win->w_p_pvw)
7348 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007349 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007350 break;
7351 }
7352}
Bram Moolenaard9967712006-03-11 21:18:15 +00007353# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007354
Bram Moolenaarf740b292006-02-16 22:11:02 +00007355/*
7356 * Close window "win" and take care of handling closing the last window for a
7357 * modified buffer.
7358 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007359 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007360ex_win_close(
7361 int forceit,
7362 win_T *win,
7363 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364{
7365 int need_hide;
7366 buf_T *buf = win->w_buffer;
7367
7368 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007369 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007371# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 if ((p_confirm || cmdmod.confirm) && p_write)
7373 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007374 bufref_T bufref;
7375
7376 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007378 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 return;
7380 need_hide = FALSE;
7381 }
7382 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007383# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 {
7385 EMSG(_(e_nowrtmsg));
7386 return;
7387 }
7388 }
7389
Bram Moolenaard9967712006-03-11 21:18:15 +00007390# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007392# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007393
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007395 if (tp == NULL)
7396 win_close(win, !need_hide && !P_HID(buf));
7397 else
7398 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399}
7400
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007402 * ":tabclose": close current tab page, unless it is the last one.
7403 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 */
7405 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007406ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007408 tabpage_T *tp;
7409
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007410# ifdef FEAT_CMDWIN
7411 if (cmdwin_type != 0)
7412 cmdwin_result = K_IGNORE;
7413 else
7414# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007415 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007416 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007417 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007419 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007420 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007421 tp = find_tabpage((int)eap->line2);
7422 if (tp == NULL)
7423 {
7424 beep_flush();
7425 return;
7426 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007427 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007428 {
7429 tabpage_close_other(tp, eap->forceit);
7430 return;
7431 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007432 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007433 if (!text_locked()
7434#ifdef FEAT_AUTOCMD
7435 && !curbuf_locked()
7436#endif
7437 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00007438 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007440}
7441
7442/*
7443 * ":tabonly": close all tab pages except the current one
7444 */
7445 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007446ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007447{
7448 tabpage_T *tp;
7449 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007450
7451# ifdef FEAT_CMDWIN
7452 if (cmdwin_type != 0)
7453 cmdwin_result = K_IGNORE;
7454 else
7455# endif
7456 if (first_tabpage->tp_next == NULL)
7457 MSG(_("Already only one tab page"));
7458 else
7459 {
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007460 if (eap->addr_count > 0)
7461 goto_tabpage(eap->line2);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007462 /* Repeat this up to a 1000 times, because autocommands may mess
7463 * up the lists. */
7464 for (done = 0; done < 1000; ++done)
7465 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007466 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007467 if (tp->tp_topframe != topframe)
7468 {
7469 tabpage_close_other(tp, eap->forceit);
7470 /* if we failed to close it quit */
7471 if (valid_tabpage(tp))
7472 done = 1000;
7473 /* start over, "tp" is now invalid */
7474 break;
7475 }
7476 if (first_tabpage->tp_next == NULL)
7477 break;
7478 }
7479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481
7482/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007483 * Close the current tab page.
7484 */
7485 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007486tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007487{
7488 /* First close all the windows but the current one. If that worked then
7489 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007490 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007491 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007492 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007493 ex_win_close(forceit, curwin, NULL);
7494# ifdef FEAT_GUI
7495 need_mouse_correct = TRUE;
7496# endif
7497}
7498
7499/*
7500 * Close tab page "tp", which is not the current tab page.
7501 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007502 * Also takes care of the tab pages line disappearing when closing the
7503 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007504 */
7505 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007506tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007507{
7508 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007509 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007510 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007511
7512 /* Limit to 1000 windows, autocommands may add a window while we close
7513 * one. OK, so I'm paranoid... */
7514 while (++done < 1000)
7515 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007516 wp = tp->tp_firstwin;
7517 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007518
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007519 /* Autocommands may delete the tab page under our fingers and we may
7520 * fail to close a window with a modified buffer. */
7521 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007522 break;
7523 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007524
Bram Moolenaar29323592016-07-24 22:04:11 +02007525#ifdef FEAT_AUTOCMD
7526 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
7527#endif
7528
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007529 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007530 if (h != tabline_height())
7531 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007532}
7533
7534/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 * ":only".
7536 */
7537 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007538ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007540 win_T *wp;
7541 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542# ifdef FEAT_GUI
7543 need_mouse_correct = TRUE;
7544# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007545 if (eap->addr_count > 0)
7546 {
7547 wnr = eap->line2;
7548 for (wp = firstwin; --wnr > 0; )
7549 {
7550 if (wp->w_next == NULL)
7551 break;
7552 else
7553 wp = wp->w_next;
7554 }
7555 win_goto(wp);
7556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 close_others(TRUE, eap->forceit);
7558}
7559
7560/*
7561 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007562 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007564 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007565ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566{
7567 if (eap->addr_count == 0)
7568 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007569 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570}
7571#endif /* FEAT_WINDOWS */
7572
7573 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007574ex_hide(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007576 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577#ifdef FEAT_WINDOWS
Bram Moolenaar2256c992016-11-15 21:17:07 +01007578 if (!eap->skip)
7579 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580# ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007581 need_mouse_correct = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582# endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007583 if (eap->addr_count == 0)
7584 win_close(curwin, FALSE); /* don't free buffer */
7585 else
7586 {
7587 int winnr = 0;
7588 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007589
Bram Moolenaar2256c992016-11-15 21:17:07 +01007590 FOR_ALL_WINDOWS(win)
7591 {
7592 winnr++;
7593 if (winnr == eap->line2)
7594 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007595 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007596 if (win == NULL)
7597 win = lastwin;
7598 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602}
7603
7604/*
7605 * ":stop" and ":suspend": Suspend Vim.
7606 */
7607 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007608ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609{
7610 /*
7611 * Disallow suspending for "rvim".
7612 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007613 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 {
7615 if (!eap->forceit)
7616 autowrite_all();
7617 windgoto((int)Rows - 1, 0);
7618 out_char('\n');
7619 out_flush();
7620 stoptermcap();
7621 out_flush(); /* needed for SUN to restore xterm buffer */
7622#ifdef FEAT_TITLE
7623 mch_restore_title(3); /* restore window titles */
7624#endif
7625 ui_suspend(); /* call machine specific function */
7626#ifdef FEAT_TITLE
7627 maketitle();
7628 resettitle(); /* force updating the title */
7629#endif
7630 starttermcap();
7631 scroll_start(); /* scroll screen before redrawing */
7632 redraw_later_clear();
7633 shell_resized(); /* may have resized window */
7634 }
7635}
7636
7637/*
7638 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7639 */
7640 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007641ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642{
7643#ifdef FEAT_CMDWIN
7644 if (cmdwin_type != 0)
7645 {
7646 cmdwin_result = Ctrl_C;
7647 return;
7648 }
7649#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007650 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007651 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007652 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007653 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007654 return;
7655 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007656#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007657 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7658 /* Refuse to quit when locked or when the buffer in the last window is
7659 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007660 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007661 return;
7662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663
7664 /*
7665 * if more files or windows we won't exit
7666 */
7667 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7668 exiting = TRUE;
7669 if ( ((eap->cmdidx == CMD_wq
7670 || curbufIsChanged())
7671 && do_write(eap) == FAIL)
7672 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007673 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 {
7675 not_exiting();
7676 }
7677 else
7678 {
7679#ifdef FEAT_WINDOWS
7680 if (only_one_window()) /* quit last window, exit Vim */
7681#endif
7682 getout(0);
7683#ifdef FEAT_WINDOWS
7684# ifdef FEAT_GUI
7685 need_mouse_correct = TRUE;
7686# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007687 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 win_close(curwin, !P_HID(curwin->w_buffer));
7689#endif
7690 }
7691}
7692
7693/*
7694 * ":print", ":list", ":number".
7695 */
7696 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007697ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007699 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7700 EMSG(_(e_emptybuf));
7701 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007703 for ( ;!got_int; ui_breakcheck())
7704 {
7705 print_line(eap->line1,
7706 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7707 || (eap->flags & EXFLAG_NR)),
7708 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7709 if (++eap->line1 > eap->line2)
7710 break;
7711 out_flush(); /* show one line at a time */
7712 }
7713 setpcmark();
7714 /* put cursor at last line */
7715 curwin->w_cursor.lnum = eap->line2;
7716 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 }
7718
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720}
7721
7722#ifdef FEAT_BYTEOFF
7723 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007724ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725{
7726 goto_byte(eap->line2);
7727}
7728#endif
7729
7730/*
7731 * ":shell".
7732 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007734ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735{
7736 do_shell(NULL, 0);
7737}
7738
7739#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7740 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007741 || defined(FEAT_GUI_MSWIN) \
7742 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 || defined(PROTO)
7744
7745/*
7746 * Handle a file drop. The code is here because a drop is *nearly* like an
7747 * :args command, but not quite (we have a list of exact filenames, so we
7748 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7749 * code is very similar to :args and hence needs access to a lot of the static
7750 * functions in this file.
7751 *
7752 * The list should be allocated using alloc(), as should each item in the
7753 * list. This function takes over responsibility for freeing the list.
7754 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007755 * XXX The list is made into the argument list. This is freed using
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007756 * FreeWild(), which does a series of vim_free() calls.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 */
7758 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007759handle_drop(
7760 int filec, /* the number of files dropped */
7761 char_u **filev, /* the list of files dropped */
7762 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763{
7764 exarg_T ea;
7765 int save_msg_scroll = msg_scroll;
7766
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007767 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007768 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007770#ifdef FEAT_AUTOCMD
7771 if (curbuf_locked())
7772 return;
7773#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007774 /* When the screen is being updated we should not change buffers and
7775 * windows structures, it may cause freed memory to be used. */
7776 if (updating_screen)
7777 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778
7779 /* Check whether the current buffer is changed. If so, we will need
7780 * to split the current window or data could be lost.
7781 * We don't need to check if the 'hidden' option is set, as in this
7782 * case the buffer won't be lost.
7783 */
7784 if (!P_HID(curbuf) && !split)
7785 {
7786 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007787 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 --emsg_off;
7789 }
7790 if (split)
7791 {
7792# ifdef FEAT_WINDOWS
7793 if (win_split(0, 0) == FAIL)
7794 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007795 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796
7797 /* When splitting the window, create a new alist. Otherwise the
7798 * existing one is overwritten. */
7799 alist_unlink(curwin->w_alist);
7800 alist_new();
7801# else
7802 return; /* can't split, always fail */
7803# endif
7804 }
7805
7806 /*
7807 * Set up the new argument list.
7808 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007809 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810
7811 /*
7812 * Move to the first file.
7813 */
7814 /* Fake up a minimal "next" command for do_argfile() */
7815 vim_memset(&ea, 0, sizeof(ea));
7816 ea.cmd = (char_u *)"next";
7817 do_argfile(&ea, 0);
7818
7819 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7820 * mode that is not needed here. */
7821 need_start_insertmode = FALSE;
7822
7823 /* Restore msg_scroll, otherwise a following command may cause scrolling
7824 * unexpectedly. The screen will be redrawn by the caller, thus
7825 * msg_scroll being set by displaying a message is irrelevant. */
7826 msg_scroll = save_msg_scroll;
7827}
7828#endif
7829
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830/*
7831 * Clear an argument list: free all file names and reset it to zero entries.
7832 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007833 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007834alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835{
7836 while (--al->al_ga.ga_len >= 0)
7837 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7838 ga_clear(&al->al_ga);
7839}
7840
7841/*
7842 * Init an argument list.
7843 */
7844 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007845alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846{
7847 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7848}
7849
7850#if defined(FEAT_WINDOWS) || defined(PROTO)
7851
7852/*
7853 * Remove a reference from an argument list.
7854 * Ignored when the argument list is the global one.
7855 * If the argument list is no longer used by any window, free it.
7856 */
7857 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007858alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859{
7860 if (al != &global_alist && --al->al_refcount <= 0)
7861 {
7862 alist_clear(al);
7863 vim_free(al);
7864 }
7865}
7866
7867# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7868/*
7869 * Create a new argument list and use it for the current window.
7870 */
7871 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007872alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873{
7874 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7875 if (curwin->w_alist == NULL)
7876 {
7877 curwin->w_alist = &global_alist;
7878 ++global_alist.al_refcount;
7879 }
7880 else
7881 {
7882 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007883 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 alist_init(curwin->w_alist);
7885 }
7886}
7887# endif
7888#endif
7889
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007890#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891/*
7892 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007893 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7894 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 */
7896 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007897alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898{
7899 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007900 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 char_u **new_arg_files;
7902 int new_arg_file_count;
7903 char_u *save_p_su = p_su;
7904 int i;
7905
7906 /* Don't use 'suffixes' here. This should work like the shell did the
7907 * expansion. Also, the vimrc file isn't read yet, thus the user
7908 * can't set the options. */
7909 p_su = empty_option;
7910 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7911 if (old_arg_files != NULL)
7912 {
7913 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007914 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7915 old_arg_count = GARGCOUNT;
7916 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007918 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 && new_arg_file_count > 0)
7920 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007921 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7922 TRUE, fnum_list, fnum_len);
7923 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 }
7926 p_su = save_p_su;
7927}
7928#endif
7929
7930/*
7931 * Set the argument list for the current window.
7932 * Takes over the allocated files[] and the allocated fnames in it.
7933 */
7934 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007935alist_set(
7936 alist_T *al,
7937 int count,
7938 char_u **files,
7939 int use_curbuf,
7940 int *fnum_list,
7941 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942{
7943 int i;
7944
7945 alist_clear(al);
7946 if (ga_grow(&al->al_ga, count) == OK)
7947 {
7948 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007949 {
7950 if (got_int)
7951 {
7952 /* When adding many buffers this can take a long time. Allow
7953 * interrupting here. */
7954 while (i < count)
7955 vim_free(files[i++]);
7956 break;
7957 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007958
7959 /* May set buffer name of a buffer previously used for the
7960 * argument list, so that it's re-used by alist_add. */
7961 if (fnum_list != NULL && i < fnum_len)
7962 buf_set_name(fnum_list[i], files[i]);
7963
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007965 ui_breakcheck();
7966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 vim_free(files);
7968 }
7969 else
7970 FreeWild(count, files);
7971#ifdef FEAT_WINDOWS
7972 if (al == &global_alist)
7973#endif
7974 arg_had_last = FALSE;
7975}
7976
7977/*
7978 * Add file "fname" to argument list "al".
7979 * "fname" must have been allocated and "al" must have been checked for room.
7980 */
7981 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007982alist_add(
7983 alist_T *al,
7984 char_u *fname,
7985 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986{
7987 if (fname == NULL) /* don't add NULL file names */
7988 return;
7989#ifdef BACKSLASH_IN_FILENAME
7990 slash_adjust(fname);
7991#endif
7992 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7993 if (set_fnum > 0)
7994 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7995 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7996 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997}
7998
7999#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8000/*
8001 * Adjust slashes in file names. Called after 'shellslash' was set.
8002 */
8003 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008004alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005{
8006 int i;
8007# ifdef FEAT_WINDOWS
8008 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008009 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010# endif
8011
8012 for (i = 0; i < GARGCOUNT; ++i)
8013 if (GARGLIST[i].ae_fname != NULL)
8014 slash_adjust(GARGLIST[i].ae_fname);
8015# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00008016 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 if (wp->w_alist != &global_alist)
8018 for (i = 0; i < WARGCOUNT(wp); ++i)
8019 if (WARGLIST(wp)[i].ae_fname != NULL)
8020 slash_adjust(WARGLIST(wp)[i].ae_fname);
8021# endif
8022}
8023#endif
8024
8025/*
8026 * ":preserve".
8027 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008029ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008031 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 ml_preserve(curbuf, TRUE);
8033}
8034
8035/*
8036 * ":recover".
8037 */
8038 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008039ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040{
8041 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8042 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008043 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8044 | CCGD_MULTWIN
8045 | (eap->forceit ? CCGD_FORCEIT : 0)
8046 | CCGD_EXCMD)
8047
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 && (*eap->arg == NUL
8049 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8050 ml_recover();
8051 recoverymode = FALSE;
8052}
8053
8054/*
8055 * Command modifier used in a wrong way.
8056 */
8057 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008058ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059{
8060 eap->errmsg = e_invcmd;
8061}
8062
8063#ifdef FEAT_WINDOWS
8064/*
8065 * :sview [+command] file split window with new file, read-only
8066 * :split [[+command] file] split window with current or new file
8067 * :vsplit [[+command] file] split window vertically with current or new file
8068 * :new [[+command] file] split window with no or new file
8069 * :vnew [[+command] file] split vertically window with no or new file
8070 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008071 *
8072 * :tabedit open new Tab page with empty window
8073 * :tabedit [+command] file open new Tab page and edit "file"
8074 * :tabnew [[+command] file] just like :tabedit
8075 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 */
8077 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008078ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008080 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008081# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008083# endif
8084# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008086# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008088# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008090# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008092# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008094 * quickfix windows. But it's OK when doing ":tab split". */
8095 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 {
8097 if (eap->cmdidx == CMD_split)
8098 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 if (eap->cmdidx == CMD_vsplit)
8100 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008102# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008104# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008105 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 {
8107 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8108 FNAME_MESS, TRUE, curbuf->b_ffname);
8109 if (fname == NULL)
8110 goto theend;
8111 eap->arg = fname;
8112 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008113# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008115# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008117# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 && eap->cmdidx != CMD_new)
8121 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008122# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008123 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008124# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008125 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008126# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008127 au_has_group((char_u *)"FileExplorer"))
8128 {
8129 /* No browsing supported but we do have the file explorer:
8130 * Edit the directory. */
8131 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8132 eap->arg = (char_u *)".";
8133 }
8134 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008135# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008136 {
8137 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008139 if (fname == NULL)
8140 goto theend;
8141 eap->arg = fname;
8142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 }
8144 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008145# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008147 /*
8148 * Either open new tab page or split the window.
8149 */
8150 if (eap->cmdidx == CMD_tabedit
8151 || eap->cmdidx == CMD_tabfind
8152 || eap->cmdidx == CMD_tabnew)
8153 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008154 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8155 : eap->addr_count == 0 ? 0
8156 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008157 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008158 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008159
8160 /* set the alternate buffer for the window we came from */
8161 if (curwin != old_curwin
8162 && win_valid(old_curwin)
8163 && old_curwin->w_buffer != curbuf
8164 && !cmdmod.keepalt)
8165 old_curwin->w_alt_fnum = curbuf->b_fnum;
8166 }
8167 }
8168 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8170 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008171# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 /* Reset 'scrollbind' when editing another file, but keep it when
8173 * doing ":split" without arguments. */
8174 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008175# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008177# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008179 {
8180 RESET_BINDING(curwin);
8181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 else
8183 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008184# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 do_exedit(eap, old_curwin);
8186 }
8187
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008188# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008190# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008192# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193theend:
8194 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008195# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008197
8198/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008199 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008200 */
8201 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008202tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008203{
8204 exarg_T ea;
8205
8206 vim_memset(&ea, 0, sizeof(ea));
8207 ea.cmdidx = CMD_tabnew;
8208 ea.cmd = (char_u *)"tabn";
8209 ea.arg = (char_u *)"";
8210 ex_splitview(&ea);
8211}
8212
8213/*
8214 * :tabnext command
8215 */
8216 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008217ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008218{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008219 switch (eap->cmdidx)
8220 {
8221 case CMD_tabfirst:
8222 case CMD_tabrewind:
8223 goto_tabpage(1);
8224 break;
8225 case CMD_tablast:
8226 goto_tabpage(9999);
8227 break;
8228 case CMD_tabprevious:
8229 case CMD_tabNext:
8230 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
8231 break;
8232 default: /* CMD_tabnext */
8233 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
8234 break;
8235 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008236}
8237
8238/*
8239 * :tabmove command
8240 */
8241 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008242ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008243{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008244 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008245
8246 if (eap->arg && *eap->arg != NUL)
8247 {
8248 char_u *p = eap->arg;
8249 int relative = 0; /* argument +N/-N means: move N places to the
8250 * right/left relative to the current position. */
8251
8252 if (*eap->arg == '-')
8253 {
8254 relative = -1;
8255 p = eap->arg + 1;
8256 }
8257 else if (*eap->arg == '+')
8258 {
8259 relative = 1;
8260 p = eap->arg + 1;
8261 }
8262 else
8263 p = eap->arg;
8264
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008265 if (relative == 0)
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008266 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008267 if (STRCMP(p, "$") == 0)
8268 tab_number = LAST_TAB_NR;
8269 else if (p == skipdigits(p))
8270 {
8271 /* No numbers as argument. */
8272 eap->errmsg = e_invarg;
8273 return;
8274 }
8275 else
8276 tab_number = getdigits(&p);
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008277 }
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008278 else
8279 {
8280 if (*p != NUL)
8281 tab_number = getdigits(&p);
8282 else
8283 tab_number = 1;
8284 tab_number = tab_number * relative + tabpage_index(curtab);
8285 if (relative == -1)
8286 --tab_number;
8287 }
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008288 }
8289 else if (eap->addr_count != 0)
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008290 {
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008291 tab_number = eap->line2;
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008292 if (**eap->cmdlinep == '-')
8293 --tab_number;
8294 }
8295 else
8296 tab_number = LAST_TAB_NR;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008297
8298 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008299}
8300
8301/*
8302 * :tabs command: List tabs and their contents.
8303 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008304 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008305ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008306{
8307 tabpage_T *tp;
8308 win_T *wp;
8309 int tabcount = 1;
8310
8311 msg_start();
8312 msg_scroll = TRUE;
8313 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8314 {
8315 msg_putchar('\n');
8316 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
8317 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
8318 out_flush(); /* output one line at a time */
8319 ui_breakcheck();
8320
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008321 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008322 wp = firstwin;
8323 else
8324 wp = tp->tp_firstwin;
8325 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8326 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008327 msg_putchar('\n');
8328 msg_putchar(wp == curwin ? '>' : ' ');
8329 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008330 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8331 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008332 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008333 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008334 else
8335 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8336 IObuff, IOSIZE, TRUE);
8337 msg_outtrans(IObuff);
8338 out_flush(); /* output one line at a time */
8339 ui_breakcheck();
8340 }
8341 }
8342}
8343
8344#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345
8346/*
8347 * ":mode": Set screen mode.
8348 * If no argument given, just get the screen size and redraw.
8349 */
8350 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008351ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352{
8353 if (*eap->arg == NUL)
8354 shell_resized();
8355 else
8356 mch_screenmode(eap->arg);
8357}
8358
8359#ifdef FEAT_WINDOWS
8360/*
8361 * ":resize".
8362 * set, increment or decrement current window height
8363 */
8364 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008365ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366{
8367 int n;
8368 win_T *wp = curwin;
8369
8370 if (eap->addr_count > 0)
8371 {
8372 n = eap->line2;
8373 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8374 ;
8375 }
8376
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008377# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008379# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 if (cmdmod.split & WSP_VERT)
8382 {
8383 if (*eap->arg == '-' || *eap->arg == '+')
8384 n += W_WIDTH(curwin);
8385 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8386 n = 9999;
8387 win_setwidth_win((int)n, wp);
8388 }
8389 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 {
8391 if (*eap->arg == '-' || *eap->arg == '+')
8392 n += curwin->w_height;
8393 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8394 n = 9999;
8395 win_setheight_win((int)n, wp);
8396 }
8397}
8398#endif
8399
8400/*
8401 * ":find [+command] <file>" command.
8402 */
8403 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008404ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405{
8406#ifdef FEAT_SEARCHPATH
8407 char_u *fname;
8408 int count;
8409
8410 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8411 TRUE, curbuf->b_ffname);
8412 if (eap->addr_count > 0)
8413 {
8414 /* Repeat finding the file "count" times. This matters when it
8415 * appears several times in the path. */
8416 count = eap->line2;
8417 while (fname != NULL && --count > 0)
8418 {
8419 vim_free(fname);
8420 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8421 FALSE, curbuf->b_ffname);
8422 }
8423 }
8424
8425 if (fname != NULL)
8426 {
8427 eap->arg = fname;
8428#endif
8429 do_exedit(eap, NULL);
8430#ifdef FEAT_SEARCHPATH
8431 vim_free(fname);
8432 }
8433#endif
8434}
8435
8436/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008437 * ":open" simulation: for now just work like ":visual".
8438 */
8439 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008440ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008441{
8442 regmatch_T regmatch;
8443 char_u *p;
8444
8445 curwin->w_cursor.lnum = eap->line2;
8446 beginline(BL_SOL | BL_FIX);
8447 if (*eap->arg == '/')
8448 {
8449 /* ":open /pattern/": put cursor in column found with pattern */
8450 ++eap->arg;
8451 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8452 *p = NUL;
8453 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8454 if (regmatch.regprog != NULL)
8455 {
8456 regmatch.rm_ic = p_ic;
8457 p = ml_get_curline();
8458 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008459 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008460 else
8461 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008462 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008463 }
8464 /* Move to the NUL, ignore any other arguments. */
8465 eap->arg += STRLEN(eap->arg);
8466 }
8467 check_cursor();
8468
8469 eap->cmdidx = CMD_visual;
8470 do_exedit(eap, NULL);
8471}
8472
8473/*
8474 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 */
8476 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008477ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478{
8479 do_exedit(eap, NULL);
8480}
8481
8482/*
8483 * ":edit <file>" command and alikes.
8484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008486do_exedit(
8487 exarg_T *eap,
8488 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489{
8490 int n;
8491#ifdef FEAT_WINDOWS
8492 int need_hide;
8493#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008494 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495
8496 /*
8497 * ":vi" command ends Ex mode.
8498 */
8499 if (exmode_active && (eap->cmdidx == CMD_visual
8500 || eap->cmdidx == CMD_view))
8501 {
8502 exmode_active = FALSE;
8503 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008504 {
8505 /* Special case: ":global/pat/visual\NLvi-commands" */
8506 if (global_busy)
8507 {
8508 int rd = RedrawingDisabled;
8509 int nwr = no_wait_return;
8510 int ms = msg_scroll;
8511#ifdef FEAT_GUI
8512 int he = hold_gui_events;
8513#endif
8514
8515 if (eap->nextcmd != NULL)
8516 {
8517 stuffReadbuff(eap->nextcmd);
8518 eap->nextcmd = NULL;
8519 }
8520
8521 if (exmode_was != EXMODE_VIM)
8522 settmode(TMODE_RAW);
8523 RedrawingDisabled = 0;
8524 no_wait_return = 0;
8525 need_wait_return = FALSE;
8526 msg_scroll = 0;
8527#ifdef FEAT_GUI
8528 hold_gui_events = 0;
8529#endif
8530 must_redraw = CLEAR;
8531
8532 main_loop(FALSE, TRUE);
8533
8534 RedrawingDisabled = rd;
8535 no_wait_return = nwr;
8536 msg_scroll = ms;
8537#ifdef FEAT_GUI
8538 hold_gui_events = he;
8539#endif
8540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 }
8544
8545 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008546 || eap->cmdidx == CMD_tabnew
8547 || eap->cmdidx == CMD_tabedit
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008548#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 || eap->cmdidx == CMD_vnew
8550#endif
8551 ) && *eap->arg == NUL)
8552 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008553 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 setpcmark();
8555 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008556 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8557 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 }
8559 else if ((eap->cmdidx != CMD_split
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008560#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 && eap->cmdidx != CMD_vsplit
8562#endif
8563 )
8564 || *eap->arg != NUL
8565#ifdef FEAT_BROWSE
8566 || cmdmod.browse
8567#endif
8568 )
8569 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008570#ifdef FEAT_AUTOCMD
8571 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8572 * can bring us here, others are stopped earlier. */
8573 if (*eap->arg != NUL && curbuf_locked())
8574 return;
8575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 n = readonlymode;
8577 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8578 readonlymode = TRUE;
8579 else if (eap->cmdidx == CMD_enew)
8580 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8581 empty buffer */
8582 setpcmark();
8583 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8584 NULL, eap,
8585 /* ":edit" goes to first line if Vi compatible */
8586 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8587 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8588 ? ECMD_ONE : eap->do_ecmd_lnum,
8589 (P_HID(curbuf) ? ECMD_HIDE : 0)
8590 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008591 /* after a split we can use an existing buffer */
8592 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593#ifdef FEAT_LISTCMDS
8594 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8595#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008596 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 {
8598 /* Editing the file failed. If the window was split, close it. */
8599#ifdef FEAT_WINDOWS
8600 if (old_curwin != NULL)
8601 {
8602 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8603 if (!need_hide || P_HID(curbuf))
8604 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008605# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8606 cleanup_T cs;
8607
8608 /* Reset the error/interrupt/exception state here so that
8609 * aborting() returns FALSE when closing a window. */
8610 enter_cleanup(&cs);
8611# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612# ifdef FEAT_GUI
8613 need_mouse_correct = TRUE;
8614# endif
8615 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008616
8617# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8618 /* Restore the error/interrupt/exception state if not
8619 * discarded by a new aborting error, interrupt, or
8620 * uncaught exception. */
8621 leave_cleanup(&cs);
8622# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 }
8624 }
8625#endif
8626 }
8627 else if (readonlymode && curbuf->b_nwindows == 1)
8628 {
8629 /* When editing an already visited buffer, 'readonly' won't be set
8630 * but the previous value is kept. With ":view" and ":sview" we
8631 * want the file to be readonly, except when another window is
8632 * editing the same buffer. */
8633 curbuf->b_p_ro = TRUE;
8634 }
8635 readonlymode = n;
8636 }
8637 else
8638 {
8639 if (eap->do_ecmd_cmd != NULL)
8640 do_cmdline_cmd(eap->do_ecmd_cmd);
8641#ifdef FEAT_TITLE
8642 n = curwin->w_arg_idx_invalid;
8643#endif
8644 check_arg_idx(curwin);
8645#ifdef FEAT_TITLE
8646 if (n != curwin->w_arg_idx_invalid)
8647 maketitle();
8648#endif
8649 }
8650
8651#ifdef FEAT_WINDOWS
8652 /*
8653 * if ":split file" worked, set alternate file name in old window to new
8654 * file
8655 */
8656 if (old_curwin != NULL
8657 && *eap->arg != NUL
8658 && curwin != old_curwin
8659 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008660 && old_curwin->w_buffer != curbuf
8661 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 old_curwin->w_alt_fnum = curbuf->b_fnum;
8663#endif
8664
8665 ex_no_reprint = TRUE;
8666}
8667
8668#ifndef FEAT_GUI
8669/*
8670 * ":gui" and ":gvim" when there is no GUI.
8671 */
8672 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008673ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674{
8675 eap->errmsg = e_nogvim;
8676}
8677#endif
8678
8679#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8680 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008681ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682{
8683 gui_make_tearoff(eap->arg);
8684}
8685#endif
8686
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008687#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008689ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008691 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692}
8693#endif
8694
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008696ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697{
8698 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8699 MSG(_("No swap file"));
8700 else
8701 msg(curbuf->b_ml.ml_mfp->mf_fname);
8702}
8703
8704/*
8705 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8706 * offset.
8707 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8708 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008710ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711{
8712#ifdef FEAT_SCROLLBIND
8713 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008714 win_T *save_curwin = curwin;
8715 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 long topline;
8717 long y;
8718 linenr_T old_linenr = curwin->w_cursor.lnum;
8719
8720 setpcmark();
8721
8722 /*
8723 * determine max topline
8724 */
8725 if (curwin->w_p_scb)
8726 {
8727 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008728 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 {
8730 if (wp->w_p_scb && wp->w_buffer)
8731 {
8732 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8733 if (topline > y)
8734 topline = y;
8735 }
8736 }
8737 if (topline < 1)
8738 topline = 1;
8739 }
8740 else
8741 {
8742 topline = 1;
8743 }
8744
8745
8746 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008747 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008749 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 {
8751 if (curwin->w_p_scb)
8752 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008753 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 y = topline - curwin->w_topline;
8755 if (y > 0)
8756 scrollup(y, TRUE);
8757 else
8758 scrolldown(-y, TRUE);
8759 curwin->w_scbind_pos = topline;
8760 redraw_later(VALID);
8761 cursor_correct();
8762#ifdef FEAT_WINDOWS
8763 curwin->w_redr_status = TRUE;
8764#endif
8765 }
8766 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008767 curwin = save_curwin;
8768 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 if (curwin->w_p_scb)
8770 {
8771 did_syncbind = TRUE;
8772 checkpcmark();
8773 if (old_linenr != curwin->w_cursor.lnum)
8774 {
8775 char_u ctrl_o[2];
8776
8777 ctrl_o[0] = Ctrl_O;
8778 ctrl_o[1] = 0;
8779 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8780 }
8781 }
8782#endif
8783}
8784
8785
8786 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008787ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008789 int i;
8790 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8791 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792
8793 if (eap->usefilter) /* :r!cmd */
8794 do_bang(1, eap, FALSE, FALSE, TRUE);
8795 else
8796 {
8797 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8798 return;
8799
8800#ifdef FEAT_BROWSE
8801 if (cmdmod.browse)
8802 {
8803 char_u *browseFile;
8804
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008805 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 NULL, NULL, NULL, curbuf);
8807 if (browseFile != NULL)
8808 {
8809 i = readfile(browseFile, NULL,
8810 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8811 vim_free(browseFile);
8812 }
8813 else
8814 i = OK;
8815 }
8816 else
8817#endif
8818 if (*eap->arg == NUL)
8819 {
8820 if (check_fname() == FAIL) /* check for no file name */
8821 return;
8822 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8823 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8824 }
8825 else
8826 {
8827 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8828 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8829 i = readfile(eap->arg, NULL,
8830 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8831
8832 }
8833 if (i == FAIL)
8834 {
8835#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8836 if (!aborting())
8837#endif
8838 EMSG2(_(e_notopen), eap->arg);
8839 }
8840 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008841 {
8842 if (empty && exmode_active)
8843 {
8844 /* Delete the empty line that remains. Historically ex does
8845 * this but vi doesn't. */
8846 if (eap->line2 == 0)
8847 lnum = curbuf->b_ml.ml_line_count;
8848 else
8849 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008850 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008851 {
8852 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008853 if (curwin->w_cursor.lnum > 1
8854 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008855 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008856 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008857 }
8858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 }
8862}
8863
Bram Moolenaarea408852005-06-25 22:49:46 +00008864static char_u *prev_dir = NULL;
8865
8866#if defined(EXITFREE) || defined(PROTO)
8867 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008868free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00008869{
8870 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008871 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008872
8873 vim_free(globaldir);
8874 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008875}
8876#endif
8877
Bram Moolenaarf4258302013-06-02 18:20:17 +02008878/*
8879 * Deal with the side effects of changing the current directory.
8880 * When "local" is TRUE then this was after an ":lcd" command.
8881 */
8882 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008883post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008884{
8885 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008886 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008887 if (local)
8888 {
8889 /* If still in global directory, need to remember current
8890 * directory as global directory. */
8891 if (globaldir == NULL && prev_dir != NULL)
8892 globaldir = vim_strsave(prev_dir);
8893 /* Remember this local directory for the window. */
8894 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8895 curwin->w_localdir = vim_strsave(NameBuff);
8896 }
8897 else
8898 {
8899 /* We are now in the global directory, no need to remember its
8900 * name. */
8901 vim_free(globaldir);
8902 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008903 }
8904
8905 shorten_fnames(TRUE);
8906}
8907
Bram Moolenaarea408852005-06-25 22:49:46 +00008908
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909/*
8910 * ":cd", ":lcd", ":chdir" and ":lchdir".
8911 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008912 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008913ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 char_u *new_dir;
8916 char_u *tofree;
8917
8918 new_dir = eap->arg;
8919#if !defined(UNIX) && !defined(VMS)
8920 /* for non-UNIX ":cd" means: print current directory */
8921 if (*new_dir == NUL)
8922 ex_pwd(NULL);
8923 else
8924#endif
8925 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008926#ifdef FEAT_AUTOCMD
8927 if (allbuf_locked())
8928 return;
8929#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008930 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8931 && !eap->forceit)
8932 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008933 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008934 return;
8935 }
8936
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 /* ":cd -": Change to previous directory */
8938 if (STRCMP(new_dir, "-") == 0)
8939 {
8940 if (prev_dir == NULL)
8941 {
8942 EMSG(_("E186: No previous directory"));
8943 return;
8944 }
8945 new_dir = prev_dir;
8946 }
8947
8948 /* Save current directory for next ":cd -" */
8949 tofree = prev_dir;
8950 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8951 prev_dir = vim_strsave(NameBuff);
8952 else
8953 prev_dir = NULL;
8954
8955#if defined(UNIX) || defined(VMS)
8956 /* for UNIX ":cd" means: go to home directory */
8957 if (*new_dir == NUL)
8958 {
8959 /* use NameBuff for home directory name */
8960# ifdef VMS
8961 char_u *p;
8962
8963 p = mch_getenv((char_u *)"SYS$LOGIN");
8964 if (p == NULL || *p == NUL) /* empty is the same as not set */
8965 NameBuff[0] = NUL;
8966 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008967 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968# else
8969 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8970# endif
8971 new_dir = NameBuff;
8972 }
8973#endif
8974 if (new_dir == NULL || vim_chdir(new_dir))
8975 EMSG(_(e_failed));
8976 else
8977 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008978 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979
8980 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008981 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 ex_pwd(eap);
8983 }
8984 vim_free(tofree);
8985 }
8986}
8987
8988/*
8989 * ":pwd".
8990 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008992ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993{
8994 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8995 {
8996#ifdef BACKSLASH_IN_FILENAME
8997 slash_adjust(NameBuff);
8998#endif
8999 msg(NameBuff);
9000 }
9001 else
9002 EMSG(_("E187: Unknown"));
9003}
9004
9005/*
9006 * ":=".
9007 */
9008 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009009ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010{
9011 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009012 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013}
9014
9015 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009016ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009018 int n;
9019 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020
9021 if (cursor_valid())
9022 {
9023 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9024 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01009025 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009027
9028 len = eap->line2;
9029 switch (*eap->arg)
9030 {
9031 case 'm': break;
9032 case NUL: len *= 1000L; break;
9033 default: EMSG2(_(e_invarg2), eap->arg); return;
9034 }
9035 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036}
9037
9038/*
9039 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9040 */
9041 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009042do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043{
9044 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009045 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046
9047 cursor_on();
9048 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009049 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009051 wait_now = msec - done > 1000L ? 1000L : msec - done;
9052#ifdef FEAT_TIMERS
9053 {
9054 long due_time = check_due_timer();
9055
9056 if (due_time > 0 && due_time < wait_now)
9057 wait_now = due_time;
9058 }
9059#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009060#ifdef FEAT_JOB_CHANNEL
9061 if (has_any_channel() && wait_now > 100L)
9062 wait_now = 100L;
9063#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009064 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009065#ifdef FEAT_JOB_CHANNEL
9066 if (has_any_channel())
9067 ui_breakcheck_force(TRUE);
9068 else
9069#endif
9070 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009071#ifdef MESSAGE_QUEUE
9072 /* Process the netbeans and clientserver messages that may have been
9073 * received in the call to ui_breakcheck() when the GUI is in use. This
9074 * may occur when running a test case. */
9075 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 }
9078}
9079
9080 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009081do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082{
9083 int mode;
9084 char_u *cmdp;
9085
9086 cmdp = eap->cmd;
9087 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9088
9089 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9090 eap->arg, mode, isabbrev))
9091 {
9092 case 1: EMSG(_(e_invarg));
9093 break;
9094 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9095 break;
9096 }
9097}
9098
9099/*
9100 * ":winsize" command (obsolete).
9101 */
9102 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009103ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104{
9105 int w, h;
9106 char_u *arg = eap->arg;
9107 char_u *p;
9108
9109 w = getdigits(&arg);
9110 arg = skipwhite(arg);
9111 p = arg;
9112 h = getdigits(&arg);
9113 if (*p != NUL && *arg == NUL)
9114 set_shellsize(w, h, TRUE);
9115 else
9116 EMSG(_("E465: :winsize requires two number arguments"));
9117}
9118
9119#ifdef FEAT_WINDOWS
9120 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009121ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122{
9123 int xchar = NUL;
9124 char_u *p;
9125
9126 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9127 {
9128 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9129 if (eap->arg[1] == NUL)
9130 {
9131 EMSG(_(e_invarg));
9132 return;
9133 }
9134 xchar = eap->arg[1];
9135 p = eap->arg + 2;
9136 }
9137 else
9138 p = eap->arg + 1;
9139
9140 eap->nextcmd = check_nextcmd(p);
9141 p = skipwhite(p);
9142 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9143 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009144 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 {
9146 /* Pass flags on for ":vertical wincmd ]". */
9147 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009148 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9150 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009151 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 }
9153}
9154#endif
9155
Bram Moolenaar843ee412004-06-30 16:16:41 +00009156#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157/*
9158 * ":winpos".
9159 */
9160 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009161ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162{
9163 int x, y;
9164 char_u *arg = eap->arg;
9165 char_u *p;
9166
9167 if (*arg == NUL)
9168 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009169# if defined(FEAT_GUI) || defined(MSWIN)
9170# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009172# else
9173 if (mch_get_winpos(&x, &y) != FAIL)
9174# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 {
9176 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9177 msg(IObuff);
9178 }
9179 else
9180# endif
9181 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9182 }
9183 else
9184 {
9185 x = getdigits(&arg);
9186 arg = skipwhite(arg);
9187 p = arg;
9188 y = getdigits(&arg);
9189 if (*p == NUL || *arg != NUL)
9190 {
9191 EMSG(_("E466: :winpos requires two number arguments"));
9192 return;
9193 }
9194# ifdef FEAT_GUI
9195 if (gui.in_use)
9196 gui_mch_set_winpos(x, y);
9197 else if (gui.starting)
9198 {
9199 /* Remember the coordinates for when the window is opened. */
9200 gui_win_x = x;
9201 gui_win_y = y;
9202 }
9203# ifdef HAVE_TGETENT
9204 else
9205# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009206# else
9207# ifdef MSWIN
9208 mch_set_winpos(x, y);
9209# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210# endif
9211# ifdef HAVE_TGETENT
9212 if (*T_CWP)
9213 term_set_winpos(x, y);
9214# endif
9215 }
9216}
9217#endif
9218
9219/*
9220 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9221 */
9222 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009223ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224{
9225 oparg_T oa;
9226
9227 clear_oparg(&oa);
9228 oa.regname = eap->regname;
9229 oa.start.lnum = eap->line1;
9230 oa.end.lnum = eap->line2;
9231 oa.line_count = eap->line2 - eap->line1 + 1;
9232 oa.motion_type = MLINE;
9233#ifdef FEAT_VIRTUALEDIT
9234 virtual_op = FALSE;
9235#endif
9236 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9237 {
9238 setpcmark();
9239 curwin->w_cursor.lnum = eap->line1;
9240 beginline(BL_SOL | BL_FIX);
9241 }
9242
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009243 if (VIsual_active)
9244 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009245
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 switch (eap->cmdidx)
9247 {
9248 case CMD_delete:
9249 oa.op_type = OP_DELETE;
9250 op_delete(&oa);
9251 break;
9252
9253 case CMD_yank:
9254 oa.op_type = OP_YANK;
9255 (void)op_yank(&oa, FALSE, TRUE);
9256 break;
9257
9258 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009259 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009261 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9262#else
9263 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009265 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 oa.op_type = OP_RSHIFT;
9267 else
9268 oa.op_type = OP_LSHIFT;
9269 op_shift(&oa, FALSE, eap->amount);
9270 break;
9271 }
9272#ifdef FEAT_VIRTUALEDIT
9273 virtual_op = MAYBE;
9274#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009275 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276}
9277
9278/*
9279 * ":put".
9280 */
9281 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009282ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283{
9284 /* ":0put" works like ":1put!". */
9285 if (eap->line2 == 0)
9286 {
9287 eap->line2 = 1;
9288 eap->forceit = TRUE;
9289 }
9290 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009291 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9292 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293}
9294
9295/*
9296 * Handle ":copy" and ":move".
9297 */
9298 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009299ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300{
9301 long n;
9302
Bram Moolenaaraa23b372015-09-08 18:46:31 +02009303 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 if (eap->arg == NULL) /* error detected */
9305 {
9306 eap->nextcmd = NULL;
9307 return;
9308 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009309 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310
9311 /*
9312 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9313 */
9314 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9315 {
9316 EMSG(_(e_invaddr));
9317 return;
9318 }
9319
9320 if (eap->cmdidx == CMD_move)
9321 {
9322 if (do_move(eap->line1, eap->line2, n) == FAIL)
9323 return;
9324 }
9325 else
9326 ex_copy(eap->line1, eap->line2, n);
9327 u_clearline();
9328 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009329 ex_may_print(eap);
9330}
9331
9332/*
9333 * Print the current line if flags were given to the Ex command.
9334 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009335 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009336ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009337{
9338 if (eap->flags != 0)
9339 {
9340 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9341 (eap->flags & EXFLAG_LIST));
9342 ex_no_reprint = TRUE;
9343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344}
9345
9346/*
9347 * ":smagic" and ":snomagic".
9348 */
9349 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009350ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351{
9352 int magic_save = p_magic;
9353
9354 p_magic = (eap->cmdidx == CMD_smagic);
9355 do_sub(eap);
9356 p_magic = magic_save;
9357}
9358
9359/*
9360 * ":join".
9361 */
9362 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009363ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364{
9365 curwin->w_cursor.lnum = eap->line1;
9366 if (eap->line1 == eap->line2)
9367 {
9368 if (eap->addr_count >= 2) /* :2,2join does nothing */
9369 return;
9370 if (eap->line2 == curbuf->b_ml.ml_line_count)
9371 {
9372 beep_flush();
9373 return;
9374 }
9375 ++eap->line2;
9376 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009377 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009379 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380}
9381
9382/*
9383 * ":[addr]@r" or ":[addr]*r": execute register
9384 */
9385 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009386ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387{
9388 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009389 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390
9391 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009392 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393
9394#ifdef USE_ON_FLY_SCROLL
9395 dont_scroll = TRUE; /* disallow scrolling here */
9396#endif
9397
9398 /* get the register name. No name means to use the previous one */
9399 c = *eap->arg;
9400 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9401 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009402 /* Put the register in the typeahead buffer with the "silent" flag. */
9403 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9404 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009405 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 else
9409 {
9410 int save_efr = exec_from_reg;
9411
9412 exec_from_reg = TRUE;
9413
9414 /*
9415 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009416 * Continue until the stuff buffer is empty and all added characters
9417 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009419 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9421
9422 exec_from_reg = save_efr;
9423 }
9424}
9425
9426/*
9427 * ":!".
9428 */
9429 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009430ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431{
9432 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9433}
9434
9435/*
9436 * ":undo".
9437 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009439ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009441 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009442 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009443 else
9444 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445}
9446
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009447#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009448 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009449ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009450{
9451 char_u hash[UNDO_HASH_SIZE];
9452
9453 u_compute_hash(hash);
9454 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9455}
9456
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009457 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009458ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009459{
9460 char_u hash[UNDO_HASH_SIZE];
9461
9462 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009463 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009464}
9465#endif
9466
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467/*
9468 * ":redo".
9469 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009471ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472{
9473 u_redo(1);
9474}
9475
9476/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009477 * ":earlier" and ":later".
9478 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009479 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009480ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009481{
9482 long count = 0;
9483 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009484 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009485 char_u *p = eap->arg;
9486
9487 if (*p == NUL)
9488 count = 1;
9489 else if (isdigit(*p))
9490 {
9491 count = getdigits(&p);
9492 switch (*p)
9493 {
9494 case 's': ++p; sec = TRUE; break;
9495 case 'm': ++p; sec = TRUE; count *= 60; break;
9496 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009497 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9498 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009499 }
9500 }
9501
9502 if (*p != NUL)
9503 EMSG2(_(e_invarg2), eap->arg);
9504 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009505 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9506 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009507}
9508
9509/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 * ":redir": start/stop redirection.
9511 */
9512 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009513ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514{
9515 char *mode;
9516 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009517 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518
Bram Moolenaarba768492016-07-08 15:32:54 +02009519#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009520 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009521 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009522 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009523 return;
9524 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009525#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009526
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527 if (STRICMP(eap->arg, "END") == 0)
9528 close_redir();
9529 else
9530 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009531 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009533 ++arg;
9534 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009536 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537 mode = "a";
9538 }
9539 else
9540 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009541 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542
9543 close_redir();
9544
9545 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009546 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 if (fname == NULL)
9548 return;
9549#ifdef FEAT_BROWSE
9550 if (cmdmod.browse)
9551 {
9552 char_u *browseFile;
9553
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009554 browseFile = do_browse(BROWSE_SAVE,
9555 (char_u *)_("Save Redirection"),
9556 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 if (browseFile == NULL)
9558 return; /* operation cancelled */
9559 vim_free(fname);
9560 fname = browseFile;
9561 eap->forceit = TRUE; /* since dialog already asked */
9562 }
9563#endif
9564
9565 redir_fd = open_exfile(fname, eap->forceit, mode);
9566 vim_free(fname);
9567 }
9568#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009569 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 {
9571 /* redirect to a register a-z (resp. A-Z for appending) */
9572 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009573 ++arg;
9574 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009576 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009577 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009579 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009581 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009582 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009583 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009584 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009586 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009587 if (*arg == '>')
9588 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009589 /* Make register empty when not using @A-@Z and the
9590 * command is valid. */
9591 if (*arg == NUL && !isupper(redir_reg))
9592 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009594 }
9595 if (*arg != NUL)
9596 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009597 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009598 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009599 }
9600 }
9601 else if (*arg == '=' && arg[1] == '>')
9602 {
9603 int append;
9604
9605 /* redirect to a variable */
9606 close_redir();
9607 arg += 2;
9608
9609 if (*arg == '>')
9610 {
9611 ++arg;
9612 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 }
9614 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009615 append = FALSE;
9616
9617 if (var_redir_start(skipwhite(arg), append) == OK)
9618 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 }
9620#endif
9621
9622 /* TODO: redirect to a buffer */
9623
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009625 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009627
9628 /* Make sure redirection is not off. Can happen for cmdline completion
9629 * that indirectly invokes a command to catch its output. */
9630 if (redir_fd != NULL
9631#ifdef FEAT_EVAL
9632 || redir_reg || redir_vname
9633#endif
9634 )
9635 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636}
9637
9638/*
9639 * ":redraw": force redraw
9640 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009641 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009642ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643{
9644 int r = RedrawingDisabled;
9645 int p = p_lz;
9646
9647 RedrawingDisabled = 0;
9648 p_lz = FALSE;
9649 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009650 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651#ifdef FEAT_TITLE
9652 if (need_maketitle)
9653 maketitle();
9654#endif
9655 RedrawingDisabled = r;
9656 p_lz = p;
9657
9658 /* Reset msg_didout, so that a message that's there is overwritten. */
9659 msg_didout = FALSE;
9660 msg_col = 0;
9661
Bram Moolenaar56667a52013-07-17 11:54:28 +02009662 /* No need to wait after an intentional redraw. */
9663 need_wait_return = FALSE;
9664
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 out_flush();
9666}
9667
9668/*
9669 * ":redrawstatus": force redraw of status line(s)
9670 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009672ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673{
9674#if defined(FEAT_WINDOWS)
9675 int r = RedrawingDisabled;
9676 int p = p_lz;
9677
9678 RedrawingDisabled = 0;
9679 p_lz = FALSE;
9680 if (eap->forceit)
9681 status_redraw_all();
9682 else
9683 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009684 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 RedrawingDisabled = r;
9686 p_lz = p;
9687 out_flush();
9688#endif
9689}
9690
9691 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009692close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693{
9694 if (redir_fd != NULL)
9695 {
9696 fclose(redir_fd);
9697 redir_fd = NULL;
9698 }
9699#ifdef FEAT_EVAL
9700 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009701 if (redir_vname)
9702 {
9703 var_redir_stop();
9704 redir_vname = 0;
9705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706#endif
9707}
9708
9709#if defined(FEAT_SESSION) && defined(USE_CRNL)
9710# define MKSESSION_NL
9711static int mksession_nl = FALSE; /* use NL only in put_eol() */
9712#endif
9713
9714/*
9715 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9716 */
9717 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009718ex_mkrc(
9719 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720{
9721 FILE *fd;
9722 int failed = FALSE;
9723 char_u *fname;
9724#ifdef FEAT_BROWSE
9725 char_u *browseFile = NULL;
9726#endif
9727#ifdef FEAT_SESSION
9728 int view_session = FALSE;
9729 int using_vdir = FALSE; /* using 'viewdir'? */
9730 char_u *viewFile = NULL;
9731 unsigned *flagp;
9732#endif
9733
9734 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9735 {
9736#ifdef FEAT_SESSION
9737 view_session = TRUE;
9738#else
9739 ex_ni(eap);
9740 return;
9741#endif
9742 }
9743
9744#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009745 /* Use the short file name until ":lcd" is used. We also don't use the
9746 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009747 did_lcd = FALSE;
9748
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9750 if (eap->cmdidx == CMD_mkview
9751 && (*eap->arg == NUL
9752 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9753 {
9754 eap->forceit = TRUE;
9755 fname = get_view_file(*eap->arg);
9756 if (fname == NULL)
9757 return;
9758 viewFile = fname;
9759 using_vdir = TRUE;
9760 }
9761 else
9762#endif
9763 if (*eap->arg != NUL)
9764 fname = eap->arg;
9765 else if (eap->cmdidx == CMD_mkvimrc)
9766 fname = (char_u *)VIMRC_FILE;
9767#ifdef FEAT_SESSION
9768 else if (eap->cmdidx == CMD_mksession)
9769 fname = (char_u *)SESSION_FILE;
9770#endif
9771 else
9772 fname = (char_u *)EXRC_FILE;
9773
9774#ifdef FEAT_BROWSE
9775 if (cmdmod.browse)
9776 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009777 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778# ifdef FEAT_SESSION
9779 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9780 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9781# endif
9782 (char_u *)_("Save Setup"),
9783 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9784 if (browseFile == NULL)
9785 goto theend;
9786 fname = browseFile;
9787 eap->forceit = TRUE; /* since dialog already asked */
9788 }
9789#endif
9790
9791#if defined(FEAT_SESSION) && defined(vim_mkdir)
9792 /* When using 'viewdir' may have to create the directory. */
9793 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009794 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795#endif
9796
9797 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9798 if (fd != NULL)
9799 {
9800#ifdef FEAT_SESSION
9801 if (eap->cmdidx == CMD_mkview)
9802 flagp = &vop_flags;
9803 else
9804 flagp = &ssop_flags;
9805#endif
9806
9807#ifdef MKSESSION_NL
9808 /* "unix" in 'sessionoptions': use NL line separator */
9809 if (view_session && (*flagp & SSOP_UNIX))
9810 mksession_nl = TRUE;
9811#endif
9812
9813 /* Write the version command for :mkvimrc */
9814 if (eap->cmdidx == CMD_mkvimrc)
9815 (void)put_line(fd, "version 6.0");
9816
9817#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009818 if (eap->cmdidx == CMD_mksession)
9819 {
9820 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9821 failed = TRUE;
9822 }
9823
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824 if (eap->cmdidx != CMD_mkview)
9825#endif
9826 {
9827 /* Write setting 'compatible' first, because it has side effects.
9828 * For that same reason only do it when needed. */
9829 if (p_cp)
9830 (void)put_line(fd, "if !&cp | set cp | endif");
9831 else
9832 (void)put_line(fd, "if &cp | set nocp | endif");
9833 }
9834
9835#ifdef FEAT_SESSION
9836 if (!view_session
9837 || (eap->cmdidx == CMD_mksession
9838 && (*flagp & SSOP_OPTIONS)))
9839#endif
9840 failed |= (makemap(fd, NULL) == FAIL
9841 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9842
9843#ifdef FEAT_SESSION
9844 if (!failed && view_session)
9845 {
9846 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9847 failed = TRUE;
9848 if (eap->cmdidx == CMD_mksession)
9849 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009850 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851
Bram Moolenaard9462e32011-04-11 21:35:11 +02009852 dirnow = alloc(MAXPATHL);
9853 if (dirnow == NULL)
9854 failed = TRUE;
9855 else
9856 {
9857 /*
9858 * Change to session file's dir.
9859 */
9860 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009862 *dirnow = NUL;
9863 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9864 {
9865 if (vim_chdirfile(fname) == OK)
9866 shorten_fnames(TRUE);
9867 }
9868 else if (*dirnow != NUL
9869 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9870 {
9871 if (mch_chdir((char *)globaldir) == 0)
9872 shorten_fnames(TRUE);
9873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874
Bram Moolenaard9462e32011-04-11 21:35:11 +02009875 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876
Bram Moolenaard9462e32011-04-11 21:35:11 +02009877 /* restore original dir */
9878 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009880 {
9881 if (mch_chdir((char *)dirnow) != 0)
9882 EMSG(_(e_prev_dir));
9883 shorten_fnames(TRUE);
9884 }
9885 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 }
9887 }
9888 else
9889 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009890 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9891 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 }
9893 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9894 == FAIL)
9895 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009896 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9897 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009898 if (eap->cmdidx == CMD_mksession)
9899 {
9900 if (put_line(fd, "unlet SessionLoad") == FAIL)
9901 failed = TRUE;
9902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903 }
9904#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009905 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9906 failed = TRUE;
9907
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908 failed |= fclose(fd);
9909
9910 if (failed)
9911 EMSG(_(e_write));
9912#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9913 else if (eap->cmdidx == CMD_mksession)
9914 {
9915 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009916 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917
Bram Moolenaard9462e32011-04-11 21:35:11 +02009918 tbuf = alloc(MAXPATHL);
9919 if (tbuf != NULL)
9920 {
9921 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9922 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9923 vim_free(tbuf);
9924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925 }
9926#endif
9927#ifdef MKSESSION_NL
9928 mksession_nl = FALSE;
9929#endif
9930 }
9931
9932#ifdef FEAT_BROWSE
9933theend:
9934 vim_free(browseFile);
9935#endif
9936#ifdef FEAT_SESSION
9937 vim_free(viewFile);
9938#endif
9939}
9940
Bram Moolenaardf177f62005-02-22 08:39:57 +00009941#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9942 || defined(PROTO)
9943 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009944vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009945{
9946 if (vim_mkdir(name, prot) != 0)
9947 {
9948 EMSG2(_("E739: Cannot create directory: %s"), name);
9949 return FAIL;
9950 }
9951 return OK;
9952}
9953#endif
9954
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955/*
9956 * Open a file for writing for an Ex command, with some checks.
9957 * Return file descriptor, or NULL on failure.
9958 */
9959 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009960open_exfile(
9961 char_u *fname,
9962 int forceit,
9963 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964{
9965 FILE *fd;
9966
9967#ifdef UNIX
9968 /* with Unix it is possible to open a directory */
9969 if (mch_isdir(fname))
9970 {
9971 EMSG2(_(e_isadir2), fname);
9972 return NULL;
9973 }
9974#endif
9975 if (!forceit && *mode != 'a' && vim_fexists(fname))
9976 {
9977 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9978 return NULL;
9979 }
9980
9981 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9982 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9983
9984 return fd;
9985}
9986
9987/*
9988 * ":mark" and ":k".
9989 */
9990 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009991ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992{
9993 pos_T pos;
9994
9995 if (*eap->arg == NUL) /* No argument? */
9996 EMSG(_(e_argreq));
9997 else if (eap->arg[1] != NUL) /* more than one character? */
9998 EMSG(_(e_trailing));
9999 else
10000 {
10001 pos = curwin->w_cursor; /* save curwin->w_cursor */
10002 curwin->w_cursor.lnum = eap->line2;
10003 beginline(BL_WHITE | BL_FIX);
10004 if (setmark(*eap->arg) == FAIL) /* set mark */
10005 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10006 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10007 }
10008}
10009
10010/*
10011 * Update w_topline, w_leftcol and the cursor position.
10012 */
10013 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010014update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015{
10016 check_cursor(); /* put cursor on valid line */
10017 update_topline();
10018 if (!curwin->w_p_wrap)
10019 validate_cursor();
10020 update_curswant();
10021}
10022
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023/*
10024 * ":normal[!] {commands}": Execute normal mode commands.
10025 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010026 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010027ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 int save_msg_scroll = msg_scroll;
10030 int save_restart_edit = restart_edit;
10031 int save_msg_didout = msg_didout;
10032 int save_State = State;
10033 tasave_T tabuf;
10034 int save_insertmode = p_im;
10035 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010036 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037#ifdef FEAT_MBYTE
10038 char_u *arg = NULL;
10039 int l;
10040 char_u *p;
10041#endif
10042
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010043 if (ex_normal_lock > 0)
10044 {
10045 EMSG(_(e_secure));
10046 return;
10047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 if (ex_normal_busy >= p_mmd)
10049 {
10050 EMSG(_("E192: Recursive use of :normal too deep"));
10051 return;
10052 }
10053 ++ex_normal_busy;
10054
10055 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10056 restart_edit = 0; /* don't go to Insert mode */
10057 p_im = FALSE; /* don't use 'insertmode' */
10058
10059#ifdef FEAT_MBYTE
10060 /*
10061 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10062 * this for the K_SPECIAL leading byte, otherwise special keys will not
10063 * work.
10064 */
10065 if (has_mbyte)
10066 {
10067 int len = 0;
10068
10069 /* Count the number of characters to be escaped. */
10070 for (p = eap->arg; *p != NUL; ++p)
10071 {
10072# ifdef FEAT_GUI
10073 if (*p == CSI) /* leadbyte CSI */
10074 len += 2;
10075# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010076 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10078# ifdef FEAT_GUI
10079 || *p == CSI
10080# endif
10081 )
10082 len += 2;
10083 }
10084 if (len > 0)
10085 {
10086 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10087 if (arg != NULL)
10088 {
10089 len = 0;
10090 for (p = eap->arg; *p != NUL; ++p)
10091 {
10092 arg[len++] = *p;
10093# ifdef FEAT_GUI
10094 if (*p == CSI)
10095 {
10096 arg[len++] = KS_EXTRA;
10097 arg[len++] = (int)KE_CSI;
10098 }
10099# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010100 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 {
10102 arg[len++] = *++p;
10103 if (*p == K_SPECIAL)
10104 {
10105 arg[len++] = KS_SPECIAL;
10106 arg[len++] = KE_FILLER;
10107 }
10108# ifdef FEAT_GUI
10109 else if (*p == CSI)
10110 {
10111 arg[len++] = KS_EXTRA;
10112 arg[len++] = (int)KE_CSI;
10113 }
10114# endif
10115 }
10116 arg[len] = NUL;
10117 }
10118 }
10119 }
10120 }
10121#endif
10122
10123 /*
10124 * Save the current typeahead. This is required to allow using ":normal"
10125 * from an event handler and makes sure we don't hang when the argument
10126 * ends with half a command.
10127 */
10128 save_typeahead(&tabuf);
10129 if (tabuf.typebuf_valid)
10130 {
10131 /*
10132 * Repeat the :normal command for each line in the range. When no
10133 * range given, execute it just once, without positioning the cursor
10134 * first.
10135 */
10136 do
10137 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 if (eap->addr_count != 0)
10139 {
10140 curwin->w_cursor.lnum = eap->line1++;
10141 curwin->w_cursor.col = 0;
10142 }
10143
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010144 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145#ifdef FEAT_MBYTE
10146 arg != NULL ? arg :
10147#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010148 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149 }
10150 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10151 }
10152
10153 /* Might not return to the main loop when in an event handler. */
10154 update_topline_cursor();
10155
10156 /* Restore the previous typeahead. */
10157 restore_typeahead(&tabuf);
10158
10159 --ex_normal_busy;
10160 msg_scroll = save_msg_scroll;
10161 restart_edit = save_restart_edit;
10162 p_im = save_insertmode;
10163 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010164 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
10166
10167 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +010010168 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +010010170#ifdef FEAT_MOUSE
10171 setmouse();
10172#endif
10173#ifdef CURSOR_SHAPE
10174 ui_cursor_shape(); /* may show different cursor shape */
10175#endif
10176
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177#ifdef FEAT_MBYTE
10178 vim_free(arg);
10179#endif
10180}
10181
10182/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010183 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184 */
10185 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010186ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010188 if (eap->forceit)
10189 {
10190 coladvance((colnr_T)MAXCOL);
10191 curwin->w_curswant = MAXCOL;
10192 curwin->w_set_curswant = FALSE;
10193 }
10194
Bram Moolenaara40c5002005-01-09 21:16:21 +000010195 /* Ignore the command when already in Insert mode. Inserting an
10196 * expression register that invokes a function can do this. */
10197 if (State & INSERT)
10198 return;
10199
Bram Moolenaar64969662005-12-14 21:59:55 +000010200 if (eap->cmdidx == CMD_startinsert)
10201 restart_edit = 'a';
10202 else if (eap->cmdidx == CMD_startreplace)
10203 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010205 restart_edit = 'V';
10206
10207 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010209 if (eap->cmdidx == CMD_startinsert)
10210 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 curwin->w_curswant = 0; /* avoid MAXCOL */
10212 }
10213}
10214
10215/*
10216 * ":stopinsert"
10217 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010219ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220{
10221 restart_edit = 0;
10222 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010223 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010226/*
10227 * Execute normal mode command "cmd".
10228 * "remap" can be REMAP_NONE or REMAP_YES.
10229 */
10230 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010231exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010232{
Bram Moolenaar25281632016-01-21 23:32:32 +010010233 /* Stuff the argument into the typeahead buffer. */
10234 ins_typebuf(cmd, remap, 0, TRUE, silent);
10235 exec_normal(FALSE);
10236}
Bram Moolenaar25281632016-01-21 23:32:32 +010010237
Bram Moolenaar25281632016-01-21 23:32:32 +010010238/*
10239 * Execute normal_cmd() until there is no typeahead left.
10240 */
10241 void
10242exec_normal(int was_typed)
10243{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010244 oparg_T oa;
10245
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010246 clear_oparg(&oa);
10247 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010248 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10249 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010250 {
10251 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010252 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010253 }
10254}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010255
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256#ifdef FEAT_FIND_ID
10257 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010258ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259{
10260 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10261 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10262 (linenr_T)1, (linenr_T)MAXLNUM);
10263}
10264
10265#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
10266/*
10267 * ":psearch"
10268 */
10269 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010270ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271{
10272 g_do_tagpreview = p_pvh;
10273 ex_findpat(eap);
10274 g_do_tagpreview = 0;
10275}
10276#endif
10277
10278 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010279ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280{
10281 int whole = TRUE;
10282 long n;
10283 char_u *p;
10284 int action;
10285
10286 switch (cmdnames[eap->cmdidx].cmd_name[2])
10287 {
10288 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10289 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10290 action = ACTION_GOTO;
10291 else
10292 action = ACTION_SHOW;
10293 break;
10294 case 'i': /* ":ilist" and ":dlist" */
10295 action = ACTION_SHOW_ALL;
10296 break;
10297 case 'u': /* ":ijump" and ":djump" */
10298 action = ACTION_GOTO;
10299 break;
10300 default: /* ":isplit" and ":dsplit" */
10301 action = ACTION_SPLIT;
10302 break;
10303 }
10304
10305 n = 1;
10306 if (vim_isdigit(*eap->arg)) /* get count */
10307 {
10308 n = getdigits(&eap->arg);
10309 eap->arg = skipwhite(eap->arg);
10310 }
10311 if (*eap->arg == '/') /* Match regexp, not just whole words */
10312 {
10313 whole = FALSE;
10314 ++eap->arg;
10315 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10316 if (*p)
10317 {
10318 *p++ = NUL;
10319 p = skipwhite(p);
10320
10321 /* Check for trailing illegal characters */
10322 if (!ends_excmd(*p))
10323 eap->errmsg = e_trailing;
10324 else
10325 eap->nextcmd = check_nextcmd(p);
10326 }
10327 }
10328 if (!eap->skip)
10329 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10330 whole, !eap->forceit,
10331 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10332 n, action, eap->line1, eap->line2);
10333}
10334#endif
10335
10336#ifdef FEAT_WINDOWS
10337
10338# ifdef FEAT_QUICKFIX
10339/*
10340 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10341 */
10342 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010343ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010345 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10347}
10348
10349/*
10350 * ":pedit"
10351 */
10352 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010353ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354{
10355 win_T *curwin_save = curwin;
10356
10357 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010358 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 keep_help_flag = curwin_save->w_buffer->b_help;
10360 do_exedit(eap, NULL);
10361 keep_help_flag = FALSE;
10362 if (curwin != curwin_save && win_valid(curwin_save))
10363 {
10364 /* Return cursor to where we were */
10365 validate_cursor();
10366 redraw_later(VALID);
10367 win_enter(curwin_save, TRUE);
10368 }
10369 g_do_tagpreview = 0;
10370}
10371# endif
10372
10373/*
10374 * ":stag", ":stselect" and ":stjump".
10375 */
10376 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010377ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378{
10379 postponed_split = -1;
10380 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010381 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10383 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010384 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385}
10386#endif
10387
10388/*
10389 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10390 */
10391 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010392ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393{
10394 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10395}
10396
10397 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010398ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399{
10400 int cmd;
10401
10402 switch (name[1])
10403 {
10404 case 'j': cmd = DT_JUMP; /* ":tjump" */
10405 break;
10406 case 's': cmd = DT_SELECT; /* ":tselect" */
10407 break;
10408 case 'p': cmd = DT_PREV; /* ":tprevious" */
10409 break;
10410 case 'N': cmd = DT_PREV; /* ":tNext" */
10411 break;
10412 case 'n': cmd = DT_NEXT; /* ":tnext" */
10413 break;
10414 case 'o': cmd = DT_POP; /* ":pop" */
10415 break;
10416 case 'f': /* ":tfirst" */
10417 case 'r': cmd = DT_FIRST; /* ":trewind" */
10418 break;
10419 case 'l': cmd = DT_LAST; /* ":tlast" */
10420 break;
10421 default: /* ":tag" */
10422#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010423 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010425 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426 return;
10427 }
10428#endif
10429 cmd = DT_TAG;
10430 break;
10431 }
10432
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010433 if (name[0] == 'l')
10434 {
10435#ifndef FEAT_QUICKFIX
10436 ex_ni(eap);
10437 return;
10438#else
10439 cmd = DT_LTAG;
10440#endif
10441 }
10442
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10444 eap->forceit, TRUE);
10445}
10446
10447/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010448 * Check "str" for starting with a special cmdline variable.
10449 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10450 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10451 */
10452 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010453find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010454{
10455 int len;
10456 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010457 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010458 "%",
10459#define SPEC_PERC 0
10460 "#",
10461#define SPEC_HASH 1
10462 "<cword>", /* cursor word */
10463#define SPEC_CWORD 2
10464 "<cWORD>", /* cursor WORD */
10465#define SPEC_CCWORD 3
10466 "<cfile>", /* cursor path name */
10467#define SPEC_CFILE 4
10468 "<sfile>", /* ":so" file name */
10469#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010470 "<slnum>", /* ":so" file line number */
10471#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010472#ifdef FEAT_AUTOCMD
10473 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010474# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010475 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010476# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010477 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010478# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010479#endif
10480#ifdef FEAT_CLIENTSERVER
10481 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010482# ifdef FEAT_AUTOCMD
10483# define SPEC_CLIENT 10
10484# else
10485# define SPEC_CLIENT 7
10486# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010487#endif
10488 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010489
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010490 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010491 {
10492 len = (int)STRLEN(spec_str[i]);
10493 if (STRNCMP(src, spec_str[i], len) == 0)
10494 {
10495 *usedlen = len;
10496 return i;
10497 }
10498 }
10499 return -1;
10500}
10501
10502/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503 * Evaluate cmdline variables.
10504 *
10505 * change '%' to curbuf->b_ffname
10506 * '#' to curwin->w_altfile
10507 * '<cword>' to word under the cursor
10508 * '<cWORD>' to WORD under the cursor
10509 * '<cfile>' to path name under the cursor
10510 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010511 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512 * '<afile>' to file name for autocommand
10513 * '<abuf>' to buffer number for autocommand
10514 * '<amatch>' to matching name for autocommand
10515 *
10516 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10517 * "" for error without a message) and NULL is returned.
10518 * Returns an allocated string if a valid match was found.
10519 * Returns NULL if no match was found. "usedlen" then still contains the
10520 * number of characters to skip.
10521 */
10522 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010523eval_vars(
10524 char_u *src, /* pointer into commandline */
10525 char_u *srcstart, /* beginning of valid memory for src */
10526 int *usedlen, /* characters after src that are used */
10527 linenr_T *lnump, /* line number for :e command, or NULL */
10528 char_u **errormsg, /* pointer to error message */
10529 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010530 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531{
10532 int i;
10533 char_u *s;
10534 char_u *result;
10535 char_u *resultbuf = NULL;
10536 int resultlen;
10537 buf_T *buf;
10538 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10539 int spec_idx;
10540#ifdef FEAT_MODIFY_FNAME
10541 int skip_mod = FALSE;
10542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544
10545 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010546 if (escaped != NULL)
10547 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548
10549 /*
10550 * Check if there is something to do.
10551 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010552 spec_idx = find_cmdline_var(src, usedlen);
10553 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554 {
10555 *usedlen = 1;
10556 return NULL;
10557 }
10558
10559 /*
10560 * Skip when preceded with a backslash "\%" and "\#".
10561 * Note: In "\\%" the % is also not recognized!
10562 */
10563 if (src > srcstart && src[-1] == '\\')
10564 {
10565 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010566 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567 return NULL;
10568 }
10569
10570 /*
10571 * word or WORD under cursor
10572 */
10573 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10574 {
10575 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10576 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10577 if (resultlen == 0)
10578 {
10579 *errormsg = (char_u *)"";
10580 return NULL;
10581 }
10582 }
10583
10584 /*
10585 * '#': Alternate file name
10586 * '%': Current file name
10587 * File name under the cursor
10588 * File name for autocommand
10589 * and following modifiers
10590 */
10591 else
10592 {
10593 switch (spec_idx)
10594 {
10595 case SPEC_PERC: /* '%': current file */
10596 if (curbuf->b_fname == NULL)
10597 {
10598 result = (char_u *)"";
10599 valid = 0; /* Must have ":p:h" to be valid */
10600 }
10601 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603 break;
10604
10605 case SPEC_HASH: /* '#' or "#99": alternate file */
10606 if (src[1] == '#') /* "##": the argument list */
10607 {
10608 result = arg_all();
10609 resultbuf = result;
10610 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010611 if (escaped != NULL)
10612 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613#ifdef FEAT_MODIFY_FNAME
10614 skip_mod = TRUE;
10615#endif
10616 break;
10617 }
10618 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010619 if (*s == '<') /* "#<99" uses v:oldfiles */
10620 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621 i = (int)getdigits(&s);
10622 *usedlen = (int)(s - src); /* length of what we expand */
10623
Bram Moolenaard812df62008-11-09 12:46:09 +000010624 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010626 if (*usedlen < 2)
10627 {
10628 /* Should we give an error message for #<text? */
10629 *usedlen = 1;
10630 return NULL;
10631 }
10632#ifdef FEAT_EVAL
10633 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10634 (long)i);
10635 if (result == NULL)
10636 {
10637 *errormsg = (char_u *)"";
10638 return NULL;
10639 }
10640#else
10641 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644 }
10645 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010646 {
10647 buf = buflist_findnr(i);
10648 if (buf == NULL)
10649 {
10650 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10651 return NULL;
10652 }
10653 if (lnump != NULL)
10654 *lnump = ECMD_LAST;
10655 if (buf->b_fname == NULL)
10656 {
10657 result = (char_u *)"";
10658 valid = 0; /* Must have ":p:h" to be valid */
10659 }
10660 else
10661 result = buf->b_fname;
10662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663 break;
10664
10665#ifdef FEAT_SEARCHPATH
10666 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010667 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668 if (result == NULL)
10669 {
10670 *errormsg = (char_u *)"";
10671 return NULL;
10672 }
10673 resultbuf = result; /* remember allocated string */
10674 break;
10675#endif
10676
10677#ifdef FEAT_AUTOCMD
10678 case SPEC_AFILE: /* file name for autocommand */
10679 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010680 if (result != NULL && !autocmd_fname_full)
10681 {
10682 /* Still need to turn the fname into a full path. It is
10683 * postponed to avoid a delay when <afile> is not used. */
10684 autocmd_fname_full = TRUE;
10685 result = FullName_save(autocmd_fname, FALSE);
10686 vim_free(autocmd_fname);
10687 autocmd_fname = result;
10688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689 if (result == NULL)
10690 {
10691 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10692 return NULL;
10693 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010694 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695 break;
10696
10697 case SPEC_ABUF: /* buffer number for autocommand */
10698 if (autocmd_bufnr <= 0)
10699 {
10700 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10701 return NULL;
10702 }
10703 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10704 result = strbuf;
10705 break;
10706
10707 case SPEC_AMATCH: /* match name for autocommand */
10708 result = autocmd_match;
10709 if (result == NULL)
10710 {
10711 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10712 return NULL;
10713 }
10714 break;
10715
10716#endif
10717 case SPEC_SFILE: /* file name for ":so" command */
10718 result = sourcing_name;
10719 if (result == NULL)
10720 {
10721 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10722 return NULL;
10723 }
10724 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010725 case SPEC_SLNUM: /* line in file for ":so" command */
10726 if (sourcing_name == NULL || sourcing_lnum == 0)
10727 {
10728 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10729 return NULL;
10730 }
10731 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10732 result = strbuf;
10733 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734#if defined(FEAT_CLIENTSERVER)
10735 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010736 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10737 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738 result = strbuf;
10739 break;
10740#endif
10741 }
10742
10743 resultlen = (int)STRLEN(result); /* length of new string */
10744 if (src[*usedlen] == '<') /* remove the file name extension */
10745 {
10746 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748 resultlen = (int)(s - result);
10749 }
10750#ifdef FEAT_MODIFY_FNAME
10751 else if (!skip_mod)
10752 {
10753 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10754 &resultlen);
10755 if (result == NULL)
10756 {
10757 *errormsg = (char_u *)"";
10758 return NULL;
10759 }
10760 }
10761#endif
10762 }
10763
10764 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10765 {
10766 if (valid != VALID_HEAD + VALID_PATH)
10767 /* xgettext:no-c-format */
10768 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10769 else
10770 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10771 result = NULL;
10772 }
10773 else
10774 result = vim_strnsave(result, resultlen);
10775 vim_free(resultbuf);
10776 return result;
10777}
10778
10779/*
10780 * Concatenate all files in the argument list, separated by spaces, and return
10781 * it in one allocated string.
10782 * Spaces and backslashes in the file names are escaped with a backslash.
10783 * Returns NULL when out of memory.
10784 */
10785 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010786arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787{
10788 int len;
10789 int idx;
10790 char_u *retval = NULL;
10791 char_u *p;
10792
10793 /*
10794 * Do this loop two times:
10795 * first time: compute the total length
10796 * second time: concatenate the names
10797 */
10798 for (;;)
10799 {
10800 len = 0;
10801 for (idx = 0; idx < ARGCOUNT; ++idx)
10802 {
10803 p = alist_name(&ARGLIST[idx]);
10804 if (p != NULL)
10805 {
10806 if (len > 0)
10807 {
10808 /* insert a space in between names */
10809 if (retval != NULL)
10810 retval[len] = ' ';
10811 ++len;
10812 }
10813 for ( ; *p != NUL; ++p)
10814 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010815 if (*p == ' '
10816#ifndef BACKSLASH_IN_FILENAME
10817 || *p == '\\'
10818#endif
10819 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 {
10821 /* insert a backslash */
10822 if (retval != NULL)
10823 retval[len] = '\\';
10824 ++len;
10825 }
10826 if (retval != NULL)
10827 retval[len] = *p;
10828 ++len;
10829 }
10830 }
10831 }
10832
10833 /* second time: break here */
10834 if (retval != NULL)
10835 {
10836 retval[len] = NUL;
10837 break;
10838 }
10839
10840 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010841 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842 if (retval == NULL)
10843 break;
10844 }
10845
10846 return retval;
10847}
10848
10849#if defined(FEAT_AUTOCMD) || defined(PROTO)
10850/*
10851 * Expand the <sfile> string in "arg".
10852 *
10853 * Returns an allocated string, or NULL for any error.
10854 */
10855 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010856expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857{
10858 char_u *errormsg;
10859 int len;
10860 char_u *result;
10861 char_u *newres;
10862 char_u *repl;
10863 int srclen;
10864 char_u *p;
10865
10866 result = vim_strsave(arg);
10867 if (result == NULL)
10868 return NULL;
10869
10870 for (p = result; *p; )
10871 {
10872 if (STRNCMP(p, "<sfile>", 7) != 0)
10873 ++p;
10874 else
10875 {
10876 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010877 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878 if (errormsg != NULL)
10879 {
10880 if (*errormsg)
10881 emsg(errormsg);
10882 vim_free(result);
10883 return NULL;
10884 }
10885 if (repl == NULL) /* no match (cannot happen) */
10886 {
10887 p += srclen;
10888 continue;
10889 }
10890 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10891 newres = alloc(len);
10892 if (newres == NULL)
10893 {
10894 vim_free(repl);
10895 vim_free(result);
10896 return NULL;
10897 }
10898 mch_memmove(newres, result, (size_t)(p - result));
10899 STRCPY(newres + (p - result), repl);
10900 len = (int)STRLEN(newres);
10901 STRCAT(newres, p + srclen);
10902 vim_free(repl);
10903 vim_free(result);
10904 result = newres;
10905 p = newres + len; /* continue after the match */
10906 }
10907 }
10908
10909 return result;
10910}
10911#endif
10912
10913#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010010914static int ses_winsizes(FILE *fd, int restore_size,
10915 win_T *tab_firstwin);
10916static int ses_win_rec(FILE *fd, frame_T *fr);
10917static frame_T *ses_skipframe(frame_T *fr);
10918static int ses_do_frame(frame_T *fr);
10919static int ses_do_win(win_T *wp);
10920static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
10921static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
10922static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923
10924/*
10925 * Write openfile commands for the current buffers to an .exrc file.
10926 * Return FAIL on error, OK otherwise.
10927 */
10928 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010929makeopens(
10930 FILE *fd,
10931 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932{
10933 buf_T *buf;
10934 int only_save_windows = TRUE;
10935 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936 int restore_size = TRUE;
10937 win_T *wp;
10938 char_u *sname;
10939 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010940 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010941 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010942 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010943 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010944 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010945 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946
10947 if (ssop_flags & SSOP_BUFFERS)
10948 only_save_windows = FALSE; /* Save ALL buffers */
10949
10950 /*
10951 * Begin by setting the this_session variable, and then other
10952 * sessionable variables.
10953 */
10954#ifdef FEAT_EVAL
10955 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10956 return FAIL;
10957 if (ssop_flags & SSOP_GLOBALS)
10958 if (store_session_globals(fd) == FAIL)
10959 return FAIL;
10960#endif
10961
10962 /*
10963 * Close all windows but one.
10964 */
10965 if (put_line(fd, "silent only") == FAIL)
10966 return FAIL;
10967
10968 /*
10969 * Now a :cd command to the session directory or the current directory
10970 */
10971 if (ssop_flags & SSOP_SESDIR)
10972 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010973 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10974 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975 return FAIL;
10976 }
10977 else if (ssop_flags & SSOP_CURDIR)
10978 {
10979 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10980 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010981 || fputs("cd ", fd) < 0
10982 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10983 || put_eol(fd) == FAIL)
10984 {
10985 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988 vim_free(sname);
10989 }
10990
10991 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010992 * If there is an empty, unnamed buffer we will wipe it out later.
10993 * Remember the buffer number.
10994 */
10995 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10996 return FAIL;
10997 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10998 return FAIL;
10999 if (put_line(fd, "endif") == FAIL)
11000 return FAIL;
11001
11002 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003 * Now save the current files, current buffer first.
11004 */
11005 if (put_line(fd, "set shortmess=aoO") == FAIL)
11006 return FAIL;
11007
11008 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011009 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010 {
11011 if (!(only_save_windows && buf->b_nwindows == 0)
11012 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
11013 && buf->b_fname != NULL
11014 && buf->b_p_bl)
11015 {
11016 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11017 : buf->b_wininfo->wi_fpos.lnum) < 0
11018 || ses_fname(fd, buf, &ssop_flags) == FAIL)
11019 return FAIL;
11020 }
11021 }
11022
11023 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011024 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11026 return FAIL;
11027
11028 if (ssop_flags & SSOP_RESIZE)
11029 {
11030 /* Note: after the restore we still check it worked!*/
11031 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11032 || put_eol(fd) == FAIL)
11033 return FAIL;
11034 }
11035
11036#ifdef FEAT_GUI
11037 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11038 {
11039 int x, y;
11040
11041 if (gui_mch_get_winpos(&x, &y) == OK)
11042 {
11043 /* Note: after the restore we still check it worked!*/
11044 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11045 return FAIL;
11046 }
11047 }
11048#endif
11049
11050 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011051 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11052 * will be displayed when creating the next tab. That resizes the windows
11053 * in the first tab, which may cause problems. Set 'showtabline' to 2
11054 * temporarily to avoid that.
11055 */
11056 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11057 {
11058 if (put_line(fd, "set stal=2") == FAIL)
11059 return FAIL;
11060 restore_stal = TRUE;
11061 }
11062
11063 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011064 * May repeat putting Windows for each tab, when "tabpages" is in
11065 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011066 * Don't use goto_tabpage(), it may change directory and trigger
11067 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011069 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011070 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011071 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011073 int need_tabnew = FALSE;
11074 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011075
Bram Moolenaar18144c82006-04-12 21:52:12 +000011076 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011078 tabpage_T *tp = find_tabpage(tabnr);
11079
11080 if (tp == NULL)
11081 break; /* done all tab pages */
11082 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011083 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011084 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011085 tab_topframe = topframe;
11086 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011087 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011088 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011089 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011090 tab_topframe = tp->tp_topframe;
11091 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011092 if (tabnr > 1)
11093 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095
Bram Moolenaar18144c82006-04-12 21:52:12 +000011096 /*
11097 * Before creating the window layout, try loading one file. If this
11098 * is aborted we don't end up with a number of useless windows.
11099 * This may have side effects! (e.g., compressed or network file).
11100 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011101 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011102 {
11103 if (ses_do_win(wp)
11104 && wp->w_buffer->b_ffname != NULL
11105 && !wp->w_buffer->b_help
11106#ifdef FEAT_QUICKFIX
11107 && !bt_nofile(wp->w_buffer)
11108#endif
11109 )
11110 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011111 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000011112 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
11113 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011114 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011115 if (!wp->w_arg_idx_invalid)
11116 edited_win = wp;
11117 break;
11118 }
11119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011121 /* If no file got edited create an empty tab page. */
11122 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11123 return FAIL;
11124
Bram Moolenaar18144c82006-04-12 21:52:12 +000011125 /*
11126 * Save current window layout.
11127 */
11128 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011130 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011132 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11133 return FAIL;
11134 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11135 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136
Bram Moolenaar18144c82006-04-12 21:52:12 +000011137 /*
11138 * Check if window sizes can be restored (no windows omitted).
11139 * Remember the window number of the current window after restoring.
11140 */
11141 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011142 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011143 {
11144 if (ses_do_win(wp))
11145 ++nr;
11146 else
11147 restore_size = FALSE;
11148 if (curwin == wp)
11149 cnr = nr;
11150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151
Bram Moolenaar18144c82006-04-12 21:52:12 +000011152 /* Go to the first window. */
11153 if (put_line(fd, "wincmd t") == FAIL)
11154 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011155
Bram Moolenaar18144c82006-04-12 21:52:12 +000011156 /*
11157 * If more than one window, see if sizes can be restored.
11158 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11159 * resized when moving between windows.
11160 * Do this before restoring the view, so that the topline and the
11161 * cursor can be set. This is done again below.
11162 */
11163 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
11164 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011165 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011166 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167
Bram Moolenaar18144c82006-04-12 21:52:12 +000011168 /*
11169 * Restore the view of the window (options, file, cursor, etc.).
11170 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011171 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011172 {
11173 if (!ses_do_win(wp))
11174 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011175 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11176 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011177 return FAIL;
11178 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11179 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011180 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011181 }
11182
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011183 /* The argument index in the first tab page is zero, need to set it in
11184 * each window. For further tab pages it's the window where we do
11185 * "tabedit". */
11186 cur_arg_idx = next_arg_idx;
11187
Bram Moolenaar18144c82006-04-12 21:52:12 +000011188 /*
11189 * Restore cursor to the current window if it's not the first one.
11190 */
11191 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11192 || put_eol(fd) == FAIL))
11193 return FAIL;
11194
11195 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011196 * Restore window sizes again after jumping around in windows, because
11197 * the current window has a minimum size while others may not.
11198 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011199 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011200 return FAIL;
11201
Bram Moolenaar18144c82006-04-12 21:52:12 +000011202 /* Don't continue in another tab page when doing only the current one
11203 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011204 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011205 break;
11206 }
11207
11208 if (ssop_flags & SSOP_TABPAGES)
11209 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011210 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11211 || put_eol(fd) == FAIL)
11212 return FAIL;
11213 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011214 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11215 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011216
Bram Moolenaar9c102382006-05-03 21:26:49 +000011217 /*
11218 * Wipe out an empty unnamed buffer we started in.
11219 */
11220 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11221 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011222 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011223 return FAIL;
11224 if (put_line(fd, "endif") == FAIL)
11225 return FAIL;
11226 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11227 return FAIL;
11228
11229 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11230 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11231 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11232 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233
11234 /*
11235 * Lastly, execute the x.vim file if it exists.
11236 */
11237 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11238 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011239 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240 || put_line(fd, "endif") == FAIL)
11241 return FAIL;
11242
11243 return OK;
11244}
11245
11246 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011247ses_winsizes(
11248 FILE *fd,
11249 int restore_size,
11250 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251{
11252 int n = 0;
11253 win_T *wp;
11254
11255 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11256 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011257 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258 {
11259 if (!ses_do_win(wp))
11260 continue;
11261 ++n;
11262
11263 /* restore height when not full height */
11264 if (wp->w_height + wp->w_status_height < topframe->fr_height
11265 && (fprintf(fd,
11266 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11267 n, (long)wp->w_height, Rows / 2, Rows) < 0
11268 || put_eol(fd) == FAIL))
11269 return FAIL;
11270
11271 /* restore width when not full width */
11272 if (wp->w_width < Columns && (fprintf(fd,
11273 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11274 n, (long)wp->w_width, Columns / 2, Columns) < 0
11275 || put_eol(fd) == FAIL))
11276 return FAIL;
11277 }
11278 }
11279 else
11280 {
11281 /* Just equalise window sizes */
11282 if (put_line(fd, "wincmd =") == FAIL)
11283 return FAIL;
11284 }
11285 return OK;
11286}
11287
11288/*
11289 * Write commands to "fd" to recursively create windows for frame "fr",
11290 * horizontally and vertically split.
11291 * After the commands the last window in the frame is the current window.
11292 * Returns FAIL when writing the commands to "fd" fails.
11293 */
11294 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011295ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296{
11297 frame_T *frc;
11298 int count = 0;
11299
11300 if (fr->fr_layout != FR_LEAF)
11301 {
11302 /* Find first frame that's not skipped and then create a window for
11303 * each following one (first frame is already there). */
11304 frc = ses_skipframe(fr->fr_child);
11305 if (frc != NULL)
11306 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11307 {
11308 /* Make window as big as possible so that we have lots of room
11309 * to split. */
11310 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11311 || put_line(fd, fr->fr_layout == FR_COL
11312 ? "split" : "vsplit") == FAIL)
11313 return FAIL;
11314 ++count;
11315 }
11316
11317 /* Go back to the first window. */
11318 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11319 ? "%dwincmd k" : "%dwincmd h", count) < 0
11320 || put_eol(fd) == FAIL))
11321 return FAIL;
11322
11323 /* Recursively create frames/windows in each window of this column or
11324 * row. */
11325 frc = ses_skipframe(fr->fr_child);
11326 while (frc != NULL)
11327 {
11328 ses_win_rec(fd, frc);
11329 frc = ses_skipframe(frc->fr_next);
11330 /* Go to next window. */
11331 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11332 return FAIL;
11333 }
11334 }
11335 return OK;
11336}
11337
11338/*
11339 * Skip frames that don't contain windows we want to save in the Session.
11340 * Returns NULL when there none.
11341 */
11342 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011343ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344{
11345 frame_T *frc;
11346
11347 for (frc = fr; frc != NULL; frc = frc->fr_next)
11348 if (ses_do_frame(frc))
11349 break;
11350 return frc;
11351}
11352
11353/*
11354 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11355 * the Session.
11356 */
11357 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011358ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359{
11360 frame_T *frc;
11361
11362 if (fr->fr_layout == FR_LEAF)
11363 return ses_do_win(fr->fr_win);
11364 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11365 if (ses_do_frame(frc))
11366 return TRUE;
11367 return FALSE;
11368}
11369
11370/*
11371 * Return non-zero if window "wp" is to be stored in the Session.
11372 */
11373 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011374ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011375{
11376 if (wp->w_buffer->b_fname == NULL
11377#ifdef FEAT_QUICKFIX
11378 /* When 'buftype' is "nofile" can't restore the window contents. */
11379 || bt_nofile(wp->w_buffer)
11380#endif
11381 )
11382 return (ssop_flags & SSOP_BLANK);
11383 if (wp->w_buffer->b_help)
11384 return (ssop_flags & SSOP_HELP);
11385 return TRUE;
11386}
11387
11388/*
11389 * Write commands to "fd" to restore the view of a window.
11390 * Caller must make sure 'scrolloff' is zero.
11391 */
11392 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011393put_view(
11394 FILE *fd,
11395 win_T *wp,
11396 int add_edit, /* add ":edit" command to view */
11397 unsigned *flagp, /* vop_flags or ssop_flags */
11398 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011399 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011400{
11401 win_T *save_curwin;
11402 int f;
11403 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011404 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011405
11406 /* Always restore cursor position for ":mksession". For ":mkview" only
11407 * when 'viewoptions' contains "cursor". */
11408 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11409
11410 /*
11411 * Local argument list.
11412 */
11413 if (wp->w_alist == &global_alist)
11414 {
11415 if (put_line(fd, "argglobal") == FAIL)
11416 return FAIL;
11417 }
11418 else
11419 {
11420 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11421 flagp == &vop_flags
11422 || !(*flagp & SSOP_CURDIR)
11423 || wp->w_localdir != NULL, flagp) == FAIL)
11424 return FAIL;
11425 }
11426
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011427 /* Only when part of a session: restore the argument index. Some
11428 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011429 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011430 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011431 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011432 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433 || put_eol(fd) == FAIL)
11434 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011435 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011436 }
11437
11438 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011439 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011440 {
11441 /*
11442 * Load the file.
11443 */
11444 if (wp->w_buffer->b_ffname != NULL
11445#ifdef FEAT_QUICKFIX
11446 && !bt_nofile(wp->w_buffer)
11447#endif
11448 )
11449 {
11450 /*
11451 * Editing a file in this buffer: use ":edit file".
11452 * This may have side effects! (e.g., compressed or network file).
11453 */
11454 if (fputs("edit ", fd) < 0
11455 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11456 return FAIL;
11457 }
11458 else
11459 {
11460 /* No file in this buffer, just make it empty. */
11461 if (put_line(fd, "enew") == FAIL)
11462 return FAIL;
11463#ifdef FEAT_QUICKFIX
11464 if (wp->w_buffer->b_ffname != NULL)
11465 {
11466 /* The buffer does have a name, but it's not a file name. */
11467 if (fputs("file ", fd) < 0
11468 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11469 return FAIL;
11470 }
11471#endif
11472 do_cursor = FALSE;
11473 }
11474 }
11475
11476 /*
11477 * Local mappings and abbreviations.
11478 */
11479 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11480 && makemap(fd, wp->w_buffer) == FAIL)
11481 return FAIL;
11482
11483 /*
11484 * Local options. Need to go to the window temporarily.
11485 * Store only local values when using ":mkview" and when ":mksession" is
11486 * used and 'sessionoptions' doesn't include "options".
11487 * Some folding options are always stored when "folds" is included,
11488 * otherwise the folds would not be restored correctly.
11489 */
11490 save_curwin = curwin;
11491 curwin = wp;
11492 curbuf = curwin->w_buffer;
11493 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11494 f = makeset(fd, OPT_LOCAL,
11495 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11496#ifdef FEAT_FOLDING
11497 else if (*flagp & SSOP_FOLDS)
11498 f = makefoldset(fd);
11499#endif
11500 else
11501 f = OK;
11502 curwin = save_curwin;
11503 curbuf = curwin->w_buffer;
11504 if (f == FAIL)
11505 return FAIL;
11506
11507#ifdef FEAT_FOLDING
11508 /*
11509 * Save Folds when 'buftype' is empty and for help files.
11510 */
11511 if ((*flagp & SSOP_FOLDS)
11512 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011513# ifdef FEAT_QUICKFIX
11514 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11515# endif
11516 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011517 {
11518 if (put_folds(fd, wp) == FAIL)
11519 return FAIL;
11520 }
11521#endif
11522
11523 /*
11524 * Set the cursor after creating folds, since that moves the cursor.
11525 */
11526 if (do_cursor)
11527 {
11528
11529 /* Restore the cursor line in the file and relatively in the
11530 * window. Don't use "G", it changes the jumplist. */
11531 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11532 (long)wp->w_cursor.lnum,
11533 (long)(wp->w_cursor.lnum - wp->w_topline),
11534 (long)wp->w_height / 2, (long)wp->w_height) < 0
11535 || put_eol(fd) == FAIL
11536 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11537 || put_line(fd, "exe s:l") == FAIL
11538 || put_line(fd, "normal! zt") == FAIL
11539 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11540 || put_eol(fd) == FAIL)
11541 return FAIL;
11542 /* Restore the cursor column and left offset when not wrapping. */
11543 if (wp->w_cursor.col == 0)
11544 {
11545 if (put_line(fd, "normal! 0") == FAIL)
11546 return FAIL;
11547 }
11548 else
11549 {
11550 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11551 {
11552 if (fprintf(fd,
11553 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011554 (long)wp->w_virtcol + 1,
11555 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011556 (long)wp->w_width / 2, (long)wp->w_width) < 0
11557 || put_eol(fd) == FAIL
11558 || put_line(fd, "if s:c > 0") == FAIL
11559 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011560 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11561 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011562 || put_eol(fd) == FAIL
11563 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011564 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011565 || put_eol(fd) == FAIL
11566 || put_line(fd, "endif") == FAIL)
11567 return FAIL;
11568 }
11569 else
11570 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011571 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011572 || put_eol(fd) == FAIL)
11573 return FAIL;
11574 }
11575 }
11576 }
11577
11578 /*
11579 * Local directory.
11580 */
11581 if (wp->w_localdir != NULL)
11582 {
11583 if (fputs("lcd ", fd) < 0
11584 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11585 || put_eol(fd) == FAIL)
11586 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011587 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588 }
11589
11590 return OK;
11591}
11592
11593/*
11594 * Write an argument list to the session file.
11595 * Returns FAIL if writing fails.
11596 */
11597 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011598ses_arglist(
11599 FILE *fd,
11600 char *cmd,
11601 garray_T *gap,
11602 int fullname, /* TRUE: use full path name */
11603 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604{
11605 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011606 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011607 char_u *s;
11608
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011609 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11610 return FAIL;
11611 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612 return FAIL;
11613 for (i = 0; i < gap->ga_len; ++i)
11614 {
11615 /* NULL file names are skipped (only happens when out of memory). */
11616 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11617 if (s != NULL)
11618 {
11619 if (fullname)
11620 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011621 buf = alloc(MAXPATHL);
11622 if (buf != NULL)
11623 {
11624 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11625 s = buf;
11626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011627 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011628 if (fputs("argadd ", fd) < 0
11629 || ses_put_fname(fd, s, flagp) == FAIL
11630 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011631 {
11632 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011633 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011634 }
11635 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011636 }
11637 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011638 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011639}
11640
11641/*
11642 * Write a buffer name to the session file.
11643 * Also ends the line.
11644 * Returns FAIL if writing fails.
11645 */
11646 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011647ses_fname(FILE *fd, buf_T *buf, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011648{
11649 char_u *name;
11650
11651 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011652 * the session file will be sourced.
11653 * Don't do this for ":mkview", we don't know the current directory.
11654 * Don't do this after ":lcd", we don't keep track of what the current
11655 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011656 if (buf->b_sfname != NULL
11657 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011658 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011659#ifdef FEAT_AUTOCHDIR
11660 && !p_acd
11661#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011662 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011663 name = buf->b_sfname;
11664 else
11665 name = buf->b_ffname;
11666 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11667 return FAIL;
11668 return OK;
11669}
11670
11671/*
11672 * Write a file name to the session file.
11673 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11674 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011675 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011676 */
11677 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011678ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011679{
11680 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011681 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011682 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011683
11684 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011685 if (sname == NULL)
11686 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011687
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011688 if (*flagp & SSOP_SLASH)
11689 {
11690 /* change all backslashes to forward slashes */
11691 for (p = sname; *p != NUL; mb_ptr_adv(p))
11692 if (*p == '\\')
11693 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011694 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011695
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011696 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011697 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011698 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011699 if (p == NULL)
11700 return FAIL;
11701
11702 /* write the result */
11703 if (fputs((char *)p, fd) < 0)
11704 retval = FAIL;
11705
11706 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011707 return retval;
11708}
11709
11710/*
11711 * ":loadview [nr]"
11712 */
11713 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011714ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011715{
11716 char_u *fname;
11717
11718 fname = get_view_file(*eap->arg);
11719 if (fname != NULL)
11720 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011721 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011722 vim_free(fname);
11723 }
11724}
11725
11726/*
11727 * Get the name of the view file for the current buffer.
11728 */
11729 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011730get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731{
11732 int len = 0;
11733 char_u *p, *s;
11734 char_u *retval;
11735 char_u *sname;
11736
11737 if (curbuf->b_ffname == NULL)
11738 {
11739 EMSG(_(e_noname));
11740 return NULL;
11741 }
11742 sname = home_replace_save(NULL, curbuf->b_ffname);
11743 if (sname == NULL)
11744 return NULL;
11745
11746 /*
11747 * We want a file name without separators, because we're not going to make
11748 * a directory.
11749 * "normal" path separator -> "=+"
11750 * "=" -> "=="
11751 * ":" path separator -> "=-"
11752 */
11753 for (p = sname; *p; ++p)
11754 if (*p == '=' || vim_ispathsep(*p))
11755 ++len;
11756 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11757 if (retval != NULL)
11758 {
11759 STRCPY(retval, p_vdir);
11760 add_pathsep(retval);
11761 s = retval + STRLEN(retval);
11762 for (p = sname; *p; ++p)
11763 {
11764 if (*p == '=')
11765 {
11766 *s++ = '=';
11767 *s++ = '=';
11768 }
11769 else if (vim_ispathsep(*p))
11770 {
11771 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011772#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011773 if (*p == ':')
11774 *s++ = '-';
11775 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011776#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011777 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011778 }
11779 else
11780 *s++ = *p;
11781 }
11782 *s++ = '=';
11783 *s++ = c;
11784 STRCPY(s, ".vim");
11785 }
11786
11787 vim_free(sname);
11788 return retval;
11789}
11790
11791#endif /* FEAT_SESSION */
11792
11793/*
11794 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11795 * Return FAIL for a write error.
11796 */
11797 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011798put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011799{
11800 if (
11801#ifdef USE_CRNL
11802 (
11803# ifdef MKSESSION_NL
11804 !mksession_nl &&
11805# endif
11806 (putc('\r', fd) < 0)) ||
11807#endif
11808 (putc('\n', fd) < 0))
11809 return FAIL;
11810 return OK;
11811}
11812
11813/*
11814 * Write a line to "fd".
11815 * Return FAIL for a write error.
11816 */
11817 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011818put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011819{
11820 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11821 return FAIL;
11822 return OK;
11823}
11824
11825#ifdef FEAT_VIMINFO
11826/*
11827 * ":rviminfo" and ":wviminfo".
11828 */
11829 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011830ex_viminfo(
11831 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011832{
11833 char_u *save_viminfo;
11834
11835 save_viminfo = p_viminfo;
11836 if (*p_viminfo == NUL)
11837 p_viminfo = (char_u *)"'100";
11838 if (eap->cmdidx == CMD_rviminfo)
11839 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011840 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11841 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842 EMSG(_("E195: Cannot open viminfo file for reading"));
11843 }
11844 else
11845 write_viminfo(eap->arg, eap->forceit);
11846 p_viminfo = save_viminfo;
11847}
11848#endif
11849
11850#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011851/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011852 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011853 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011854 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011856dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011857{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858 if (fname == NULL)
11859 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011860 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011861}
11862#endif
11863
11864/*
11865 * ":behave {mswin,xterm}"
11866 */
11867 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011868ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011869{
11870 if (STRCMP(eap->arg, "mswin") == 0)
11871 {
11872 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11873 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11874 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11875 set_option_value((char_u *)"keymodel", 0L,
11876 (char_u *)"startsel,stopsel", 0);
11877 }
11878 else if (STRCMP(eap->arg, "xterm") == 0)
11879 {
11880 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11881 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11882 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11883 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11884 }
11885 else
11886 EMSG2(_(e_invarg2), eap->arg);
11887}
11888
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011889#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11890/*
11891 * Function given to ExpandGeneric() to obtain the possible arguments of the
11892 * ":behave {mswin,xterm}" command.
11893 */
11894 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011895get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011896{
11897 if (idx == 0)
11898 return (char_u *)"mswin";
11899 if (idx == 1)
11900 return (char_u *)"xterm";
11901 return NULL;
11902}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020011903
11904/*
11905 * Function given to ExpandGeneric() to obtain the possible arguments of the
11906 * ":messages {clear}" command.
11907 */
11908 char_u *
11909get_messages_arg(expand_T *xp UNUSED, int idx)
11910{
11911 if (idx == 0)
11912 return (char_u *)"clear";
11913 return NULL;
11914}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011915#endif
11916
Bram Moolenaar071d4272004-06-13 20:20:40 +000011917#ifdef FEAT_AUTOCMD
11918static int filetype_detect = FALSE;
11919static int filetype_plugin = FALSE;
11920static int filetype_indent = FALSE;
11921
11922/*
11923 * ":filetype [plugin] [indent] {on,off,detect}"
11924 * on: Load the filetype.vim file to install autocommands for file types.
11925 * off: Load the ftoff.vim file to remove all autocommands for file types.
11926 * plugin on: load filetype.vim and ftplugin.vim
11927 * plugin off: load ftplugof.vim
11928 * indent on: load filetype.vim and indent.vim
11929 * indent off: load indoff.vim
11930 */
11931 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011932ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011933{
11934 char_u *arg = eap->arg;
11935 int plugin = FALSE;
11936 int indent = FALSE;
11937
11938 if (*eap->arg == NUL)
11939 {
11940 /* Print current status. */
11941 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11942 filetype_detect ? "ON" : "OFF",
11943 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11944 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11945 return;
11946 }
11947
11948 /* Accept "plugin" and "indent" in any order. */
11949 for (;;)
11950 {
11951 if (STRNCMP(arg, "plugin", 6) == 0)
11952 {
11953 plugin = TRUE;
11954 arg = skipwhite(arg + 6);
11955 continue;
11956 }
11957 if (STRNCMP(arg, "indent", 6) == 0)
11958 {
11959 indent = TRUE;
11960 arg = skipwhite(arg + 6);
11961 continue;
11962 }
11963 break;
11964 }
11965 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11966 {
11967 if (*arg == 'o' || !filetype_detect)
11968 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011969 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011970 filetype_detect = TRUE;
11971 if (plugin)
11972 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011973 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011974 filetype_plugin = TRUE;
11975 }
11976 if (indent)
11977 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011978 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979 filetype_indent = TRUE;
11980 }
11981 }
11982 if (*arg == 'd')
11983 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020011984 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011985 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011986 }
11987 }
11988 else if (STRCMP(arg, "off") == 0)
11989 {
11990 if (plugin || indent)
11991 {
11992 if (plugin)
11993 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011994 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011995 filetype_plugin = FALSE;
11996 }
11997 if (indent)
11998 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011999 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000 filetype_indent = FALSE;
12001 }
12002 }
12003 else
12004 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012005 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012006 filetype_detect = FALSE;
12007 }
12008 }
12009 else
12010 EMSG2(_(e_invarg2), arg);
12011}
12012
12013/*
12014 * ":setfiletype {name}"
12015 */
12016 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012017ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012018{
12019 if (!did_filetype)
12020 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
12021}
12022#endif
12023
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012025ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012026{
12027#ifdef FEAT_DIGRAPHS
12028 if (*eap->arg != NUL)
12029 putdigraph(eap->arg);
12030 else
12031 listdigraphs();
12032#else
12033 EMSG(_("E196: No digraphs in this version"));
12034#endif
12035}
12036
12037 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012038ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012039{
12040 int flags = 0;
12041
12042 if (eap->cmdidx == CMD_setlocal)
12043 flags = OPT_LOCAL;
12044 else if (eap->cmdidx == CMD_setglobal)
12045 flags = OPT_GLOBAL;
12046#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
12047 if (cmdmod.browse && flags == 0)
12048 ex_options(eap);
12049 else
12050#endif
12051 (void)do_set(eap->arg, flags);
12052}
12053
12054#ifdef FEAT_SEARCH_EXTRA
12055/*
12056 * ":nohlsearch"
12057 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012058 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012059ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012060{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010012061 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012062 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012063}
12064
12065/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012066 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067 * Sets nextcmd to the start of the next command, if any. Also called when
12068 * skipping commands to find the next command.
12069 */
12070 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012071ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012072{
12073 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012074 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012075 char_u *end;
12076 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012077 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012078
12079 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012080 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012081 else
12082 {
12083 EMSG(e_invcmd);
12084 return;
12085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086
12087 /* First clear any old pattern. */
12088 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012089 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012090
12091 if (ends_excmd(*eap->arg))
12092 end = eap->arg;
12093 else if ((STRNICMP(eap->arg, "none", 4) == 0
12094 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
12095 end = eap->arg + 4;
12096 else
12097 {
12098 p = skiptowhite(eap->arg);
12099 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012100 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101 p = skipwhite(p);
12102 if (*p == NUL)
12103 {
12104 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012105 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012106 EMSG2(_(e_invarg2), eap->arg);
12107 return;
12108 }
12109 end = skip_regexp(p + 1, *p, TRUE, NULL);
12110 if (!eap->skip)
12111 {
12112 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12113 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012114 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012115 eap->errmsg = e_trailing;
12116 return;
12117 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012118 if (*end != *p)
12119 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012120 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012121 EMSG2(_(e_invarg2), p);
12122 return;
12123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012124
12125 c = *end;
12126 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012127 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012128 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012129 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012130 }
12131 }
12132 eap->nextcmd = find_nextcmd(end);
12133}
12134#endif
12135
12136#ifdef FEAT_CRYPT
12137/*
12138 * ":X": Get crypt key
12139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012140 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012141ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012142{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012143 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012144 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012145}
12146#endif
12147
12148#ifdef FEAT_FOLDING
12149 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012150ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151{
12152 if (foldManualAllowed(TRUE))
12153 foldCreate(eap->line1, eap->line2);
12154}
12155
12156 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012157ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158{
12159 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12160 eap->forceit, FALSE);
12161}
12162
12163 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012164ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165{
12166 linenr_T lnum;
12167
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012168#ifdef FEAT_CLIPBOARD
12169 start_global_changes();
12170#endif
12171
Bram Moolenaar071d4272004-06-13 20:20:40 +000012172 /* First set the marks for all lines closed/open. */
12173 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12174 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12175 ml_setmarked(lnum);
12176
12177 /* Execute the command on the marked lines. */
12178 global_exe(eap->arg);
12179 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012180#ifdef FEAT_CLIPBOARD
12181 end_global_changes();
12182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012183}
12184#endif