blob: cff41cf1486b64197ca3dc44aee01d901c06c3cb [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);
Bram Moolenaarded27822017-01-02 14:27:34 +0100140static linenr_T get_address(exarg_T *, char_u **, int addr_type, int skip, int to_other_file, int address_count);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100141static 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
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100790 * here. The value of 200 allows nested function calls, ":source", etc.
791 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100792 if (call_depth >= 200
793#ifdef FEAT_EVAL
794 && call_depth >= p_mfd
795#endif
796 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 {
798 EMSG(_("E169: Command too recursive"));
799#ifdef FEAT_EVAL
800 /* When converting to an exception, we do not include the command name
801 * since this is not an error of the specific command. */
802 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
803 msg_list = saved_msg_list;
804#endif
805 return FAIL;
806 }
807 ++call_depth;
808
809#ifdef FEAT_EVAL
810 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000811 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 cstack.cs_trylevel = 0;
813 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000814 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
816
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100817 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818
819 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100820 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000821 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 ++ex_nesting_level;
823
824 /* Get the function or script name and the address where the next breakpoint
825 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000826 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 {
828 fname = func_name(real_cookie);
829 breakpoint = func_breakpoint(real_cookie);
830 dbg_tick = func_dbg_tick(real_cookie);
831 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100832 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 {
834 fname = sourcing_name;
835 breakpoint = source_breakpoint(real_cookie);
836 dbg_tick = source_dbg_tick(real_cookie);
837 }
838
839 /*
840 * Initialize "force_abort" and "suppress_errthrow" at the top level.
841 */
842 if (!recursive)
843 {
844 force_abort = FALSE;
845 suppress_errthrow = FALSE;
846 }
847
848 /*
849 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000850 * exception handling (used when debugging). Otherwise clear it to avoid
851 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000853 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000854 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000855 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200856 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857
858 initial_trylevel = trylevel;
859
860 /*
861 * "did_throw" will be set to TRUE when an exception is being thrown.
862 */
863 did_throw = FALSE;
864#endif
865 /*
866 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000867 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 * If force_abort is set, we cancel everything.
869 */
870 did_emsg = FALSE;
871
872 /*
873 * KeyTyped is only set when calling vgetc(). Reset it here when not
874 * calling vgetc() (sourced command lines).
875 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100876 if (!(flags & DOCMD_KEYTYPED)
877 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 KeyTyped = FALSE;
879
880 /*
881 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000882 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 * - for multiple commands on one line, separated with '|'
884 * - when repeating until there are no more lines (for ":source")
885 */
886 next_cmdline = cmdline;
887 do
888 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000889#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100890 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000891#endif
892
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000893 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 if (next_cmdline == NULL
895#ifdef FEAT_EVAL
896 && !force_abort
897 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000898 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899#endif
900 )
901 did_emsg = FALSE;
902
903 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000904 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100905 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 * 3. If a line is given: Make a copy, so we can mess with it.
907 */
908
909#ifdef FEAT_EVAL
910 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000911 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 {
913 /* Each '|' separated command is stored separately in lines_ga, to
914 * be able to jump to it. Don't use next_cmdline now. */
915 vim_free(cmdline_copy);
916 cmdline_copy = NULL;
917
918 /* Check if a function has returned or, unless it has an unclosed
919 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000920 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000922# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000923 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000924 func_line_end(real_cookie);
925# endif
926 if (func_has_ended(real_cookie))
927 {
928 retval = FAIL;
929 break;
930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000932#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000933 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100934 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000935 script_line_end();
936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937
938 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100939 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 {
941 retval = FAIL;
942 break;
943 }
944
945 /* If breakpoints have been added/deleted need to check for it. */
946 if (breakpoint != NULL && dbg_tick != NULL
947 && *dbg_tick != debug_tick)
948 {
949 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100950 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 fname, sourcing_lnum);
952 *dbg_tick = debug_tick;
953 }
954
955 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
956 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
957
958 /* Did we encounter a breakpoint? */
959 if (breakpoint != NULL && *breakpoint != 0
960 && *breakpoint <= sourcing_lnum)
961 {
962 dbg_breakpoint(fname, sourcing_lnum);
963 /* Find next breakpoint. */
964 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100965 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 fname, sourcing_lnum);
967 *dbg_tick = debug_tick;
968 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000969# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000970 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000971 {
972 if (getline_is_func)
973 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100974 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000975 script_line_start();
976 }
977# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 }
979
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000980 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000982 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100983 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 * below, so that it stores lines in or reads them from
985 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000986 * while/for loop. */
987 cmd_getline = get_loop_line;
988 cmd_cookie = (void *)&cmd_loop_cookie;
989 cmd_loop_cookie.lines_gap = &lines_ga;
990 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100991 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000992 cmd_loop_cookie.cookie = cookie;
993 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 }
995 else
996 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100997 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 cmd_cookie = cookie;
999 }
1000#endif
1001
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001002 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 if (next_cmdline == NULL)
1004 {
1005 /*
1006 * Need to set msg_didout for the first line after an ":if",
1007 * otherwise the ":if" will be overwritten.
1008 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001009 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001011 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012#ifdef FEAT_EVAL
1013 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1014#else
1015 0
1016#endif
1017 )) == NULL)
1018 {
1019 /* Don't call wait_return for aborted command line. The NULL
1020 * returned for the end of a sourced file or executed function
1021 * doesn't do this. */
1022 if (KeyTyped && !(flags & DOCMD_REPEAT))
1023 need_wait_return = FALSE;
1024 retval = FAIL;
1025 break;
1026 }
1027 used_getline = TRUE;
1028
1029 /*
1030 * Keep the first typed line. Clear it when more lines are typed.
1031 */
1032 if (flags & DOCMD_KEEPLINE)
1033 {
1034 vim_free(repeat_cmdline);
1035 if (count == 0)
1036 repeat_cmdline = vim_strsave(next_cmdline);
1037 else
1038 repeat_cmdline = NULL;
1039 }
1040 }
1041
1042 /* 3. Make a copy of the command so we can mess with it. */
1043 else if (cmdline_copy == NULL)
1044 {
1045 next_cmdline = vim_strsave(next_cmdline);
1046 if (next_cmdline == NULL)
1047 {
1048 EMSG(_(e_outofmem));
1049 retval = FAIL;
1050 break;
1051 }
1052 }
1053 cmdline_copy = next_cmdline;
1054
1055#ifdef FEAT_EVAL
1056 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001057 * Save the current line when inside a ":while" or ":for", and when
1058 * the command looks like a ":while" or ":for", because we may need it
1059 * later. When there is a '|' and another command, it is stored
1060 * separately, because we need to be able to jump back to it from an
1061 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001063 if (current_line == lines_ga.ga_len
1064 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001066 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 {
1068 retval = FAIL;
1069 break;
1070 }
1071 }
1072 did_endif = FALSE;
1073#endif
1074
1075 if (count++ == 0)
1076 {
1077 /*
1078 * All output from the commands is put below each other, without
1079 * waiting for a return. Don't do this when executing commands
1080 * from a script or when being called recursive (e.g. for ":e
1081 * +command file").
1082 */
1083 if (!(flags & DOCMD_NOWAIT) && !recursive)
1084 {
1085 msg_didout_before_start = msg_didout;
1086 msg_didany = FALSE; /* no output yet */
1087 msg_start();
1088 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001089 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 ++RedrawingDisabled;
1091 did_inc = TRUE;
1092 }
1093 }
1094
1095 if (p_verbose >= 15 && sourcing_name != NULL)
1096 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001098 verbose_enter_scroll();
1099
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 smsg((char_u *)_("line %ld: %s"),
1101 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001102 if (msg_silent == 0)
1103 msg_puts((char_u *)"\n"); /* don't overwrite this */
1104
1105 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 --no_wait_return;
1107 }
1108
1109 /*
1110 * 2. Execute one '|' separated command.
1111 * do_one_cmd() will return NULL if there is no trailing '|'.
1112 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1113 */
1114 ++recursive;
1115 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1116#ifdef FEAT_EVAL
1117 &cstack,
1118#endif
1119 cmd_getline, cmd_cookie);
1120 --recursive;
1121
1122#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001123 if (cmd_cookie == (void *)&cmd_loop_cookie)
1124 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001126 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127#endif
1128
1129 if (next_cmdline == NULL)
1130 {
1131 vim_free(cmdline_copy);
1132 cmdline_copy = NULL;
1133#ifdef FEAT_CMDHIST
1134 /*
1135 * If the command was typed, remember it for the ':' register.
1136 * Do this AFTER executing the command to make :@: work.
1137 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001138 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 && new_last_cmdline != NULL)
1140 {
1141 vim_free(last_cmdline);
1142 last_cmdline = new_last_cmdline;
1143 new_last_cmdline = NULL;
1144 }
1145#endif
1146 }
1147 else
1148 {
1149 /* need to copy the command after the '|' to cmdline_copy, for the
1150 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001151 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 next_cmdline = cmdline_copy;
1153 }
1154
1155
1156#ifdef FEAT_EVAL
1157 /* reset did_emsg for a function that is not aborted by an error */
1158 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001159 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 && !func_has_abort(real_cookie))
1161 did_emsg = FALSE;
1162
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001163 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 {
1165 ++current_line;
1166
1167 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001168 * An ":endwhile", ":endfor" and ":continue" is handled here.
1169 * If we were executing commands, jump back to the ":while" or
1170 * ":for".
1171 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001173 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001175 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001177 /* Jump back to the matching ":while" or ":for". Be careful
1178 * not to use a cs_line[] from an entry that isn't a ":while"
1179 * or ":for": It would make "current_line" invalid and can
1180 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 if (!did_emsg && !got_int && !did_throw
1182 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001183 && (cstack.cs_flags[cstack.cs_idx]
1184 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 && cstack.cs_line[cstack.cs_idx] >= 0
1186 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1187 {
1188 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001189 /* remember we jumped there */
1190 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 line_breakcheck(); /* check if CTRL-C typed */
1192
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001193 /* Check for the next breakpoint at or after the ":while"
1194 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 if (breakpoint != NULL)
1196 {
1197 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001198 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 fname,
1200 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1201 *dbg_tick = debug_tick;
1202 }
1203 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001204 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001206 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001208 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1209 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 }
1211 }
1212
1213 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001214 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001216 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001218 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1220 }
1221 }
1222
1223 /*
1224 * When not inside any ":while" loop, clear remembered lines.
1225 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001226 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 {
1228 if (lines_ga.ga_len > 0)
1229 {
1230 sourcing_lnum =
1231 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1232 free_cmdlines(&lines_ga);
1233 }
1234 current_line = 0;
1235 }
1236
1237 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001238 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1239 * being restored at the ":endtry". Reset them here and set the
1240 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1241 * This includes the case where a missing ":endif", ":endwhile" or
1242 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001244 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001246 cstack.cs_lflags &= ~CSL_HAD_FINA;
1247 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1248 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 did_throw ? (void *)current_exception : NULL);
1250 did_emsg = got_int = did_throw = FALSE;
1251 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1252 }
1253
1254 /* Update global "trylevel" for recursive calls to do_cmdline() from
1255 * within this loop. */
1256 trylevel = initial_trylevel + cstack.cs_trylevel;
1257
1258 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001259 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 * files) is aborted because of an error, an interrupt, or an uncaught
1261 * exception, cancel everything. If it is left normally, reset
1262 * force_abort to get the non-EH compatible abortion behavior for
1263 * the rest of the script.
1264 */
1265 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1266 force_abort = FALSE;
1267
1268 /* Convert an interrupt to an exception if appropriate. */
1269 (void)do_intthrow(&cstack);
1270#endif /* FEAT_EVAL */
1271
1272 }
1273 /*
1274 * Continue executing command lines when:
1275 * - no CTRL-C typed, no aborting error, no exception thrown or try
1276 * conditionals need to be checked for executing finally clauses or
1277 * catching an interrupt exception
1278 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001279 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 * looping for ":source" command or function call.
1281 */
1282 while (!((got_int
1283#ifdef FEAT_EVAL
1284 || (did_emsg && force_abort) || did_throw
1285#endif
1286 )
1287#ifdef FEAT_EVAL
1288 && cstack.cs_trylevel == 0
1289#endif
1290 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001291 && !(did_emsg
1292#ifdef FEAT_EVAL
1293 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001294 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001295 * the :endtry to be missed. */
1296 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1297#endif
1298 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001299 && (getline_equal(fgetline, cookie, getexmodeline)
1300 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 && (next_cmdline != NULL
1302#ifdef FEAT_EVAL
1303 || cstack.cs_idx >= 0
1304#endif
1305 || (flags & DOCMD_REPEAT)));
1306
1307 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001308 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309#ifdef FEAT_EVAL
1310 free_cmdlines(&lines_ga);
1311 ga_clear(&lines_ga);
1312
1313 if (cstack.cs_idx >= 0)
1314 {
1315 /*
1316 * If a sourced file or executed function ran to its end, report the
1317 * unclosed conditional.
1318 */
1319 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001320 && ((getline_equal(fgetline, cookie, getsourceline)
1321 && !source_finished(fgetline, cookie))
1322 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 && !func_has_ended(real_cookie))))
1324 {
1325 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1326 EMSG(_(e_endtry));
1327 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1328 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001329 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1330 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 else
1332 EMSG(_(e_endif));
1333 }
1334
1335 /*
1336 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1337 * ":endtry" in a sourced file or executed function. If the try
1338 * conditional is in its finally clause, ignore anything pending.
1339 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001340 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 */
1342 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001343 {
1344 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1345
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001346 if (idx >= 0)
1347 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001348 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1349 &cstack.cs_looplevel);
1350 }
1351 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 trylevel = initial_trylevel;
1353 }
1354
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001355 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1356 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001358 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 ? (char_u *)"endfunction" : (char_u *)NULL);
1360
1361 if (trylevel == 0)
1362 {
1363 /*
1364 * When an exception is being thrown out of the outermost try
1365 * conditional, discard the uncaught exception, disable the conversion
1366 * of interrupts or errors to exceptions, and ensure that no more
1367 * commands are executed.
1368 */
1369 if (did_throw)
1370 {
1371 void *p = NULL;
1372 char_u *saved_sourcing_name;
1373 int saved_sourcing_lnum;
1374 struct msglist *messages = NULL, *next;
1375
1376 /*
1377 * If the uncaught exception is a user exception, report it as an
1378 * error. If it is an error exception, display the saved error
1379 * message now. For an interrupt exception, do nothing; the
1380 * interrupt message is given elsewhere.
1381 */
1382 switch (current_exception->type)
1383 {
1384 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001385 vim_snprintf((char *)IObuff, IOSIZE,
1386 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 current_exception->value);
1388 p = vim_strsave(IObuff);
1389 break;
1390 case ET_ERROR:
1391 messages = current_exception->messages;
1392 current_exception->messages = NULL;
1393 break;
1394 case ET_INTERRUPT:
1395 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 }
1397
1398 saved_sourcing_name = sourcing_name;
1399 saved_sourcing_lnum = sourcing_lnum;
1400 sourcing_name = current_exception->throw_name;
1401 sourcing_lnum = current_exception->throw_lnum;
1402 current_exception->throw_name = NULL;
1403
1404 discard_current_exception(); /* uses IObuff if 'verbose' */
1405 suppress_errthrow = TRUE;
1406 force_abort = TRUE;
1407
1408 if (messages != NULL)
1409 {
1410 do
1411 {
1412 next = messages->next;
1413 emsg(messages->msg);
1414 vim_free(messages->msg);
1415 vim_free(messages);
1416 messages = next;
1417 }
1418 while (messages != NULL);
1419 }
1420 else if (p != NULL)
1421 {
1422 emsg(p);
1423 vim_free(p);
1424 }
1425 vim_free(sourcing_name);
1426 sourcing_name = saved_sourcing_name;
1427 sourcing_lnum = saved_sourcing_lnum;
1428 }
1429
1430 /*
1431 * On an interrupt or an aborting error not converted to an exception,
1432 * disable the conversion of errors to exceptions. (Interrupts are not
1433 * converted any more, here.) This enables also the interrupt message
1434 * when force_abort is set and did_emsg unset in case of an interrupt
1435 * from a finally clause after an error.
1436 */
1437 else if (got_int || (did_emsg && force_abort))
1438 suppress_errthrow = TRUE;
1439 }
1440
1441 /*
1442 * The current cstack will be freed when do_cmdline() returns. An uncaught
1443 * exception will have to be rethrown in the previous cstack. If a function
1444 * has just returned or a script file was just finished and the previous
1445 * cstack belongs to the same function or, respectively, script file, it
1446 * will have to be checked for finally clauses to be executed due to the
1447 * ":return" or ":finish". This is done in do_one_cmd().
1448 */
1449 if (did_throw)
1450 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001451 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001453 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 && ex_nesting_level > func_level(real_cookie) + 1))
1455 {
1456 if (!did_throw)
1457 check_cstack = TRUE;
1458 }
1459 else
1460 {
1461 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001462 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 --ex_nesting_level;
1464 /*
1465 * Go to debug mode when returning from a function in which we are
1466 * single-stepping.
1467 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001468 if ((getline_equal(fgetline, cookie, getsourceline)
1469 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001471 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 ? (char_u *)_("End of sourced file")
1473 : (char_u *)_("End of function"));
1474 }
1475
1476 /*
1477 * Restore the exception environment (done after returning from the
1478 * debugger).
1479 */
1480 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001481 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482
1483 msg_list = saved_msg_list;
1484#endif /* FEAT_EVAL */
1485
1486 /*
1487 * If there was too much output to fit on the command line, ask the user to
1488 * hit return before redrawing the screen. With the ":global" command we do
1489 * this only once after the command is finished.
1490 */
1491 if (did_inc)
1492 {
1493 --RedrawingDisabled;
1494 --no_wait_return;
1495 msg_scroll = FALSE;
1496
1497 /*
1498 * When just finished an ":if"-":else" which was typed, no need to
1499 * wait for hit-return. Also for an error situation.
1500 */
1501 if (retval == FAIL
1502#ifdef FEAT_EVAL
1503 || (did_endif && KeyTyped && !did_emsg)
1504#endif
1505 )
1506 {
1507 need_wait_return = FALSE;
1508 msg_didany = FALSE; /* don't wait when restarting edit */
1509 }
1510 else if (need_wait_return)
1511 {
1512 /*
1513 * The msg_start() above clears msg_didout. The wait_return we do
1514 * here should not overwrite the command that may be shown before
1515 * doing that.
1516 */
1517 msg_didout |= msg_didout_before_start;
1518 wait_return(FALSE);
1519 }
1520 }
1521
Bram Moolenaar2a942252012-11-28 23:03:07 +01001522#ifdef FEAT_EVAL
1523 did_endif = FALSE; /* in case do_cmdline used recursively */
1524#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 /*
1526 * Reset if_level, in case a sourced script file contains more ":if" than
1527 * ":endif" (could be ":if x | foo | endif").
1528 */
1529 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001530#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001531
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 --call_depth;
1533 return retval;
1534}
1535
1536#ifdef FEAT_EVAL
1537/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001538 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 */
1540 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001541get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001543 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 wcmd_T *wp;
1545 char_u *line;
1546
1547 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1548 {
1549 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001550 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001552 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 if (cp->getline == NULL)
1554 line = getcmdline(c, 0L, indent);
1555 else
1556 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001557 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 ++cp->current_line;
1559
1560 return line;
1561 }
1562
1563 KeyTyped = FALSE;
1564 ++cp->current_line;
1565 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1566 sourcing_lnum = wp->lnum;
1567 return vim_strsave(wp->line);
1568}
1569
1570/*
1571 * Store a line in "gap" so that a ":while" loop can execute it again.
1572 */
1573 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001574store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575{
1576 if (ga_grow(gap, 1) == FAIL)
1577 return FAIL;
1578 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1579 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1580 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 return OK;
1582}
1583
1584/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001585 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 */
1587 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001588free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589{
1590 while (gap->ga_len > 0)
1591 {
1592 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1593 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 }
1595}
1596#endif
1597
1598/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001599 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1600 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001603getline_equal(
1604 char_u *(*fgetline)(int, void *, int),
1605 void *cookie UNUSED, /* argument for fgetline() */
1606 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607{
1608#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001609 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001610 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611
Bram Moolenaar89d40322006-08-29 15:30:07 +00001612 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001613 * function that's originally used to obtain the lines. This may be
1614 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001615 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001616 cp = (struct loop_cookie *)cookie;
1617 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 {
1619 gp = cp->getline;
1620 cp = cp->cookie;
1621 }
1622 return gp == func;
1623#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001624 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625#endif
1626}
1627
1628#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1629/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001630 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 * getline function. Otherwise return "cookie".
1632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001634getline_cookie(
1635 char_u *(*fgetline)(int, void *, int) UNUSED,
1636 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637{
1638# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001639 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001640 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641
Bram Moolenaar89d40322006-08-29 15:30:07 +00001642 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001643 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001645 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001646 cp = (struct loop_cookie *)cookie;
1647 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 {
1649 gp = cp->getline;
1650 cp = cp->cookie;
1651 }
1652 return cp;
1653# else
1654 return cookie;
1655# endif
1656}
1657#endif
1658
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001659
1660/*
1661 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1662 * ":bwipeout", etc.
1663 * Returns the buffer number.
1664 */
1665 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001666compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001667{
1668 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001669 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001670 int count = offset;
1671
1672 buf = firstbuf;
1673 while (buf->b_next != NULL && buf->b_fnum < lnum)
1674 buf = buf->b_next;
1675 while (count != 0)
1676 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001677 count += (offset < 0) ? 1 : -1;
1678 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1679 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001680 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001681 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001682 if (addr_type == ADDR_LOADED_BUFFERS)
1683 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001684 while (buf->b_ml.ml_mfp == NULL)
1685 {
1686 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1687 if (nextbuf == NULL)
1688 break;
1689 buf = nextbuf;
1690 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001691 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001692 /* we might have gone too far, last buffer is not loadedd */
1693 if (addr_type == ADDR_LOADED_BUFFERS)
1694 while (buf->b_ml.ml_mfp == NULL)
1695 {
1696 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1697 if (nextbuf == NULL)
1698 break;
1699 buf = nextbuf;
1700 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001701 return buf->b_fnum;
1702}
1703
Bram Moolenaarf240e182014-11-27 18:33:02 +01001704#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001705static int current_win_nr(win_T *win);
1706static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001707
1708 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001709current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001710{
1711 win_T *wp;
1712 int nr = 0;
1713
Bram Moolenaar29323592016-07-24 22:04:11 +02001714 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001715 {
1716 ++nr;
1717 if (wp == win)
1718 break;
1719 }
1720 return nr;
1721}
1722
1723 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001724current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001725{
1726 tabpage_T *tp;
1727 int nr = 0;
1728
Bram Moolenaar29323592016-07-24 22:04:11 +02001729 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001730 {
1731 ++nr;
1732 if (tp == tab)
1733 break;
1734 }
1735 return nr;
1736}
1737
1738# define CURRENT_WIN_NR current_win_nr(curwin)
1739# define LAST_WIN_NR current_win_nr(NULL)
1740# define CURRENT_TAB_NR current_tab_nr(curtab)
1741# define LAST_TAB_NR current_tab_nr(NULL)
1742#else
1743# define CURRENT_WIN_NR 1
1744# define LAST_WIN_NR 1
1745# define CURRENT_TAB_NR 1
1746# define LAST_TAB_NR 1
1747#endif
1748
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001749
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750/*
1751 * Execute one Ex command.
1752 *
1753 * If 'sourcing' is TRUE, the command will be included in the error message.
1754 *
1755 * 1. skip comment lines and leading space
1756 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001757 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001758 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001759 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001760 * 6. parse arguments
1761 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001763 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 *
1765 * This function may be called recursively!
1766 */
1767#if (_MSC_VER == 1200)
1768/*
Bram Moolenaared203462004-06-16 11:19:22 +00001769 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001771 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772#endif
1773 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001774do_one_cmd(
1775 char_u **cmdlinep,
1776 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001778 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001780 char_u *(*fgetline)(int, void *, int),
1781 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782{
1783 char_u *p;
1784 linenr_T lnum;
1785 long n;
1786 char_u *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001787 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 exarg_T ea; /* Ex command arguments */
1789 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001790 int save_msg_scroll = msg_scroll;
1791 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001793#ifdef HAVE_SANDBOX
1794 int did_sandbox = FALSE;
1795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 cmdmod_T save_cmdmod;
1797 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001798 char_u *cmd;
Bram Moolenaarded27822017-01-02 14:27:34 +01001799 int address_count = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800
1801 vim_memset(&ea, 0, sizeof(ea));
1802 ea.line1 = 1;
1803 ea.line2 = 1;
1804#ifdef FEAT_EVAL
1805 ++ex_nesting_level;
1806#endif
1807
Bram Moolenaar21691f82012-12-05 19:13:18 +01001808 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 if (quitmore
1810#ifdef FEAT_EVAL
1811 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001812 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001813#endif
1814#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001815 /* avoid that an autocommand, e.g. QuitPre, does this */
1816 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817#endif
1818 )
1819 --quitmore;
1820
1821 /*
1822 * Reset browse, confirm, etc.. They are restored when returning, for
1823 * recursive calls.
1824 */
1825 save_cmdmod = cmdmod;
1826 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1827
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001828 /* "#!anything" is handled like a comment. */
1829 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1830 goto doend;
1831
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 /*
1833 * Repeat until no more command modifiers are found.
1834 */
1835 ea.cmd = *cmdlinep;
1836 for (;;)
1837 {
1838/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001839 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 */
1841 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1842 ++ea.cmd;
1843
1844 /* in ex mode, an empty line works like :+ */
1845 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001846 && (getline_equal(fgetline, cookie, getexmodeline)
1847 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001848 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 {
1850 ea.cmd = (char_u *)"+";
1851 ex_pressedreturn = TRUE;
1852 }
1853
1854 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001855 if (*ea.cmd == '"')
1856 goto doend;
1857 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001858 {
1859 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862
1863/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001864 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 */
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001866 p = skip_range(ea.cmd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 switch (*p)
1868 {
1869 /* When adding an entry, also modify cmd_exists(). */
1870 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1871 break;
1872#ifdef FEAT_WINDOWS
1873 cmdmod.split |= WSP_ABOVE;
1874#endif
1875 continue;
1876
1877 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1878 {
1879#ifdef FEAT_WINDOWS
1880 cmdmod.split |= WSP_BELOW;
1881#endif
1882 continue;
1883 }
1884 if (checkforcmd(&ea.cmd, "browse", 3))
1885 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001886#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 cmdmod.browse = TRUE;
1888#endif
1889 continue;
1890 }
1891 if (!checkforcmd(&ea.cmd, "botright", 2))
1892 break;
1893#ifdef FEAT_WINDOWS
1894 cmdmod.split |= WSP_BOT;
1895#endif
1896 continue;
1897
1898 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1899 break;
1900#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1901 cmdmod.confirm = TRUE;
1902#endif
1903 continue;
1904
1905 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1906 {
1907 cmdmod.keepmarks = TRUE;
1908 continue;
1909 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001910 if (checkforcmd(&ea.cmd, "keepalt", 5))
1911 {
1912 cmdmod.keepalt = TRUE;
1913 continue;
1914 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001915 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1916 {
1917 cmdmod.keeppatterns = TRUE;
1918 continue;
1919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1921 break;
1922 cmdmod.keepjumps = TRUE;
1923 continue;
1924
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001925 case 'f': /* only accept ":filter {pat} cmd" */
1926 {
1927 char_u *reg_pat;
1928
1929 if (!checkforcmd(&p, "filter", 4)
1930 || *p == NUL || ends_excmd(*p))
1931 break;
Bram Moolenaard29459b2016-08-26 22:29:11 +02001932 if (*p == '!')
1933 {
1934 cmdmod.filter_force = TRUE;
1935 p = skipwhite(p + 1);
1936 if (*p == NUL || ends_excmd(*p))
1937 break;
1938 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001939 p = skip_vimgrep_pat(p, &reg_pat, NULL);
1940 if (p == NULL || *p == NUL)
1941 break;
1942 cmdmod.filter_regmatch.regprog =
1943 vim_regcomp(reg_pat, RE_MAGIC);
1944 if (cmdmod.filter_regmatch.regprog == NULL)
1945 break;
1946 ea.cmd = p;
1947 continue;
1948 }
1949
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 /* ":hide" and ":hide | cmd" are not modifiers */
1951 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1952 || *p == NUL || ends_excmd(*p))
1953 break;
1954 ea.cmd = p;
1955 cmdmod.hide = TRUE;
1956 continue;
1957
1958 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1959 {
1960 cmdmod.lockmarks = TRUE;
1961 continue;
1962 }
1963
1964 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1965 break;
1966#ifdef FEAT_WINDOWS
1967 cmdmod.split |= WSP_ABOVE;
1968#endif
1969 continue;
1970
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001971 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001972 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001973#ifdef FEAT_AUTOCMD
1974 if (cmdmod.save_ei == NULL)
1975 {
1976 /* Set 'eventignore' to "all". Restore the
1977 * existing option value later. */
1978 cmdmod.save_ei = vim_strsave(p_ei);
1979 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001980 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001981 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001982#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001983 continue;
1984 }
Bram Moolenaar3bcfca32016-07-30 19:39:29 +02001985 if (!checkforcmd(&ea.cmd, "noswapfile", 3))
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001986 break;
1987 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001988 continue;
1989
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1991 break;
1992#ifdef FEAT_WINDOWS
1993 cmdmod.split |= WSP_BELOW;
1994#endif
1995 continue;
1996
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001997 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1998 {
1999#ifdef HAVE_SANDBOX
2000 if (!did_sandbox)
2001 ++sandbox;
2002 did_sandbox = TRUE;
2003#endif
2004 continue;
2005 }
2006 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002008 if (save_msg_silent == -1)
2009 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
2012 {
2013 /* ":silent!", but not "silent !cmd" */
2014 ea.cmd = skipwhite(ea.cmd + 1);
2015 ++emsg_silent;
2016 ++did_esilent;
2017 }
2018 continue;
2019
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002020 case 't': if (checkforcmd(&p, "tab", 3))
2021 {
2022#ifdef FEAT_WINDOWS
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02002023 long tabnr = get_address(&ea, &ea.cmd, ADDR_TABS,
Bram Moolenaarded27822017-01-02 14:27:34 +01002024 ea.skip, FALSE, 1);
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02002025 if (tabnr == MAXLNUM)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002026 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02002027 else
2028 {
2029 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2030 {
2031 errormsg = (char_u *)_(e_invrange);
2032 goto doend;
2033 }
2034 cmdmod.tab = tabnr + 1;
2035 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002036 ea.cmd = p;
2037#endif
2038 continue;
2039 }
2040 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 break;
2042#ifdef FEAT_WINDOWS
2043 cmdmod.split |= WSP_TOP;
2044#endif
2045 continue;
2046
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002047 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
2048 break;
2049 if (save_msg_silent == -1)
2050 save_msg_silent = msg_silent;
2051 msg_silent = 0;
2052 continue;
2053
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
2055 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002056#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 cmdmod.split |= WSP_VERT;
2058#endif
2059 continue;
2060 }
2061 if (!checkforcmd(&p, "verbose", 4))
2062 break;
2063 if (verbose_save < 0)
2064 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00002065 if (vim_isdigit(*ea.cmd))
2066 p_verbose = atoi((char *)ea.cmd);
2067 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 p_verbose = 1;
2069 ea.cmd = p;
2070 continue;
2071 }
2072 break;
2073 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002074 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075
2076#ifdef FEAT_EVAL
2077 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2078 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2079#else
2080 ea.skip = (if_level > 0);
2081#endif
2082
2083#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002084# ifdef FEAT_PROFILE
2085 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002086 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002087 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002088 if (getline_equal(fgetline, cookie, get_func_line))
2089 func_line_exec(getline_cookie(fgetline, cookie));
2090 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002091 script_line_exec();
2092 }
2093#endif
2094
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 /* May go to debug mode. If this happens and the ">quit" debug command is
2096 * used, throw an interrupt exception and skip the next command. */
2097 dbg_check_breakpoint(&ea);
2098 if (!ea.skip && got_int)
2099 {
2100 ea.skip = TRUE;
2101 (void)do_intthrow(cstack);
2102 }
2103#endif
2104
2105/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002106 * 3. Skip over the range to find the command. Let "p" point to after it.
2107 *
2108 * We need the command to know what kind of range it uses.
2109 */
2110 cmd = ea.cmd;
2111 ea.cmd = skip_range(ea.cmd, NULL);
2112 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2113 ea.cmd = skipwhite(ea.cmd + 1);
2114 p = find_command(&ea, NULL);
2115
2116/*
2117 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 *
2119 * where 'addr' is:
2120 *
2121 * % (entire file)
2122 * $ [+-NUM]
2123 * 'x [+-NUM] (where x denotes a currently defined mark)
2124 * . [+-NUM]
2125 * [+-NUM]..
2126 * NUM
2127 *
2128 * The ea.cmd pointer is updated to point to the first character following the
2129 * range spec. If an initial address is found, but no second, the upper bound
2130 * is equal to the lower.
2131 */
2132
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002133 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002134 if (!IS_USER_CMDIDX(ea.cmdidx))
2135 {
2136 if (ea.cmdidx != CMD_SIZE)
2137 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2138 else
2139 ea.addr_type = ADDR_LINES;
2140
2141#ifdef FEAT_WINDOWS
2142 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002143 if (ea.cmdidx == CMD_wincmd && p != NULL)
2144 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002145#endif
2146 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002147
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002149 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 for (;;)
2151 {
2152 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002153 switch (ea.addr_type)
2154 {
2155 case ADDR_LINES:
2156 /* default is current line number */
2157 ea.line2 = curwin->w_cursor.lnum;
2158 break;
2159 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002160 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002161 ea.line2 = lnum;
2162 break;
2163 case ADDR_ARGUMENTS:
2164 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002165 if (ea.line2 > ARGCOUNT)
2166 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002167 break;
2168 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002169 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002170 ea.line2 = curbuf->b_fnum;
2171 break;
2172 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002173 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002174 ea.line2 = lnum;
2175 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002176#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002177 case ADDR_QUICKFIX:
2178 ea.line2 = qf_get_cur_valid_idx(&ea);
2179 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002180#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002183 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip,
Bram Moolenaarded27822017-01-02 14:27:34 +01002184 ea.addr_count == 0, address_count++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 if (ea.cmd == NULL) /* error detected */
2186 goto doend;
2187 if (lnum == MAXLNUM)
2188 {
2189 if (*ea.cmd == '%') /* '%' - all lines */
2190 {
2191 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002192 switch (ea.addr_type)
2193 {
2194 case ADDR_LINES:
2195 ea.line1 = 1;
2196 ea.line2 = curbuf->b_ml.ml_line_count;
2197 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002198 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002199 {
2200 buf_T *buf = firstbuf;
2201
2202 while (buf->b_next != NULL
2203 && buf->b_ml.ml_mfp == NULL)
2204 buf = buf->b_next;
2205 ea.line1 = buf->b_fnum;
2206 buf = lastbuf;
2207 while (buf->b_prev != NULL
2208 && buf->b_ml.ml_mfp == NULL)
2209 buf = buf->b_prev;
2210 ea.line2 = buf->b_fnum;
2211 break;
2212 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002213 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002214 ea.line1 = firstbuf->b_fnum;
2215 ea.line2 = lastbuf->b_fnum;
2216 break;
2217 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002218 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002219 if (IS_USER_CMDIDX(ea.cmdidx))
2220 {
2221 ea.line1 = 1;
2222 ea.line2 = ea.addr_type == ADDR_WINDOWS
2223 ? LAST_WIN_NR : LAST_TAB_NR;
2224 }
2225 else
2226 {
2227 /* there is no Vim command which uses '%' and
2228 * ADDR_WINDOWS or ADDR_TABS */
2229 errormsg = (char_u *)_(e_invrange);
2230 goto doend;
2231 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002232 break;
2233 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002234 if (ARGCOUNT == 0)
2235 ea.line1 = ea.line2 = 0;
2236 else
2237 {
2238 ea.line1 = 1;
2239 ea.line2 = ARGCOUNT;
2240 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002241 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002242#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002243 case ADDR_QUICKFIX:
2244 ea.line1 = 1;
2245 ea.line2 = qf_get_size(&ea);
2246 if (ea.line2 == 0)
2247 ea.line2 = 1;
2248 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002249#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 ++ea.addr_count;
2252 }
2253 /* '*' - visual area */
2254 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2255 {
2256 pos_T *fp;
2257
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002258 if (ea.addr_type != ADDR_LINES)
2259 {
2260 errormsg = (char_u *)_(e_invrange);
2261 goto doend;
2262 }
2263
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 ++ea.cmd;
2265 if (!ea.skip)
2266 {
2267 fp = getmark('<', FALSE);
2268 if (check_mark(fp) == FAIL)
2269 goto doend;
2270 ea.line1 = fp->lnum;
2271 fp = getmark('>', FALSE);
2272 if (check_mark(fp) == FAIL)
2273 goto doend;
2274 ea.line2 = fp->lnum;
2275 ++ea.addr_count;
2276 }
2277 }
2278 }
2279 else
2280 ea.line2 = lnum;
2281 ea.addr_count++;
2282
2283 if (*ea.cmd == ';')
2284 {
2285 if (!ea.skip)
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002286 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 curwin->w_cursor.lnum = ea.line2;
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002288 /* don't leave the cursor on an illegal line */
2289 check_cursor_lnum();
2290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 }
2292 else if (*ea.cmd != ',')
2293 break;
2294 ++ea.cmd;
2295 }
2296
2297 /* One address given: set start and end lines */
2298 if (ea.addr_count == 1)
2299 {
2300 ea.line1 = ea.line2;
2301 /* ... but only implicit: really no address given */
2302 if (lnum == MAXLNUM)
2303 ea.addr_count = 0;
2304 }
2305
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002307 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 */
2309
2310 /*
2311 * Skip ':' and any white space
2312 */
2313 ea.cmd = skipwhite(ea.cmd);
2314 while (*ea.cmd == ':')
2315 ea.cmd = skipwhite(ea.cmd + 1);
2316
2317 /*
2318 * If we got a line, but no command, then go to the line.
2319 * If we find a '|' or '\n' we set ea.nextcmd.
2320 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002321 if (*ea.cmd == NUL || *ea.cmd == '"'
2322 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 {
2324 /*
2325 * strange vi behaviour:
2326 * ":3" jumps to line 3
2327 * ":3|..." prints line 3
2328 * ":|" prints current line
2329 */
2330 if (ea.skip) /* skip this if inside :if */
2331 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002332 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 {
2334 ea.cmdidx = CMD_print;
2335 ea.argt = RANGE+COUNT+TRLBAR;
2336 if ((errormsg = invalid_range(&ea)) == NULL)
2337 {
2338 correct_range(&ea);
2339 ex_print(&ea);
2340 }
2341 }
2342 else if (ea.addr_count != 0)
2343 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002344 if (ea.line2 > curbuf->b_ml.ml_line_count)
2345 {
2346 /* With '-' in 'cpoptions' a line number past the file is an
2347 * error, otherwise put it at the end of the file. */
2348 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2349 ea.line2 = -1;
2350 else
2351 ea.line2 = curbuf->b_ml.ml_line_count;
2352 }
2353
2354 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002355 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 else
2357 {
2358 if (ea.line2 == 0)
2359 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 else
2361 curwin->w_cursor.lnum = ea.line2;
2362 beginline(BL_SOL | BL_FIX);
2363 }
2364 }
2365 goto doend;
2366 }
2367
Bram Moolenaard5005162014-08-22 23:05:54 +02002368#ifdef FEAT_AUTOCMD
2369 /* If this looks like an undefined user command and there are CmdUndefined
2370 * autocommands defined, trigger the matching autocommands. */
2371 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2372 && ASCII_ISUPPER(*ea.cmd)
2373 && has_cmdundefined())
2374 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002375 int ret;
2376
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002377 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002378 while (ASCII_ISALNUM(*p))
2379 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002380 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002381 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2382 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002383 /* If the autocommands did something and didn't cause an error, try
2384 * finding the command again. */
Bram Moolenaareac784e2016-07-28 22:08:24 +02002385 p = (ret && !aborting()) ? find_command(&ea, NULL) : ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002386 }
2387#endif
2388
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389#ifdef FEAT_USR_CMDS
2390 if (p == NULL)
2391 {
2392 if (!ea.skip)
2393 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2394 goto doend;
2395 }
2396 /* Check for wrong commands. */
2397 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2398 {
2399 errormsg = uc_fun_cmd();
2400 goto doend;
2401 }
2402#endif
2403 if (ea.cmdidx == CMD_SIZE)
2404 {
2405 if (!ea.skip)
2406 {
2407 STRCPY(IObuff, _("E492: Not an editor command"));
2408 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002409 {
2410 /* If the modifier was parsed OK the error must be in the
2411 * following command */
2412 if (after_modifier != NULL)
2413 append_command(after_modifier);
2414 else
2415 append_command(*cmdlinep);
2416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002418 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 }
2420 goto doend;
2421 }
2422
Bram Moolenaar958636c2014-10-21 20:01:58 +02002423 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2424 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002425#ifdef HAVE_EX_SCRIPT_NI
2426 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2427#endif
2428 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429
2430#ifndef FEAT_EVAL
2431 /*
2432 * When the expression evaluation is disabled, recognize the ":if" and
2433 * ":endif" commands and ignore everything in between it.
2434 */
2435 if (ea.cmdidx == CMD_if)
2436 ++if_level;
2437 if (if_level)
2438 {
2439 if (ea.cmdidx == CMD_endif)
2440 --if_level;
2441 goto doend;
2442 }
2443
2444#endif
2445
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002446 /* forced commands */
2447 if (*p == '!' && ea.cmdidx != CMD_substitute
2448 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 {
2450 ++p;
2451 ea.forceit = TRUE;
2452 }
2453 else
2454 ea.forceit = FALSE;
2455
2456/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002457 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002459 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002460 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461
2462 if (!ea.skip)
2463 {
2464#ifdef HAVE_SANDBOX
2465 if (sandbox != 0 && !(ea.argt & SBOXOK))
2466 {
2467 /* Command not allowed in sandbox. */
2468 errormsg = (char_u *)_(e_sandbox);
2469 goto doend;
2470 }
2471#endif
2472 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2473 {
2474 /* Command not allowed in non-'modifiable' buffer */
2475 errormsg = (char_u *)_(e_modifiable);
2476 goto doend;
2477 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002478
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002479 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002480 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002482 /* Command not allowed when editing the command line. */
Bram Moolenaar5a497892016-09-03 16:29:04 +02002483 errormsg = get_text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 goto doend;
2485 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002486#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002487 /* Disallow editing another buffer when "curbuf_lock" is set.
2488 * Do allow ":edit" (check for argument later).
2489 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002490 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002491 && ea.cmdidx != CMD_edit
2492 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002493 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002494 && curbuf_locked())
2495 goto doend;
2496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497
2498 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2499 {
2500 /* no range allowed */
2501 errormsg = (char_u *)_(e_norange);
2502 goto doend;
2503 }
2504 }
2505
2506 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2507 {
2508 errormsg = (char_u *)_(e_nobang);
2509 goto doend;
2510 }
2511
2512 /*
2513 * Don't complain about the range if it is not used
2514 * (could happen if line_count is accidentally set to 0).
2515 */
2516 if (!ea.skip && !ni)
2517 {
2518 /*
2519 * If the range is backwards, ask for confirmation and, if given, swap
2520 * ea.line1 & ea.line2 so it's forwards again.
2521 * When global command is busy, don't ask, will fail below.
2522 */
2523 if (!global_busy && ea.line1 > ea.line2)
2524 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002525 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002527 if (sourcing || exmode_active)
2528 {
2529 errormsg = (char_u *)_("E493: Backwards range given");
2530 goto doend;
2531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 if (ask_yesno((char_u *)
2533 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002534 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 }
2536 lnum = ea.line1;
2537 ea.line1 = ea.line2;
2538 ea.line2 = lnum;
2539 }
2540 if ((errormsg = invalid_range(&ea)) != NULL)
2541 goto doend;
2542 }
2543
2544 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2545 ea.line2 = 1;
2546
2547 correct_range(&ea);
2548
2549#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002550 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2551 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 {
2553 /* Put the first line at the start of a closed fold, put the last line
2554 * at the end of a closed fold. */
2555 (void)hasFolding(ea.line1, &ea.line1, NULL);
2556 (void)hasFolding(ea.line2, NULL, &ea.line2);
2557 }
2558#endif
2559
2560#ifdef FEAT_QUICKFIX
2561 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002562 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 * option here, so things like % get expanded.
2564 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002565 p = replace_makeprg(&ea, p, cmdlinep);
2566 if (p == NULL)
2567 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568#endif
2569
2570 /*
2571 * Skip to start of argument.
2572 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2573 */
2574 if (ea.cmdidx == CMD_bang)
2575 ea.arg = p;
2576 else
2577 ea.arg = skipwhite(p);
2578
2579 /*
2580 * Check for "++opt=val" argument.
2581 * Must be first, allow ":w ++enc=utf8 !cmd"
2582 */
2583 if (ea.argt & ARGOPT)
2584 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2585 if (getargopt(&ea) == FAIL && !ni)
2586 {
2587 errormsg = (char_u *)_(e_invarg);
2588 goto doend;
2589 }
2590
2591 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2592 {
2593 if (*ea.arg == '>') /* append */
2594 {
2595 if (*++ea.arg != '>') /* typed wrong */
2596 {
2597 errormsg = (char_u *)_("E494: Use w or w>>");
2598 goto doend;
2599 }
2600 ea.arg = skipwhite(ea.arg + 1);
2601 ea.append = TRUE;
2602 }
2603 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2604 {
2605 ++ea.arg;
2606 ea.usefilter = TRUE;
2607 }
2608 }
2609
2610 if (ea.cmdidx == CMD_read)
2611 {
2612 if (ea.forceit)
2613 {
2614 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2615 ea.forceit = FALSE;
2616 }
2617 else if (*ea.arg == '!') /* :r !filter */
2618 {
2619 ++ea.arg;
2620 ea.usefilter = TRUE;
2621 }
2622 }
2623
2624 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2625 {
2626 ea.amount = 1;
2627 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2628 {
2629 ++ea.arg;
2630 ++ea.amount;
2631 }
2632 ea.arg = skipwhite(ea.arg);
2633 }
2634
2635 /*
2636 * Check for "+command" argument, before checking for next command.
2637 * Don't do this for ":read !cmd" and ":write !cmd".
2638 */
2639 if ((ea.argt & EDITCMD) && !ea.usefilter)
2640 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2641
2642 /*
2643 * Check for '|' to separate commands and '"' to start comments.
2644 * Don't do this for ":read !cmd" and ":write !cmd".
2645 */
2646 if ((ea.argt & TRLBAR) && !ea.usefilter)
2647 separate_nextcmd(&ea);
2648
2649 /*
2650 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002651 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2652 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002654 else if (ea.cmdidx == CMD_bang
2655 || ea.cmdidx == CMD_global
2656 || ea.cmdidx == CMD_vglobal
2657 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 {
2659 for (p = ea.arg; *p; ++p)
2660 {
2661 /* Remove one backslash before a newline, so that it's possible to
2662 * pass a newline to the shell and also a newline that is preceded
2663 * with a backslash. This makes it impossible to end a shell
2664 * command in a backslash, but that doesn't appear useful.
2665 * Halving the number of backslashes is incompatible with previous
2666 * versions. */
2667 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002668 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 else if (*p == '\n')
2670 {
2671 ea.nextcmd = p + 1;
2672 *p = NUL;
2673 break;
2674 }
2675 }
2676 }
2677
2678 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2679 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002680 buf_T *buf;
2681
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002683 switch (ea.addr_type)
2684 {
2685 case ADDR_LINES:
2686 ea.line2 = curbuf->b_ml.ml_line_count;
2687 break;
2688 case ADDR_LOADED_BUFFERS:
2689 buf = firstbuf;
2690 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2691 buf = buf->b_next;
2692 ea.line1 = buf->b_fnum;
2693 buf = lastbuf;
2694 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2695 buf = buf->b_prev;
2696 ea.line2 = buf->b_fnum;
2697 break;
2698 case ADDR_BUFFERS:
2699 ea.line1 = firstbuf->b_fnum;
2700 ea.line2 = lastbuf->b_fnum;
2701 break;
2702 case ADDR_WINDOWS:
2703 ea.line2 = LAST_WIN_NR;
2704 break;
2705 case ADDR_TABS:
2706 ea.line2 = LAST_TAB_NR;
2707 break;
2708 case ADDR_ARGUMENTS:
2709 if (ARGCOUNT == 0)
2710 ea.line1 = ea.line2 = 0;
2711 else
2712 ea.line2 = ARGCOUNT;
2713 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002714#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002715 case ADDR_QUICKFIX:
2716 ea.line2 = qf_get_size(&ea);
2717 if (ea.line2 == 0)
2718 ea.line2 = 1;
2719 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002720#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 }
2723
2724 /* accept numbered register only when no count allowed (:put) */
2725 if ( (ea.argt & REGSTR)
2726 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002727 /* Do not allow register = for user commands */
2728 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2730 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002731#ifndef FEAT_CLIPBOARD
2732 /* check these explicitly for a more specific error message */
2733 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002735 errormsg = (char_u *)_(e_invalidreg);
2736 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 }
2738#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002739 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2740 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002741 {
2742 ea.regname = *ea.arg++;
2743#ifdef FEAT_EVAL
2744 /* for '=' register: accept the rest of the line as an expression */
2745 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2746 {
2747 set_expr_line(vim_strsave(ea.arg));
2748 ea.arg += STRLEN(ea.arg);
2749 }
2750#endif
2751 ea.arg = skipwhite(ea.arg);
2752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 }
2754
2755 /*
2756 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2757 * count, it's a buffer name.
2758 */
2759 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2760 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2761 || vim_iswhite(*p)))
2762 {
2763 n = getdigits(&ea.arg);
2764 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002765 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 {
2767 errormsg = (char_u *)_(e_zerocount);
2768 goto doend;
2769 }
2770 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2771 {
2772 ea.line2 = n;
2773 if (ea.addr_count == 0)
2774 ea.addr_count = 1;
2775 }
2776 else
2777 {
2778 ea.line1 = ea.line2;
2779 ea.line2 += n - 1;
2780 ++ea.addr_count;
2781 /*
2782 * Be vi compatible: no error message for out of range.
2783 */
2784 if (ea.line2 > curbuf->b_ml.ml_line_count)
2785 ea.line2 = curbuf->b_ml.ml_line_count;
2786 }
2787 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002788
2789 /*
2790 * Check for flags: 'l', 'p' and '#'.
2791 */
2792 if (ea.argt & EXFLAGS)
2793 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002795 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002796 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 {
2798 errormsg = (char_u *)_(e_trailing);
2799 goto doend;
2800 }
2801
2802 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2803 {
2804 errormsg = (char_u *)_(e_argreq);
2805 goto doend;
2806 }
2807
2808#ifdef FEAT_EVAL
2809 /*
2810 * Skip the command when it's not going to be executed.
2811 * The commands like :if, :endif, etc. always need to be executed.
2812 * Also make an exception for commands that handle a trailing command
2813 * themselves.
2814 */
2815 if (ea.skip)
2816 {
2817 switch (ea.cmdidx)
2818 {
2819 /* commands that need evaluation */
2820 case CMD_while:
2821 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002822 case CMD_for:
2823 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 case CMD_if:
2825 case CMD_elseif:
2826 case CMD_else:
2827 case CMD_endif:
2828 case CMD_try:
2829 case CMD_catch:
2830 case CMD_finally:
2831 case CMD_endtry:
2832 case CMD_function:
2833 break;
2834
2835 /* Commands that handle '|' themselves. Check: A command should
2836 * either have the TRLBAR flag, appear in this list or appear in
2837 * the list at ":help :bar". */
2838 case CMD_aboveleft:
2839 case CMD_and:
2840 case CMD_belowright:
2841 case CMD_botright:
2842 case CMD_browse:
2843 case CMD_call:
2844 case CMD_confirm:
2845 case CMD_delfunction:
2846 case CMD_djump:
2847 case CMD_dlist:
2848 case CMD_dsearch:
2849 case CMD_dsplit:
2850 case CMD_echo:
2851 case CMD_echoerr:
2852 case CMD_echomsg:
2853 case CMD_echon:
2854 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002855 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 case CMD_help:
2857 case CMD_hide:
2858 case CMD_ijump:
2859 case CMD_ilist:
2860 case CMD_isearch:
2861 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002862 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 case CMD_keepjumps:
2864 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002865 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 case CMD_leftabove:
2867 case CMD_let:
2868 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002869 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002871 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002872 case CMD_noautocmd:
2873 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 case CMD_perl:
2875 case CMD_psearch:
2876 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002877 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002878 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 case CMD_return:
2880 case CMD_rightbelow:
2881 case CMD_ruby:
2882 case CMD_silent:
2883 case CMD_smagic:
2884 case CMD_snomagic:
2885 case CMD_substitute:
2886 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002887 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 case CMD_tcl:
2889 case CMD_throw:
2890 case CMD_tilde:
2891 case CMD_topleft:
2892 case CMD_unlet:
2893 case CMD_verbose:
2894 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002895 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 break;
2897
2898 default: goto doend;
2899 }
2900 }
2901#endif
2902
2903 if (ea.argt & XFILE)
2904 {
2905 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2906 goto doend;
2907 }
2908
2909#ifdef FEAT_LISTCMDS
2910 /*
2911 * Accept buffer name. Cannot be used at the same time with a buffer
2912 * number. Don't do this for a user command.
2913 */
2914 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002915 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 {
2917 /*
2918 * :bdelete, :bwipeout and :bunload take several arguments, separated
2919 * by spaces: find next space (skipping over escaped characters).
2920 * The others take one argument: ignore trailing spaces.
2921 */
2922 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2923 || ea.cmdidx == CMD_bunload)
2924 p = skiptowhite_esc(ea.arg);
2925 else
2926 {
2927 p = ea.arg + STRLEN(ea.arg);
2928 while (p > ea.arg && vim_iswhite(p[-1]))
2929 --p;
2930 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002931 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2932 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 if (ea.line2 < 0) /* failed */
2934 goto doend;
2935 ea.addr_count = 1;
2936 ea.arg = skipwhite(p);
2937 }
2938#endif
2939
2940/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002941 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 *
2943 * The "ea" structure holds the arguments that can be used.
2944 */
2945 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002946 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 ea.cookie = cookie;
2948#ifdef FEAT_EVAL
2949 ea.cstack = cstack;
2950#endif
2951
2952#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002953 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 {
2955 /*
2956 * Execute a user-defined command.
2957 */
2958 do_ucmd(&ea);
2959 }
2960 else
2961#endif
2962 {
2963 /*
2964 * Call the function to execute the command.
2965 */
2966 ea.errmsg = NULL;
2967 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2968 if (ea.errmsg != NULL)
2969 errormsg = (char_u *)_(ea.errmsg);
2970 }
2971
2972#ifdef FEAT_EVAL
2973 /*
2974 * If the command just executed called do_cmdline(), any throw or ":return"
2975 * or ":finish" encountered there must also check the cstack of the still
2976 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2977 * exception, or reanimate a returned function or finished script file and
2978 * return or finish it again.
2979 */
2980 if (need_rethrow)
2981 do_throw(cstack);
2982 else if (check_cstack)
2983 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002984 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002986 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 && current_func_returned())
2988 do_return(&ea, TRUE, FALSE, NULL);
2989 }
2990 need_rethrow = check_cstack = FALSE;
2991#endif
2992
2993doend:
2994 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2995 curwin->w_cursor.lnum = 1;
2996
2997 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2998 {
2999 if (sourcing)
3000 {
3001 if (errormsg != IObuff)
3002 {
3003 STRCPY(IObuff, errormsg);
3004 errormsg = IObuff;
3005 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003006 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 }
3008 emsg(errormsg);
3009 }
3010#ifdef FEAT_EVAL
3011 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02003012 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
3013 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014#endif
3015
3016 if (verbose_save >= 0)
3017 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00003018#ifdef FEAT_AUTOCMD
3019 if (cmdmod.save_ei != NULL)
3020 {
3021 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003022 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
3023 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00003024 free_string_option(cmdmod.save_ei);
3025 }
3026#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003027 if (cmdmod.filter_regmatch.regprog != NULL)
3028 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029
3030 cmdmod = save_cmdmod;
3031
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003032 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 {
3034 /* messages could be enabled for a serious error, need to check if the
3035 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00003036 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003037 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 emsg_silent -= did_esilent;
3039 if (emsg_silent < 0)
3040 emsg_silent = 0;
3041 /* Restore msg_scroll, it's set by file I/O commands, even when no
3042 * message is actually displayed. */
3043 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003044
3045 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
3046 * somewhere in the line. Put it back in the first column. */
3047 if (redirecting())
3048 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 }
3050
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003051#ifdef HAVE_SANDBOX
3052 if (did_sandbox)
3053 --sandbox;
3054#endif
3055
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3057 ea.nextcmd = NULL;
3058
3059#ifdef FEAT_EVAL
3060 --ex_nesting_level;
3061#endif
3062
3063 return ea.nextcmd;
3064}
3065#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003066 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067#endif
3068
3069/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003070 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 * If there is a match advance "pp" to the argument and return TRUE.
3072 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003073 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003074checkforcmd(
3075 char_u **pp, /* start of command */
3076 char *cmd, /* name of command */
3077 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078{
3079 int i;
3080
3081 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003082 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 break;
3084 if (i >= len && !isalpha((*pp)[i]))
3085 {
3086 *pp = skipwhite(*pp + i);
3087 return TRUE;
3088 }
3089 return FALSE;
3090}
3091
3092/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003093 * Append "cmd" to the error message in IObuff.
3094 * Takes care of limiting the length and handling 0xa0, which would be
3095 * invisible otherwise.
3096 */
3097 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003098append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003099{
3100 char_u *s = cmd;
3101 char_u *d;
3102
3103 STRCAT(IObuff, ": ");
3104 d = IObuff + STRLEN(IObuff);
3105 while (*s != NUL && d - IObuff < IOSIZE - 7)
3106 {
3107 if (
3108#ifdef FEAT_MBYTE
3109 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3110#endif
3111 *s == 0xa0)
3112 {
3113 s +=
3114#ifdef FEAT_MBYTE
3115 enc_utf8 ? 2 :
3116#endif
3117 1;
3118 STRCPY(d, "<a0>");
3119 d += 4;
3120 }
3121 else
3122 MB_COPY_CHAR(s, d);
3123 }
3124 *d = NUL;
3125}
3126
3127/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003129 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003131 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 * Returns NULL for an ambiguous user command.
3133 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003135find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136{
3137 int len;
3138 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003139 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140
3141 /*
3142 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003143 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 * - the 'k' command can directly be followed by any character.
3145 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003146 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003148 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 */
3150 p = eap->cmd;
3151 if (*p == 'k')
3152 {
3153 eap->cmdidx = CMD_k;
3154 ++p;
3155 }
3156 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003157 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3158 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 || p[1] == 'g'
3160 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3161 || p[1] == 'I'
3162 || (p[1] == 'r' && p[2] != 'e')))
3163 {
3164 eap->cmdidx = CMD_substitute;
3165 ++p;
3166 }
3167 else
3168 {
3169 while (ASCII_ISALPHA(*p))
3170 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003171 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003172 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003173 while (ASCII_ISALNUM(*p))
3174 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003175
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 /* check for non-alpha command */
3177 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3178 ++p;
3179 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003180 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3181 {
3182 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3183 * :delete with the 'l' flag. Same for 'p'. */
3184 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003185 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003186 break;
3187 if (i == len - 1)
3188 {
3189 --len;
3190 if (p[-1] == 'l')
3191 eap->flags |= EXFLAG_LIST;
3192 else
3193 eap->flags |= EXFLAG_PRINT;
3194 }
3195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196
3197 if (ASCII_ISLOWER(*eap->cmd))
3198 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
3199 else
3200 eap->cmdidx = cmdidxs[26];
3201
3202 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3203 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3204 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3205 (size_t)len) == 0)
3206 {
3207#ifdef FEAT_EVAL
3208 if (full != NULL
3209 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3210 *full = TRUE;
3211#endif
3212 break;
3213 }
3214
3215#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003216 /* Look for a user defined command as a last resort. Let ":Print" be
3217 * overruled by a user defined command. */
3218 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3219 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003221 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 while (ASCII_ISALNUM(*p))
3223 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003224 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 }
3226#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003227 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 eap->cmdidx = CMD_SIZE;
3229 }
3230
3231 return p;
3232}
3233
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003234#ifdef FEAT_USR_CMDS
3235/*
3236 * Search for a user command that matches "eap->cmd".
3237 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3238 * Return a pointer to just after the command.
3239 * Return NULL if there is no matching command.
3240 */
3241 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003242find_ucmd(
3243 exarg_T *eap,
3244 char_u *p, /* end of the command (possibly including count) */
3245 int *full, /* set to TRUE for a full match */
3246 expand_T *xp, /* used for completion, NULL otherwise */
3247 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003248{
3249 int len = (int)(p - eap->cmd);
3250 int j, k, matchlen = 0;
3251 ucmd_T *uc;
3252 int found = FALSE;
3253 int possible = FALSE;
3254 char_u *cp, *np; /* Point into typed cmd and test name */
3255 garray_T *gap;
3256 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3257 only full match global is accepted. */
3258
3259 /*
3260 * Look for buffer-local user commands first, then global ones.
3261 */
3262 gap = &curbuf->b_ucmds;
3263 for (;;)
3264 {
3265 for (j = 0; j < gap->ga_len; ++j)
3266 {
3267 uc = USER_CMD_GA(gap, j);
3268 cp = eap->cmd;
3269 np = uc->uc_name;
3270 k = 0;
3271 while (k < len && *np != NUL && *cp++ == *np++)
3272 k++;
3273 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3274 {
3275 /* If finding a second match, the command is ambiguous. But
3276 * not if a buffer-local command wasn't a full match and a
3277 * global command is a full match. */
3278 if (k == len && found && *np != NUL)
3279 {
3280 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003281 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003282 amb_local = TRUE;
3283 }
3284
3285 if (!found || (k == len && *np == NUL))
3286 {
3287 /* If we matched up to a digit, then there could
3288 * be another command including the digit that we
3289 * should use instead.
3290 */
3291 if (k == len)
3292 found = TRUE;
3293 else
3294 possible = TRUE;
3295
3296 if (gap == &ucmds)
3297 eap->cmdidx = CMD_USER;
3298 else
3299 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003300 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003301 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003302 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003303
3304# ifdef FEAT_CMDL_COMPL
3305 if (compl != NULL)
3306 *compl = uc->uc_compl;
3307# ifdef FEAT_EVAL
3308 if (xp != NULL)
3309 {
3310 xp->xp_arg = uc->uc_compl_arg;
3311 xp->xp_scriptID = uc->uc_scriptID;
3312 }
3313# endif
3314# endif
3315 /* Do not search for further abbreviations
3316 * if this is an exact match. */
3317 matchlen = k;
3318 if (k == len && *np == NUL)
3319 {
3320 if (full != NULL)
3321 *full = TRUE;
3322 amb_local = FALSE;
3323 break;
3324 }
3325 }
3326 }
3327 }
3328
3329 /* Stop if we found a full match or searched all. */
3330 if (j < gap->ga_len || gap == &ucmds)
3331 break;
3332 gap = &ucmds;
3333 }
3334
3335 /* Only found ambiguous matches. */
3336 if (amb_local)
3337 {
3338 if (xp != NULL)
3339 xp->xp_context = EXPAND_UNSUCCESSFUL;
3340 return NULL;
3341 }
3342
3343 /* The match we found may be followed immediately by a number. Move "p"
3344 * back to point to it. */
3345 if (found || possible)
3346 return p + (matchlen - len);
3347 return p;
3348}
3349#endif
3350
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003352static struct cmdmod
3353{
3354 char *name;
3355 int minlen;
3356 int has_count; /* :123verbose :3tab */
3357} cmdmods[] = {
3358 {"aboveleft", 3, FALSE},
3359 {"belowright", 3, FALSE},
3360 {"botright", 2, FALSE},
3361 {"browse", 3, FALSE},
3362 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003363 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003364 {"hide", 3, FALSE},
3365 {"keepalt", 5, FALSE},
3366 {"keepjumps", 5, FALSE},
3367 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003368 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003369 {"leftabove", 5, FALSE},
3370 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003371 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003372 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003373 {"rightbelow", 6, FALSE},
3374 {"sandbox", 3, FALSE},
3375 {"silent", 3, FALSE},
3376 {"tab", 3, TRUE},
3377 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003378 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003379 {"verbose", 4, TRUE},
3380 {"vertical", 4, FALSE},
3381};
3382
3383/*
3384 * Return length of a command modifier (including optional count).
3385 * Return zero when it's not a modifier.
3386 */
3387 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003388modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003389{
3390 int i, j;
3391 char_u *p = cmd;
3392
3393 if (VIM_ISDIGIT(*cmd))
3394 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003395 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003396 {
3397 for (j = 0; p[j] != NUL; ++j)
3398 if (p[j] != cmdmods[i].name[j])
3399 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003400 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003401 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003402 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003403 }
3404 return 0;
3405}
3406
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407/*
3408 * Return > 0 if an Ex command "name" exists.
3409 * Return 2 if there is an exact match.
3410 * Return 3 if there is an ambiguous match.
3411 */
3412 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003413cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414{
3415 exarg_T ea;
3416 int full = FALSE;
3417 int i;
3418 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003419 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420
3421 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003422 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 {
3424 for (j = 0; name[j] != NUL; ++j)
3425 if (name[j] != cmdmods[i].name[j])
3426 break;
3427 if (name[j] == NUL && j >= cmdmods[i].minlen)
3428 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3429 }
3430
Bram Moolenaara9587612006-05-04 21:47:50 +00003431 /* Check built-in commands and user defined commands.
3432 * For ":2match" and ":3match" we need to skip the number. */
3433 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003435 p = find_command(&ea, &full);
3436 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003438 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3439 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003440 if (*skipwhite(p) != NUL)
3441 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3443}
3444#endif
3445
3446/*
3447 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3448 * we don't need/want deleted. Maybe this could be done better if we didn't
3449 * repeat all this stuff. The only problem is that they may not stay
3450 * perfectly compatible with each other, but then the command line syntax
3451 * probably won't change that much -- webb.
3452 */
3453 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003454set_one_cmd_context(
3455 expand_T *xp,
3456 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457{
3458 char_u *p;
3459 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003460 int len = 0;
3461 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3463 int compl = EXPAND_NOTHING;
3464#endif
3465#ifdef FEAT_CMDL_COMPL
3466 int delim;
3467#endif
3468 int forceit = FALSE;
3469 int usefilter = FALSE; /* filter instead of file name */
3470
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003471 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 xp->xp_pattern = buff;
3473 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003474 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475
3476/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003477 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 */
3479 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3480 ;
3481 xp->xp_pattern = cmd;
3482
3483 if (*cmd == NUL)
3484 return NULL;
3485 if (*cmd == '"') /* ignore comment lines */
3486 {
3487 xp->xp_context = EXPAND_NOTHING;
3488 return NULL;
3489 }
3490
3491/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003492 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 */
3494 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 xp->xp_pattern = cmd;
3496 if (*cmd == NUL)
3497 return NULL;
3498 if (*cmd == '"')
3499 {
3500 xp->xp_context = EXPAND_NOTHING;
3501 return NULL;
3502 }
3503
3504 if (*cmd == '|' || *cmd == '\n')
3505 return cmd + 1; /* There's another command */
3506
3507 /*
3508 * Isolate the command and search for it in the command table.
3509 * Exceptions:
3510 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003511 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3513 */
3514 if (*cmd == 'k' && cmd[1] != 'e')
3515 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003516 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 p = cmd + 1;
3518 }
3519 else
3520 {
3521 p = cmd;
3522 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3523 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003524 /* a user command may contain digits */
3525 if (ASCII_ISUPPER(cmd[0]))
3526 while (ASCII_ISALNUM(*p) || *p == '*')
3527 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003528 /* for python 3.x: ":py3*" commands completion */
3529 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003530 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003531 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003532 while (ASCII_ISALPHA(*p) || *p == '*')
3533 ++p;
3534 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003535 /* check for non-alpha command */
3536 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3537 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003538 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003540 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 {
3542 xp->xp_context = EXPAND_UNSUCCESSFUL;
3543 return NULL;
3544 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003545 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003546 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3547 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3548 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 break;
3550
3551#ifdef FEAT_USR_CMDS
3552 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3554 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555#endif
3556 }
3557
3558 /*
3559 * If the cursor is touching the command, and it ends in an alpha-numeric
3560 * character, complete the command name.
3561 */
3562 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3563 return NULL;
3564
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003565 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 {
3567 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3568 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003569 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 p = cmd + 1;
3571 }
3572#ifdef FEAT_USR_CMDS
3573 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3574 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003575 ea.cmd = cmd;
3576 p = find_ucmd(&ea, p, NULL, xp,
3577# if defined(FEAT_CMDL_COMPL)
3578 &compl
3579# else
3580 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003582 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003583 if (p == NULL)
3584 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 }
3586#endif
3587 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003588 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 {
3590 /* Not still touching the command and it was an illegal one */
3591 xp->xp_context = EXPAND_UNSUCCESSFUL;
3592 return NULL;
3593 }
3594
3595 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3596
3597 if (*p == '!') /* forced commands */
3598 {
3599 forceit = TRUE;
3600 ++p;
3601 }
3602
3603/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003604 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003606 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003607 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608
3609 arg = skipwhite(p);
3610
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003611 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 {
3613 if (*arg == '>') /* append */
3614 {
3615 if (*++arg == '>')
3616 ++arg;
3617 arg = skipwhite(arg);
3618 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003619 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 {
3621 ++arg;
3622 usefilter = TRUE;
3623 }
3624 }
3625
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003626 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 {
3628 usefilter = forceit; /* :r! filter if forced */
3629 if (*arg == '!') /* :r !filter */
3630 {
3631 ++arg;
3632 usefilter = TRUE;
3633 }
3634 }
3635
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003636 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 {
3638 while (*arg == *cmd) /* allow any number of '>' or '<' */
3639 ++arg;
3640 arg = skipwhite(arg);
3641 }
3642
3643 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003644 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 {
3646 /* Check if we're in the +command */
3647 p = arg + 1;
3648 arg = skip_cmd_arg(arg, FALSE);
3649
3650 /* Still touching the command after '+'? */
3651 if (*arg == NUL)
3652 return p;
3653
3654 /* Skip space(s) after +command to get to the real argument */
3655 arg = skipwhite(arg);
3656 }
3657
3658 /*
3659 * Check for '|' to separate commands and '"' to start comments.
3660 * Don't do this for ":read !cmd" and ":write !cmd".
3661 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003662 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 {
3664 p = arg;
3665 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003666 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 p += 2;
3668 while (*p)
3669 {
3670 if (*p == Ctrl_V)
3671 {
3672 if (p[1] != NUL)
3673 ++p;
3674 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003675 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 || *p == '|' || *p == '\n')
3677 {
3678 if (*(p - 1) != '\\')
3679 {
3680 if (*p == '|' || *p == '\n')
3681 return p + 1;
3682 return NULL; /* It's a comment */
3683 }
3684 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003685 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 }
3687 }
3688
3689 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003690 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 vim_strchr((char_u *)"|\"", *arg) == NULL)
3692 return NULL;
3693
3694 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003695 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003697 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003698 while (*p && p < buff + len)
3699 {
3700 if (*p == ' ' || *p == TAB)
3701 {
3702 /* argument starts after a space */
3703 xp->xp_pattern = ++p;
3704 }
3705 else
3706 {
3707 if (*p == '\\' && *(p + 1) != NUL)
3708 ++p; /* skip over escaped character */
3709 mb_ptr_adv(p);
3710 }
3711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003713 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003715 int c;
3716 int in_quote = FALSE;
3717 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718
3719 /*
3720 * Allow spaces within back-quotes to count as part of the argument
3721 * being expanded.
3722 */
3723 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003724 p = xp->xp_pattern;
3725 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003727#ifdef FEAT_MBYTE
3728 if (has_mbyte)
3729 c = mb_ptr2char(p);
3730 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003732 c = *p;
3733 if (c == '\\' && p[1] != NUL)
3734 ++p;
3735 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 {
3737 if (!in_quote)
3738 {
3739 xp->xp_pattern = p;
3740 bow = p + 1;
3741 }
3742 in_quote = !in_quote;
3743 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003744 /* An argument can contain just about everything, except
3745 * characters that end the command and white space. */
3746 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003747#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003748 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003749#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003750 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003751 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003752 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003753 while (*p != NUL)
3754 {
3755#ifdef FEAT_MBYTE
3756 if (has_mbyte)
3757 c = mb_ptr2char(p);
3758 else
3759#endif
3760 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003761 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003762 break;
3763#ifdef FEAT_MBYTE
3764 if (has_mbyte)
3765 len = (*mb_ptr2len)(p);
3766 else
3767#endif
3768 len = 1;
3769 mb_ptr_adv(p);
3770 }
3771 if (in_quote)
3772 bow = p;
3773 else
3774 xp->xp_pattern = p;
3775 p -= len;
3776 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003777 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 }
3779
3780 /*
3781 * If we are still inside the quotes, and we passed a space, just
3782 * expand from there.
3783 */
3784 if (bow != NULL && in_quote)
3785 xp->xp_pattern = bow;
3786 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003787
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003788 /* For a shell command more chars need to be escaped. */
3789 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003790 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003791#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003792 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003793#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003794 /* When still after the command name expand executables. */
3795 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003796 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798
3799 /* Check for environment variable */
3800 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003801#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 || *xp->xp_pattern == '%'
3803#endif
3804 )
3805 {
3806 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3807 if (!vim_isIDc(*p))
3808 break;
3809 if (*p == NUL)
3810 {
3811 xp->xp_context = EXPAND_ENV_VARS;
3812 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003813#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3814 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003815 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003816 compl = EXPAND_ENV_VARS;
3817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 }
3819 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003820#if defined(FEAT_CMDL_COMPL)
3821 /* Check for user names */
3822 if (*xp->xp_pattern == '~')
3823 {
3824 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3825 ;
3826 /* Complete ~user only if it partially matches a user name.
3827 * A full match ~user<Tab> will be replaced by user's home
3828 * directory i.e. something like ~user<Tab> -> /home/user/ */
3829 if (*p == NUL && p > xp->xp_pattern + 1
3830 && match_user(xp->xp_pattern + 1) == 1)
3831 {
3832 xp->xp_context = EXPAND_USER;
3833 ++xp->xp_pattern;
3834 }
3835 }
3836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 }
3838
3839/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003840 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003842 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003844 case CMD_find:
3845 case CMD_sfind:
3846 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003847 if (xp->xp_context == EXPAND_FILES)
3848 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003849 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 case CMD_cd:
3851 case CMD_chdir:
3852 case CMD_lcd:
3853 case CMD_lchdir:
3854 if (xp->xp_context == EXPAND_FILES)
3855 xp->xp_context = EXPAND_DIRECTORIES;
3856 break;
3857 case CMD_help:
3858 xp->xp_context = EXPAND_HELP;
3859 xp->xp_pattern = arg;
3860 break;
3861
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003862 /* Command modifiers: return the argument.
3863 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003865 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 case CMD_belowright:
3867 case CMD_botright:
3868 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003869 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003870 case CMD_cdo:
3871 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003873 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 case CMD_folddoclosed:
3875 case CMD_folddoopen:
3876 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003877 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 case CMD_keepjumps:
3879 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003880 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003881 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003883 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003885 case CMD_noautocmd:
3886 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003888 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003890 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003891 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 case CMD_topleft:
3893 case CMD_verbose:
3894 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003895 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 return arg;
3897
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003898 case CMD_filter:
3899 if (*arg != NUL)
3900 arg = skip_vimgrep_pat(arg, NULL, NULL);
3901 if (arg == NULL || *arg == NUL)
3902 {
3903 xp->xp_context = EXPAND_NOTHING;
3904 return NULL;
3905 }
3906 return skipwhite(arg);
3907
Bram Moolenaar4f688582007-07-24 12:34:30 +00003908#ifdef FEAT_CMDL_COMPL
3909# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 case CMD_match:
3911 if (*arg == NUL || !ends_excmd(*arg))
3912 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003913 /* also complete "None" */
3914 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 arg = skipwhite(skiptowhite(arg));
3916 if (*arg != NUL)
3917 {
3918 xp->xp_context = EXPAND_NOTHING;
3919 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3920 }
3921 }
3922 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003923# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925/*
3926 * All completion for the +cmdline_compl feature goes here.
3927 */
3928
3929# ifdef FEAT_USR_CMDS
3930 case CMD_command:
3931 /* Check for attributes */
3932 while (*arg == '-')
3933 {
3934 arg++; /* Skip "-" */
3935 p = skiptowhite(arg);
3936 if (*p == NUL)
3937 {
3938 /* Cursor is still in the attribute */
3939 p = vim_strchr(arg, '=');
3940 if (p == NULL)
3941 {
3942 /* No "=", so complete attribute names */
3943 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3944 xp->xp_pattern = arg;
3945 return NULL;
3946 }
3947
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003948 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 * their arguments as well.
3950 */
3951 if (STRNICMP(arg, "complete", p - arg) == 0)
3952 {
3953 xp->xp_context = EXPAND_USER_COMPLETE;
3954 xp->xp_pattern = p + 1;
3955 return NULL;
3956 }
3957 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3958 {
3959 xp->xp_context = EXPAND_USER_NARGS;
3960 xp->xp_pattern = p + 1;
3961 return NULL;
3962 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003963 else if (STRNICMP(arg, "addr", p - arg) == 0)
3964 {
3965 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3966 xp->xp_pattern = p + 1;
3967 return NULL;
3968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 return NULL;
3970 }
3971 arg = skipwhite(p);
3972 }
3973
3974 /* After the attributes comes the new command name */
3975 p = skiptowhite(arg);
3976 if (*p == NUL)
3977 {
3978 xp->xp_context = EXPAND_USER_COMMANDS;
3979 xp->xp_pattern = arg;
3980 break;
3981 }
3982
3983 /* And finally comes a normal command */
3984 return skipwhite(p);
3985
3986 case CMD_delcommand:
3987 xp->xp_context = EXPAND_USER_COMMANDS;
3988 xp->xp_pattern = arg;
3989 break;
3990# endif
3991
3992 case CMD_global:
3993 case CMD_vglobal:
3994 delim = *arg; /* get the delimiter */
3995 if (delim)
3996 ++arg; /* skip delimiter if there is one */
3997
3998 while (arg[0] != NUL && arg[0] != delim)
3999 {
4000 if (arg[0] == '\\' && arg[1] != NUL)
4001 ++arg;
4002 ++arg;
4003 }
4004 if (arg[0] != NUL)
4005 return arg + 1;
4006 break;
4007 case CMD_and:
4008 case CMD_substitute:
4009 delim = *arg;
4010 if (delim)
4011 {
4012 /* skip "from" part */
4013 ++arg;
4014 arg = skip_regexp(arg, delim, p_magic, NULL);
4015 }
4016 /* skip "to" part */
4017 while (arg[0] != NUL && arg[0] != delim)
4018 {
4019 if (arg[0] == '\\' && arg[1] != NUL)
4020 ++arg;
4021 ++arg;
4022 }
4023 if (arg[0] != NUL) /* skip delimiter */
4024 ++arg;
4025 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
4026 ++arg;
4027 if (arg[0] != NUL)
4028 return arg;
4029 break;
4030 case CMD_isearch:
4031 case CMD_dsearch:
4032 case CMD_ilist:
4033 case CMD_dlist:
4034 case CMD_ijump:
4035 case CMD_psearch:
4036 case CMD_djump:
4037 case CMD_isplit:
4038 case CMD_dsplit:
4039 arg = skipwhite(skipdigits(arg)); /* skip count */
4040 if (*arg == '/') /* Match regexp, not just whole words */
4041 {
4042 for (++arg; *arg && *arg != '/'; arg++)
4043 if (*arg == '\\' && arg[1] != NUL)
4044 arg++;
4045 if (*arg)
4046 {
4047 arg = skipwhite(arg + 1);
4048
4049 /* Check for trailing illegal characters */
4050 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4051 xp->xp_context = EXPAND_NOTHING;
4052 else
4053 return arg;
4054 }
4055 }
4056 break;
4057#ifdef FEAT_AUTOCMD
4058 case CMD_autocmd:
4059 return set_context_in_autocmd(xp, arg, FALSE);
4060
4061 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004062 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 return set_context_in_autocmd(xp, arg, TRUE);
4064#endif
4065 case CMD_set:
4066 set_context_in_set_cmd(xp, arg, 0);
4067 break;
4068 case CMD_setglobal:
4069 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4070 break;
4071 case CMD_setlocal:
4072 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4073 break;
4074 case CMD_tag:
4075 case CMD_stag:
4076 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004077 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 case CMD_tselect:
4079 case CMD_stselect:
4080 case CMD_ptselect:
4081 case CMD_tjump:
4082 case CMD_stjump:
4083 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004084 if (*p_wop != NUL)
4085 xp->xp_context = EXPAND_TAGS_LISTFILES;
4086 else
4087 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 xp->xp_pattern = arg;
4089 break;
4090 case CMD_augroup:
4091 xp->xp_context = EXPAND_AUGROUP;
4092 xp->xp_pattern = arg;
4093 break;
4094#ifdef FEAT_SYN_HL
4095 case CMD_syntax:
4096 set_context_in_syntax_cmd(xp, arg);
4097 break;
4098#endif
4099#ifdef FEAT_EVAL
4100 case CMD_let:
4101 case CMD_if:
4102 case CMD_elseif:
4103 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004104 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 case CMD_echo:
4106 case CMD_echon:
4107 case CMD_execute:
4108 case CMD_echomsg:
4109 case CMD_echoerr:
4110 case CMD_call:
4111 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004112 case CMD_cexpr:
4113 case CMD_caddexpr:
4114 case CMD_cgetexpr:
4115 case CMD_lexpr:
4116 case CMD_laddexpr:
4117 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004118 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 break;
4120
4121 case CMD_unlet:
4122 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4123 arg = xp->xp_pattern + 1;
4124 xp->xp_context = EXPAND_USER_VARS;
4125 xp->xp_pattern = arg;
4126 break;
4127
4128 case CMD_function:
4129 case CMD_delfunction:
4130 xp->xp_context = EXPAND_USER_FUNC;
4131 xp->xp_pattern = arg;
4132 break;
4133
4134 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004135 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 break;
4137#endif
4138 case CMD_highlight:
4139 set_context_in_highlight_cmd(xp, arg);
4140 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004141#ifdef FEAT_CSCOPE
4142 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004143 case CMD_lcscope:
4144 case CMD_scscope:
4145 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004146 break;
4147#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004148#ifdef FEAT_SIGNS
4149 case CMD_sign:
4150 set_context_in_sign_cmd(xp, arg);
4151 break;
4152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153#ifdef FEAT_LISTCMDS
4154 case CMD_bdelete:
4155 case CMD_bwipeout:
4156 case CMD_bunload:
4157 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4158 arg = xp->xp_pattern + 1;
4159 /*FALLTHROUGH*/
4160 case CMD_buffer:
4161 case CMD_sbuffer:
4162 case CMD_checktime:
4163 xp->xp_context = EXPAND_BUFFERS;
4164 xp->xp_pattern = arg;
4165 break;
4166#endif
4167#ifdef FEAT_USR_CMDS
4168 case CMD_USER:
4169 case CMD_USER_BUF:
4170 if (compl != EXPAND_NOTHING)
4171 {
4172 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004173 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 {
4175# ifdef FEAT_MENU
4176 if (compl == EXPAND_MENUS)
4177 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4178# endif
4179 if (compl == EXPAND_COMMANDS)
4180 return arg;
4181 if (compl == EXPAND_MAPPINGS)
4182 return set_context_in_map_cmd(xp, (char_u *)"map",
4183 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004184 /* Find start of last argument. */
4185 p = arg;
4186 while (*p)
4187 {
4188 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004189 /* argument starts after a space */
4190 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004191 else if (*p == '\\' && *(p + 1) != NUL)
4192 ++p; /* skip over escaped character */
4193 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 xp->xp_pattern = arg;
4196 }
4197 xp->xp_context = compl;
4198 }
4199 break;
4200#endif
4201 case CMD_map: case CMD_noremap:
4202 case CMD_nmap: case CMD_nnoremap:
4203 case CMD_vmap: case CMD_vnoremap:
4204 case CMD_omap: case CMD_onoremap:
4205 case CMD_imap: case CMD_inoremap:
4206 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004207 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004208 case CMD_smap: case CMD_snoremap:
4209 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004211 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 case CMD_unmap:
4213 case CMD_nunmap:
4214 case CMD_vunmap:
4215 case CMD_ounmap:
4216 case CMD_iunmap:
4217 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004218 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004219 case CMD_sunmap:
4220 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004222 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 case CMD_abbreviate: case CMD_noreabbrev:
4224 case CMD_cabbrev: case CMD_cnoreabbrev:
4225 case CMD_iabbrev: case CMD_inoreabbrev:
4226 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004227 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 case CMD_unabbreviate:
4229 case CMD_cunabbrev:
4230 case CMD_iunabbrev:
4231 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004232 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233#ifdef FEAT_MENU
4234 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4235 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4236 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4237 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4238 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4239 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4240 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4241 case CMD_tmenu: case CMD_tunmenu:
4242 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4243 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4244#endif
4245
4246 case CMD_colorscheme:
4247 xp->xp_context = EXPAND_COLORS;
4248 xp->xp_pattern = arg;
4249 break;
4250
4251 case CMD_compiler:
4252 xp->xp_context = EXPAND_COMPILER;
4253 xp->xp_pattern = arg;
4254 break;
4255
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004256 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004257 xp->xp_context = EXPAND_OWNSYNTAX;
4258 xp->xp_pattern = arg;
4259 break;
4260
4261 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004262 xp->xp_context = EXPAND_FILETYPE;
4263 xp->xp_pattern = arg;
4264 break;
4265
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004266 case CMD_packadd:
4267 xp->xp_context = EXPAND_PACKADD;
4268 xp->xp_pattern = arg;
4269 break;
4270
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4272 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4273 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004274 p = skiptowhite(arg);
4275 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 {
4277 xp->xp_context = EXPAND_LANGUAGE;
4278 xp->xp_pattern = arg;
4279 }
4280 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004281 {
4282 if ( STRNCMP(arg, "messages", p - arg) == 0
4283 || STRNCMP(arg, "ctype", p - arg) == 0
4284 || STRNCMP(arg, "time", p - arg) == 0)
4285 {
4286 xp->xp_context = EXPAND_LOCALES;
4287 xp->xp_pattern = skipwhite(p);
4288 }
4289 else
4290 xp->xp_context = EXPAND_NOTHING;
4291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 break;
4293#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004294#if defined(FEAT_PROFILE)
4295 case CMD_profile:
4296 set_context_in_profile_cmd(xp, arg);
4297 break;
4298#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004299 case CMD_behave:
4300 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004301 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004302 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004304 case CMD_messages:
4305 xp->xp_context = EXPAND_MESSAGES;
4306 xp->xp_pattern = arg;
4307 break;
4308
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004309#if defined(FEAT_CMDHIST)
4310 case CMD_history:
4311 xp->xp_context = EXPAND_HISTORY;
4312 xp->xp_pattern = arg;
4313 break;
4314#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004315#if defined(FEAT_PROFILE)
4316 case CMD_syntime:
4317 xp->xp_context = EXPAND_SYNTIME;
4318 xp->xp_pattern = arg;
4319 break;
4320#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004321
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322#endif /* FEAT_CMDL_COMPL */
4323
4324 default:
4325 break;
4326 }
4327 return NULL;
4328}
4329
4330/*
4331 * skip a range specifier of the form: addr [,addr] [;addr] ..
4332 *
4333 * Backslashed delimiters after / or ? will be skipped, and commands will
4334 * not be expanded between /'s and ?'s or after "'".
4335 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004336 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 * Returns the "cmd" pointer advanced to beyond the range.
4338 */
4339 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004340skip_range(
4341 char_u *cmd,
4342 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004344 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345
Bram Moolenaardf177f62005-02-22 08:39:57 +00004346 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 {
4348 if (*cmd == '\'')
4349 {
4350 if (*++cmd == NUL && ctx != NULL)
4351 *ctx = EXPAND_NOTHING;
4352 }
4353 else if (*cmd == '/' || *cmd == '?')
4354 {
4355 delim = *cmd++;
4356 while (*cmd != NUL && *cmd != delim)
4357 if (*cmd++ == '\\' && *cmd != NUL)
4358 ++cmd;
4359 if (*cmd == NUL && ctx != NULL)
4360 *ctx = EXPAND_NOTHING;
4361 }
4362 if (*cmd != NUL)
4363 ++cmd;
4364 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004365
4366 /* Skip ":" and white space. */
4367 while (*cmd == ':')
4368 cmd = skipwhite(cmd + 1);
4369
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 return cmd;
4371}
4372
4373/*
4374 * get a single EX address
4375 *
4376 * Set ptr to the next character after the part that was interpreted.
4377 * Set ptr to NULL when an error is encountered.
4378 *
4379 * Return MAXLNUM when no Ex address was found.
4380 */
4381 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004382get_address(
4383 exarg_T *eap UNUSED,
4384 char_u **ptr,
4385 int addr_type, /* flag: one of ADDR_LINES, ... */
4386 int skip, /* only skip the address, don't use it */
Bram Moolenaarded27822017-01-02 14:27:34 +01004387 int to_other_file, /* flag: may jump to other file */
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01004388 int address_count UNUSED) /* 1 for first address, >1 after comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389{
4390 int c;
4391 int i;
4392 long n;
4393 char_u *cmd;
4394 pos_T pos;
4395 pos_T *fp;
4396 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004397 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398
4399 cmd = skipwhite(*ptr);
4400 lnum = MAXLNUM;
4401 do
4402 {
4403 switch (*cmd)
4404 {
4405 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004406 ++cmd;
4407 switch (addr_type)
4408 {
4409 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 lnum = curwin->w_cursor.lnum;
4411 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004412 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004413 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004414 break;
4415 case ADDR_ARGUMENTS:
4416 lnum = curwin->w_arg_idx + 1;
4417 break;
4418 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004419 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004420 lnum = curbuf->b_fnum;
4421 break;
4422 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004423 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004424 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004425#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004426 case ADDR_QUICKFIX:
4427 lnum = qf_get_cur_valid_idx(eap);
4428 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004429#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004430 }
4431 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432
4433 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004434 ++cmd;
4435 switch (addr_type)
4436 {
4437 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 lnum = curbuf->b_ml.ml_line_count;
4439 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004440 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004441 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004442 break;
4443 case ADDR_ARGUMENTS:
4444 lnum = ARGCOUNT;
4445 break;
4446 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004447 buf = lastbuf;
4448 while (buf->b_ml.ml_mfp == NULL)
4449 {
4450 if (buf->b_prev == NULL)
4451 break;
4452 buf = buf->b_prev;
4453 }
4454 lnum = buf->b_fnum;
4455 break;
4456 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004457 lnum = lastbuf->b_fnum;
4458 break;
4459 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004460 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004461 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004462#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004463 case ADDR_QUICKFIX:
4464 lnum = qf_get_size(eap);
4465 if (lnum == 0)
4466 lnum = 1;
4467 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004468#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004469 }
4470 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471
4472 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004473 if (*++cmd == NUL)
4474 {
4475 cmd = NULL;
4476 goto error;
4477 }
4478 if (addr_type != ADDR_LINES)
4479 {
4480 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004481 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004482 goto error;
4483 }
4484 if (skip)
4485 ++cmd;
4486 else
4487 {
4488 /* Only accept a mark in another file when it is
4489 * used by itself: ":'M". */
4490 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4491 ++cmd;
4492 if (fp == (pos_T *)-1)
4493 /* Jumped to another file. */
4494 lnum = curwin->w_cursor.lnum;
4495 else
4496 {
4497 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 {
4499 cmd = NULL;
4500 goto error;
4501 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004502 lnum = fp->lnum;
4503 }
4504 }
4505 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506
4507 case '/':
4508 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004509 c = *cmd++;
4510 if (addr_type != ADDR_LINES)
4511 {
4512 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004513 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004514 goto error;
4515 }
4516 if (skip) /* skip "/pat/" */
4517 {
4518 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4519 if (*cmd == c)
4520 ++cmd;
4521 }
4522 else
4523 {
4524 pos = curwin->w_cursor; /* save curwin->w_cursor */
4525 /*
4526 * When '/' or '?' follows another address, start
4527 * from there.
4528 */
4529 if (lnum != MAXLNUM)
4530 curwin->w_cursor.lnum = lnum;
4531 /*
4532 * Start a forward search at the end of the line.
4533 * Start a backward search at the start of the line.
4534 * This makes sure we never match in the current
4535 * line, and can match anywhere in the
4536 * next/previous line.
4537 */
4538 if (c == '/')
4539 curwin->w_cursor.col = MAXCOL;
4540 else
4541 curwin->w_cursor.col = 0;
4542 searchcmdlen = 0;
4543 if (!do_search(NULL, c, cmd, 1L,
4544 SEARCH_HIS | SEARCH_MSG, NULL))
4545 {
4546 curwin->w_cursor = pos;
4547 cmd = NULL;
4548 goto error;
4549 }
4550 lnum = curwin->w_cursor.lnum;
4551 curwin->w_cursor = pos;
4552 /* adjust command string pointer */
4553 cmd += searchcmdlen;
4554 }
4555 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556
4557 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004558 ++cmd;
4559 if (addr_type != ADDR_LINES)
4560 {
4561 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004562 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004563 goto error;
4564 }
4565 if (*cmd == '&')
4566 i = RE_SUBST;
4567 else if (*cmd == '?' || *cmd == '/')
4568 i = RE_SEARCH;
4569 else
4570 {
4571 EMSG(_(e_backslash));
4572 cmd = NULL;
4573 goto error;
4574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004576 if (!skip)
4577 {
4578 /*
4579 * When search follows another address, start from
4580 * there.
4581 */
4582 if (lnum != MAXLNUM)
4583 pos.lnum = lnum;
4584 else
4585 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004587 /*
4588 * Start the search just like for the above
4589 * do_search().
4590 */
4591 if (*cmd != '?')
4592 pos.col = MAXCOL;
4593 else
4594 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004595#ifdef FEAT_VIRTUALEDIT
4596 pos.coladd = 0;
4597#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004598 if (searchit(curwin, curbuf, &pos,
4599 *cmd == '?' ? BACKWARD : FORWARD,
4600 (char_u *)"", 1L, SEARCH_MSG,
4601 i, (linenr_T)0, NULL) != FAIL)
4602 lnum = pos.lnum;
4603 else
4604 {
4605 cmd = NULL;
4606 goto error;
4607 }
4608 }
4609 ++cmd;
4610 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611
4612 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004613 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4614 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 }
4616
4617 for (;;)
4618 {
4619 cmd = skipwhite(cmd);
4620 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4621 break;
4622
4623 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004624 {
4625 switch (addr_type)
4626 {
4627 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004628 /* "+1" is same as ".+1" */
4629 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004630 break;
4631 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004632 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004633 break;
4634 case ADDR_ARGUMENTS:
4635 lnum = curwin->w_arg_idx + 1;
4636 break;
4637 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004638 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004639 lnum = curbuf->b_fnum;
4640 break;
4641 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004642 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004643 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004644#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004645 case ADDR_QUICKFIX:
4646 lnum = qf_get_cur_valid_idx(eap);
4647 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004648#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004649 }
4650 }
4651
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 if (VIM_ISDIGIT(*cmd))
4653 i = '+'; /* "number" is same as "+number" */
4654 else
4655 i = *cmd++;
4656 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4657 n = 1;
4658 else
4659 n = getdigits(&cmd);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004660 if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004661 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004662 lnum = compute_buffer_local_count(
4663 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004665 {
4666#ifdef FEAT_FOLDING
4667 /* Relative line addressing, need to adjust for folded lines
4668 * now, but only do it after the first address. */
4669 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4670 && address_count >= 2)
4671 (void)hasFolding(lnum, NULL, &lnum);
4672#endif
4673 if (i == '-')
4674 lnum -= n;
4675 else
4676 lnum += n;
4677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 }
4679 } while (*cmd == '/' || *cmd == '?');
4680
4681error:
4682 *ptr = cmd;
4683 return lnum;
4684}
4685
4686/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004687 * Get flags from an Ex command argument.
4688 */
4689 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004690get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004691{
4692 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4693 {
4694 if (*eap->arg == 'l')
4695 eap->flags |= EXFLAG_LIST;
4696 else if (*eap->arg == 'p')
4697 eap->flags |= EXFLAG_PRINT;
4698 else
4699 eap->flags |= EXFLAG_NR;
4700 eap->arg = skipwhite(eap->arg + 1);
4701 }
4702}
4703
4704/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 * Function called for command which is Not Implemented. NI!
4706 */
4707 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004708ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709{
4710 if (!eap->skip)
4711 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4712}
4713
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004714#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715/*
4716 * Function called for script command which is Not Implemented. NI!
4717 * Skips over ":perl <<EOF" constructs.
4718 */
4719 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004720ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721{
4722 if (!eap->skip)
4723 ex_ni(eap);
4724 else
4725 vim_free(script_get(eap, eap->arg));
4726}
4727#endif
4728
4729/*
4730 * Check range in Ex command for validity.
4731 * Return NULL when valid, error message when invalid.
4732 */
4733 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004734invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004736 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 if ( eap->line1 < 0
4738 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004739 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004741
4742 if (eap->argt & RANGE)
4743 {
4744 switch(eap->addr_type)
4745 {
4746 case ADDR_LINES:
4747 if (!(eap->argt & NOTADR)
4748 && eap->line2 > curbuf->b_ml.ml_line_count
4749#ifdef FEAT_DIFF
4750 + (eap->cmdidx == CMD_diffget)
4751#endif
4752 )
4753 return (char_u *)_(e_invrange);
4754 break;
4755 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004756 /* add 1 if ARGCOUNT is 0 */
4757 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004758 return (char_u *)_(e_invrange);
4759 break;
4760 case ADDR_BUFFERS:
4761 if (eap->line1 < firstbuf->b_fnum
4762 || eap->line2 > lastbuf->b_fnum)
4763 return (char_u *)_(e_invrange);
4764 break;
4765 case ADDR_LOADED_BUFFERS:
4766 buf = firstbuf;
4767 while (buf->b_ml.ml_mfp == NULL)
4768 {
4769 if (buf->b_next == NULL)
4770 return (char_u *)_(e_invrange);
4771 buf = buf->b_next;
4772 }
4773 if (eap->line1 < buf->b_fnum)
4774 return (char_u *)_(e_invrange);
4775 buf = lastbuf;
4776 while (buf->b_ml.ml_mfp == NULL)
4777 {
4778 if (buf->b_prev == NULL)
4779 return (char_u *)_(e_invrange);
4780 buf = buf->b_prev;
4781 }
4782 if (eap->line2 > buf->b_fnum)
4783 return (char_u *)_(e_invrange);
4784 break;
4785 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004786 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004787 return (char_u *)_(e_invrange);
4788 break;
4789 case ADDR_TABS:
4790 if (eap->line2 > LAST_TAB_NR)
4791 return (char_u *)_(e_invrange);
4792 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004793#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004794 case ADDR_QUICKFIX:
4795 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4796 return (char_u *)_(e_invrange);
4797 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004798#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004799 }
4800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 return NULL;
4802}
4803
4804/*
4805 * Correct the range for zero line number, if required.
4806 */
4807 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004808correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809{
4810 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4811 {
4812 if (eap->line1 == 0)
4813 eap->line1 = 1;
4814 if (eap->line2 == 0)
4815 eap->line2 = 1;
4816 }
4817}
4818
Bram Moolenaar748bf032005-02-02 23:04:36 +00004819#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004820static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004821
4822/*
4823 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4824 * pattern. Otherwise return eap->arg.
4825 */
4826 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004827skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004828{
4829 char_u *p = eap->arg;
4830
Bram Moolenaara37420f2006-02-04 22:37:47 +00004831 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4832 || eap->cmdidx == CMD_vimgrepadd
4833 || eap->cmdidx == CMD_lvimgrepadd
4834 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004835 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004836 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004837 if (p == NULL)
4838 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004839 }
4840 return p;
4841}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004842
4843/*
4844 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4845 * in the command line, so that things like % get expanded.
4846 */
4847 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004848replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004849{
4850 char_u *new_cmdline;
4851 char_u *program;
4852 char_u *pos;
4853 char_u *ptr;
4854 int len;
4855 int i;
4856
4857 /*
4858 * Don't do it when ":vimgrep" is used for ":grep".
4859 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004860 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4861 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4862 || eap->cmdidx == CMD_grepadd
4863 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004864 && !grep_internal(eap->cmdidx))
4865 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004866 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4867 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004868 {
4869 if (*curbuf->b_p_gp == NUL)
4870 program = p_gp;
4871 else
4872 program = curbuf->b_p_gp;
4873 }
4874 else
4875 {
4876 if (*curbuf->b_p_mp == NUL)
4877 program = p_mp;
4878 else
4879 program = curbuf->b_p_mp;
4880 }
4881
4882 p = skipwhite(p);
4883
4884 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4885 {
4886 /* replace $* by given arguments */
4887 i = 1;
4888 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4889 ++i;
4890 len = (int)STRLEN(p);
4891 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4892 if (new_cmdline == NULL)
4893 return NULL; /* out of memory */
4894 ptr = new_cmdline;
4895 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4896 {
4897 i = (int)(pos - program);
4898 STRNCPY(ptr, program, i);
4899 STRCPY(ptr += i, p);
4900 ptr += len;
4901 program = pos + 2;
4902 }
4903 STRCPY(ptr, program);
4904 }
4905 else
4906 {
4907 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4908 if (new_cmdline == NULL)
4909 return NULL; /* out of memory */
4910 STRCPY(new_cmdline, program);
4911 STRCAT(new_cmdline, " ");
4912 STRCAT(new_cmdline, p);
4913 }
4914 msg_make(p);
4915
4916 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4917 vim_free(*cmdlinep);
4918 *cmdlinep = new_cmdline;
4919 p = new_cmdline;
4920 }
4921 return p;
4922}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004923#endif
4924
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925/*
4926 * Expand file name in Ex command argument.
4927 * Return FAIL for failure, OK otherwise.
4928 */
4929 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004930expand_filename(
4931 exarg_T *eap,
4932 char_u **cmdlinep,
4933 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934{
4935 int has_wildcards; /* need to expand wildcards */
4936 char_u *repl;
4937 int srclen;
4938 char_u *p;
4939 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004940 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941
Bram Moolenaar748bf032005-02-02 23:04:36 +00004942#ifdef FEAT_QUICKFIX
4943 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4944 p = skip_grep_pat(eap);
4945#else
4946 p = eap->arg;
4947#endif
4948
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 /*
4950 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4951 * the file name contains a wildcard it should not cause expanding.
4952 * (it will be expanded anyway if there is a wildcard before replacing).
4953 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004954 has_wildcards = mch_has_wildcard(p);
4955 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004957#ifdef FEAT_EVAL
4958 /* Skip over `=expr`, wildcards in it are not expanded. */
4959 if (p[0] == '`' && p[1] == '=')
4960 {
4961 p += 2;
4962 (void)skip_expr(&p);
4963 if (*p == '`')
4964 ++p;
4965 continue;
4966 }
4967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 /*
4969 * Quick check if this cannot be the start of a special string.
4970 * Also removes backslash before '%', '#' and '<'.
4971 */
4972 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4973 {
4974 ++p;
4975 continue;
4976 }
4977
4978 /*
4979 * Try to find a match at this position.
4980 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004981 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4982 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 if (*errormsgp != NULL) /* error detected */
4984 return FAIL;
4985 if (repl == NULL) /* no match found */
4986 {
4987 p += srclen;
4988 continue;
4989 }
4990
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004991 /* Wildcards won't be expanded below, the replacement is taken
4992 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4993 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4994 {
4995 char_u *l = repl;
4996
4997 repl = expand_env_save(repl);
4998 vim_free(l);
4999 }
5000
Bram Moolenaar63b92542007-03-27 14:57:09 +00005001 /* Need to escape white space et al. with a backslash.
5002 * Don't do this for:
5003 * - replacement that already has been escaped: "##"
5004 * - shell commands (may have to use quotes instead).
5005 * - non-unix systems when there is a single argument (spaces don't
5006 * separate arguments then).
5007 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005009 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 && eap->cmdidx != CMD_bang
5011 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00005012 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00005014 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00005016 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017#ifndef UNIX
5018 && !(eap->argt & NOSPC)
5019#endif
5020 )
5021 {
5022 char_u *l;
5023#ifdef BACKSLASH_IN_FILENAME
5024 /* Don't escape a backslash here, because rem_backslash() doesn't
5025 * remove it later. */
5026 static char_u *nobslash = (char_u *)" \t\"|";
5027# define ESCAPE_CHARS nobslash
5028#else
5029# define ESCAPE_CHARS escape_chars
5030#endif
5031
5032 for (l = repl; *l; ++l)
5033 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5034 {
5035 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5036 if (l != NULL)
5037 {
5038 vim_free(repl);
5039 repl = l;
5040 }
5041 break;
5042 }
5043 }
5044
5045 /* For a shell command a '!' must be escaped. */
5046 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005047 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 {
5049 char_u *l;
5050
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005051 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 if (l != NULL)
5053 {
5054 vim_free(repl);
5055 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 }
5057 }
5058
5059 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5060 vim_free(repl);
5061 if (p == NULL)
5062 return FAIL;
5063 }
5064
5065 /*
5066 * One file argument: Expand wildcards.
5067 * Don't do this with ":r !command" or ":w !command".
5068 */
5069 if ((eap->argt & NOSPC) && !eap->usefilter)
5070 {
5071 /*
5072 * May do this twice:
5073 * 1. Replace environment variables.
5074 * 2. Replace any other wildcards, remove backslashes.
5075 */
5076 for (n = 1; n <= 2; ++n)
5077 {
5078 if (n == 2)
5079 {
5080#ifdef UNIX
5081 /*
5082 * Only for Unix we check for more than one file name.
5083 * For other systems spaces are considered to be part
5084 * of the file name.
5085 * Only check here if there is no wildcard, otherwise
5086 * ExpandOne() will check for errors. This allows
5087 * ":e `ls ve*.c`" on Unix.
5088 */
5089 if (!has_wildcards)
5090 for (p = eap->arg; *p; ++p)
5091 {
5092 /* skip escaped characters */
5093 if (p[1] && (*p == '\\' || *p == Ctrl_V))
5094 ++p;
5095 else if (vim_iswhite(*p))
5096 {
5097 *errormsgp = (char_u *)_("E172: Only one file name allowed");
5098 return FAIL;
5099 }
5100 }
5101#endif
5102
5103 /*
5104 * Halve the number of backslashes (this is Vi compatible).
5105 * For Unix and OS/2, when wildcards are expanded, this is
5106 * done by ExpandOne() below.
5107 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005108#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 if (!has_wildcards)
5110#endif
5111 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 }
5113
5114 if (has_wildcards)
5115 {
5116 if (n == 1)
5117 {
5118 /*
5119 * First loop: May expand environment variables. This
5120 * can be done much faster with expand_env() than with
5121 * something else (e.g., calling a shell).
5122 * After expanding environment variables, check again
5123 * if there are still wildcards present.
5124 */
5125 if (vim_strchr(eap->arg, '$') != NULL
5126 || vim_strchr(eap->arg, '~') != NULL)
5127 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005128 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005129 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 has_wildcards = mch_has_wildcard(NameBuff);
5131 p = NameBuff;
5132 }
5133 else
5134 p = NULL;
5135 }
5136 else /* n == 2 */
5137 {
5138 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005139 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140
5141 ExpandInit(&xpc);
5142 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005143 if (p_wic)
5144 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005146 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 if (p == NULL)
5148 return FAIL;
5149 }
5150 if (p != NULL)
5151 {
5152 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5153 p, cmdlinep);
5154 if (n == 2) /* p came from ExpandOne() */
5155 vim_free(p);
5156 }
5157 }
5158 }
5159 }
5160 return OK;
5161}
5162
5163/*
5164 * Replace part of the command line, keeping eap->cmd, eap->arg and
5165 * eap->nextcmd correct.
5166 * "src" points to the part that is to be replaced, of length "srclen".
5167 * "repl" is the replacement string.
5168 * Returns a pointer to the character after the replaced string.
5169 * Returns NULL for failure.
5170 */
5171 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005172repl_cmdline(
5173 exarg_T *eap,
5174 char_u *src,
5175 int srclen,
5176 char_u *repl,
5177 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178{
5179 int len;
5180 int i;
5181 char_u *new_cmdline;
5182
5183 /*
5184 * The new command line is build in new_cmdline[].
5185 * First allocate it.
5186 * Careful: a "+cmd" argument may have been NUL terminated.
5187 */
5188 len = (int)STRLEN(repl);
5189 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005190 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5192 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5193 return NULL; /* out of memory! */
5194
5195 /*
5196 * Copy the stuff before the expanded part.
5197 * Copy the expanded stuff.
5198 * Copy what came after the expanded part.
5199 * Copy the next commands, if there are any.
5200 */
5201 i = (int)(src - *cmdlinep); /* length of part before match */
5202 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005203
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 mch_memmove(new_cmdline + i, repl, (size_t)len);
5205 i += len; /* remember the end of the string */
5206 STRCPY(new_cmdline + i, src + srclen);
5207 src = new_cmdline + i; /* remember where to continue */
5208
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005209 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 {
5211 i = (int)STRLEN(new_cmdline) + 1;
5212 STRCPY(new_cmdline + i, eap->nextcmd);
5213 eap->nextcmd = new_cmdline + i;
5214 }
5215 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5216 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5217 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5218 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5219 vim_free(*cmdlinep);
5220 *cmdlinep = new_cmdline;
5221
5222 return src;
5223}
5224
5225/*
5226 * Check for '|' to separate commands and '"' to start comments.
5227 */
5228 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005229separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230{
5231 char_u *p;
5232
Bram Moolenaar86b68352004-12-27 21:59:20 +00005233#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005234 p = skip_grep_pat(eap);
5235#else
5236 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005237#endif
5238
5239 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 {
5241 if (*p == Ctrl_V)
5242 {
5243 if (eap->argt & (USECTRLV | XFILE))
5244 ++p; /* skip CTRL-V and next char */
5245 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005246 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005247 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 if (*p == NUL) /* stop at NUL after CTRL-V */
5249 break;
5250 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005251
5252#ifdef FEAT_EVAL
5253 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005254 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005255 {
5256 p += 2;
5257 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005258 }
5259#endif
5260
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 /* Check for '"': start of comment or '|': next command */
5262 /* :@" and :*" do not start a comment!
5263 * :redir @" doesn't either. */
5264 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5265 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5266 || p != eap->arg)
5267 && (eap->cmdidx != CMD_redir
5268 || p != eap->arg + 1 || p[-1] != '@'))
5269 || *p == '|' || *p == '\n')
5270 {
5271 /*
5272 * We remove the '\' before the '|', unless USECTRLV is used
5273 * AND 'b' is present in 'cpoptions'.
5274 */
5275 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5276 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5277 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005278 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 --p;
5280 }
5281 else
5282 {
5283 eap->nextcmd = check_nextcmd(p);
5284 *p = NUL;
5285 break;
5286 }
5287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005289
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5291 del_trailing_spaces(eap->arg);
5292}
5293
5294/*
5295 * get + command from ex argument
5296 */
5297 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005298getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299{
5300 char_u *arg = *argp;
5301 char_u *command = NULL;
5302
5303 if (*arg == '+') /* +[command] */
5304 {
5305 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005306 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 command = dollar_command;
5308 else
5309 {
5310 command = arg;
5311 arg = skip_cmd_arg(command, TRUE);
5312 if (*arg != NUL)
5313 *arg++ = NUL; /* terminate command with NUL */
5314 }
5315
5316 arg = skipwhite(arg); /* skip over spaces */
5317 *argp = arg;
5318 }
5319 return command;
5320}
5321
5322/*
5323 * Find end of "+command" argument. Skip over "\ " and "\\".
5324 */
5325 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005326skip_cmd_arg(
5327 char_u *p,
5328 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329{
5330 while (*p && !vim_isspace(*p))
5331 {
5332 if (*p == '\\' && p[1] != NUL)
5333 {
5334 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005335 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 else
5337 ++p;
5338 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005339 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 }
5341 return p;
5342}
5343
5344/*
5345 * Get "++opt=arg" argument.
5346 * Return FAIL or OK.
5347 */
5348 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005349getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350{
5351 char_u *arg = eap->arg + 2;
5352 int *pp = NULL;
5353#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005354 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 char_u *p;
5356#endif
5357
5358 /* ":edit ++[no]bin[ary] file" */
5359 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5360 {
5361 if (*arg == 'n')
5362 {
5363 arg += 2;
5364 eap->force_bin = FORCE_NOBIN;
5365 }
5366 else
5367 eap->force_bin = FORCE_BIN;
5368 if (!checkforcmd(&arg, "binary", 3))
5369 return FAIL;
5370 eap->arg = skipwhite(arg);
5371 return OK;
5372 }
5373
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005374 /* ":read ++edit file" */
5375 if (STRNCMP(arg, "edit", 4) == 0)
5376 {
5377 eap->read_edit = TRUE;
5378 eap->arg = skipwhite(arg + 4);
5379 return OK;
5380 }
5381
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 if (STRNCMP(arg, "ff", 2) == 0)
5383 {
5384 arg += 2;
5385 pp = &eap->force_ff;
5386 }
5387 else if (STRNCMP(arg, "fileformat", 10) == 0)
5388 {
5389 arg += 10;
5390 pp = &eap->force_ff;
5391 }
5392#ifdef FEAT_MBYTE
5393 else if (STRNCMP(arg, "enc", 3) == 0)
5394 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005395 if (STRNCMP(arg, "encoding", 8) == 0)
5396 arg += 8;
5397 else
5398 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 pp = &eap->force_enc;
5400 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005401 else if (STRNCMP(arg, "bad", 3) == 0)
5402 {
5403 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005404 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406#endif
5407
5408 if (pp == NULL || *arg != '=')
5409 return FAIL;
5410
5411 ++arg;
5412 *pp = (int)(arg - eap->cmd);
5413 arg = skip_cmd_arg(arg, FALSE);
5414 eap->arg = skipwhite(arg);
5415 *arg = NUL;
5416
5417#ifdef FEAT_MBYTE
5418 if (pp == &eap->force_ff)
5419 {
5420#endif
5421 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5422 return FAIL;
5423#ifdef FEAT_MBYTE
5424 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005425 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 {
5427 /* Make 'fileencoding' lower case. */
5428 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5429 *p = TOLOWER_ASC(*p);
5430 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005431 else
5432 {
5433 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5434 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005435 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005436 if (STRICMP(p, "keep") == 0)
5437 eap->bad_char = BAD_KEEP;
5438 else if (STRICMP(p, "drop") == 0)
5439 eap->bad_char = BAD_DROP;
5440 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5441 eap->bad_char = *p;
5442 else
5443 return FAIL;
5444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445#endif
5446
5447 return OK;
5448}
5449
5450/*
5451 * ":abbreviate" and friends.
5452 */
5453 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005454ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455{
5456 do_exmap(eap, TRUE); /* almost the same as mapping */
5457}
5458
5459/*
5460 * ":map" and friends.
5461 */
5462 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005463ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464{
5465 /*
5466 * If we are sourcing .exrc or .vimrc in current directory we
5467 * print the mappings for security reasons.
5468 */
5469 if (secure)
5470 {
5471 secure = 2;
5472 msg_outtrans(eap->cmd);
5473 msg_putchar('\n');
5474 }
5475 do_exmap(eap, FALSE);
5476}
5477
5478/*
5479 * ":unmap" and friends.
5480 */
5481 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005482ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483{
5484 do_exmap(eap, FALSE);
5485}
5486
5487/*
5488 * ":mapclear" and friends.
5489 */
5490 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005491ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492{
5493 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5494}
5495
5496/*
5497 * ":abclear" and friends.
5498 */
5499 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005500ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501{
5502 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5503}
5504
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005505#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005507ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508{
5509 /*
5510 * Disallow auto commands from .exrc and .vimrc in current
5511 * directory for security reasons.
5512 */
5513 if (secure)
5514 {
5515 secure = 2;
5516 eap->errmsg = e_curdir;
5517 }
5518 else if (eap->cmdidx == CMD_autocmd)
5519 do_autocmd(eap->arg, eap->forceit);
5520 else
5521 do_augroup(eap->arg, eap->forceit);
5522}
5523
5524/*
5525 * ":doautocmd": Apply the automatic commands to the current buffer.
5526 */
5527 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005528ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005530 char_u *arg = eap->arg;
5531 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005532 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005533
Bram Moolenaar1610d052016-06-09 22:53:01 +02005534 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5535 /* Only when there is no <nomodeline>. */
5536 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005537 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538}
5539#endif
5540
5541#ifdef FEAT_LISTCMDS
5542/*
5543 * :[N]bunload[!] [N] [bufname] unload buffer
5544 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5545 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5546 */
5547 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005548ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549{
5550 eap->errmsg = do_bufdel(
5551 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5552 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5553 : DOBUF_UNLOAD, eap->arg,
5554 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5555}
5556
5557/*
5558 * :[N]buffer [N] to buffer N
5559 * :[N]sbuffer [N] to buffer N
5560 */
5561 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005562ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563{
5564 if (*eap->arg)
5565 eap->errmsg = e_trailing;
5566 else
5567 {
5568 if (eap->addr_count == 0) /* default is current buffer */
5569 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5570 else
5571 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005572 if (eap->do_ecmd_cmd != NULL)
5573 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 }
5575}
5576
5577/*
5578 * :[N]bmodified [N] to next mod. buffer
5579 * :[N]sbmodified [N] to next mod. buffer
5580 */
5581 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005582ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583{
5584 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005585 if (eap->do_ecmd_cmd != NULL)
5586 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587}
5588
5589/*
5590 * :[N]bnext [N] to next buffer
5591 * :[N]sbnext [N] split and to next buffer
5592 */
5593 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005594ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595{
5596 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005597 if (eap->do_ecmd_cmd != NULL)
5598 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599}
5600
5601/*
5602 * :[N]bNext [N] to previous buffer
5603 * :[N]bprevious [N] to previous buffer
5604 * :[N]sbNext [N] split and to previous buffer
5605 * :[N]sbprevious [N] split and to previous buffer
5606 */
5607 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005608ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609{
5610 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005611 if (eap->do_ecmd_cmd != NULL)
5612 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613}
5614
5615/*
5616 * :brewind to first buffer
5617 * :bfirst to first buffer
5618 * :sbrewind split and to first buffer
5619 * :sbfirst split and to first buffer
5620 */
5621 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005622ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623{
5624 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005625 if (eap->do_ecmd_cmd != NULL)
5626 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627}
5628
5629/*
5630 * :blast to last buffer
5631 * :sblast split and to last buffer
5632 */
5633 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005634ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635{
5636 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005637 if (eap->do_ecmd_cmd != NULL)
5638 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639}
5640#endif
5641
5642 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005643ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644{
5645 return (c == NUL || c == '|' || c == '"' || c == '\n');
5646}
5647
5648#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5649 || defined(PROTO)
5650/*
5651 * Return the next command, after the first '|' or '\n'.
5652 * Return NULL if not found.
5653 */
5654 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005655find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656{
5657 while (*p != '|' && *p != '\n')
5658 {
5659 if (*p == NUL)
5660 return NULL;
5661 ++p;
5662 }
5663 return (p + 1);
5664}
5665#endif
5666
5667/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005668 * Check if *p is a separator between Ex commands, skipping over white space.
5669 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 */
5671 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005672check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005674 char_u *s = skipwhite(p);
5675
5676 if (*s == '|' || *s == '\n')
5677 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 else
5679 return NULL;
5680}
5681
5682/*
5683 * - if there are more files to edit
5684 * - and this is the last window
5685 * - and forceit not used
5686 * - and not repeated twice on a row
5687 * return FAIL and give error message if 'message' TRUE
5688 * return OK otherwise
5689 */
5690 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005691check_more(
5692 int message, /* when FALSE check only, no messages */
5693 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694{
5695 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5696
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005697 if (!forceit && only_one_window()
5698 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 {
5700 if (message)
5701 {
5702#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5703 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5704 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005705 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706
5707 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005708 vim_strncpy(buff,
5709 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005710 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005712 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 _("%d more files to edit. Quit anyway?"), n);
5714 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5715 return OK;
5716 return FAIL;
5717 }
5718#endif
5719 if (n == 1)
5720 EMSG(_("E173: 1 more file to edit"));
5721 else
5722 EMSGN(_("E173: %ld more files to edit"), n);
5723 quitmore = 2; /* next try to quit is allowed */
5724 }
5725 return FAIL;
5726 }
5727 return OK;
5728}
5729
5730#ifdef FEAT_CMDL_COMPL
5731/*
5732 * Function given to ExpandGeneric() to obtain the list of command names.
5733 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005735get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736{
5737 if (idx >= (int)CMD_SIZE)
5738# ifdef FEAT_USR_CMDS
5739 return get_user_command_name(idx);
5740# else
5741 return NULL;
5742# endif
5743 return cmdnames[idx].cmd_name;
5744}
5745#endif
5746
5747#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005748static 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);
5749static void uc_list(char_u *name, size_t name_len);
5750static 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);
5751static char_u *uc_split_args(char_u *arg, size_t *lenp);
5752static 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 +00005753
5754 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005755uc_add_command(
5756 char_u *name,
5757 size_t name_len,
5758 char_u *rep,
5759 long argt,
5760 long def,
5761 int flags,
5762 int compl,
5763 char_u *compl_arg,
5764 int addr_type,
5765 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766{
5767 ucmd_T *cmd = NULL;
5768 char_u *p;
5769 int i;
5770 int cmp = 1;
5771 char_u *rep_buf = NULL;
5772 garray_T *gap;
5773
Bram Moolenaar9c102382006-05-03 21:26:49 +00005774 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 if (rep_buf == NULL)
5776 {
5777 /* Can't replace termcodes - try using the string as is */
5778 rep_buf = vim_strsave(rep);
5779
5780 /* Give up if out of memory */
5781 if (rep_buf == NULL)
5782 return FAIL;
5783 }
5784
5785 /* get address of growarray: global or in curbuf */
5786 if (flags & UC_BUFFER)
5787 {
5788 gap = &curbuf->b_ucmds;
5789 if (gap->ga_itemsize == 0)
5790 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5791 }
5792 else
5793 gap = &ucmds;
5794
5795 /* Search for the command in the already defined commands. */
5796 for (i = 0; i < gap->ga_len; ++i)
5797 {
5798 size_t len;
5799
5800 cmd = USER_CMD_GA(gap, i);
5801 len = STRLEN(cmd->uc_name);
5802 cmp = STRNCMP(name, cmd->uc_name, name_len);
5803 if (cmp == 0)
5804 {
5805 if (name_len < len)
5806 cmp = -1;
5807 else if (name_len > len)
5808 cmp = 1;
5809 }
5810
5811 if (cmp == 0)
5812 {
5813 if (!force)
5814 {
5815 EMSG(_("E174: Command already exists: add ! to replace it"));
5816 goto fail;
5817 }
5818
5819 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005820 cmd->uc_rep = NULL;
5821#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5822 vim_free(cmd->uc_compl_arg);
5823 cmd->uc_compl_arg = NULL;
5824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 break;
5826 }
5827
5828 /* Stop as soon as we pass the name to add */
5829 if (cmp < 0)
5830 break;
5831 }
5832
5833 /* Extend the array unless we're replacing an existing command */
5834 if (cmp != 0)
5835 {
5836 if (ga_grow(gap, 1) != OK)
5837 goto fail;
5838 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5839 goto fail;
5840
5841 cmd = USER_CMD_GA(gap, i);
5842 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5843
5844 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845
5846 cmd->uc_name = p;
5847 }
5848
5849 cmd->uc_rep = rep_buf;
5850 cmd->uc_argt = argt;
5851 cmd->uc_def = def;
5852 cmd->uc_compl = compl;
5853#ifdef FEAT_EVAL
5854 cmd->uc_scriptID = current_SID;
5855# ifdef FEAT_CMDL_COMPL
5856 cmd->uc_compl_arg = compl_arg;
5857# endif
5858#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005859 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860
5861 return OK;
5862
5863fail:
5864 vim_free(rep_buf);
5865#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5866 vim_free(compl_arg);
5867#endif
5868 return FAIL;
5869}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005872#if defined(FEAT_USR_CMDS)
5873static struct
5874{
5875 int expand;
5876 char *name;
5877} addr_type_complete[] =
5878{
5879 {ADDR_ARGUMENTS, "arguments"},
5880 {ADDR_LINES, "lines"},
5881 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5882 {ADDR_TABS, "tabs"},
5883 {ADDR_BUFFERS, "buffers"},
5884 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005885 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005886 {-1, NULL}
5887};
5888#endif
5889
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005890#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891/*
5892 * List of names for completion for ":command" with the EXPAND_ flag.
5893 * Must be alphabetical for completion.
5894 */
5895static struct
5896{
5897 int expand;
5898 char *name;
5899} command_complete[] =
5900{
5901 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005902 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005904 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005906 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005907#if defined(FEAT_CSCOPE)
5908 {EXPAND_CSCOPE, "cscope"},
5909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5911 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005912 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913#endif
5914 {EXPAND_DIRECTORIES, "dir"},
5915 {EXPAND_ENV_VARS, "environment"},
5916 {EXPAND_EVENTS, "event"},
5917 {EXPAND_EXPRESSION, "expression"},
5918 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005919 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005920 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 {EXPAND_FUNCTIONS, "function"},
5922 {EXPAND_HELP, "help"},
5923 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005924#if defined(FEAT_CMDHIST)
5925 {EXPAND_HISTORY, "history"},
5926#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005927#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005928 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005929 {EXPAND_LOCALES, "locale"},
5930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 {EXPAND_MAPPINGS, "mapping"},
5932 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005933 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005934 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005935#if defined(FEAT_PROFILE)
5936 {EXPAND_SYNTIME, "syntime"},
5937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005939 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005940 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005941#if defined(FEAT_SIGNS)
5942 {EXPAND_SIGN, "sign"},
5943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 {EXPAND_TAGS, "tag"},
5945 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005946 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 {EXPAND_USER_VARS, "var"},
5948 {0, NULL}
5949};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005950#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005952#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005954uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955{
5956 int i, j;
5957 int found = FALSE;
5958 ucmd_T *cmd;
5959 int len;
5960 long a;
5961 garray_T *gap;
5962
5963 gap = &curbuf->b_ucmds;
5964 for (;;)
5965 {
5966 for (i = 0; i < gap->ga_len; ++i)
5967 {
5968 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005969 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970
Bram Moolenaard29459b2016-08-26 22:29:11 +02005971 /* Skip commands which don't match the requested prefix and
5972 * commands filtered out. */
5973 if (STRNCMP(name, cmd->uc_name, name_len) != 0
5974 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 continue;
5976
5977 /* Put out the title first time */
5978 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005979 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 found = TRUE;
5981 msg_putchar('\n');
5982 if (got_int)
5983 break;
5984
5985 /* Special cases */
5986 msg_putchar(a & BANG ? '!' : ' ');
5987 msg_putchar(a & REGSTR ? '"' : ' ');
5988 msg_putchar(gap != &ucmds ? 'b' : ' ');
5989 msg_putchar(' ');
5990
5991 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5992 len = (int)STRLEN(cmd->uc_name) + 4;
5993
5994 do {
5995 msg_putchar(' ');
5996 ++len;
5997 } while (len < 16);
5998
5999 len = 0;
6000
6001 /* Arguments */
6002 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
6003 {
6004 case 0: IObuff[len++] = '0'; break;
6005 case (EXTRA): IObuff[len++] = '*'; break;
6006 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
6007 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
6008 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
6009 }
6010
6011 do {
6012 IObuff[len++] = ' ';
6013 } while (len < 5);
6014
6015 /* Range */
6016 if (a & (RANGE|COUNT))
6017 {
6018 if (a & COUNT)
6019 {
6020 /* -count=N */
6021 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6022 len += (int)STRLEN(IObuff + len);
6023 }
6024 else if (a & DFLALL)
6025 IObuff[len++] = '%';
6026 else if (cmd->uc_def >= 0)
6027 {
6028 /* -range=N */
6029 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6030 len += (int)STRLEN(IObuff + len);
6031 }
6032 else
6033 IObuff[len++] = '.';
6034 }
6035
6036 do {
6037 IObuff[len++] = ' ';
6038 } while (len < 11);
6039
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006040 /* Address Type */
6041 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6042 if (addr_type_complete[j].expand != ADDR_LINES
6043 && addr_type_complete[j].expand == cmd->uc_addr_type)
6044 {
6045 STRCPY(IObuff + len, addr_type_complete[j].name);
6046 len += (int)STRLEN(IObuff + len);
6047 break;
6048 }
6049
6050 do {
6051 IObuff[len++] = ' ';
6052 } while (len < 21);
6053
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 /* Completion */
6055 for (j = 0; command_complete[j].expand != 0; ++j)
6056 if (command_complete[j].expand == cmd->uc_compl)
6057 {
6058 STRCPY(IObuff + len, command_complete[j].name);
6059 len += (int)STRLEN(IObuff + len);
6060 break;
6061 }
6062
6063 do {
6064 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006065 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066
6067 IObuff[len] = '\0';
6068 msg_outtrans(IObuff);
6069
6070 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006071#ifdef FEAT_EVAL
6072 if (p_verbose > 0)
6073 last_set_msg(cmd->uc_scriptID);
6074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 out_flush();
6076 ui_breakcheck();
6077 if (got_int)
6078 break;
6079 }
6080 if (gap == &ucmds || i < gap->ga_len)
6081 break;
6082 gap = &ucmds;
6083 }
6084
6085 if (!found)
6086 MSG(_("No user-defined commands found"));
6087}
6088
6089 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006090uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091{
6092 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6093 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6094 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6095 0xb9, 0x7f, 0};
6096 int i;
6097
6098 for (i = 0; fcmd[i]; ++i)
6099 IObuff[i] = fcmd[i] - 0x40;
6100 IObuff[i] = 0;
6101 return IObuff;
6102}
6103
6104 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006105uc_scan_attr(
6106 char_u *attr,
6107 size_t len,
6108 long *argt,
6109 long *def,
6110 int *flags,
6111 int *compl,
6112 char_u **compl_arg,
6113 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114{
6115 char_u *p;
6116
6117 if (len == 0)
6118 {
6119 EMSG(_("E175: No attribute specified"));
6120 return FAIL;
6121 }
6122
6123 /* First, try the simple attributes (no arguments) */
6124 if (STRNICMP(attr, "bang", len) == 0)
6125 *argt |= BANG;
6126 else if (STRNICMP(attr, "buffer", len) == 0)
6127 *flags |= UC_BUFFER;
6128 else if (STRNICMP(attr, "register", len) == 0)
6129 *argt |= REGSTR;
6130 else if (STRNICMP(attr, "bar", len) == 0)
6131 *argt |= TRLBAR;
6132 else
6133 {
6134 int i;
6135 char_u *val = NULL;
6136 size_t vallen = 0;
6137 size_t attrlen = len;
6138
6139 /* Look for the attribute name - which is the part before any '=' */
6140 for (i = 0; i < (int)len; ++i)
6141 {
6142 if (attr[i] == '=')
6143 {
6144 val = &attr[i + 1];
6145 vallen = len - i - 1;
6146 attrlen = i;
6147 break;
6148 }
6149 }
6150
6151 if (STRNICMP(attr, "nargs", attrlen) == 0)
6152 {
6153 if (vallen == 1)
6154 {
6155 if (*val == '0')
6156 /* Do nothing - this is the default */;
6157 else if (*val == '1')
6158 *argt |= (EXTRA | NOSPC | NEEDARG);
6159 else if (*val == '*')
6160 *argt |= EXTRA;
6161 else if (*val == '?')
6162 *argt |= (EXTRA | NOSPC);
6163 else if (*val == '+')
6164 *argt |= (EXTRA | NEEDARG);
6165 else
6166 goto wrong_nargs;
6167 }
6168 else
6169 {
6170wrong_nargs:
6171 EMSG(_("E176: Invalid number of arguments"));
6172 return FAIL;
6173 }
6174 }
6175 else if (STRNICMP(attr, "range", attrlen) == 0)
6176 {
6177 *argt |= RANGE;
6178 if (vallen == 1 && *val == '%')
6179 *argt |= DFLALL;
6180 else if (val != NULL)
6181 {
6182 p = val;
6183 if (*def >= 0)
6184 {
6185two_count:
6186 EMSG(_("E177: Count cannot be specified twice"));
6187 return FAIL;
6188 }
6189
6190 *def = getdigits(&p);
6191 *argt |= (ZEROR | NOTADR);
6192
6193 if (p != val + vallen || vallen == 0)
6194 {
6195invalid_count:
6196 EMSG(_("E178: Invalid default value for count"));
6197 return FAIL;
6198 }
6199 }
6200 }
6201 else if (STRNICMP(attr, "count", attrlen) == 0)
6202 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006203 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204
6205 if (val != NULL)
6206 {
6207 p = val;
6208 if (*def >= 0)
6209 goto two_count;
6210
6211 *def = getdigits(&p);
6212
6213 if (p != val + vallen)
6214 goto invalid_count;
6215 }
6216
6217 if (*def < 0)
6218 *def = 0;
6219 }
6220 else if (STRNICMP(attr, "complete", attrlen) == 0)
6221 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 if (val == NULL)
6223 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006224 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 return FAIL;
6226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006228 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6229 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006232 else if (STRNICMP(attr, "addr", attrlen) == 0)
6233 {
6234 *argt |= RANGE;
6235 if (val == NULL)
6236 {
6237 EMSG(_("E179: argument required for -addr"));
6238 return FAIL;
6239 }
6240 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6241 == FAIL)
6242 return FAIL;
6243 if (addr_type_arg != ADDR_LINES)
6244 *argt |= (ZEROR | NOTADR) ;
6245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 else
6247 {
6248 char_u ch = attr[len];
6249 attr[len] = '\0';
6250 EMSG2(_("E181: Invalid attribute: %s"), attr);
6251 attr[len] = ch;
6252 return FAIL;
6253 }
6254 }
6255
6256 return OK;
6257}
6258
Bram Moolenaara850a712009-01-28 14:42:59 +00006259/*
6260 * ":command ..."
6261 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006263ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264{
6265 char_u *name;
6266 char_u *end;
6267 char_u *p;
6268 long argt = 0;
6269 long def = -1;
6270 int flags = 0;
6271 int compl = EXPAND_NOTHING;
6272 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006273 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006275 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276
6277 p = eap->arg;
6278
6279 /* Check for attributes */
6280 while (*p == '-')
6281 {
6282 ++p;
6283 end = skiptowhite(p);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006284 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 == FAIL)
6286 return;
6287 p = skipwhite(end);
6288 }
6289
6290 /* Get the name (if any) and skip to the following argument */
6291 name = p;
6292 if (ASCII_ISALPHA(*p))
6293 while (ASCII_ISALNUM(*p))
6294 ++p;
6295 if (!ends_excmd(*p) && !vim_iswhite(*p))
6296 {
6297 EMSG(_("E182: Invalid command name"));
6298 return;
6299 }
6300 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006301 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302
6303 /* If there is nothing after the name, and no attributes were specified,
6304 * we are listing commands
6305 */
6306 p = skipwhite(end);
6307 if (!has_attr && ends_excmd(*p))
6308 {
6309 uc_list(name, end - name);
6310 }
6311 else if (!ASCII_ISUPPER(*name))
6312 {
6313 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6314 return;
6315 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006316 else if ((name_len == 1 && *name == 'X')
6317 || (name_len <= 4
6318 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6319 {
6320 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6321 return;
6322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 else
6324 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006325 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326}
6327
6328/*
6329 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 * Clear all user commands, global and for current buffer.
6331 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006332 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006333ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334{
6335 uc_clear(&ucmds);
6336 uc_clear(&curbuf->b_ucmds);
6337}
6338
6339/*
6340 * Clear all user commands for "gap".
6341 */
6342 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006343uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344{
6345 int i;
6346 ucmd_T *cmd;
6347
6348 for (i = 0; i < gap->ga_len; ++i)
6349 {
6350 cmd = USER_CMD_GA(gap, i);
6351 vim_free(cmd->uc_name);
6352 vim_free(cmd->uc_rep);
6353# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6354 vim_free(cmd->uc_compl_arg);
6355# endif
6356 }
6357 ga_clear(gap);
6358}
6359
6360 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006361ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362{
6363 int i = 0;
6364 ucmd_T *cmd = NULL;
6365 int cmp = -1;
6366 garray_T *gap;
6367
6368 gap = &curbuf->b_ucmds;
6369 for (;;)
6370 {
6371 for (i = 0; i < gap->ga_len; ++i)
6372 {
6373 cmd = USER_CMD_GA(gap, i);
6374 cmp = STRCMP(eap->arg, cmd->uc_name);
6375 if (cmp <= 0)
6376 break;
6377 }
6378 if (gap == &ucmds || cmp == 0)
6379 break;
6380 gap = &ucmds;
6381 }
6382
6383 if (cmp != 0)
6384 {
6385 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6386 return;
6387 }
6388
6389 vim_free(cmd->uc_name);
6390 vim_free(cmd->uc_rep);
6391# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6392 vim_free(cmd->uc_compl_arg);
6393# endif
6394
6395 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396
6397 if (i < gap->ga_len)
6398 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6399}
6400
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006401/*
6402 * split and quote args for <f-args>
6403 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006405uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406{
6407 char_u *buf;
6408 char_u *p;
6409 char_u *q;
6410 int len;
6411
6412 /* Precalculate length */
6413 p = arg;
6414 len = 2; /* Initial and final quotes */
6415
6416 while (*p)
6417 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006418 if (p[0] == '\\' && p[1] == '\\')
6419 {
6420 len += 2;
6421 p += 2;
6422 }
6423 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 {
6425 len += 1;
6426 p += 2;
6427 }
6428 else if (*p == '\\' || *p == '"')
6429 {
6430 len += 2;
6431 p += 1;
6432 }
6433 else if (vim_iswhite(*p))
6434 {
6435 p = skipwhite(p);
6436 if (*p == NUL)
6437 break;
6438 len += 3; /* "," */
6439 }
6440 else
6441 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006442#ifdef FEAT_MBYTE
6443 int charlen = (*mb_ptr2len)(p);
6444 len += charlen;
6445 p += charlen;
6446#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 ++len;
6448 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 }
6451 }
6452
6453 buf = alloc(len + 1);
6454 if (buf == NULL)
6455 {
6456 *lenp = 0;
6457 return buf;
6458 }
6459
6460 p = arg;
6461 q = buf;
6462 *q++ = '"';
6463 while (*p)
6464 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006465 if (p[0] == '\\' && p[1] == '\\')
6466 {
6467 *q++ = '\\';
6468 *q++ = '\\';
6469 p += 2;
6470 }
6471 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 {
6473 *q++ = p[1];
6474 p += 2;
6475 }
6476 else if (*p == '\\' || *p == '"')
6477 {
6478 *q++ = '\\';
6479 *q++ = *p++;
6480 }
6481 else if (vim_iswhite(*p))
6482 {
6483 p = skipwhite(p);
6484 if (*p == NUL)
6485 break;
6486 *q++ = '"';
6487 *q++ = ',';
6488 *q++ = '"';
6489 }
6490 else
6491 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006492 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 }
6494 }
6495 *q++ = '"';
6496 *q = 0;
6497
6498 *lenp = len;
6499 return buf;
6500}
6501
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006502 static size_t
6503add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6504{
6505 size_t result;
6506
6507 result = STRLEN(mod_str);
6508 if (*multi_mods)
6509 result += 1;
6510 if (buf != NULL)
6511 {
6512 if (*multi_mods)
6513 STRCAT(buf, " ");
6514 STRCAT(buf, mod_str);
6515 }
6516
6517 *multi_mods = 1;
6518
6519 return result;
6520}
6521
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522/*
6523 * Check for a <> code in a user command.
6524 * "code" points to the '<'. "len" the length of the <> (inclusive).
6525 * "buf" is where the result is to be added.
6526 * "split_buf" points to a buffer used for splitting, caller should free it.
6527 * "split_len" is the length of what "split_buf" contains.
6528 * Returns the length of the replacement, which has been added to "buf".
6529 * Returns -1 if there was no match, and only the "<" has been copied.
6530 */
6531 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006532uc_check_code(
6533 char_u *code,
6534 size_t len,
6535 char_u *buf,
6536 ucmd_T *cmd, /* the user command we're expanding */
6537 exarg_T *eap, /* ex arguments */
6538 char_u **split_buf,
6539 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540{
6541 size_t result = 0;
6542 char_u *p = code + 1;
6543 size_t l = len - 2;
6544 int quote = 0;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006545 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_MODS,
6546 ct_REGISTER, ct_LT, ct_NONE } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547
6548 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6549 {
6550 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6551 p += 2;
6552 l -= 2;
6553 }
6554
Bram Moolenaar371d5402006-03-20 21:47:49 +00006555 ++l;
6556 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006558 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006560 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006562 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006564 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006566 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006568 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006570 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006572 else if (STRNICMP(p, "mods>", l) == 0)
6573 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574
6575 switch (type)
6576 {
6577 case ct_ARGS:
6578 /* Simple case first */
6579 if (*eap->arg == NUL)
6580 {
6581 if (quote == 1)
6582 {
6583 result = 2;
6584 if (buf != NULL)
6585 STRCPY(buf, "''");
6586 }
6587 else
6588 result = 0;
6589 break;
6590 }
6591
6592 /* When specified there is a single argument don't split it.
6593 * Works for ":Cmd %" when % is "a b c". */
6594 if ((eap->argt & NOSPC) && quote == 2)
6595 quote = 1;
6596
6597 switch (quote)
6598 {
6599 case 0: /* No quoting, no splitting */
6600 result = STRLEN(eap->arg);
6601 if (buf != NULL)
6602 STRCPY(buf, eap->arg);
6603 break;
6604 case 1: /* Quote, but don't split */
6605 result = STRLEN(eap->arg) + 2;
6606 for (p = eap->arg; *p; ++p)
6607 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006608#ifdef FEAT_MBYTE
6609 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6610 /* DBCS can contain \ in a trail byte, skip the
6611 * double-byte character. */
6612 ++p;
6613 else
6614#endif
6615 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 ++result;
6617 }
6618
6619 if (buf != NULL)
6620 {
6621 *buf++ = '"';
6622 for (p = eap->arg; *p; ++p)
6623 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006624#ifdef FEAT_MBYTE
6625 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6626 /* DBCS can contain \ in a trail byte, copy the
6627 * double-byte character to avoid escaping. */
6628 *buf++ = *p++;
6629 else
6630#endif
6631 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 *buf++ = '\\';
6633 *buf++ = *p;
6634 }
6635 *buf = '"';
6636 }
6637
6638 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006639 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 /* This is hard, so only do it once, and cache the result */
6641 if (*split_buf == NULL)
6642 *split_buf = uc_split_args(eap->arg, split_len);
6643
6644 result = *split_len;
6645 if (buf != NULL && result != 0)
6646 STRCPY(buf, *split_buf);
6647
6648 break;
6649 }
6650 break;
6651
6652 case ct_BANG:
6653 result = eap->forceit ? 1 : 0;
6654 if (quote)
6655 result += 2;
6656 if (buf != NULL)
6657 {
6658 if (quote)
6659 *buf++ = '"';
6660 if (eap->forceit)
6661 *buf++ = '!';
6662 if (quote)
6663 *buf = '"';
6664 }
6665 break;
6666
6667 case ct_LINE1:
6668 case ct_LINE2:
6669 case ct_COUNT:
6670 {
6671 char num_buf[20];
6672 long num = (type == ct_LINE1) ? eap->line1 :
6673 (type == ct_LINE2) ? eap->line2 :
6674 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6675 size_t num_len;
6676
6677 sprintf(num_buf, "%ld", num);
6678 num_len = STRLEN(num_buf);
6679 result = num_len;
6680
6681 if (quote)
6682 result += 2;
6683
6684 if (buf != NULL)
6685 {
6686 if (quote)
6687 *buf++ = '"';
6688 STRCPY(buf, num_buf);
6689 buf += num_len;
6690 if (quote)
6691 *buf = '"';
6692 }
6693
6694 break;
6695 }
6696
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006697 case ct_MODS:
6698 {
6699 int multi_mods = 0;
6700 typedef struct {
6701 int *varp;
6702 char *name;
6703 } mod_entry_T;
6704 static mod_entry_T mod_entries[] = {
6705#ifdef FEAT_BROWSE_CMD
6706 {&cmdmod.browse, "browse"},
6707#endif
6708#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6709 {&cmdmod.confirm, "confirm"},
6710#endif
6711 {&cmdmod.hide, "hide"},
6712 {&cmdmod.keepalt, "keepalt"},
6713 {&cmdmod.keepjumps, "keepjumps"},
6714 {&cmdmod.keepmarks, "keepmarks"},
6715 {&cmdmod.keeppatterns, "keeppatterns"},
6716 {&cmdmod.lockmarks, "lockmarks"},
6717 {&cmdmod.noswapfile, "noswapfile"},
6718 {NULL, NULL}
6719 };
6720 int i;
6721
6722 result = quote ? 2 : 0;
6723 if (buf != NULL)
6724 {
6725 if (quote)
6726 *buf++ = '"';
6727 *buf = '\0';
6728 }
6729
6730#ifdef FEAT_WINDOWS
6731 /* :aboveleft and :leftabove */
6732 if (cmdmod.split & WSP_ABOVE)
6733 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6734 /* :belowright and :rightbelow */
6735 if (cmdmod.split & WSP_BELOW)
6736 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6737 /* :botright */
6738 if (cmdmod.split & WSP_BOT)
6739 result += add_cmd_modifier(buf, "botright", &multi_mods);
6740#endif
6741
6742 /* the modifiers that are simple flags */
6743 for (i = 0; mod_entries[i].varp != NULL; ++i)
6744 if (*mod_entries[i].varp)
6745 result += add_cmd_modifier(buf, mod_entries[i].name,
6746 &multi_mods);
6747
6748 /* TODO: How to support :noautocmd? */
6749#ifdef HAVE_SANDBOX
6750 /* TODO: How to support :sandbox?*/
6751#endif
6752 /* :silent */
6753 if (msg_silent > 0)
6754 result += add_cmd_modifier(buf,
6755 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
6756#ifdef FEAT_WINDOWS
6757 /* :tab */
6758 if (cmdmod.tab > 0)
6759 result += add_cmd_modifier(buf, "tab", &multi_mods);
6760 /* :topleft */
6761 if (cmdmod.split & WSP_TOP)
6762 result += add_cmd_modifier(buf, "topleft", &multi_mods);
6763#endif
6764 /* TODO: How to support :unsilent?*/
6765 /* :verbose */
6766 if (p_verbose > 0)
6767 result += add_cmd_modifier(buf, "verbose", &multi_mods);
6768#ifdef FEAT_WINDOWS
6769 /* :vertical */
6770 if (cmdmod.split & WSP_VERT)
6771 result += add_cmd_modifier(buf, "vertical", &multi_mods);
6772#endif
6773 if (quote && buf != NULL)
6774 {
6775 buf += result - 2;
6776 *buf = '"';
6777 }
6778 break;
6779 }
6780
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 case ct_REGISTER:
6782 result = eap->regname ? 1 : 0;
6783 if (quote)
6784 result += 2;
6785 if (buf != NULL)
6786 {
6787 if (quote)
6788 *buf++ = '\'';
6789 if (eap->regname)
6790 *buf++ = eap->regname;
6791 if (quote)
6792 *buf = '\'';
6793 }
6794 break;
6795
6796 case ct_LT:
6797 result = 1;
6798 if (buf != NULL)
6799 *buf = '<';
6800 break;
6801
6802 default:
6803 /* Not recognized: just copy the '<' and return -1. */
6804 result = (size_t)-1;
6805 if (buf != NULL)
6806 *buf = '<';
6807 break;
6808 }
6809
6810 return result;
6811}
6812
6813 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006814do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815{
6816 char_u *buf;
6817 char_u *p;
6818 char_u *q;
6819
6820 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006821 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006822 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 size_t len, totlen;
6824
6825 size_t split_len = 0;
6826 char_u *split_buf = NULL;
6827 ucmd_T *cmd;
6828#ifdef FEAT_EVAL
6829 scid_T save_current_SID = current_SID;
6830#endif
6831
6832 if (eap->cmdidx == CMD_USER)
6833 cmd = USER_CMD(eap->useridx);
6834 else
6835 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6836
6837 /*
6838 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006839 * First round: "buf" is NULL, compute length, allocate "buf".
6840 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 */
6842 buf = NULL;
6843 for (;;)
6844 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006845 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006846 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006848
6849 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006851 start = vim_strchr(p, '<');
6852 if (start != NULL)
6853 end = vim_strchr(start + 1, '>');
6854 if (buf != NULL)
6855 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006856 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6857 ;
6858 if (*ksp == K_SPECIAL
6859 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006860 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6861# ifdef FEAT_GUI
6862 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6863# endif
6864 ))
6865 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006866 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006867 * KS_SPECIAL KE_FILLER, like for mappings, but
6868 * do_cmdline() doesn't handle that, so convert it back.
6869 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6870 len = ksp - p;
6871 if (len > 0)
6872 {
6873 mch_memmove(q, p, len);
6874 q += len;
6875 }
6876 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6877 p = ksp + 3;
6878 continue;
6879 }
6880 }
6881
6882 /* break if there no <item> is found */
6883 if (start == NULL || end == NULL)
6884 break;
6885
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 /* Include the '>' */
6887 ++end;
6888
6889 /* Take everything up to the '<' */
6890 len = start - p;
6891 if (buf == NULL)
6892 totlen += len;
6893 else
6894 {
6895 mch_memmove(q, p, len);
6896 q += len;
6897 }
6898
6899 len = uc_check_code(start, end - start, q, cmd, eap,
6900 &split_buf, &split_len);
6901 if (len == (size_t)-1)
6902 {
6903 /* no match, continue after '<' */
6904 p = start + 1;
6905 len = 1;
6906 }
6907 else
6908 p = end;
6909 if (buf == NULL)
6910 totlen += len;
6911 else
6912 q += len;
6913 }
6914 if (buf != NULL) /* second time here, finished */
6915 {
6916 STRCPY(q, p);
6917 break;
6918 }
6919
6920 totlen += STRLEN(p); /* Add on the trailing characters */
6921 buf = alloc((unsigned)(totlen + 1));
6922 if (buf == NULL)
6923 {
6924 vim_free(split_buf);
6925 return;
6926 }
6927 }
6928
6929#ifdef FEAT_EVAL
6930 current_SID = cmd->uc_scriptID;
6931#endif
6932 (void)do_cmdline(buf, eap->getline, eap->cookie,
6933 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6934#ifdef FEAT_EVAL
6935 current_SID = save_current_SID;
6936#endif
6937 vim_free(buf);
6938 vim_free(split_buf);
6939}
6940
6941# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6942 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006943get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944{
6945 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6946}
6947
6948/*
6949 * Function given to ExpandGeneric() to obtain the list of user command names.
6950 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006952get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953{
6954 if (idx < curbuf->b_ucmds.ga_len)
6955 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6956 idx -= curbuf->b_ucmds.ga_len;
6957 if (idx < ucmds.ga_len)
6958 return USER_CMD(idx)->uc_name;
6959 return NULL;
6960}
6961
6962/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006963 * Function given to ExpandGeneric() to obtain the list of user address type names.
6964 */
6965 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006966get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006967{
6968 return (char_u *)addr_type_complete[idx].name;
6969}
6970
6971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 * Function given to ExpandGeneric() to obtain the list of user command
6973 * attributes.
6974 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006976get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977{
6978 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006979 {"addr", "bang", "bar", "buffer", "complete",
6980 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006982 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 return NULL;
6984 return (char_u *)user_cmd_flags[idx];
6985}
6986
6987/*
6988 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6989 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006991get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992{
6993 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6994
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006995 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 return NULL;
6997 return (char_u *)user_cmd_nargs[idx];
6998}
6999
7000/*
7001 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7002 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007004get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005{
7006 return (char_u *)command_complete[idx].name;
7007}
7008# endif /* FEAT_CMDL_COMPL */
7009
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007010/*
7011 * Parse address type argument
7012 */
7013 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007014parse_addr_type_arg(
7015 char_u *value,
7016 int vallen,
7017 long *argt,
7018 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007019{
7020 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007021
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007022 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7023 {
7024 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7025 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7026 if (a && b)
7027 {
7028 *addr_type_arg = addr_type_complete[i].expand;
7029 break;
7030 }
7031 }
7032
7033 if (addr_type_complete[i].expand == -1)
7034 {
7035 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007036
7037 for (i = 0; err[i] != NUL && !vim_iswhite(err[i]); i++)
7038 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007039 err[i] = NUL;
7040 EMSG2(_("E180: Invalid address type value: %s"), err);
7041 return FAIL;
7042 }
7043
7044 if (*addr_type_arg != ADDR_LINES)
7045 *argt |= NOTADR;
7046
7047 return OK;
7048}
7049
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050#endif /* FEAT_USR_CMDS */
7051
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007052#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7053/*
7054 * Parse a completion argument "value[vallen]".
7055 * The detected completion goes in "*complp", argument type in "*argt".
7056 * When there is an argument, for function and user defined completion, it's
7057 * copied to allocated memory and stored in "*compl_arg".
7058 * Returns FAIL if something is wrong.
7059 */
7060 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007061parse_compl_arg(
7062 char_u *value,
7063 int vallen,
7064 int *complp,
7065 long *argt,
7066 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007067{
7068 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007069# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007070 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007071# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007072 int i;
7073 int valend = vallen;
7074
7075 /* Look for any argument part - which is the part after any ',' */
7076 for (i = 0; i < vallen; ++i)
7077 {
7078 if (value[i] == ',')
7079 {
7080 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007081# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007082 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007083# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007084 valend = i;
7085 break;
7086 }
7087 }
7088
7089 for (i = 0; command_complete[i].expand != 0; ++i)
7090 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007091 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007092 && STRNCMP(value, command_complete[i].name, valend) == 0)
7093 {
7094 *complp = command_complete[i].expand;
7095 if (command_complete[i].expand == EXPAND_BUFFERS)
7096 *argt |= BUFNAME;
7097 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7098 || command_complete[i].expand == EXPAND_FILES)
7099 *argt |= XFILE;
7100 break;
7101 }
7102 }
7103
7104 if (command_complete[i].expand == 0)
7105 {
7106 EMSG2(_("E180: Invalid complete value: %s"), value);
7107 return FAIL;
7108 }
7109
7110# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7111 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7112 && arg != NULL)
7113# else
7114 if (arg != NULL)
7115# endif
7116 {
7117 EMSG(_("E468: Completion argument only allowed for custom completion"));
7118 return FAIL;
7119 }
7120
7121# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7122 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7123 && arg == NULL)
7124 {
7125 EMSG(_("E467: Custom completion requires a function argument"));
7126 return FAIL;
7127 }
7128
7129 if (arg != NULL)
7130 *compl_arg = vim_strnsave(arg, (int)arglen);
7131# endif
7132 return OK;
7133}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007134
7135 int
7136cmdcomplete_str_to_type(char_u *complete_str)
7137{
7138 int i;
7139
7140 for (i = 0; command_complete[i].expand != 0; ++i)
7141 if (STRCMP(complete_str, command_complete[i].name) == 0)
7142 return command_complete[i].expand;
7143
7144 return EXPAND_NOTHING;
7145}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007146#endif
7147
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007149ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150{
Bram Moolenaare6850792010-05-14 15:28:44 +02007151 if (*eap->arg == NUL)
7152 {
7153#ifdef FEAT_EVAL
7154 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7155 char_u *p = NULL;
7156
7157 if (expr != NULL)
7158 {
7159 ++emsg_off;
7160 p = eval_to_string(expr, NULL, FALSE);
7161 --emsg_off;
7162 vim_free(expr);
7163 }
7164 if (p != NULL)
7165 {
7166 MSG(p);
7167 vim_free(p);
7168 }
7169 else
7170 MSG("default");
7171#else
7172 MSG(_("unknown"));
7173#endif
7174 }
7175 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007176 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177}
7178
7179 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007180ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181{
7182 if (*eap->arg == NUL && eap->cmd[2] == '!')
7183 MSG(_("Greetings, Vim user!"));
7184 do_highlight(eap->arg, eap->forceit, FALSE);
7185}
7186
7187
7188/*
7189 * Call this function if we thought we were going to exit, but we won't
7190 * (because of an error). May need to restore the terminal mode.
7191 */
7192 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007193not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194{
7195 exiting = FALSE;
7196 settmode(TMODE_RAW);
7197}
7198
7199/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007200 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 */
7202 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007203ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204{
Bram Moolenaarf240e182014-11-27 18:33:02 +01007205#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007206 win_T *wp;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007207#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007208
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209#ifdef FEAT_CMDWIN
7210 if (cmdwin_type != 0)
7211 {
7212 cmdwin_result = Ctrl_C;
7213 return;
7214 }
7215#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007216 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007217 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007218 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007219 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007220 return;
7221 }
Bram Moolenaarf240e182014-11-27 18:33:02 +01007222#ifdef FEAT_WINDOWS
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007223 if (eap->addr_count > 0)
7224 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007225 int wnr = eap->line2;
7226
7227 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7228 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007229 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007230 }
7231 else
Bram Moolenaarf240e182014-11-27 18:33:02 +01007232#endif
7233#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007234 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007235#endif
7236
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007237#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02007238 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007239 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02007240 * being closed (can only happen in autocommands). */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007241 if (curbuf_locked() || (wp->w_buffer->b_nwindows == 1
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007242 && wp->w_buffer->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007243 return;
7244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245
7246#ifdef FEAT_NETBEANS_INTG
7247 netbeansForcedQuit = eap->forceit;
7248#endif
7249
7250 /*
7251 * If there are more files or windows we won't exit.
7252 */
7253 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7254 exiting = TRUE;
7255 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007256 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7257 | (eap->forceit ? CCGD_FORCEIT : 0)
7258 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007260 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 {
7262 not_exiting();
7263 }
7264 else
7265 {
7266#ifdef FEAT_WINDOWS
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007267 /* quit last window
7268 * Note: only_one_window() returns true, even so a help window is
7269 * still open. In that case only quit, if no address has been
7270 * specified. Example:
7271 * :h|wincmd w|1q - don't quit
7272 * :h|wincmd w|q - quit
7273 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007274 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275#endif
7276 getout(0);
7277#ifdef FEAT_WINDOWS
7278# ifdef FEAT_GUI
7279 need_mouse_correct = TRUE;
7280# endif
7281 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007282 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283#endif
7284 }
7285}
7286
7287/*
7288 * ":cquit".
7289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007291ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292{
7293 getout(1); /* this does not always pass on the exit code to the Manx
7294 compiler. why? */
7295}
7296
7297/*
7298 * ":qall": try to quit all windows
7299 */
7300 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007301ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302{
7303# ifdef FEAT_CMDWIN
7304 if (cmdwin_type != 0)
7305 {
7306 if (eap->forceit)
7307 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7308 else
7309 cmdwin_result = K_XF2;
7310 return;
7311 }
7312# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007313
7314 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007315 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007316 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007317 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007318 return;
7319 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007320#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007321 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7322 /* Refuse to quit when locked or when the buffer in the last window is
7323 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007324 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007325 return;
7326#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007327
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007329 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 getout(0);
7331 not_exiting();
7332}
7333
Bram Moolenaard9967712006-03-11 21:18:15 +00007334#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335/*
7336 * ":close": close current window, unless it is the last one
7337 */
7338 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007339ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007341 win_T *win;
7342 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343# ifdef FEAT_CMDWIN
7344 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007345 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 else
7347# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007348 if (!text_locked()
7349#ifdef FEAT_AUTOCMD
7350 && !curbuf_locked()
7351#endif
7352 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007353 {
7354 if (eap->addr_count == 0)
7355 ex_win_close(eap->forceit, curwin, NULL);
7356 else {
Bram Moolenaar29323592016-07-24 22:04:11 +02007357 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007358 {
7359 winnr++;
7360 if (winnr == eap->line2)
7361 break;
7362 }
7363 if (win == NULL)
7364 win = lastwin;
7365 ex_win_close(eap->forceit, win, NULL);
7366 }
7367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368}
7369
Bram Moolenaard9967712006-03-11 21:18:15 +00007370# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007371/*
7372 * ":pclose": Close any preview window.
7373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007375ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007376{
7377 win_T *win;
7378
Bram Moolenaar29323592016-07-24 22:04:11 +02007379 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007380 if (win->w_p_pvw)
7381 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007382 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007383 break;
7384 }
7385}
Bram Moolenaard9967712006-03-11 21:18:15 +00007386# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007387
Bram Moolenaarf740b292006-02-16 22:11:02 +00007388/*
7389 * Close window "win" and take care of handling closing the last window for a
7390 * modified buffer.
7391 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007392 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007393ex_win_close(
7394 int forceit,
7395 win_T *win,
7396 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397{
7398 int need_hide;
7399 buf_T *buf = win->w_buffer;
7400
7401 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007402 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007404# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 if ((p_confirm || cmdmod.confirm) && p_write)
7406 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007407 bufref_T bufref;
7408
7409 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007411 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 return;
7413 need_hide = FALSE;
7414 }
7415 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007416# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 {
7418 EMSG(_(e_nowrtmsg));
7419 return;
7420 }
7421 }
7422
Bram Moolenaard9967712006-03-11 21:18:15 +00007423# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007425# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007426
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007428 if (tp == NULL)
7429 win_close(win, !need_hide && !P_HID(buf));
7430 else
7431 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432}
7433
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007435 * ":tabclose": close current tab page, unless it is the last one.
7436 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 */
7438 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007439ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007441 tabpage_T *tp;
7442
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007443# ifdef FEAT_CMDWIN
7444 if (cmdwin_type != 0)
7445 cmdwin_result = K_IGNORE;
7446 else
7447# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007448 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007449 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007450 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007452 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007453 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007454 tp = find_tabpage((int)eap->line2);
7455 if (tp == NULL)
7456 {
7457 beep_flush();
7458 return;
7459 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007460 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007461 {
7462 tabpage_close_other(tp, eap->forceit);
7463 return;
7464 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007465 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007466 if (!text_locked()
7467#ifdef FEAT_AUTOCMD
7468 && !curbuf_locked()
7469#endif
7470 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00007471 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007473}
7474
7475/*
7476 * ":tabonly": close all tab pages except the current one
7477 */
7478 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007479ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007480{
7481 tabpage_T *tp;
7482 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007483
7484# ifdef FEAT_CMDWIN
7485 if (cmdwin_type != 0)
7486 cmdwin_result = K_IGNORE;
7487 else
7488# endif
7489 if (first_tabpage->tp_next == NULL)
7490 MSG(_("Already only one tab page"));
7491 else
7492 {
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007493 if (eap->addr_count > 0)
7494 goto_tabpage(eap->line2);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007495 /* Repeat this up to a 1000 times, because autocommands may mess
7496 * up the lists. */
7497 for (done = 0; done < 1000; ++done)
7498 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007499 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007500 if (tp->tp_topframe != topframe)
7501 {
7502 tabpage_close_other(tp, eap->forceit);
7503 /* if we failed to close it quit */
7504 if (valid_tabpage(tp))
7505 done = 1000;
7506 /* start over, "tp" is now invalid */
7507 break;
7508 }
7509 if (first_tabpage->tp_next == NULL)
7510 break;
7511 }
7512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514
7515/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007516 * Close the current tab page.
7517 */
7518 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007519tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007520{
7521 /* First close all the windows but the current one. If that worked then
7522 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007523 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007524 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007525 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007526 ex_win_close(forceit, curwin, NULL);
7527# ifdef FEAT_GUI
7528 need_mouse_correct = TRUE;
7529# endif
7530}
7531
7532/*
7533 * Close tab page "tp", which is not the current tab page.
7534 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007535 * Also takes care of the tab pages line disappearing when closing the
7536 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007537 */
7538 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007539tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007540{
7541 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007542 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007543 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007544
7545 /* Limit to 1000 windows, autocommands may add a window while we close
7546 * one. OK, so I'm paranoid... */
7547 while (++done < 1000)
7548 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007549 wp = tp->tp_firstwin;
7550 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007551
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007552 /* Autocommands may delete the tab page under our fingers and we may
7553 * fail to close a window with a modified buffer. */
7554 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007555 break;
7556 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007557
Bram Moolenaar29323592016-07-24 22:04:11 +02007558#ifdef FEAT_AUTOCMD
7559 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
7560#endif
7561
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007562 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007563 if (h != tabline_height())
7564 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007565}
7566
7567/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 * ":only".
7569 */
7570 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007571ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007573 win_T *wp;
7574 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575# ifdef FEAT_GUI
7576 need_mouse_correct = TRUE;
7577# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007578 if (eap->addr_count > 0)
7579 {
7580 wnr = eap->line2;
7581 for (wp = firstwin; --wnr > 0; )
7582 {
7583 if (wp->w_next == NULL)
7584 break;
7585 else
7586 wp = wp->w_next;
7587 }
7588 win_goto(wp);
7589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 close_others(TRUE, eap->forceit);
7591}
7592
7593/*
7594 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007595 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007597 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007598ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599{
7600 if (eap->addr_count == 0)
7601 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007602 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603}
7604#endif /* FEAT_WINDOWS */
7605
7606 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007607ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007609 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610#ifdef FEAT_WINDOWS
Bram Moolenaar2256c992016-11-15 21:17:07 +01007611 if (!eap->skip)
7612 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613# ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007614 need_mouse_correct = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615# endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007616 if (eap->addr_count == 0)
7617 win_close(curwin, FALSE); /* don't free buffer */
7618 else
7619 {
7620 int winnr = 0;
7621 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007622
Bram Moolenaar2256c992016-11-15 21:17:07 +01007623 FOR_ALL_WINDOWS(win)
7624 {
7625 winnr++;
7626 if (winnr == eap->line2)
7627 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007628 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007629 if (win == NULL)
7630 win = lastwin;
7631 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635}
7636
7637/*
7638 * ":stop" and ":suspend": Suspend Vim.
7639 */
7640 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007641ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642{
7643 /*
7644 * Disallow suspending for "rvim".
7645 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007646 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 {
7648 if (!eap->forceit)
7649 autowrite_all();
7650 windgoto((int)Rows - 1, 0);
7651 out_char('\n');
7652 out_flush();
7653 stoptermcap();
7654 out_flush(); /* needed for SUN to restore xterm buffer */
7655#ifdef FEAT_TITLE
7656 mch_restore_title(3); /* restore window titles */
7657#endif
7658 ui_suspend(); /* call machine specific function */
7659#ifdef FEAT_TITLE
7660 maketitle();
7661 resettitle(); /* force updating the title */
7662#endif
7663 starttermcap();
7664 scroll_start(); /* scroll screen before redrawing */
7665 redraw_later_clear();
7666 shell_resized(); /* may have resized window */
7667 }
7668}
7669
7670/*
7671 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7672 */
7673 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007674ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675{
7676#ifdef FEAT_CMDWIN
7677 if (cmdwin_type != 0)
7678 {
7679 cmdwin_result = Ctrl_C;
7680 return;
7681 }
7682#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007683 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007684 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007685 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007686 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007687 return;
7688 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007689#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007690 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7691 /* Refuse to quit when locked or when the buffer in the last window is
7692 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007693 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007694 return;
7695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696
7697 /*
7698 * if more files or windows we won't exit
7699 */
7700 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7701 exiting = TRUE;
7702 if ( ((eap->cmdidx == CMD_wq
7703 || curbufIsChanged())
7704 && do_write(eap) == FAIL)
7705 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007706 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 {
7708 not_exiting();
7709 }
7710 else
7711 {
7712#ifdef FEAT_WINDOWS
7713 if (only_one_window()) /* quit last window, exit Vim */
7714#endif
7715 getout(0);
7716#ifdef FEAT_WINDOWS
7717# ifdef FEAT_GUI
7718 need_mouse_correct = TRUE;
7719# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007720 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 win_close(curwin, !P_HID(curwin->w_buffer));
7722#endif
7723 }
7724}
7725
7726/*
7727 * ":print", ":list", ":number".
7728 */
7729 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007730ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007732 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7733 EMSG(_(e_emptybuf));
7734 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007736 for ( ;!got_int; ui_breakcheck())
7737 {
7738 print_line(eap->line1,
7739 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7740 || (eap->flags & EXFLAG_NR)),
7741 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7742 if (++eap->line1 > eap->line2)
7743 break;
7744 out_flush(); /* show one line at a time */
7745 }
7746 setpcmark();
7747 /* put cursor at last line */
7748 curwin->w_cursor.lnum = eap->line2;
7749 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 }
7751
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753}
7754
7755#ifdef FEAT_BYTEOFF
7756 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007757ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758{
7759 goto_byte(eap->line2);
7760}
7761#endif
7762
7763/*
7764 * ":shell".
7765 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007767ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768{
7769 do_shell(NULL, 0);
7770}
7771
7772#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7773 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007774 || defined(FEAT_GUI_MSWIN) \
7775 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 || defined(PROTO)
7777
7778/*
7779 * Handle a file drop. The code is here because a drop is *nearly* like an
7780 * :args command, but not quite (we have a list of exact filenames, so we
7781 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7782 * code is very similar to :args and hence needs access to a lot of the static
7783 * functions in this file.
7784 *
7785 * The list should be allocated using alloc(), as should each item in the
7786 * list. This function takes over responsibility for freeing the list.
7787 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007788 * XXX The list is made into the argument list. This is freed using
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007789 * FreeWild(), which does a series of vim_free() calls.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 */
7791 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007792handle_drop(
7793 int filec, /* the number of files dropped */
7794 char_u **filev, /* the list of files dropped */
7795 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796{
7797 exarg_T ea;
7798 int save_msg_scroll = msg_scroll;
7799
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007800 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007801 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007803#ifdef FEAT_AUTOCMD
7804 if (curbuf_locked())
7805 return;
7806#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007807 /* When the screen is being updated we should not change buffers and
7808 * windows structures, it may cause freed memory to be used. */
7809 if (updating_screen)
7810 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811
7812 /* Check whether the current buffer is changed. If so, we will need
7813 * to split the current window or data could be lost.
7814 * We don't need to check if the 'hidden' option is set, as in this
7815 * case the buffer won't be lost.
7816 */
7817 if (!P_HID(curbuf) && !split)
7818 {
7819 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007820 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 --emsg_off;
7822 }
7823 if (split)
7824 {
7825# ifdef FEAT_WINDOWS
7826 if (win_split(0, 0) == FAIL)
7827 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007828 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829
7830 /* When splitting the window, create a new alist. Otherwise the
7831 * existing one is overwritten. */
7832 alist_unlink(curwin->w_alist);
7833 alist_new();
7834# else
7835 return; /* can't split, always fail */
7836# endif
7837 }
7838
7839 /*
7840 * Set up the new argument list.
7841 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007842 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843
7844 /*
7845 * Move to the first file.
7846 */
7847 /* Fake up a minimal "next" command for do_argfile() */
7848 vim_memset(&ea, 0, sizeof(ea));
7849 ea.cmd = (char_u *)"next";
7850 do_argfile(&ea, 0);
7851
7852 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7853 * mode that is not needed here. */
7854 need_start_insertmode = FALSE;
7855
7856 /* Restore msg_scroll, otherwise a following command may cause scrolling
7857 * unexpectedly. The screen will be redrawn by the caller, thus
7858 * msg_scroll being set by displaying a message is irrelevant. */
7859 msg_scroll = save_msg_scroll;
7860}
7861#endif
7862
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863/*
7864 * Clear an argument list: free all file names and reset it to zero entries.
7865 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007866 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007867alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868{
7869 while (--al->al_ga.ga_len >= 0)
7870 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7871 ga_clear(&al->al_ga);
7872}
7873
7874/*
7875 * Init an argument list.
7876 */
7877 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007878alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879{
7880 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7881}
7882
7883#if defined(FEAT_WINDOWS) || defined(PROTO)
7884
7885/*
7886 * Remove a reference from an argument list.
7887 * Ignored when the argument list is the global one.
7888 * If the argument list is no longer used by any window, free it.
7889 */
7890 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007891alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892{
7893 if (al != &global_alist && --al->al_refcount <= 0)
7894 {
7895 alist_clear(al);
7896 vim_free(al);
7897 }
7898}
7899
7900# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7901/*
7902 * Create a new argument list and use it for the current window.
7903 */
7904 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007905alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906{
7907 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7908 if (curwin->w_alist == NULL)
7909 {
7910 curwin->w_alist = &global_alist;
7911 ++global_alist.al_refcount;
7912 }
7913 else
7914 {
7915 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007916 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 alist_init(curwin->w_alist);
7918 }
7919}
7920# endif
7921#endif
7922
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007923#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924/*
7925 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007926 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7927 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 */
7929 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007930alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931{
7932 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007933 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 char_u **new_arg_files;
7935 int new_arg_file_count;
7936 char_u *save_p_su = p_su;
7937 int i;
7938
7939 /* Don't use 'suffixes' here. This should work like the shell did the
7940 * expansion. Also, the vimrc file isn't read yet, thus the user
7941 * can't set the options. */
7942 p_su = empty_option;
7943 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7944 if (old_arg_files != NULL)
7945 {
7946 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007947 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7948 old_arg_count = GARGCOUNT;
7949 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007951 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 && new_arg_file_count > 0)
7953 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007954 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7955 TRUE, fnum_list, fnum_len);
7956 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 }
7959 p_su = save_p_su;
7960}
7961#endif
7962
7963/*
7964 * Set the argument list for the current window.
7965 * Takes over the allocated files[] and the allocated fnames in it.
7966 */
7967 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007968alist_set(
7969 alist_T *al,
7970 int count,
7971 char_u **files,
7972 int use_curbuf,
7973 int *fnum_list,
7974 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975{
7976 int i;
7977
7978 alist_clear(al);
7979 if (ga_grow(&al->al_ga, count) == OK)
7980 {
7981 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007982 {
7983 if (got_int)
7984 {
7985 /* When adding many buffers this can take a long time. Allow
7986 * interrupting here. */
7987 while (i < count)
7988 vim_free(files[i++]);
7989 break;
7990 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007991
7992 /* May set buffer name of a buffer previously used for the
7993 * argument list, so that it's re-used by alist_add. */
7994 if (fnum_list != NULL && i < fnum_len)
7995 buf_set_name(fnum_list[i], files[i]);
7996
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007998 ui_breakcheck();
7999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 vim_free(files);
8001 }
8002 else
8003 FreeWild(count, files);
8004#ifdef FEAT_WINDOWS
8005 if (al == &global_alist)
8006#endif
8007 arg_had_last = FALSE;
8008}
8009
8010/*
8011 * Add file "fname" to argument list "al".
8012 * "fname" must have been allocated and "al" must have been checked for room.
8013 */
8014 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008015alist_add(
8016 alist_T *al,
8017 char_u *fname,
8018 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019{
8020 if (fname == NULL) /* don't add NULL file names */
8021 return;
8022#ifdef BACKSLASH_IN_FILENAME
8023 slash_adjust(fname);
8024#endif
8025 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8026 if (set_fnum > 0)
8027 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8028 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8029 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030}
8031
8032#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8033/*
8034 * Adjust slashes in file names. Called after 'shellslash' was set.
8035 */
8036 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008037alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038{
8039 int i;
8040# ifdef FEAT_WINDOWS
8041 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008042 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043# endif
8044
8045 for (i = 0; i < GARGCOUNT; ++i)
8046 if (GARGLIST[i].ae_fname != NULL)
8047 slash_adjust(GARGLIST[i].ae_fname);
8048# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00008049 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 if (wp->w_alist != &global_alist)
8051 for (i = 0; i < WARGCOUNT(wp); ++i)
8052 if (WARGLIST(wp)[i].ae_fname != NULL)
8053 slash_adjust(WARGLIST(wp)[i].ae_fname);
8054# endif
8055}
8056#endif
8057
8058/*
8059 * ":preserve".
8060 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008062ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008064 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 ml_preserve(curbuf, TRUE);
8066}
8067
8068/*
8069 * ":recover".
8070 */
8071 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008072ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073{
8074 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8075 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008076 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8077 | CCGD_MULTWIN
8078 | (eap->forceit ? CCGD_FORCEIT : 0)
8079 | CCGD_EXCMD)
8080
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 && (*eap->arg == NUL
8082 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8083 ml_recover();
8084 recoverymode = FALSE;
8085}
8086
8087/*
8088 * Command modifier used in a wrong way.
8089 */
8090 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008091ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092{
8093 eap->errmsg = e_invcmd;
8094}
8095
8096#ifdef FEAT_WINDOWS
8097/*
8098 * :sview [+command] file split window with new file, read-only
8099 * :split [[+command] file] split window with current or new file
8100 * :vsplit [[+command] file] split window vertically with current or new file
8101 * :new [[+command] file] split window with no or new file
8102 * :vnew [[+command] file] split vertically window with no or new file
8103 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008104 *
8105 * :tabedit open new Tab page with empty window
8106 * :tabedit [+command] file open new Tab page and edit "file"
8107 * :tabnew [[+command] file] just like :tabedit
8108 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 */
8110 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008111ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008113 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008114# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008116# endif
8117# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008119# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008121# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008123# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008125# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008127 * quickfix windows. But it's OK when doing ":tab split". */
8128 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 {
8130 if (eap->cmdidx == CMD_split)
8131 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 if (eap->cmdidx == CMD_vsplit)
8133 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008135# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008137# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008138 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 {
8140 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8141 FNAME_MESS, TRUE, curbuf->b_ffname);
8142 if (fname == NULL)
8143 goto theend;
8144 eap->arg = fname;
8145 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008146# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008148# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008150# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 && eap->cmdidx != CMD_new)
8154 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008155# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008156 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008157# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008158 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008159# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008160 au_has_group((char_u *)"FileExplorer"))
8161 {
8162 /* No browsing supported but we do have the file explorer:
8163 * Edit the directory. */
8164 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8165 eap->arg = (char_u *)".";
8166 }
8167 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008168# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008169 {
8170 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008172 if (fname == NULL)
8173 goto theend;
8174 eap->arg = fname;
8175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 }
8177 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008178# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008180 /*
8181 * Either open new tab page or split the window.
8182 */
8183 if (eap->cmdidx == CMD_tabedit
8184 || eap->cmdidx == CMD_tabfind
8185 || eap->cmdidx == CMD_tabnew)
8186 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008187 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8188 : eap->addr_count == 0 ? 0
8189 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008190 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008191 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008192
8193 /* set the alternate buffer for the window we came from */
8194 if (curwin != old_curwin
8195 && win_valid(old_curwin)
8196 && old_curwin->w_buffer != curbuf
8197 && !cmdmod.keepalt)
8198 old_curwin->w_alt_fnum = curbuf->b_fnum;
8199 }
8200 }
8201 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8203 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008204# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 /* Reset 'scrollbind' when editing another file, but keep it when
8206 * doing ":split" without arguments. */
8207 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008208# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008210# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008212 {
8213 RESET_BINDING(curwin);
8214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 else
8216 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008217# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 do_exedit(eap, old_curwin);
8219 }
8220
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008221# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008223# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008225# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226theend:
8227 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008228# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008230
8231/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008232 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008233 */
8234 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008235tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008236{
8237 exarg_T ea;
8238
8239 vim_memset(&ea, 0, sizeof(ea));
8240 ea.cmdidx = CMD_tabnew;
8241 ea.cmd = (char_u *)"tabn";
8242 ea.arg = (char_u *)"";
8243 ex_splitview(&ea);
8244}
8245
8246/*
8247 * :tabnext command
8248 */
8249 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008250ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008251{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008252 switch (eap->cmdidx)
8253 {
8254 case CMD_tabfirst:
8255 case CMD_tabrewind:
8256 goto_tabpage(1);
8257 break;
8258 case CMD_tablast:
8259 goto_tabpage(9999);
8260 break;
8261 case CMD_tabprevious:
8262 case CMD_tabNext:
8263 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
8264 break;
8265 default: /* CMD_tabnext */
8266 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
8267 break;
8268 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008269}
8270
8271/*
8272 * :tabmove command
8273 */
8274 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008275ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008276{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008277 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008278
8279 if (eap->arg && *eap->arg != NUL)
8280 {
8281 char_u *p = eap->arg;
8282 int relative = 0; /* argument +N/-N means: move N places to the
8283 * right/left relative to the current position. */
8284
8285 if (*eap->arg == '-')
8286 {
8287 relative = -1;
8288 p = eap->arg + 1;
8289 }
8290 else if (*eap->arg == '+')
8291 {
8292 relative = 1;
8293 p = eap->arg + 1;
8294 }
8295 else
8296 p = eap->arg;
8297
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008298 if (relative == 0)
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008299 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008300 if (STRCMP(p, "$") == 0)
8301 tab_number = LAST_TAB_NR;
8302 else if (p == skipdigits(p))
8303 {
8304 /* No numbers as argument. */
8305 eap->errmsg = e_invarg;
8306 return;
8307 }
8308 else
8309 tab_number = getdigits(&p);
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008310 }
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008311 else
8312 {
8313 if (*p != NUL)
8314 tab_number = getdigits(&p);
8315 else
8316 tab_number = 1;
8317 tab_number = tab_number * relative + tabpage_index(curtab);
8318 if (relative == -1)
8319 --tab_number;
8320 }
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008321 }
8322 else if (eap->addr_count != 0)
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008323 {
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008324 tab_number = eap->line2;
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008325 if (**eap->cmdlinep == '-')
8326 --tab_number;
8327 }
8328 else
8329 tab_number = LAST_TAB_NR;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008330
8331 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008332}
8333
8334/*
8335 * :tabs command: List tabs and their contents.
8336 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008337 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008338ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008339{
8340 tabpage_T *tp;
8341 win_T *wp;
8342 int tabcount = 1;
8343
8344 msg_start();
8345 msg_scroll = TRUE;
8346 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8347 {
8348 msg_putchar('\n');
8349 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
8350 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
8351 out_flush(); /* output one line at a time */
8352 ui_breakcheck();
8353
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008354 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008355 wp = firstwin;
8356 else
8357 wp = tp->tp_firstwin;
8358 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8359 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008360 msg_putchar('\n');
8361 msg_putchar(wp == curwin ? '>' : ' ');
8362 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008363 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8364 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008365 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008366 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008367 else
8368 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8369 IObuff, IOSIZE, TRUE);
8370 msg_outtrans(IObuff);
8371 out_flush(); /* output one line at a time */
8372 ui_breakcheck();
8373 }
8374 }
8375}
8376
8377#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378
8379/*
8380 * ":mode": Set screen mode.
8381 * If no argument given, just get the screen size and redraw.
8382 */
8383 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008384ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385{
8386 if (*eap->arg == NUL)
8387 shell_resized();
8388 else
8389 mch_screenmode(eap->arg);
8390}
8391
8392#ifdef FEAT_WINDOWS
8393/*
8394 * ":resize".
8395 * set, increment or decrement current window height
8396 */
8397 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008398ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399{
8400 int n;
8401 win_T *wp = curwin;
8402
8403 if (eap->addr_count > 0)
8404 {
8405 n = eap->line2;
8406 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8407 ;
8408 }
8409
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008410# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008412# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 if (cmdmod.split & WSP_VERT)
8415 {
8416 if (*eap->arg == '-' || *eap->arg == '+')
8417 n += W_WIDTH(curwin);
8418 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8419 n = 9999;
8420 win_setwidth_win((int)n, wp);
8421 }
8422 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 {
8424 if (*eap->arg == '-' || *eap->arg == '+')
8425 n += curwin->w_height;
8426 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8427 n = 9999;
8428 win_setheight_win((int)n, wp);
8429 }
8430}
8431#endif
8432
8433/*
8434 * ":find [+command] <file>" command.
8435 */
8436 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008437ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438{
8439#ifdef FEAT_SEARCHPATH
8440 char_u *fname;
8441 int count;
8442
8443 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8444 TRUE, curbuf->b_ffname);
8445 if (eap->addr_count > 0)
8446 {
8447 /* Repeat finding the file "count" times. This matters when it
8448 * appears several times in the path. */
8449 count = eap->line2;
8450 while (fname != NULL && --count > 0)
8451 {
8452 vim_free(fname);
8453 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8454 FALSE, curbuf->b_ffname);
8455 }
8456 }
8457
8458 if (fname != NULL)
8459 {
8460 eap->arg = fname;
8461#endif
8462 do_exedit(eap, NULL);
8463#ifdef FEAT_SEARCHPATH
8464 vim_free(fname);
8465 }
8466#endif
8467}
8468
8469/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008470 * ":open" simulation: for now just work like ":visual".
8471 */
8472 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008473ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008474{
8475 regmatch_T regmatch;
8476 char_u *p;
8477
8478 curwin->w_cursor.lnum = eap->line2;
8479 beginline(BL_SOL | BL_FIX);
8480 if (*eap->arg == '/')
8481 {
8482 /* ":open /pattern/": put cursor in column found with pattern */
8483 ++eap->arg;
8484 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8485 *p = NUL;
8486 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8487 if (regmatch.regprog != NULL)
8488 {
8489 regmatch.rm_ic = p_ic;
8490 p = ml_get_curline();
8491 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008492 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008493 else
8494 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008495 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008496 }
8497 /* Move to the NUL, ignore any other arguments. */
8498 eap->arg += STRLEN(eap->arg);
8499 }
8500 check_cursor();
8501
8502 eap->cmdidx = CMD_visual;
8503 do_exedit(eap, NULL);
8504}
8505
8506/*
8507 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 */
8509 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008510ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511{
8512 do_exedit(eap, NULL);
8513}
8514
8515/*
8516 * ":edit <file>" command and alikes.
8517 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008519do_exedit(
8520 exarg_T *eap,
8521 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522{
8523 int n;
8524#ifdef FEAT_WINDOWS
8525 int need_hide;
8526#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008527 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528
8529 /*
8530 * ":vi" command ends Ex mode.
8531 */
8532 if (exmode_active && (eap->cmdidx == CMD_visual
8533 || eap->cmdidx == CMD_view))
8534 {
8535 exmode_active = FALSE;
8536 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008537 {
8538 /* Special case: ":global/pat/visual\NLvi-commands" */
8539 if (global_busy)
8540 {
8541 int rd = RedrawingDisabled;
8542 int nwr = no_wait_return;
8543 int ms = msg_scroll;
8544#ifdef FEAT_GUI
8545 int he = hold_gui_events;
8546#endif
8547
8548 if (eap->nextcmd != NULL)
8549 {
8550 stuffReadbuff(eap->nextcmd);
8551 eap->nextcmd = NULL;
8552 }
8553
8554 if (exmode_was != EXMODE_VIM)
8555 settmode(TMODE_RAW);
8556 RedrawingDisabled = 0;
8557 no_wait_return = 0;
8558 need_wait_return = FALSE;
8559 msg_scroll = 0;
8560#ifdef FEAT_GUI
8561 hold_gui_events = 0;
8562#endif
8563 must_redraw = CLEAR;
8564
8565 main_loop(FALSE, TRUE);
8566
8567 RedrawingDisabled = rd;
8568 no_wait_return = nwr;
8569 msg_scroll = ms;
8570#ifdef FEAT_GUI
8571 hold_gui_events = he;
8572#endif
8573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 }
8577
8578 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008579 || eap->cmdidx == CMD_tabnew
8580 || eap->cmdidx == CMD_tabedit
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008581#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 || eap->cmdidx == CMD_vnew
8583#endif
8584 ) && *eap->arg == NUL)
8585 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008586 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 setpcmark();
8588 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008589 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8590 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 }
8592 else if ((eap->cmdidx != CMD_split
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008593#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 && eap->cmdidx != CMD_vsplit
8595#endif
8596 )
8597 || *eap->arg != NUL
8598#ifdef FEAT_BROWSE
8599 || cmdmod.browse
8600#endif
8601 )
8602 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008603#ifdef FEAT_AUTOCMD
8604 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8605 * can bring us here, others are stopped earlier. */
8606 if (*eap->arg != NUL && curbuf_locked())
8607 return;
8608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 n = readonlymode;
8610 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8611 readonlymode = TRUE;
8612 else if (eap->cmdidx == CMD_enew)
8613 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8614 empty buffer */
8615 setpcmark();
8616 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8617 NULL, eap,
8618 /* ":edit" goes to first line if Vi compatible */
8619 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8620 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8621 ? ECMD_ONE : eap->do_ecmd_lnum,
8622 (P_HID(curbuf) ? ECMD_HIDE : 0)
8623 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008624 /* after a split we can use an existing buffer */
8625 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626#ifdef FEAT_LISTCMDS
8627 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8628#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008629 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 {
8631 /* Editing the file failed. If the window was split, close it. */
8632#ifdef FEAT_WINDOWS
8633 if (old_curwin != NULL)
8634 {
8635 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8636 if (!need_hide || P_HID(curbuf))
8637 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008638# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8639 cleanup_T cs;
8640
8641 /* Reset the error/interrupt/exception state here so that
8642 * aborting() returns FALSE when closing a window. */
8643 enter_cleanup(&cs);
8644# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645# ifdef FEAT_GUI
8646 need_mouse_correct = TRUE;
8647# endif
8648 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008649
8650# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8651 /* Restore the error/interrupt/exception state if not
8652 * discarded by a new aborting error, interrupt, or
8653 * uncaught exception. */
8654 leave_cleanup(&cs);
8655# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 }
8657 }
8658#endif
8659 }
8660 else if (readonlymode && curbuf->b_nwindows == 1)
8661 {
8662 /* When editing an already visited buffer, 'readonly' won't be set
8663 * but the previous value is kept. With ":view" and ":sview" we
8664 * want the file to be readonly, except when another window is
8665 * editing the same buffer. */
8666 curbuf->b_p_ro = TRUE;
8667 }
8668 readonlymode = n;
8669 }
8670 else
8671 {
8672 if (eap->do_ecmd_cmd != NULL)
8673 do_cmdline_cmd(eap->do_ecmd_cmd);
8674#ifdef FEAT_TITLE
8675 n = curwin->w_arg_idx_invalid;
8676#endif
8677 check_arg_idx(curwin);
8678#ifdef FEAT_TITLE
8679 if (n != curwin->w_arg_idx_invalid)
8680 maketitle();
8681#endif
8682 }
8683
8684#ifdef FEAT_WINDOWS
8685 /*
8686 * if ":split file" worked, set alternate file name in old window to new
8687 * file
8688 */
8689 if (old_curwin != NULL
8690 && *eap->arg != NUL
8691 && curwin != old_curwin
8692 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008693 && old_curwin->w_buffer != curbuf
8694 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 old_curwin->w_alt_fnum = curbuf->b_fnum;
8696#endif
8697
8698 ex_no_reprint = TRUE;
8699}
8700
8701#ifndef FEAT_GUI
8702/*
8703 * ":gui" and ":gvim" when there is no GUI.
8704 */
8705 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008706ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707{
8708 eap->errmsg = e_nogvim;
8709}
8710#endif
8711
8712#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8713 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008714ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715{
8716 gui_make_tearoff(eap->arg);
8717}
8718#endif
8719
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008720#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008722ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008724 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725}
8726#endif
8727
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008729ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730{
8731 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8732 MSG(_("No swap file"));
8733 else
8734 msg(curbuf->b_ml.ml_mfp->mf_fname);
8735}
8736
8737/*
8738 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8739 * offset.
8740 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8741 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008743ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744{
8745#ifdef FEAT_SCROLLBIND
8746 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008747 win_T *save_curwin = curwin;
8748 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 long topline;
8750 long y;
8751 linenr_T old_linenr = curwin->w_cursor.lnum;
8752
8753 setpcmark();
8754
8755 /*
8756 * determine max topline
8757 */
8758 if (curwin->w_p_scb)
8759 {
8760 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008761 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 {
8763 if (wp->w_p_scb && wp->w_buffer)
8764 {
8765 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8766 if (topline > y)
8767 topline = y;
8768 }
8769 }
8770 if (topline < 1)
8771 topline = 1;
8772 }
8773 else
8774 {
8775 topline = 1;
8776 }
8777
8778
8779 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008780 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008782 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 {
8784 if (curwin->w_p_scb)
8785 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008786 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 y = topline - curwin->w_topline;
8788 if (y > 0)
8789 scrollup(y, TRUE);
8790 else
8791 scrolldown(-y, TRUE);
8792 curwin->w_scbind_pos = topline;
8793 redraw_later(VALID);
8794 cursor_correct();
8795#ifdef FEAT_WINDOWS
8796 curwin->w_redr_status = TRUE;
8797#endif
8798 }
8799 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008800 curwin = save_curwin;
8801 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 if (curwin->w_p_scb)
8803 {
8804 did_syncbind = TRUE;
8805 checkpcmark();
8806 if (old_linenr != curwin->w_cursor.lnum)
8807 {
8808 char_u ctrl_o[2];
8809
8810 ctrl_o[0] = Ctrl_O;
8811 ctrl_o[1] = 0;
8812 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8813 }
8814 }
8815#endif
8816}
8817
8818
8819 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008820ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008822 int i;
8823 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8824 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825
8826 if (eap->usefilter) /* :r!cmd */
8827 do_bang(1, eap, FALSE, FALSE, TRUE);
8828 else
8829 {
8830 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8831 return;
8832
8833#ifdef FEAT_BROWSE
8834 if (cmdmod.browse)
8835 {
8836 char_u *browseFile;
8837
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008838 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 NULL, NULL, NULL, curbuf);
8840 if (browseFile != NULL)
8841 {
8842 i = readfile(browseFile, NULL,
8843 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8844 vim_free(browseFile);
8845 }
8846 else
8847 i = OK;
8848 }
8849 else
8850#endif
8851 if (*eap->arg == NUL)
8852 {
8853 if (check_fname() == FAIL) /* check for no file name */
8854 return;
8855 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8856 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8857 }
8858 else
8859 {
8860 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8861 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8862 i = readfile(eap->arg, NULL,
8863 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8864
8865 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01008866 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 {
8868#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8869 if (!aborting())
8870#endif
8871 EMSG2(_(e_notopen), eap->arg);
8872 }
8873 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008874 {
8875 if (empty && exmode_active)
8876 {
8877 /* Delete the empty line that remains. Historically ex does
8878 * this but vi doesn't. */
8879 if (eap->line2 == 0)
8880 lnum = curbuf->b_ml.ml_line_count;
8881 else
8882 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008883 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008884 {
8885 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008886 if (curwin->w_cursor.lnum > 1
8887 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008888 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008889 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008890 }
8891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 }
8895}
8896
Bram Moolenaarea408852005-06-25 22:49:46 +00008897static char_u *prev_dir = NULL;
8898
8899#if defined(EXITFREE) || defined(PROTO)
8900 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008901free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00008902{
8903 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008904 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008905
8906 vim_free(globaldir);
8907 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008908}
8909#endif
8910
Bram Moolenaarf4258302013-06-02 18:20:17 +02008911/*
8912 * Deal with the side effects of changing the current directory.
8913 * When "local" is TRUE then this was after an ":lcd" command.
8914 */
8915 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008916post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008917{
8918 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008919 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008920 if (local)
8921 {
8922 /* If still in global directory, need to remember current
8923 * directory as global directory. */
8924 if (globaldir == NULL && prev_dir != NULL)
8925 globaldir = vim_strsave(prev_dir);
8926 /* Remember this local directory for the window. */
8927 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8928 curwin->w_localdir = vim_strsave(NameBuff);
8929 }
8930 else
8931 {
8932 /* We are now in the global directory, no need to remember its
8933 * name. */
8934 vim_free(globaldir);
8935 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008936 }
8937
8938 shorten_fnames(TRUE);
8939}
8940
Bram Moolenaarea408852005-06-25 22:49:46 +00008941
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942/*
8943 * ":cd", ":lcd", ":chdir" and ":lchdir".
8944 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008945 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008946ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 char_u *new_dir;
8949 char_u *tofree;
8950
8951 new_dir = eap->arg;
8952#if !defined(UNIX) && !defined(VMS)
8953 /* for non-UNIX ":cd" means: print current directory */
8954 if (*new_dir == NUL)
8955 ex_pwd(NULL);
8956 else
8957#endif
8958 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008959#ifdef FEAT_AUTOCMD
8960 if (allbuf_locked())
8961 return;
8962#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008963 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8964 && !eap->forceit)
8965 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008966 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008967 return;
8968 }
8969
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 /* ":cd -": Change to previous directory */
8971 if (STRCMP(new_dir, "-") == 0)
8972 {
8973 if (prev_dir == NULL)
8974 {
8975 EMSG(_("E186: No previous directory"));
8976 return;
8977 }
8978 new_dir = prev_dir;
8979 }
8980
8981 /* Save current directory for next ":cd -" */
8982 tofree = prev_dir;
8983 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8984 prev_dir = vim_strsave(NameBuff);
8985 else
8986 prev_dir = NULL;
8987
8988#if defined(UNIX) || defined(VMS)
8989 /* for UNIX ":cd" means: go to home directory */
8990 if (*new_dir == NUL)
8991 {
8992 /* use NameBuff for home directory name */
8993# ifdef VMS
8994 char_u *p;
8995
8996 p = mch_getenv((char_u *)"SYS$LOGIN");
8997 if (p == NULL || *p == NUL) /* empty is the same as not set */
8998 NameBuff[0] = NUL;
8999 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009000 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001# else
9002 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9003# endif
9004 new_dir = NameBuff;
9005 }
9006#endif
9007 if (new_dir == NULL || vim_chdir(new_dir))
9008 EMSG(_(e_failed));
9009 else
9010 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02009011 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012
9013 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009014 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 ex_pwd(eap);
9016 }
9017 vim_free(tofree);
9018 }
9019}
9020
9021/*
9022 * ":pwd".
9023 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009025ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026{
9027 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9028 {
9029#ifdef BACKSLASH_IN_FILENAME
9030 slash_adjust(NameBuff);
9031#endif
9032 msg(NameBuff);
9033 }
9034 else
9035 EMSG(_("E187: Unknown"));
9036}
9037
9038/*
9039 * ":=".
9040 */
9041 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009042ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043{
9044 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009045 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046}
9047
9048 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009049ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009051 int n;
9052 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053
9054 if (cursor_valid())
9055 {
9056 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9057 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01009058 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009060
9061 len = eap->line2;
9062 switch (*eap->arg)
9063 {
9064 case 'm': break;
9065 case NUL: len *= 1000L; break;
9066 default: EMSG2(_(e_invarg2), eap->arg); return;
9067 }
9068 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069}
9070
9071/*
9072 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9073 */
9074 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009075do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076{
9077 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009078 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079
9080 cursor_on();
9081 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009082 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009084 wait_now = msec - done > 1000L ? 1000L : msec - done;
9085#ifdef FEAT_TIMERS
9086 {
9087 long due_time = check_due_timer();
9088
9089 if (due_time > 0 && due_time < wait_now)
9090 wait_now = due_time;
9091 }
9092#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009093#ifdef FEAT_JOB_CHANNEL
9094 if (has_any_channel() && wait_now > 100L)
9095 wait_now = 100L;
9096#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009097 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009098#ifdef FEAT_JOB_CHANNEL
9099 if (has_any_channel())
9100 ui_breakcheck_force(TRUE);
9101 else
9102#endif
9103 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009104#ifdef MESSAGE_QUEUE
9105 /* Process the netbeans and clientserver messages that may have been
9106 * received in the call to ui_breakcheck() when the GUI is in use. This
9107 * may occur when running a test case. */
9108 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110 }
9111}
9112
9113 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009114do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115{
9116 int mode;
9117 char_u *cmdp;
9118
9119 cmdp = eap->cmd;
9120 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9121
9122 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9123 eap->arg, mode, isabbrev))
9124 {
9125 case 1: EMSG(_(e_invarg));
9126 break;
9127 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9128 break;
9129 }
9130}
9131
9132/*
9133 * ":winsize" command (obsolete).
9134 */
9135 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009136ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137{
9138 int w, h;
9139 char_u *arg = eap->arg;
9140 char_u *p;
9141
9142 w = getdigits(&arg);
9143 arg = skipwhite(arg);
9144 p = arg;
9145 h = getdigits(&arg);
9146 if (*p != NUL && *arg == NUL)
9147 set_shellsize(w, h, TRUE);
9148 else
9149 EMSG(_("E465: :winsize requires two number arguments"));
9150}
9151
9152#ifdef FEAT_WINDOWS
9153 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009154ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155{
9156 int xchar = NUL;
9157 char_u *p;
9158
9159 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9160 {
9161 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9162 if (eap->arg[1] == NUL)
9163 {
9164 EMSG(_(e_invarg));
9165 return;
9166 }
9167 xchar = eap->arg[1];
9168 p = eap->arg + 2;
9169 }
9170 else
9171 p = eap->arg + 1;
9172
9173 eap->nextcmd = check_nextcmd(p);
9174 p = skipwhite(p);
9175 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9176 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009177 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 {
9179 /* Pass flags on for ":vertical wincmd ]". */
9180 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009181 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9183 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009184 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 }
9186}
9187#endif
9188
Bram Moolenaar843ee412004-06-30 16:16:41 +00009189#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190/*
9191 * ":winpos".
9192 */
9193 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009194ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195{
9196 int x, y;
9197 char_u *arg = eap->arg;
9198 char_u *p;
9199
9200 if (*arg == NUL)
9201 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009202# if defined(FEAT_GUI) || defined(MSWIN)
9203# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009205# else
9206 if (mch_get_winpos(&x, &y) != FAIL)
9207# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 {
9209 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9210 msg(IObuff);
9211 }
9212 else
9213# endif
9214 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9215 }
9216 else
9217 {
9218 x = getdigits(&arg);
9219 arg = skipwhite(arg);
9220 p = arg;
9221 y = getdigits(&arg);
9222 if (*p == NUL || *arg != NUL)
9223 {
9224 EMSG(_("E466: :winpos requires two number arguments"));
9225 return;
9226 }
9227# ifdef FEAT_GUI
9228 if (gui.in_use)
9229 gui_mch_set_winpos(x, y);
9230 else if (gui.starting)
9231 {
9232 /* Remember the coordinates for when the window is opened. */
9233 gui_win_x = x;
9234 gui_win_y = y;
9235 }
9236# ifdef HAVE_TGETENT
9237 else
9238# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009239# else
9240# ifdef MSWIN
9241 mch_set_winpos(x, y);
9242# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243# endif
9244# ifdef HAVE_TGETENT
9245 if (*T_CWP)
9246 term_set_winpos(x, y);
9247# endif
9248 }
9249}
9250#endif
9251
9252/*
9253 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9254 */
9255 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009256ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257{
9258 oparg_T oa;
9259
9260 clear_oparg(&oa);
9261 oa.regname = eap->regname;
9262 oa.start.lnum = eap->line1;
9263 oa.end.lnum = eap->line2;
9264 oa.line_count = eap->line2 - eap->line1 + 1;
9265 oa.motion_type = MLINE;
9266#ifdef FEAT_VIRTUALEDIT
9267 virtual_op = FALSE;
9268#endif
9269 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9270 {
9271 setpcmark();
9272 curwin->w_cursor.lnum = eap->line1;
9273 beginline(BL_SOL | BL_FIX);
9274 }
9275
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009276 if (VIsual_active)
9277 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009278
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279 switch (eap->cmdidx)
9280 {
9281 case CMD_delete:
9282 oa.op_type = OP_DELETE;
9283 op_delete(&oa);
9284 break;
9285
9286 case CMD_yank:
9287 oa.op_type = OP_YANK;
9288 (void)op_yank(&oa, FALSE, TRUE);
9289 break;
9290
9291 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009292 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009294 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9295#else
9296 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009298 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299 oa.op_type = OP_RSHIFT;
9300 else
9301 oa.op_type = OP_LSHIFT;
9302 op_shift(&oa, FALSE, eap->amount);
9303 break;
9304 }
9305#ifdef FEAT_VIRTUALEDIT
9306 virtual_op = MAYBE;
9307#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009308 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309}
9310
9311/*
9312 * ":put".
9313 */
9314 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009315ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316{
9317 /* ":0put" works like ":1put!". */
9318 if (eap->line2 == 0)
9319 {
9320 eap->line2 = 1;
9321 eap->forceit = TRUE;
9322 }
9323 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009324 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9325 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326}
9327
9328/*
9329 * Handle ":copy" and ":move".
9330 */
9331 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009332ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333{
9334 long n;
9335
Bram Moolenaarded27822017-01-02 14:27:34 +01009336 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337 if (eap->arg == NULL) /* error detected */
9338 {
9339 eap->nextcmd = NULL;
9340 return;
9341 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009342 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343
9344 /*
9345 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9346 */
9347 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9348 {
9349 EMSG(_(e_invaddr));
9350 return;
9351 }
9352
9353 if (eap->cmdidx == CMD_move)
9354 {
9355 if (do_move(eap->line1, eap->line2, n) == FAIL)
9356 return;
9357 }
9358 else
9359 ex_copy(eap->line1, eap->line2, n);
9360 u_clearline();
9361 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009362 ex_may_print(eap);
9363}
9364
9365/*
9366 * Print the current line if flags were given to the Ex command.
9367 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009368 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009369ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009370{
9371 if (eap->flags != 0)
9372 {
9373 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9374 (eap->flags & EXFLAG_LIST));
9375 ex_no_reprint = TRUE;
9376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377}
9378
9379/*
9380 * ":smagic" and ":snomagic".
9381 */
9382 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009383ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384{
9385 int magic_save = p_magic;
9386
9387 p_magic = (eap->cmdidx == CMD_smagic);
9388 do_sub(eap);
9389 p_magic = magic_save;
9390}
9391
9392/*
9393 * ":join".
9394 */
9395 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009396ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397{
9398 curwin->w_cursor.lnum = eap->line1;
9399 if (eap->line1 == eap->line2)
9400 {
9401 if (eap->addr_count >= 2) /* :2,2join does nothing */
9402 return;
9403 if (eap->line2 == curbuf->b_ml.ml_line_count)
9404 {
9405 beep_flush();
9406 return;
9407 }
9408 ++eap->line2;
9409 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009410 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009412 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413}
9414
9415/*
9416 * ":[addr]@r" or ":[addr]*r": execute register
9417 */
9418 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009419ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420{
9421 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009422 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423
9424 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009425 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426
9427#ifdef USE_ON_FLY_SCROLL
9428 dont_scroll = TRUE; /* disallow scrolling here */
9429#endif
9430
9431 /* get the register name. No name means to use the previous one */
9432 c = *eap->arg;
9433 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9434 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009435 /* Put the register in the typeahead buffer with the "silent" flag. */
9436 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9437 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009438 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 else
9442 {
9443 int save_efr = exec_from_reg;
9444
9445 exec_from_reg = TRUE;
9446
9447 /*
9448 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009449 * Continue until the stuff buffer is empty and all added characters
9450 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009452 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9454
9455 exec_from_reg = save_efr;
9456 }
9457}
9458
9459/*
9460 * ":!".
9461 */
9462 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009463ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464{
9465 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9466}
9467
9468/*
9469 * ":undo".
9470 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009472ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009474 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009475 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009476 else
9477 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478}
9479
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009480#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009481 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009482ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009483{
9484 char_u hash[UNDO_HASH_SIZE];
9485
9486 u_compute_hash(hash);
9487 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9488}
9489
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009490 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009491ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009492{
9493 char_u hash[UNDO_HASH_SIZE];
9494
9495 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009496 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009497}
9498#endif
9499
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500/*
9501 * ":redo".
9502 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009504ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505{
9506 u_redo(1);
9507}
9508
9509/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009510 * ":earlier" and ":later".
9511 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009512 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009513ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009514{
9515 long count = 0;
9516 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009517 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009518 char_u *p = eap->arg;
9519
9520 if (*p == NUL)
9521 count = 1;
9522 else if (isdigit(*p))
9523 {
9524 count = getdigits(&p);
9525 switch (*p)
9526 {
9527 case 's': ++p; sec = TRUE; break;
9528 case 'm': ++p; sec = TRUE; count *= 60; break;
9529 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009530 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9531 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009532 }
9533 }
9534
9535 if (*p != NUL)
9536 EMSG2(_(e_invarg2), eap->arg);
9537 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009538 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9539 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009540}
9541
9542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 * ":redir": start/stop redirection.
9544 */
9545 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009546ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547{
9548 char *mode;
9549 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009550 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551
Bram Moolenaarba768492016-07-08 15:32:54 +02009552#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009553 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009554 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009555 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009556 return;
9557 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009558#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009559
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 if (STRICMP(eap->arg, "END") == 0)
9561 close_redir();
9562 else
9563 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009564 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009566 ++arg;
9567 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009569 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 mode = "a";
9571 }
9572 else
9573 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009574 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575
9576 close_redir();
9577
9578 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009579 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 if (fname == NULL)
9581 return;
9582#ifdef FEAT_BROWSE
9583 if (cmdmod.browse)
9584 {
9585 char_u *browseFile;
9586
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009587 browseFile = do_browse(BROWSE_SAVE,
9588 (char_u *)_("Save Redirection"),
9589 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 if (browseFile == NULL)
9591 return; /* operation cancelled */
9592 vim_free(fname);
9593 fname = browseFile;
9594 eap->forceit = TRUE; /* since dialog already asked */
9595 }
9596#endif
9597
9598 redir_fd = open_exfile(fname, eap->forceit, mode);
9599 vim_free(fname);
9600 }
9601#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009602 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 {
9604 /* redirect to a register a-z (resp. A-Z for appending) */
9605 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009606 ++arg;
9607 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009609 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009610 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009612 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009614 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009615 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009616 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009617 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009619 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009620 if (*arg == '>')
9621 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009622 /* Make register empty when not using @A-@Z and the
9623 * command is valid. */
9624 if (*arg == NUL && !isupper(redir_reg))
9625 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009627 }
9628 if (*arg != NUL)
9629 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009630 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009631 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009632 }
9633 }
9634 else if (*arg == '=' && arg[1] == '>')
9635 {
9636 int append;
9637
9638 /* redirect to a variable */
9639 close_redir();
9640 arg += 2;
9641
9642 if (*arg == '>')
9643 {
9644 ++arg;
9645 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 }
9647 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009648 append = FALSE;
9649
9650 if (var_redir_start(skipwhite(arg), append) == OK)
9651 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652 }
9653#endif
9654
9655 /* TODO: redirect to a buffer */
9656
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009658 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009660
9661 /* Make sure redirection is not off. Can happen for cmdline completion
9662 * that indirectly invokes a command to catch its output. */
9663 if (redir_fd != NULL
9664#ifdef FEAT_EVAL
9665 || redir_reg || redir_vname
9666#endif
9667 )
9668 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669}
9670
9671/*
9672 * ":redraw": force redraw
9673 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009674 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009675ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676{
9677 int r = RedrawingDisabled;
9678 int p = p_lz;
9679
9680 RedrawingDisabled = 0;
9681 p_lz = FALSE;
9682 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009683 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684#ifdef FEAT_TITLE
9685 if (need_maketitle)
9686 maketitle();
9687#endif
9688 RedrawingDisabled = r;
9689 p_lz = p;
9690
9691 /* Reset msg_didout, so that a message that's there is overwritten. */
9692 msg_didout = FALSE;
9693 msg_col = 0;
9694
Bram Moolenaar56667a52013-07-17 11:54:28 +02009695 /* No need to wait after an intentional redraw. */
9696 need_wait_return = FALSE;
9697
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 out_flush();
9699}
9700
9701/*
9702 * ":redrawstatus": force redraw of status line(s)
9703 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009705ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706{
9707#if defined(FEAT_WINDOWS)
9708 int r = RedrawingDisabled;
9709 int p = p_lz;
9710
9711 RedrawingDisabled = 0;
9712 p_lz = FALSE;
9713 if (eap->forceit)
9714 status_redraw_all();
9715 else
9716 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009717 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 RedrawingDisabled = r;
9719 p_lz = p;
9720 out_flush();
9721#endif
9722}
9723
9724 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009725close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726{
9727 if (redir_fd != NULL)
9728 {
9729 fclose(redir_fd);
9730 redir_fd = NULL;
9731 }
9732#ifdef FEAT_EVAL
9733 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009734 if (redir_vname)
9735 {
9736 var_redir_stop();
9737 redir_vname = 0;
9738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739#endif
9740}
9741
9742#if defined(FEAT_SESSION) && defined(USE_CRNL)
9743# define MKSESSION_NL
9744static int mksession_nl = FALSE; /* use NL only in put_eol() */
9745#endif
9746
9747/*
9748 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9749 */
9750 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009751ex_mkrc(
9752 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753{
9754 FILE *fd;
9755 int failed = FALSE;
9756 char_u *fname;
9757#ifdef FEAT_BROWSE
9758 char_u *browseFile = NULL;
9759#endif
9760#ifdef FEAT_SESSION
9761 int view_session = FALSE;
9762 int using_vdir = FALSE; /* using 'viewdir'? */
9763 char_u *viewFile = NULL;
9764 unsigned *flagp;
9765#endif
9766
9767 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9768 {
9769#ifdef FEAT_SESSION
9770 view_session = TRUE;
9771#else
9772 ex_ni(eap);
9773 return;
9774#endif
9775 }
9776
9777#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009778 /* Use the short file name until ":lcd" is used. We also don't use the
9779 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009780 did_lcd = FALSE;
9781
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9783 if (eap->cmdidx == CMD_mkview
9784 && (*eap->arg == NUL
9785 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9786 {
9787 eap->forceit = TRUE;
9788 fname = get_view_file(*eap->arg);
9789 if (fname == NULL)
9790 return;
9791 viewFile = fname;
9792 using_vdir = TRUE;
9793 }
9794 else
9795#endif
9796 if (*eap->arg != NUL)
9797 fname = eap->arg;
9798 else if (eap->cmdidx == CMD_mkvimrc)
9799 fname = (char_u *)VIMRC_FILE;
9800#ifdef FEAT_SESSION
9801 else if (eap->cmdidx == CMD_mksession)
9802 fname = (char_u *)SESSION_FILE;
9803#endif
9804 else
9805 fname = (char_u *)EXRC_FILE;
9806
9807#ifdef FEAT_BROWSE
9808 if (cmdmod.browse)
9809 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009810 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811# ifdef FEAT_SESSION
9812 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9813 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9814# endif
9815 (char_u *)_("Save Setup"),
9816 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9817 if (browseFile == NULL)
9818 goto theend;
9819 fname = browseFile;
9820 eap->forceit = TRUE; /* since dialog already asked */
9821 }
9822#endif
9823
9824#if defined(FEAT_SESSION) && defined(vim_mkdir)
9825 /* When using 'viewdir' may have to create the directory. */
9826 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009827 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828#endif
9829
9830 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9831 if (fd != NULL)
9832 {
9833#ifdef FEAT_SESSION
9834 if (eap->cmdidx == CMD_mkview)
9835 flagp = &vop_flags;
9836 else
9837 flagp = &ssop_flags;
9838#endif
9839
9840#ifdef MKSESSION_NL
9841 /* "unix" in 'sessionoptions': use NL line separator */
9842 if (view_session && (*flagp & SSOP_UNIX))
9843 mksession_nl = TRUE;
9844#endif
9845
9846 /* Write the version command for :mkvimrc */
9847 if (eap->cmdidx == CMD_mkvimrc)
9848 (void)put_line(fd, "version 6.0");
9849
9850#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009851 if (eap->cmdidx == CMD_mksession)
9852 {
9853 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9854 failed = TRUE;
9855 }
9856
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 if (eap->cmdidx != CMD_mkview)
9858#endif
9859 {
9860 /* Write setting 'compatible' first, because it has side effects.
9861 * For that same reason only do it when needed. */
9862 if (p_cp)
9863 (void)put_line(fd, "if !&cp | set cp | endif");
9864 else
9865 (void)put_line(fd, "if &cp | set nocp | endif");
9866 }
9867
9868#ifdef FEAT_SESSION
9869 if (!view_session
9870 || (eap->cmdidx == CMD_mksession
9871 && (*flagp & SSOP_OPTIONS)))
9872#endif
9873 failed |= (makemap(fd, NULL) == FAIL
9874 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9875
9876#ifdef FEAT_SESSION
9877 if (!failed && view_session)
9878 {
9879 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9880 failed = TRUE;
9881 if (eap->cmdidx == CMD_mksession)
9882 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009883 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884
Bram Moolenaard9462e32011-04-11 21:35:11 +02009885 dirnow = alloc(MAXPATHL);
9886 if (dirnow == NULL)
9887 failed = TRUE;
9888 else
9889 {
9890 /*
9891 * Change to session file's dir.
9892 */
9893 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009895 *dirnow = NUL;
9896 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9897 {
9898 if (vim_chdirfile(fname) == OK)
9899 shorten_fnames(TRUE);
9900 }
9901 else if (*dirnow != NUL
9902 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9903 {
9904 if (mch_chdir((char *)globaldir) == 0)
9905 shorten_fnames(TRUE);
9906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907
Bram Moolenaard9462e32011-04-11 21:35:11 +02009908 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909
Bram Moolenaard9462e32011-04-11 21:35:11 +02009910 /* restore original dir */
9911 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009913 {
9914 if (mch_chdir((char *)dirnow) != 0)
9915 EMSG(_(e_prev_dir));
9916 shorten_fnames(TRUE);
9917 }
9918 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 }
9920 }
9921 else
9922 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009923 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9924 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925 }
9926 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9927 == FAIL)
9928 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009929 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9930 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009931 if (eap->cmdidx == CMD_mksession)
9932 {
9933 if (put_line(fd, "unlet SessionLoad") == FAIL)
9934 failed = TRUE;
9935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 }
9937#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009938 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9939 failed = TRUE;
9940
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 failed |= fclose(fd);
9942
9943 if (failed)
9944 EMSG(_(e_write));
9945#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9946 else if (eap->cmdidx == CMD_mksession)
9947 {
9948 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009949 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950
Bram Moolenaard9462e32011-04-11 21:35:11 +02009951 tbuf = alloc(MAXPATHL);
9952 if (tbuf != NULL)
9953 {
9954 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9955 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9956 vim_free(tbuf);
9957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 }
9959#endif
9960#ifdef MKSESSION_NL
9961 mksession_nl = FALSE;
9962#endif
9963 }
9964
9965#ifdef FEAT_BROWSE
9966theend:
9967 vim_free(browseFile);
9968#endif
9969#ifdef FEAT_SESSION
9970 vim_free(viewFile);
9971#endif
9972}
9973
Bram Moolenaardf177f62005-02-22 08:39:57 +00009974#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9975 || defined(PROTO)
9976 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009977vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009978{
9979 if (vim_mkdir(name, prot) != 0)
9980 {
9981 EMSG2(_("E739: Cannot create directory: %s"), name);
9982 return FAIL;
9983 }
9984 return OK;
9985}
9986#endif
9987
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988/*
9989 * Open a file for writing for an Ex command, with some checks.
9990 * Return file descriptor, or NULL on failure.
9991 */
9992 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009993open_exfile(
9994 char_u *fname,
9995 int forceit,
9996 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997{
9998 FILE *fd;
9999
10000#ifdef UNIX
10001 /* with Unix it is possible to open a directory */
10002 if (mch_isdir(fname))
10003 {
10004 EMSG2(_(e_isadir2), fname);
10005 return NULL;
10006 }
10007#endif
10008 if (!forceit && *mode != 'a' && vim_fexists(fname))
10009 {
10010 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
10011 return NULL;
10012 }
10013
10014 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
10015 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
10016
10017 return fd;
10018}
10019
10020/*
10021 * ":mark" and ":k".
10022 */
10023 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010024ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025{
10026 pos_T pos;
10027
10028 if (*eap->arg == NUL) /* No argument? */
10029 EMSG(_(e_argreq));
10030 else if (eap->arg[1] != NUL) /* more than one character? */
10031 EMSG(_(e_trailing));
10032 else
10033 {
10034 pos = curwin->w_cursor; /* save curwin->w_cursor */
10035 curwin->w_cursor.lnum = eap->line2;
10036 beginline(BL_WHITE | BL_FIX);
10037 if (setmark(*eap->arg) == FAIL) /* set mark */
10038 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10039 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10040 }
10041}
10042
10043/*
10044 * Update w_topline, w_leftcol and the cursor position.
10045 */
10046 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010047update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048{
10049 check_cursor(); /* put cursor on valid line */
10050 update_topline();
10051 if (!curwin->w_p_wrap)
10052 validate_cursor();
10053 update_curswant();
10054}
10055
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056/*
10057 * ":normal[!] {commands}": Execute normal mode commands.
10058 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010059 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010060ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 int save_msg_scroll = msg_scroll;
10063 int save_restart_edit = restart_edit;
10064 int save_msg_didout = msg_didout;
10065 int save_State = State;
10066 tasave_T tabuf;
10067 int save_insertmode = p_im;
10068 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010069 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070#ifdef FEAT_MBYTE
10071 char_u *arg = NULL;
10072 int l;
10073 char_u *p;
10074#endif
10075
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010076 if (ex_normal_lock > 0)
10077 {
10078 EMSG(_(e_secure));
10079 return;
10080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081 if (ex_normal_busy >= p_mmd)
10082 {
10083 EMSG(_("E192: Recursive use of :normal too deep"));
10084 return;
10085 }
10086 ++ex_normal_busy;
10087
10088 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10089 restart_edit = 0; /* don't go to Insert mode */
10090 p_im = FALSE; /* don't use 'insertmode' */
10091
10092#ifdef FEAT_MBYTE
10093 /*
10094 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10095 * this for the K_SPECIAL leading byte, otherwise special keys will not
10096 * work.
10097 */
10098 if (has_mbyte)
10099 {
10100 int len = 0;
10101
10102 /* Count the number of characters to be escaped. */
10103 for (p = eap->arg; *p != NUL; ++p)
10104 {
10105# ifdef FEAT_GUI
10106 if (*p == CSI) /* leadbyte CSI */
10107 len += 2;
10108# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010109 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10111# ifdef FEAT_GUI
10112 || *p == CSI
10113# endif
10114 )
10115 len += 2;
10116 }
10117 if (len > 0)
10118 {
10119 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10120 if (arg != NULL)
10121 {
10122 len = 0;
10123 for (p = eap->arg; *p != NUL; ++p)
10124 {
10125 arg[len++] = *p;
10126# ifdef FEAT_GUI
10127 if (*p == CSI)
10128 {
10129 arg[len++] = KS_EXTRA;
10130 arg[len++] = (int)KE_CSI;
10131 }
10132# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010133 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 {
10135 arg[len++] = *++p;
10136 if (*p == K_SPECIAL)
10137 {
10138 arg[len++] = KS_SPECIAL;
10139 arg[len++] = KE_FILLER;
10140 }
10141# ifdef FEAT_GUI
10142 else if (*p == CSI)
10143 {
10144 arg[len++] = KS_EXTRA;
10145 arg[len++] = (int)KE_CSI;
10146 }
10147# endif
10148 }
10149 arg[len] = NUL;
10150 }
10151 }
10152 }
10153 }
10154#endif
10155
10156 /*
10157 * Save the current typeahead. This is required to allow using ":normal"
10158 * from an event handler and makes sure we don't hang when the argument
10159 * ends with half a command.
10160 */
10161 save_typeahead(&tabuf);
10162 if (tabuf.typebuf_valid)
10163 {
10164 /*
10165 * Repeat the :normal command for each line in the range. When no
10166 * range given, execute it just once, without positioning the cursor
10167 * first.
10168 */
10169 do
10170 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171 if (eap->addr_count != 0)
10172 {
10173 curwin->w_cursor.lnum = eap->line1++;
10174 curwin->w_cursor.col = 0;
10175 }
10176
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010177 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178#ifdef FEAT_MBYTE
10179 arg != NULL ? arg :
10180#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010181 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182 }
10183 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10184 }
10185
10186 /* Might not return to the main loop when in an event handler. */
10187 update_topline_cursor();
10188
10189 /* Restore the previous typeahead. */
10190 restore_typeahead(&tabuf);
10191
10192 --ex_normal_busy;
10193 msg_scroll = save_msg_scroll;
10194 restart_edit = save_restart_edit;
10195 p_im = save_insertmode;
10196 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010197 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
10199
10200 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +010010201 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +010010203#ifdef FEAT_MOUSE
10204 setmouse();
10205#endif
10206#ifdef CURSOR_SHAPE
10207 ui_cursor_shape(); /* may show different cursor shape */
10208#endif
10209
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210#ifdef FEAT_MBYTE
10211 vim_free(arg);
10212#endif
10213}
10214
10215/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010216 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 */
10218 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010219ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010221 if (eap->forceit)
10222 {
10223 coladvance((colnr_T)MAXCOL);
10224 curwin->w_curswant = MAXCOL;
10225 curwin->w_set_curswant = FALSE;
10226 }
10227
Bram Moolenaara40c5002005-01-09 21:16:21 +000010228 /* Ignore the command when already in Insert mode. Inserting an
10229 * expression register that invokes a function can do this. */
10230 if (State & INSERT)
10231 return;
10232
Bram Moolenaar64969662005-12-14 21:59:55 +000010233 if (eap->cmdidx == CMD_startinsert)
10234 restart_edit = 'a';
10235 else if (eap->cmdidx == CMD_startreplace)
10236 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010238 restart_edit = 'V';
10239
10240 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010242 if (eap->cmdidx == CMD_startinsert)
10243 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 curwin->w_curswant = 0; /* avoid MAXCOL */
10245 }
10246}
10247
10248/*
10249 * ":stopinsert"
10250 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010252ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253{
10254 restart_edit = 0;
10255 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010256 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010259/*
10260 * Execute normal mode command "cmd".
10261 * "remap" can be REMAP_NONE or REMAP_YES.
10262 */
10263 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010264exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010265{
Bram Moolenaar25281632016-01-21 23:32:32 +010010266 /* Stuff the argument into the typeahead buffer. */
10267 ins_typebuf(cmd, remap, 0, TRUE, silent);
10268 exec_normal(FALSE);
10269}
Bram Moolenaar25281632016-01-21 23:32:32 +010010270
Bram Moolenaar25281632016-01-21 23:32:32 +010010271/*
10272 * Execute normal_cmd() until there is no typeahead left.
10273 */
10274 void
10275exec_normal(int was_typed)
10276{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010277 oparg_T oa;
10278
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010279 clear_oparg(&oa);
10280 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010281 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10282 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010283 {
10284 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010285 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010286 }
10287}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010288
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289#ifdef FEAT_FIND_ID
10290 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010291ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292{
10293 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10294 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10295 (linenr_T)1, (linenr_T)MAXLNUM);
10296}
10297
10298#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
10299/*
10300 * ":psearch"
10301 */
10302 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010303ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304{
10305 g_do_tagpreview = p_pvh;
10306 ex_findpat(eap);
10307 g_do_tagpreview = 0;
10308}
10309#endif
10310
10311 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010312ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313{
10314 int whole = TRUE;
10315 long n;
10316 char_u *p;
10317 int action;
10318
10319 switch (cmdnames[eap->cmdidx].cmd_name[2])
10320 {
10321 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10322 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10323 action = ACTION_GOTO;
10324 else
10325 action = ACTION_SHOW;
10326 break;
10327 case 'i': /* ":ilist" and ":dlist" */
10328 action = ACTION_SHOW_ALL;
10329 break;
10330 case 'u': /* ":ijump" and ":djump" */
10331 action = ACTION_GOTO;
10332 break;
10333 default: /* ":isplit" and ":dsplit" */
10334 action = ACTION_SPLIT;
10335 break;
10336 }
10337
10338 n = 1;
10339 if (vim_isdigit(*eap->arg)) /* get count */
10340 {
10341 n = getdigits(&eap->arg);
10342 eap->arg = skipwhite(eap->arg);
10343 }
10344 if (*eap->arg == '/') /* Match regexp, not just whole words */
10345 {
10346 whole = FALSE;
10347 ++eap->arg;
10348 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10349 if (*p)
10350 {
10351 *p++ = NUL;
10352 p = skipwhite(p);
10353
10354 /* Check for trailing illegal characters */
10355 if (!ends_excmd(*p))
10356 eap->errmsg = e_trailing;
10357 else
10358 eap->nextcmd = check_nextcmd(p);
10359 }
10360 }
10361 if (!eap->skip)
10362 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10363 whole, !eap->forceit,
10364 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10365 n, action, eap->line1, eap->line2);
10366}
10367#endif
10368
10369#ifdef FEAT_WINDOWS
10370
10371# ifdef FEAT_QUICKFIX
10372/*
10373 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10374 */
10375 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010376ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010378 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10380}
10381
10382/*
10383 * ":pedit"
10384 */
10385 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010386ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387{
10388 win_T *curwin_save = curwin;
10389
10390 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010391 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392 keep_help_flag = curwin_save->w_buffer->b_help;
10393 do_exedit(eap, NULL);
10394 keep_help_flag = FALSE;
10395 if (curwin != curwin_save && win_valid(curwin_save))
10396 {
10397 /* Return cursor to where we were */
10398 validate_cursor();
10399 redraw_later(VALID);
10400 win_enter(curwin_save, TRUE);
10401 }
10402 g_do_tagpreview = 0;
10403}
10404# endif
10405
10406/*
10407 * ":stag", ":stselect" and ":stjump".
10408 */
10409 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010410ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411{
10412 postponed_split = -1;
10413 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010414 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10416 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010417 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418}
10419#endif
10420
10421/*
10422 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10423 */
10424 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010425ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426{
10427 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10428}
10429
10430 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010431ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432{
10433 int cmd;
10434
10435 switch (name[1])
10436 {
10437 case 'j': cmd = DT_JUMP; /* ":tjump" */
10438 break;
10439 case 's': cmd = DT_SELECT; /* ":tselect" */
10440 break;
10441 case 'p': cmd = DT_PREV; /* ":tprevious" */
10442 break;
10443 case 'N': cmd = DT_PREV; /* ":tNext" */
10444 break;
10445 case 'n': cmd = DT_NEXT; /* ":tnext" */
10446 break;
10447 case 'o': cmd = DT_POP; /* ":pop" */
10448 break;
10449 case 'f': /* ":tfirst" */
10450 case 'r': cmd = DT_FIRST; /* ":trewind" */
10451 break;
10452 case 'l': cmd = DT_LAST; /* ":tlast" */
10453 break;
10454 default: /* ":tag" */
10455#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010456 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010458 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459 return;
10460 }
10461#endif
10462 cmd = DT_TAG;
10463 break;
10464 }
10465
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010466 if (name[0] == 'l')
10467 {
10468#ifndef FEAT_QUICKFIX
10469 ex_ni(eap);
10470 return;
10471#else
10472 cmd = DT_LTAG;
10473#endif
10474 }
10475
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10477 eap->forceit, TRUE);
10478}
10479
10480/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010481 * Check "str" for starting with a special cmdline variable.
10482 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10483 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10484 */
10485 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010486find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010487{
10488 int len;
10489 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010490 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010491 "%",
10492#define SPEC_PERC 0
10493 "#",
10494#define SPEC_HASH 1
10495 "<cword>", /* cursor word */
10496#define SPEC_CWORD 2
10497 "<cWORD>", /* cursor WORD */
10498#define SPEC_CCWORD 3
10499 "<cfile>", /* cursor path name */
10500#define SPEC_CFILE 4
10501 "<sfile>", /* ":so" file name */
10502#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010503 "<slnum>", /* ":so" file line number */
10504#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010505#ifdef FEAT_AUTOCMD
10506 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010507# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010508 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010509# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010510 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010511# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010512#endif
10513#ifdef FEAT_CLIENTSERVER
10514 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010515# ifdef FEAT_AUTOCMD
10516# define SPEC_CLIENT 10
10517# else
10518# define SPEC_CLIENT 7
10519# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010520#endif
10521 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010522
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010523 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010524 {
10525 len = (int)STRLEN(spec_str[i]);
10526 if (STRNCMP(src, spec_str[i], len) == 0)
10527 {
10528 *usedlen = len;
10529 return i;
10530 }
10531 }
10532 return -1;
10533}
10534
10535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536 * Evaluate cmdline variables.
10537 *
10538 * change '%' to curbuf->b_ffname
10539 * '#' to curwin->w_altfile
10540 * '<cword>' to word under the cursor
10541 * '<cWORD>' to WORD under the cursor
10542 * '<cfile>' to path name under the cursor
10543 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010544 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545 * '<afile>' to file name for autocommand
10546 * '<abuf>' to buffer number for autocommand
10547 * '<amatch>' to matching name for autocommand
10548 *
10549 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10550 * "" for error without a message) and NULL is returned.
10551 * Returns an allocated string if a valid match was found.
10552 * Returns NULL if no match was found. "usedlen" then still contains the
10553 * number of characters to skip.
10554 */
10555 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010556eval_vars(
10557 char_u *src, /* pointer into commandline */
10558 char_u *srcstart, /* beginning of valid memory for src */
10559 int *usedlen, /* characters after src that are used */
10560 linenr_T *lnump, /* line number for :e command, or NULL */
10561 char_u **errormsg, /* pointer to error message */
10562 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010563 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564{
10565 int i;
10566 char_u *s;
10567 char_u *result;
10568 char_u *resultbuf = NULL;
10569 int resultlen;
10570 buf_T *buf;
10571 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10572 int spec_idx;
10573#ifdef FEAT_MODIFY_FNAME
10574 int skip_mod = FALSE;
10575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577
10578 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010579 if (escaped != NULL)
10580 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581
10582 /*
10583 * Check if there is something to do.
10584 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010585 spec_idx = find_cmdline_var(src, usedlen);
10586 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587 {
10588 *usedlen = 1;
10589 return NULL;
10590 }
10591
10592 /*
10593 * Skip when preceded with a backslash "\%" and "\#".
10594 * Note: In "\\%" the % is also not recognized!
10595 */
10596 if (src > srcstart && src[-1] == '\\')
10597 {
10598 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010599 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600 return NULL;
10601 }
10602
10603 /*
10604 * word or WORD under cursor
10605 */
10606 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10607 {
10608 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10609 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10610 if (resultlen == 0)
10611 {
10612 *errormsg = (char_u *)"";
10613 return NULL;
10614 }
10615 }
10616
10617 /*
10618 * '#': Alternate file name
10619 * '%': Current file name
10620 * File name under the cursor
10621 * File name for autocommand
10622 * and following modifiers
10623 */
10624 else
10625 {
10626 switch (spec_idx)
10627 {
10628 case SPEC_PERC: /* '%': current file */
10629 if (curbuf->b_fname == NULL)
10630 {
10631 result = (char_u *)"";
10632 valid = 0; /* Must have ":p:h" to be valid */
10633 }
10634 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636 break;
10637
10638 case SPEC_HASH: /* '#' or "#99": alternate file */
10639 if (src[1] == '#') /* "##": the argument list */
10640 {
10641 result = arg_all();
10642 resultbuf = result;
10643 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010644 if (escaped != NULL)
10645 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646#ifdef FEAT_MODIFY_FNAME
10647 skip_mod = TRUE;
10648#endif
10649 break;
10650 }
10651 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010652 if (*s == '<') /* "#<99" uses v:oldfiles */
10653 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654 i = (int)getdigits(&s);
10655 *usedlen = (int)(s - src); /* length of what we expand */
10656
Bram Moolenaard812df62008-11-09 12:46:09 +000010657 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010659 if (*usedlen < 2)
10660 {
10661 /* Should we give an error message for #<text? */
10662 *usedlen = 1;
10663 return NULL;
10664 }
10665#ifdef FEAT_EVAL
10666 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10667 (long)i);
10668 if (result == NULL)
10669 {
10670 *errormsg = (char_u *)"";
10671 return NULL;
10672 }
10673#else
10674 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 }
10678 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010679 {
10680 buf = buflist_findnr(i);
10681 if (buf == NULL)
10682 {
10683 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10684 return NULL;
10685 }
10686 if (lnump != NULL)
10687 *lnump = ECMD_LAST;
10688 if (buf->b_fname == NULL)
10689 {
10690 result = (char_u *)"";
10691 valid = 0; /* Must have ":p:h" to be valid */
10692 }
10693 else
10694 result = buf->b_fname;
10695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696 break;
10697
10698#ifdef FEAT_SEARCHPATH
10699 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010700 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701 if (result == NULL)
10702 {
10703 *errormsg = (char_u *)"";
10704 return NULL;
10705 }
10706 resultbuf = result; /* remember allocated string */
10707 break;
10708#endif
10709
10710#ifdef FEAT_AUTOCMD
10711 case SPEC_AFILE: /* file name for autocommand */
10712 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010713 if (result != NULL && !autocmd_fname_full)
10714 {
10715 /* Still need to turn the fname into a full path. It is
10716 * postponed to avoid a delay when <afile> is not used. */
10717 autocmd_fname_full = TRUE;
10718 result = FullName_save(autocmd_fname, FALSE);
10719 vim_free(autocmd_fname);
10720 autocmd_fname = result;
10721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722 if (result == NULL)
10723 {
10724 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10725 return NULL;
10726 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010727 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 break;
10729
10730 case SPEC_ABUF: /* buffer number for autocommand */
10731 if (autocmd_bufnr <= 0)
10732 {
10733 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10734 return NULL;
10735 }
10736 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10737 result = strbuf;
10738 break;
10739
10740 case SPEC_AMATCH: /* match name for autocommand */
10741 result = autocmd_match;
10742 if (result == NULL)
10743 {
10744 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10745 return NULL;
10746 }
10747 break;
10748
10749#endif
10750 case SPEC_SFILE: /* file name for ":so" command */
10751 result = sourcing_name;
10752 if (result == NULL)
10753 {
10754 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10755 return NULL;
10756 }
10757 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010758 case SPEC_SLNUM: /* line in file for ":so" command */
10759 if (sourcing_name == NULL || sourcing_lnum == 0)
10760 {
10761 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10762 return NULL;
10763 }
10764 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10765 result = strbuf;
10766 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767#if defined(FEAT_CLIENTSERVER)
10768 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010769 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10770 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771 result = strbuf;
10772 break;
10773#endif
10774 }
10775
10776 resultlen = (int)STRLEN(result); /* length of new string */
10777 if (src[*usedlen] == '<') /* remove the file name extension */
10778 {
10779 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781 resultlen = (int)(s - result);
10782 }
10783#ifdef FEAT_MODIFY_FNAME
10784 else if (!skip_mod)
10785 {
10786 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10787 &resultlen);
10788 if (result == NULL)
10789 {
10790 *errormsg = (char_u *)"";
10791 return NULL;
10792 }
10793 }
10794#endif
10795 }
10796
10797 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10798 {
10799 if (valid != VALID_HEAD + VALID_PATH)
10800 /* xgettext:no-c-format */
10801 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10802 else
10803 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10804 result = NULL;
10805 }
10806 else
10807 result = vim_strnsave(result, resultlen);
10808 vim_free(resultbuf);
10809 return result;
10810}
10811
10812/*
10813 * Concatenate all files in the argument list, separated by spaces, and return
10814 * it in one allocated string.
10815 * Spaces and backslashes in the file names are escaped with a backslash.
10816 * Returns NULL when out of memory.
10817 */
10818 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010819arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820{
10821 int len;
10822 int idx;
10823 char_u *retval = NULL;
10824 char_u *p;
10825
10826 /*
10827 * Do this loop two times:
10828 * first time: compute the total length
10829 * second time: concatenate the names
10830 */
10831 for (;;)
10832 {
10833 len = 0;
10834 for (idx = 0; idx < ARGCOUNT; ++idx)
10835 {
10836 p = alist_name(&ARGLIST[idx]);
10837 if (p != NULL)
10838 {
10839 if (len > 0)
10840 {
10841 /* insert a space in between names */
10842 if (retval != NULL)
10843 retval[len] = ' ';
10844 ++len;
10845 }
10846 for ( ; *p != NUL; ++p)
10847 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010848 if (*p == ' '
10849#ifndef BACKSLASH_IN_FILENAME
10850 || *p == '\\'
10851#endif
10852 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853 {
10854 /* insert a backslash */
10855 if (retval != NULL)
10856 retval[len] = '\\';
10857 ++len;
10858 }
10859 if (retval != NULL)
10860 retval[len] = *p;
10861 ++len;
10862 }
10863 }
10864 }
10865
10866 /* second time: break here */
10867 if (retval != NULL)
10868 {
10869 retval[len] = NUL;
10870 break;
10871 }
10872
10873 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010874 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875 if (retval == NULL)
10876 break;
10877 }
10878
10879 return retval;
10880}
10881
10882#if defined(FEAT_AUTOCMD) || defined(PROTO)
10883/*
10884 * Expand the <sfile> string in "arg".
10885 *
10886 * Returns an allocated string, or NULL for any error.
10887 */
10888 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010889expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890{
10891 char_u *errormsg;
10892 int len;
10893 char_u *result;
10894 char_u *newres;
10895 char_u *repl;
10896 int srclen;
10897 char_u *p;
10898
10899 result = vim_strsave(arg);
10900 if (result == NULL)
10901 return NULL;
10902
10903 for (p = result; *p; )
10904 {
10905 if (STRNCMP(p, "<sfile>", 7) != 0)
10906 ++p;
10907 else
10908 {
10909 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010910 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911 if (errormsg != NULL)
10912 {
10913 if (*errormsg)
10914 emsg(errormsg);
10915 vim_free(result);
10916 return NULL;
10917 }
10918 if (repl == NULL) /* no match (cannot happen) */
10919 {
10920 p += srclen;
10921 continue;
10922 }
10923 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10924 newres = alloc(len);
10925 if (newres == NULL)
10926 {
10927 vim_free(repl);
10928 vim_free(result);
10929 return NULL;
10930 }
10931 mch_memmove(newres, result, (size_t)(p - result));
10932 STRCPY(newres + (p - result), repl);
10933 len = (int)STRLEN(newres);
10934 STRCAT(newres, p + srclen);
10935 vim_free(repl);
10936 vim_free(result);
10937 result = newres;
10938 p = newres + len; /* continue after the match */
10939 }
10940 }
10941
10942 return result;
10943}
10944#endif
10945
10946#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010010947static int ses_winsizes(FILE *fd, int restore_size,
10948 win_T *tab_firstwin);
10949static int ses_win_rec(FILE *fd, frame_T *fr);
10950static frame_T *ses_skipframe(frame_T *fr);
10951static int ses_do_frame(frame_T *fr);
10952static int ses_do_win(win_T *wp);
10953static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
10954static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
10955static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956
10957/*
10958 * Write openfile commands for the current buffers to an .exrc file.
10959 * Return FAIL on error, OK otherwise.
10960 */
10961 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010962makeopens(
10963 FILE *fd,
10964 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965{
10966 buf_T *buf;
10967 int only_save_windows = TRUE;
10968 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969 int restore_size = TRUE;
10970 win_T *wp;
10971 char_u *sname;
10972 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010973 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010974 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010975 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010976 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010977 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010978 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979
10980 if (ssop_flags & SSOP_BUFFERS)
10981 only_save_windows = FALSE; /* Save ALL buffers */
10982
10983 /*
10984 * Begin by setting the this_session variable, and then other
10985 * sessionable variables.
10986 */
10987#ifdef FEAT_EVAL
10988 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10989 return FAIL;
10990 if (ssop_flags & SSOP_GLOBALS)
10991 if (store_session_globals(fd) == FAIL)
10992 return FAIL;
10993#endif
10994
10995 /*
10996 * Close all windows but one.
10997 */
10998 if (put_line(fd, "silent only") == FAIL)
10999 return FAIL;
11000
11001 /*
11002 * Now a :cd command to the session directory or the current directory
11003 */
11004 if (ssop_flags & SSOP_SESDIR)
11005 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011006 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11007 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008 return FAIL;
11009 }
11010 else if (ssop_flags & SSOP_CURDIR)
11011 {
11012 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11013 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011014 || fputs("cd ", fd) < 0
11015 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11016 || put_eol(fd) == FAIL)
11017 {
11018 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 vim_free(sname);
11022 }
11023
11024 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011025 * If there is an empty, unnamed buffer we will wipe it out later.
11026 * Remember the buffer number.
11027 */
11028 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11029 return FAIL;
11030 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11031 return FAIL;
11032 if (put_line(fd, "endif") == FAIL)
11033 return FAIL;
11034
11035 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036 * Now save the current files, current buffer first.
11037 */
11038 if (put_line(fd, "set shortmess=aoO") == FAIL)
11039 return FAIL;
11040
11041 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011042 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043 {
11044 if (!(only_save_windows && buf->b_nwindows == 0)
11045 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
11046 && buf->b_fname != NULL
11047 && buf->b_p_bl)
11048 {
11049 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11050 : buf->b_wininfo->wi_fpos.lnum) < 0
11051 || ses_fname(fd, buf, &ssop_flags) == FAIL)
11052 return FAIL;
11053 }
11054 }
11055
11056 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011057 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11059 return FAIL;
11060
11061 if (ssop_flags & SSOP_RESIZE)
11062 {
11063 /* Note: after the restore we still check it worked!*/
11064 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11065 || put_eol(fd) == FAIL)
11066 return FAIL;
11067 }
11068
11069#ifdef FEAT_GUI
11070 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11071 {
11072 int x, y;
11073
11074 if (gui_mch_get_winpos(&x, &y) == OK)
11075 {
11076 /* Note: after the restore we still check it worked!*/
11077 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11078 return FAIL;
11079 }
11080 }
11081#endif
11082
11083 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011084 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11085 * will be displayed when creating the next tab. That resizes the windows
11086 * in the first tab, which may cause problems. Set 'showtabline' to 2
11087 * temporarily to avoid that.
11088 */
11089 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11090 {
11091 if (put_line(fd, "set stal=2") == FAIL)
11092 return FAIL;
11093 restore_stal = TRUE;
11094 }
11095
11096 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011097 * May repeat putting Windows for each tab, when "tabpages" is in
11098 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011099 * Don't use goto_tabpage(), it may change directory and trigger
11100 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011102 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011103 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011104 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011106 int need_tabnew = FALSE;
11107 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011108
Bram Moolenaar18144c82006-04-12 21:52:12 +000011109 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011111 tabpage_T *tp = find_tabpage(tabnr);
11112
11113 if (tp == NULL)
11114 break; /* done all tab pages */
11115 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011116 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011117 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011118 tab_topframe = topframe;
11119 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011120 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011121 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011122 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011123 tab_topframe = tp->tp_topframe;
11124 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011125 if (tabnr > 1)
11126 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128
Bram Moolenaar18144c82006-04-12 21:52:12 +000011129 /*
11130 * Before creating the window layout, try loading one file. If this
11131 * is aborted we don't end up with a number of useless windows.
11132 * This may have side effects! (e.g., compressed or network file).
11133 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011134 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011135 {
11136 if (ses_do_win(wp)
11137 && wp->w_buffer->b_ffname != NULL
11138 && !wp->w_buffer->b_help
11139#ifdef FEAT_QUICKFIX
11140 && !bt_nofile(wp->w_buffer)
11141#endif
11142 )
11143 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011144 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000011145 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
11146 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011147 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011148 if (!wp->w_arg_idx_invalid)
11149 edited_win = wp;
11150 break;
11151 }
11152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011154 /* If no file got edited create an empty tab page. */
11155 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11156 return FAIL;
11157
Bram Moolenaar18144c82006-04-12 21:52:12 +000011158 /*
11159 * Save current window layout.
11160 */
11161 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011163 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011164 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011165 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11166 return FAIL;
11167 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11168 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169
Bram Moolenaar18144c82006-04-12 21:52:12 +000011170 /*
11171 * Check if window sizes can be restored (no windows omitted).
11172 * Remember the window number of the current window after restoring.
11173 */
11174 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011175 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011176 {
11177 if (ses_do_win(wp))
11178 ++nr;
11179 else
11180 restore_size = FALSE;
11181 if (curwin == wp)
11182 cnr = nr;
11183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184
Bram Moolenaar18144c82006-04-12 21:52:12 +000011185 /* Go to the first window. */
11186 if (put_line(fd, "wincmd t") == FAIL)
11187 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011188
Bram Moolenaar18144c82006-04-12 21:52:12 +000011189 /*
11190 * If more than one window, see if sizes can be restored.
11191 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11192 * resized when moving between windows.
11193 * Do this before restoring the view, so that the topline and the
11194 * cursor can be set. This is done again below.
11195 */
11196 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
11197 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011198 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011199 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200
Bram Moolenaar18144c82006-04-12 21:52:12 +000011201 /*
11202 * Restore the view of the window (options, file, cursor, etc.).
11203 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011204 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011205 {
11206 if (!ses_do_win(wp))
11207 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011208 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11209 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011210 return FAIL;
11211 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11212 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011213 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011214 }
11215
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011216 /* The argument index in the first tab page is zero, need to set it in
11217 * each window. For further tab pages it's the window where we do
11218 * "tabedit". */
11219 cur_arg_idx = next_arg_idx;
11220
Bram Moolenaar18144c82006-04-12 21:52:12 +000011221 /*
11222 * Restore cursor to the current window if it's not the first one.
11223 */
11224 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11225 || put_eol(fd) == FAIL))
11226 return FAIL;
11227
11228 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011229 * Restore window sizes again after jumping around in windows, because
11230 * the current window has a minimum size while others may not.
11231 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011232 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011233 return FAIL;
11234
Bram Moolenaar18144c82006-04-12 21:52:12 +000011235 /* Don't continue in another tab page when doing only the current one
11236 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011237 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011238 break;
11239 }
11240
11241 if (ssop_flags & SSOP_TABPAGES)
11242 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011243 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11244 || put_eol(fd) == FAIL)
11245 return FAIL;
11246 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011247 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11248 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011249
Bram Moolenaar9c102382006-05-03 21:26:49 +000011250 /*
11251 * Wipe out an empty unnamed buffer we started in.
11252 */
11253 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11254 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011255 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011256 return FAIL;
11257 if (put_line(fd, "endif") == FAIL)
11258 return FAIL;
11259 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11260 return FAIL;
11261
11262 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11263 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11264 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11265 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266
11267 /*
11268 * Lastly, execute the x.vim file if it exists.
11269 */
11270 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11271 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011272 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273 || put_line(fd, "endif") == FAIL)
11274 return FAIL;
11275
11276 return OK;
11277}
11278
11279 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011280ses_winsizes(
11281 FILE *fd,
11282 int restore_size,
11283 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011284{
11285 int n = 0;
11286 win_T *wp;
11287
11288 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11289 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011290 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291 {
11292 if (!ses_do_win(wp))
11293 continue;
11294 ++n;
11295
11296 /* restore height when not full height */
11297 if (wp->w_height + wp->w_status_height < topframe->fr_height
11298 && (fprintf(fd,
11299 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11300 n, (long)wp->w_height, Rows / 2, Rows) < 0
11301 || put_eol(fd) == FAIL))
11302 return FAIL;
11303
11304 /* restore width when not full width */
11305 if (wp->w_width < Columns && (fprintf(fd,
11306 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11307 n, (long)wp->w_width, Columns / 2, Columns) < 0
11308 || put_eol(fd) == FAIL))
11309 return FAIL;
11310 }
11311 }
11312 else
11313 {
11314 /* Just equalise window sizes */
11315 if (put_line(fd, "wincmd =") == FAIL)
11316 return FAIL;
11317 }
11318 return OK;
11319}
11320
11321/*
11322 * Write commands to "fd" to recursively create windows for frame "fr",
11323 * horizontally and vertically split.
11324 * After the commands the last window in the frame is the current window.
11325 * Returns FAIL when writing the commands to "fd" fails.
11326 */
11327 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011328ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329{
11330 frame_T *frc;
11331 int count = 0;
11332
11333 if (fr->fr_layout != FR_LEAF)
11334 {
11335 /* Find first frame that's not skipped and then create a window for
11336 * each following one (first frame is already there). */
11337 frc = ses_skipframe(fr->fr_child);
11338 if (frc != NULL)
11339 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11340 {
11341 /* Make window as big as possible so that we have lots of room
11342 * to split. */
11343 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11344 || put_line(fd, fr->fr_layout == FR_COL
11345 ? "split" : "vsplit") == FAIL)
11346 return FAIL;
11347 ++count;
11348 }
11349
11350 /* Go back to the first window. */
11351 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11352 ? "%dwincmd k" : "%dwincmd h", count) < 0
11353 || put_eol(fd) == FAIL))
11354 return FAIL;
11355
11356 /* Recursively create frames/windows in each window of this column or
11357 * row. */
11358 frc = ses_skipframe(fr->fr_child);
11359 while (frc != NULL)
11360 {
11361 ses_win_rec(fd, frc);
11362 frc = ses_skipframe(frc->fr_next);
11363 /* Go to next window. */
11364 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11365 return FAIL;
11366 }
11367 }
11368 return OK;
11369}
11370
11371/*
11372 * Skip frames that don't contain windows we want to save in the Session.
11373 * Returns NULL when there none.
11374 */
11375 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011376ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377{
11378 frame_T *frc;
11379
11380 for (frc = fr; frc != NULL; frc = frc->fr_next)
11381 if (ses_do_frame(frc))
11382 break;
11383 return frc;
11384}
11385
11386/*
11387 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11388 * the Session.
11389 */
11390 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011391ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392{
11393 frame_T *frc;
11394
11395 if (fr->fr_layout == FR_LEAF)
11396 return ses_do_win(fr->fr_win);
11397 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11398 if (ses_do_frame(frc))
11399 return TRUE;
11400 return FALSE;
11401}
11402
11403/*
11404 * Return non-zero if window "wp" is to be stored in the Session.
11405 */
11406 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011407ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408{
11409 if (wp->w_buffer->b_fname == NULL
11410#ifdef FEAT_QUICKFIX
11411 /* When 'buftype' is "nofile" can't restore the window contents. */
11412 || bt_nofile(wp->w_buffer)
11413#endif
11414 )
11415 return (ssop_flags & SSOP_BLANK);
11416 if (wp->w_buffer->b_help)
11417 return (ssop_flags & SSOP_HELP);
11418 return TRUE;
11419}
11420
11421/*
11422 * Write commands to "fd" to restore the view of a window.
11423 * Caller must make sure 'scrolloff' is zero.
11424 */
11425 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011426put_view(
11427 FILE *fd,
11428 win_T *wp,
11429 int add_edit, /* add ":edit" command to view */
11430 unsigned *flagp, /* vop_flags or ssop_flags */
11431 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011432 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433{
11434 win_T *save_curwin;
11435 int f;
11436 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011437 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011438
11439 /* Always restore cursor position for ":mksession". For ":mkview" only
11440 * when 'viewoptions' contains "cursor". */
11441 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11442
11443 /*
11444 * Local argument list.
11445 */
11446 if (wp->w_alist == &global_alist)
11447 {
11448 if (put_line(fd, "argglobal") == FAIL)
11449 return FAIL;
11450 }
11451 else
11452 {
11453 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11454 flagp == &vop_flags
11455 || !(*flagp & SSOP_CURDIR)
11456 || wp->w_localdir != NULL, flagp) == FAIL)
11457 return FAIL;
11458 }
11459
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011460 /* Only when part of a session: restore the argument index. Some
11461 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011462 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011463 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011464 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011465 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466 || put_eol(fd) == FAIL)
11467 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011468 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469 }
11470
11471 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011472 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473 {
11474 /*
11475 * Load the file.
11476 */
11477 if (wp->w_buffer->b_ffname != NULL
11478#ifdef FEAT_QUICKFIX
11479 && !bt_nofile(wp->w_buffer)
11480#endif
11481 )
11482 {
11483 /*
11484 * Editing a file in this buffer: use ":edit file".
11485 * This may have side effects! (e.g., compressed or network file).
11486 */
11487 if (fputs("edit ", fd) < 0
11488 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11489 return FAIL;
11490 }
11491 else
11492 {
11493 /* No file in this buffer, just make it empty. */
11494 if (put_line(fd, "enew") == FAIL)
11495 return FAIL;
11496#ifdef FEAT_QUICKFIX
11497 if (wp->w_buffer->b_ffname != NULL)
11498 {
11499 /* The buffer does have a name, but it's not a file name. */
11500 if (fputs("file ", fd) < 0
11501 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11502 return FAIL;
11503 }
11504#endif
11505 do_cursor = FALSE;
11506 }
11507 }
11508
11509 /*
11510 * Local mappings and abbreviations.
11511 */
11512 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11513 && makemap(fd, wp->w_buffer) == FAIL)
11514 return FAIL;
11515
11516 /*
11517 * Local options. Need to go to the window temporarily.
11518 * Store only local values when using ":mkview" and when ":mksession" is
11519 * used and 'sessionoptions' doesn't include "options".
11520 * Some folding options are always stored when "folds" is included,
11521 * otherwise the folds would not be restored correctly.
11522 */
11523 save_curwin = curwin;
11524 curwin = wp;
11525 curbuf = curwin->w_buffer;
11526 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11527 f = makeset(fd, OPT_LOCAL,
11528 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11529#ifdef FEAT_FOLDING
11530 else if (*flagp & SSOP_FOLDS)
11531 f = makefoldset(fd);
11532#endif
11533 else
11534 f = OK;
11535 curwin = save_curwin;
11536 curbuf = curwin->w_buffer;
11537 if (f == FAIL)
11538 return FAIL;
11539
11540#ifdef FEAT_FOLDING
11541 /*
11542 * Save Folds when 'buftype' is empty and for help files.
11543 */
11544 if ((*flagp & SSOP_FOLDS)
11545 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011546# ifdef FEAT_QUICKFIX
11547 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11548# endif
11549 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550 {
11551 if (put_folds(fd, wp) == FAIL)
11552 return FAIL;
11553 }
11554#endif
11555
11556 /*
11557 * Set the cursor after creating folds, since that moves the cursor.
11558 */
11559 if (do_cursor)
11560 {
11561
11562 /* Restore the cursor line in the file and relatively in the
11563 * window. Don't use "G", it changes the jumplist. */
11564 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11565 (long)wp->w_cursor.lnum,
11566 (long)(wp->w_cursor.lnum - wp->w_topline),
11567 (long)wp->w_height / 2, (long)wp->w_height) < 0
11568 || put_eol(fd) == FAIL
11569 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11570 || put_line(fd, "exe s:l") == FAIL
11571 || put_line(fd, "normal! zt") == FAIL
11572 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11573 || put_eol(fd) == FAIL)
11574 return FAIL;
11575 /* Restore the cursor column and left offset when not wrapping. */
11576 if (wp->w_cursor.col == 0)
11577 {
11578 if (put_line(fd, "normal! 0") == FAIL)
11579 return FAIL;
11580 }
11581 else
11582 {
11583 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11584 {
11585 if (fprintf(fd,
11586 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011587 (long)wp->w_virtcol + 1,
11588 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011589 (long)wp->w_width / 2, (long)wp->w_width) < 0
11590 || put_eol(fd) == FAIL
11591 || put_line(fd, "if s:c > 0") == FAIL
11592 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011593 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11594 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011595 || put_eol(fd) == FAIL
11596 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011597 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011598 || put_eol(fd) == FAIL
11599 || put_line(fd, "endif") == FAIL)
11600 return FAIL;
11601 }
11602 else
11603 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011604 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011605 || put_eol(fd) == FAIL)
11606 return FAIL;
11607 }
11608 }
11609 }
11610
11611 /*
11612 * Local directory.
11613 */
11614 if (wp->w_localdir != NULL)
11615 {
11616 if (fputs("lcd ", fd) < 0
11617 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11618 || put_eol(fd) == FAIL)
11619 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011620 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011621 }
11622
11623 return OK;
11624}
11625
11626/*
11627 * Write an argument list to the session file.
11628 * Returns FAIL if writing fails.
11629 */
11630 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011631ses_arglist(
11632 FILE *fd,
11633 char *cmd,
11634 garray_T *gap,
11635 int fullname, /* TRUE: use full path name */
11636 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011637{
11638 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011639 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011640 char_u *s;
11641
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011642 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11643 return FAIL;
11644 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011645 return FAIL;
11646 for (i = 0; i < gap->ga_len; ++i)
11647 {
11648 /* NULL file names are skipped (only happens when out of memory). */
11649 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11650 if (s != NULL)
11651 {
11652 if (fullname)
11653 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011654 buf = alloc(MAXPATHL);
11655 if (buf != NULL)
11656 {
11657 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11658 s = buf;
11659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011660 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011661 if (fputs("argadd ", fd) < 0
11662 || ses_put_fname(fd, s, flagp) == FAIL
11663 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011664 {
11665 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011666 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011667 }
11668 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011669 }
11670 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011671 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011672}
11673
11674/*
11675 * Write a buffer name to the session file.
11676 * Also ends the line.
11677 * Returns FAIL if writing fails.
11678 */
11679 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011680ses_fname(FILE *fd, buf_T *buf, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011681{
11682 char_u *name;
11683
11684 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011685 * the session file will be sourced.
11686 * Don't do this for ":mkview", we don't know the current directory.
11687 * Don't do this after ":lcd", we don't keep track of what the current
11688 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011689 if (buf->b_sfname != NULL
11690 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011691 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011692#ifdef FEAT_AUTOCHDIR
11693 && !p_acd
11694#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011695 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696 name = buf->b_sfname;
11697 else
11698 name = buf->b_ffname;
11699 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11700 return FAIL;
11701 return OK;
11702}
11703
11704/*
11705 * Write a file name to the session file.
11706 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11707 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011708 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709 */
11710 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011711ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011712{
11713 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011714 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011715 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011716
11717 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011718 if (sname == NULL)
11719 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011720
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011721 if (*flagp & SSOP_SLASH)
11722 {
11723 /* change all backslashes to forward slashes */
11724 for (p = sname; *p != NUL; mb_ptr_adv(p))
11725 if (*p == '\\')
11726 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011728
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011729 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011730 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011732 if (p == NULL)
11733 return FAIL;
11734
11735 /* write the result */
11736 if (fputs((char *)p, fd) < 0)
11737 retval = FAIL;
11738
11739 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011740 return retval;
11741}
11742
11743/*
11744 * ":loadview [nr]"
11745 */
11746 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011747ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011748{
11749 char_u *fname;
11750
11751 fname = get_view_file(*eap->arg);
11752 if (fname != NULL)
11753 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011754 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011755 vim_free(fname);
11756 }
11757}
11758
11759/*
11760 * Get the name of the view file for the current buffer.
11761 */
11762 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011763get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011764{
11765 int len = 0;
11766 char_u *p, *s;
11767 char_u *retval;
11768 char_u *sname;
11769
11770 if (curbuf->b_ffname == NULL)
11771 {
11772 EMSG(_(e_noname));
11773 return NULL;
11774 }
11775 sname = home_replace_save(NULL, curbuf->b_ffname);
11776 if (sname == NULL)
11777 return NULL;
11778
11779 /*
11780 * We want a file name without separators, because we're not going to make
11781 * a directory.
11782 * "normal" path separator -> "=+"
11783 * "=" -> "=="
11784 * ":" path separator -> "=-"
11785 */
11786 for (p = sname; *p; ++p)
11787 if (*p == '=' || vim_ispathsep(*p))
11788 ++len;
11789 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11790 if (retval != NULL)
11791 {
11792 STRCPY(retval, p_vdir);
11793 add_pathsep(retval);
11794 s = retval + STRLEN(retval);
11795 for (p = sname; *p; ++p)
11796 {
11797 if (*p == '=')
11798 {
11799 *s++ = '=';
11800 *s++ = '=';
11801 }
11802 else if (vim_ispathsep(*p))
11803 {
11804 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011805#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806 if (*p == ':')
11807 *s++ = '-';
11808 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011810 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811 }
11812 else
11813 *s++ = *p;
11814 }
11815 *s++ = '=';
11816 *s++ = c;
11817 STRCPY(s, ".vim");
11818 }
11819
11820 vim_free(sname);
11821 return retval;
11822}
11823
11824#endif /* FEAT_SESSION */
11825
11826/*
11827 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11828 * Return FAIL for a write error.
11829 */
11830 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011831put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011832{
11833 if (
11834#ifdef USE_CRNL
11835 (
11836# ifdef MKSESSION_NL
11837 !mksession_nl &&
11838# endif
11839 (putc('\r', fd) < 0)) ||
11840#endif
11841 (putc('\n', fd) < 0))
11842 return FAIL;
11843 return OK;
11844}
11845
11846/*
11847 * Write a line to "fd".
11848 * Return FAIL for a write error.
11849 */
11850 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011851put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852{
11853 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11854 return FAIL;
11855 return OK;
11856}
11857
11858#ifdef FEAT_VIMINFO
11859/*
11860 * ":rviminfo" and ":wviminfo".
11861 */
11862 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011863ex_viminfo(
11864 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865{
11866 char_u *save_viminfo;
11867
11868 save_viminfo = p_viminfo;
11869 if (*p_viminfo == NUL)
11870 p_viminfo = (char_u *)"'100";
11871 if (eap->cmdidx == CMD_rviminfo)
11872 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011873 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11874 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011875 EMSG(_("E195: Cannot open viminfo file for reading"));
11876 }
11877 else
11878 write_viminfo(eap->arg, eap->forceit);
11879 p_viminfo = save_viminfo;
11880}
11881#endif
11882
11883#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011884/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011885 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011886 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011887 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011888 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011889dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011890{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011891 if (fname == NULL)
11892 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011893 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011894}
11895#endif
11896
11897/*
11898 * ":behave {mswin,xterm}"
11899 */
11900 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011901ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011902{
11903 if (STRCMP(eap->arg, "mswin") == 0)
11904 {
11905 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11906 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11907 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11908 set_option_value((char_u *)"keymodel", 0L,
11909 (char_u *)"startsel,stopsel", 0);
11910 }
11911 else if (STRCMP(eap->arg, "xterm") == 0)
11912 {
11913 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11914 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11915 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11916 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11917 }
11918 else
11919 EMSG2(_(e_invarg2), eap->arg);
11920}
11921
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011922#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11923/*
11924 * Function given to ExpandGeneric() to obtain the possible arguments of the
11925 * ":behave {mswin,xterm}" command.
11926 */
11927 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011928get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011929{
11930 if (idx == 0)
11931 return (char_u *)"mswin";
11932 if (idx == 1)
11933 return (char_u *)"xterm";
11934 return NULL;
11935}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020011936
11937/*
11938 * Function given to ExpandGeneric() to obtain the possible arguments of the
11939 * ":messages {clear}" command.
11940 */
11941 char_u *
11942get_messages_arg(expand_T *xp UNUSED, int idx)
11943{
11944 if (idx == 0)
11945 return (char_u *)"clear";
11946 return NULL;
11947}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011948#endif
11949
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950#ifdef FEAT_AUTOCMD
11951static int filetype_detect = FALSE;
11952static int filetype_plugin = FALSE;
11953static int filetype_indent = FALSE;
11954
11955/*
11956 * ":filetype [plugin] [indent] {on,off,detect}"
11957 * on: Load the filetype.vim file to install autocommands for file types.
11958 * off: Load the ftoff.vim file to remove all autocommands for file types.
11959 * plugin on: load filetype.vim and ftplugin.vim
11960 * plugin off: load ftplugof.vim
11961 * indent on: load filetype.vim and indent.vim
11962 * indent off: load indoff.vim
11963 */
11964 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011965ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011966{
11967 char_u *arg = eap->arg;
11968 int plugin = FALSE;
11969 int indent = FALSE;
11970
11971 if (*eap->arg == NUL)
11972 {
11973 /* Print current status. */
11974 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11975 filetype_detect ? "ON" : "OFF",
11976 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11977 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11978 return;
11979 }
11980
11981 /* Accept "plugin" and "indent" in any order. */
11982 for (;;)
11983 {
11984 if (STRNCMP(arg, "plugin", 6) == 0)
11985 {
11986 plugin = TRUE;
11987 arg = skipwhite(arg + 6);
11988 continue;
11989 }
11990 if (STRNCMP(arg, "indent", 6) == 0)
11991 {
11992 indent = TRUE;
11993 arg = skipwhite(arg + 6);
11994 continue;
11995 }
11996 break;
11997 }
11998 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11999 {
12000 if (*arg == 'o' || !filetype_detect)
12001 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012002 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003 filetype_detect = TRUE;
12004 if (plugin)
12005 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012006 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012007 filetype_plugin = TRUE;
12008 }
12009 if (indent)
12010 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012011 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012012 filetype_indent = TRUE;
12013 }
12014 }
12015 if (*arg == 'd')
12016 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012017 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012018 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019 }
12020 }
12021 else if (STRCMP(arg, "off") == 0)
12022 {
12023 if (plugin || indent)
12024 {
12025 if (plugin)
12026 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012027 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012028 filetype_plugin = FALSE;
12029 }
12030 if (indent)
12031 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012032 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033 filetype_indent = FALSE;
12034 }
12035 }
12036 else
12037 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012038 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012039 filetype_detect = FALSE;
12040 }
12041 }
12042 else
12043 EMSG2(_(e_invarg2), arg);
12044}
12045
12046/*
12047 * ":setfiletype {name}"
12048 */
12049 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012050ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051{
12052 if (!did_filetype)
12053 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
12054}
12055#endif
12056
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012058ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059{
12060#ifdef FEAT_DIGRAPHS
12061 if (*eap->arg != NUL)
12062 putdigraph(eap->arg);
12063 else
12064 listdigraphs();
12065#else
12066 EMSG(_("E196: No digraphs in this version"));
12067#endif
12068}
12069
12070 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012071ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012072{
12073 int flags = 0;
12074
12075 if (eap->cmdidx == CMD_setlocal)
12076 flags = OPT_LOCAL;
12077 else if (eap->cmdidx == CMD_setglobal)
12078 flags = OPT_GLOBAL;
12079#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
12080 if (cmdmod.browse && flags == 0)
12081 ex_options(eap);
12082 else
12083#endif
12084 (void)do_set(eap->arg, flags);
12085}
12086
12087#ifdef FEAT_SEARCH_EXTRA
12088/*
12089 * ":nohlsearch"
12090 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012092ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012093{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010012094 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012095 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012096}
12097
12098/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012099 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100 * Sets nextcmd to the start of the next command, if any. Also called when
12101 * skipping commands to find the next command.
12102 */
12103 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012104ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012105{
12106 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012107 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012108 char_u *end;
12109 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012110 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012111
12112 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012113 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012114 else
12115 {
12116 EMSG(e_invcmd);
12117 return;
12118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119
12120 /* First clear any old pattern. */
12121 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012122 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012123
12124 if (ends_excmd(*eap->arg))
12125 end = eap->arg;
12126 else if ((STRNICMP(eap->arg, "none", 4) == 0
12127 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
12128 end = eap->arg + 4;
12129 else
12130 {
12131 p = skiptowhite(eap->arg);
12132 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012133 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012134 p = skipwhite(p);
12135 if (*p == NUL)
12136 {
12137 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012138 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012139 EMSG2(_(e_invarg2), eap->arg);
12140 return;
12141 }
12142 end = skip_regexp(p + 1, *p, TRUE, NULL);
12143 if (!eap->skip)
12144 {
12145 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12146 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012147 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012148 eap->errmsg = e_trailing;
12149 return;
12150 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012151 if (*end != *p)
12152 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012153 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012154 EMSG2(_(e_invarg2), p);
12155 return;
12156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157
12158 c = *end;
12159 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012160 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012161 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012162 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012163 }
12164 }
12165 eap->nextcmd = find_nextcmd(end);
12166}
12167#endif
12168
12169#ifdef FEAT_CRYPT
12170/*
12171 * ":X": Get crypt key
12172 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012173 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012174ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012175{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012176 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012177 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012178}
12179#endif
12180
12181#ifdef FEAT_FOLDING
12182 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012183ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184{
12185 if (foldManualAllowed(TRUE))
12186 foldCreate(eap->line1, eap->line2);
12187}
12188
12189 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012190ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012191{
12192 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12193 eap->forceit, FALSE);
12194}
12195
12196 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012197ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198{
12199 linenr_T lnum;
12200
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012201#ifdef FEAT_CLIPBOARD
12202 start_global_changes();
12203#endif
12204
Bram Moolenaar071d4272004-06-13 20:20:40 +000012205 /* First set the marks for all lines closed/open. */
12206 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12207 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12208 ml_setmarked(lnum);
12209
12210 /* Execute the command on the marked lines. */
12211 global_exe(eap->arg);
12212 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012213#ifdef FEAT_CLIPBOARD
12214 end_global_changes();
12215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012216}
12217#endif