blob: 2b0f15ee9d9c3ecc3f8eb326e6826f56eef0e905 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
22#ifdef FEAT_USR_CMDS
23typedef struct ucmd
24{
25 char_u *uc_name; /* The command name */
26 long_u uc_argt; /* The argument type */
27 char_u *uc_rep; /* The command's replacement string */
28 long uc_def; /* The default value for a range/count */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int uc_compl; /* completion type */
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +010030 int uc_addr_type; /* The command's address type */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010031# ifdef FEAT_EVAL
32 scid_T uc_scriptID; /* SID where the command was defined */
33# ifdef FEAT_CMDL_COMPL
Bram Moolenaar071d4272004-06-13 20:20:40 +000034 char_u *uc_compl_arg; /* completion argument if any */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010035# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000036# endif
37} ucmd_T;
38
39#define UC_BUFFER 1 /* -buffer: local to current buffer */
40
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000041static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
43#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
44#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
45
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010046static void do_ucmd(exarg_T *eap);
47static void ex_command(exarg_T *eap);
48static void ex_delcommand(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# ifdef FEAT_CMDL_COMPL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010050static char_u *get_user_command_name(int idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051# endif
52
Bram Moolenaar958636c2014-10-21 20:01:58 +020053/* Wether a command index indicates a user command. */
54# define IS_USER_CMDIDX(idx) ((int)(idx) < 0)
55
Bram Moolenaar071d4272004-06-13 20:20:40 +000056#else
57# define ex_command ex_ni
58# define ex_comclear ex_ni
59# define ex_delcommand ex_ni
Bram Moolenaar958636c2014-10-21 20:01:58 +020060/* Wether a command index indicates a user command. */
61# define IS_USER_CMDIDX(idx) (FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000062#endif
63
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010064static int compute_buffer_local_count(int addr_type, int lnum, int local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000065#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010066static char_u *do_one_cmd(char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000067#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010068static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000069static int if_level = 0; /* depth in :if */
70#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010071static void append_command(char_u *cmd);
72static char_u *find_command(exarg_T *eap, int *full);
Bram Moolenaar071d4272004-06-13 20:20:40 +000073
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010074static void ex_abbreviate(exarg_T *eap);
75static void ex_map(exarg_T *eap);
76static void ex_unmap(exarg_T *eap);
77static void ex_mapclear(exarg_T *eap);
78static void ex_abclear(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#ifndef FEAT_MENU
80# define ex_emenu ex_ni
81# define ex_menu ex_ni
82# define ex_menutranslate ex_ni
83#endif
84#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static void ex_autocmd(exarg_T *eap);
86static void ex_doautocmd(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#else
88# define ex_autocmd ex_ni
89# define ex_doautocmd ex_ni
90# define ex_doautoall ex_ni
91#endif
92#ifdef FEAT_LISTCMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010093static void ex_bunload(exarg_T *eap);
94static void ex_buffer(exarg_T *eap);
95static void ex_bmodified(exarg_T *eap);
96static void ex_bnext(exarg_T *eap);
97static void ex_bprevious(exarg_T *eap);
98static void ex_brewind(exarg_T *eap);
99static void ex_blast(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#else
101# define ex_bunload ex_ni
102# define ex_buffer ex_ni
103# define ex_bmodified ex_ni
104# define ex_bnext ex_ni
105# define ex_bprevious ex_ni
106# define ex_brewind ex_ni
107# define ex_blast ex_ni
108# define buflist_list ex_ni
109# define ex_checktime ex_ni
110#endif
111#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
112# define ex_buffer_all ex_ni
113#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100114static char_u *getargcmd(char_u **);
115static char_u *skip_cmd_arg(char_u *p, int rembs);
116static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#ifndef FEAT_QUICKFIX
118# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000119# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120# define ex_cc ex_ni
121# define ex_cnext ex_ni
122# define ex_cfile ex_ni
123# define qf_list ex_ni
124# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200125# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000127# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#endif
129#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
130# define ex_cclose ex_ni
131# define ex_copen ex_ni
132# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +0200133# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000135#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
136# define ex_cexpr ex_ni
137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100139static int check_more(int, int);
140static linenr_T get_address(exarg_T *, char_u **, int addr_type, int skip, int to_other_file);
141static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200142#if !defined(FEAT_PERL) \
143 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
144 || !defined(FEAT_TCL) \
145 || !defined(FEAT_RUBY) \
146 || !defined(FEAT_LUA) \
147 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000148# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100149static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100151static char_u *invalid_range(exarg_T *eap);
152static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000153#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100154static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000155#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100156static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
157static void ex_highlight(exarg_T *eap);
158static void ex_colorscheme(exarg_T *eap);
159static void ex_quit(exarg_T *eap);
160static void ex_cquit(exarg_T *eap);
161static void ex_quit_all(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100163static void ex_close(exarg_T *eap);
164static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
165static void ex_only(exarg_T *eap);
166static void ex_resize(exarg_T *eap);
167static void ex_stag(exarg_T *eap);
168static void ex_tabclose(exarg_T *eap);
169static void ex_tabonly(exarg_T *eap);
170static void ex_tabnext(exarg_T *eap);
171static void ex_tabmove(exarg_T *eap);
172static void ex_tabs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173#else
174# define ex_close ex_ni
175# define ex_only ex_ni
176# define ex_all ex_ni
177# define ex_resize ex_ni
178# define ex_splitview ex_ni
179# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000180# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000181# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000182# define ex_tabs ex_ni
183# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000184# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185#endif
186#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100187static void ex_pclose(exarg_T *eap);
188static void ex_ptag(exarg_T *eap);
189static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190#else
191# define ex_pclose ex_ni
192# define ex_ptag ex_ni
193# define ex_pedit ex_ni
194#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100195static void ex_hide(exarg_T *eap);
196static void ex_stop(exarg_T *eap);
197static void ex_exit(exarg_T *eap);
198static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100200static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#else
202# define ex_goto ex_ni
203#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100204static void ex_shell(exarg_T *eap);
205static void ex_preserve(exarg_T *eap);
206static void ex_recover(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207#ifndef FEAT_LISTCMDS
208# define ex_argedit ex_ni
209# define ex_argadd ex_ni
210# define ex_argdelete ex_ni
211# define ex_listdo ex_ni
212#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100213static void ex_mode(exarg_T *eap);
214static void ex_wrongmodifier(exarg_T *eap);
215static void ex_find(exarg_T *eap);
216static void ex_open(exarg_T *eap);
217static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
219# define ex_drop ex_ni
220#endif
221#ifndef FEAT_GUI
222# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#endif
225#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100226static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227#else
228# define ex_tearoff ex_ni
229#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000230#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100231static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232#else
233# define ex_popup ex_ni
234#endif
235#ifndef FEAT_GUI_MSWIN
236# define ex_simalt ex_ni
237#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000238#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239# define gui_mch_find_dialog ex_ni
240# define gui_mch_replace_dialog ex_ni
241#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000242#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243# define ex_helpfind ex_ni
244#endif
245#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100246# define ex_cscope ex_ni
247# define ex_scscope ex_ni
248# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249#endif
250#ifndef FEAT_SYN_HL
251# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200252# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000253#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200254#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200255# define ex_syntime ex_ni
256#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000257#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000258# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000259# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000260# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000261# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000262# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000263#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200264#ifndef FEAT_PERSISTENT_UNDO
265# define ex_rundo ex_ni
266# define ex_wundo ex_ni
267#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200268#ifndef FEAT_LUA
269# define ex_lua ex_script_ni
270# define ex_luado ex_ni
271# define ex_luafile ex_ni
272#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000273#ifndef FEAT_MZSCHEME
274# define ex_mzscheme ex_script_ni
275# define ex_mzfile ex_ni
276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#ifndef FEAT_PERL
278# define ex_perl ex_script_ni
279# define ex_perldo ex_ni
280#endif
281#ifndef FEAT_PYTHON
282# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200283# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284# define ex_pyfile ex_ni
285#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200286#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200287# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200288# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200289# define ex_py3file ex_ni
290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291#ifndef FEAT_TCL
292# define ex_tcl ex_script_ni
293# define ex_tcldo ex_ni
294# define ex_tclfile ex_ni
295#endif
296#ifndef FEAT_RUBY
297# define ex_ruby ex_script_ni
298# define ex_rubydo ex_ni
299# define ex_rubyfile ex_ni
300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301#ifndef FEAT_KEYMAP
302# define ex_loadkeymap ex_ni
303#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100304static void ex_swapname(exarg_T *eap);
305static void ex_syncbind(exarg_T *eap);
306static void ex_read(exarg_T *eap);
307static void ex_pwd(exarg_T *eap);
308static void ex_equal(exarg_T *eap);
309static void ex_sleep(exarg_T *eap);
310static void do_exmap(exarg_T *eap, int isabbrev);
311static void ex_winsize(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100313static void ex_wincmd(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314#else
315# define ex_wincmd ex_ni
316#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000317#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319#else
320# define ex_winpos ex_ni
321#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100322static void ex_operators(exarg_T *eap);
323static void ex_put(exarg_T *eap);
324static void ex_copymove(exarg_T *eap);
325static void ex_submagic(exarg_T *eap);
326static void ex_join(exarg_T *eap);
327static void ex_at(exarg_T *eap);
328static void ex_bang(exarg_T *eap);
329static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200330#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100331static void ex_wundo(exarg_T *eap);
332static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200333#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100334static void ex_redo(exarg_T *eap);
335static void ex_later(exarg_T *eap);
336static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100337static void ex_redrawstatus(exarg_T *eap);
338static void close_redir(void);
339static void ex_mkrc(exarg_T *eap);
340static void ex_mark(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341#ifdef FEAT_USR_CMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100342static char_u *uc_fun_cmd(void);
343static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100345static void ex_startinsert(exarg_T *eap);
346static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100348static void ex_checkpath(exarg_T *eap);
349static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350#else
351# define ex_findpat ex_ni
352# define ex_checkpath ex_ni
353#endif
354#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100355static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356#else
357# define ex_psearch ex_ni
358#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100359static void ex_tag(exarg_T *eap);
360static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361#ifndef FEAT_EVAL
362# define ex_scriptnames ex_ni
363# define ex_finish ex_ni
364# define ex_echo ex_ni
365# define ex_echohl ex_ni
366# define ex_execute ex_ni
367# define ex_call ex_ni
368# define ex_if ex_ni
369# define ex_endif ex_ni
370# define ex_else ex_ni
371# define ex_while ex_ni
372# define ex_continue ex_ni
373# define ex_break ex_ni
374# define ex_endwhile ex_ni
375# define ex_throw ex_ni
376# define ex_try ex_ni
377# define ex_catch ex_ni
378# define ex_finally ex_ni
379# define ex_endtry ex_ni
380# define ex_endfunction ex_ni
381# define ex_let ex_ni
382# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000383# define ex_lockvar ex_ni
384# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385# define ex_function ex_ni
386# define ex_delfunction ex_ni
387# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000388# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100390static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100392static int makeopens(FILE *fd, char_u *dirnow);
393static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
394static void ex_loadview(exarg_T *eap);
395static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000396static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397#else
398# define ex_loadview ex_ni
399#endif
400#ifndef FEAT_EVAL
401# define ex_compiler ex_ni
402#endif
403#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100404static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405#else
406# define ex_viminfo ex_ni
407#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100408static void ex_behave(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100410static void ex_filetype(exarg_T *eap);
411static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412#else
413# define ex_filetype ex_ni
414# define ex_setfiletype ex_ni
415#endif
416#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000417# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418# define ex_diffpatch ex_ni
419# define ex_diffgetput ex_ni
420# define ex_diffsplit ex_ni
421# define ex_diffthis ex_ni
422# define ex_diffupdate ex_ni
423#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100424static void ex_digraphs(exarg_T *eap);
425static void ex_set(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
427# define ex_options ex_ni
428#endif
429#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100430static void ex_nohlsearch(exarg_T *eap);
431static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432#else
433# define ex_nohlsearch ex_ni
434# define ex_match ex_ni
435#endif
436#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100437static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438#else
439# define ex_X ex_ni
440#endif
441#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100442static void ex_fold(exarg_T *eap);
443static void ex_foldopen(exarg_T *eap);
444static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445#else
446# define ex_fold ex_ni
447# define ex_foldopen ex_ni
448# define ex_folddo ex_ni
449#endif
450#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
451 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
452# define ex_language ex_ni
453#endif
454#ifndef FEAT_SIGNS
455# define ex_sign ex_ni
456#endif
457#ifndef FEAT_SUN_WORKSHOP
458# define ex_wsverb ex_ni
459#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000460#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200461# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000462# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200463# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465
466#ifndef FEAT_EVAL
467# define ex_debug ex_ni
468# define ex_breakadd ex_ni
469# define ex_debuggreedy ex_ni
470# define ex_breakdel ex_ni
471# define ex_breaklist ex_ni
472#endif
473
474#ifndef FEAT_CMDHIST
475# define ex_history ex_ni
476#endif
477#ifndef FEAT_JUMPLIST
478# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200479# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480# define ex_changes ex_ni
481#endif
482
Bram Moolenaar05159a02005-02-26 23:04:13 +0000483#ifndef FEAT_PROFILE
484# define ex_profile ex_ni
485#endif
486
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487/*
488 * Declare cmdnames[].
489 */
490#define DO_DECLARE_EXCMD
491#include "ex_cmds.h"
492
493/*
494 * Table used to quickly search for a command, based on its first character.
495 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000496static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497{
498 CMD_append,
499 CMD_buffer,
500 CMD_change,
501 CMD_delete,
502 CMD_edit,
503 CMD_file,
504 CMD_global,
505 CMD_help,
506 CMD_insert,
507 CMD_join,
508 CMD_k,
509 CMD_list,
510 CMD_move,
511 CMD_next,
512 CMD_open,
513 CMD_print,
514 CMD_quit,
515 CMD_read,
516 CMD_substitute,
517 CMD_t,
518 CMD_undo,
519 CMD_vglobal,
520 CMD_write,
521 CMD_xit,
522 CMD_yank,
523 CMD_z,
524 CMD_bang
525};
526
527static char_u dollar_command[2] = {'$', 0};
528
529
530#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000531/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532typedef struct
533{
534 char_u *line; /* command line */
535 linenr_T lnum; /* sourcing_lnum of the line */
536} wcmd_T;
537
538/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000539 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000541 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000543struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544{
545 garray_T *lines_gap; /* growarray with line info */
546 int current_line; /* last read line from growarray */
547 int repeating; /* TRUE when looping a second time */
548 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100549 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 void *cookie;
551};
552
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100553static char_u *get_loop_line(int c, void *cookie, int indent);
554static int store_loop_line(garray_T *gap, char_u *line);
555static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000556
557/* Struct to save a few things while debugging. Used in do_cmdline() only. */
558struct dbg_stuff
559{
560 int trylevel;
561 int force_abort;
562 except_T *caught_stack;
563 char_u *vv_exception;
564 char_u *vv_throwpoint;
565 int did_emsg;
566 int got_int;
567 int did_throw;
568 int need_rethrow;
569 int check_cstack;
570 except_T *current_exception;
571};
572
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100573static void save_dbg_stuff(struct dbg_stuff *dsp);
574static void restore_dbg_stuff(struct dbg_stuff *dsp);
Bram Moolenaared203462004-06-16 11:19:22 +0000575
576 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100577save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000578{
579 dsp->trylevel = trylevel; trylevel = 0;
580 dsp->force_abort = force_abort; force_abort = FALSE;
581 dsp->caught_stack = caught_stack; caught_stack = NULL;
582 dsp->vv_exception = v_exception(NULL);
583 dsp->vv_throwpoint = v_throwpoint(NULL);
584
585 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
586 dsp->did_emsg = did_emsg; did_emsg = FALSE;
587 dsp->got_int = got_int; got_int = FALSE;
588 dsp->did_throw = did_throw; did_throw = FALSE;
589 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
590 dsp->check_cstack = check_cstack; check_cstack = FALSE;
591 dsp->current_exception = current_exception; current_exception = NULL;
592}
593
594 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100595restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000596{
597 suppress_errthrow = FALSE;
598 trylevel = dsp->trylevel;
599 force_abort = dsp->force_abort;
600 caught_stack = dsp->caught_stack;
601 (void)v_exception(dsp->vv_exception);
602 (void)v_throwpoint(dsp->vv_throwpoint);
603 did_emsg = dsp->did_emsg;
604 got_int = dsp->got_int;
605 did_throw = dsp->did_throw;
606 need_rethrow = dsp->need_rethrow;
607 check_cstack = dsp->check_cstack;
608 current_exception = dsp->current_exception;
609}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610#endif
611
612
613/*
614 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
615 * command is given.
616 */
617 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100618do_exmode(
619 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620{
621 int save_msg_scroll;
622 int prev_msg_row;
623 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000624 int changedtick;
625
626 if (improved)
627 exmode_active = EXMODE_VIM;
628 else
629 exmode_active = EXMODE_NORMAL;
630 State = NORMAL;
631
632 /* When using ":global /pat/ visual" and then "Q" we return to continue
633 * the :global command. */
634 if (global_busy)
635 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636
637 save_msg_scroll = msg_scroll;
638 ++RedrawingDisabled; /* don't redisplay the window */
639 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640#ifdef FEAT_GUI
641 /* Ignore scrollbar and mouse events in Ex mode */
642 ++hold_gui_events;
643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644
645 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
646 while (exmode_active)
647 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000648 /* Check for a ":normal" command and no more characters left. */
649 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
650 {
651 exmode_active = FALSE;
652 break;
653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 msg_scroll = TRUE;
655 need_wait_return = FALSE;
656 ex_pressedreturn = FALSE;
657 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000658 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 prev_msg_row = msg_row;
660 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 if (improved)
662 {
663 cmdline_row = msg_row;
664 do_cmdline(NULL, getexline, NULL, 0);
665 }
666 else
667 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
668 lines_left = Rows - 1;
669
Bram Moolenaardf177f62005-02-22 08:39:57 +0000670 if ((prev_line != curwin->w_cursor.lnum
671 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000673 if (curbuf->b_ml.ml_flags & ML_EMPTY)
674 EMSG(_(e_emptybuf));
675 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000677 if (ex_pressedreturn)
678 {
679 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000680 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000681 msg_row = prev_msg_row;
682 if (prev_msg_row == Rows - 1)
683 msg_row--;
684 }
685 msg_col = 0;
686 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
687 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000690 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
691 {
692 if (curbuf->b_ml.ml_flags & ML_EMPTY)
693 EMSG(_(e_emptybuf));
694 else
695 EMSG(_("E501: At end-of-file"));
696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 }
698
699#ifdef FEAT_GUI
700 --hold_gui_events;
701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 --RedrawingDisabled;
703 --no_wait_return;
704 update_screen(CLEAR);
705 need_wait_return = FALSE;
706 msg_scroll = save_msg_scroll;
707}
708
709/*
710 * Execute a simple command line. Used for translated commands like "*".
711 */
712 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100713do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714{
715 return do_cmdline(cmd, NULL, NULL,
716 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
717}
718
719/*
720 * do_cmdline(): execute one Ex command line
721 *
722 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100723 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 * 2. Split up in parts separated with '|'.
725 *
726 * This function can be called recursively!
727 *
728 * flags:
729 * DOCMD_VERBOSE - The command will be included in the error message.
730 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100731 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 * DOCMD_KEYTYPED - Don't reset KeyTyped.
733 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
734 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
735 *
736 * return FAIL if cmdline could not be executed, OK otherwise
737 */
738 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100739do_cmdline(
740 char_u *cmdline,
741 char_u *(*fgetline)(int, void *, int),
742 void *cookie, /* argument for fgetline() */
743 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744{
745 char_u *next_cmdline; /* next cmd to execute */
746 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100747 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 static int recursive = 0; /* recursive depth */
749 int msg_didout_before_start = 0;
750 int count = 0; /* line number count */
751 int did_inc = FALSE; /* incremented RedrawingDisabled */
752 int retval = OK;
753#ifdef FEAT_EVAL
754 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000755 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 int current_line = 0; /* active line in lines_ga */
757 char_u *fname = NULL; /* function or script name */
758 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
759 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000760 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 int initial_trylevel;
762 struct msglist **saved_msg_list = NULL;
763 struct msglist *private_msg_list;
764
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100765 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100766 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000768 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000770 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100772# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773# define cmd_cookie cookie
774#endif
775 static int call_depth = 0; /* recursiveness */
776
777#ifdef FEAT_EVAL
778 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
779 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000780 * This ensures that the do_errthrow() call in do_one_cmd() does not
781 * combine the messages stored by an earlier invocation of do_one_cmd()
782 * with the command name of the later one. This would happen when
783 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 saved_msg_list = msg_list;
785 msg_list = &private_msg_list;
786 private_msg_list = NULL;
787#endif
788
789 /* It's possible to create an endless loop with ":execute", catch that
790 * here. The value of 200 allows nested function calls, ":source", etc. */
791 if (call_depth == 200)
792 {
793 EMSG(_("E169: Command too recursive"));
794#ifdef FEAT_EVAL
795 /* When converting to an exception, we do not include the command name
796 * since this is not an error of the specific command. */
797 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
798 msg_list = saved_msg_list;
799#endif
800 return FAIL;
801 }
802 ++call_depth;
803
804#ifdef FEAT_EVAL
805 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000806 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 cstack.cs_trylevel = 0;
808 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000809 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
811
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100812 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
814 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100815 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000816 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 ++ex_nesting_level;
818
819 /* Get the function or script name and the address where the next breakpoint
820 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000821 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 {
823 fname = func_name(real_cookie);
824 breakpoint = func_breakpoint(real_cookie);
825 dbg_tick = func_dbg_tick(real_cookie);
826 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100827 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 {
829 fname = sourcing_name;
830 breakpoint = source_breakpoint(real_cookie);
831 dbg_tick = source_dbg_tick(real_cookie);
832 }
833
834 /*
835 * Initialize "force_abort" and "suppress_errthrow" at the top level.
836 */
837 if (!recursive)
838 {
839 force_abort = FALSE;
840 suppress_errthrow = FALSE;
841 }
842
843 /*
844 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000845 * exception handling (used when debugging). Otherwise clear it to avoid
846 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000848 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000849 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000850 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200851 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852
853 initial_trylevel = trylevel;
854
855 /*
856 * "did_throw" will be set to TRUE when an exception is being thrown.
857 */
858 did_throw = FALSE;
859#endif
860 /*
861 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000862 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 * If force_abort is set, we cancel everything.
864 */
865 did_emsg = FALSE;
866
867 /*
868 * KeyTyped is only set when calling vgetc(). Reset it here when not
869 * calling vgetc() (sourced command lines).
870 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100871 if (!(flags & DOCMD_KEYTYPED)
872 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 KeyTyped = FALSE;
874
875 /*
876 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000877 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 * - for multiple commands on one line, separated with '|'
879 * - when repeating until there are no more lines (for ":source")
880 */
881 next_cmdline = cmdline;
882 do
883 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000884#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100885 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000886#endif
887
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000888 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (next_cmdline == NULL
890#ifdef FEAT_EVAL
891 && !force_abort
892 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000893 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894#endif
895 )
896 did_emsg = FALSE;
897
898 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000899 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100900 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 * 3. If a line is given: Make a copy, so we can mess with it.
902 */
903
904#ifdef FEAT_EVAL
905 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000906 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 {
908 /* Each '|' separated command is stored separately in lines_ga, to
909 * be able to jump to it. Don't use next_cmdline now. */
910 vim_free(cmdline_copy);
911 cmdline_copy = NULL;
912
913 /* Check if a function has returned or, unless it has an unclosed
914 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000915 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000917# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000918 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000919 func_line_end(real_cookie);
920# endif
921 if (func_has_ended(real_cookie))
922 {
923 retval = FAIL;
924 break;
925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000927#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000928 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100929 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000930 script_line_end();
931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932
933 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100934 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 {
936 retval = FAIL;
937 break;
938 }
939
940 /* If breakpoints have been added/deleted need to check for it. */
941 if (breakpoint != NULL && dbg_tick != NULL
942 && *dbg_tick != debug_tick)
943 {
944 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100945 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 fname, sourcing_lnum);
947 *dbg_tick = debug_tick;
948 }
949
950 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
951 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
952
953 /* Did we encounter a breakpoint? */
954 if (breakpoint != NULL && *breakpoint != 0
955 && *breakpoint <= sourcing_lnum)
956 {
957 dbg_breakpoint(fname, sourcing_lnum);
958 /* Find next breakpoint. */
959 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100960 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 fname, sourcing_lnum);
962 *dbg_tick = debug_tick;
963 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000964# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000965 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000966 {
967 if (getline_is_func)
968 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100969 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000970 script_line_start();
971 }
972# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 }
974
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000975 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000977 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100978 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 * below, so that it stores lines in or reads them from
980 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000981 * while/for loop. */
982 cmd_getline = get_loop_line;
983 cmd_cookie = (void *)&cmd_loop_cookie;
984 cmd_loop_cookie.lines_gap = &lines_ga;
985 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100986 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000987 cmd_loop_cookie.cookie = cookie;
988 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 }
990 else
991 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100992 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 cmd_cookie = cookie;
994 }
995#endif
996
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100997 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 if (next_cmdline == NULL)
999 {
1000 /*
1001 * Need to set msg_didout for the first line after an ":if",
1002 * otherwise the ":if" will be overwritten.
1003 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001004 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001006 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007#ifdef FEAT_EVAL
1008 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1009#else
1010 0
1011#endif
1012 )) == NULL)
1013 {
1014 /* Don't call wait_return for aborted command line. The NULL
1015 * returned for the end of a sourced file or executed function
1016 * doesn't do this. */
1017 if (KeyTyped && !(flags & DOCMD_REPEAT))
1018 need_wait_return = FALSE;
1019 retval = FAIL;
1020 break;
1021 }
1022 used_getline = TRUE;
1023
1024 /*
1025 * Keep the first typed line. Clear it when more lines are typed.
1026 */
1027 if (flags & DOCMD_KEEPLINE)
1028 {
1029 vim_free(repeat_cmdline);
1030 if (count == 0)
1031 repeat_cmdline = vim_strsave(next_cmdline);
1032 else
1033 repeat_cmdline = NULL;
1034 }
1035 }
1036
1037 /* 3. Make a copy of the command so we can mess with it. */
1038 else if (cmdline_copy == NULL)
1039 {
1040 next_cmdline = vim_strsave(next_cmdline);
1041 if (next_cmdline == NULL)
1042 {
1043 EMSG(_(e_outofmem));
1044 retval = FAIL;
1045 break;
1046 }
1047 }
1048 cmdline_copy = next_cmdline;
1049
1050#ifdef FEAT_EVAL
1051 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001052 * Save the current line when inside a ":while" or ":for", and when
1053 * the command looks like a ":while" or ":for", because we may need it
1054 * later. When there is a '|' and another command, it is stored
1055 * separately, because we need to be able to jump back to it from an
1056 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001058 if (current_line == lines_ga.ga_len
1059 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001061 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 {
1063 retval = FAIL;
1064 break;
1065 }
1066 }
1067 did_endif = FALSE;
1068#endif
1069
1070 if (count++ == 0)
1071 {
1072 /*
1073 * All output from the commands is put below each other, without
1074 * waiting for a return. Don't do this when executing commands
1075 * from a script or when being called recursive (e.g. for ":e
1076 * +command file").
1077 */
1078 if (!(flags & DOCMD_NOWAIT) && !recursive)
1079 {
1080 msg_didout_before_start = msg_didout;
1081 msg_didany = FALSE; /* no output yet */
1082 msg_start();
1083 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001084 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 ++RedrawingDisabled;
1086 did_inc = TRUE;
1087 }
1088 }
1089
1090 if (p_verbose >= 15 && sourcing_name != NULL)
1091 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001093 verbose_enter_scroll();
1094
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 smsg((char_u *)_("line %ld: %s"),
1096 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001097 if (msg_silent == 0)
1098 msg_puts((char_u *)"\n"); /* don't overwrite this */
1099
1100 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 --no_wait_return;
1102 }
1103
1104 /*
1105 * 2. Execute one '|' separated command.
1106 * do_one_cmd() will return NULL if there is no trailing '|'.
1107 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1108 */
1109 ++recursive;
1110 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1111#ifdef FEAT_EVAL
1112 &cstack,
1113#endif
1114 cmd_getline, cmd_cookie);
1115 --recursive;
1116
1117#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001118 if (cmd_cookie == (void *)&cmd_loop_cookie)
1119 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001121 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122#endif
1123
1124 if (next_cmdline == NULL)
1125 {
1126 vim_free(cmdline_copy);
1127 cmdline_copy = NULL;
1128#ifdef FEAT_CMDHIST
1129 /*
1130 * If the command was typed, remember it for the ':' register.
1131 * Do this AFTER executing the command to make :@: work.
1132 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001133 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 && new_last_cmdline != NULL)
1135 {
1136 vim_free(last_cmdline);
1137 last_cmdline = new_last_cmdline;
1138 new_last_cmdline = NULL;
1139 }
1140#endif
1141 }
1142 else
1143 {
1144 /* need to copy the command after the '|' to cmdline_copy, for the
1145 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001146 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 next_cmdline = cmdline_copy;
1148 }
1149
1150
1151#ifdef FEAT_EVAL
1152 /* reset did_emsg for a function that is not aborted by an error */
1153 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001154 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 && !func_has_abort(real_cookie))
1156 did_emsg = FALSE;
1157
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001158 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 {
1160 ++current_line;
1161
1162 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001163 * An ":endwhile", ":endfor" and ":continue" is handled here.
1164 * If we were executing commands, jump back to the ":while" or
1165 * ":for".
1166 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001168 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001170 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001172 /* Jump back to the matching ":while" or ":for". Be careful
1173 * not to use a cs_line[] from an entry that isn't a ":while"
1174 * or ":for": It would make "current_line" invalid and can
1175 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 if (!did_emsg && !got_int && !did_throw
1177 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001178 && (cstack.cs_flags[cstack.cs_idx]
1179 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 && cstack.cs_line[cstack.cs_idx] >= 0
1181 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1182 {
1183 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001184 /* remember we jumped there */
1185 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 line_breakcheck(); /* check if CTRL-C typed */
1187
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001188 /* Check for the next breakpoint at or after the ":while"
1189 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 if (breakpoint != NULL)
1191 {
1192 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001193 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 fname,
1195 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1196 *dbg_tick = debug_tick;
1197 }
1198 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001199 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001201 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001203 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1204 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 }
1206 }
1207
1208 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001209 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001211 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001213 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1215 }
1216 }
1217
1218 /*
1219 * When not inside any ":while" loop, clear remembered lines.
1220 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001221 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 {
1223 if (lines_ga.ga_len > 0)
1224 {
1225 sourcing_lnum =
1226 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1227 free_cmdlines(&lines_ga);
1228 }
1229 current_line = 0;
1230 }
1231
1232 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001233 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1234 * being restored at the ":endtry". Reset them here and set the
1235 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1236 * This includes the case where a missing ":endif", ":endwhile" or
1237 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001239 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001241 cstack.cs_lflags &= ~CSL_HAD_FINA;
1242 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1243 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 did_throw ? (void *)current_exception : NULL);
1245 did_emsg = got_int = did_throw = FALSE;
1246 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1247 }
1248
1249 /* Update global "trylevel" for recursive calls to do_cmdline() from
1250 * within this loop. */
1251 trylevel = initial_trylevel + cstack.cs_trylevel;
1252
1253 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001254 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 * files) is aborted because of an error, an interrupt, or an uncaught
1256 * exception, cancel everything. If it is left normally, reset
1257 * force_abort to get the non-EH compatible abortion behavior for
1258 * the rest of the script.
1259 */
1260 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1261 force_abort = FALSE;
1262
1263 /* Convert an interrupt to an exception if appropriate. */
1264 (void)do_intthrow(&cstack);
1265#endif /* FEAT_EVAL */
1266
1267 }
1268 /*
1269 * Continue executing command lines when:
1270 * - no CTRL-C typed, no aborting error, no exception thrown or try
1271 * conditionals need to be checked for executing finally clauses or
1272 * catching an interrupt exception
1273 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001274 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 * looping for ":source" command or function call.
1276 */
1277 while (!((got_int
1278#ifdef FEAT_EVAL
1279 || (did_emsg && force_abort) || did_throw
1280#endif
1281 )
1282#ifdef FEAT_EVAL
1283 && cstack.cs_trylevel == 0
1284#endif
1285 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001286 && !(did_emsg
1287#ifdef FEAT_EVAL
1288 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001289 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001290 * the :endtry to be missed. */
1291 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1292#endif
1293 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001294 && (getline_equal(fgetline, cookie, getexmodeline)
1295 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 && (next_cmdline != NULL
1297#ifdef FEAT_EVAL
1298 || cstack.cs_idx >= 0
1299#endif
1300 || (flags & DOCMD_REPEAT)));
1301
1302 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001303 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304#ifdef FEAT_EVAL
1305 free_cmdlines(&lines_ga);
1306 ga_clear(&lines_ga);
1307
1308 if (cstack.cs_idx >= 0)
1309 {
1310 /*
1311 * If a sourced file or executed function ran to its end, report the
1312 * unclosed conditional.
1313 */
1314 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001315 && ((getline_equal(fgetline, cookie, getsourceline)
1316 && !source_finished(fgetline, cookie))
1317 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 && !func_has_ended(real_cookie))))
1319 {
1320 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1321 EMSG(_(e_endtry));
1322 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1323 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001324 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1325 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 else
1327 EMSG(_(e_endif));
1328 }
1329
1330 /*
1331 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1332 * ":endtry" in a sourced file or executed function. If the try
1333 * conditional is in its finally clause, ignore anything pending.
1334 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001335 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 */
1337 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001338 {
1339 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1340
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001341 if (idx >= 0)
1342 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001343 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1344 &cstack.cs_looplevel);
1345 }
1346 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 trylevel = initial_trylevel;
1348 }
1349
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001350 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1351 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001353 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 ? (char_u *)"endfunction" : (char_u *)NULL);
1355
1356 if (trylevel == 0)
1357 {
1358 /*
1359 * When an exception is being thrown out of the outermost try
1360 * conditional, discard the uncaught exception, disable the conversion
1361 * of interrupts or errors to exceptions, and ensure that no more
1362 * commands are executed.
1363 */
1364 if (did_throw)
1365 {
1366 void *p = NULL;
1367 char_u *saved_sourcing_name;
1368 int saved_sourcing_lnum;
1369 struct msglist *messages = NULL, *next;
1370
1371 /*
1372 * If the uncaught exception is a user exception, report it as an
1373 * error. If it is an error exception, display the saved error
1374 * message now. For an interrupt exception, do nothing; the
1375 * interrupt message is given elsewhere.
1376 */
1377 switch (current_exception->type)
1378 {
1379 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001380 vim_snprintf((char *)IObuff, IOSIZE,
1381 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 current_exception->value);
1383 p = vim_strsave(IObuff);
1384 break;
1385 case ET_ERROR:
1386 messages = current_exception->messages;
1387 current_exception->messages = NULL;
1388 break;
1389 case ET_INTERRUPT:
1390 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 }
1392
1393 saved_sourcing_name = sourcing_name;
1394 saved_sourcing_lnum = sourcing_lnum;
1395 sourcing_name = current_exception->throw_name;
1396 sourcing_lnum = current_exception->throw_lnum;
1397 current_exception->throw_name = NULL;
1398
1399 discard_current_exception(); /* uses IObuff if 'verbose' */
1400 suppress_errthrow = TRUE;
1401 force_abort = TRUE;
1402
1403 if (messages != NULL)
1404 {
1405 do
1406 {
1407 next = messages->next;
1408 emsg(messages->msg);
1409 vim_free(messages->msg);
1410 vim_free(messages);
1411 messages = next;
1412 }
1413 while (messages != NULL);
1414 }
1415 else if (p != NULL)
1416 {
1417 emsg(p);
1418 vim_free(p);
1419 }
1420 vim_free(sourcing_name);
1421 sourcing_name = saved_sourcing_name;
1422 sourcing_lnum = saved_sourcing_lnum;
1423 }
1424
1425 /*
1426 * On an interrupt or an aborting error not converted to an exception,
1427 * disable the conversion of errors to exceptions. (Interrupts are not
1428 * converted any more, here.) This enables also the interrupt message
1429 * when force_abort is set and did_emsg unset in case of an interrupt
1430 * from a finally clause after an error.
1431 */
1432 else if (got_int || (did_emsg && force_abort))
1433 suppress_errthrow = TRUE;
1434 }
1435
1436 /*
1437 * The current cstack will be freed when do_cmdline() returns. An uncaught
1438 * exception will have to be rethrown in the previous cstack. If a function
1439 * has just returned or a script file was just finished and the previous
1440 * cstack belongs to the same function or, respectively, script file, it
1441 * will have to be checked for finally clauses to be executed due to the
1442 * ":return" or ":finish". This is done in do_one_cmd().
1443 */
1444 if (did_throw)
1445 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001446 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001448 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 && ex_nesting_level > func_level(real_cookie) + 1))
1450 {
1451 if (!did_throw)
1452 check_cstack = TRUE;
1453 }
1454 else
1455 {
1456 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001457 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 --ex_nesting_level;
1459 /*
1460 * Go to debug mode when returning from a function in which we are
1461 * single-stepping.
1462 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001463 if ((getline_equal(fgetline, cookie, getsourceline)
1464 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001466 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 ? (char_u *)_("End of sourced file")
1468 : (char_u *)_("End of function"));
1469 }
1470
1471 /*
1472 * Restore the exception environment (done after returning from the
1473 * debugger).
1474 */
1475 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001476 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477
1478 msg_list = saved_msg_list;
1479#endif /* FEAT_EVAL */
1480
1481 /*
1482 * If there was too much output to fit on the command line, ask the user to
1483 * hit return before redrawing the screen. With the ":global" command we do
1484 * this only once after the command is finished.
1485 */
1486 if (did_inc)
1487 {
1488 --RedrawingDisabled;
1489 --no_wait_return;
1490 msg_scroll = FALSE;
1491
1492 /*
1493 * When just finished an ":if"-":else" which was typed, no need to
1494 * wait for hit-return. Also for an error situation.
1495 */
1496 if (retval == FAIL
1497#ifdef FEAT_EVAL
1498 || (did_endif && KeyTyped && !did_emsg)
1499#endif
1500 )
1501 {
1502 need_wait_return = FALSE;
1503 msg_didany = FALSE; /* don't wait when restarting edit */
1504 }
1505 else if (need_wait_return)
1506 {
1507 /*
1508 * The msg_start() above clears msg_didout. The wait_return we do
1509 * here should not overwrite the command that may be shown before
1510 * doing that.
1511 */
1512 msg_didout |= msg_didout_before_start;
1513 wait_return(FALSE);
1514 }
1515 }
1516
Bram Moolenaar2a942252012-11-28 23:03:07 +01001517#ifdef FEAT_EVAL
1518 did_endif = FALSE; /* in case do_cmdline used recursively */
1519#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 /*
1521 * Reset if_level, in case a sourced script file contains more ":if" than
1522 * ":endif" (could be ":if x | foo | endif").
1523 */
1524 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001525#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001526
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 --call_depth;
1528 return retval;
1529}
1530
1531#ifdef FEAT_EVAL
1532/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001533 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 */
1535 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001536get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001538 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 wcmd_T *wp;
1540 char_u *line;
1541
1542 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1543 {
1544 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001545 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001547 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 if (cp->getline == NULL)
1549 line = getcmdline(c, 0L, indent);
1550 else
1551 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001552 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 ++cp->current_line;
1554
1555 return line;
1556 }
1557
1558 KeyTyped = FALSE;
1559 ++cp->current_line;
1560 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1561 sourcing_lnum = wp->lnum;
1562 return vim_strsave(wp->line);
1563}
1564
1565/*
1566 * Store a line in "gap" so that a ":while" loop can execute it again.
1567 */
1568 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001569store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570{
1571 if (ga_grow(gap, 1) == FAIL)
1572 return FAIL;
1573 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1574 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1575 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 return OK;
1577}
1578
1579/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001580 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 */
1582 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001583free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584{
1585 while (gap->ga_len > 0)
1586 {
1587 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1588 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 }
1590}
1591#endif
1592
1593/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001594 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1595 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001598getline_equal(
1599 char_u *(*fgetline)(int, void *, int),
1600 void *cookie UNUSED, /* argument for fgetline() */
1601 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602{
1603#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001604 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001605 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606
Bram Moolenaar89d40322006-08-29 15:30:07 +00001607 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001608 * function that's originally used to obtain the lines. This may be
1609 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001610 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001611 cp = (struct loop_cookie *)cookie;
1612 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 {
1614 gp = cp->getline;
1615 cp = cp->cookie;
1616 }
1617 return gp == func;
1618#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001619 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620#endif
1621}
1622
1623#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1624/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001625 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 * getline function. Otherwise return "cookie".
1627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001629getline_cookie(
1630 char_u *(*fgetline)(int, void *, int) UNUSED,
1631 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632{
1633# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001634 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001635 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
Bram Moolenaar89d40322006-08-29 15:30:07 +00001637 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001638 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001640 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001641 cp = (struct loop_cookie *)cookie;
1642 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 {
1644 gp = cp->getline;
1645 cp = cp->cookie;
1646 }
1647 return cp;
1648# else
1649 return cookie;
1650# endif
1651}
1652#endif
1653
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001654
1655/*
1656 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1657 * ":bwipeout", etc.
1658 * Returns the buffer number.
1659 */
1660 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001661compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001662{
1663 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001664 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001665 int count = offset;
1666
1667 buf = firstbuf;
1668 while (buf->b_next != NULL && buf->b_fnum < lnum)
1669 buf = buf->b_next;
1670 while (count != 0)
1671 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001672 count += (offset < 0) ? 1 : -1;
1673 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1674 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001675 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001676 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001677 if (addr_type == ADDR_LOADED_BUFFERS)
1678 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001679 while (buf->b_ml.ml_mfp == NULL)
1680 {
1681 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1682 if (nextbuf == NULL)
1683 break;
1684 buf = nextbuf;
1685 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001686 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001687 /* we might have gone too far, last buffer is not loadedd */
1688 if (addr_type == ADDR_LOADED_BUFFERS)
1689 while (buf->b_ml.ml_mfp == NULL)
1690 {
1691 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1692 if (nextbuf == NULL)
1693 break;
1694 buf = nextbuf;
1695 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001696 return buf->b_fnum;
1697}
1698
Bram Moolenaarf240e182014-11-27 18:33:02 +01001699#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001700static int current_win_nr(win_T *win);
1701static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001702
1703 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001704current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001705{
1706 win_T *wp;
1707 int nr = 0;
1708
Bram Moolenaar29323592016-07-24 22:04:11 +02001709 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001710 {
1711 ++nr;
1712 if (wp == win)
1713 break;
1714 }
1715 return nr;
1716}
1717
1718 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001719current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001720{
1721 tabpage_T *tp;
1722 int nr = 0;
1723
Bram Moolenaar29323592016-07-24 22:04:11 +02001724 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001725 {
1726 ++nr;
1727 if (tp == tab)
1728 break;
1729 }
1730 return nr;
1731}
1732
1733# define CURRENT_WIN_NR current_win_nr(curwin)
1734# define LAST_WIN_NR current_win_nr(NULL)
1735# define CURRENT_TAB_NR current_tab_nr(curtab)
1736# define LAST_TAB_NR current_tab_nr(NULL)
1737#else
1738# define CURRENT_WIN_NR 1
1739# define LAST_WIN_NR 1
1740# define CURRENT_TAB_NR 1
1741# define LAST_TAB_NR 1
1742#endif
1743
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001744
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745/*
1746 * Execute one Ex command.
1747 *
1748 * If 'sourcing' is TRUE, the command will be included in the error message.
1749 *
1750 * 1. skip comment lines and leading space
1751 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001752 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001753 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001754 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001755 * 6. parse arguments
1756 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001758 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 *
1760 * This function may be called recursively!
1761 */
1762#if (_MSC_VER == 1200)
1763/*
Bram Moolenaared203462004-06-16 11:19:22 +00001764 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001766 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767#endif
1768 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001769do_one_cmd(
1770 char_u **cmdlinep,
1771 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001773 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001775 char_u *(*fgetline)(int, void *, int),
1776 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777{
1778 char_u *p;
1779 linenr_T lnum;
1780 long n;
1781 char_u *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001782 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 exarg_T ea; /* Ex command arguments */
1784 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001785 int save_msg_scroll = msg_scroll;
1786 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001788#ifdef HAVE_SANDBOX
1789 int did_sandbox = FALSE;
1790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 cmdmod_T save_cmdmod;
1792 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001793 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794
1795 vim_memset(&ea, 0, sizeof(ea));
1796 ea.line1 = 1;
1797 ea.line2 = 1;
1798#ifdef FEAT_EVAL
1799 ++ex_nesting_level;
1800#endif
1801
Bram Moolenaar21691f82012-12-05 19:13:18 +01001802 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 if (quitmore
1804#ifdef FEAT_EVAL
1805 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001806 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001807#endif
1808#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001809 /* avoid that an autocommand, e.g. QuitPre, does this */
1810 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811#endif
1812 )
1813 --quitmore;
1814
1815 /*
1816 * Reset browse, confirm, etc.. They are restored when returning, for
1817 * recursive calls.
1818 */
1819 save_cmdmod = cmdmod;
1820 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1821
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001822 /* "#!anything" is handled like a comment. */
1823 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1824 goto doend;
1825
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 /*
1827 * Repeat until no more command modifiers are found.
1828 */
1829 ea.cmd = *cmdlinep;
1830 for (;;)
1831 {
1832/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001833 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 */
1835 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1836 ++ea.cmd;
1837
1838 /* in ex mode, an empty line works like :+ */
1839 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001840 && (getline_equal(fgetline, cookie, getexmodeline)
1841 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001842 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 {
1844 ea.cmd = (char_u *)"+";
1845 ex_pressedreturn = TRUE;
1846 }
1847
1848 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001849 if (*ea.cmd == '"')
1850 goto doend;
1851 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001852 {
1853 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856
1857/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001858 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 */
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001860 p = skip_range(ea.cmd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 switch (*p)
1862 {
1863 /* When adding an entry, also modify cmd_exists(). */
1864 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1865 break;
1866#ifdef FEAT_WINDOWS
1867 cmdmod.split |= WSP_ABOVE;
1868#endif
1869 continue;
1870
1871 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1872 {
1873#ifdef FEAT_WINDOWS
1874 cmdmod.split |= WSP_BELOW;
1875#endif
1876 continue;
1877 }
1878 if (checkforcmd(&ea.cmd, "browse", 3))
1879 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001880#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 cmdmod.browse = TRUE;
1882#endif
1883 continue;
1884 }
1885 if (!checkforcmd(&ea.cmd, "botright", 2))
1886 break;
1887#ifdef FEAT_WINDOWS
1888 cmdmod.split |= WSP_BOT;
1889#endif
1890 continue;
1891
1892 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1893 break;
1894#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1895 cmdmod.confirm = TRUE;
1896#endif
1897 continue;
1898
1899 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1900 {
1901 cmdmod.keepmarks = TRUE;
1902 continue;
1903 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001904 if (checkforcmd(&ea.cmd, "keepalt", 5))
1905 {
1906 cmdmod.keepalt = TRUE;
1907 continue;
1908 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001909 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1910 {
1911 cmdmod.keeppatterns = TRUE;
1912 continue;
1913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1915 break;
1916 cmdmod.keepjumps = TRUE;
1917 continue;
1918
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001919 case 'f': /* only accept ":filter {pat} cmd" */
1920 {
1921 char_u *reg_pat;
1922
1923 if (!checkforcmd(&p, "filter", 4)
1924 || *p == NUL || ends_excmd(*p))
1925 break;
Bram Moolenaard29459b2016-08-26 22:29:11 +02001926 if (*p == '!')
1927 {
1928 cmdmod.filter_force = TRUE;
1929 p = skipwhite(p + 1);
1930 if (*p == NUL || ends_excmd(*p))
1931 break;
1932 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001933 p = skip_vimgrep_pat(p, &reg_pat, NULL);
1934 if (p == NULL || *p == NUL)
1935 break;
1936 cmdmod.filter_regmatch.regprog =
1937 vim_regcomp(reg_pat, RE_MAGIC);
1938 if (cmdmod.filter_regmatch.regprog == NULL)
1939 break;
1940 ea.cmd = p;
1941 continue;
1942 }
1943
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 /* ":hide" and ":hide | cmd" are not modifiers */
1945 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1946 || *p == NUL || ends_excmd(*p))
1947 break;
1948 ea.cmd = p;
1949 cmdmod.hide = TRUE;
1950 continue;
1951
1952 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1953 {
1954 cmdmod.lockmarks = TRUE;
1955 continue;
1956 }
1957
1958 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1959 break;
1960#ifdef FEAT_WINDOWS
1961 cmdmod.split |= WSP_ABOVE;
1962#endif
1963 continue;
1964
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001965 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001966 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001967#ifdef FEAT_AUTOCMD
1968 if (cmdmod.save_ei == NULL)
1969 {
1970 /* Set 'eventignore' to "all". Restore the
1971 * existing option value later. */
1972 cmdmod.save_ei = vim_strsave(p_ei);
1973 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001974 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001975 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001976#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001977 continue;
1978 }
Bram Moolenaar3bcfca32016-07-30 19:39:29 +02001979 if (!checkforcmd(&ea.cmd, "noswapfile", 3))
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001980 break;
1981 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001982 continue;
1983
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1985 break;
1986#ifdef FEAT_WINDOWS
1987 cmdmod.split |= WSP_BELOW;
1988#endif
1989 continue;
1990
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001991 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1992 {
1993#ifdef HAVE_SANDBOX
1994 if (!did_sandbox)
1995 ++sandbox;
1996 did_sandbox = TRUE;
1997#endif
1998 continue;
1999 }
2000 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002002 if (save_msg_silent == -1)
2003 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
2006 {
2007 /* ":silent!", but not "silent !cmd" */
2008 ea.cmd = skipwhite(ea.cmd + 1);
2009 ++emsg_silent;
2010 ++did_esilent;
2011 }
2012 continue;
2013
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002014 case 't': if (checkforcmd(&p, "tab", 3))
2015 {
2016#ifdef FEAT_WINDOWS
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02002017 long tabnr = get_address(&ea, &ea.cmd, ADDR_TABS,
2018 ea.skip, FALSE);
2019 if (tabnr == MAXLNUM)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002020 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02002021 else
2022 {
2023 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2024 {
2025 errormsg = (char_u *)_(e_invrange);
2026 goto doend;
2027 }
2028 cmdmod.tab = tabnr + 1;
2029 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002030 ea.cmd = p;
2031#endif
2032 continue;
2033 }
2034 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 break;
2036#ifdef FEAT_WINDOWS
2037 cmdmod.split |= WSP_TOP;
2038#endif
2039 continue;
2040
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002041 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
2042 break;
2043 if (save_msg_silent == -1)
2044 save_msg_silent = msg_silent;
2045 msg_silent = 0;
2046 continue;
2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
2049 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002050#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 cmdmod.split |= WSP_VERT;
2052#endif
2053 continue;
2054 }
2055 if (!checkforcmd(&p, "verbose", 4))
2056 break;
2057 if (verbose_save < 0)
2058 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00002059 if (vim_isdigit(*ea.cmd))
2060 p_verbose = atoi((char *)ea.cmd);
2061 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 p_verbose = 1;
2063 ea.cmd = p;
2064 continue;
2065 }
2066 break;
2067 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002068 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069
2070#ifdef FEAT_EVAL
2071 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2072 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2073#else
2074 ea.skip = (if_level > 0);
2075#endif
2076
2077#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002078# ifdef FEAT_PROFILE
2079 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002080 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002081 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002082 if (getline_equal(fgetline, cookie, get_func_line))
2083 func_line_exec(getline_cookie(fgetline, cookie));
2084 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002085 script_line_exec();
2086 }
2087#endif
2088
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 /* May go to debug mode. If this happens and the ">quit" debug command is
2090 * used, throw an interrupt exception and skip the next command. */
2091 dbg_check_breakpoint(&ea);
2092 if (!ea.skip && got_int)
2093 {
2094 ea.skip = TRUE;
2095 (void)do_intthrow(cstack);
2096 }
2097#endif
2098
2099/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002100 * 3. Skip over the range to find the command. Let "p" point to after it.
2101 *
2102 * We need the command to know what kind of range it uses.
2103 */
2104 cmd = ea.cmd;
2105 ea.cmd = skip_range(ea.cmd, NULL);
2106 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2107 ea.cmd = skipwhite(ea.cmd + 1);
2108 p = find_command(&ea, NULL);
2109
2110/*
2111 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 *
2113 * where 'addr' is:
2114 *
2115 * % (entire file)
2116 * $ [+-NUM]
2117 * 'x [+-NUM] (where x denotes a currently defined mark)
2118 * . [+-NUM]
2119 * [+-NUM]..
2120 * NUM
2121 *
2122 * The ea.cmd pointer is updated to point to the first character following the
2123 * range spec. If an initial address is found, but no second, the upper bound
2124 * is equal to the lower.
2125 */
2126
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002127 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002128 if (!IS_USER_CMDIDX(ea.cmdidx))
2129 {
2130 if (ea.cmdidx != CMD_SIZE)
2131 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2132 else
2133 ea.addr_type = ADDR_LINES;
2134
2135#ifdef FEAT_WINDOWS
2136 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002137 if (ea.cmdidx == CMD_wincmd && p != NULL)
2138 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002139#endif
2140 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002141
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002143 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 for (;;)
2145 {
2146 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002147 switch (ea.addr_type)
2148 {
2149 case ADDR_LINES:
2150 /* default is current line number */
2151 ea.line2 = curwin->w_cursor.lnum;
2152 break;
2153 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002154 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002155 ea.line2 = lnum;
2156 break;
2157 case ADDR_ARGUMENTS:
2158 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002159 if (ea.line2 > ARGCOUNT)
2160 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002161 break;
2162 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002163 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002164 ea.line2 = curbuf->b_fnum;
2165 break;
2166 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002167 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002168 ea.line2 = lnum;
2169 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002170#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002171 case ADDR_QUICKFIX:
2172 ea.line2 = qf_get_cur_valid_idx(&ea);
2173 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002174#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002177 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip,
2178 ea.addr_count == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 if (ea.cmd == NULL) /* error detected */
2180 goto doend;
2181 if (lnum == MAXLNUM)
2182 {
2183 if (*ea.cmd == '%') /* '%' - all lines */
2184 {
2185 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002186 switch (ea.addr_type)
2187 {
2188 case ADDR_LINES:
2189 ea.line1 = 1;
2190 ea.line2 = curbuf->b_ml.ml_line_count;
2191 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002192 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002193 {
2194 buf_T *buf = firstbuf;
2195
2196 while (buf->b_next != NULL
2197 && buf->b_ml.ml_mfp == NULL)
2198 buf = buf->b_next;
2199 ea.line1 = buf->b_fnum;
2200 buf = lastbuf;
2201 while (buf->b_prev != NULL
2202 && buf->b_ml.ml_mfp == NULL)
2203 buf = buf->b_prev;
2204 ea.line2 = buf->b_fnum;
2205 break;
2206 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002207 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002208 ea.line1 = firstbuf->b_fnum;
2209 ea.line2 = lastbuf->b_fnum;
2210 break;
2211 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002212 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002213 if (IS_USER_CMDIDX(ea.cmdidx))
2214 {
2215 ea.line1 = 1;
2216 ea.line2 = ea.addr_type == ADDR_WINDOWS
2217 ? LAST_WIN_NR : LAST_TAB_NR;
2218 }
2219 else
2220 {
2221 /* there is no Vim command which uses '%' and
2222 * ADDR_WINDOWS or ADDR_TABS */
2223 errormsg = (char_u *)_(e_invrange);
2224 goto doend;
2225 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002226 break;
2227 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002228 if (ARGCOUNT == 0)
2229 ea.line1 = ea.line2 = 0;
2230 else
2231 {
2232 ea.line1 = 1;
2233 ea.line2 = ARGCOUNT;
2234 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002235 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002236#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002237 case ADDR_QUICKFIX:
2238 ea.line1 = 1;
2239 ea.line2 = qf_get_size(&ea);
2240 if (ea.line2 == 0)
2241 ea.line2 = 1;
2242 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002243#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 ++ea.addr_count;
2246 }
2247 /* '*' - visual area */
2248 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2249 {
2250 pos_T *fp;
2251
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002252 if (ea.addr_type != ADDR_LINES)
2253 {
2254 errormsg = (char_u *)_(e_invrange);
2255 goto doend;
2256 }
2257
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 ++ea.cmd;
2259 if (!ea.skip)
2260 {
2261 fp = getmark('<', FALSE);
2262 if (check_mark(fp) == FAIL)
2263 goto doend;
2264 ea.line1 = fp->lnum;
2265 fp = getmark('>', FALSE);
2266 if (check_mark(fp) == FAIL)
2267 goto doend;
2268 ea.line2 = fp->lnum;
2269 ++ea.addr_count;
2270 }
2271 }
2272 }
2273 else
2274 ea.line2 = lnum;
2275 ea.addr_count++;
2276
2277 if (*ea.cmd == ';')
2278 {
2279 if (!ea.skip)
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002280 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 curwin->w_cursor.lnum = ea.line2;
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002282 /* don't leave the cursor on an illegal line */
2283 check_cursor_lnum();
2284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 }
2286 else if (*ea.cmd != ',')
2287 break;
2288 ++ea.cmd;
2289 }
2290
2291 /* One address given: set start and end lines */
2292 if (ea.addr_count == 1)
2293 {
2294 ea.line1 = ea.line2;
2295 /* ... but only implicit: really no address given */
2296 if (lnum == MAXLNUM)
2297 ea.addr_count = 0;
2298 }
2299
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002301 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 */
2303
2304 /*
2305 * Skip ':' and any white space
2306 */
2307 ea.cmd = skipwhite(ea.cmd);
2308 while (*ea.cmd == ':')
2309 ea.cmd = skipwhite(ea.cmd + 1);
2310
2311 /*
2312 * If we got a line, but no command, then go to the line.
2313 * If we find a '|' or '\n' we set ea.nextcmd.
2314 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002315 if (*ea.cmd == NUL || *ea.cmd == '"'
2316 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 {
2318 /*
2319 * strange vi behaviour:
2320 * ":3" jumps to line 3
2321 * ":3|..." prints line 3
2322 * ":|" prints current line
2323 */
2324 if (ea.skip) /* skip this if inside :if */
2325 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002326 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 {
2328 ea.cmdidx = CMD_print;
2329 ea.argt = RANGE+COUNT+TRLBAR;
2330 if ((errormsg = invalid_range(&ea)) == NULL)
2331 {
2332 correct_range(&ea);
2333 ex_print(&ea);
2334 }
2335 }
2336 else if (ea.addr_count != 0)
2337 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002338 if (ea.line2 > curbuf->b_ml.ml_line_count)
2339 {
2340 /* With '-' in 'cpoptions' a line number past the file is an
2341 * error, otherwise put it at the end of the file. */
2342 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2343 ea.line2 = -1;
2344 else
2345 ea.line2 = curbuf->b_ml.ml_line_count;
2346 }
2347
2348 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002349 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 else
2351 {
2352 if (ea.line2 == 0)
2353 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 else
2355 curwin->w_cursor.lnum = ea.line2;
2356 beginline(BL_SOL | BL_FIX);
2357 }
2358 }
2359 goto doend;
2360 }
2361
Bram Moolenaard5005162014-08-22 23:05:54 +02002362#ifdef FEAT_AUTOCMD
2363 /* If this looks like an undefined user command and there are CmdUndefined
2364 * autocommands defined, trigger the matching autocommands. */
2365 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2366 && ASCII_ISUPPER(*ea.cmd)
2367 && has_cmdundefined())
2368 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002369 int ret;
2370
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002371 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002372 while (ASCII_ISALNUM(*p))
2373 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002374 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002375 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2376 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002377 /* If the autocommands did something and didn't cause an error, try
2378 * finding the command again. */
Bram Moolenaareac784e2016-07-28 22:08:24 +02002379 p = (ret && !aborting()) ? find_command(&ea, NULL) : ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002380 }
2381#endif
2382
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383#ifdef FEAT_USR_CMDS
2384 if (p == NULL)
2385 {
2386 if (!ea.skip)
2387 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2388 goto doend;
2389 }
2390 /* Check for wrong commands. */
2391 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2392 {
2393 errormsg = uc_fun_cmd();
2394 goto doend;
2395 }
2396#endif
2397 if (ea.cmdidx == CMD_SIZE)
2398 {
2399 if (!ea.skip)
2400 {
2401 STRCPY(IObuff, _("E492: Not an editor command"));
2402 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002403 {
2404 /* If the modifier was parsed OK the error must be in the
2405 * following command */
2406 if (after_modifier != NULL)
2407 append_command(after_modifier);
2408 else
2409 append_command(*cmdlinep);
2410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002412 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 }
2414 goto doend;
2415 }
2416
Bram Moolenaar958636c2014-10-21 20:01:58 +02002417 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2418 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002419#ifdef HAVE_EX_SCRIPT_NI
2420 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2421#endif
2422 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423
2424#ifndef FEAT_EVAL
2425 /*
2426 * When the expression evaluation is disabled, recognize the ":if" and
2427 * ":endif" commands and ignore everything in between it.
2428 */
2429 if (ea.cmdidx == CMD_if)
2430 ++if_level;
2431 if (if_level)
2432 {
2433 if (ea.cmdidx == CMD_endif)
2434 --if_level;
2435 goto doend;
2436 }
2437
2438#endif
2439
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002440 /* forced commands */
2441 if (*p == '!' && ea.cmdidx != CMD_substitute
2442 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 {
2444 ++p;
2445 ea.forceit = TRUE;
2446 }
2447 else
2448 ea.forceit = FALSE;
2449
2450/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002451 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002453 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002454 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455
2456 if (!ea.skip)
2457 {
2458#ifdef HAVE_SANDBOX
2459 if (sandbox != 0 && !(ea.argt & SBOXOK))
2460 {
2461 /* Command not allowed in sandbox. */
2462 errormsg = (char_u *)_(e_sandbox);
2463 goto doend;
2464 }
2465#endif
2466 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2467 {
2468 /* Command not allowed in non-'modifiable' buffer */
2469 errormsg = (char_u *)_(e_modifiable);
2470 goto doend;
2471 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002472
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002473 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002474 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002476 /* Command not allowed when editing the command line. */
Bram Moolenaar5a497892016-09-03 16:29:04 +02002477 errormsg = get_text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 goto doend;
2479 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002480#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002481 /* Disallow editing another buffer when "curbuf_lock" is set.
2482 * Do allow ":edit" (check for argument later).
2483 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002484 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002485 && ea.cmdidx != CMD_edit
2486 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002487 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002488 && curbuf_locked())
2489 goto doend;
2490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491
2492 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2493 {
2494 /* no range allowed */
2495 errormsg = (char_u *)_(e_norange);
2496 goto doend;
2497 }
2498 }
2499
2500 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2501 {
2502 errormsg = (char_u *)_(e_nobang);
2503 goto doend;
2504 }
2505
2506 /*
2507 * Don't complain about the range if it is not used
2508 * (could happen if line_count is accidentally set to 0).
2509 */
2510 if (!ea.skip && !ni)
2511 {
2512 /*
2513 * If the range is backwards, ask for confirmation and, if given, swap
2514 * ea.line1 & ea.line2 so it's forwards again.
2515 * When global command is busy, don't ask, will fail below.
2516 */
2517 if (!global_busy && ea.line1 > ea.line2)
2518 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002519 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002521 if (sourcing || exmode_active)
2522 {
2523 errormsg = (char_u *)_("E493: Backwards range given");
2524 goto doend;
2525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 if (ask_yesno((char_u *)
2527 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002528 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 }
2530 lnum = ea.line1;
2531 ea.line1 = ea.line2;
2532 ea.line2 = lnum;
2533 }
2534 if ((errormsg = invalid_range(&ea)) != NULL)
2535 goto doend;
2536 }
2537
2538 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2539 ea.line2 = 1;
2540
2541 correct_range(&ea);
2542
2543#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002544 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2545 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 {
2547 /* Put the first line at the start of a closed fold, put the last line
2548 * at the end of a closed fold. */
2549 (void)hasFolding(ea.line1, &ea.line1, NULL);
2550 (void)hasFolding(ea.line2, NULL, &ea.line2);
2551 }
2552#endif
2553
2554#ifdef FEAT_QUICKFIX
2555 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002556 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 * option here, so things like % get expanded.
2558 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002559 p = replace_makeprg(&ea, p, cmdlinep);
2560 if (p == NULL)
2561 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562#endif
2563
2564 /*
2565 * Skip to start of argument.
2566 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2567 */
2568 if (ea.cmdidx == CMD_bang)
2569 ea.arg = p;
2570 else
2571 ea.arg = skipwhite(p);
2572
2573 /*
2574 * Check for "++opt=val" argument.
2575 * Must be first, allow ":w ++enc=utf8 !cmd"
2576 */
2577 if (ea.argt & ARGOPT)
2578 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2579 if (getargopt(&ea) == FAIL && !ni)
2580 {
2581 errormsg = (char_u *)_(e_invarg);
2582 goto doend;
2583 }
2584
2585 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2586 {
2587 if (*ea.arg == '>') /* append */
2588 {
2589 if (*++ea.arg != '>') /* typed wrong */
2590 {
2591 errormsg = (char_u *)_("E494: Use w or w>>");
2592 goto doend;
2593 }
2594 ea.arg = skipwhite(ea.arg + 1);
2595 ea.append = TRUE;
2596 }
2597 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2598 {
2599 ++ea.arg;
2600 ea.usefilter = TRUE;
2601 }
2602 }
2603
2604 if (ea.cmdidx == CMD_read)
2605 {
2606 if (ea.forceit)
2607 {
2608 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2609 ea.forceit = FALSE;
2610 }
2611 else if (*ea.arg == '!') /* :r !filter */
2612 {
2613 ++ea.arg;
2614 ea.usefilter = TRUE;
2615 }
2616 }
2617
2618 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2619 {
2620 ea.amount = 1;
2621 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2622 {
2623 ++ea.arg;
2624 ++ea.amount;
2625 }
2626 ea.arg = skipwhite(ea.arg);
2627 }
2628
2629 /*
2630 * Check for "+command" argument, before checking for next command.
2631 * Don't do this for ":read !cmd" and ":write !cmd".
2632 */
2633 if ((ea.argt & EDITCMD) && !ea.usefilter)
2634 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2635
2636 /*
2637 * Check for '|' to separate commands and '"' to start comments.
2638 * Don't do this for ":read !cmd" and ":write !cmd".
2639 */
2640 if ((ea.argt & TRLBAR) && !ea.usefilter)
2641 separate_nextcmd(&ea);
2642
2643 /*
2644 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002645 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2646 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002648 else if (ea.cmdidx == CMD_bang
2649 || ea.cmdidx == CMD_global
2650 || ea.cmdidx == CMD_vglobal
2651 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 {
2653 for (p = ea.arg; *p; ++p)
2654 {
2655 /* Remove one backslash before a newline, so that it's possible to
2656 * pass a newline to the shell and also a newline that is preceded
2657 * with a backslash. This makes it impossible to end a shell
2658 * command in a backslash, but that doesn't appear useful.
2659 * Halving the number of backslashes is incompatible with previous
2660 * versions. */
2661 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002662 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 else if (*p == '\n')
2664 {
2665 ea.nextcmd = p + 1;
2666 *p = NUL;
2667 break;
2668 }
2669 }
2670 }
2671
2672 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2673 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002674 buf_T *buf;
2675
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002677 switch (ea.addr_type)
2678 {
2679 case ADDR_LINES:
2680 ea.line2 = curbuf->b_ml.ml_line_count;
2681 break;
2682 case ADDR_LOADED_BUFFERS:
2683 buf = firstbuf;
2684 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2685 buf = buf->b_next;
2686 ea.line1 = buf->b_fnum;
2687 buf = lastbuf;
2688 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2689 buf = buf->b_prev;
2690 ea.line2 = buf->b_fnum;
2691 break;
2692 case ADDR_BUFFERS:
2693 ea.line1 = firstbuf->b_fnum;
2694 ea.line2 = lastbuf->b_fnum;
2695 break;
2696 case ADDR_WINDOWS:
2697 ea.line2 = LAST_WIN_NR;
2698 break;
2699 case ADDR_TABS:
2700 ea.line2 = LAST_TAB_NR;
2701 break;
2702 case ADDR_ARGUMENTS:
2703 if (ARGCOUNT == 0)
2704 ea.line1 = ea.line2 = 0;
2705 else
2706 ea.line2 = ARGCOUNT;
2707 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002708#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002709 case ADDR_QUICKFIX:
2710 ea.line2 = qf_get_size(&ea);
2711 if (ea.line2 == 0)
2712 ea.line2 = 1;
2713 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002714#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 }
2717
2718 /* accept numbered register only when no count allowed (:put) */
2719 if ( (ea.argt & REGSTR)
2720 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002721 /* Do not allow register = for user commands */
2722 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2724 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002725#ifndef FEAT_CLIPBOARD
2726 /* check these explicitly for a more specific error message */
2727 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002729 errormsg = (char_u *)_(e_invalidreg);
2730 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 }
2732#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002733 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2734 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002735 {
2736 ea.regname = *ea.arg++;
2737#ifdef FEAT_EVAL
2738 /* for '=' register: accept the rest of the line as an expression */
2739 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2740 {
2741 set_expr_line(vim_strsave(ea.arg));
2742 ea.arg += STRLEN(ea.arg);
2743 }
2744#endif
2745 ea.arg = skipwhite(ea.arg);
2746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 }
2748
2749 /*
2750 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2751 * count, it's a buffer name.
2752 */
2753 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2754 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2755 || vim_iswhite(*p)))
2756 {
2757 n = getdigits(&ea.arg);
2758 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002759 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 {
2761 errormsg = (char_u *)_(e_zerocount);
2762 goto doend;
2763 }
2764 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2765 {
2766 ea.line2 = n;
2767 if (ea.addr_count == 0)
2768 ea.addr_count = 1;
2769 }
2770 else
2771 {
2772 ea.line1 = ea.line2;
2773 ea.line2 += n - 1;
2774 ++ea.addr_count;
2775 /*
2776 * Be vi compatible: no error message for out of range.
2777 */
2778 if (ea.line2 > curbuf->b_ml.ml_line_count)
2779 ea.line2 = curbuf->b_ml.ml_line_count;
2780 }
2781 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002782
2783 /*
2784 * Check for flags: 'l', 'p' and '#'.
2785 */
2786 if (ea.argt & EXFLAGS)
2787 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002789 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002790 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 {
2792 errormsg = (char_u *)_(e_trailing);
2793 goto doend;
2794 }
2795
2796 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2797 {
2798 errormsg = (char_u *)_(e_argreq);
2799 goto doend;
2800 }
2801
2802#ifdef FEAT_EVAL
2803 /*
2804 * Skip the command when it's not going to be executed.
2805 * The commands like :if, :endif, etc. always need to be executed.
2806 * Also make an exception for commands that handle a trailing command
2807 * themselves.
2808 */
2809 if (ea.skip)
2810 {
2811 switch (ea.cmdidx)
2812 {
2813 /* commands that need evaluation */
2814 case CMD_while:
2815 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002816 case CMD_for:
2817 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 case CMD_if:
2819 case CMD_elseif:
2820 case CMD_else:
2821 case CMD_endif:
2822 case CMD_try:
2823 case CMD_catch:
2824 case CMD_finally:
2825 case CMD_endtry:
2826 case CMD_function:
2827 break;
2828
2829 /* Commands that handle '|' themselves. Check: A command should
2830 * either have the TRLBAR flag, appear in this list or appear in
2831 * the list at ":help :bar". */
2832 case CMD_aboveleft:
2833 case CMD_and:
2834 case CMD_belowright:
2835 case CMD_botright:
2836 case CMD_browse:
2837 case CMD_call:
2838 case CMD_confirm:
2839 case CMD_delfunction:
2840 case CMD_djump:
2841 case CMD_dlist:
2842 case CMD_dsearch:
2843 case CMD_dsplit:
2844 case CMD_echo:
2845 case CMD_echoerr:
2846 case CMD_echomsg:
2847 case CMD_echon:
2848 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002849 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 case CMD_help:
2851 case CMD_hide:
2852 case CMD_ijump:
2853 case CMD_ilist:
2854 case CMD_isearch:
2855 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002856 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 case CMD_keepjumps:
2858 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002859 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 case CMD_leftabove:
2861 case CMD_let:
2862 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002863 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002865 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002866 case CMD_noautocmd:
2867 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 case CMD_perl:
2869 case CMD_psearch:
2870 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002871 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002872 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 case CMD_return:
2874 case CMD_rightbelow:
2875 case CMD_ruby:
2876 case CMD_silent:
2877 case CMD_smagic:
2878 case CMD_snomagic:
2879 case CMD_substitute:
2880 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002881 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 case CMD_tcl:
2883 case CMD_throw:
2884 case CMD_tilde:
2885 case CMD_topleft:
2886 case CMD_unlet:
2887 case CMD_verbose:
2888 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002889 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 break;
2891
2892 default: goto doend;
2893 }
2894 }
2895#endif
2896
2897 if (ea.argt & XFILE)
2898 {
2899 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2900 goto doend;
2901 }
2902
2903#ifdef FEAT_LISTCMDS
2904 /*
2905 * Accept buffer name. Cannot be used at the same time with a buffer
2906 * number. Don't do this for a user command.
2907 */
2908 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002909 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 {
2911 /*
2912 * :bdelete, :bwipeout and :bunload take several arguments, separated
2913 * by spaces: find next space (skipping over escaped characters).
2914 * The others take one argument: ignore trailing spaces.
2915 */
2916 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2917 || ea.cmdidx == CMD_bunload)
2918 p = skiptowhite_esc(ea.arg);
2919 else
2920 {
2921 p = ea.arg + STRLEN(ea.arg);
2922 while (p > ea.arg && vim_iswhite(p[-1]))
2923 --p;
2924 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002925 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2926 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 if (ea.line2 < 0) /* failed */
2928 goto doend;
2929 ea.addr_count = 1;
2930 ea.arg = skipwhite(p);
2931 }
2932#endif
2933
2934/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002935 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 *
2937 * The "ea" structure holds the arguments that can be used.
2938 */
2939 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002940 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 ea.cookie = cookie;
2942#ifdef FEAT_EVAL
2943 ea.cstack = cstack;
2944#endif
2945
2946#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002947 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 {
2949 /*
2950 * Execute a user-defined command.
2951 */
2952 do_ucmd(&ea);
2953 }
2954 else
2955#endif
2956 {
2957 /*
2958 * Call the function to execute the command.
2959 */
2960 ea.errmsg = NULL;
2961 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2962 if (ea.errmsg != NULL)
2963 errormsg = (char_u *)_(ea.errmsg);
2964 }
2965
2966#ifdef FEAT_EVAL
2967 /*
2968 * If the command just executed called do_cmdline(), any throw or ":return"
2969 * or ":finish" encountered there must also check the cstack of the still
2970 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2971 * exception, or reanimate a returned function or finished script file and
2972 * return or finish it again.
2973 */
2974 if (need_rethrow)
2975 do_throw(cstack);
2976 else if (check_cstack)
2977 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002978 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002980 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 && current_func_returned())
2982 do_return(&ea, TRUE, FALSE, NULL);
2983 }
2984 need_rethrow = check_cstack = FALSE;
2985#endif
2986
2987doend:
2988 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2989 curwin->w_cursor.lnum = 1;
2990
2991 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2992 {
2993 if (sourcing)
2994 {
2995 if (errormsg != IObuff)
2996 {
2997 STRCPY(IObuff, errormsg);
2998 errormsg = IObuff;
2999 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003000 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 }
3002 emsg(errormsg);
3003 }
3004#ifdef FEAT_EVAL
3005 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02003006 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
3007 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008#endif
3009
3010 if (verbose_save >= 0)
3011 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00003012#ifdef FEAT_AUTOCMD
3013 if (cmdmod.save_ei != NULL)
3014 {
3015 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003016 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
3017 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00003018 free_string_option(cmdmod.save_ei);
3019 }
3020#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003021 if (cmdmod.filter_regmatch.regprog != NULL)
3022 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023
3024 cmdmod = save_cmdmod;
3025
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003026 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 {
3028 /* messages could be enabled for a serious error, need to check if the
3029 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00003030 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003031 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 emsg_silent -= did_esilent;
3033 if (emsg_silent < 0)
3034 emsg_silent = 0;
3035 /* Restore msg_scroll, it's set by file I/O commands, even when no
3036 * message is actually displayed. */
3037 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003038
3039 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
3040 * somewhere in the line. Put it back in the first column. */
3041 if (redirecting())
3042 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 }
3044
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003045#ifdef HAVE_SANDBOX
3046 if (did_sandbox)
3047 --sandbox;
3048#endif
3049
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3051 ea.nextcmd = NULL;
3052
3053#ifdef FEAT_EVAL
3054 --ex_nesting_level;
3055#endif
3056
3057 return ea.nextcmd;
3058}
3059#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003060 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061#endif
3062
3063/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003064 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 * If there is a match advance "pp" to the argument and return TRUE.
3066 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003067 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003068checkforcmd(
3069 char_u **pp, /* start of command */
3070 char *cmd, /* name of command */
3071 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072{
3073 int i;
3074
3075 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003076 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 break;
3078 if (i >= len && !isalpha((*pp)[i]))
3079 {
3080 *pp = skipwhite(*pp + i);
3081 return TRUE;
3082 }
3083 return FALSE;
3084}
3085
3086/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003087 * Append "cmd" to the error message in IObuff.
3088 * Takes care of limiting the length and handling 0xa0, which would be
3089 * invisible otherwise.
3090 */
3091 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003092append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003093{
3094 char_u *s = cmd;
3095 char_u *d;
3096
3097 STRCAT(IObuff, ": ");
3098 d = IObuff + STRLEN(IObuff);
3099 while (*s != NUL && d - IObuff < IOSIZE - 7)
3100 {
3101 if (
3102#ifdef FEAT_MBYTE
3103 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3104#endif
3105 *s == 0xa0)
3106 {
3107 s +=
3108#ifdef FEAT_MBYTE
3109 enc_utf8 ? 2 :
3110#endif
3111 1;
3112 STRCPY(d, "<a0>");
3113 d += 4;
3114 }
3115 else
3116 MB_COPY_CHAR(s, d);
3117 }
3118 *d = NUL;
3119}
3120
3121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003123 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003125 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 * Returns NULL for an ambiguous user command.
3127 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003129find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130{
3131 int len;
3132 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003133 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134
3135 /*
3136 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003137 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 * - the 'k' command can directly be followed by any character.
3139 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003140 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003142 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 */
3144 p = eap->cmd;
3145 if (*p == 'k')
3146 {
3147 eap->cmdidx = CMD_k;
3148 ++p;
3149 }
3150 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003151 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3152 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 || p[1] == 'g'
3154 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3155 || p[1] == 'I'
3156 || (p[1] == 'r' && p[2] != 'e')))
3157 {
3158 eap->cmdidx = CMD_substitute;
3159 ++p;
3160 }
3161 else
3162 {
3163 while (ASCII_ISALPHA(*p))
3164 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003165 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003166 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003167 while (ASCII_ISALNUM(*p))
3168 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003169
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 /* check for non-alpha command */
3171 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3172 ++p;
3173 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003174 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3175 {
3176 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3177 * :delete with the 'l' flag. Same for 'p'. */
3178 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003179 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003180 break;
3181 if (i == len - 1)
3182 {
3183 --len;
3184 if (p[-1] == 'l')
3185 eap->flags |= EXFLAG_LIST;
3186 else
3187 eap->flags |= EXFLAG_PRINT;
3188 }
3189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190
3191 if (ASCII_ISLOWER(*eap->cmd))
3192 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
3193 else
3194 eap->cmdidx = cmdidxs[26];
3195
3196 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3197 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3198 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3199 (size_t)len) == 0)
3200 {
3201#ifdef FEAT_EVAL
3202 if (full != NULL
3203 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3204 *full = TRUE;
3205#endif
3206 break;
3207 }
3208
3209#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003210 /* Look for a user defined command as a last resort. Let ":Print" be
3211 * overruled by a user defined command. */
3212 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3213 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003215 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 while (ASCII_ISALNUM(*p))
3217 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003218 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 }
3220#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003221 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 eap->cmdidx = CMD_SIZE;
3223 }
3224
3225 return p;
3226}
3227
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003228#ifdef FEAT_USR_CMDS
3229/*
3230 * Search for a user command that matches "eap->cmd".
3231 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3232 * Return a pointer to just after the command.
3233 * Return NULL if there is no matching command.
3234 */
3235 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003236find_ucmd(
3237 exarg_T *eap,
3238 char_u *p, /* end of the command (possibly including count) */
3239 int *full, /* set to TRUE for a full match */
3240 expand_T *xp, /* used for completion, NULL otherwise */
3241 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003242{
3243 int len = (int)(p - eap->cmd);
3244 int j, k, matchlen = 0;
3245 ucmd_T *uc;
3246 int found = FALSE;
3247 int possible = FALSE;
3248 char_u *cp, *np; /* Point into typed cmd and test name */
3249 garray_T *gap;
3250 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3251 only full match global is accepted. */
3252
3253 /*
3254 * Look for buffer-local user commands first, then global ones.
3255 */
3256 gap = &curbuf->b_ucmds;
3257 for (;;)
3258 {
3259 for (j = 0; j < gap->ga_len; ++j)
3260 {
3261 uc = USER_CMD_GA(gap, j);
3262 cp = eap->cmd;
3263 np = uc->uc_name;
3264 k = 0;
3265 while (k < len && *np != NUL && *cp++ == *np++)
3266 k++;
3267 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3268 {
3269 /* If finding a second match, the command is ambiguous. But
3270 * not if a buffer-local command wasn't a full match and a
3271 * global command is a full match. */
3272 if (k == len && found && *np != NUL)
3273 {
3274 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003275 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003276 amb_local = TRUE;
3277 }
3278
3279 if (!found || (k == len && *np == NUL))
3280 {
3281 /* If we matched up to a digit, then there could
3282 * be another command including the digit that we
3283 * should use instead.
3284 */
3285 if (k == len)
3286 found = TRUE;
3287 else
3288 possible = TRUE;
3289
3290 if (gap == &ucmds)
3291 eap->cmdidx = CMD_USER;
3292 else
3293 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003294 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003295 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003296 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003297
3298# ifdef FEAT_CMDL_COMPL
3299 if (compl != NULL)
3300 *compl = uc->uc_compl;
3301# ifdef FEAT_EVAL
3302 if (xp != NULL)
3303 {
3304 xp->xp_arg = uc->uc_compl_arg;
3305 xp->xp_scriptID = uc->uc_scriptID;
3306 }
3307# endif
3308# endif
3309 /* Do not search for further abbreviations
3310 * if this is an exact match. */
3311 matchlen = k;
3312 if (k == len && *np == NUL)
3313 {
3314 if (full != NULL)
3315 *full = TRUE;
3316 amb_local = FALSE;
3317 break;
3318 }
3319 }
3320 }
3321 }
3322
3323 /* Stop if we found a full match or searched all. */
3324 if (j < gap->ga_len || gap == &ucmds)
3325 break;
3326 gap = &ucmds;
3327 }
3328
3329 /* Only found ambiguous matches. */
3330 if (amb_local)
3331 {
3332 if (xp != NULL)
3333 xp->xp_context = EXPAND_UNSUCCESSFUL;
3334 return NULL;
3335 }
3336
3337 /* The match we found may be followed immediately by a number. Move "p"
3338 * back to point to it. */
3339 if (found || possible)
3340 return p + (matchlen - len);
3341 return p;
3342}
3343#endif
3344
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003346static struct cmdmod
3347{
3348 char *name;
3349 int minlen;
3350 int has_count; /* :123verbose :3tab */
3351} cmdmods[] = {
3352 {"aboveleft", 3, FALSE},
3353 {"belowright", 3, FALSE},
3354 {"botright", 2, FALSE},
3355 {"browse", 3, FALSE},
3356 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003357 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003358 {"hide", 3, FALSE},
3359 {"keepalt", 5, FALSE},
3360 {"keepjumps", 5, FALSE},
3361 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003362 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003363 {"leftabove", 5, FALSE},
3364 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003365 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003366 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003367 {"rightbelow", 6, FALSE},
3368 {"sandbox", 3, FALSE},
3369 {"silent", 3, FALSE},
3370 {"tab", 3, TRUE},
3371 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003372 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003373 {"verbose", 4, TRUE},
3374 {"vertical", 4, FALSE},
3375};
3376
3377/*
3378 * Return length of a command modifier (including optional count).
3379 * Return zero when it's not a modifier.
3380 */
3381 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003382modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003383{
3384 int i, j;
3385 char_u *p = cmd;
3386
3387 if (VIM_ISDIGIT(*cmd))
3388 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003389 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003390 {
3391 for (j = 0; p[j] != NUL; ++j)
3392 if (p[j] != cmdmods[i].name[j])
3393 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003394 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003395 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003396 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003397 }
3398 return 0;
3399}
3400
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401/*
3402 * Return > 0 if an Ex command "name" exists.
3403 * Return 2 if there is an exact match.
3404 * Return 3 if there is an ambiguous match.
3405 */
3406 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003407cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408{
3409 exarg_T ea;
3410 int full = FALSE;
3411 int i;
3412 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003413 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414
3415 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003416 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 {
3418 for (j = 0; name[j] != NUL; ++j)
3419 if (name[j] != cmdmods[i].name[j])
3420 break;
3421 if (name[j] == NUL && j >= cmdmods[i].minlen)
3422 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3423 }
3424
Bram Moolenaara9587612006-05-04 21:47:50 +00003425 /* Check built-in commands and user defined commands.
3426 * For ":2match" and ":3match" we need to skip the number. */
3427 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003429 p = find_command(&ea, &full);
3430 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003432 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3433 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003434 if (*skipwhite(p) != NUL)
3435 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3437}
3438#endif
3439
3440/*
3441 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3442 * we don't need/want deleted. Maybe this could be done better if we didn't
3443 * repeat all this stuff. The only problem is that they may not stay
3444 * perfectly compatible with each other, but then the command line syntax
3445 * probably won't change that much -- webb.
3446 */
3447 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003448set_one_cmd_context(
3449 expand_T *xp,
3450 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451{
3452 char_u *p;
3453 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003454 int len = 0;
3455 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3457 int compl = EXPAND_NOTHING;
3458#endif
3459#ifdef FEAT_CMDL_COMPL
3460 int delim;
3461#endif
3462 int forceit = FALSE;
3463 int usefilter = FALSE; /* filter instead of file name */
3464
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003465 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 xp->xp_pattern = buff;
3467 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003468 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469
3470/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003471 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 */
3473 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3474 ;
3475 xp->xp_pattern = cmd;
3476
3477 if (*cmd == NUL)
3478 return NULL;
3479 if (*cmd == '"') /* ignore comment lines */
3480 {
3481 xp->xp_context = EXPAND_NOTHING;
3482 return NULL;
3483 }
3484
3485/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003486 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 */
3488 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 xp->xp_pattern = cmd;
3490 if (*cmd == NUL)
3491 return NULL;
3492 if (*cmd == '"')
3493 {
3494 xp->xp_context = EXPAND_NOTHING;
3495 return NULL;
3496 }
3497
3498 if (*cmd == '|' || *cmd == '\n')
3499 return cmd + 1; /* There's another command */
3500
3501 /*
3502 * Isolate the command and search for it in the command table.
3503 * Exceptions:
3504 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003505 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3507 */
3508 if (*cmd == 'k' && cmd[1] != 'e')
3509 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003510 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 p = cmd + 1;
3512 }
3513 else
3514 {
3515 p = cmd;
3516 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3517 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003518 /* a user command may contain digits */
3519 if (ASCII_ISUPPER(cmd[0]))
3520 while (ASCII_ISALNUM(*p) || *p == '*')
3521 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003522 /* for python 3.x: ":py3*" commands completion */
3523 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003524 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003525 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003526 while (ASCII_ISALPHA(*p) || *p == '*')
3527 ++p;
3528 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003529 /* check for non-alpha command */
3530 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3531 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003532 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003534 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 {
3536 xp->xp_context = EXPAND_UNSUCCESSFUL;
3537 return NULL;
3538 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003539 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003540 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3541 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3542 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 break;
3544
3545#ifdef FEAT_USR_CMDS
3546 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3548 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549#endif
3550 }
3551
3552 /*
3553 * If the cursor is touching the command, and it ends in an alpha-numeric
3554 * character, complete the command name.
3555 */
3556 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3557 return NULL;
3558
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003559 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 {
3561 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3562 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003563 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 p = cmd + 1;
3565 }
3566#ifdef FEAT_USR_CMDS
3567 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3568 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003569 ea.cmd = cmd;
3570 p = find_ucmd(&ea, p, NULL, xp,
3571# if defined(FEAT_CMDL_COMPL)
3572 &compl
3573# else
3574 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003576 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003577 if (p == NULL)
3578 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 }
3580#endif
3581 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003582 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 {
3584 /* Not still touching the command and it was an illegal one */
3585 xp->xp_context = EXPAND_UNSUCCESSFUL;
3586 return NULL;
3587 }
3588
3589 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3590
3591 if (*p == '!') /* forced commands */
3592 {
3593 forceit = TRUE;
3594 ++p;
3595 }
3596
3597/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003598 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003600 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003601 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602
3603 arg = skipwhite(p);
3604
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003605 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 {
3607 if (*arg == '>') /* append */
3608 {
3609 if (*++arg == '>')
3610 ++arg;
3611 arg = skipwhite(arg);
3612 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003613 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 {
3615 ++arg;
3616 usefilter = TRUE;
3617 }
3618 }
3619
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003620 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 {
3622 usefilter = forceit; /* :r! filter if forced */
3623 if (*arg == '!') /* :r !filter */
3624 {
3625 ++arg;
3626 usefilter = TRUE;
3627 }
3628 }
3629
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003630 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 {
3632 while (*arg == *cmd) /* allow any number of '>' or '<' */
3633 ++arg;
3634 arg = skipwhite(arg);
3635 }
3636
3637 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003638 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 {
3640 /* Check if we're in the +command */
3641 p = arg + 1;
3642 arg = skip_cmd_arg(arg, FALSE);
3643
3644 /* Still touching the command after '+'? */
3645 if (*arg == NUL)
3646 return p;
3647
3648 /* Skip space(s) after +command to get to the real argument */
3649 arg = skipwhite(arg);
3650 }
3651
3652 /*
3653 * Check for '|' to separate commands and '"' to start comments.
3654 * Don't do this for ":read !cmd" and ":write !cmd".
3655 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003656 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
3658 p = arg;
3659 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003660 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 p += 2;
3662 while (*p)
3663 {
3664 if (*p == Ctrl_V)
3665 {
3666 if (p[1] != NUL)
3667 ++p;
3668 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003669 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 || *p == '|' || *p == '\n')
3671 {
3672 if (*(p - 1) != '\\')
3673 {
3674 if (*p == '|' || *p == '\n')
3675 return p + 1;
3676 return NULL; /* It's a comment */
3677 }
3678 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003679 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 }
3681 }
3682
3683 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003684 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 vim_strchr((char_u *)"|\"", *arg) == NULL)
3686 return NULL;
3687
3688 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003689 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003691 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003692 while (*p && p < buff + len)
3693 {
3694 if (*p == ' ' || *p == TAB)
3695 {
3696 /* argument starts after a space */
3697 xp->xp_pattern = ++p;
3698 }
3699 else
3700 {
3701 if (*p == '\\' && *(p + 1) != NUL)
3702 ++p; /* skip over escaped character */
3703 mb_ptr_adv(p);
3704 }
3705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003707 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003709 int c;
3710 int in_quote = FALSE;
3711 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712
3713 /*
3714 * Allow spaces within back-quotes to count as part of the argument
3715 * being expanded.
3716 */
3717 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003718 p = xp->xp_pattern;
3719 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003721#ifdef FEAT_MBYTE
3722 if (has_mbyte)
3723 c = mb_ptr2char(p);
3724 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003726 c = *p;
3727 if (c == '\\' && p[1] != NUL)
3728 ++p;
3729 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 {
3731 if (!in_quote)
3732 {
3733 xp->xp_pattern = p;
3734 bow = p + 1;
3735 }
3736 in_quote = !in_quote;
3737 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003738 /* An argument can contain just about everything, except
3739 * characters that end the command and white space. */
3740 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003741#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003742 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003743#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003744 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003745 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003746 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003747 while (*p != NUL)
3748 {
3749#ifdef FEAT_MBYTE
3750 if (has_mbyte)
3751 c = mb_ptr2char(p);
3752 else
3753#endif
3754 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003755 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003756 break;
3757#ifdef FEAT_MBYTE
3758 if (has_mbyte)
3759 len = (*mb_ptr2len)(p);
3760 else
3761#endif
3762 len = 1;
3763 mb_ptr_adv(p);
3764 }
3765 if (in_quote)
3766 bow = p;
3767 else
3768 xp->xp_pattern = p;
3769 p -= len;
3770 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003771 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 }
3773
3774 /*
3775 * If we are still inside the quotes, and we passed a space, just
3776 * expand from there.
3777 */
3778 if (bow != NULL && in_quote)
3779 xp->xp_pattern = bow;
3780 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003781
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003782 /* For a shell command more chars need to be escaped. */
3783 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003784 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003785#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003786 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003787#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003788 /* When still after the command name expand executables. */
3789 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003790 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792
3793 /* Check for environment variable */
3794 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003795#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 || *xp->xp_pattern == '%'
3797#endif
3798 )
3799 {
3800 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3801 if (!vim_isIDc(*p))
3802 break;
3803 if (*p == NUL)
3804 {
3805 xp->xp_context = EXPAND_ENV_VARS;
3806 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003807#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3808 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003809 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003810 compl = EXPAND_ENV_VARS;
3811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 }
3813 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003814#if defined(FEAT_CMDL_COMPL)
3815 /* Check for user names */
3816 if (*xp->xp_pattern == '~')
3817 {
3818 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3819 ;
3820 /* Complete ~user only if it partially matches a user name.
3821 * A full match ~user<Tab> will be replaced by user's home
3822 * directory i.e. something like ~user<Tab> -> /home/user/ */
3823 if (*p == NUL && p > xp->xp_pattern + 1
3824 && match_user(xp->xp_pattern + 1) == 1)
3825 {
3826 xp->xp_context = EXPAND_USER;
3827 ++xp->xp_pattern;
3828 }
3829 }
3830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 }
3832
3833/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003834 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003836 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003838 case CMD_find:
3839 case CMD_sfind:
3840 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003841 if (xp->xp_context == EXPAND_FILES)
3842 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003843 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 case CMD_cd:
3845 case CMD_chdir:
3846 case CMD_lcd:
3847 case CMD_lchdir:
3848 if (xp->xp_context == EXPAND_FILES)
3849 xp->xp_context = EXPAND_DIRECTORIES;
3850 break;
3851 case CMD_help:
3852 xp->xp_context = EXPAND_HELP;
3853 xp->xp_pattern = arg;
3854 break;
3855
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003856 /* Command modifiers: return the argument.
3857 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003859 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 case CMD_belowright:
3861 case CMD_botright:
3862 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003863 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003864 case CMD_cdo:
3865 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003867 case CMD_debug:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003868 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 case CMD_folddoclosed:
3870 case CMD_folddoopen:
3871 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003872 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 case CMD_keepjumps:
3874 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003875 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003876 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003878 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003880 case CMD_noautocmd:
3881 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003883 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003885 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003886 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 case CMD_topleft:
3888 case CMD_verbose:
3889 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003890 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 return arg;
3892
Bram Moolenaar4f688582007-07-24 12:34:30 +00003893#ifdef FEAT_CMDL_COMPL
3894# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 case CMD_match:
3896 if (*arg == NUL || !ends_excmd(*arg))
3897 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003898 /* also complete "None" */
3899 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 arg = skipwhite(skiptowhite(arg));
3901 if (*arg != NUL)
3902 {
3903 xp->xp_context = EXPAND_NOTHING;
3904 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3905 }
3906 }
3907 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003908# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910/*
3911 * All completion for the +cmdline_compl feature goes here.
3912 */
3913
3914# ifdef FEAT_USR_CMDS
3915 case CMD_command:
3916 /* Check for attributes */
3917 while (*arg == '-')
3918 {
3919 arg++; /* Skip "-" */
3920 p = skiptowhite(arg);
3921 if (*p == NUL)
3922 {
3923 /* Cursor is still in the attribute */
3924 p = vim_strchr(arg, '=');
3925 if (p == NULL)
3926 {
3927 /* No "=", so complete attribute names */
3928 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3929 xp->xp_pattern = arg;
3930 return NULL;
3931 }
3932
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003933 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 * their arguments as well.
3935 */
3936 if (STRNICMP(arg, "complete", p - arg) == 0)
3937 {
3938 xp->xp_context = EXPAND_USER_COMPLETE;
3939 xp->xp_pattern = p + 1;
3940 return NULL;
3941 }
3942 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3943 {
3944 xp->xp_context = EXPAND_USER_NARGS;
3945 xp->xp_pattern = p + 1;
3946 return NULL;
3947 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003948 else if (STRNICMP(arg, "addr", p - arg) == 0)
3949 {
3950 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3951 xp->xp_pattern = p + 1;
3952 return NULL;
3953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 return NULL;
3955 }
3956 arg = skipwhite(p);
3957 }
3958
3959 /* After the attributes comes the new command name */
3960 p = skiptowhite(arg);
3961 if (*p == NUL)
3962 {
3963 xp->xp_context = EXPAND_USER_COMMANDS;
3964 xp->xp_pattern = arg;
3965 break;
3966 }
3967
3968 /* And finally comes a normal command */
3969 return skipwhite(p);
3970
3971 case CMD_delcommand:
3972 xp->xp_context = EXPAND_USER_COMMANDS;
3973 xp->xp_pattern = arg;
3974 break;
3975# endif
3976
3977 case CMD_global:
3978 case CMD_vglobal:
3979 delim = *arg; /* get the delimiter */
3980 if (delim)
3981 ++arg; /* skip delimiter if there is one */
3982
3983 while (arg[0] != NUL && arg[0] != delim)
3984 {
3985 if (arg[0] == '\\' && arg[1] != NUL)
3986 ++arg;
3987 ++arg;
3988 }
3989 if (arg[0] != NUL)
3990 return arg + 1;
3991 break;
3992 case CMD_and:
3993 case CMD_substitute:
3994 delim = *arg;
3995 if (delim)
3996 {
3997 /* skip "from" part */
3998 ++arg;
3999 arg = skip_regexp(arg, delim, p_magic, NULL);
4000 }
4001 /* skip "to" part */
4002 while (arg[0] != NUL && arg[0] != delim)
4003 {
4004 if (arg[0] == '\\' && arg[1] != NUL)
4005 ++arg;
4006 ++arg;
4007 }
4008 if (arg[0] != NUL) /* skip delimiter */
4009 ++arg;
4010 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
4011 ++arg;
4012 if (arg[0] != NUL)
4013 return arg;
4014 break;
4015 case CMD_isearch:
4016 case CMD_dsearch:
4017 case CMD_ilist:
4018 case CMD_dlist:
4019 case CMD_ijump:
4020 case CMD_psearch:
4021 case CMD_djump:
4022 case CMD_isplit:
4023 case CMD_dsplit:
4024 arg = skipwhite(skipdigits(arg)); /* skip count */
4025 if (*arg == '/') /* Match regexp, not just whole words */
4026 {
4027 for (++arg; *arg && *arg != '/'; arg++)
4028 if (*arg == '\\' && arg[1] != NUL)
4029 arg++;
4030 if (*arg)
4031 {
4032 arg = skipwhite(arg + 1);
4033
4034 /* Check for trailing illegal characters */
4035 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4036 xp->xp_context = EXPAND_NOTHING;
4037 else
4038 return arg;
4039 }
4040 }
4041 break;
4042#ifdef FEAT_AUTOCMD
4043 case CMD_autocmd:
4044 return set_context_in_autocmd(xp, arg, FALSE);
4045
4046 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004047 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 return set_context_in_autocmd(xp, arg, TRUE);
4049#endif
4050 case CMD_set:
4051 set_context_in_set_cmd(xp, arg, 0);
4052 break;
4053 case CMD_setglobal:
4054 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4055 break;
4056 case CMD_setlocal:
4057 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4058 break;
4059 case CMD_tag:
4060 case CMD_stag:
4061 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004062 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 case CMD_tselect:
4064 case CMD_stselect:
4065 case CMD_ptselect:
4066 case CMD_tjump:
4067 case CMD_stjump:
4068 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004069 if (*p_wop != NUL)
4070 xp->xp_context = EXPAND_TAGS_LISTFILES;
4071 else
4072 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 xp->xp_pattern = arg;
4074 break;
4075 case CMD_augroup:
4076 xp->xp_context = EXPAND_AUGROUP;
4077 xp->xp_pattern = arg;
4078 break;
4079#ifdef FEAT_SYN_HL
4080 case CMD_syntax:
4081 set_context_in_syntax_cmd(xp, arg);
4082 break;
4083#endif
4084#ifdef FEAT_EVAL
4085 case CMD_let:
4086 case CMD_if:
4087 case CMD_elseif:
4088 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004089 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 case CMD_echo:
4091 case CMD_echon:
4092 case CMD_execute:
4093 case CMD_echomsg:
4094 case CMD_echoerr:
4095 case CMD_call:
4096 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004097 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 break;
4099
4100 case CMD_unlet:
4101 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4102 arg = xp->xp_pattern + 1;
4103 xp->xp_context = EXPAND_USER_VARS;
4104 xp->xp_pattern = arg;
4105 break;
4106
4107 case CMD_function:
4108 case CMD_delfunction:
4109 xp->xp_context = EXPAND_USER_FUNC;
4110 xp->xp_pattern = arg;
4111 break;
4112
4113 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004114 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 break;
4116#endif
4117 case CMD_highlight:
4118 set_context_in_highlight_cmd(xp, arg);
4119 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004120#ifdef FEAT_CSCOPE
4121 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004122 case CMD_lcscope:
4123 case CMD_scscope:
4124 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004125 break;
4126#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004127#ifdef FEAT_SIGNS
4128 case CMD_sign:
4129 set_context_in_sign_cmd(xp, arg);
4130 break;
4131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132#ifdef FEAT_LISTCMDS
4133 case CMD_bdelete:
4134 case CMD_bwipeout:
4135 case CMD_bunload:
4136 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4137 arg = xp->xp_pattern + 1;
4138 /*FALLTHROUGH*/
4139 case CMD_buffer:
4140 case CMD_sbuffer:
4141 case CMD_checktime:
4142 xp->xp_context = EXPAND_BUFFERS;
4143 xp->xp_pattern = arg;
4144 break;
4145#endif
4146#ifdef FEAT_USR_CMDS
4147 case CMD_USER:
4148 case CMD_USER_BUF:
4149 if (compl != EXPAND_NOTHING)
4150 {
4151 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004152 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 {
4154# ifdef FEAT_MENU
4155 if (compl == EXPAND_MENUS)
4156 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4157# endif
4158 if (compl == EXPAND_COMMANDS)
4159 return arg;
4160 if (compl == EXPAND_MAPPINGS)
4161 return set_context_in_map_cmd(xp, (char_u *)"map",
4162 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004163 /* Find start of last argument. */
4164 p = arg;
4165 while (*p)
4166 {
4167 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004168 /* argument starts after a space */
4169 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004170 else if (*p == '\\' && *(p + 1) != NUL)
4171 ++p; /* skip over escaped character */
4172 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 xp->xp_pattern = arg;
4175 }
4176 xp->xp_context = compl;
4177 }
4178 break;
4179#endif
4180 case CMD_map: case CMD_noremap:
4181 case CMD_nmap: case CMD_nnoremap:
4182 case CMD_vmap: case CMD_vnoremap:
4183 case CMD_omap: case CMD_onoremap:
4184 case CMD_imap: case CMD_inoremap:
4185 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004186 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004187 case CMD_smap: case CMD_snoremap:
4188 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004190 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 case CMD_unmap:
4192 case CMD_nunmap:
4193 case CMD_vunmap:
4194 case CMD_ounmap:
4195 case CMD_iunmap:
4196 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004197 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004198 case CMD_sunmap:
4199 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004201 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 case CMD_abbreviate: case CMD_noreabbrev:
4203 case CMD_cabbrev: case CMD_cnoreabbrev:
4204 case CMD_iabbrev: case CMD_inoreabbrev:
4205 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004206 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 case CMD_unabbreviate:
4208 case CMD_cunabbrev:
4209 case CMD_iunabbrev:
4210 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004211 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212#ifdef FEAT_MENU
4213 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4214 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4215 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4216 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4217 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4218 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4219 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4220 case CMD_tmenu: case CMD_tunmenu:
4221 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4222 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4223#endif
4224
4225 case CMD_colorscheme:
4226 xp->xp_context = EXPAND_COLORS;
4227 xp->xp_pattern = arg;
4228 break;
4229
4230 case CMD_compiler:
4231 xp->xp_context = EXPAND_COMPILER;
4232 xp->xp_pattern = arg;
4233 break;
4234
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004235 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004236 xp->xp_context = EXPAND_OWNSYNTAX;
4237 xp->xp_pattern = arg;
4238 break;
4239
4240 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004241 xp->xp_context = EXPAND_FILETYPE;
4242 xp->xp_pattern = arg;
4243 break;
4244
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004245 case CMD_packadd:
4246 xp->xp_context = EXPAND_PACKADD;
4247 xp->xp_pattern = arg;
4248 break;
4249
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4251 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4252 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004253 p = skiptowhite(arg);
4254 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 {
4256 xp->xp_context = EXPAND_LANGUAGE;
4257 xp->xp_pattern = arg;
4258 }
4259 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004260 {
4261 if ( STRNCMP(arg, "messages", p - arg) == 0
4262 || STRNCMP(arg, "ctype", p - arg) == 0
4263 || STRNCMP(arg, "time", p - arg) == 0)
4264 {
4265 xp->xp_context = EXPAND_LOCALES;
4266 xp->xp_pattern = skipwhite(p);
4267 }
4268 else
4269 xp->xp_context = EXPAND_NOTHING;
4270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 break;
4272#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004273#if defined(FEAT_PROFILE)
4274 case CMD_profile:
4275 set_context_in_profile_cmd(xp, arg);
4276 break;
4277#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004278 case CMD_behave:
4279 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004280 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004281 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004283 case CMD_messages:
4284 xp->xp_context = EXPAND_MESSAGES;
4285 xp->xp_pattern = arg;
4286 break;
4287
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004288#if defined(FEAT_CMDHIST)
4289 case CMD_history:
4290 xp->xp_context = EXPAND_HISTORY;
4291 xp->xp_pattern = arg;
4292 break;
4293#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004294#if defined(FEAT_PROFILE)
4295 case CMD_syntime:
4296 xp->xp_context = EXPAND_SYNTIME;
4297 xp->xp_pattern = arg;
4298 break;
4299#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004300
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301#endif /* FEAT_CMDL_COMPL */
4302
4303 default:
4304 break;
4305 }
4306 return NULL;
4307}
4308
4309/*
4310 * skip a range specifier of the form: addr [,addr] [;addr] ..
4311 *
4312 * Backslashed delimiters after / or ? will be skipped, and commands will
4313 * not be expanded between /'s and ?'s or after "'".
4314 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004315 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 * Returns the "cmd" pointer advanced to beyond the range.
4317 */
4318 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004319skip_range(
4320 char_u *cmd,
4321 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004323 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324
Bram Moolenaardf177f62005-02-22 08:39:57 +00004325 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 {
4327 if (*cmd == '\'')
4328 {
4329 if (*++cmd == NUL && ctx != NULL)
4330 *ctx = EXPAND_NOTHING;
4331 }
4332 else if (*cmd == '/' || *cmd == '?')
4333 {
4334 delim = *cmd++;
4335 while (*cmd != NUL && *cmd != delim)
4336 if (*cmd++ == '\\' && *cmd != NUL)
4337 ++cmd;
4338 if (*cmd == NUL && ctx != NULL)
4339 *ctx = EXPAND_NOTHING;
4340 }
4341 if (*cmd != NUL)
4342 ++cmd;
4343 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004344
4345 /* Skip ":" and white space. */
4346 while (*cmd == ':')
4347 cmd = skipwhite(cmd + 1);
4348
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 return cmd;
4350}
4351
4352/*
4353 * get a single EX address
4354 *
4355 * Set ptr to the next character after the part that was interpreted.
4356 * Set ptr to NULL when an error is encountered.
4357 *
4358 * Return MAXLNUM when no Ex address was found.
4359 */
4360 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004361get_address(
4362 exarg_T *eap UNUSED,
4363 char_u **ptr,
4364 int addr_type, /* flag: one of ADDR_LINES, ... */
4365 int skip, /* only skip the address, don't use it */
4366 int to_other_file) /* flag: may jump to other file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367{
4368 int c;
4369 int i;
4370 long n;
4371 char_u *cmd;
4372 pos_T pos;
4373 pos_T *fp;
4374 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004375 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376
4377 cmd = skipwhite(*ptr);
4378 lnum = MAXLNUM;
4379 do
4380 {
4381 switch (*cmd)
4382 {
4383 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004384 ++cmd;
4385 switch (addr_type)
4386 {
4387 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 lnum = curwin->w_cursor.lnum;
4389 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004390 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004391 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004392 break;
4393 case ADDR_ARGUMENTS:
4394 lnum = curwin->w_arg_idx + 1;
4395 break;
4396 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004397 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004398 lnum = curbuf->b_fnum;
4399 break;
4400 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004401 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004402 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004403#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004404 case ADDR_QUICKFIX:
4405 lnum = qf_get_cur_valid_idx(eap);
4406 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004407#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004408 }
4409 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410
4411 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004412 ++cmd;
4413 switch (addr_type)
4414 {
4415 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 lnum = curbuf->b_ml.ml_line_count;
4417 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004418 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004419 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004420 break;
4421 case ADDR_ARGUMENTS:
4422 lnum = ARGCOUNT;
4423 break;
4424 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004425 buf = lastbuf;
4426 while (buf->b_ml.ml_mfp == NULL)
4427 {
4428 if (buf->b_prev == NULL)
4429 break;
4430 buf = buf->b_prev;
4431 }
4432 lnum = buf->b_fnum;
4433 break;
4434 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004435 lnum = lastbuf->b_fnum;
4436 break;
4437 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004438 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004439 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004440#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004441 case ADDR_QUICKFIX:
4442 lnum = qf_get_size(eap);
4443 if (lnum == 0)
4444 lnum = 1;
4445 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004446#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004447 }
4448 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449
4450 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004451 if (*++cmd == NUL)
4452 {
4453 cmd = NULL;
4454 goto error;
4455 }
4456 if (addr_type != ADDR_LINES)
4457 {
4458 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004459 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004460 goto error;
4461 }
4462 if (skip)
4463 ++cmd;
4464 else
4465 {
4466 /* Only accept a mark in another file when it is
4467 * used by itself: ":'M". */
4468 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4469 ++cmd;
4470 if (fp == (pos_T *)-1)
4471 /* Jumped to another file. */
4472 lnum = curwin->w_cursor.lnum;
4473 else
4474 {
4475 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 {
4477 cmd = NULL;
4478 goto error;
4479 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004480 lnum = fp->lnum;
4481 }
4482 }
4483 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484
4485 case '/':
4486 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004487 c = *cmd++;
4488 if (addr_type != ADDR_LINES)
4489 {
4490 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004491 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004492 goto error;
4493 }
4494 if (skip) /* skip "/pat/" */
4495 {
4496 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4497 if (*cmd == c)
4498 ++cmd;
4499 }
4500 else
4501 {
4502 pos = curwin->w_cursor; /* save curwin->w_cursor */
4503 /*
4504 * When '/' or '?' follows another address, start
4505 * from there.
4506 */
4507 if (lnum != MAXLNUM)
4508 curwin->w_cursor.lnum = lnum;
4509 /*
4510 * Start a forward search at the end of the line.
4511 * Start a backward search at the start of the line.
4512 * This makes sure we never match in the current
4513 * line, and can match anywhere in the
4514 * next/previous line.
4515 */
4516 if (c == '/')
4517 curwin->w_cursor.col = MAXCOL;
4518 else
4519 curwin->w_cursor.col = 0;
4520 searchcmdlen = 0;
4521 if (!do_search(NULL, c, cmd, 1L,
4522 SEARCH_HIS | SEARCH_MSG, NULL))
4523 {
4524 curwin->w_cursor = pos;
4525 cmd = NULL;
4526 goto error;
4527 }
4528 lnum = curwin->w_cursor.lnum;
4529 curwin->w_cursor = pos;
4530 /* adjust command string pointer */
4531 cmd += searchcmdlen;
4532 }
4533 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534
4535 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004536 ++cmd;
4537 if (addr_type != ADDR_LINES)
4538 {
4539 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004540 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004541 goto error;
4542 }
4543 if (*cmd == '&')
4544 i = RE_SUBST;
4545 else if (*cmd == '?' || *cmd == '/')
4546 i = RE_SEARCH;
4547 else
4548 {
4549 EMSG(_(e_backslash));
4550 cmd = NULL;
4551 goto error;
4552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004554 if (!skip)
4555 {
4556 /*
4557 * When search follows another address, start from
4558 * there.
4559 */
4560 if (lnum != MAXLNUM)
4561 pos.lnum = lnum;
4562 else
4563 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004565 /*
4566 * Start the search just like for the above
4567 * do_search().
4568 */
4569 if (*cmd != '?')
4570 pos.col = MAXCOL;
4571 else
4572 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004573#ifdef FEAT_VIRTUALEDIT
4574 pos.coladd = 0;
4575#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004576 if (searchit(curwin, curbuf, &pos,
4577 *cmd == '?' ? BACKWARD : FORWARD,
4578 (char_u *)"", 1L, SEARCH_MSG,
4579 i, (linenr_T)0, NULL) != FAIL)
4580 lnum = pos.lnum;
4581 else
4582 {
4583 cmd = NULL;
4584 goto error;
4585 }
4586 }
4587 ++cmd;
4588 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589
4590 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004591 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4592 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 }
4594
4595 for (;;)
4596 {
4597 cmd = skipwhite(cmd);
4598 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4599 break;
4600
4601 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004602 {
4603 switch (addr_type)
4604 {
4605 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004606 /* "+1" is same as ".+1" */
4607 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004608 break;
4609 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004610 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004611 break;
4612 case ADDR_ARGUMENTS:
4613 lnum = curwin->w_arg_idx + 1;
4614 break;
4615 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004616 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004617 lnum = curbuf->b_fnum;
4618 break;
4619 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004620 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004621 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004622#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004623 case ADDR_QUICKFIX:
4624 lnum = qf_get_cur_valid_idx(eap);
4625 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004626#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004627 }
4628 }
4629
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 if (VIM_ISDIGIT(*cmd))
4631 i = '+'; /* "number" is same as "+number" */
4632 else
4633 i = *cmd++;
4634 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4635 n = 1;
4636 else
4637 n = getdigits(&cmd);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004638 if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004639 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004640 lnum = compute_buffer_local_count(
4641 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004642 else if (i == '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 lnum -= n;
4644 else
4645 lnum += n;
4646 }
4647 } while (*cmd == '/' || *cmd == '?');
4648
4649error:
4650 *ptr = cmd;
4651 return lnum;
4652}
4653
4654/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004655 * Get flags from an Ex command argument.
4656 */
4657 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004658get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004659{
4660 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4661 {
4662 if (*eap->arg == 'l')
4663 eap->flags |= EXFLAG_LIST;
4664 else if (*eap->arg == 'p')
4665 eap->flags |= EXFLAG_PRINT;
4666 else
4667 eap->flags |= EXFLAG_NR;
4668 eap->arg = skipwhite(eap->arg + 1);
4669 }
4670}
4671
4672/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 * Function called for command which is Not Implemented. NI!
4674 */
4675 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004676ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677{
4678 if (!eap->skip)
4679 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4680}
4681
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004682#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683/*
4684 * Function called for script command which is Not Implemented. NI!
4685 * Skips over ":perl <<EOF" constructs.
4686 */
4687 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004688ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689{
4690 if (!eap->skip)
4691 ex_ni(eap);
4692 else
4693 vim_free(script_get(eap, eap->arg));
4694}
4695#endif
4696
4697/*
4698 * Check range in Ex command for validity.
4699 * Return NULL when valid, error message when invalid.
4700 */
4701 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004702invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004704 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 if ( eap->line1 < 0
4706 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004707 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004709
4710 if (eap->argt & RANGE)
4711 {
4712 switch(eap->addr_type)
4713 {
4714 case ADDR_LINES:
4715 if (!(eap->argt & NOTADR)
4716 && eap->line2 > curbuf->b_ml.ml_line_count
4717#ifdef FEAT_DIFF
4718 + (eap->cmdidx == CMD_diffget)
4719#endif
4720 )
4721 return (char_u *)_(e_invrange);
4722 break;
4723 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004724 /* add 1 if ARGCOUNT is 0 */
4725 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004726 return (char_u *)_(e_invrange);
4727 break;
4728 case ADDR_BUFFERS:
4729 if (eap->line1 < firstbuf->b_fnum
4730 || eap->line2 > lastbuf->b_fnum)
4731 return (char_u *)_(e_invrange);
4732 break;
4733 case ADDR_LOADED_BUFFERS:
4734 buf = firstbuf;
4735 while (buf->b_ml.ml_mfp == NULL)
4736 {
4737 if (buf->b_next == NULL)
4738 return (char_u *)_(e_invrange);
4739 buf = buf->b_next;
4740 }
4741 if (eap->line1 < buf->b_fnum)
4742 return (char_u *)_(e_invrange);
4743 buf = lastbuf;
4744 while (buf->b_ml.ml_mfp == NULL)
4745 {
4746 if (buf->b_prev == NULL)
4747 return (char_u *)_(e_invrange);
4748 buf = buf->b_prev;
4749 }
4750 if (eap->line2 > buf->b_fnum)
4751 return (char_u *)_(e_invrange);
4752 break;
4753 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004754 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004755 return (char_u *)_(e_invrange);
4756 break;
4757 case ADDR_TABS:
4758 if (eap->line2 > LAST_TAB_NR)
4759 return (char_u *)_(e_invrange);
4760 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004761#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004762 case ADDR_QUICKFIX:
4763 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4764 return (char_u *)_(e_invrange);
4765 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004766#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004767 }
4768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 return NULL;
4770}
4771
4772/*
4773 * Correct the range for zero line number, if required.
4774 */
4775 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004776correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777{
4778 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4779 {
4780 if (eap->line1 == 0)
4781 eap->line1 = 1;
4782 if (eap->line2 == 0)
4783 eap->line2 = 1;
4784 }
4785}
4786
Bram Moolenaar748bf032005-02-02 23:04:36 +00004787#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004788static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004789
4790/*
4791 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4792 * pattern. Otherwise return eap->arg.
4793 */
4794 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004795skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004796{
4797 char_u *p = eap->arg;
4798
Bram Moolenaara37420f2006-02-04 22:37:47 +00004799 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4800 || eap->cmdidx == CMD_vimgrepadd
4801 || eap->cmdidx == CMD_lvimgrepadd
4802 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004803 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004804 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004805 if (p == NULL)
4806 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004807 }
4808 return p;
4809}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004810
4811/*
4812 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4813 * in the command line, so that things like % get expanded.
4814 */
4815 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004816replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004817{
4818 char_u *new_cmdline;
4819 char_u *program;
4820 char_u *pos;
4821 char_u *ptr;
4822 int len;
4823 int i;
4824
4825 /*
4826 * Don't do it when ":vimgrep" is used for ":grep".
4827 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004828 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4829 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4830 || eap->cmdidx == CMD_grepadd
4831 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004832 && !grep_internal(eap->cmdidx))
4833 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004834 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4835 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004836 {
4837 if (*curbuf->b_p_gp == NUL)
4838 program = p_gp;
4839 else
4840 program = curbuf->b_p_gp;
4841 }
4842 else
4843 {
4844 if (*curbuf->b_p_mp == NUL)
4845 program = p_mp;
4846 else
4847 program = curbuf->b_p_mp;
4848 }
4849
4850 p = skipwhite(p);
4851
4852 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4853 {
4854 /* replace $* by given arguments */
4855 i = 1;
4856 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4857 ++i;
4858 len = (int)STRLEN(p);
4859 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4860 if (new_cmdline == NULL)
4861 return NULL; /* out of memory */
4862 ptr = new_cmdline;
4863 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4864 {
4865 i = (int)(pos - program);
4866 STRNCPY(ptr, program, i);
4867 STRCPY(ptr += i, p);
4868 ptr += len;
4869 program = pos + 2;
4870 }
4871 STRCPY(ptr, program);
4872 }
4873 else
4874 {
4875 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4876 if (new_cmdline == NULL)
4877 return NULL; /* out of memory */
4878 STRCPY(new_cmdline, program);
4879 STRCAT(new_cmdline, " ");
4880 STRCAT(new_cmdline, p);
4881 }
4882 msg_make(p);
4883
4884 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4885 vim_free(*cmdlinep);
4886 *cmdlinep = new_cmdline;
4887 p = new_cmdline;
4888 }
4889 return p;
4890}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004891#endif
4892
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893/*
4894 * Expand file name in Ex command argument.
4895 * Return FAIL for failure, OK otherwise.
4896 */
4897 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004898expand_filename(
4899 exarg_T *eap,
4900 char_u **cmdlinep,
4901 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902{
4903 int has_wildcards; /* need to expand wildcards */
4904 char_u *repl;
4905 int srclen;
4906 char_u *p;
4907 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004908 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909
Bram Moolenaar748bf032005-02-02 23:04:36 +00004910#ifdef FEAT_QUICKFIX
4911 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4912 p = skip_grep_pat(eap);
4913#else
4914 p = eap->arg;
4915#endif
4916
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 /*
4918 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4919 * the file name contains a wildcard it should not cause expanding.
4920 * (it will be expanded anyway if there is a wildcard before replacing).
4921 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004922 has_wildcards = mch_has_wildcard(p);
4923 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004925#ifdef FEAT_EVAL
4926 /* Skip over `=expr`, wildcards in it are not expanded. */
4927 if (p[0] == '`' && p[1] == '=')
4928 {
4929 p += 2;
4930 (void)skip_expr(&p);
4931 if (*p == '`')
4932 ++p;
4933 continue;
4934 }
4935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 /*
4937 * Quick check if this cannot be the start of a special string.
4938 * Also removes backslash before '%', '#' and '<'.
4939 */
4940 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4941 {
4942 ++p;
4943 continue;
4944 }
4945
4946 /*
4947 * Try to find a match at this position.
4948 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004949 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4950 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 if (*errormsgp != NULL) /* error detected */
4952 return FAIL;
4953 if (repl == NULL) /* no match found */
4954 {
4955 p += srclen;
4956 continue;
4957 }
4958
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004959 /* Wildcards won't be expanded below, the replacement is taken
4960 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4961 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4962 {
4963 char_u *l = repl;
4964
4965 repl = expand_env_save(repl);
4966 vim_free(l);
4967 }
4968
Bram Moolenaar63b92542007-03-27 14:57:09 +00004969 /* Need to escape white space et al. with a backslash.
4970 * Don't do this for:
4971 * - replacement that already has been escaped: "##"
4972 * - shell commands (may have to use quotes instead).
4973 * - non-unix systems when there is a single argument (spaces don't
4974 * separate arguments then).
4975 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004977 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 && eap->cmdidx != CMD_bang
4979 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004980 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004982 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004984 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985#ifndef UNIX
4986 && !(eap->argt & NOSPC)
4987#endif
4988 )
4989 {
4990 char_u *l;
4991#ifdef BACKSLASH_IN_FILENAME
4992 /* Don't escape a backslash here, because rem_backslash() doesn't
4993 * remove it later. */
4994 static char_u *nobslash = (char_u *)" \t\"|";
4995# define ESCAPE_CHARS nobslash
4996#else
4997# define ESCAPE_CHARS escape_chars
4998#endif
4999
5000 for (l = repl; *l; ++l)
5001 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5002 {
5003 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5004 if (l != NULL)
5005 {
5006 vim_free(repl);
5007 repl = l;
5008 }
5009 break;
5010 }
5011 }
5012
5013 /* For a shell command a '!' must be escaped. */
5014 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005015 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 {
5017 char_u *l;
5018
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005019 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 if (l != NULL)
5021 {
5022 vim_free(repl);
5023 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 }
5025 }
5026
5027 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5028 vim_free(repl);
5029 if (p == NULL)
5030 return FAIL;
5031 }
5032
5033 /*
5034 * One file argument: Expand wildcards.
5035 * Don't do this with ":r !command" or ":w !command".
5036 */
5037 if ((eap->argt & NOSPC) && !eap->usefilter)
5038 {
5039 /*
5040 * May do this twice:
5041 * 1. Replace environment variables.
5042 * 2. Replace any other wildcards, remove backslashes.
5043 */
5044 for (n = 1; n <= 2; ++n)
5045 {
5046 if (n == 2)
5047 {
5048#ifdef UNIX
5049 /*
5050 * Only for Unix we check for more than one file name.
5051 * For other systems spaces are considered to be part
5052 * of the file name.
5053 * Only check here if there is no wildcard, otherwise
5054 * ExpandOne() will check for errors. This allows
5055 * ":e `ls ve*.c`" on Unix.
5056 */
5057 if (!has_wildcards)
5058 for (p = eap->arg; *p; ++p)
5059 {
5060 /* skip escaped characters */
5061 if (p[1] && (*p == '\\' || *p == Ctrl_V))
5062 ++p;
5063 else if (vim_iswhite(*p))
5064 {
5065 *errormsgp = (char_u *)_("E172: Only one file name allowed");
5066 return FAIL;
5067 }
5068 }
5069#endif
5070
5071 /*
5072 * Halve the number of backslashes (this is Vi compatible).
5073 * For Unix and OS/2, when wildcards are expanded, this is
5074 * done by ExpandOne() below.
5075 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005076#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 if (!has_wildcards)
5078#endif
5079 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 }
5081
5082 if (has_wildcards)
5083 {
5084 if (n == 1)
5085 {
5086 /*
5087 * First loop: May expand environment variables. This
5088 * can be done much faster with expand_env() than with
5089 * something else (e.g., calling a shell).
5090 * After expanding environment variables, check again
5091 * if there are still wildcards present.
5092 */
5093 if (vim_strchr(eap->arg, '$') != NULL
5094 || vim_strchr(eap->arg, '~') != NULL)
5095 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005096 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005097 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 has_wildcards = mch_has_wildcard(NameBuff);
5099 p = NameBuff;
5100 }
5101 else
5102 p = NULL;
5103 }
5104 else /* n == 2 */
5105 {
5106 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005107 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108
5109 ExpandInit(&xpc);
5110 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005111 if (p_wic)
5112 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005114 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 if (p == NULL)
5116 return FAIL;
5117 }
5118 if (p != NULL)
5119 {
5120 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5121 p, cmdlinep);
5122 if (n == 2) /* p came from ExpandOne() */
5123 vim_free(p);
5124 }
5125 }
5126 }
5127 }
5128 return OK;
5129}
5130
5131/*
5132 * Replace part of the command line, keeping eap->cmd, eap->arg and
5133 * eap->nextcmd correct.
5134 * "src" points to the part that is to be replaced, of length "srclen".
5135 * "repl" is the replacement string.
5136 * Returns a pointer to the character after the replaced string.
5137 * Returns NULL for failure.
5138 */
5139 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005140repl_cmdline(
5141 exarg_T *eap,
5142 char_u *src,
5143 int srclen,
5144 char_u *repl,
5145 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146{
5147 int len;
5148 int i;
5149 char_u *new_cmdline;
5150
5151 /*
5152 * The new command line is build in new_cmdline[].
5153 * First allocate it.
5154 * Careful: a "+cmd" argument may have been NUL terminated.
5155 */
5156 len = (int)STRLEN(repl);
5157 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005158 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5160 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5161 return NULL; /* out of memory! */
5162
5163 /*
5164 * Copy the stuff before the expanded part.
5165 * Copy the expanded stuff.
5166 * Copy what came after the expanded part.
5167 * Copy the next commands, if there are any.
5168 */
5169 i = (int)(src - *cmdlinep); /* length of part before match */
5170 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005171
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 mch_memmove(new_cmdline + i, repl, (size_t)len);
5173 i += len; /* remember the end of the string */
5174 STRCPY(new_cmdline + i, src + srclen);
5175 src = new_cmdline + i; /* remember where to continue */
5176
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005177 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 {
5179 i = (int)STRLEN(new_cmdline) + 1;
5180 STRCPY(new_cmdline + i, eap->nextcmd);
5181 eap->nextcmd = new_cmdline + i;
5182 }
5183 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5184 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5185 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5186 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5187 vim_free(*cmdlinep);
5188 *cmdlinep = new_cmdline;
5189
5190 return src;
5191}
5192
5193/*
5194 * Check for '|' to separate commands and '"' to start comments.
5195 */
5196 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005197separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198{
5199 char_u *p;
5200
Bram Moolenaar86b68352004-12-27 21:59:20 +00005201#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005202 p = skip_grep_pat(eap);
5203#else
5204 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005205#endif
5206
5207 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 {
5209 if (*p == Ctrl_V)
5210 {
5211 if (eap->argt & (USECTRLV | XFILE))
5212 ++p; /* skip CTRL-V and next char */
5213 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005214 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005215 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 if (*p == NUL) /* stop at NUL after CTRL-V */
5217 break;
5218 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005219
5220#ifdef FEAT_EVAL
5221 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005222 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005223 {
5224 p += 2;
5225 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005226 }
5227#endif
5228
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 /* Check for '"': start of comment or '|': next command */
5230 /* :@" and :*" do not start a comment!
5231 * :redir @" doesn't either. */
5232 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5233 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5234 || p != eap->arg)
5235 && (eap->cmdidx != CMD_redir
5236 || p != eap->arg + 1 || p[-1] != '@'))
5237 || *p == '|' || *p == '\n')
5238 {
5239 /*
5240 * We remove the '\' before the '|', unless USECTRLV is used
5241 * AND 'b' is present in 'cpoptions'.
5242 */
5243 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5244 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5245 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005246 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 --p;
5248 }
5249 else
5250 {
5251 eap->nextcmd = check_nextcmd(p);
5252 *p = NUL;
5253 break;
5254 }
5255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005257
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5259 del_trailing_spaces(eap->arg);
5260}
5261
5262/*
5263 * get + command from ex argument
5264 */
5265 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005266getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267{
5268 char_u *arg = *argp;
5269 char_u *command = NULL;
5270
5271 if (*arg == '+') /* +[command] */
5272 {
5273 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005274 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 command = dollar_command;
5276 else
5277 {
5278 command = arg;
5279 arg = skip_cmd_arg(command, TRUE);
5280 if (*arg != NUL)
5281 *arg++ = NUL; /* terminate command with NUL */
5282 }
5283
5284 arg = skipwhite(arg); /* skip over spaces */
5285 *argp = arg;
5286 }
5287 return command;
5288}
5289
5290/*
5291 * Find end of "+command" argument. Skip over "\ " and "\\".
5292 */
5293 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005294skip_cmd_arg(
5295 char_u *p,
5296 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297{
5298 while (*p && !vim_isspace(*p))
5299 {
5300 if (*p == '\\' && p[1] != NUL)
5301 {
5302 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005303 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 else
5305 ++p;
5306 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005307 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 }
5309 return p;
5310}
5311
5312/*
5313 * Get "++opt=arg" argument.
5314 * Return FAIL or OK.
5315 */
5316 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005317getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318{
5319 char_u *arg = eap->arg + 2;
5320 int *pp = NULL;
5321#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005322 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 char_u *p;
5324#endif
5325
5326 /* ":edit ++[no]bin[ary] file" */
5327 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5328 {
5329 if (*arg == 'n')
5330 {
5331 arg += 2;
5332 eap->force_bin = FORCE_NOBIN;
5333 }
5334 else
5335 eap->force_bin = FORCE_BIN;
5336 if (!checkforcmd(&arg, "binary", 3))
5337 return FAIL;
5338 eap->arg = skipwhite(arg);
5339 return OK;
5340 }
5341
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005342 /* ":read ++edit file" */
5343 if (STRNCMP(arg, "edit", 4) == 0)
5344 {
5345 eap->read_edit = TRUE;
5346 eap->arg = skipwhite(arg + 4);
5347 return OK;
5348 }
5349
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 if (STRNCMP(arg, "ff", 2) == 0)
5351 {
5352 arg += 2;
5353 pp = &eap->force_ff;
5354 }
5355 else if (STRNCMP(arg, "fileformat", 10) == 0)
5356 {
5357 arg += 10;
5358 pp = &eap->force_ff;
5359 }
5360#ifdef FEAT_MBYTE
5361 else if (STRNCMP(arg, "enc", 3) == 0)
5362 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005363 if (STRNCMP(arg, "encoding", 8) == 0)
5364 arg += 8;
5365 else
5366 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 pp = &eap->force_enc;
5368 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005369 else if (STRNCMP(arg, "bad", 3) == 0)
5370 {
5371 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005372 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374#endif
5375
5376 if (pp == NULL || *arg != '=')
5377 return FAIL;
5378
5379 ++arg;
5380 *pp = (int)(arg - eap->cmd);
5381 arg = skip_cmd_arg(arg, FALSE);
5382 eap->arg = skipwhite(arg);
5383 *arg = NUL;
5384
5385#ifdef FEAT_MBYTE
5386 if (pp == &eap->force_ff)
5387 {
5388#endif
5389 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5390 return FAIL;
5391#ifdef FEAT_MBYTE
5392 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005393 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 {
5395 /* Make 'fileencoding' lower case. */
5396 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5397 *p = TOLOWER_ASC(*p);
5398 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005399 else
5400 {
5401 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5402 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005403 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005404 if (STRICMP(p, "keep") == 0)
5405 eap->bad_char = BAD_KEEP;
5406 else if (STRICMP(p, "drop") == 0)
5407 eap->bad_char = BAD_DROP;
5408 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5409 eap->bad_char = *p;
5410 else
5411 return FAIL;
5412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413#endif
5414
5415 return OK;
5416}
5417
5418/*
5419 * ":abbreviate" and friends.
5420 */
5421 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005422ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423{
5424 do_exmap(eap, TRUE); /* almost the same as mapping */
5425}
5426
5427/*
5428 * ":map" and friends.
5429 */
5430 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005431ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432{
5433 /*
5434 * If we are sourcing .exrc or .vimrc in current directory we
5435 * print the mappings for security reasons.
5436 */
5437 if (secure)
5438 {
5439 secure = 2;
5440 msg_outtrans(eap->cmd);
5441 msg_putchar('\n');
5442 }
5443 do_exmap(eap, FALSE);
5444}
5445
5446/*
5447 * ":unmap" and friends.
5448 */
5449 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005450ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451{
5452 do_exmap(eap, FALSE);
5453}
5454
5455/*
5456 * ":mapclear" and friends.
5457 */
5458 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005459ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460{
5461 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5462}
5463
5464/*
5465 * ":abclear" and friends.
5466 */
5467 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005468ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469{
5470 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5471}
5472
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005473#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005475ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476{
5477 /*
5478 * Disallow auto commands from .exrc and .vimrc in current
5479 * directory for security reasons.
5480 */
5481 if (secure)
5482 {
5483 secure = 2;
5484 eap->errmsg = e_curdir;
5485 }
5486 else if (eap->cmdidx == CMD_autocmd)
5487 do_autocmd(eap->arg, eap->forceit);
5488 else
5489 do_augroup(eap->arg, eap->forceit);
5490}
5491
5492/*
5493 * ":doautocmd": Apply the automatic commands to the current buffer.
5494 */
5495 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005496ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005498 char_u *arg = eap->arg;
5499 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005500 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005501
Bram Moolenaar1610d052016-06-09 22:53:01 +02005502 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5503 /* Only when there is no <nomodeline>. */
5504 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005505 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506}
5507#endif
5508
5509#ifdef FEAT_LISTCMDS
5510/*
5511 * :[N]bunload[!] [N] [bufname] unload buffer
5512 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5513 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5514 */
5515 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005516ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517{
5518 eap->errmsg = do_bufdel(
5519 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5520 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5521 : DOBUF_UNLOAD, eap->arg,
5522 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5523}
5524
5525/*
5526 * :[N]buffer [N] to buffer N
5527 * :[N]sbuffer [N] to buffer N
5528 */
5529 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005530ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531{
5532 if (*eap->arg)
5533 eap->errmsg = e_trailing;
5534 else
5535 {
5536 if (eap->addr_count == 0) /* default is current buffer */
5537 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5538 else
5539 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005540 if (eap->do_ecmd_cmd != NULL)
5541 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 }
5543}
5544
5545/*
5546 * :[N]bmodified [N] to next mod. buffer
5547 * :[N]sbmodified [N] to next mod. buffer
5548 */
5549 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005550ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551{
5552 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005553 if (eap->do_ecmd_cmd != NULL)
5554 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555}
5556
5557/*
5558 * :[N]bnext [N] to next buffer
5559 * :[N]sbnext [N] split and to next buffer
5560 */
5561 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005562ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563{
5564 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005565 if (eap->do_ecmd_cmd != NULL)
5566 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567}
5568
5569/*
5570 * :[N]bNext [N] to previous buffer
5571 * :[N]bprevious [N] to previous buffer
5572 * :[N]sbNext [N] split and to previous buffer
5573 * :[N]sbprevious [N] split and to previous buffer
5574 */
5575 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005576ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577{
5578 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005579 if (eap->do_ecmd_cmd != NULL)
5580 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581}
5582
5583/*
5584 * :brewind to first buffer
5585 * :bfirst to first buffer
5586 * :sbrewind split and to first buffer
5587 * :sbfirst split and to first buffer
5588 */
5589 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005590ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591{
5592 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005593 if (eap->do_ecmd_cmd != NULL)
5594 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595}
5596
5597/*
5598 * :blast to last buffer
5599 * :sblast split and to last buffer
5600 */
5601 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005602ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603{
5604 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005605 if (eap->do_ecmd_cmd != NULL)
5606 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607}
5608#endif
5609
5610 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005611ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612{
5613 return (c == NUL || c == '|' || c == '"' || c == '\n');
5614}
5615
5616#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5617 || defined(PROTO)
5618/*
5619 * Return the next command, after the first '|' or '\n'.
5620 * Return NULL if not found.
5621 */
5622 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005623find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624{
5625 while (*p != '|' && *p != '\n')
5626 {
5627 if (*p == NUL)
5628 return NULL;
5629 ++p;
5630 }
5631 return (p + 1);
5632}
5633#endif
5634
5635/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005636 * Check if *p is a separator between Ex commands, skipping over white space.
5637 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 */
5639 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005640check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005642 char_u *s = skipwhite(p);
5643
5644 if (*s == '|' || *s == '\n')
5645 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 else
5647 return NULL;
5648}
5649
5650/*
5651 * - if there are more files to edit
5652 * - and this is the last window
5653 * - and forceit not used
5654 * - and not repeated twice on a row
5655 * return FAIL and give error message if 'message' TRUE
5656 * return OK otherwise
5657 */
5658 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005659check_more(
5660 int message, /* when FALSE check only, no messages */
5661 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662{
5663 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5664
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005665 if (!forceit && only_one_window()
5666 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 {
5668 if (message)
5669 {
5670#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5671 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5672 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005673 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674
5675 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005676 vim_strncpy(buff,
5677 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005678 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005680 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 _("%d more files to edit. Quit anyway?"), n);
5682 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5683 return OK;
5684 return FAIL;
5685 }
5686#endif
5687 if (n == 1)
5688 EMSG(_("E173: 1 more file to edit"));
5689 else
5690 EMSGN(_("E173: %ld more files to edit"), n);
5691 quitmore = 2; /* next try to quit is allowed */
5692 }
5693 return FAIL;
5694 }
5695 return OK;
5696}
5697
5698#ifdef FEAT_CMDL_COMPL
5699/*
5700 * Function given to ExpandGeneric() to obtain the list of command names.
5701 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005703get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704{
5705 if (idx >= (int)CMD_SIZE)
5706# ifdef FEAT_USR_CMDS
5707 return get_user_command_name(idx);
5708# else
5709 return NULL;
5710# endif
5711 return cmdnames[idx].cmd_name;
5712}
5713#endif
5714
5715#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005716static 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);
5717static void uc_list(char_u *name, size_t name_len);
5718static 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);
5719static char_u *uc_split_args(char_u *arg, size_t *lenp);
5720static 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 +00005721
5722 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005723uc_add_command(
5724 char_u *name,
5725 size_t name_len,
5726 char_u *rep,
5727 long argt,
5728 long def,
5729 int flags,
5730 int compl,
5731 char_u *compl_arg,
5732 int addr_type,
5733 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734{
5735 ucmd_T *cmd = NULL;
5736 char_u *p;
5737 int i;
5738 int cmp = 1;
5739 char_u *rep_buf = NULL;
5740 garray_T *gap;
5741
Bram Moolenaar9c102382006-05-03 21:26:49 +00005742 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 if (rep_buf == NULL)
5744 {
5745 /* Can't replace termcodes - try using the string as is */
5746 rep_buf = vim_strsave(rep);
5747
5748 /* Give up if out of memory */
5749 if (rep_buf == NULL)
5750 return FAIL;
5751 }
5752
5753 /* get address of growarray: global or in curbuf */
5754 if (flags & UC_BUFFER)
5755 {
5756 gap = &curbuf->b_ucmds;
5757 if (gap->ga_itemsize == 0)
5758 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5759 }
5760 else
5761 gap = &ucmds;
5762
5763 /* Search for the command in the already defined commands. */
5764 for (i = 0; i < gap->ga_len; ++i)
5765 {
5766 size_t len;
5767
5768 cmd = USER_CMD_GA(gap, i);
5769 len = STRLEN(cmd->uc_name);
5770 cmp = STRNCMP(name, cmd->uc_name, name_len);
5771 if (cmp == 0)
5772 {
5773 if (name_len < len)
5774 cmp = -1;
5775 else if (name_len > len)
5776 cmp = 1;
5777 }
5778
5779 if (cmp == 0)
5780 {
5781 if (!force)
5782 {
5783 EMSG(_("E174: Command already exists: add ! to replace it"));
5784 goto fail;
5785 }
5786
5787 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005788 cmd->uc_rep = NULL;
5789#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5790 vim_free(cmd->uc_compl_arg);
5791 cmd->uc_compl_arg = NULL;
5792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 break;
5794 }
5795
5796 /* Stop as soon as we pass the name to add */
5797 if (cmp < 0)
5798 break;
5799 }
5800
5801 /* Extend the array unless we're replacing an existing command */
5802 if (cmp != 0)
5803 {
5804 if (ga_grow(gap, 1) != OK)
5805 goto fail;
5806 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5807 goto fail;
5808
5809 cmd = USER_CMD_GA(gap, i);
5810 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5811
5812 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813
5814 cmd->uc_name = p;
5815 }
5816
5817 cmd->uc_rep = rep_buf;
5818 cmd->uc_argt = argt;
5819 cmd->uc_def = def;
5820 cmd->uc_compl = compl;
5821#ifdef FEAT_EVAL
5822 cmd->uc_scriptID = current_SID;
5823# ifdef FEAT_CMDL_COMPL
5824 cmd->uc_compl_arg = compl_arg;
5825# endif
5826#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005827 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828
5829 return OK;
5830
5831fail:
5832 vim_free(rep_buf);
5833#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5834 vim_free(compl_arg);
5835#endif
5836 return FAIL;
5837}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005840#if defined(FEAT_USR_CMDS)
5841static struct
5842{
5843 int expand;
5844 char *name;
5845} addr_type_complete[] =
5846{
5847 {ADDR_ARGUMENTS, "arguments"},
5848 {ADDR_LINES, "lines"},
5849 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5850 {ADDR_TABS, "tabs"},
5851 {ADDR_BUFFERS, "buffers"},
5852 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005853 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005854 {-1, NULL}
5855};
5856#endif
5857
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005858#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859/*
5860 * List of names for completion for ":command" with the EXPAND_ flag.
5861 * Must be alphabetical for completion.
5862 */
5863static struct
5864{
5865 int expand;
5866 char *name;
5867} command_complete[] =
5868{
5869 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005870 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005872 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005874 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005875#if defined(FEAT_CSCOPE)
5876 {EXPAND_CSCOPE, "cscope"},
5877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5879 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005880 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881#endif
5882 {EXPAND_DIRECTORIES, "dir"},
5883 {EXPAND_ENV_VARS, "environment"},
5884 {EXPAND_EVENTS, "event"},
5885 {EXPAND_EXPRESSION, "expression"},
5886 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005887 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005888 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 {EXPAND_FUNCTIONS, "function"},
5890 {EXPAND_HELP, "help"},
5891 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005892#if defined(FEAT_CMDHIST)
5893 {EXPAND_HISTORY, "history"},
5894#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005895#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005896 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005897 {EXPAND_LOCALES, "locale"},
5898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 {EXPAND_MAPPINGS, "mapping"},
5900 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005901 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005902 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005903#if defined(FEAT_PROFILE)
5904 {EXPAND_SYNTIME, "syntime"},
5905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005907 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005908 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005909#if defined(FEAT_SIGNS)
5910 {EXPAND_SIGN, "sign"},
5911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 {EXPAND_TAGS, "tag"},
5913 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005914 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 {EXPAND_USER_VARS, "var"},
5916 {0, NULL}
5917};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005920#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005922uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923{
5924 int i, j;
5925 int found = FALSE;
5926 ucmd_T *cmd;
5927 int len;
5928 long a;
5929 garray_T *gap;
5930
5931 gap = &curbuf->b_ucmds;
5932 for (;;)
5933 {
5934 for (i = 0; i < gap->ga_len; ++i)
5935 {
5936 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005937 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938
Bram Moolenaard29459b2016-08-26 22:29:11 +02005939 /* Skip commands which don't match the requested prefix and
5940 * commands filtered out. */
5941 if (STRNCMP(name, cmd->uc_name, name_len) != 0
5942 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 continue;
5944
5945 /* Put out the title first time */
5946 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005947 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 found = TRUE;
5949 msg_putchar('\n');
5950 if (got_int)
5951 break;
5952
5953 /* Special cases */
5954 msg_putchar(a & BANG ? '!' : ' ');
5955 msg_putchar(a & REGSTR ? '"' : ' ');
5956 msg_putchar(gap != &ucmds ? 'b' : ' ');
5957 msg_putchar(' ');
5958
5959 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5960 len = (int)STRLEN(cmd->uc_name) + 4;
5961
5962 do {
5963 msg_putchar(' ');
5964 ++len;
5965 } while (len < 16);
5966
5967 len = 0;
5968
5969 /* Arguments */
5970 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5971 {
5972 case 0: IObuff[len++] = '0'; break;
5973 case (EXTRA): IObuff[len++] = '*'; break;
5974 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5975 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5976 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5977 }
5978
5979 do {
5980 IObuff[len++] = ' ';
5981 } while (len < 5);
5982
5983 /* Range */
5984 if (a & (RANGE|COUNT))
5985 {
5986 if (a & COUNT)
5987 {
5988 /* -count=N */
5989 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5990 len += (int)STRLEN(IObuff + len);
5991 }
5992 else if (a & DFLALL)
5993 IObuff[len++] = '%';
5994 else if (cmd->uc_def >= 0)
5995 {
5996 /* -range=N */
5997 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5998 len += (int)STRLEN(IObuff + len);
5999 }
6000 else
6001 IObuff[len++] = '.';
6002 }
6003
6004 do {
6005 IObuff[len++] = ' ';
6006 } while (len < 11);
6007
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006008 /* Address Type */
6009 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6010 if (addr_type_complete[j].expand != ADDR_LINES
6011 && addr_type_complete[j].expand == cmd->uc_addr_type)
6012 {
6013 STRCPY(IObuff + len, addr_type_complete[j].name);
6014 len += (int)STRLEN(IObuff + len);
6015 break;
6016 }
6017
6018 do {
6019 IObuff[len++] = ' ';
6020 } while (len < 21);
6021
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 /* Completion */
6023 for (j = 0; command_complete[j].expand != 0; ++j)
6024 if (command_complete[j].expand == cmd->uc_compl)
6025 {
6026 STRCPY(IObuff + len, command_complete[j].name);
6027 len += (int)STRLEN(IObuff + len);
6028 break;
6029 }
6030
6031 do {
6032 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006033 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034
6035 IObuff[len] = '\0';
6036 msg_outtrans(IObuff);
6037
6038 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006039#ifdef FEAT_EVAL
6040 if (p_verbose > 0)
6041 last_set_msg(cmd->uc_scriptID);
6042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 out_flush();
6044 ui_breakcheck();
6045 if (got_int)
6046 break;
6047 }
6048 if (gap == &ucmds || i < gap->ga_len)
6049 break;
6050 gap = &ucmds;
6051 }
6052
6053 if (!found)
6054 MSG(_("No user-defined commands found"));
6055}
6056
6057 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006058uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059{
6060 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6061 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6062 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6063 0xb9, 0x7f, 0};
6064 int i;
6065
6066 for (i = 0; fcmd[i]; ++i)
6067 IObuff[i] = fcmd[i] - 0x40;
6068 IObuff[i] = 0;
6069 return IObuff;
6070}
6071
6072 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006073uc_scan_attr(
6074 char_u *attr,
6075 size_t len,
6076 long *argt,
6077 long *def,
6078 int *flags,
6079 int *compl,
6080 char_u **compl_arg,
6081 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082{
6083 char_u *p;
6084
6085 if (len == 0)
6086 {
6087 EMSG(_("E175: No attribute specified"));
6088 return FAIL;
6089 }
6090
6091 /* First, try the simple attributes (no arguments) */
6092 if (STRNICMP(attr, "bang", len) == 0)
6093 *argt |= BANG;
6094 else if (STRNICMP(attr, "buffer", len) == 0)
6095 *flags |= UC_BUFFER;
6096 else if (STRNICMP(attr, "register", len) == 0)
6097 *argt |= REGSTR;
6098 else if (STRNICMP(attr, "bar", len) == 0)
6099 *argt |= TRLBAR;
6100 else
6101 {
6102 int i;
6103 char_u *val = NULL;
6104 size_t vallen = 0;
6105 size_t attrlen = len;
6106
6107 /* Look for the attribute name - which is the part before any '=' */
6108 for (i = 0; i < (int)len; ++i)
6109 {
6110 if (attr[i] == '=')
6111 {
6112 val = &attr[i + 1];
6113 vallen = len - i - 1;
6114 attrlen = i;
6115 break;
6116 }
6117 }
6118
6119 if (STRNICMP(attr, "nargs", attrlen) == 0)
6120 {
6121 if (vallen == 1)
6122 {
6123 if (*val == '0')
6124 /* Do nothing - this is the default */;
6125 else if (*val == '1')
6126 *argt |= (EXTRA | NOSPC | NEEDARG);
6127 else if (*val == '*')
6128 *argt |= EXTRA;
6129 else if (*val == '?')
6130 *argt |= (EXTRA | NOSPC);
6131 else if (*val == '+')
6132 *argt |= (EXTRA | NEEDARG);
6133 else
6134 goto wrong_nargs;
6135 }
6136 else
6137 {
6138wrong_nargs:
6139 EMSG(_("E176: Invalid number of arguments"));
6140 return FAIL;
6141 }
6142 }
6143 else if (STRNICMP(attr, "range", attrlen) == 0)
6144 {
6145 *argt |= RANGE;
6146 if (vallen == 1 && *val == '%')
6147 *argt |= DFLALL;
6148 else if (val != NULL)
6149 {
6150 p = val;
6151 if (*def >= 0)
6152 {
6153two_count:
6154 EMSG(_("E177: Count cannot be specified twice"));
6155 return FAIL;
6156 }
6157
6158 *def = getdigits(&p);
6159 *argt |= (ZEROR | NOTADR);
6160
6161 if (p != val + vallen || vallen == 0)
6162 {
6163invalid_count:
6164 EMSG(_("E178: Invalid default value for count"));
6165 return FAIL;
6166 }
6167 }
6168 }
6169 else if (STRNICMP(attr, "count", attrlen) == 0)
6170 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006171 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172
6173 if (val != NULL)
6174 {
6175 p = val;
6176 if (*def >= 0)
6177 goto two_count;
6178
6179 *def = getdigits(&p);
6180
6181 if (p != val + vallen)
6182 goto invalid_count;
6183 }
6184
6185 if (*def < 0)
6186 *def = 0;
6187 }
6188 else if (STRNICMP(attr, "complete", attrlen) == 0)
6189 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 if (val == NULL)
6191 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006192 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 return FAIL;
6194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006196 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6197 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006200 else if (STRNICMP(attr, "addr", attrlen) == 0)
6201 {
6202 *argt |= RANGE;
6203 if (val == NULL)
6204 {
6205 EMSG(_("E179: argument required for -addr"));
6206 return FAIL;
6207 }
6208 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6209 == FAIL)
6210 return FAIL;
6211 if (addr_type_arg != ADDR_LINES)
6212 *argt |= (ZEROR | NOTADR) ;
6213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 else
6215 {
6216 char_u ch = attr[len];
6217 attr[len] = '\0';
6218 EMSG2(_("E181: Invalid attribute: %s"), attr);
6219 attr[len] = ch;
6220 return FAIL;
6221 }
6222 }
6223
6224 return OK;
6225}
6226
Bram Moolenaara850a712009-01-28 14:42:59 +00006227/*
6228 * ":command ..."
6229 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006231ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232{
6233 char_u *name;
6234 char_u *end;
6235 char_u *p;
6236 long argt = 0;
6237 long def = -1;
6238 int flags = 0;
6239 int compl = EXPAND_NOTHING;
6240 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006241 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006243 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244
6245 p = eap->arg;
6246
6247 /* Check for attributes */
6248 while (*p == '-')
6249 {
6250 ++p;
6251 end = skiptowhite(p);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006252 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 == FAIL)
6254 return;
6255 p = skipwhite(end);
6256 }
6257
6258 /* Get the name (if any) and skip to the following argument */
6259 name = p;
6260 if (ASCII_ISALPHA(*p))
6261 while (ASCII_ISALNUM(*p))
6262 ++p;
6263 if (!ends_excmd(*p) && !vim_iswhite(*p))
6264 {
6265 EMSG(_("E182: Invalid command name"));
6266 return;
6267 }
6268 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006269 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270
6271 /* If there is nothing after the name, and no attributes were specified,
6272 * we are listing commands
6273 */
6274 p = skipwhite(end);
6275 if (!has_attr && ends_excmd(*p))
6276 {
6277 uc_list(name, end - name);
6278 }
6279 else if (!ASCII_ISUPPER(*name))
6280 {
6281 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6282 return;
6283 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006284 else if ((name_len == 1 && *name == 'X')
6285 || (name_len <= 4
6286 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6287 {
6288 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6289 return;
6290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 else
6292 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006293 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294}
6295
6296/*
6297 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 * Clear all user commands, global and for current buffer.
6299 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006300 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006301ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302{
6303 uc_clear(&ucmds);
6304 uc_clear(&curbuf->b_ucmds);
6305}
6306
6307/*
6308 * Clear all user commands for "gap".
6309 */
6310 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006311uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312{
6313 int i;
6314 ucmd_T *cmd;
6315
6316 for (i = 0; i < gap->ga_len; ++i)
6317 {
6318 cmd = USER_CMD_GA(gap, i);
6319 vim_free(cmd->uc_name);
6320 vim_free(cmd->uc_rep);
6321# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6322 vim_free(cmd->uc_compl_arg);
6323# endif
6324 }
6325 ga_clear(gap);
6326}
6327
6328 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006329ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330{
6331 int i = 0;
6332 ucmd_T *cmd = NULL;
6333 int cmp = -1;
6334 garray_T *gap;
6335
6336 gap = &curbuf->b_ucmds;
6337 for (;;)
6338 {
6339 for (i = 0; i < gap->ga_len; ++i)
6340 {
6341 cmd = USER_CMD_GA(gap, i);
6342 cmp = STRCMP(eap->arg, cmd->uc_name);
6343 if (cmp <= 0)
6344 break;
6345 }
6346 if (gap == &ucmds || cmp == 0)
6347 break;
6348 gap = &ucmds;
6349 }
6350
6351 if (cmp != 0)
6352 {
6353 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6354 return;
6355 }
6356
6357 vim_free(cmd->uc_name);
6358 vim_free(cmd->uc_rep);
6359# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6360 vim_free(cmd->uc_compl_arg);
6361# endif
6362
6363 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364
6365 if (i < gap->ga_len)
6366 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6367}
6368
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006369/*
6370 * split and quote args for <f-args>
6371 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006373uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374{
6375 char_u *buf;
6376 char_u *p;
6377 char_u *q;
6378 int len;
6379
6380 /* Precalculate length */
6381 p = arg;
6382 len = 2; /* Initial and final quotes */
6383
6384 while (*p)
6385 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006386 if (p[0] == '\\' && p[1] == '\\')
6387 {
6388 len += 2;
6389 p += 2;
6390 }
6391 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 {
6393 len += 1;
6394 p += 2;
6395 }
6396 else if (*p == '\\' || *p == '"')
6397 {
6398 len += 2;
6399 p += 1;
6400 }
6401 else if (vim_iswhite(*p))
6402 {
6403 p = skipwhite(p);
6404 if (*p == NUL)
6405 break;
6406 len += 3; /* "," */
6407 }
6408 else
6409 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006410#ifdef FEAT_MBYTE
6411 int charlen = (*mb_ptr2len)(p);
6412 len += charlen;
6413 p += charlen;
6414#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 ++len;
6416 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 }
6419 }
6420
6421 buf = alloc(len + 1);
6422 if (buf == NULL)
6423 {
6424 *lenp = 0;
6425 return buf;
6426 }
6427
6428 p = arg;
6429 q = buf;
6430 *q++ = '"';
6431 while (*p)
6432 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006433 if (p[0] == '\\' && p[1] == '\\')
6434 {
6435 *q++ = '\\';
6436 *q++ = '\\';
6437 p += 2;
6438 }
6439 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 {
6441 *q++ = p[1];
6442 p += 2;
6443 }
6444 else if (*p == '\\' || *p == '"')
6445 {
6446 *q++ = '\\';
6447 *q++ = *p++;
6448 }
6449 else if (vim_iswhite(*p))
6450 {
6451 p = skipwhite(p);
6452 if (*p == NUL)
6453 break;
6454 *q++ = '"';
6455 *q++ = ',';
6456 *q++ = '"';
6457 }
6458 else
6459 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006460 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 }
6462 }
6463 *q++ = '"';
6464 *q = 0;
6465
6466 *lenp = len;
6467 return buf;
6468}
6469
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006470 static size_t
6471add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6472{
6473 size_t result;
6474
6475 result = STRLEN(mod_str);
6476 if (*multi_mods)
6477 result += 1;
6478 if (buf != NULL)
6479 {
6480 if (*multi_mods)
6481 STRCAT(buf, " ");
6482 STRCAT(buf, mod_str);
6483 }
6484
6485 *multi_mods = 1;
6486
6487 return result;
6488}
6489
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490/*
6491 * Check for a <> code in a user command.
6492 * "code" points to the '<'. "len" the length of the <> (inclusive).
6493 * "buf" is where the result is to be added.
6494 * "split_buf" points to a buffer used for splitting, caller should free it.
6495 * "split_len" is the length of what "split_buf" contains.
6496 * Returns the length of the replacement, which has been added to "buf".
6497 * Returns -1 if there was no match, and only the "<" has been copied.
6498 */
6499 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006500uc_check_code(
6501 char_u *code,
6502 size_t len,
6503 char_u *buf,
6504 ucmd_T *cmd, /* the user command we're expanding */
6505 exarg_T *eap, /* ex arguments */
6506 char_u **split_buf,
6507 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508{
6509 size_t result = 0;
6510 char_u *p = code + 1;
6511 size_t l = len - 2;
6512 int quote = 0;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006513 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_MODS,
6514 ct_REGISTER, ct_LT, ct_NONE } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515
6516 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6517 {
6518 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6519 p += 2;
6520 l -= 2;
6521 }
6522
Bram Moolenaar371d5402006-03-20 21:47:49 +00006523 ++l;
6524 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006526 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006528 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006530 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006532 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006534 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006536 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006538 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006540 else if (STRNICMP(p, "mods>", l) == 0)
6541 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542
6543 switch (type)
6544 {
6545 case ct_ARGS:
6546 /* Simple case first */
6547 if (*eap->arg == NUL)
6548 {
6549 if (quote == 1)
6550 {
6551 result = 2;
6552 if (buf != NULL)
6553 STRCPY(buf, "''");
6554 }
6555 else
6556 result = 0;
6557 break;
6558 }
6559
6560 /* When specified there is a single argument don't split it.
6561 * Works for ":Cmd %" when % is "a b c". */
6562 if ((eap->argt & NOSPC) && quote == 2)
6563 quote = 1;
6564
6565 switch (quote)
6566 {
6567 case 0: /* No quoting, no splitting */
6568 result = STRLEN(eap->arg);
6569 if (buf != NULL)
6570 STRCPY(buf, eap->arg);
6571 break;
6572 case 1: /* Quote, but don't split */
6573 result = STRLEN(eap->arg) + 2;
6574 for (p = eap->arg; *p; ++p)
6575 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006576#ifdef FEAT_MBYTE
6577 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6578 /* DBCS can contain \ in a trail byte, skip the
6579 * double-byte character. */
6580 ++p;
6581 else
6582#endif
6583 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 ++result;
6585 }
6586
6587 if (buf != NULL)
6588 {
6589 *buf++ = '"';
6590 for (p = eap->arg; *p; ++p)
6591 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006592#ifdef FEAT_MBYTE
6593 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6594 /* DBCS can contain \ in a trail byte, copy the
6595 * double-byte character to avoid escaping. */
6596 *buf++ = *p++;
6597 else
6598#endif
6599 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 *buf++ = '\\';
6601 *buf++ = *p;
6602 }
6603 *buf = '"';
6604 }
6605
6606 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006607 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 /* This is hard, so only do it once, and cache the result */
6609 if (*split_buf == NULL)
6610 *split_buf = uc_split_args(eap->arg, split_len);
6611
6612 result = *split_len;
6613 if (buf != NULL && result != 0)
6614 STRCPY(buf, *split_buf);
6615
6616 break;
6617 }
6618 break;
6619
6620 case ct_BANG:
6621 result = eap->forceit ? 1 : 0;
6622 if (quote)
6623 result += 2;
6624 if (buf != NULL)
6625 {
6626 if (quote)
6627 *buf++ = '"';
6628 if (eap->forceit)
6629 *buf++ = '!';
6630 if (quote)
6631 *buf = '"';
6632 }
6633 break;
6634
6635 case ct_LINE1:
6636 case ct_LINE2:
6637 case ct_COUNT:
6638 {
6639 char num_buf[20];
6640 long num = (type == ct_LINE1) ? eap->line1 :
6641 (type == ct_LINE2) ? eap->line2 :
6642 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6643 size_t num_len;
6644
6645 sprintf(num_buf, "%ld", num);
6646 num_len = STRLEN(num_buf);
6647 result = num_len;
6648
6649 if (quote)
6650 result += 2;
6651
6652 if (buf != NULL)
6653 {
6654 if (quote)
6655 *buf++ = '"';
6656 STRCPY(buf, num_buf);
6657 buf += num_len;
6658 if (quote)
6659 *buf = '"';
6660 }
6661
6662 break;
6663 }
6664
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006665 case ct_MODS:
6666 {
6667 int multi_mods = 0;
6668 typedef struct {
6669 int *varp;
6670 char *name;
6671 } mod_entry_T;
6672 static mod_entry_T mod_entries[] = {
6673#ifdef FEAT_BROWSE_CMD
6674 {&cmdmod.browse, "browse"},
6675#endif
6676#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6677 {&cmdmod.confirm, "confirm"},
6678#endif
6679 {&cmdmod.hide, "hide"},
6680 {&cmdmod.keepalt, "keepalt"},
6681 {&cmdmod.keepjumps, "keepjumps"},
6682 {&cmdmod.keepmarks, "keepmarks"},
6683 {&cmdmod.keeppatterns, "keeppatterns"},
6684 {&cmdmod.lockmarks, "lockmarks"},
6685 {&cmdmod.noswapfile, "noswapfile"},
6686 {NULL, NULL}
6687 };
6688 int i;
6689
6690 result = quote ? 2 : 0;
6691 if (buf != NULL)
6692 {
6693 if (quote)
6694 *buf++ = '"';
6695 *buf = '\0';
6696 }
6697
6698#ifdef FEAT_WINDOWS
6699 /* :aboveleft and :leftabove */
6700 if (cmdmod.split & WSP_ABOVE)
6701 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6702 /* :belowright and :rightbelow */
6703 if (cmdmod.split & WSP_BELOW)
6704 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6705 /* :botright */
6706 if (cmdmod.split & WSP_BOT)
6707 result += add_cmd_modifier(buf, "botright", &multi_mods);
6708#endif
6709
6710 /* the modifiers that are simple flags */
6711 for (i = 0; mod_entries[i].varp != NULL; ++i)
6712 if (*mod_entries[i].varp)
6713 result += add_cmd_modifier(buf, mod_entries[i].name,
6714 &multi_mods);
6715
6716 /* TODO: How to support :noautocmd? */
6717#ifdef HAVE_SANDBOX
6718 /* TODO: How to support :sandbox?*/
6719#endif
6720 /* :silent */
6721 if (msg_silent > 0)
6722 result += add_cmd_modifier(buf,
6723 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
6724#ifdef FEAT_WINDOWS
6725 /* :tab */
6726 if (cmdmod.tab > 0)
6727 result += add_cmd_modifier(buf, "tab", &multi_mods);
6728 /* :topleft */
6729 if (cmdmod.split & WSP_TOP)
6730 result += add_cmd_modifier(buf, "topleft", &multi_mods);
6731#endif
6732 /* TODO: How to support :unsilent?*/
6733 /* :verbose */
6734 if (p_verbose > 0)
6735 result += add_cmd_modifier(buf, "verbose", &multi_mods);
6736#ifdef FEAT_WINDOWS
6737 /* :vertical */
6738 if (cmdmod.split & WSP_VERT)
6739 result += add_cmd_modifier(buf, "vertical", &multi_mods);
6740#endif
6741 if (quote && buf != NULL)
6742 {
6743 buf += result - 2;
6744 *buf = '"';
6745 }
6746 break;
6747 }
6748
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 case ct_REGISTER:
6750 result = eap->regname ? 1 : 0;
6751 if (quote)
6752 result += 2;
6753 if (buf != NULL)
6754 {
6755 if (quote)
6756 *buf++ = '\'';
6757 if (eap->regname)
6758 *buf++ = eap->regname;
6759 if (quote)
6760 *buf = '\'';
6761 }
6762 break;
6763
6764 case ct_LT:
6765 result = 1;
6766 if (buf != NULL)
6767 *buf = '<';
6768 break;
6769
6770 default:
6771 /* Not recognized: just copy the '<' and return -1. */
6772 result = (size_t)-1;
6773 if (buf != NULL)
6774 *buf = '<';
6775 break;
6776 }
6777
6778 return result;
6779}
6780
6781 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006782do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783{
6784 char_u *buf;
6785 char_u *p;
6786 char_u *q;
6787
6788 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006789 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006790 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 size_t len, totlen;
6792
6793 size_t split_len = 0;
6794 char_u *split_buf = NULL;
6795 ucmd_T *cmd;
6796#ifdef FEAT_EVAL
6797 scid_T save_current_SID = current_SID;
6798#endif
6799
6800 if (eap->cmdidx == CMD_USER)
6801 cmd = USER_CMD(eap->useridx);
6802 else
6803 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6804
6805 /*
6806 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006807 * First round: "buf" is NULL, compute length, allocate "buf".
6808 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 */
6810 buf = NULL;
6811 for (;;)
6812 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006813 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006814 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006816
6817 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006819 start = vim_strchr(p, '<');
6820 if (start != NULL)
6821 end = vim_strchr(start + 1, '>');
6822 if (buf != NULL)
6823 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006824 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6825 ;
6826 if (*ksp == K_SPECIAL
6827 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006828 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6829# ifdef FEAT_GUI
6830 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6831# endif
6832 ))
6833 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006834 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006835 * KS_SPECIAL KE_FILLER, like for mappings, but
6836 * do_cmdline() doesn't handle that, so convert it back.
6837 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6838 len = ksp - p;
6839 if (len > 0)
6840 {
6841 mch_memmove(q, p, len);
6842 q += len;
6843 }
6844 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6845 p = ksp + 3;
6846 continue;
6847 }
6848 }
6849
6850 /* break if there no <item> is found */
6851 if (start == NULL || end == NULL)
6852 break;
6853
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 /* Include the '>' */
6855 ++end;
6856
6857 /* Take everything up to the '<' */
6858 len = start - p;
6859 if (buf == NULL)
6860 totlen += len;
6861 else
6862 {
6863 mch_memmove(q, p, len);
6864 q += len;
6865 }
6866
6867 len = uc_check_code(start, end - start, q, cmd, eap,
6868 &split_buf, &split_len);
6869 if (len == (size_t)-1)
6870 {
6871 /* no match, continue after '<' */
6872 p = start + 1;
6873 len = 1;
6874 }
6875 else
6876 p = end;
6877 if (buf == NULL)
6878 totlen += len;
6879 else
6880 q += len;
6881 }
6882 if (buf != NULL) /* second time here, finished */
6883 {
6884 STRCPY(q, p);
6885 break;
6886 }
6887
6888 totlen += STRLEN(p); /* Add on the trailing characters */
6889 buf = alloc((unsigned)(totlen + 1));
6890 if (buf == NULL)
6891 {
6892 vim_free(split_buf);
6893 return;
6894 }
6895 }
6896
6897#ifdef FEAT_EVAL
6898 current_SID = cmd->uc_scriptID;
6899#endif
6900 (void)do_cmdline(buf, eap->getline, eap->cookie,
6901 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6902#ifdef FEAT_EVAL
6903 current_SID = save_current_SID;
6904#endif
6905 vim_free(buf);
6906 vim_free(split_buf);
6907}
6908
6909# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6910 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006911get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912{
6913 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6914}
6915
6916/*
6917 * Function given to ExpandGeneric() to obtain the list of user command names.
6918 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006920get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921{
6922 if (idx < curbuf->b_ucmds.ga_len)
6923 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6924 idx -= curbuf->b_ucmds.ga_len;
6925 if (idx < ucmds.ga_len)
6926 return USER_CMD(idx)->uc_name;
6927 return NULL;
6928}
6929
6930/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006931 * Function given to ExpandGeneric() to obtain the list of user address type names.
6932 */
6933 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006934get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006935{
6936 return (char_u *)addr_type_complete[idx].name;
6937}
6938
6939/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 * Function given to ExpandGeneric() to obtain the list of user command
6941 * attributes.
6942 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006944get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945{
6946 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006947 {"addr", "bang", "bar", "buffer", "complete",
6948 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006950 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 return NULL;
6952 return (char_u *)user_cmd_flags[idx];
6953}
6954
6955/*
6956 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6957 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006959get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960{
6961 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6962
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006963 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 return NULL;
6965 return (char_u *)user_cmd_nargs[idx];
6966}
6967
6968/*
6969 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6970 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006972get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973{
6974 return (char_u *)command_complete[idx].name;
6975}
6976# endif /* FEAT_CMDL_COMPL */
6977
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006978/*
6979 * Parse address type argument
6980 */
6981 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006982parse_addr_type_arg(
6983 char_u *value,
6984 int vallen,
6985 long *argt,
6986 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006987{
6988 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01006989
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006990 for (i = 0; addr_type_complete[i].expand != -1; ++i)
6991 {
6992 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
6993 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
6994 if (a && b)
6995 {
6996 *addr_type_arg = addr_type_complete[i].expand;
6997 break;
6998 }
6999 }
7000
7001 if (addr_type_complete[i].expand == -1)
7002 {
7003 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007004
7005 for (i = 0; err[i] != NUL && !vim_iswhite(err[i]); i++)
7006 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007007 err[i] = NUL;
7008 EMSG2(_("E180: Invalid address type value: %s"), err);
7009 return FAIL;
7010 }
7011
7012 if (*addr_type_arg != ADDR_LINES)
7013 *argt |= NOTADR;
7014
7015 return OK;
7016}
7017
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018#endif /* FEAT_USR_CMDS */
7019
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007020#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7021/*
7022 * Parse a completion argument "value[vallen]".
7023 * The detected completion goes in "*complp", argument type in "*argt".
7024 * When there is an argument, for function and user defined completion, it's
7025 * copied to allocated memory and stored in "*compl_arg".
7026 * Returns FAIL if something is wrong.
7027 */
7028 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007029parse_compl_arg(
7030 char_u *value,
7031 int vallen,
7032 int *complp,
7033 long *argt,
7034 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007035{
7036 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007037# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007038 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007039# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007040 int i;
7041 int valend = vallen;
7042
7043 /* Look for any argument part - which is the part after any ',' */
7044 for (i = 0; i < vallen; ++i)
7045 {
7046 if (value[i] == ',')
7047 {
7048 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007049# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007050 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007051# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007052 valend = i;
7053 break;
7054 }
7055 }
7056
7057 for (i = 0; command_complete[i].expand != 0; ++i)
7058 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007059 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007060 && STRNCMP(value, command_complete[i].name, valend) == 0)
7061 {
7062 *complp = command_complete[i].expand;
7063 if (command_complete[i].expand == EXPAND_BUFFERS)
7064 *argt |= BUFNAME;
7065 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7066 || command_complete[i].expand == EXPAND_FILES)
7067 *argt |= XFILE;
7068 break;
7069 }
7070 }
7071
7072 if (command_complete[i].expand == 0)
7073 {
7074 EMSG2(_("E180: Invalid complete value: %s"), value);
7075 return FAIL;
7076 }
7077
7078# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7079 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7080 && arg != NULL)
7081# else
7082 if (arg != NULL)
7083# endif
7084 {
7085 EMSG(_("E468: Completion argument only allowed for custom completion"));
7086 return FAIL;
7087 }
7088
7089# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7090 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7091 && arg == NULL)
7092 {
7093 EMSG(_("E467: Custom completion requires a function argument"));
7094 return FAIL;
7095 }
7096
7097 if (arg != NULL)
7098 *compl_arg = vim_strnsave(arg, (int)arglen);
7099# endif
7100 return OK;
7101}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007102
7103 int
7104cmdcomplete_str_to_type(char_u *complete_str)
7105{
7106 int i;
7107
7108 for (i = 0; command_complete[i].expand != 0; ++i)
7109 if (STRCMP(complete_str, command_complete[i].name) == 0)
7110 return command_complete[i].expand;
7111
7112 return EXPAND_NOTHING;
7113}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007114#endif
7115
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007117ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118{
Bram Moolenaare6850792010-05-14 15:28:44 +02007119 if (*eap->arg == NUL)
7120 {
7121#ifdef FEAT_EVAL
7122 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7123 char_u *p = NULL;
7124
7125 if (expr != NULL)
7126 {
7127 ++emsg_off;
7128 p = eval_to_string(expr, NULL, FALSE);
7129 --emsg_off;
7130 vim_free(expr);
7131 }
7132 if (p != NULL)
7133 {
7134 MSG(p);
7135 vim_free(p);
7136 }
7137 else
7138 MSG("default");
7139#else
7140 MSG(_("unknown"));
7141#endif
7142 }
7143 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007144 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145}
7146
7147 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007148ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149{
7150 if (*eap->arg == NUL && eap->cmd[2] == '!')
7151 MSG(_("Greetings, Vim user!"));
7152 do_highlight(eap->arg, eap->forceit, FALSE);
7153}
7154
7155
7156/*
7157 * Call this function if we thought we were going to exit, but we won't
7158 * (because of an error). May need to restore the terminal mode.
7159 */
7160 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007161not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162{
7163 exiting = FALSE;
7164 settmode(TMODE_RAW);
7165}
7166
7167/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007168 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 */
7170 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007171ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172{
Bram Moolenaarf240e182014-11-27 18:33:02 +01007173#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007174 win_T *wp;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007175#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007176
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177#ifdef FEAT_CMDWIN
7178 if (cmdwin_type != 0)
7179 {
7180 cmdwin_result = Ctrl_C;
7181 return;
7182 }
7183#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007184 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007185 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007186 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007187 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007188 return;
7189 }
Bram Moolenaarf240e182014-11-27 18:33:02 +01007190#ifdef FEAT_WINDOWS
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007191 if (eap->addr_count > 0)
7192 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007193 int wnr = eap->line2;
7194
7195 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7196 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007197 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007198 }
7199 else
Bram Moolenaarf240e182014-11-27 18:33:02 +01007200#endif
7201#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007202 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007203#endif
7204
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007205#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02007206 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007207 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02007208 * being closed (can only happen in autocommands). */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007209 if (curbuf_locked() || (wp->w_buffer->b_nwindows == 1
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007210 && wp->w_buffer->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007211 return;
7212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213
7214#ifdef FEAT_NETBEANS_INTG
7215 netbeansForcedQuit = eap->forceit;
7216#endif
7217
7218 /*
7219 * If there are more files or windows we won't exit.
7220 */
7221 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7222 exiting = TRUE;
7223 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007224 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7225 | (eap->forceit ? CCGD_FORCEIT : 0)
7226 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007228 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 {
7230 not_exiting();
7231 }
7232 else
7233 {
7234#ifdef FEAT_WINDOWS
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007235 /* quit last window
7236 * Note: only_one_window() returns true, even so a help window is
7237 * still open. In that case only quit, if no address has been
7238 * specified. Example:
7239 * :h|wincmd w|1q - don't quit
7240 * :h|wincmd w|q - quit
7241 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007242 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243#endif
7244 getout(0);
7245#ifdef FEAT_WINDOWS
7246# ifdef FEAT_GUI
7247 need_mouse_correct = TRUE;
7248# endif
7249 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007250 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251#endif
7252 }
7253}
7254
7255/*
7256 * ":cquit".
7257 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007259ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260{
7261 getout(1); /* this does not always pass on the exit code to the Manx
7262 compiler. why? */
7263}
7264
7265/*
7266 * ":qall": try to quit all windows
7267 */
7268 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007269ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270{
7271# ifdef FEAT_CMDWIN
7272 if (cmdwin_type != 0)
7273 {
7274 if (eap->forceit)
7275 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7276 else
7277 cmdwin_result = K_XF2;
7278 return;
7279 }
7280# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007281
7282 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007283 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007284 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007285 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007286 return;
7287 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007288#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007289 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7290 /* Refuse to quit when locked or when the buffer in the last window is
7291 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007292 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007293 return;
7294#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007295
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007297 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 getout(0);
7299 not_exiting();
7300}
7301
Bram Moolenaard9967712006-03-11 21:18:15 +00007302#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303/*
7304 * ":close": close current window, unless it is the last one
7305 */
7306 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007307ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007309 win_T *win;
7310 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311# ifdef FEAT_CMDWIN
7312 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007313 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 else
7315# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007316 if (!text_locked()
7317#ifdef FEAT_AUTOCMD
7318 && !curbuf_locked()
7319#endif
7320 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007321 {
7322 if (eap->addr_count == 0)
7323 ex_win_close(eap->forceit, curwin, NULL);
7324 else {
Bram Moolenaar29323592016-07-24 22:04:11 +02007325 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007326 {
7327 winnr++;
7328 if (winnr == eap->line2)
7329 break;
7330 }
7331 if (win == NULL)
7332 win = lastwin;
7333 ex_win_close(eap->forceit, win, NULL);
7334 }
7335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336}
7337
Bram Moolenaard9967712006-03-11 21:18:15 +00007338# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007339/*
7340 * ":pclose": Close any preview window.
7341 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007343ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007344{
7345 win_T *win;
7346
Bram Moolenaar29323592016-07-24 22:04:11 +02007347 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007348 if (win->w_p_pvw)
7349 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007350 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007351 break;
7352 }
7353}
Bram Moolenaard9967712006-03-11 21:18:15 +00007354# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007355
Bram Moolenaarf740b292006-02-16 22:11:02 +00007356/*
7357 * Close window "win" and take care of handling closing the last window for a
7358 * modified buffer.
7359 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007360 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007361ex_win_close(
7362 int forceit,
7363 win_T *win,
7364 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365{
7366 int need_hide;
7367 buf_T *buf = win->w_buffer;
7368
7369 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007370 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007372# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 if ((p_confirm || cmdmod.confirm) && p_write)
7374 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007375 bufref_T bufref;
7376
7377 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007379 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 return;
7381 need_hide = FALSE;
7382 }
7383 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007384# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 {
7386 EMSG(_(e_nowrtmsg));
7387 return;
7388 }
7389 }
7390
Bram Moolenaard9967712006-03-11 21:18:15 +00007391# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007393# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007394
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007396 if (tp == NULL)
7397 win_close(win, !need_hide && !P_HID(buf));
7398 else
7399 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400}
7401
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007403 * ":tabclose": close current tab page, unless it is the last one.
7404 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 */
7406 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007407ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007409 tabpage_T *tp;
7410
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007411# ifdef FEAT_CMDWIN
7412 if (cmdwin_type != 0)
7413 cmdwin_result = K_IGNORE;
7414 else
7415# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007416 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007417 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007418 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007420 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007421 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007422 tp = find_tabpage((int)eap->line2);
7423 if (tp == NULL)
7424 {
7425 beep_flush();
7426 return;
7427 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007428 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007429 {
7430 tabpage_close_other(tp, eap->forceit);
7431 return;
7432 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007433 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007434 if (!text_locked()
7435#ifdef FEAT_AUTOCMD
7436 && !curbuf_locked()
7437#endif
7438 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00007439 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007441}
7442
7443/*
7444 * ":tabonly": close all tab pages except the current one
7445 */
7446 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007447ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007448{
7449 tabpage_T *tp;
7450 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007451
7452# ifdef FEAT_CMDWIN
7453 if (cmdwin_type != 0)
7454 cmdwin_result = K_IGNORE;
7455 else
7456# endif
7457 if (first_tabpage->tp_next == NULL)
7458 MSG(_("Already only one tab page"));
7459 else
7460 {
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007461 if (eap->addr_count > 0)
7462 goto_tabpage(eap->line2);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007463 /* Repeat this up to a 1000 times, because autocommands may mess
7464 * up the lists. */
7465 for (done = 0; done < 1000; ++done)
7466 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007467 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007468 if (tp->tp_topframe != topframe)
7469 {
7470 tabpage_close_other(tp, eap->forceit);
7471 /* if we failed to close it quit */
7472 if (valid_tabpage(tp))
7473 done = 1000;
7474 /* start over, "tp" is now invalid */
7475 break;
7476 }
7477 if (first_tabpage->tp_next == NULL)
7478 break;
7479 }
7480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482
7483/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007484 * Close the current tab page.
7485 */
7486 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007487tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007488{
7489 /* First close all the windows but the current one. If that worked then
7490 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007491 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007492 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007493 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007494 ex_win_close(forceit, curwin, NULL);
7495# ifdef FEAT_GUI
7496 need_mouse_correct = TRUE;
7497# endif
7498}
7499
7500/*
7501 * Close tab page "tp", which is not the current tab page.
7502 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007503 * Also takes care of the tab pages line disappearing when closing the
7504 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007505 */
7506 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007507tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007508{
7509 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007510 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007511 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007512
7513 /* Limit to 1000 windows, autocommands may add a window while we close
7514 * one. OK, so I'm paranoid... */
7515 while (++done < 1000)
7516 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007517 wp = tp->tp_firstwin;
7518 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007519
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007520 /* Autocommands may delete the tab page under our fingers and we may
7521 * fail to close a window with a modified buffer. */
7522 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007523 break;
7524 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007525
Bram Moolenaar29323592016-07-24 22:04:11 +02007526#ifdef FEAT_AUTOCMD
7527 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
7528#endif
7529
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007530 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007531 if (h != tabline_height())
7532 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007533}
7534
7535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 * ":only".
7537 */
7538 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007539ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007541 win_T *wp;
7542 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543# ifdef FEAT_GUI
7544 need_mouse_correct = TRUE;
7545# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007546 if (eap->addr_count > 0)
7547 {
7548 wnr = eap->line2;
7549 for (wp = firstwin; --wnr > 0; )
7550 {
7551 if (wp->w_next == NULL)
7552 break;
7553 else
7554 wp = wp->w_next;
7555 }
7556 win_goto(wp);
7557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 close_others(TRUE, eap->forceit);
7559}
7560
7561/*
7562 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007563 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007565 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007566ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567{
7568 if (eap->addr_count == 0)
7569 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007570 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571}
7572#endif /* FEAT_WINDOWS */
7573
7574 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007575ex_hide(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007577 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578#ifdef FEAT_WINDOWS
Bram Moolenaar2256c992016-11-15 21:17:07 +01007579 if (!eap->skip)
7580 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581# ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007582 need_mouse_correct = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583# endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007584 if (eap->addr_count == 0)
7585 win_close(curwin, FALSE); /* don't free buffer */
7586 else
7587 {
7588 int winnr = 0;
7589 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007590
Bram Moolenaar2256c992016-11-15 21:17:07 +01007591 FOR_ALL_WINDOWS(win)
7592 {
7593 winnr++;
7594 if (winnr == eap->line2)
7595 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007596 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007597 if (win == NULL)
7598 win = lastwin;
7599 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007602#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603}
7604
7605/*
7606 * ":stop" and ":suspend": Suspend Vim.
7607 */
7608 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007609ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610{
7611 /*
7612 * Disallow suspending for "rvim".
7613 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007614 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 {
7616 if (!eap->forceit)
7617 autowrite_all();
7618 windgoto((int)Rows - 1, 0);
7619 out_char('\n');
7620 out_flush();
7621 stoptermcap();
7622 out_flush(); /* needed for SUN to restore xterm buffer */
7623#ifdef FEAT_TITLE
7624 mch_restore_title(3); /* restore window titles */
7625#endif
7626 ui_suspend(); /* call machine specific function */
7627#ifdef FEAT_TITLE
7628 maketitle();
7629 resettitle(); /* force updating the title */
7630#endif
7631 starttermcap();
7632 scroll_start(); /* scroll screen before redrawing */
7633 redraw_later_clear();
7634 shell_resized(); /* may have resized window */
7635 }
7636}
7637
7638/*
7639 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7640 */
7641 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007642ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643{
7644#ifdef FEAT_CMDWIN
7645 if (cmdwin_type != 0)
7646 {
7647 cmdwin_result = Ctrl_C;
7648 return;
7649 }
7650#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007651 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007652 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007653 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007654 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007655 return;
7656 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007657#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007658 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7659 /* Refuse to quit when locked or when the buffer in the last window is
7660 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007661 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007662 return;
7663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664
7665 /*
7666 * if more files or windows we won't exit
7667 */
7668 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7669 exiting = TRUE;
7670 if ( ((eap->cmdidx == CMD_wq
7671 || curbufIsChanged())
7672 && do_write(eap) == FAIL)
7673 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007674 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 {
7676 not_exiting();
7677 }
7678 else
7679 {
7680#ifdef FEAT_WINDOWS
7681 if (only_one_window()) /* quit last window, exit Vim */
7682#endif
7683 getout(0);
7684#ifdef FEAT_WINDOWS
7685# ifdef FEAT_GUI
7686 need_mouse_correct = TRUE;
7687# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007688 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 win_close(curwin, !P_HID(curwin->w_buffer));
7690#endif
7691 }
7692}
7693
7694/*
7695 * ":print", ":list", ":number".
7696 */
7697 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007698ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007700 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7701 EMSG(_(e_emptybuf));
7702 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007704 for ( ;!got_int; ui_breakcheck())
7705 {
7706 print_line(eap->line1,
7707 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7708 || (eap->flags & EXFLAG_NR)),
7709 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7710 if (++eap->line1 > eap->line2)
7711 break;
7712 out_flush(); /* show one line at a time */
7713 }
7714 setpcmark();
7715 /* put cursor at last line */
7716 curwin->w_cursor.lnum = eap->line2;
7717 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 }
7719
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721}
7722
7723#ifdef FEAT_BYTEOFF
7724 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007725ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726{
7727 goto_byte(eap->line2);
7728}
7729#endif
7730
7731/*
7732 * ":shell".
7733 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007735ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736{
7737 do_shell(NULL, 0);
7738}
7739
7740#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7741 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007742 || defined(FEAT_GUI_MSWIN) \
7743 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 || defined(PROTO)
7745
7746/*
7747 * Handle a file drop. The code is here because a drop is *nearly* like an
7748 * :args command, but not quite (we have a list of exact filenames, so we
7749 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7750 * code is very similar to :args and hence needs access to a lot of the static
7751 * functions in this file.
7752 *
7753 * The list should be allocated using alloc(), as should each item in the
7754 * list. This function takes over responsibility for freeing the list.
7755 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007756 * XXX The list is made into the argument list. This is freed using
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007757 * FreeWild(), which does a series of vim_free() calls.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 */
7759 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007760handle_drop(
7761 int filec, /* the number of files dropped */
7762 char_u **filev, /* the list of files dropped */
7763 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764{
7765 exarg_T ea;
7766 int save_msg_scroll = msg_scroll;
7767
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007768 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007769 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007771#ifdef FEAT_AUTOCMD
7772 if (curbuf_locked())
7773 return;
7774#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007775 /* When the screen is being updated we should not change buffers and
7776 * windows structures, it may cause freed memory to be used. */
7777 if (updating_screen)
7778 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779
7780 /* Check whether the current buffer is changed. If so, we will need
7781 * to split the current window or data could be lost.
7782 * We don't need to check if the 'hidden' option is set, as in this
7783 * case the buffer won't be lost.
7784 */
7785 if (!P_HID(curbuf) && !split)
7786 {
7787 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007788 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 --emsg_off;
7790 }
7791 if (split)
7792 {
7793# ifdef FEAT_WINDOWS
7794 if (win_split(0, 0) == FAIL)
7795 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007796 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797
7798 /* When splitting the window, create a new alist. Otherwise the
7799 * existing one is overwritten. */
7800 alist_unlink(curwin->w_alist);
7801 alist_new();
7802# else
7803 return; /* can't split, always fail */
7804# endif
7805 }
7806
7807 /*
7808 * Set up the new argument list.
7809 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007810 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811
7812 /*
7813 * Move to the first file.
7814 */
7815 /* Fake up a minimal "next" command for do_argfile() */
7816 vim_memset(&ea, 0, sizeof(ea));
7817 ea.cmd = (char_u *)"next";
7818 do_argfile(&ea, 0);
7819
7820 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7821 * mode that is not needed here. */
7822 need_start_insertmode = FALSE;
7823
7824 /* Restore msg_scroll, otherwise a following command may cause scrolling
7825 * unexpectedly. The screen will be redrawn by the caller, thus
7826 * msg_scroll being set by displaying a message is irrelevant. */
7827 msg_scroll = save_msg_scroll;
7828}
7829#endif
7830
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831/*
7832 * Clear an argument list: free all file names and reset it to zero entries.
7833 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007834 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007835alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836{
7837 while (--al->al_ga.ga_len >= 0)
7838 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7839 ga_clear(&al->al_ga);
7840}
7841
7842/*
7843 * Init an argument list.
7844 */
7845 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007846alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847{
7848 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7849}
7850
7851#if defined(FEAT_WINDOWS) || defined(PROTO)
7852
7853/*
7854 * Remove a reference from an argument list.
7855 * Ignored when the argument list is the global one.
7856 * If the argument list is no longer used by any window, free it.
7857 */
7858 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007859alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860{
7861 if (al != &global_alist && --al->al_refcount <= 0)
7862 {
7863 alist_clear(al);
7864 vim_free(al);
7865 }
7866}
7867
7868# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7869/*
7870 * Create a new argument list and use it for the current window.
7871 */
7872 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007873alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874{
7875 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7876 if (curwin->w_alist == NULL)
7877 {
7878 curwin->w_alist = &global_alist;
7879 ++global_alist.al_refcount;
7880 }
7881 else
7882 {
7883 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007884 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 alist_init(curwin->w_alist);
7886 }
7887}
7888# endif
7889#endif
7890
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007891#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892/*
7893 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007894 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7895 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 */
7897 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007898alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899{
7900 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007901 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 char_u **new_arg_files;
7903 int new_arg_file_count;
7904 char_u *save_p_su = p_su;
7905 int i;
7906
7907 /* Don't use 'suffixes' here. This should work like the shell did the
7908 * expansion. Also, the vimrc file isn't read yet, thus the user
7909 * can't set the options. */
7910 p_su = empty_option;
7911 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7912 if (old_arg_files != NULL)
7913 {
7914 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007915 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7916 old_arg_count = GARGCOUNT;
7917 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007919 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 && new_arg_file_count > 0)
7921 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007922 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7923 TRUE, fnum_list, fnum_len);
7924 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 }
7927 p_su = save_p_su;
7928}
7929#endif
7930
7931/*
7932 * Set the argument list for the current window.
7933 * Takes over the allocated files[] and the allocated fnames in it.
7934 */
7935 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007936alist_set(
7937 alist_T *al,
7938 int count,
7939 char_u **files,
7940 int use_curbuf,
7941 int *fnum_list,
7942 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943{
7944 int i;
7945
7946 alist_clear(al);
7947 if (ga_grow(&al->al_ga, count) == OK)
7948 {
7949 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007950 {
7951 if (got_int)
7952 {
7953 /* When adding many buffers this can take a long time. Allow
7954 * interrupting here. */
7955 while (i < count)
7956 vim_free(files[i++]);
7957 break;
7958 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007959
7960 /* May set buffer name of a buffer previously used for the
7961 * argument list, so that it's re-used by alist_add. */
7962 if (fnum_list != NULL && i < fnum_len)
7963 buf_set_name(fnum_list[i], files[i]);
7964
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007966 ui_breakcheck();
7967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 vim_free(files);
7969 }
7970 else
7971 FreeWild(count, files);
7972#ifdef FEAT_WINDOWS
7973 if (al == &global_alist)
7974#endif
7975 arg_had_last = FALSE;
7976}
7977
7978/*
7979 * Add file "fname" to argument list "al".
7980 * "fname" must have been allocated and "al" must have been checked for room.
7981 */
7982 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007983alist_add(
7984 alist_T *al,
7985 char_u *fname,
7986 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987{
7988 if (fname == NULL) /* don't add NULL file names */
7989 return;
7990#ifdef BACKSLASH_IN_FILENAME
7991 slash_adjust(fname);
7992#endif
7993 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7994 if (set_fnum > 0)
7995 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7996 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7997 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998}
7999
8000#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8001/*
8002 * Adjust slashes in file names. Called after 'shellslash' was set.
8003 */
8004 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008005alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006{
8007 int i;
8008# ifdef FEAT_WINDOWS
8009 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008010 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011# endif
8012
8013 for (i = 0; i < GARGCOUNT; ++i)
8014 if (GARGLIST[i].ae_fname != NULL)
8015 slash_adjust(GARGLIST[i].ae_fname);
8016# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00008017 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 if (wp->w_alist != &global_alist)
8019 for (i = 0; i < WARGCOUNT(wp); ++i)
8020 if (WARGLIST(wp)[i].ae_fname != NULL)
8021 slash_adjust(WARGLIST(wp)[i].ae_fname);
8022# endif
8023}
8024#endif
8025
8026/*
8027 * ":preserve".
8028 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008030ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008032 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 ml_preserve(curbuf, TRUE);
8034}
8035
8036/*
8037 * ":recover".
8038 */
8039 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008040ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041{
8042 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8043 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008044 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8045 | CCGD_MULTWIN
8046 | (eap->forceit ? CCGD_FORCEIT : 0)
8047 | CCGD_EXCMD)
8048
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 && (*eap->arg == NUL
8050 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8051 ml_recover();
8052 recoverymode = FALSE;
8053}
8054
8055/*
8056 * Command modifier used in a wrong way.
8057 */
8058 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008059ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060{
8061 eap->errmsg = e_invcmd;
8062}
8063
8064#ifdef FEAT_WINDOWS
8065/*
8066 * :sview [+command] file split window with new file, read-only
8067 * :split [[+command] file] split window with current or new file
8068 * :vsplit [[+command] file] split window vertically with current or new file
8069 * :new [[+command] file] split window with no or new file
8070 * :vnew [[+command] file] split vertically window with no or new file
8071 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008072 *
8073 * :tabedit open new Tab page with empty window
8074 * :tabedit [+command] file open new Tab page and edit "file"
8075 * :tabnew [[+command] file] just like :tabedit
8076 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 */
8078 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008079ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008081 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008082# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008084# endif
8085# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008087# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008089# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008091# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008093# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008095 * quickfix windows. But it's OK when doing ":tab split". */
8096 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 {
8098 if (eap->cmdidx == CMD_split)
8099 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 if (eap->cmdidx == CMD_vsplit)
8101 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008103# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008105# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008106 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 {
8108 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8109 FNAME_MESS, TRUE, curbuf->b_ffname);
8110 if (fname == NULL)
8111 goto theend;
8112 eap->arg = fname;
8113 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008114# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008116# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008118# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 && eap->cmdidx != CMD_new)
8122 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008123# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008124 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008125# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008126 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008127# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008128 au_has_group((char_u *)"FileExplorer"))
8129 {
8130 /* No browsing supported but we do have the file explorer:
8131 * Edit the directory. */
8132 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8133 eap->arg = (char_u *)".";
8134 }
8135 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008136# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008137 {
8138 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008140 if (fname == NULL)
8141 goto theend;
8142 eap->arg = fname;
8143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 }
8145 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008146# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008148 /*
8149 * Either open new tab page or split the window.
8150 */
8151 if (eap->cmdidx == CMD_tabedit
8152 || eap->cmdidx == CMD_tabfind
8153 || eap->cmdidx == CMD_tabnew)
8154 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008155 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8156 : eap->addr_count == 0 ? 0
8157 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008158 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008159 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008160
8161 /* set the alternate buffer for the window we came from */
8162 if (curwin != old_curwin
8163 && win_valid(old_curwin)
8164 && old_curwin->w_buffer != curbuf
8165 && !cmdmod.keepalt)
8166 old_curwin->w_alt_fnum = curbuf->b_fnum;
8167 }
8168 }
8169 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8171 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008172# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 /* Reset 'scrollbind' when editing another file, but keep it when
8174 * doing ":split" without arguments. */
8175 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008176# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008178# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008180 {
8181 RESET_BINDING(curwin);
8182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 else
8184 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008185# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 do_exedit(eap, old_curwin);
8187 }
8188
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008189# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008191# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008193# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194theend:
8195 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008196# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008198
8199/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008200 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008201 */
8202 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008203tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008204{
8205 exarg_T ea;
8206
8207 vim_memset(&ea, 0, sizeof(ea));
8208 ea.cmdidx = CMD_tabnew;
8209 ea.cmd = (char_u *)"tabn";
8210 ea.arg = (char_u *)"";
8211 ex_splitview(&ea);
8212}
8213
8214/*
8215 * :tabnext command
8216 */
8217 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008218ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008219{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008220 switch (eap->cmdidx)
8221 {
8222 case CMD_tabfirst:
8223 case CMD_tabrewind:
8224 goto_tabpage(1);
8225 break;
8226 case CMD_tablast:
8227 goto_tabpage(9999);
8228 break;
8229 case CMD_tabprevious:
8230 case CMD_tabNext:
8231 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
8232 break;
8233 default: /* CMD_tabnext */
8234 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
8235 break;
8236 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008237}
8238
8239/*
8240 * :tabmove command
8241 */
8242 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008243ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008244{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008245 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008246
8247 if (eap->arg && *eap->arg != NUL)
8248 {
8249 char_u *p = eap->arg;
8250 int relative = 0; /* argument +N/-N means: move N places to the
8251 * right/left relative to the current position. */
8252
8253 if (*eap->arg == '-')
8254 {
8255 relative = -1;
8256 p = eap->arg + 1;
8257 }
8258 else if (*eap->arg == '+')
8259 {
8260 relative = 1;
8261 p = eap->arg + 1;
8262 }
8263 else
8264 p = eap->arg;
8265
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008266 if (relative == 0)
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008267 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008268 if (STRCMP(p, "$") == 0)
8269 tab_number = LAST_TAB_NR;
8270 else if (p == skipdigits(p))
8271 {
8272 /* No numbers as argument. */
8273 eap->errmsg = e_invarg;
8274 return;
8275 }
8276 else
8277 tab_number = getdigits(&p);
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008278 }
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008279 else
8280 {
8281 if (*p != NUL)
8282 tab_number = getdigits(&p);
8283 else
8284 tab_number = 1;
8285 tab_number = tab_number * relative + tabpage_index(curtab);
8286 if (relative == -1)
8287 --tab_number;
8288 }
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008289 }
8290 else if (eap->addr_count != 0)
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008291 {
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008292 tab_number = eap->line2;
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008293 if (**eap->cmdlinep == '-')
8294 --tab_number;
8295 }
8296 else
8297 tab_number = LAST_TAB_NR;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008298
8299 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008300}
8301
8302/*
8303 * :tabs command: List tabs and their contents.
8304 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008305 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008306ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008307{
8308 tabpage_T *tp;
8309 win_T *wp;
8310 int tabcount = 1;
8311
8312 msg_start();
8313 msg_scroll = TRUE;
8314 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8315 {
8316 msg_putchar('\n');
8317 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
8318 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
8319 out_flush(); /* output one line at a time */
8320 ui_breakcheck();
8321
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008322 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008323 wp = firstwin;
8324 else
8325 wp = tp->tp_firstwin;
8326 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8327 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008328 msg_putchar('\n');
8329 msg_putchar(wp == curwin ? '>' : ' ');
8330 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008331 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8332 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008333 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008334 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008335 else
8336 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8337 IObuff, IOSIZE, TRUE);
8338 msg_outtrans(IObuff);
8339 out_flush(); /* output one line at a time */
8340 ui_breakcheck();
8341 }
8342 }
8343}
8344
8345#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346
8347/*
8348 * ":mode": Set screen mode.
8349 * If no argument given, just get the screen size and redraw.
8350 */
8351 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008352ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353{
8354 if (*eap->arg == NUL)
8355 shell_resized();
8356 else
8357 mch_screenmode(eap->arg);
8358}
8359
8360#ifdef FEAT_WINDOWS
8361/*
8362 * ":resize".
8363 * set, increment or decrement current window height
8364 */
8365 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008366ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367{
8368 int n;
8369 win_T *wp = curwin;
8370
8371 if (eap->addr_count > 0)
8372 {
8373 n = eap->line2;
8374 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8375 ;
8376 }
8377
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008378# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008380# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 if (cmdmod.split & WSP_VERT)
8383 {
8384 if (*eap->arg == '-' || *eap->arg == '+')
8385 n += W_WIDTH(curwin);
8386 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8387 n = 9999;
8388 win_setwidth_win((int)n, wp);
8389 }
8390 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 {
8392 if (*eap->arg == '-' || *eap->arg == '+')
8393 n += curwin->w_height;
8394 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8395 n = 9999;
8396 win_setheight_win((int)n, wp);
8397 }
8398}
8399#endif
8400
8401/*
8402 * ":find [+command] <file>" command.
8403 */
8404 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008405ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406{
8407#ifdef FEAT_SEARCHPATH
8408 char_u *fname;
8409 int count;
8410
8411 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8412 TRUE, curbuf->b_ffname);
8413 if (eap->addr_count > 0)
8414 {
8415 /* Repeat finding the file "count" times. This matters when it
8416 * appears several times in the path. */
8417 count = eap->line2;
8418 while (fname != NULL && --count > 0)
8419 {
8420 vim_free(fname);
8421 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8422 FALSE, curbuf->b_ffname);
8423 }
8424 }
8425
8426 if (fname != NULL)
8427 {
8428 eap->arg = fname;
8429#endif
8430 do_exedit(eap, NULL);
8431#ifdef FEAT_SEARCHPATH
8432 vim_free(fname);
8433 }
8434#endif
8435}
8436
8437/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008438 * ":open" simulation: for now just work like ":visual".
8439 */
8440 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008441ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008442{
8443 regmatch_T regmatch;
8444 char_u *p;
8445
8446 curwin->w_cursor.lnum = eap->line2;
8447 beginline(BL_SOL | BL_FIX);
8448 if (*eap->arg == '/')
8449 {
8450 /* ":open /pattern/": put cursor in column found with pattern */
8451 ++eap->arg;
8452 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8453 *p = NUL;
8454 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8455 if (regmatch.regprog != NULL)
8456 {
8457 regmatch.rm_ic = p_ic;
8458 p = ml_get_curline();
8459 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008460 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008461 else
8462 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008463 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008464 }
8465 /* Move to the NUL, ignore any other arguments. */
8466 eap->arg += STRLEN(eap->arg);
8467 }
8468 check_cursor();
8469
8470 eap->cmdidx = CMD_visual;
8471 do_exedit(eap, NULL);
8472}
8473
8474/*
8475 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 */
8477 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008478ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479{
8480 do_exedit(eap, NULL);
8481}
8482
8483/*
8484 * ":edit <file>" command and alikes.
8485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008487do_exedit(
8488 exarg_T *eap,
8489 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490{
8491 int n;
8492#ifdef FEAT_WINDOWS
8493 int need_hide;
8494#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008495 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496
8497 /*
8498 * ":vi" command ends Ex mode.
8499 */
8500 if (exmode_active && (eap->cmdidx == CMD_visual
8501 || eap->cmdidx == CMD_view))
8502 {
8503 exmode_active = FALSE;
8504 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008505 {
8506 /* Special case: ":global/pat/visual\NLvi-commands" */
8507 if (global_busy)
8508 {
8509 int rd = RedrawingDisabled;
8510 int nwr = no_wait_return;
8511 int ms = msg_scroll;
8512#ifdef FEAT_GUI
8513 int he = hold_gui_events;
8514#endif
8515
8516 if (eap->nextcmd != NULL)
8517 {
8518 stuffReadbuff(eap->nextcmd);
8519 eap->nextcmd = NULL;
8520 }
8521
8522 if (exmode_was != EXMODE_VIM)
8523 settmode(TMODE_RAW);
8524 RedrawingDisabled = 0;
8525 no_wait_return = 0;
8526 need_wait_return = FALSE;
8527 msg_scroll = 0;
8528#ifdef FEAT_GUI
8529 hold_gui_events = 0;
8530#endif
8531 must_redraw = CLEAR;
8532
8533 main_loop(FALSE, TRUE);
8534
8535 RedrawingDisabled = rd;
8536 no_wait_return = nwr;
8537 msg_scroll = ms;
8538#ifdef FEAT_GUI
8539 hold_gui_events = he;
8540#endif
8541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 }
8545
8546 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008547 || eap->cmdidx == CMD_tabnew
8548 || eap->cmdidx == CMD_tabedit
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008549#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 || eap->cmdidx == CMD_vnew
8551#endif
8552 ) && *eap->arg == NUL)
8553 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008554 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 setpcmark();
8556 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008557 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8558 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 }
8560 else if ((eap->cmdidx != CMD_split
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008561#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 && eap->cmdidx != CMD_vsplit
8563#endif
8564 )
8565 || *eap->arg != NUL
8566#ifdef FEAT_BROWSE
8567 || cmdmod.browse
8568#endif
8569 )
8570 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008571#ifdef FEAT_AUTOCMD
8572 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8573 * can bring us here, others are stopped earlier. */
8574 if (*eap->arg != NUL && curbuf_locked())
8575 return;
8576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 n = readonlymode;
8578 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8579 readonlymode = TRUE;
8580 else if (eap->cmdidx == CMD_enew)
8581 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8582 empty buffer */
8583 setpcmark();
8584 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8585 NULL, eap,
8586 /* ":edit" goes to first line if Vi compatible */
8587 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8588 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8589 ? ECMD_ONE : eap->do_ecmd_lnum,
8590 (P_HID(curbuf) ? ECMD_HIDE : 0)
8591 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008592 /* after a split we can use an existing buffer */
8593 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594#ifdef FEAT_LISTCMDS
8595 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8596#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008597 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 {
8599 /* Editing the file failed. If the window was split, close it. */
8600#ifdef FEAT_WINDOWS
8601 if (old_curwin != NULL)
8602 {
8603 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8604 if (!need_hide || P_HID(curbuf))
8605 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008606# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8607 cleanup_T cs;
8608
8609 /* Reset the error/interrupt/exception state here so that
8610 * aborting() returns FALSE when closing a window. */
8611 enter_cleanup(&cs);
8612# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613# ifdef FEAT_GUI
8614 need_mouse_correct = TRUE;
8615# endif
8616 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008617
8618# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8619 /* Restore the error/interrupt/exception state if not
8620 * discarded by a new aborting error, interrupt, or
8621 * uncaught exception. */
8622 leave_cleanup(&cs);
8623# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 }
8625 }
8626#endif
8627 }
8628 else if (readonlymode && curbuf->b_nwindows == 1)
8629 {
8630 /* When editing an already visited buffer, 'readonly' won't be set
8631 * but the previous value is kept. With ":view" and ":sview" we
8632 * want the file to be readonly, except when another window is
8633 * editing the same buffer. */
8634 curbuf->b_p_ro = TRUE;
8635 }
8636 readonlymode = n;
8637 }
8638 else
8639 {
8640 if (eap->do_ecmd_cmd != NULL)
8641 do_cmdline_cmd(eap->do_ecmd_cmd);
8642#ifdef FEAT_TITLE
8643 n = curwin->w_arg_idx_invalid;
8644#endif
8645 check_arg_idx(curwin);
8646#ifdef FEAT_TITLE
8647 if (n != curwin->w_arg_idx_invalid)
8648 maketitle();
8649#endif
8650 }
8651
8652#ifdef FEAT_WINDOWS
8653 /*
8654 * if ":split file" worked, set alternate file name in old window to new
8655 * file
8656 */
8657 if (old_curwin != NULL
8658 && *eap->arg != NUL
8659 && curwin != old_curwin
8660 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008661 && old_curwin->w_buffer != curbuf
8662 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 old_curwin->w_alt_fnum = curbuf->b_fnum;
8664#endif
8665
8666 ex_no_reprint = TRUE;
8667}
8668
8669#ifndef FEAT_GUI
8670/*
8671 * ":gui" and ":gvim" when there is no GUI.
8672 */
8673 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008674ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675{
8676 eap->errmsg = e_nogvim;
8677}
8678#endif
8679
8680#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8681 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008682ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683{
8684 gui_make_tearoff(eap->arg);
8685}
8686#endif
8687
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008688#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008690ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008692 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693}
8694#endif
8695
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008697ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698{
8699 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8700 MSG(_("No swap file"));
8701 else
8702 msg(curbuf->b_ml.ml_mfp->mf_fname);
8703}
8704
8705/*
8706 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8707 * offset.
8708 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8709 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008711ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712{
8713#ifdef FEAT_SCROLLBIND
8714 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008715 win_T *save_curwin = curwin;
8716 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 long topline;
8718 long y;
8719 linenr_T old_linenr = curwin->w_cursor.lnum;
8720
8721 setpcmark();
8722
8723 /*
8724 * determine max topline
8725 */
8726 if (curwin->w_p_scb)
8727 {
8728 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008729 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 {
8731 if (wp->w_p_scb && wp->w_buffer)
8732 {
8733 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8734 if (topline > y)
8735 topline = y;
8736 }
8737 }
8738 if (topline < 1)
8739 topline = 1;
8740 }
8741 else
8742 {
8743 topline = 1;
8744 }
8745
8746
8747 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008748 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008750 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 {
8752 if (curwin->w_p_scb)
8753 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008754 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 y = topline - curwin->w_topline;
8756 if (y > 0)
8757 scrollup(y, TRUE);
8758 else
8759 scrolldown(-y, TRUE);
8760 curwin->w_scbind_pos = topline;
8761 redraw_later(VALID);
8762 cursor_correct();
8763#ifdef FEAT_WINDOWS
8764 curwin->w_redr_status = TRUE;
8765#endif
8766 }
8767 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008768 curwin = save_curwin;
8769 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 if (curwin->w_p_scb)
8771 {
8772 did_syncbind = TRUE;
8773 checkpcmark();
8774 if (old_linenr != curwin->w_cursor.lnum)
8775 {
8776 char_u ctrl_o[2];
8777
8778 ctrl_o[0] = Ctrl_O;
8779 ctrl_o[1] = 0;
8780 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8781 }
8782 }
8783#endif
8784}
8785
8786
8787 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008788ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008790 int i;
8791 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8792 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793
8794 if (eap->usefilter) /* :r!cmd */
8795 do_bang(1, eap, FALSE, FALSE, TRUE);
8796 else
8797 {
8798 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8799 return;
8800
8801#ifdef FEAT_BROWSE
8802 if (cmdmod.browse)
8803 {
8804 char_u *browseFile;
8805
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008806 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 NULL, NULL, NULL, curbuf);
8808 if (browseFile != NULL)
8809 {
8810 i = readfile(browseFile, NULL,
8811 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8812 vim_free(browseFile);
8813 }
8814 else
8815 i = OK;
8816 }
8817 else
8818#endif
8819 if (*eap->arg == NUL)
8820 {
8821 if (check_fname() == FAIL) /* check for no file name */
8822 return;
8823 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8824 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8825 }
8826 else
8827 {
8828 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8829 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8830 i = readfile(eap->arg, NULL,
8831 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8832
8833 }
8834 if (i == FAIL)
8835 {
8836#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8837 if (!aborting())
8838#endif
8839 EMSG2(_(e_notopen), eap->arg);
8840 }
8841 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008842 {
8843 if (empty && exmode_active)
8844 {
8845 /* Delete the empty line that remains. Historically ex does
8846 * this but vi doesn't. */
8847 if (eap->line2 == 0)
8848 lnum = curbuf->b_ml.ml_line_count;
8849 else
8850 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008851 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008852 {
8853 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008854 if (curwin->w_cursor.lnum > 1
8855 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008856 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008857 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008858 }
8859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 }
8863}
8864
Bram Moolenaarea408852005-06-25 22:49:46 +00008865static char_u *prev_dir = NULL;
8866
8867#if defined(EXITFREE) || defined(PROTO)
8868 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008869free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00008870{
8871 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008872 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008873
8874 vim_free(globaldir);
8875 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008876}
8877#endif
8878
Bram Moolenaarf4258302013-06-02 18:20:17 +02008879/*
8880 * Deal with the side effects of changing the current directory.
8881 * When "local" is TRUE then this was after an ":lcd" command.
8882 */
8883 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008884post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008885{
8886 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008887 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008888 if (local)
8889 {
8890 /* If still in global directory, need to remember current
8891 * directory as global directory. */
8892 if (globaldir == NULL && prev_dir != NULL)
8893 globaldir = vim_strsave(prev_dir);
8894 /* Remember this local directory for the window. */
8895 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8896 curwin->w_localdir = vim_strsave(NameBuff);
8897 }
8898 else
8899 {
8900 /* We are now in the global directory, no need to remember its
8901 * name. */
8902 vim_free(globaldir);
8903 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008904 }
8905
8906 shorten_fnames(TRUE);
8907}
8908
Bram Moolenaarea408852005-06-25 22:49:46 +00008909
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910/*
8911 * ":cd", ":lcd", ":chdir" and ":lchdir".
8912 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008913 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008914ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 char_u *new_dir;
8917 char_u *tofree;
8918
8919 new_dir = eap->arg;
8920#if !defined(UNIX) && !defined(VMS)
8921 /* for non-UNIX ":cd" means: print current directory */
8922 if (*new_dir == NUL)
8923 ex_pwd(NULL);
8924 else
8925#endif
8926 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008927#ifdef FEAT_AUTOCMD
8928 if (allbuf_locked())
8929 return;
8930#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008931 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8932 && !eap->forceit)
8933 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008934 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008935 return;
8936 }
8937
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 /* ":cd -": Change to previous directory */
8939 if (STRCMP(new_dir, "-") == 0)
8940 {
8941 if (prev_dir == NULL)
8942 {
8943 EMSG(_("E186: No previous directory"));
8944 return;
8945 }
8946 new_dir = prev_dir;
8947 }
8948
8949 /* Save current directory for next ":cd -" */
8950 tofree = prev_dir;
8951 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8952 prev_dir = vim_strsave(NameBuff);
8953 else
8954 prev_dir = NULL;
8955
8956#if defined(UNIX) || defined(VMS)
8957 /* for UNIX ":cd" means: go to home directory */
8958 if (*new_dir == NUL)
8959 {
8960 /* use NameBuff for home directory name */
8961# ifdef VMS
8962 char_u *p;
8963
8964 p = mch_getenv((char_u *)"SYS$LOGIN");
8965 if (p == NULL || *p == NUL) /* empty is the same as not set */
8966 NameBuff[0] = NUL;
8967 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008968 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969# else
8970 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8971# endif
8972 new_dir = NameBuff;
8973 }
8974#endif
8975 if (new_dir == NULL || vim_chdir(new_dir))
8976 EMSG(_(e_failed));
8977 else
8978 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008979 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980
8981 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008982 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983 ex_pwd(eap);
8984 }
8985 vim_free(tofree);
8986 }
8987}
8988
8989/*
8990 * ":pwd".
8991 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008993ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994{
8995 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8996 {
8997#ifdef BACKSLASH_IN_FILENAME
8998 slash_adjust(NameBuff);
8999#endif
9000 msg(NameBuff);
9001 }
9002 else
9003 EMSG(_("E187: Unknown"));
9004}
9005
9006/*
9007 * ":=".
9008 */
9009 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009010ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011{
9012 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009013 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014}
9015
9016 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009017ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009019 int n;
9020 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021
9022 if (cursor_valid())
9023 {
9024 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9025 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01009026 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009028
9029 len = eap->line2;
9030 switch (*eap->arg)
9031 {
9032 case 'm': break;
9033 case NUL: len *= 1000L; break;
9034 default: EMSG2(_(e_invarg2), eap->arg); return;
9035 }
9036 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037}
9038
9039/*
9040 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9041 */
9042 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009043do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044{
9045 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009046 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047
9048 cursor_on();
9049 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009050 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009052 wait_now = msec - done > 1000L ? 1000L : msec - done;
9053#ifdef FEAT_TIMERS
9054 {
9055 long due_time = check_due_timer();
9056
9057 if (due_time > 0 && due_time < wait_now)
9058 wait_now = due_time;
9059 }
9060#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009061#ifdef FEAT_JOB_CHANNEL
9062 if (has_any_channel() && wait_now > 100L)
9063 wait_now = 100L;
9064#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009065 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009066#ifdef FEAT_JOB_CHANNEL
9067 if (has_any_channel())
9068 ui_breakcheck_force(TRUE);
9069 else
9070#endif
9071 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009072#ifdef MESSAGE_QUEUE
9073 /* Process the netbeans and clientserver messages that may have been
9074 * received in the call to ui_breakcheck() when the GUI is in use. This
9075 * may occur when running a test case. */
9076 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 }
9079}
9080
9081 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009082do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083{
9084 int mode;
9085 char_u *cmdp;
9086
9087 cmdp = eap->cmd;
9088 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9089
9090 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9091 eap->arg, mode, isabbrev))
9092 {
9093 case 1: EMSG(_(e_invarg));
9094 break;
9095 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9096 break;
9097 }
9098}
9099
9100/*
9101 * ":winsize" command (obsolete).
9102 */
9103 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009104ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105{
9106 int w, h;
9107 char_u *arg = eap->arg;
9108 char_u *p;
9109
9110 w = getdigits(&arg);
9111 arg = skipwhite(arg);
9112 p = arg;
9113 h = getdigits(&arg);
9114 if (*p != NUL && *arg == NUL)
9115 set_shellsize(w, h, TRUE);
9116 else
9117 EMSG(_("E465: :winsize requires two number arguments"));
9118}
9119
9120#ifdef FEAT_WINDOWS
9121 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009122ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123{
9124 int xchar = NUL;
9125 char_u *p;
9126
9127 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9128 {
9129 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9130 if (eap->arg[1] == NUL)
9131 {
9132 EMSG(_(e_invarg));
9133 return;
9134 }
9135 xchar = eap->arg[1];
9136 p = eap->arg + 2;
9137 }
9138 else
9139 p = eap->arg + 1;
9140
9141 eap->nextcmd = check_nextcmd(p);
9142 p = skipwhite(p);
9143 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9144 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009145 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146 {
9147 /* Pass flags on for ":vertical wincmd ]". */
9148 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009149 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9151 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009152 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 }
9154}
9155#endif
9156
Bram Moolenaar843ee412004-06-30 16:16:41 +00009157#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158/*
9159 * ":winpos".
9160 */
9161 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009162ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163{
9164 int x, y;
9165 char_u *arg = eap->arg;
9166 char_u *p;
9167
9168 if (*arg == NUL)
9169 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009170# if defined(FEAT_GUI) || defined(MSWIN)
9171# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009173# else
9174 if (mch_get_winpos(&x, &y) != FAIL)
9175# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 {
9177 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9178 msg(IObuff);
9179 }
9180 else
9181# endif
9182 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9183 }
9184 else
9185 {
9186 x = getdigits(&arg);
9187 arg = skipwhite(arg);
9188 p = arg;
9189 y = getdigits(&arg);
9190 if (*p == NUL || *arg != NUL)
9191 {
9192 EMSG(_("E466: :winpos requires two number arguments"));
9193 return;
9194 }
9195# ifdef FEAT_GUI
9196 if (gui.in_use)
9197 gui_mch_set_winpos(x, y);
9198 else if (gui.starting)
9199 {
9200 /* Remember the coordinates for when the window is opened. */
9201 gui_win_x = x;
9202 gui_win_y = y;
9203 }
9204# ifdef HAVE_TGETENT
9205 else
9206# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009207# else
9208# ifdef MSWIN
9209 mch_set_winpos(x, y);
9210# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211# endif
9212# ifdef HAVE_TGETENT
9213 if (*T_CWP)
9214 term_set_winpos(x, y);
9215# endif
9216 }
9217}
9218#endif
9219
9220/*
9221 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9222 */
9223 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009224ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225{
9226 oparg_T oa;
9227
9228 clear_oparg(&oa);
9229 oa.regname = eap->regname;
9230 oa.start.lnum = eap->line1;
9231 oa.end.lnum = eap->line2;
9232 oa.line_count = eap->line2 - eap->line1 + 1;
9233 oa.motion_type = MLINE;
9234#ifdef FEAT_VIRTUALEDIT
9235 virtual_op = FALSE;
9236#endif
9237 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9238 {
9239 setpcmark();
9240 curwin->w_cursor.lnum = eap->line1;
9241 beginline(BL_SOL | BL_FIX);
9242 }
9243
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009244 if (VIsual_active)
9245 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009246
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 switch (eap->cmdidx)
9248 {
9249 case CMD_delete:
9250 oa.op_type = OP_DELETE;
9251 op_delete(&oa);
9252 break;
9253
9254 case CMD_yank:
9255 oa.op_type = OP_YANK;
9256 (void)op_yank(&oa, FALSE, TRUE);
9257 break;
9258
9259 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009260 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009262 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9263#else
9264 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009266 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 oa.op_type = OP_RSHIFT;
9268 else
9269 oa.op_type = OP_LSHIFT;
9270 op_shift(&oa, FALSE, eap->amount);
9271 break;
9272 }
9273#ifdef FEAT_VIRTUALEDIT
9274 virtual_op = MAYBE;
9275#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009276 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277}
9278
9279/*
9280 * ":put".
9281 */
9282 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009283ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284{
9285 /* ":0put" works like ":1put!". */
9286 if (eap->line2 == 0)
9287 {
9288 eap->line2 = 1;
9289 eap->forceit = TRUE;
9290 }
9291 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009292 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9293 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294}
9295
9296/*
9297 * Handle ":copy" and ":move".
9298 */
9299 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009300ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301{
9302 long n;
9303
Bram Moolenaaraa23b372015-09-08 18:46:31 +02009304 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305 if (eap->arg == NULL) /* error detected */
9306 {
9307 eap->nextcmd = NULL;
9308 return;
9309 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009310 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311
9312 /*
9313 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9314 */
9315 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9316 {
9317 EMSG(_(e_invaddr));
9318 return;
9319 }
9320
9321 if (eap->cmdidx == CMD_move)
9322 {
9323 if (do_move(eap->line1, eap->line2, n) == FAIL)
9324 return;
9325 }
9326 else
9327 ex_copy(eap->line1, eap->line2, n);
9328 u_clearline();
9329 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009330 ex_may_print(eap);
9331}
9332
9333/*
9334 * Print the current line if flags were given to the Ex command.
9335 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009336 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009337ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009338{
9339 if (eap->flags != 0)
9340 {
9341 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9342 (eap->flags & EXFLAG_LIST));
9343 ex_no_reprint = TRUE;
9344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345}
9346
9347/*
9348 * ":smagic" and ":snomagic".
9349 */
9350 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009351ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352{
9353 int magic_save = p_magic;
9354
9355 p_magic = (eap->cmdidx == CMD_smagic);
9356 do_sub(eap);
9357 p_magic = magic_save;
9358}
9359
9360/*
9361 * ":join".
9362 */
9363 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009364ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365{
9366 curwin->w_cursor.lnum = eap->line1;
9367 if (eap->line1 == eap->line2)
9368 {
9369 if (eap->addr_count >= 2) /* :2,2join does nothing */
9370 return;
9371 if (eap->line2 == curbuf->b_ml.ml_line_count)
9372 {
9373 beep_flush();
9374 return;
9375 }
9376 ++eap->line2;
9377 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009378 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009380 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381}
9382
9383/*
9384 * ":[addr]@r" or ":[addr]*r": execute register
9385 */
9386 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009387ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388{
9389 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009390 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391
9392 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009393 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394
9395#ifdef USE_ON_FLY_SCROLL
9396 dont_scroll = TRUE; /* disallow scrolling here */
9397#endif
9398
9399 /* get the register name. No name means to use the previous one */
9400 c = *eap->arg;
9401 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9402 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009403 /* Put the register in the typeahead buffer with the "silent" flag. */
9404 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9405 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009406 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 else
9410 {
9411 int save_efr = exec_from_reg;
9412
9413 exec_from_reg = TRUE;
9414
9415 /*
9416 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009417 * Continue until the stuff buffer is empty and all added characters
9418 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009420 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9422
9423 exec_from_reg = save_efr;
9424 }
9425}
9426
9427/*
9428 * ":!".
9429 */
9430 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009431ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432{
9433 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9434}
9435
9436/*
9437 * ":undo".
9438 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009440ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009442 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009443 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009444 else
9445 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446}
9447
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009448#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009449 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009450ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009451{
9452 char_u hash[UNDO_HASH_SIZE];
9453
9454 u_compute_hash(hash);
9455 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9456}
9457
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009458 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009459ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009460{
9461 char_u hash[UNDO_HASH_SIZE];
9462
9463 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009464 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009465}
9466#endif
9467
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468/*
9469 * ":redo".
9470 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009472ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473{
9474 u_redo(1);
9475}
9476
9477/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009478 * ":earlier" and ":later".
9479 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009480 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009481ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009482{
9483 long count = 0;
9484 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009485 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009486 char_u *p = eap->arg;
9487
9488 if (*p == NUL)
9489 count = 1;
9490 else if (isdigit(*p))
9491 {
9492 count = getdigits(&p);
9493 switch (*p)
9494 {
9495 case 's': ++p; sec = TRUE; break;
9496 case 'm': ++p; sec = TRUE; count *= 60; break;
9497 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009498 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9499 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009500 }
9501 }
9502
9503 if (*p != NUL)
9504 EMSG2(_(e_invarg2), eap->arg);
9505 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009506 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9507 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009508}
9509
9510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 * ":redir": start/stop redirection.
9512 */
9513 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009514ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515{
9516 char *mode;
9517 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009518 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519
Bram Moolenaarba768492016-07-08 15:32:54 +02009520#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009521 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009522 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009523 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009524 return;
9525 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009526#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009527
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 if (STRICMP(eap->arg, "END") == 0)
9529 close_redir();
9530 else
9531 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009532 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009534 ++arg;
9535 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009537 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 mode = "a";
9539 }
9540 else
9541 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009542 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543
9544 close_redir();
9545
9546 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009547 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548 if (fname == NULL)
9549 return;
9550#ifdef FEAT_BROWSE
9551 if (cmdmod.browse)
9552 {
9553 char_u *browseFile;
9554
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009555 browseFile = do_browse(BROWSE_SAVE,
9556 (char_u *)_("Save Redirection"),
9557 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 if (browseFile == NULL)
9559 return; /* operation cancelled */
9560 vim_free(fname);
9561 fname = browseFile;
9562 eap->forceit = TRUE; /* since dialog already asked */
9563 }
9564#endif
9565
9566 redir_fd = open_exfile(fname, eap->forceit, mode);
9567 vim_free(fname);
9568 }
9569#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009570 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 {
9572 /* redirect to a register a-z (resp. A-Z for appending) */
9573 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009574 ++arg;
9575 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009577 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009578 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009580 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009582 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009583 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009584 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009585 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009587 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009588 if (*arg == '>')
9589 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009590 /* Make register empty when not using @A-@Z and the
9591 * command is valid. */
9592 if (*arg == NUL && !isupper(redir_reg))
9593 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009595 }
9596 if (*arg != NUL)
9597 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009598 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009599 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009600 }
9601 }
9602 else if (*arg == '=' && arg[1] == '>')
9603 {
9604 int append;
9605
9606 /* redirect to a variable */
9607 close_redir();
9608 arg += 2;
9609
9610 if (*arg == '>')
9611 {
9612 ++arg;
9613 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 }
9615 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009616 append = FALSE;
9617
9618 if (var_redir_start(skipwhite(arg), append) == OK)
9619 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 }
9621#endif
9622
9623 /* TODO: redirect to a buffer */
9624
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009626 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009628
9629 /* Make sure redirection is not off. Can happen for cmdline completion
9630 * that indirectly invokes a command to catch its output. */
9631 if (redir_fd != NULL
9632#ifdef FEAT_EVAL
9633 || redir_reg || redir_vname
9634#endif
9635 )
9636 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637}
9638
9639/*
9640 * ":redraw": force redraw
9641 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009642 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009643ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644{
9645 int r = RedrawingDisabled;
9646 int p = p_lz;
9647
9648 RedrawingDisabled = 0;
9649 p_lz = FALSE;
9650 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009651 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652#ifdef FEAT_TITLE
9653 if (need_maketitle)
9654 maketitle();
9655#endif
9656 RedrawingDisabled = r;
9657 p_lz = p;
9658
9659 /* Reset msg_didout, so that a message that's there is overwritten. */
9660 msg_didout = FALSE;
9661 msg_col = 0;
9662
Bram Moolenaar56667a52013-07-17 11:54:28 +02009663 /* No need to wait after an intentional redraw. */
9664 need_wait_return = FALSE;
9665
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 out_flush();
9667}
9668
9669/*
9670 * ":redrawstatus": force redraw of status line(s)
9671 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009673ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674{
9675#if defined(FEAT_WINDOWS)
9676 int r = RedrawingDisabled;
9677 int p = p_lz;
9678
9679 RedrawingDisabled = 0;
9680 p_lz = FALSE;
9681 if (eap->forceit)
9682 status_redraw_all();
9683 else
9684 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009685 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 RedrawingDisabled = r;
9687 p_lz = p;
9688 out_flush();
9689#endif
9690}
9691
9692 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009693close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694{
9695 if (redir_fd != NULL)
9696 {
9697 fclose(redir_fd);
9698 redir_fd = NULL;
9699 }
9700#ifdef FEAT_EVAL
9701 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009702 if (redir_vname)
9703 {
9704 var_redir_stop();
9705 redir_vname = 0;
9706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707#endif
9708}
9709
9710#if defined(FEAT_SESSION) && defined(USE_CRNL)
9711# define MKSESSION_NL
9712static int mksession_nl = FALSE; /* use NL only in put_eol() */
9713#endif
9714
9715/*
9716 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9717 */
9718 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009719ex_mkrc(
9720 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721{
9722 FILE *fd;
9723 int failed = FALSE;
9724 char_u *fname;
9725#ifdef FEAT_BROWSE
9726 char_u *browseFile = NULL;
9727#endif
9728#ifdef FEAT_SESSION
9729 int view_session = FALSE;
9730 int using_vdir = FALSE; /* using 'viewdir'? */
9731 char_u *viewFile = NULL;
9732 unsigned *flagp;
9733#endif
9734
9735 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9736 {
9737#ifdef FEAT_SESSION
9738 view_session = TRUE;
9739#else
9740 ex_ni(eap);
9741 return;
9742#endif
9743 }
9744
9745#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009746 /* Use the short file name until ":lcd" is used. We also don't use the
9747 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009748 did_lcd = FALSE;
9749
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9751 if (eap->cmdidx == CMD_mkview
9752 && (*eap->arg == NUL
9753 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9754 {
9755 eap->forceit = TRUE;
9756 fname = get_view_file(*eap->arg);
9757 if (fname == NULL)
9758 return;
9759 viewFile = fname;
9760 using_vdir = TRUE;
9761 }
9762 else
9763#endif
9764 if (*eap->arg != NUL)
9765 fname = eap->arg;
9766 else if (eap->cmdidx == CMD_mkvimrc)
9767 fname = (char_u *)VIMRC_FILE;
9768#ifdef FEAT_SESSION
9769 else if (eap->cmdidx == CMD_mksession)
9770 fname = (char_u *)SESSION_FILE;
9771#endif
9772 else
9773 fname = (char_u *)EXRC_FILE;
9774
9775#ifdef FEAT_BROWSE
9776 if (cmdmod.browse)
9777 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009778 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779# ifdef FEAT_SESSION
9780 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9781 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9782# endif
9783 (char_u *)_("Save Setup"),
9784 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9785 if (browseFile == NULL)
9786 goto theend;
9787 fname = browseFile;
9788 eap->forceit = TRUE; /* since dialog already asked */
9789 }
9790#endif
9791
9792#if defined(FEAT_SESSION) && defined(vim_mkdir)
9793 /* When using 'viewdir' may have to create the directory. */
9794 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009795 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796#endif
9797
9798 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9799 if (fd != NULL)
9800 {
9801#ifdef FEAT_SESSION
9802 if (eap->cmdidx == CMD_mkview)
9803 flagp = &vop_flags;
9804 else
9805 flagp = &ssop_flags;
9806#endif
9807
9808#ifdef MKSESSION_NL
9809 /* "unix" in 'sessionoptions': use NL line separator */
9810 if (view_session && (*flagp & SSOP_UNIX))
9811 mksession_nl = TRUE;
9812#endif
9813
9814 /* Write the version command for :mkvimrc */
9815 if (eap->cmdidx == CMD_mkvimrc)
9816 (void)put_line(fd, "version 6.0");
9817
9818#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009819 if (eap->cmdidx == CMD_mksession)
9820 {
9821 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9822 failed = TRUE;
9823 }
9824
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825 if (eap->cmdidx != CMD_mkview)
9826#endif
9827 {
9828 /* Write setting 'compatible' first, because it has side effects.
9829 * For that same reason only do it when needed. */
9830 if (p_cp)
9831 (void)put_line(fd, "if !&cp | set cp | endif");
9832 else
9833 (void)put_line(fd, "if &cp | set nocp | endif");
9834 }
9835
9836#ifdef FEAT_SESSION
9837 if (!view_session
9838 || (eap->cmdidx == CMD_mksession
9839 && (*flagp & SSOP_OPTIONS)))
9840#endif
9841 failed |= (makemap(fd, NULL) == FAIL
9842 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9843
9844#ifdef FEAT_SESSION
9845 if (!failed && view_session)
9846 {
9847 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9848 failed = TRUE;
9849 if (eap->cmdidx == CMD_mksession)
9850 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009851 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852
Bram Moolenaard9462e32011-04-11 21:35:11 +02009853 dirnow = alloc(MAXPATHL);
9854 if (dirnow == NULL)
9855 failed = TRUE;
9856 else
9857 {
9858 /*
9859 * Change to session file's dir.
9860 */
9861 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009863 *dirnow = NUL;
9864 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9865 {
9866 if (vim_chdirfile(fname) == OK)
9867 shorten_fnames(TRUE);
9868 }
9869 else if (*dirnow != NUL
9870 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9871 {
9872 if (mch_chdir((char *)globaldir) == 0)
9873 shorten_fnames(TRUE);
9874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875
Bram Moolenaard9462e32011-04-11 21:35:11 +02009876 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877
Bram Moolenaard9462e32011-04-11 21:35:11 +02009878 /* restore original dir */
9879 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009881 {
9882 if (mch_chdir((char *)dirnow) != 0)
9883 EMSG(_(e_prev_dir));
9884 shorten_fnames(TRUE);
9885 }
9886 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 }
9888 }
9889 else
9890 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009891 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9892 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 }
9894 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9895 == FAIL)
9896 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009897 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9898 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009899 if (eap->cmdidx == CMD_mksession)
9900 {
9901 if (put_line(fd, "unlet SessionLoad") == FAIL)
9902 failed = TRUE;
9903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 }
9905#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009906 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9907 failed = TRUE;
9908
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 failed |= fclose(fd);
9910
9911 if (failed)
9912 EMSG(_(e_write));
9913#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9914 else if (eap->cmdidx == CMD_mksession)
9915 {
9916 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009917 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918
Bram Moolenaard9462e32011-04-11 21:35:11 +02009919 tbuf = alloc(MAXPATHL);
9920 if (tbuf != NULL)
9921 {
9922 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9923 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9924 vim_free(tbuf);
9925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926 }
9927#endif
9928#ifdef MKSESSION_NL
9929 mksession_nl = FALSE;
9930#endif
9931 }
9932
9933#ifdef FEAT_BROWSE
9934theend:
9935 vim_free(browseFile);
9936#endif
9937#ifdef FEAT_SESSION
9938 vim_free(viewFile);
9939#endif
9940}
9941
Bram Moolenaardf177f62005-02-22 08:39:57 +00009942#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9943 || defined(PROTO)
9944 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009945vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009946{
9947 if (vim_mkdir(name, prot) != 0)
9948 {
9949 EMSG2(_("E739: Cannot create directory: %s"), name);
9950 return FAIL;
9951 }
9952 return OK;
9953}
9954#endif
9955
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956/*
9957 * Open a file for writing for an Ex command, with some checks.
9958 * Return file descriptor, or NULL on failure.
9959 */
9960 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009961open_exfile(
9962 char_u *fname,
9963 int forceit,
9964 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965{
9966 FILE *fd;
9967
9968#ifdef UNIX
9969 /* with Unix it is possible to open a directory */
9970 if (mch_isdir(fname))
9971 {
9972 EMSG2(_(e_isadir2), fname);
9973 return NULL;
9974 }
9975#endif
9976 if (!forceit && *mode != 'a' && vim_fexists(fname))
9977 {
9978 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9979 return NULL;
9980 }
9981
9982 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9983 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9984
9985 return fd;
9986}
9987
9988/*
9989 * ":mark" and ":k".
9990 */
9991 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009992ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993{
9994 pos_T pos;
9995
9996 if (*eap->arg == NUL) /* No argument? */
9997 EMSG(_(e_argreq));
9998 else if (eap->arg[1] != NUL) /* more than one character? */
9999 EMSG(_(e_trailing));
10000 else
10001 {
10002 pos = curwin->w_cursor; /* save curwin->w_cursor */
10003 curwin->w_cursor.lnum = eap->line2;
10004 beginline(BL_WHITE | BL_FIX);
10005 if (setmark(*eap->arg) == FAIL) /* set mark */
10006 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10007 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10008 }
10009}
10010
10011/*
10012 * Update w_topline, w_leftcol and the cursor position.
10013 */
10014 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010015update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016{
10017 check_cursor(); /* put cursor on valid line */
10018 update_topline();
10019 if (!curwin->w_p_wrap)
10020 validate_cursor();
10021 update_curswant();
10022}
10023
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024/*
10025 * ":normal[!] {commands}": Execute normal mode commands.
10026 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010027 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010028ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 int save_msg_scroll = msg_scroll;
10031 int save_restart_edit = restart_edit;
10032 int save_msg_didout = msg_didout;
10033 int save_State = State;
10034 tasave_T tabuf;
10035 int save_insertmode = p_im;
10036 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010037 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038#ifdef FEAT_MBYTE
10039 char_u *arg = NULL;
10040 int l;
10041 char_u *p;
10042#endif
10043
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010044 if (ex_normal_lock > 0)
10045 {
10046 EMSG(_(e_secure));
10047 return;
10048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 if (ex_normal_busy >= p_mmd)
10050 {
10051 EMSG(_("E192: Recursive use of :normal too deep"));
10052 return;
10053 }
10054 ++ex_normal_busy;
10055
10056 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10057 restart_edit = 0; /* don't go to Insert mode */
10058 p_im = FALSE; /* don't use 'insertmode' */
10059
10060#ifdef FEAT_MBYTE
10061 /*
10062 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10063 * this for the K_SPECIAL leading byte, otherwise special keys will not
10064 * work.
10065 */
10066 if (has_mbyte)
10067 {
10068 int len = 0;
10069
10070 /* Count the number of characters to be escaped. */
10071 for (p = eap->arg; *p != NUL; ++p)
10072 {
10073# ifdef FEAT_GUI
10074 if (*p == CSI) /* leadbyte CSI */
10075 len += 2;
10076# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010077 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10079# ifdef FEAT_GUI
10080 || *p == CSI
10081# endif
10082 )
10083 len += 2;
10084 }
10085 if (len > 0)
10086 {
10087 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10088 if (arg != NULL)
10089 {
10090 len = 0;
10091 for (p = eap->arg; *p != NUL; ++p)
10092 {
10093 arg[len++] = *p;
10094# ifdef FEAT_GUI
10095 if (*p == CSI)
10096 {
10097 arg[len++] = KS_EXTRA;
10098 arg[len++] = (int)KE_CSI;
10099 }
10100# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010101 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102 {
10103 arg[len++] = *++p;
10104 if (*p == K_SPECIAL)
10105 {
10106 arg[len++] = KS_SPECIAL;
10107 arg[len++] = KE_FILLER;
10108 }
10109# ifdef FEAT_GUI
10110 else if (*p == CSI)
10111 {
10112 arg[len++] = KS_EXTRA;
10113 arg[len++] = (int)KE_CSI;
10114 }
10115# endif
10116 }
10117 arg[len] = NUL;
10118 }
10119 }
10120 }
10121 }
10122#endif
10123
10124 /*
10125 * Save the current typeahead. This is required to allow using ":normal"
10126 * from an event handler and makes sure we don't hang when the argument
10127 * ends with half a command.
10128 */
10129 save_typeahead(&tabuf);
10130 if (tabuf.typebuf_valid)
10131 {
10132 /*
10133 * Repeat the :normal command for each line in the range. When no
10134 * range given, execute it just once, without positioning the cursor
10135 * first.
10136 */
10137 do
10138 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 if (eap->addr_count != 0)
10140 {
10141 curwin->w_cursor.lnum = eap->line1++;
10142 curwin->w_cursor.col = 0;
10143 }
10144
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010145 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146#ifdef FEAT_MBYTE
10147 arg != NULL ? arg :
10148#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010149 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 }
10151 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10152 }
10153
10154 /* Might not return to the main loop when in an event handler. */
10155 update_topline_cursor();
10156
10157 /* Restore the previous typeahead. */
10158 restore_typeahead(&tabuf);
10159
10160 --ex_normal_busy;
10161 msg_scroll = save_msg_scroll;
10162 restart_edit = save_restart_edit;
10163 p_im = save_insertmode;
10164 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010165 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
10167
10168 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +010010169 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +010010171#ifdef FEAT_MOUSE
10172 setmouse();
10173#endif
10174#ifdef CURSOR_SHAPE
10175 ui_cursor_shape(); /* may show different cursor shape */
10176#endif
10177
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178#ifdef FEAT_MBYTE
10179 vim_free(arg);
10180#endif
10181}
10182
10183/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010184 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185 */
10186 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010187ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010189 if (eap->forceit)
10190 {
10191 coladvance((colnr_T)MAXCOL);
10192 curwin->w_curswant = MAXCOL;
10193 curwin->w_set_curswant = FALSE;
10194 }
10195
Bram Moolenaara40c5002005-01-09 21:16:21 +000010196 /* Ignore the command when already in Insert mode. Inserting an
10197 * expression register that invokes a function can do this. */
10198 if (State & INSERT)
10199 return;
10200
Bram Moolenaar64969662005-12-14 21:59:55 +000010201 if (eap->cmdidx == CMD_startinsert)
10202 restart_edit = 'a';
10203 else if (eap->cmdidx == CMD_startreplace)
10204 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010206 restart_edit = 'V';
10207
10208 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010210 if (eap->cmdidx == CMD_startinsert)
10211 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 curwin->w_curswant = 0; /* avoid MAXCOL */
10213 }
10214}
10215
10216/*
10217 * ":stopinsert"
10218 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010220ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221{
10222 restart_edit = 0;
10223 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010224 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010227/*
10228 * Execute normal mode command "cmd".
10229 * "remap" can be REMAP_NONE or REMAP_YES.
10230 */
10231 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010232exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010233{
Bram Moolenaar25281632016-01-21 23:32:32 +010010234 /* Stuff the argument into the typeahead buffer. */
10235 ins_typebuf(cmd, remap, 0, TRUE, silent);
10236 exec_normal(FALSE);
10237}
Bram Moolenaar25281632016-01-21 23:32:32 +010010238
Bram Moolenaar25281632016-01-21 23:32:32 +010010239/*
10240 * Execute normal_cmd() until there is no typeahead left.
10241 */
10242 void
10243exec_normal(int was_typed)
10244{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010245 oparg_T oa;
10246
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010247 clear_oparg(&oa);
10248 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010249 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10250 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010251 {
10252 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010253 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010254 }
10255}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010256
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257#ifdef FEAT_FIND_ID
10258 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010259ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260{
10261 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10262 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10263 (linenr_T)1, (linenr_T)MAXLNUM);
10264}
10265
10266#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
10267/*
10268 * ":psearch"
10269 */
10270 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010271ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272{
10273 g_do_tagpreview = p_pvh;
10274 ex_findpat(eap);
10275 g_do_tagpreview = 0;
10276}
10277#endif
10278
10279 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010280ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281{
10282 int whole = TRUE;
10283 long n;
10284 char_u *p;
10285 int action;
10286
10287 switch (cmdnames[eap->cmdidx].cmd_name[2])
10288 {
10289 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10290 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10291 action = ACTION_GOTO;
10292 else
10293 action = ACTION_SHOW;
10294 break;
10295 case 'i': /* ":ilist" and ":dlist" */
10296 action = ACTION_SHOW_ALL;
10297 break;
10298 case 'u': /* ":ijump" and ":djump" */
10299 action = ACTION_GOTO;
10300 break;
10301 default: /* ":isplit" and ":dsplit" */
10302 action = ACTION_SPLIT;
10303 break;
10304 }
10305
10306 n = 1;
10307 if (vim_isdigit(*eap->arg)) /* get count */
10308 {
10309 n = getdigits(&eap->arg);
10310 eap->arg = skipwhite(eap->arg);
10311 }
10312 if (*eap->arg == '/') /* Match regexp, not just whole words */
10313 {
10314 whole = FALSE;
10315 ++eap->arg;
10316 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10317 if (*p)
10318 {
10319 *p++ = NUL;
10320 p = skipwhite(p);
10321
10322 /* Check for trailing illegal characters */
10323 if (!ends_excmd(*p))
10324 eap->errmsg = e_trailing;
10325 else
10326 eap->nextcmd = check_nextcmd(p);
10327 }
10328 }
10329 if (!eap->skip)
10330 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10331 whole, !eap->forceit,
10332 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10333 n, action, eap->line1, eap->line2);
10334}
10335#endif
10336
10337#ifdef FEAT_WINDOWS
10338
10339# ifdef FEAT_QUICKFIX
10340/*
10341 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10342 */
10343 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010344ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010346 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10348}
10349
10350/*
10351 * ":pedit"
10352 */
10353 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010354ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355{
10356 win_T *curwin_save = curwin;
10357
10358 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010359 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 keep_help_flag = curwin_save->w_buffer->b_help;
10361 do_exedit(eap, NULL);
10362 keep_help_flag = FALSE;
10363 if (curwin != curwin_save && win_valid(curwin_save))
10364 {
10365 /* Return cursor to where we were */
10366 validate_cursor();
10367 redraw_later(VALID);
10368 win_enter(curwin_save, TRUE);
10369 }
10370 g_do_tagpreview = 0;
10371}
10372# endif
10373
10374/*
10375 * ":stag", ":stselect" and ":stjump".
10376 */
10377 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010378ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379{
10380 postponed_split = -1;
10381 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010382 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10384 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010385 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386}
10387#endif
10388
10389/*
10390 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10391 */
10392 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010393ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394{
10395 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10396}
10397
10398 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010399ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400{
10401 int cmd;
10402
10403 switch (name[1])
10404 {
10405 case 'j': cmd = DT_JUMP; /* ":tjump" */
10406 break;
10407 case 's': cmd = DT_SELECT; /* ":tselect" */
10408 break;
10409 case 'p': cmd = DT_PREV; /* ":tprevious" */
10410 break;
10411 case 'N': cmd = DT_PREV; /* ":tNext" */
10412 break;
10413 case 'n': cmd = DT_NEXT; /* ":tnext" */
10414 break;
10415 case 'o': cmd = DT_POP; /* ":pop" */
10416 break;
10417 case 'f': /* ":tfirst" */
10418 case 'r': cmd = DT_FIRST; /* ":trewind" */
10419 break;
10420 case 'l': cmd = DT_LAST; /* ":tlast" */
10421 break;
10422 default: /* ":tag" */
10423#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010424 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010426 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427 return;
10428 }
10429#endif
10430 cmd = DT_TAG;
10431 break;
10432 }
10433
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010434 if (name[0] == 'l')
10435 {
10436#ifndef FEAT_QUICKFIX
10437 ex_ni(eap);
10438 return;
10439#else
10440 cmd = DT_LTAG;
10441#endif
10442 }
10443
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10445 eap->forceit, TRUE);
10446}
10447
10448/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010449 * Check "str" for starting with a special cmdline variable.
10450 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10451 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10452 */
10453 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010454find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010455{
10456 int len;
10457 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010458 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010459 "%",
10460#define SPEC_PERC 0
10461 "#",
10462#define SPEC_HASH 1
10463 "<cword>", /* cursor word */
10464#define SPEC_CWORD 2
10465 "<cWORD>", /* cursor WORD */
10466#define SPEC_CCWORD 3
10467 "<cfile>", /* cursor path name */
10468#define SPEC_CFILE 4
10469 "<sfile>", /* ":so" file name */
10470#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010471 "<slnum>", /* ":so" file line number */
10472#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010473#ifdef FEAT_AUTOCMD
10474 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010475# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010476 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010477# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010478 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010479# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010480#endif
10481#ifdef FEAT_CLIENTSERVER
10482 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010483# ifdef FEAT_AUTOCMD
10484# define SPEC_CLIENT 10
10485# else
10486# define SPEC_CLIENT 7
10487# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010488#endif
10489 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010490
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010491 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010492 {
10493 len = (int)STRLEN(spec_str[i]);
10494 if (STRNCMP(src, spec_str[i], len) == 0)
10495 {
10496 *usedlen = len;
10497 return i;
10498 }
10499 }
10500 return -1;
10501}
10502
10503/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504 * Evaluate cmdline variables.
10505 *
10506 * change '%' to curbuf->b_ffname
10507 * '#' to curwin->w_altfile
10508 * '<cword>' to word under the cursor
10509 * '<cWORD>' to WORD under the cursor
10510 * '<cfile>' to path name under the cursor
10511 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010512 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513 * '<afile>' to file name for autocommand
10514 * '<abuf>' to buffer number for autocommand
10515 * '<amatch>' to matching name for autocommand
10516 *
10517 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10518 * "" for error without a message) and NULL is returned.
10519 * Returns an allocated string if a valid match was found.
10520 * Returns NULL if no match was found. "usedlen" then still contains the
10521 * number of characters to skip.
10522 */
10523 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010524eval_vars(
10525 char_u *src, /* pointer into commandline */
10526 char_u *srcstart, /* beginning of valid memory for src */
10527 int *usedlen, /* characters after src that are used */
10528 linenr_T *lnump, /* line number for :e command, or NULL */
10529 char_u **errormsg, /* pointer to error message */
10530 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010531 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532{
10533 int i;
10534 char_u *s;
10535 char_u *result;
10536 char_u *resultbuf = NULL;
10537 int resultlen;
10538 buf_T *buf;
10539 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10540 int spec_idx;
10541#ifdef FEAT_MODIFY_FNAME
10542 int skip_mod = FALSE;
10543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545
10546 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010547 if (escaped != NULL)
10548 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549
10550 /*
10551 * Check if there is something to do.
10552 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010553 spec_idx = find_cmdline_var(src, usedlen);
10554 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555 {
10556 *usedlen = 1;
10557 return NULL;
10558 }
10559
10560 /*
10561 * Skip when preceded with a backslash "\%" and "\#".
10562 * Note: In "\\%" the % is also not recognized!
10563 */
10564 if (src > srcstart && src[-1] == '\\')
10565 {
10566 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010567 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568 return NULL;
10569 }
10570
10571 /*
10572 * word or WORD under cursor
10573 */
10574 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10575 {
10576 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10577 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10578 if (resultlen == 0)
10579 {
10580 *errormsg = (char_u *)"";
10581 return NULL;
10582 }
10583 }
10584
10585 /*
10586 * '#': Alternate file name
10587 * '%': Current file name
10588 * File name under the cursor
10589 * File name for autocommand
10590 * and following modifiers
10591 */
10592 else
10593 {
10594 switch (spec_idx)
10595 {
10596 case SPEC_PERC: /* '%': current file */
10597 if (curbuf->b_fname == NULL)
10598 {
10599 result = (char_u *)"";
10600 valid = 0; /* Must have ":p:h" to be valid */
10601 }
10602 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604 break;
10605
10606 case SPEC_HASH: /* '#' or "#99": alternate file */
10607 if (src[1] == '#') /* "##": the argument list */
10608 {
10609 result = arg_all();
10610 resultbuf = result;
10611 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010612 if (escaped != NULL)
10613 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614#ifdef FEAT_MODIFY_FNAME
10615 skip_mod = TRUE;
10616#endif
10617 break;
10618 }
10619 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010620 if (*s == '<') /* "#<99" uses v:oldfiles */
10621 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622 i = (int)getdigits(&s);
10623 *usedlen = (int)(s - src); /* length of what we expand */
10624
Bram Moolenaard812df62008-11-09 12:46:09 +000010625 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010627 if (*usedlen < 2)
10628 {
10629 /* Should we give an error message for #<text? */
10630 *usedlen = 1;
10631 return NULL;
10632 }
10633#ifdef FEAT_EVAL
10634 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10635 (long)i);
10636 if (result == NULL)
10637 {
10638 *errormsg = (char_u *)"";
10639 return NULL;
10640 }
10641#else
10642 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645 }
10646 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010647 {
10648 buf = buflist_findnr(i);
10649 if (buf == NULL)
10650 {
10651 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10652 return NULL;
10653 }
10654 if (lnump != NULL)
10655 *lnump = ECMD_LAST;
10656 if (buf->b_fname == NULL)
10657 {
10658 result = (char_u *)"";
10659 valid = 0; /* Must have ":p:h" to be valid */
10660 }
10661 else
10662 result = buf->b_fname;
10663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664 break;
10665
10666#ifdef FEAT_SEARCHPATH
10667 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010668 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669 if (result == NULL)
10670 {
10671 *errormsg = (char_u *)"";
10672 return NULL;
10673 }
10674 resultbuf = result; /* remember allocated string */
10675 break;
10676#endif
10677
10678#ifdef FEAT_AUTOCMD
10679 case SPEC_AFILE: /* file name for autocommand */
10680 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010681 if (result != NULL && !autocmd_fname_full)
10682 {
10683 /* Still need to turn the fname into a full path. It is
10684 * postponed to avoid a delay when <afile> is not used. */
10685 autocmd_fname_full = TRUE;
10686 result = FullName_save(autocmd_fname, FALSE);
10687 vim_free(autocmd_fname);
10688 autocmd_fname = result;
10689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690 if (result == NULL)
10691 {
10692 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10693 return NULL;
10694 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010695 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696 break;
10697
10698 case SPEC_ABUF: /* buffer number for autocommand */
10699 if (autocmd_bufnr <= 0)
10700 {
10701 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10702 return NULL;
10703 }
10704 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10705 result = strbuf;
10706 break;
10707
10708 case SPEC_AMATCH: /* match name for autocommand */
10709 result = autocmd_match;
10710 if (result == NULL)
10711 {
10712 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10713 return NULL;
10714 }
10715 break;
10716
10717#endif
10718 case SPEC_SFILE: /* file name for ":so" command */
10719 result = sourcing_name;
10720 if (result == NULL)
10721 {
10722 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10723 return NULL;
10724 }
10725 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010726 case SPEC_SLNUM: /* line in file for ":so" command */
10727 if (sourcing_name == NULL || sourcing_lnum == 0)
10728 {
10729 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10730 return NULL;
10731 }
10732 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10733 result = strbuf;
10734 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735#if defined(FEAT_CLIENTSERVER)
10736 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010737 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10738 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739 result = strbuf;
10740 break;
10741#endif
10742 }
10743
10744 resultlen = (int)STRLEN(result); /* length of new string */
10745 if (src[*usedlen] == '<') /* remove the file name extension */
10746 {
10747 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749 resultlen = (int)(s - result);
10750 }
10751#ifdef FEAT_MODIFY_FNAME
10752 else if (!skip_mod)
10753 {
10754 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10755 &resultlen);
10756 if (result == NULL)
10757 {
10758 *errormsg = (char_u *)"";
10759 return NULL;
10760 }
10761 }
10762#endif
10763 }
10764
10765 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10766 {
10767 if (valid != VALID_HEAD + VALID_PATH)
10768 /* xgettext:no-c-format */
10769 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10770 else
10771 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10772 result = NULL;
10773 }
10774 else
10775 result = vim_strnsave(result, resultlen);
10776 vim_free(resultbuf);
10777 return result;
10778}
10779
10780/*
10781 * Concatenate all files in the argument list, separated by spaces, and return
10782 * it in one allocated string.
10783 * Spaces and backslashes in the file names are escaped with a backslash.
10784 * Returns NULL when out of memory.
10785 */
10786 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010787arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788{
10789 int len;
10790 int idx;
10791 char_u *retval = NULL;
10792 char_u *p;
10793
10794 /*
10795 * Do this loop two times:
10796 * first time: compute the total length
10797 * second time: concatenate the names
10798 */
10799 for (;;)
10800 {
10801 len = 0;
10802 for (idx = 0; idx < ARGCOUNT; ++idx)
10803 {
10804 p = alist_name(&ARGLIST[idx]);
10805 if (p != NULL)
10806 {
10807 if (len > 0)
10808 {
10809 /* insert a space in between names */
10810 if (retval != NULL)
10811 retval[len] = ' ';
10812 ++len;
10813 }
10814 for ( ; *p != NUL; ++p)
10815 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010816 if (*p == ' '
10817#ifndef BACKSLASH_IN_FILENAME
10818 || *p == '\\'
10819#endif
10820 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010821 {
10822 /* insert a backslash */
10823 if (retval != NULL)
10824 retval[len] = '\\';
10825 ++len;
10826 }
10827 if (retval != NULL)
10828 retval[len] = *p;
10829 ++len;
10830 }
10831 }
10832 }
10833
10834 /* second time: break here */
10835 if (retval != NULL)
10836 {
10837 retval[len] = NUL;
10838 break;
10839 }
10840
10841 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010842 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 if (retval == NULL)
10844 break;
10845 }
10846
10847 return retval;
10848}
10849
10850#if defined(FEAT_AUTOCMD) || defined(PROTO)
10851/*
10852 * Expand the <sfile> string in "arg".
10853 *
10854 * Returns an allocated string, or NULL for any error.
10855 */
10856 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010857expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858{
10859 char_u *errormsg;
10860 int len;
10861 char_u *result;
10862 char_u *newres;
10863 char_u *repl;
10864 int srclen;
10865 char_u *p;
10866
10867 result = vim_strsave(arg);
10868 if (result == NULL)
10869 return NULL;
10870
10871 for (p = result; *p; )
10872 {
10873 if (STRNCMP(p, "<sfile>", 7) != 0)
10874 ++p;
10875 else
10876 {
10877 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010878 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 if (errormsg != NULL)
10880 {
10881 if (*errormsg)
10882 emsg(errormsg);
10883 vim_free(result);
10884 return NULL;
10885 }
10886 if (repl == NULL) /* no match (cannot happen) */
10887 {
10888 p += srclen;
10889 continue;
10890 }
10891 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10892 newres = alloc(len);
10893 if (newres == NULL)
10894 {
10895 vim_free(repl);
10896 vim_free(result);
10897 return NULL;
10898 }
10899 mch_memmove(newres, result, (size_t)(p - result));
10900 STRCPY(newres + (p - result), repl);
10901 len = (int)STRLEN(newres);
10902 STRCAT(newres, p + srclen);
10903 vim_free(repl);
10904 vim_free(result);
10905 result = newres;
10906 p = newres + len; /* continue after the match */
10907 }
10908 }
10909
10910 return result;
10911}
10912#endif
10913
10914#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010010915static int ses_winsizes(FILE *fd, int restore_size,
10916 win_T *tab_firstwin);
10917static int ses_win_rec(FILE *fd, frame_T *fr);
10918static frame_T *ses_skipframe(frame_T *fr);
10919static int ses_do_frame(frame_T *fr);
10920static int ses_do_win(win_T *wp);
10921static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
10922static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
10923static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924
10925/*
10926 * Write openfile commands for the current buffers to an .exrc file.
10927 * Return FAIL on error, OK otherwise.
10928 */
10929 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010930makeopens(
10931 FILE *fd,
10932 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933{
10934 buf_T *buf;
10935 int only_save_windows = TRUE;
10936 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937 int restore_size = TRUE;
10938 win_T *wp;
10939 char_u *sname;
10940 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010941 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010942 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010943 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010944 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010945 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010946 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947
10948 if (ssop_flags & SSOP_BUFFERS)
10949 only_save_windows = FALSE; /* Save ALL buffers */
10950
10951 /*
10952 * Begin by setting the this_session variable, and then other
10953 * sessionable variables.
10954 */
10955#ifdef FEAT_EVAL
10956 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10957 return FAIL;
10958 if (ssop_flags & SSOP_GLOBALS)
10959 if (store_session_globals(fd) == FAIL)
10960 return FAIL;
10961#endif
10962
10963 /*
10964 * Close all windows but one.
10965 */
10966 if (put_line(fd, "silent only") == FAIL)
10967 return FAIL;
10968
10969 /*
10970 * Now a :cd command to the session directory or the current directory
10971 */
10972 if (ssop_flags & SSOP_SESDIR)
10973 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010974 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10975 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 return FAIL;
10977 }
10978 else if (ssop_flags & SSOP_CURDIR)
10979 {
10980 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10981 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010982 || fputs("cd ", fd) < 0
10983 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10984 || put_eol(fd) == FAIL)
10985 {
10986 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989 vim_free(sname);
10990 }
10991
10992 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010993 * If there is an empty, unnamed buffer we will wipe it out later.
10994 * Remember the buffer number.
10995 */
10996 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10997 return FAIL;
10998 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10999 return FAIL;
11000 if (put_line(fd, "endif") == FAIL)
11001 return FAIL;
11002
11003 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011004 * Now save the current files, current buffer first.
11005 */
11006 if (put_line(fd, "set shortmess=aoO") == FAIL)
11007 return FAIL;
11008
11009 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011010 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011 {
11012 if (!(only_save_windows && buf->b_nwindows == 0)
11013 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
11014 && buf->b_fname != NULL
11015 && buf->b_p_bl)
11016 {
11017 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11018 : buf->b_wininfo->wi_fpos.lnum) < 0
11019 || ses_fname(fd, buf, &ssop_flags) == FAIL)
11020 return FAIL;
11021 }
11022 }
11023
11024 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011025 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11027 return FAIL;
11028
11029 if (ssop_flags & SSOP_RESIZE)
11030 {
11031 /* Note: after the restore we still check it worked!*/
11032 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11033 || put_eol(fd) == FAIL)
11034 return FAIL;
11035 }
11036
11037#ifdef FEAT_GUI
11038 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11039 {
11040 int x, y;
11041
11042 if (gui_mch_get_winpos(&x, &y) == OK)
11043 {
11044 /* Note: after the restore we still check it worked!*/
11045 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11046 return FAIL;
11047 }
11048 }
11049#endif
11050
11051 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011052 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11053 * will be displayed when creating the next tab. That resizes the windows
11054 * in the first tab, which may cause problems. Set 'showtabline' to 2
11055 * temporarily to avoid that.
11056 */
11057 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11058 {
11059 if (put_line(fd, "set stal=2") == FAIL)
11060 return FAIL;
11061 restore_stal = TRUE;
11062 }
11063
11064 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011065 * May repeat putting Windows for each tab, when "tabpages" is in
11066 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011067 * Don't use goto_tabpage(), it may change directory and trigger
11068 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011070 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011071 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011072 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011074 int need_tabnew = FALSE;
11075 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011076
Bram Moolenaar18144c82006-04-12 21:52:12 +000011077 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011079 tabpage_T *tp = find_tabpage(tabnr);
11080
11081 if (tp == NULL)
11082 break; /* done all tab pages */
11083 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011084 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011085 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011086 tab_topframe = topframe;
11087 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011088 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011089 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011090 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011091 tab_topframe = tp->tp_topframe;
11092 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011093 if (tabnr > 1)
11094 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096
Bram Moolenaar18144c82006-04-12 21:52:12 +000011097 /*
11098 * Before creating the window layout, try loading one file. If this
11099 * is aborted we don't end up with a number of useless windows.
11100 * This may have side effects! (e.g., compressed or network file).
11101 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011102 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011103 {
11104 if (ses_do_win(wp)
11105 && wp->w_buffer->b_ffname != NULL
11106 && !wp->w_buffer->b_help
11107#ifdef FEAT_QUICKFIX
11108 && !bt_nofile(wp->w_buffer)
11109#endif
11110 )
11111 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011112 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000011113 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
11114 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011115 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011116 if (!wp->w_arg_idx_invalid)
11117 edited_win = wp;
11118 break;
11119 }
11120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011122 /* If no file got edited create an empty tab page. */
11123 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11124 return FAIL;
11125
Bram Moolenaar18144c82006-04-12 21:52:12 +000011126 /*
11127 * Save current window layout.
11128 */
11129 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011131 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011133 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11134 return FAIL;
11135 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11136 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137
Bram Moolenaar18144c82006-04-12 21:52:12 +000011138 /*
11139 * Check if window sizes can be restored (no windows omitted).
11140 * Remember the window number of the current window after restoring.
11141 */
11142 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011143 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011144 {
11145 if (ses_do_win(wp))
11146 ++nr;
11147 else
11148 restore_size = FALSE;
11149 if (curwin == wp)
11150 cnr = nr;
11151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011152
Bram Moolenaar18144c82006-04-12 21:52:12 +000011153 /* Go to the first window. */
11154 if (put_line(fd, "wincmd t") == FAIL)
11155 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011156
Bram Moolenaar18144c82006-04-12 21:52:12 +000011157 /*
11158 * If more than one window, see if sizes can be restored.
11159 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11160 * resized when moving between windows.
11161 * Do this before restoring the view, so that the topline and the
11162 * cursor can be set. This is done again below.
11163 */
11164 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
11165 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011166 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011167 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168
Bram Moolenaar18144c82006-04-12 21:52:12 +000011169 /*
11170 * Restore the view of the window (options, file, cursor, etc.).
11171 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011172 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011173 {
11174 if (!ses_do_win(wp))
11175 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011176 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11177 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011178 return FAIL;
11179 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11180 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011181 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011182 }
11183
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011184 /* The argument index in the first tab page is zero, need to set it in
11185 * each window. For further tab pages it's the window where we do
11186 * "tabedit". */
11187 cur_arg_idx = next_arg_idx;
11188
Bram Moolenaar18144c82006-04-12 21:52:12 +000011189 /*
11190 * Restore cursor to the current window if it's not the first one.
11191 */
11192 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11193 || put_eol(fd) == FAIL))
11194 return FAIL;
11195
11196 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011197 * Restore window sizes again after jumping around in windows, because
11198 * the current window has a minimum size while others may not.
11199 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011200 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011201 return FAIL;
11202
Bram Moolenaar18144c82006-04-12 21:52:12 +000011203 /* Don't continue in another tab page when doing only the current one
11204 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011205 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011206 break;
11207 }
11208
11209 if (ssop_flags & SSOP_TABPAGES)
11210 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011211 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11212 || put_eol(fd) == FAIL)
11213 return FAIL;
11214 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011215 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11216 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011217
Bram Moolenaar9c102382006-05-03 21:26:49 +000011218 /*
11219 * Wipe out an empty unnamed buffer we started in.
11220 */
11221 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11222 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011223 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011224 return FAIL;
11225 if (put_line(fd, "endif") == FAIL)
11226 return FAIL;
11227 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11228 return FAIL;
11229
11230 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11231 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11232 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11233 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234
11235 /*
11236 * Lastly, execute the x.vim file if it exists.
11237 */
11238 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11239 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011240 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241 || put_line(fd, "endif") == FAIL)
11242 return FAIL;
11243
11244 return OK;
11245}
11246
11247 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011248ses_winsizes(
11249 FILE *fd,
11250 int restore_size,
11251 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252{
11253 int n = 0;
11254 win_T *wp;
11255
11256 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11257 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011258 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259 {
11260 if (!ses_do_win(wp))
11261 continue;
11262 ++n;
11263
11264 /* restore height when not full height */
11265 if (wp->w_height + wp->w_status_height < topframe->fr_height
11266 && (fprintf(fd,
11267 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11268 n, (long)wp->w_height, Rows / 2, Rows) < 0
11269 || put_eol(fd) == FAIL))
11270 return FAIL;
11271
11272 /* restore width when not full width */
11273 if (wp->w_width < Columns && (fprintf(fd,
11274 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11275 n, (long)wp->w_width, Columns / 2, Columns) < 0
11276 || put_eol(fd) == FAIL))
11277 return FAIL;
11278 }
11279 }
11280 else
11281 {
11282 /* Just equalise window sizes */
11283 if (put_line(fd, "wincmd =") == FAIL)
11284 return FAIL;
11285 }
11286 return OK;
11287}
11288
11289/*
11290 * Write commands to "fd" to recursively create windows for frame "fr",
11291 * horizontally and vertically split.
11292 * After the commands the last window in the frame is the current window.
11293 * Returns FAIL when writing the commands to "fd" fails.
11294 */
11295 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011296ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297{
11298 frame_T *frc;
11299 int count = 0;
11300
11301 if (fr->fr_layout != FR_LEAF)
11302 {
11303 /* Find first frame that's not skipped and then create a window for
11304 * each following one (first frame is already there). */
11305 frc = ses_skipframe(fr->fr_child);
11306 if (frc != NULL)
11307 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11308 {
11309 /* Make window as big as possible so that we have lots of room
11310 * to split. */
11311 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11312 || put_line(fd, fr->fr_layout == FR_COL
11313 ? "split" : "vsplit") == FAIL)
11314 return FAIL;
11315 ++count;
11316 }
11317
11318 /* Go back to the first window. */
11319 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11320 ? "%dwincmd k" : "%dwincmd h", count) < 0
11321 || put_eol(fd) == FAIL))
11322 return FAIL;
11323
11324 /* Recursively create frames/windows in each window of this column or
11325 * row. */
11326 frc = ses_skipframe(fr->fr_child);
11327 while (frc != NULL)
11328 {
11329 ses_win_rec(fd, frc);
11330 frc = ses_skipframe(frc->fr_next);
11331 /* Go to next window. */
11332 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11333 return FAIL;
11334 }
11335 }
11336 return OK;
11337}
11338
11339/*
11340 * Skip frames that don't contain windows we want to save in the Session.
11341 * Returns NULL when there none.
11342 */
11343 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011344ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345{
11346 frame_T *frc;
11347
11348 for (frc = fr; frc != NULL; frc = frc->fr_next)
11349 if (ses_do_frame(frc))
11350 break;
11351 return frc;
11352}
11353
11354/*
11355 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11356 * the Session.
11357 */
11358 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011359ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360{
11361 frame_T *frc;
11362
11363 if (fr->fr_layout == FR_LEAF)
11364 return ses_do_win(fr->fr_win);
11365 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11366 if (ses_do_frame(frc))
11367 return TRUE;
11368 return FALSE;
11369}
11370
11371/*
11372 * Return non-zero if window "wp" is to be stored in the Session.
11373 */
11374 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011375ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011376{
11377 if (wp->w_buffer->b_fname == NULL
11378#ifdef FEAT_QUICKFIX
11379 /* When 'buftype' is "nofile" can't restore the window contents. */
11380 || bt_nofile(wp->w_buffer)
11381#endif
11382 )
11383 return (ssop_flags & SSOP_BLANK);
11384 if (wp->w_buffer->b_help)
11385 return (ssop_flags & SSOP_HELP);
11386 return TRUE;
11387}
11388
11389/*
11390 * Write commands to "fd" to restore the view of a window.
11391 * Caller must make sure 'scrolloff' is zero.
11392 */
11393 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011394put_view(
11395 FILE *fd,
11396 win_T *wp,
11397 int add_edit, /* add ":edit" command to view */
11398 unsigned *flagp, /* vop_flags or ssop_flags */
11399 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011400 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401{
11402 win_T *save_curwin;
11403 int f;
11404 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011405 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011406
11407 /* Always restore cursor position for ":mksession". For ":mkview" only
11408 * when 'viewoptions' contains "cursor". */
11409 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11410
11411 /*
11412 * Local argument list.
11413 */
11414 if (wp->w_alist == &global_alist)
11415 {
11416 if (put_line(fd, "argglobal") == FAIL)
11417 return FAIL;
11418 }
11419 else
11420 {
11421 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11422 flagp == &vop_flags
11423 || !(*flagp & SSOP_CURDIR)
11424 || wp->w_localdir != NULL, flagp) == FAIL)
11425 return FAIL;
11426 }
11427
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011428 /* Only when part of a session: restore the argument index. Some
11429 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011430 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011431 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011433 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434 || put_eol(fd) == FAIL)
11435 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011436 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437 }
11438
11439 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011440 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011441 {
11442 /*
11443 * Load the file.
11444 */
11445 if (wp->w_buffer->b_ffname != NULL
11446#ifdef FEAT_QUICKFIX
11447 && !bt_nofile(wp->w_buffer)
11448#endif
11449 )
11450 {
11451 /*
11452 * Editing a file in this buffer: use ":edit file".
11453 * This may have side effects! (e.g., compressed or network file).
11454 */
11455 if (fputs("edit ", fd) < 0
11456 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11457 return FAIL;
11458 }
11459 else
11460 {
11461 /* No file in this buffer, just make it empty. */
11462 if (put_line(fd, "enew") == FAIL)
11463 return FAIL;
11464#ifdef FEAT_QUICKFIX
11465 if (wp->w_buffer->b_ffname != NULL)
11466 {
11467 /* The buffer does have a name, but it's not a file name. */
11468 if (fputs("file ", fd) < 0
11469 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11470 return FAIL;
11471 }
11472#endif
11473 do_cursor = FALSE;
11474 }
11475 }
11476
11477 /*
11478 * Local mappings and abbreviations.
11479 */
11480 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11481 && makemap(fd, wp->w_buffer) == FAIL)
11482 return FAIL;
11483
11484 /*
11485 * Local options. Need to go to the window temporarily.
11486 * Store only local values when using ":mkview" and when ":mksession" is
11487 * used and 'sessionoptions' doesn't include "options".
11488 * Some folding options are always stored when "folds" is included,
11489 * otherwise the folds would not be restored correctly.
11490 */
11491 save_curwin = curwin;
11492 curwin = wp;
11493 curbuf = curwin->w_buffer;
11494 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11495 f = makeset(fd, OPT_LOCAL,
11496 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11497#ifdef FEAT_FOLDING
11498 else if (*flagp & SSOP_FOLDS)
11499 f = makefoldset(fd);
11500#endif
11501 else
11502 f = OK;
11503 curwin = save_curwin;
11504 curbuf = curwin->w_buffer;
11505 if (f == FAIL)
11506 return FAIL;
11507
11508#ifdef FEAT_FOLDING
11509 /*
11510 * Save Folds when 'buftype' is empty and for help files.
11511 */
11512 if ((*flagp & SSOP_FOLDS)
11513 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011514# ifdef FEAT_QUICKFIX
11515 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11516# endif
11517 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518 {
11519 if (put_folds(fd, wp) == FAIL)
11520 return FAIL;
11521 }
11522#endif
11523
11524 /*
11525 * Set the cursor after creating folds, since that moves the cursor.
11526 */
11527 if (do_cursor)
11528 {
11529
11530 /* Restore the cursor line in the file and relatively in the
11531 * window. Don't use "G", it changes the jumplist. */
11532 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11533 (long)wp->w_cursor.lnum,
11534 (long)(wp->w_cursor.lnum - wp->w_topline),
11535 (long)wp->w_height / 2, (long)wp->w_height) < 0
11536 || put_eol(fd) == FAIL
11537 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11538 || put_line(fd, "exe s:l") == FAIL
11539 || put_line(fd, "normal! zt") == FAIL
11540 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11541 || put_eol(fd) == FAIL)
11542 return FAIL;
11543 /* Restore the cursor column and left offset when not wrapping. */
11544 if (wp->w_cursor.col == 0)
11545 {
11546 if (put_line(fd, "normal! 0") == FAIL)
11547 return FAIL;
11548 }
11549 else
11550 {
11551 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11552 {
11553 if (fprintf(fd,
11554 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011555 (long)wp->w_virtcol + 1,
11556 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011557 (long)wp->w_width / 2, (long)wp->w_width) < 0
11558 || put_eol(fd) == FAIL
11559 || put_line(fd, "if s:c > 0") == FAIL
11560 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011561 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11562 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011563 || put_eol(fd) == FAIL
11564 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011565 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566 || put_eol(fd) == FAIL
11567 || put_line(fd, "endif") == FAIL)
11568 return FAIL;
11569 }
11570 else
11571 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011572 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573 || put_eol(fd) == FAIL)
11574 return FAIL;
11575 }
11576 }
11577 }
11578
11579 /*
11580 * Local directory.
11581 */
11582 if (wp->w_localdir != NULL)
11583 {
11584 if (fputs("lcd ", fd) < 0
11585 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11586 || put_eol(fd) == FAIL)
11587 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011588 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011589 }
11590
11591 return OK;
11592}
11593
11594/*
11595 * Write an argument list to the session file.
11596 * Returns FAIL if writing fails.
11597 */
11598 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011599ses_arglist(
11600 FILE *fd,
11601 char *cmd,
11602 garray_T *gap,
11603 int fullname, /* TRUE: use full path name */
11604 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011605{
11606 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011607 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608 char_u *s;
11609
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011610 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11611 return FAIL;
11612 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011613 return FAIL;
11614 for (i = 0; i < gap->ga_len; ++i)
11615 {
11616 /* NULL file names are skipped (only happens when out of memory). */
11617 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11618 if (s != NULL)
11619 {
11620 if (fullname)
11621 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011622 buf = alloc(MAXPATHL);
11623 if (buf != NULL)
11624 {
11625 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11626 s = buf;
11627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011628 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011629 if (fputs("argadd ", fd) < 0
11630 || ses_put_fname(fd, s, flagp) == FAIL
11631 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011632 {
11633 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011634 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011635 }
11636 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011637 }
11638 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011639 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011640}
11641
11642/*
11643 * Write a buffer name to the session file.
11644 * Also ends the line.
11645 * Returns FAIL if writing fails.
11646 */
11647 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011648ses_fname(FILE *fd, buf_T *buf, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011649{
11650 char_u *name;
11651
11652 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011653 * the session file will be sourced.
11654 * Don't do this for ":mkview", we don't know the current directory.
11655 * Don't do this after ":lcd", we don't keep track of what the current
11656 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011657 if (buf->b_sfname != NULL
11658 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011659 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011660#ifdef FEAT_AUTOCHDIR
11661 && !p_acd
11662#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011663 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011664 name = buf->b_sfname;
11665 else
11666 name = buf->b_ffname;
11667 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11668 return FAIL;
11669 return OK;
11670}
11671
11672/*
11673 * Write a file name to the session file.
11674 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11675 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011676 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677 */
11678 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011679ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680{
11681 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011682 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011683 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011684
11685 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011686 if (sname == NULL)
11687 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011689 if (*flagp & SSOP_SLASH)
11690 {
11691 /* change all backslashes to forward slashes */
11692 for (p = sname; *p != NUL; mb_ptr_adv(p))
11693 if (*p == '\\')
11694 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011695 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011696
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011697 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011698 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011699 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011700 if (p == NULL)
11701 return FAIL;
11702
11703 /* write the result */
11704 if (fputs((char *)p, fd) < 0)
11705 retval = FAIL;
11706
11707 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011708 return retval;
11709}
11710
11711/*
11712 * ":loadview [nr]"
11713 */
11714 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011715ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011716{
11717 char_u *fname;
11718
11719 fname = get_view_file(*eap->arg);
11720 if (fname != NULL)
11721 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011722 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011723 vim_free(fname);
11724 }
11725}
11726
11727/*
11728 * Get the name of the view file for the current buffer.
11729 */
11730 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011731get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011732{
11733 int len = 0;
11734 char_u *p, *s;
11735 char_u *retval;
11736 char_u *sname;
11737
11738 if (curbuf->b_ffname == NULL)
11739 {
11740 EMSG(_(e_noname));
11741 return NULL;
11742 }
11743 sname = home_replace_save(NULL, curbuf->b_ffname);
11744 if (sname == NULL)
11745 return NULL;
11746
11747 /*
11748 * We want a file name without separators, because we're not going to make
11749 * a directory.
11750 * "normal" path separator -> "=+"
11751 * "=" -> "=="
11752 * ":" path separator -> "=-"
11753 */
11754 for (p = sname; *p; ++p)
11755 if (*p == '=' || vim_ispathsep(*p))
11756 ++len;
11757 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11758 if (retval != NULL)
11759 {
11760 STRCPY(retval, p_vdir);
11761 add_pathsep(retval);
11762 s = retval + STRLEN(retval);
11763 for (p = sname; *p; ++p)
11764 {
11765 if (*p == '=')
11766 {
11767 *s++ = '=';
11768 *s++ = '=';
11769 }
11770 else if (vim_ispathsep(*p))
11771 {
11772 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011773#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774 if (*p == ':')
11775 *s++ = '-';
11776 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011777#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011778 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011779 }
11780 else
11781 *s++ = *p;
11782 }
11783 *s++ = '=';
11784 *s++ = c;
11785 STRCPY(s, ".vim");
11786 }
11787
11788 vim_free(sname);
11789 return retval;
11790}
11791
11792#endif /* FEAT_SESSION */
11793
11794/*
11795 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11796 * Return FAIL for a write error.
11797 */
11798 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011799put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011800{
11801 if (
11802#ifdef USE_CRNL
11803 (
11804# ifdef MKSESSION_NL
11805 !mksession_nl &&
11806# endif
11807 (putc('\r', fd) < 0)) ||
11808#endif
11809 (putc('\n', fd) < 0))
11810 return FAIL;
11811 return OK;
11812}
11813
11814/*
11815 * Write a line to "fd".
11816 * Return FAIL for a write error.
11817 */
11818 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011819put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011820{
11821 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11822 return FAIL;
11823 return OK;
11824}
11825
11826#ifdef FEAT_VIMINFO
11827/*
11828 * ":rviminfo" and ":wviminfo".
11829 */
11830 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011831ex_viminfo(
11832 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011833{
11834 char_u *save_viminfo;
11835
11836 save_viminfo = p_viminfo;
11837 if (*p_viminfo == NUL)
11838 p_viminfo = (char_u *)"'100";
11839 if (eap->cmdidx == CMD_rviminfo)
11840 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011841 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11842 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011843 EMSG(_("E195: Cannot open viminfo file for reading"));
11844 }
11845 else
11846 write_viminfo(eap->arg, eap->forceit);
11847 p_viminfo = save_viminfo;
11848}
11849#endif
11850
11851#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011852/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011853 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011854 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011855 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011857dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859 if (fname == NULL)
11860 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011861 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011862}
11863#endif
11864
11865/*
11866 * ":behave {mswin,xterm}"
11867 */
11868 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011869ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011870{
11871 if (STRCMP(eap->arg, "mswin") == 0)
11872 {
11873 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11874 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11875 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11876 set_option_value((char_u *)"keymodel", 0L,
11877 (char_u *)"startsel,stopsel", 0);
11878 }
11879 else if (STRCMP(eap->arg, "xterm") == 0)
11880 {
11881 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11882 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11883 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11884 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11885 }
11886 else
11887 EMSG2(_(e_invarg2), eap->arg);
11888}
11889
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011890#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11891/*
11892 * Function given to ExpandGeneric() to obtain the possible arguments of the
11893 * ":behave {mswin,xterm}" command.
11894 */
11895 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011896get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011897{
11898 if (idx == 0)
11899 return (char_u *)"mswin";
11900 if (idx == 1)
11901 return (char_u *)"xterm";
11902 return NULL;
11903}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020011904
11905/*
11906 * Function given to ExpandGeneric() to obtain the possible arguments of the
11907 * ":messages {clear}" command.
11908 */
11909 char_u *
11910get_messages_arg(expand_T *xp UNUSED, int idx)
11911{
11912 if (idx == 0)
11913 return (char_u *)"clear";
11914 return NULL;
11915}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011916#endif
11917
Bram Moolenaar071d4272004-06-13 20:20:40 +000011918#ifdef FEAT_AUTOCMD
11919static int filetype_detect = FALSE;
11920static int filetype_plugin = FALSE;
11921static int filetype_indent = FALSE;
11922
11923/*
11924 * ":filetype [plugin] [indent] {on,off,detect}"
11925 * on: Load the filetype.vim file to install autocommands for file types.
11926 * off: Load the ftoff.vim file to remove all autocommands for file types.
11927 * plugin on: load filetype.vim and ftplugin.vim
11928 * plugin off: load ftplugof.vim
11929 * indent on: load filetype.vim and indent.vim
11930 * indent off: load indoff.vim
11931 */
11932 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011933ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011934{
11935 char_u *arg = eap->arg;
11936 int plugin = FALSE;
11937 int indent = FALSE;
11938
11939 if (*eap->arg == NUL)
11940 {
11941 /* Print current status. */
11942 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11943 filetype_detect ? "ON" : "OFF",
11944 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11945 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11946 return;
11947 }
11948
11949 /* Accept "plugin" and "indent" in any order. */
11950 for (;;)
11951 {
11952 if (STRNCMP(arg, "plugin", 6) == 0)
11953 {
11954 plugin = TRUE;
11955 arg = skipwhite(arg + 6);
11956 continue;
11957 }
11958 if (STRNCMP(arg, "indent", 6) == 0)
11959 {
11960 indent = TRUE;
11961 arg = skipwhite(arg + 6);
11962 continue;
11963 }
11964 break;
11965 }
11966 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11967 {
11968 if (*arg == 'o' || !filetype_detect)
11969 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011970 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971 filetype_detect = TRUE;
11972 if (plugin)
11973 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011974 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975 filetype_plugin = TRUE;
11976 }
11977 if (indent)
11978 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011979 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011980 filetype_indent = TRUE;
11981 }
11982 }
11983 if (*arg == 'd')
11984 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020011985 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011986 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011987 }
11988 }
11989 else if (STRCMP(arg, "off") == 0)
11990 {
11991 if (plugin || indent)
11992 {
11993 if (plugin)
11994 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011995 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996 filetype_plugin = FALSE;
11997 }
11998 if (indent)
11999 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012000 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012001 filetype_indent = FALSE;
12002 }
12003 }
12004 else
12005 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012006 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012007 filetype_detect = FALSE;
12008 }
12009 }
12010 else
12011 EMSG2(_(e_invarg2), arg);
12012}
12013
12014/*
12015 * ":setfiletype {name}"
12016 */
12017 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012018ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019{
12020 if (!did_filetype)
12021 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
12022}
12023#endif
12024
Bram Moolenaar071d4272004-06-13 20:20:40 +000012025 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012026ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012027{
12028#ifdef FEAT_DIGRAPHS
12029 if (*eap->arg != NUL)
12030 putdigraph(eap->arg);
12031 else
12032 listdigraphs();
12033#else
12034 EMSG(_("E196: No digraphs in this version"));
12035#endif
12036}
12037
12038 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012039ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012040{
12041 int flags = 0;
12042
12043 if (eap->cmdidx == CMD_setlocal)
12044 flags = OPT_LOCAL;
12045 else if (eap->cmdidx == CMD_setglobal)
12046 flags = OPT_GLOBAL;
12047#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
12048 if (cmdmod.browse && flags == 0)
12049 ex_options(eap);
12050 else
12051#endif
12052 (void)do_set(eap->arg, flags);
12053}
12054
12055#ifdef FEAT_SEARCH_EXTRA
12056/*
12057 * ":nohlsearch"
12058 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012060ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012061{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010012062 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012063 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064}
12065
12066/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012067 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012068 * Sets nextcmd to the start of the next command, if any. Also called when
12069 * skipping commands to find the next command.
12070 */
12071 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012072ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073{
12074 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012075 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012076 char_u *end;
12077 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012078 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012079
12080 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012081 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012082 else
12083 {
12084 EMSG(e_invcmd);
12085 return;
12086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012087
12088 /* First clear any old pattern. */
12089 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012090 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091
12092 if (ends_excmd(*eap->arg))
12093 end = eap->arg;
12094 else if ((STRNICMP(eap->arg, "none", 4) == 0
12095 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
12096 end = eap->arg + 4;
12097 else
12098 {
12099 p = skiptowhite(eap->arg);
12100 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012101 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012102 p = skipwhite(p);
12103 if (*p == NUL)
12104 {
12105 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012106 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012107 EMSG2(_(e_invarg2), eap->arg);
12108 return;
12109 }
12110 end = skip_regexp(p + 1, *p, TRUE, NULL);
12111 if (!eap->skip)
12112 {
12113 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12114 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012115 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012116 eap->errmsg = e_trailing;
12117 return;
12118 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012119 if (*end != *p)
12120 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012121 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012122 EMSG2(_(e_invarg2), p);
12123 return;
12124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012125
12126 c = *end;
12127 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012128 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012129 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012130 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012131 }
12132 }
12133 eap->nextcmd = find_nextcmd(end);
12134}
12135#endif
12136
12137#ifdef FEAT_CRYPT
12138/*
12139 * ":X": Get crypt key
12140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012142ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012143{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012144 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012145 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012146}
12147#endif
12148
12149#ifdef FEAT_FOLDING
12150 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012151ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012152{
12153 if (foldManualAllowed(TRUE))
12154 foldCreate(eap->line1, eap->line2);
12155}
12156
12157 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012158ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159{
12160 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12161 eap->forceit, FALSE);
12162}
12163
12164 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012165ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012166{
12167 linenr_T lnum;
12168
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012169#ifdef FEAT_CLIPBOARD
12170 start_global_changes();
12171#endif
12172
Bram Moolenaar071d4272004-06-13 20:20:40 +000012173 /* First set the marks for all lines closed/open. */
12174 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12175 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12176 ml_setmarked(lnum);
12177
12178 /* Execute the command on the marked lines. */
12179 global_exe(eap->arg);
12180 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012181#ifdef FEAT_CLIPBOARD
12182 end_global_changes();
12183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184}
12185#endif