blob: 1af60abd28b7b4f794c335a2a49c9a70550a0322 [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
790 * here. The value of 200 allows nested function calls, ":source", etc. */
791 if (call_depth == 200)
792 {
793 EMSG(_("E169: Command too recursive"));
794#ifdef FEAT_EVAL
795 /* When converting to an exception, we do not include the command name
796 * since this is not an error of the specific command. */
797 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
798 msg_list = saved_msg_list;
799#endif
800 return FAIL;
801 }
802 ++call_depth;
803
804#ifdef FEAT_EVAL
805 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000806 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 cstack.cs_trylevel = 0;
808 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000809 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
811
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100812 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
814 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100815 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000816 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 ++ex_nesting_level;
818
819 /* Get the function or script name and the address where the next breakpoint
820 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000821 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 {
823 fname = func_name(real_cookie);
824 breakpoint = func_breakpoint(real_cookie);
825 dbg_tick = func_dbg_tick(real_cookie);
826 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100827 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 {
829 fname = sourcing_name;
830 breakpoint = source_breakpoint(real_cookie);
831 dbg_tick = source_dbg_tick(real_cookie);
832 }
833
834 /*
835 * Initialize "force_abort" and "suppress_errthrow" at the top level.
836 */
837 if (!recursive)
838 {
839 force_abort = FALSE;
840 suppress_errthrow = FALSE;
841 }
842
843 /*
844 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000845 * exception handling (used when debugging). Otherwise clear it to avoid
846 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000848 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000849 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000850 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200851 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852
853 initial_trylevel = trylevel;
854
855 /*
856 * "did_throw" will be set to TRUE when an exception is being thrown.
857 */
858 did_throw = FALSE;
859#endif
860 /*
861 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000862 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 * If force_abort is set, we cancel everything.
864 */
865 did_emsg = FALSE;
866
867 /*
868 * KeyTyped is only set when calling vgetc(). Reset it here when not
869 * calling vgetc() (sourced command lines).
870 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100871 if (!(flags & DOCMD_KEYTYPED)
872 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 KeyTyped = FALSE;
874
875 /*
876 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000877 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 * - for multiple commands on one line, separated with '|'
879 * - when repeating until there are no more lines (for ":source")
880 */
881 next_cmdline = cmdline;
882 do
883 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000884#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100885 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000886#endif
887
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000888 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (next_cmdline == NULL
890#ifdef FEAT_EVAL
891 && !force_abort
892 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000893 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894#endif
895 )
896 did_emsg = FALSE;
897
898 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000899 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100900 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 * 3. If a line is given: Make a copy, so we can mess with it.
902 */
903
904#ifdef FEAT_EVAL
905 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000906 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 {
908 /* Each '|' separated command is stored separately in lines_ga, to
909 * be able to jump to it. Don't use next_cmdline now. */
910 vim_free(cmdline_copy);
911 cmdline_copy = NULL;
912
913 /* Check if a function has returned or, unless it has an unclosed
914 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000915 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000917# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000918 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000919 func_line_end(real_cookie);
920# endif
921 if (func_has_ended(real_cookie))
922 {
923 retval = FAIL;
924 break;
925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000927#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000928 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100929 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000930 script_line_end();
931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932
933 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100934 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 {
936 retval = FAIL;
937 break;
938 }
939
940 /* If breakpoints have been added/deleted need to check for it. */
941 if (breakpoint != NULL && dbg_tick != NULL
942 && *dbg_tick != debug_tick)
943 {
944 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100945 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 fname, sourcing_lnum);
947 *dbg_tick = debug_tick;
948 }
949
950 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
951 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
952
953 /* Did we encounter a breakpoint? */
954 if (breakpoint != NULL && *breakpoint != 0
955 && *breakpoint <= sourcing_lnum)
956 {
957 dbg_breakpoint(fname, sourcing_lnum);
958 /* Find next breakpoint. */
959 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100960 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 fname, sourcing_lnum);
962 *dbg_tick = debug_tick;
963 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000964# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000965 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000966 {
967 if (getline_is_func)
968 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100969 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000970 script_line_start();
971 }
972# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 }
974
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000975 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000977 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100978 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 * below, so that it stores lines in or reads them from
980 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000981 * while/for loop. */
982 cmd_getline = get_loop_line;
983 cmd_cookie = (void *)&cmd_loop_cookie;
984 cmd_loop_cookie.lines_gap = &lines_ga;
985 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100986 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000987 cmd_loop_cookie.cookie = cookie;
988 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 }
990 else
991 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100992 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 cmd_cookie = cookie;
994 }
995#endif
996
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100997 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 if (next_cmdline == NULL)
999 {
1000 /*
1001 * Need to set msg_didout for the first line after an ":if",
1002 * otherwise the ":if" will be overwritten.
1003 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001004 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001006 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007#ifdef FEAT_EVAL
1008 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1009#else
1010 0
1011#endif
1012 )) == NULL)
1013 {
1014 /* Don't call wait_return for aborted command line. The NULL
1015 * returned for the end of a sourced file or executed function
1016 * doesn't do this. */
1017 if (KeyTyped && !(flags & DOCMD_REPEAT))
1018 need_wait_return = FALSE;
1019 retval = FAIL;
1020 break;
1021 }
1022 used_getline = TRUE;
1023
1024 /*
1025 * Keep the first typed line. Clear it when more lines are typed.
1026 */
1027 if (flags & DOCMD_KEEPLINE)
1028 {
1029 vim_free(repeat_cmdline);
1030 if (count == 0)
1031 repeat_cmdline = vim_strsave(next_cmdline);
1032 else
1033 repeat_cmdline = NULL;
1034 }
1035 }
1036
1037 /* 3. Make a copy of the command so we can mess with it. */
1038 else if (cmdline_copy == NULL)
1039 {
1040 next_cmdline = vim_strsave(next_cmdline);
1041 if (next_cmdline == NULL)
1042 {
1043 EMSG(_(e_outofmem));
1044 retval = FAIL;
1045 break;
1046 }
1047 }
1048 cmdline_copy = next_cmdline;
1049
1050#ifdef FEAT_EVAL
1051 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001052 * Save the current line when inside a ":while" or ":for", and when
1053 * the command looks like a ":while" or ":for", because we may need it
1054 * later. When there is a '|' and another command, it is stored
1055 * separately, because we need to be able to jump back to it from an
1056 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001058 if (current_line == lines_ga.ga_len
1059 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001061 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 {
1063 retval = FAIL;
1064 break;
1065 }
1066 }
1067 did_endif = FALSE;
1068#endif
1069
1070 if (count++ == 0)
1071 {
1072 /*
1073 * All output from the commands is put below each other, without
1074 * waiting for a return. Don't do this when executing commands
1075 * from a script or when being called recursive (e.g. for ":e
1076 * +command file").
1077 */
1078 if (!(flags & DOCMD_NOWAIT) && !recursive)
1079 {
1080 msg_didout_before_start = msg_didout;
1081 msg_didany = FALSE; /* no output yet */
1082 msg_start();
1083 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001084 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 ++RedrawingDisabled;
1086 did_inc = TRUE;
1087 }
1088 }
1089
1090 if (p_verbose >= 15 && sourcing_name != NULL)
1091 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001093 verbose_enter_scroll();
1094
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 smsg((char_u *)_("line %ld: %s"),
1096 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001097 if (msg_silent == 0)
1098 msg_puts((char_u *)"\n"); /* don't overwrite this */
1099
1100 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 --no_wait_return;
1102 }
1103
1104 /*
1105 * 2. Execute one '|' separated command.
1106 * do_one_cmd() will return NULL if there is no trailing '|'.
1107 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1108 */
1109 ++recursive;
1110 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1111#ifdef FEAT_EVAL
1112 &cstack,
1113#endif
1114 cmd_getline, cmd_cookie);
1115 --recursive;
1116
1117#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001118 if (cmd_cookie == (void *)&cmd_loop_cookie)
1119 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001121 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122#endif
1123
1124 if (next_cmdline == NULL)
1125 {
1126 vim_free(cmdline_copy);
1127 cmdline_copy = NULL;
1128#ifdef FEAT_CMDHIST
1129 /*
1130 * If the command was typed, remember it for the ':' register.
1131 * Do this AFTER executing the command to make :@: work.
1132 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001133 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 && new_last_cmdline != NULL)
1135 {
1136 vim_free(last_cmdline);
1137 last_cmdline = new_last_cmdline;
1138 new_last_cmdline = NULL;
1139 }
1140#endif
1141 }
1142 else
1143 {
1144 /* need to copy the command after the '|' to cmdline_copy, for the
1145 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001146 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 next_cmdline = cmdline_copy;
1148 }
1149
1150
1151#ifdef FEAT_EVAL
1152 /* reset did_emsg for a function that is not aborted by an error */
1153 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001154 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 && !func_has_abort(real_cookie))
1156 did_emsg = FALSE;
1157
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001158 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 {
1160 ++current_line;
1161
1162 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001163 * An ":endwhile", ":endfor" and ":continue" is handled here.
1164 * If we were executing commands, jump back to the ":while" or
1165 * ":for".
1166 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001168 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001170 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001172 /* Jump back to the matching ":while" or ":for". Be careful
1173 * not to use a cs_line[] from an entry that isn't a ":while"
1174 * or ":for": It would make "current_line" invalid and can
1175 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 if (!did_emsg && !got_int && !did_throw
1177 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001178 && (cstack.cs_flags[cstack.cs_idx]
1179 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 && cstack.cs_line[cstack.cs_idx] >= 0
1181 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1182 {
1183 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001184 /* remember we jumped there */
1185 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 line_breakcheck(); /* check if CTRL-C typed */
1187
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001188 /* Check for the next breakpoint at or after the ":while"
1189 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 if (breakpoint != NULL)
1191 {
1192 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001193 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 fname,
1195 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1196 *dbg_tick = debug_tick;
1197 }
1198 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001199 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001201 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001203 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1204 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 }
1206 }
1207
1208 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001209 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001211 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001213 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1215 }
1216 }
1217
1218 /*
1219 * When not inside any ":while" loop, clear remembered lines.
1220 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001221 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 {
1223 if (lines_ga.ga_len > 0)
1224 {
1225 sourcing_lnum =
1226 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1227 free_cmdlines(&lines_ga);
1228 }
1229 current_line = 0;
1230 }
1231
1232 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001233 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1234 * being restored at the ":endtry". Reset them here and set the
1235 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1236 * This includes the case where a missing ":endif", ":endwhile" or
1237 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001239 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001241 cstack.cs_lflags &= ~CSL_HAD_FINA;
1242 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1243 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 did_throw ? (void *)current_exception : NULL);
1245 did_emsg = got_int = did_throw = FALSE;
1246 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1247 }
1248
1249 /* Update global "trylevel" for recursive calls to do_cmdline() from
1250 * within this loop. */
1251 trylevel = initial_trylevel + cstack.cs_trylevel;
1252
1253 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001254 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 * files) is aborted because of an error, an interrupt, or an uncaught
1256 * exception, cancel everything. If it is left normally, reset
1257 * force_abort to get the non-EH compatible abortion behavior for
1258 * the rest of the script.
1259 */
1260 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1261 force_abort = FALSE;
1262
1263 /* Convert an interrupt to an exception if appropriate. */
1264 (void)do_intthrow(&cstack);
1265#endif /* FEAT_EVAL */
1266
1267 }
1268 /*
1269 * Continue executing command lines when:
1270 * - no CTRL-C typed, no aborting error, no exception thrown or try
1271 * conditionals need to be checked for executing finally clauses or
1272 * catching an interrupt exception
1273 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001274 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 * looping for ":source" command or function call.
1276 */
1277 while (!((got_int
1278#ifdef FEAT_EVAL
1279 || (did_emsg && force_abort) || did_throw
1280#endif
1281 )
1282#ifdef FEAT_EVAL
1283 && cstack.cs_trylevel == 0
1284#endif
1285 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001286 && !(did_emsg
1287#ifdef FEAT_EVAL
1288 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001289 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001290 * the :endtry to be missed. */
1291 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1292#endif
1293 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001294 && (getline_equal(fgetline, cookie, getexmodeline)
1295 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 && (next_cmdline != NULL
1297#ifdef FEAT_EVAL
1298 || cstack.cs_idx >= 0
1299#endif
1300 || (flags & DOCMD_REPEAT)));
1301
1302 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001303 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304#ifdef FEAT_EVAL
1305 free_cmdlines(&lines_ga);
1306 ga_clear(&lines_ga);
1307
1308 if (cstack.cs_idx >= 0)
1309 {
1310 /*
1311 * If a sourced file or executed function ran to its end, report the
1312 * unclosed conditional.
1313 */
1314 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001315 && ((getline_equal(fgetline, cookie, getsourceline)
1316 && !source_finished(fgetline, cookie))
1317 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 && !func_has_ended(real_cookie))))
1319 {
1320 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1321 EMSG(_(e_endtry));
1322 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1323 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001324 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1325 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 else
1327 EMSG(_(e_endif));
1328 }
1329
1330 /*
1331 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1332 * ":endtry" in a sourced file or executed function. If the try
1333 * conditional is in its finally clause, ignore anything pending.
1334 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001335 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 */
1337 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001338 {
1339 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1340
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001341 if (idx >= 0)
1342 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001343 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1344 &cstack.cs_looplevel);
1345 }
1346 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 trylevel = initial_trylevel;
1348 }
1349
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001350 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1351 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001353 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 ? (char_u *)"endfunction" : (char_u *)NULL);
1355
1356 if (trylevel == 0)
1357 {
1358 /*
1359 * When an exception is being thrown out of the outermost try
1360 * conditional, discard the uncaught exception, disable the conversion
1361 * of interrupts or errors to exceptions, and ensure that no more
1362 * commands are executed.
1363 */
1364 if (did_throw)
1365 {
1366 void *p = NULL;
1367 char_u *saved_sourcing_name;
1368 int saved_sourcing_lnum;
1369 struct msglist *messages = NULL, *next;
1370
1371 /*
1372 * If the uncaught exception is a user exception, report it as an
1373 * error. If it is an error exception, display the saved error
1374 * message now. For an interrupt exception, do nothing; the
1375 * interrupt message is given elsewhere.
1376 */
1377 switch (current_exception->type)
1378 {
1379 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001380 vim_snprintf((char *)IObuff, IOSIZE,
1381 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 current_exception->value);
1383 p = vim_strsave(IObuff);
1384 break;
1385 case ET_ERROR:
1386 messages = current_exception->messages;
1387 current_exception->messages = NULL;
1388 break;
1389 case ET_INTERRUPT:
1390 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 }
1392
1393 saved_sourcing_name = sourcing_name;
1394 saved_sourcing_lnum = sourcing_lnum;
1395 sourcing_name = current_exception->throw_name;
1396 sourcing_lnum = current_exception->throw_lnum;
1397 current_exception->throw_name = NULL;
1398
1399 discard_current_exception(); /* uses IObuff if 'verbose' */
1400 suppress_errthrow = TRUE;
1401 force_abort = TRUE;
1402
1403 if (messages != NULL)
1404 {
1405 do
1406 {
1407 next = messages->next;
1408 emsg(messages->msg);
1409 vim_free(messages->msg);
1410 vim_free(messages);
1411 messages = next;
1412 }
1413 while (messages != NULL);
1414 }
1415 else if (p != NULL)
1416 {
1417 emsg(p);
1418 vim_free(p);
1419 }
1420 vim_free(sourcing_name);
1421 sourcing_name = saved_sourcing_name;
1422 sourcing_lnum = saved_sourcing_lnum;
1423 }
1424
1425 /*
1426 * On an interrupt or an aborting error not converted to an exception,
1427 * disable the conversion of errors to exceptions. (Interrupts are not
1428 * converted any more, here.) This enables also the interrupt message
1429 * when force_abort is set and did_emsg unset in case of an interrupt
1430 * from a finally clause after an error.
1431 */
1432 else if (got_int || (did_emsg && force_abort))
1433 suppress_errthrow = TRUE;
1434 }
1435
1436 /*
1437 * The current cstack will be freed when do_cmdline() returns. An uncaught
1438 * exception will have to be rethrown in the previous cstack. If a function
1439 * has just returned or a script file was just finished and the previous
1440 * cstack belongs to the same function or, respectively, script file, it
1441 * will have to be checked for finally clauses to be executed due to the
1442 * ":return" or ":finish". This is done in do_one_cmd().
1443 */
1444 if (did_throw)
1445 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001446 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001448 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 && ex_nesting_level > func_level(real_cookie) + 1))
1450 {
1451 if (!did_throw)
1452 check_cstack = TRUE;
1453 }
1454 else
1455 {
1456 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001457 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 --ex_nesting_level;
1459 /*
1460 * Go to debug mode when returning from a function in which we are
1461 * single-stepping.
1462 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001463 if ((getline_equal(fgetline, cookie, getsourceline)
1464 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001466 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 ? (char_u *)_("End of sourced file")
1468 : (char_u *)_("End of function"));
1469 }
1470
1471 /*
1472 * Restore the exception environment (done after returning from the
1473 * debugger).
1474 */
1475 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001476 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477
1478 msg_list = saved_msg_list;
1479#endif /* FEAT_EVAL */
1480
1481 /*
1482 * If there was too much output to fit on the command line, ask the user to
1483 * hit return before redrawing the screen. With the ":global" command we do
1484 * this only once after the command is finished.
1485 */
1486 if (did_inc)
1487 {
1488 --RedrawingDisabled;
1489 --no_wait_return;
1490 msg_scroll = FALSE;
1491
1492 /*
1493 * When just finished an ":if"-":else" which was typed, no need to
1494 * wait for hit-return. Also for an error situation.
1495 */
1496 if (retval == FAIL
1497#ifdef FEAT_EVAL
1498 || (did_endif && KeyTyped && !did_emsg)
1499#endif
1500 )
1501 {
1502 need_wait_return = FALSE;
1503 msg_didany = FALSE; /* don't wait when restarting edit */
1504 }
1505 else if (need_wait_return)
1506 {
1507 /*
1508 * The msg_start() above clears msg_didout. The wait_return we do
1509 * here should not overwrite the command that may be shown before
1510 * doing that.
1511 */
1512 msg_didout |= msg_didout_before_start;
1513 wait_return(FALSE);
1514 }
1515 }
1516
Bram Moolenaar2a942252012-11-28 23:03:07 +01001517#ifdef FEAT_EVAL
1518 did_endif = FALSE; /* in case do_cmdline used recursively */
1519#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 /*
1521 * Reset if_level, in case a sourced script file contains more ":if" than
1522 * ":endif" (could be ":if x | foo | endif").
1523 */
1524 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001525#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001526
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 --call_depth;
1528 return retval;
1529}
1530
1531#ifdef FEAT_EVAL
1532/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001533 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 */
1535 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001536get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001538 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 wcmd_T *wp;
1540 char_u *line;
1541
1542 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1543 {
1544 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001545 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001547 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 if (cp->getline == NULL)
1549 line = getcmdline(c, 0L, indent);
1550 else
1551 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001552 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 ++cp->current_line;
1554
1555 return line;
1556 }
1557
1558 KeyTyped = FALSE;
1559 ++cp->current_line;
1560 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1561 sourcing_lnum = wp->lnum;
1562 return vim_strsave(wp->line);
1563}
1564
1565/*
1566 * Store a line in "gap" so that a ":while" loop can execute it again.
1567 */
1568 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001569store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570{
1571 if (ga_grow(gap, 1) == FAIL)
1572 return FAIL;
1573 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1574 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1575 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 return OK;
1577}
1578
1579/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001580 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 */
1582 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001583free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584{
1585 while (gap->ga_len > 0)
1586 {
1587 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1588 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 }
1590}
1591#endif
1592
1593/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001594 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1595 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001598getline_equal(
1599 char_u *(*fgetline)(int, void *, int),
1600 void *cookie UNUSED, /* argument for fgetline() */
1601 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602{
1603#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001604 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001605 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606
Bram Moolenaar89d40322006-08-29 15:30:07 +00001607 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001608 * function that's originally used to obtain the lines. This may be
1609 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001610 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001611 cp = (struct loop_cookie *)cookie;
1612 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 {
1614 gp = cp->getline;
1615 cp = cp->cookie;
1616 }
1617 return gp == func;
1618#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001619 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620#endif
1621}
1622
1623#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1624/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001625 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 * getline function. Otherwise return "cookie".
1627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001629getline_cookie(
1630 char_u *(*fgetline)(int, void *, int) UNUSED,
1631 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632{
1633# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001634 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001635 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
Bram Moolenaar89d40322006-08-29 15:30:07 +00001637 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001638 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001640 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001641 cp = (struct loop_cookie *)cookie;
1642 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 {
1644 gp = cp->getline;
1645 cp = cp->cookie;
1646 }
1647 return cp;
1648# else
1649 return cookie;
1650# endif
1651}
1652#endif
1653
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001654
1655/*
1656 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1657 * ":bwipeout", etc.
1658 * Returns the buffer number.
1659 */
1660 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001661compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001662{
1663 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001664 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001665 int count = offset;
1666
1667 buf = firstbuf;
1668 while (buf->b_next != NULL && buf->b_fnum < lnum)
1669 buf = buf->b_next;
1670 while (count != 0)
1671 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001672 count += (offset < 0) ? 1 : -1;
1673 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1674 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001675 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001676 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001677 if (addr_type == ADDR_LOADED_BUFFERS)
1678 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001679 while (buf->b_ml.ml_mfp == NULL)
1680 {
1681 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1682 if (nextbuf == NULL)
1683 break;
1684 buf = nextbuf;
1685 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001686 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001687 /* we might have gone too far, last buffer is not loadedd */
1688 if (addr_type == ADDR_LOADED_BUFFERS)
1689 while (buf->b_ml.ml_mfp == NULL)
1690 {
1691 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1692 if (nextbuf == NULL)
1693 break;
1694 buf = nextbuf;
1695 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001696 return buf->b_fnum;
1697}
1698
Bram Moolenaarf240e182014-11-27 18:33:02 +01001699#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001700static int current_win_nr(win_T *win);
1701static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001702
1703 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001704current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001705{
1706 win_T *wp;
1707 int nr = 0;
1708
Bram Moolenaar29323592016-07-24 22:04:11 +02001709 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001710 {
1711 ++nr;
1712 if (wp == win)
1713 break;
1714 }
1715 return nr;
1716}
1717
1718 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001719current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001720{
1721 tabpage_T *tp;
1722 int nr = 0;
1723
Bram Moolenaar29323592016-07-24 22:04:11 +02001724 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001725 {
1726 ++nr;
1727 if (tp == tab)
1728 break;
1729 }
1730 return nr;
1731}
1732
1733# define CURRENT_WIN_NR current_win_nr(curwin)
1734# define LAST_WIN_NR current_win_nr(NULL)
1735# define CURRENT_TAB_NR current_tab_nr(curtab)
1736# define LAST_TAB_NR current_tab_nr(NULL)
1737#else
1738# define CURRENT_WIN_NR 1
1739# define LAST_WIN_NR 1
1740# define CURRENT_TAB_NR 1
1741# define LAST_TAB_NR 1
1742#endif
1743
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001744
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745/*
1746 * Execute one Ex command.
1747 *
1748 * If 'sourcing' is TRUE, the command will be included in the error message.
1749 *
1750 * 1. skip comment lines and leading space
1751 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001752 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001753 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001754 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001755 * 6. parse arguments
1756 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001758 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 *
1760 * This function may be called recursively!
1761 */
1762#if (_MSC_VER == 1200)
1763/*
Bram Moolenaared203462004-06-16 11:19:22 +00001764 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001766 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767#endif
1768 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001769do_one_cmd(
1770 char_u **cmdlinep,
1771 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001773 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001775 char_u *(*fgetline)(int, void *, int),
1776 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777{
1778 char_u *p;
1779 linenr_T lnum;
1780 long n;
1781 char_u *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001782 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 exarg_T ea; /* Ex command arguments */
1784 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001785 int save_msg_scroll = msg_scroll;
1786 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001788#ifdef HAVE_SANDBOX
1789 int did_sandbox = FALSE;
1790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 cmdmod_T save_cmdmod;
1792 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001793 char_u *cmd;
Bram Moolenaarded27822017-01-02 14:27:34 +01001794 int address_count = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795
1796 vim_memset(&ea, 0, sizeof(ea));
1797 ea.line1 = 1;
1798 ea.line2 = 1;
1799#ifdef FEAT_EVAL
1800 ++ex_nesting_level;
1801#endif
1802
Bram Moolenaar21691f82012-12-05 19:13:18 +01001803 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 if (quitmore
1805#ifdef FEAT_EVAL
1806 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001807 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001808#endif
1809#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001810 /* avoid that an autocommand, e.g. QuitPre, does this */
1811 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812#endif
1813 )
1814 --quitmore;
1815
1816 /*
1817 * Reset browse, confirm, etc.. They are restored when returning, for
1818 * recursive calls.
1819 */
1820 save_cmdmod = cmdmod;
1821 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1822
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001823 /* "#!anything" is handled like a comment. */
1824 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1825 goto doend;
1826
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 /*
1828 * Repeat until no more command modifiers are found.
1829 */
1830 ea.cmd = *cmdlinep;
1831 for (;;)
1832 {
1833/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001834 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 */
1836 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1837 ++ea.cmd;
1838
1839 /* in ex mode, an empty line works like :+ */
1840 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001841 && (getline_equal(fgetline, cookie, getexmodeline)
1842 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001843 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 {
1845 ea.cmd = (char_u *)"+";
1846 ex_pressedreturn = TRUE;
1847 }
1848
1849 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001850 if (*ea.cmd == '"')
1851 goto doend;
1852 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001853 {
1854 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857
1858/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001859 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 */
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001861 p = skip_range(ea.cmd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 switch (*p)
1863 {
1864 /* When adding an entry, also modify cmd_exists(). */
1865 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1866 break;
1867#ifdef FEAT_WINDOWS
1868 cmdmod.split |= WSP_ABOVE;
1869#endif
1870 continue;
1871
1872 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1873 {
1874#ifdef FEAT_WINDOWS
1875 cmdmod.split |= WSP_BELOW;
1876#endif
1877 continue;
1878 }
1879 if (checkforcmd(&ea.cmd, "browse", 3))
1880 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001881#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 cmdmod.browse = TRUE;
1883#endif
1884 continue;
1885 }
1886 if (!checkforcmd(&ea.cmd, "botright", 2))
1887 break;
1888#ifdef FEAT_WINDOWS
1889 cmdmod.split |= WSP_BOT;
1890#endif
1891 continue;
1892
1893 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1894 break;
1895#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1896 cmdmod.confirm = TRUE;
1897#endif
1898 continue;
1899
1900 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1901 {
1902 cmdmod.keepmarks = TRUE;
1903 continue;
1904 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001905 if (checkforcmd(&ea.cmd, "keepalt", 5))
1906 {
1907 cmdmod.keepalt = TRUE;
1908 continue;
1909 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001910 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1911 {
1912 cmdmod.keeppatterns = TRUE;
1913 continue;
1914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1916 break;
1917 cmdmod.keepjumps = TRUE;
1918 continue;
1919
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001920 case 'f': /* only accept ":filter {pat} cmd" */
1921 {
1922 char_u *reg_pat;
1923
1924 if (!checkforcmd(&p, "filter", 4)
1925 || *p == NUL || ends_excmd(*p))
1926 break;
Bram Moolenaard29459b2016-08-26 22:29:11 +02001927 if (*p == '!')
1928 {
1929 cmdmod.filter_force = TRUE;
1930 p = skipwhite(p + 1);
1931 if (*p == NUL || ends_excmd(*p))
1932 break;
1933 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001934 p = skip_vimgrep_pat(p, &reg_pat, NULL);
1935 if (p == NULL || *p == NUL)
1936 break;
1937 cmdmod.filter_regmatch.regprog =
1938 vim_regcomp(reg_pat, RE_MAGIC);
1939 if (cmdmod.filter_regmatch.regprog == NULL)
1940 break;
1941 ea.cmd = p;
1942 continue;
1943 }
1944
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 /* ":hide" and ":hide | cmd" are not modifiers */
1946 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1947 || *p == NUL || ends_excmd(*p))
1948 break;
1949 ea.cmd = p;
1950 cmdmod.hide = TRUE;
1951 continue;
1952
1953 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1954 {
1955 cmdmod.lockmarks = TRUE;
1956 continue;
1957 }
1958
1959 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1960 break;
1961#ifdef FEAT_WINDOWS
1962 cmdmod.split |= WSP_ABOVE;
1963#endif
1964 continue;
1965
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001966 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001967 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001968#ifdef FEAT_AUTOCMD
1969 if (cmdmod.save_ei == NULL)
1970 {
1971 /* Set 'eventignore' to "all". Restore the
1972 * existing option value later. */
1973 cmdmod.save_ei = vim_strsave(p_ei);
1974 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001975 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001976 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001977#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001978 continue;
1979 }
Bram Moolenaar3bcfca32016-07-30 19:39:29 +02001980 if (!checkforcmd(&ea.cmd, "noswapfile", 3))
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001981 break;
1982 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001983 continue;
1984
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1986 break;
1987#ifdef FEAT_WINDOWS
1988 cmdmod.split |= WSP_BELOW;
1989#endif
1990 continue;
1991
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001992 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1993 {
1994#ifdef HAVE_SANDBOX
1995 if (!did_sandbox)
1996 ++sandbox;
1997 did_sandbox = TRUE;
1998#endif
1999 continue;
2000 }
2001 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002003 if (save_msg_silent == -1)
2004 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
2007 {
2008 /* ":silent!", but not "silent !cmd" */
2009 ea.cmd = skipwhite(ea.cmd + 1);
2010 ++emsg_silent;
2011 ++did_esilent;
2012 }
2013 continue;
2014
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002015 case 't': if (checkforcmd(&p, "tab", 3))
2016 {
2017#ifdef FEAT_WINDOWS
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02002018 long tabnr = get_address(&ea, &ea.cmd, ADDR_TABS,
Bram Moolenaarded27822017-01-02 14:27:34 +01002019 ea.skip, FALSE, 1);
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02002020 if (tabnr == MAXLNUM)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002021 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02002022 else
2023 {
2024 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2025 {
2026 errormsg = (char_u *)_(e_invrange);
2027 goto doend;
2028 }
2029 cmdmod.tab = tabnr + 1;
2030 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002031 ea.cmd = p;
2032#endif
2033 continue;
2034 }
2035 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 break;
2037#ifdef FEAT_WINDOWS
2038 cmdmod.split |= WSP_TOP;
2039#endif
2040 continue;
2041
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002042 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
2043 break;
2044 if (save_msg_silent == -1)
2045 save_msg_silent = msg_silent;
2046 msg_silent = 0;
2047 continue;
2048
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
2050 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002051#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 cmdmod.split |= WSP_VERT;
2053#endif
2054 continue;
2055 }
2056 if (!checkforcmd(&p, "verbose", 4))
2057 break;
2058 if (verbose_save < 0)
2059 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00002060 if (vim_isdigit(*ea.cmd))
2061 p_verbose = atoi((char *)ea.cmd);
2062 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 p_verbose = 1;
2064 ea.cmd = p;
2065 continue;
2066 }
2067 break;
2068 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002069 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070
2071#ifdef FEAT_EVAL
2072 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2073 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2074#else
2075 ea.skip = (if_level > 0);
2076#endif
2077
2078#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002079# ifdef FEAT_PROFILE
2080 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002081 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002082 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002083 if (getline_equal(fgetline, cookie, get_func_line))
2084 func_line_exec(getline_cookie(fgetline, cookie));
2085 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002086 script_line_exec();
2087 }
2088#endif
2089
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 /* May go to debug mode. If this happens and the ">quit" debug command is
2091 * used, throw an interrupt exception and skip the next command. */
2092 dbg_check_breakpoint(&ea);
2093 if (!ea.skip && got_int)
2094 {
2095 ea.skip = TRUE;
2096 (void)do_intthrow(cstack);
2097 }
2098#endif
2099
2100/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002101 * 3. Skip over the range to find the command. Let "p" point to after it.
2102 *
2103 * We need the command to know what kind of range it uses.
2104 */
2105 cmd = ea.cmd;
2106 ea.cmd = skip_range(ea.cmd, NULL);
2107 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2108 ea.cmd = skipwhite(ea.cmd + 1);
2109 p = find_command(&ea, NULL);
2110
2111/*
2112 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 *
2114 * where 'addr' is:
2115 *
2116 * % (entire file)
2117 * $ [+-NUM]
2118 * 'x [+-NUM] (where x denotes a currently defined mark)
2119 * . [+-NUM]
2120 * [+-NUM]..
2121 * NUM
2122 *
2123 * The ea.cmd pointer is updated to point to the first character following the
2124 * range spec. If an initial address is found, but no second, the upper bound
2125 * is equal to the lower.
2126 */
2127
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002128 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002129 if (!IS_USER_CMDIDX(ea.cmdidx))
2130 {
2131 if (ea.cmdidx != CMD_SIZE)
2132 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2133 else
2134 ea.addr_type = ADDR_LINES;
2135
2136#ifdef FEAT_WINDOWS
2137 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002138 if (ea.cmdidx == CMD_wincmd && p != NULL)
2139 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002140#endif
2141 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002142
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002144 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 for (;;)
2146 {
2147 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002148 switch (ea.addr_type)
2149 {
2150 case ADDR_LINES:
2151 /* default is current line number */
2152 ea.line2 = curwin->w_cursor.lnum;
2153 break;
2154 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002155 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002156 ea.line2 = lnum;
2157 break;
2158 case ADDR_ARGUMENTS:
2159 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002160 if (ea.line2 > ARGCOUNT)
2161 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002162 break;
2163 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002164 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002165 ea.line2 = curbuf->b_fnum;
2166 break;
2167 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002168 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002169 ea.line2 = lnum;
2170 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002171#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002172 case ADDR_QUICKFIX:
2173 ea.line2 = qf_get_cur_valid_idx(&ea);
2174 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002175#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002178 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip,
Bram Moolenaarded27822017-01-02 14:27:34 +01002179 ea.addr_count == 0, address_count++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 if (ea.cmd == NULL) /* error detected */
2181 goto doend;
2182 if (lnum == MAXLNUM)
2183 {
2184 if (*ea.cmd == '%') /* '%' - all lines */
2185 {
2186 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002187 switch (ea.addr_type)
2188 {
2189 case ADDR_LINES:
2190 ea.line1 = 1;
2191 ea.line2 = curbuf->b_ml.ml_line_count;
2192 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002193 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002194 {
2195 buf_T *buf = firstbuf;
2196
2197 while (buf->b_next != NULL
2198 && buf->b_ml.ml_mfp == NULL)
2199 buf = buf->b_next;
2200 ea.line1 = buf->b_fnum;
2201 buf = lastbuf;
2202 while (buf->b_prev != NULL
2203 && buf->b_ml.ml_mfp == NULL)
2204 buf = buf->b_prev;
2205 ea.line2 = buf->b_fnum;
2206 break;
2207 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002208 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002209 ea.line1 = firstbuf->b_fnum;
2210 ea.line2 = lastbuf->b_fnum;
2211 break;
2212 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002213 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002214 if (IS_USER_CMDIDX(ea.cmdidx))
2215 {
2216 ea.line1 = 1;
2217 ea.line2 = ea.addr_type == ADDR_WINDOWS
2218 ? LAST_WIN_NR : LAST_TAB_NR;
2219 }
2220 else
2221 {
2222 /* there is no Vim command which uses '%' and
2223 * ADDR_WINDOWS or ADDR_TABS */
2224 errormsg = (char_u *)_(e_invrange);
2225 goto doend;
2226 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002227 break;
2228 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002229 if (ARGCOUNT == 0)
2230 ea.line1 = ea.line2 = 0;
2231 else
2232 {
2233 ea.line1 = 1;
2234 ea.line2 = ARGCOUNT;
2235 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002236 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002237#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002238 case ADDR_QUICKFIX:
2239 ea.line1 = 1;
2240 ea.line2 = qf_get_size(&ea);
2241 if (ea.line2 == 0)
2242 ea.line2 = 1;
2243 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002244#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 ++ea.addr_count;
2247 }
2248 /* '*' - visual area */
2249 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2250 {
2251 pos_T *fp;
2252
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002253 if (ea.addr_type != ADDR_LINES)
2254 {
2255 errormsg = (char_u *)_(e_invrange);
2256 goto doend;
2257 }
2258
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 ++ea.cmd;
2260 if (!ea.skip)
2261 {
2262 fp = getmark('<', FALSE);
2263 if (check_mark(fp) == FAIL)
2264 goto doend;
2265 ea.line1 = fp->lnum;
2266 fp = getmark('>', FALSE);
2267 if (check_mark(fp) == FAIL)
2268 goto doend;
2269 ea.line2 = fp->lnum;
2270 ++ea.addr_count;
2271 }
2272 }
2273 }
2274 else
2275 ea.line2 = lnum;
2276 ea.addr_count++;
2277
2278 if (*ea.cmd == ';')
2279 {
2280 if (!ea.skip)
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002281 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 curwin->w_cursor.lnum = ea.line2;
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002283 /* don't leave the cursor on an illegal line */
2284 check_cursor_lnum();
2285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 }
2287 else if (*ea.cmd != ',')
2288 break;
2289 ++ea.cmd;
2290 }
2291
2292 /* One address given: set start and end lines */
2293 if (ea.addr_count == 1)
2294 {
2295 ea.line1 = ea.line2;
2296 /* ... but only implicit: really no address given */
2297 if (lnum == MAXLNUM)
2298 ea.addr_count = 0;
2299 }
2300
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002302 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 */
2304
2305 /*
2306 * Skip ':' and any white space
2307 */
2308 ea.cmd = skipwhite(ea.cmd);
2309 while (*ea.cmd == ':')
2310 ea.cmd = skipwhite(ea.cmd + 1);
2311
2312 /*
2313 * If we got a line, but no command, then go to the line.
2314 * If we find a '|' or '\n' we set ea.nextcmd.
2315 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002316 if (*ea.cmd == NUL || *ea.cmd == '"'
2317 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 {
2319 /*
2320 * strange vi behaviour:
2321 * ":3" jumps to line 3
2322 * ":3|..." prints line 3
2323 * ":|" prints current line
2324 */
2325 if (ea.skip) /* skip this if inside :if */
2326 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002327 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 {
2329 ea.cmdidx = CMD_print;
2330 ea.argt = RANGE+COUNT+TRLBAR;
2331 if ((errormsg = invalid_range(&ea)) == NULL)
2332 {
2333 correct_range(&ea);
2334 ex_print(&ea);
2335 }
2336 }
2337 else if (ea.addr_count != 0)
2338 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002339 if (ea.line2 > curbuf->b_ml.ml_line_count)
2340 {
2341 /* With '-' in 'cpoptions' a line number past the file is an
2342 * error, otherwise put it at the end of the file. */
2343 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2344 ea.line2 = -1;
2345 else
2346 ea.line2 = curbuf->b_ml.ml_line_count;
2347 }
2348
2349 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002350 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 else
2352 {
2353 if (ea.line2 == 0)
2354 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 else
2356 curwin->w_cursor.lnum = ea.line2;
2357 beginline(BL_SOL | BL_FIX);
2358 }
2359 }
2360 goto doend;
2361 }
2362
Bram Moolenaard5005162014-08-22 23:05:54 +02002363#ifdef FEAT_AUTOCMD
2364 /* If this looks like an undefined user command and there are CmdUndefined
2365 * autocommands defined, trigger the matching autocommands. */
2366 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2367 && ASCII_ISUPPER(*ea.cmd)
2368 && has_cmdundefined())
2369 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002370 int ret;
2371
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002372 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002373 while (ASCII_ISALNUM(*p))
2374 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002375 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002376 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2377 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002378 /* If the autocommands did something and didn't cause an error, try
2379 * finding the command again. */
Bram Moolenaareac784e2016-07-28 22:08:24 +02002380 p = (ret && !aborting()) ? find_command(&ea, NULL) : ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002381 }
2382#endif
2383
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384#ifdef FEAT_USR_CMDS
2385 if (p == NULL)
2386 {
2387 if (!ea.skip)
2388 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2389 goto doend;
2390 }
2391 /* Check for wrong commands. */
2392 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2393 {
2394 errormsg = uc_fun_cmd();
2395 goto doend;
2396 }
2397#endif
2398 if (ea.cmdidx == CMD_SIZE)
2399 {
2400 if (!ea.skip)
2401 {
2402 STRCPY(IObuff, _("E492: Not an editor command"));
2403 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002404 {
2405 /* If the modifier was parsed OK the error must be in the
2406 * following command */
2407 if (after_modifier != NULL)
2408 append_command(after_modifier);
2409 else
2410 append_command(*cmdlinep);
2411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002413 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 }
2415 goto doend;
2416 }
2417
Bram Moolenaar958636c2014-10-21 20:01:58 +02002418 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2419 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002420#ifdef HAVE_EX_SCRIPT_NI
2421 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2422#endif
2423 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424
2425#ifndef FEAT_EVAL
2426 /*
2427 * When the expression evaluation is disabled, recognize the ":if" and
2428 * ":endif" commands and ignore everything in between it.
2429 */
2430 if (ea.cmdidx == CMD_if)
2431 ++if_level;
2432 if (if_level)
2433 {
2434 if (ea.cmdidx == CMD_endif)
2435 --if_level;
2436 goto doend;
2437 }
2438
2439#endif
2440
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002441 /* forced commands */
2442 if (*p == '!' && ea.cmdidx != CMD_substitute
2443 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 {
2445 ++p;
2446 ea.forceit = TRUE;
2447 }
2448 else
2449 ea.forceit = FALSE;
2450
2451/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002452 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002454 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002455 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456
2457 if (!ea.skip)
2458 {
2459#ifdef HAVE_SANDBOX
2460 if (sandbox != 0 && !(ea.argt & SBOXOK))
2461 {
2462 /* Command not allowed in sandbox. */
2463 errormsg = (char_u *)_(e_sandbox);
2464 goto doend;
2465 }
2466#endif
2467 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2468 {
2469 /* Command not allowed in non-'modifiable' buffer */
2470 errormsg = (char_u *)_(e_modifiable);
2471 goto doend;
2472 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002473
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002474 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002475 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002477 /* Command not allowed when editing the command line. */
Bram Moolenaar5a497892016-09-03 16:29:04 +02002478 errormsg = get_text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 goto doend;
2480 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002481#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002482 /* Disallow editing another buffer when "curbuf_lock" is set.
2483 * Do allow ":edit" (check for argument later).
2484 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002485 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002486 && ea.cmdidx != CMD_edit
2487 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002488 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002489 && curbuf_locked())
2490 goto doend;
2491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492
2493 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2494 {
2495 /* no range allowed */
2496 errormsg = (char_u *)_(e_norange);
2497 goto doend;
2498 }
2499 }
2500
2501 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2502 {
2503 errormsg = (char_u *)_(e_nobang);
2504 goto doend;
2505 }
2506
2507 /*
2508 * Don't complain about the range if it is not used
2509 * (could happen if line_count is accidentally set to 0).
2510 */
2511 if (!ea.skip && !ni)
2512 {
2513 /*
2514 * If the range is backwards, ask for confirmation and, if given, swap
2515 * ea.line1 & ea.line2 so it's forwards again.
2516 * When global command is busy, don't ask, will fail below.
2517 */
2518 if (!global_busy && ea.line1 > ea.line2)
2519 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002520 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002522 if (sourcing || exmode_active)
2523 {
2524 errormsg = (char_u *)_("E493: Backwards range given");
2525 goto doend;
2526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 if (ask_yesno((char_u *)
2528 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002529 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 }
2531 lnum = ea.line1;
2532 ea.line1 = ea.line2;
2533 ea.line2 = lnum;
2534 }
2535 if ((errormsg = invalid_range(&ea)) != NULL)
2536 goto doend;
2537 }
2538
2539 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2540 ea.line2 = 1;
2541
2542 correct_range(&ea);
2543
2544#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002545 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2546 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 {
2548 /* Put the first line at the start of a closed fold, put the last line
2549 * at the end of a closed fold. */
2550 (void)hasFolding(ea.line1, &ea.line1, NULL);
2551 (void)hasFolding(ea.line2, NULL, &ea.line2);
2552 }
2553#endif
2554
2555#ifdef FEAT_QUICKFIX
2556 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002557 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 * option here, so things like % get expanded.
2559 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002560 p = replace_makeprg(&ea, p, cmdlinep);
2561 if (p == NULL)
2562 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563#endif
2564
2565 /*
2566 * Skip to start of argument.
2567 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2568 */
2569 if (ea.cmdidx == CMD_bang)
2570 ea.arg = p;
2571 else
2572 ea.arg = skipwhite(p);
2573
2574 /*
2575 * Check for "++opt=val" argument.
2576 * Must be first, allow ":w ++enc=utf8 !cmd"
2577 */
2578 if (ea.argt & ARGOPT)
2579 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2580 if (getargopt(&ea) == FAIL && !ni)
2581 {
2582 errormsg = (char_u *)_(e_invarg);
2583 goto doend;
2584 }
2585
2586 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2587 {
2588 if (*ea.arg == '>') /* append */
2589 {
2590 if (*++ea.arg != '>') /* typed wrong */
2591 {
2592 errormsg = (char_u *)_("E494: Use w or w>>");
2593 goto doend;
2594 }
2595 ea.arg = skipwhite(ea.arg + 1);
2596 ea.append = TRUE;
2597 }
2598 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2599 {
2600 ++ea.arg;
2601 ea.usefilter = TRUE;
2602 }
2603 }
2604
2605 if (ea.cmdidx == CMD_read)
2606 {
2607 if (ea.forceit)
2608 {
2609 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2610 ea.forceit = FALSE;
2611 }
2612 else if (*ea.arg == '!') /* :r !filter */
2613 {
2614 ++ea.arg;
2615 ea.usefilter = TRUE;
2616 }
2617 }
2618
2619 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2620 {
2621 ea.amount = 1;
2622 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2623 {
2624 ++ea.arg;
2625 ++ea.amount;
2626 }
2627 ea.arg = skipwhite(ea.arg);
2628 }
2629
2630 /*
2631 * Check for "+command" argument, before checking for next command.
2632 * Don't do this for ":read !cmd" and ":write !cmd".
2633 */
2634 if ((ea.argt & EDITCMD) && !ea.usefilter)
2635 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2636
2637 /*
2638 * Check for '|' to separate commands and '"' to start comments.
2639 * Don't do this for ":read !cmd" and ":write !cmd".
2640 */
2641 if ((ea.argt & TRLBAR) && !ea.usefilter)
2642 separate_nextcmd(&ea);
2643
2644 /*
2645 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002646 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2647 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002649 else if (ea.cmdidx == CMD_bang
2650 || ea.cmdidx == CMD_global
2651 || ea.cmdidx == CMD_vglobal
2652 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 {
2654 for (p = ea.arg; *p; ++p)
2655 {
2656 /* Remove one backslash before a newline, so that it's possible to
2657 * pass a newline to the shell and also a newline that is preceded
2658 * with a backslash. This makes it impossible to end a shell
2659 * command in a backslash, but that doesn't appear useful.
2660 * Halving the number of backslashes is incompatible with previous
2661 * versions. */
2662 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002663 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 else if (*p == '\n')
2665 {
2666 ea.nextcmd = p + 1;
2667 *p = NUL;
2668 break;
2669 }
2670 }
2671 }
2672
2673 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2674 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002675 buf_T *buf;
2676
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002678 switch (ea.addr_type)
2679 {
2680 case ADDR_LINES:
2681 ea.line2 = curbuf->b_ml.ml_line_count;
2682 break;
2683 case ADDR_LOADED_BUFFERS:
2684 buf = firstbuf;
2685 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2686 buf = buf->b_next;
2687 ea.line1 = buf->b_fnum;
2688 buf = lastbuf;
2689 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2690 buf = buf->b_prev;
2691 ea.line2 = buf->b_fnum;
2692 break;
2693 case ADDR_BUFFERS:
2694 ea.line1 = firstbuf->b_fnum;
2695 ea.line2 = lastbuf->b_fnum;
2696 break;
2697 case ADDR_WINDOWS:
2698 ea.line2 = LAST_WIN_NR;
2699 break;
2700 case ADDR_TABS:
2701 ea.line2 = LAST_TAB_NR;
2702 break;
2703 case ADDR_ARGUMENTS:
2704 if (ARGCOUNT == 0)
2705 ea.line1 = ea.line2 = 0;
2706 else
2707 ea.line2 = ARGCOUNT;
2708 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002709#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002710 case ADDR_QUICKFIX:
2711 ea.line2 = qf_get_size(&ea);
2712 if (ea.line2 == 0)
2713 ea.line2 = 1;
2714 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002715#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 }
2718
2719 /* accept numbered register only when no count allowed (:put) */
2720 if ( (ea.argt & REGSTR)
2721 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002722 /* Do not allow register = for user commands */
2723 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2725 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002726#ifndef FEAT_CLIPBOARD
2727 /* check these explicitly for a more specific error message */
2728 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002730 errormsg = (char_u *)_(e_invalidreg);
2731 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 }
2733#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002734 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2735 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002736 {
2737 ea.regname = *ea.arg++;
2738#ifdef FEAT_EVAL
2739 /* for '=' register: accept the rest of the line as an expression */
2740 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2741 {
2742 set_expr_line(vim_strsave(ea.arg));
2743 ea.arg += STRLEN(ea.arg);
2744 }
2745#endif
2746 ea.arg = skipwhite(ea.arg);
2747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 }
2749
2750 /*
2751 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2752 * count, it's a buffer name.
2753 */
2754 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2755 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2756 || vim_iswhite(*p)))
2757 {
2758 n = getdigits(&ea.arg);
2759 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002760 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 {
2762 errormsg = (char_u *)_(e_zerocount);
2763 goto doend;
2764 }
2765 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2766 {
2767 ea.line2 = n;
2768 if (ea.addr_count == 0)
2769 ea.addr_count = 1;
2770 }
2771 else
2772 {
2773 ea.line1 = ea.line2;
2774 ea.line2 += n - 1;
2775 ++ea.addr_count;
2776 /*
2777 * Be vi compatible: no error message for out of range.
2778 */
2779 if (ea.line2 > curbuf->b_ml.ml_line_count)
2780 ea.line2 = curbuf->b_ml.ml_line_count;
2781 }
2782 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002783
2784 /*
2785 * Check for flags: 'l', 'p' and '#'.
2786 */
2787 if (ea.argt & EXFLAGS)
2788 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002790 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002791 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 {
2793 errormsg = (char_u *)_(e_trailing);
2794 goto doend;
2795 }
2796
2797 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2798 {
2799 errormsg = (char_u *)_(e_argreq);
2800 goto doend;
2801 }
2802
2803#ifdef FEAT_EVAL
2804 /*
2805 * Skip the command when it's not going to be executed.
2806 * The commands like :if, :endif, etc. always need to be executed.
2807 * Also make an exception for commands that handle a trailing command
2808 * themselves.
2809 */
2810 if (ea.skip)
2811 {
2812 switch (ea.cmdidx)
2813 {
2814 /* commands that need evaluation */
2815 case CMD_while:
2816 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002817 case CMD_for:
2818 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 case CMD_if:
2820 case CMD_elseif:
2821 case CMD_else:
2822 case CMD_endif:
2823 case CMD_try:
2824 case CMD_catch:
2825 case CMD_finally:
2826 case CMD_endtry:
2827 case CMD_function:
2828 break;
2829
2830 /* Commands that handle '|' themselves. Check: A command should
2831 * either have the TRLBAR flag, appear in this list or appear in
2832 * the list at ":help :bar". */
2833 case CMD_aboveleft:
2834 case CMD_and:
2835 case CMD_belowright:
2836 case CMD_botright:
2837 case CMD_browse:
2838 case CMD_call:
2839 case CMD_confirm:
2840 case CMD_delfunction:
2841 case CMD_djump:
2842 case CMD_dlist:
2843 case CMD_dsearch:
2844 case CMD_dsplit:
2845 case CMD_echo:
2846 case CMD_echoerr:
2847 case CMD_echomsg:
2848 case CMD_echon:
2849 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002850 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 case CMD_help:
2852 case CMD_hide:
2853 case CMD_ijump:
2854 case CMD_ilist:
2855 case CMD_isearch:
2856 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002857 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 case CMD_keepjumps:
2859 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002860 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 case CMD_leftabove:
2862 case CMD_let:
2863 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002864 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002866 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002867 case CMD_noautocmd:
2868 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 case CMD_perl:
2870 case CMD_psearch:
2871 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002872 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002873 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 case CMD_return:
2875 case CMD_rightbelow:
2876 case CMD_ruby:
2877 case CMD_silent:
2878 case CMD_smagic:
2879 case CMD_snomagic:
2880 case CMD_substitute:
2881 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002882 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 case CMD_tcl:
2884 case CMD_throw:
2885 case CMD_tilde:
2886 case CMD_topleft:
2887 case CMD_unlet:
2888 case CMD_verbose:
2889 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002890 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 break;
2892
2893 default: goto doend;
2894 }
2895 }
2896#endif
2897
2898 if (ea.argt & XFILE)
2899 {
2900 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2901 goto doend;
2902 }
2903
2904#ifdef FEAT_LISTCMDS
2905 /*
2906 * Accept buffer name. Cannot be used at the same time with a buffer
2907 * number. Don't do this for a user command.
2908 */
2909 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002910 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 {
2912 /*
2913 * :bdelete, :bwipeout and :bunload take several arguments, separated
2914 * by spaces: find next space (skipping over escaped characters).
2915 * The others take one argument: ignore trailing spaces.
2916 */
2917 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2918 || ea.cmdidx == CMD_bunload)
2919 p = skiptowhite_esc(ea.arg);
2920 else
2921 {
2922 p = ea.arg + STRLEN(ea.arg);
2923 while (p > ea.arg && vim_iswhite(p[-1]))
2924 --p;
2925 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002926 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2927 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 if (ea.line2 < 0) /* failed */
2929 goto doend;
2930 ea.addr_count = 1;
2931 ea.arg = skipwhite(p);
2932 }
2933#endif
2934
2935/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002936 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 *
2938 * The "ea" structure holds the arguments that can be used.
2939 */
2940 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002941 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 ea.cookie = cookie;
2943#ifdef FEAT_EVAL
2944 ea.cstack = cstack;
2945#endif
2946
2947#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002948 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 {
2950 /*
2951 * Execute a user-defined command.
2952 */
2953 do_ucmd(&ea);
2954 }
2955 else
2956#endif
2957 {
2958 /*
2959 * Call the function to execute the command.
2960 */
2961 ea.errmsg = NULL;
2962 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2963 if (ea.errmsg != NULL)
2964 errormsg = (char_u *)_(ea.errmsg);
2965 }
2966
2967#ifdef FEAT_EVAL
2968 /*
2969 * If the command just executed called do_cmdline(), any throw or ":return"
2970 * or ":finish" encountered there must also check the cstack of the still
2971 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2972 * exception, or reanimate a returned function or finished script file and
2973 * return or finish it again.
2974 */
2975 if (need_rethrow)
2976 do_throw(cstack);
2977 else if (check_cstack)
2978 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002979 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002981 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 && current_func_returned())
2983 do_return(&ea, TRUE, FALSE, NULL);
2984 }
2985 need_rethrow = check_cstack = FALSE;
2986#endif
2987
2988doend:
2989 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2990 curwin->w_cursor.lnum = 1;
2991
2992 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2993 {
2994 if (sourcing)
2995 {
2996 if (errormsg != IObuff)
2997 {
2998 STRCPY(IObuff, errormsg);
2999 errormsg = IObuff;
3000 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003001 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 }
3003 emsg(errormsg);
3004 }
3005#ifdef FEAT_EVAL
3006 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02003007 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
3008 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009#endif
3010
3011 if (verbose_save >= 0)
3012 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00003013#ifdef FEAT_AUTOCMD
3014 if (cmdmod.save_ei != NULL)
3015 {
3016 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003017 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
3018 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00003019 free_string_option(cmdmod.save_ei);
3020 }
3021#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003022 if (cmdmod.filter_regmatch.regprog != NULL)
3023 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024
3025 cmdmod = save_cmdmod;
3026
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003027 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 {
3029 /* messages could be enabled for a serious error, need to check if the
3030 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00003031 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003032 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 emsg_silent -= did_esilent;
3034 if (emsg_silent < 0)
3035 emsg_silent = 0;
3036 /* Restore msg_scroll, it's set by file I/O commands, even when no
3037 * message is actually displayed. */
3038 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003039
3040 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
3041 * somewhere in the line. Put it back in the first column. */
3042 if (redirecting())
3043 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 }
3045
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003046#ifdef HAVE_SANDBOX
3047 if (did_sandbox)
3048 --sandbox;
3049#endif
3050
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3052 ea.nextcmd = NULL;
3053
3054#ifdef FEAT_EVAL
3055 --ex_nesting_level;
3056#endif
3057
3058 return ea.nextcmd;
3059}
3060#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003061 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062#endif
3063
3064/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003065 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 * If there is a match advance "pp" to the argument and return TRUE.
3067 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003068 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003069checkforcmd(
3070 char_u **pp, /* start of command */
3071 char *cmd, /* name of command */
3072 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073{
3074 int i;
3075
3076 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003077 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 break;
3079 if (i >= len && !isalpha((*pp)[i]))
3080 {
3081 *pp = skipwhite(*pp + i);
3082 return TRUE;
3083 }
3084 return FALSE;
3085}
3086
3087/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003088 * Append "cmd" to the error message in IObuff.
3089 * Takes care of limiting the length and handling 0xa0, which would be
3090 * invisible otherwise.
3091 */
3092 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003093append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003094{
3095 char_u *s = cmd;
3096 char_u *d;
3097
3098 STRCAT(IObuff, ": ");
3099 d = IObuff + STRLEN(IObuff);
3100 while (*s != NUL && d - IObuff < IOSIZE - 7)
3101 {
3102 if (
3103#ifdef FEAT_MBYTE
3104 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3105#endif
3106 *s == 0xa0)
3107 {
3108 s +=
3109#ifdef FEAT_MBYTE
3110 enc_utf8 ? 2 :
3111#endif
3112 1;
3113 STRCPY(d, "<a0>");
3114 d += 4;
3115 }
3116 else
3117 MB_COPY_CHAR(s, d);
3118 }
3119 *d = NUL;
3120}
3121
3122/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003124 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003126 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 * Returns NULL for an ambiguous user command.
3128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003130find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131{
3132 int len;
3133 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003134 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135
3136 /*
3137 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003138 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 * - the 'k' command can directly be followed by any character.
3140 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003141 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003143 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 */
3145 p = eap->cmd;
3146 if (*p == 'k')
3147 {
3148 eap->cmdidx = CMD_k;
3149 ++p;
3150 }
3151 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003152 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3153 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 || p[1] == 'g'
3155 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3156 || p[1] == 'I'
3157 || (p[1] == 'r' && p[2] != 'e')))
3158 {
3159 eap->cmdidx = CMD_substitute;
3160 ++p;
3161 }
3162 else
3163 {
3164 while (ASCII_ISALPHA(*p))
3165 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003166 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003167 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003168 while (ASCII_ISALNUM(*p))
3169 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003170
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 /* check for non-alpha command */
3172 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3173 ++p;
3174 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003175 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3176 {
3177 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3178 * :delete with the 'l' flag. Same for 'p'. */
3179 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003180 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003181 break;
3182 if (i == len - 1)
3183 {
3184 --len;
3185 if (p[-1] == 'l')
3186 eap->flags |= EXFLAG_LIST;
3187 else
3188 eap->flags |= EXFLAG_PRINT;
3189 }
3190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191
3192 if (ASCII_ISLOWER(*eap->cmd))
3193 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
3194 else
3195 eap->cmdidx = cmdidxs[26];
3196
3197 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3198 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3199 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3200 (size_t)len) == 0)
3201 {
3202#ifdef FEAT_EVAL
3203 if (full != NULL
3204 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3205 *full = TRUE;
3206#endif
3207 break;
3208 }
3209
3210#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003211 /* Look for a user defined command as a last resort. Let ":Print" be
3212 * overruled by a user defined command. */
3213 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3214 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003216 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 while (ASCII_ISALNUM(*p))
3218 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003219 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 }
3221#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003222 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 eap->cmdidx = CMD_SIZE;
3224 }
3225
3226 return p;
3227}
3228
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003229#ifdef FEAT_USR_CMDS
3230/*
3231 * Search for a user command that matches "eap->cmd".
3232 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3233 * Return a pointer to just after the command.
3234 * Return NULL if there is no matching command.
3235 */
3236 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003237find_ucmd(
3238 exarg_T *eap,
3239 char_u *p, /* end of the command (possibly including count) */
3240 int *full, /* set to TRUE for a full match */
3241 expand_T *xp, /* used for completion, NULL otherwise */
3242 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003243{
3244 int len = (int)(p - eap->cmd);
3245 int j, k, matchlen = 0;
3246 ucmd_T *uc;
3247 int found = FALSE;
3248 int possible = FALSE;
3249 char_u *cp, *np; /* Point into typed cmd and test name */
3250 garray_T *gap;
3251 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3252 only full match global is accepted. */
3253
3254 /*
3255 * Look for buffer-local user commands first, then global ones.
3256 */
3257 gap = &curbuf->b_ucmds;
3258 for (;;)
3259 {
3260 for (j = 0; j < gap->ga_len; ++j)
3261 {
3262 uc = USER_CMD_GA(gap, j);
3263 cp = eap->cmd;
3264 np = uc->uc_name;
3265 k = 0;
3266 while (k < len && *np != NUL && *cp++ == *np++)
3267 k++;
3268 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3269 {
3270 /* If finding a second match, the command is ambiguous. But
3271 * not if a buffer-local command wasn't a full match and a
3272 * global command is a full match. */
3273 if (k == len && found && *np != NUL)
3274 {
3275 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003276 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003277 amb_local = TRUE;
3278 }
3279
3280 if (!found || (k == len && *np == NUL))
3281 {
3282 /* If we matched up to a digit, then there could
3283 * be another command including the digit that we
3284 * should use instead.
3285 */
3286 if (k == len)
3287 found = TRUE;
3288 else
3289 possible = TRUE;
3290
3291 if (gap == &ucmds)
3292 eap->cmdidx = CMD_USER;
3293 else
3294 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003295 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003296 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003297 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003298
3299# ifdef FEAT_CMDL_COMPL
3300 if (compl != NULL)
3301 *compl = uc->uc_compl;
3302# ifdef FEAT_EVAL
3303 if (xp != NULL)
3304 {
3305 xp->xp_arg = uc->uc_compl_arg;
3306 xp->xp_scriptID = uc->uc_scriptID;
3307 }
3308# endif
3309# endif
3310 /* Do not search for further abbreviations
3311 * if this is an exact match. */
3312 matchlen = k;
3313 if (k == len && *np == NUL)
3314 {
3315 if (full != NULL)
3316 *full = TRUE;
3317 amb_local = FALSE;
3318 break;
3319 }
3320 }
3321 }
3322 }
3323
3324 /* Stop if we found a full match or searched all. */
3325 if (j < gap->ga_len || gap == &ucmds)
3326 break;
3327 gap = &ucmds;
3328 }
3329
3330 /* Only found ambiguous matches. */
3331 if (amb_local)
3332 {
3333 if (xp != NULL)
3334 xp->xp_context = EXPAND_UNSUCCESSFUL;
3335 return NULL;
3336 }
3337
3338 /* The match we found may be followed immediately by a number. Move "p"
3339 * back to point to it. */
3340 if (found || possible)
3341 return p + (matchlen - len);
3342 return p;
3343}
3344#endif
3345
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003347static struct cmdmod
3348{
3349 char *name;
3350 int minlen;
3351 int has_count; /* :123verbose :3tab */
3352} cmdmods[] = {
3353 {"aboveleft", 3, FALSE},
3354 {"belowright", 3, FALSE},
3355 {"botright", 2, FALSE},
3356 {"browse", 3, FALSE},
3357 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003358 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003359 {"hide", 3, FALSE},
3360 {"keepalt", 5, FALSE},
3361 {"keepjumps", 5, FALSE},
3362 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003363 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003364 {"leftabove", 5, FALSE},
3365 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003366 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003367 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003368 {"rightbelow", 6, FALSE},
3369 {"sandbox", 3, FALSE},
3370 {"silent", 3, FALSE},
3371 {"tab", 3, TRUE},
3372 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003373 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003374 {"verbose", 4, TRUE},
3375 {"vertical", 4, FALSE},
3376};
3377
3378/*
3379 * Return length of a command modifier (including optional count).
3380 * Return zero when it's not a modifier.
3381 */
3382 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003383modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003384{
3385 int i, j;
3386 char_u *p = cmd;
3387
3388 if (VIM_ISDIGIT(*cmd))
3389 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003390 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003391 {
3392 for (j = 0; p[j] != NUL; ++j)
3393 if (p[j] != cmdmods[i].name[j])
3394 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003395 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003396 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003397 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003398 }
3399 return 0;
3400}
3401
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402/*
3403 * Return > 0 if an Ex command "name" exists.
3404 * Return 2 if there is an exact match.
3405 * Return 3 if there is an ambiguous match.
3406 */
3407 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003408cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409{
3410 exarg_T ea;
3411 int full = FALSE;
3412 int i;
3413 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003414 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415
3416 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003417 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 {
3419 for (j = 0; name[j] != NUL; ++j)
3420 if (name[j] != cmdmods[i].name[j])
3421 break;
3422 if (name[j] == NUL && j >= cmdmods[i].minlen)
3423 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3424 }
3425
Bram Moolenaara9587612006-05-04 21:47:50 +00003426 /* Check built-in commands and user defined commands.
3427 * For ":2match" and ":3match" we need to skip the number. */
3428 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003430 p = find_command(&ea, &full);
3431 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003433 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3434 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003435 if (*skipwhite(p) != NUL)
3436 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3438}
3439#endif
3440
3441/*
3442 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3443 * we don't need/want deleted. Maybe this could be done better if we didn't
3444 * repeat all this stuff. The only problem is that they may not stay
3445 * perfectly compatible with each other, but then the command line syntax
3446 * probably won't change that much -- webb.
3447 */
3448 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003449set_one_cmd_context(
3450 expand_T *xp,
3451 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452{
3453 char_u *p;
3454 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003455 int len = 0;
3456 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3458 int compl = EXPAND_NOTHING;
3459#endif
3460#ifdef FEAT_CMDL_COMPL
3461 int delim;
3462#endif
3463 int forceit = FALSE;
3464 int usefilter = FALSE; /* filter instead of file name */
3465
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003466 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 xp->xp_pattern = buff;
3468 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003469 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470
3471/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003472 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 */
3474 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3475 ;
3476 xp->xp_pattern = cmd;
3477
3478 if (*cmd == NUL)
3479 return NULL;
3480 if (*cmd == '"') /* ignore comment lines */
3481 {
3482 xp->xp_context = EXPAND_NOTHING;
3483 return NULL;
3484 }
3485
3486/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003487 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 */
3489 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 xp->xp_pattern = cmd;
3491 if (*cmd == NUL)
3492 return NULL;
3493 if (*cmd == '"')
3494 {
3495 xp->xp_context = EXPAND_NOTHING;
3496 return NULL;
3497 }
3498
3499 if (*cmd == '|' || *cmd == '\n')
3500 return cmd + 1; /* There's another command */
3501
3502 /*
3503 * Isolate the command and search for it in the command table.
3504 * Exceptions:
3505 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003506 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3508 */
3509 if (*cmd == 'k' && cmd[1] != 'e')
3510 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003511 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 p = cmd + 1;
3513 }
3514 else
3515 {
3516 p = cmd;
3517 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3518 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003519 /* a user command may contain digits */
3520 if (ASCII_ISUPPER(cmd[0]))
3521 while (ASCII_ISALNUM(*p) || *p == '*')
3522 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003523 /* for python 3.x: ":py3*" commands completion */
3524 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003525 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003526 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003527 while (ASCII_ISALPHA(*p) || *p == '*')
3528 ++p;
3529 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003530 /* check for non-alpha command */
3531 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3532 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003533 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003535 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 {
3537 xp->xp_context = EXPAND_UNSUCCESSFUL;
3538 return NULL;
3539 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003540 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003541 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3542 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3543 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 break;
3545
3546#ifdef FEAT_USR_CMDS
3547 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3549 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550#endif
3551 }
3552
3553 /*
3554 * If the cursor is touching the command, and it ends in an alpha-numeric
3555 * character, complete the command name.
3556 */
3557 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3558 return NULL;
3559
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003560 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 {
3562 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3563 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003564 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 p = cmd + 1;
3566 }
3567#ifdef FEAT_USR_CMDS
3568 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3569 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003570 ea.cmd = cmd;
3571 p = find_ucmd(&ea, p, NULL, xp,
3572# if defined(FEAT_CMDL_COMPL)
3573 &compl
3574# else
3575 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003577 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003578 if (p == NULL)
3579 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 }
3581#endif
3582 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003583 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 {
3585 /* Not still touching the command and it was an illegal one */
3586 xp->xp_context = EXPAND_UNSUCCESSFUL;
3587 return NULL;
3588 }
3589
3590 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3591
3592 if (*p == '!') /* forced commands */
3593 {
3594 forceit = TRUE;
3595 ++p;
3596 }
3597
3598/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003599 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003601 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003602 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603
3604 arg = skipwhite(p);
3605
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003606 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 {
3608 if (*arg == '>') /* append */
3609 {
3610 if (*++arg == '>')
3611 ++arg;
3612 arg = skipwhite(arg);
3613 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003614 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 {
3616 ++arg;
3617 usefilter = TRUE;
3618 }
3619 }
3620
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003621 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 {
3623 usefilter = forceit; /* :r! filter if forced */
3624 if (*arg == '!') /* :r !filter */
3625 {
3626 ++arg;
3627 usefilter = TRUE;
3628 }
3629 }
3630
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003631 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 {
3633 while (*arg == *cmd) /* allow any number of '>' or '<' */
3634 ++arg;
3635 arg = skipwhite(arg);
3636 }
3637
3638 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003639 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 {
3641 /* Check if we're in the +command */
3642 p = arg + 1;
3643 arg = skip_cmd_arg(arg, FALSE);
3644
3645 /* Still touching the command after '+'? */
3646 if (*arg == NUL)
3647 return p;
3648
3649 /* Skip space(s) after +command to get to the real argument */
3650 arg = skipwhite(arg);
3651 }
3652
3653 /*
3654 * Check for '|' to separate commands and '"' to start comments.
3655 * Don't do this for ":read !cmd" and ":write !cmd".
3656 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003657 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 {
3659 p = arg;
3660 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003661 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 p += 2;
3663 while (*p)
3664 {
3665 if (*p == Ctrl_V)
3666 {
3667 if (p[1] != NUL)
3668 ++p;
3669 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003670 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 || *p == '|' || *p == '\n')
3672 {
3673 if (*(p - 1) != '\\')
3674 {
3675 if (*p == '|' || *p == '\n')
3676 return p + 1;
3677 return NULL; /* It's a comment */
3678 }
3679 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003680 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 }
3682 }
3683
3684 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003685 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 vim_strchr((char_u *)"|\"", *arg) == NULL)
3687 return NULL;
3688
3689 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003690 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003692 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003693 while (*p && p < buff + len)
3694 {
3695 if (*p == ' ' || *p == TAB)
3696 {
3697 /* argument starts after a space */
3698 xp->xp_pattern = ++p;
3699 }
3700 else
3701 {
3702 if (*p == '\\' && *(p + 1) != NUL)
3703 ++p; /* skip over escaped character */
3704 mb_ptr_adv(p);
3705 }
3706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003708 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003710 int c;
3711 int in_quote = FALSE;
3712 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713
3714 /*
3715 * Allow spaces within back-quotes to count as part of the argument
3716 * being expanded.
3717 */
3718 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003719 p = xp->xp_pattern;
3720 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003722#ifdef FEAT_MBYTE
3723 if (has_mbyte)
3724 c = mb_ptr2char(p);
3725 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003727 c = *p;
3728 if (c == '\\' && p[1] != NUL)
3729 ++p;
3730 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 {
3732 if (!in_quote)
3733 {
3734 xp->xp_pattern = p;
3735 bow = p + 1;
3736 }
3737 in_quote = !in_quote;
3738 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003739 /* An argument can contain just about everything, except
3740 * characters that end the command and white space. */
3741 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003742#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003743 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003744#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003745 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003746 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003747 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003748 while (*p != NUL)
3749 {
3750#ifdef FEAT_MBYTE
3751 if (has_mbyte)
3752 c = mb_ptr2char(p);
3753 else
3754#endif
3755 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003756 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003757 break;
3758#ifdef FEAT_MBYTE
3759 if (has_mbyte)
3760 len = (*mb_ptr2len)(p);
3761 else
3762#endif
3763 len = 1;
3764 mb_ptr_adv(p);
3765 }
3766 if (in_quote)
3767 bow = p;
3768 else
3769 xp->xp_pattern = p;
3770 p -= len;
3771 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003772 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 }
3774
3775 /*
3776 * If we are still inside the quotes, and we passed a space, just
3777 * expand from there.
3778 */
3779 if (bow != NULL && in_quote)
3780 xp->xp_pattern = bow;
3781 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003782
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003783 /* For a shell command more chars need to be escaped. */
3784 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003785 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003786#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003787 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003788#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003789 /* When still after the command name expand executables. */
3790 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003791 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793
3794 /* Check for environment variable */
3795 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003796#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 || *xp->xp_pattern == '%'
3798#endif
3799 )
3800 {
3801 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3802 if (!vim_isIDc(*p))
3803 break;
3804 if (*p == NUL)
3805 {
3806 xp->xp_context = EXPAND_ENV_VARS;
3807 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003808#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3809 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003810 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003811 compl = EXPAND_ENV_VARS;
3812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 }
3814 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003815#if defined(FEAT_CMDL_COMPL)
3816 /* Check for user names */
3817 if (*xp->xp_pattern == '~')
3818 {
3819 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3820 ;
3821 /* Complete ~user only if it partially matches a user name.
3822 * A full match ~user<Tab> will be replaced by user's home
3823 * directory i.e. something like ~user<Tab> -> /home/user/ */
3824 if (*p == NUL && p > xp->xp_pattern + 1
3825 && match_user(xp->xp_pattern + 1) == 1)
3826 {
3827 xp->xp_context = EXPAND_USER;
3828 ++xp->xp_pattern;
3829 }
3830 }
3831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 }
3833
3834/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003835 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003837 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003839 case CMD_find:
3840 case CMD_sfind:
3841 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003842 if (xp->xp_context == EXPAND_FILES)
3843 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003844 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 case CMD_cd:
3846 case CMD_chdir:
3847 case CMD_lcd:
3848 case CMD_lchdir:
3849 if (xp->xp_context == EXPAND_FILES)
3850 xp->xp_context = EXPAND_DIRECTORIES;
3851 break;
3852 case CMD_help:
3853 xp->xp_context = EXPAND_HELP;
3854 xp->xp_pattern = arg;
3855 break;
3856
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003857 /* Command modifiers: return the argument.
3858 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003860 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 case CMD_belowright:
3862 case CMD_botright:
3863 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003864 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003865 case CMD_cdo:
3866 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003868 case CMD_debug:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003869 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 case CMD_folddoclosed:
3871 case CMD_folddoopen:
3872 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003873 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 case CMD_keepjumps:
3875 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003876 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003877 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003879 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003881 case CMD_noautocmd:
3882 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003884 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003886 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003887 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 case CMD_topleft:
3889 case CMD_verbose:
3890 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003891 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 return arg;
3893
Bram Moolenaar4f688582007-07-24 12:34:30 +00003894#ifdef FEAT_CMDL_COMPL
3895# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 case CMD_match:
3897 if (*arg == NUL || !ends_excmd(*arg))
3898 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003899 /* also complete "None" */
3900 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 arg = skipwhite(skiptowhite(arg));
3902 if (*arg != NUL)
3903 {
3904 xp->xp_context = EXPAND_NOTHING;
3905 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3906 }
3907 }
3908 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003909# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911/*
3912 * All completion for the +cmdline_compl feature goes here.
3913 */
3914
3915# ifdef FEAT_USR_CMDS
3916 case CMD_command:
3917 /* Check for attributes */
3918 while (*arg == '-')
3919 {
3920 arg++; /* Skip "-" */
3921 p = skiptowhite(arg);
3922 if (*p == NUL)
3923 {
3924 /* Cursor is still in the attribute */
3925 p = vim_strchr(arg, '=');
3926 if (p == NULL)
3927 {
3928 /* No "=", so complete attribute names */
3929 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3930 xp->xp_pattern = arg;
3931 return NULL;
3932 }
3933
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003934 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 * their arguments as well.
3936 */
3937 if (STRNICMP(arg, "complete", p - arg) == 0)
3938 {
3939 xp->xp_context = EXPAND_USER_COMPLETE;
3940 xp->xp_pattern = p + 1;
3941 return NULL;
3942 }
3943 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3944 {
3945 xp->xp_context = EXPAND_USER_NARGS;
3946 xp->xp_pattern = p + 1;
3947 return NULL;
3948 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003949 else if (STRNICMP(arg, "addr", p - arg) == 0)
3950 {
3951 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3952 xp->xp_pattern = p + 1;
3953 return NULL;
3954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 return NULL;
3956 }
3957 arg = skipwhite(p);
3958 }
3959
3960 /* After the attributes comes the new command name */
3961 p = skiptowhite(arg);
3962 if (*p == NUL)
3963 {
3964 xp->xp_context = EXPAND_USER_COMMANDS;
3965 xp->xp_pattern = arg;
3966 break;
3967 }
3968
3969 /* And finally comes a normal command */
3970 return skipwhite(p);
3971
3972 case CMD_delcommand:
3973 xp->xp_context = EXPAND_USER_COMMANDS;
3974 xp->xp_pattern = arg;
3975 break;
3976# endif
3977
3978 case CMD_global:
3979 case CMD_vglobal:
3980 delim = *arg; /* get the delimiter */
3981 if (delim)
3982 ++arg; /* skip delimiter if there is one */
3983
3984 while (arg[0] != NUL && arg[0] != delim)
3985 {
3986 if (arg[0] == '\\' && arg[1] != NUL)
3987 ++arg;
3988 ++arg;
3989 }
3990 if (arg[0] != NUL)
3991 return arg + 1;
3992 break;
3993 case CMD_and:
3994 case CMD_substitute:
3995 delim = *arg;
3996 if (delim)
3997 {
3998 /* skip "from" part */
3999 ++arg;
4000 arg = skip_regexp(arg, delim, p_magic, NULL);
4001 }
4002 /* skip "to" part */
4003 while (arg[0] != NUL && arg[0] != delim)
4004 {
4005 if (arg[0] == '\\' && arg[1] != NUL)
4006 ++arg;
4007 ++arg;
4008 }
4009 if (arg[0] != NUL) /* skip delimiter */
4010 ++arg;
4011 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
4012 ++arg;
4013 if (arg[0] != NUL)
4014 return arg;
4015 break;
4016 case CMD_isearch:
4017 case CMD_dsearch:
4018 case CMD_ilist:
4019 case CMD_dlist:
4020 case CMD_ijump:
4021 case CMD_psearch:
4022 case CMD_djump:
4023 case CMD_isplit:
4024 case CMD_dsplit:
4025 arg = skipwhite(skipdigits(arg)); /* skip count */
4026 if (*arg == '/') /* Match regexp, not just whole words */
4027 {
4028 for (++arg; *arg && *arg != '/'; arg++)
4029 if (*arg == '\\' && arg[1] != NUL)
4030 arg++;
4031 if (*arg)
4032 {
4033 arg = skipwhite(arg + 1);
4034
4035 /* Check for trailing illegal characters */
4036 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4037 xp->xp_context = EXPAND_NOTHING;
4038 else
4039 return arg;
4040 }
4041 }
4042 break;
4043#ifdef FEAT_AUTOCMD
4044 case CMD_autocmd:
4045 return set_context_in_autocmd(xp, arg, FALSE);
4046
4047 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004048 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 return set_context_in_autocmd(xp, arg, TRUE);
4050#endif
4051 case CMD_set:
4052 set_context_in_set_cmd(xp, arg, 0);
4053 break;
4054 case CMD_setglobal:
4055 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4056 break;
4057 case CMD_setlocal:
4058 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4059 break;
4060 case CMD_tag:
4061 case CMD_stag:
4062 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004063 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 case CMD_tselect:
4065 case CMD_stselect:
4066 case CMD_ptselect:
4067 case CMD_tjump:
4068 case CMD_stjump:
4069 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004070 if (*p_wop != NUL)
4071 xp->xp_context = EXPAND_TAGS_LISTFILES;
4072 else
4073 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 xp->xp_pattern = arg;
4075 break;
4076 case CMD_augroup:
4077 xp->xp_context = EXPAND_AUGROUP;
4078 xp->xp_pattern = arg;
4079 break;
4080#ifdef FEAT_SYN_HL
4081 case CMD_syntax:
4082 set_context_in_syntax_cmd(xp, arg);
4083 break;
4084#endif
4085#ifdef FEAT_EVAL
4086 case CMD_let:
4087 case CMD_if:
4088 case CMD_elseif:
4089 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004090 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 case CMD_echo:
4092 case CMD_echon:
4093 case CMD_execute:
4094 case CMD_echomsg:
4095 case CMD_echoerr:
4096 case CMD_call:
4097 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004098 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 break;
4100
4101 case CMD_unlet:
4102 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4103 arg = xp->xp_pattern + 1;
4104 xp->xp_context = EXPAND_USER_VARS;
4105 xp->xp_pattern = arg;
4106 break;
4107
4108 case CMD_function:
4109 case CMD_delfunction:
4110 xp->xp_context = EXPAND_USER_FUNC;
4111 xp->xp_pattern = arg;
4112 break;
4113
4114 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004115 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 break;
4117#endif
4118 case CMD_highlight:
4119 set_context_in_highlight_cmd(xp, arg);
4120 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004121#ifdef FEAT_CSCOPE
4122 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004123 case CMD_lcscope:
4124 case CMD_scscope:
4125 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004126 break;
4127#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004128#ifdef FEAT_SIGNS
4129 case CMD_sign:
4130 set_context_in_sign_cmd(xp, arg);
4131 break;
4132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133#ifdef FEAT_LISTCMDS
4134 case CMD_bdelete:
4135 case CMD_bwipeout:
4136 case CMD_bunload:
4137 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4138 arg = xp->xp_pattern + 1;
4139 /*FALLTHROUGH*/
4140 case CMD_buffer:
4141 case CMD_sbuffer:
4142 case CMD_checktime:
4143 xp->xp_context = EXPAND_BUFFERS;
4144 xp->xp_pattern = arg;
4145 break;
4146#endif
4147#ifdef FEAT_USR_CMDS
4148 case CMD_USER:
4149 case CMD_USER_BUF:
4150 if (compl != EXPAND_NOTHING)
4151 {
4152 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004153 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 {
4155# ifdef FEAT_MENU
4156 if (compl == EXPAND_MENUS)
4157 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4158# endif
4159 if (compl == EXPAND_COMMANDS)
4160 return arg;
4161 if (compl == EXPAND_MAPPINGS)
4162 return set_context_in_map_cmd(xp, (char_u *)"map",
4163 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004164 /* Find start of last argument. */
4165 p = arg;
4166 while (*p)
4167 {
4168 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004169 /* argument starts after a space */
4170 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004171 else if (*p == '\\' && *(p + 1) != NUL)
4172 ++p; /* skip over escaped character */
4173 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 xp->xp_pattern = arg;
4176 }
4177 xp->xp_context = compl;
4178 }
4179 break;
4180#endif
4181 case CMD_map: case CMD_noremap:
4182 case CMD_nmap: case CMD_nnoremap:
4183 case CMD_vmap: case CMD_vnoremap:
4184 case CMD_omap: case CMD_onoremap:
4185 case CMD_imap: case CMD_inoremap:
4186 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004187 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004188 case CMD_smap: case CMD_snoremap:
4189 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004191 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 case CMD_unmap:
4193 case CMD_nunmap:
4194 case CMD_vunmap:
4195 case CMD_ounmap:
4196 case CMD_iunmap:
4197 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004198 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004199 case CMD_sunmap:
4200 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004202 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 case CMD_abbreviate: case CMD_noreabbrev:
4204 case CMD_cabbrev: case CMD_cnoreabbrev:
4205 case CMD_iabbrev: case CMD_inoreabbrev:
4206 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004207 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 case CMD_unabbreviate:
4209 case CMD_cunabbrev:
4210 case CMD_iunabbrev:
4211 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004212 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213#ifdef FEAT_MENU
4214 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4215 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4216 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4217 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4218 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4219 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4220 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4221 case CMD_tmenu: case CMD_tunmenu:
4222 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4223 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4224#endif
4225
4226 case CMD_colorscheme:
4227 xp->xp_context = EXPAND_COLORS;
4228 xp->xp_pattern = arg;
4229 break;
4230
4231 case CMD_compiler:
4232 xp->xp_context = EXPAND_COMPILER;
4233 xp->xp_pattern = arg;
4234 break;
4235
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004236 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004237 xp->xp_context = EXPAND_OWNSYNTAX;
4238 xp->xp_pattern = arg;
4239 break;
4240
4241 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004242 xp->xp_context = EXPAND_FILETYPE;
4243 xp->xp_pattern = arg;
4244 break;
4245
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004246 case CMD_packadd:
4247 xp->xp_context = EXPAND_PACKADD;
4248 xp->xp_pattern = arg;
4249 break;
4250
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4252 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4253 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004254 p = skiptowhite(arg);
4255 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 {
4257 xp->xp_context = EXPAND_LANGUAGE;
4258 xp->xp_pattern = arg;
4259 }
4260 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004261 {
4262 if ( STRNCMP(arg, "messages", p - arg) == 0
4263 || STRNCMP(arg, "ctype", p - arg) == 0
4264 || STRNCMP(arg, "time", p - arg) == 0)
4265 {
4266 xp->xp_context = EXPAND_LOCALES;
4267 xp->xp_pattern = skipwhite(p);
4268 }
4269 else
4270 xp->xp_context = EXPAND_NOTHING;
4271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 break;
4273#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004274#if defined(FEAT_PROFILE)
4275 case CMD_profile:
4276 set_context_in_profile_cmd(xp, arg);
4277 break;
4278#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004279 case CMD_behave:
4280 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004281 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004282 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004284 case CMD_messages:
4285 xp->xp_context = EXPAND_MESSAGES;
4286 xp->xp_pattern = arg;
4287 break;
4288
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004289#if defined(FEAT_CMDHIST)
4290 case CMD_history:
4291 xp->xp_context = EXPAND_HISTORY;
4292 xp->xp_pattern = arg;
4293 break;
4294#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004295#if defined(FEAT_PROFILE)
4296 case CMD_syntime:
4297 xp->xp_context = EXPAND_SYNTIME;
4298 xp->xp_pattern = arg;
4299 break;
4300#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004301
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302#endif /* FEAT_CMDL_COMPL */
4303
4304 default:
4305 break;
4306 }
4307 return NULL;
4308}
4309
4310/*
4311 * skip a range specifier of the form: addr [,addr] [;addr] ..
4312 *
4313 * Backslashed delimiters after / or ? will be skipped, and commands will
4314 * not be expanded between /'s and ?'s or after "'".
4315 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004316 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 * Returns the "cmd" pointer advanced to beyond the range.
4318 */
4319 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004320skip_range(
4321 char_u *cmd,
4322 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004324 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325
Bram Moolenaardf177f62005-02-22 08:39:57 +00004326 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 {
4328 if (*cmd == '\'')
4329 {
4330 if (*++cmd == NUL && ctx != NULL)
4331 *ctx = EXPAND_NOTHING;
4332 }
4333 else if (*cmd == '/' || *cmd == '?')
4334 {
4335 delim = *cmd++;
4336 while (*cmd != NUL && *cmd != delim)
4337 if (*cmd++ == '\\' && *cmd != NUL)
4338 ++cmd;
4339 if (*cmd == NUL && ctx != NULL)
4340 *ctx = EXPAND_NOTHING;
4341 }
4342 if (*cmd != NUL)
4343 ++cmd;
4344 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004345
4346 /* Skip ":" and white space. */
4347 while (*cmd == ':')
4348 cmd = skipwhite(cmd + 1);
4349
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 return cmd;
4351}
4352
4353/*
4354 * get a single EX address
4355 *
4356 * Set ptr to the next character after the part that was interpreted.
4357 * Set ptr to NULL when an error is encountered.
4358 *
4359 * Return MAXLNUM when no Ex address was found.
4360 */
4361 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004362get_address(
4363 exarg_T *eap UNUSED,
4364 char_u **ptr,
4365 int addr_type, /* flag: one of ADDR_LINES, ... */
4366 int skip, /* only skip the address, don't use it */
Bram Moolenaarded27822017-01-02 14:27:34 +01004367 int to_other_file, /* flag: may jump to other file */
4368 int address_count) /* 1 for first address, >1 after comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369{
4370 int c;
4371 int i;
4372 long n;
4373 char_u *cmd;
4374 pos_T pos;
4375 pos_T *fp;
4376 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004377 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378
4379 cmd = skipwhite(*ptr);
4380 lnum = MAXLNUM;
4381 do
4382 {
4383 switch (*cmd)
4384 {
4385 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004386 ++cmd;
4387 switch (addr_type)
4388 {
4389 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 lnum = curwin->w_cursor.lnum;
4391 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004392 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004393 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004394 break;
4395 case ADDR_ARGUMENTS:
4396 lnum = curwin->w_arg_idx + 1;
4397 break;
4398 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004399 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004400 lnum = curbuf->b_fnum;
4401 break;
4402 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004403 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004404 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004405#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004406 case ADDR_QUICKFIX:
4407 lnum = qf_get_cur_valid_idx(eap);
4408 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004409#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004410 }
4411 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412
4413 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004414 ++cmd;
4415 switch (addr_type)
4416 {
4417 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 lnum = curbuf->b_ml.ml_line_count;
4419 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004420 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004421 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004422 break;
4423 case ADDR_ARGUMENTS:
4424 lnum = ARGCOUNT;
4425 break;
4426 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004427 buf = lastbuf;
4428 while (buf->b_ml.ml_mfp == NULL)
4429 {
4430 if (buf->b_prev == NULL)
4431 break;
4432 buf = buf->b_prev;
4433 }
4434 lnum = buf->b_fnum;
4435 break;
4436 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004437 lnum = lastbuf->b_fnum;
4438 break;
4439 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004440 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004441 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004442#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004443 case ADDR_QUICKFIX:
4444 lnum = qf_get_size(eap);
4445 if (lnum == 0)
4446 lnum = 1;
4447 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004448#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004449 }
4450 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451
4452 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004453 if (*++cmd == NUL)
4454 {
4455 cmd = NULL;
4456 goto error;
4457 }
4458 if (addr_type != ADDR_LINES)
4459 {
4460 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004461 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004462 goto error;
4463 }
4464 if (skip)
4465 ++cmd;
4466 else
4467 {
4468 /* Only accept a mark in another file when it is
4469 * used by itself: ":'M". */
4470 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4471 ++cmd;
4472 if (fp == (pos_T *)-1)
4473 /* Jumped to another file. */
4474 lnum = curwin->w_cursor.lnum;
4475 else
4476 {
4477 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 {
4479 cmd = NULL;
4480 goto error;
4481 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004482 lnum = fp->lnum;
4483 }
4484 }
4485 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486
4487 case '/':
4488 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004489 c = *cmd++;
4490 if (addr_type != ADDR_LINES)
4491 {
4492 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004493 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004494 goto error;
4495 }
4496 if (skip) /* skip "/pat/" */
4497 {
4498 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4499 if (*cmd == c)
4500 ++cmd;
4501 }
4502 else
4503 {
4504 pos = curwin->w_cursor; /* save curwin->w_cursor */
4505 /*
4506 * When '/' or '?' follows another address, start
4507 * from there.
4508 */
4509 if (lnum != MAXLNUM)
4510 curwin->w_cursor.lnum = lnum;
4511 /*
4512 * Start a forward search at the end of the line.
4513 * Start a backward search at the start of the line.
4514 * This makes sure we never match in the current
4515 * line, and can match anywhere in the
4516 * next/previous line.
4517 */
4518 if (c == '/')
4519 curwin->w_cursor.col = MAXCOL;
4520 else
4521 curwin->w_cursor.col = 0;
4522 searchcmdlen = 0;
4523 if (!do_search(NULL, c, cmd, 1L,
4524 SEARCH_HIS | SEARCH_MSG, NULL))
4525 {
4526 curwin->w_cursor = pos;
4527 cmd = NULL;
4528 goto error;
4529 }
4530 lnum = curwin->w_cursor.lnum;
4531 curwin->w_cursor = pos;
4532 /* adjust command string pointer */
4533 cmd += searchcmdlen;
4534 }
4535 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536
4537 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004538 ++cmd;
4539 if (addr_type != ADDR_LINES)
4540 {
4541 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004542 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004543 goto error;
4544 }
4545 if (*cmd == '&')
4546 i = RE_SUBST;
4547 else if (*cmd == '?' || *cmd == '/')
4548 i = RE_SEARCH;
4549 else
4550 {
4551 EMSG(_(e_backslash));
4552 cmd = NULL;
4553 goto error;
4554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004556 if (!skip)
4557 {
4558 /*
4559 * When search follows another address, start from
4560 * there.
4561 */
4562 if (lnum != MAXLNUM)
4563 pos.lnum = lnum;
4564 else
4565 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004567 /*
4568 * Start the search just like for the above
4569 * do_search().
4570 */
4571 if (*cmd != '?')
4572 pos.col = MAXCOL;
4573 else
4574 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004575#ifdef FEAT_VIRTUALEDIT
4576 pos.coladd = 0;
4577#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004578 if (searchit(curwin, curbuf, &pos,
4579 *cmd == '?' ? BACKWARD : FORWARD,
4580 (char_u *)"", 1L, SEARCH_MSG,
4581 i, (linenr_T)0, NULL) != FAIL)
4582 lnum = pos.lnum;
4583 else
4584 {
4585 cmd = NULL;
4586 goto error;
4587 }
4588 }
4589 ++cmd;
4590 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591
4592 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004593 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4594 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 }
4596
4597 for (;;)
4598 {
4599 cmd = skipwhite(cmd);
4600 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4601 break;
4602
4603 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004604 {
4605 switch (addr_type)
4606 {
4607 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004608 /* "+1" is same as ".+1" */
4609 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004610 break;
4611 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004612 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004613 break;
4614 case ADDR_ARGUMENTS:
4615 lnum = curwin->w_arg_idx + 1;
4616 break;
4617 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004618 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004619 lnum = curbuf->b_fnum;
4620 break;
4621 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004622 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004623 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004624#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004625 case ADDR_QUICKFIX:
4626 lnum = qf_get_cur_valid_idx(eap);
4627 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004628#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004629 }
4630 }
4631
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 if (VIM_ISDIGIT(*cmd))
4633 i = '+'; /* "number" is same as "+number" */
4634 else
4635 i = *cmd++;
4636 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4637 n = 1;
4638 else
4639 n = getdigits(&cmd);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004640 if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004641 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004642 lnum = compute_buffer_local_count(
4643 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004645 {
4646#ifdef FEAT_FOLDING
4647 /* Relative line addressing, need to adjust for folded lines
4648 * now, but only do it after the first address. */
4649 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4650 && address_count >= 2)
4651 (void)hasFolding(lnum, NULL, &lnum);
4652#endif
4653 if (i == '-')
4654 lnum -= n;
4655 else
4656 lnum += n;
4657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 }
4659 } while (*cmd == '/' || *cmd == '?');
4660
4661error:
4662 *ptr = cmd;
4663 return lnum;
4664}
4665
4666/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004667 * Get flags from an Ex command argument.
4668 */
4669 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004670get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004671{
4672 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4673 {
4674 if (*eap->arg == 'l')
4675 eap->flags |= EXFLAG_LIST;
4676 else if (*eap->arg == 'p')
4677 eap->flags |= EXFLAG_PRINT;
4678 else
4679 eap->flags |= EXFLAG_NR;
4680 eap->arg = skipwhite(eap->arg + 1);
4681 }
4682}
4683
4684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 * Function called for command which is Not Implemented. NI!
4686 */
4687 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004688ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689{
4690 if (!eap->skip)
4691 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4692}
4693
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004694#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695/*
4696 * Function called for script command which is Not Implemented. NI!
4697 * Skips over ":perl <<EOF" constructs.
4698 */
4699 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004700ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701{
4702 if (!eap->skip)
4703 ex_ni(eap);
4704 else
4705 vim_free(script_get(eap, eap->arg));
4706}
4707#endif
4708
4709/*
4710 * Check range in Ex command for validity.
4711 * Return NULL when valid, error message when invalid.
4712 */
4713 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004714invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004716 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 if ( eap->line1 < 0
4718 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004719 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004721
4722 if (eap->argt & RANGE)
4723 {
4724 switch(eap->addr_type)
4725 {
4726 case ADDR_LINES:
4727 if (!(eap->argt & NOTADR)
4728 && eap->line2 > curbuf->b_ml.ml_line_count
4729#ifdef FEAT_DIFF
4730 + (eap->cmdidx == CMD_diffget)
4731#endif
4732 )
4733 return (char_u *)_(e_invrange);
4734 break;
4735 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004736 /* add 1 if ARGCOUNT is 0 */
4737 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004738 return (char_u *)_(e_invrange);
4739 break;
4740 case ADDR_BUFFERS:
4741 if (eap->line1 < firstbuf->b_fnum
4742 || eap->line2 > lastbuf->b_fnum)
4743 return (char_u *)_(e_invrange);
4744 break;
4745 case ADDR_LOADED_BUFFERS:
4746 buf = firstbuf;
4747 while (buf->b_ml.ml_mfp == NULL)
4748 {
4749 if (buf->b_next == NULL)
4750 return (char_u *)_(e_invrange);
4751 buf = buf->b_next;
4752 }
4753 if (eap->line1 < buf->b_fnum)
4754 return (char_u *)_(e_invrange);
4755 buf = lastbuf;
4756 while (buf->b_ml.ml_mfp == NULL)
4757 {
4758 if (buf->b_prev == NULL)
4759 return (char_u *)_(e_invrange);
4760 buf = buf->b_prev;
4761 }
4762 if (eap->line2 > buf->b_fnum)
4763 return (char_u *)_(e_invrange);
4764 break;
4765 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004766 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004767 return (char_u *)_(e_invrange);
4768 break;
4769 case ADDR_TABS:
4770 if (eap->line2 > LAST_TAB_NR)
4771 return (char_u *)_(e_invrange);
4772 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004773#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004774 case ADDR_QUICKFIX:
4775 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4776 return (char_u *)_(e_invrange);
4777 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004778#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004779 }
4780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 return NULL;
4782}
4783
4784/*
4785 * Correct the range for zero line number, if required.
4786 */
4787 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004788correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789{
4790 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4791 {
4792 if (eap->line1 == 0)
4793 eap->line1 = 1;
4794 if (eap->line2 == 0)
4795 eap->line2 = 1;
4796 }
4797}
4798
Bram Moolenaar748bf032005-02-02 23:04:36 +00004799#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004800static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004801
4802/*
4803 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4804 * pattern. Otherwise return eap->arg.
4805 */
4806 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004807skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004808{
4809 char_u *p = eap->arg;
4810
Bram Moolenaara37420f2006-02-04 22:37:47 +00004811 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4812 || eap->cmdidx == CMD_vimgrepadd
4813 || eap->cmdidx == CMD_lvimgrepadd
4814 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004815 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004816 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004817 if (p == NULL)
4818 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004819 }
4820 return p;
4821}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004822
4823/*
4824 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4825 * in the command line, so that things like % get expanded.
4826 */
4827 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004828replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004829{
4830 char_u *new_cmdline;
4831 char_u *program;
4832 char_u *pos;
4833 char_u *ptr;
4834 int len;
4835 int i;
4836
4837 /*
4838 * Don't do it when ":vimgrep" is used for ":grep".
4839 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004840 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4841 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4842 || eap->cmdidx == CMD_grepadd
4843 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004844 && !grep_internal(eap->cmdidx))
4845 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004846 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4847 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004848 {
4849 if (*curbuf->b_p_gp == NUL)
4850 program = p_gp;
4851 else
4852 program = curbuf->b_p_gp;
4853 }
4854 else
4855 {
4856 if (*curbuf->b_p_mp == NUL)
4857 program = p_mp;
4858 else
4859 program = curbuf->b_p_mp;
4860 }
4861
4862 p = skipwhite(p);
4863
4864 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4865 {
4866 /* replace $* by given arguments */
4867 i = 1;
4868 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4869 ++i;
4870 len = (int)STRLEN(p);
4871 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4872 if (new_cmdline == NULL)
4873 return NULL; /* out of memory */
4874 ptr = new_cmdline;
4875 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4876 {
4877 i = (int)(pos - program);
4878 STRNCPY(ptr, program, i);
4879 STRCPY(ptr += i, p);
4880 ptr += len;
4881 program = pos + 2;
4882 }
4883 STRCPY(ptr, program);
4884 }
4885 else
4886 {
4887 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4888 if (new_cmdline == NULL)
4889 return NULL; /* out of memory */
4890 STRCPY(new_cmdline, program);
4891 STRCAT(new_cmdline, " ");
4892 STRCAT(new_cmdline, p);
4893 }
4894 msg_make(p);
4895
4896 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4897 vim_free(*cmdlinep);
4898 *cmdlinep = new_cmdline;
4899 p = new_cmdline;
4900 }
4901 return p;
4902}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004903#endif
4904
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905/*
4906 * Expand file name in Ex command argument.
4907 * Return FAIL for failure, OK otherwise.
4908 */
4909 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004910expand_filename(
4911 exarg_T *eap,
4912 char_u **cmdlinep,
4913 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914{
4915 int has_wildcards; /* need to expand wildcards */
4916 char_u *repl;
4917 int srclen;
4918 char_u *p;
4919 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004920 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921
Bram Moolenaar748bf032005-02-02 23:04:36 +00004922#ifdef FEAT_QUICKFIX
4923 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4924 p = skip_grep_pat(eap);
4925#else
4926 p = eap->arg;
4927#endif
4928
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 /*
4930 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4931 * the file name contains a wildcard it should not cause expanding.
4932 * (it will be expanded anyway if there is a wildcard before replacing).
4933 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004934 has_wildcards = mch_has_wildcard(p);
4935 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004937#ifdef FEAT_EVAL
4938 /* Skip over `=expr`, wildcards in it are not expanded. */
4939 if (p[0] == '`' && p[1] == '=')
4940 {
4941 p += 2;
4942 (void)skip_expr(&p);
4943 if (*p == '`')
4944 ++p;
4945 continue;
4946 }
4947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 /*
4949 * Quick check if this cannot be the start of a special string.
4950 * Also removes backslash before '%', '#' and '<'.
4951 */
4952 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4953 {
4954 ++p;
4955 continue;
4956 }
4957
4958 /*
4959 * Try to find a match at this position.
4960 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004961 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4962 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 if (*errormsgp != NULL) /* error detected */
4964 return FAIL;
4965 if (repl == NULL) /* no match found */
4966 {
4967 p += srclen;
4968 continue;
4969 }
4970
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004971 /* Wildcards won't be expanded below, the replacement is taken
4972 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4973 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4974 {
4975 char_u *l = repl;
4976
4977 repl = expand_env_save(repl);
4978 vim_free(l);
4979 }
4980
Bram Moolenaar63b92542007-03-27 14:57:09 +00004981 /* Need to escape white space et al. with a backslash.
4982 * Don't do this for:
4983 * - replacement that already has been escaped: "##"
4984 * - shell commands (may have to use quotes instead).
4985 * - non-unix systems when there is a single argument (spaces don't
4986 * separate arguments then).
4987 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004989 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 && eap->cmdidx != CMD_bang
4991 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004992 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004994 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004996 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997#ifndef UNIX
4998 && !(eap->argt & NOSPC)
4999#endif
5000 )
5001 {
5002 char_u *l;
5003#ifdef BACKSLASH_IN_FILENAME
5004 /* Don't escape a backslash here, because rem_backslash() doesn't
5005 * remove it later. */
5006 static char_u *nobslash = (char_u *)" \t\"|";
5007# define ESCAPE_CHARS nobslash
5008#else
5009# define ESCAPE_CHARS escape_chars
5010#endif
5011
5012 for (l = repl; *l; ++l)
5013 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5014 {
5015 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5016 if (l != NULL)
5017 {
5018 vim_free(repl);
5019 repl = l;
5020 }
5021 break;
5022 }
5023 }
5024
5025 /* For a shell command a '!' must be escaped. */
5026 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005027 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 {
5029 char_u *l;
5030
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005031 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 if (l != NULL)
5033 {
5034 vim_free(repl);
5035 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 }
5037 }
5038
5039 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5040 vim_free(repl);
5041 if (p == NULL)
5042 return FAIL;
5043 }
5044
5045 /*
5046 * One file argument: Expand wildcards.
5047 * Don't do this with ":r !command" or ":w !command".
5048 */
5049 if ((eap->argt & NOSPC) && !eap->usefilter)
5050 {
5051 /*
5052 * May do this twice:
5053 * 1. Replace environment variables.
5054 * 2. Replace any other wildcards, remove backslashes.
5055 */
5056 for (n = 1; n <= 2; ++n)
5057 {
5058 if (n == 2)
5059 {
5060#ifdef UNIX
5061 /*
5062 * Only for Unix we check for more than one file name.
5063 * For other systems spaces are considered to be part
5064 * of the file name.
5065 * Only check here if there is no wildcard, otherwise
5066 * ExpandOne() will check for errors. This allows
5067 * ":e `ls ve*.c`" on Unix.
5068 */
5069 if (!has_wildcards)
5070 for (p = eap->arg; *p; ++p)
5071 {
5072 /* skip escaped characters */
5073 if (p[1] && (*p == '\\' || *p == Ctrl_V))
5074 ++p;
5075 else if (vim_iswhite(*p))
5076 {
5077 *errormsgp = (char_u *)_("E172: Only one file name allowed");
5078 return FAIL;
5079 }
5080 }
5081#endif
5082
5083 /*
5084 * Halve the number of backslashes (this is Vi compatible).
5085 * For Unix and OS/2, when wildcards are expanded, this is
5086 * done by ExpandOne() below.
5087 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005088#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 if (!has_wildcards)
5090#endif
5091 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 }
5093
5094 if (has_wildcards)
5095 {
5096 if (n == 1)
5097 {
5098 /*
5099 * First loop: May expand environment variables. This
5100 * can be done much faster with expand_env() than with
5101 * something else (e.g., calling a shell).
5102 * After expanding environment variables, check again
5103 * if there are still wildcards present.
5104 */
5105 if (vim_strchr(eap->arg, '$') != NULL
5106 || vim_strchr(eap->arg, '~') != NULL)
5107 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005108 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005109 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 has_wildcards = mch_has_wildcard(NameBuff);
5111 p = NameBuff;
5112 }
5113 else
5114 p = NULL;
5115 }
5116 else /* n == 2 */
5117 {
5118 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005119 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120
5121 ExpandInit(&xpc);
5122 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005123 if (p_wic)
5124 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005126 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 if (p == NULL)
5128 return FAIL;
5129 }
5130 if (p != NULL)
5131 {
5132 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5133 p, cmdlinep);
5134 if (n == 2) /* p came from ExpandOne() */
5135 vim_free(p);
5136 }
5137 }
5138 }
5139 }
5140 return OK;
5141}
5142
5143/*
5144 * Replace part of the command line, keeping eap->cmd, eap->arg and
5145 * eap->nextcmd correct.
5146 * "src" points to the part that is to be replaced, of length "srclen".
5147 * "repl" is the replacement string.
5148 * Returns a pointer to the character after the replaced string.
5149 * Returns NULL for failure.
5150 */
5151 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005152repl_cmdline(
5153 exarg_T *eap,
5154 char_u *src,
5155 int srclen,
5156 char_u *repl,
5157 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158{
5159 int len;
5160 int i;
5161 char_u *new_cmdline;
5162
5163 /*
5164 * The new command line is build in new_cmdline[].
5165 * First allocate it.
5166 * Careful: a "+cmd" argument may have been NUL terminated.
5167 */
5168 len = (int)STRLEN(repl);
5169 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005170 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5172 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5173 return NULL; /* out of memory! */
5174
5175 /*
5176 * Copy the stuff before the expanded part.
5177 * Copy the expanded stuff.
5178 * Copy what came after the expanded part.
5179 * Copy the next commands, if there are any.
5180 */
5181 i = (int)(src - *cmdlinep); /* length of part before match */
5182 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005183
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 mch_memmove(new_cmdline + i, repl, (size_t)len);
5185 i += len; /* remember the end of the string */
5186 STRCPY(new_cmdline + i, src + srclen);
5187 src = new_cmdline + i; /* remember where to continue */
5188
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005189 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 {
5191 i = (int)STRLEN(new_cmdline) + 1;
5192 STRCPY(new_cmdline + i, eap->nextcmd);
5193 eap->nextcmd = new_cmdline + i;
5194 }
5195 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5196 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5197 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5198 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5199 vim_free(*cmdlinep);
5200 *cmdlinep = new_cmdline;
5201
5202 return src;
5203}
5204
5205/*
5206 * Check for '|' to separate commands and '"' to start comments.
5207 */
5208 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005209separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210{
5211 char_u *p;
5212
Bram Moolenaar86b68352004-12-27 21:59:20 +00005213#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005214 p = skip_grep_pat(eap);
5215#else
5216 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005217#endif
5218
5219 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 {
5221 if (*p == Ctrl_V)
5222 {
5223 if (eap->argt & (USECTRLV | XFILE))
5224 ++p; /* skip CTRL-V and next char */
5225 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005226 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005227 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 if (*p == NUL) /* stop at NUL after CTRL-V */
5229 break;
5230 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005231
5232#ifdef FEAT_EVAL
5233 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005234 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005235 {
5236 p += 2;
5237 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005238 }
5239#endif
5240
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 /* Check for '"': start of comment or '|': next command */
5242 /* :@" and :*" do not start a comment!
5243 * :redir @" doesn't either. */
5244 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5245 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5246 || p != eap->arg)
5247 && (eap->cmdidx != CMD_redir
5248 || p != eap->arg + 1 || p[-1] != '@'))
5249 || *p == '|' || *p == '\n')
5250 {
5251 /*
5252 * We remove the '\' before the '|', unless USECTRLV is used
5253 * AND 'b' is present in 'cpoptions'.
5254 */
5255 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5256 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5257 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005258 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 --p;
5260 }
5261 else
5262 {
5263 eap->nextcmd = check_nextcmd(p);
5264 *p = NUL;
5265 break;
5266 }
5267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005269
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5271 del_trailing_spaces(eap->arg);
5272}
5273
5274/*
5275 * get + command from ex argument
5276 */
5277 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005278getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279{
5280 char_u *arg = *argp;
5281 char_u *command = NULL;
5282
5283 if (*arg == '+') /* +[command] */
5284 {
5285 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005286 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 command = dollar_command;
5288 else
5289 {
5290 command = arg;
5291 arg = skip_cmd_arg(command, TRUE);
5292 if (*arg != NUL)
5293 *arg++ = NUL; /* terminate command with NUL */
5294 }
5295
5296 arg = skipwhite(arg); /* skip over spaces */
5297 *argp = arg;
5298 }
5299 return command;
5300}
5301
5302/*
5303 * Find end of "+command" argument. Skip over "\ " and "\\".
5304 */
5305 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005306skip_cmd_arg(
5307 char_u *p,
5308 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309{
5310 while (*p && !vim_isspace(*p))
5311 {
5312 if (*p == '\\' && p[1] != NUL)
5313 {
5314 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005315 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 else
5317 ++p;
5318 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005319 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 }
5321 return p;
5322}
5323
5324/*
5325 * Get "++opt=arg" argument.
5326 * Return FAIL or OK.
5327 */
5328 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005329getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330{
5331 char_u *arg = eap->arg + 2;
5332 int *pp = NULL;
5333#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005334 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 char_u *p;
5336#endif
5337
5338 /* ":edit ++[no]bin[ary] file" */
5339 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5340 {
5341 if (*arg == 'n')
5342 {
5343 arg += 2;
5344 eap->force_bin = FORCE_NOBIN;
5345 }
5346 else
5347 eap->force_bin = FORCE_BIN;
5348 if (!checkforcmd(&arg, "binary", 3))
5349 return FAIL;
5350 eap->arg = skipwhite(arg);
5351 return OK;
5352 }
5353
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005354 /* ":read ++edit file" */
5355 if (STRNCMP(arg, "edit", 4) == 0)
5356 {
5357 eap->read_edit = TRUE;
5358 eap->arg = skipwhite(arg + 4);
5359 return OK;
5360 }
5361
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 if (STRNCMP(arg, "ff", 2) == 0)
5363 {
5364 arg += 2;
5365 pp = &eap->force_ff;
5366 }
5367 else if (STRNCMP(arg, "fileformat", 10) == 0)
5368 {
5369 arg += 10;
5370 pp = &eap->force_ff;
5371 }
5372#ifdef FEAT_MBYTE
5373 else if (STRNCMP(arg, "enc", 3) == 0)
5374 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005375 if (STRNCMP(arg, "encoding", 8) == 0)
5376 arg += 8;
5377 else
5378 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 pp = &eap->force_enc;
5380 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005381 else if (STRNCMP(arg, "bad", 3) == 0)
5382 {
5383 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005384 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386#endif
5387
5388 if (pp == NULL || *arg != '=')
5389 return FAIL;
5390
5391 ++arg;
5392 *pp = (int)(arg - eap->cmd);
5393 arg = skip_cmd_arg(arg, FALSE);
5394 eap->arg = skipwhite(arg);
5395 *arg = NUL;
5396
5397#ifdef FEAT_MBYTE
5398 if (pp == &eap->force_ff)
5399 {
5400#endif
5401 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5402 return FAIL;
5403#ifdef FEAT_MBYTE
5404 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005405 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 {
5407 /* Make 'fileencoding' lower case. */
5408 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5409 *p = TOLOWER_ASC(*p);
5410 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005411 else
5412 {
5413 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5414 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005415 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005416 if (STRICMP(p, "keep") == 0)
5417 eap->bad_char = BAD_KEEP;
5418 else if (STRICMP(p, "drop") == 0)
5419 eap->bad_char = BAD_DROP;
5420 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5421 eap->bad_char = *p;
5422 else
5423 return FAIL;
5424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425#endif
5426
5427 return OK;
5428}
5429
5430/*
5431 * ":abbreviate" and friends.
5432 */
5433 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005434ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435{
5436 do_exmap(eap, TRUE); /* almost the same as mapping */
5437}
5438
5439/*
5440 * ":map" and friends.
5441 */
5442 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005443ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444{
5445 /*
5446 * If we are sourcing .exrc or .vimrc in current directory we
5447 * print the mappings for security reasons.
5448 */
5449 if (secure)
5450 {
5451 secure = 2;
5452 msg_outtrans(eap->cmd);
5453 msg_putchar('\n');
5454 }
5455 do_exmap(eap, FALSE);
5456}
5457
5458/*
5459 * ":unmap" and friends.
5460 */
5461 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005462ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463{
5464 do_exmap(eap, FALSE);
5465}
5466
5467/*
5468 * ":mapclear" and friends.
5469 */
5470 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005471ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472{
5473 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5474}
5475
5476/*
5477 * ":abclear" and friends.
5478 */
5479 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005480ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481{
5482 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5483}
5484
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005485#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005487ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488{
5489 /*
5490 * Disallow auto commands from .exrc and .vimrc in current
5491 * directory for security reasons.
5492 */
5493 if (secure)
5494 {
5495 secure = 2;
5496 eap->errmsg = e_curdir;
5497 }
5498 else if (eap->cmdidx == CMD_autocmd)
5499 do_autocmd(eap->arg, eap->forceit);
5500 else
5501 do_augroup(eap->arg, eap->forceit);
5502}
5503
5504/*
5505 * ":doautocmd": Apply the automatic commands to the current buffer.
5506 */
5507 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005508ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005510 char_u *arg = eap->arg;
5511 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005512 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005513
Bram Moolenaar1610d052016-06-09 22:53:01 +02005514 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5515 /* Only when there is no <nomodeline>. */
5516 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005517 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518}
5519#endif
5520
5521#ifdef FEAT_LISTCMDS
5522/*
5523 * :[N]bunload[!] [N] [bufname] unload buffer
5524 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5525 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5526 */
5527 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005528ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529{
5530 eap->errmsg = do_bufdel(
5531 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5532 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5533 : DOBUF_UNLOAD, eap->arg,
5534 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5535}
5536
5537/*
5538 * :[N]buffer [N] to buffer N
5539 * :[N]sbuffer [N] to buffer N
5540 */
5541 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005542ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543{
5544 if (*eap->arg)
5545 eap->errmsg = e_trailing;
5546 else
5547 {
5548 if (eap->addr_count == 0) /* default is current buffer */
5549 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5550 else
5551 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005552 if (eap->do_ecmd_cmd != NULL)
5553 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 }
5555}
5556
5557/*
5558 * :[N]bmodified [N] to next mod. buffer
5559 * :[N]sbmodified [N] to next mod. buffer
5560 */
5561 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005562ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563{
5564 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005565 if (eap->do_ecmd_cmd != NULL)
5566 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567}
5568
5569/*
5570 * :[N]bnext [N] to next buffer
5571 * :[N]sbnext [N] split and to next buffer
5572 */
5573 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005574ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575{
5576 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005577 if (eap->do_ecmd_cmd != NULL)
5578 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579}
5580
5581/*
5582 * :[N]bNext [N] to previous buffer
5583 * :[N]bprevious [N] to previous buffer
5584 * :[N]sbNext [N] split and to previous buffer
5585 * :[N]sbprevious [N] split and to previous buffer
5586 */
5587 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005588ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589{
5590 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005591 if (eap->do_ecmd_cmd != NULL)
5592 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593}
5594
5595/*
5596 * :brewind to first buffer
5597 * :bfirst to first buffer
5598 * :sbrewind split and to first buffer
5599 * :sbfirst split and to first buffer
5600 */
5601 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005602ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603{
5604 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005605 if (eap->do_ecmd_cmd != NULL)
5606 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607}
5608
5609/*
5610 * :blast to last buffer
5611 * :sblast split and to last buffer
5612 */
5613 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005614ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615{
5616 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005617 if (eap->do_ecmd_cmd != NULL)
5618 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619}
5620#endif
5621
5622 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005623ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624{
5625 return (c == NUL || c == '|' || c == '"' || c == '\n');
5626}
5627
5628#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5629 || defined(PROTO)
5630/*
5631 * Return the next command, after the first '|' or '\n'.
5632 * Return NULL if not found.
5633 */
5634 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005635find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636{
5637 while (*p != '|' && *p != '\n')
5638 {
5639 if (*p == NUL)
5640 return NULL;
5641 ++p;
5642 }
5643 return (p + 1);
5644}
5645#endif
5646
5647/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005648 * Check if *p is a separator between Ex commands, skipping over white space.
5649 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 */
5651 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005652check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005654 char_u *s = skipwhite(p);
5655
5656 if (*s == '|' || *s == '\n')
5657 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 else
5659 return NULL;
5660}
5661
5662/*
5663 * - if there are more files to edit
5664 * - and this is the last window
5665 * - and forceit not used
5666 * - and not repeated twice on a row
5667 * return FAIL and give error message if 'message' TRUE
5668 * return OK otherwise
5669 */
5670 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005671check_more(
5672 int message, /* when FALSE check only, no messages */
5673 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674{
5675 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5676
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005677 if (!forceit && only_one_window()
5678 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 {
5680 if (message)
5681 {
5682#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5683 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5684 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005685 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686
5687 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005688 vim_strncpy(buff,
5689 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005690 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005692 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 _("%d more files to edit. Quit anyway?"), n);
5694 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5695 return OK;
5696 return FAIL;
5697 }
5698#endif
5699 if (n == 1)
5700 EMSG(_("E173: 1 more file to edit"));
5701 else
5702 EMSGN(_("E173: %ld more files to edit"), n);
5703 quitmore = 2; /* next try to quit is allowed */
5704 }
5705 return FAIL;
5706 }
5707 return OK;
5708}
5709
5710#ifdef FEAT_CMDL_COMPL
5711/*
5712 * Function given to ExpandGeneric() to obtain the list of command names.
5713 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005715get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716{
5717 if (idx >= (int)CMD_SIZE)
5718# ifdef FEAT_USR_CMDS
5719 return get_user_command_name(idx);
5720# else
5721 return NULL;
5722# endif
5723 return cmdnames[idx].cmd_name;
5724}
5725#endif
5726
5727#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005728static 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);
5729static void uc_list(char_u *name, size_t name_len);
5730static 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);
5731static char_u *uc_split_args(char_u *arg, size_t *lenp);
5732static 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 +00005733
5734 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005735uc_add_command(
5736 char_u *name,
5737 size_t name_len,
5738 char_u *rep,
5739 long argt,
5740 long def,
5741 int flags,
5742 int compl,
5743 char_u *compl_arg,
5744 int addr_type,
5745 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746{
5747 ucmd_T *cmd = NULL;
5748 char_u *p;
5749 int i;
5750 int cmp = 1;
5751 char_u *rep_buf = NULL;
5752 garray_T *gap;
5753
Bram Moolenaar9c102382006-05-03 21:26:49 +00005754 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 if (rep_buf == NULL)
5756 {
5757 /* Can't replace termcodes - try using the string as is */
5758 rep_buf = vim_strsave(rep);
5759
5760 /* Give up if out of memory */
5761 if (rep_buf == NULL)
5762 return FAIL;
5763 }
5764
5765 /* get address of growarray: global or in curbuf */
5766 if (flags & UC_BUFFER)
5767 {
5768 gap = &curbuf->b_ucmds;
5769 if (gap->ga_itemsize == 0)
5770 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5771 }
5772 else
5773 gap = &ucmds;
5774
5775 /* Search for the command in the already defined commands. */
5776 for (i = 0; i < gap->ga_len; ++i)
5777 {
5778 size_t len;
5779
5780 cmd = USER_CMD_GA(gap, i);
5781 len = STRLEN(cmd->uc_name);
5782 cmp = STRNCMP(name, cmd->uc_name, name_len);
5783 if (cmp == 0)
5784 {
5785 if (name_len < len)
5786 cmp = -1;
5787 else if (name_len > len)
5788 cmp = 1;
5789 }
5790
5791 if (cmp == 0)
5792 {
5793 if (!force)
5794 {
5795 EMSG(_("E174: Command already exists: add ! to replace it"));
5796 goto fail;
5797 }
5798
5799 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005800 cmd->uc_rep = NULL;
5801#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5802 vim_free(cmd->uc_compl_arg);
5803 cmd->uc_compl_arg = NULL;
5804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 break;
5806 }
5807
5808 /* Stop as soon as we pass the name to add */
5809 if (cmp < 0)
5810 break;
5811 }
5812
5813 /* Extend the array unless we're replacing an existing command */
5814 if (cmp != 0)
5815 {
5816 if (ga_grow(gap, 1) != OK)
5817 goto fail;
5818 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5819 goto fail;
5820
5821 cmd = USER_CMD_GA(gap, i);
5822 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5823
5824 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825
5826 cmd->uc_name = p;
5827 }
5828
5829 cmd->uc_rep = rep_buf;
5830 cmd->uc_argt = argt;
5831 cmd->uc_def = def;
5832 cmd->uc_compl = compl;
5833#ifdef FEAT_EVAL
5834 cmd->uc_scriptID = current_SID;
5835# ifdef FEAT_CMDL_COMPL
5836 cmd->uc_compl_arg = compl_arg;
5837# endif
5838#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005839 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840
5841 return OK;
5842
5843fail:
5844 vim_free(rep_buf);
5845#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5846 vim_free(compl_arg);
5847#endif
5848 return FAIL;
5849}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005852#if defined(FEAT_USR_CMDS)
5853static struct
5854{
5855 int expand;
5856 char *name;
5857} addr_type_complete[] =
5858{
5859 {ADDR_ARGUMENTS, "arguments"},
5860 {ADDR_LINES, "lines"},
5861 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5862 {ADDR_TABS, "tabs"},
5863 {ADDR_BUFFERS, "buffers"},
5864 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005865 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005866 {-1, NULL}
5867};
5868#endif
5869
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005870#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871/*
5872 * List of names for completion for ":command" with the EXPAND_ flag.
5873 * Must be alphabetical for completion.
5874 */
5875static struct
5876{
5877 int expand;
5878 char *name;
5879} command_complete[] =
5880{
5881 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005882 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005884 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005886 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005887#if defined(FEAT_CSCOPE)
5888 {EXPAND_CSCOPE, "cscope"},
5889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5891 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005892 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893#endif
5894 {EXPAND_DIRECTORIES, "dir"},
5895 {EXPAND_ENV_VARS, "environment"},
5896 {EXPAND_EVENTS, "event"},
5897 {EXPAND_EXPRESSION, "expression"},
5898 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005899 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005900 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 {EXPAND_FUNCTIONS, "function"},
5902 {EXPAND_HELP, "help"},
5903 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005904#if defined(FEAT_CMDHIST)
5905 {EXPAND_HISTORY, "history"},
5906#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005907#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005908 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005909 {EXPAND_LOCALES, "locale"},
5910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 {EXPAND_MAPPINGS, "mapping"},
5912 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005913 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005914 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005915#if defined(FEAT_PROFILE)
5916 {EXPAND_SYNTIME, "syntime"},
5917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005919 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005920 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005921#if defined(FEAT_SIGNS)
5922 {EXPAND_SIGN, "sign"},
5923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 {EXPAND_TAGS, "tag"},
5925 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005926 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 {EXPAND_USER_VARS, "var"},
5928 {0, NULL}
5929};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005932#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005934uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935{
5936 int i, j;
5937 int found = FALSE;
5938 ucmd_T *cmd;
5939 int len;
5940 long a;
5941 garray_T *gap;
5942
5943 gap = &curbuf->b_ucmds;
5944 for (;;)
5945 {
5946 for (i = 0; i < gap->ga_len; ++i)
5947 {
5948 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005949 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950
Bram Moolenaard29459b2016-08-26 22:29:11 +02005951 /* Skip commands which don't match the requested prefix and
5952 * commands filtered out. */
5953 if (STRNCMP(name, cmd->uc_name, name_len) != 0
5954 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 continue;
5956
5957 /* Put out the title first time */
5958 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005959 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 found = TRUE;
5961 msg_putchar('\n');
5962 if (got_int)
5963 break;
5964
5965 /* Special cases */
5966 msg_putchar(a & BANG ? '!' : ' ');
5967 msg_putchar(a & REGSTR ? '"' : ' ');
5968 msg_putchar(gap != &ucmds ? 'b' : ' ');
5969 msg_putchar(' ');
5970
5971 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5972 len = (int)STRLEN(cmd->uc_name) + 4;
5973
5974 do {
5975 msg_putchar(' ');
5976 ++len;
5977 } while (len < 16);
5978
5979 len = 0;
5980
5981 /* Arguments */
5982 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5983 {
5984 case 0: IObuff[len++] = '0'; break;
5985 case (EXTRA): IObuff[len++] = '*'; break;
5986 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5987 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5988 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5989 }
5990
5991 do {
5992 IObuff[len++] = ' ';
5993 } while (len < 5);
5994
5995 /* Range */
5996 if (a & (RANGE|COUNT))
5997 {
5998 if (a & COUNT)
5999 {
6000 /* -count=N */
6001 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6002 len += (int)STRLEN(IObuff + len);
6003 }
6004 else if (a & DFLALL)
6005 IObuff[len++] = '%';
6006 else if (cmd->uc_def >= 0)
6007 {
6008 /* -range=N */
6009 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6010 len += (int)STRLEN(IObuff + len);
6011 }
6012 else
6013 IObuff[len++] = '.';
6014 }
6015
6016 do {
6017 IObuff[len++] = ' ';
6018 } while (len < 11);
6019
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006020 /* Address Type */
6021 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6022 if (addr_type_complete[j].expand != ADDR_LINES
6023 && addr_type_complete[j].expand == cmd->uc_addr_type)
6024 {
6025 STRCPY(IObuff + len, addr_type_complete[j].name);
6026 len += (int)STRLEN(IObuff + len);
6027 break;
6028 }
6029
6030 do {
6031 IObuff[len++] = ' ';
6032 } while (len < 21);
6033
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 /* Completion */
6035 for (j = 0; command_complete[j].expand != 0; ++j)
6036 if (command_complete[j].expand == cmd->uc_compl)
6037 {
6038 STRCPY(IObuff + len, command_complete[j].name);
6039 len += (int)STRLEN(IObuff + len);
6040 break;
6041 }
6042
6043 do {
6044 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006045 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046
6047 IObuff[len] = '\0';
6048 msg_outtrans(IObuff);
6049
6050 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006051#ifdef FEAT_EVAL
6052 if (p_verbose > 0)
6053 last_set_msg(cmd->uc_scriptID);
6054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 out_flush();
6056 ui_breakcheck();
6057 if (got_int)
6058 break;
6059 }
6060 if (gap == &ucmds || i < gap->ga_len)
6061 break;
6062 gap = &ucmds;
6063 }
6064
6065 if (!found)
6066 MSG(_("No user-defined commands found"));
6067}
6068
6069 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006070uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071{
6072 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6073 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6074 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6075 0xb9, 0x7f, 0};
6076 int i;
6077
6078 for (i = 0; fcmd[i]; ++i)
6079 IObuff[i] = fcmd[i] - 0x40;
6080 IObuff[i] = 0;
6081 return IObuff;
6082}
6083
6084 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006085uc_scan_attr(
6086 char_u *attr,
6087 size_t len,
6088 long *argt,
6089 long *def,
6090 int *flags,
6091 int *compl,
6092 char_u **compl_arg,
6093 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094{
6095 char_u *p;
6096
6097 if (len == 0)
6098 {
6099 EMSG(_("E175: No attribute specified"));
6100 return FAIL;
6101 }
6102
6103 /* First, try the simple attributes (no arguments) */
6104 if (STRNICMP(attr, "bang", len) == 0)
6105 *argt |= BANG;
6106 else if (STRNICMP(attr, "buffer", len) == 0)
6107 *flags |= UC_BUFFER;
6108 else if (STRNICMP(attr, "register", len) == 0)
6109 *argt |= REGSTR;
6110 else if (STRNICMP(attr, "bar", len) == 0)
6111 *argt |= TRLBAR;
6112 else
6113 {
6114 int i;
6115 char_u *val = NULL;
6116 size_t vallen = 0;
6117 size_t attrlen = len;
6118
6119 /* Look for the attribute name - which is the part before any '=' */
6120 for (i = 0; i < (int)len; ++i)
6121 {
6122 if (attr[i] == '=')
6123 {
6124 val = &attr[i + 1];
6125 vallen = len - i - 1;
6126 attrlen = i;
6127 break;
6128 }
6129 }
6130
6131 if (STRNICMP(attr, "nargs", attrlen) == 0)
6132 {
6133 if (vallen == 1)
6134 {
6135 if (*val == '0')
6136 /* Do nothing - this is the default */;
6137 else if (*val == '1')
6138 *argt |= (EXTRA | NOSPC | NEEDARG);
6139 else if (*val == '*')
6140 *argt |= EXTRA;
6141 else if (*val == '?')
6142 *argt |= (EXTRA | NOSPC);
6143 else if (*val == '+')
6144 *argt |= (EXTRA | NEEDARG);
6145 else
6146 goto wrong_nargs;
6147 }
6148 else
6149 {
6150wrong_nargs:
6151 EMSG(_("E176: Invalid number of arguments"));
6152 return FAIL;
6153 }
6154 }
6155 else if (STRNICMP(attr, "range", attrlen) == 0)
6156 {
6157 *argt |= RANGE;
6158 if (vallen == 1 && *val == '%')
6159 *argt |= DFLALL;
6160 else if (val != NULL)
6161 {
6162 p = val;
6163 if (*def >= 0)
6164 {
6165two_count:
6166 EMSG(_("E177: Count cannot be specified twice"));
6167 return FAIL;
6168 }
6169
6170 *def = getdigits(&p);
6171 *argt |= (ZEROR | NOTADR);
6172
6173 if (p != val + vallen || vallen == 0)
6174 {
6175invalid_count:
6176 EMSG(_("E178: Invalid default value for count"));
6177 return FAIL;
6178 }
6179 }
6180 }
6181 else if (STRNICMP(attr, "count", attrlen) == 0)
6182 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006183 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184
6185 if (val != NULL)
6186 {
6187 p = val;
6188 if (*def >= 0)
6189 goto two_count;
6190
6191 *def = getdigits(&p);
6192
6193 if (p != val + vallen)
6194 goto invalid_count;
6195 }
6196
6197 if (*def < 0)
6198 *def = 0;
6199 }
6200 else if (STRNICMP(attr, "complete", attrlen) == 0)
6201 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 if (val == NULL)
6203 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006204 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 return FAIL;
6206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006208 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6209 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006212 else if (STRNICMP(attr, "addr", attrlen) == 0)
6213 {
6214 *argt |= RANGE;
6215 if (val == NULL)
6216 {
6217 EMSG(_("E179: argument required for -addr"));
6218 return FAIL;
6219 }
6220 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6221 == FAIL)
6222 return FAIL;
6223 if (addr_type_arg != ADDR_LINES)
6224 *argt |= (ZEROR | NOTADR) ;
6225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 else
6227 {
6228 char_u ch = attr[len];
6229 attr[len] = '\0';
6230 EMSG2(_("E181: Invalid attribute: %s"), attr);
6231 attr[len] = ch;
6232 return FAIL;
6233 }
6234 }
6235
6236 return OK;
6237}
6238
Bram Moolenaara850a712009-01-28 14:42:59 +00006239/*
6240 * ":command ..."
6241 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006243ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244{
6245 char_u *name;
6246 char_u *end;
6247 char_u *p;
6248 long argt = 0;
6249 long def = -1;
6250 int flags = 0;
6251 int compl = EXPAND_NOTHING;
6252 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006253 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006255 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256
6257 p = eap->arg;
6258
6259 /* Check for attributes */
6260 while (*p == '-')
6261 {
6262 ++p;
6263 end = skiptowhite(p);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006264 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 == FAIL)
6266 return;
6267 p = skipwhite(end);
6268 }
6269
6270 /* Get the name (if any) and skip to the following argument */
6271 name = p;
6272 if (ASCII_ISALPHA(*p))
6273 while (ASCII_ISALNUM(*p))
6274 ++p;
6275 if (!ends_excmd(*p) && !vim_iswhite(*p))
6276 {
6277 EMSG(_("E182: Invalid command name"));
6278 return;
6279 }
6280 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006281 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282
6283 /* If there is nothing after the name, and no attributes were specified,
6284 * we are listing commands
6285 */
6286 p = skipwhite(end);
6287 if (!has_attr && ends_excmd(*p))
6288 {
6289 uc_list(name, end - name);
6290 }
6291 else if (!ASCII_ISUPPER(*name))
6292 {
6293 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6294 return;
6295 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006296 else if ((name_len == 1 && *name == 'X')
6297 || (name_len <= 4
6298 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6299 {
6300 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6301 return;
6302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 else
6304 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006305 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306}
6307
6308/*
6309 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 * Clear all user commands, global and for current buffer.
6311 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006312 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006313ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314{
6315 uc_clear(&ucmds);
6316 uc_clear(&curbuf->b_ucmds);
6317}
6318
6319/*
6320 * Clear all user commands for "gap".
6321 */
6322 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006323uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324{
6325 int i;
6326 ucmd_T *cmd;
6327
6328 for (i = 0; i < gap->ga_len; ++i)
6329 {
6330 cmd = USER_CMD_GA(gap, i);
6331 vim_free(cmd->uc_name);
6332 vim_free(cmd->uc_rep);
6333# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6334 vim_free(cmd->uc_compl_arg);
6335# endif
6336 }
6337 ga_clear(gap);
6338}
6339
6340 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006341ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342{
6343 int i = 0;
6344 ucmd_T *cmd = NULL;
6345 int cmp = -1;
6346 garray_T *gap;
6347
6348 gap = &curbuf->b_ucmds;
6349 for (;;)
6350 {
6351 for (i = 0; i < gap->ga_len; ++i)
6352 {
6353 cmd = USER_CMD_GA(gap, i);
6354 cmp = STRCMP(eap->arg, cmd->uc_name);
6355 if (cmp <= 0)
6356 break;
6357 }
6358 if (gap == &ucmds || cmp == 0)
6359 break;
6360 gap = &ucmds;
6361 }
6362
6363 if (cmp != 0)
6364 {
6365 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6366 return;
6367 }
6368
6369 vim_free(cmd->uc_name);
6370 vim_free(cmd->uc_rep);
6371# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6372 vim_free(cmd->uc_compl_arg);
6373# endif
6374
6375 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376
6377 if (i < gap->ga_len)
6378 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6379}
6380
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006381/*
6382 * split and quote args for <f-args>
6383 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006385uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386{
6387 char_u *buf;
6388 char_u *p;
6389 char_u *q;
6390 int len;
6391
6392 /* Precalculate length */
6393 p = arg;
6394 len = 2; /* Initial and final quotes */
6395
6396 while (*p)
6397 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006398 if (p[0] == '\\' && p[1] == '\\')
6399 {
6400 len += 2;
6401 p += 2;
6402 }
6403 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 {
6405 len += 1;
6406 p += 2;
6407 }
6408 else if (*p == '\\' || *p == '"')
6409 {
6410 len += 2;
6411 p += 1;
6412 }
6413 else if (vim_iswhite(*p))
6414 {
6415 p = skipwhite(p);
6416 if (*p == NUL)
6417 break;
6418 len += 3; /* "," */
6419 }
6420 else
6421 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006422#ifdef FEAT_MBYTE
6423 int charlen = (*mb_ptr2len)(p);
6424 len += charlen;
6425 p += charlen;
6426#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 ++len;
6428 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 }
6431 }
6432
6433 buf = alloc(len + 1);
6434 if (buf == NULL)
6435 {
6436 *lenp = 0;
6437 return buf;
6438 }
6439
6440 p = arg;
6441 q = buf;
6442 *q++ = '"';
6443 while (*p)
6444 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006445 if (p[0] == '\\' && p[1] == '\\')
6446 {
6447 *q++ = '\\';
6448 *q++ = '\\';
6449 p += 2;
6450 }
6451 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 {
6453 *q++ = p[1];
6454 p += 2;
6455 }
6456 else if (*p == '\\' || *p == '"')
6457 {
6458 *q++ = '\\';
6459 *q++ = *p++;
6460 }
6461 else if (vim_iswhite(*p))
6462 {
6463 p = skipwhite(p);
6464 if (*p == NUL)
6465 break;
6466 *q++ = '"';
6467 *q++ = ',';
6468 *q++ = '"';
6469 }
6470 else
6471 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006472 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 }
6474 }
6475 *q++ = '"';
6476 *q = 0;
6477
6478 *lenp = len;
6479 return buf;
6480}
6481
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006482 static size_t
6483add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6484{
6485 size_t result;
6486
6487 result = STRLEN(mod_str);
6488 if (*multi_mods)
6489 result += 1;
6490 if (buf != NULL)
6491 {
6492 if (*multi_mods)
6493 STRCAT(buf, " ");
6494 STRCAT(buf, mod_str);
6495 }
6496
6497 *multi_mods = 1;
6498
6499 return result;
6500}
6501
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502/*
6503 * Check for a <> code in a user command.
6504 * "code" points to the '<'. "len" the length of the <> (inclusive).
6505 * "buf" is where the result is to be added.
6506 * "split_buf" points to a buffer used for splitting, caller should free it.
6507 * "split_len" is the length of what "split_buf" contains.
6508 * Returns the length of the replacement, which has been added to "buf".
6509 * Returns -1 if there was no match, and only the "<" has been copied.
6510 */
6511 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006512uc_check_code(
6513 char_u *code,
6514 size_t len,
6515 char_u *buf,
6516 ucmd_T *cmd, /* the user command we're expanding */
6517 exarg_T *eap, /* ex arguments */
6518 char_u **split_buf,
6519 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520{
6521 size_t result = 0;
6522 char_u *p = code + 1;
6523 size_t l = len - 2;
6524 int quote = 0;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006525 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_MODS,
6526 ct_REGISTER, ct_LT, ct_NONE } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527
6528 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6529 {
6530 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6531 p += 2;
6532 l -= 2;
6533 }
6534
Bram Moolenaar371d5402006-03-20 21:47:49 +00006535 ++l;
6536 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006538 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006540 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006542 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006544 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006546 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006548 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006550 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006552 else if (STRNICMP(p, "mods>", l) == 0)
6553 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554
6555 switch (type)
6556 {
6557 case ct_ARGS:
6558 /* Simple case first */
6559 if (*eap->arg == NUL)
6560 {
6561 if (quote == 1)
6562 {
6563 result = 2;
6564 if (buf != NULL)
6565 STRCPY(buf, "''");
6566 }
6567 else
6568 result = 0;
6569 break;
6570 }
6571
6572 /* When specified there is a single argument don't split it.
6573 * Works for ":Cmd %" when % is "a b c". */
6574 if ((eap->argt & NOSPC) && quote == 2)
6575 quote = 1;
6576
6577 switch (quote)
6578 {
6579 case 0: /* No quoting, no splitting */
6580 result = STRLEN(eap->arg);
6581 if (buf != NULL)
6582 STRCPY(buf, eap->arg);
6583 break;
6584 case 1: /* Quote, but don't split */
6585 result = STRLEN(eap->arg) + 2;
6586 for (p = eap->arg; *p; ++p)
6587 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006588#ifdef FEAT_MBYTE
6589 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6590 /* DBCS can contain \ in a trail byte, skip the
6591 * double-byte character. */
6592 ++p;
6593 else
6594#endif
6595 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 ++result;
6597 }
6598
6599 if (buf != NULL)
6600 {
6601 *buf++ = '"';
6602 for (p = eap->arg; *p; ++p)
6603 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006604#ifdef FEAT_MBYTE
6605 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6606 /* DBCS can contain \ in a trail byte, copy the
6607 * double-byte character to avoid escaping. */
6608 *buf++ = *p++;
6609 else
6610#endif
6611 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 *buf++ = '\\';
6613 *buf++ = *p;
6614 }
6615 *buf = '"';
6616 }
6617
6618 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006619 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 /* This is hard, so only do it once, and cache the result */
6621 if (*split_buf == NULL)
6622 *split_buf = uc_split_args(eap->arg, split_len);
6623
6624 result = *split_len;
6625 if (buf != NULL && result != 0)
6626 STRCPY(buf, *split_buf);
6627
6628 break;
6629 }
6630 break;
6631
6632 case ct_BANG:
6633 result = eap->forceit ? 1 : 0;
6634 if (quote)
6635 result += 2;
6636 if (buf != NULL)
6637 {
6638 if (quote)
6639 *buf++ = '"';
6640 if (eap->forceit)
6641 *buf++ = '!';
6642 if (quote)
6643 *buf = '"';
6644 }
6645 break;
6646
6647 case ct_LINE1:
6648 case ct_LINE2:
6649 case ct_COUNT:
6650 {
6651 char num_buf[20];
6652 long num = (type == ct_LINE1) ? eap->line1 :
6653 (type == ct_LINE2) ? eap->line2 :
6654 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6655 size_t num_len;
6656
6657 sprintf(num_buf, "%ld", num);
6658 num_len = STRLEN(num_buf);
6659 result = num_len;
6660
6661 if (quote)
6662 result += 2;
6663
6664 if (buf != NULL)
6665 {
6666 if (quote)
6667 *buf++ = '"';
6668 STRCPY(buf, num_buf);
6669 buf += num_len;
6670 if (quote)
6671 *buf = '"';
6672 }
6673
6674 break;
6675 }
6676
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006677 case ct_MODS:
6678 {
6679 int multi_mods = 0;
6680 typedef struct {
6681 int *varp;
6682 char *name;
6683 } mod_entry_T;
6684 static mod_entry_T mod_entries[] = {
6685#ifdef FEAT_BROWSE_CMD
6686 {&cmdmod.browse, "browse"},
6687#endif
6688#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6689 {&cmdmod.confirm, "confirm"},
6690#endif
6691 {&cmdmod.hide, "hide"},
6692 {&cmdmod.keepalt, "keepalt"},
6693 {&cmdmod.keepjumps, "keepjumps"},
6694 {&cmdmod.keepmarks, "keepmarks"},
6695 {&cmdmod.keeppatterns, "keeppatterns"},
6696 {&cmdmod.lockmarks, "lockmarks"},
6697 {&cmdmod.noswapfile, "noswapfile"},
6698 {NULL, NULL}
6699 };
6700 int i;
6701
6702 result = quote ? 2 : 0;
6703 if (buf != NULL)
6704 {
6705 if (quote)
6706 *buf++ = '"';
6707 *buf = '\0';
6708 }
6709
6710#ifdef FEAT_WINDOWS
6711 /* :aboveleft and :leftabove */
6712 if (cmdmod.split & WSP_ABOVE)
6713 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6714 /* :belowright and :rightbelow */
6715 if (cmdmod.split & WSP_BELOW)
6716 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6717 /* :botright */
6718 if (cmdmod.split & WSP_BOT)
6719 result += add_cmd_modifier(buf, "botright", &multi_mods);
6720#endif
6721
6722 /* the modifiers that are simple flags */
6723 for (i = 0; mod_entries[i].varp != NULL; ++i)
6724 if (*mod_entries[i].varp)
6725 result += add_cmd_modifier(buf, mod_entries[i].name,
6726 &multi_mods);
6727
6728 /* TODO: How to support :noautocmd? */
6729#ifdef HAVE_SANDBOX
6730 /* TODO: How to support :sandbox?*/
6731#endif
6732 /* :silent */
6733 if (msg_silent > 0)
6734 result += add_cmd_modifier(buf,
6735 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
6736#ifdef FEAT_WINDOWS
6737 /* :tab */
6738 if (cmdmod.tab > 0)
6739 result += add_cmd_modifier(buf, "tab", &multi_mods);
6740 /* :topleft */
6741 if (cmdmod.split & WSP_TOP)
6742 result += add_cmd_modifier(buf, "topleft", &multi_mods);
6743#endif
6744 /* TODO: How to support :unsilent?*/
6745 /* :verbose */
6746 if (p_verbose > 0)
6747 result += add_cmd_modifier(buf, "verbose", &multi_mods);
6748#ifdef FEAT_WINDOWS
6749 /* :vertical */
6750 if (cmdmod.split & WSP_VERT)
6751 result += add_cmd_modifier(buf, "vertical", &multi_mods);
6752#endif
6753 if (quote && buf != NULL)
6754 {
6755 buf += result - 2;
6756 *buf = '"';
6757 }
6758 break;
6759 }
6760
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 case ct_REGISTER:
6762 result = eap->regname ? 1 : 0;
6763 if (quote)
6764 result += 2;
6765 if (buf != NULL)
6766 {
6767 if (quote)
6768 *buf++ = '\'';
6769 if (eap->regname)
6770 *buf++ = eap->regname;
6771 if (quote)
6772 *buf = '\'';
6773 }
6774 break;
6775
6776 case ct_LT:
6777 result = 1;
6778 if (buf != NULL)
6779 *buf = '<';
6780 break;
6781
6782 default:
6783 /* Not recognized: just copy the '<' and return -1. */
6784 result = (size_t)-1;
6785 if (buf != NULL)
6786 *buf = '<';
6787 break;
6788 }
6789
6790 return result;
6791}
6792
6793 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006794do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795{
6796 char_u *buf;
6797 char_u *p;
6798 char_u *q;
6799
6800 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006801 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006802 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 size_t len, totlen;
6804
6805 size_t split_len = 0;
6806 char_u *split_buf = NULL;
6807 ucmd_T *cmd;
6808#ifdef FEAT_EVAL
6809 scid_T save_current_SID = current_SID;
6810#endif
6811
6812 if (eap->cmdidx == CMD_USER)
6813 cmd = USER_CMD(eap->useridx);
6814 else
6815 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6816
6817 /*
6818 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006819 * First round: "buf" is NULL, compute length, allocate "buf".
6820 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 */
6822 buf = NULL;
6823 for (;;)
6824 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006825 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006826 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006828
6829 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006831 start = vim_strchr(p, '<');
6832 if (start != NULL)
6833 end = vim_strchr(start + 1, '>');
6834 if (buf != NULL)
6835 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006836 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6837 ;
6838 if (*ksp == K_SPECIAL
6839 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006840 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6841# ifdef FEAT_GUI
6842 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6843# endif
6844 ))
6845 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006846 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006847 * KS_SPECIAL KE_FILLER, like for mappings, but
6848 * do_cmdline() doesn't handle that, so convert it back.
6849 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6850 len = ksp - p;
6851 if (len > 0)
6852 {
6853 mch_memmove(q, p, len);
6854 q += len;
6855 }
6856 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6857 p = ksp + 3;
6858 continue;
6859 }
6860 }
6861
6862 /* break if there no <item> is found */
6863 if (start == NULL || end == NULL)
6864 break;
6865
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 /* Include the '>' */
6867 ++end;
6868
6869 /* Take everything up to the '<' */
6870 len = start - p;
6871 if (buf == NULL)
6872 totlen += len;
6873 else
6874 {
6875 mch_memmove(q, p, len);
6876 q += len;
6877 }
6878
6879 len = uc_check_code(start, end - start, q, cmd, eap,
6880 &split_buf, &split_len);
6881 if (len == (size_t)-1)
6882 {
6883 /* no match, continue after '<' */
6884 p = start + 1;
6885 len = 1;
6886 }
6887 else
6888 p = end;
6889 if (buf == NULL)
6890 totlen += len;
6891 else
6892 q += len;
6893 }
6894 if (buf != NULL) /* second time here, finished */
6895 {
6896 STRCPY(q, p);
6897 break;
6898 }
6899
6900 totlen += STRLEN(p); /* Add on the trailing characters */
6901 buf = alloc((unsigned)(totlen + 1));
6902 if (buf == NULL)
6903 {
6904 vim_free(split_buf);
6905 return;
6906 }
6907 }
6908
6909#ifdef FEAT_EVAL
6910 current_SID = cmd->uc_scriptID;
6911#endif
6912 (void)do_cmdline(buf, eap->getline, eap->cookie,
6913 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6914#ifdef FEAT_EVAL
6915 current_SID = save_current_SID;
6916#endif
6917 vim_free(buf);
6918 vim_free(split_buf);
6919}
6920
6921# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6922 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006923get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924{
6925 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6926}
6927
6928/*
6929 * Function given to ExpandGeneric() to obtain the list of user command names.
6930 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006932get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933{
6934 if (idx < curbuf->b_ucmds.ga_len)
6935 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6936 idx -= curbuf->b_ucmds.ga_len;
6937 if (idx < ucmds.ga_len)
6938 return USER_CMD(idx)->uc_name;
6939 return NULL;
6940}
6941
6942/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006943 * Function given to ExpandGeneric() to obtain the list of user address type names.
6944 */
6945 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006946get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006947{
6948 return (char_u *)addr_type_complete[idx].name;
6949}
6950
6951/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 * Function given to ExpandGeneric() to obtain the list of user command
6953 * attributes.
6954 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006956get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957{
6958 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006959 {"addr", "bang", "bar", "buffer", "complete",
6960 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006962 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 return NULL;
6964 return (char_u *)user_cmd_flags[idx];
6965}
6966
6967/*
6968 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6969 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006971get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972{
6973 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6974
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006975 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 return NULL;
6977 return (char_u *)user_cmd_nargs[idx];
6978}
6979
6980/*
6981 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6982 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006984get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985{
6986 return (char_u *)command_complete[idx].name;
6987}
6988# endif /* FEAT_CMDL_COMPL */
6989
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006990/*
6991 * Parse address type argument
6992 */
6993 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006994parse_addr_type_arg(
6995 char_u *value,
6996 int vallen,
6997 long *argt,
6998 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006999{
7000 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007001
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007002 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7003 {
7004 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7005 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7006 if (a && b)
7007 {
7008 *addr_type_arg = addr_type_complete[i].expand;
7009 break;
7010 }
7011 }
7012
7013 if (addr_type_complete[i].expand == -1)
7014 {
7015 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007016
7017 for (i = 0; err[i] != NUL && !vim_iswhite(err[i]); i++)
7018 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007019 err[i] = NUL;
7020 EMSG2(_("E180: Invalid address type value: %s"), err);
7021 return FAIL;
7022 }
7023
7024 if (*addr_type_arg != ADDR_LINES)
7025 *argt |= NOTADR;
7026
7027 return OK;
7028}
7029
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030#endif /* FEAT_USR_CMDS */
7031
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007032#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7033/*
7034 * Parse a completion argument "value[vallen]".
7035 * The detected completion goes in "*complp", argument type in "*argt".
7036 * When there is an argument, for function and user defined completion, it's
7037 * copied to allocated memory and stored in "*compl_arg".
7038 * Returns FAIL if something is wrong.
7039 */
7040 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007041parse_compl_arg(
7042 char_u *value,
7043 int vallen,
7044 int *complp,
7045 long *argt,
7046 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007047{
7048 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007049# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007050 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007051# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007052 int i;
7053 int valend = vallen;
7054
7055 /* Look for any argument part - which is the part after any ',' */
7056 for (i = 0; i < vallen; ++i)
7057 {
7058 if (value[i] == ',')
7059 {
7060 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007061# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007062 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007063# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007064 valend = i;
7065 break;
7066 }
7067 }
7068
7069 for (i = 0; command_complete[i].expand != 0; ++i)
7070 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007071 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007072 && STRNCMP(value, command_complete[i].name, valend) == 0)
7073 {
7074 *complp = command_complete[i].expand;
7075 if (command_complete[i].expand == EXPAND_BUFFERS)
7076 *argt |= BUFNAME;
7077 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7078 || command_complete[i].expand == EXPAND_FILES)
7079 *argt |= XFILE;
7080 break;
7081 }
7082 }
7083
7084 if (command_complete[i].expand == 0)
7085 {
7086 EMSG2(_("E180: Invalid complete value: %s"), value);
7087 return FAIL;
7088 }
7089
7090# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7091 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7092 && arg != NULL)
7093# else
7094 if (arg != NULL)
7095# endif
7096 {
7097 EMSG(_("E468: Completion argument only allowed for custom completion"));
7098 return FAIL;
7099 }
7100
7101# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7102 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7103 && arg == NULL)
7104 {
7105 EMSG(_("E467: Custom completion requires a function argument"));
7106 return FAIL;
7107 }
7108
7109 if (arg != NULL)
7110 *compl_arg = vim_strnsave(arg, (int)arglen);
7111# endif
7112 return OK;
7113}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007114
7115 int
7116cmdcomplete_str_to_type(char_u *complete_str)
7117{
7118 int i;
7119
7120 for (i = 0; command_complete[i].expand != 0; ++i)
7121 if (STRCMP(complete_str, command_complete[i].name) == 0)
7122 return command_complete[i].expand;
7123
7124 return EXPAND_NOTHING;
7125}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007126#endif
7127
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007129ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130{
Bram Moolenaare6850792010-05-14 15:28:44 +02007131 if (*eap->arg == NUL)
7132 {
7133#ifdef FEAT_EVAL
7134 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7135 char_u *p = NULL;
7136
7137 if (expr != NULL)
7138 {
7139 ++emsg_off;
7140 p = eval_to_string(expr, NULL, FALSE);
7141 --emsg_off;
7142 vim_free(expr);
7143 }
7144 if (p != NULL)
7145 {
7146 MSG(p);
7147 vim_free(p);
7148 }
7149 else
7150 MSG("default");
7151#else
7152 MSG(_("unknown"));
7153#endif
7154 }
7155 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007156 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157}
7158
7159 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007160ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161{
7162 if (*eap->arg == NUL && eap->cmd[2] == '!')
7163 MSG(_("Greetings, Vim user!"));
7164 do_highlight(eap->arg, eap->forceit, FALSE);
7165}
7166
7167
7168/*
7169 * Call this function if we thought we were going to exit, but we won't
7170 * (because of an error). May need to restore the terminal mode.
7171 */
7172 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007173not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174{
7175 exiting = FALSE;
7176 settmode(TMODE_RAW);
7177}
7178
7179/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007180 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 */
7182 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007183ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184{
Bram Moolenaarf240e182014-11-27 18:33:02 +01007185#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007186 win_T *wp;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007187#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007188
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189#ifdef FEAT_CMDWIN
7190 if (cmdwin_type != 0)
7191 {
7192 cmdwin_result = Ctrl_C;
7193 return;
7194 }
7195#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007196 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007197 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007198 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007199 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007200 return;
7201 }
Bram Moolenaarf240e182014-11-27 18:33:02 +01007202#ifdef FEAT_WINDOWS
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007203 if (eap->addr_count > 0)
7204 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007205 int wnr = eap->line2;
7206
7207 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7208 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007209 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007210 }
7211 else
Bram Moolenaarf240e182014-11-27 18:33:02 +01007212#endif
7213#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007214 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007215#endif
7216
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007217#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02007218 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007219 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02007220 * being closed (can only happen in autocommands). */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007221 if (curbuf_locked() || (wp->w_buffer->b_nwindows == 1
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007222 && wp->w_buffer->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007223 return;
7224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225
7226#ifdef FEAT_NETBEANS_INTG
7227 netbeansForcedQuit = eap->forceit;
7228#endif
7229
7230 /*
7231 * If there are more files or windows we won't exit.
7232 */
7233 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7234 exiting = TRUE;
7235 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007236 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7237 | (eap->forceit ? CCGD_FORCEIT : 0)
7238 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007240 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 {
7242 not_exiting();
7243 }
7244 else
7245 {
7246#ifdef FEAT_WINDOWS
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007247 /* quit last window
7248 * Note: only_one_window() returns true, even so a help window is
7249 * still open. In that case only quit, if no address has been
7250 * specified. Example:
7251 * :h|wincmd w|1q - don't quit
7252 * :h|wincmd w|q - quit
7253 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007254 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255#endif
7256 getout(0);
7257#ifdef FEAT_WINDOWS
7258# ifdef FEAT_GUI
7259 need_mouse_correct = TRUE;
7260# endif
7261 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007262 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263#endif
7264 }
7265}
7266
7267/*
7268 * ":cquit".
7269 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007271ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272{
7273 getout(1); /* this does not always pass on the exit code to the Manx
7274 compiler. why? */
7275}
7276
7277/*
7278 * ":qall": try to quit all windows
7279 */
7280 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007281ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282{
7283# ifdef FEAT_CMDWIN
7284 if (cmdwin_type != 0)
7285 {
7286 if (eap->forceit)
7287 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7288 else
7289 cmdwin_result = K_XF2;
7290 return;
7291 }
7292# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007293
7294 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007295 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007296 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007297 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007298 return;
7299 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007300#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007301 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7302 /* Refuse to quit when locked or when the buffer in the last window is
7303 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007304 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007305 return;
7306#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007307
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007309 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 getout(0);
7311 not_exiting();
7312}
7313
Bram Moolenaard9967712006-03-11 21:18:15 +00007314#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315/*
7316 * ":close": close current window, unless it is the last one
7317 */
7318 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007319ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007321 win_T *win;
7322 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323# ifdef FEAT_CMDWIN
7324 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007325 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 else
7327# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007328 if (!text_locked()
7329#ifdef FEAT_AUTOCMD
7330 && !curbuf_locked()
7331#endif
7332 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007333 {
7334 if (eap->addr_count == 0)
7335 ex_win_close(eap->forceit, curwin, NULL);
7336 else {
Bram Moolenaar29323592016-07-24 22:04:11 +02007337 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007338 {
7339 winnr++;
7340 if (winnr == eap->line2)
7341 break;
7342 }
7343 if (win == NULL)
7344 win = lastwin;
7345 ex_win_close(eap->forceit, win, NULL);
7346 }
7347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348}
7349
Bram Moolenaard9967712006-03-11 21:18:15 +00007350# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007351/*
7352 * ":pclose": Close any preview window.
7353 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007355ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007356{
7357 win_T *win;
7358
Bram Moolenaar29323592016-07-24 22:04:11 +02007359 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007360 if (win->w_p_pvw)
7361 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007362 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007363 break;
7364 }
7365}
Bram Moolenaard9967712006-03-11 21:18:15 +00007366# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007367
Bram Moolenaarf740b292006-02-16 22:11:02 +00007368/*
7369 * Close window "win" and take care of handling closing the last window for a
7370 * modified buffer.
7371 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007372 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007373ex_win_close(
7374 int forceit,
7375 win_T *win,
7376 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377{
7378 int need_hide;
7379 buf_T *buf = win->w_buffer;
7380
7381 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007382 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007384# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 if ((p_confirm || cmdmod.confirm) && p_write)
7386 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007387 bufref_T bufref;
7388
7389 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007391 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 return;
7393 need_hide = FALSE;
7394 }
7395 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007396# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 {
7398 EMSG(_(e_nowrtmsg));
7399 return;
7400 }
7401 }
7402
Bram Moolenaard9967712006-03-11 21:18:15 +00007403# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007405# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007406
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007408 if (tp == NULL)
7409 win_close(win, !need_hide && !P_HID(buf));
7410 else
7411 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412}
7413
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007415 * ":tabclose": close current tab page, unless it is the last one.
7416 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 */
7418 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007419ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007421 tabpage_T *tp;
7422
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007423# ifdef FEAT_CMDWIN
7424 if (cmdwin_type != 0)
7425 cmdwin_result = K_IGNORE;
7426 else
7427# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007428 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007429 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007430 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007432 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007433 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007434 tp = find_tabpage((int)eap->line2);
7435 if (tp == NULL)
7436 {
7437 beep_flush();
7438 return;
7439 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007440 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007441 {
7442 tabpage_close_other(tp, eap->forceit);
7443 return;
7444 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007445 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007446 if (!text_locked()
7447#ifdef FEAT_AUTOCMD
7448 && !curbuf_locked()
7449#endif
7450 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00007451 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007453}
7454
7455/*
7456 * ":tabonly": close all tab pages except the current one
7457 */
7458 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007459ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007460{
7461 tabpage_T *tp;
7462 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007463
7464# ifdef FEAT_CMDWIN
7465 if (cmdwin_type != 0)
7466 cmdwin_result = K_IGNORE;
7467 else
7468# endif
7469 if (first_tabpage->tp_next == NULL)
7470 MSG(_("Already only one tab page"));
7471 else
7472 {
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007473 if (eap->addr_count > 0)
7474 goto_tabpage(eap->line2);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007475 /* Repeat this up to a 1000 times, because autocommands may mess
7476 * up the lists. */
7477 for (done = 0; done < 1000; ++done)
7478 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007479 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007480 if (tp->tp_topframe != topframe)
7481 {
7482 tabpage_close_other(tp, eap->forceit);
7483 /* if we failed to close it quit */
7484 if (valid_tabpage(tp))
7485 done = 1000;
7486 /* start over, "tp" is now invalid */
7487 break;
7488 }
7489 if (first_tabpage->tp_next == NULL)
7490 break;
7491 }
7492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494
7495/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007496 * Close the current tab page.
7497 */
7498 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007499tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007500{
7501 /* First close all the windows but the current one. If that worked then
7502 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007503 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007504 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007505 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007506 ex_win_close(forceit, curwin, NULL);
7507# ifdef FEAT_GUI
7508 need_mouse_correct = TRUE;
7509# endif
7510}
7511
7512/*
7513 * Close tab page "tp", which is not the current tab page.
7514 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007515 * Also takes care of the tab pages line disappearing when closing the
7516 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007517 */
7518 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007519tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007520{
7521 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007522 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007523 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007524
7525 /* Limit to 1000 windows, autocommands may add a window while we close
7526 * one. OK, so I'm paranoid... */
7527 while (++done < 1000)
7528 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007529 wp = tp->tp_firstwin;
7530 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007531
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007532 /* Autocommands may delete the tab page under our fingers and we may
7533 * fail to close a window with a modified buffer. */
7534 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007535 break;
7536 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007537
Bram Moolenaar29323592016-07-24 22:04:11 +02007538#ifdef FEAT_AUTOCMD
7539 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
7540#endif
7541
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007542 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007543 if (h != tabline_height())
7544 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007545}
7546
7547/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 * ":only".
7549 */
7550 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007551ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007553 win_T *wp;
7554 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555# ifdef FEAT_GUI
7556 need_mouse_correct = TRUE;
7557# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007558 if (eap->addr_count > 0)
7559 {
7560 wnr = eap->line2;
7561 for (wp = firstwin; --wnr > 0; )
7562 {
7563 if (wp->w_next == NULL)
7564 break;
7565 else
7566 wp = wp->w_next;
7567 }
7568 win_goto(wp);
7569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 close_others(TRUE, eap->forceit);
7571}
7572
7573/*
7574 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007575 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007577 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007578ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579{
7580 if (eap->addr_count == 0)
7581 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007582 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583}
7584#endif /* FEAT_WINDOWS */
7585
7586 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007587ex_hide(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007589 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590#ifdef FEAT_WINDOWS
Bram Moolenaar2256c992016-11-15 21:17:07 +01007591 if (!eap->skip)
7592 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593# ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007594 need_mouse_correct = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595# endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007596 if (eap->addr_count == 0)
7597 win_close(curwin, FALSE); /* don't free buffer */
7598 else
7599 {
7600 int winnr = 0;
7601 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007602
Bram Moolenaar2256c992016-11-15 21:17:07 +01007603 FOR_ALL_WINDOWS(win)
7604 {
7605 winnr++;
7606 if (winnr == eap->line2)
7607 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007608 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007609 if (win == NULL)
7610 win = lastwin;
7611 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615}
7616
7617/*
7618 * ":stop" and ":suspend": Suspend Vim.
7619 */
7620 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007621ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622{
7623 /*
7624 * Disallow suspending for "rvim".
7625 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007626 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 {
7628 if (!eap->forceit)
7629 autowrite_all();
7630 windgoto((int)Rows - 1, 0);
7631 out_char('\n');
7632 out_flush();
7633 stoptermcap();
7634 out_flush(); /* needed for SUN to restore xterm buffer */
7635#ifdef FEAT_TITLE
7636 mch_restore_title(3); /* restore window titles */
7637#endif
7638 ui_suspend(); /* call machine specific function */
7639#ifdef FEAT_TITLE
7640 maketitle();
7641 resettitle(); /* force updating the title */
7642#endif
7643 starttermcap();
7644 scroll_start(); /* scroll screen before redrawing */
7645 redraw_later_clear();
7646 shell_resized(); /* may have resized window */
7647 }
7648}
7649
7650/*
7651 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7652 */
7653 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007654ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655{
7656#ifdef FEAT_CMDWIN
7657 if (cmdwin_type != 0)
7658 {
7659 cmdwin_result = Ctrl_C;
7660 return;
7661 }
7662#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007663 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007664 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007665 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007666 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007667 return;
7668 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007669#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007670 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7671 /* Refuse to quit when locked or when the buffer in the last window is
7672 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007673 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007674 return;
7675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676
7677 /*
7678 * if more files or windows we won't exit
7679 */
7680 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7681 exiting = TRUE;
7682 if ( ((eap->cmdidx == CMD_wq
7683 || curbufIsChanged())
7684 && do_write(eap) == FAIL)
7685 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007686 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 {
7688 not_exiting();
7689 }
7690 else
7691 {
7692#ifdef FEAT_WINDOWS
7693 if (only_one_window()) /* quit last window, exit Vim */
7694#endif
7695 getout(0);
7696#ifdef FEAT_WINDOWS
7697# ifdef FEAT_GUI
7698 need_mouse_correct = TRUE;
7699# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007700 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 win_close(curwin, !P_HID(curwin->w_buffer));
7702#endif
7703 }
7704}
7705
7706/*
7707 * ":print", ":list", ":number".
7708 */
7709 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007710ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007712 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7713 EMSG(_(e_emptybuf));
7714 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007716 for ( ;!got_int; ui_breakcheck())
7717 {
7718 print_line(eap->line1,
7719 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7720 || (eap->flags & EXFLAG_NR)),
7721 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7722 if (++eap->line1 > eap->line2)
7723 break;
7724 out_flush(); /* show one line at a time */
7725 }
7726 setpcmark();
7727 /* put cursor at last line */
7728 curwin->w_cursor.lnum = eap->line2;
7729 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 }
7731
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733}
7734
7735#ifdef FEAT_BYTEOFF
7736 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007737ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738{
7739 goto_byte(eap->line2);
7740}
7741#endif
7742
7743/*
7744 * ":shell".
7745 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007747ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748{
7749 do_shell(NULL, 0);
7750}
7751
7752#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7753 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007754 || defined(FEAT_GUI_MSWIN) \
7755 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 || defined(PROTO)
7757
7758/*
7759 * Handle a file drop. The code is here because a drop is *nearly* like an
7760 * :args command, but not quite (we have a list of exact filenames, so we
7761 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7762 * code is very similar to :args and hence needs access to a lot of the static
7763 * functions in this file.
7764 *
7765 * The list should be allocated using alloc(), as should each item in the
7766 * list. This function takes over responsibility for freeing the list.
7767 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007768 * XXX The list is made into the argument list. This is freed using
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007769 * FreeWild(), which does a series of vim_free() calls.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 */
7771 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007772handle_drop(
7773 int filec, /* the number of files dropped */
7774 char_u **filev, /* the list of files dropped */
7775 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776{
7777 exarg_T ea;
7778 int save_msg_scroll = msg_scroll;
7779
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007780 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007781 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007783#ifdef FEAT_AUTOCMD
7784 if (curbuf_locked())
7785 return;
7786#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007787 /* When the screen is being updated we should not change buffers and
7788 * windows structures, it may cause freed memory to be used. */
7789 if (updating_screen)
7790 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791
7792 /* Check whether the current buffer is changed. If so, we will need
7793 * to split the current window or data could be lost.
7794 * We don't need to check if the 'hidden' option is set, as in this
7795 * case the buffer won't be lost.
7796 */
7797 if (!P_HID(curbuf) && !split)
7798 {
7799 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007800 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 --emsg_off;
7802 }
7803 if (split)
7804 {
7805# ifdef FEAT_WINDOWS
7806 if (win_split(0, 0) == FAIL)
7807 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007808 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809
7810 /* When splitting the window, create a new alist. Otherwise the
7811 * existing one is overwritten. */
7812 alist_unlink(curwin->w_alist);
7813 alist_new();
7814# else
7815 return; /* can't split, always fail */
7816# endif
7817 }
7818
7819 /*
7820 * Set up the new argument list.
7821 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007822 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823
7824 /*
7825 * Move to the first file.
7826 */
7827 /* Fake up a minimal "next" command for do_argfile() */
7828 vim_memset(&ea, 0, sizeof(ea));
7829 ea.cmd = (char_u *)"next";
7830 do_argfile(&ea, 0);
7831
7832 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7833 * mode that is not needed here. */
7834 need_start_insertmode = FALSE;
7835
7836 /* Restore msg_scroll, otherwise a following command may cause scrolling
7837 * unexpectedly. The screen will be redrawn by the caller, thus
7838 * msg_scroll being set by displaying a message is irrelevant. */
7839 msg_scroll = save_msg_scroll;
7840}
7841#endif
7842
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843/*
7844 * Clear an argument list: free all file names and reset it to zero entries.
7845 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007846 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007847alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848{
7849 while (--al->al_ga.ga_len >= 0)
7850 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7851 ga_clear(&al->al_ga);
7852}
7853
7854/*
7855 * Init an argument list.
7856 */
7857 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007858alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859{
7860 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7861}
7862
7863#if defined(FEAT_WINDOWS) || defined(PROTO)
7864
7865/*
7866 * Remove a reference from an argument list.
7867 * Ignored when the argument list is the global one.
7868 * If the argument list is no longer used by any window, free it.
7869 */
7870 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007871alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872{
7873 if (al != &global_alist && --al->al_refcount <= 0)
7874 {
7875 alist_clear(al);
7876 vim_free(al);
7877 }
7878}
7879
7880# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7881/*
7882 * Create a new argument list and use it for the current window.
7883 */
7884 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007885alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886{
7887 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7888 if (curwin->w_alist == NULL)
7889 {
7890 curwin->w_alist = &global_alist;
7891 ++global_alist.al_refcount;
7892 }
7893 else
7894 {
7895 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007896 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 alist_init(curwin->w_alist);
7898 }
7899}
7900# endif
7901#endif
7902
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007903#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904/*
7905 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007906 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7907 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 */
7909 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007910alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911{
7912 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007913 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 char_u **new_arg_files;
7915 int new_arg_file_count;
7916 char_u *save_p_su = p_su;
7917 int i;
7918
7919 /* Don't use 'suffixes' here. This should work like the shell did the
7920 * expansion. Also, the vimrc file isn't read yet, thus the user
7921 * can't set the options. */
7922 p_su = empty_option;
7923 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7924 if (old_arg_files != NULL)
7925 {
7926 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007927 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7928 old_arg_count = GARGCOUNT;
7929 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007931 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 && new_arg_file_count > 0)
7933 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007934 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7935 TRUE, fnum_list, fnum_len);
7936 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 }
7939 p_su = save_p_su;
7940}
7941#endif
7942
7943/*
7944 * Set the argument list for the current window.
7945 * Takes over the allocated files[] and the allocated fnames in it.
7946 */
7947 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007948alist_set(
7949 alist_T *al,
7950 int count,
7951 char_u **files,
7952 int use_curbuf,
7953 int *fnum_list,
7954 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955{
7956 int i;
7957
7958 alist_clear(al);
7959 if (ga_grow(&al->al_ga, count) == OK)
7960 {
7961 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007962 {
7963 if (got_int)
7964 {
7965 /* When adding many buffers this can take a long time. Allow
7966 * interrupting here. */
7967 while (i < count)
7968 vim_free(files[i++]);
7969 break;
7970 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007971
7972 /* May set buffer name of a buffer previously used for the
7973 * argument list, so that it's re-used by alist_add. */
7974 if (fnum_list != NULL && i < fnum_len)
7975 buf_set_name(fnum_list[i], files[i]);
7976
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007978 ui_breakcheck();
7979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 vim_free(files);
7981 }
7982 else
7983 FreeWild(count, files);
7984#ifdef FEAT_WINDOWS
7985 if (al == &global_alist)
7986#endif
7987 arg_had_last = FALSE;
7988}
7989
7990/*
7991 * Add file "fname" to argument list "al".
7992 * "fname" must have been allocated and "al" must have been checked for room.
7993 */
7994 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007995alist_add(
7996 alist_T *al,
7997 char_u *fname,
7998 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999{
8000 if (fname == NULL) /* don't add NULL file names */
8001 return;
8002#ifdef BACKSLASH_IN_FILENAME
8003 slash_adjust(fname);
8004#endif
8005 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8006 if (set_fnum > 0)
8007 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8008 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8009 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010}
8011
8012#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8013/*
8014 * Adjust slashes in file names. Called after 'shellslash' was set.
8015 */
8016 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008017alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018{
8019 int i;
8020# ifdef FEAT_WINDOWS
8021 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008022 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023# endif
8024
8025 for (i = 0; i < GARGCOUNT; ++i)
8026 if (GARGLIST[i].ae_fname != NULL)
8027 slash_adjust(GARGLIST[i].ae_fname);
8028# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00008029 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 if (wp->w_alist != &global_alist)
8031 for (i = 0; i < WARGCOUNT(wp); ++i)
8032 if (WARGLIST(wp)[i].ae_fname != NULL)
8033 slash_adjust(WARGLIST(wp)[i].ae_fname);
8034# endif
8035}
8036#endif
8037
8038/*
8039 * ":preserve".
8040 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008042ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008044 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 ml_preserve(curbuf, TRUE);
8046}
8047
8048/*
8049 * ":recover".
8050 */
8051 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008052ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053{
8054 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8055 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008056 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8057 | CCGD_MULTWIN
8058 | (eap->forceit ? CCGD_FORCEIT : 0)
8059 | CCGD_EXCMD)
8060
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 && (*eap->arg == NUL
8062 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8063 ml_recover();
8064 recoverymode = FALSE;
8065}
8066
8067/*
8068 * Command modifier used in a wrong way.
8069 */
8070 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008071ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072{
8073 eap->errmsg = e_invcmd;
8074}
8075
8076#ifdef FEAT_WINDOWS
8077/*
8078 * :sview [+command] file split window with new file, read-only
8079 * :split [[+command] file] split window with current or new file
8080 * :vsplit [[+command] file] split window vertically with current or new file
8081 * :new [[+command] file] split window with no or new file
8082 * :vnew [[+command] file] split vertically window with no or new file
8083 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008084 *
8085 * :tabedit open new Tab page with empty window
8086 * :tabedit [+command] file open new Tab page and edit "file"
8087 * :tabnew [[+command] file] just like :tabedit
8088 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 */
8090 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008091ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008093 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008094# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008096# endif
8097# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008099# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008101# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008103# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008105# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008107 * quickfix windows. But it's OK when doing ":tab split". */
8108 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 {
8110 if (eap->cmdidx == CMD_split)
8111 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 if (eap->cmdidx == CMD_vsplit)
8113 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008115# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008117# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008118 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 {
8120 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8121 FNAME_MESS, TRUE, curbuf->b_ffname);
8122 if (fname == NULL)
8123 goto theend;
8124 eap->arg = fname;
8125 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008126# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008128# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008130# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 && eap->cmdidx != CMD_new)
8134 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008135# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008136 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008137# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008138 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008139# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008140 au_has_group((char_u *)"FileExplorer"))
8141 {
8142 /* No browsing supported but we do have the file explorer:
8143 * Edit the directory. */
8144 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8145 eap->arg = (char_u *)".";
8146 }
8147 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008148# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008149 {
8150 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008152 if (fname == NULL)
8153 goto theend;
8154 eap->arg = fname;
8155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 }
8157 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008158# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008160 /*
8161 * Either open new tab page or split the window.
8162 */
8163 if (eap->cmdidx == CMD_tabedit
8164 || eap->cmdidx == CMD_tabfind
8165 || eap->cmdidx == CMD_tabnew)
8166 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008167 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8168 : eap->addr_count == 0 ? 0
8169 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008170 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008171 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008172
8173 /* set the alternate buffer for the window we came from */
8174 if (curwin != old_curwin
8175 && win_valid(old_curwin)
8176 && old_curwin->w_buffer != curbuf
8177 && !cmdmod.keepalt)
8178 old_curwin->w_alt_fnum = curbuf->b_fnum;
8179 }
8180 }
8181 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8183 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008184# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 /* Reset 'scrollbind' when editing another file, but keep it when
8186 * doing ":split" without arguments. */
8187 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008188# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008190# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008192 {
8193 RESET_BINDING(curwin);
8194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 else
8196 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008197# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 do_exedit(eap, old_curwin);
8199 }
8200
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008201# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008203# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008205# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206theend:
8207 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008208# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008210
8211/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008212 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008213 */
8214 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008215tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008216{
8217 exarg_T ea;
8218
8219 vim_memset(&ea, 0, sizeof(ea));
8220 ea.cmdidx = CMD_tabnew;
8221 ea.cmd = (char_u *)"tabn";
8222 ea.arg = (char_u *)"";
8223 ex_splitview(&ea);
8224}
8225
8226/*
8227 * :tabnext command
8228 */
8229 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008230ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008231{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008232 switch (eap->cmdidx)
8233 {
8234 case CMD_tabfirst:
8235 case CMD_tabrewind:
8236 goto_tabpage(1);
8237 break;
8238 case CMD_tablast:
8239 goto_tabpage(9999);
8240 break;
8241 case CMD_tabprevious:
8242 case CMD_tabNext:
8243 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
8244 break;
8245 default: /* CMD_tabnext */
8246 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
8247 break;
8248 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008249}
8250
8251/*
8252 * :tabmove command
8253 */
8254 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008255ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008256{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008257 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008258
8259 if (eap->arg && *eap->arg != NUL)
8260 {
8261 char_u *p = eap->arg;
8262 int relative = 0; /* argument +N/-N means: move N places to the
8263 * right/left relative to the current position. */
8264
8265 if (*eap->arg == '-')
8266 {
8267 relative = -1;
8268 p = eap->arg + 1;
8269 }
8270 else if (*eap->arg == '+')
8271 {
8272 relative = 1;
8273 p = eap->arg + 1;
8274 }
8275 else
8276 p = eap->arg;
8277
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008278 if (relative == 0)
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008279 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008280 if (STRCMP(p, "$") == 0)
8281 tab_number = LAST_TAB_NR;
8282 else if (p == skipdigits(p))
8283 {
8284 /* No numbers as argument. */
8285 eap->errmsg = e_invarg;
8286 return;
8287 }
8288 else
8289 tab_number = getdigits(&p);
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008290 }
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008291 else
8292 {
8293 if (*p != NUL)
8294 tab_number = getdigits(&p);
8295 else
8296 tab_number = 1;
8297 tab_number = tab_number * relative + tabpage_index(curtab);
8298 if (relative == -1)
8299 --tab_number;
8300 }
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008301 }
8302 else if (eap->addr_count != 0)
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008303 {
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008304 tab_number = eap->line2;
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008305 if (**eap->cmdlinep == '-')
8306 --tab_number;
8307 }
8308 else
8309 tab_number = LAST_TAB_NR;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008310
8311 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008312}
8313
8314/*
8315 * :tabs command: List tabs and their contents.
8316 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008317 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008318ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008319{
8320 tabpage_T *tp;
8321 win_T *wp;
8322 int tabcount = 1;
8323
8324 msg_start();
8325 msg_scroll = TRUE;
8326 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8327 {
8328 msg_putchar('\n');
8329 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
8330 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
8331 out_flush(); /* output one line at a time */
8332 ui_breakcheck();
8333
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008334 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008335 wp = firstwin;
8336 else
8337 wp = tp->tp_firstwin;
8338 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8339 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008340 msg_putchar('\n');
8341 msg_putchar(wp == curwin ? '>' : ' ');
8342 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008343 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8344 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008345 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008346 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008347 else
8348 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8349 IObuff, IOSIZE, TRUE);
8350 msg_outtrans(IObuff);
8351 out_flush(); /* output one line at a time */
8352 ui_breakcheck();
8353 }
8354 }
8355}
8356
8357#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358
8359/*
8360 * ":mode": Set screen mode.
8361 * If no argument given, just get the screen size and redraw.
8362 */
8363 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008364ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365{
8366 if (*eap->arg == NUL)
8367 shell_resized();
8368 else
8369 mch_screenmode(eap->arg);
8370}
8371
8372#ifdef FEAT_WINDOWS
8373/*
8374 * ":resize".
8375 * set, increment or decrement current window height
8376 */
8377 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008378ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379{
8380 int n;
8381 win_T *wp = curwin;
8382
8383 if (eap->addr_count > 0)
8384 {
8385 n = eap->line2;
8386 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8387 ;
8388 }
8389
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008390# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008392# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 if (cmdmod.split & WSP_VERT)
8395 {
8396 if (*eap->arg == '-' || *eap->arg == '+')
8397 n += W_WIDTH(curwin);
8398 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8399 n = 9999;
8400 win_setwidth_win((int)n, wp);
8401 }
8402 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 {
8404 if (*eap->arg == '-' || *eap->arg == '+')
8405 n += curwin->w_height;
8406 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8407 n = 9999;
8408 win_setheight_win((int)n, wp);
8409 }
8410}
8411#endif
8412
8413/*
8414 * ":find [+command] <file>" command.
8415 */
8416 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008417ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418{
8419#ifdef FEAT_SEARCHPATH
8420 char_u *fname;
8421 int count;
8422
8423 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8424 TRUE, curbuf->b_ffname);
8425 if (eap->addr_count > 0)
8426 {
8427 /* Repeat finding the file "count" times. This matters when it
8428 * appears several times in the path. */
8429 count = eap->line2;
8430 while (fname != NULL && --count > 0)
8431 {
8432 vim_free(fname);
8433 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8434 FALSE, curbuf->b_ffname);
8435 }
8436 }
8437
8438 if (fname != NULL)
8439 {
8440 eap->arg = fname;
8441#endif
8442 do_exedit(eap, NULL);
8443#ifdef FEAT_SEARCHPATH
8444 vim_free(fname);
8445 }
8446#endif
8447}
8448
8449/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008450 * ":open" simulation: for now just work like ":visual".
8451 */
8452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008453ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008454{
8455 regmatch_T regmatch;
8456 char_u *p;
8457
8458 curwin->w_cursor.lnum = eap->line2;
8459 beginline(BL_SOL | BL_FIX);
8460 if (*eap->arg == '/')
8461 {
8462 /* ":open /pattern/": put cursor in column found with pattern */
8463 ++eap->arg;
8464 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8465 *p = NUL;
8466 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8467 if (regmatch.regprog != NULL)
8468 {
8469 regmatch.rm_ic = p_ic;
8470 p = ml_get_curline();
8471 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008472 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008473 else
8474 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008475 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008476 }
8477 /* Move to the NUL, ignore any other arguments. */
8478 eap->arg += STRLEN(eap->arg);
8479 }
8480 check_cursor();
8481
8482 eap->cmdidx = CMD_visual;
8483 do_exedit(eap, NULL);
8484}
8485
8486/*
8487 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 */
8489 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008490ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491{
8492 do_exedit(eap, NULL);
8493}
8494
8495/*
8496 * ":edit <file>" command and alikes.
8497 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008499do_exedit(
8500 exarg_T *eap,
8501 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502{
8503 int n;
8504#ifdef FEAT_WINDOWS
8505 int need_hide;
8506#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008507 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508
8509 /*
8510 * ":vi" command ends Ex mode.
8511 */
8512 if (exmode_active && (eap->cmdidx == CMD_visual
8513 || eap->cmdidx == CMD_view))
8514 {
8515 exmode_active = FALSE;
8516 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008517 {
8518 /* Special case: ":global/pat/visual\NLvi-commands" */
8519 if (global_busy)
8520 {
8521 int rd = RedrawingDisabled;
8522 int nwr = no_wait_return;
8523 int ms = msg_scroll;
8524#ifdef FEAT_GUI
8525 int he = hold_gui_events;
8526#endif
8527
8528 if (eap->nextcmd != NULL)
8529 {
8530 stuffReadbuff(eap->nextcmd);
8531 eap->nextcmd = NULL;
8532 }
8533
8534 if (exmode_was != EXMODE_VIM)
8535 settmode(TMODE_RAW);
8536 RedrawingDisabled = 0;
8537 no_wait_return = 0;
8538 need_wait_return = FALSE;
8539 msg_scroll = 0;
8540#ifdef FEAT_GUI
8541 hold_gui_events = 0;
8542#endif
8543 must_redraw = CLEAR;
8544
8545 main_loop(FALSE, TRUE);
8546
8547 RedrawingDisabled = rd;
8548 no_wait_return = nwr;
8549 msg_scroll = ms;
8550#ifdef FEAT_GUI
8551 hold_gui_events = he;
8552#endif
8553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 }
8557
8558 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008559 || eap->cmdidx == CMD_tabnew
8560 || eap->cmdidx == CMD_tabedit
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008561#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 || eap->cmdidx == CMD_vnew
8563#endif
8564 ) && *eap->arg == NUL)
8565 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008566 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 setpcmark();
8568 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008569 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8570 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 }
8572 else if ((eap->cmdidx != CMD_split
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008573#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 && eap->cmdidx != CMD_vsplit
8575#endif
8576 )
8577 || *eap->arg != NUL
8578#ifdef FEAT_BROWSE
8579 || cmdmod.browse
8580#endif
8581 )
8582 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008583#ifdef FEAT_AUTOCMD
8584 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8585 * can bring us here, others are stopped earlier. */
8586 if (*eap->arg != NUL && curbuf_locked())
8587 return;
8588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 n = readonlymode;
8590 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8591 readonlymode = TRUE;
8592 else if (eap->cmdidx == CMD_enew)
8593 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8594 empty buffer */
8595 setpcmark();
8596 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8597 NULL, eap,
8598 /* ":edit" goes to first line if Vi compatible */
8599 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8600 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8601 ? ECMD_ONE : eap->do_ecmd_lnum,
8602 (P_HID(curbuf) ? ECMD_HIDE : 0)
8603 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008604 /* after a split we can use an existing buffer */
8605 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606#ifdef FEAT_LISTCMDS
8607 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8608#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008609 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 {
8611 /* Editing the file failed. If the window was split, close it. */
8612#ifdef FEAT_WINDOWS
8613 if (old_curwin != NULL)
8614 {
8615 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8616 if (!need_hide || P_HID(curbuf))
8617 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008618# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8619 cleanup_T cs;
8620
8621 /* Reset the error/interrupt/exception state here so that
8622 * aborting() returns FALSE when closing a window. */
8623 enter_cleanup(&cs);
8624# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625# ifdef FEAT_GUI
8626 need_mouse_correct = TRUE;
8627# endif
8628 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008629
8630# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8631 /* Restore the error/interrupt/exception state if not
8632 * discarded by a new aborting error, interrupt, or
8633 * uncaught exception. */
8634 leave_cleanup(&cs);
8635# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 }
8637 }
8638#endif
8639 }
8640 else if (readonlymode && curbuf->b_nwindows == 1)
8641 {
8642 /* When editing an already visited buffer, 'readonly' won't be set
8643 * but the previous value is kept. With ":view" and ":sview" we
8644 * want the file to be readonly, except when another window is
8645 * editing the same buffer. */
8646 curbuf->b_p_ro = TRUE;
8647 }
8648 readonlymode = n;
8649 }
8650 else
8651 {
8652 if (eap->do_ecmd_cmd != NULL)
8653 do_cmdline_cmd(eap->do_ecmd_cmd);
8654#ifdef FEAT_TITLE
8655 n = curwin->w_arg_idx_invalid;
8656#endif
8657 check_arg_idx(curwin);
8658#ifdef FEAT_TITLE
8659 if (n != curwin->w_arg_idx_invalid)
8660 maketitle();
8661#endif
8662 }
8663
8664#ifdef FEAT_WINDOWS
8665 /*
8666 * if ":split file" worked, set alternate file name in old window to new
8667 * file
8668 */
8669 if (old_curwin != NULL
8670 && *eap->arg != NUL
8671 && curwin != old_curwin
8672 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008673 && old_curwin->w_buffer != curbuf
8674 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 old_curwin->w_alt_fnum = curbuf->b_fnum;
8676#endif
8677
8678 ex_no_reprint = TRUE;
8679}
8680
8681#ifndef FEAT_GUI
8682/*
8683 * ":gui" and ":gvim" when there is no GUI.
8684 */
8685 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008686ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687{
8688 eap->errmsg = e_nogvim;
8689}
8690#endif
8691
8692#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8693 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008694ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695{
8696 gui_make_tearoff(eap->arg);
8697}
8698#endif
8699
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008700#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008702ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008704 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705}
8706#endif
8707
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008709ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710{
8711 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8712 MSG(_("No swap file"));
8713 else
8714 msg(curbuf->b_ml.ml_mfp->mf_fname);
8715}
8716
8717/*
8718 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8719 * offset.
8720 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8721 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008723ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724{
8725#ifdef FEAT_SCROLLBIND
8726 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008727 win_T *save_curwin = curwin;
8728 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 long topline;
8730 long y;
8731 linenr_T old_linenr = curwin->w_cursor.lnum;
8732
8733 setpcmark();
8734
8735 /*
8736 * determine max topline
8737 */
8738 if (curwin->w_p_scb)
8739 {
8740 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008741 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 {
8743 if (wp->w_p_scb && wp->w_buffer)
8744 {
8745 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8746 if (topline > y)
8747 topline = y;
8748 }
8749 }
8750 if (topline < 1)
8751 topline = 1;
8752 }
8753 else
8754 {
8755 topline = 1;
8756 }
8757
8758
8759 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008760 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008762 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 {
8764 if (curwin->w_p_scb)
8765 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008766 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 y = topline - curwin->w_topline;
8768 if (y > 0)
8769 scrollup(y, TRUE);
8770 else
8771 scrolldown(-y, TRUE);
8772 curwin->w_scbind_pos = topline;
8773 redraw_later(VALID);
8774 cursor_correct();
8775#ifdef FEAT_WINDOWS
8776 curwin->w_redr_status = TRUE;
8777#endif
8778 }
8779 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008780 curwin = save_curwin;
8781 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 if (curwin->w_p_scb)
8783 {
8784 did_syncbind = TRUE;
8785 checkpcmark();
8786 if (old_linenr != curwin->w_cursor.lnum)
8787 {
8788 char_u ctrl_o[2];
8789
8790 ctrl_o[0] = Ctrl_O;
8791 ctrl_o[1] = 0;
8792 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8793 }
8794 }
8795#endif
8796}
8797
8798
8799 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008800ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008802 int i;
8803 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8804 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805
8806 if (eap->usefilter) /* :r!cmd */
8807 do_bang(1, eap, FALSE, FALSE, TRUE);
8808 else
8809 {
8810 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8811 return;
8812
8813#ifdef FEAT_BROWSE
8814 if (cmdmod.browse)
8815 {
8816 char_u *browseFile;
8817
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008818 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 NULL, NULL, NULL, curbuf);
8820 if (browseFile != NULL)
8821 {
8822 i = readfile(browseFile, NULL,
8823 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8824 vim_free(browseFile);
8825 }
8826 else
8827 i = OK;
8828 }
8829 else
8830#endif
8831 if (*eap->arg == NUL)
8832 {
8833 if (check_fname() == FAIL) /* check for no file name */
8834 return;
8835 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8836 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8837 }
8838 else
8839 {
8840 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8841 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8842 i = readfile(eap->arg, NULL,
8843 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8844
8845 }
8846 if (i == FAIL)
8847 {
8848#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8849 if (!aborting())
8850#endif
8851 EMSG2(_(e_notopen), eap->arg);
8852 }
8853 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008854 {
8855 if (empty && exmode_active)
8856 {
8857 /* Delete the empty line that remains. Historically ex does
8858 * this but vi doesn't. */
8859 if (eap->line2 == 0)
8860 lnum = curbuf->b_ml.ml_line_count;
8861 else
8862 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008863 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008864 {
8865 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008866 if (curwin->w_cursor.lnum > 1
8867 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008868 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008869 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008870 }
8871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 }
8875}
8876
Bram Moolenaarea408852005-06-25 22:49:46 +00008877static char_u *prev_dir = NULL;
8878
8879#if defined(EXITFREE) || defined(PROTO)
8880 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008881free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00008882{
8883 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008884 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008885
8886 vim_free(globaldir);
8887 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008888}
8889#endif
8890
Bram Moolenaarf4258302013-06-02 18:20:17 +02008891/*
8892 * Deal with the side effects of changing the current directory.
8893 * When "local" is TRUE then this was after an ":lcd" command.
8894 */
8895 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008896post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008897{
8898 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008899 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008900 if (local)
8901 {
8902 /* If still in global directory, need to remember current
8903 * directory as global directory. */
8904 if (globaldir == NULL && prev_dir != NULL)
8905 globaldir = vim_strsave(prev_dir);
8906 /* Remember this local directory for the window. */
8907 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8908 curwin->w_localdir = vim_strsave(NameBuff);
8909 }
8910 else
8911 {
8912 /* We are now in the global directory, no need to remember its
8913 * name. */
8914 vim_free(globaldir);
8915 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008916 }
8917
8918 shorten_fnames(TRUE);
8919}
8920
Bram Moolenaarea408852005-06-25 22:49:46 +00008921
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922/*
8923 * ":cd", ":lcd", ":chdir" and ":lchdir".
8924 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008925 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008926ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 char_u *new_dir;
8929 char_u *tofree;
8930
8931 new_dir = eap->arg;
8932#if !defined(UNIX) && !defined(VMS)
8933 /* for non-UNIX ":cd" means: print current directory */
8934 if (*new_dir == NUL)
8935 ex_pwd(NULL);
8936 else
8937#endif
8938 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008939#ifdef FEAT_AUTOCMD
8940 if (allbuf_locked())
8941 return;
8942#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008943 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8944 && !eap->forceit)
8945 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008946 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008947 return;
8948 }
8949
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 /* ":cd -": Change to previous directory */
8951 if (STRCMP(new_dir, "-") == 0)
8952 {
8953 if (prev_dir == NULL)
8954 {
8955 EMSG(_("E186: No previous directory"));
8956 return;
8957 }
8958 new_dir = prev_dir;
8959 }
8960
8961 /* Save current directory for next ":cd -" */
8962 tofree = prev_dir;
8963 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8964 prev_dir = vim_strsave(NameBuff);
8965 else
8966 prev_dir = NULL;
8967
8968#if defined(UNIX) || defined(VMS)
8969 /* for UNIX ":cd" means: go to home directory */
8970 if (*new_dir == NUL)
8971 {
8972 /* use NameBuff for home directory name */
8973# ifdef VMS
8974 char_u *p;
8975
8976 p = mch_getenv((char_u *)"SYS$LOGIN");
8977 if (p == NULL || *p == NUL) /* empty is the same as not set */
8978 NameBuff[0] = NUL;
8979 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008980 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981# else
8982 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8983# endif
8984 new_dir = NameBuff;
8985 }
8986#endif
8987 if (new_dir == NULL || vim_chdir(new_dir))
8988 EMSG(_(e_failed));
8989 else
8990 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008991 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992
8993 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008994 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 ex_pwd(eap);
8996 }
8997 vim_free(tofree);
8998 }
8999}
9000
9001/*
9002 * ":pwd".
9003 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009005ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006{
9007 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9008 {
9009#ifdef BACKSLASH_IN_FILENAME
9010 slash_adjust(NameBuff);
9011#endif
9012 msg(NameBuff);
9013 }
9014 else
9015 EMSG(_("E187: Unknown"));
9016}
9017
9018/*
9019 * ":=".
9020 */
9021 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009022ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023{
9024 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009025 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026}
9027
9028 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009029ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009031 int n;
9032 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033
9034 if (cursor_valid())
9035 {
9036 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9037 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01009038 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009040
9041 len = eap->line2;
9042 switch (*eap->arg)
9043 {
9044 case 'm': break;
9045 case NUL: len *= 1000L; break;
9046 default: EMSG2(_(e_invarg2), eap->arg); return;
9047 }
9048 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049}
9050
9051/*
9052 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9053 */
9054 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009055do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056{
9057 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009058 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059
9060 cursor_on();
9061 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009062 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009064 wait_now = msec - done > 1000L ? 1000L : msec - done;
9065#ifdef FEAT_TIMERS
9066 {
9067 long due_time = check_due_timer();
9068
9069 if (due_time > 0 && due_time < wait_now)
9070 wait_now = due_time;
9071 }
9072#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009073#ifdef FEAT_JOB_CHANNEL
9074 if (has_any_channel() && wait_now > 100L)
9075 wait_now = 100L;
9076#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009077 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009078#ifdef FEAT_JOB_CHANNEL
9079 if (has_any_channel())
9080 ui_breakcheck_force(TRUE);
9081 else
9082#endif
9083 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009084#ifdef MESSAGE_QUEUE
9085 /* Process the netbeans and clientserver messages that may have been
9086 * received in the call to ui_breakcheck() when the GUI is in use. This
9087 * may occur when running a test case. */
9088 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 }
9091}
9092
9093 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009094do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095{
9096 int mode;
9097 char_u *cmdp;
9098
9099 cmdp = eap->cmd;
9100 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9101
9102 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9103 eap->arg, mode, isabbrev))
9104 {
9105 case 1: EMSG(_(e_invarg));
9106 break;
9107 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9108 break;
9109 }
9110}
9111
9112/*
9113 * ":winsize" command (obsolete).
9114 */
9115 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009116ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117{
9118 int w, h;
9119 char_u *arg = eap->arg;
9120 char_u *p;
9121
9122 w = getdigits(&arg);
9123 arg = skipwhite(arg);
9124 p = arg;
9125 h = getdigits(&arg);
9126 if (*p != NUL && *arg == NUL)
9127 set_shellsize(w, h, TRUE);
9128 else
9129 EMSG(_("E465: :winsize requires two number arguments"));
9130}
9131
9132#ifdef FEAT_WINDOWS
9133 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009134ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135{
9136 int xchar = NUL;
9137 char_u *p;
9138
9139 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9140 {
9141 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9142 if (eap->arg[1] == NUL)
9143 {
9144 EMSG(_(e_invarg));
9145 return;
9146 }
9147 xchar = eap->arg[1];
9148 p = eap->arg + 2;
9149 }
9150 else
9151 p = eap->arg + 1;
9152
9153 eap->nextcmd = check_nextcmd(p);
9154 p = skipwhite(p);
9155 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9156 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009157 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 {
9159 /* Pass flags on for ":vertical wincmd ]". */
9160 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009161 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9163 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009164 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 }
9166}
9167#endif
9168
Bram Moolenaar843ee412004-06-30 16:16:41 +00009169#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170/*
9171 * ":winpos".
9172 */
9173 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009174ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175{
9176 int x, y;
9177 char_u *arg = eap->arg;
9178 char_u *p;
9179
9180 if (*arg == NUL)
9181 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009182# if defined(FEAT_GUI) || defined(MSWIN)
9183# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009185# else
9186 if (mch_get_winpos(&x, &y) != FAIL)
9187# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 {
9189 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9190 msg(IObuff);
9191 }
9192 else
9193# endif
9194 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9195 }
9196 else
9197 {
9198 x = getdigits(&arg);
9199 arg = skipwhite(arg);
9200 p = arg;
9201 y = getdigits(&arg);
9202 if (*p == NUL || *arg != NUL)
9203 {
9204 EMSG(_("E466: :winpos requires two number arguments"));
9205 return;
9206 }
9207# ifdef FEAT_GUI
9208 if (gui.in_use)
9209 gui_mch_set_winpos(x, y);
9210 else if (gui.starting)
9211 {
9212 /* Remember the coordinates for when the window is opened. */
9213 gui_win_x = x;
9214 gui_win_y = y;
9215 }
9216# ifdef HAVE_TGETENT
9217 else
9218# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009219# else
9220# ifdef MSWIN
9221 mch_set_winpos(x, y);
9222# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223# endif
9224# ifdef HAVE_TGETENT
9225 if (*T_CWP)
9226 term_set_winpos(x, y);
9227# endif
9228 }
9229}
9230#endif
9231
9232/*
9233 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9234 */
9235 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009236ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237{
9238 oparg_T oa;
9239
9240 clear_oparg(&oa);
9241 oa.regname = eap->regname;
9242 oa.start.lnum = eap->line1;
9243 oa.end.lnum = eap->line2;
9244 oa.line_count = eap->line2 - eap->line1 + 1;
9245 oa.motion_type = MLINE;
9246#ifdef FEAT_VIRTUALEDIT
9247 virtual_op = FALSE;
9248#endif
9249 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9250 {
9251 setpcmark();
9252 curwin->w_cursor.lnum = eap->line1;
9253 beginline(BL_SOL | BL_FIX);
9254 }
9255
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009256 if (VIsual_active)
9257 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009258
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259 switch (eap->cmdidx)
9260 {
9261 case CMD_delete:
9262 oa.op_type = OP_DELETE;
9263 op_delete(&oa);
9264 break;
9265
9266 case CMD_yank:
9267 oa.op_type = OP_YANK;
9268 (void)op_yank(&oa, FALSE, TRUE);
9269 break;
9270
9271 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009272 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009274 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9275#else
9276 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009278 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279 oa.op_type = OP_RSHIFT;
9280 else
9281 oa.op_type = OP_LSHIFT;
9282 op_shift(&oa, FALSE, eap->amount);
9283 break;
9284 }
9285#ifdef FEAT_VIRTUALEDIT
9286 virtual_op = MAYBE;
9287#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009288 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289}
9290
9291/*
9292 * ":put".
9293 */
9294 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009295ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296{
9297 /* ":0put" works like ":1put!". */
9298 if (eap->line2 == 0)
9299 {
9300 eap->line2 = 1;
9301 eap->forceit = TRUE;
9302 }
9303 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009304 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9305 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306}
9307
9308/*
9309 * Handle ":copy" and ":move".
9310 */
9311 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009312ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313{
9314 long n;
9315
Bram Moolenaarded27822017-01-02 14:27:34 +01009316 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317 if (eap->arg == NULL) /* error detected */
9318 {
9319 eap->nextcmd = NULL;
9320 return;
9321 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009322 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323
9324 /*
9325 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9326 */
9327 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9328 {
9329 EMSG(_(e_invaddr));
9330 return;
9331 }
9332
9333 if (eap->cmdidx == CMD_move)
9334 {
9335 if (do_move(eap->line1, eap->line2, n) == FAIL)
9336 return;
9337 }
9338 else
9339 ex_copy(eap->line1, eap->line2, n);
9340 u_clearline();
9341 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009342 ex_may_print(eap);
9343}
9344
9345/*
9346 * Print the current line if flags were given to the Ex command.
9347 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009348 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009349ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009350{
9351 if (eap->flags != 0)
9352 {
9353 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9354 (eap->flags & EXFLAG_LIST));
9355 ex_no_reprint = TRUE;
9356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357}
9358
9359/*
9360 * ":smagic" and ":snomagic".
9361 */
9362 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009363ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364{
9365 int magic_save = p_magic;
9366
9367 p_magic = (eap->cmdidx == CMD_smagic);
9368 do_sub(eap);
9369 p_magic = magic_save;
9370}
9371
9372/*
9373 * ":join".
9374 */
9375 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009376ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377{
9378 curwin->w_cursor.lnum = eap->line1;
9379 if (eap->line1 == eap->line2)
9380 {
9381 if (eap->addr_count >= 2) /* :2,2join does nothing */
9382 return;
9383 if (eap->line2 == curbuf->b_ml.ml_line_count)
9384 {
9385 beep_flush();
9386 return;
9387 }
9388 ++eap->line2;
9389 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009390 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009392 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393}
9394
9395/*
9396 * ":[addr]@r" or ":[addr]*r": execute register
9397 */
9398 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009399ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400{
9401 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009402 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403
9404 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009405 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406
9407#ifdef USE_ON_FLY_SCROLL
9408 dont_scroll = TRUE; /* disallow scrolling here */
9409#endif
9410
9411 /* get the register name. No name means to use the previous one */
9412 c = *eap->arg;
9413 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9414 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009415 /* Put the register in the typeahead buffer with the "silent" flag. */
9416 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9417 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009418 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421 else
9422 {
9423 int save_efr = exec_from_reg;
9424
9425 exec_from_reg = TRUE;
9426
9427 /*
9428 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009429 * Continue until the stuff buffer is empty and all added characters
9430 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009432 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9434
9435 exec_from_reg = save_efr;
9436 }
9437}
9438
9439/*
9440 * ":!".
9441 */
9442 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009443ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444{
9445 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9446}
9447
9448/*
9449 * ":undo".
9450 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009452ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009454 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009455 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009456 else
9457 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458}
9459
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009460#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009461 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009462ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009463{
9464 char_u hash[UNDO_HASH_SIZE];
9465
9466 u_compute_hash(hash);
9467 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9468}
9469
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009470 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009471ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009472{
9473 char_u hash[UNDO_HASH_SIZE];
9474
9475 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009476 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009477}
9478#endif
9479
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480/*
9481 * ":redo".
9482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009484ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485{
9486 u_redo(1);
9487}
9488
9489/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009490 * ":earlier" and ":later".
9491 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009492 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009493ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009494{
9495 long count = 0;
9496 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009497 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009498 char_u *p = eap->arg;
9499
9500 if (*p == NUL)
9501 count = 1;
9502 else if (isdigit(*p))
9503 {
9504 count = getdigits(&p);
9505 switch (*p)
9506 {
9507 case 's': ++p; sec = TRUE; break;
9508 case 'm': ++p; sec = TRUE; count *= 60; break;
9509 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009510 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9511 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009512 }
9513 }
9514
9515 if (*p != NUL)
9516 EMSG2(_(e_invarg2), eap->arg);
9517 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009518 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9519 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009520}
9521
9522/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523 * ":redir": start/stop redirection.
9524 */
9525 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009526ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527{
9528 char *mode;
9529 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009530 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531
Bram Moolenaarba768492016-07-08 15:32:54 +02009532#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009533 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009534 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009535 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009536 return;
9537 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009538#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009539
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540 if (STRICMP(eap->arg, "END") == 0)
9541 close_redir();
9542 else
9543 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009544 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009546 ++arg;
9547 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009549 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 mode = "a";
9551 }
9552 else
9553 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009554 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555
9556 close_redir();
9557
9558 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009559 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 if (fname == NULL)
9561 return;
9562#ifdef FEAT_BROWSE
9563 if (cmdmod.browse)
9564 {
9565 char_u *browseFile;
9566
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009567 browseFile = do_browse(BROWSE_SAVE,
9568 (char_u *)_("Save Redirection"),
9569 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 if (browseFile == NULL)
9571 return; /* operation cancelled */
9572 vim_free(fname);
9573 fname = browseFile;
9574 eap->forceit = TRUE; /* since dialog already asked */
9575 }
9576#endif
9577
9578 redir_fd = open_exfile(fname, eap->forceit, mode);
9579 vim_free(fname);
9580 }
9581#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009582 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 {
9584 /* redirect to a register a-z (resp. A-Z for appending) */
9585 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009586 ++arg;
9587 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009589 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009590 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009592 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009594 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009595 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009596 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009597 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009599 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009600 if (*arg == '>')
9601 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009602 /* Make register empty when not using @A-@Z and the
9603 * command is valid. */
9604 if (*arg == NUL && !isupper(redir_reg))
9605 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009607 }
9608 if (*arg != NUL)
9609 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009610 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009611 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009612 }
9613 }
9614 else if (*arg == '=' && arg[1] == '>')
9615 {
9616 int append;
9617
9618 /* redirect to a variable */
9619 close_redir();
9620 arg += 2;
9621
9622 if (*arg == '>')
9623 {
9624 ++arg;
9625 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 }
9627 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009628 append = FALSE;
9629
9630 if (var_redir_start(skipwhite(arg), append) == OK)
9631 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632 }
9633#endif
9634
9635 /* TODO: redirect to a buffer */
9636
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009638 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009640
9641 /* Make sure redirection is not off. Can happen for cmdline completion
9642 * that indirectly invokes a command to catch its output. */
9643 if (redir_fd != NULL
9644#ifdef FEAT_EVAL
9645 || redir_reg || redir_vname
9646#endif
9647 )
9648 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649}
9650
9651/*
9652 * ":redraw": force redraw
9653 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009654 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009655ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656{
9657 int r = RedrawingDisabled;
9658 int p = p_lz;
9659
9660 RedrawingDisabled = 0;
9661 p_lz = FALSE;
9662 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009663 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664#ifdef FEAT_TITLE
9665 if (need_maketitle)
9666 maketitle();
9667#endif
9668 RedrawingDisabled = r;
9669 p_lz = p;
9670
9671 /* Reset msg_didout, so that a message that's there is overwritten. */
9672 msg_didout = FALSE;
9673 msg_col = 0;
9674
Bram Moolenaar56667a52013-07-17 11:54:28 +02009675 /* No need to wait after an intentional redraw. */
9676 need_wait_return = FALSE;
9677
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 out_flush();
9679}
9680
9681/*
9682 * ":redrawstatus": force redraw of status line(s)
9683 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009685ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686{
9687#if defined(FEAT_WINDOWS)
9688 int r = RedrawingDisabled;
9689 int p = p_lz;
9690
9691 RedrawingDisabled = 0;
9692 p_lz = FALSE;
9693 if (eap->forceit)
9694 status_redraw_all();
9695 else
9696 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009697 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 RedrawingDisabled = r;
9699 p_lz = p;
9700 out_flush();
9701#endif
9702}
9703
9704 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009705close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706{
9707 if (redir_fd != NULL)
9708 {
9709 fclose(redir_fd);
9710 redir_fd = NULL;
9711 }
9712#ifdef FEAT_EVAL
9713 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009714 if (redir_vname)
9715 {
9716 var_redir_stop();
9717 redir_vname = 0;
9718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719#endif
9720}
9721
9722#if defined(FEAT_SESSION) && defined(USE_CRNL)
9723# define MKSESSION_NL
9724static int mksession_nl = FALSE; /* use NL only in put_eol() */
9725#endif
9726
9727/*
9728 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9729 */
9730 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009731ex_mkrc(
9732 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733{
9734 FILE *fd;
9735 int failed = FALSE;
9736 char_u *fname;
9737#ifdef FEAT_BROWSE
9738 char_u *browseFile = NULL;
9739#endif
9740#ifdef FEAT_SESSION
9741 int view_session = FALSE;
9742 int using_vdir = FALSE; /* using 'viewdir'? */
9743 char_u *viewFile = NULL;
9744 unsigned *flagp;
9745#endif
9746
9747 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9748 {
9749#ifdef FEAT_SESSION
9750 view_session = TRUE;
9751#else
9752 ex_ni(eap);
9753 return;
9754#endif
9755 }
9756
9757#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009758 /* Use the short file name until ":lcd" is used. We also don't use the
9759 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009760 did_lcd = FALSE;
9761
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9763 if (eap->cmdidx == CMD_mkview
9764 && (*eap->arg == NUL
9765 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9766 {
9767 eap->forceit = TRUE;
9768 fname = get_view_file(*eap->arg);
9769 if (fname == NULL)
9770 return;
9771 viewFile = fname;
9772 using_vdir = TRUE;
9773 }
9774 else
9775#endif
9776 if (*eap->arg != NUL)
9777 fname = eap->arg;
9778 else if (eap->cmdidx == CMD_mkvimrc)
9779 fname = (char_u *)VIMRC_FILE;
9780#ifdef FEAT_SESSION
9781 else if (eap->cmdidx == CMD_mksession)
9782 fname = (char_u *)SESSION_FILE;
9783#endif
9784 else
9785 fname = (char_u *)EXRC_FILE;
9786
9787#ifdef FEAT_BROWSE
9788 if (cmdmod.browse)
9789 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009790 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791# ifdef FEAT_SESSION
9792 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9793 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9794# endif
9795 (char_u *)_("Save Setup"),
9796 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9797 if (browseFile == NULL)
9798 goto theend;
9799 fname = browseFile;
9800 eap->forceit = TRUE; /* since dialog already asked */
9801 }
9802#endif
9803
9804#if defined(FEAT_SESSION) && defined(vim_mkdir)
9805 /* When using 'viewdir' may have to create the directory. */
9806 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009807 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808#endif
9809
9810 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9811 if (fd != NULL)
9812 {
9813#ifdef FEAT_SESSION
9814 if (eap->cmdidx == CMD_mkview)
9815 flagp = &vop_flags;
9816 else
9817 flagp = &ssop_flags;
9818#endif
9819
9820#ifdef MKSESSION_NL
9821 /* "unix" in 'sessionoptions': use NL line separator */
9822 if (view_session && (*flagp & SSOP_UNIX))
9823 mksession_nl = TRUE;
9824#endif
9825
9826 /* Write the version command for :mkvimrc */
9827 if (eap->cmdidx == CMD_mkvimrc)
9828 (void)put_line(fd, "version 6.0");
9829
9830#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009831 if (eap->cmdidx == CMD_mksession)
9832 {
9833 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9834 failed = TRUE;
9835 }
9836
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837 if (eap->cmdidx != CMD_mkview)
9838#endif
9839 {
9840 /* Write setting 'compatible' first, because it has side effects.
9841 * For that same reason only do it when needed. */
9842 if (p_cp)
9843 (void)put_line(fd, "if !&cp | set cp | endif");
9844 else
9845 (void)put_line(fd, "if &cp | set nocp | endif");
9846 }
9847
9848#ifdef FEAT_SESSION
9849 if (!view_session
9850 || (eap->cmdidx == CMD_mksession
9851 && (*flagp & SSOP_OPTIONS)))
9852#endif
9853 failed |= (makemap(fd, NULL) == FAIL
9854 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9855
9856#ifdef FEAT_SESSION
9857 if (!failed && view_session)
9858 {
9859 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9860 failed = TRUE;
9861 if (eap->cmdidx == CMD_mksession)
9862 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009863 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864
Bram Moolenaard9462e32011-04-11 21:35:11 +02009865 dirnow = alloc(MAXPATHL);
9866 if (dirnow == NULL)
9867 failed = TRUE;
9868 else
9869 {
9870 /*
9871 * Change to session file's dir.
9872 */
9873 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009875 *dirnow = NUL;
9876 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9877 {
9878 if (vim_chdirfile(fname) == OK)
9879 shorten_fnames(TRUE);
9880 }
9881 else if (*dirnow != NUL
9882 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9883 {
9884 if (mch_chdir((char *)globaldir) == 0)
9885 shorten_fnames(TRUE);
9886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887
Bram Moolenaard9462e32011-04-11 21:35:11 +02009888 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889
Bram Moolenaard9462e32011-04-11 21:35:11 +02009890 /* restore original dir */
9891 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009893 {
9894 if (mch_chdir((char *)dirnow) != 0)
9895 EMSG(_(e_prev_dir));
9896 shorten_fnames(TRUE);
9897 }
9898 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 }
9900 }
9901 else
9902 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009903 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9904 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 }
9906 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9907 == FAIL)
9908 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009909 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9910 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009911 if (eap->cmdidx == CMD_mksession)
9912 {
9913 if (put_line(fd, "unlet SessionLoad") == FAIL)
9914 failed = TRUE;
9915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916 }
9917#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009918 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9919 failed = TRUE;
9920
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 failed |= fclose(fd);
9922
9923 if (failed)
9924 EMSG(_(e_write));
9925#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9926 else if (eap->cmdidx == CMD_mksession)
9927 {
9928 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009929 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930
Bram Moolenaard9462e32011-04-11 21:35:11 +02009931 tbuf = alloc(MAXPATHL);
9932 if (tbuf != NULL)
9933 {
9934 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9935 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9936 vim_free(tbuf);
9937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 }
9939#endif
9940#ifdef MKSESSION_NL
9941 mksession_nl = FALSE;
9942#endif
9943 }
9944
9945#ifdef FEAT_BROWSE
9946theend:
9947 vim_free(browseFile);
9948#endif
9949#ifdef FEAT_SESSION
9950 vim_free(viewFile);
9951#endif
9952}
9953
Bram Moolenaardf177f62005-02-22 08:39:57 +00009954#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9955 || defined(PROTO)
9956 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009957vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009958{
9959 if (vim_mkdir(name, prot) != 0)
9960 {
9961 EMSG2(_("E739: Cannot create directory: %s"), name);
9962 return FAIL;
9963 }
9964 return OK;
9965}
9966#endif
9967
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968/*
9969 * Open a file for writing for an Ex command, with some checks.
9970 * Return file descriptor, or NULL on failure.
9971 */
9972 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009973open_exfile(
9974 char_u *fname,
9975 int forceit,
9976 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977{
9978 FILE *fd;
9979
9980#ifdef UNIX
9981 /* with Unix it is possible to open a directory */
9982 if (mch_isdir(fname))
9983 {
9984 EMSG2(_(e_isadir2), fname);
9985 return NULL;
9986 }
9987#endif
9988 if (!forceit && *mode != 'a' && vim_fexists(fname))
9989 {
9990 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9991 return NULL;
9992 }
9993
9994 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9995 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9996
9997 return fd;
9998}
9999
10000/*
10001 * ":mark" and ":k".
10002 */
10003 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010004ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005{
10006 pos_T pos;
10007
10008 if (*eap->arg == NUL) /* No argument? */
10009 EMSG(_(e_argreq));
10010 else if (eap->arg[1] != NUL) /* more than one character? */
10011 EMSG(_(e_trailing));
10012 else
10013 {
10014 pos = curwin->w_cursor; /* save curwin->w_cursor */
10015 curwin->w_cursor.lnum = eap->line2;
10016 beginline(BL_WHITE | BL_FIX);
10017 if (setmark(*eap->arg) == FAIL) /* set mark */
10018 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10019 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10020 }
10021}
10022
10023/*
10024 * Update w_topline, w_leftcol and the cursor position.
10025 */
10026 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010027update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028{
10029 check_cursor(); /* put cursor on valid line */
10030 update_topline();
10031 if (!curwin->w_p_wrap)
10032 validate_cursor();
10033 update_curswant();
10034}
10035
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036/*
10037 * ":normal[!] {commands}": Execute normal mode commands.
10038 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010039 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010040ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 int save_msg_scroll = msg_scroll;
10043 int save_restart_edit = restart_edit;
10044 int save_msg_didout = msg_didout;
10045 int save_State = State;
10046 tasave_T tabuf;
10047 int save_insertmode = p_im;
10048 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010049 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050#ifdef FEAT_MBYTE
10051 char_u *arg = NULL;
10052 int l;
10053 char_u *p;
10054#endif
10055
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010056 if (ex_normal_lock > 0)
10057 {
10058 EMSG(_(e_secure));
10059 return;
10060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061 if (ex_normal_busy >= p_mmd)
10062 {
10063 EMSG(_("E192: Recursive use of :normal too deep"));
10064 return;
10065 }
10066 ++ex_normal_busy;
10067
10068 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10069 restart_edit = 0; /* don't go to Insert mode */
10070 p_im = FALSE; /* don't use 'insertmode' */
10071
10072#ifdef FEAT_MBYTE
10073 /*
10074 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10075 * this for the K_SPECIAL leading byte, otherwise special keys will not
10076 * work.
10077 */
10078 if (has_mbyte)
10079 {
10080 int len = 0;
10081
10082 /* Count the number of characters to be escaped. */
10083 for (p = eap->arg; *p != NUL; ++p)
10084 {
10085# ifdef FEAT_GUI
10086 if (*p == CSI) /* leadbyte CSI */
10087 len += 2;
10088# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010089 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10091# ifdef FEAT_GUI
10092 || *p == CSI
10093# endif
10094 )
10095 len += 2;
10096 }
10097 if (len > 0)
10098 {
10099 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10100 if (arg != NULL)
10101 {
10102 len = 0;
10103 for (p = eap->arg; *p != NUL; ++p)
10104 {
10105 arg[len++] = *p;
10106# ifdef FEAT_GUI
10107 if (*p == CSI)
10108 {
10109 arg[len++] = KS_EXTRA;
10110 arg[len++] = (int)KE_CSI;
10111 }
10112# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010113 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 {
10115 arg[len++] = *++p;
10116 if (*p == K_SPECIAL)
10117 {
10118 arg[len++] = KS_SPECIAL;
10119 arg[len++] = KE_FILLER;
10120 }
10121# ifdef FEAT_GUI
10122 else if (*p == CSI)
10123 {
10124 arg[len++] = KS_EXTRA;
10125 arg[len++] = (int)KE_CSI;
10126 }
10127# endif
10128 }
10129 arg[len] = NUL;
10130 }
10131 }
10132 }
10133 }
10134#endif
10135
10136 /*
10137 * Save the current typeahead. This is required to allow using ":normal"
10138 * from an event handler and makes sure we don't hang when the argument
10139 * ends with half a command.
10140 */
10141 save_typeahead(&tabuf);
10142 if (tabuf.typebuf_valid)
10143 {
10144 /*
10145 * Repeat the :normal command for each line in the range. When no
10146 * range given, execute it just once, without positioning the cursor
10147 * first.
10148 */
10149 do
10150 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151 if (eap->addr_count != 0)
10152 {
10153 curwin->w_cursor.lnum = eap->line1++;
10154 curwin->w_cursor.col = 0;
10155 }
10156
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010157 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158#ifdef FEAT_MBYTE
10159 arg != NULL ? arg :
10160#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010161 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162 }
10163 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10164 }
10165
10166 /* Might not return to the main loop when in an event handler. */
10167 update_topline_cursor();
10168
10169 /* Restore the previous typeahead. */
10170 restore_typeahead(&tabuf);
10171
10172 --ex_normal_busy;
10173 msg_scroll = save_msg_scroll;
10174 restart_edit = save_restart_edit;
10175 p_im = save_insertmode;
10176 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010177 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
10179
10180 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +010010181 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +010010183#ifdef FEAT_MOUSE
10184 setmouse();
10185#endif
10186#ifdef CURSOR_SHAPE
10187 ui_cursor_shape(); /* may show different cursor shape */
10188#endif
10189
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190#ifdef FEAT_MBYTE
10191 vim_free(arg);
10192#endif
10193}
10194
10195/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010196 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197 */
10198 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010199ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010201 if (eap->forceit)
10202 {
10203 coladvance((colnr_T)MAXCOL);
10204 curwin->w_curswant = MAXCOL;
10205 curwin->w_set_curswant = FALSE;
10206 }
10207
Bram Moolenaara40c5002005-01-09 21:16:21 +000010208 /* Ignore the command when already in Insert mode. Inserting an
10209 * expression register that invokes a function can do this. */
10210 if (State & INSERT)
10211 return;
10212
Bram Moolenaar64969662005-12-14 21:59:55 +000010213 if (eap->cmdidx == CMD_startinsert)
10214 restart_edit = 'a';
10215 else if (eap->cmdidx == CMD_startreplace)
10216 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010218 restart_edit = 'V';
10219
10220 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010222 if (eap->cmdidx == CMD_startinsert)
10223 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 curwin->w_curswant = 0; /* avoid MAXCOL */
10225 }
10226}
10227
10228/*
10229 * ":stopinsert"
10230 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010232ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233{
10234 restart_edit = 0;
10235 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010236 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010239/*
10240 * Execute normal mode command "cmd".
10241 * "remap" can be REMAP_NONE or REMAP_YES.
10242 */
10243 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010244exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010245{
Bram Moolenaar25281632016-01-21 23:32:32 +010010246 /* Stuff the argument into the typeahead buffer. */
10247 ins_typebuf(cmd, remap, 0, TRUE, silent);
10248 exec_normal(FALSE);
10249}
Bram Moolenaar25281632016-01-21 23:32:32 +010010250
Bram Moolenaar25281632016-01-21 23:32:32 +010010251/*
10252 * Execute normal_cmd() until there is no typeahead left.
10253 */
10254 void
10255exec_normal(int was_typed)
10256{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010257 oparg_T oa;
10258
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010259 clear_oparg(&oa);
10260 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010261 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10262 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010263 {
10264 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010265 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010266 }
10267}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010268
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269#ifdef FEAT_FIND_ID
10270 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010271ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272{
10273 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10274 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10275 (linenr_T)1, (linenr_T)MAXLNUM);
10276}
10277
10278#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
10279/*
10280 * ":psearch"
10281 */
10282 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010283ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284{
10285 g_do_tagpreview = p_pvh;
10286 ex_findpat(eap);
10287 g_do_tagpreview = 0;
10288}
10289#endif
10290
10291 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010292ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293{
10294 int whole = TRUE;
10295 long n;
10296 char_u *p;
10297 int action;
10298
10299 switch (cmdnames[eap->cmdidx].cmd_name[2])
10300 {
10301 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10302 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10303 action = ACTION_GOTO;
10304 else
10305 action = ACTION_SHOW;
10306 break;
10307 case 'i': /* ":ilist" and ":dlist" */
10308 action = ACTION_SHOW_ALL;
10309 break;
10310 case 'u': /* ":ijump" and ":djump" */
10311 action = ACTION_GOTO;
10312 break;
10313 default: /* ":isplit" and ":dsplit" */
10314 action = ACTION_SPLIT;
10315 break;
10316 }
10317
10318 n = 1;
10319 if (vim_isdigit(*eap->arg)) /* get count */
10320 {
10321 n = getdigits(&eap->arg);
10322 eap->arg = skipwhite(eap->arg);
10323 }
10324 if (*eap->arg == '/') /* Match regexp, not just whole words */
10325 {
10326 whole = FALSE;
10327 ++eap->arg;
10328 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10329 if (*p)
10330 {
10331 *p++ = NUL;
10332 p = skipwhite(p);
10333
10334 /* Check for trailing illegal characters */
10335 if (!ends_excmd(*p))
10336 eap->errmsg = e_trailing;
10337 else
10338 eap->nextcmd = check_nextcmd(p);
10339 }
10340 }
10341 if (!eap->skip)
10342 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10343 whole, !eap->forceit,
10344 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10345 n, action, eap->line1, eap->line2);
10346}
10347#endif
10348
10349#ifdef FEAT_WINDOWS
10350
10351# ifdef FEAT_QUICKFIX
10352/*
10353 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10354 */
10355 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010356ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010358 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10360}
10361
10362/*
10363 * ":pedit"
10364 */
10365 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010366ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367{
10368 win_T *curwin_save = curwin;
10369
10370 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010371 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372 keep_help_flag = curwin_save->w_buffer->b_help;
10373 do_exedit(eap, NULL);
10374 keep_help_flag = FALSE;
10375 if (curwin != curwin_save && win_valid(curwin_save))
10376 {
10377 /* Return cursor to where we were */
10378 validate_cursor();
10379 redraw_later(VALID);
10380 win_enter(curwin_save, TRUE);
10381 }
10382 g_do_tagpreview = 0;
10383}
10384# endif
10385
10386/*
10387 * ":stag", ":stselect" and ":stjump".
10388 */
10389 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010390ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391{
10392 postponed_split = -1;
10393 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010394 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10396 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010397 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398}
10399#endif
10400
10401/*
10402 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10403 */
10404 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010405ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406{
10407 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10408}
10409
10410 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010411ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412{
10413 int cmd;
10414
10415 switch (name[1])
10416 {
10417 case 'j': cmd = DT_JUMP; /* ":tjump" */
10418 break;
10419 case 's': cmd = DT_SELECT; /* ":tselect" */
10420 break;
10421 case 'p': cmd = DT_PREV; /* ":tprevious" */
10422 break;
10423 case 'N': cmd = DT_PREV; /* ":tNext" */
10424 break;
10425 case 'n': cmd = DT_NEXT; /* ":tnext" */
10426 break;
10427 case 'o': cmd = DT_POP; /* ":pop" */
10428 break;
10429 case 'f': /* ":tfirst" */
10430 case 'r': cmd = DT_FIRST; /* ":trewind" */
10431 break;
10432 case 'l': cmd = DT_LAST; /* ":tlast" */
10433 break;
10434 default: /* ":tag" */
10435#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010436 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010438 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439 return;
10440 }
10441#endif
10442 cmd = DT_TAG;
10443 break;
10444 }
10445
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010446 if (name[0] == 'l')
10447 {
10448#ifndef FEAT_QUICKFIX
10449 ex_ni(eap);
10450 return;
10451#else
10452 cmd = DT_LTAG;
10453#endif
10454 }
10455
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10457 eap->forceit, TRUE);
10458}
10459
10460/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010461 * Check "str" for starting with a special cmdline variable.
10462 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10463 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10464 */
10465 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010466find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010467{
10468 int len;
10469 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010470 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010471 "%",
10472#define SPEC_PERC 0
10473 "#",
10474#define SPEC_HASH 1
10475 "<cword>", /* cursor word */
10476#define SPEC_CWORD 2
10477 "<cWORD>", /* cursor WORD */
10478#define SPEC_CCWORD 3
10479 "<cfile>", /* cursor path name */
10480#define SPEC_CFILE 4
10481 "<sfile>", /* ":so" file name */
10482#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010483 "<slnum>", /* ":so" file line number */
10484#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010485#ifdef FEAT_AUTOCMD
10486 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010487# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010488 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010489# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010490 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010491# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010492#endif
10493#ifdef FEAT_CLIENTSERVER
10494 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010495# ifdef FEAT_AUTOCMD
10496# define SPEC_CLIENT 10
10497# else
10498# define SPEC_CLIENT 7
10499# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010500#endif
10501 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010502
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010503 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010504 {
10505 len = (int)STRLEN(spec_str[i]);
10506 if (STRNCMP(src, spec_str[i], len) == 0)
10507 {
10508 *usedlen = len;
10509 return i;
10510 }
10511 }
10512 return -1;
10513}
10514
10515/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516 * Evaluate cmdline variables.
10517 *
10518 * change '%' to curbuf->b_ffname
10519 * '#' to curwin->w_altfile
10520 * '<cword>' to word under the cursor
10521 * '<cWORD>' to WORD under the cursor
10522 * '<cfile>' to path name under the cursor
10523 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010524 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525 * '<afile>' to file name for autocommand
10526 * '<abuf>' to buffer number for autocommand
10527 * '<amatch>' to matching name for autocommand
10528 *
10529 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10530 * "" for error without a message) and NULL is returned.
10531 * Returns an allocated string if a valid match was found.
10532 * Returns NULL if no match was found. "usedlen" then still contains the
10533 * number of characters to skip.
10534 */
10535 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010536eval_vars(
10537 char_u *src, /* pointer into commandline */
10538 char_u *srcstart, /* beginning of valid memory for src */
10539 int *usedlen, /* characters after src that are used */
10540 linenr_T *lnump, /* line number for :e command, or NULL */
10541 char_u **errormsg, /* pointer to error message */
10542 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010543 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544{
10545 int i;
10546 char_u *s;
10547 char_u *result;
10548 char_u *resultbuf = NULL;
10549 int resultlen;
10550 buf_T *buf;
10551 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10552 int spec_idx;
10553#ifdef FEAT_MODIFY_FNAME
10554 int skip_mod = FALSE;
10555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557
10558 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010559 if (escaped != NULL)
10560 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561
10562 /*
10563 * Check if there is something to do.
10564 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010565 spec_idx = find_cmdline_var(src, usedlen);
10566 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567 {
10568 *usedlen = 1;
10569 return NULL;
10570 }
10571
10572 /*
10573 * Skip when preceded with a backslash "\%" and "\#".
10574 * Note: In "\\%" the % is also not recognized!
10575 */
10576 if (src > srcstart && src[-1] == '\\')
10577 {
10578 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010579 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580 return NULL;
10581 }
10582
10583 /*
10584 * word or WORD under cursor
10585 */
10586 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10587 {
10588 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10589 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10590 if (resultlen == 0)
10591 {
10592 *errormsg = (char_u *)"";
10593 return NULL;
10594 }
10595 }
10596
10597 /*
10598 * '#': Alternate file name
10599 * '%': Current file name
10600 * File name under the cursor
10601 * File name for autocommand
10602 * and following modifiers
10603 */
10604 else
10605 {
10606 switch (spec_idx)
10607 {
10608 case SPEC_PERC: /* '%': current file */
10609 if (curbuf->b_fname == NULL)
10610 {
10611 result = (char_u *)"";
10612 valid = 0; /* Must have ":p:h" to be valid */
10613 }
10614 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616 break;
10617
10618 case SPEC_HASH: /* '#' or "#99": alternate file */
10619 if (src[1] == '#') /* "##": the argument list */
10620 {
10621 result = arg_all();
10622 resultbuf = result;
10623 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010624 if (escaped != NULL)
10625 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626#ifdef FEAT_MODIFY_FNAME
10627 skip_mod = TRUE;
10628#endif
10629 break;
10630 }
10631 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010632 if (*s == '<') /* "#<99" uses v:oldfiles */
10633 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634 i = (int)getdigits(&s);
10635 *usedlen = (int)(s - src); /* length of what we expand */
10636
Bram Moolenaard812df62008-11-09 12:46:09 +000010637 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010639 if (*usedlen < 2)
10640 {
10641 /* Should we give an error message for #<text? */
10642 *usedlen = 1;
10643 return NULL;
10644 }
10645#ifdef FEAT_EVAL
10646 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10647 (long)i);
10648 if (result == NULL)
10649 {
10650 *errormsg = (char_u *)"";
10651 return NULL;
10652 }
10653#else
10654 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657 }
10658 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010659 {
10660 buf = buflist_findnr(i);
10661 if (buf == NULL)
10662 {
10663 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10664 return NULL;
10665 }
10666 if (lnump != NULL)
10667 *lnump = ECMD_LAST;
10668 if (buf->b_fname == NULL)
10669 {
10670 result = (char_u *)"";
10671 valid = 0; /* Must have ":p:h" to be valid */
10672 }
10673 else
10674 result = buf->b_fname;
10675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010676 break;
10677
10678#ifdef FEAT_SEARCHPATH
10679 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010680 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681 if (result == NULL)
10682 {
10683 *errormsg = (char_u *)"";
10684 return NULL;
10685 }
10686 resultbuf = result; /* remember allocated string */
10687 break;
10688#endif
10689
10690#ifdef FEAT_AUTOCMD
10691 case SPEC_AFILE: /* file name for autocommand */
10692 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010693 if (result != NULL && !autocmd_fname_full)
10694 {
10695 /* Still need to turn the fname into a full path. It is
10696 * postponed to avoid a delay when <afile> is not used. */
10697 autocmd_fname_full = TRUE;
10698 result = FullName_save(autocmd_fname, FALSE);
10699 vim_free(autocmd_fname);
10700 autocmd_fname = result;
10701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702 if (result == NULL)
10703 {
10704 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10705 return NULL;
10706 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010707 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708 break;
10709
10710 case SPEC_ABUF: /* buffer number for autocommand */
10711 if (autocmd_bufnr <= 0)
10712 {
10713 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10714 return NULL;
10715 }
10716 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10717 result = strbuf;
10718 break;
10719
10720 case SPEC_AMATCH: /* match name for autocommand */
10721 result = autocmd_match;
10722 if (result == NULL)
10723 {
10724 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10725 return NULL;
10726 }
10727 break;
10728
10729#endif
10730 case SPEC_SFILE: /* file name for ":so" command */
10731 result = sourcing_name;
10732 if (result == NULL)
10733 {
10734 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10735 return NULL;
10736 }
10737 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010738 case SPEC_SLNUM: /* line in file for ":so" command */
10739 if (sourcing_name == NULL || sourcing_lnum == 0)
10740 {
10741 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10742 return NULL;
10743 }
10744 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10745 result = strbuf;
10746 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747#if defined(FEAT_CLIENTSERVER)
10748 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010749 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10750 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751 result = strbuf;
10752 break;
10753#endif
10754 }
10755
10756 resultlen = (int)STRLEN(result); /* length of new string */
10757 if (src[*usedlen] == '<') /* remove the file name extension */
10758 {
10759 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010761 resultlen = (int)(s - result);
10762 }
10763#ifdef FEAT_MODIFY_FNAME
10764 else if (!skip_mod)
10765 {
10766 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10767 &resultlen);
10768 if (result == NULL)
10769 {
10770 *errormsg = (char_u *)"";
10771 return NULL;
10772 }
10773 }
10774#endif
10775 }
10776
10777 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10778 {
10779 if (valid != VALID_HEAD + VALID_PATH)
10780 /* xgettext:no-c-format */
10781 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10782 else
10783 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10784 result = NULL;
10785 }
10786 else
10787 result = vim_strnsave(result, resultlen);
10788 vim_free(resultbuf);
10789 return result;
10790}
10791
10792/*
10793 * Concatenate all files in the argument list, separated by spaces, and return
10794 * it in one allocated string.
10795 * Spaces and backslashes in the file names are escaped with a backslash.
10796 * Returns NULL when out of memory.
10797 */
10798 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010799arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800{
10801 int len;
10802 int idx;
10803 char_u *retval = NULL;
10804 char_u *p;
10805
10806 /*
10807 * Do this loop two times:
10808 * first time: compute the total length
10809 * second time: concatenate the names
10810 */
10811 for (;;)
10812 {
10813 len = 0;
10814 for (idx = 0; idx < ARGCOUNT; ++idx)
10815 {
10816 p = alist_name(&ARGLIST[idx]);
10817 if (p != NULL)
10818 {
10819 if (len > 0)
10820 {
10821 /* insert a space in between names */
10822 if (retval != NULL)
10823 retval[len] = ' ';
10824 ++len;
10825 }
10826 for ( ; *p != NUL; ++p)
10827 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010828 if (*p == ' '
10829#ifndef BACKSLASH_IN_FILENAME
10830 || *p == '\\'
10831#endif
10832 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833 {
10834 /* insert a backslash */
10835 if (retval != NULL)
10836 retval[len] = '\\';
10837 ++len;
10838 }
10839 if (retval != NULL)
10840 retval[len] = *p;
10841 ++len;
10842 }
10843 }
10844 }
10845
10846 /* second time: break here */
10847 if (retval != NULL)
10848 {
10849 retval[len] = NUL;
10850 break;
10851 }
10852
10853 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010854 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 if (retval == NULL)
10856 break;
10857 }
10858
10859 return retval;
10860}
10861
10862#if defined(FEAT_AUTOCMD) || defined(PROTO)
10863/*
10864 * Expand the <sfile> string in "arg".
10865 *
10866 * Returns an allocated string, or NULL for any error.
10867 */
10868 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010869expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870{
10871 char_u *errormsg;
10872 int len;
10873 char_u *result;
10874 char_u *newres;
10875 char_u *repl;
10876 int srclen;
10877 char_u *p;
10878
10879 result = vim_strsave(arg);
10880 if (result == NULL)
10881 return NULL;
10882
10883 for (p = result; *p; )
10884 {
10885 if (STRNCMP(p, "<sfile>", 7) != 0)
10886 ++p;
10887 else
10888 {
10889 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010890 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891 if (errormsg != NULL)
10892 {
10893 if (*errormsg)
10894 emsg(errormsg);
10895 vim_free(result);
10896 return NULL;
10897 }
10898 if (repl == NULL) /* no match (cannot happen) */
10899 {
10900 p += srclen;
10901 continue;
10902 }
10903 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10904 newres = alloc(len);
10905 if (newres == NULL)
10906 {
10907 vim_free(repl);
10908 vim_free(result);
10909 return NULL;
10910 }
10911 mch_memmove(newres, result, (size_t)(p - result));
10912 STRCPY(newres + (p - result), repl);
10913 len = (int)STRLEN(newres);
10914 STRCAT(newres, p + srclen);
10915 vim_free(repl);
10916 vim_free(result);
10917 result = newres;
10918 p = newres + len; /* continue after the match */
10919 }
10920 }
10921
10922 return result;
10923}
10924#endif
10925
10926#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010010927static int ses_winsizes(FILE *fd, int restore_size,
10928 win_T *tab_firstwin);
10929static int ses_win_rec(FILE *fd, frame_T *fr);
10930static frame_T *ses_skipframe(frame_T *fr);
10931static int ses_do_frame(frame_T *fr);
10932static int ses_do_win(win_T *wp);
10933static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
10934static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
10935static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936
10937/*
10938 * Write openfile commands for the current buffers to an .exrc file.
10939 * Return FAIL on error, OK otherwise.
10940 */
10941 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010942makeopens(
10943 FILE *fd,
10944 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945{
10946 buf_T *buf;
10947 int only_save_windows = TRUE;
10948 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949 int restore_size = TRUE;
10950 win_T *wp;
10951 char_u *sname;
10952 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010953 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010954 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010955 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010956 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010957 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010958 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959
10960 if (ssop_flags & SSOP_BUFFERS)
10961 only_save_windows = FALSE; /* Save ALL buffers */
10962
10963 /*
10964 * Begin by setting the this_session variable, and then other
10965 * sessionable variables.
10966 */
10967#ifdef FEAT_EVAL
10968 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10969 return FAIL;
10970 if (ssop_flags & SSOP_GLOBALS)
10971 if (store_session_globals(fd) == FAIL)
10972 return FAIL;
10973#endif
10974
10975 /*
10976 * Close all windows but one.
10977 */
10978 if (put_line(fd, "silent only") == FAIL)
10979 return FAIL;
10980
10981 /*
10982 * Now a :cd command to the session directory or the current directory
10983 */
10984 if (ssop_flags & SSOP_SESDIR)
10985 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010986 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10987 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988 return FAIL;
10989 }
10990 else if (ssop_flags & SSOP_CURDIR)
10991 {
10992 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10993 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010994 || fputs("cd ", fd) < 0
10995 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10996 || put_eol(fd) == FAIL)
10997 {
10998 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001 vim_free(sname);
11002 }
11003
11004 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011005 * If there is an empty, unnamed buffer we will wipe it out later.
11006 * Remember the buffer number.
11007 */
11008 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11009 return FAIL;
11010 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11011 return FAIL;
11012 if (put_line(fd, "endif") == FAIL)
11013 return FAIL;
11014
11015 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016 * Now save the current files, current buffer first.
11017 */
11018 if (put_line(fd, "set shortmess=aoO") == FAIL)
11019 return FAIL;
11020
11021 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011022 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 {
11024 if (!(only_save_windows && buf->b_nwindows == 0)
11025 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
11026 && buf->b_fname != NULL
11027 && buf->b_p_bl)
11028 {
11029 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11030 : buf->b_wininfo->wi_fpos.lnum) < 0
11031 || ses_fname(fd, buf, &ssop_flags) == FAIL)
11032 return FAIL;
11033 }
11034 }
11035
11036 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011037 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11039 return FAIL;
11040
11041 if (ssop_flags & SSOP_RESIZE)
11042 {
11043 /* Note: after the restore we still check it worked!*/
11044 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11045 || put_eol(fd) == FAIL)
11046 return FAIL;
11047 }
11048
11049#ifdef FEAT_GUI
11050 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11051 {
11052 int x, y;
11053
11054 if (gui_mch_get_winpos(&x, &y) == OK)
11055 {
11056 /* Note: after the restore we still check it worked!*/
11057 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11058 return FAIL;
11059 }
11060 }
11061#endif
11062
11063 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011064 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11065 * will be displayed when creating the next tab. That resizes the windows
11066 * in the first tab, which may cause problems. Set 'showtabline' to 2
11067 * temporarily to avoid that.
11068 */
11069 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11070 {
11071 if (put_line(fd, "set stal=2") == FAIL)
11072 return FAIL;
11073 restore_stal = TRUE;
11074 }
11075
11076 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011077 * May repeat putting Windows for each tab, when "tabpages" is in
11078 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011079 * Don't use goto_tabpage(), it may change directory and trigger
11080 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011081 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011082 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011083 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011084 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011085 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011086 int need_tabnew = FALSE;
11087 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011088
Bram Moolenaar18144c82006-04-12 21:52:12 +000011089 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011091 tabpage_T *tp = find_tabpage(tabnr);
11092
11093 if (tp == NULL)
11094 break; /* done all tab pages */
11095 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011096 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011097 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011098 tab_topframe = topframe;
11099 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011100 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011101 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011102 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011103 tab_topframe = tp->tp_topframe;
11104 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011105 if (tabnr > 1)
11106 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108
Bram Moolenaar18144c82006-04-12 21:52:12 +000011109 /*
11110 * Before creating the window layout, try loading one file. If this
11111 * is aborted we don't end up with a number of useless windows.
11112 * This may have side effects! (e.g., compressed or network file).
11113 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011114 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011115 {
11116 if (ses_do_win(wp)
11117 && wp->w_buffer->b_ffname != NULL
11118 && !wp->w_buffer->b_help
11119#ifdef FEAT_QUICKFIX
11120 && !bt_nofile(wp->w_buffer)
11121#endif
11122 )
11123 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011124 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000011125 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
11126 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011127 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011128 if (!wp->w_arg_idx_invalid)
11129 edited_win = wp;
11130 break;
11131 }
11132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011134 /* If no file got edited create an empty tab page. */
11135 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11136 return FAIL;
11137
Bram Moolenaar18144c82006-04-12 21:52:12 +000011138 /*
11139 * Save current window layout.
11140 */
11141 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011143 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011145 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11146 return FAIL;
11147 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11148 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149
Bram Moolenaar18144c82006-04-12 21:52:12 +000011150 /*
11151 * Check if window sizes can be restored (no windows omitted).
11152 * Remember the window number of the current window after restoring.
11153 */
11154 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011155 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011156 {
11157 if (ses_do_win(wp))
11158 ++nr;
11159 else
11160 restore_size = FALSE;
11161 if (curwin == wp)
11162 cnr = nr;
11163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011164
Bram Moolenaar18144c82006-04-12 21:52:12 +000011165 /* Go to the first window. */
11166 if (put_line(fd, "wincmd t") == FAIL)
11167 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011168
Bram Moolenaar18144c82006-04-12 21:52:12 +000011169 /*
11170 * If more than one window, see if sizes can be restored.
11171 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11172 * resized when moving between windows.
11173 * Do this before restoring the view, so that the topline and the
11174 * cursor can be set. This is done again below.
11175 */
11176 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
11177 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011178 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011179 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011180
Bram Moolenaar18144c82006-04-12 21:52:12 +000011181 /*
11182 * Restore the view of the window (options, file, cursor, etc.).
11183 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011184 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011185 {
11186 if (!ses_do_win(wp))
11187 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011188 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11189 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011190 return FAIL;
11191 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11192 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011193 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011194 }
11195
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011196 /* The argument index in the first tab page is zero, need to set it in
11197 * each window. For further tab pages it's the window where we do
11198 * "tabedit". */
11199 cur_arg_idx = next_arg_idx;
11200
Bram Moolenaar18144c82006-04-12 21:52:12 +000011201 /*
11202 * Restore cursor to the current window if it's not the first one.
11203 */
11204 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11205 || put_eol(fd) == FAIL))
11206 return FAIL;
11207
11208 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011209 * Restore window sizes again after jumping around in windows, because
11210 * the current window has a minimum size while others may not.
11211 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011212 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011213 return FAIL;
11214
Bram Moolenaar18144c82006-04-12 21:52:12 +000011215 /* Don't continue in another tab page when doing only the current one
11216 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011217 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011218 break;
11219 }
11220
11221 if (ssop_flags & SSOP_TABPAGES)
11222 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011223 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11224 || put_eol(fd) == FAIL)
11225 return FAIL;
11226 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011227 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11228 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011229
Bram Moolenaar9c102382006-05-03 21:26:49 +000011230 /*
11231 * Wipe out an empty unnamed buffer we started in.
11232 */
11233 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11234 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011235 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011236 return FAIL;
11237 if (put_line(fd, "endif") == FAIL)
11238 return FAIL;
11239 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11240 return FAIL;
11241
11242 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11243 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11244 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11245 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246
11247 /*
11248 * Lastly, execute the x.vim file if it exists.
11249 */
11250 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11251 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011252 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253 || put_line(fd, "endif") == FAIL)
11254 return FAIL;
11255
11256 return OK;
11257}
11258
11259 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011260ses_winsizes(
11261 FILE *fd,
11262 int restore_size,
11263 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011264{
11265 int n = 0;
11266 win_T *wp;
11267
11268 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11269 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011270 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271 {
11272 if (!ses_do_win(wp))
11273 continue;
11274 ++n;
11275
11276 /* restore height when not full height */
11277 if (wp->w_height + wp->w_status_height < topframe->fr_height
11278 && (fprintf(fd,
11279 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11280 n, (long)wp->w_height, Rows / 2, Rows) < 0
11281 || put_eol(fd) == FAIL))
11282 return FAIL;
11283
11284 /* restore width when not full width */
11285 if (wp->w_width < Columns && (fprintf(fd,
11286 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11287 n, (long)wp->w_width, Columns / 2, Columns) < 0
11288 || put_eol(fd) == FAIL))
11289 return FAIL;
11290 }
11291 }
11292 else
11293 {
11294 /* Just equalise window sizes */
11295 if (put_line(fd, "wincmd =") == FAIL)
11296 return FAIL;
11297 }
11298 return OK;
11299}
11300
11301/*
11302 * Write commands to "fd" to recursively create windows for frame "fr",
11303 * horizontally and vertically split.
11304 * After the commands the last window in the frame is the current window.
11305 * Returns FAIL when writing the commands to "fd" fails.
11306 */
11307 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011308ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011309{
11310 frame_T *frc;
11311 int count = 0;
11312
11313 if (fr->fr_layout != FR_LEAF)
11314 {
11315 /* Find first frame that's not skipped and then create a window for
11316 * each following one (first frame is already there). */
11317 frc = ses_skipframe(fr->fr_child);
11318 if (frc != NULL)
11319 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11320 {
11321 /* Make window as big as possible so that we have lots of room
11322 * to split. */
11323 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11324 || put_line(fd, fr->fr_layout == FR_COL
11325 ? "split" : "vsplit") == FAIL)
11326 return FAIL;
11327 ++count;
11328 }
11329
11330 /* Go back to the first window. */
11331 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11332 ? "%dwincmd k" : "%dwincmd h", count) < 0
11333 || put_eol(fd) == FAIL))
11334 return FAIL;
11335
11336 /* Recursively create frames/windows in each window of this column or
11337 * row. */
11338 frc = ses_skipframe(fr->fr_child);
11339 while (frc != NULL)
11340 {
11341 ses_win_rec(fd, frc);
11342 frc = ses_skipframe(frc->fr_next);
11343 /* Go to next window. */
11344 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11345 return FAIL;
11346 }
11347 }
11348 return OK;
11349}
11350
11351/*
11352 * Skip frames that don't contain windows we want to save in the Session.
11353 * Returns NULL when there none.
11354 */
11355 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011356ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011357{
11358 frame_T *frc;
11359
11360 for (frc = fr; frc != NULL; frc = frc->fr_next)
11361 if (ses_do_frame(frc))
11362 break;
11363 return frc;
11364}
11365
11366/*
11367 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11368 * the Session.
11369 */
11370 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011371ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372{
11373 frame_T *frc;
11374
11375 if (fr->fr_layout == FR_LEAF)
11376 return ses_do_win(fr->fr_win);
11377 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11378 if (ses_do_frame(frc))
11379 return TRUE;
11380 return FALSE;
11381}
11382
11383/*
11384 * Return non-zero if window "wp" is to be stored in the Session.
11385 */
11386 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011387ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388{
11389 if (wp->w_buffer->b_fname == NULL
11390#ifdef FEAT_QUICKFIX
11391 /* When 'buftype' is "nofile" can't restore the window contents. */
11392 || bt_nofile(wp->w_buffer)
11393#endif
11394 )
11395 return (ssop_flags & SSOP_BLANK);
11396 if (wp->w_buffer->b_help)
11397 return (ssop_flags & SSOP_HELP);
11398 return TRUE;
11399}
11400
11401/*
11402 * Write commands to "fd" to restore the view of a window.
11403 * Caller must make sure 'scrolloff' is zero.
11404 */
11405 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011406put_view(
11407 FILE *fd,
11408 win_T *wp,
11409 int add_edit, /* add ":edit" command to view */
11410 unsigned *flagp, /* vop_flags or ssop_flags */
11411 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011412 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413{
11414 win_T *save_curwin;
11415 int f;
11416 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011417 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011418
11419 /* Always restore cursor position for ":mksession". For ":mkview" only
11420 * when 'viewoptions' contains "cursor". */
11421 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11422
11423 /*
11424 * Local argument list.
11425 */
11426 if (wp->w_alist == &global_alist)
11427 {
11428 if (put_line(fd, "argglobal") == FAIL)
11429 return FAIL;
11430 }
11431 else
11432 {
11433 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11434 flagp == &vop_flags
11435 || !(*flagp & SSOP_CURDIR)
11436 || wp->w_localdir != NULL, flagp) == FAIL)
11437 return FAIL;
11438 }
11439
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011440 /* Only when part of a session: restore the argument index. Some
11441 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011442 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011443 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011445 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446 || put_eol(fd) == FAIL)
11447 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011448 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449 }
11450
11451 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011452 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011453 {
11454 /*
11455 * Load the file.
11456 */
11457 if (wp->w_buffer->b_ffname != NULL
11458#ifdef FEAT_QUICKFIX
11459 && !bt_nofile(wp->w_buffer)
11460#endif
11461 )
11462 {
11463 /*
11464 * Editing a file in this buffer: use ":edit file".
11465 * This may have side effects! (e.g., compressed or network file).
11466 */
11467 if (fputs("edit ", fd) < 0
11468 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11469 return FAIL;
11470 }
11471 else
11472 {
11473 /* No file in this buffer, just make it empty. */
11474 if (put_line(fd, "enew") == FAIL)
11475 return FAIL;
11476#ifdef FEAT_QUICKFIX
11477 if (wp->w_buffer->b_ffname != NULL)
11478 {
11479 /* The buffer does have a name, but it's not a file name. */
11480 if (fputs("file ", fd) < 0
11481 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11482 return FAIL;
11483 }
11484#endif
11485 do_cursor = FALSE;
11486 }
11487 }
11488
11489 /*
11490 * Local mappings and abbreviations.
11491 */
11492 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11493 && makemap(fd, wp->w_buffer) == FAIL)
11494 return FAIL;
11495
11496 /*
11497 * Local options. Need to go to the window temporarily.
11498 * Store only local values when using ":mkview" and when ":mksession" is
11499 * used and 'sessionoptions' doesn't include "options".
11500 * Some folding options are always stored when "folds" is included,
11501 * otherwise the folds would not be restored correctly.
11502 */
11503 save_curwin = curwin;
11504 curwin = wp;
11505 curbuf = curwin->w_buffer;
11506 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11507 f = makeset(fd, OPT_LOCAL,
11508 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11509#ifdef FEAT_FOLDING
11510 else if (*flagp & SSOP_FOLDS)
11511 f = makefoldset(fd);
11512#endif
11513 else
11514 f = OK;
11515 curwin = save_curwin;
11516 curbuf = curwin->w_buffer;
11517 if (f == FAIL)
11518 return FAIL;
11519
11520#ifdef FEAT_FOLDING
11521 /*
11522 * Save Folds when 'buftype' is empty and for help files.
11523 */
11524 if ((*flagp & SSOP_FOLDS)
11525 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011526# ifdef FEAT_QUICKFIX
11527 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11528# endif
11529 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011530 {
11531 if (put_folds(fd, wp) == FAIL)
11532 return FAIL;
11533 }
11534#endif
11535
11536 /*
11537 * Set the cursor after creating folds, since that moves the cursor.
11538 */
11539 if (do_cursor)
11540 {
11541
11542 /* Restore the cursor line in the file and relatively in the
11543 * window. Don't use "G", it changes the jumplist. */
11544 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11545 (long)wp->w_cursor.lnum,
11546 (long)(wp->w_cursor.lnum - wp->w_topline),
11547 (long)wp->w_height / 2, (long)wp->w_height) < 0
11548 || put_eol(fd) == FAIL
11549 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11550 || put_line(fd, "exe s:l") == FAIL
11551 || put_line(fd, "normal! zt") == FAIL
11552 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11553 || put_eol(fd) == FAIL)
11554 return FAIL;
11555 /* Restore the cursor column and left offset when not wrapping. */
11556 if (wp->w_cursor.col == 0)
11557 {
11558 if (put_line(fd, "normal! 0") == FAIL)
11559 return FAIL;
11560 }
11561 else
11562 {
11563 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11564 {
11565 if (fprintf(fd,
11566 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011567 (long)wp->w_virtcol + 1,
11568 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011569 (long)wp->w_width / 2, (long)wp->w_width) < 0
11570 || put_eol(fd) == FAIL
11571 || put_line(fd, "if s:c > 0") == FAIL
11572 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011573 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11574 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011575 || put_eol(fd) == FAIL
11576 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011577 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011578 || put_eol(fd) == FAIL
11579 || put_line(fd, "endif") == FAIL)
11580 return FAIL;
11581 }
11582 else
11583 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011584 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011585 || put_eol(fd) == FAIL)
11586 return FAIL;
11587 }
11588 }
11589 }
11590
11591 /*
11592 * Local directory.
11593 */
11594 if (wp->w_localdir != NULL)
11595 {
11596 if (fputs("lcd ", fd) < 0
11597 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11598 || put_eol(fd) == FAIL)
11599 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011600 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011601 }
11602
11603 return OK;
11604}
11605
11606/*
11607 * Write an argument list to the session file.
11608 * Returns FAIL if writing fails.
11609 */
11610 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011611ses_arglist(
11612 FILE *fd,
11613 char *cmd,
11614 garray_T *gap,
11615 int fullname, /* TRUE: use full path name */
11616 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011617{
11618 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011619 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011620 char_u *s;
11621
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011622 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11623 return FAIL;
11624 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625 return FAIL;
11626 for (i = 0; i < gap->ga_len; ++i)
11627 {
11628 /* NULL file names are skipped (only happens when out of memory). */
11629 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11630 if (s != NULL)
11631 {
11632 if (fullname)
11633 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011634 buf = alloc(MAXPATHL);
11635 if (buf != NULL)
11636 {
11637 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11638 s = buf;
11639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011640 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011641 if (fputs("argadd ", fd) < 0
11642 || ses_put_fname(fd, s, flagp) == FAIL
11643 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011644 {
11645 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011646 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011647 }
11648 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011649 }
11650 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011651 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011652}
11653
11654/*
11655 * Write a buffer name to the session file.
11656 * Also ends the line.
11657 * Returns FAIL if writing fails.
11658 */
11659 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011660ses_fname(FILE *fd, buf_T *buf, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011661{
11662 char_u *name;
11663
11664 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011665 * the session file will be sourced.
11666 * Don't do this for ":mkview", we don't know the current directory.
11667 * Don't do this after ":lcd", we don't keep track of what the current
11668 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011669 if (buf->b_sfname != NULL
11670 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011671 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011672#ifdef FEAT_AUTOCHDIR
11673 && !p_acd
11674#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011675 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011676 name = buf->b_sfname;
11677 else
11678 name = buf->b_ffname;
11679 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11680 return FAIL;
11681 return OK;
11682}
11683
11684/*
11685 * Write a file name to the session file.
11686 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11687 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011688 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011689 */
11690 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011691ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011692{
11693 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011694 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011695 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696
11697 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011698 if (sname == NULL)
11699 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011700
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011701 if (*flagp & SSOP_SLASH)
11702 {
11703 /* change all backslashes to forward slashes */
11704 for (p = sname; *p != NUL; mb_ptr_adv(p))
11705 if (*p == '\\')
11706 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011707 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011708
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011709 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011710 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011711 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011712 if (p == NULL)
11713 return FAIL;
11714
11715 /* write the result */
11716 if (fputs((char *)p, fd) < 0)
11717 retval = FAIL;
11718
11719 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011720 return retval;
11721}
11722
11723/*
11724 * ":loadview [nr]"
11725 */
11726 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011727ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011728{
11729 char_u *fname;
11730
11731 fname = get_view_file(*eap->arg);
11732 if (fname != NULL)
11733 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011734 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735 vim_free(fname);
11736 }
11737}
11738
11739/*
11740 * Get the name of the view file for the current buffer.
11741 */
11742 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011743get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011744{
11745 int len = 0;
11746 char_u *p, *s;
11747 char_u *retval;
11748 char_u *sname;
11749
11750 if (curbuf->b_ffname == NULL)
11751 {
11752 EMSG(_(e_noname));
11753 return NULL;
11754 }
11755 sname = home_replace_save(NULL, curbuf->b_ffname);
11756 if (sname == NULL)
11757 return NULL;
11758
11759 /*
11760 * We want a file name without separators, because we're not going to make
11761 * a directory.
11762 * "normal" path separator -> "=+"
11763 * "=" -> "=="
11764 * ":" path separator -> "=-"
11765 */
11766 for (p = sname; *p; ++p)
11767 if (*p == '=' || vim_ispathsep(*p))
11768 ++len;
11769 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11770 if (retval != NULL)
11771 {
11772 STRCPY(retval, p_vdir);
11773 add_pathsep(retval);
11774 s = retval + STRLEN(retval);
11775 for (p = sname; *p; ++p)
11776 {
11777 if (*p == '=')
11778 {
11779 *s++ = '=';
11780 *s++ = '=';
11781 }
11782 else if (vim_ispathsep(*p))
11783 {
11784 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011785#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011786 if (*p == ':')
11787 *s++ = '-';
11788 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011789#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011790 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011791 }
11792 else
11793 *s++ = *p;
11794 }
11795 *s++ = '=';
11796 *s++ = c;
11797 STRCPY(s, ".vim");
11798 }
11799
11800 vim_free(sname);
11801 return retval;
11802}
11803
11804#endif /* FEAT_SESSION */
11805
11806/*
11807 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11808 * Return FAIL for a write error.
11809 */
11810 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011811put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011812{
11813 if (
11814#ifdef USE_CRNL
11815 (
11816# ifdef MKSESSION_NL
11817 !mksession_nl &&
11818# endif
11819 (putc('\r', fd) < 0)) ||
11820#endif
11821 (putc('\n', fd) < 0))
11822 return FAIL;
11823 return OK;
11824}
11825
11826/*
11827 * Write a line to "fd".
11828 * Return FAIL for a write error.
11829 */
11830 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011831put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011832{
11833 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11834 return FAIL;
11835 return OK;
11836}
11837
11838#ifdef FEAT_VIMINFO
11839/*
11840 * ":rviminfo" and ":wviminfo".
11841 */
11842 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011843ex_viminfo(
11844 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011845{
11846 char_u *save_viminfo;
11847
11848 save_viminfo = p_viminfo;
11849 if (*p_viminfo == NUL)
11850 p_viminfo = (char_u *)"'100";
11851 if (eap->cmdidx == CMD_rviminfo)
11852 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011853 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11854 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855 EMSG(_("E195: Cannot open viminfo file for reading"));
11856 }
11857 else
11858 write_viminfo(eap->arg, eap->forceit);
11859 p_viminfo = save_viminfo;
11860}
11861#endif
11862
11863#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011864/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011865 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011866 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011867 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011869dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011870{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011871 if (fname == NULL)
11872 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011873 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011874}
11875#endif
11876
11877/*
11878 * ":behave {mswin,xterm}"
11879 */
11880 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011881ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011882{
11883 if (STRCMP(eap->arg, "mswin") == 0)
11884 {
11885 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11886 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11887 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11888 set_option_value((char_u *)"keymodel", 0L,
11889 (char_u *)"startsel,stopsel", 0);
11890 }
11891 else if (STRCMP(eap->arg, "xterm") == 0)
11892 {
11893 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11894 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11895 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11896 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11897 }
11898 else
11899 EMSG2(_(e_invarg2), eap->arg);
11900}
11901
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011902#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11903/*
11904 * Function given to ExpandGeneric() to obtain the possible arguments of the
11905 * ":behave {mswin,xterm}" command.
11906 */
11907 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011908get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011909{
11910 if (idx == 0)
11911 return (char_u *)"mswin";
11912 if (idx == 1)
11913 return (char_u *)"xterm";
11914 return NULL;
11915}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020011916
11917/*
11918 * Function given to ExpandGeneric() to obtain the possible arguments of the
11919 * ":messages {clear}" command.
11920 */
11921 char_u *
11922get_messages_arg(expand_T *xp UNUSED, int idx)
11923{
11924 if (idx == 0)
11925 return (char_u *)"clear";
11926 return NULL;
11927}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011928#endif
11929
Bram Moolenaar071d4272004-06-13 20:20:40 +000011930#ifdef FEAT_AUTOCMD
11931static int filetype_detect = FALSE;
11932static int filetype_plugin = FALSE;
11933static int filetype_indent = FALSE;
11934
11935/*
11936 * ":filetype [plugin] [indent] {on,off,detect}"
11937 * on: Load the filetype.vim file to install autocommands for file types.
11938 * off: Load the ftoff.vim file to remove all autocommands for file types.
11939 * plugin on: load filetype.vim and ftplugin.vim
11940 * plugin off: load ftplugof.vim
11941 * indent on: load filetype.vim and indent.vim
11942 * indent off: load indoff.vim
11943 */
11944 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011945ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011946{
11947 char_u *arg = eap->arg;
11948 int plugin = FALSE;
11949 int indent = FALSE;
11950
11951 if (*eap->arg == NUL)
11952 {
11953 /* Print current status. */
11954 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11955 filetype_detect ? "ON" : "OFF",
11956 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11957 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11958 return;
11959 }
11960
11961 /* Accept "plugin" and "indent" in any order. */
11962 for (;;)
11963 {
11964 if (STRNCMP(arg, "plugin", 6) == 0)
11965 {
11966 plugin = TRUE;
11967 arg = skipwhite(arg + 6);
11968 continue;
11969 }
11970 if (STRNCMP(arg, "indent", 6) == 0)
11971 {
11972 indent = TRUE;
11973 arg = skipwhite(arg + 6);
11974 continue;
11975 }
11976 break;
11977 }
11978 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11979 {
11980 if (*arg == 'o' || !filetype_detect)
11981 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011982 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011983 filetype_detect = TRUE;
11984 if (plugin)
11985 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011986 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011987 filetype_plugin = TRUE;
11988 }
11989 if (indent)
11990 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011991 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992 filetype_indent = TRUE;
11993 }
11994 }
11995 if (*arg == 'd')
11996 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020011997 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011998 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999 }
12000 }
12001 else if (STRCMP(arg, "off") == 0)
12002 {
12003 if (plugin || indent)
12004 {
12005 if (plugin)
12006 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012007 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008 filetype_plugin = FALSE;
12009 }
12010 if (indent)
12011 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012012 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012013 filetype_indent = FALSE;
12014 }
12015 }
12016 else
12017 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012018 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019 filetype_detect = FALSE;
12020 }
12021 }
12022 else
12023 EMSG2(_(e_invarg2), arg);
12024}
12025
12026/*
12027 * ":setfiletype {name}"
12028 */
12029 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012030ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012031{
12032 if (!did_filetype)
12033 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
12034}
12035#endif
12036
Bram Moolenaar071d4272004-06-13 20:20:40 +000012037 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012038ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012039{
12040#ifdef FEAT_DIGRAPHS
12041 if (*eap->arg != NUL)
12042 putdigraph(eap->arg);
12043 else
12044 listdigraphs();
12045#else
12046 EMSG(_("E196: No digraphs in this version"));
12047#endif
12048}
12049
12050 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012051ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012052{
12053 int flags = 0;
12054
12055 if (eap->cmdidx == CMD_setlocal)
12056 flags = OPT_LOCAL;
12057 else if (eap->cmdidx == CMD_setglobal)
12058 flags = OPT_GLOBAL;
12059#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
12060 if (cmdmod.browse && flags == 0)
12061 ex_options(eap);
12062 else
12063#endif
12064 (void)do_set(eap->arg, flags);
12065}
12066
12067#ifdef FEAT_SEARCH_EXTRA
12068/*
12069 * ":nohlsearch"
12070 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012072ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010012074 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012075 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012076}
12077
12078/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012079 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080 * Sets nextcmd to the start of the next command, if any. Also called when
12081 * skipping commands to find the next command.
12082 */
12083 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012084ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012085{
12086 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012087 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088 char_u *end;
12089 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012090 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012091
12092 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012093 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012094 else
12095 {
12096 EMSG(e_invcmd);
12097 return;
12098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012099
12100 /* First clear any old pattern. */
12101 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012102 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012103
12104 if (ends_excmd(*eap->arg))
12105 end = eap->arg;
12106 else if ((STRNICMP(eap->arg, "none", 4) == 0
12107 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
12108 end = eap->arg + 4;
12109 else
12110 {
12111 p = skiptowhite(eap->arg);
12112 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012113 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012114 p = skipwhite(p);
12115 if (*p == NUL)
12116 {
12117 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012118 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119 EMSG2(_(e_invarg2), eap->arg);
12120 return;
12121 }
12122 end = skip_regexp(p + 1, *p, TRUE, NULL);
12123 if (!eap->skip)
12124 {
12125 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12126 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012127 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012128 eap->errmsg = e_trailing;
12129 return;
12130 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012131 if (*end != *p)
12132 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012133 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012134 EMSG2(_(e_invarg2), p);
12135 return;
12136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012137
12138 c = *end;
12139 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012140 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012141 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012142 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012143 }
12144 }
12145 eap->nextcmd = find_nextcmd(end);
12146}
12147#endif
12148
12149#ifdef FEAT_CRYPT
12150/*
12151 * ":X": Get crypt key
12152 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012153 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012154ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012155{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012156 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012157 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158}
12159#endif
12160
12161#ifdef FEAT_FOLDING
12162 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012163ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012164{
12165 if (foldManualAllowed(TRUE))
12166 foldCreate(eap->line1, eap->line2);
12167}
12168
12169 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012170ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012171{
12172 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12173 eap->forceit, FALSE);
12174}
12175
12176 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012177ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012178{
12179 linenr_T lnum;
12180
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012181#ifdef FEAT_CLIPBOARD
12182 start_global_changes();
12183#endif
12184
Bram Moolenaar071d4272004-06-13 20:20:40 +000012185 /* First set the marks for all lines closed/open. */
12186 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12187 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12188 ml_setmarked(lnum);
12189
12190 /* Execute the command on the marked lines. */
12191 global_exe(eap->arg);
12192 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012193#ifdef FEAT_CLIPBOARD
12194 end_global_changes();
12195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012196}
12197#endif