blob: 28de56ab056551220b95830720d189cec26cbdd4 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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
246# define do_cscope ex_ni
247# define do_scscope ex_ni
248# define do_cstag ex_ni
249#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;
1391 default:
1392 p = vim_strsave((char_u *)_(e_internal));
1393 }
1394
1395 saved_sourcing_name = sourcing_name;
1396 saved_sourcing_lnum = sourcing_lnum;
1397 sourcing_name = current_exception->throw_name;
1398 sourcing_lnum = current_exception->throw_lnum;
1399 current_exception->throw_name = NULL;
1400
1401 discard_current_exception(); /* uses IObuff if 'verbose' */
1402 suppress_errthrow = TRUE;
1403 force_abort = TRUE;
1404
1405 if (messages != NULL)
1406 {
1407 do
1408 {
1409 next = messages->next;
1410 emsg(messages->msg);
1411 vim_free(messages->msg);
1412 vim_free(messages);
1413 messages = next;
1414 }
1415 while (messages != NULL);
1416 }
1417 else if (p != NULL)
1418 {
1419 emsg(p);
1420 vim_free(p);
1421 }
1422 vim_free(sourcing_name);
1423 sourcing_name = saved_sourcing_name;
1424 sourcing_lnum = saved_sourcing_lnum;
1425 }
1426
1427 /*
1428 * On an interrupt or an aborting error not converted to an exception,
1429 * disable the conversion of errors to exceptions. (Interrupts are not
1430 * converted any more, here.) This enables also the interrupt message
1431 * when force_abort is set and did_emsg unset in case of an interrupt
1432 * from a finally clause after an error.
1433 */
1434 else if (got_int || (did_emsg && force_abort))
1435 suppress_errthrow = TRUE;
1436 }
1437
1438 /*
1439 * The current cstack will be freed when do_cmdline() returns. An uncaught
1440 * exception will have to be rethrown in the previous cstack. If a function
1441 * has just returned or a script file was just finished and the previous
1442 * cstack belongs to the same function or, respectively, script file, it
1443 * will have to be checked for finally clauses to be executed due to the
1444 * ":return" or ":finish". This is done in do_one_cmd().
1445 */
1446 if (did_throw)
1447 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001448 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001450 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 && ex_nesting_level > func_level(real_cookie) + 1))
1452 {
1453 if (!did_throw)
1454 check_cstack = TRUE;
1455 }
1456 else
1457 {
1458 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001459 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 --ex_nesting_level;
1461 /*
1462 * Go to debug mode when returning from a function in which we are
1463 * single-stepping.
1464 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001465 if ((getline_equal(fgetline, cookie, getsourceline)
1466 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001468 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 ? (char_u *)_("End of sourced file")
1470 : (char_u *)_("End of function"));
1471 }
1472
1473 /*
1474 * Restore the exception environment (done after returning from the
1475 * debugger).
1476 */
1477 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001478 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479
1480 msg_list = saved_msg_list;
1481#endif /* FEAT_EVAL */
1482
1483 /*
1484 * If there was too much output to fit on the command line, ask the user to
1485 * hit return before redrawing the screen. With the ":global" command we do
1486 * this only once after the command is finished.
1487 */
1488 if (did_inc)
1489 {
1490 --RedrawingDisabled;
1491 --no_wait_return;
1492 msg_scroll = FALSE;
1493
1494 /*
1495 * When just finished an ":if"-":else" which was typed, no need to
1496 * wait for hit-return. Also for an error situation.
1497 */
1498 if (retval == FAIL
1499#ifdef FEAT_EVAL
1500 || (did_endif && KeyTyped && !did_emsg)
1501#endif
1502 )
1503 {
1504 need_wait_return = FALSE;
1505 msg_didany = FALSE; /* don't wait when restarting edit */
1506 }
1507 else if (need_wait_return)
1508 {
1509 /*
1510 * The msg_start() above clears msg_didout. The wait_return we do
1511 * here should not overwrite the command that may be shown before
1512 * doing that.
1513 */
1514 msg_didout |= msg_didout_before_start;
1515 wait_return(FALSE);
1516 }
1517 }
1518
Bram Moolenaar2a942252012-11-28 23:03:07 +01001519#ifdef FEAT_EVAL
1520 did_endif = FALSE; /* in case do_cmdline used recursively */
1521#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 /*
1523 * Reset if_level, in case a sourced script file contains more ":if" than
1524 * ":endif" (could be ":if x | foo | endif").
1525 */
1526 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001527#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001528
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 --call_depth;
1530 return retval;
1531}
1532
1533#ifdef FEAT_EVAL
1534/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001535 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 */
1537 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001538get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001540 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 wcmd_T *wp;
1542 char_u *line;
1543
1544 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1545 {
1546 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001547 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001549 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 if (cp->getline == NULL)
1551 line = getcmdline(c, 0L, indent);
1552 else
1553 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001554 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 ++cp->current_line;
1556
1557 return line;
1558 }
1559
1560 KeyTyped = FALSE;
1561 ++cp->current_line;
1562 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1563 sourcing_lnum = wp->lnum;
1564 return vim_strsave(wp->line);
1565}
1566
1567/*
1568 * Store a line in "gap" so that a ":while" loop can execute it again.
1569 */
1570 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001571store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572{
1573 if (ga_grow(gap, 1) == FAIL)
1574 return FAIL;
1575 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1576 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1577 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 return OK;
1579}
1580
1581/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001582 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 */
1584 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001585free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586{
1587 while (gap->ga_len > 0)
1588 {
1589 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1590 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 }
1592}
1593#endif
1594
1595/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001596 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1597 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001600getline_equal(
1601 char_u *(*fgetline)(int, void *, int),
1602 void *cookie UNUSED, /* argument for fgetline() */
1603 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604{
1605#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001606 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001607 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608
Bram Moolenaar89d40322006-08-29 15:30:07 +00001609 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001610 * function that's originally used to obtain the lines. This may be
1611 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001612 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001613 cp = (struct loop_cookie *)cookie;
1614 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 {
1616 gp = cp->getline;
1617 cp = cp->cookie;
1618 }
1619 return gp == func;
1620#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001621 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622#endif
1623}
1624
1625#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1626/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001627 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 * getline function. Otherwise return "cookie".
1629 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001631getline_cookie(
1632 char_u *(*fgetline)(int, void *, int) UNUSED,
1633 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634{
1635# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001636 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001637 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638
Bram Moolenaar89d40322006-08-29 15:30:07 +00001639 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001640 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001642 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001643 cp = (struct loop_cookie *)cookie;
1644 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 {
1646 gp = cp->getline;
1647 cp = cp->cookie;
1648 }
1649 return cp;
1650# else
1651 return cookie;
1652# endif
1653}
1654#endif
1655
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001656
1657/*
1658 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1659 * ":bwipeout", etc.
1660 * Returns the buffer number.
1661 */
1662 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001663compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001664{
1665 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001666 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001667 int count = offset;
1668
1669 buf = firstbuf;
1670 while (buf->b_next != NULL && buf->b_fnum < lnum)
1671 buf = buf->b_next;
1672 while (count != 0)
1673 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001674 count += (offset < 0) ? 1 : -1;
1675 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1676 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001677 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001678 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001679 if (addr_type == ADDR_LOADED_BUFFERS)
1680 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001681 while (buf->b_ml.ml_mfp == NULL)
1682 {
1683 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1684 if (nextbuf == NULL)
1685 break;
1686 buf = nextbuf;
1687 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001688 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001689 /* we might have gone too far, last buffer is not loadedd */
1690 if (addr_type == ADDR_LOADED_BUFFERS)
1691 while (buf->b_ml.ml_mfp == NULL)
1692 {
1693 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1694 if (nextbuf == NULL)
1695 break;
1696 buf = nextbuf;
1697 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001698 return buf->b_fnum;
1699}
1700
Bram Moolenaarf240e182014-11-27 18:33:02 +01001701#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001702static int current_win_nr(win_T *win);
1703static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001704
1705 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001706current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001707{
1708 win_T *wp;
1709 int nr = 0;
1710
Bram Moolenaar29323592016-07-24 22:04:11 +02001711 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001712 {
1713 ++nr;
1714 if (wp == win)
1715 break;
1716 }
1717 return nr;
1718}
1719
1720 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001721current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001722{
1723 tabpage_T *tp;
1724 int nr = 0;
1725
Bram Moolenaar29323592016-07-24 22:04:11 +02001726 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001727 {
1728 ++nr;
1729 if (tp == tab)
1730 break;
1731 }
1732 return nr;
1733}
1734
1735# define CURRENT_WIN_NR current_win_nr(curwin)
1736# define LAST_WIN_NR current_win_nr(NULL)
1737# define CURRENT_TAB_NR current_tab_nr(curtab)
1738# define LAST_TAB_NR current_tab_nr(NULL)
1739#else
1740# define CURRENT_WIN_NR 1
1741# define LAST_WIN_NR 1
1742# define CURRENT_TAB_NR 1
1743# define LAST_TAB_NR 1
1744#endif
1745
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001746
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747/*
1748 * Execute one Ex command.
1749 *
1750 * If 'sourcing' is TRUE, the command will be included in the error message.
1751 *
1752 * 1. skip comment lines and leading space
1753 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001754 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001755 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001756 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001757 * 6. parse arguments
1758 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001760 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 *
1762 * This function may be called recursively!
1763 */
1764#if (_MSC_VER == 1200)
1765/*
Bram Moolenaared203462004-06-16 11:19:22 +00001766 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001768 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769#endif
1770 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001771do_one_cmd(
1772 char_u **cmdlinep,
1773 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001775 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001777 char_u *(*fgetline)(int, void *, int),
1778 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779{
1780 char_u *p;
1781 linenr_T lnum;
1782 long n;
1783 char_u *errormsg = NULL; /* error message */
1784 exarg_T ea; /* Ex command arguments */
1785 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001786 int save_msg_scroll = msg_scroll;
1787 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001789#ifdef HAVE_SANDBOX
1790 int did_sandbox = FALSE;
1791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 cmdmod_T save_cmdmod;
1793 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001794 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795
1796 vim_memset(&ea, 0, sizeof(ea));
1797 ea.line1 = 1;
1798 ea.line2 = 1;
1799#ifdef FEAT_EVAL
1800 ++ex_nesting_level;
1801#endif
1802
Bram Moolenaar21691f82012-12-05 19:13:18 +01001803 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 if (quitmore
1805#ifdef FEAT_EVAL
1806 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001807 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001808#endif
1809#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001810 /* avoid that an autocommand, e.g. QuitPre, does this */
1811 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812#endif
1813 )
1814 --quitmore;
1815
1816 /*
1817 * Reset browse, confirm, etc.. They are restored when returning, for
1818 * recursive calls.
1819 */
1820 save_cmdmod = cmdmod;
1821 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1822
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001823 /* "#!anything" is handled like a comment. */
1824 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1825 goto doend;
1826
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 /*
1828 * Repeat until no more command modifiers are found.
1829 */
1830 ea.cmd = *cmdlinep;
1831 for (;;)
1832 {
1833/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001834 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 */
1836 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1837 ++ea.cmd;
1838
1839 /* in ex mode, an empty line works like :+ */
1840 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001841 && (getline_equal(fgetline, cookie, getexmodeline)
1842 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001843 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 {
1845 ea.cmd = (char_u *)"+";
1846 ex_pressedreturn = TRUE;
1847 }
1848
1849 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001850 if (*ea.cmd == '"')
1851 goto doend;
1852 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001853 {
1854 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857
1858/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001859 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 */
1861 p = ea.cmd;
1862 if (VIM_ISDIGIT(*ea.cmd))
1863 p = skipwhite(skipdigits(ea.cmd));
1864 switch (*p)
1865 {
1866 /* When adding an entry, also modify cmd_exists(). */
1867 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1868 break;
1869#ifdef FEAT_WINDOWS
1870 cmdmod.split |= WSP_ABOVE;
1871#endif
1872 continue;
1873
1874 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1875 {
1876#ifdef FEAT_WINDOWS
1877 cmdmod.split |= WSP_BELOW;
1878#endif
1879 continue;
1880 }
1881 if (checkforcmd(&ea.cmd, "browse", 3))
1882 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001883#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 cmdmod.browse = TRUE;
1885#endif
1886 continue;
1887 }
1888 if (!checkforcmd(&ea.cmd, "botright", 2))
1889 break;
1890#ifdef FEAT_WINDOWS
1891 cmdmod.split |= WSP_BOT;
1892#endif
1893 continue;
1894
1895 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1896 break;
1897#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1898 cmdmod.confirm = TRUE;
1899#endif
1900 continue;
1901
1902 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1903 {
1904 cmdmod.keepmarks = TRUE;
1905 continue;
1906 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001907 if (checkforcmd(&ea.cmd, "keepalt", 5))
1908 {
1909 cmdmod.keepalt = TRUE;
1910 continue;
1911 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001912 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1913 {
1914 cmdmod.keeppatterns = TRUE;
1915 continue;
1916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1918 break;
1919 cmdmod.keepjumps = TRUE;
1920 continue;
1921
1922 /* ":hide" and ":hide | cmd" are not modifiers */
1923 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1924 || *p == NUL || ends_excmd(*p))
1925 break;
1926 ea.cmd = p;
1927 cmdmod.hide = TRUE;
1928 continue;
1929
1930 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1931 {
1932 cmdmod.lockmarks = TRUE;
1933 continue;
1934 }
1935
1936 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1937 break;
1938#ifdef FEAT_WINDOWS
1939 cmdmod.split |= WSP_ABOVE;
1940#endif
1941 continue;
1942
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001943 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001944 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001945#ifdef FEAT_AUTOCMD
1946 if (cmdmod.save_ei == NULL)
1947 {
1948 /* Set 'eventignore' to "all". Restore the
1949 * existing option value later. */
1950 cmdmod.save_ei = vim_strsave(p_ei);
1951 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001952 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001953 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001954#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001955 continue;
1956 }
1957 if (!checkforcmd(&ea.cmd, "noswapfile", 6))
1958 break;
1959 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001960 continue;
1961
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1963 break;
1964#ifdef FEAT_WINDOWS
1965 cmdmod.split |= WSP_BELOW;
1966#endif
1967 continue;
1968
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001969 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1970 {
1971#ifdef HAVE_SANDBOX
1972 if (!did_sandbox)
1973 ++sandbox;
1974 did_sandbox = TRUE;
1975#endif
1976 continue;
1977 }
1978 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001980 if (save_msg_silent == -1)
1981 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1984 {
1985 /* ":silent!", but not "silent !cmd" */
1986 ea.cmd = skipwhite(ea.cmd + 1);
1987 ++emsg_silent;
1988 ++did_esilent;
1989 }
1990 continue;
1991
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001992 case 't': if (checkforcmd(&p, "tab", 3))
1993 {
1994#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001995 if (vim_isdigit(*ea.cmd))
1996 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1997 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001998 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001999 ea.cmd = p;
2000#endif
2001 continue;
2002 }
2003 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 break;
2005#ifdef FEAT_WINDOWS
2006 cmdmod.split |= WSP_TOP;
2007#endif
2008 continue;
2009
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002010 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
2011 break;
2012 if (save_msg_silent == -1)
2013 save_msg_silent = msg_silent;
2014 msg_silent = 0;
2015 continue;
2016
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
2018 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002019#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 cmdmod.split |= WSP_VERT;
2021#endif
2022 continue;
2023 }
2024 if (!checkforcmd(&p, "verbose", 4))
2025 break;
2026 if (verbose_save < 0)
2027 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00002028 if (vim_isdigit(*ea.cmd))
2029 p_verbose = atoi((char *)ea.cmd);
2030 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 p_verbose = 1;
2032 ea.cmd = p;
2033 continue;
2034 }
2035 break;
2036 }
2037
2038#ifdef FEAT_EVAL
2039 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2040 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2041#else
2042 ea.skip = (if_level > 0);
2043#endif
2044
2045#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002046# ifdef FEAT_PROFILE
2047 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002048 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002049 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002050 if (getline_equal(fgetline, cookie, get_func_line))
2051 func_line_exec(getline_cookie(fgetline, cookie));
2052 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002053 script_line_exec();
2054 }
2055#endif
2056
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 /* May go to debug mode. If this happens and the ">quit" debug command is
2058 * used, throw an interrupt exception and skip the next command. */
2059 dbg_check_breakpoint(&ea);
2060 if (!ea.skip && got_int)
2061 {
2062 ea.skip = TRUE;
2063 (void)do_intthrow(cstack);
2064 }
2065#endif
2066
2067/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002068 * 3. Skip over the range to find the command. Let "p" point to after it.
2069 *
2070 * We need the command to know what kind of range it uses.
2071 */
2072 cmd = ea.cmd;
2073 ea.cmd = skip_range(ea.cmd, NULL);
2074 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2075 ea.cmd = skipwhite(ea.cmd + 1);
2076 p = find_command(&ea, NULL);
2077
2078/*
2079 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 *
2081 * where 'addr' is:
2082 *
2083 * % (entire file)
2084 * $ [+-NUM]
2085 * 'x [+-NUM] (where x denotes a currently defined mark)
2086 * . [+-NUM]
2087 * [+-NUM]..
2088 * NUM
2089 *
2090 * The ea.cmd pointer is updated to point to the first character following the
2091 * range spec. If an initial address is found, but no second, the upper bound
2092 * is equal to the lower.
2093 */
2094
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002095 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002096 if (!IS_USER_CMDIDX(ea.cmdidx))
2097 {
2098 if (ea.cmdidx != CMD_SIZE)
2099 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2100 else
2101 ea.addr_type = ADDR_LINES;
2102
2103#ifdef FEAT_WINDOWS
2104 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002105 if (ea.cmdidx == CMD_wincmd && p != NULL)
2106 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002107#endif
2108 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002109
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002111 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 for (;;)
2113 {
2114 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002115 switch (ea.addr_type)
2116 {
2117 case ADDR_LINES:
2118 /* default is current line number */
2119 ea.line2 = curwin->w_cursor.lnum;
2120 break;
2121 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002122 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002123 ea.line2 = lnum;
2124 break;
2125 case ADDR_ARGUMENTS:
2126 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002127 if (ea.line2 > ARGCOUNT)
2128 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002129 break;
2130 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002131 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002132 ea.line2 = curbuf->b_fnum;
2133 break;
2134 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002135 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002136 ea.line2 = lnum;
2137 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002138#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002139 case ADDR_QUICKFIX:
2140 ea.line2 = qf_get_cur_valid_idx(&ea);
2141 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002142#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002145 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip,
2146 ea.addr_count == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 if (ea.cmd == NULL) /* error detected */
2148 goto doend;
2149 if (lnum == MAXLNUM)
2150 {
2151 if (*ea.cmd == '%') /* '%' - all lines */
2152 {
2153 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002154 switch (ea.addr_type)
2155 {
2156 case ADDR_LINES:
2157 ea.line1 = 1;
2158 ea.line2 = curbuf->b_ml.ml_line_count;
2159 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002160 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002161 {
2162 buf_T *buf = firstbuf;
2163
2164 while (buf->b_next != NULL
2165 && buf->b_ml.ml_mfp == NULL)
2166 buf = buf->b_next;
2167 ea.line1 = buf->b_fnum;
2168 buf = lastbuf;
2169 while (buf->b_prev != NULL
2170 && buf->b_ml.ml_mfp == NULL)
2171 buf = buf->b_prev;
2172 ea.line2 = buf->b_fnum;
2173 break;
2174 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002175 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002176 ea.line1 = firstbuf->b_fnum;
2177 ea.line2 = lastbuf->b_fnum;
2178 break;
2179 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002180 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002181 if (IS_USER_CMDIDX(ea.cmdidx))
2182 {
2183 ea.line1 = 1;
2184 ea.line2 = ea.addr_type == ADDR_WINDOWS
2185 ? LAST_WIN_NR : LAST_TAB_NR;
2186 }
2187 else
2188 {
2189 /* there is no Vim command which uses '%' and
2190 * ADDR_WINDOWS or ADDR_TABS */
2191 errormsg = (char_u *)_(e_invrange);
2192 goto doend;
2193 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002194 break;
2195 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002196 if (ARGCOUNT == 0)
2197 ea.line1 = ea.line2 = 0;
2198 else
2199 {
2200 ea.line1 = 1;
2201 ea.line2 = ARGCOUNT;
2202 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002203 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002204#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002205 case ADDR_QUICKFIX:
2206 ea.line1 = 1;
2207 ea.line2 = qf_get_size(&ea);
2208 if (ea.line2 == 0)
2209 ea.line2 = 1;
2210 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002211#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 ++ea.addr_count;
2214 }
2215 /* '*' - visual area */
2216 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2217 {
2218 pos_T *fp;
2219
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002220 if (ea.addr_type != ADDR_LINES)
2221 {
2222 errormsg = (char_u *)_(e_invrange);
2223 goto doend;
2224 }
2225
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 ++ea.cmd;
2227 if (!ea.skip)
2228 {
2229 fp = getmark('<', FALSE);
2230 if (check_mark(fp) == FAIL)
2231 goto doend;
2232 ea.line1 = fp->lnum;
2233 fp = getmark('>', FALSE);
2234 if (check_mark(fp) == FAIL)
2235 goto doend;
2236 ea.line2 = fp->lnum;
2237 ++ea.addr_count;
2238 }
2239 }
2240 }
2241 else
2242 ea.line2 = lnum;
2243 ea.addr_count++;
2244
2245 if (*ea.cmd == ';')
2246 {
2247 if (!ea.skip)
2248 curwin->w_cursor.lnum = ea.line2;
2249 }
2250 else if (*ea.cmd != ',')
2251 break;
2252 ++ea.cmd;
2253 }
2254
2255 /* One address given: set start and end lines */
2256 if (ea.addr_count == 1)
2257 {
2258 ea.line1 = ea.line2;
2259 /* ... but only implicit: really no address given */
2260 if (lnum == MAXLNUM)
2261 ea.addr_count = 0;
2262 }
2263
2264 /* Don't leave the cursor on an illegal line (caused by ';') */
2265 check_cursor_lnum();
2266
2267/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002268 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 */
2270
2271 /*
2272 * Skip ':' and any white space
2273 */
2274 ea.cmd = skipwhite(ea.cmd);
2275 while (*ea.cmd == ':')
2276 ea.cmd = skipwhite(ea.cmd + 1);
2277
2278 /*
2279 * If we got a line, but no command, then go to the line.
2280 * If we find a '|' or '\n' we set ea.nextcmd.
2281 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002282 if (*ea.cmd == NUL || *ea.cmd == '"'
2283 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 {
2285 /*
2286 * strange vi behaviour:
2287 * ":3" jumps to line 3
2288 * ":3|..." prints line 3
2289 * ":|" prints current line
2290 */
2291 if (ea.skip) /* skip this if inside :if */
2292 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002293 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 {
2295 ea.cmdidx = CMD_print;
2296 ea.argt = RANGE+COUNT+TRLBAR;
2297 if ((errormsg = invalid_range(&ea)) == NULL)
2298 {
2299 correct_range(&ea);
2300 ex_print(&ea);
2301 }
2302 }
2303 else if (ea.addr_count != 0)
2304 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002305 if (ea.line2 > curbuf->b_ml.ml_line_count)
2306 {
2307 /* With '-' in 'cpoptions' a line number past the file is an
2308 * error, otherwise put it at the end of the file. */
2309 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2310 ea.line2 = -1;
2311 else
2312 ea.line2 = curbuf->b_ml.ml_line_count;
2313 }
2314
2315 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002316 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 else
2318 {
2319 if (ea.line2 == 0)
2320 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 else
2322 curwin->w_cursor.lnum = ea.line2;
2323 beginline(BL_SOL | BL_FIX);
2324 }
2325 }
2326 goto doend;
2327 }
2328
Bram Moolenaard5005162014-08-22 23:05:54 +02002329#ifdef FEAT_AUTOCMD
2330 /* If this looks like an undefined user command and there are CmdUndefined
2331 * autocommands defined, trigger the matching autocommands. */
2332 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2333 && ASCII_ISUPPER(*ea.cmd)
2334 && has_cmdundefined())
2335 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002336 int ret;
2337
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002338 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002339 while (ASCII_ISALNUM(*p))
2340 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002341 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002342 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2343 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002344 /* If the autocommands did something and didn't cause an error, try
2345 * finding the command again. */
2346 p = (ret && !aborting()) ? find_command(&ea, NULL) : NULL;
Bram Moolenaard5005162014-08-22 23:05:54 +02002347 }
2348#endif
2349
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350#ifdef FEAT_USR_CMDS
2351 if (p == NULL)
2352 {
2353 if (!ea.skip)
2354 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2355 goto doend;
2356 }
2357 /* Check for wrong commands. */
2358 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2359 {
2360 errormsg = uc_fun_cmd();
2361 goto doend;
2362 }
2363#endif
2364 if (ea.cmdidx == CMD_SIZE)
2365 {
2366 if (!ea.skip)
2367 {
2368 STRCPY(IObuff, _("E492: Not an editor command"));
2369 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002370 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002372 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 }
2374 goto doend;
2375 }
2376
Bram Moolenaar958636c2014-10-21 20:01:58 +02002377 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2378 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002379#ifdef HAVE_EX_SCRIPT_NI
2380 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2381#endif
2382 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383
2384#ifndef FEAT_EVAL
2385 /*
2386 * When the expression evaluation is disabled, recognize the ":if" and
2387 * ":endif" commands and ignore everything in between it.
2388 */
2389 if (ea.cmdidx == CMD_if)
2390 ++if_level;
2391 if (if_level)
2392 {
2393 if (ea.cmdidx == CMD_endif)
2394 --if_level;
2395 goto doend;
2396 }
2397
2398#endif
2399
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002400 /* forced commands */
2401 if (*p == '!' && ea.cmdidx != CMD_substitute
2402 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 {
2404 ++p;
2405 ea.forceit = TRUE;
2406 }
2407 else
2408 ea.forceit = FALSE;
2409
2410/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002411 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002413 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002414 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415
2416 if (!ea.skip)
2417 {
2418#ifdef HAVE_SANDBOX
2419 if (sandbox != 0 && !(ea.argt & SBOXOK))
2420 {
2421 /* Command not allowed in sandbox. */
2422 errormsg = (char_u *)_(e_sandbox);
2423 goto doend;
2424 }
2425#endif
2426 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2427 {
2428 /* Command not allowed in non-'modifiable' buffer */
2429 errormsg = (char_u *)_(e_modifiable);
2430 goto doend;
2431 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002432
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002433 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002434 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002436 /* Command not allowed when editing the command line. */
2437#ifdef FEAT_CMDWIN
2438 if (cmdwin_type != 0)
2439 errormsg = (char_u *)_(e_cmdwin);
2440 else
2441#endif
2442 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 goto doend;
2444 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002445#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002446 /* Disallow editing another buffer when "curbuf_lock" is set.
2447 * Do allow ":edit" (check for argument later).
2448 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002449 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002450 && ea.cmdidx != CMD_edit
2451 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002452 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002453 && curbuf_locked())
2454 goto doend;
2455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456
2457 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2458 {
2459 /* no range allowed */
2460 errormsg = (char_u *)_(e_norange);
2461 goto doend;
2462 }
2463 }
2464
2465 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2466 {
2467 errormsg = (char_u *)_(e_nobang);
2468 goto doend;
2469 }
2470
2471 /*
2472 * Don't complain about the range if it is not used
2473 * (could happen if line_count is accidentally set to 0).
2474 */
2475 if (!ea.skip && !ni)
2476 {
2477 /*
2478 * If the range is backwards, ask for confirmation and, if given, swap
2479 * ea.line1 & ea.line2 so it's forwards again.
2480 * When global command is busy, don't ask, will fail below.
2481 */
2482 if (!global_busy && ea.line1 > ea.line2)
2483 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002484 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002486 if (sourcing || exmode_active)
2487 {
2488 errormsg = (char_u *)_("E493: Backwards range given");
2489 goto doend;
2490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 if (ask_yesno((char_u *)
2492 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002493 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 }
2495 lnum = ea.line1;
2496 ea.line1 = ea.line2;
2497 ea.line2 = lnum;
2498 }
2499 if ((errormsg = invalid_range(&ea)) != NULL)
2500 goto doend;
2501 }
2502
2503 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2504 ea.line2 = 1;
2505
2506 correct_range(&ea);
2507
2508#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002509 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2510 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 {
2512 /* Put the first line at the start of a closed fold, put the last line
2513 * at the end of a closed fold. */
2514 (void)hasFolding(ea.line1, &ea.line1, NULL);
2515 (void)hasFolding(ea.line2, NULL, &ea.line2);
2516 }
2517#endif
2518
2519#ifdef FEAT_QUICKFIX
2520 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002521 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 * option here, so things like % get expanded.
2523 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002524 p = replace_makeprg(&ea, p, cmdlinep);
2525 if (p == NULL)
2526 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527#endif
2528
2529 /*
2530 * Skip to start of argument.
2531 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2532 */
2533 if (ea.cmdidx == CMD_bang)
2534 ea.arg = p;
2535 else
2536 ea.arg = skipwhite(p);
2537
2538 /*
2539 * Check for "++opt=val" argument.
2540 * Must be first, allow ":w ++enc=utf8 !cmd"
2541 */
2542 if (ea.argt & ARGOPT)
2543 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2544 if (getargopt(&ea) == FAIL && !ni)
2545 {
2546 errormsg = (char_u *)_(e_invarg);
2547 goto doend;
2548 }
2549
2550 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2551 {
2552 if (*ea.arg == '>') /* append */
2553 {
2554 if (*++ea.arg != '>') /* typed wrong */
2555 {
2556 errormsg = (char_u *)_("E494: Use w or w>>");
2557 goto doend;
2558 }
2559 ea.arg = skipwhite(ea.arg + 1);
2560 ea.append = TRUE;
2561 }
2562 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2563 {
2564 ++ea.arg;
2565 ea.usefilter = TRUE;
2566 }
2567 }
2568
2569 if (ea.cmdidx == CMD_read)
2570 {
2571 if (ea.forceit)
2572 {
2573 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2574 ea.forceit = FALSE;
2575 }
2576 else if (*ea.arg == '!') /* :r !filter */
2577 {
2578 ++ea.arg;
2579 ea.usefilter = TRUE;
2580 }
2581 }
2582
2583 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2584 {
2585 ea.amount = 1;
2586 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2587 {
2588 ++ea.arg;
2589 ++ea.amount;
2590 }
2591 ea.arg = skipwhite(ea.arg);
2592 }
2593
2594 /*
2595 * Check for "+command" argument, before checking for next command.
2596 * Don't do this for ":read !cmd" and ":write !cmd".
2597 */
2598 if ((ea.argt & EDITCMD) && !ea.usefilter)
2599 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2600
2601 /*
2602 * Check for '|' to separate commands and '"' to start comments.
2603 * Don't do this for ":read !cmd" and ":write !cmd".
2604 */
2605 if ((ea.argt & TRLBAR) && !ea.usefilter)
2606 separate_nextcmd(&ea);
2607
2608 /*
2609 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002610 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2611 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002613 else if (ea.cmdidx == CMD_bang
2614 || ea.cmdidx == CMD_global
2615 || ea.cmdidx == CMD_vglobal
2616 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 {
2618 for (p = ea.arg; *p; ++p)
2619 {
2620 /* Remove one backslash before a newline, so that it's possible to
2621 * pass a newline to the shell and also a newline that is preceded
2622 * with a backslash. This makes it impossible to end a shell
2623 * command in a backslash, but that doesn't appear useful.
2624 * Halving the number of backslashes is incompatible with previous
2625 * versions. */
2626 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002627 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 else if (*p == '\n')
2629 {
2630 ea.nextcmd = p + 1;
2631 *p = NUL;
2632 break;
2633 }
2634 }
2635 }
2636
2637 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2638 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002639 buf_T *buf;
2640
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002642 switch (ea.addr_type)
2643 {
2644 case ADDR_LINES:
2645 ea.line2 = curbuf->b_ml.ml_line_count;
2646 break;
2647 case ADDR_LOADED_BUFFERS:
2648 buf = firstbuf;
2649 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2650 buf = buf->b_next;
2651 ea.line1 = buf->b_fnum;
2652 buf = lastbuf;
2653 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2654 buf = buf->b_prev;
2655 ea.line2 = buf->b_fnum;
2656 break;
2657 case ADDR_BUFFERS:
2658 ea.line1 = firstbuf->b_fnum;
2659 ea.line2 = lastbuf->b_fnum;
2660 break;
2661 case ADDR_WINDOWS:
2662 ea.line2 = LAST_WIN_NR;
2663 break;
2664 case ADDR_TABS:
2665 ea.line2 = LAST_TAB_NR;
2666 break;
2667 case ADDR_ARGUMENTS:
2668 if (ARGCOUNT == 0)
2669 ea.line1 = ea.line2 = 0;
2670 else
2671 ea.line2 = ARGCOUNT;
2672 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002673#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002674 case ADDR_QUICKFIX:
2675 ea.line2 = qf_get_size(&ea);
2676 if (ea.line2 == 0)
2677 ea.line2 = 1;
2678 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002679#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 }
2682
2683 /* accept numbered register only when no count allowed (:put) */
2684 if ( (ea.argt & REGSTR)
2685 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002686 /* Do not allow register = for user commands */
2687 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2689 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002690#ifndef FEAT_CLIPBOARD
2691 /* check these explicitly for a more specific error message */
2692 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002694 errormsg = (char_u *)_(e_invalidreg);
2695 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 }
2697#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002698 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2699 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002700 {
2701 ea.regname = *ea.arg++;
2702#ifdef FEAT_EVAL
2703 /* for '=' register: accept the rest of the line as an expression */
2704 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2705 {
2706 set_expr_line(vim_strsave(ea.arg));
2707 ea.arg += STRLEN(ea.arg);
2708 }
2709#endif
2710 ea.arg = skipwhite(ea.arg);
2711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 }
2713
2714 /*
2715 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2716 * count, it's a buffer name.
2717 */
2718 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2719 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2720 || vim_iswhite(*p)))
2721 {
2722 n = getdigits(&ea.arg);
2723 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002724 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 {
2726 errormsg = (char_u *)_(e_zerocount);
2727 goto doend;
2728 }
2729 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2730 {
2731 ea.line2 = n;
2732 if (ea.addr_count == 0)
2733 ea.addr_count = 1;
2734 }
2735 else
2736 {
2737 ea.line1 = ea.line2;
2738 ea.line2 += n - 1;
2739 ++ea.addr_count;
2740 /*
2741 * Be vi compatible: no error message for out of range.
2742 */
2743 if (ea.line2 > curbuf->b_ml.ml_line_count)
2744 ea.line2 = curbuf->b_ml.ml_line_count;
2745 }
2746 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002747
2748 /*
2749 * Check for flags: 'l', 'p' and '#'.
2750 */
2751 if (ea.argt & EXFLAGS)
2752 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002754 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002755 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 {
2757 errormsg = (char_u *)_(e_trailing);
2758 goto doend;
2759 }
2760
2761 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2762 {
2763 errormsg = (char_u *)_(e_argreq);
2764 goto doend;
2765 }
2766
2767#ifdef FEAT_EVAL
2768 /*
2769 * Skip the command when it's not going to be executed.
2770 * The commands like :if, :endif, etc. always need to be executed.
2771 * Also make an exception for commands that handle a trailing command
2772 * themselves.
2773 */
2774 if (ea.skip)
2775 {
2776 switch (ea.cmdidx)
2777 {
2778 /* commands that need evaluation */
2779 case CMD_while:
2780 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002781 case CMD_for:
2782 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 case CMD_if:
2784 case CMD_elseif:
2785 case CMD_else:
2786 case CMD_endif:
2787 case CMD_try:
2788 case CMD_catch:
2789 case CMD_finally:
2790 case CMD_endtry:
2791 case CMD_function:
2792 break;
2793
2794 /* Commands that handle '|' themselves. Check: A command should
2795 * either have the TRLBAR flag, appear in this list or appear in
2796 * the list at ":help :bar". */
2797 case CMD_aboveleft:
2798 case CMD_and:
2799 case CMD_belowright:
2800 case CMD_botright:
2801 case CMD_browse:
2802 case CMD_call:
2803 case CMD_confirm:
2804 case CMD_delfunction:
2805 case CMD_djump:
2806 case CMD_dlist:
2807 case CMD_dsearch:
2808 case CMD_dsplit:
2809 case CMD_echo:
2810 case CMD_echoerr:
2811 case CMD_echomsg:
2812 case CMD_echon:
2813 case CMD_execute:
2814 case CMD_help:
2815 case CMD_hide:
2816 case CMD_ijump:
2817 case CMD_ilist:
2818 case CMD_isearch:
2819 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002820 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 case CMD_keepjumps:
2822 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002823 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 case CMD_leftabove:
2825 case CMD_let:
2826 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002827 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002829 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002830 case CMD_noautocmd:
2831 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 case CMD_perl:
2833 case CMD_psearch:
2834 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002835 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002836 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 case CMD_return:
2838 case CMD_rightbelow:
2839 case CMD_ruby:
2840 case CMD_silent:
2841 case CMD_smagic:
2842 case CMD_snomagic:
2843 case CMD_substitute:
2844 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002845 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 case CMD_tcl:
2847 case CMD_throw:
2848 case CMD_tilde:
2849 case CMD_topleft:
2850 case CMD_unlet:
2851 case CMD_verbose:
2852 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002853 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 break;
2855
2856 default: goto doend;
2857 }
2858 }
2859#endif
2860
2861 if (ea.argt & XFILE)
2862 {
2863 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2864 goto doend;
2865 }
2866
2867#ifdef FEAT_LISTCMDS
2868 /*
2869 * Accept buffer name. Cannot be used at the same time with a buffer
2870 * number. Don't do this for a user command.
2871 */
2872 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002873 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 {
2875 /*
2876 * :bdelete, :bwipeout and :bunload take several arguments, separated
2877 * by spaces: find next space (skipping over escaped characters).
2878 * The others take one argument: ignore trailing spaces.
2879 */
2880 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2881 || ea.cmdidx == CMD_bunload)
2882 p = skiptowhite_esc(ea.arg);
2883 else
2884 {
2885 p = ea.arg + STRLEN(ea.arg);
2886 while (p > ea.arg && vim_iswhite(p[-1]))
2887 --p;
2888 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002889 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2890 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 if (ea.line2 < 0) /* failed */
2892 goto doend;
2893 ea.addr_count = 1;
2894 ea.arg = skipwhite(p);
2895 }
2896#endif
2897
2898/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002899 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 *
2901 * The "ea" structure holds the arguments that can be used.
2902 */
2903 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002904 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 ea.cookie = cookie;
2906#ifdef FEAT_EVAL
2907 ea.cstack = cstack;
2908#endif
2909
2910#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002911 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 {
2913 /*
2914 * Execute a user-defined command.
2915 */
2916 do_ucmd(&ea);
2917 }
2918 else
2919#endif
2920 {
2921 /*
2922 * Call the function to execute the command.
2923 */
2924 ea.errmsg = NULL;
2925 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2926 if (ea.errmsg != NULL)
2927 errormsg = (char_u *)_(ea.errmsg);
2928 }
2929
2930#ifdef FEAT_EVAL
2931 /*
2932 * If the command just executed called do_cmdline(), any throw or ":return"
2933 * or ":finish" encountered there must also check the cstack of the still
2934 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2935 * exception, or reanimate a returned function or finished script file and
2936 * return or finish it again.
2937 */
2938 if (need_rethrow)
2939 do_throw(cstack);
2940 else if (check_cstack)
2941 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002942 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002944 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 && current_func_returned())
2946 do_return(&ea, TRUE, FALSE, NULL);
2947 }
2948 need_rethrow = check_cstack = FALSE;
2949#endif
2950
2951doend:
2952 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2953 curwin->w_cursor.lnum = 1;
2954
2955 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2956 {
2957 if (sourcing)
2958 {
2959 if (errormsg != IObuff)
2960 {
2961 STRCPY(IObuff, errormsg);
2962 errormsg = IObuff;
2963 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002964 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 }
2966 emsg(errormsg);
2967 }
2968#ifdef FEAT_EVAL
2969 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002970 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2971 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972#endif
2973
2974 if (verbose_save >= 0)
2975 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002976#ifdef FEAT_AUTOCMD
2977 if (cmdmod.save_ei != NULL)
2978 {
2979 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002980 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2981 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002982 free_string_option(cmdmod.save_ei);
2983 }
2984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985
2986 cmdmod = save_cmdmod;
2987
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002988 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 {
2990 /* messages could be enabled for a serious error, need to check if the
2991 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002992 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002993 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 emsg_silent -= did_esilent;
2995 if (emsg_silent < 0)
2996 emsg_silent = 0;
2997 /* Restore msg_scroll, it's set by file I/O commands, even when no
2998 * message is actually displayed. */
2999 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003000
3001 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
3002 * somewhere in the line. Put it back in the first column. */
3003 if (redirecting())
3004 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 }
3006
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003007#ifdef HAVE_SANDBOX
3008 if (did_sandbox)
3009 --sandbox;
3010#endif
3011
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3013 ea.nextcmd = NULL;
3014
3015#ifdef FEAT_EVAL
3016 --ex_nesting_level;
3017#endif
3018
3019 return ea.nextcmd;
3020}
3021#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003022 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023#endif
3024
3025/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003026 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 * If there is a match advance "pp" to the argument and return TRUE.
3028 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003029 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003030checkforcmd(
3031 char_u **pp, /* start of command */
3032 char *cmd, /* name of command */
3033 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034{
3035 int i;
3036
3037 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003038 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 break;
3040 if (i >= len && !isalpha((*pp)[i]))
3041 {
3042 *pp = skipwhite(*pp + i);
3043 return TRUE;
3044 }
3045 return FALSE;
3046}
3047
3048/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003049 * Append "cmd" to the error message in IObuff.
3050 * Takes care of limiting the length and handling 0xa0, which would be
3051 * invisible otherwise.
3052 */
3053 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003054append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003055{
3056 char_u *s = cmd;
3057 char_u *d;
3058
3059 STRCAT(IObuff, ": ");
3060 d = IObuff + STRLEN(IObuff);
3061 while (*s != NUL && d - IObuff < IOSIZE - 7)
3062 {
3063 if (
3064#ifdef FEAT_MBYTE
3065 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3066#endif
3067 *s == 0xa0)
3068 {
3069 s +=
3070#ifdef FEAT_MBYTE
3071 enc_utf8 ? 2 :
3072#endif
3073 1;
3074 STRCPY(d, "<a0>");
3075 d += 4;
3076 }
3077 else
3078 MB_COPY_CHAR(s, d);
3079 }
3080 *d = NUL;
3081}
3082
3083/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003085 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003087 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 * Returns NULL for an ambiguous user command.
3089 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003091find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092{
3093 int len;
3094 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003095 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096
3097 /*
3098 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003099 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 * - the 'k' command can directly be followed by any character.
3101 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003102 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003104 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 */
3106 p = eap->cmd;
3107 if (*p == 'k')
3108 {
3109 eap->cmdidx = CMD_k;
3110 ++p;
3111 }
3112 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003113 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3114 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 || p[1] == 'g'
3116 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3117 || p[1] == 'I'
3118 || (p[1] == 'r' && p[2] != 'e')))
3119 {
3120 eap->cmdidx = CMD_substitute;
3121 ++p;
3122 }
3123 else
3124 {
3125 while (ASCII_ISALPHA(*p))
3126 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003127 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003128 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003129 while (ASCII_ISALNUM(*p))
3130 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003131
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 /* check for non-alpha command */
3133 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3134 ++p;
3135 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003136 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3137 {
3138 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3139 * :delete with the 'l' flag. Same for 'p'. */
3140 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003141 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003142 break;
3143 if (i == len - 1)
3144 {
3145 --len;
3146 if (p[-1] == 'l')
3147 eap->flags |= EXFLAG_LIST;
3148 else
3149 eap->flags |= EXFLAG_PRINT;
3150 }
3151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
3153 if (ASCII_ISLOWER(*eap->cmd))
3154 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
3155 else
3156 eap->cmdidx = cmdidxs[26];
3157
3158 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3159 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3160 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3161 (size_t)len) == 0)
3162 {
3163#ifdef FEAT_EVAL
3164 if (full != NULL
3165 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3166 *full = TRUE;
3167#endif
3168 break;
3169 }
3170
3171#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003172 /* Look for a user defined command as a last resort. Let ":Print" be
3173 * overruled by a user defined command. */
3174 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3175 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003177 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 while (ASCII_ISALNUM(*p))
3179 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003180 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 }
3182#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003183 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 eap->cmdidx = CMD_SIZE;
3185 }
3186
3187 return p;
3188}
3189
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003190#ifdef FEAT_USR_CMDS
3191/*
3192 * Search for a user command that matches "eap->cmd".
3193 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3194 * Return a pointer to just after the command.
3195 * Return NULL if there is no matching command.
3196 */
3197 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003198find_ucmd(
3199 exarg_T *eap,
3200 char_u *p, /* end of the command (possibly including count) */
3201 int *full, /* set to TRUE for a full match */
3202 expand_T *xp, /* used for completion, NULL otherwise */
3203 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003204{
3205 int len = (int)(p - eap->cmd);
3206 int j, k, matchlen = 0;
3207 ucmd_T *uc;
3208 int found = FALSE;
3209 int possible = FALSE;
3210 char_u *cp, *np; /* Point into typed cmd and test name */
3211 garray_T *gap;
3212 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3213 only full match global is accepted. */
3214
3215 /*
3216 * Look for buffer-local user commands first, then global ones.
3217 */
3218 gap = &curbuf->b_ucmds;
3219 for (;;)
3220 {
3221 for (j = 0; j < gap->ga_len; ++j)
3222 {
3223 uc = USER_CMD_GA(gap, j);
3224 cp = eap->cmd;
3225 np = uc->uc_name;
3226 k = 0;
3227 while (k < len && *np != NUL && *cp++ == *np++)
3228 k++;
3229 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3230 {
3231 /* If finding a second match, the command is ambiguous. But
3232 * not if a buffer-local command wasn't a full match and a
3233 * global command is a full match. */
3234 if (k == len && found && *np != NUL)
3235 {
3236 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003237 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003238 amb_local = TRUE;
3239 }
3240
3241 if (!found || (k == len && *np == NUL))
3242 {
3243 /* If we matched up to a digit, then there could
3244 * be another command including the digit that we
3245 * should use instead.
3246 */
3247 if (k == len)
3248 found = TRUE;
3249 else
3250 possible = TRUE;
3251
3252 if (gap == &ucmds)
3253 eap->cmdidx = CMD_USER;
3254 else
3255 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003256 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003257 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003258 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003259
3260# ifdef FEAT_CMDL_COMPL
3261 if (compl != NULL)
3262 *compl = uc->uc_compl;
3263# ifdef FEAT_EVAL
3264 if (xp != NULL)
3265 {
3266 xp->xp_arg = uc->uc_compl_arg;
3267 xp->xp_scriptID = uc->uc_scriptID;
3268 }
3269# endif
3270# endif
3271 /* Do not search for further abbreviations
3272 * if this is an exact match. */
3273 matchlen = k;
3274 if (k == len && *np == NUL)
3275 {
3276 if (full != NULL)
3277 *full = TRUE;
3278 amb_local = FALSE;
3279 break;
3280 }
3281 }
3282 }
3283 }
3284
3285 /* Stop if we found a full match or searched all. */
3286 if (j < gap->ga_len || gap == &ucmds)
3287 break;
3288 gap = &ucmds;
3289 }
3290
3291 /* Only found ambiguous matches. */
3292 if (amb_local)
3293 {
3294 if (xp != NULL)
3295 xp->xp_context = EXPAND_UNSUCCESSFUL;
3296 return NULL;
3297 }
3298
3299 /* The match we found may be followed immediately by a number. Move "p"
3300 * back to point to it. */
3301 if (found || possible)
3302 return p + (matchlen - len);
3303 return p;
3304}
3305#endif
3306
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003308static struct cmdmod
3309{
3310 char *name;
3311 int minlen;
3312 int has_count; /* :123verbose :3tab */
3313} cmdmods[] = {
3314 {"aboveleft", 3, FALSE},
3315 {"belowright", 3, FALSE},
3316 {"botright", 2, FALSE},
3317 {"browse", 3, FALSE},
3318 {"confirm", 4, FALSE},
3319 {"hide", 3, FALSE},
3320 {"keepalt", 5, FALSE},
3321 {"keepjumps", 5, FALSE},
3322 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003323 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003324 {"leftabove", 5, FALSE},
3325 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003326 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003327 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003328 {"rightbelow", 6, FALSE},
3329 {"sandbox", 3, FALSE},
3330 {"silent", 3, FALSE},
3331 {"tab", 3, TRUE},
3332 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003333 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003334 {"verbose", 4, TRUE},
3335 {"vertical", 4, FALSE},
3336};
3337
3338/*
3339 * Return length of a command modifier (including optional count).
3340 * Return zero when it's not a modifier.
3341 */
3342 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003343modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003344{
3345 int i, j;
3346 char_u *p = cmd;
3347
3348 if (VIM_ISDIGIT(*cmd))
3349 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003350 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003351 {
3352 for (j = 0; p[j] != NUL; ++j)
3353 if (p[j] != cmdmods[i].name[j])
3354 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003355 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003356 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003357 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003358 }
3359 return 0;
3360}
3361
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362/*
3363 * Return > 0 if an Ex command "name" exists.
3364 * Return 2 if there is an exact match.
3365 * Return 3 if there is an ambiguous match.
3366 */
3367 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003368cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369{
3370 exarg_T ea;
3371 int full = FALSE;
3372 int i;
3373 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003374 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375
3376 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003377 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 {
3379 for (j = 0; name[j] != NUL; ++j)
3380 if (name[j] != cmdmods[i].name[j])
3381 break;
3382 if (name[j] == NUL && j >= cmdmods[i].minlen)
3383 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3384 }
3385
Bram Moolenaara9587612006-05-04 21:47:50 +00003386 /* Check built-in commands and user defined commands.
3387 * For ":2match" and ":3match" we need to skip the number. */
3388 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003390 p = find_command(&ea, &full);
3391 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003393 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3394 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003395 if (*skipwhite(p) != NUL)
3396 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3398}
3399#endif
3400
3401/*
3402 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3403 * we don't need/want deleted. Maybe this could be done better if we didn't
3404 * repeat all this stuff. The only problem is that they may not stay
3405 * perfectly compatible with each other, but then the command line syntax
3406 * probably won't change that much -- webb.
3407 */
3408 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003409set_one_cmd_context(
3410 expand_T *xp,
3411 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412{
3413 char_u *p;
3414 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003415 int len = 0;
3416 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3418 int compl = EXPAND_NOTHING;
3419#endif
3420#ifdef FEAT_CMDL_COMPL
3421 int delim;
3422#endif
3423 int forceit = FALSE;
3424 int usefilter = FALSE; /* filter instead of file name */
3425
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003426 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 xp->xp_pattern = buff;
3428 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003429 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
3431/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003432 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 */
3434 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3435 ;
3436 xp->xp_pattern = cmd;
3437
3438 if (*cmd == NUL)
3439 return NULL;
3440 if (*cmd == '"') /* ignore comment lines */
3441 {
3442 xp->xp_context = EXPAND_NOTHING;
3443 return NULL;
3444 }
3445
3446/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003447 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 */
3449 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 xp->xp_pattern = cmd;
3451 if (*cmd == NUL)
3452 return NULL;
3453 if (*cmd == '"')
3454 {
3455 xp->xp_context = EXPAND_NOTHING;
3456 return NULL;
3457 }
3458
3459 if (*cmd == '|' || *cmd == '\n')
3460 return cmd + 1; /* There's another command */
3461
3462 /*
3463 * Isolate the command and search for it in the command table.
3464 * Exceptions:
3465 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003466 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3468 */
3469 if (*cmd == 'k' && cmd[1] != 'e')
3470 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003471 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 p = cmd + 1;
3473 }
3474 else
3475 {
3476 p = cmd;
3477 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3478 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003479 /* a user command may contain digits */
3480 if (ASCII_ISUPPER(cmd[0]))
3481 while (ASCII_ISALNUM(*p) || *p == '*')
3482 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003483 /* for python 3.x: ":py3*" commands completion */
3484 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003485 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003486 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003487 while (ASCII_ISALPHA(*p) || *p == '*')
3488 ++p;
3489 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003490 /* check for non-alpha command */
3491 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3492 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003493 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003495 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 {
3497 xp->xp_context = EXPAND_UNSUCCESSFUL;
3498 return NULL;
3499 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003500 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003501 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3502 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3503 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 break;
3505
3506#ifdef FEAT_USR_CMDS
3507 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3509 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510#endif
3511 }
3512
3513 /*
3514 * If the cursor is touching the command, and it ends in an alpha-numeric
3515 * character, complete the command name.
3516 */
3517 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3518 return NULL;
3519
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003520 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 {
3522 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3523 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003524 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 p = cmd + 1;
3526 }
3527#ifdef FEAT_USR_CMDS
3528 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3529 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003530 ea.cmd = cmd;
3531 p = find_ucmd(&ea, p, NULL, xp,
3532# if defined(FEAT_CMDL_COMPL)
3533 &compl
3534# else
3535 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003537 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003538 if (p == NULL)
3539 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 }
3541#endif
3542 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003543 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 {
3545 /* Not still touching the command and it was an illegal one */
3546 xp->xp_context = EXPAND_UNSUCCESSFUL;
3547 return NULL;
3548 }
3549
3550 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3551
3552 if (*p == '!') /* forced commands */
3553 {
3554 forceit = TRUE;
3555 ++p;
3556 }
3557
3558/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003559 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003561 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003562 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563
3564 arg = skipwhite(p);
3565
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003566 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 {
3568 if (*arg == '>') /* append */
3569 {
3570 if (*++arg == '>')
3571 ++arg;
3572 arg = skipwhite(arg);
3573 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003574 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 {
3576 ++arg;
3577 usefilter = TRUE;
3578 }
3579 }
3580
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003581 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 {
3583 usefilter = forceit; /* :r! filter if forced */
3584 if (*arg == '!') /* :r !filter */
3585 {
3586 ++arg;
3587 usefilter = TRUE;
3588 }
3589 }
3590
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003591 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 {
3593 while (*arg == *cmd) /* allow any number of '>' or '<' */
3594 ++arg;
3595 arg = skipwhite(arg);
3596 }
3597
3598 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003599 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 {
3601 /* Check if we're in the +command */
3602 p = arg + 1;
3603 arg = skip_cmd_arg(arg, FALSE);
3604
3605 /* Still touching the command after '+'? */
3606 if (*arg == NUL)
3607 return p;
3608
3609 /* Skip space(s) after +command to get to the real argument */
3610 arg = skipwhite(arg);
3611 }
3612
3613 /*
3614 * Check for '|' to separate commands and '"' to start comments.
3615 * Don't do this for ":read !cmd" and ":write !cmd".
3616 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003617 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 {
3619 p = arg;
3620 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003621 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 p += 2;
3623 while (*p)
3624 {
3625 if (*p == Ctrl_V)
3626 {
3627 if (p[1] != NUL)
3628 ++p;
3629 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003630 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 || *p == '|' || *p == '\n')
3632 {
3633 if (*(p - 1) != '\\')
3634 {
3635 if (*p == '|' || *p == '\n')
3636 return p + 1;
3637 return NULL; /* It's a comment */
3638 }
3639 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003640 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 }
3642 }
3643
3644 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003645 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 vim_strchr((char_u *)"|\"", *arg) == NULL)
3647 return NULL;
3648
3649 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003650 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003652 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003653 while (*p && p < buff + len)
3654 {
3655 if (*p == ' ' || *p == TAB)
3656 {
3657 /* argument starts after a space */
3658 xp->xp_pattern = ++p;
3659 }
3660 else
3661 {
3662 if (*p == '\\' && *(p + 1) != NUL)
3663 ++p; /* skip over escaped character */
3664 mb_ptr_adv(p);
3665 }
3666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003668 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003670 int c;
3671 int in_quote = FALSE;
3672 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673
3674 /*
3675 * Allow spaces within back-quotes to count as part of the argument
3676 * being expanded.
3677 */
3678 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003679 p = xp->xp_pattern;
3680 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003682#ifdef FEAT_MBYTE
3683 if (has_mbyte)
3684 c = mb_ptr2char(p);
3685 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003687 c = *p;
3688 if (c == '\\' && p[1] != NUL)
3689 ++p;
3690 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 {
3692 if (!in_quote)
3693 {
3694 xp->xp_pattern = p;
3695 bow = p + 1;
3696 }
3697 in_quote = !in_quote;
3698 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003699 /* An argument can contain just about everything, except
3700 * characters that end the command and white space. */
3701 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003702#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003703 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003704#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003705 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003706 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003707 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003708 while (*p != NUL)
3709 {
3710#ifdef FEAT_MBYTE
3711 if (has_mbyte)
3712 c = mb_ptr2char(p);
3713 else
3714#endif
3715 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003716 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003717 break;
3718#ifdef FEAT_MBYTE
3719 if (has_mbyte)
3720 len = (*mb_ptr2len)(p);
3721 else
3722#endif
3723 len = 1;
3724 mb_ptr_adv(p);
3725 }
3726 if (in_quote)
3727 bow = p;
3728 else
3729 xp->xp_pattern = p;
3730 p -= len;
3731 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003732 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 }
3734
3735 /*
3736 * If we are still inside the quotes, and we passed a space, just
3737 * expand from there.
3738 */
3739 if (bow != NULL && in_quote)
3740 xp->xp_pattern = bow;
3741 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003742
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003743 /* For a shell command more chars need to be escaped. */
3744 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003745 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003746#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003747 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003748#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003749 /* When still after the command name expand executables. */
3750 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003751 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753
3754 /* Check for environment variable */
3755 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003756#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 || *xp->xp_pattern == '%'
3758#endif
3759 )
3760 {
3761 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3762 if (!vim_isIDc(*p))
3763 break;
3764 if (*p == NUL)
3765 {
3766 xp->xp_context = EXPAND_ENV_VARS;
3767 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003768#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3769 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003770 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003771 compl = EXPAND_ENV_VARS;
3772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 }
3774 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003775#if defined(FEAT_CMDL_COMPL)
3776 /* Check for user names */
3777 if (*xp->xp_pattern == '~')
3778 {
3779 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3780 ;
3781 /* Complete ~user only if it partially matches a user name.
3782 * A full match ~user<Tab> will be replaced by user's home
3783 * directory i.e. something like ~user<Tab> -> /home/user/ */
3784 if (*p == NUL && p > xp->xp_pattern + 1
3785 && match_user(xp->xp_pattern + 1) == 1)
3786 {
3787 xp->xp_context = EXPAND_USER;
3788 ++xp->xp_pattern;
3789 }
3790 }
3791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 }
3793
3794/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003795 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003797 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003799 case CMD_find:
3800 case CMD_sfind:
3801 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003802 if (xp->xp_context == EXPAND_FILES)
3803 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003804 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 case CMD_cd:
3806 case CMD_chdir:
3807 case CMD_lcd:
3808 case CMD_lchdir:
3809 if (xp->xp_context == EXPAND_FILES)
3810 xp->xp_context = EXPAND_DIRECTORIES;
3811 break;
3812 case CMD_help:
3813 xp->xp_context = EXPAND_HELP;
3814 xp->xp_pattern = arg;
3815 break;
3816
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003817 /* Command modifiers: return the argument.
3818 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003820 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 case CMD_belowright:
3822 case CMD_botright:
3823 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003824 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003825 case CMD_cdo:
3826 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003828 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 case CMD_folddoclosed:
3830 case CMD_folddoopen:
3831 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003832 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 case CMD_keepjumps:
3834 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003835 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003836 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003838 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003840 case CMD_noautocmd:
3841 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003843 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003845 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003846 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 case CMD_topleft:
3848 case CMD_verbose:
3849 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003850 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 return arg;
3852
Bram Moolenaar4f688582007-07-24 12:34:30 +00003853#ifdef FEAT_CMDL_COMPL
3854# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 case CMD_match:
3856 if (*arg == NUL || !ends_excmd(*arg))
3857 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003858 /* also complete "None" */
3859 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 arg = skipwhite(skiptowhite(arg));
3861 if (*arg != NUL)
3862 {
3863 xp->xp_context = EXPAND_NOTHING;
3864 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3865 }
3866 }
3867 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003868# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870/*
3871 * All completion for the +cmdline_compl feature goes here.
3872 */
3873
3874# ifdef FEAT_USR_CMDS
3875 case CMD_command:
3876 /* Check for attributes */
3877 while (*arg == '-')
3878 {
3879 arg++; /* Skip "-" */
3880 p = skiptowhite(arg);
3881 if (*p == NUL)
3882 {
3883 /* Cursor is still in the attribute */
3884 p = vim_strchr(arg, '=');
3885 if (p == NULL)
3886 {
3887 /* No "=", so complete attribute names */
3888 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3889 xp->xp_pattern = arg;
3890 return NULL;
3891 }
3892
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003893 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 * their arguments as well.
3895 */
3896 if (STRNICMP(arg, "complete", p - arg) == 0)
3897 {
3898 xp->xp_context = EXPAND_USER_COMPLETE;
3899 xp->xp_pattern = p + 1;
3900 return NULL;
3901 }
3902 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3903 {
3904 xp->xp_context = EXPAND_USER_NARGS;
3905 xp->xp_pattern = p + 1;
3906 return NULL;
3907 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003908 else if (STRNICMP(arg, "addr", p - arg) == 0)
3909 {
3910 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3911 xp->xp_pattern = p + 1;
3912 return NULL;
3913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 return NULL;
3915 }
3916 arg = skipwhite(p);
3917 }
3918
3919 /* After the attributes comes the new command name */
3920 p = skiptowhite(arg);
3921 if (*p == NUL)
3922 {
3923 xp->xp_context = EXPAND_USER_COMMANDS;
3924 xp->xp_pattern = arg;
3925 break;
3926 }
3927
3928 /* And finally comes a normal command */
3929 return skipwhite(p);
3930
3931 case CMD_delcommand:
3932 xp->xp_context = EXPAND_USER_COMMANDS;
3933 xp->xp_pattern = arg;
3934 break;
3935# endif
3936
3937 case CMD_global:
3938 case CMD_vglobal:
3939 delim = *arg; /* get the delimiter */
3940 if (delim)
3941 ++arg; /* skip delimiter if there is one */
3942
3943 while (arg[0] != NUL && arg[0] != delim)
3944 {
3945 if (arg[0] == '\\' && arg[1] != NUL)
3946 ++arg;
3947 ++arg;
3948 }
3949 if (arg[0] != NUL)
3950 return arg + 1;
3951 break;
3952 case CMD_and:
3953 case CMD_substitute:
3954 delim = *arg;
3955 if (delim)
3956 {
3957 /* skip "from" part */
3958 ++arg;
3959 arg = skip_regexp(arg, delim, p_magic, NULL);
3960 }
3961 /* skip "to" part */
3962 while (arg[0] != NUL && arg[0] != delim)
3963 {
3964 if (arg[0] == '\\' && arg[1] != NUL)
3965 ++arg;
3966 ++arg;
3967 }
3968 if (arg[0] != NUL) /* skip delimiter */
3969 ++arg;
3970 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3971 ++arg;
3972 if (arg[0] != NUL)
3973 return arg;
3974 break;
3975 case CMD_isearch:
3976 case CMD_dsearch:
3977 case CMD_ilist:
3978 case CMD_dlist:
3979 case CMD_ijump:
3980 case CMD_psearch:
3981 case CMD_djump:
3982 case CMD_isplit:
3983 case CMD_dsplit:
3984 arg = skipwhite(skipdigits(arg)); /* skip count */
3985 if (*arg == '/') /* Match regexp, not just whole words */
3986 {
3987 for (++arg; *arg && *arg != '/'; arg++)
3988 if (*arg == '\\' && arg[1] != NUL)
3989 arg++;
3990 if (*arg)
3991 {
3992 arg = skipwhite(arg + 1);
3993
3994 /* Check for trailing illegal characters */
3995 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3996 xp->xp_context = EXPAND_NOTHING;
3997 else
3998 return arg;
3999 }
4000 }
4001 break;
4002#ifdef FEAT_AUTOCMD
4003 case CMD_autocmd:
4004 return set_context_in_autocmd(xp, arg, FALSE);
4005
4006 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004007 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 return set_context_in_autocmd(xp, arg, TRUE);
4009#endif
4010 case CMD_set:
4011 set_context_in_set_cmd(xp, arg, 0);
4012 break;
4013 case CMD_setglobal:
4014 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4015 break;
4016 case CMD_setlocal:
4017 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4018 break;
4019 case CMD_tag:
4020 case CMD_stag:
4021 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004022 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 case CMD_tselect:
4024 case CMD_stselect:
4025 case CMD_ptselect:
4026 case CMD_tjump:
4027 case CMD_stjump:
4028 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004029 if (*p_wop != NUL)
4030 xp->xp_context = EXPAND_TAGS_LISTFILES;
4031 else
4032 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 xp->xp_pattern = arg;
4034 break;
4035 case CMD_augroup:
4036 xp->xp_context = EXPAND_AUGROUP;
4037 xp->xp_pattern = arg;
4038 break;
4039#ifdef FEAT_SYN_HL
4040 case CMD_syntax:
4041 set_context_in_syntax_cmd(xp, arg);
4042 break;
4043#endif
4044#ifdef FEAT_EVAL
4045 case CMD_let:
4046 case CMD_if:
4047 case CMD_elseif:
4048 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004049 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 case CMD_echo:
4051 case CMD_echon:
4052 case CMD_execute:
4053 case CMD_echomsg:
4054 case CMD_echoerr:
4055 case CMD_call:
4056 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004057 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 break;
4059
4060 case CMD_unlet:
4061 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4062 arg = xp->xp_pattern + 1;
4063 xp->xp_context = EXPAND_USER_VARS;
4064 xp->xp_pattern = arg;
4065 break;
4066
4067 case CMD_function:
4068 case CMD_delfunction:
4069 xp->xp_context = EXPAND_USER_FUNC;
4070 xp->xp_pattern = arg;
4071 break;
4072
4073 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004074 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 break;
4076#endif
4077 case CMD_highlight:
4078 set_context_in_highlight_cmd(xp, arg);
4079 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004080#ifdef FEAT_CSCOPE
4081 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004082 case CMD_lcscope:
4083 case CMD_scscope:
4084 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004085 break;
4086#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004087#ifdef FEAT_SIGNS
4088 case CMD_sign:
4089 set_context_in_sign_cmd(xp, arg);
4090 break;
4091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092#ifdef FEAT_LISTCMDS
4093 case CMD_bdelete:
4094 case CMD_bwipeout:
4095 case CMD_bunload:
4096 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4097 arg = xp->xp_pattern + 1;
4098 /*FALLTHROUGH*/
4099 case CMD_buffer:
4100 case CMD_sbuffer:
4101 case CMD_checktime:
4102 xp->xp_context = EXPAND_BUFFERS;
4103 xp->xp_pattern = arg;
4104 break;
4105#endif
4106#ifdef FEAT_USR_CMDS
4107 case CMD_USER:
4108 case CMD_USER_BUF:
4109 if (compl != EXPAND_NOTHING)
4110 {
4111 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004112 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 {
4114# ifdef FEAT_MENU
4115 if (compl == EXPAND_MENUS)
4116 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4117# endif
4118 if (compl == EXPAND_COMMANDS)
4119 return arg;
4120 if (compl == EXPAND_MAPPINGS)
4121 return set_context_in_map_cmd(xp, (char_u *)"map",
4122 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004123 /* Find start of last argument. */
4124 p = arg;
4125 while (*p)
4126 {
4127 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004128 /* argument starts after a space */
4129 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004130 else if (*p == '\\' && *(p + 1) != NUL)
4131 ++p; /* skip over escaped character */
4132 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 xp->xp_pattern = arg;
4135 }
4136 xp->xp_context = compl;
4137 }
4138 break;
4139#endif
4140 case CMD_map: case CMD_noremap:
4141 case CMD_nmap: case CMD_nnoremap:
4142 case CMD_vmap: case CMD_vnoremap:
4143 case CMD_omap: case CMD_onoremap:
4144 case CMD_imap: case CMD_inoremap:
4145 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004146 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004147 case CMD_smap: case CMD_snoremap:
4148 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004150 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 case CMD_unmap:
4152 case CMD_nunmap:
4153 case CMD_vunmap:
4154 case CMD_ounmap:
4155 case CMD_iunmap:
4156 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004157 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004158 case CMD_sunmap:
4159 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004161 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 case CMD_abbreviate: case CMD_noreabbrev:
4163 case CMD_cabbrev: case CMD_cnoreabbrev:
4164 case CMD_iabbrev: case CMD_inoreabbrev:
4165 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004166 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 case CMD_unabbreviate:
4168 case CMD_cunabbrev:
4169 case CMD_iunabbrev:
4170 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004171 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172#ifdef FEAT_MENU
4173 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4174 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4175 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4176 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4177 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4178 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4179 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4180 case CMD_tmenu: case CMD_tunmenu:
4181 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4182 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4183#endif
4184
4185 case CMD_colorscheme:
4186 xp->xp_context = EXPAND_COLORS;
4187 xp->xp_pattern = arg;
4188 break;
4189
4190 case CMD_compiler:
4191 xp->xp_context = EXPAND_COMPILER;
4192 xp->xp_pattern = arg;
4193 break;
4194
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004195 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004196 xp->xp_context = EXPAND_OWNSYNTAX;
4197 xp->xp_pattern = arg;
4198 break;
4199
4200 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004201 xp->xp_context = EXPAND_FILETYPE;
4202 xp->xp_pattern = arg;
4203 break;
4204
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004205 case CMD_packadd:
4206 xp->xp_context = EXPAND_PACKADD;
4207 xp->xp_pattern = arg;
4208 break;
4209
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4211 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4212 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004213 p = skiptowhite(arg);
4214 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 {
4216 xp->xp_context = EXPAND_LANGUAGE;
4217 xp->xp_pattern = arg;
4218 }
4219 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004220 {
4221 if ( STRNCMP(arg, "messages", p - arg) == 0
4222 || STRNCMP(arg, "ctype", p - arg) == 0
4223 || STRNCMP(arg, "time", p - arg) == 0)
4224 {
4225 xp->xp_context = EXPAND_LOCALES;
4226 xp->xp_pattern = skipwhite(p);
4227 }
4228 else
4229 xp->xp_context = EXPAND_NOTHING;
4230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 break;
4232#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004233#if defined(FEAT_PROFILE)
4234 case CMD_profile:
4235 set_context_in_profile_cmd(xp, arg);
4236 break;
4237#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004238 case CMD_behave:
4239 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004240 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004241 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004243#if defined(FEAT_CMDHIST)
4244 case CMD_history:
4245 xp->xp_context = EXPAND_HISTORY;
4246 xp->xp_pattern = arg;
4247 break;
4248#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004249#if defined(FEAT_PROFILE)
4250 case CMD_syntime:
4251 xp->xp_context = EXPAND_SYNTIME;
4252 xp->xp_pattern = arg;
4253 break;
4254#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004255
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256#endif /* FEAT_CMDL_COMPL */
4257
4258 default:
4259 break;
4260 }
4261 return NULL;
4262}
4263
4264/*
4265 * skip a range specifier of the form: addr [,addr] [;addr] ..
4266 *
4267 * Backslashed delimiters after / or ? will be skipped, and commands will
4268 * not be expanded between /'s and ?'s or after "'".
4269 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004270 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 * Returns the "cmd" pointer advanced to beyond the range.
4272 */
4273 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004274skip_range(
4275 char_u *cmd,
4276 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004278 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279
Bram Moolenaardf177f62005-02-22 08:39:57 +00004280 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 {
4282 if (*cmd == '\'')
4283 {
4284 if (*++cmd == NUL && ctx != NULL)
4285 *ctx = EXPAND_NOTHING;
4286 }
4287 else if (*cmd == '/' || *cmd == '?')
4288 {
4289 delim = *cmd++;
4290 while (*cmd != NUL && *cmd != delim)
4291 if (*cmd++ == '\\' && *cmd != NUL)
4292 ++cmd;
4293 if (*cmd == NUL && ctx != NULL)
4294 *ctx = EXPAND_NOTHING;
4295 }
4296 if (*cmd != NUL)
4297 ++cmd;
4298 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004299
4300 /* Skip ":" and white space. */
4301 while (*cmd == ':')
4302 cmd = skipwhite(cmd + 1);
4303
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 return cmd;
4305}
4306
4307/*
4308 * get a single EX address
4309 *
4310 * Set ptr to the next character after the part that was interpreted.
4311 * Set ptr to NULL when an error is encountered.
4312 *
4313 * Return MAXLNUM when no Ex address was found.
4314 */
4315 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004316get_address(
4317 exarg_T *eap UNUSED,
4318 char_u **ptr,
4319 int addr_type, /* flag: one of ADDR_LINES, ... */
4320 int skip, /* only skip the address, don't use it */
4321 int to_other_file) /* flag: may jump to other file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322{
4323 int c;
4324 int i;
4325 long n;
4326 char_u *cmd;
4327 pos_T pos;
4328 pos_T *fp;
4329 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004330 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331
4332 cmd = skipwhite(*ptr);
4333 lnum = MAXLNUM;
4334 do
4335 {
4336 switch (*cmd)
4337 {
4338 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004339 ++cmd;
4340 switch (addr_type)
4341 {
4342 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 lnum = curwin->w_cursor.lnum;
4344 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004345 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004346 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004347 break;
4348 case ADDR_ARGUMENTS:
4349 lnum = curwin->w_arg_idx + 1;
4350 break;
4351 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004352 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004353 lnum = curbuf->b_fnum;
4354 break;
4355 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004356 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004357 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004358#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004359 case ADDR_QUICKFIX:
4360 lnum = qf_get_cur_valid_idx(eap);
4361 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004362#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004363 }
4364 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365
4366 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004367 ++cmd;
4368 switch (addr_type)
4369 {
4370 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 lnum = curbuf->b_ml.ml_line_count;
4372 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004373 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004374 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004375 break;
4376 case ADDR_ARGUMENTS:
4377 lnum = ARGCOUNT;
4378 break;
4379 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004380 buf = lastbuf;
4381 while (buf->b_ml.ml_mfp == NULL)
4382 {
4383 if (buf->b_prev == NULL)
4384 break;
4385 buf = buf->b_prev;
4386 }
4387 lnum = buf->b_fnum;
4388 break;
4389 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004390 lnum = lastbuf->b_fnum;
4391 break;
4392 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004393 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004394 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004395#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004396 case ADDR_QUICKFIX:
4397 lnum = qf_get_size(eap);
4398 if (lnum == 0)
4399 lnum = 1;
4400 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004401#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004402 }
4403 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404
4405 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004406 if (*++cmd == NUL)
4407 {
4408 cmd = NULL;
4409 goto error;
4410 }
4411 if (addr_type != ADDR_LINES)
4412 {
4413 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004414 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004415 goto error;
4416 }
4417 if (skip)
4418 ++cmd;
4419 else
4420 {
4421 /* Only accept a mark in another file when it is
4422 * used by itself: ":'M". */
4423 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4424 ++cmd;
4425 if (fp == (pos_T *)-1)
4426 /* Jumped to another file. */
4427 lnum = curwin->w_cursor.lnum;
4428 else
4429 {
4430 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 {
4432 cmd = NULL;
4433 goto error;
4434 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004435 lnum = fp->lnum;
4436 }
4437 }
4438 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439
4440 case '/':
4441 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004442 c = *cmd++;
4443 if (addr_type != ADDR_LINES)
4444 {
4445 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004446 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004447 goto error;
4448 }
4449 if (skip) /* skip "/pat/" */
4450 {
4451 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4452 if (*cmd == c)
4453 ++cmd;
4454 }
4455 else
4456 {
4457 pos = curwin->w_cursor; /* save curwin->w_cursor */
4458 /*
4459 * When '/' or '?' follows another address, start
4460 * from there.
4461 */
4462 if (lnum != MAXLNUM)
4463 curwin->w_cursor.lnum = lnum;
4464 /*
4465 * Start a forward search at the end of the line.
4466 * Start a backward search at the start of the line.
4467 * This makes sure we never match in the current
4468 * line, and can match anywhere in the
4469 * next/previous line.
4470 */
4471 if (c == '/')
4472 curwin->w_cursor.col = MAXCOL;
4473 else
4474 curwin->w_cursor.col = 0;
4475 searchcmdlen = 0;
4476 if (!do_search(NULL, c, cmd, 1L,
4477 SEARCH_HIS | SEARCH_MSG, NULL))
4478 {
4479 curwin->w_cursor = pos;
4480 cmd = NULL;
4481 goto error;
4482 }
4483 lnum = curwin->w_cursor.lnum;
4484 curwin->w_cursor = pos;
4485 /* adjust command string pointer */
4486 cmd += searchcmdlen;
4487 }
4488 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489
4490 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004491 ++cmd;
4492 if (addr_type != ADDR_LINES)
4493 {
4494 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004495 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004496 goto error;
4497 }
4498 if (*cmd == '&')
4499 i = RE_SUBST;
4500 else if (*cmd == '?' || *cmd == '/')
4501 i = RE_SEARCH;
4502 else
4503 {
4504 EMSG(_(e_backslash));
4505 cmd = NULL;
4506 goto error;
4507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004509 if (!skip)
4510 {
4511 /*
4512 * When search follows another address, start from
4513 * there.
4514 */
4515 if (lnum != MAXLNUM)
4516 pos.lnum = lnum;
4517 else
4518 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004520 /*
4521 * Start the search just like for the above
4522 * do_search().
4523 */
4524 if (*cmd != '?')
4525 pos.col = MAXCOL;
4526 else
4527 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004528#ifdef FEAT_VIRTUALEDIT
4529 pos.coladd = 0;
4530#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004531 if (searchit(curwin, curbuf, &pos,
4532 *cmd == '?' ? BACKWARD : FORWARD,
4533 (char_u *)"", 1L, SEARCH_MSG,
4534 i, (linenr_T)0, NULL) != FAIL)
4535 lnum = pos.lnum;
4536 else
4537 {
4538 cmd = NULL;
4539 goto error;
4540 }
4541 }
4542 ++cmd;
4543 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544
4545 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004546 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4547 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 }
4549
4550 for (;;)
4551 {
4552 cmd = skipwhite(cmd);
4553 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4554 break;
4555
4556 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004557 {
4558 switch (addr_type)
4559 {
4560 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004561 /* "+1" is same as ".+1" */
4562 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004563 break;
4564 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004565 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004566 break;
4567 case ADDR_ARGUMENTS:
4568 lnum = curwin->w_arg_idx + 1;
4569 break;
4570 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004571 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004572 lnum = curbuf->b_fnum;
4573 break;
4574 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004575 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004576 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004577#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004578 case ADDR_QUICKFIX:
4579 lnum = qf_get_cur_valid_idx(eap);
4580 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004581#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004582 }
4583 }
4584
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 if (VIM_ISDIGIT(*cmd))
4586 i = '+'; /* "number" is same as "+number" */
4587 else
4588 i = *cmd++;
4589 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4590 n = 1;
4591 else
4592 n = getdigits(&cmd);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004593 if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004594 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004595 lnum = compute_buffer_local_count(
4596 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004597 else if (i == '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 lnum -= n;
4599 else
4600 lnum += n;
4601 }
4602 } while (*cmd == '/' || *cmd == '?');
4603
4604error:
4605 *ptr = cmd;
4606 return lnum;
4607}
4608
4609/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004610 * Get flags from an Ex command argument.
4611 */
4612 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004613get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004614{
4615 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4616 {
4617 if (*eap->arg == 'l')
4618 eap->flags |= EXFLAG_LIST;
4619 else if (*eap->arg == 'p')
4620 eap->flags |= EXFLAG_PRINT;
4621 else
4622 eap->flags |= EXFLAG_NR;
4623 eap->arg = skipwhite(eap->arg + 1);
4624 }
4625}
4626
4627/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 * Function called for command which is Not Implemented. NI!
4629 */
4630 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004631ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632{
4633 if (!eap->skip)
4634 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4635}
4636
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004637#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638/*
4639 * Function called for script command which is Not Implemented. NI!
4640 * Skips over ":perl <<EOF" constructs.
4641 */
4642 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004643ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644{
4645 if (!eap->skip)
4646 ex_ni(eap);
4647 else
4648 vim_free(script_get(eap, eap->arg));
4649}
4650#endif
4651
4652/*
4653 * Check range in Ex command for validity.
4654 * Return NULL when valid, error message when invalid.
4655 */
4656 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004657invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004659 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 if ( eap->line1 < 0
4661 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004662 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004664
4665 if (eap->argt & RANGE)
4666 {
4667 switch(eap->addr_type)
4668 {
4669 case ADDR_LINES:
4670 if (!(eap->argt & NOTADR)
4671 && eap->line2 > curbuf->b_ml.ml_line_count
4672#ifdef FEAT_DIFF
4673 + (eap->cmdidx == CMD_diffget)
4674#endif
4675 )
4676 return (char_u *)_(e_invrange);
4677 break;
4678 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004679 /* add 1 if ARGCOUNT is 0 */
4680 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004681 return (char_u *)_(e_invrange);
4682 break;
4683 case ADDR_BUFFERS:
4684 if (eap->line1 < firstbuf->b_fnum
4685 || eap->line2 > lastbuf->b_fnum)
4686 return (char_u *)_(e_invrange);
4687 break;
4688 case ADDR_LOADED_BUFFERS:
4689 buf = firstbuf;
4690 while (buf->b_ml.ml_mfp == NULL)
4691 {
4692 if (buf->b_next == NULL)
4693 return (char_u *)_(e_invrange);
4694 buf = buf->b_next;
4695 }
4696 if (eap->line1 < buf->b_fnum)
4697 return (char_u *)_(e_invrange);
4698 buf = lastbuf;
4699 while (buf->b_ml.ml_mfp == NULL)
4700 {
4701 if (buf->b_prev == NULL)
4702 return (char_u *)_(e_invrange);
4703 buf = buf->b_prev;
4704 }
4705 if (eap->line2 > buf->b_fnum)
4706 return (char_u *)_(e_invrange);
4707 break;
4708 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004709 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004710 return (char_u *)_(e_invrange);
4711 break;
4712 case ADDR_TABS:
4713 if (eap->line2 > LAST_TAB_NR)
4714 return (char_u *)_(e_invrange);
4715 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004716#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004717 case ADDR_QUICKFIX:
4718 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4719 return (char_u *)_(e_invrange);
4720 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004721#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004722 }
4723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 return NULL;
4725}
4726
4727/*
4728 * Correct the range for zero line number, if required.
4729 */
4730 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004731correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732{
4733 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4734 {
4735 if (eap->line1 == 0)
4736 eap->line1 = 1;
4737 if (eap->line2 == 0)
4738 eap->line2 = 1;
4739 }
4740}
4741
Bram Moolenaar748bf032005-02-02 23:04:36 +00004742#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004743static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004744
4745/*
4746 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4747 * pattern. Otherwise return eap->arg.
4748 */
4749 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004750skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004751{
4752 char_u *p = eap->arg;
4753
Bram Moolenaara37420f2006-02-04 22:37:47 +00004754 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4755 || eap->cmdidx == CMD_vimgrepadd
4756 || eap->cmdidx == CMD_lvimgrepadd
4757 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004758 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004759 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004760 if (p == NULL)
4761 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004762 }
4763 return p;
4764}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004765
4766/*
4767 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4768 * in the command line, so that things like % get expanded.
4769 */
4770 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004771replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004772{
4773 char_u *new_cmdline;
4774 char_u *program;
4775 char_u *pos;
4776 char_u *ptr;
4777 int len;
4778 int i;
4779
4780 /*
4781 * Don't do it when ":vimgrep" is used for ":grep".
4782 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004783 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4784 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4785 || eap->cmdidx == CMD_grepadd
4786 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004787 && !grep_internal(eap->cmdidx))
4788 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004789 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4790 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004791 {
4792 if (*curbuf->b_p_gp == NUL)
4793 program = p_gp;
4794 else
4795 program = curbuf->b_p_gp;
4796 }
4797 else
4798 {
4799 if (*curbuf->b_p_mp == NUL)
4800 program = p_mp;
4801 else
4802 program = curbuf->b_p_mp;
4803 }
4804
4805 p = skipwhite(p);
4806
4807 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4808 {
4809 /* replace $* by given arguments */
4810 i = 1;
4811 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4812 ++i;
4813 len = (int)STRLEN(p);
4814 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4815 if (new_cmdline == NULL)
4816 return NULL; /* out of memory */
4817 ptr = new_cmdline;
4818 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4819 {
4820 i = (int)(pos - program);
4821 STRNCPY(ptr, program, i);
4822 STRCPY(ptr += i, p);
4823 ptr += len;
4824 program = pos + 2;
4825 }
4826 STRCPY(ptr, program);
4827 }
4828 else
4829 {
4830 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4831 if (new_cmdline == NULL)
4832 return NULL; /* out of memory */
4833 STRCPY(new_cmdline, program);
4834 STRCAT(new_cmdline, " ");
4835 STRCAT(new_cmdline, p);
4836 }
4837 msg_make(p);
4838
4839 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4840 vim_free(*cmdlinep);
4841 *cmdlinep = new_cmdline;
4842 p = new_cmdline;
4843 }
4844 return p;
4845}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004846#endif
4847
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848/*
4849 * Expand file name in Ex command argument.
4850 * Return FAIL for failure, OK otherwise.
4851 */
4852 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004853expand_filename(
4854 exarg_T *eap,
4855 char_u **cmdlinep,
4856 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857{
4858 int has_wildcards; /* need to expand wildcards */
4859 char_u *repl;
4860 int srclen;
4861 char_u *p;
4862 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004863 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864
Bram Moolenaar748bf032005-02-02 23:04:36 +00004865#ifdef FEAT_QUICKFIX
4866 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4867 p = skip_grep_pat(eap);
4868#else
4869 p = eap->arg;
4870#endif
4871
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 /*
4873 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4874 * the file name contains a wildcard it should not cause expanding.
4875 * (it will be expanded anyway if there is a wildcard before replacing).
4876 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004877 has_wildcards = mch_has_wildcard(p);
4878 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004880#ifdef FEAT_EVAL
4881 /* Skip over `=expr`, wildcards in it are not expanded. */
4882 if (p[0] == '`' && p[1] == '=')
4883 {
4884 p += 2;
4885 (void)skip_expr(&p);
4886 if (*p == '`')
4887 ++p;
4888 continue;
4889 }
4890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 /*
4892 * Quick check if this cannot be the start of a special string.
4893 * Also removes backslash before '%', '#' and '<'.
4894 */
4895 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4896 {
4897 ++p;
4898 continue;
4899 }
4900
4901 /*
4902 * Try to find a match at this position.
4903 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004904 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4905 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 if (*errormsgp != NULL) /* error detected */
4907 return FAIL;
4908 if (repl == NULL) /* no match found */
4909 {
4910 p += srclen;
4911 continue;
4912 }
4913
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004914 /* Wildcards won't be expanded below, the replacement is taken
4915 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4916 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4917 {
4918 char_u *l = repl;
4919
4920 repl = expand_env_save(repl);
4921 vim_free(l);
4922 }
4923
Bram Moolenaar63b92542007-03-27 14:57:09 +00004924 /* Need to escape white space et al. with a backslash.
4925 * Don't do this for:
4926 * - replacement that already has been escaped: "##"
4927 * - shell commands (may have to use quotes instead).
4928 * - non-unix systems when there is a single argument (spaces don't
4929 * separate arguments then).
4930 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004932 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 && eap->cmdidx != CMD_bang
4934 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004935 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004937 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004939 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940#ifndef UNIX
4941 && !(eap->argt & NOSPC)
4942#endif
4943 )
4944 {
4945 char_u *l;
4946#ifdef BACKSLASH_IN_FILENAME
4947 /* Don't escape a backslash here, because rem_backslash() doesn't
4948 * remove it later. */
4949 static char_u *nobslash = (char_u *)" \t\"|";
4950# define ESCAPE_CHARS nobslash
4951#else
4952# define ESCAPE_CHARS escape_chars
4953#endif
4954
4955 for (l = repl; *l; ++l)
4956 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4957 {
4958 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4959 if (l != NULL)
4960 {
4961 vim_free(repl);
4962 repl = l;
4963 }
4964 break;
4965 }
4966 }
4967
4968 /* For a shell command a '!' must be escaped. */
4969 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004970 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 {
4972 char_u *l;
4973
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004974 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 if (l != NULL)
4976 {
4977 vim_free(repl);
4978 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 }
4980 }
4981
4982 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4983 vim_free(repl);
4984 if (p == NULL)
4985 return FAIL;
4986 }
4987
4988 /*
4989 * One file argument: Expand wildcards.
4990 * Don't do this with ":r !command" or ":w !command".
4991 */
4992 if ((eap->argt & NOSPC) && !eap->usefilter)
4993 {
4994 /*
4995 * May do this twice:
4996 * 1. Replace environment variables.
4997 * 2. Replace any other wildcards, remove backslashes.
4998 */
4999 for (n = 1; n <= 2; ++n)
5000 {
5001 if (n == 2)
5002 {
5003#ifdef UNIX
5004 /*
5005 * Only for Unix we check for more than one file name.
5006 * For other systems spaces are considered to be part
5007 * of the file name.
5008 * Only check here if there is no wildcard, otherwise
5009 * ExpandOne() will check for errors. This allows
5010 * ":e `ls ve*.c`" on Unix.
5011 */
5012 if (!has_wildcards)
5013 for (p = eap->arg; *p; ++p)
5014 {
5015 /* skip escaped characters */
5016 if (p[1] && (*p == '\\' || *p == Ctrl_V))
5017 ++p;
5018 else if (vim_iswhite(*p))
5019 {
5020 *errormsgp = (char_u *)_("E172: Only one file name allowed");
5021 return FAIL;
5022 }
5023 }
5024#endif
5025
5026 /*
5027 * Halve the number of backslashes (this is Vi compatible).
5028 * For Unix and OS/2, when wildcards are expanded, this is
5029 * done by ExpandOne() below.
5030 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005031#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 if (!has_wildcards)
5033#endif
5034 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 }
5036
5037 if (has_wildcards)
5038 {
5039 if (n == 1)
5040 {
5041 /*
5042 * First loop: May expand environment variables. This
5043 * can be done much faster with expand_env() than with
5044 * something else (e.g., calling a shell).
5045 * After expanding environment variables, check again
5046 * if there are still wildcards present.
5047 */
5048 if (vim_strchr(eap->arg, '$') != NULL
5049 || vim_strchr(eap->arg, '~') != NULL)
5050 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005051 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005052 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 has_wildcards = mch_has_wildcard(NameBuff);
5054 p = NameBuff;
5055 }
5056 else
5057 p = NULL;
5058 }
5059 else /* n == 2 */
5060 {
5061 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005062 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063
5064 ExpandInit(&xpc);
5065 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005066 if (p_wic)
5067 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005069 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 if (p == NULL)
5071 return FAIL;
5072 }
5073 if (p != NULL)
5074 {
5075 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5076 p, cmdlinep);
5077 if (n == 2) /* p came from ExpandOne() */
5078 vim_free(p);
5079 }
5080 }
5081 }
5082 }
5083 return OK;
5084}
5085
5086/*
5087 * Replace part of the command line, keeping eap->cmd, eap->arg and
5088 * eap->nextcmd correct.
5089 * "src" points to the part that is to be replaced, of length "srclen".
5090 * "repl" is the replacement string.
5091 * Returns a pointer to the character after the replaced string.
5092 * Returns NULL for failure.
5093 */
5094 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005095repl_cmdline(
5096 exarg_T *eap,
5097 char_u *src,
5098 int srclen,
5099 char_u *repl,
5100 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101{
5102 int len;
5103 int i;
5104 char_u *new_cmdline;
5105
5106 /*
5107 * The new command line is build in new_cmdline[].
5108 * First allocate it.
5109 * Careful: a "+cmd" argument may have been NUL terminated.
5110 */
5111 len = (int)STRLEN(repl);
5112 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005113 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5115 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5116 return NULL; /* out of memory! */
5117
5118 /*
5119 * Copy the stuff before the expanded part.
5120 * Copy the expanded stuff.
5121 * Copy what came after the expanded part.
5122 * Copy the next commands, if there are any.
5123 */
5124 i = (int)(src - *cmdlinep); /* length of part before match */
5125 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005126
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 mch_memmove(new_cmdline + i, repl, (size_t)len);
5128 i += len; /* remember the end of the string */
5129 STRCPY(new_cmdline + i, src + srclen);
5130 src = new_cmdline + i; /* remember where to continue */
5131
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005132 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 {
5134 i = (int)STRLEN(new_cmdline) + 1;
5135 STRCPY(new_cmdline + i, eap->nextcmd);
5136 eap->nextcmd = new_cmdline + i;
5137 }
5138 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5139 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5140 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5141 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5142 vim_free(*cmdlinep);
5143 *cmdlinep = new_cmdline;
5144
5145 return src;
5146}
5147
5148/*
5149 * Check for '|' to separate commands and '"' to start comments.
5150 */
5151 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005152separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153{
5154 char_u *p;
5155
Bram Moolenaar86b68352004-12-27 21:59:20 +00005156#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005157 p = skip_grep_pat(eap);
5158#else
5159 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005160#endif
5161
5162 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 {
5164 if (*p == Ctrl_V)
5165 {
5166 if (eap->argt & (USECTRLV | XFILE))
5167 ++p; /* skip CTRL-V and next char */
5168 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005169 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005170 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 if (*p == NUL) /* stop at NUL after CTRL-V */
5172 break;
5173 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005174
5175#ifdef FEAT_EVAL
5176 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005177 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005178 {
5179 p += 2;
5180 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005181 }
5182#endif
5183
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 /* Check for '"': start of comment or '|': next command */
5185 /* :@" and :*" do not start a comment!
5186 * :redir @" doesn't either. */
5187 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5188 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5189 || p != eap->arg)
5190 && (eap->cmdidx != CMD_redir
5191 || p != eap->arg + 1 || p[-1] != '@'))
5192 || *p == '|' || *p == '\n')
5193 {
5194 /*
5195 * We remove the '\' before the '|', unless USECTRLV is used
5196 * AND 'b' is present in 'cpoptions'.
5197 */
5198 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5199 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5200 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005201 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 --p;
5203 }
5204 else
5205 {
5206 eap->nextcmd = check_nextcmd(p);
5207 *p = NUL;
5208 break;
5209 }
5210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005212
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5214 del_trailing_spaces(eap->arg);
5215}
5216
5217/*
5218 * get + command from ex argument
5219 */
5220 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005221getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222{
5223 char_u *arg = *argp;
5224 char_u *command = NULL;
5225
5226 if (*arg == '+') /* +[command] */
5227 {
5228 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005229 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 command = dollar_command;
5231 else
5232 {
5233 command = arg;
5234 arg = skip_cmd_arg(command, TRUE);
5235 if (*arg != NUL)
5236 *arg++ = NUL; /* terminate command with NUL */
5237 }
5238
5239 arg = skipwhite(arg); /* skip over spaces */
5240 *argp = arg;
5241 }
5242 return command;
5243}
5244
5245/*
5246 * Find end of "+command" argument. Skip over "\ " and "\\".
5247 */
5248 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005249skip_cmd_arg(
5250 char_u *p,
5251 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252{
5253 while (*p && !vim_isspace(*p))
5254 {
5255 if (*p == '\\' && p[1] != NUL)
5256 {
5257 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005258 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 else
5260 ++p;
5261 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005262 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 }
5264 return p;
5265}
5266
5267/*
5268 * Get "++opt=arg" argument.
5269 * Return FAIL or OK.
5270 */
5271 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005272getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273{
5274 char_u *arg = eap->arg + 2;
5275 int *pp = NULL;
5276#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005277 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 char_u *p;
5279#endif
5280
5281 /* ":edit ++[no]bin[ary] file" */
5282 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5283 {
5284 if (*arg == 'n')
5285 {
5286 arg += 2;
5287 eap->force_bin = FORCE_NOBIN;
5288 }
5289 else
5290 eap->force_bin = FORCE_BIN;
5291 if (!checkforcmd(&arg, "binary", 3))
5292 return FAIL;
5293 eap->arg = skipwhite(arg);
5294 return OK;
5295 }
5296
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005297 /* ":read ++edit file" */
5298 if (STRNCMP(arg, "edit", 4) == 0)
5299 {
5300 eap->read_edit = TRUE;
5301 eap->arg = skipwhite(arg + 4);
5302 return OK;
5303 }
5304
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 if (STRNCMP(arg, "ff", 2) == 0)
5306 {
5307 arg += 2;
5308 pp = &eap->force_ff;
5309 }
5310 else if (STRNCMP(arg, "fileformat", 10) == 0)
5311 {
5312 arg += 10;
5313 pp = &eap->force_ff;
5314 }
5315#ifdef FEAT_MBYTE
5316 else if (STRNCMP(arg, "enc", 3) == 0)
5317 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005318 if (STRNCMP(arg, "encoding", 8) == 0)
5319 arg += 8;
5320 else
5321 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 pp = &eap->force_enc;
5323 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005324 else if (STRNCMP(arg, "bad", 3) == 0)
5325 {
5326 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005327 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329#endif
5330
5331 if (pp == NULL || *arg != '=')
5332 return FAIL;
5333
5334 ++arg;
5335 *pp = (int)(arg - eap->cmd);
5336 arg = skip_cmd_arg(arg, FALSE);
5337 eap->arg = skipwhite(arg);
5338 *arg = NUL;
5339
5340#ifdef FEAT_MBYTE
5341 if (pp == &eap->force_ff)
5342 {
5343#endif
5344 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5345 return FAIL;
5346#ifdef FEAT_MBYTE
5347 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005348 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 {
5350 /* Make 'fileencoding' lower case. */
5351 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5352 *p = TOLOWER_ASC(*p);
5353 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005354 else
5355 {
5356 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5357 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005358 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005359 if (STRICMP(p, "keep") == 0)
5360 eap->bad_char = BAD_KEEP;
5361 else if (STRICMP(p, "drop") == 0)
5362 eap->bad_char = BAD_DROP;
5363 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5364 eap->bad_char = *p;
5365 else
5366 return FAIL;
5367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368#endif
5369
5370 return OK;
5371}
5372
5373/*
5374 * ":abbreviate" and friends.
5375 */
5376 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005377ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378{
5379 do_exmap(eap, TRUE); /* almost the same as mapping */
5380}
5381
5382/*
5383 * ":map" and friends.
5384 */
5385 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005386ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387{
5388 /*
5389 * If we are sourcing .exrc or .vimrc in current directory we
5390 * print the mappings for security reasons.
5391 */
5392 if (secure)
5393 {
5394 secure = 2;
5395 msg_outtrans(eap->cmd);
5396 msg_putchar('\n');
5397 }
5398 do_exmap(eap, FALSE);
5399}
5400
5401/*
5402 * ":unmap" and friends.
5403 */
5404 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005405ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406{
5407 do_exmap(eap, FALSE);
5408}
5409
5410/*
5411 * ":mapclear" and friends.
5412 */
5413 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005414ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415{
5416 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5417}
5418
5419/*
5420 * ":abclear" and friends.
5421 */
5422 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005423ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424{
5425 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5426}
5427
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005428#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005430ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431{
5432 /*
5433 * Disallow auto commands from .exrc and .vimrc in current
5434 * directory for security reasons.
5435 */
5436 if (secure)
5437 {
5438 secure = 2;
5439 eap->errmsg = e_curdir;
5440 }
5441 else if (eap->cmdidx == CMD_autocmd)
5442 do_autocmd(eap->arg, eap->forceit);
5443 else
5444 do_augroup(eap->arg, eap->forceit);
5445}
5446
5447/*
5448 * ":doautocmd": Apply the automatic commands to the current buffer.
5449 */
5450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005451ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005453 char_u *arg = eap->arg;
5454 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005455 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005456
Bram Moolenaar1610d052016-06-09 22:53:01 +02005457 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5458 /* Only when there is no <nomodeline>. */
5459 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005460 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461}
5462#endif
5463
5464#ifdef FEAT_LISTCMDS
5465/*
5466 * :[N]bunload[!] [N] [bufname] unload buffer
5467 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5468 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5469 */
5470 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005471ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472{
5473 eap->errmsg = do_bufdel(
5474 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5475 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5476 : DOBUF_UNLOAD, eap->arg,
5477 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5478}
5479
5480/*
5481 * :[N]buffer [N] to buffer N
5482 * :[N]sbuffer [N] to buffer N
5483 */
5484 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005485ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486{
5487 if (*eap->arg)
5488 eap->errmsg = e_trailing;
5489 else
5490 {
5491 if (eap->addr_count == 0) /* default is current buffer */
5492 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5493 else
5494 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005495 if (eap->do_ecmd_cmd != NULL)
5496 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 }
5498}
5499
5500/*
5501 * :[N]bmodified [N] to next mod. buffer
5502 * :[N]sbmodified [N] to next mod. buffer
5503 */
5504 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005505ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506{
5507 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005508 if (eap->do_ecmd_cmd != NULL)
5509 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510}
5511
5512/*
5513 * :[N]bnext [N] to next buffer
5514 * :[N]sbnext [N] split and to next buffer
5515 */
5516 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005517ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518{
5519 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005520 if (eap->do_ecmd_cmd != NULL)
5521 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522}
5523
5524/*
5525 * :[N]bNext [N] to previous buffer
5526 * :[N]bprevious [N] to previous buffer
5527 * :[N]sbNext [N] split and to previous buffer
5528 * :[N]sbprevious [N] split and to previous buffer
5529 */
5530 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005531ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532{
5533 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005534 if (eap->do_ecmd_cmd != NULL)
5535 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536}
5537
5538/*
5539 * :brewind to first buffer
5540 * :bfirst to first buffer
5541 * :sbrewind split and to first buffer
5542 * :sbfirst split and to first buffer
5543 */
5544 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005545ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546{
5547 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005548 if (eap->do_ecmd_cmd != NULL)
5549 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550}
5551
5552/*
5553 * :blast to last buffer
5554 * :sblast split and to last buffer
5555 */
5556 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005557ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558{
5559 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005560 if (eap->do_ecmd_cmd != NULL)
5561 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562}
5563#endif
5564
5565 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005566ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567{
5568 return (c == NUL || c == '|' || c == '"' || c == '\n');
5569}
5570
5571#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5572 || defined(PROTO)
5573/*
5574 * Return the next command, after the first '|' or '\n'.
5575 * Return NULL if not found.
5576 */
5577 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005578find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579{
5580 while (*p != '|' && *p != '\n')
5581 {
5582 if (*p == NUL)
5583 return NULL;
5584 ++p;
5585 }
5586 return (p + 1);
5587}
5588#endif
5589
5590/*
5591 * Check if *p is a separator between Ex commands.
5592 * Return NULL if it isn't, (p + 1) if it is.
5593 */
5594 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005595check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596{
5597 p = skipwhite(p);
5598 if (*p == '|' || *p == '\n')
5599 return (p + 1);
5600 else
5601 return NULL;
5602}
5603
5604/*
5605 * - if there are more files to edit
5606 * - and this is the last window
5607 * - and forceit not used
5608 * - and not repeated twice on a row
5609 * return FAIL and give error message if 'message' TRUE
5610 * return OK otherwise
5611 */
5612 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005613check_more(
5614 int message, /* when FALSE check only, no messages */
5615 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616{
5617 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5618
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005619 if (!forceit && only_one_window()
5620 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 {
5622 if (message)
5623 {
5624#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5625 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5626 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005627 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628
5629 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005630 vim_strncpy(buff,
5631 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005632 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005634 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 _("%d more files to edit. Quit anyway?"), n);
5636 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5637 return OK;
5638 return FAIL;
5639 }
5640#endif
5641 if (n == 1)
5642 EMSG(_("E173: 1 more file to edit"));
5643 else
5644 EMSGN(_("E173: %ld more files to edit"), n);
5645 quitmore = 2; /* next try to quit is allowed */
5646 }
5647 return FAIL;
5648 }
5649 return OK;
5650}
5651
5652#ifdef FEAT_CMDL_COMPL
5653/*
5654 * Function given to ExpandGeneric() to obtain the list of command names.
5655 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005657get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658{
5659 if (idx >= (int)CMD_SIZE)
5660# ifdef FEAT_USR_CMDS
5661 return get_user_command_name(idx);
5662# else
5663 return NULL;
5664# endif
5665 return cmdnames[idx].cmd_name;
5666}
5667#endif
5668
5669#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005670static 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);
5671static void uc_list(char_u *name, size_t name_len);
5672static 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);
5673static char_u *uc_split_args(char_u *arg, size_t *lenp);
5674static 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 +00005675
5676 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005677uc_add_command(
5678 char_u *name,
5679 size_t name_len,
5680 char_u *rep,
5681 long argt,
5682 long def,
5683 int flags,
5684 int compl,
5685 char_u *compl_arg,
5686 int addr_type,
5687 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688{
5689 ucmd_T *cmd = NULL;
5690 char_u *p;
5691 int i;
5692 int cmp = 1;
5693 char_u *rep_buf = NULL;
5694 garray_T *gap;
5695
Bram Moolenaar9c102382006-05-03 21:26:49 +00005696 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 if (rep_buf == NULL)
5698 {
5699 /* Can't replace termcodes - try using the string as is */
5700 rep_buf = vim_strsave(rep);
5701
5702 /* Give up if out of memory */
5703 if (rep_buf == NULL)
5704 return FAIL;
5705 }
5706
5707 /* get address of growarray: global or in curbuf */
5708 if (flags & UC_BUFFER)
5709 {
5710 gap = &curbuf->b_ucmds;
5711 if (gap->ga_itemsize == 0)
5712 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5713 }
5714 else
5715 gap = &ucmds;
5716
5717 /* Search for the command in the already defined commands. */
5718 for (i = 0; i < gap->ga_len; ++i)
5719 {
5720 size_t len;
5721
5722 cmd = USER_CMD_GA(gap, i);
5723 len = STRLEN(cmd->uc_name);
5724 cmp = STRNCMP(name, cmd->uc_name, name_len);
5725 if (cmp == 0)
5726 {
5727 if (name_len < len)
5728 cmp = -1;
5729 else if (name_len > len)
5730 cmp = 1;
5731 }
5732
5733 if (cmp == 0)
5734 {
5735 if (!force)
5736 {
5737 EMSG(_("E174: Command already exists: add ! to replace it"));
5738 goto fail;
5739 }
5740
5741 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005742 cmd->uc_rep = NULL;
5743#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5744 vim_free(cmd->uc_compl_arg);
5745 cmd->uc_compl_arg = NULL;
5746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 break;
5748 }
5749
5750 /* Stop as soon as we pass the name to add */
5751 if (cmp < 0)
5752 break;
5753 }
5754
5755 /* Extend the array unless we're replacing an existing command */
5756 if (cmp != 0)
5757 {
5758 if (ga_grow(gap, 1) != OK)
5759 goto fail;
5760 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5761 goto fail;
5762
5763 cmd = USER_CMD_GA(gap, i);
5764 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5765
5766 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767
5768 cmd->uc_name = p;
5769 }
5770
5771 cmd->uc_rep = rep_buf;
5772 cmd->uc_argt = argt;
5773 cmd->uc_def = def;
5774 cmd->uc_compl = compl;
5775#ifdef FEAT_EVAL
5776 cmd->uc_scriptID = current_SID;
5777# ifdef FEAT_CMDL_COMPL
5778 cmd->uc_compl_arg = compl_arg;
5779# endif
5780#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005781 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782
5783 return OK;
5784
5785fail:
5786 vim_free(rep_buf);
5787#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5788 vim_free(compl_arg);
5789#endif
5790 return FAIL;
5791}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005794#if defined(FEAT_USR_CMDS)
5795static struct
5796{
5797 int expand;
5798 char *name;
5799} addr_type_complete[] =
5800{
5801 {ADDR_ARGUMENTS, "arguments"},
5802 {ADDR_LINES, "lines"},
5803 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5804 {ADDR_TABS, "tabs"},
5805 {ADDR_BUFFERS, "buffers"},
5806 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005807 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005808 {-1, NULL}
5809};
5810#endif
5811
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005812#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813/*
5814 * List of names for completion for ":command" with the EXPAND_ flag.
5815 * Must be alphabetical for completion.
5816 */
5817static struct
5818{
5819 int expand;
5820 char *name;
5821} command_complete[] =
5822{
5823 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005824 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005826 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005828 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005829#if defined(FEAT_CSCOPE)
5830 {EXPAND_CSCOPE, "cscope"},
5831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5833 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005834 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835#endif
5836 {EXPAND_DIRECTORIES, "dir"},
5837 {EXPAND_ENV_VARS, "environment"},
5838 {EXPAND_EVENTS, "event"},
5839 {EXPAND_EXPRESSION, "expression"},
5840 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005841 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005842 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 {EXPAND_FUNCTIONS, "function"},
5844 {EXPAND_HELP, "help"},
5845 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005846#if defined(FEAT_CMDHIST)
5847 {EXPAND_HISTORY, "history"},
5848#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005849#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005850 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005851 {EXPAND_LOCALES, "locale"},
5852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 {EXPAND_MAPPINGS, "mapping"},
5854 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005855 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005856#if defined(FEAT_PROFILE)
5857 {EXPAND_SYNTIME, "syntime"},
5858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005860 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005861 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005862#if defined(FEAT_SIGNS)
5863 {EXPAND_SIGN, "sign"},
5864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 {EXPAND_TAGS, "tag"},
5866 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005867 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 {EXPAND_USER_VARS, "var"},
5869 {0, NULL}
5870};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005873#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005875uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876{
5877 int i, j;
5878 int found = FALSE;
5879 ucmd_T *cmd;
5880 int len;
5881 long a;
5882 garray_T *gap;
5883
5884 gap = &curbuf->b_ucmds;
5885 for (;;)
5886 {
5887 for (i = 0; i < gap->ga_len; ++i)
5888 {
5889 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005890 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891
5892 /* Skip commands which don't match the requested prefix */
5893 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5894 continue;
5895
5896 /* Put out the title first time */
5897 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005898 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 found = TRUE;
5900 msg_putchar('\n');
5901 if (got_int)
5902 break;
5903
5904 /* Special cases */
5905 msg_putchar(a & BANG ? '!' : ' ');
5906 msg_putchar(a & REGSTR ? '"' : ' ');
5907 msg_putchar(gap != &ucmds ? 'b' : ' ');
5908 msg_putchar(' ');
5909
5910 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5911 len = (int)STRLEN(cmd->uc_name) + 4;
5912
5913 do {
5914 msg_putchar(' ');
5915 ++len;
5916 } while (len < 16);
5917
5918 len = 0;
5919
5920 /* Arguments */
5921 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5922 {
5923 case 0: IObuff[len++] = '0'; break;
5924 case (EXTRA): IObuff[len++] = '*'; break;
5925 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5926 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5927 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5928 }
5929
5930 do {
5931 IObuff[len++] = ' ';
5932 } while (len < 5);
5933
5934 /* Range */
5935 if (a & (RANGE|COUNT))
5936 {
5937 if (a & COUNT)
5938 {
5939 /* -count=N */
5940 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5941 len += (int)STRLEN(IObuff + len);
5942 }
5943 else if (a & DFLALL)
5944 IObuff[len++] = '%';
5945 else if (cmd->uc_def >= 0)
5946 {
5947 /* -range=N */
5948 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5949 len += (int)STRLEN(IObuff + len);
5950 }
5951 else
5952 IObuff[len++] = '.';
5953 }
5954
5955 do {
5956 IObuff[len++] = ' ';
5957 } while (len < 11);
5958
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005959 /* Address Type */
5960 for (j = 0; addr_type_complete[j].expand != -1; ++j)
5961 if (addr_type_complete[j].expand != ADDR_LINES
5962 && addr_type_complete[j].expand == cmd->uc_addr_type)
5963 {
5964 STRCPY(IObuff + len, addr_type_complete[j].name);
5965 len += (int)STRLEN(IObuff + len);
5966 break;
5967 }
5968
5969 do {
5970 IObuff[len++] = ' ';
5971 } while (len < 21);
5972
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 /* Completion */
5974 for (j = 0; command_complete[j].expand != 0; ++j)
5975 if (command_complete[j].expand == cmd->uc_compl)
5976 {
5977 STRCPY(IObuff + len, command_complete[j].name);
5978 len += (int)STRLEN(IObuff + len);
5979 break;
5980 }
5981
5982 do {
5983 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005984 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985
5986 IObuff[len] = '\0';
5987 msg_outtrans(IObuff);
5988
5989 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005990#ifdef FEAT_EVAL
5991 if (p_verbose > 0)
5992 last_set_msg(cmd->uc_scriptID);
5993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 out_flush();
5995 ui_breakcheck();
5996 if (got_int)
5997 break;
5998 }
5999 if (gap == &ucmds || i < gap->ga_len)
6000 break;
6001 gap = &ucmds;
6002 }
6003
6004 if (!found)
6005 MSG(_("No user-defined commands found"));
6006}
6007
6008 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006009uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010{
6011 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6012 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6013 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6014 0xb9, 0x7f, 0};
6015 int i;
6016
6017 for (i = 0; fcmd[i]; ++i)
6018 IObuff[i] = fcmd[i] - 0x40;
6019 IObuff[i] = 0;
6020 return IObuff;
6021}
6022
6023 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006024uc_scan_attr(
6025 char_u *attr,
6026 size_t len,
6027 long *argt,
6028 long *def,
6029 int *flags,
6030 int *compl,
6031 char_u **compl_arg,
6032 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033{
6034 char_u *p;
6035
6036 if (len == 0)
6037 {
6038 EMSG(_("E175: No attribute specified"));
6039 return FAIL;
6040 }
6041
6042 /* First, try the simple attributes (no arguments) */
6043 if (STRNICMP(attr, "bang", len) == 0)
6044 *argt |= BANG;
6045 else if (STRNICMP(attr, "buffer", len) == 0)
6046 *flags |= UC_BUFFER;
6047 else if (STRNICMP(attr, "register", len) == 0)
6048 *argt |= REGSTR;
6049 else if (STRNICMP(attr, "bar", len) == 0)
6050 *argt |= TRLBAR;
6051 else
6052 {
6053 int i;
6054 char_u *val = NULL;
6055 size_t vallen = 0;
6056 size_t attrlen = len;
6057
6058 /* Look for the attribute name - which is the part before any '=' */
6059 for (i = 0; i < (int)len; ++i)
6060 {
6061 if (attr[i] == '=')
6062 {
6063 val = &attr[i + 1];
6064 vallen = len - i - 1;
6065 attrlen = i;
6066 break;
6067 }
6068 }
6069
6070 if (STRNICMP(attr, "nargs", attrlen) == 0)
6071 {
6072 if (vallen == 1)
6073 {
6074 if (*val == '0')
6075 /* Do nothing - this is the default */;
6076 else if (*val == '1')
6077 *argt |= (EXTRA | NOSPC | NEEDARG);
6078 else if (*val == '*')
6079 *argt |= EXTRA;
6080 else if (*val == '?')
6081 *argt |= (EXTRA | NOSPC);
6082 else if (*val == '+')
6083 *argt |= (EXTRA | NEEDARG);
6084 else
6085 goto wrong_nargs;
6086 }
6087 else
6088 {
6089wrong_nargs:
6090 EMSG(_("E176: Invalid number of arguments"));
6091 return FAIL;
6092 }
6093 }
6094 else if (STRNICMP(attr, "range", attrlen) == 0)
6095 {
6096 *argt |= RANGE;
6097 if (vallen == 1 && *val == '%')
6098 *argt |= DFLALL;
6099 else if (val != NULL)
6100 {
6101 p = val;
6102 if (*def >= 0)
6103 {
6104two_count:
6105 EMSG(_("E177: Count cannot be specified twice"));
6106 return FAIL;
6107 }
6108
6109 *def = getdigits(&p);
6110 *argt |= (ZEROR | NOTADR);
6111
6112 if (p != val + vallen || vallen == 0)
6113 {
6114invalid_count:
6115 EMSG(_("E178: Invalid default value for count"));
6116 return FAIL;
6117 }
6118 }
6119 }
6120 else if (STRNICMP(attr, "count", attrlen) == 0)
6121 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006122 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123
6124 if (val != NULL)
6125 {
6126 p = val;
6127 if (*def >= 0)
6128 goto two_count;
6129
6130 *def = getdigits(&p);
6131
6132 if (p != val + vallen)
6133 goto invalid_count;
6134 }
6135
6136 if (*def < 0)
6137 *def = 0;
6138 }
6139 else if (STRNICMP(attr, "complete", attrlen) == 0)
6140 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 if (val == NULL)
6142 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006143 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 return FAIL;
6145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006147 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6148 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006151 else if (STRNICMP(attr, "addr", attrlen) == 0)
6152 {
6153 *argt |= RANGE;
6154 if (val == NULL)
6155 {
6156 EMSG(_("E179: argument required for -addr"));
6157 return FAIL;
6158 }
6159 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6160 == FAIL)
6161 return FAIL;
6162 if (addr_type_arg != ADDR_LINES)
6163 *argt |= (ZEROR | NOTADR) ;
6164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 else
6166 {
6167 char_u ch = attr[len];
6168 attr[len] = '\0';
6169 EMSG2(_("E181: Invalid attribute: %s"), attr);
6170 attr[len] = ch;
6171 return FAIL;
6172 }
6173 }
6174
6175 return OK;
6176}
6177
Bram Moolenaara850a712009-01-28 14:42:59 +00006178/*
6179 * ":command ..."
6180 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006182ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183{
6184 char_u *name;
6185 char_u *end;
6186 char_u *p;
6187 long argt = 0;
6188 long def = -1;
6189 int flags = 0;
6190 int compl = EXPAND_NOTHING;
6191 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006192 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006194 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195
6196 p = eap->arg;
6197
6198 /* Check for attributes */
6199 while (*p == '-')
6200 {
6201 ++p;
6202 end = skiptowhite(p);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006203 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 == FAIL)
6205 return;
6206 p = skipwhite(end);
6207 }
6208
6209 /* Get the name (if any) and skip to the following argument */
6210 name = p;
6211 if (ASCII_ISALPHA(*p))
6212 while (ASCII_ISALNUM(*p))
6213 ++p;
6214 if (!ends_excmd(*p) && !vim_iswhite(*p))
6215 {
6216 EMSG(_("E182: Invalid command name"));
6217 return;
6218 }
6219 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006220 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221
6222 /* If there is nothing after the name, and no attributes were specified,
6223 * we are listing commands
6224 */
6225 p = skipwhite(end);
6226 if (!has_attr && ends_excmd(*p))
6227 {
6228 uc_list(name, end - name);
6229 }
6230 else if (!ASCII_ISUPPER(*name))
6231 {
6232 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6233 return;
6234 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006235 else if ((name_len == 1 && *name == 'X')
6236 || (name_len <= 4
6237 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6238 {
6239 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6240 return;
6241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 else
6243 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006244 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245}
6246
6247/*
6248 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 * Clear all user commands, global and for current buffer.
6250 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006251 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006252ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253{
6254 uc_clear(&ucmds);
6255 uc_clear(&curbuf->b_ucmds);
6256}
6257
6258/*
6259 * Clear all user commands for "gap".
6260 */
6261 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006262uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263{
6264 int i;
6265 ucmd_T *cmd;
6266
6267 for (i = 0; i < gap->ga_len; ++i)
6268 {
6269 cmd = USER_CMD_GA(gap, i);
6270 vim_free(cmd->uc_name);
6271 vim_free(cmd->uc_rep);
6272# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6273 vim_free(cmd->uc_compl_arg);
6274# endif
6275 }
6276 ga_clear(gap);
6277}
6278
6279 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006280ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281{
6282 int i = 0;
6283 ucmd_T *cmd = NULL;
6284 int cmp = -1;
6285 garray_T *gap;
6286
6287 gap = &curbuf->b_ucmds;
6288 for (;;)
6289 {
6290 for (i = 0; i < gap->ga_len; ++i)
6291 {
6292 cmd = USER_CMD_GA(gap, i);
6293 cmp = STRCMP(eap->arg, cmd->uc_name);
6294 if (cmp <= 0)
6295 break;
6296 }
6297 if (gap == &ucmds || cmp == 0)
6298 break;
6299 gap = &ucmds;
6300 }
6301
6302 if (cmp != 0)
6303 {
6304 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6305 return;
6306 }
6307
6308 vim_free(cmd->uc_name);
6309 vim_free(cmd->uc_rep);
6310# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6311 vim_free(cmd->uc_compl_arg);
6312# endif
6313
6314 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315
6316 if (i < gap->ga_len)
6317 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6318}
6319
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006320/*
6321 * split and quote args for <f-args>
6322 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006324uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325{
6326 char_u *buf;
6327 char_u *p;
6328 char_u *q;
6329 int len;
6330
6331 /* Precalculate length */
6332 p = arg;
6333 len = 2; /* Initial and final quotes */
6334
6335 while (*p)
6336 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006337 if (p[0] == '\\' && p[1] == '\\')
6338 {
6339 len += 2;
6340 p += 2;
6341 }
6342 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 {
6344 len += 1;
6345 p += 2;
6346 }
6347 else if (*p == '\\' || *p == '"')
6348 {
6349 len += 2;
6350 p += 1;
6351 }
6352 else if (vim_iswhite(*p))
6353 {
6354 p = skipwhite(p);
6355 if (*p == NUL)
6356 break;
6357 len += 3; /* "," */
6358 }
6359 else
6360 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006361#ifdef FEAT_MBYTE
6362 int charlen = (*mb_ptr2len)(p);
6363 len += charlen;
6364 p += charlen;
6365#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 ++len;
6367 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 }
6370 }
6371
6372 buf = alloc(len + 1);
6373 if (buf == NULL)
6374 {
6375 *lenp = 0;
6376 return buf;
6377 }
6378
6379 p = arg;
6380 q = buf;
6381 *q++ = '"';
6382 while (*p)
6383 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006384 if (p[0] == '\\' && p[1] == '\\')
6385 {
6386 *q++ = '\\';
6387 *q++ = '\\';
6388 p += 2;
6389 }
6390 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 {
6392 *q++ = p[1];
6393 p += 2;
6394 }
6395 else if (*p == '\\' || *p == '"')
6396 {
6397 *q++ = '\\';
6398 *q++ = *p++;
6399 }
6400 else if (vim_iswhite(*p))
6401 {
6402 p = skipwhite(p);
6403 if (*p == NUL)
6404 break;
6405 *q++ = '"';
6406 *q++ = ',';
6407 *q++ = '"';
6408 }
6409 else
6410 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006411 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 }
6413 }
6414 *q++ = '"';
6415 *q = 0;
6416
6417 *lenp = len;
6418 return buf;
6419}
6420
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006421 static size_t
6422add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6423{
6424 size_t result;
6425
6426 result = STRLEN(mod_str);
6427 if (*multi_mods)
6428 result += 1;
6429 if (buf != NULL)
6430 {
6431 if (*multi_mods)
6432 STRCAT(buf, " ");
6433 STRCAT(buf, mod_str);
6434 }
6435
6436 *multi_mods = 1;
6437
6438 return result;
6439}
6440
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441/*
6442 * Check for a <> code in a user command.
6443 * "code" points to the '<'. "len" the length of the <> (inclusive).
6444 * "buf" is where the result is to be added.
6445 * "split_buf" points to a buffer used for splitting, caller should free it.
6446 * "split_len" is the length of what "split_buf" contains.
6447 * Returns the length of the replacement, which has been added to "buf".
6448 * Returns -1 if there was no match, and only the "<" has been copied.
6449 */
6450 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006451uc_check_code(
6452 char_u *code,
6453 size_t len,
6454 char_u *buf,
6455 ucmd_T *cmd, /* the user command we're expanding */
6456 exarg_T *eap, /* ex arguments */
6457 char_u **split_buf,
6458 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459{
6460 size_t result = 0;
6461 char_u *p = code + 1;
6462 size_t l = len - 2;
6463 int quote = 0;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006464 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_MODS,
6465 ct_REGISTER, ct_LT, ct_NONE } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466
6467 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6468 {
6469 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6470 p += 2;
6471 l -= 2;
6472 }
6473
Bram Moolenaar371d5402006-03-20 21:47:49 +00006474 ++l;
6475 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006477 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006479 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006481 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006483 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006485 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006487 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006489 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006491 else if (STRNICMP(p, "mods>", l) == 0)
6492 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493
6494 switch (type)
6495 {
6496 case ct_ARGS:
6497 /* Simple case first */
6498 if (*eap->arg == NUL)
6499 {
6500 if (quote == 1)
6501 {
6502 result = 2;
6503 if (buf != NULL)
6504 STRCPY(buf, "''");
6505 }
6506 else
6507 result = 0;
6508 break;
6509 }
6510
6511 /* When specified there is a single argument don't split it.
6512 * Works for ":Cmd %" when % is "a b c". */
6513 if ((eap->argt & NOSPC) && quote == 2)
6514 quote = 1;
6515
6516 switch (quote)
6517 {
6518 case 0: /* No quoting, no splitting */
6519 result = STRLEN(eap->arg);
6520 if (buf != NULL)
6521 STRCPY(buf, eap->arg);
6522 break;
6523 case 1: /* Quote, but don't split */
6524 result = STRLEN(eap->arg) + 2;
6525 for (p = eap->arg; *p; ++p)
6526 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006527#ifdef FEAT_MBYTE
6528 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6529 /* DBCS can contain \ in a trail byte, skip the
6530 * double-byte character. */
6531 ++p;
6532 else
6533#endif
6534 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 ++result;
6536 }
6537
6538 if (buf != NULL)
6539 {
6540 *buf++ = '"';
6541 for (p = eap->arg; *p; ++p)
6542 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006543#ifdef FEAT_MBYTE
6544 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6545 /* DBCS can contain \ in a trail byte, copy the
6546 * double-byte character to avoid escaping. */
6547 *buf++ = *p++;
6548 else
6549#endif
6550 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 *buf++ = '\\';
6552 *buf++ = *p;
6553 }
6554 *buf = '"';
6555 }
6556
6557 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006558 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 /* This is hard, so only do it once, and cache the result */
6560 if (*split_buf == NULL)
6561 *split_buf = uc_split_args(eap->arg, split_len);
6562
6563 result = *split_len;
6564 if (buf != NULL && result != 0)
6565 STRCPY(buf, *split_buf);
6566
6567 break;
6568 }
6569 break;
6570
6571 case ct_BANG:
6572 result = eap->forceit ? 1 : 0;
6573 if (quote)
6574 result += 2;
6575 if (buf != NULL)
6576 {
6577 if (quote)
6578 *buf++ = '"';
6579 if (eap->forceit)
6580 *buf++ = '!';
6581 if (quote)
6582 *buf = '"';
6583 }
6584 break;
6585
6586 case ct_LINE1:
6587 case ct_LINE2:
6588 case ct_COUNT:
6589 {
6590 char num_buf[20];
6591 long num = (type == ct_LINE1) ? eap->line1 :
6592 (type == ct_LINE2) ? eap->line2 :
6593 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6594 size_t num_len;
6595
6596 sprintf(num_buf, "%ld", num);
6597 num_len = STRLEN(num_buf);
6598 result = num_len;
6599
6600 if (quote)
6601 result += 2;
6602
6603 if (buf != NULL)
6604 {
6605 if (quote)
6606 *buf++ = '"';
6607 STRCPY(buf, num_buf);
6608 buf += num_len;
6609 if (quote)
6610 *buf = '"';
6611 }
6612
6613 break;
6614 }
6615
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006616 case ct_MODS:
6617 {
6618 int multi_mods = 0;
6619 typedef struct {
6620 int *varp;
6621 char *name;
6622 } mod_entry_T;
6623 static mod_entry_T mod_entries[] = {
6624#ifdef FEAT_BROWSE_CMD
6625 {&cmdmod.browse, "browse"},
6626#endif
6627#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6628 {&cmdmod.confirm, "confirm"},
6629#endif
6630 {&cmdmod.hide, "hide"},
6631 {&cmdmod.keepalt, "keepalt"},
6632 {&cmdmod.keepjumps, "keepjumps"},
6633 {&cmdmod.keepmarks, "keepmarks"},
6634 {&cmdmod.keeppatterns, "keeppatterns"},
6635 {&cmdmod.lockmarks, "lockmarks"},
6636 {&cmdmod.noswapfile, "noswapfile"},
6637 {NULL, NULL}
6638 };
6639 int i;
6640
6641 result = quote ? 2 : 0;
6642 if (buf != NULL)
6643 {
6644 if (quote)
6645 *buf++ = '"';
6646 *buf = '\0';
6647 }
6648
6649#ifdef FEAT_WINDOWS
6650 /* :aboveleft and :leftabove */
6651 if (cmdmod.split & WSP_ABOVE)
6652 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6653 /* :belowright and :rightbelow */
6654 if (cmdmod.split & WSP_BELOW)
6655 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6656 /* :botright */
6657 if (cmdmod.split & WSP_BOT)
6658 result += add_cmd_modifier(buf, "botright", &multi_mods);
6659#endif
6660
6661 /* the modifiers that are simple flags */
6662 for (i = 0; mod_entries[i].varp != NULL; ++i)
6663 if (*mod_entries[i].varp)
6664 result += add_cmd_modifier(buf, mod_entries[i].name,
6665 &multi_mods);
6666
6667 /* TODO: How to support :noautocmd? */
6668#ifdef HAVE_SANDBOX
6669 /* TODO: How to support :sandbox?*/
6670#endif
6671 /* :silent */
6672 if (msg_silent > 0)
6673 result += add_cmd_modifier(buf,
6674 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
6675#ifdef FEAT_WINDOWS
6676 /* :tab */
6677 if (cmdmod.tab > 0)
6678 result += add_cmd_modifier(buf, "tab", &multi_mods);
6679 /* :topleft */
6680 if (cmdmod.split & WSP_TOP)
6681 result += add_cmd_modifier(buf, "topleft", &multi_mods);
6682#endif
6683 /* TODO: How to support :unsilent?*/
6684 /* :verbose */
6685 if (p_verbose > 0)
6686 result += add_cmd_modifier(buf, "verbose", &multi_mods);
6687#ifdef FEAT_WINDOWS
6688 /* :vertical */
6689 if (cmdmod.split & WSP_VERT)
6690 result += add_cmd_modifier(buf, "vertical", &multi_mods);
6691#endif
6692 if (quote && buf != NULL)
6693 {
6694 buf += result - 2;
6695 *buf = '"';
6696 }
6697 break;
6698 }
6699
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 case ct_REGISTER:
6701 result = eap->regname ? 1 : 0;
6702 if (quote)
6703 result += 2;
6704 if (buf != NULL)
6705 {
6706 if (quote)
6707 *buf++ = '\'';
6708 if (eap->regname)
6709 *buf++ = eap->regname;
6710 if (quote)
6711 *buf = '\'';
6712 }
6713 break;
6714
6715 case ct_LT:
6716 result = 1;
6717 if (buf != NULL)
6718 *buf = '<';
6719 break;
6720
6721 default:
6722 /* Not recognized: just copy the '<' and return -1. */
6723 result = (size_t)-1;
6724 if (buf != NULL)
6725 *buf = '<';
6726 break;
6727 }
6728
6729 return result;
6730}
6731
6732 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006733do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734{
6735 char_u *buf;
6736 char_u *p;
6737 char_u *q;
6738
6739 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006740 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006741 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 size_t len, totlen;
6743
6744 size_t split_len = 0;
6745 char_u *split_buf = NULL;
6746 ucmd_T *cmd;
6747#ifdef FEAT_EVAL
6748 scid_T save_current_SID = current_SID;
6749#endif
6750
6751 if (eap->cmdidx == CMD_USER)
6752 cmd = USER_CMD(eap->useridx);
6753 else
6754 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6755
6756 /*
6757 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006758 * First round: "buf" is NULL, compute length, allocate "buf".
6759 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 */
6761 buf = NULL;
6762 for (;;)
6763 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006764 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006765 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006767
6768 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006770 start = vim_strchr(p, '<');
6771 if (start != NULL)
6772 end = vim_strchr(start + 1, '>');
6773 if (buf != NULL)
6774 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006775 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6776 ;
6777 if (*ksp == K_SPECIAL
6778 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006779 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6780# ifdef FEAT_GUI
6781 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6782# endif
6783 ))
6784 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006785 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006786 * KS_SPECIAL KE_FILLER, like for mappings, but
6787 * do_cmdline() doesn't handle that, so convert it back.
6788 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6789 len = ksp - p;
6790 if (len > 0)
6791 {
6792 mch_memmove(q, p, len);
6793 q += len;
6794 }
6795 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6796 p = ksp + 3;
6797 continue;
6798 }
6799 }
6800
6801 /* break if there no <item> is found */
6802 if (start == NULL || end == NULL)
6803 break;
6804
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 /* Include the '>' */
6806 ++end;
6807
6808 /* Take everything up to the '<' */
6809 len = start - p;
6810 if (buf == NULL)
6811 totlen += len;
6812 else
6813 {
6814 mch_memmove(q, p, len);
6815 q += len;
6816 }
6817
6818 len = uc_check_code(start, end - start, q, cmd, eap,
6819 &split_buf, &split_len);
6820 if (len == (size_t)-1)
6821 {
6822 /* no match, continue after '<' */
6823 p = start + 1;
6824 len = 1;
6825 }
6826 else
6827 p = end;
6828 if (buf == NULL)
6829 totlen += len;
6830 else
6831 q += len;
6832 }
6833 if (buf != NULL) /* second time here, finished */
6834 {
6835 STRCPY(q, p);
6836 break;
6837 }
6838
6839 totlen += STRLEN(p); /* Add on the trailing characters */
6840 buf = alloc((unsigned)(totlen + 1));
6841 if (buf == NULL)
6842 {
6843 vim_free(split_buf);
6844 return;
6845 }
6846 }
6847
6848#ifdef FEAT_EVAL
6849 current_SID = cmd->uc_scriptID;
6850#endif
6851 (void)do_cmdline(buf, eap->getline, eap->cookie,
6852 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6853#ifdef FEAT_EVAL
6854 current_SID = save_current_SID;
6855#endif
6856 vim_free(buf);
6857 vim_free(split_buf);
6858}
6859
6860# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6861 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006862get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863{
6864 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6865}
6866
6867/*
6868 * Function given to ExpandGeneric() to obtain the list of user command names.
6869 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006871get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872{
6873 if (idx < curbuf->b_ucmds.ga_len)
6874 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6875 idx -= curbuf->b_ucmds.ga_len;
6876 if (idx < ucmds.ga_len)
6877 return USER_CMD(idx)->uc_name;
6878 return NULL;
6879}
6880
6881/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006882 * Function given to ExpandGeneric() to obtain the list of user address type names.
6883 */
6884 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006885get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006886{
6887 return (char_u *)addr_type_complete[idx].name;
6888}
6889
6890/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 * Function given to ExpandGeneric() to obtain the list of user command
6892 * attributes.
6893 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006895get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896{
6897 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006898 {"addr", "bang", "bar", "buffer", "complete",
6899 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006901 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 return NULL;
6903 return (char_u *)user_cmd_flags[idx];
6904}
6905
6906/*
6907 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6908 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006910get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911{
6912 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6913
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006914 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 return NULL;
6916 return (char_u *)user_cmd_nargs[idx];
6917}
6918
6919/*
6920 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6921 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006923get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924{
6925 return (char_u *)command_complete[idx].name;
6926}
6927# endif /* FEAT_CMDL_COMPL */
6928
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006929/*
6930 * Parse address type argument
6931 */
6932 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006933parse_addr_type_arg(
6934 char_u *value,
6935 int vallen,
6936 long *argt,
6937 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006938{
6939 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01006940
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006941 for (i = 0; addr_type_complete[i].expand != -1; ++i)
6942 {
6943 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
6944 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
6945 if (a && b)
6946 {
6947 *addr_type_arg = addr_type_complete[i].expand;
6948 break;
6949 }
6950 }
6951
6952 if (addr_type_complete[i].expand == -1)
6953 {
6954 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01006955
6956 for (i = 0; err[i] != NUL && !vim_iswhite(err[i]); i++)
6957 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006958 err[i] = NUL;
6959 EMSG2(_("E180: Invalid address type value: %s"), err);
6960 return FAIL;
6961 }
6962
6963 if (*addr_type_arg != ADDR_LINES)
6964 *argt |= NOTADR;
6965
6966 return OK;
6967}
6968
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969#endif /* FEAT_USR_CMDS */
6970
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006971#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6972/*
6973 * Parse a completion argument "value[vallen]".
6974 * The detected completion goes in "*complp", argument type in "*argt".
6975 * When there is an argument, for function and user defined completion, it's
6976 * copied to allocated memory and stored in "*compl_arg".
6977 * Returns FAIL if something is wrong.
6978 */
6979 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006980parse_compl_arg(
6981 char_u *value,
6982 int vallen,
6983 int *complp,
6984 long *argt,
6985 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006986{
6987 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006988# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006989 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006990# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006991 int i;
6992 int valend = vallen;
6993
6994 /* Look for any argument part - which is the part after any ',' */
6995 for (i = 0; i < vallen; ++i)
6996 {
6997 if (value[i] == ',')
6998 {
6999 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007000# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007001 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007002# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007003 valend = i;
7004 break;
7005 }
7006 }
7007
7008 for (i = 0; command_complete[i].expand != 0; ++i)
7009 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007010 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007011 && STRNCMP(value, command_complete[i].name, valend) == 0)
7012 {
7013 *complp = command_complete[i].expand;
7014 if (command_complete[i].expand == EXPAND_BUFFERS)
7015 *argt |= BUFNAME;
7016 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7017 || command_complete[i].expand == EXPAND_FILES)
7018 *argt |= XFILE;
7019 break;
7020 }
7021 }
7022
7023 if (command_complete[i].expand == 0)
7024 {
7025 EMSG2(_("E180: Invalid complete value: %s"), value);
7026 return FAIL;
7027 }
7028
7029# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7030 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7031 && arg != NULL)
7032# else
7033 if (arg != NULL)
7034# endif
7035 {
7036 EMSG(_("E468: Completion argument only allowed for custom completion"));
7037 return FAIL;
7038 }
7039
7040# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7041 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7042 && arg == NULL)
7043 {
7044 EMSG(_("E467: Custom completion requires a function argument"));
7045 return FAIL;
7046 }
7047
7048 if (arg != NULL)
7049 *compl_arg = vim_strnsave(arg, (int)arglen);
7050# endif
7051 return OK;
7052}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007053
7054 int
7055cmdcomplete_str_to_type(char_u *complete_str)
7056{
7057 int i;
7058
7059 for (i = 0; command_complete[i].expand != 0; ++i)
7060 if (STRCMP(complete_str, command_complete[i].name) == 0)
7061 return command_complete[i].expand;
7062
7063 return EXPAND_NOTHING;
7064}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007065#endif
7066
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007068ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069{
Bram Moolenaare6850792010-05-14 15:28:44 +02007070 if (*eap->arg == NUL)
7071 {
7072#ifdef FEAT_EVAL
7073 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7074 char_u *p = NULL;
7075
7076 if (expr != NULL)
7077 {
7078 ++emsg_off;
7079 p = eval_to_string(expr, NULL, FALSE);
7080 --emsg_off;
7081 vim_free(expr);
7082 }
7083 if (p != NULL)
7084 {
7085 MSG(p);
7086 vim_free(p);
7087 }
7088 else
7089 MSG("default");
7090#else
7091 MSG(_("unknown"));
7092#endif
7093 }
7094 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007095 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096}
7097
7098 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007099ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100{
7101 if (*eap->arg == NUL && eap->cmd[2] == '!')
7102 MSG(_("Greetings, Vim user!"));
7103 do_highlight(eap->arg, eap->forceit, FALSE);
7104}
7105
7106
7107/*
7108 * Call this function if we thought we were going to exit, but we won't
7109 * (because of an error). May need to restore the terminal mode.
7110 */
7111 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007112not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113{
7114 exiting = FALSE;
7115 settmode(TMODE_RAW);
7116}
7117
7118/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007119 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 */
7121 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007122ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123{
Bram Moolenaarf240e182014-11-27 18:33:02 +01007124#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007125 win_T *wp;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007126#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007127
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128#ifdef FEAT_CMDWIN
7129 if (cmdwin_type != 0)
7130 {
7131 cmdwin_result = Ctrl_C;
7132 return;
7133 }
7134#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007135 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007136 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007137 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007138 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007139 return;
7140 }
Bram Moolenaarf240e182014-11-27 18:33:02 +01007141#ifdef FEAT_WINDOWS
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007142 if (eap->addr_count > 0)
7143 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007144 int wnr = eap->line2;
7145
7146 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7147 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007148 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007149 }
7150 else
Bram Moolenaarf240e182014-11-27 18:33:02 +01007151#endif
7152#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007153 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007154#endif
7155
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007156#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02007157 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007158 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02007159 * being closed (can only happen in autocommands). */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007160 if (curbuf_locked() || (wp->w_buffer->b_nwindows == 1
7161 && wp->w_buffer->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007162 return;
7163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164
7165#ifdef FEAT_NETBEANS_INTG
7166 netbeansForcedQuit = eap->forceit;
7167#endif
7168
7169 /*
7170 * If there are more files or windows we won't exit.
7171 */
7172 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7173 exiting = TRUE;
7174 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007175 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7176 | (eap->forceit ? CCGD_FORCEIT : 0)
7177 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007179 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 {
7181 not_exiting();
7182 }
7183 else
7184 {
7185#ifdef FEAT_WINDOWS
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007186 /* quit last window
7187 * Note: only_one_window() returns true, even so a help window is
7188 * still open. In that case only quit, if no address has been
7189 * specified. Example:
7190 * :h|wincmd w|1q - don't quit
7191 * :h|wincmd w|q - quit
7192 */
7193 if (only_one_window() && (firstwin == lastwin || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194#endif
7195 getout(0);
7196#ifdef FEAT_WINDOWS
7197# ifdef FEAT_GUI
7198 need_mouse_correct = TRUE;
7199# endif
7200 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007201 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202#endif
7203 }
7204}
7205
7206/*
7207 * ":cquit".
7208 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007210ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211{
7212 getout(1); /* this does not always pass on the exit code to the Manx
7213 compiler. why? */
7214}
7215
7216/*
7217 * ":qall": try to quit all windows
7218 */
7219 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007220ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221{
7222# ifdef FEAT_CMDWIN
7223 if (cmdwin_type != 0)
7224 {
7225 if (eap->forceit)
7226 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7227 else
7228 cmdwin_result = K_XF2;
7229 return;
7230 }
7231# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007232
7233 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007234 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007235 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007236 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007237 return;
7238 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007239#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007240 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7241 /* Refuse to quit when locked or when the buffer in the last window is
7242 * being closed (can only happen in autocommands). */
7243 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007244 return;
7245#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007246
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007248 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 getout(0);
7250 not_exiting();
7251}
7252
Bram Moolenaard9967712006-03-11 21:18:15 +00007253#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254/*
7255 * ":close": close current window, unless it is the last one
7256 */
7257 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007258ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007260 win_T *win;
7261 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262# ifdef FEAT_CMDWIN
7263 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007264 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 else
7266# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007267 if (!text_locked()
7268#ifdef FEAT_AUTOCMD
7269 && !curbuf_locked()
7270#endif
7271 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007272 {
7273 if (eap->addr_count == 0)
7274 ex_win_close(eap->forceit, curwin, NULL);
7275 else {
Bram Moolenaar29323592016-07-24 22:04:11 +02007276 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007277 {
7278 winnr++;
7279 if (winnr == eap->line2)
7280 break;
7281 }
7282 if (win == NULL)
7283 win = lastwin;
7284 ex_win_close(eap->forceit, win, NULL);
7285 }
7286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287}
7288
Bram Moolenaard9967712006-03-11 21:18:15 +00007289# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007290/*
7291 * ":pclose": Close any preview window.
7292 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007294ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007295{
7296 win_T *win;
7297
Bram Moolenaar29323592016-07-24 22:04:11 +02007298 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007299 if (win->w_p_pvw)
7300 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007301 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007302 break;
7303 }
7304}
Bram Moolenaard9967712006-03-11 21:18:15 +00007305# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007306
Bram Moolenaarf740b292006-02-16 22:11:02 +00007307/*
7308 * Close window "win" and take care of handling closing the last window for a
7309 * modified buffer.
7310 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007311 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007312ex_win_close(
7313 int forceit,
7314 win_T *win,
7315 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316{
7317 int need_hide;
7318 buf_T *buf = win->w_buffer;
7319
7320 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007321 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007323# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 if ((p_confirm || cmdmod.confirm) && p_write)
7325 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007326 bufref_T bufref;
7327
7328 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007330 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 return;
7332 need_hide = FALSE;
7333 }
7334 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007335# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 {
7337 EMSG(_(e_nowrtmsg));
7338 return;
7339 }
7340 }
7341
Bram Moolenaard9967712006-03-11 21:18:15 +00007342# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007344# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007345
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007347 if (tp == NULL)
7348 win_close(win, !need_hide && !P_HID(buf));
7349 else
7350 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351}
7352
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007354 * ":tabclose": close current tab page, unless it is the last one.
7355 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 */
7357 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007358ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007360 tabpage_T *tp;
7361
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007362# ifdef FEAT_CMDWIN
7363 if (cmdwin_type != 0)
7364 cmdwin_result = K_IGNORE;
7365 else
7366# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007367 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007368 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007369 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007371 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007372 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007373 tp = find_tabpage((int)eap->line2);
7374 if (tp == NULL)
7375 {
7376 beep_flush();
7377 return;
7378 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007379 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007380 {
7381 tabpage_close_other(tp, eap->forceit);
7382 return;
7383 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007384 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007385 if (!text_locked()
7386#ifdef FEAT_AUTOCMD
7387 && !curbuf_locked()
7388#endif
7389 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00007390 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007392}
7393
7394/*
7395 * ":tabonly": close all tab pages except the current one
7396 */
7397 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007398ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007399{
7400 tabpage_T *tp;
7401 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007402
7403# ifdef FEAT_CMDWIN
7404 if (cmdwin_type != 0)
7405 cmdwin_result = K_IGNORE;
7406 else
7407# endif
7408 if (first_tabpage->tp_next == NULL)
7409 MSG(_("Already only one tab page"));
7410 else
7411 {
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007412 if (eap->addr_count > 0)
7413 goto_tabpage(eap->line2);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007414 /* Repeat this up to a 1000 times, because autocommands may mess
7415 * up the lists. */
7416 for (done = 0; done < 1000; ++done)
7417 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007418 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007419 if (tp->tp_topframe != topframe)
7420 {
7421 tabpage_close_other(tp, eap->forceit);
7422 /* if we failed to close it quit */
7423 if (valid_tabpage(tp))
7424 done = 1000;
7425 /* start over, "tp" is now invalid */
7426 break;
7427 }
7428 if (first_tabpage->tp_next == NULL)
7429 break;
7430 }
7431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433
7434/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007435 * Close the current tab page.
7436 */
7437 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007438tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007439{
7440 /* First close all the windows but the current one. If that worked then
7441 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007442 if (lastwin != firstwin)
7443 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007444 if (lastwin == firstwin)
7445 ex_win_close(forceit, curwin, NULL);
7446# ifdef FEAT_GUI
7447 need_mouse_correct = TRUE;
7448# endif
7449}
7450
7451/*
7452 * Close tab page "tp", which is not the current tab page.
7453 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007454 * Also takes care of the tab pages line disappearing when closing the
7455 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007456 */
7457 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007458tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007459{
7460 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007461 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007462 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007463
7464 /* Limit to 1000 windows, autocommands may add a window while we close
7465 * one. OK, so I'm paranoid... */
7466 while (++done < 1000)
7467 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007468 wp = tp->tp_firstwin;
7469 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007470
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007471 /* Autocommands may delete the tab page under our fingers and we may
7472 * fail to close a window with a modified buffer. */
7473 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007474 break;
7475 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007476
Bram Moolenaar29323592016-07-24 22:04:11 +02007477#ifdef FEAT_AUTOCMD
7478 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
7479#endif
7480
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007481 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007482 if (h != tabline_height())
7483 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007484}
7485
7486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 * ":only".
7488 */
7489 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007490ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007492 win_T *wp;
7493 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494# ifdef FEAT_GUI
7495 need_mouse_correct = TRUE;
7496# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007497 if (eap->addr_count > 0)
7498 {
7499 wnr = eap->line2;
7500 for (wp = firstwin; --wnr > 0; )
7501 {
7502 if (wp->w_next == NULL)
7503 break;
7504 else
7505 wp = wp->w_next;
7506 }
7507 win_goto(wp);
7508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 close_others(TRUE, eap->forceit);
7510}
7511
7512/*
7513 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007514 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007516 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007517ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518{
7519 if (eap->addr_count == 0)
7520 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007521 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522}
7523#endif /* FEAT_WINDOWS */
7524
7525 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007526ex_hide(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527{
7528 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
7529 eap->errmsg = e_invarg;
7530 else
7531 {
7532 /* ":hide" or ":hide | cmd": hide current window */
7533 eap->nextcmd = check_nextcmd(eap->arg);
7534#ifdef FEAT_WINDOWS
7535 if (!eap->skip)
7536 {
7537# ifdef FEAT_GUI
7538 need_mouse_correct = TRUE;
7539# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007540 if (eap->addr_count == 0)
7541 win_close(curwin, FALSE); /* don't free buffer */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007542 else
7543 {
7544 int winnr = 0;
7545 win_T *win;
7546
Bram Moolenaar29323592016-07-24 22:04:11 +02007547 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007548 {
7549 winnr++;
7550 if (winnr == eap->line2)
7551 break;
7552 }
7553 if (win == NULL)
7554 win = lastwin;
7555 win_close(win, FALSE);
7556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 }
7558#endif
7559 }
7560}
7561
7562/*
7563 * ":stop" and ":suspend": Suspend Vim.
7564 */
7565 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007566ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567{
7568 /*
7569 * Disallow suspending for "rvim".
7570 */
7571 if (!check_restricted()
7572#ifdef WIN3264
7573 /*
7574 * Check if external commands are allowed now.
7575 */
7576 && can_end_termcap_mode(TRUE)
7577#endif
7578 )
7579 {
7580 if (!eap->forceit)
7581 autowrite_all();
7582 windgoto((int)Rows - 1, 0);
7583 out_char('\n');
7584 out_flush();
7585 stoptermcap();
7586 out_flush(); /* needed for SUN to restore xterm buffer */
7587#ifdef FEAT_TITLE
7588 mch_restore_title(3); /* restore window titles */
7589#endif
7590 ui_suspend(); /* call machine specific function */
7591#ifdef FEAT_TITLE
7592 maketitle();
7593 resettitle(); /* force updating the title */
7594#endif
7595 starttermcap();
7596 scroll_start(); /* scroll screen before redrawing */
7597 redraw_later_clear();
7598 shell_resized(); /* may have resized window */
7599 }
7600}
7601
7602/*
7603 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7604 */
7605 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007606ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607{
7608#ifdef FEAT_CMDWIN
7609 if (cmdwin_type != 0)
7610 {
7611 cmdwin_result = Ctrl_C;
7612 return;
7613 }
7614#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007615 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007616 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007617 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007618 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007619 return;
7620 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007621#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007622 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7623 /* Refuse to quit when locked or when the buffer in the last window is
7624 * being closed (can only happen in autocommands). */
7625 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007626 return;
7627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628
7629 /*
7630 * if more files or windows we won't exit
7631 */
7632 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7633 exiting = TRUE;
7634 if ( ((eap->cmdidx == CMD_wq
7635 || curbufIsChanged())
7636 && do_write(eap) == FAIL)
7637 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007638 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 {
7640 not_exiting();
7641 }
7642 else
7643 {
7644#ifdef FEAT_WINDOWS
7645 if (only_one_window()) /* quit last window, exit Vim */
7646#endif
7647 getout(0);
7648#ifdef FEAT_WINDOWS
7649# ifdef FEAT_GUI
7650 need_mouse_correct = TRUE;
7651# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007652 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 win_close(curwin, !P_HID(curwin->w_buffer));
7654#endif
7655 }
7656}
7657
7658/*
7659 * ":print", ":list", ":number".
7660 */
7661 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007662ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007664 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7665 EMSG(_(e_emptybuf));
7666 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007668 for ( ;!got_int; ui_breakcheck())
7669 {
7670 print_line(eap->line1,
7671 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7672 || (eap->flags & EXFLAG_NR)),
7673 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7674 if (++eap->line1 > eap->line2)
7675 break;
7676 out_flush(); /* show one line at a time */
7677 }
7678 setpcmark();
7679 /* put cursor at last line */
7680 curwin->w_cursor.lnum = eap->line2;
7681 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 }
7683
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685}
7686
7687#ifdef FEAT_BYTEOFF
7688 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007689ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690{
7691 goto_byte(eap->line2);
7692}
7693#endif
7694
7695/*
7696 * ":shell".
7697 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007699ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700{
7701 do_shell(NULL, 0);
7702}
7703
7704#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7705 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007706 || defined(FEAT_GUI_MSWIN) \
7707 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 || defined(PROTO)
7709
7710/*
7711 * Handle a file drop. The code is here because a drop is *nearly* like an
7712 * :args command, but not quite (we have a list of exact filenames, so we
7713 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7714 * code is very similar to :args and hence needs access to a lot of the static
7715 * functions in this file.
7716 *
7717 * The list should be allocated using alloc(), as should each item in the
7718 * list. This function takes over responsibility for freeing the list.
7719 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007720 * XXX The list is made into the argument list. This is freed using
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007721 * FreeWild(), which does a series of vim_free() calls.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 */
7723 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007724handle_drop(
7725 int filec, /* the number of files dropped */
7726 char_u **filev, /* the list of files dropped */
7727 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728{
7729 exarg_T ea;
7730 int save_msg_scroll = msg_scroll;
7731
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007732 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007733 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007735#ifdef FEAT_AUTOCMD
7736 if (curbuf_locked())
7737 return;
7738#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007739 /* When the screen is being updated we should not change buffers and
7740 * windows structures, it may cause freed memory to be used. */
7741 if (updating_screen)
7742 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743
7744 /* Check whether the current buffer is changed. If so, we will need
7745 * to split the current window or data could be lost.
7746 * We don't need to check if the 'hidden' option is set, as in this
7747 * case the buffer won't be lost.
7748 */
7749 if (!P_HID(curbuf) && !split)
7750 {
7751 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007752 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 --emsg_off;
7754 }
7755 if (split)
7756 {
7757# ifdef FEAT_WINDOWS
7758 if (win_split(0, 0) == FAIL)
7759 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007760 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761
7762 /* When splitting the window, create a new alist. Otherwise the
7763 * existing one is overwritten. */
7764 alist_unlink(curwin->w_alist);
7765 alist_new();
7766# else
7767 return; /* can't split, always fail */
7768# endif
7769 }
7770
7771 /*
7772 * Set up the new argument list.
7773 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007774 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775
7776 /*
7777 * Move to the first file.
7778 */
7779 /* Fake up a minimal "next" command for do_argfile() */
7780 vim_memset(&ea, 0, sizeof(ea));
7781 ea.cmd = (char_u *)"next";
7782 do_argfile(&ea, 0);
7783
7784 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7785 * mode that is not needed here. */
7786 need_start_insertmode = FALSE;
7787
7788 /* Restore msg_scroll, otherwise a following command may cause scrolling
7789 * unexpectedly. The screen will be redrawn by the caller, thus
7790 * msg_scroll being set by displaying a message is irrelevant. */
7791 msg_scroll = save_msg_scroll;
7792}
7793#endif
7794
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795/*
7796 * Clear an argument list: free all file names and reset it to zero entries.
7797 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007798 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007799alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800{
7801 while (--al->al_ga.ga_len >= 0)
7802 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7803 ga_clear(&al->al_ga);
7804}
7805
7806/*
7807 * Init an argument list.
7808 */
7809 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007810alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811{
7812 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7813}
7814
7815#if defined(FEAT_WINDOWS) || defined(PROTO)
7816
7817/*
7818 * Remove a reference from an argument list.
7819 * Ignored when the argument list is the global one.
7820 * If the argument list is no longer used by any window, free it.
7821 */
7822 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007823alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824{
7825 if (al != &global_alist && --al->al_refcount <= 0)
7826 {
7827 alist_clear(al);
7828 vim_free(al);
7829 }
7830}
7831
7832# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7833/*
7834 * Create a new argument list and use it for the current window.
7835 */
7836 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007837alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838{
7839 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7840 if (curwin->w_alist == NULL)
7841 {
7842 curwin->w_alist = &global_alist;
7843 ++global_alist.al_refcount;
7844 }
7845 else
7846 {
7847 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007848 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 alist_init(curwin->w_alist);
7850 }
7851}
7852# endif
7853#endif
7854
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007855#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856/*
7857 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007858 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7859 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 */
7861 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007862alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863{
7864 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007865 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 char_u **new_arg_files;
7867 int new_arg_file_count;
7868 char_u *save_p_su = p_su;
7869 int i;
7870
7871 /* Don't use 'suffixes' here. This should work like the shell did the
7872 * expansion. Also, the vimrc file isn't read yet, thus the user
7873 * can't set the options. */
7874 p_su = empty_option;
7875 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7876 if (old_arg_files != NULL)
7877 {
7878 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007879 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7880 old_arg_count = GARGCOUNT;
7881 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007883 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 && new_arg_file_count > 0)
7885 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007886 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7887 TRUE, fnum_list, fnum_len);
7888 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 }
7891 p_su = save_p_su;
7892}
7893#endif
7894
7895/*
7896 * Set the argument list for the current window.
7897 * Takes over the allocated files[] and the allocated fnames in it.
7898 */
7899 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007900alist_set(
7901 alist_T *al,
7902 int count,
7903 char_u **files,
7904 int use_curbuf,
7905 int *fnum_list,
7906 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907{
7908 int i;
7909
7910 alist_clear(al);
7911 if (ga_grow(&al->al_ga, count) == OK)
7912 {
7913 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007914 {
7915 if (got_int)
7916 {
7917 /* When adding many buffers this can take a long time. Allow
7918 * interrupting here. */
7919 while (i < count)
7920 vim_free(files[i++]);
7921 break;
7922 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007923
7924 /* May set buffer name of a buffer previously used for the
7925 * argument list, so that it's re-used by alist_add. */
7926 if (fnum_list != NULL && i < fnum_len)
7927 buf_set_name(fnum_list[i], files[i]);
7928
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007930 ui_breakcheck();
7931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 vim_free(files);
7933 }
7934 else
7935 FreeWild(count, files);
7936#ifdef FEAT_WINDOWS
7937 if (al == &global_alist)
7938#endif
7939 arg_had_last = FALSE;
7940}
7941
7942/*
7943 * Add file "fname" to argument list "al".
7944 * "fname" must have been allocated and "al" must have been checked for room.
7945 */
7946 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007947alist_add(
7948 alist_T *al,
7949 char_u *fname,
7950 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951{
7952 if (fname == NULL) /* don't add NULL file names */
7953 return;
7954#ifdef BACKSLASH_IN_FILENAME
7955 slash_adjust(fname);
7956#endif
7957 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7958 if (set_fnum > 0)
7959 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7960 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7961 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962}
7963
7964#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7965/*
7966 * Adjust slashes in file names. Called after 'shellslash' was set.
7967 */
7968 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007969alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970{
7971 int i;
7972# ifdef FEAT_WINDOWS
7973 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007974 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975# endif
7976
7977 for (i = 0; i < GARGCOUNT; ++i)
7978 if (GARGLIST[i].ae_fname != NULL)
7979 slash_adjust(GARGLIST[i].ae_fname);
7980# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007981 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 if (wp->w_alist != &global_alist)
7983 for (i = 0; i < WARGCOUNT(wp); ++i)
7984 if (WARGLIST(wp)[i].ae_fname != NULL)
7985 slash_adjust(WARGLIST(wp)[i].ae_fname);
7986# endif
7987}
7988#endif
7989
7990/*
7991 * ":preserve".
7992 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007994ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007996 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 ml_preserve(curbuf, TRUE);
7998}
7999
8000/*
8001 * ":recover".
8002 */
8003 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008004ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005{
8006 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8007 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008008 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8009 | CCGD_MULTWIN
8010 | (eap->forceit ? CCGD_FORCEIT : 0)
8011 | CCGD_EXCMD)
8012
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 && (*eap->arg == NUL
8014 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8015 ml_recover();
8016 recoverymode = FALSE;
8017}
8018
8019/*
8020 * Command modifier used in a wrong way.
8021 */
8022 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008023ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024{
8025 eap->errmsg = e_invcmd;
8026}
8027
8028#ifdef FEAT_WINDOWS
8029/*
8030 * :sview [+command] file split window with new file, read-only
8031 * :split [[+command] file] split window with current or new file
8032 * :vsplit [[+command] file] split window vertically with current or new file
8033 * :new [[+command] file] split window with no or new file
8034 * :vnew [[+command] file] split vertically window with no or new file
8035 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008036 *
8037 * :tabedit open new Tab page with empty window
8038 * :tabedit [+command] file open new Tab page and edit "file"
8039 * :tabnew [[+command] file] just like :tabedit
8040 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 */
8042 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008043ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008045 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008046# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008048# endif
8049# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008051# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008053# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008055# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008057# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008059 * quickfix windows. But it's OK when doing ":tab split". */
8060 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 {
8062 if (eap->cmdidx == CMD_split)
8063 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 if (eap->cmdidx == CMD_vsplit)
8065 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008067# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008069# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008070 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 {
8072 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8073 FNAME_MESS, TRUE, curbuf->b_ffname);
8074 if (fname == NULL)
8075 goto theend;
8076 eap->arg = fname;
8077 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008078# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008080# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008082# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 && eap->cmdidx != CMD_new)
8086 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008087# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008088 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008089# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008090 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008091# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008092 au_has_group((char_u *)"FileExplorer"))
8093 {
8094 /* No browsing supported but we do have the file explorer:
8095 * Edit the directory. */
8096 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8097 eap->arg = (char_u *)".";
8098 }
8099 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008100# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008101 {
8102 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008104 if (fname == NULL)
8105 goto theend;
8106 eap->arg = fname;
8107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 }
8109 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008110# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008112 /*
8113 * Either open new tab page or split the window.
8114 */
8115 if (eap->cmdidx == CMD_tabedit
8116 || eap->cmdidx == CMD_tabfind
8117 || eap->cmdidx == CMD_tabnew)
8118 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008119 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8120 : eap->addr_count == 0 ? 0
8121 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008122 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008123 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008124
8125 /* set the alternate buffer for the window we came from */
8126 if (curwin != old_curwin
8127 && win_valid(old_curwin)
8128 && old_curwin->w_buffer != curbuf
8129 && !cmdmod.keepalt)
8130 old_curwin->w_alt_fnum = curbuf->b_fnum;
8131 }
8132 }
8133 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8135 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008136# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 /* Reset 'scrollbind' when editing another file, but keep it when
8138 * doing ":split" without arguments. */
8139 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008140# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008142# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008144 {
8145 RESET_BINDING(curwin);
8146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 else
8148 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008149# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 do_exedit(eap, old_curwin);
8151 }
8152
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008153# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008155# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008157# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158theend:
8159 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008160# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008162
8163/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008164 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008165 */
8166 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008167tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008168{
8169 exarg_T ea;
8170
8171 vim_memset(&ea, 0, sizeof(ea));
8172 ea.cmdidx = CMD_tabnew;
8173 ea.cmd = (char_u *)"tabn";
8174 ea.arg = (char_u *)"";
8175 ex_splitview(&ea);
8176}
8177
8178/*
8179 * :tabnext command
8180 */
8181 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008182ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008183{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008184 switch (eap->cmdidx)
8185 {
8186 case CMD_tabfirst:
8187 case CMD_tabrewind:
8188 goto_tabpage(1);
8189 break;
8190 case CMD_tablast:
8191 goto_tabpage(9999);
8192 break;
8193 case CMD_tabprevious:
8194 case CMD_tabNext:
8195 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
8196 break;
8197 default: /* CMD_tabnext */
8198 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
8199 break;
8200 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008201}
8202
8203/*
8204 * :tabmove command
8205 */
8206 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008207ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008208{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008209 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008210
8211 if (eap->arg && *eap->arg != NUL)
8212 {
8213 char_u *p = eap->arg;
8214 int relative = 0; /* argument +N/-N means: move N places to the
8215 * right/left relative to the current position. */
8216
8217 if (*eap->arg == '-')
8218 {
8219 relative = -1;
8220 p = eap->arg + 1;
8221 }
8222 else if (*eap->arg == '+')
8223 {
8224 relative = 1;
8225 p = eap->arg + 1;
8226 }
8227 else
8228 p = eap->arg;
8229
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008230 if (relative == 0)
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008231 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008232 if (STRCMP(p, "$") == 0)
8233 tab_number = LAST_TAB_NR;
8234 else if (p == skipdigits(p))
8235 {
8236 /* No numbers as argument. */
8237 eap->errmsg = e_invarg;
8238 return;
8239 }
8240 else
8241 tab_number = getdigits(&p);
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008242 }
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008243 else
8244 {
8245 if (*p != NUL)
8246 tab_number = getdigits(&p);
8247 else
8248 tab_number = 1;
8249 tab_number = tab_number * relative + tabpage_index(curtab);
8250 if (relative == -1)
8251 --tab_number;
8252 }
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008253 }
8254 else if (eap->addr_count != 0)
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008255 {
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008256 tab_number = eap->line2;
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008257 if (**eap->cmdlinep == '-')
8258 --tab_number;
8259 }
8260 else
8261 tab_number = LAST_TAB_NR;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008262
8263 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008264}
8265
8266/*
8267 * :tabs command: List tabs and their contents.
8268 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008269 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008270ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008271{
8272 tabpage_T *tp;
8273 win_T *wp;
8274 int tabcount = 1;
8275
8276 msg_start();
8277 msg_scroll = TRUE;
8278 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8279 {
8280 msg_putchar('\n');
8281 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
8282 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
8283 out_flush(); /* output one line at a time */
8284 ui_breakcheck();
8285
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008286 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008287 wp = firstwin;
8288 else
8289 wp = tp->tp_firstwin;
8290 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8291 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008292 msg_putchar('\n');
8293 msg_putchar(wp == curwin ? '>' : ' ');
8294 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008295 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8296 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008297 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008298 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008299 else
8300 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8301 IObuff, IOSIZE, TRUE);
8302 msg_outtrans(IObuff);
8303 out_flush(); /* output one line at a time */
8304 ui_breakcheck();
8305 }
8306 }
8307}
8308
8309#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310
8311/*
8312 * ":mode": Set screen mode.
8313 * If no argument given, just get the screen size and redraw.
8314 */
8315 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008316ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317{
8318 if (*eap->arg == NUL)
8319 shell_resized();
8320 else
8321 mch_screenmode(eap->arg);
8322}
8323
8324#ifdef FEAT_WINDOWS
8325/*
8326 * ":resize".
8327 * set, increment or decrement current window height
8328 */
8329 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008330ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331{
8332 int n;
8333 win_T *wp = curwin;
8334
8335 if (eap->addr_count > 0)
8336 {
8337 n = eap->line2;
8338 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8339 ;
8340 }
8341
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008342# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008344# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 if (cmdmod.split & WSP_VERT)
8347 {
8348 if (*eap->arg == '-' || *eap->arg == '+')
8349 n += W_WIDTH(curwin);
8350 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8351 n = 9999;
8352 win_setwidth_win((int)n, wp);
8353 }
8354 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 {
8356 if (*eap->arg == '-' || *eap->arg == '+')
8357 n += curwin->w_height;
8358 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8359 n = 9999;
8360 win_setheight_win((int)n, wp);
8361 }
8362}
8363#endif
8364
8365/*
8366 * ":find [+command] <file>" command.
8367 */
8368 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008369ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370{
8371#ifdef FEAT_SEARCHPATH
8372 char_u *fname;
8373 int count;
8374
8375 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8376 TRUE, curbuf->b_ffname);
8377 if (eap->addr_count > 0)
8378 {
8379 /* Repeat finding the file "count" times. This matters when it
8380 * appears several times in the path. */
8381 count = eap->line2;
8382 while (fname != NULL && --count > 0)
8383 {
8384 vim_free(fname);
8385 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8386 FALSE, curbuf->b_ffname);
8387 }
8388 }
8389
8390 if (fname != NULL)
8391 {
8392 eap->arg = fname;
8393#endif
8394 do_exedit(eap, NULL);
8395#ifdef FEAT_SEARCHPATH
8396 vim_free(fname);
8397 }
8398#endif
8399}
8400
8401/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008402 * ":open" simulation: for now just work like ":visual".
8403 */
8404 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008405ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008406{
8407 regmatch_T regmatch;
8408 char_u *p;
8409
8410 curwin->w_cursor.lnum = eap->line2;
8411 beginline(BL_SOL | BL_FIX);
8412 if (*eap->arg == '/')
8413 {
8414 /* ":open /pattern/": put cursor in column found with pattern */
8415 ++eap->arg;
8416 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8417 *p = NUL;
8418 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8419 if (regmatch.regprog != NULL)
8420 {
8421 regmatch.rm_ic = p_ic;
8422 p = ml_get_curline();
8423 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008424 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008425 else
8426 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008427 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008428 }
8429 /* Move to the NUL, ignore any other arguments. */
8430 eap->arg += STRLEN(eap->arg);
8431 }
8432 check_cursor();
8433
8434 eap->cmdidx = CMD_visual;
8435 do_exedit(eap, NULL);
8436}
8437
8438/*
8439 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 */
8441 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008442ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443{
8444 do_exedit(eap, NULL);
8445}
8446
8447/*
8448 * ":edit <file>" command and alikes.
8449 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008451do_exedit(
8452 exarg_T *eap,
8453 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454{
8455 int n;
8456#ifdef FEAT_WINDOWS
8457 int need_hide;
8458#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008459 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460
8461 /*
8462 * ":vi" command ends Ex mode.
8463 */
8464 if (exmode_active && (eap->cmdidx == CMD_visual
8465 || eap->cmdidx == CMD_view))
8466 {
8467 exmode_active = FALSE;
8468 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008469 {
8470 /* Special case: ":global/pat/visual\NLvi-commands" */
8471 if (global_busy)
8472 {
8473 int rd = RedrawingDisabled;
8474 int nwr = no_wait_return;
8475 int ms = msg_scroll;
8476#ifdef FEAT_GUI
8477 int he = hold_gui_events;
8478#endif
8479
8480 if (eap->nextcmd != NULL)
8481 {
8482 stuffReadbuff(eap->nextcmd);
8483 eap->nextcmd = NULL;
8484 }
8485
8486 if (exmode_was != EXMODE_VIM)
8487 settmode(TMODE_RAW);
8488 RedrawingDisabled = 0;
8489 no_wait_return = 0;
8490 need_wait_return = FALSE;
8491 msg_scroll = 0;
8492#ifdef FEAT_GUI
8493 hold_gui_events = 0;
8494#endif
8495 must_redraw = CLEAR;
8496
8497 main_loop(FALSE, TRUE);
8498
8499 RedrawingDisabled = rd;
8500 no_wait_return = nwr;
8501 msg_scroll = ms;
8502#ifdef FEAT_GUI
8503 hold_gui_events = he;
8504#endif
8505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 }
8509
8510 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008511 || eap->cmdidx == CMD_tabnew
8512 || eap->cmdidx == CMD_tabedit
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008513#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 || eap->cmdidx == CMD_vnew
8515#endif
8516 ) && *eap->arg == NUL)
8517 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008518 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 setpcmark();
8520 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008521 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8522 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 }
8524 else if ((eap->cmdidx != CMD_split
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008525#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 && eap->cmdidx != CMD_vsplit
8527#endif
8528 )
8529 || *eap->arg != NUL
8530#ifdef FEAT_BROWSE
8531 || cmdmod.browse
8532#endif
8533 )
8534 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008535#ifdef FEAT_AUTOCMD
8536 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8537 * can bring us here, others are stopped earlier. */
8538 if (*eap->arg != NUL && curbuf_locked())
8539 return;
8540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 n = readonlymode;
8542 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8543 readonlymode = TRUE;
8544 else if (eap->cmdidx == CMD_enew)
8545 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8546 empty buffer */
8547 setpcmark();
8548 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8549 NULL, eap,
8550 /* ":edit" goes to first line if Vi compatible */
8551 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8552 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8553 ? ECMD_ONE : eap->do_ecmd_lnum,
8554 (P_HID(curbuf) ? ECMD_HIDE : 0)
8555 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008556 /* after a split we can use an existing buffer */
8557 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558#ifdef FEAT_LISTCMDS
8559 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8560#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008561 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 {
8563 /* Editing the file failed. If the window was split, close it. */
8564#ifdef FEAT_WINDOWS
8565 if (old_curwin != NULL)
8566 {
8567 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8568 if (!need_hide || P_HID(curbuf))
8569 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008570# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8571 cleanup_T cs;
8572
8573 /* Reset the error/interrupt/exception state here so that
8574 * aborting() returns FALSE when closing a window. */
8575 enter_cleanup(&cs);
8576# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577# ifdef FEAT_GUI
8578 need_mouse_correct = TRUE;
8579# endif
8580 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008581
8582# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8583 /* Restore the error/interrupt/exception state if not
8584 * discarded by a new aborting error, interrupt, or
8585 * uncaught exception. */
8586 leave_cleanup(&cs);
8587# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 }
8589 }
8590#endif
8591 }
8592 else if (readonlymode && curbuf->b_nwindows == 1)
8593 {
8594 /* When editing an already visited buffer, 'readonly' won't be set
8595 * but the previous value is kept. With ":view" and ":sview" we
8596 * want the file to be readonly, except when another window is
8597 * editing the same buffer. */
8598 curbuf->b_p_ro = TRUE;
8599 }
8600 readonlymode = n;
8601 }
8602 else
8603 {
8604 if (eap->do_ecmd_cmd != NULL)
8605 do_cmdline_cmd(eap->do_ecmd_cmd);
8606#ifdef FEAT_TITLE
8607 n = curwin->w_arg_idx_invalid;
8608#endif
8609 check_arg_idx(curwin);
8610#ifdef FEAT_TITLE
8611 if (n != curwin->w_arg_idx_invalid)
8612 maketitle();
8613#endif
8614 }
8615
8616#ifdef FEAT_WINDOWS
8617 /*
8618 * if ":split file" worked, set alternate file name in old window to new
8619 * file
8620 */
8621 if (old_curwin != NULL
8622 && *eap->arg != NUL
8623 && curwin != old_curwin
8624 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008625 && old_curwin->w_buffer != curbuf
8626 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 old_curwin->w_alt_fnum = curbuf->b_fnum;
8628#endif
8629
8630 ex_no_reprint = TRUE;
8631}
8632
8633#ifndef FEAT_GUI
8634/*
8635 * ":gui" and ":gvim" when there is no GUI.
8636 */
8637 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008638ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639{
8640 eap->errmsg = e_nogvim;
8641}
8642#endif
8643
8644#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8645 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008646ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647{
8648 gui_make_tearoff(eap->arg);
8649}
8650#endif
8651
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008652#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008654ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008656 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657}
8658#endif
8659
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008661ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662{
8663 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8664 MSG(_("No swap file"));
8665 else
8666 msg(curbuf->b_ml.ml_mfp->mf_fname);
8667}
8668
8669/*
8670 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8671 * offset.
8672 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8673 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008675ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676{
8677#ifdef FEAT_SCROLLBIND
8678 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008679 win_T *save_curwin = curwin;
8680 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 long topline;
8682 long y;
8683 linenr_T old_linenr = curwin->w_cursor.lnum;
8684
8685 setpcmark();
8686
8687 /*
8688 * determine max topline
8689 */
8690 if (curwin->w_p_scb)
8691 {
8692 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008693 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 {
8695 if (wp->w_p_scb && wp->w_buffer)
8696 {
8697 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8698 if (topline > y)
8699 topline = y;
8700 }
8701 }
8702 if (topline < 1)
8703 topline = 1;
8704 }
8705 else
8706 {
8707 topline = 1;
8708 }
8709
8710
8711 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008712 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008714 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 {
8716 if (curwin->w_p_scb)
8717 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008718 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 y = topline - curwin->w_topline;
8720 if (y > 0)
8721 scrollup(y, TRUE);
8722 else
8723 scrolldown(-y, TRUE);
8724 curwin->w_scbind_pos = topline;
8725 redraw_later(VALID);
8726 cursor_correct();
8727#ifdef FEAT_WINDOWS
8728 curwin->w_redr_status = TRUE;
8729#endif
8730 }
8731 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008732 curwin = save_curwin;
8733 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 if (curwin->w_p_scb)
8735 {
8736 did_syncbind = TRUE;
8737 checkpcmark();
8738 if (old_linenr != curwin->w_cursor.lnum)
8739 {
8740 char_u ctrl_o[2];
8741
8742 ctrl_o[0] = Ctrl_O;
8743 ctrl_o[1] = 0;
8744 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8745 }
8746 }
8747#endif
8748}
8749
8750
8751 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008752ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008754 int i;
8755 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8756 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757
8758 if (eap->usefilter) /* :r!cmd */
8759 do_bang(1, eap, FALSE, FALSE, TRUE);
8760 else
8761 {
8762 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8763 return;
8764
8765#ifdef FEAT_BROWSE
8766 if (cmdmod.browse)
8767 {
8768 char_u *browseFile;
8769
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008770 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 NULL, NULL, NULL, curbuf);
8772 if (browseFile != NULL)
8773 {
8774 i = readfile(browseFile, NULL,
8775 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8776 vim_free(browseFile);
8777 }
8778 else
8779 i = OK;
8780 }
8781 else
8782#endif
8783 if (*eap->arg == NUL)
8784 {
8785 if (check_fname() == FAIL) /* check for no file name */
8786 return;
8787 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8788 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8789 }
8790 else
8791 {
8792 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8793 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8794 i = readfile(eap->arg, NULL,
8795 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8796
8797 }
8798 if (i == FAIL)
8799 {
8800#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8801 if (!aborting())
8802#endif
8803 EMSG2(_(e_notopen), eap->arg);
8804 }
8805 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008806 {
8807 if (empty && exmode_active)
8808 {
8809 /* Delete the empty line that remains. Historically ex does
8810 * this but vi doesn't. */
8811 if (eap->line2 == 0)
8812 lnum = curbuf->b_ml.ml_line_count;
8813 else
8814 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008815 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008816 {
8817 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008818 if (curwin->w_cursor.lnum > 1
8819 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008820 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008821 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008822 }
8823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 }
8827}
8828
Bram Moolenaarea408852005-06-25 22:49:46 +00008829static char_u *prev_dir = NULL;
8830
8831#if defined(EXITFREE) || defined(PROTO)
8832 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008833free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00008834{
8835 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008836 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008837
8838 vim_free(globaldir);
8839 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008840}
8841#endif
8842
Bram Moolenaarf4258302013-06-02 18:20:17 +02008843/*
8844 * Deal with the side effects of changing the current directory.
8845 * When "local" is TRUE then this was after an ":lcd" command.
8846 */
8847 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008848post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008849{
8850 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008851 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008852 if (local)
8853 {
8854 /* If still in global directory, need to remember current
8855 * directory as global directory. */
8856 if (globaldir == NULL && prev_dir != NULL)
8857 globaldir = vim_strsave(prev_dir);
8858 /* Remember this local directory for the window. */
8859 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8860 curwin->w_localdir = vim_strsave(NameBuff);
8861 }
8862 else
8863 {
8864 /* We are now in the global directory, no need to remember its
8865 * name. */
8866 vim_free(globaldir);
8867 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008868 }
8869
8870 shorten_fnames(TRUE);
8871}
8872
Bram Moolenaarea408852005-06-25 22:49:46 +00008873
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874/*
8875 * ":cd", ":lcd", ":chdir" and ":lchdir".
8876 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008877 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008878ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 char_u *new_dir;
8881 char_u *tofree;
8882
8883 new_dir = eap->arg;
8884#if !defined(UNIX) && !defined(VMS)
8885 /* for non-UNIX ":cd" means: print current directory */
8886 if (*new_dir == NUL)
8887 ex_pwd(NULL);
8888 else
8889#endif
8890 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008891#ifdef FEAT_AUTOCMD
8892 if (allbuf_locked())
8893 return;
8894#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008895 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8896 && !eap->forceit)
8897 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008898 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008899 return;
8900 }
8901
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 /* ":cd -": Change to previous directory */
8903 if (STRCMP(new_dir, "-") == 0)
8904 {
8905 if (prev_dir == NULL)
8906 {
8907 EMSG(_("E186: No previous directory"));
8908 return;
8909 }
8910 new_dir = prev_dir;
8911 }
8912
8913 /* Save current directory for next ":cd -" */
8914 tofree = prev_dir;
8915 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8916 prev_dir = vim_strsave(NameBuff);
8917 else
8918 prev_dir = NULL;
8919
8920#if defined(UNIX) || defined(VMS)
8921 /* for UNIX ":cd" means: go to home directory */
8922 if (*new_dir == NUL)
8923 {
8924 /* use NameBuff for home directory name */
8925# ifdef VMS
8926 char_u *p;
8927
8928 p = mch_getenv((char_u *)"SYS$LOGIN");
8929 if (p == NULL || *p == NUL) /* empty is the same as not set */
8930 NameBuff[0] = NUL;
8931 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008932 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933# else
8934 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8935# endif
8936 new_dir = NameBuff;
8937 }
8938#endif
8939 if (new_dir == NULL || vim_chdir(new_dir))
8940 EMSG(_(e_failed));
8941 else
8942 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008943 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944
8945 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008946 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 ex_pwd(eap);
8948 }
8949 vim_free(tofree);
8950 }
8951}
8952
8953/*
8954 * ":pwd".
8955 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008957ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958{
8959 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8960 {
8961#ifdef BACKSLASH_IN_FILENAME
8962 slash_adjust(NameBuff);
8963#endif
8964 msg(NameBuff);
8965 }
8966 else
8967 EMSG(_("E187: Unknown"));
8968}
8969
8970/*
8971 * ":=".
8972 */
8973 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008974ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975{
8976 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008977 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978}
8979
8980 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008981ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008983 int n;
8984 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985
8986 if (cursor_valid())
8987 {
8988 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8989 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01008990 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008992
8993 len = eap->line2;
8994 switch (*eap->arg)
8995 {
8996 case 'm': break;
8997 case NUL: len *= 1000L; break;
8998 default: EMSG2(_(e_invarg2), eap->arg); return;
8999 }
9000 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001}
9002
9003/*
9004 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9005 */
9006 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009007do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008{
9009 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009010 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011
9012 cursor_on();
9013 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009014 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009016 wait_now = msec - done > 1000L ? 1000L : msec - done;
9017#ifdef FEAT_TIMERS
9018 {
9019 long due_time = check_due_timer();
9020
9021 if (due_time > 0 && due_time < wait_now)
9022 wait_now = due_time;
9023 }
9024#endif
9025 ui_delay(wait_now, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009027#ifdef MESSAGE_QUEUE
9028 /* Process the netbeans and clientserver messages that may have been
9029 * received in the call to ui_breakcheck() when the GUI is in use. This
9030 * may occur when running a test case. */
9031 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 }
9034}
9035
9036 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009037do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038{
9039 int mode;
9040 char_u *cmdp;
9041
9042 cmdp = eap->cmd;
9043 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9044
9045 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9046 eap->arg, mode, isabbrev))
9047 {
9048 case 1: EMSG(_(e_invarg));
9049 break;
9050 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9051 break;
9052 }
9053}
9054
9055/*
9056 * ":winsize" command (obsolete).
9057 */
9058 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009059ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060{
9061 int w, h;
9062 char_u *arg = eap->arg;
9063 char_u *p;
9064
9065 w = getdigits(&arg);
9066 arg = skipwhite(arg);
9067 p = arg;
9068 h = getdigits(&arg);
9069 if (*p != NUL && *arg == NUL)
9070 set_shellsize(w, h, TRUE);
9071 else
9072 EMSG(_("E465: :winsize requires two number arguments"));
9073}
9074
9075#ifdef FEAT_WINDOWS
9076 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009077ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078{
9079 int xchar = NUL;
9080 char_u *p;
9081
9082 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9083 {
9084 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9085 if (eap->arg[1] == NUL)
9086 {
9087 EMSG(_(e_invarg));
9088 return;
9089 }
9090 xchar = eap->arg[1];
9091 p = eap->arg + 2;
9092 }
9093 else
9094 p = eap->arg + 1;
9095
9096 eap->nextcmd = check_nextcmd(p);
9097 p = skipwhite(p);
9098 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9099 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009100 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 {
9102 /* Pass flags on for ":vertical wincmd ]". */
9103 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009104 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9106 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009107 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108 }
9109}
9110#endif
9111
Bram Moolenaar843ee412004-06-30 16:16:41 +00009112#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113/*
9114 * ":winpos".
9115 */
9116 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009117ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118{
9119 int x, y;
9120 char_u *arg = eap->arg;
9121 char_u *p;
9122
9123 if (*arg == NUL)
9124 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009125# if defined(FEAT_GUI) || defined(MSWIN)
9126# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009128# else
9129 if (mch_get_winpos(&x, &y) != FAIL)
9130# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 {
9132 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9133 msg(IObuff);
9134 }
9135 else
9136# endif
9137 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9138 }
9139 else
9140 {
9141 x = getdigits(&arg);
9142 arg = skipwhite(arg);
9143 p = arg;
9144 y = getdigits(&arg);
9145 if (*p == NUL || *arg != NUL)
9146 {
9147 EMSG(_("E466: :winpos requires two number arguments"));
9148 return;
9149 }
9150# ifdef FEAT_GUI
9151 if (gui.in_use)
9152 gui_mch_set_winpos(x, y);
9153 else if (gui.starting)
9154 {
9155 /* Remember the coordinates for when the window is opened. */
9156 gui_win_x = x;
9157 gui_win_y = y;
9158 }
9159# ifdef HAVE_TGETENT
9160 else
9161# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009162# else
9163# ifdef MSWIN
9164 mch_set_winpos(x, y);
9165# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166# endif
9167# ifdef HAVE_TGETENT
9168 if (*T_CWP)
9169 term_set_winpos(x, y);
9170# endif
9171 }
9172}
9173#endif
9174
9175/*
9176 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9177 */
9178 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009179ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180{
9181 oparg_T oa;
9182
9183 clear_oparg(&oa);
9184 oa.regname = eap->regname;
9185 oa.start.lnum = eap->line1;
9186 oa.end.lnum = eap->line2;
9187 oa.line_count = eap->line2 - eap->line1 + 1;
9188 oa.motion_type = MLINE;
9189#ifdef FEAT_VIRTUALEDIT
9190 virtual_op = FALSE;
9191#endif
9192 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9193 {
9194 setpcmark();
9195 curwin->w_cursor.lnum = eap->line1;
9196 beginline(BL_SOL | BL_FIX);
9197 }
9198
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009199 if (VIsual_active)
9200 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009201
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 switch (eap->cmdidx)
9203 {
9204 case CMD_delete:
9205 oa.op_type = OP_DELETE;
9206 op_delete(&oa);
9207 break;
9208
9209 case CMD_yank:
9210 oa.op_type = OP_YANK;
9211 (void)op_yank(&oa, FALSE, TRUE);
9212 break;
9213
9214 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009215 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009217 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9218#else
9219 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009221 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222 oa.op_type = OP_RSHIFT;
9223 else
9224 oa.op_type = OP_LSHIFT;
9225 op_shift(&oa, FALSE, eap->amount);
9226 break;
9227 }
9228#ifdef FEAT_VIRTUALEDIT
9229 virtual_op = MAYBE;
9230#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009231 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232}
9233
9234/*
9235 * ":put".
9236 */
9237 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009238ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239{
9240 /* ":0put" works like ":1put!". */
9241 if (eap->line2 == 0)
9242 {
9243 eap->line2 = 1;
9244 eap->forceit = TRUE;
9245 }
9246 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009247 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9248 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249}
9250
9251/*
9252 * Handle ":copy" and ":move".
9253 */
9254 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009255ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256{
9257 long n;
9258
Bram Moolenaaraa23b372015-09-08 18:46:31 +02009259 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260 if (eap->arg == NULL) /* error detected */
9261 {
9262 eap->nextcmd = NULL;
9263 return;
9264 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009265 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266
9267 /*
9268 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9269 */
9270 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9271 {
9272 EMSG(_(e_invaddr));
9273 return;
9274 }
9275
9276 if (eap->cmdidx == CMD_move)
9277 {
9278 if (do_move(eap->line1, eap->line2, n) == FAIL)
9279 return;
9280 }
9281 else
9282 ex_copy(eap->line1, eap->line2, n);
9283 u_clearline();
9284 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009285 ex_may_print(eap);
9286}
9287
9288/*
9289 * Print the current line if flags were given to the Ex command.
9290 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009291 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009292ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009293{
9294 if (eap->flags != 0)
9295 {
9296 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9297 (eap->flags & EXFLAG_LIST));
9298 ex_no_reprint = TRUE;
9299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300}
9301
9302/*
9303 * ":smagic" and ":snomagic".
9304 */
9305 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009306ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307{
9308 int magic_save = p_magic;
9309
9310 p_magic = (eap->cmdidx == CMD_smagic);
9311 do_sub(eap);
9312 p_magic = magic_save;
9313}
9314
9315/*
9316 * ":join".
9317 */
9318 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009319ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320{
9321 curwin->w_cursor.lnum = eap->line1;
9322 if (eap->line1 == eap->line2)
9323 {
9324 if (eap->addr_count >= 2) /* :2,2join does nothing */
9325 return;
9326 if (eap->line2 == curbuf->b_ml.ml_line_count)
9327 {
9328 beep_flush();
9329 return;
9330 }
9331 ++eap->line2;
9332 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009333 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009335 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336}
9337
9338/*
9339 * ":[addr]@r" or ":[addr]*r": execute register
9340 */
9341 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009342ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343{
9344 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009345 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346
9347 curwin->w_cursor.lnum = eap->line2;
9348
9349#ifdef USE_ON_FLY_SCROLL
9350 dont_scroll = TRUE; /* disallow scrolling here */
9351#endif
9352
9353 /* get the register name. No name means to use the previous one */
9354 c = *eap->arg;
9355 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9356 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009357 /* Put the register in the typeahead buffer with the "silent" flag. */
9358 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9359 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009360 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 else
9364 {
9365 int save_efr = exec_from_reg;
9366
9367 exec_from_reg = TRUE;
9368
9369 /*
9370 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009371 * Continue until the stuff buffer is empty and all added characters
9372 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009374 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9376
9377 exec_from_reg = save_efr;
9378 }
9379}
9380
9381/*
9382 * ":!".
9383 */
9384 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009385ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386{
9387 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9388}
9389
9390/*
9391 * ":undo".
9392 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009394ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009396 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009397 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009398 else
9399 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400}
9401
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009402#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009403 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009404ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009405{
9406 char_u hash[UNDO_HASH_SIZE];
9407
9408 u_compute_hash(hash);
9409 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9410}
9411
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009412 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009413ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009414{
9415 char_u hash[UNDO_HASH_SIZE];
9416
9417 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009418 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009419}
9420#endif
9421
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422/*
9423 * ":redo".
9424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009426ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427{
9428 u_redo(1);
9429}
9430
9431/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009432 * ":earlier" and ":later".
9433 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009434 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009435ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009436{
9437 long count = 0;
9438 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009439 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009440 char_u *p = eap->arg;
9441
9442 if (*p == NUL)
9443 count = 1;
9444 else if (isdigit(*p))
9445 {
9446 count = getdigits(&p);
9447 switch (*p)
9448 {
9449 case 's': ++p; sec = TRUE; break;
9450 case 'm': ++p; sec = TRUE; count *= 60; break;
9451 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009452 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9453 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009454 }
9455 }
9456
9457 if (*p != NUL)
9458 EMSG2(_(e_invarg2), eap->arg);
9459 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009460 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9461 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009462}
9463
9464/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 * ":redir": start/stop redirection.
9466 */
9467 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009468ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469{
9470 char *mode;
9471 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009472 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473
Bram Moolenaarba768492016-07-08 15:32:54 +02009474#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009475 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009476 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009477 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009478 return;
9479 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009480#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009481
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 if (STRICMP(eap->arg, "END") == 0)
9483 close_redir();
9484 else
9485 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009486 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009488 ++arg;
9489 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009491 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 mode = "a";
9493 }
9494 else
9495 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009496 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497
9498 close_redir();
9499
9500 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009501 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 if (fname == NULL)
9503 return;
9504#ifdef FEAT_BROWSE
9505 if (cmdmod.browse)
9506 {
9507 char_u *browseFile;
9508
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009509 browseFile = do_browse(BROWSE_SAVE,
9510 (char_u *)_("Save Redirection"),
9511 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 if (browseFile == NULL)
9513 return; /* operation cancelled */
9514 vim_free(fname);
9515 fname = browseFile;
9516 eap->forceit = TRUE; /* since dialog already asked */
9517 }
9518#endif
9519
9520 redir_fd = open_exfile(fname, eap->forceit, mode);
9521 vim_free(fname);
9522 }
9523#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009524 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525 {
9526 /* redirect to a register a-z (resp. A-Z for appending) */
9527 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009528 ++arg;
9529 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009531 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009532 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009534 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009536 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009537 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009538 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009539 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009541 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009542 if (*arg == '>')
9543 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009544 /* Make register empty when not using @A-@Z and the
9545 * command is valid. */
9546 if (*arg == NUL && !isupper(redir_reg))
9547 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009549 }
9550 if (*arg != NUL)
9551 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009552 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009553 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009554 }
9555 }
9556 else if (*arg == '=' && arg[1] == '>')
9557 {
9558 int append;
9559
9560 /* redirect to a variable */
9561 close_redir();
9562 arg += 2;
9563
9564 if (*arg == '>')
9565 {
9566 ++arg;
9567 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568 }
9569 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009570 append = FALSE;
9571
9572 if (var_redir_start(skipwhite(arg), append) == OK)
9573 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 }
9575#endif
9576
9577 /* TODO: redirect to a buffer */
9578
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009580 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009582
9583 /* Make sure redirection is not off. Can happen for cmdline completion
9584 * that indirectly invokes a command to catch its output. */
9585 if (redir_fd != NULL
9586#ifdef FEAT_EVAL
9587 || redir_reg || redir_vname
9588#endif
9589 )
9590 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591}
9592
9593/*
9594 * ":redraw": force redraw
9595 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009596 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009597ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598{
9599 int r = RedrawingDisabled;
9600 int p = p_lz;
9601
9602 RedrawingDisabled = 0;
9603 p_lz = FALSE;
9604 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009605 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606#ifdef FEAT_TITLE
9607 if (need_maketitle)
9608 maketitle();
9609#endif
9610 RedrawingDisabled = r;
9611 p_lz = p;
9612
9613 /* Reset msg_didout, so that a message that's there is overwritten. */
9614 msg_didout = FALSE;
9615 msg_col = 0;
9616
Bram Moolenaar56667a52013-07-17 11:54:28 +02009617 /* No need to wait after an intentional redraw. */
9618 need_wait_return = FALSE;
9619
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 out_flush();
9621}
9622
9623/*
9624 * ":redrawstatus": force redraw of status line(s)
9625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009627ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628{
9629#if defined(FEAT_WINDOWS)
9630 int r = RedrawingDisabled;
9631 int p = p_lz;
9632
9633 RedrawingDisabled = 0;
9634 p_lz = FALSE;
9635 if (eap->forceit)
9636 status_redraw_all();
9637 else
9638 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009639 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640 RedrawingDisabled = r;
9641 p_lz = p;
9642 out_flush();
9643#endif
9644}
9645
9646 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009647close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648{
9649 if (redir_fd != NULL)
9650 {
9651 fclose(redir_fd);
9652 redir_fd = NULL;
9653 }
9654#ifdef FEAT_EVAL
9655 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009656 if (redir_vname)
9657 {
9658 var_redir_stop();
9659 redir_vname = 0;
9660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661#endif
9662}
9663
9664#if defined(FEAT_SESSION) && defined(USE_CRNL)
9665# define MKSESSION_NL
9666static int mksession_nl = FALSE; /* use NL only in put_eol() */
9667#endif
9668
9669/*
9670 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9671 */
9672 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009673ex_mkrc(
9674 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675{
9676 FILE *fd;
9677 int failed = FALSE;
9678 char_u *fname;
9679#ifdef FEAT_BROWSE
9680 char_u *browseFile = NULL;
9681#endif
9682#ifdef FEAT_SESSION
9683 int view_session = FALSE;
9684 int using_vdir = FALSE; /* using 'viewdir'? */
9685 char_u *viewFile = NULL;
9686 unsigned *flagp;
9687#endif
9688
9689 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9690 {
9691#ifdef FEAT_SESSION
9692 view_session = TRUE;
9693#else
9694 ex_ni(eap);
9695 return;
9696#endif
9697 }
9698
9699#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009700 /* Use the short file name until ":lcd" is used. We also don't use the
9701 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009702 did_lcd = FALSE;
9703
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9705 if (eap->cmdidx == CMD_mkview
9706 && (*eap->arg == NUL
9707 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9708 {
9709 eap->forceit = TRUE;
9710 fname = get_view_file(*eap->arg);
9711 if (fname == NULL)
9712 return;
9713 viewFile = fname;
9714 using_vdir = TRUE;
9715 }
9716 else
9717#endif
9718 if (*eap->arg != NUL)
9719 fname = eap->arg;
9720 else if (eap->cmdidx == CMD_mkvimrc)
9721 fname = (char_u *)VIMRC_FILE;
9722#ifdef FEAT_SESSION
9723 else if (eap->cmdidx == CMD_mksession)
9724 fname = (char_u *)SESSION_FILE;
9725#endif
9726 else
9727 fname = (char_u *)EXRC_FILE;
9728
9729#ifdef FEAT_BROWSE
9730 if (cmdmod.browse)
9731 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009732 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733# ifdef FEAT_SESSION
9734 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9735 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9736# endif
9737 (char_u *)_("Save Setup"),
9738 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9739 if (browseFile == NULL)
9740 goto theend;
9741 fname = browseFile;
9742 eap->forceit = TRUE; /* since dialog already asked */
9743 }
9744#endif
9745
9746#if defined(FEAT_SESSION) && defined(vim_mkdir)
9747 /* When using 'viewdir' may have to create the directory. */
9748 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009749 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750#endif
9751
9752 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9753 if (fd != NULL)
9754 {
9755#ifdef FEAT_SESSION
9756 if (eap->cmdidx == CMD_mkview)
9757 flagp = &vop_flags;
9758 else
9759 flagp = &ssop_flags;
9760#endif
9761
9762#ifdef MKSESSION_NL
9763 /* "unix" in 'sessionoptions': use NL line separator */
9764 if (view_session && (*flagp & SSOP_UNIX))
9765 mksession_nl = TRUE;
9766#endif
9767
9768 /* Write the version command for :mkvimrc */
9769 if (eap->cmdidx == CMD_mkvimrc)
9770 (void)put_line(fd, "version 6.0");
9771
9772#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009773 if (eap->cmdidx == CMD_mksession)
9774 {
9775 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9776 failed = TRUE;
9777 }
9778
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 if (eap->cmdidx != CMD_mkview)
9780#endif
9781 {
9782 /* Write setting 'compatible' first, because it has side effects.
9783 * For that same reason only do it when needed. */
9784 if (p_cp)
9785 (void)put_line(fd, "if !&cp | set cp | endif");
9786 else
9787 (void)put_line(fd, "if &cp | set nocp | endif");
9788 }
9789
9790#ifdef FEAT_SESSION
9791 if (!view_session
9792 || (eap->cmdidx == CMD_mksession
9793 && (*flagp & SSOP_OPTIONS)))
9794#endif
9795 failed |= (makemap(fd, NULL) == FAIL
9796 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9797
9798#ifdef FEAT_SESSION
9799 if (!failed && view_session)
9800 {
9801 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9802 failed = TRUE;
9803 if (eap->cmdidx == CMD_mksession)
9804 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009805 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806
Bram Moolenaard9462e32011-04-11 21:35:11 +02009807 dirnow = alloc(MAXPATHL);
9808 if (dirnow == NULL)
9809 failed = TRUE;
9810 else
9811 {
9812 /*
9813 * Change to session file's dir.
9814 */
9815 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009817 *dirnow = NUL;
9818 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9819 {
9820 if (vim_chdirfile(fname) == OK)
9821 shorten_fnames(TRUE);
9822 }
9823 else if (*dirnow != NUL
9824 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9825 {
9826 if (mch_chdir((char *)globaldir) == 0)
9827 shorten_fnames(TRUE);
9828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829
Bram Moolenaard9462e32011-04-11 21:35:11 +02009830 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831
Bram Moolenaard9462e32011-04-11 21:35:11 +02009832 /* restore original dir */
9833 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009835 {
9836 if (mch_chdir((char *)dirnow) != 0)
9837 EMSG(_(e_prev_dir));
9838 shorten_fnames(TRUE);
9839 }
9840 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 }
9842 }
9843 else
9844 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009845 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9846 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847 }
9848 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9849 == FAIL)
9850 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009851 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9852 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009853 if (eap->cmdidx == CMD_mksession)
9854 {
9855 if (put_line(fd, "unlet SessionLoad") == FAIL)
9856 failed = TRUE;
9857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 }
9859#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009860 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9861 failed = TRUE;
9862
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863 failed |= fclose(fd);
9864
9865 if (failed)
9866 EMSG(_(e_write));
9867#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9868 else if (eap->cmdidx == CMD_mksession)
9869 {
9870 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009871 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872
Bram Moolenaard9462e32011-04-11 21:35:11 +02009873 tbuf = alloc(MAXPATHL);
9874 if (tbuf != NULL)
9875 {
9876 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9877 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9878 vim_free(tbuf);
9879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 }
9881#endif
9882#ifdef MKSESSION_NL
9883 mksession_nl = FALSE;
9884#endif
9885 }
9886
9887#ifdef FEAT_BROWSE
9888theend:
9889 vim_free(browseFile);
9890#endif
9891#ifdef FEAT_SESSION
9892 vim_free(viewFile);
9893#endif
9894}
9895
Bram Moolenaardf177f62005-02-22 08:39:57 +00009896#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9897 || defined(PROTO)
9898 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009899vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009900{
9901 if (vim_mkdir(name, prot) != 0)
9902 {
9903 EMSG2(_("E739: Cannot create directory: %s"), name);
9904 return FAIL;
9905 }
9906 return OK;
9907}
9908#endif
9909
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910/*
9911 * Open a file for writing for an Ex command, with some checks.
9912 * Return file descriptor, or NULL on failure.
9913 */
9914 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009915open_exfile(
9916 char_u *fname,
9917 int forceit,
9918 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919{
9920 FILE *fd;
9921
9922#ifdef UNIX
9923 /* with Unix it is possible to open a directory */
9924 if (mch_isdir(fname))
9925 {
9926 EMSG2(_(e_isadir2), fname);
9927 return NULL;
9928 }
9929#endif
9930 if (!forceit && *mode != 'a' && vim_fexists(fname))
9931 {
9932 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9933 return NULL;
9934 }
9935
9936 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9937 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9938
9939 return fd;
9940}
9941
9942/*
9943 * ":mark" and ":k".
9944 */
9945 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009946ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947{
9948 pos_T pos;
9949
9950 if (*eap->arg == NUL) /* No argument? */
9951 EMSG(_(e_argreq));
9952 else if (eap->arg[1] != NUL) /* more than one character? */
9953 EMSG(_(e_trailing));
9954 else
9955 {
9956 pos = curwin->w_cursor; /* save curwin->w_cursor */
9957 curwin->w_cursor.lnum = eap->line2;
9958 beginline(BL_WHITE | BL_FIX);
9959 if (setmark(*eap->arg) == FAIL) /* set mark */
9960 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9961 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9962 }
9963}
9964
9965/*
9966 * Update w_topline, w_leftcol and the cursor position.
9967 */
9968 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009969update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970{
9971 check_cursor(); /* put cursor on valid line */
9972 update_topline();
9973 if (!curwin->w_p_wrap)
9974 validate_cursor();
9975 update_curswant();
9976}
9977
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978/*
9979 * ":normal[!] {commands}": Execute normal mode commands.
9980 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01009981 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009982ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 int save_msg_scroll = msg_scroll;
9985 int save_restart_edit = restart_edit;
9986 int save_msg_didout = msg_didout;
9987 int save_State = State;
9988 tasave_T tabuf;
9989 int save_insertmode = p_im;
9990 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009991 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992#ifdef FEAT_MBYTE
9993 char_u *arg = NULL;
9994 int l;
9995 char_u *p;
9996#endif
9997
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009998 if (ex_normal_lock > 0)
9999 {
10000 EMSG(_(e_secure));
10001 return;
10002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 if (ex_normal_busy >= p_mmd)
10004 {
10005 EMSG(_("E192: Recursive use of :normal too deep"));
10006 return;
10007 }
10008 ++ex_normal_busy;
10009
10010 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10011 restart_edit = 0; /* don't go to Insert mode */
10012 p_im = FALSE; /* don't use 'insertmode' */
10013
10014#ifdef FEAT_MBYTE
10015 /*
10016 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10017 * this for the K_SPECIAL leading byte, otherwise special keys will not
10018 * work.
10019 */
10020 if (has_mbyte)
10021 {
10022 int len = 0;
10023
10024 /* Count the number of characters to be escaped. */
10025 for (p = eap->arg; *p != NUL; ++p)
10026 {
10027# ifdef FEAT_GUI
10028 if (*p == CSI) /* leadbyte CSI */
10029 len += 2;
10030# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010031 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10033# ifdef FEAT_GUI
10034 || *p == CSI
10035# endif
10036 )
10037 len += 2;
10038 }
10039 if (len > 0)
10040 {
10041 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10042 if (arg != NULL)
10043 {
10044 len = 0;
10045 for (p = eap->arg; *p != NUL; ++p)
10046 {
10047 arg[len++] = *p;
10048# ifdef FEAT_GUI
10049 if (*p == CSI)
10050 {
10051 arg[len++] = KS_EXTRA;
10052 arg[len++] = (int)KE_CSI;
10053 }
10054# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010055 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056 {
10057 arg[len++] = *++p;
10058 if (*p == K_SPECIAL)
10059 {
10060 arg[len++] = KS_SPECIAL;
10061 arg[len++] = KE_FILLER;
10062 }
10063# ifdef FEAT_GUI
10064 else if (*p == CSI)
10065 {
10066 arg[len++] = KS_EXTRA;
10067 arg[len++] = (int)KE_CSI;
10068 }
10069# endif
10070 }
10071 arg[len] = NUL;
10072 }
10073 }
10074 }
10075 }
10076#endif
10077
10078 /*
10079 * Save the current typeahead. This is required to allow using ":normal"
10080 * from an event handler and makes sure we don't hang when the argument
10081 * ends with half a command.
10082 */
10083 save_typeahead(&tabuf);
10084 if (tabuf.typebuf_valid)
10085 {
10086 /*
10087 * Repeat the :normal command for each line in the range. When no
10088 * range given, execute it just once, without positioning the cursor
10089 * first.
10090 */
10091 do
10092 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 if (eap->addr_count != 0)
10094 {
10095 curwin->w_cursor.lnum = eap->line1++;
10096 curwin->w_cursor.col = 0;
10097 }
10098
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010099 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100#ifdef FEAT_MBYTE
10101 arg != NULL ? arg :
10102#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010103 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 }
10105 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10106 }
10107
10108 /* Might not return to the main loop when in an event handler. */
10109 update_topline_cursor();
10110
10111 /* Restore the previous typeahead. */
10112 restore_typeahead(&tabuf);
10113
10114 --ex_normal_busy;
10115 msg_scroll = save_msg_scroll;
10116 restart_edit = save_restart_edit;
10117 p_im = save_insertmode;
10118 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010119 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
10121
10122 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +010010123 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +010010125#ifdef FEAT_MOUSE
10126 setmouse();
10127#endif
10128#ifdef CURSOR_SHAPE
10129 ui_cursor_shape(); /* may show different cursor shape */
10130#endif
10131
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132#ifdef FEAT_MBYTE
10133 vim_free(arg);
10134#endif
10135}
10136
10137/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010138 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 */
10140 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010141ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010143 if (eap->forceit)
10144 {
10145 coladvance((colnr_T)MAXCOL);
10146 curwin->w_curswant = MAXCOL;
10147 curwin->w_set_curswant = FALSE;
10148 }
10149
Bram Moolenaara40c5002005-01-09 21:16:21 +000010150 /* Ignore the command when already in Insert mode. Inserting an
10151 * expression register that invokes a function can do this. */
10152 if (State & INSERT)
10153 return;
10154
Bram Moolenaar64969662005-12-14 21:59:55 +000010155 if (eap->cmdidx == CMD_startinsert)
10156 restart_edit = 'a';
10157 else if (eap->cmdidx == CMD_startreplace)
10158 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010160 restart_edit = 'V';
10161
10162 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010164 if (eap->cmdidx == CMD_startinsert)
10165 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166 curwin->w_curswant = 0; /* avoid MAXCOL */
10167 }
10168}
10169
10170/*
10171 * ":stopinsert"
10172 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010174ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175{
10176 restart_edit = 0;
10177 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010178 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010181/*
10182 * Execute normal mode command "cmd".
10183 * "remap" can be REMAP_NONE or REMAP_YES.
10184 */
10185 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010186exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010187{
Bram Moolenaar25281632016-01-21 23:32:32 +010010188 /* Stuff the argument into the typeahead buffer. */
10189 ins_typebuf(cmd, remap, 0, TRUE, silent);
10190 exec_normal(FALSE);
10191}
Bram Moolenaar25281632016-01-21 23:32:32 +010010192
Bram Moolenaar25281632016-01-21 23:32:32 +010010193/*
10194 * Execute normal_cmd() until there is no typeahead left.
10195 */
10196 void
10197exec_normal(int was_typed)
10198{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010199 oparg_T oa;
10200
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010201 clear_oparg(&oa);
10202 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010203 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10204 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010205 {
10206 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010207 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010208 }
10209}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010210
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211#ifdef FEAT_FIND_ID
10212 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010213ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214{
10215 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10216 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10217 (linenr_T)1, (linenr_T)MAXLNUM);
10218}
10219
10220#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
10221/*
10222 * ":psearch"
10223 */
10224 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010225ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226{
10227 g_do_tagpreview = p_pvh;
10228 ex_findpat(eap);
10229 g_do_tagpreview = 0;
10230}
10231#endif
10232
10233 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010234ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235{
10236 int whole = TRUE;
10237 long n;
10238 char_u *p;
10239 int action;
10240
10241 switch (cmdnames[eap->cmdidx].cmd_name[2])
10242 {
10243 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10244 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10245 action = ACTION_GOTO;
10246 else
10247 action = ACTION_SHOW;
10248 break;
10249 case 'i': /* ":ilist" and ":dlist" */
10250 action = ACTION_SHOW_ALL;
10251 break;
10252 case 'u': /* ":ijump" and ":djump" */
10253 action = ACTION_GOTO;
10254 break;
10255 default: /* ":isplit" and ":dsplit" */
10256 action = ACTION_SPLIT;
10257 break;
10258 }
10259
10260 n = 1;
10261 if (vim_isdigit(*eap->arg)) /* get count */
10262 {
10263 n = getdigits(&eap->arg);
10264 eap->arg = skipwhite(eap->arg);
10265 }
10266 if (*eap->arg == '/') /* Match regexp, not just whole words */
10267 {
10268 whole = FALSE;
10269 ++eap->arg;
10270 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10271 if (*p)
10272 {
10273 *p++ = NUL;
10274 p = skipwhite(p);
10275
10276 /* Check for trailing illegal characters */
10277 if (!ends_excmd(*p))
10278 eap->errmsg = e_trailing;
10279 else
10280 eap->nextcmd = check_nextcmd(p);
10281 }
10282 }
10283 if (!eap->skip)
10284 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10285 whole, !eap->forceit,
10286 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10287 n, action, eap->line1, eap->line2);
10288}
10289#endif
10290
10291#ifdef FEAT_WINDOWS
10292
10293# ifdef FEAT_QUICKFIX
10294/*
10295 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10296 */
10297 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010298ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010300 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10302}
10303
10304/*
10305 * ":pedit"
10306 */
10307 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010308ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309{
10310 win_T *curwin_save = curwin;
10311
10312 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010313 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314 keep_help_flag = curwin_save->w_buffer->b_help;
10315 do_exedit(eap, NULL);
10316 keep_help_flag = FALSE;
10317 if (curwin != curwin_save && win_valid(curwin_save))
10318 {
10319 /* Return cursor to where we were */
10320 validate_cursor();
10321 redraw_later(VALID);
10322 win_enter(curwin_save, TRUE);
10323 }
10324 g_do_tagpreview = 0;
10325}
10326# endif
10327
10328/*
10329 * ":stag", ":stselect" and ":stjump".
10330 */
10331 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010332ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333{
10334 postponed_split = -1;
10335 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010336 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10338 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010339 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340}
10341#endif
10342
10343/*
10344 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10345 */
10346 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010347ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348{
10349 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10350}
10351
10352 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010353ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354{
10355 int cmd;
10356
10357 switch (name[1])
10358 {
10359 case 'j': cmd = DT_JUMP; /* ":tjump" */
10360 break;
10361 case 's': cmd = DT_SELECT; /* ":tselect" */
10362 break;
10363 case 'p': cmd = DT_PREV; /* ":tprevious" */
10364 break;
10365 case 'N': cmd = DT_PREV; /* ":tNext" */
10366 break;
10367 case 'n': cmd = DT_NEXT; /* ":tnext" */
10368 break;
10369 case 'o': cmd = DT_POP; /* ":pop" */
10370 break;
10371 case 'f': /* ":tfirst" */
10372 case 'r': cmd = DT_FIRST; /* ":trewind" */
10373 break;
10374 case 'l': cmd = DT_LAST; /* ":tlast" */
10375 break;
10376 default: /* ":tag" */
10377#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010378 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379 {
10380 do_cstag(eap);
10381 return;
10382 }
10383#endif
10384 cmd = DT_TAG;
10385 break;
10386 }
10387
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010388 if (name[0] == 'l')
10389 {
10390#ifndef FEAT_QUICKFIX
10391 ex_ni(eap);
10392 return;
10393#else
10394 cmd = DT_LTAG;
10395#endif
10396 }
10397
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10399 eap->forceit, TRUE);
10400}
10401
10402/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010403 * Check "str" for starting with a special cmdline variable.
10404 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10405 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10406 */
10407 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010408find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010409{
10410 int len;
10411 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010412 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010413 "%",
10414#define SPEC_PERC 0
10415 "#",
10416#define SPEC_HASH 1
10417 "<cword>", /* cursor word */
10418#define SPEC_CWORD 2
10419 "<cWORD>", /* cursor WORD */
10420#define SPEC_CCWORD 3
10421 "<cfile>", /* cursor path name */
10422#define SPEC_CFILE 4
10423 "<sfile>", /* ":so" file name */
10424#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010425 "<slnum>", /* ":so" file line number */
10426#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010427#ifdef FEAT_AUTOCMD
10428 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010429# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010430 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010431# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010432 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010433# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010434#endif
10435#ifdef FEAT_CLIENTSERVER
10436 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010437# ifdef FEAT_AUTOCMD
10438# define SPEC_CLIENT 10
10439# else
10440# define SPEC_CLIENT 7
10441# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010442#endif
10443 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010444
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010445 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010446 {
10447 len = (int)STRLEN(spec_str[i]);
10448 if (STRNCMP(src, spec_str[i], len) == 0)
10449 {
10450 *usedlen = len;
10451 return i;
10452 }
10453 }
10454 return -1;
10455}
10456
10457/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458 * Evaluate cmdline variables.
10459 *
10460 * change '%' to curbuf->b_ffname
10461 * '#' to curwin->w_altfile
10462 * '<cword>' to word under the cursor
10463 * '<cWORD>' to WORD under the cursor
10464 * '<cfile>' to path name under the cursor
10465 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010466 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 * '<afile>' to file name for autocommand
10468 * '<abuf>' to buffer number for autocommand
10469 * '<amatch>' to matching name for autocommand
10470 *
10471 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10472 * "" for error without a message) and NULL is returned.
10473 * Returns an allocated string if a valid match was found.
10474 * Returns NULL if no match was found. "usedlen" then still contains the
10475 * number of characters to skip.
10476 */
10477 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010478eval_vars(
10479 char_u *src, /* pointer into commandline */
10480 char_u *srcstart, /* beginning of valid memory for src */
10481 int *usedlen, /* characters after src that are used */
10482 linenr_T *lnump, /* line number for :e command, or NULL */
10483 char_u **errormsg, /* pointer to error message */
10484 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010485 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486{
10487 int i;
10488 char_u *s;
10489 char_u *result;
10490 char_u *resultbuf = NULL;
10491 int resultlen;
10492 buf_T *buf;
10493 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10494 int spec_idx;
10495#ifdef FEAT_MODIFY_FNAME
10496 int skip_mod = FALSE;
10497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499
10500 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010501 if (escaped != NULL)
10502 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503
10504 /*
10505 * Check if there is something to do.
10506 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010507 spec_idx = find_cmdline_var(src, usedlen);
10508 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 {
10510 *usedlen = 1;
10511 return NULL;
10512 }
10513
10514 /*
10515 * Skip when preceded with a backslash "\%" and "\#".
10516 * Note: In "\\%" the % is also not recognized!
10517 */
10518 if (src > srcstart && src[-1] == '\\')
10519 {
10520 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010521 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522 return NULL;
10523 }
10524
10525 /*
10526 * word or WORD under cursor
10527 */
10528 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10529 {
10530 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10531 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10532 if (resultlen == 0)
10533 {
10534 *errormsg = (char_u *)"";
10535 return NULL;
10536 }
10537 }
10538
10539 /*
10540 * '#': Alternate file name
10541 * '%': Current file name
10542 * File name under the cursor
10543 * File name for autocommand
10544 * and following modifiers
10545 */
10546 else
10547 {
10548 switch (spec_idx)
10549 {
10550 case SPEC_PERC: /* '%': current file */
10551 if (curbuf->b_fname == NULL)
10552 {
10553 result = (char_u *)"";
10554 valid = 0; /* Must have ":p:h" to be valid */
10555 }
10556 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558 break;
10559
10560 case SPEC_HASH: /* '#' or "#99": alternate file */
10561 if (src[1] == '#') /* "##": the argument list */
10562 {
10563 result = arg_all();
10564 resultbuf = result;
10565 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010566 if (escaped != NULL)
10567 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568#ifdef FEAT_MODIFY_FNAME
10569 skip_mod = TRUE;
10570#endif
10571 break;
10572 }
10573 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010574 if (*s == '<') /* "#<99" uses v:oldfiles */
10575 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576 i = (int)getdigits(&s);
10577 *usedlen = (int)(s - src); /* length of what we expand */
10578
Bram Moolenaard812df62008-11-09 12:46:09 +000010579 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010581 if (*usedlen < 2)
10582 {
10583 /* Should we give an error message for #<text? */
10584 *usedlen = 1;
10585 return NULL;
10586 }
10587#ifdef FEAT_EVAL
10588 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10589 (long)i);
10590 if (result == NULL)
10591 {
10592 *errormsg = (char_u *)"";
10593 return NULL;
10594 }
10595#else
10596 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599 }
10600 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010601 {
10602 buf = buflist_findnr(i);
10603 if (buf == NULL)
10604 {
10605 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10606 return NULL;
10607 }
10608 if (lnump != NULL)
10609 *lnump = ECMD_LAST;
10610 if (buf->b_fname == NULL)
10611 {
10612 result = (char_u *)"";
10613 valid = 0; /* Must have ":p:h" to be valid */
10614 }
10615 else
10616 result = buf->b_fname;
10617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618 break;
10619
10620#ifdef FEAT_SEARCHPATH
10621 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010622 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623 if (result == NULL)
10624 {
10625 *errormsg = (char_u *)"";
10626 return NULL;
10627 }
10628 resultbuf = result; /* remember allocated string */
10629 break;
10630#endif
10631
10632#ifdef FEAT_AUTOCMD
10633 case SPEC_AFILE: /* file name for autocommand */
10634 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010635 if (result != NULL && !autocmd_fname_full)
10636 {
10637 /* Still need to turn the fname into a full path. It is
10638 * postponed to avoid a delay when <afile> is not used. */
10639 autocmd_fname_full = TRUE;
10640 result = FullName_save(autocmd_fname, FALSE);
10641 vim_free(autocmd_fname);
10642 autocmd_fname = result;
10643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644 if (result == NULL)
10645 {
10646 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10647 return NULL;
10648 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010649 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650 break;
10651
10652 case SPEC_ABUF: /* buffer number for autocommand */
10653 if (autocmd_bufnr <= 0)
10654 {
10655 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10656 return NULL;
10657 }
10658 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10659 result = strbuf;
10660 break;
10661
10662 case SPEC_AMATCH: /* match name for autocommand */
10663 result = autocmd_match;
10664 if (result == NULL)
10665 {
10666 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10667 return NULL;
10668 }
10669 break;
10670
10671#endif
10672 case SPEC_SFILE: /* file name for ":so" command */
10673 result = sourcing_name;
10674 if (result == NULL)
10675 {
10676 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10677 return NULL;
10678 }
10679 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010680 case SPEC_SLNUM: /* line in file for ":so" command */
10681 if (sourcing_name == NULL || sourcing_lnum == 0)
10682 {
10683 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10684 return NULL;
10685 }
10686 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10687 result = strbuf;
10688 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689#if defined(FEAT_CLIENTSERVER)
10690 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010691 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10692 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693 result = strbuf;
10694 break;
10695#endif
10696 }
10697
10698 resultlen = (int)STRLEN(result); /* length of new string */
10699 if (src[*usedlen] == '<') /* remove the file name extension */
10700 {
10701 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703 resultlen = (int)(s - result);
10704 }
10705#ifdef FEAT_MODIFY_FNAME
10706 else if (!skip_mod)
10707 {
10708 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10709 &resultlen);
10710 if (result == NULL)
10711 {
10712 *errormsg = (char_u *)"";
10713 return NULL;
10714 }
10715 }
10716#endif
10717 }
10718
10719 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10720 {
10721 if (valid != VALID_HEAD + VALID_PATH)
10722 /* xgettext:no-c-format */
10723 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10724 else
10725 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10726 result = NULL;
10727 }
10728 else
10729 result = vim_strnsave(result, resultlen);
10730 vim_free(resultbuf);
10731 return result;
10732}
10733
10734/*
10735 * Concatenate all files in the argument list, separated by spaces, and return
10736 * it in one allocated string.
10737 * Spaces and backslashes in the file names are escaped with a backslash.
10738 * Returns NULL when out of memory.
10739 */
10740 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010741arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742{
10743 int len;
10744 int idx;
10745 char_u *retval = NULL;
10746 char_u *p;
10747
10748 /*
10749 * Do this loop two times:
10750 * first time: compute the total length
10751 * second time: concatenate the names
10752 */
10753 for (;;)
10754 {
10755 len = 0;
10756 for (idx = 0; idx < ARGCOUNT; ++idx)
10757 {
10758 p = alist_name(&ARGLIST[idx]);
10759 if (p != NULL)
10760 {
10761 if (len > 0)
10762 {
10763 /* insert a space in between names */
10764 if (retval != NULL)
10765 retval[len] = ' ';
10766 ++len;
10767 }
10768 for ( ; *p != NUL; ++p)
10769 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010770 if (*p == ' '
10771#ifndef BACKSLASH_IN_FILENAME
10772 || *p == '\\'
10773#endif
10774 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775 {
10776 /* insert a backslash */
10777 if (retval != NULL)
10778 retval[len] = '\\';
10779 ++len;
10780 }
10781 if (retval != NULL)
10782 retval[len] = *p;
10783 ++len;
10784 }
10785 }
10786 }
10787
10788 /* second time: break here */
10789 if (retval != NULL)
10790 {
10791 retval[len] = NUL;
10792 break;
10793 }
10794
10795 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010796 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 if (retval == NULL)
10798 break;
10799 }
10800
10801 return retval;
10802}
10803
10804#if defined(FEAT_AUTOCMD) || defined(PROTO)
10805/*
10806 * Expand the <sfile> string in "arg".
10807 *
10808 * Returns an allocated string, or NULL for any error.
10809 */
10810 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010811expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812{
10813 char_u *errormsg;
10814 int len;
10815 char_u *result;
10816 char_u *newres;
10817 char_u *repl;
10818 int srclen;
10819 char_u *p;
10820
10821 result = vim_strsave(arg);
10822 if (result == NULL)
10823 return NULL;
10824
10825 for (p = result; *p; )
10826 {
10827 if (STRNCMP(p, "<sfile>", 7) != 0)
10828 ++p;
10829 else
10830 {
10831 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010832 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833 if (errormsg != NULL)
10834 {
10835 if (*errormsg)
10836 emsg(errormsg);
10837 vim_free(result);
10838 return NULL;
10839 }
10840 if (repl == NULL) /* no match (cannot happen) */
10841 {
10842 p += srclen;
10843 continue;
10844 }
10845 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10846 newres = alloc(len);
10847 if (newres == NULL)
10848 {
10849 vim_free(repl);
10850 vim_free(result);
10851 return NULL;
10852 }
10853 mch_memmove(newres, result, (size_t)(p - result));
10854 STRCPY(newres + (p - result), repl);
10855 len = (int)STRLEN(newres);
10856 STRCAT(newres, p + srclen);
10857 vim_free(repl);
10858 vim_free(result);
10859 result = newres;
10860 p = newres + len; /* continue after the match */
10861 }
10862 }
10863
10864 return result;
10865}
10866#endif
10867
10868#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010010869static int ses_winsizes(FILE *fd, int restore_size,
10870 win_T *tab_firstwin);
10871static int ses_win_rec(FILE *fd, frame_T *fr);
10872static frame_T *ses_skipframe(frame_T *fr);
10873static int ses_do_frame(frame_T *fr);
10874static int ses_do_win(win_T *wp);
10875static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
10876static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
10877static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878
10879/*
10880 * Write openfile commands for the current buffers to an .exrc file.
10881 * Return FAIL on error, OK otherwise.
10882 */
10883 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010884makeopens(
10885 FILE *fd,
10886 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887{
10888 buf_T *buf;
10889 int only_save_windows = TRUE;
10890 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891 int restore_size = TRUE;
10892 win_T *wp;
10893 char_u *sname;
10894 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010895 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010896 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010897 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010898 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010899 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010900 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901
10902 if (ssop_flags & SSOP_BUFFERS)
10903 only_save_windows = FALSE; /* Save ALL buffers */
10904
10905 /*
10906 * Begin by setting the this_session variable, and then other
10907 * sessionable variables.
10908 */
10909#ifdef FEAT_EVAL
10910 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10911 return FAIL;
10912 if (ssop_flags & SSOP_GLOBALS)
10913 if (store_session_globals(fd) == FAIL)
10914 return FAIL;
10915#endif
10916
10917 /*
10918 * Close all windows but one.
10919 */
10920 if (put_line(fd, "silent only") == FAIL)
10921 return FAIL;
10922
10923 /*
10924 * Now a :cd command to the session directory or the current directory
10925 */
10926 if (ssop_flags & SSOP_SESDIR)
10927 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010928 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10929 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930 return FAIL;
10931 }
10932 else if (ssop_flags & SSOP_CURDIR)
10933 {
10934 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10935 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010936 || fputs("cd ", fd) < 0
10937 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10938 || put_eol(fd) == FAIL)
10939 {
10940 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943 vim_free(sname);
10944 }
10945
10946 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010947 * If there is an empty, unnamed buffer we will wipe it out later.
10948 * Remember the buffer number.
10949 */
10950 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10951 return FAIL;
10952 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10953 return FAIL;
10954 if (put_line(fd, "endif") == FAIL)
10955 return FAIL;
10956
10957 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958 * Now save the current files, current buffer first.
10959 */
10960 if (put_line(fd, "set shortmess=aoO") == FAIL)
10961 return FAIL;
10962
10963 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020010964 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965 {
10966 if (!(only_save_windows && buf->b_nwindows == 0)
10967 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10968 && buf->b_fname != NULL
10969 && buf->b_p_bl)
10970 {
10971 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10972 : buf->b_wininfo->wi_fpos.lnum) < 0
10973 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10974 return FAIL;
10975 }
10976 }
10977
10978 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010979 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10981 return FAIL;
10982
10983 if (ssop_flags & SSOP_RESIZE)
10984 {
10985 /* Note: after the restore we still check it worked!*/
10986 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10987 || put_eol(fd) == FAIL)
10988 return FAIL;
10989 }
10990
10991#ifdef FEAT_GUI
10992 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10993 {
10994 int x, y;
10995
10996 if (gui_mch_get_winpos(&x, &y) == OK)
10997 {
10998 /* Note: after the restore we still check it worked!*/
10999 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11000 return FAIL;
11001 }
11002 }
11003#endif
11004
11005 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011006 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11007 * will be displayed when creating the next tab. That resizes the windows
11008 * in the first tab, which may cause problems. Set 'showtabline' to 2
11009 * temporarily to avoid that.
11010 */
11011 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11012 {
11013 if (put_line(fd, "set stal=2") == FAIL)
11014 return FAIL;
11015 restore_stal = TRUE;
11016 }
11017
11018 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011019 * May repeat putting Windows for each tab, when "tabpages" is in
11020 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011021 * Don't use goto_tabpage(), it may change directory and trigger
11022 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011024 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011025 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011026 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011028 int need_tabnew = FALSE;
11029 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011030
Bram Moolenaar18144c82006-04-12 21:52:12 +000011031 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011033 tabpage_T *tp = find_tabpage(tabnr);
11034
11035 if (tp == NULL)
11036 break; /* done all tab pages */
11037 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011038 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011039 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011040 tab_topframe = topframe;
11041 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011042 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011043 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011044 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011045 tab_topframe = tp->tp_topframe;
11046 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011047 if (tabnr > 1)
11048 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011050
Bram Moolenaar18144c82006-04-12 21:52:12 +000011051 /*
11052 * Before creating the window layout, try loading one file. If this
11053 * is aborted we don't end up with a number of useless windows.
11054 * This may have side effects! (e.g., compressed or network file).
11055 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011056 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011057 {
11058 if (ses_do_win(wp)
11059 && wp->w_buffer->b_ffname != NULL
11060 && !wp->w_buffer->b_help
11061#ifdef FEAT_QUICKFIX
11062 && !bt_nofile(wp->w_buffer)
11063#endif
11064 )
11065 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011066 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000011067 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
11068 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011069 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011070 if (!wp->w_arg_idx_invalid)
11071 edited_win = wp;
11072 break;
11073 }
11074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011076 /* If no file got edited create an empty tab page. */
11077 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11078 return FAIL;
11079
Bram Moolenaar18144c82006-04-12 21:52:12 +000011080 /*
11081 * Save current window layout.
11082 */
11083 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011085 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011087 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11088 return FAIL;
11089 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11090 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091
Bram Moolenaar18144c82006-04-12 21:52:12 +000011092 /*
11093 * Check if window sizes can be restored (no windows omitted).
11094 * Remember the window number of the current window after restoring.
11095 */
11096 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011097 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011098 {
11099 if (ses_do_win(wp))
11100 ++nr;
11101 else
11102 restore_size = FALSE;
11103 if (curwin == wp)
11104 cnr = nr;
11105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106
Bram Moolenaar18144c82006-04-12 21:52:12 +000011107 /* Go to the first window. */
11108 if (put_line(fd, "wincmd t") == FAIL)
11109 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011110
Bram Moolenaar18144c82006-04-12 21:52:12 +000011111 /*
11112 * If more than one window, see if sizes can be restored.
11113 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11114 * resized when moving between windows.
11115 * Do this before restoring the view, so that the topline and the
11116 * cursor can be set. This is done again below.
11117 */
11118 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
11119 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011120 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011121 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122
Bram Moolenaar18144c82006-04-12 21:52:12 +000011123 /*
11124 * Restore the view of the window (options, file, cursor, etc.).
11125 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011126 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011127 {
11128 if (!ses_do_win(wp))
11129 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011130 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11131 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011132 return FAIL;
11133 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11134 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011135 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011136 }
11137
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011138 /* The argument index in the first tab page is zero, need to set it in
11139 * each window. For further tab pages it's the window where we do
11140 * "tabedit". */
11141 cur_arg_idx = next_arg_idx;
11142
Bram Moolenaar18144c82006-04-12 21:52:12 +000011143 /*
11144 * Restore cursor to the current window if it's not the first one.
11145 */
11146 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11147 || put_eol(fd) == FAIL))
11148 return FAIL;
11149
11150 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011151 * Restore window sizes again after jumping around in windows, because
11152 * the current window has a minimum size while others may not.
11153 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011154 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011155 return FAIL;
11156
Bram Moolenaar18144c82006-04-12 21:52:12 +000011157 /* Don't continue in another tab page when doing only the current one
11158 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011159 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011160 break;
11161 }
11162
11163 if (ssop_flags & SSOP_TABPAGES)
11164 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011165 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11166 || put_eol(fd) == FAIL)
11167 return FAIL;
11168 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011169 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11170 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011171
Bram Moolenaar9c102382006-05-03 21:26:49 +000011172 /*
11173 * Wipe out an empty unnamed buffer we started in.
11174 */
11175 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11176 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011177 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011178 return FAIL;
11179 if (put_line(fd, "endif") == FAIL)
11180 return FAIL;
11181 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11182 return FAIL;
11183
11184 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11185 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11186 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11187 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188
11189 /*
11190 * Lastly, execute the x.vim file if it exists.
11191 */
11192 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11193 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011194 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195 || put_line(fd, "endif") == FAIL)
11196 return FAIL;
11197
11198 return OK;
11199}
11200
11201 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011202ses_winsizes(
11203 FILE *fd,
11204 int restore_size,
11205 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011206{
11207 int n = 0;
11208 win_T *wp;
11209
11210 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11211 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011212 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213 {
11214 if (!ses_do_win(wp))
11215 continue;
11216 ++n;
11217
11218 /* restore height when not full height */
11219 if (wp->w_height + wp->w_status_height < topframe->fr_height
11220 && (fprintf(fd,
11221 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11222 n, (long)wp->w_height, Rows / 2, Rows) < 0
11223 || put_eol(fd) == FAIL))
11224 return FAIL;
11225
11226 /* restore width when not full width */
11227 if (wp->w_width < Columns && (fprintf(fd,
11228 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11229 n, (long)wp->w_width, Columns / 2, Columns) < 0
11230 || put_eol(fd) == FAIL))
11231 return FAIL;
11232 }
11233 }
11234 else
11235 {
11236 /* Just equalise window sizes */
11237 if (put_line(fd, "wincmd =") == FAIL)
11238 return FAIL;
11239 }
11240 return OK;
11241}
11242
11243/*
11244 * Write commands to "fd" to recursively create windows for frame "fr",
11245 * horizontally and vertically split.
11246 * After the commands the last window in the frame is the current window.
11247 * Returns FAIL when writing the commands to "fd" fails.
11248 */
11249 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011250ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251{
11252 frame_T *frc;
11253 int count = 0;
11254
11255 if (fr->fr_layout != FR_LEAF)
11256 {
11257 /* Find first frame that's not skipped and then create a window for
11258 * each following one (first frame is already there). */
11259 frc = ses_skipframe(fr->fr_child);
11260 if (frc != NULL)
11261 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11262 {
11263 /* Make window as big as possible so that we have lots of room
11264 * to split. */
11265 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11266 || put_line(fd, fr->fr_layout == FR_COL
11267 ? "split" : "vsplit") == FAIL)
11268 return FAIL;
11269 ++count;
11270 }
11271
11272 /* Go back to the first window. */
11273 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11274 ? "%dwincmd k" : "%dwincmd h", count) < 0
11275 || put_eol(fd) == FAIL))
11276 return FAIL;
11277
11278 /* Recursively create frames/windows in each window of this column or
11279 * row. */
11280 frc = ses_skipframe(fr->fr_child);
11281 while (frc != NULL)
11282 {
11283 ses_win_rec(fd, frc);
11284 frc = ses_skipframe(frc->fr_next);
11285 /* Go to next window. */
11286 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11287 return FAIL;
11288 }
11289 }
11290 return OK;
11291}
11292
11293/*
11294 * Skip frames that don't contain windows we want to save in the Session.
11295 * Returns NULL when there none.
11296 */
11297 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011298ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299{
11300 frame_T *frc;
11301
11302 for (frc = fr; frc != NULL; frc = frc->fr_next)
11303 if (ses_do_frame(frc))
11304 break;
11305 return frc;
11306}
11307
11308/*
11309 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11310 * the Session.
11311 */
11312 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011313ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011314{
11315 frame_T *frc;
11316
11317 if (fr->fr_layout == FR_LEAF)
11318 return ses_do_win(fr->fr_win);
11319 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11320 if (ses_do_frame(frc))
11321 return TRUE;
11322 return FALSE;
11323}
11324
11325/*
11326 * Return non-zero if window "wp" is to be stored in the Session.
11327 */
11328 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011329ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330{
11331 if (wp->w_buffer->b_fname == NULL
11332#ifdef FEAT_QUICKFIX
11333 /* When 'buftype' is "nofile" can't restore the window contents. */
11334 || bt_nofile(wp->w_buffer)
11335#endif
11336 )
11337 return (ssop_flags & SSOP_BLANK);
11338 if (wp->w_buffer->b_help)
11339 return (ssop_flags & SSOP_HELP);
11340 return TRUE;
11341}
11342
11343/*
11344 * Write commands to "fd" to restore the view of a window.
11345 * Caller must make sure 'scrolloff' is zero.
11346 */
11347 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011348put_view(
11349 FILE *fd,
11350 win_T *wp,
11351 int add_edit, /* add ":edit" command to view */
11352 unsigned *flagp, /* vop_flags or ssop_flags */
11353 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011354 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355{
11356 win_T *save_curwin;
11357 int f;
11358 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011359 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360
11361 /* Always restore cursor position for ":mksession". For ":mkview" only
11362 * when 'viewoptions' contains "cursor". */
11363 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11364
11365 /*
11366 * Local argument list.
11367 */
11368 if (wp->w_alist == &global_alist)
11369 {
11370 if (put_line(fd, "argglobal") == FAIL)
11371 return FAIL;
11372 }
11373 else
11374 {
11375 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11376 flagp == &vop_flags
11377 || !(*flagp & SSOP_CURDIR)
11378 || wp->w_localdir != NULL, flagp) == FAIL)
11379 return FAIL;
11380 }
11381
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011382 /* Only when part of a session: restore the argument index. Some
11383 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011384 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011385 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011386 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011387 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388 || put_eol(fd) == FAIL)
11389 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011390 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011391 }
11392
11393 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011394 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395 {
11396 /*
11397 * Load the file.
11398 */
11399 if (wp->w_buffer->b_ffname != NULL
11400#ifdef FEAT_QUICKFIX
11401 && !bt_nofile(wp->w_buffer)
11402#endif
11403 )
11404 {
11405 /*
11406 * Editing a file in this buffer: use ":edit file".
11407 * This may have side effects! (e.g., compressed or network file).
11408 */
11409 if (fputs("edit ", fd) < 0
11410 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11411 return FAIL;
11412 }
11413 else
11414 {
11415 /* No file in this buffer, just make it empty. */
11416 if (put_line(fd, "enew") == FAIL)
11417 return FAIL;
11418#ifdef FEAT_QUICKFIX
11419 if (wp->w_buffer->b_ffname != NULL)
11420 {
11421 /* The buffer does have a name, but it's not a file name. */
11422 if (fputs("file ", fd) < 0
11423 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11424 return FAIL;
11425 }
11426#endif
11427 do_cursor = FALSE;
11428 }
11429 }
11430
11431 /*
11432 * Local mappings and abbreviations.
11433 */
11434 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11435 && makemap(fd, wp->w_buffer) == FAIL)
11436 return FAIL;
11437
11438 /*
11439 * Local options. Need to go to the window temporarily.
11440 * Store only local values when using ":mkview" and when ":mksession" is
11441 * used and 'sessionoptions' doesn't include "options".
11442 * Some folding options are always stored when "folds" is included,
11443 * otherwise the folds would not be restored correctly.
11444 */
11445 save_curwin = curwin;
11446 curwin = wp;
11447 curbuf = curwin->w_buffer;
11448 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11449 f = makeset(fd, OPT_LOCAL,
11450 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11451#ifdef FEAT_FOLDING
11452 else if (*flagp & SSOP_FOLDS)
11453 f = makefoldset(fd);
11454#endif
11455 else
11456 f = OK;
11457 curwin = save_curwin;
11458 curbuf = curwin->w_buffer;
11459 if (f == FAIL)
11460 return FAIL;
11461
11462#ifdef FEAT_FOLDING
11463 /*
11464 * Save Folds when 'buftype' is empty and for help files.
11465 */
11466 if ((*flagp & SSOP_FOLDS)
11467 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011468# ifdef FEAT_QUICKFIX
11469 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11470# endif
11471 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472 {
11473 if (put_folds(fd, wp) == FAIL)
11474 return FAIL;
11475 }
11476#endif
11477
11478 /*
11479 * Set the cursor after creating folds, since that moves the cursor.
11480 */
11481 if (do_cursor)
11482 {
11483
11484 /* Restore the cursor line in the file and relatively in the
11485 * window. Don't use "G", it changes the jumplist. */
11486 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11487 (long)wp->w_cursor.lnum,
11488 (long)(wp->w_cursor.lnum - wp->w_topline),
11489 (long)wp->w_height / 2, (long)wp->w_height) < 0
11490 || put_eol(fd) == FAIL
11491 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11492 || put_line(fd, "exe s:l") == FAIL
11493 || put_line(fd, "normal! zt") == FAIL
11494 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11495 || put_eol(fd) == FAIL)
11496 return FAIL;
11497 /* Restore the cursor column and left offset when not wrapping. */
11498 if (wp->w_cursor.col == 0)
11499 {
11500 if (put_line(fd, "normal! 0") == FAIL)
11501 return FAIL;
11502 }
11503 else
11504 {
11505 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11506 {
11507 if (fprintf(fd,
11508 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011509 (long)wp->w_virtcol + 1,
11510 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011511 (long)wp->w_width / 2, (long)wp->w_width) < 0
11512 || put_eol(fd) == FAIL
11513 || put_line(fd, "if s:c > 0") == FAIL
11514 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011515 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11516 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011517 || put_eol(fd) == FAIL
11518 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011519 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011520 || put_eol(fd) == FAIL
11521 || put_line(fd, "endif") == FAIL)
11522 return FAIL;
11523 }
11524 else
11525 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011526 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011527 || put_eol(fd) == FAIL)
11528 return FAIL;
11529 }
11530 }
11531 }
11532
11533 /*
11534 * Local directory.
11535 */
11536 if (wp->w_localdir != NULL)
11537 {
11538 if (fputs("lcd ", fd) < 0
11539 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11540 || put_eol(fd) == FAIL)
11541 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011542 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011543 }
11544
11545 return OK;
11546}
11547
11548/*
11549 * Write an argument list to the session file.
11550 * Returns FAIL if writing fails.
11551 */
11552 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011553ses_arglist(
11554 FILE *fd,
11555 char *cmd,
11556 garray_T *gap,
11557 int fullname, /* TRUE: use full path name */
11558 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011559{
11560 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011561 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011562 char_u *s;
11563
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011564 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11565 return FAIL;
11566 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567 return FAIL;
11568 for (i = 0; i < gap->ga_len; ++i)
11569 {
11570 /* NULL file names are skipped (only happens when out of memory). */
11571 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11572 if (s != NULL)
11573 {
11574 if (fullname)
11575 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011576 buf = alloc(MAXPATHL);
11577 if (buf != NULL)
11578 {
11579 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11580 s = buf;
11581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011582 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011583 if (fputs("argadd ", fd) < 0
11584 || ses_put_fname(fd, s, flagp) == FAIL
11585 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011586 {
11587 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011589 }
11590 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011591 }
11592 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011593 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011594}
11595
11596/*
11597 * Write a buffer name to the session file.
11598 * Also ends the line.
11599 * Returns FAIL if writing fails.
11600 */
11601 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011602ses_fname(FILE *fd, buf_T *buf, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603{
11604 char_u *name;
11605
11606 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011607 * the session file will be sourced.
11608 * Don't do this for ":mkview", we don't know the current directory.
11609 * Don't do this after ":lcd", we don't keep track of what the current
11610 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011611 if (buf->b_sfname != NULL
11612 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011613 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011614#ifdef FEAT_AUTOCHDIR
11615 && !p_acd
11616#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011617 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618 name = buf->b_sfname;
11619 else
11620 name = buf->b_ffname;
11621 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11622 return FAIL;
11623 return OK;
11624}
11625
11626/*
11627 * Write a file name to the session file.
11628 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11629 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011630 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011631 */
11632 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011633ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011634{
11635 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011636 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011637 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011638
11639 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011640 if (sname == NULL)
11641 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011642
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011643 if (*flagp & SSOP_SLASH)
11644 {
11645 /* change all backslashes to forward slashes */
11646 for (p = sname; *p != NUL; mb_ptr_adv(p))
11647 if (*p == '\\')
11648 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011649 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011650
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011651 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011652 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011653 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011654 if (p == NULL)
11655 return FAIL;
11656
11657 /* write the result */
11658 if (fputs((char *)p, fd) < 0)
11659 retval = FAIL;
11660
11661 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662 return retval;
11663}
11664
11665/*
11666 * ":loadview [nr]"
11667 */
11668 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011669ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011670{
11671 char_u *fname;
11672
11673 fname = get_view_file(*eap->arg);
11674 if (fname != NULL)
11675 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011676 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677 vim_free(fname);
11678 }
11679}
11680
11681/*
11682 * Get the name of the view file for the current buffer.
11683 */
11684 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011685get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011686{
11687 int len = 0;
11688 char_u *p, *s;
11689 char_u *retval;
11690 char_u *sname;
11691
11692 if (curbuf->b_ffname == NULL)
11693 {
11694 EMSG(_(e_noname));
11695 return NULL;
11696 }
11697 sname = home_replace_save(NULL, curbuf->b_ffname);
11698 if (sname == NULL)
11699 return NULL;
11700
11701 /*
11702 * We want a file name without separators, because we're not going to make
11703 * a directory.
11704 * "normal" path separator -> "=+"
11705 * "=" -> "=="
11706 * ":" path separator -> "=-"
11707 */
11708 for (p = sname; *p; ++p)
11709 if (*p == '=' || vim_ispathsep(*p))
11710 ++len;
11711 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11712 if (retval != NULL)
11713 {
11714 STRCPY(retval, p_vdir);
11715 add_pathsep(retval);
11716 s = retval + STRLEN(retval);
11717 for (p = sname; *p; ++p)
11718 {
11719 if (*p == '=')
11720 {
11721 *s++ = '=';
11722 *s++ = '=';
11723 }
11724 else if (vim_ispathsep(*p))
11725 {
11726 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011727#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011728 if (*p == ':')
11729 *s++ = '-';
11730 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011732 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011733 }
11734 else
11735 *s++ = *p;
11736 }
11737 *s++ = '=';
11738 *s++ = c;
11739 STRCPY(s, ".vim");
11740 }
11741
11742 vim_free(sname);
11743 return retval;
11744}
11745
11746#endif /* FEAT_SESSION */
11747
11748/*
11749 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11750 * Return FAIL for a write error.
11751 */
11752 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011753put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754{
11755 if (
11756#ifdef USE_CRNL
11757 (
11758# ifdef MKSESSION_NL
11759 !mksession_nl &&
11760# endif
11761 (putc('\r', fd) < 0)) ||
11762#endif
11763 (putc('\n', fd) < 0))
11764 return FAIL;
11765 return OK;
11766}
11767
11768/*
11769 * Write a line to "fd".
11770 * Return FAIL for a write error.
11771 */
11772 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011773put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774{
11775 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11776 return FAIL;
11777 return OK;
11778}
11779
11780#ifdef FEAT_VIMINFO
11781/*
11782 * ":rviminfo" and ":wviminfo".
11783 */
11784 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011785ex_viminfo(
11786 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011787{
11788 char_u *save_viminfo;
11789
11790 save_viminfo = p_viminfo;
11791 if (*p_viminfo == NUL)
11792 p_viminfo = (char_u *)"'100";
11793 if (eap->cmdidx == CMD_rviminfo)
11794 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011795 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11796 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797 EMSG(_("E195: Cannot open viminfo file for reading"));
11798 }
11799 else
11800 write_viminfo(eap->arg, eap->forceit);
11801 p_viminfo = save_viminfo;
11802}
11803#endif
11804
11805#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011806/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011807 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011808 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011809 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011811dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011812{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011813 if (fname == NULL)
11814 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011815 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011816}
11817#endif
11818
11819/*
11820 * ":behave {mswin,xterm}"
11821 */
11822 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011823ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011824{
11825 if (STRCMP(eap->arg, "mswin") == 0)
11826 {
11827 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11828 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11829 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11830 set_option_value((char_u *)"keymodel", 0L,
11831 (char_u *)"startsel,stopsel", 0);
11832 }
11833 else if (STRCMP(eap->arg, "xterm") == 0)
11834 {
11835 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11836 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11837 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11838 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11839 }
11840 else
11841 EMSG2(_(e_invarg2), eap->arg);
11842}
11843
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011844#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11845/*
11846 * Function given to ExpandGeneric() to obtain the possible arguments of the
11847 * ":behave {mswin,xterm}" command.
11848 */
11849 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011850get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011851{
11852 if (idx == 0)
11853 return (char_u *)"mswin";
11854 if (idx == 1)
11855 return (char_u *)"xterm";
11856 return NULL;
11857}
11858#endif
11859
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860#ifdef FEAT_AUTOCMD
11861static int filetype_detect = FALSE;
11862static int filetype_plugin = FALSE;
11863static int filetype_indent = FALSE;
11864
11865/*
11866 * ":filetype [plugin] [indent] {on,off,detect}"
11867 * on: Load the filetype.vim file to install autocommands for file types.
11868 * off: Load the ftoff.vim file to remove all autocommands for file types.
11869 * plugin on: load filetype.vim and ftplugin.vim
11870 * plugin off: load ftplugof.vim
11871 * indent on: load filetype.vim and indent.vim
11872 * indent off: load indoff.vim
11873 */
11874 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011875ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011876{
11877 char_u *arg = eap->arg;
11878 int plugin = FALSE;
11879 int indent = FALSE;
11880
11881 if (*eap->arg == NUL)
11882 {
11883 /* Print current status. */
11884 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11885 filetype_detect ? "ON" : "OFF",
11886 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11887 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11888 return;
11889 }
11890
11891 /* Accept "plugin" and "indent" in any order. */
11892 for (;;)
11893 {
11894 if (STRNCMP(arg, "plugin", 6) == 0)
11895 {
11896 plugin = TRUE;
11897 arg = skipwhite(arg + 6);
11898 continue;
11899 }
11900 if (STRNCMP(arg, "indent", 6) == 0)
11901 {
11902 indent = TRUE;
11903 arg = skipwhite(arg + 6);
11904 continue;
11905 }
11906 break;
11907 }
11908 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11909 {
11910 if (*arg == 'o' || !filetype_detect)
11911 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011912 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011913 filetype_detect = TRUE;
11914 if (plugin)
11915 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011916 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011917 filetype_plugin = TRUE;
11918 }
11919 if (indent)
11920 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011921 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011922 filetype_indent = TRUE;
11923 }
11924 }
11925 if (*arg == 'd')
11926 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020011927 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011928 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011929 }
11930 }
11931 else if (STRCMP(arg, "off") == 0)
11932 {
11933 if (plugin || indent)
11934 {
11935 if (plugin)
11936 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011937 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938 filetype_plugin = FALSE;
11939 }
11940 if (indent)
11941 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011942 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011943 filetype_indent = FALSE;
11944 }
11945 }
11946 else
11947 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011948 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011949 filetype_detect = FALSE;
11950 }
11951 }
11952 else
11953 EMSG2(_(e_invarg2), arg);
11954}
11955
11956/*
11957 * ":setfiletype {name}"
11958 */
11959 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011960ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961{
11962 if (!did_filetype)
11963 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11964}
11965#endif
11966
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011968ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969{
11970#ifdef FEAT_DIGRAPHS
11971 if (*eap->arg != NUL)
11972 putdigraph(eap->arg);
11973 else
11974 listdigraphs();
11975#else
11976 EMSG(_("E196: No digraphs in this version"));
11977#endif
11978}
11979
11980 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011981ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011982{
11983 int flags = 0;
11984
11985 if (eap->cmdidx == CMD_setlocal)
11986 flags = OPT_LOCAL;
11987 else if (eap->cmdidx == CMD_setglobal)
11988 flags = OPT_GLOBAL;
11989#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11990 if (cmdmod.browse && flags == 0)
11991 ex_options(eap);
11992 else
11993#endif
11994 (void)do_set(eap->arg, flags);
11995}
11996
11997#ifdef FEAT_SEARCH_EXTRA
11998/*
11999 * ":nohlsearch"
12000 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012001 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012002ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010012004 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012005 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012006}
12007
12008/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012009 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010 * Sets nextcmd to the start of the next command, if any. Also called when
12011 * skipping commands to find the next command.
12012 */
12013 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012014ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012015{
12016 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012017 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012018 char_u *end;
12019 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012020 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012021
12022 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012023 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012024 else
12025 {
12026 EMSG(e_invcmd);
12027 return;
12028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029
12030 /* First clear any old pattern. */
12031 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012032 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033
12034 if (ends_excmd(*eap->arg))
12035 end = eap->arg;
12036 else if ((STRNICMP(eap->arg, "none", 4) == 0
12037 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
12038 end = eap->arg + 4;
12039 else
12040 {
12041 p = skiptowhite(eap->arg);
12042 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012043 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044 p = skipwhite(p);
12045 if (*p == NUL)
12046 {
12047 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012048 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012049 EMSG2(_(e_invarg2), eap->arg);
12050 return;
12051 }
12052 end = skip_regexp(p + 1, *p, TRUE, NULL);
12053 if (!eap->skip)
12054 {
12055 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12056 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012057 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012058 eap->errmsg = e_trailing;
12059 return;
12060 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012061 if (*end != *p)
12062 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012063 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012064 EMSG2(_(e_invarg2), p);
12065 return;
12066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067
12068 c = *end;
12069 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012070 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012071 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012072 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073 }
12074 }
12075 eap->nextcmd = find_nextcmd(end);
12076}
12077#endif
12078
12079#ifdef FEAT_CRYPT
12080/*
12081 * ":X": Get crypt key
12082 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012084ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012085{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012086 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012087 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088}
12089#endif
12090
12091#ifdef FEAT_FOLDING
12092 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012093ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012094{
12095 if (foldManualAllowed(TRUE))
12096 foldCreate(eap->line1, eap->line2);
12097}
12098
12099 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012100ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101{
12102 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12103 eap->forceit, FALSE);
12104}
12105
12106 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012107ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012108{
12109 linenr_T lnum;
12110
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012111#ifdef FEAT_CLIPBOARD
12112 start_global_changes();
12113#endif
12114
Bram Moolenaar071d4272004-06-13 20:20:40 +000012115 /* First set the marks for all lines closed/open. */
12116 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12117 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12118 ml_setmarked(lnum);
12119
12120 /* Execute the command on the marked lines. */
12121 global_exe(eap->arg);
12122 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012123#ifdef FEAT_CLIPBOARD
12124 end_global_changes();
12125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012126}
12127#endif