blob: 94216b371e0152e20b6b70363a9b2f777065a921 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
22#ifdef FEAT_USR_CMDS
23typedef struct ucmd
24{
25 char_u *uc_name; /* The command name */
26 long_u uc_argt; /* The argument type */
27 char_u *uc_rep; /* The command's replacement string */
28 long uc_def; /* The default value for a range/count */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int uc_compl; /* completion type */
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +010030 int uc_addr_type; /* The command's address type */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010031# ifdef FEAT_EVAL
32 scid_T uc_scriptID; /* SID where the command was defined */
33# ifdef FEAT_CMDL_COMPL
Bram Moolenaar071d4272004-06-13 20:20:40 +000034 char_u *uc_compl_arg; /* completion argument if any */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010035# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000036# endif
37} ucmd_T;
38
39#define UC_BUFFER 1 /* -buffer: local to current buffer */
40
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000041static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
43#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
44#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
45
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010046static void do_ucmd(exarg_T *eap);
47static void ex_command(exarg_T *eap);
48static void ex_delcommand(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# ifdef FEAT_CMDL_COMPL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010050static char_u *get_user_command_name(int idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051# endif
52
Bram Moolenaar958636c2014-10-21 20:01:58 +020053/* Wether a command index indicates a user command. */
54# define IS_USER_CMDIDX(idx) ((int)(idx) < 0)
55
Bram Moolenaar071d4272004-06-13 20:20:40 +000056#else
57# define ex_command ex_ni
58# define ex_comclear ex_ni
59# define ex_delcommand ex_ni
Bram Moolenaar958636c2014-10-21 20:01:58 +020060/* Wether a command index indicates a user command. */
61# define IS_USER_CMDIDX(idx) (FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000062#endif
63
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010064static int compute_buffer_local_count(int addr_type, int lnum, int local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000065#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010066static char_u *do_one_cmd(char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000067#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010068static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000069static int if_level = 0; /* depth in :if */
70#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010071static void append_command(char_u *cmd);
72static char_u *find_command(exarg_T *eap, int *full);
Bram Moolenaar071d4272004-06-13 20:20:40 +000073
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010074static void ex_abbreviate(exarg_T *eap);
75static void ex_map(exarg_T *eap);
76static void ex_unmap(exarg_T *eap);
77static void ex_mapclear(exarg_T *eap);
78static void ex_abclear(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#ifndef FEAT_MENU
80# define ex_emenu ex_ni
81# define ex_menu ex_ni
82# define ex_menutranslate ex_ni
83#endif
84#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static void ex_autocmd(exarg_T *eap);
86static void ex_doautocmd(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#else
88# define ex_autocmd ex_ni
89# define ex_doautocmd ex_ni
90# define ex_doautoall ex_ni
91#endif
92#ifdef FEAT_LISTCMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010093static void ex_bunload(exarg_T *eap);
94static void ex_buffer(exarg_T *eap);
95static void ex_bmodified(exarg_T *eap);
96static void ex_bnext(exarg_T *eap);
97static void ex_bprevious(exarg_T *eap);
98static void ex_brewind(exarg_T *eap);
99static void ex_blast(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#else
101# define ex_bunload ex_ni
102# define ex_buffer ex_ni
103# define ex_bmodified ex_ni
104# define ex_bnext ex_ni
105# define ex_bprevious ex_ni
106# define ex_brewind ex_ni
107# define ex_blast ex_ni
108# define buflist_list ex_ni
109# define ex_checktime ex_ni
110#endif
111#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
112# define ex_buffer_all ex_ni
113#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100114static char_u *getargcmd(char_u **);
115static char_u *skip_cmd_arg(char_u *p, int rembs);
116static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#ifndef FEAT_QUICKFIX
118# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000119# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120# define ex_cc ex_ni
121# define ex_cnext ex_ni
122# define ex_cfile ex_ni
123# define qf_list ex_ni
124# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200125# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000127# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#endif
129#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
130# define ex_cclose ex_ni
131# define ex_copen ex_ni
132# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +0200133# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000135#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
136# define ex_cexpr ex_ni
137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100139static int check_more(int, int);
140static linenr_T get_address(exarg_T *, char_u **, int addr_type, int skip, int to_other_file);
141static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200142#if !defined(FEAT_PERL) \
143 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
144 || !defined(FEAT_TCL) \
145 || !defined(FEAT_RUBY) \
146 || !defined(FEAT_LUA) \
147 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000148# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100149static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100151static char_u *invalid_range(exarg_T *eap);
152static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000153#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100154static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000155#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100156static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
157static void ex_highlight(exarg_T *eap);
158static void ex_colorscheme(exarg_T *eap);
159static void ex_quit(exarg_T *eap);
160static void ex_cquit(exarg_T *eap);
161static void ex_quit_all(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100163static void ex_close(exarg_T *eap);
164static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
165static void ex_only(exarg_T *eap);
166static void ex_resize(exarg_T *eap);
167static void ex_stag(exarg_T *eap);
168static void ex_tabclose(exarg_T *eap);
169static void ex_tabonly(exarg_T *eap);
170static void ex_tabnext(exarg_T *eap);
171static void ex_tabmove(exarg_T *eap);
172static void ex_tabs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173#else
174# define ex_close ex_ni
175# define ex_only ex_ni
176# define ex_all ex_ni
177# define ex_resize ex_ni
178# define ex_splitview ex_ni
179# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000180# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000181# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000182# define ex_tabs ex_ni
183# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000184# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185#endif
186#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100187static void ex_pclose(exarg_T *eap);
188static void ex_ptag(exarg_T *eap);
189static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190#else
191# define ex_pclose ex_ni
192# define ex_ptag ex_ni
193# define ex_pedit ex_ni
194#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100195static void ex_hide(exarg_T *eap);
196static void ex_stop(exarg_T *eap);
197static void ex_exit(exarg_T *eap);
198static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100200static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#else
202# define ex_goto ex_ni
203#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100204static void ex_shell(exarg_T *eap);
205static void ex_preserve(exarg_T *eap);
206static void ex_recover(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207#ifndef FEAT_LISTCMDS
208# define ex_argedit ex_ni
209# define ex_argadd ex_ni
210# define ex_argdelete ex_ni
211# define ex_listdo ex_ni
212#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100213static void ex_mode(exarg_T *eap);
214static void ex_wrongmodifier(exarg_T *eap);
215static void ex_find(exarg_T *eap);
216static void ex_open(exarg_T *eap);
217static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
219# define ex_drop ex_ni
220#endif
221#ifndef FEAT_GUI
222# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#endif
225#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100226static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227#else
228# define ex_tearoff ex_ni
229#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000230#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100231static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232#else
233# define ex_popup ex_ni
234#endif
235#ifndef FEAT_GUI_MSWIN
236# define ex_simalt ex_ni
237#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000238#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239# define gui_mch_find_dialog ex_ni
240# define gui_mch_replace_dialog ex_ni
241#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000242#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243# define ex_helpfind ex_ni
244#endif
245#ifndef FEAT_CSCOPE
246# define do_cscope ex_ni
247# define do_scscope ex_ni
248# define do_cstag ex_ni
249#endif
250#ifndef FEAT_SYN_HL
251# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200252# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000253#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200254#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200255# define ex_syntime ex_ni
256#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000257#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000258# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000259# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000260# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000261# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000262# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000263#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200264#ifndef FEAT_PERSISTENT_UNDO
265# define ex_rundo ex_ni
266# define ex_wundo ex_ni
267#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200268#ifndef FEAT_LUA
269# define ex_lua ex_script_ni
270# define ex_luado ex_ni
271# define ex_luafile ex_ni
272#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000273#ifndef FEAT_MZSCHEME
274# define ex_mzscheme ex_script_ni
275# define ex_mzfile ex_ni
276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#ifndef FEAT_PERL
278# define ex_perl ex_script_ni
279# define ex_perldo ex_ni
280#endif
281#ifndef FEAT_PYTHON
282# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200283# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284# define ex_pyfile ex_ni
285#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200286#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200287# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200288# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200289# define ex_py3file ex_ni
290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291#ifndef FEAT_TCL
292# define ex_tcl ex_script_ni
293# define ex_tcldo ex_ni
294# define ex_tclfile ex_ni
295#endif
296#ifndef FEAT_RUBY
297# define ex_ruby ex_script_ni
298# define ex_rubydo ex_ni
299# define ex_rubyfile ex_ni
300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301#ifndef FEAT_KEYMAP
302# define ex_loadkeymap ex_ni
303#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100304static void ex_swapname(exarg_T *eap);
305static void ex_syncbind(exarg_T *eap);
306static void ex_read(exarg_T *eap);
307static void ex_pwd(exarg_T *eap);
308static void ex_equal(exarg_T *eap);
309static void ex_sleep(exarg_T *eap);
310static void do_exmap(exarg_T *eap, int isabbrev);
311static void ex_winsize(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100313static void ex_wincmd(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314#else
315# define ex_wincmd ex_ni
316#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000317#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319#else
320# define ex_winpos ex_ni
321#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100322static void ex_operators(exarg_T *eap);
323static void ex_put(exarg_T *eap);
324static void ex_copymove(exarg_T *eap);
325static void ex_submagic(exarg_T *eap);
326static void ex_join(exarg_T *eap);
327static void ex_at(exarg_T *eap);
328static void ex_bang(exarg_T *eap);
329static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200330#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100331static void ex_wundo(exarg_T *eap);
332static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200333#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100334static void ex_redo(exarg_T *eap);
335static void ex_later(exarg_T *eap);
336static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100337static void ex_redrawstatus(exarg_T *eap);
338static void close_redir(void);
339static void ex_mkrc(exarg_T *eap);
340static void ex_mark(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341#ifdef FEAT_USR_CMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100342static char_u *uc_fun_cmd(void);
343static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100345static void ex_startinsert(exarg_T *eap);
346static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100348static void ex_checkpath(exarg_T *eap);
349static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350#else
351# define ex_findpat ex_ni
352# define ex_checkpath ex_ni
353#endif
354#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100355static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356#else
357# define ex_psearch ex_ni
358#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100359static void ex_tag(exarg_T *eap);
360static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361#ifndef FEAT_EVAL
362# define ex_scriptnames ex_ni
363# define ex_finish ex_ni
364# define ex_echo ex_ni
365# define ex_echohl ex_ni
366# define ex_execute ex_ni
367# define ex_call ex_ni
368# define ex_if ex_ni
369# define ex_endif ex_ni
370# define ex_else ex_ni
371# define ex_while ex_ni
372# define ex_continue ex_ni
373# define ex_break ex_ni
374# define ex_endwhile ex_ni
375# define ex_throw ex_ni
376# define ex_try ex_ni
377# define ex_catch ex_ni
378# define ex_finally ex_ni
379# define ex_endtry ex_ni
380# define ex_endfunction ex_ni
381# define ex_let ex_ni
382# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000383# define ex_lockvar ex_ni
384# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385# define ex_function ex_ni
386# define ex_delfunction ex_ni
387# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000388# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100390static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100392static int makeopens(FILE *fd, char_u *dirnow);
393static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
394static void ex_loadview(exarg_T *eap);
395static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000396static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397#else
398# define ex_loadview ex_ni
399#endif
400#ifndef FEAT_EVAL
401# define ex_compiler ex_ni
402#endif
403#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100404static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405#else
406# define ex_viminfo ex_ni
407#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100408static void ex_behave(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100410static void ex_filetype(exarg_T *eap);
411static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412#else
413# define ex_filetype ex_ni
414# define ex_setfiletype ex_ni
415#endif
416#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000417# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418# define ex_diffpatch ex_ni
419# define ex_diffgetput ex_ni
420# define ex_diffsplit ex_ni
421# define ex_diffthis ex_ni
422# define ex_diffupdate ex_ni
423#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100424static void ex_digraphs(exarg_T *eap);
425static void ex_set(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
427# define ex_options ex_ni
428#endif
429#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100430static void ex_nohlsearch(exarg_T *eap);
431static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432#else
433# define ex_nohlsearch ex_ni
434# define ex_match ex_ni
435#endif
436#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100437static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438#else
439# define ex_X ex_ni
440#endif
441#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100442static void ex_fold(exarg_T *eap);
443static void ex_foldopen(exarg_T *eap);
444static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445#else
446# define ex_fold ex_ni
447# define ex_foldopen ex_ni
448# define ex_folddo ex_ni
449#endif
450#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
451 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
452# define ex_language ex_ni
453#endif
454#ifndef FEAT_SIGNS
455# define ex_sign ex_ni
456#endif
457#ifndef FEAT_SUN_WORKSHOP
458# define ex_wsverb ex_ni
459#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000460#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200461# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000462# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200463# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465
466#ifndef FEAT_EVAL
467# define ex_debug ex_ni
468# define ex_breakadd ex_ni
469# define ex_debuggreedy ex_ni
470# define ex_breakdel ex_ni
471# define ex_breaklist ex_ni
472#endif
473
474#ifndef FEAT_CMDHIST
475# define ex_history ex_ni
476#endif
477#ifndef FEAT_JUMPLIST
478# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200479# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480# define ex_changes ex_ni
481#endif
482
Bram Moolenaar05159a02005-02-26 23:04:13 +0000483#ifndef FEAT_PROFILE
484# define ex_profile ex_ni
485#endif
486
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487/*
488 * Declare cmdnames[].
489 */
490#define DO_DECLARE_EXCMD
491#include "ex_cmds.h"
492
493/*
494 * Table used to quickly search for a command, based on its first character.
495 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000496static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497{
498 CMD_append,
499 CMD_buffer,
500 CMD_change,
501 CMD_delete,
502 CMD_edit,
503 CMD_file,
504 CMD_global,
505 CMD_help,
506 CMD_insert,
507 CMD_join,
508 CMD_k,
509 CMD_list,
510 CMD_move,
511 CMD_next,
512 CMD_open,
513 CMD_print,
514 CMD_quit,
515 CMD_read,
516 CMD_substitute,
517 CMD_t,
518 CMD_undo,
519 CMD_vglobal,
520 CMD_write,
521 CMD_xit,
522 CMD_yank,
523 CMD_z,
524 CMD_bang
525};
526
527static char_u dollar_command[2] = {'$', 0};
528
529
530#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000531/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532typedef struct
533{
534 char_u *line; /* command line */
535 linenr_T lnum; /* sourcing_lnum of the line */
536} wcmd_T;
537
538/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000539 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000541 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000543struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544{
545 garray_T *lines_gap; /* growarray with line info */
546 int current_line; /* last read line from growarray */
547 int repeating; /* TRUE when looping a second time */
548 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100549 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 void *cookie;
551};
552
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100553static char_u *get_loop_line(int c, void *cookie, int indent);
554static int store_loop_line(garray_T *gap, char_u *line);
555static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000556
557/* Struct to save a few things while debugging. Used in do_cmdline() only. */
558struct dbg_stuff
559{
560 int trylevel;
561 int force_abort;
562 except_T *caught_stack;
563 char_u *vv_exception;
564 char_u *vv_throwpoint;
565 int did_emsg;
566 int got_int;
567 int did_throw;
568 int need_rethrow;
569 int check_cstack;
570 except_T *current_exception;
571};
572
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100573static void save_dbg_stuff(struct dbg_stuff *dsp);
574static void restore_dbg_stuff(struct dbg_stuff *dsp);
Bram Moolenaared203462004-06-16 11:19:22 +0000575
576 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100577save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000578{
579 dsp->trylevel = trylevel; trylevel = 0;
580 dsp->force_abort = force_abort; force_abort = FALSE;
581 dsp->caught_stack = caught_stack; caught_stack = NULL;
582 dsp->vv_exception = v_exception(NULL);
583 dsp->vv_throwpoint = v_throwpoint(NULL);
584
585 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
586 dsp->did_emsg = did_emsg; did_emsg = FALSE;
587 dsp->got_int = got_int; got_int = FALSE;
588 dsp->did_throw = did_throw; did_throw = FALSE;
589 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
590 dsp->check_cstack = check_cstack; check_cstack = FALSE;
591 dsp->current_exception = current_exception; current_exception = NULL;
592}
593
594 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100595restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000596{
597 suppress_errthrow = FALSE;
598 trylevel = dsp->trylevel;
599 force_abort = dsp->force_abort;
600 caught_stack = dsp->caught_stack;
601 (void)v_exception(dsp->vv_exception);
602 (void)v_throwpoint(dsp->vv_throwpoint);
603 did_emsg = dsp->did_emsg;
604 got_int = dsp->got_int;
605 did_throw = dsp->did_throw;
606 need_rethrow = dsp->need_rethrow;
607 check_cstack = dsp->check_cstack;
608 current_exception = dsp->current_exception;
609}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610#endif
611
612
613/*
614 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
615 * command is given.
616 */
617 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100618do_exmode(
619 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620{
621 int save_msg_scroll;
622 int prev_msg_row;
623 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000624 int changedtick;
625
626 if (improved)
627 exmode_active = EXMODE_VIM;
628 else
629 exmode_active = EXMODE_NORMAL;
630 State = NORMAL;
631
632 /* When using ":global /pat/ visual" and then "Q" we return to continue
633 * the :global command. */
634 if (global_busy)
635 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636
637 save_msg_scroll = msg_scroll;
638 ++RedrawingDisabled; /* don't redisplay the window */
639 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640#ifdef FEAT_GUI
641 /* Ignore scrollbar and mouse events in Ex mode */
642 ++hold_gui_events;
643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644
645 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
646 while (exmode_active)
647 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000648 /* Check for a ":normal" command and no more characters left. */
649 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
650 {
651 exmode_active = FALSE;
652 break;
653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 msg_scroll = TRUE;
655 need_wait_return = FALSE;
656 ex_pressedreturn = FALSE;
657 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000658 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 prev_msg_row = msg_row;
660 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 if (improved)
662 {
663 cmdline_row = msg_row;
664 do_cmdline(NULL, getexline, NULL, 0);
665 }
666 else
667 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
668 lines_left = Rows - 1;
669
Bram Moolenaardf177f62005-02-22 08:39:57 +0000670 if ((prev_line != curwin->w_cursor.lnum
671 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000673 if (curbuf->b_ml.ml_flags & ML_EMPTY)
674 EMSG(_(e_emptybuf));
675 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000677 if (ex_pressedreturn)
678 {
679 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000680 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000681 msg_row = prev_msg_row;
682 if (prev_msg_row == Rows - 1)
683 msg_row--;
684 }
685 msg_col = 0;
686 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
687 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000690 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
691 {
692 if (curbuf->b_ml.ml_flags & ML_EMPTY)
693 EMSG(_(e_emptybuf));
694 else
695 EMSG(_("E501: At end-of-file"));
696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 }
698
699#ifdef FEAT_GUI
700 --hold_gui_events;
701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 --RedrawingDisabled;
703 --no_wait_return;
704 update_screen(CLEAR);
705 need_wait_return = FALSE;
706 msg_scroll = save_msg_scroll;
707}
708
709/*
710 * Execute a simple command line. Used for translated commands like "*".
711 */
712 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100713do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714{
715 return do_cmdline(cmd, NULL, NULL,
716 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
717}
718
719/*
720 * do_cmdline(): execute one Ex command line
721 *
722 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100723 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 * 2. Split up in parts separated with '|'.
725 *
726 * This function can be called recursively!
727 *
728 * flags:
729 * DOCMD_VERBOSE - The command will be included in the error message.
730 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100731 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 * DOCMD_KEYTYPED - Don't reset KeyTyped.
733 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
734 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
735 *
736 * return FAIL if cmdline could not be executed, OK otherwise
737 */
738 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100739do_cmdline(
740 char_u *cmdline,
741 char_u *(*fgetline)(int, void *, int),
742 void *cookie, /* argument for fgetline() */
743 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744{
745 char_u *next_cmdline; /* next cmd to execute */
746 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100747 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 static int recursive = 0; /* recursive depth */
749 int msg_didout_before_start = 0;
750 int count = 0; /* line number count */
751 int did_inc = FALSE; /* incremented RedrawingDisabled */
752 int retval = OK;
753#ifdef FEAT_EVAL
754 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000755 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 int current_line = 0; /* active line in lines_ga */
757 char_u *fname = NULL; /* function or script name */
758 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
759 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000760 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 int initial_trylevel;
762 struct msglist **saved_msg_list = NULL;
763 struct msglist *private_msg_list;
764
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100765 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100766 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000768 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000770 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100772# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773# define cmd_cookie cookie
774#endif
775 static int call_depth = 0; /* recursiveness */
776
777#ifdef FEAT_EVAL
778 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
779 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000780 * This ensures that the do_errthrow() call in do_one_cmd() does not
781 * combine the messages stored by an earlier invocation of do_one_cmd()
782 * with the command name of the later one. This would happen when
783 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 saved_msg_list = msg_list;
785 msg_list = &private_msg_list;
786 private_msg_list = NULL;
787#endif
788
789 /* It's possible to create an endless loop with ":execute", catch that
790 * here. The value of 200 allows nested function calls, ":source", etc. */
791 if (call_depth == 200)
792 {
793 EMSG(_("E169: Command too recursive"));
794#ifdef FEAT_EVAL
795 /* When converting to an exception, we do not include the command name
796 * since this is not an error of the specific command. */
797 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
798 msg_list = saved_msg_list;
799#endif
800 return FAIL;
801 }
802 ++call_depth;
803
804#ifdef FEAT_EVAL
805 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000806 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 cstack.cs_trylevel = 0;
808 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000809 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
811
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100812 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
814 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100815 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000816 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 ++ex_nesting_level;
818
819 /* Get the function or script name and the address where the next breakpoint
820 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000821 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 {
823 fname = func_name(real_cookie);
824 breakpoint = func_breakpoint(real_cookie);
825 dbg_tick = func_dbg_tick(real_cookie);
826 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100827 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 {
829 fname = sourcing_name;
830 breakpoint = source_breakpoint(real_cookie);
831 dbg_tick = source_dbg_tick(real_cookie);
832 }
833
834 /*
835 * Initialize "force_abort" and "suppress_errthrow" at the top level.
836 */
837 if (!recursive)
838 {
839 force_abort = FALSE;
840 suppress_errthrow = FALSE;
841 }
842
843 /*
844 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000845 * exception handling (used when debugging). Otherwise clear it to avoid
846 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000848 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000849 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000850 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200851 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852
853 initial_trylevel = trylevel;
854
855 /*
856 * "did_throw" will be set to TRUE when an exception is being thrown.
857 */
858 did_throw = FALSE;
859#endif
860 /*
861 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000862 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 * If force_abort is set, we cancel everything.
864 */
865 did_emsg = FALSE;
866
867 /*
868 * KeyTyped is only set when calling vgetc(). Reset it here when not
869 * calling vgetc() (sourced command lines).
870 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100871 if (!(flags & DOCMD_KEYTYPED)
872 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 KeyTyped = FALSE;
874
875 /*
876 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000877 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 * - for multiple commands on one line, separated with '|'
879 * - when repeating until there are no more lines (for ":source")
880 */
881 next_cmdline = cmdline;
882 do
883 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000884#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100885 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000886#endif
887
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000888 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (next_cmdline == NULL
890#ifdef FEAT_EVAL
891 && !force_abort
892 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000893 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894#endif
895 )
896 did_emsg = FALSE;
897
898 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000899 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100900 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 * 3. If a line is given: Make a copy, so we can mess with it.
902 */
903
904#ifdef FEAT_EVAL
905 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000906 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 {
908 /* Each '|' separated command is stored separately in lines_ga, to
909 * be able to jump to it. Don't use next_cmdline now. */
910 vim_free(cmdline_copy);
911 cmdline_copy = NULL;
912
913 /* Check if a function has returned or, unless it has an unclosed
914 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000915 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000917# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000918 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000919 func_line_end(real_cookie);
920# endif
921 if (func_has_ended(real_cookie))
922 {
923 retval = FAIL;
924 break;
925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000927#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000928 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100929 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000930 script_line_end();
931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932
933 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100934 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 {
936 retval = FAIL;
937 break;
938 }
939
940 /* If breakpoints have been added/deleted need to check for it. */
941 if (breakpoint != NULL && dbg_tick != NULL
942 && *dbg_tick != debug_tick)
943 {
944 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100945 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 fname, sourcing_lnum);
947 *dbg_tick = debug_tick;
948 }
949
950 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
951 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
952
953 /* Did we encounter a breakpoint? */
954 if (breakpoint != NULL && *breakpoint != 0
955 && *breakpoint <= sourcing_lnum)
956 {
957 dbg_breakpoint(fname, sourcing_lnum);
958 /* Find next breakpoint. */
959 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100960 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 fname, sourcing_lnum);
962 *dbg_tick = debug_tick;
963 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000964# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000965 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000966 {
967 if (getline_is_func)
968 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100969 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000970 script_line_start();
971 }
972# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 }
974
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000975 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000977 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100978 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 * below, so that it stores lines in or reads them from
980 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000981 * while/for loop. */
982 cmd_getline = get_loop_line;
983 cmd_cookie = (void *)&cmd_loop_cookie;
984 cmd_loop_cookie.lines_gap = &lines_ga;
985 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100986 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000987 cmd_loop_cookie.cookie = cookie;
988 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 }
990 else
991 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100992 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 cmd_cookie = cookie;
994 }
995#endif
996
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100997 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 if (next_cmdline == NULL)
999 {
1000 /*
1001 * Need to set msg_didout for the first line after an ":if",
1002 * otherwise the ":if" will be overwritten.
1003 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001004 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001006 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007#ifdef FEAT_EVAL
1008 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1009#else
1010 0
1011#endif
1012 )) == NULL)
1013 {
1014 /* Don't call wait_return for aborted command line. The NULL
1015 * returned for the end of a sourced file or executed function
1016 * doesn't do this. */
1017 if (KeyTyped && !(flags & DOCMD_REPEAT))
1018 need_wait_return = FALSE;
1019 retval = FAIL;
1020 break;
1021 }
1022 used_getline = TRUE;
1023
1024 /*
1025 * Keep the first typed line. Clear it when more lines are typed.
1026 */
1027 if (flags & DOCMD_KEEPLINE)
1028 {
1029 vim_free(repeat_cmdline);
1030 if (count == 0)
1031 repeat_cmdline = vim_strsave(next_cmdline);
1032 else
1033 repeat_cmdline = NULL;
1034 }
1035 }
1036
1037 /* 3. Make a copy of the command so we can mess with it. */
1038 else if (cmdline_copy == NULL)
1039 {
1040 next_cmdline = vim_strsave(next_cmdline);
1041 if (next_cmdline == NULL)
1042 {
1043 EMSG(_(e_outofmem));
1044 retval = FAIL;
1045 break;
1046 }
1047 }
1048 cmdline_copy = next_cmdline;
1049
1050#ifdef FEAT_EVAL
1051 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001052 * Save the current line when inside a ":while" or ":for", and when
1053 * the command looks like a ":while" or ":for", because we may need it
1054 * later. When there is a '|' and another command, it is stored
1055 * separately, because we need to be able to jump back to it from an
1056 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001058 if (current_line == lines_ga.ga_len
1059 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001061 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 {
1063 retval = FAIL;
1064 break;
1065 }
1066 }
1067 did_endif = FALSE;
1068#endif
1069
1070 if (count++ == 0)
1071 {
1072 /*
1073 * All output from the commands is put below each other, without
1074 * waiting for a return. Don't do this when executing commands
1075 * from a script or when being called recursive (e.g. for ":e
1076 * +command file").
1077 */
1078 if (!(flags & DOCMD_NOWAIT) && !recursive)
1079 {
1080 msg_didout_before_start = msg_didout;
1081 msg_didany = FALSE; /* no output yet */
1082 msg_start();
1083 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001084 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 ++RedrawingDisabled;
1086 did_inc = TRUE;
1087 }
1088 }
1089
1090 if (p_verbose >= 15 && sourcing_name != NULL)
1091 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001093 verbose_enter_scroll();
1094
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 smsg((char_u *)_("line %ld: %s"),
1096 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001097 if (msg_silent == 0)
1098 msg_puts((char_u *)"\n"); /* don't overwrite this */
1099
1100 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 --no_wait_return;
1102 }
1103
1104 /*
1105 * 2. Execute one '|' separated command.
1106 * do_one_cmd() will return NULL if there is no trailing '|'.
1107 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1108 */
1109 ++recursive;
1110 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1111#ifdef FEAT_EVAL
1112 &cstack,
1113#endif
1114 cmd_getline, cmd_cookie);
1115 --recursive;
1116
1117#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001118 if (cmd_cookie == (void *)&cmd_loop_cookie)
1119 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001121 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122#endif
1123
1124 if (next_cmdline == NULL)
1125 {
1126 vim_free(cmdline_copy);
1127 cmdline_copy = NULL;
1128#ifdef FEAT_CMDHIST
1129 /*
1130 * If the command was typed, remember it for the ':' register.
1131 * Do this AFTER executing the command to make :@: work.
1132 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001133 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 && new_last_cmdline != NULL)
1135 {
1136 vim_free(last_cmdline);
1137 last_cmdline = new_last_cmdline;
1138 new_last_cmdline = NULL;
1139 }
1140#endif
1141 }
1142 else
1143 {
1144 /* need to copy the command after the '|' to cmdline_copy, for the
1145 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001146 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 next_cmdline = cmdline_copy;
1148 }
1149
1150
1151#ifdef FEAT_EVAL
1152 /* reset did_emsg for a function that is not aborted by an error */
1153 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001154 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 && !func_has_abort(real_cookie))
1156 did_emsg = FALSE;
1157
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001158 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 {
1160 ++current_line;
1161
1162 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001163 * An ":endwhile", ":endfor" and ":continue" is handled here.
1164 * If we were executing commands, jump back to the ":while" or
1165 * ":for".
1166 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001168 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001170 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001172 /* Jump back to the matching ":while" or ":for". Be careful
1173 * not to use a cs_line[] from an entry that isn't a ":while"
1174 * or ":for": It would make "current_line" invalid and can
1175 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 if (!did_emsg && !got_int && !did_throw
1177 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001178 && (cstack.cs_flags[cstack.cs_idx]
1179 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 && cstack.cs_line[cstack.cs_idx] >= 0
1181 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1182 {
1183 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001184 /* remember we jumped there */
1185 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 line_breakcheck(); /* check if CTRL-C typed */
1187
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001188 /* Check for the next breakpoint at or after the ":while"
1189 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 if (breakpoint != NULL)
1191 {
1192 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001193 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 fname,
1195 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1196 *dbg_tick = debug_tick;
1197 }
1198 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001199 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001201 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001203 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1204 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 }
1206 }
1207
1208 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001209 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001211 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001213 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1215 }
1216 }
1217
1218 /*
1219 * When not inside any ":while" loop, clear remembered lines.
1220 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001221 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 {
1223 if (lines_ga.ga_len > 0)
1224 {
1225 sourcing_lnum =
1226 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1227 free_cmdlines(&lines_ga);
1228 }
1229 current_line = 0;
1230 }
1231
1232 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001233 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1234 * being restored at the ":endtry". Reset them here and set the
1235 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1236 * This includes the case where a missing ":endif", ":endwhile" or
1237 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001239 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001241 cstack.cs_lflags &= ~CSL_HAD_FINA;
1242 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1243 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 did_throw ? (void *)current_exception : NULL);
1245 did_emsg = got_int = did_throw = FALSE;
1246 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1247 }
1248
1249 /* Update global "trylevel" for recursive calls to do_cmdline() from
1250 * within this loop. */
1251 trylevel = initial_trylevel + cstack.cs_trylevel;
1252
1253 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001254 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 * files) is aborted because of an error, an interrupt, or an uncaught
1256 * exception, cancel everything. If it is left normally, reset
1257 * force_abort to get the non-EH compatible abortion behavior for
1258 * the rest of the script.
1259 */
1260 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1261 force_abort = FALSE;
1262
1263 /* Convert an interrupt to an exception if appropriate. */
1264 (void)do_intthrow(&cstack);
1265#endif /* FEAT_EVAL */
1266
1267 }
1268 /*
1269 * Continue executing command lines when:
1270 * - no CTRL-C typed, no aborting error, no exception thrown or try
1271 * conditionals need to be checked for executing finally clauses or
1272 * catching an interrupt exception
1273 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001274 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 * looping for ":source" command or function call.
1276 */
1277 while (!((got_int
1278#ifdef FEAT_EVAL
1279 || (did_emsg && force_abort) || did_throw
1280#endif
1281 )
1282#ifdef FEAT_EVAL
1283 && cstack.cs_trylevel == 0
1284#endif
1285 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001286 && !(did_emsg
1287#ifdef FEAT_EVAL
1288 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001289 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001290 * the :endtry to be missed. */
1291 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1292#endif
1293 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001294 && (getline_equal(fgetline, cookie, getexmodeline)
1295 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 && (next_cmdline != NULL
1297#ifdef FEAT_EVAL
1298 || cstack.cs_idx >= 0
1299#endif
1300 || (flags & DOCMD_REPEAT)));
1301
1302 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001303 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304#ifdef FEAT_EVAL
1305 free_cmdlines(&lines_ga);
1306 ga_clear(&lines_ga);
1307
1308 if (cstack.cs_idx >= 0)
1309 {
1310 /*
1311 * If a sourced file or executed function ran to its end, report the
1312 * unclosed conditional.
1313 */
1314 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001315 && ((getline_equal(fgetline, cookie, getsourceline)
1316 && !source_finished(fgetline, cookie))
1317 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 && !func_has_ended(real_cookie))))
1319 {
1320 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1321 EMSG(_(e_endtry));
1322 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1323 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001324 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1325 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 else
1327 EMSG(_(e_endif));
1328 }
1329
1330 /*
1331 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1332 * ":endtry" in a sourced file or executed function. If the try
1333 * conditional is in its finally clause, ignore anything pending.
1334 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001335 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 */
1337 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001338 {
1339 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1340
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001341 if (idx >= 0)
1342 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001343 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1344 &cstack.cs_looplevel);
1345 }
1346 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 trylevel = initial_trylevel;
1348 }
1349
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001350 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1351 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001353 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 ? (char_u *)"endfunction" : (char_u *)NULL);
1355
1356 if (trylevel == 0)
1357 {
1358 /*
1359 * When an exception is being thrown out of the outermost try
1360 * conditional, discard the uncaught exception, disable the conversion
1361 * of interrupts or errors to exceptions, and ensure that no more
1362 * commands are executed.
1363 */
1364 if (did_throw)
1365 {
1366 void *p = NULL;
1367 char_u *saved_sourcing_name;
1368 int saved_sourcing_lnum;
1369 struct msglist *messages = NULL, *next;
1370
1371 /*
1372 * If the uncaught exception is a user exception, report it as an
1373 * error. If it is an error exception, display the saved error
1374 * message now. For an interrupt exception, do nothing; the
1375 * interrupt message is given elsewhere.
1376 */
1377 switch (current_exception->type)
1378 {
1379 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001380 vim_snprintf((char *)IObuff, IOSIZE,
1381 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 current_exception->value);
1383 p = vim_strsave(IObuff);
1384 break;
1385 case ET_ERROR:
1386 messages = current_exception->messages;
1387 current_exception->messages = NULL;
1388 break;
1389 case ET_INTERRUPT:
1390 break;
1391 default:
1392 p = vim_strsave((char_u *)_(e_internal));
1393 }
1394
1395 saved_sourcing_name = sourcing_name;
1396 saved_sourcing_lnum = sourcing_lnum;
1397 sourcing_name = current_exception->throw_name;
1398 sourcing_lnum = current_exception->throw_lnum;
1399 current_exception->throw_name = NULL;
1400
1401 discard_current_exception(); /* uses IObuff if 'verbose' */
1402 suppress_errthrow = TRUE;
1403 force_abort = TRUE;
1404
1405 if (messages != NULL)
1406 {
1407 do
1408 {
1409 next = messages->next;
1410 emsg(messages->msg);
1411 vim_free(messages->msg);
1412 vim_free(messages);
1413 messages = next;
1414 }
1415 while (messages != NULL);
1416 }
1417 else if (p != NULL)
1418 {
1419 emsg(p);
1420 vim_free(p);
1421 }
1422 vim_free(sourcing_name);
1423 sourcing_name = saved_sourcing_name;
1424 sourcing_lnum = saved_sourcing_lnum;
1425 }
1426
1427 /*
1428 * On an interrupt or an aborting error not converted to an exception,
1429 * disable the conversion of errors to exceptions. (Interrupts are not
1430 * converted any more, here.) This enables also the interrupt message
1431 * when force_abort is set and did_emsg unset in case of an interrupt
1432 * from a finally clause after an error.
1433 */
1434 else if (got_int || (did_emsg && force_abort))
1435 suppress_errthrow = TRUE;
1436 }
1437
1438 /*
1439 * The current cstack will be freed when do_cmdline() returns. An uncaught
1440 * exception will have to be rethrown in the previous cstack. If a function
1441 * has just returned or a script file was just finished and the previous
1442 * cstack belongs to the same function or, respectively, script file, it
1443 * will have to be checked for finally clauses to be executed due to the
1444 * ":return" or ":finish". This is done in do_one_cmd().
1445 */
1446 if (did_throw)
1447 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001448 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001450 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 && ex_nesting_level > func_level(real_cookie) + 1))
1452 {
1453 if (!did_throw)
1454 check_cstack = TRUE;
1455 }
1456 else
1457 {
1458 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001459 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 --ex_nesting_level;
1461 /*
1462 * Go to debug mode when returning from a function in which we are
1463 * single-stepping.
1464 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001465 if ((getline_equal(fgetline, cookie, getsourceline)
1466 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001468 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 ? (char_u *)_("End of sourced file")
1470 : (char_u *)_("End of function"));
1471 }
1472
1473 /*
1474 * Restore the exception environment (done after returning from the
1475 * debugger).
1476 */
1477 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001478 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479
1480 msg_list = saved_msg_list;
1481#endif /* FEAT_EVAL */
1482
1483 /*
1484 * If there was too much output to fit on the command line, ask the user to
1485 * hit return before redrawing the screen. With the ":global" command we do
1486 * this only once after the command is finished.
1487 */
1488 if (did_inc)
1489 {
1490 --RedrawingDisabled;
1491 --no_wait_return;
1492 msg_scroll = FALSE;
1493
1494 /*
1495 * When just finished an ":if"-":else" which was typed, no need to
1496 * wait for hit-return. Also for an error situation.
1497 */
1498 if (retval == FAIL
1499#ifdef FEAT_EVAL
1500 || (did_endif && KeyTyped && !did_emsg)
1501#endif
1502 )
1503 {
1504 need_wait_return = FALSE;
1505 msg_didany = FALSE; /* don't wait when restarting edit */
1506 }
1507 else if (need_wait_return)
1508 {
1509 /*
1510 * The msg_start() above clears msg_didout. The wait_return we do
1511 * here should not overwrite the command that may be shown before
1512 * doing that.
1513 */
1514 msg_didout |= msg_didout_before_start;
1515 wait_return(FALSE);
1516 }
1517 }
1518
Bram Moolenaar2a942252012-11-28 23:03:07 +01001519#ifdef FEAT_EVAL
1520 did_endif = FALSE; /* in case do_cmdline used recursively */
1521#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 /*
1523 * Reset if_level, in case a sourced script file contains more ":if" than
1524 * ":endif" (could be ":if x | foo | endif").
1525 */
1526 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001527#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001528
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 --call_depth;
1530 return retval;
1531}
1532
1533#ifdef FEAT_EVAL
1534/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001535 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 */
1537 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001538get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001540 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 wcmd_T *wp;
1542 char_u *line;
1543
1544 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1545 {
1546 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001547 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001549 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 if (cp->getline == NULL)
1551 line = getcmdline(c, 0L, indent);
1552 else
1553 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001554 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 ++cp->current_line;
1556
1557 return line;
1558 }
1559
1560 KeyTyped = FALSE;
1561 ++cp->current_line;
1562 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1563 sourcing_lnum = wp->lnum;
1564 return vim_strsave(wp->line);
1565}
1566
1567/*
1568 * Store a line in "gap" so that a ":while" loop can execute it again.
1569 */
1570 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001571store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572{
1573 if (ga_grow(gap, 1) == FAIL)
1574 return FAIL;
1575 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1576 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1577 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 return OK;
1579}
1580
1581/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001582 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 */
1584 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001585free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586{
1587 while (gap->ga_len > 0)
1588 {
1589 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1590 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 }
1592}
1593#endif
1594
1595/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001596 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1597 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001600getline_equal(
1601 char_u *(*fgetline)(int, void *, int),
1602 void *cookie UNUSED, /* argument for fgetline() */
1603 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604{
1605#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001606 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001607 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608
Bram Moolenaar89d40322006-08-29 15:30:07 +00001609 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001610 * function that's originally used to obtain the lines. This may be
1611 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001612 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001613 cp = (struct loop_cookie *)cookie;
1614 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 {
1616 gp = cp->getline;
1617 cp = cp->cookie;
1618 }
1619 return gp == func;
1620#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001621 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622#endif
1623}
1624
1625#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1626/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001627 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 * getline function. Otherwise return "cookie".
1629 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001631getline_cookie(
1632 char_u *(*fgetline)(int, void *, int) UNUSED,
1633 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634{
1635# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001636 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001637 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638
Bram Moolenaar89d40322006-08-29 15:30:07 +00001639 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001640 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001642 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001643 cp = (struct loop_cookie *)cookie;
1644 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 {
1646 gp = cp->getline;
1647 cp = cp->cookie;
1648 }
1649 return cp;
1650# else
1651 return cookie;
1652# endif
1653}
1654#endif
1655
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001656
1657/*
1658 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1659 * ":bwipeout", etc.
1660 * Returns the buffer number.
1661 */
1662 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001663compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001664{
1665 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001666 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001667 int count = offset;
1668
1669 buf = firstbuf;
1670 while (buf->b_next != NULL && buf->b_fnum < lnum)
1671 buf = buf->b_next;
1672 while (count != 0)
1673 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001674 count += (offset < 0) ? 1 : -1;
1675 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1676 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001677 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001678 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001679 if (addr_type == ADDR_LOADED_BUFFERS)
1680 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001681 while (buf->b_ml.ml_mfp == NULL)
1682 {
1683 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1684 if (nextbuf == NULL)
1685 break;
1686 buf = nextbuf;
1687 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001688 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001689 /* we might have gone too far, last buffer is not loadedd */
1690 if (addr_type == ADDR_LOADED_BUFFERS)
1691 while (buf->b_ml.ml_mfp == NULL)
1692 {
1693 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1694 if (nextbuf == NULL)
1695 break;
1696 buf = nextbuf;
1697 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001698 return buf->b_fnum;
1699}
1700
Bram Moolenaarf240e182014-11-27 18:33:02 +01001701#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001702static int current_win_nr(win_T *win);
1703static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001704
1705 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001706current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001707{
1708 win_T *wp;
1709 int nr = 0;
1710
Bram Moolenaar29323592016-07-24 22:04:11 +02001711 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001712 {
1713 ++nr;
1714 if (wp == win)
1715 break;
1716 }
1717 return nr;
1718}
1719
1720 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001721current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001722{
1723 tabpage_T *tp;
1724 int nr = 0;
1725
Bram Moolenaar29323592016-07-24 22:04:11 +02001726 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001727 {
1728 ++nr;
1729 if (tp == tab)
1730 break;
1731 }
1732 return nr;
1733}
1734
1735# define CURRENT_WIN_NR current_win_nr(curwin)
1736# define LAST_WIN_NR current_win_nr(NULL)
1737# define CURRENT_TAB_NR current_tab_nr(curtab)
1738# define LAST_TAB_NR current_tab_nr(NULL)
1739#else
1740# define CURRENT_WIN_NR 1
1741# define LAST_WIN_NR 1
1742# define CURRENT_TAB_NR 1
1743# define LAST_TAB_NR 1
1744#endif
1745
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001746
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747/*
1748 * Execute one Ex command.
1749 *
1750 * If 'sourcing' is TRUE, the command will be included in the error message.
1751 *
1752 * 1. skip comment lines and leading space
1753 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001754 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001755 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001756 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001757 * 6. parse arguments
1758 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001760 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 *
1762 * This function may be called recursively!
1763 */
1764#if (_MSC_VER == 1200)
1765/*
Bram Moolenaared203462004-06-16 11:19:22 +00001766 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001768 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769#endif
1770 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001771do_one_cmd(
1772 char_u **cmdlinep,
1773 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001775 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001777 char_u *(*fgetline)(int, void *, int),
1778 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779{
1780 char_u *p;
1781 linenr_T lnum;
1782 long n;
1783 char_u *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001784 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 exarg_T ea; /* Ex command arguments */
1786 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001787 int save_msg_scroll = msg_scroll;
1788 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001790#ifdef HAVE_SANDBOX
1791 int did_sandbox = FALSE;
1792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 cmdmod_T save_cmdmod;
1794 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001795 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796
1797 vim_memset(&ea, 0, sizeof(ea));
1798 ea.line1 = 1;
1799 ea.line2 = 1;
1800#ifdef FEAT_EVAL
1801 ++ex_nesting_level;
1802#endif
1803
Bram Moolenaar21691f82012-12-05 19:13:18 +01001804 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 if (quitmore
1806#ifdef FEAT_EVAL
1807 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001808 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001809#endif
1810#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001811 /* avoid that an autocommand, e.g. QuitPre, does this */
1812 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813#endif
1814 )
1815 --quitmore;
1816
1817 /*
1818 * Reset browse, confirm, etc.. They are restored when returning, for
1819 * recursive calls.
1820 */
1821 save_cmdmod = cmdmod;
1822 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1823
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001824 /* "#!anything" is handled like a comment. */
1825 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1826 goto doend;
1827
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 /*
1829 * Repeat until no more command modifiers are found.
1830 */
1831 ea.cmd = *cmdlinep;
1832 for (;;)
1833 {
1834/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001835 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 */
1837 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1838 ++ea.cmd;
1839
1840 /* in ex mode, an empty line works like :+ */
1841 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001842 && (getline_equal(fgetline, cookie, getexmodeline)
1843 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001844 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 {
1846 ea.cmd = (char_u *)"+";
1847 ex_pressedreturn = TRUE;
1848 }
1849
1850 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001851 if (*ea.cmd == '"')
1852 goto doend;
1853 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001854 {
1855 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858
1859/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001860 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 */
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001862 p = skip_range(ea.cmd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 switch (*p)
1864 {
1865 /* When adding an entry, also modify cmd_exists(). */
1866 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1867 break;
1868#ifdef FEAT_WINDOWS
1869 cmdmod.split |= WSP_ABOVE;
1870#endif
1871 continue;
1872
1873 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1874 {
1875#ifdef FEAT_WINDOWS
1876 cmdmod.split |= WSP_BELOW;
1877#endif
1878 continue;
1879 }
1880 if (checkforcmd(&ea.cmd, "browse", 3))
1881 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001882#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 cmdmod.browse = TRUE;
1884#endif
1885 continue;
1886 }
1887 if (!checkforcmd(&ea.cmd, "botright", 2))
1888 break;
1889#ifdef FEAT_WINDOWS
1890 cmdmod.split |= WSP_BOT;
1891#endif
1892 continue;
1893
1894 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1895 break;
1896#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1897 cmdmod.confirm = TRUE;
1898#endif
1899 continue;
1900
1901 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1902 {
1903 cmdmod.keepmarks = TRUE;
1904 continue;
1905 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001906 if (checkforcmd(&ea.cmd, "keepalt", 5))
1907 {
1908 cmdmod.keepalt = TRUE;
1909 continue;
1910 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001911 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1912 {
1913 cmdmod.keeppatterns = TRUE;
1914 continue;
1915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1917 break;
1918 cmdmod.keepjumps = TRUE;
1919 continue;
1920
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001921 case 'f': /* only accept ":filter {pat} cmd" */
1922 {
1923 char_u *reg_pat;
1924
1925 if (!checkforcmd(&p, "filter", 4)
1926 || *p == NUL || ends_excmd(*p))
1927 break;
1928 p = skip_vimgrep_pat(p, &reg_pat, NULL);
1929 if (p == NULL || *p == NUL)
1930 break;
1931 cmdmod.filter_regmatch.regprog =
1932 vim_regcomp(reg_pat, RE_MAGIC);
1933 if (cmdmod.filter_regmatch.regprog == NULL)
1934 break;
1935 ea.cmd = p;
1936 continue;
1937 }
1938
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 /* ":hide" and ":hide | cmd" are not modifiers */
1940 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1941 || *p == NUL || ends_excmd(*p))
1942 break;
1943 ea.cmd = p;
1944 cmdmod.hide = TRUE;
1945 continue;
1946
1947 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1948 {
1949 cmdmod.lockmarks = TRUE;
1950 continue;
1951 }
1952
1953 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1954 break;
1955#ifdef FEAT_WINDOWS
1956 cmdmod.split |= WSP_ABOVE;
1957#endif
1958 continue;
1959
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001960 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001961 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001962#ifdef FEAT_AUTOCMD
1963 if (cmdmod.save_ei == NULL)
1964 {
1965 /* Set 'eventignore' to "all". Restore the
1966 * existing option value later. */
1967 cmdmod.save_ei = vim_strsave(p_ei);
1968 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001969 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001970 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001971#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001972 continue;
1973 }
Bram Moolenaar3bcfca32016-07-30 19:39:29 +02001974 if (!checkforcmd(&ea.cmd, "noswapfile", 3))
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001975 break;
1976 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001977 continue;
1978
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1980 break;
1981#ifdef FEAT_WINDOWS
1982 cmdmod.split |= WSP_BELOW;
1983#endif
1984 continue;
1985
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001986 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1987 {
1988#ifdef HAVE_SANDBOX
1989 if (!did_sandbox)
1990 ++sandbox;
1991 did_sandbox = TRUE;
1992#endif
1993 continue;
1994 }
1995 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001997 if (save_msg_silent == -1)
1998 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
2001 {
2002 /* ":silent!", but not "silent !cmd" */
2003 ea.cmd = skipwhite(ea.cmd + 1);
2004 ++emsg_silent;
2005 ++did_esilent;
2006 }
2007 continue;
2008
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002009 case 't': if (checkforcmd(&p, "tab", 3))
2010 {
2011#ifdef FEAT_WINDOWS
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02002012 long tabnr = get_address(&ea, &ea.cmd, ADDR_TABS,
2013 ea.skip, FALSE);
2014 if (tabnr == MAXLNUM)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002015 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02002016 else
2017 {
2018 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2019 {
2020 errormsg = (char_u *)_(e_invrange);
2021 goto doend;
2022 }
2023 cmdmod.tab = tabnr + 1;
2024 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002025 ea.cmd = p;
2026#endif
2027 continue;
2028 }
2029 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 break;
2031#ifdef FEAT_WINDOWS
2032 cmdmod.split |= WSP_TOP;
2033#endif
2034 continue;
2035
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002036 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
2037 break;
2038 if (save_msg_silent == -1)
2039 save_msg_silent = msg_silent;
2040 msg_silent = 0;
2041 continue;
2042
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
2044 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002045#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 cmdmod.split |= WSP_VERT;
2047#endif
2048 continue;
2049 }
2050 if (!checkforcmd(&p, "verbose", 4))
2051 break;
2052 if (verbose_save < 0)
2053 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00002054 if (vim_isdigit(*ea.cmd))
2055 p_verbose = atoi((char *)ea.cmd);
2056 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 p_verbose = 1;
2058 ea.cmd = p;
2059 continue;
2060 }
2061 break;
2062 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002063 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064
2065#ifdef FEAT_EVAL
2066 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2067 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2068#else
2069 ea.skip = (if_level > 0);
2070#endif
2071
2072#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002073# ifdef FEAT_PROFILE
2074 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002075 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002076 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002077 if (getline_equal(fgetline, cookie, get_func_line))
2078 func_line_exec(getline_cookie(fgetline, cookie));
2079 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002080 script_line_exec();
2081 }
2082#endif
2083
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 /* May go to debug mode. If this happens and the ">quit" debug command is
2085 * used, throw an interrupt exception and skip the next command. */
2086 dbg_check_breakpoint(&ea);
2087 if (!ea.skip && got_int)
2088 {
2089 ea.skip = TRUE;
2090 (void)do_intthrow(cstack);
2091 }
2092#endif
2093
2094/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002095 * 3. Skip over the range to find the command. Let "p" point to after it.
2096 *
2097 * We need the command to know what kind of range it uses.
2098 */
2099 cmd = ea.cmd;
2100 ea.cmd = skip_range(ea.cmd, NULL);
2101 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2102 ea.cmd = skipwhite(ea.cmd + 1);
2103 p = find_command(&ea, NULL);
2104
2105/*
2106 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 *
2108 * where 'addr' is:
2109 *
2110 * % (entire file)
2111 * $ [+-NUM]
2112 * 'x [+-NUM] (where x denotes a currently defined mark)
2113 * . [+-NUM]
2114 * [+-NUM]..
2115 * NUM
2116 *
2117 * The ea.cmd pointer is updated to point to the first character following the
2118 * range spec. If an initial address is found, but no second, the upper bound
2119 * is equal to the lower.
2120 */
2121
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002122 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002123 if (!IS_USER_CMDIDX(ea.cmdidx))
2124 {
2125 if (ea.cmdidx != CMD_SIZE)
2126 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2127 else
2128 ea.addr_type = ADDR_LINES;
2129
2130#ifdef FEAT_WINDOWS
2131 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002132 if (ea.cmdidx == CMD_wincmd && p != NULL)
2133 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002134#endif
2135 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002136
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002138 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 for (;;)
2140 {
2141 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002142 switch (ea.addr_type)
2143 {
2144 case ADDR_LINES:
2145 /* default is current line number */
2146 ea.line2 = curwin->w_cursor.lnum;
2147 break;
2148 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002149 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002150 ea.line2 = lnum;
2151 break;
2152 case ADDR_ARGUMENTS:
2153 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002154 if (ea.line2 > ARGCOUNT)
2155 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002156 break;
2157 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002158 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002159 ea.line2 = curbuf->b_fnum;
2160 break;
2161 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002162 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002163 ea.line2 = lnum;
2164 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002165#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002166 case ADDR_QUICKFIX:
2167 ea.line2 = qf_get_cur_valid_idx(&ea);
2168 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002169#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002172 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip,
2173 ea.addr_count == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 if (ea.cmd == NULL) /* error detected */
2175 goto doend;
2176 if (lnum == MAXLNUM)
2177 {
2178 if (*ea.cmd == '%') /* '%' - all lines */
2179 {
2180 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002181 switch (ea.addr_type)
2182 {
2183 case ADDR_LINES:
2184 ea.line1 = 1;
2185 ea.line2 = curbuf->b_ml.ml_line_count;
2186 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002187 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002188 {
2189 buf_T *buf = firstbuf;
2190
2191 while (buf->b_next != NULL
2192 && buf->b_ml.ml_mfp == NULL)
2193 buf = buf->b_next;
2194 ea.line1 = buf->b_fnum;
2195 buf = lastbuf;
2196 while (buf->b_prev != NULL
2197 && buf->b_ml.ml_mfp == NULL)
2198 buf = buf->b_prev;
2199 ea.line2 = buf->b_fnum;
2200 break;
2201 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002202 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002203 ea.line1 = firstbuf->b_fnum;
2204 ea.line2 = lastbuf->b_fnum;
2205 break;
2206 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002207 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002208 if (IS_USER_CMDIDX(ea.cmdidx))
2209 {
2210 ea.line1 = 1;
2211 ea.line2 = ea.addr_type == ADDR_WINDOWS
2212 ? LAST_WIN_NR : LAST_TAB_NR;
2213 }
2214 else
2215 {
2216 /* there is no Vim command which uses '%' and
2217 * ADDR_WINDOWS or ADDR_TABS */
2218 errormsg = (char_u *)_(e_invrange);
2219 goto doend;
2220 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002221 break;
2222 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002223 if (ARGCOUNT == 0)
2224 ea.line1 = ea.line2 = 0;
2225 else
2226 {
2227 ea.line1 = 1;
2228 ea.line2 = ARGCOUNT;
2229 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002230 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002231#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002232 case ADDR_QUICKFIX:
2233 ea.line1 = 1;
2234 ea.line2 = qf_get_size(&ea);
2235 if (ea.line2 == 0)
2236 ea.line2 = 1;
2237 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002238#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 ++ea.addr_count;
2241 }
2242 /* '*' - visual area */
2243 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2244 {
2245 pos_T *fp;
2246
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002247 if (ea.addr_type != ADDR_LINES)
2248 {
2249 errormsg = (char_u *)_(e_invrange);
2250 goto doend;
2251 }
2252
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 ++ea.cmd;
2254 if (!ea.skip)
2255 {
2256 fp = getmark('<', FALSE);
2257 if (check_mark(fp) == FAIL)
2258 goto doend;
2259 ea.line1 = fp->lnum;
2260 fp = getmark('>', FALSE);
2261 if (check_mark(fp) == FAIL)
2262 goto doend;
2263 ea.line2 = fp->lnum;
2264 ++ea.addr_count;
2265 }
2266 }
2267 }
2268 else
2269 ea.line2 = lnum;
2270 ea.addr_count++;
2271
2272 if (*ea.cmd == ';')
2273 {
2274 if (!ea.skip)
2275 curwin->w_cursor.lnum = ea.line2;
2276 }
2277 else if (*ea.cmd != ',')
2278 break;
2279 ++ea.cmd;
2280 }
2281
2282 /* One address given: set start and end lines */
2283 if (ea.addr_count == 1)
2284 {
2285 ea.line1 = ea.line2;
2286 /* ... but only implicit: really no address given */
2287 if (lnum == MAXLNUM)
2288 ea.addr_count = 0;
2289 }
2290
2291 /* Don't leave the cursor on an illegal line (caused by ';') */
2292 check_cursor_lnum();
2293
2294/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002295 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 */
2297
2298 /*
2299 * Skip ':' and any white space
2300 */
2301 ea.cmd = skipwhite(ea.cmd);
2302 while (*ea.cmd == ':')
2303 ea.cmd = skipwhite(ea.cmd + 1);
2304
2305 /*
2306 * If we got a line, but no command, then go to the line.
2307 * If we find a '|' or '\n' we set ea.nextcmd.
2308 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002309 if (*ea.cmd == NUL || *ea.cmd == '"'
2310 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 {
2312 /*
2313 * strange vi behaviour:
2314 * ":3" jumps to line 3
2315 * ":3|..." prints line 3
2316 * ":|" prints current line
2317 */
2318 if (ea.skip) /* skip this if inside :if */
2319 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002320 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 {
2322 ea.cmdidx = CMD_print;
2323 ea.argt = RANGE+COUNT+TRLBAR;
2324 if ((errormsg = invalid_range(&ea)) == NULL)
2325 {
2326 correct_range(&ea);
2327 ex_print(&ea);
2328 }
2329 }
2330 else if (ea.addr_count != 0)
2331 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002332 if (ea.line2 > curbuf->b_ml.ml_line_count)
2333 {
2334 /* With '-' in 'cpoptions' a line number past the file is an
2335 * error, otherwise put it at the end of the file. */
2336 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2337 ea.line2 = -1;
2338 else
2339 ea.line2 = curbuf->b_ml.ml_line_count;
2340 }
2341
2342 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002343 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 else
2345 {
2346 if (ea.line2 == 0)
2347 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 else
2349 curwin->w_cursor.lnum = ea.line2;
2350 beginline(BL_SOL | BL_FIX);
2351 }
2352 }
2353 goto doend;
2354 }
2355
Bram Moolenaard5005162014-08-22 23:05:54 +02002356#ifdef FEAT_AUTOCMD
2357 /* If this looks like an undefined user command and there are CmdUndefined
2358 * autocommands defined, trigger the matching autocommands. */
2359 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2360 && ASCII_ISUPPER(*ea.cmd)
2361 && has_cmdundefined())
2362 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002363 int ret;
2364
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002365 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002366 while (ASCII_ISALNUM(*p))
2367 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002368 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002369 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2370 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002371 /* If the autocommands did something and didn't cause an error, try
2372 * finding the command again. */
Bram Moolenaareac784e2016-07-28 22:08:24 +02002373 p = (ret && !aborting()) ? find_command(&ea, NULL) : ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002374 }
2375#endif
2376
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377#ifdef FEAT_USR_CMDS
2378 if (p == NULL)
2379 {
2380 if (!ea.skip)
2381 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2382 goto doend;
2383 }
2384 /* Check for wrong commands. */
2385 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2386 {
2387 errormsg = uc_fun_cmd();
2388 goto doend;
2389 }
2390#endif
2391 if (ea.cmdidx == CMD_SIZE)
2392 {
2393 if (!ea.skip)
2394 {
2395 STRCPY(IObuff, _("E492: Not an editor command"));
2396 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002397 {
2398 /* If the modifier was parsed OK the error must be in the
2399 * following command */
2400 if (after_modifier != NULL)
2401 append_command(after_modifier);
2402 else
2403 append_command(*cmdlinep);
2404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002406 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 }
2408 goto doend;
2409 }
2410
Bram Moolenaar958636c2014-10-21 20:01:58 +02002411 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2412 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002413#ifdef HAVE_EX_SCRIPT_NI
2414 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2415#endif
2416 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417
2418#ifndef FEAT_EVAL
2419 /*
2420 * When the expression evaluation is disabled, recognize the ":if" and
2421 * ":endif" commands and ignore everything in between it.
2422 */
2423 if (ea.cmdidx == CMD_if)
2424 ++if_level;
2425 if (if_level)
2426 {
2427 if (ea.cmdidx == CMD_endif)
2428 --if_level;
2429 goto doend;
2430 }
2431
2432#endif
2433
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002434 /* forced commands */
2435 if (*p == '!' && ea.cmdidx != CMD_substitute
2436 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 {
2438 ++p;
2439 ea.forceit = TRUE;
2440 }
2441 else
2442 ea.forceit = FALSE;
2443
2444/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002445 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002447 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002448 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449
2450 if (!ea.skip)
2451 {
2452#ifdef HAVE_SANDBOX
2453 if (sandbox != 0 && !(ea.argt & SBOXOK))
2454 {
2455 /* Command not allowed in sandbox. */
2456 errormsg = (char_u *)_(e_sandbox);
2457 goto doend;
2458 }
2459#endif
2460 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2461 {
2462 /* Command not allowed in non-'modifiable' buffer */
2463 errormsg = (char_u *)_(e_modifiable);
2464 goto doend;
2465 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002466
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002467 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002468 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002470 /* Command not allowed when editing the command line. */
2471#ifdef FEAT_CMDWIN
2472 if (cmdwin_type != 0)
2473 errormsg = (char_u *)_(e_cmdwin);
2474 else
2475#endif
2476 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 goto doend;
2478 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002479#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002480 /* Disallow editing another buffer when "curbuf_lock" is set.
2481 * Do allow ":edit" (check for argument later).
2482 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002483 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002484 && ea.cmdidx != CMD_edit
2485 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002486 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002487 && curbuf_locked())
2488 goto doend;
2489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490
2491 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2492 {
2493 /* no range allowed */
2494 errormsg = (char_u *)_(e_norange);
2495 goto doend;
2496 }
2497 }
2498
2499 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2500 {
2501 errormsg = (char_u *)_(e_nobang);
2502 goto doend;
2503 }
2504
2505 /*
2506 * Don't complain about the range if it is not used
2507 * (could happen if line_count is accidentally set to 0).
2508 */
2509 if (!ea.skip && !ni)
2510 {
2511 /*
2512 * If the range is backwards, ask for confirmation and, if given, swap
2513 * ea.line1 & ea.line2 so it's forwards again.
2514 * When global command is busy, don't ask, will fail below.
2515 */
2516 if (!global_busy && ea.line1 > ea.line2)
2517 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002518 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002520 if (sourcing || exmode_active)
2521 {
2522 errormsg = (char_u *)_("E493: Backwards range given");
2523 goto doend;
2524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 if (ask_yesno((char_u *)
2526 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002527 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 }
2529 lnum = ea.line1;
2530 ea.line1 = ea.line2;
2531 ea.line2 = lnum;
2532 }
2533 if ((errormsg = invalid_range(&ea)) != NULL)
2534 goto doend;
2535 }
2536
2537 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2538 ea.line2 = 1;
2539
2540 correct_range(&ea);
2541
2542#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002543 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2544 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 {
2546 /* Put the first line at the start of a closed fold, put the last line
2547 * at the end of a closed fold. */
2548 (void)hasFolding(ea.line1, &ea.line1, NULL);
2549 (void)hasFolding(ea.line2, NULL, &ea.line2);
2550 }
2551#endif
2552
2553#ifdef FEAT_QUICKFIX
2554 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002555 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 * option here, so things like % get expanded.
2557 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002558 p = replace_makeprg(&ea, p, cmdlinep);
2559 if (p == NULL)
2560 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561#endif
2562
2563 /*
2564 * Skip to start of argument.
2565 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2566 */
2567 if (ea.cmdidx == CMD_bang)
2568 ea.arg = p;
2569 else
2570 ea.arg = skipwhite(p);
2571
2572 /*
2573 * Check for "++opt=val" argument.
2574 * Must be first, allow ":w ++enc=utf8 !cmd"
2575 */
2576 if (ea.argt & ARGOPT)
2577 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2578 if (getargopt(&ea) == FAIL && !ni)
2579 {
2580 errormsg = (char_u *)_(e_invarg);
2581 goto doend;
2582 }
2583
2584 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2585 {
2586 if (*ea.arg == '>') /* append */
2587 {
2588 if (*++ea.arg != '>') /* typed wrong */
2589 {
2590 errormsg = (char_u *)_("E494: Use w or w>>");
2591 goto doend;
2592 }
2593 ea.arg = skipwhite(ea.arg + 1);
2594 ea.append = TRUE;
2595 }
2596 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2597 {
2598 ++ea.arg;
2599 ea.usefilter = TRUE;
2600 }
2601 }
2602
2603 if (ea.cmdidx == CMD_read)
2604 {
2605 if (ea.forceit)
2606 {
2607 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2608 ea.forceit = FALSE;
2609 }
2610 else if (*ea.arg == '!') /* :r !filter */
2611 {
2612 ++ea.arg;
2613 ea.usefilter = TRUE;
2614 }
2615 }
2616
2617 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2618 {
2619 ea.amount = 1;
2620 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2621 {
2622 ++ea.arg;
2623 ++ea.amount;
2624 }
2625 ea.arg = skipwhite(ea.arg);
2626 }
2627
2628 /*
2629 * Check for "+command" argument, before checking for next command.
2630 * Don't do this for ":read !cmd" and ":write !cmd".
2631 */
2632 if ((ea.argt & EDITCMD) && !ea.usefilter)
2633 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2634
2635 /*
2636 * Check for '|' to separate commands and '"' to start comments.
2637 * Don't do this for ":read !cmd" and ":write !cmd".
2638 */
2639 if ((ea.argt & TRLBAR) && !ea.usefilter)
2640 separate_nextcmd(&ea);
2641
2642 /*
2643 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002644 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2645 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002647 else if (ea.cmdidx == CMD_bang
2648 || ea.cmdidx == CMD_global
2649 || ea.cmdidx == CMD_vglobal
2650 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 {
2652 for (p = ea.arg; *p; ++p)
2653 {
2654 /* Remove one backslash before a newline, so that it's possible to
2655 * pass a newline to the shell and also a newline that is preceded
2656 * with a backslash. This makes it impossible to end a shell
2657 * command in a backslash, but that doesn't appear useful.
2658 * Halving the number of backslashes is incompatible with previous
2659 * versions. */
2660 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002661 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 else if (*p == '\n')
2663 {
2664 ea.nextcmd = p + 1;
2665 *p = NUL;
2666 break;
2667 }
2668 }
2669 }
2670
2671 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2672 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002673 buf_T *buf;
2674
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002676 switch (ea.addr_type)
2677 {
2678 case ADDR_LINES:
2679 ea.line2 = curbuf->b_ml.ml_line_count;
2680 break;
2681 case ADDR_LOADED_BUFFERS:
2682 buf = firstbuf;
2683 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2684 buf = buf->b_next;
2685 ea.line1 = buf->b_fnum;
2686 buf = lastbuf;
2687 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2688 buf = buf->b_prev;
2689 ea.line2 = buf->b_fnum;
2690 break;
2691 case ADDR_BUFFERS:
2692 ea.line1 = firstbuf->b_fnum;
2693 ea.line2 = lastbuf->b_fnum;
2694 break;
2695 case ADDR_WINDOWS:
2696 ea.line2 = LAST_WIN_NR;
2697 break;
2698 case ADDR_TABS:
2699 ea.line2 = LAST_TAB_NR;
2700 break;
2701 case ADDR_ARGUMENTS:
2702 if (ARGCOUNT == 0)
2703 ea.line1 = ea.line2 = 0;
2704 else
2705 ea.line2 = ARGCOUNT;
2706 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002707#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002708 case ADDR_QUICKFIX:
2709 ea.line2 = qf_get_size(&ea);
2710 if (ea.line2 == 0)
2711 ea.line2 = 1;
2712 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002713#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 }
2716
2717 /* accept numbered register only when no count allowed (:put) */
2718 if ( (ea.argt & REGSTR)
2719 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002720 /* Do not allow register = for user commands */
2721 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2723 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002724#ifndef FEAT_CLIPBOARD
2725 /* check these explicitly for a more specific error message */
2726 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002728 errormsg = (char_u *)_(e_invalidreg);
2729 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 }
2731#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002732 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2733 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002734 {
2735 ea.regname = *ea.arg++;
2736#ifdef FEAT_EVAL
2737 /* for '=' register: accept the rest of the line as an expression */
2738 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2739 {
2740 set_expr_line(vim_strsave(ea.arg));
2741 ea.arg += STRLEN(ea.arg);
2742 }
2743#endif
2744 ea.arg = skipwhite(ea.arg);
2745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 }
2747
2748 /*
2749 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2750 * count, it's a buffer name.
2751 */
2752 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2753 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2754 || vim_iswhite(*p)))
2755 {
2756 n = getdigits(&ea.arg);
2757 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002758 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 {
2760 errormsg = (char_u *)_(e_zerocount);
2761 goto doend;
2762 }
2763 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2764 {
2765 ea.line2 = n;
2766 if (ea.addr_count == 0)
2767 ea.addr_count = 1;
2768 }
2769 else
2770 {
2771 ea.line1 = ea.line2;
2772 ea.line2 += n - 1;
2773 ++ea.addr_count;
2774 /*
2775 * Be vi compatible: no error message for out of range.
2776 */
2777 if (ea.line2 > curbuf->b_ml.ml_line_count)
2778 ea.line2 = curbuf->b_ml.ml_line_count;
2779 }
2780 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002781
2782 /*
2783 * Check for flags: 'l', 'p' and '#'.
2784 */
2785 if (ea.argt & EXFLAGS)
2786 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002788 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002789 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 {
2791 errormsg = (char_u *)_(e_trailing);
2792 goto doend;
2793 }
2794
2795 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2796 {
2797 errormsg = (char_u *)_(e_argreq);
2798 goto doend;
2799 }
2800
2801#ifdef FEAT_EVAL
2802 /*
2803 * Skip the command when it's not going to be executed.
2804 * The commands like :if, :endif, etc. always need to be executed.
2805 * Also make an exception for commands that handle a trailing command
2806 * themselves.
2807 */
2808 if (ea.skip)
2809 {
2810 switch (ea.cmdidx)
2811 {
2812 /* commands that need evaluation */
2813 case CMD_while:
2814 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002815 case CMD_for:
2816 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 case CMD_if:
2818 case CMD_elseif:
2819 case CMD_else:
2820 case CMD_endif:
2821 case CMD_try:
2822 case CMD_catch:
2823 case CMD_finally:
2824 case CMD_endtry:
2825 case CMD_function:
2826 break;
2827
2828 /* Commands that handle '|' themselves. Check: A command should
2829 * either have the TRLBAR flag, appear in this list or appear in
2830 * the list at ":help :bar". */
2831 case CMD_aboveleft:
2832 case CMD_and:
2833 case CMD_belowright:
2834 case CMD_botright:
2835 case CMD_browse:
2836 case CMD_call:
2837 case CMD_confirm:
2838 case CMD_delfunction:
2839 case CMD_djump:
2840 case CMD_dlist:
2841 case CMD_dsearch:
2842 case CMD_dsplit:
2843 case CMD_echo:
2844 case CMD_echoerr:
2845 case CMD_echomsg:
2846 case CMD_echon:
2847 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002848 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 case CMD_help:
2850 case CMD_hide:
2851 case CMD_ijump:
2852 case CMD_ilist:
2853 case CMD_isearch:
2854 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002855 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 case CMD_keepjumps:
2857 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002858 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 case CMD_leftabove:
2860 case CMD_let:
2861 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002862 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002864 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002865 case CMD_noautocmd:
2866 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 case CMD_perl:
2868 case CMD_psearch:
2869 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002870 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002871 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 case CMD_return:
2873 case CMD_rightbelow:
2874 case CMD_ruby:
2875 case CMD_silent:
2876 case CMD_smagic:
2877 case CMD_snomagic:
2878 case CMD_substitute:
2879 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002880 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 case CMD_tcl:
2882 case CMD_throw:
2883 case CMD_tilde:
2884 case CMD_topleft:
2885 case CMD_unlet:
2886 case CMD_verbose:
2887 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002888 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 break;
2890
2891 default: goto doend;
2892 }
2893 }
2894#endif
2895
2896 if (ea.argt & XFILE)
2897 {
2898 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2899 goto doend;
2900 }
2901
2902#ifdef FEAT_LISTCMDS
2903 /*
2904 * Accept buffer name. Cannot be used at the same time with a buffer
2905 * number. Don't do this for a user command.
2906 */
2907 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002908 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 {
2910 /*
2911 * :bdelete, :bwipeout and :bunload take several arguments, separated
2912 * by spaces: find next space (skipping over escaped characters).
2913 * The others take one argument: ignore trailing spaces.
2914 */
2915 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2916 || ea.cmdidx == CMD_bunload)
2917 p = skiptowhite_esc(ea.arg);
2918 else
2919 {
2920 p = ea.arg + STRLEN(ea.arg);
2921 while (p > ea.arg && vim_iswhite(p[-1]))
2922 --p;
2923 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002924 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2925 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 if (ea.line2 < 0) /* failed */
2927 goto doend;
2928 ea.addr_count = 1;
2929 ea.arg = skipwhite(p);
2930 }
2931#endif
2932
2933/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002934 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 *
2936 * The "ea" structure holds the arguments that can be used.
2937 */
2938 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002939 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 ea.cookie = cookie;
2941#ifdef FEAT_EVAL
2942 ea.cstack = cstack;
2943#endif
2944
2945#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002946 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 {
2948 /*
2949 * Execute a user-defined command.
2950 */
2951 do_ucmd(&ea);
2952 }
2953 else
2954#endif
2955 {
2956 /*
2957 * Call the function to execute the command.
2958 */
2959 ea.errmsg = NULL;
2960 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2961 if (ea.errmsg != NULL)
2962 errormsg = (char_u *)_(ea.errmsg);
2963 }
2964
2965#ifdef FEAT_EVAL
2966 /*
2967 * If the command just executed called do_cmdline(), any throw or ":return"
2968 * or ":finish" encountered there must also check the cstack of the still
2969 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2970 * exception, or reanimate a returned function or finished script file and
2971 * return or finish it again.
2972 */
2973 if (need_rethrow)
2974 do_throw(cstack);
2975 else if (check_cstack)
2976 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002977 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002979 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 && current_func_returned())
2981 do_return(&ea, TRUE, FALSE, NULL);
2982 }
2983 need_rethrow = check_cstack = FALSE;
2984#endif
2985
2986doend:
2987 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2988 curwin->w_cursor.lnum = 1;
2989
2990 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2991 {
2992 if (sourcing)
2993 {
2994 if (errormsg != IObuff)
2995 {
2996 STRCPY(IObuff, errormsg);
2997 errormsg = IObuff;
2998 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002999 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 }
3001 emsg(errormsg);
3002 }
3003#ifdef FEAT_EVAL
3004 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02003005 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
3006 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007#endif
3008
3009 if (verbose_save >= 0)
3010 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00003011#ifdef FEAT_AUTOCMD
3012 if (cmdmod.save_ei != NULL)
3013 {
3014 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003015 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
3016 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00003017 free_string_option(cmdmod.save_ei);
3018 }
3019#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003020 if (cmdmod.filter_regmatch.regprog != NULL)
3021 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022
3023 cmdmod = save_cmdmod;
3024
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003025 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 {
3027 /* messages could be enabled for a serious error, need to check if the
3028 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00003029 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003030 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 emsg_silent -= did_esilent;
3032 if (emsg_silent < 0)
3033 emsg_silent = 0;
3034 /* Restore msg_scroll, it's set by file I/O commands, even when no
3035 * message is actually displayed. */
3036 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003037
3038 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
3039 * somewhere in the line. Put it back in the first column. */
3040 if (redirecting())
3041 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 }
3043
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003044#ifdef HAVE_SANDBOX
3045 if (did_sandbox)
3046 --sandbox;
3047#endif
3048
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3050 ea.nextcmd = NULL;
3051
3052#ifdef FEAT_EVAL
3053 --ex_nesting_level;
3054#endif
3055
3056 return ea.nextcmd;
3057}
3058#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003059 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060#endif
3061
3062/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003063 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 * If there is a match advance "pp" to the argument and return TRUE.
3065 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003066 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003067checkforcmd(
3068 char_u **pp, /* start of command */
3069 char *cmd, /* name of command */
3070 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071{
3072 int i;
3073
3074 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003075 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 break;
3077 if (i >= len && !isalpha((*pp)[i]))
3078 {
3079 *pp = skipwhite(*pp + i);
3080 return TRUE;
3081 }
3082 return FALSE;
3083}
3084
3085/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003086 * Append "cmd" to the error message in IObuff.
3087 * Takes care of limiting the length and handling 0xa0, which would be
3088 * invisible otherwise.
3089 */
3090 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003091append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003092{
3093 char_u *s = cmd;
3094 char_u *d;
3095
3096 STRCAT(IObuff, ": ");
3097 d = IObuff + STRLEN(IObuff);
3098 while (*s != NUL && d - IObuff < IOSIZE - 7)
3099 {
3100 if (
3101#ifdef FEAT_MBYTE
3102 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3103#endif
3104 *s == 0xa0)
3105 {
3106 s +=
3107#ifdef FEAT_MBYTE
3108 enc_utf8 ? 2 :
3109#endif
3110 1;
3111 STRCPY(d, "<a0>");
3112 d += 4;
3113 }
3114 else
3115 MB_COPY_CHAR(s, d);
3116 }
3117 *d = NUL;
3118}
3119
3120/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003122 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003124 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 * Returns NULL for an ambiguous user command.
3126 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003128find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129{
3130 int len;
3131 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003132 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133
3134 /*
3135 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003136 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 * - the 'k' command can directly be followed by any character.
3138 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003139 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003141 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 */
3143 p = eap->cmd;
3144 if (*p == 'k')
3145 {
3146 eap->cmdidx = CMD_k;
3147 ++p;
3148 }
3149 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003150 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3151 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 || p[1] == 'g'
3153 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3154 || p[1] == 'I'
3155 || (p[1] == 'r' && p[2] != 'e')))
3156 {
3157 eap->cmdidx = CMD_substitute;
3158 ++p;
3159 }
3160 else
3161 {
3162 while (ASCII_ISALPHA(*p))
3163 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003164 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003165 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003166 while (ASCII_ISALNUM(*p))
3167 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003168
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 /* check for non-alpha command */
3170 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3171 ++p;
3172 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003173 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3174 {
3175 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3176 * :delete with the 'l' flag. Same for 'p'. */
3177 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003178 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003179 break;
3180 if (i == len - 1)
3181 {
3182 --len;
3183 if (p[-1] == 'l')
3184 eap->flags |= EXFLAG_LIST;
3185 else
3186 eap->flags |= EXFLAG_PRINT;
3187 }
3188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189
3190 if (ASCII_ISLOWER(*eap->cmd))
3191 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
3192 else
3193 eap->cmdidx = cmdidxs[26];
3194
3195 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3196 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3197 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3198 (size_t)len) == 0)
3199 {
3200#ifdef FEAT_EVAL
3201 if (full != NULL
3202 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3203 *full = TRUE;
3204#endif
3205 break;
3206 }
3207
3208#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003209 /* Look for a user defined command as a last resort. Let ":Print" be
3210 * overruled by a user defined command. */
3211 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3212 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003214 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 while (ASCII_ISALNUM(*p))
3216 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003217 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 }
3219#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003220 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 eap->cmdidx = CMD_SIZE;
3222 }
3223
3224 return p;
3225}
3226
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003227#ifdef FEAT_USR_CMDS
3228/*
3229 * Search for a user command that matches "eap->cmd".
3230 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3231 * Return a pointer to just after the command.
3232 * Return NULL if there is no matching command.
3233 */
3234 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003235find_ucmd(
3236 exarg_T *eap,
3237 char_u *p, /* end of the command (possibly including count) */
3238 int *full, /* set to TRUE for a full match */
3239 expand_T *xp, /* used for completion, NULL otherwise */
3240 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003241{
3242 int len = (int)(p - eap->cmd);
3243 int j, k, matchlen = 0;
3244 ucmd_T *uc;
3245 int found = FALSE;
3246 int possible = FALSE;
3247 char_u *cp, *np; /* Point into typed cmd and test name */
3248 garray_T *gap;
3249 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3250 only full match global is accepted. */
3251
3252 /*
3253 * Look for buffer-local user commands first, then global ones.
3254 */
3255 gap = &curbuf->b_ucmds;
3256 for (;;)
3257 {
3258 for (j = 0; j < gap->ga_len; ++j)
3259 {
3260 uc = USER_CMD_GA(gap, j);
3261 cp = eap->cmd;
3262 np = uc->uc_name;
3263 k = 0;
3264 while (k < len && *np != NUL && *cp++ == *np++)
3265 k++;
3266 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3267 {
3268 /* If finding a second match, the command is ambiguous. But
3269 * not if a buffer-local command wasn't a full match and a
3270 * global command is a full match. */
3271 if (k == len && found && *np != NUL)
3272 {
3273 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003274 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003275 amb_local = TRUE;
3276 }
3277
3278 if (!found || (k == len && *np == NUL))
3279 {
3280 /* If we matched up to a digit, then there could
3281 * be another command including the digit that we
3282 * should use instead.
3283 */
3284 if (k == len)
3285 found = TRUE;
3286 else
3287 possible = TRUE;
3288
3289 if (gap == &ucmds)
3290 eap->cmdidx = CMD_USER;
3291 else
3292 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003293 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003294 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003295 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003296
3297# ifdef FEAT_CMDL_COMPL
3298 if (compl != NULL)
3299 *compl = uc->uc_compl;
3300# ifdef FEAT_EVAL
3301 if (xp != NULL)
3302 {
3303 xp->xp_arg = uc->uc_compl_arg;
3304 xp->xp_scriptID = uc->uc_scriptID;
3305 }
3306# endif
3307# endif
3308 /* Do not search for further abbreviations
3309 * if this is an exact match. */
3310 matchlen = k;
3311 if (k == len && *np == NUL)
3312 {
3313 if (full != NULL)
3314 *full = TRUE;
3315 amb_local = FALSE;
3316 break;
3317 }
3318 }
3319 }
3320 }
3321
3322 /* Stop if we found a full match or searched all. */
3323 if (j < gap->ga_len || gap == &ucmds)
3324 break;
3325 gap = &ucmds;
3326 }
3327
3328 /* Only found ambiguous matches. */
3329 if (amb_local)
3330 {
3331 if (xp != NULL)
3332 xp->xp_context = EXPAND_UNSUCCESSFUL;
3333 return NULL;
3334 }
3335
3336 /* The match we found may be followed immediately by a number. Move "p"
3337 * back to point to it. */
3338 if (found || possible)
3339 return p + (matchlen - len);
3340 return p;
3341}
3342#endif
3343
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003345static struct cmdmod
3346{
3347 char *name;
3348 int minlen;
3349 int has_count; /* :123verbose :3tab */
3350} cmdmods[] = {
3351 {"aboveleft", 3, FALSE},
3352 {"belowright", 3, FALSE},
3353 {"botright", 2, FALSE},
3354 {"browse", 3, FALSE},
3355 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003356 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003357 {"hide", 3, FALSE},
3358 {"keepalt", 5, FALSE},
3359 {"keepjumps", 5, FALSE},
3360 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003361 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003362 {"leftabove", 5, FALSE},
3363 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003364 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003365 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003366 {"rightbelow", 6, FALSE},
3367 {"sandbox", 3, FALSE},
3368 {"silent", 3, FALSE},
3369 {"tab", 3, TRUE},
3370 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003371 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003372 {"verbose", 4, TRUE},
3373 {"vertical", 4, FALSE},
3374};
3375
3376/*
3377 * Return length of a command modifier (including optional count).
3378 * Return zero when it's not a modifier.
3379 */
3380 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003381modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003382{
3383 int i, j;
3384 char_u *p = cmd;
3385
3386 if (VIM_ISDIGIT(*cmd))
3387 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003388 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003389 {
3390 for (j = 0; p[j] != NUL; ++j)
3391 if (p[j] != cmdmods[i].name[j])
3392 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003393 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003394 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003395 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003396 }
3397 return 0;
3398}
3399
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400/*
3401 * Return > 0 if an Ex command "name" exists.
3402 * Return 2 if there is an exact match.
3403 * Return 3 if there is an ambiguous match.
3404 */
3405 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003406cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407{
3408 exarg_T ea;
3409 int full = FALSE;
3410 int i;
3411 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003412 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413
3414 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003415 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 {
3417 for (j = 0; name[j] != NUL; ++j)
3418 if (name[j] != cmdmods[i].name[j])
3419 break;
3420 if (name[j] == NUL && j >= cmdmods[i].minlen)
3421 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3422 }
3423
Bram Moolenaara9587612006-05-04 21:47:50 +00003424 /* Check built-in commands and user defined commands.
3425 * For ":2match" and ":3match" we need to skip the number. */
3426 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003428 p = find_command(&ea, &full);
3429 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003431 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3432 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003433 if (*skipwhite(p) != NUL)
3434 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3436}
3437#endif
3438
3439/*
3440 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3441 * we don't need/want deleted. Maybe this could be done better if we didn't
3442 * repeat all this stuff. The only problem is that they may not stay
3443 * perfectly compatible with each other, but then the command line syntax
3444 * probably won't change that much -- webb.
3445 */
3446 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003447set_one_cmd_context(
3448 expand_T *xp,
3449 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450{
3451 char_u *p;
3452 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003453 int len = 0;
3454 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3456 int compl = EXPAND_NOTHING;
3457#endif
3458#ifdef FEAT_CMDL_COMPL
3459 int delim;
3460#endif
3461 int forceit = FALSE;
3462 int usefilter = FALSE; /* filter instead of file name */
3463
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003464 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 xp->xp_pattern = buff;
3466 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003467 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468
3469/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003470 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 */
3472 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3473 ;
3474 xp->xp_pattern = cmd;
3475
3476 if (*cmd == NUL)
3477 return NULL;
3478 if (*cmd == '"') /* ignore comment lines */
3479 {
3480 xp->xp_context = EXPAND_NOTHING;
3481 return NULL;
3482 }
3483
3484/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003485 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 */
3487 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 xp->xp_pattern = cmd;
3489 if (*cmd == NUL)
3490 return NULL;
3491 if (*cmd == '"')
3492 {
3493 xp->xp_context = EXPAND_NOTHING;
3494 return NULL;
3495 }
3496
3497 if (*cmd == '|' || *cmd == '\n')
3498 return cmd + 1; /* There's another command */
3499
3500 /*
3501 * Isolate the command and search for it in the command table.
3502 * Exceptions:
3503 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003504 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3506 */
3507 if (*cmd == 'k' && cmd[1] != 'e')
3508 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003509 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 p = cmd + 1;
3511 }
3512 else
3513 {
3514 p = cmd;
3515 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3516 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003517 /* a user command may contain digits */
3518 if (ASCII_ISUPPER(cmd[0]))
3519 while (ASCII_ISALNUM(*p) || *p == '*')
3520 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003521 /* for python 3.x: ":py3*" commands completion */
3522 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003523 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003524 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003525 while (ASCII_ISALPHA(*p) || *p == '*')
3526 ++p;
3527 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003528 /* check for non-alpha command */
3529 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3530 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003531 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003533 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 {
3535 xp->xp_context = EXPAND_UNSUCCESSFUL;
3536 return NULL;
3537 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003538 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003539 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3540 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3541 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 break;
3543
3544#ifdef FEAT_USR_CMDS
3545 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3547 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548#endif
3549 }
3550
3551 /*
3552 * If the cursor is touching the command, and it ends in an alpha-numeric
3553 * character, complete the command name.
3554 */
3555 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3556 return NULL;
3557
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003558 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 {
3560 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3561 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003562 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 p = cmd + 1;
3564 }
3565#ifdef FEAT_USR_CMDS
3566 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3567 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003568 ea.cmd = cmd;
3569 p = find_ucmd(&ea, p, NULL, xp,
3570# if defined(FEAT_CMDL_COMPL)
3571 &compl
3572# else
3573 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003575 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003576 if (p == NULL)
3577 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 }
3579#endif
3580 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003581 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 {
3583 /* Not still touching the command and it was an illegal one */
3584 xp->xp_context = EXPAND_UNSUCCESSFUL;
3585 return NULL;
3586 }
3587
3588 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3589
3590 if (*p == '!') /* forced commands */
3591 {
3592 forceit = TRUE;
3593 ++p;
3594 }
3595
3596/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003597 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003599 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003600 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601
3602 arg = skipwhite(p);
3603
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003604 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 {
3606 if (*arg == '>') /* append */
3607 {
3608 if (*++arg == '>')
3609 ++arg;
3610 arg = skipwhite(arg);
3611 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003612 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 {
3614 ++arg;
3615 usefilter = TRUE;
3616 }
3617 }
3618
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003619 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 {
3621 usefilter = forceit; /* :r! filter if forced */
3622 if (*arg == '!') /* :r !filter */
3623 {
3624 ++arg;
3625 usefilter = TRUE;
3626 }
3627 }
3628
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003629 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 {
3631 while (*arg == *cmd) /* allow any number of '>' or '<' */
3632 ++arg;
3633 arg = skipwhite(arg);
3634 }
3635
3636 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003637 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 {
3639 /* Check if we're in the +command */
3640 p = arg + 1;
3641 arg = skip_cmd_arg(arg, FALSE);
3642
3643 /* Still touching the command after '+'? */
3644 if (*arg == NUL)
3645 return p;
3646
3647 /* Skip space(s) after +command to get to the real argument */
3648 arg = skipwhite(arg);
3649 }
3650
3651 /*
3652 * Check for '|' to separate commands and '"' to start comments.
3653 * Don't do this for ":read !cmd" and ":write !cmd".
3654 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003655 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 {
3657 p = arg;
3658 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003659 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 p += 2;
3661 while (*p)
3662 {
3663 if (*p == Ctrl_V)
3664 {
3665 if (p[1] != NUL)
3666 ++p;
3667 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003668 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 || *p == '|' || *p == '\n')
3670 {
3671 if (*(p - 1) != '\\')
3672 {
3673 if (*p == '|' || *p == '\n')
3674 return p + 1;
3675 return NULL; /* It's a comment */
3676 }
3677 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003678 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 }
3680 }
3681
3682 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003683 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 vim_strchr((char_u *)"|\"", *arg) == NULL)
3685 return NULL;
3686
3687 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003688 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003690 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003691 while (*p && p < buff + len)
3692 {
3693 if (*p == ' ' || *p == TAB)
3694 {
3695 /* argument starts after a space */
3696 xp->xp_pattern = ++p;
3697 }
3698 else
3699 {
3700 if (*p == '\\' && *(p + 1) != NUL)
3701 ++p; /* skip over escaped character */
3702 mb_ptr_adv(p);
3703 }
3704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003706 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003708 int c;
3709 int in_quote = FALSE;
3710 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711
3712 /*
3713 * Allow spaces within back-quotes to count as part of the argument
3714 * being expanded.
3715 */
3716 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003717 p = xp->xp_pattern;
3718 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003720#ifdef FEAT_MBYTE
3721 if (has_mbyte)
3722 c = mb_ptr2char(p);
3723 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003725 c = *p;
3726 if (c == '\\' && p[1] != NUL)
3727 ++p;
3728 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 {
3730 if (!in_quote)
3731 {
3732 xp->xp_pattern = p;
3733 bow = p + 1;
3734 }
3735 in_quote = !in_quote;
3736 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003737 /* An argument can contain just about everything, except
3738 * characters that end the command and white space. */
3739 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003740#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003741 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003742#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003743 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003744 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003745 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003746 while (*p != NUL)
3747 {
3748#ifdef FEAT_MBYTE
3749 if (has_mbyte)
3750 c = mb_ptr2char(p);
3751 else
3752#endif
3753 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003754 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003755 break;
3756#ifdef FEAT_MBYTE
3757 if (has_mbyte)
3758 len = (*mb_ptr2len)(p);
3759 else
3760#endif
3761 len = 1;
3762 mb_ptr_adv(p);
3763 }
3764 if (in_quote)
3765 bow = p;
3766 else
3767 xp->xp_pattern = p;
3768 p -= len;
3769 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003770 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 }
3772
3773 /*
3774 * If we are still inside the quotes, and we passed a space, just
3775 * expand from there.
3776 */
3777 if (bow != NULL && in_quote)
3778 xp->xp_pattern = bow;
3779 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003780
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003781 /* For a shell command more chars need to be escaped. */
3782 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003783 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003784#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003785 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003786#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003787 /* When still after the command name expand executables. */
3788 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003789 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791
3792 /* Check for environment variable */
3793 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003794#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 || *xp->xp_pattern == '%'
3796#endif
3797 )
3798 {
3799 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3800 if (!vim_isIDc(*p))
3801 break;
3802 if (*p == NUL)
3803 {
3804 xp->xp_context = EXPAND_ENV_VARS;
3805 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003806#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3807 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003808 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003809 compl = EXPAND_ENV_VARS;
3810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 }
3812 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003813#if defined(FEAT_CMDL_COMPL)
3814 /* Check for user names */
3815 if (*xp->xp_pattern == '~')
3816 {
3817 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3818 ;
3819 /* Complete ~user only if it partially matches a user name.
3820 * A full match ~user<Tab> will be replaced by user's home
3821 * directory i.e. something like ~user<Tab> -> /home/user/ */
3822 if (*p == NUL && p > xp->xp_pattern + 1
3823 && match_user(xp->xp_pattern + 1) == 1)
3824 {
3825 xp->xp_context = EXPAND_USER;
3826 ++xp->xp_pattern;
3827 }
3828 }
3829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 }
3831
3832/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003833 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003835 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003837 case CMD_find:
3838 case CMD_sfind:
3839 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003840 if (xp->xp_context == EXPAND_FILES)
3841 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003842 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 case CMD_cd:
3844 case CMD_chdir:
3845 case CMD_lcd:
3846 case CMD_lchdir:
3847 if (xp->xp_context == EXPAND_FILES)
3848 xp->xp_context = EXPAND_DIRECTORIES;
3849 break;
3850 case CMD_help:
3851 xp->xp_context = EXPAND_HELP;
3852 xp->xp_pattern = arg;
3853 break;
3854
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003855 /* Command modifiers: return the argument.
3856 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003858 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 case CMD_belowright:
3860 case CMD_botright:
3861 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003862 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003863 case CMD_cdo:
3864 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003866 case CMD_debug:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003867 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 case CMD_folddoclosed:
3869 case CMD_folddoopen:
3870 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003871 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 case CMD_keepjumps:
3873 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003874 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003875 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003877 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003879 case CMD_noautocmd:
3880 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003882 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003884 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003885 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 case CMD_topleft:
3887 case CMD_verbose:
3888 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003889 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 return arg;
3891
Bram Moolenaar4f688582007-07-24 12:34:30 +00003892#ifdef FEAT_CMDL_COMPL
3893# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 case CMD_match:
3895 if (*arg == NUL || !ends_excmd(*arg))
3896 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003897 /* also complete "None" */
3898 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 arg = skipwhite(skiptowhite(arg));
3900 if (*arg != NUL)
3901 {
3902 xp->xp_context = EXPAND_NOTHING;
3903 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3904 }
3905 }
3906 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003907# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909/*
3910 * All completion for the +cmdline_compl feature goes here.
3911 */
3912
3913# ifdef FEAT_USR_CMDS
3914 case CMD_command:
3915 /* Check for attributes */
3916 while (*arg == '-')
3917 {
3918 arg++; /* Skip "-" */
3919 p = skiptowhite(arg);
3920 if (*p == NUL)
3921 {
3922 /* Cursor is still in the attribute */
3923 p = vim_strchr(arg, '=');
3924 if (p == NULL)
3925 {
3926 /* No "=", so complete attribute names */
3927 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3928 xp->xp_pattern = arg;
3929 return NULL;
3930 }
3931
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003932 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 * their arguments as well.
3934 */
3935 if (STRNICMP(arg, "complete", p - arg) == 0)
3936 {
3937 xp->xp_context = EXPAND_USER_COMPLETE;
3938 xp->xp_pattern = p + 1;
3939 return NULL;
3940 }
3941 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3942 {
3943 xp->xp_context = EXPAND_USER_NARGS;
3944 xp->xp_pattern = p + 1;
3945 return NULL;
3946 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003947 else if (STRNICMP(arg, "addr", p - arg) == 0)
3948 {
3949 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3950 xp->xp_pattern = p + 1;
3951 return NULL;
3952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 return NULL;
3954 }
3955 arg = skipwhite(p);
3956 }
3957
3958 /* After the attributes comes the new command name */
3959 p = skiptowhite(arg);
3960 if (*p == NUL)
3961 {
3962 xp->xp_context = EXPAND_USER_COMMANDS;
3963 xp->xp_pattern = arg;
3964 break;
3965 }
3966
3967 /* And finally comes a normal command */
3968 return skipwhite(p);
3969
3970 case CMD_delcommand:
3971 xp->xp_context = EXPAND_USER_COMMANDS;
3972 xp->xp_pattern = arg;
3973 break;
3974# endif
3975
3976 case CMD_global:
3977 case CMD_vglobal:
3978 delim = *arg; /* get the delimiter */
3979 if (delim)
3980 ++arg; /* skip delimiter if there is one */
3981
3982 while (arg[0] != NUL && arg[0] != delim)
3983 {
3984 if (arg[0] == '\\' && arg[1] != NUL)
3985 ++arg;
3986 ++arg;
3987 }
3988 if (arg[0] != NUL)
3989 return arg + 1;
3990 break;
3991 case CMD_and:
3992 case CMD_substitute:
3993 delim = *arg;
3994 if (delim)
3995 {
3996 /* skip "from" part */
3997 ++arg;
3998 arg = skip_regexp(arg, delim, p_magic, NULL);
3999 }
4000 /* skip "to" part */
4001 while (arg[0] != NUL && arg[0] != delim)
4002 {
4003 if (arg[0] == '\\' && arg[1] != NUL)
4004 ++arg;
4005 ++arg;
4006 }
4007 if (arg[0] != NUL) /* skip delimiter */
4008 ++arg;
4009 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
4010 ++arg;
4011 if (arg[0] != NUL)
4012 return arg;
4013 break;
4014 case CMD_isearch:
4015 case CMD_dsearch:
4016 case CMD_ilist:
4017 case CMD_dlist:
4018 case CMD_ijump:
4019 case CMD_psearch:
4020 case CMD_djump:
4021 case CMD_isplit:
4022 case CMD_dsplit:
4023 arg = skipwhite(skipdigits(arg)); /* skip count */
4024 if (*arg == '/') /* Match regexp, not just whole words */
4025 {
4026 for (++arg; *arg && *arg != '/'; arg++)
4027 if (*arg == '\\' && arg[1] != NUL)
4028 arg++;
4029 if (*arg)
4030 {
4031 arg = skipwhite(arg + 1);
4032
4033 /* Check for trailing illegal characters */
4034 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4035 xp->xp_context = EXPAND_NOTHING;
4036 else
4037 return arg;
4038 }
4039 }
4040 break;
4041#ifdef FEAT_AUTOCMD
4042 case CMD_autocmd:
4043 return set_context_in_autocmd(xp, arg, FALSE);
4044
4045 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004046 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 return set_context_in_autocmd(xp, arg, TRUE);
4048#endif
4049 case CMD_set:
4050 set_context_in_set_cmd(xp, arg, 0);
4051 break;
4052 case CMD_setglobal:
4053 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4054 break;
4055 case CMD_setlocal:
4056 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4057 break;
4058 case CMD_tag:
4059 case CMD_stag:
4060 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004061 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 case CMD_tselect:
4063 case CMD_stselect:
4064 case CMD_ptselect:
4065 case CMD_tjump:
4066 case CMD_stjump:
4067 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004068 if (*p_wop != NUL)
4069 xp->xp_context = EXPAND_TAGS_LISTFILES;
4070 else
4071 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 xp->xp_pattern = arg;
4073 break;
4074 case CMD_augroup:
4075 xp->xp_context = EXPAND_AUGROUP;
4076 xp->xp_pattern = arg;
4077 break;
4078#ifdef FEAT_SYN_HL
4079 case CMD_syntax:
4080 set_context_in_syntax_cmd(xp, arg);
4081 break;
4082#endif
4083#ifdef FEAT_EVAL
4084 case CMD_let:
4085 case CMD_if:
4086 case CMD_elseif:
4087 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004088 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 case CMD_echo:
4090 case CMD_echon:
4091 case CMD_execute:
4092 case CMD_echomsg:
4093 case CMD_echoerr:
4094 case CMD_call:
4095 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004096 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 break;
4098
4099 case CMD_unlet:
4100 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4101 arg = xp->xp_pattern + 1;
4102 xp->xp_context = EXPAND_USER_VARS;
4103 xp->xp_pattern = arg;
4104 break;
4105
4106 case CMD_function:
4107 case CMD_delfunction:
4108 xp->xp_context = EXPAND_USER_FUNC;
4109 xp->xp_pattern = arg;
4110 break;
4111
4112 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004113 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 break;
4115#endif
4116 case CMD_highlight:
4117 set_context_in_highlight_cmd(xp, arg);
4118 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004119#ifdef FEAT_CSCOPE
4120 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004121 case CMD_lcscope:
4122 case CMD_scscope:
4123 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004124 break;
4125#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004126#ifdef FEAT_SIGNS
4127 case CMD_sign:
4128 set_context_in_sign_cmd(xp, arg);
4129 break;
4130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131#ifdef FEAT_LISTCMDS
4132 case CMD_bdelete:
4133 case CMD_bwipeout:
4134 case CMD_bunload:
4135 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4136 arg = xp->xp_pattern + 1;
4137 /*FALLTHROUGH*/
4138 case CMD_buffer:
4139 case CMD_sbuffer:
4140 case CMD_checktime:
4141 xp->xp_context = EXPAND_BUFFERS;
4142 xp->xp_pattern = arg;
4143 break;
4144#endif
4145#ifdef FEAT_USR_CMDS
4146 case CMD_USER:
4147 case CMD_USER_BUF:
4148 if (compl != EXPAND_NOTHING)
4149 {
4150 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004151 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 {
4153# ifdef FEAT_MENU
4154 if (compl == EXPAND_MENUS)
4155 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4156# endif
4157 if (compl == EXPAND_COMMANDS)
4158 return arg;
4159 if (compl == EXPAND_MAPPINGS)
4160 return set_context_in_map_cmd(xp, (char_u *)"map",
4161 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004162 /* Find start of last argument. */
4163 p = arg;
4164 while (*p)
4165 {
4166 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004167 /* argument starts after a space */
4168 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004169 else if (*p == '\\' && *(p + 1) != NUL)
4170 ++p; /* skip over escaped character */
4171 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 xp->xp_pattern = arg;
4174 }
4175 xp->xp_context = compl;
4176 }
4177 break;
4178#endif
4179 case CMD_map: case CMD_noremap:
4180 case CMD_nmap: case CMD_nnoremap:
4181 case CMD_vmap: case CMD_vnoremap:
4182 case CMD_omap: case CMD_onoremap:
4183 case CMD_imap: case CMD_inoremap:
4184 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004185 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004186 case CMD_smap: case CMD_snoremap:
4187 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004189 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 case CMD_unmap:
4191 case CMD_nunmap:
4192 case CMD_vunmap:
4193 case CMD_ounmap:
4194 case CMD_iunmap:
4195 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004196 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004197 case CMD_sunmap:
4198 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004200 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 case CMD_abbreviate: case CMD_noreabbrev:
4202 case CMD_cabbrev: case CMD_cnoreabbrev:
4203 case CMD_iabbrev: case CMD_inoreabbrev:
4204 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004205 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 case CMD_unabbreviate:
4207 case CMD_cunabbrev:
4208 case CMD_iunabbrev:
4209 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004210 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211#ifdef FEAT_MENU
4212 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4213 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4214 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4215 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4216 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4217 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4218 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4219 case CMD_tmenu: case CMD_tunmenu:
4220 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4221 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4222#endif
4223
4224 case CMD_colorscheme:
4225 xp->xp_context = EXPAND_COLORS;
4226 xp->xp_pattern = arg;
4227 break;
4228
4229 case CMD_compiler:
4230 xp->xp_context = EXPAND_COMPILER;
4231 xp->xp_pattern = arg;
4232 break;
4233
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004234 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004235 xp->xp_context = EXPAND_OWNSYNTAX;
4236 xp->xp_pattern = arg;
4237 break;
4238
4239 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004240 xp->xp_context = EXPAND_FILETYPE;
4241 xp->xp_pattern = arg;
4242 break;
4243
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004244 case CMD_packadd:
4245 xp->xp_context = EXPAND_PACKADD;
4246 xp->xp_pattern = arg;
4247 break;
4248
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4250 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4251 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004252 p = skiptowhite(arg);
4253 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 {
4255 xp->xp_context = EXPAND_LANGUAGE;
4256 xp->xp_pattern = arg;
4257 }
4258 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004259 {
4260 if ( STRNCMP(arg, "messages", p - arg) == 0
4261 || STRNCMP(arg, "ctype", p - arg) == 0
4262 || STRNCMP(arg, "time", p - arg) == 0)
4263 {
4264 xp->xp_context = EXPAND_LOCALES;
4265 xp->xp_pattern = skipwhite(p);
4266 }
4267 else
4268 xp->xp_context = EXPAND_NOTHING;
4269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 break;
4271#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004272#if defined(FEAT_PROFILE)
4273 case CMD_profile:
4274 set_context_in_profile_cmd(xp, arg);
4275 break;
4276#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004277 case CMD_behave:
4278 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004279 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004280 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004282#if defined(FEAT_CMDHIST)
4283 case CMD_history:
4284 xp->xp_context = EXPAND_HISTORY;
4285 xp->xp_pattern = arg;
4286 break;
4287#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004288#if defined(FEAT_PROFILE)
4289 case CMD_syntime:
4290 xp->xp_context = EXPAND_SYNTIME;
4291 xp->xp_pattern = arg;
4292 break;
4293#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004294
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295#endif /* FEAT_CMDL_COMPL */
4296
4297 default:
4298 break;
4299 }
4300 return NULL;
4301}
4302
4303/*
4304 * skip a range specifier of the form: addr [,addr] [;addr] ..
4305 *
4306 * Backslashed delimiters after / or ? will be skipped, and commands will
4307 * not be expanded between /'s and ?'s or after "'".
4308 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004309 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 * Returns the "cmd" pointer advanced to beyond the range.
4311 */
4312 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004313skip_range(
4314 char_u *cmd,
4315 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004317 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318
Bram Moolenaardf177f62005-02-22 08:39:57 +00004319 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 {
4321 if (*cmd == '\'')
4322 {
4323 if (*++cmd == NUL && ctx != NULL)
4324 *ctx = EXPAND_NOTHING;
4325 }
4326 else if (*cmd == '/' || *cmd == '?')
4327 {
4328 delim = *cmd++;
4329 while (*cmd != NUL && *cmd != delim)
4330 if (*cmd++ == '\\' && *cmd != NUL)
4331 ++cmd;
4332 if (*cmd == NUL && ctx != NULL)
4333 *ctx = EXPAND_NOTHING;
4334 }
4335 if (*cmd != NUL)
4336 ++cmd;
4337 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004338
4339 /* Skip ":" and white space. */
4340 while (*cmd == ':')
4341 cmd = skipwhite(cmd + 1);
4342
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 return cmd;
4344}
4345
4346/*
4347 * get a single EX address
4348 *
4349 * Set ptr to the next character after the part that was interpreted.
4350 * Set ptr to NULL when an error is encountered.
4351 *
4352 * Return MAXLNUM when no Ex address was found.
4353 */
4354 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004355get_address(
4356 exarg_T *eap UNUSED,
4357 char_u **ptr,
4358 int addr_type, /* flag: one of ADDR_LINES, ... */
4359 int skip, /* only skip the address, don't use it */
4360 int to_other_file) /* flag: may jump to other file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361{
4362 int c;
4363 int i;
4364 long n;
4365 char_u *cmd;
4366 pos_T pos;
4367 pos_T *fp;
4368 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004369 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370
4371 cmd = skipwhite(*ptr);
4372 lnum = MAXLNUM;
4373 do
4374 {
4375 switch (*cmd)
4376 {
4377 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004378 ++cmd;
4379 switch (addr_type)
4380 {
4381 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 lnum = curwin->w_cursor.lnum;
4383 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004384 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004385 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004386 break;
4387 case ADDR_ARGUMENTS:
4388 lnum = curwin->w_arg_idx + 1;
4389 break;
4390 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004391 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004392 lnum = curbuf->b_fnum;
4393 break;
4394 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004395 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004396 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004397#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004398 case ADDR_QUICKFIX:
4399 lnum = qf_get_cur_valid_idx(eap);
4400 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004401#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004402 }
4403 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404
4405 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004406 ++cmd;
4407 switch (addr_type)
4408 {
4409 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 lnum = curbuf->b_ml.ml_line_count;
4411 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004412 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004413 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004414 break;
4415 case ADDR_ARGUMENTS:
4416 lnum = ARGCOUNT;
4417 break;
4418 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004419 buf = lastbuf;
4420 while (buf->b_ml.ml_mfp == NULL)
4421 {
4422 if (buf->b_prev == NULL)
4423 break;
4424 buf = buf->b_prev;
4425 }
4426 lnum = buf->b_fnum;
4427 break;
4428 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004429 lnum = lastbuf->b_fnum;
4430 break;
4431 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004432 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004433 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004434#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004435 case ADDR_QUICKFIX:
4436 lnum = qf_get_size(eap);
4437 if (lnum == 0)
4438 lnum = 1;
4439 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004440#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004441 }
4442 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443
4444 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004445 if (*++cmd == NUL)
4446 {
4447 cmd = NULL;
4448 goto error;
4449 }
4450 if (addr_type != ADDR_LINES)
4451 {
4452 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004453 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004454 goto error;
4455 }
4456 if (skip)
4457 ++cmd;
4458 else
4459 {
4460 /* Only accept a mark in another file when it is
4461 * used by itself: ":'M". */
4462 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4463 ++cmd;
4464 if (fp == (pos_T *)-1)
4465 /* Jumped to another file. */
4466 lnum = curwin->w_cursor.lnum;
4467 else
4468 {
4469 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 {
4471 cmd = NULL;
4472 goto error;
4473 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004474 lnum = fp->lnum;
4475 }
4476 }
4477 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478
4479 case '/':
4480 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004481 c = *cmd++;
4482 if (addr_type != ADDR_LINES)
4483 {
4484 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004485 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004486 goto error;
4487 }
4488 if (skip) /* skip "/pat/" */
4489 {
4490 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4491 if (*cmd == c)
4492 ++cmd;
4493 }
4494 else
4495 {
4496 pos = curwin->w_cursor; /* save curwin->w_cursor */
4497 /*
4498 * When '/' or '?' follows another address, start
4499 * from there.
4500 */
4501 if (lnum != MAXLNUM)
4502 curwin->w_cursor.lnum = lnum;
4503 /*
4504 * Start a forward search at the end of the line.
4505 * Start a backward search at the start of the line.
4506 * This makes sure we never match in the current
4507 * line, and can match anywhere in the
4508 * next/previous line.
4509 */
4510 if (c == '/')
4511 curwin->w_cursor.col = MAXCOL;
4512 else
4513 curwin->w_cursor.col = 0;
4514 searchcmdlen = 0;
4515 if (!do_search(NULL, c, cmd, 1L,
4516 SEARCH_HIS | SEARCH_MSG, NULL))
4517 {
4518 curwin->w_cursor = pos;
4519 cmd = NULL;
4520 goto error;
4521 }
4522 lnum = curwin->w_cursor.lnum;
4523 curwin->w_cursor = pos;
4524 /* adjust command string pointer */
4525 cmd += searchcmdlen;
4526 }
4527 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528
4529 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004530 ++cmd;
4531 if (addr_type != ADDR_LINES)
4532 {
4533 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004534 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004535 goto error;
4536 }
4537 if (*cmd == '&')
4538 i = RE_SUBST;
4539 else if (*cmd == '?' || *cmd == '/')
4540 i = RE_SEARCH;
4541 else
4542 {
4543 EMSG(_(e_backslash));
4544 cmd = NULL;
4545 goto error;
4546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004548 if (!skip)
4549 {
4550 /*
4551 * When search follows another address, start from
4552 * there.
4553 */
4554 if (lnum != MAXLNUM)
4555 pos.lnum = lnum;
4556 else
4557 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004559 /*
4560 * Start the search just like for the above
4561 * do_search().
4562 */
4563 if (*cmd != '?')
4564 pos.col = MAXCOL;
4565 else
4566 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004567#ifdef FEAT_VIRTUALEDIT
4568 pos.coladd = 0;
4569#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004570 if (searchit(curwin, curbuf, &pos,
4571 *cmd == '?' ? BACKWARD : FORWARD,
4572 (char_u *)"", 1L, SEARCH_MSG,
4573 i, (linenr_T)0, NULL) != FAIL)
4574 lnum = pos.lnum;
4575 else
4576 {
4577 cmd = NULL;
4578 goto error;
4579 }
4580 }
4581 ++cmd;
4582 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583
4584 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004585 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4586 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 }
4588
4589 for (;;)
4590 {
4591 cmd = skipwhite(cmd);
4592 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4593 break;
4594
4595 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004596 {
4597 switch (addr_type)
4598 {
4599 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004600 /* "+1" is same as ".+1" */
4601 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004602 break;
4603 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004604 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004605 break;
4606 case ADDR_ARGUMENTS:
4607 lnum = curwin->w_arg_idx + 1;
4608 break;
4609 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004610 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004611 lnum = curbuf->b_fnum;
4612 break;
4613 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004614 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004615 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004616#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004617 case ADDR_QUICKFIX:
4618 lnum = qf_get_cur_valid_idx(eap);
4619 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004620#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004621 }
4622 }
4623
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 if (VIM_ISDIGIT(*cmd))
4625 i = '+'; /* "number" is same as "+number" */
4626 else
4627 i = *cmd++;
4628 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4629 n = 1;
4630 else
4631 n = getdigits(&cmd);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004632 if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004633 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004634 lnum = compute_buffer_local_count(
4635 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004636 else if (i == '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 lnum -= n;
4638 else
4639 lnum += n;
4640 }
4641 } while (*cmd == '/' || *cmd == '?');
4642
4643error:
4644 *ptr = cmd;
4645 return lnum;
4646}
4647
4648/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004649 * Get flags from an Ex command argument.
4650 */
4651 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004652get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004653{
4654 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4655 {
4656 if (*eap->arg == 'l')
4657 eap->flags |= EXFLAG_LIST;
4658 else if (*eap->arg == 'p')
4659 eap->flags |= EXFLAG_PRINT;
4660 else
4661 eap->flags |= EXFLAG_NR;
4662 eap->arg = skipwhite(eap->arg + 1);
4663 }
4664}
4665
4666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 * Function called for command which is Not Implemented. NI!
4668 */
4669 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004670ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671{
4672 if (!eap->skip)
4673 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4674}
4675
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004676#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677/*
4678 * Function called for script command which is Not Implemented. NI!
4679 * Skips over ":perl <<EOF" constructs.
4680 */
4681 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004682ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683{
4684 if (!eap->skip)
4685 ex_ni(eap);
4686 else
4687 vim_free(script_get(eap, eap->arg));
4688}
4689#endif
4690
4691/*
4692 * Check range in Ex command for validity.
4693 * Return NULL when valid, error message when invalid.
4694 */
4695 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004696invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004698 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 if ( eap->line1 < 0
4700 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004701 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004703
4704 if (eap->argt & RANGE)
4705 {
4706 switch(eap->addr_type)
4707 {
4708 case ADDR_LINES:
4709 if (!(eap->argt & NOTADR)
4710 && eap->line2 > curbuf->b_ml.ml_line_count
4711#ifdef FEAT_DIFF
4712 + (eap->cmdidx == CMD_diffget)
4713#endif
4714 )
4715 return (char_u *)_(e_invrange);
4716 break;
4717 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004718 /* add 1 if ARGCOUNT is 0 */
4719 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004720 return (char_u *)_(e_invrange);
4721 break;
4722 case ADDR_BUFFERS:
4723 if (eap->line1 < firstbuf->b_fnum
4724 || eap->line2 > lastbuf->b_fnum)
4725 return (char_u *)_(e_invrange);
4726 break;
4727 case ADDR_LOADED_BUFFERS:
4728 buf = firstbuf;
4729 while (buf->b_ml.ml_mfp == NULL)
4730 {
4731 if (buf->b_next == NULL)
4732 return (char_u *)_(e_invrange);
4733 buf = buf->b_next;
4734 }
4735 if (eap->line1 < buf->b_fnum)
4736 return (char_u *)_(e_invrange);
4737 buf = lastbuf;
4738 while (buf->b_ml.ml_mfp == NULL)
4739 {
4740 if (buf->b_prev == NULL)
4741 return (char_u *)_(e_invrange);
4742 buf = buf->b_prev;
4743 }
4744 if (eap->line2 > buf->b_fnum)
4745 return (char_u *)_(e_invrange);
4746 break;
4747 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004748 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004749 return (char_u *)_(e_invrange);
4750 break;
4751 case ADDR_TABS:
4752 if (eap->line2 > LAST_TAB_NR)
4753 return (char_u *)_(e_invrange);
4754 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004755#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004756 case ADDR_QUICKFIX:
4757 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4758 return (char_u *)_(e_invrange);
4759 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004760#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004761 }
4762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 return NULL;
4764}
4765
4766/*
4767 * Correct the range for zero line number, if required.
4768 */
4769 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004770correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771{
4772 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4773 {
4774 if (eap->line1 == 0)
4775 eap->line1 = 1;
4776 if (eap->line2 == 0)
4777 eap->line2 = 1;
4778 }
4779}
4780
Bram Moolenaar748bf032005-02-02 23:04:36 +00004781#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004782static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004783
4784/*
4785 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4786 * pattern. Otherwise return eap->arg.
4787 */
4788 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004789skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004790{
4791 char_u *p = eap->arg;
4792
Bram Moolenaara37420f2006-02-04 22:37:47 +00004793 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4794 || eap->cmdidx == CMD_vimgrepadd
4795 || eap->cmdidx == CMD_lvimgrepadd
4796 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004797 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004798 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004799 if (p == NULL)
4800 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004801 }
4802 return p;
4803}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004804
4805/*
4806 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4807 * in the command line, so that things like % get expanded.
4808 */
4809 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004810replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004811{
4812 char_u *new_cmdline;
4813 char_u *program;
4814 char_u *pos;
4815 char_u *ptr;
4816 int len;
4817 int i;
4818
4819 /*
4820 * Don't do it when ":vimgrep" is used for ":grep".
4821 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004822 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4823 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4824 || eap->cmdidx == CMD_grepadd
4825 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004826 && !grep_internal(eap->cmdidx))
4827 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004828 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4829 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004830 {
4831 if (*curbuf->b_p_gp == NUL)
4832 program = p_gp;
4833 else
4834 program = curbuf->b_p_gp;
4835 }
4836 else
4837 {
4838 if (*curbuf->b_p_mp == NUL)
4839 program = p_mp;
4840 else
4841 program = curbuf->b_p_mp;
4842 }
4843
4844 p = skipwhite(p);
4845
4846 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4847 {
4848 /* replace $* by given arguments */
4849 i = 1;
4850 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4851 ++i;
4852 len = (int)STRLEN(p);
4853 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4854 if (new_cmdline == NULL)
4855 return NULL; /* out of memory */
4856 ptr = new_cmdline;
4857 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4858 {
4859 i = (int)(pos - program);
4860 STRNCPY(ptr, program, i);
4861 STRCPY(ptr += i, p);
4862 ptr += len;
4863 program = pos + 2;
4864 }
4865 STRCPY(ptr, program);
4866 }
4867 else
4868 {
4869 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4870 if (new_cmdline == NULL)
4871 return NULL; /* out of memory */
4872 STRCPY(new_cmdline, program);
4873 STRCAT(new_cmdline, " ");
4874 STRCAT(new_cmdline, p);
4875 }
4876 msg_make(p);
4877
4878 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4879 vim_free(*cmdlinep);
4880 *cmdlinep = new_cmdline;
4881 p = new_cmdline;
4882 }
4883 return p;
4884}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004885#endif
4886
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887/*
4888 * Expand file name in Ex command argument.
4889 * Return FAIL for failure, OK otherwise.
4890 */
4891 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004892expand_filename(
4893 exarg_T *eap,
4894 char_u **cmdlinep,
4895 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896{
4897 int has_wildcards; /* need to expand wildcards */
4898 char_u *repl;
4899 int srclen;
4900 char_u *p;
4901 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004902 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903
Bram Moolenaar748bf032005-02-02 23:04:36 +00004904#ifdef FEAT_QUICKFIX
4905 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4906 p = skip_grep_pat(eap);
4907#else
4908 p = eap->arg;
4909#endif
4910
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 /*
4912 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4913 * the file name contains a wildcard it should not cause expanding.
4914 * (it will be expanded anyway if there is a wildcard before replacing).
4915 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004916 has_wildcards = mch_has_wildcard(p);
4917 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004919#ifdef FEAT_EVAL
4920 /* Skip over `=expr`, wildcards in it are not expanded. */
4921 if (p[0] == '`' && p[1] == '=')
4922 {
4923 p += 2;
4924 (void)skip_expr(&p);
4925 if (*p == '`')
4926 ++p;
4927 continue;
4928 }
4929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 /*
4931 * Quick check if this cannot be the start of a special string.
4932 * Also removes backslash before '%', '#' and '<'.
4933 */
4934 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4935 {
4936 ++p;
4937 continue;
4938 }
4939
4940 /*
4941 * Try to find a match at this position.
4942 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004943 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4944 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 if (*errormsgp != NULL) /* error detected */
4946 return FAIL;
4947 if (repl == NULL) /* no match found */
4948 {
4949 p += srclen;
4950 continue;
4951 }
4952
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004953 /* Wildcards won't be expanded below, the replacement is taken
4954 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4955 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4956 {
4957 char_u *l = repl;
4958
4959 repl = expand_env_save(repl);
4960 vim_free(l);
4961 }
4962
Bram Moolenaar63b92542007-03-27 14:57:09 +00004963 /* Need to escape white space et al. with a backslash.
4964 * Don't do this for:
4965 * - replacement that already has been escaped: "##"
4966 * - shell commands (may have to use quotes instead).
4967 * - non-unix systems when there is a single argument (spaces don't
4968 * separate arguments then).
4969 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004971 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 && eap->cmdidx != CMD_bang
4973 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004974 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004976 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004978 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979#ifndef UNIX
4980 && !(eap->argt & NOSPC)
4981#endif
4982 )
4983 {
4984 char_u *l;
4985#ifdef BACKSLASH_IN_FILENAME
4986 /* Don't escape a backslash here, because rem_backslash() doesn't
4987 * remove it later. */
4988 static char_u *nobslash = (char_u *)" \t\"|";
4989# define ESCAPE_CHARS nobslash
4990#else
4991# define ESCAPE_CHARS escape_chars
4992#endif
4993
4994 for (l = repl; *l; ++l)
4995 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4996 {
4997 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4998 if (l != NULL)
4999 {
5000 vim_free(repl);
5001 repl = l;
5002 }
5003 break;
5004 }
5005 }
5006
5007 /* For a shell command a '!' must be escaped. */
5008 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005009 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 {
5011 char_u *l;
5012
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005013 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 if (l != NULL)
5015 {
5016 vim_free(repl);
5017 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 }
5019 }
5020
5021 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5022 vim_free(repl);
5023 if (p == NULL)
5024 return FAIL;
5025 }
5026
5027 /*
5028 * One file argument: Expand wildcards.
5029 * Don't do this with ":r !command" or ":w !command".
5030 */
5031 if ((eap->argt & NOSPC) && !eap->usefilter)
5032 {
5033 /*
5034 * May do this twice:
5035 * 1. Replace environment variables.
5036 * 2. Replace any other wildcards, remove backslashes.
5037 */
5038 for (n = 1; n <= 2; ++n)
5039 {
5040 if (n == 2)
5041 {
5042#ifdef UNIX
5043 /*
5044 * Only for Unix we check for more than one file name.
5045 * For other systems spaces are considered to be part
5046 * of the file name.
5047 * Only check here if there is no wildcard, otherwise
5048 * ExpandOne() will check for errors. This allows
5049 * ":e `ls ve*.c`" on Unix.
5050 */
5051 if (!has_wildcards)
5052 for (p = eap->arg; *p; ++p)
5053 {
5054 /* skip escaped characters */
5055 if (p[1] && (*p == '\\' || *p == Ctrl_V))
5056 ++p;
5057 else if (vim_iswhite(*p))
5058 {
5059 *errormsgp = (char_u *)_("E172: Only one file name allowed");
5060 return FAIL;
5061 }
5062 }
5063#endif
5064
5065 /*
5066 * Halve the number of backslashes (this is Vi compatible).
5067 * For Unix and OS/2, when wildcards are expanded, this is
5068 * done by ExpandOne() below.
5069 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005070#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 if (!has_wildcards)
5072#endif
5073 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 }
5075
5076 if (has_wildcards)
5077 {
5078 if (n == 1)
5079 {
5080 /*
5081 * First loop: May expand environment variables. This
5082 * can be done much faster with expand_env() than with
5083 * something else (e.g., calling a shell).
5084 * After expanding environment variables, check again
5085 * if there are still wildcards present.
5086 */
5087 if (vim_strchr(eap->arg, '$') != NULL
5088 || vim_strchr(eap->arg, '~') != NULL)
5089 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005090 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005091 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 has_wildcards = mch_has_wildcard(NameBuff);
5093 p = NameBuff;
5094 }
5095 else
5096 p = NULL;
5097 }
5098 else /* n == 2 */
5099 {
5100 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005101 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102
5103 ExpandInit(&xpc);
5104 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005105 if (p_wic)
5106 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005108 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 if (p == NULL)
5110 return FAIL;
5111 }
5112 if (p != NULL)
5113 {
5114 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5115 p, cmdlinep);
5116 if (n == 2) /* p came from ExpandOne() */
5117 vim_free(p);
5118 }
5119 }
5120 }
5121 }
5122 return OK;
5123}
5124
5125/*
5126 * Replace part of the command line, keeping eap->cmd, eap->arg and
5127 * eap->nextcmd correct.
5128 * "src" points to the part that is to be replaced, of length "srclen".
5129 * "repl" is the replacement string.
5130 * Returns a pointer to the character after the replaced string.
5131 * Returns NULL for failure.
5132 */
5133 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005134repl_cmdline(
5135 exarg_T *eap,
5136 char_u *src,
5137 int srclen,
5138 char_u *repl,
5139 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140{
5141 int len;
5142 int i;
5143 char_u *new_cmdline;
5144
5145 /*
5146 * The new command line is build in new_cmdline[].
5147 * First allocate it.
5148 * Careful: a "+cmd" argument may have been NUL terminated.
5149 */
5150 len = (int)STRLEN(repl);
5151 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005152 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5154 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5155 return NULL; /* out of memory! */
5156
5157 /*
5158 * Copy the stuff before the expanded part.
5159 * Copy the expanded stuff.
5160 * Copy what came after the expanded part.
5161 * Copy the next commands, if there are any.
5162 */
5163 i = (int)(src - *cmdlinep); /* length of part before match */
5164 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005165
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 mch_memmove(new_cmdline + i, repl, (size_t)len);
5167 i += len; /* remember the end of the string */
5168 STRCPY(new_cmdline + i, src + srclen);
5169 src = new_cmdline + i; /* remember where to continue */
5170
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005171 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 {
5173 i = (int)STRLEN(new_cmdline) + 1;
5174 STRCPY(new_cmdline + i, eap->nextcmd);
5175 eap->nextcmd = new_cmdline + i;
5176 }
5177 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5178 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5179 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5180 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5181 vim_free(*cmdlinep);
5182 *cmdlinep = new_cmdline;
5183
5184 return src;
5185}
5186
5187/*
5188 * Check for '|' to separate commands and '"' to start comments.
5189 */
5190 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005191separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192{
5193 char_u *p;
5194
Bram Moolenaar86b68352004-12-27 21:59:20 +00005195#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005196 p = skip_grep_pat(eap);
5197#else
5198 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005199#endif
5200
5201 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 {
5203 if (*p == Ctrl_V)
5204 {
5205 if (eap->argt & (USECTRLV | XFILE))
5206 ++p; /* skip CTRL-V and next char */
5207 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005208 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005209 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 if (*p == NUL) /* stop at NUL after CTRL-V */
5211 break;
5212 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005213
5214#ifdef FEAT_EVAL
5215 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005216 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005217 {
5218 p += 2;
5219 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005220 }
5221#endif
5222
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 /* Check for '"': start of comment or '|': next command */
5224 /* :@" and :*" do not start a comment!
5225 * :redir @" doesn't either. */
5226 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5227 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5228 || p != eap->arg)
5229 && (eap->cmdidx != CMD_redir
5230 || p != eap->arg + 1 || p[-1] != '@'))
5231 || *p == '|' || *p == '\n')
5232 {
5233 /*
5234 * We remove the '\' before the '|', unless USECTRLV is used
5235 * AND 'b' is present in 'cpoptions'.
5236 */
5237 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5238 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5239 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005240 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 --p;
5242 }
5243 else
5244 {
5245 eap->nextcmd = check_nextcmd(p);
5246 *p = NUL;
5247 break;
5248 }
5249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005251
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5253 del_trailing_spaces(eap->arg);
5254}
5255
5256/*
5257 * get + command from ex argument
5258 */
5259 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005260getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261{
5262 char_u *arg = *argp;
5263 char_u *command = NULL;
5264
5265 if (*arg == '+') /* +[command] */
5266 {
5267 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005268 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 command = dollar_command;
5270 else
5271 {
5272 command = arg;
5273 arg = skip_cmd_arg(command, TRUE);
5274 if (*arg != NUL)
5275 *arg++ = NUL; /* terminate command with NUL */
5276 }
5277
5278 arg = skipwhite(arg); /* skip over spaces */
5279 *argp = arg;
5280 }
5281 return command;
5282}
5283
5284/*
5285 * Find end of "+command" argument. Skip over "\ " and "\\".
5286 */
5287 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005288skip_cmd_arg(
5289 char_u *p,
5290 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291{
5292 while (*p && !vim_isspace(*p))
5293 {
5294 if (*p == '\\' && p[1] != NUL)
5295 {
5296 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005297 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 else
5299 ++p;
5300 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005301 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 }
5303 return p;
5304}
5305
5306/*
5307 * Get "++opt=arg" argument.
5308 * Return FAIL or OK.
5309 */
5310 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005311getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312{
5313 char_u *arg = eap->arg + 2;
5314 int *pp = NULL;
5315#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005316 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 char_u *p;
5318#endif
5319
5320 /* ":edit ++[no]bin[ary] file" */
5321 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5322 {
5323 if (*arg == 'n')
5324 {
5325 arg += 2;
5326 eap->force_bin = FORCE_NOBIN;
5327 }
5328 else
5329 eap->force_bin = FORCE_BIN;
5330 if (!checkforcmd(&arg, "binary", 3))
5331 return FAIL;
5332 eap->arg = skipwhite(arg);
5333 return OK;
5334 }
5335
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005336 /* ":read ++edit file" */
5337 if (STRNCMP(arg, "edit", 4) == 0)
5338 {
5339 eap->read_edit = TRUE;
5340 eap->arg = skipwhite(arg + 4);
5341 return OK;
5342 }
5343
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 if (STRNCMP(arg, "ff", 2) == 0)
5345 {
5346 arg += 2;
5347 pp = &eap->force_ff;
5348 }
5349 else if (STRNCMP(arg, "fileformat", 10) == 0)
5350 {
5351 arg += 10;
5352 pp = &eap->force_ff;
5353 }
5354#ifdef FEAT_MBYTE
5355 else if (STRNCMP(arg, "enc", 3) == 0)
5356 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005357 if (STRNCMP(arg, "encoding", 8) == 0)
5358 arg += 8;
5359 else
5360 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 pp = &eap->force_enc;
5362 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005363 else if (STRNCMP(arg, "bad", 3) == 0)
5364 {
5365 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005366 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368#endif
5369
5370 if (pp == NULL || *arg != '=')
5371 return FAIL;
5372
5373 ++arg;
5374 *pp = (int)(arg - eap->cmd);
5375 arg = skip_cmd_arg(arg, FALSE);
5376 eap->arg = skipwhite(arg);
5377 *arg = NUL;
5378
5379#ifdef FEAT_MBYTE
5380 if (pp == &eap->force_ff)
5381 {
5382#endif
5383 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5384 return FAIL;
5385#ifdef FEAT_MBYTE
5386 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005387 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 {
5389 /* Make 'fileencoding' lower case. */
5390 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5391 *p = TOLOWER_ASC(*p);
5392 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005393 else
5394 {
5395 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5396 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005397 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005398 if (STRICMP(p, "keep") == 0)
5399 eap->bad_char = BAD_KEEP;
5400 else if (STRICMP(p, "drop") == 0)
5401 eap->bad_char = BAD_DROP;
5402 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5403 eap->bad_char = *p;
5404 else
5405 return FAIL;
5406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407#endif
5408
5409 return OK;
5410}
5411
5412/*
5413 * ":abbreviate" and friends.
5414 */
5415 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005416ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417{
5418 do_exmap(eap, TRUE); /* almost the same as mapping */
5419}
5420
5421/*
5422 * ":map" and friends.
5423 */
5424 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005425ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426{
5427 /*
5428 * If we are sourcing .exrc or .vimrc in current directory we
5429 * print the mappings for security reasons.
5430 */
5431 if (secure)
5432 {
5433 secure = 2;
5434 msg_outtrans(eap->cmd);
5435 msg_putchar('\n');
5436 }
5437 do_exmap(eap, FALSE);
5438}
5439
5440/*
5441 * ":unmap" and friends.
5442 */
5443 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005444ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445{
5446 do_exmap(eap, FALSE);
5447}
5448
5449/*
5450 * ":mapclear" and friends.
5451 */
5452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005453ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454{
5455 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5456}
5457
5458/*
5459 * ":abclear" and friends.
5460 */
5461 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005462ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463{
5464 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5465}
5466
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005467#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005469ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470{
5471 /*
5472 * Disallow auto commands from .exrc and .vimrc in current
5473 * directory for security reasons.
5474 */
5475 if (secure)
5476 {
5477 secure = 2;
5478 eap->errmsg = e_curdir;
5479 }
5480 else if (eap->cmdidx == CMD_autocmd)
5481 do_autocmd(eap->arg, eap->forceit);
5482 else
5483 do_augroup(eap->arg, eap->forceit);
5484}
5485
5486/*
5487 * ":doautocmd": Apply the automatic commands to the current buffer.
5488 */
5489 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005490ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005492 char_u *arg = eap->arg;
5493 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005494 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005495
Bram Moolenaar1610d052016-06-09 22:53:01 +02005496 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5497 /* Only when there is no <nomodeline>. */
5498 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005499 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500}
5501#endif
5502
5503#ifdef FEAT_LISTCMDS
5504/*
5505 * :[N]bunload[!] [N] [bufname] unload buffer
5506 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5507 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5508 */
5509 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005510ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511{
5512 eap->errmsg = do_bufdel(
5513 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5514 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5515 : DOBUF_UNLOAD, eap->arg,
5516 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5517}
5518
5519/*
5520 * :[N]buffer [N] to buffer N
5521 * :[N]sbuffer [N] to buffer N
5522 */
5523 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005524ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525{
5526 if (*eap->arg)
5527 eap->errmsg = e_trailing;
5528 else
5529 {
5530 if (eap->addr_count == 0) /* default is current buffer */
5531 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5532 else
5533 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005534 if (eap->do_ecmd_cmd != NULL)
5535 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 }
5537}
5538
5539/*
5540 * :[N]bmodified [N] to next mod. buffer
5541 * :[N]sbmodified [N] to next mod. buffer
5542 */
5543 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005544ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545{
5546 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005547 if (eap->do_ecmd_cmd != NULL)
5548 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549}
5550
5551/*
5552 * :[N]bnext [N] to next buffer
5553 * :[N]sbnext [N] split and to next buffer
5554 */
5555 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005556ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557{
5558 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005559 if (eap->do_ecmd_cmd != NULL)
5560 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561}
5562
5563/*
5564 * :[N]bNext [N] to previous buffer
5565 * :[N]bprevious [N] to previous buffer
5566 * :[N]sbNext [N] split and to previous buffer
5567 * :[N]sbprevious [N] split and to previous buffer
5568 */
5569 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005570ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571{
5572 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005573 if (eap->do_ecmd_cmd != NULL)
5574 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575}
5576
5577/*
5578 * :brewind to first buffer
5579 * :bfirst to first buffer
5580 * :sbrewind split and to first buffer
5581 * :sbfirst split and to first buffer
5582 */
5583 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005584ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585{
5586 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005587 if (eap->do_ecmd_cmd != NULL)
5588 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589}
5590
5591/*
5592 * :blast to last buffer
5593 * :sblast split and to last buffer
5594 */
5595 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005596ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597{
5598 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005599 if (eap->do_ecmd_cmd != NULL)
5600 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601}
5602#endif
5603
5604 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005605ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606{
5607 return (c == NUL || c == '|' || c == '"' || c == '\n');
5608}
5609
5610#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5611 || defined(PROTO)
5612/*
5613 * Return the next command, after the first '|' or '\n'.
5614 * Return NULL if not found.
5615 */
5616 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005617find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618{
5619 while (*p != '|' && *p != '\n')
5620 {
5621 if (*p == NUL)
5622 return NULL;
5623 ++p;
5624 }
5625 return (p + 1);
5626}
5627#endif
5628
5629/*
5630 * Check if *p is a separator between Ex commands.
5631 * Return NULL if it isn't, (p + 1) if it is.
5632 */
5633 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005634check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635{
5636 p = skipwhite(p);
5637 if (*p == '|' || *p == '\n')
5638 return (p + 1);
5639 else
5640 return NULL;
5641}
5642
5643/*
5644 * - if there are more files to edit
5645 * - and this is the last window
5646 * - and forceit not used
5647 * - and not repeated twice on a row
5648 * return FAIL and give error message if 'message' TRUE
5649 * return OK otherwise
5650 */
5651 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005652check_more(
5653 int message, /* when FALSE check only, no messages */
5654 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655{
5656 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5657
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005658 if (!forceit && only_one_window()
5659 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 {
5661 if (message)
5662 {
5663#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5664 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5665 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005666 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667
5668 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005669 vim_strncpy(buff,
5670 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005671 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005673 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 _("%d more files to edit. Quit anyway?"), n);
5675 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5676 return OK;
5677 return FAIL;
5678 }
5679#endif
5680 if (n == 1)
5681 EMSG(_("E173: 1 more file to edit"));
5682 else
5683 EMSGN(_("E173: %ld more files to edit"), n);
5684 quitmore = 2; /* next try to quit is allowed */
5685 }
5686 return FAIL;
5687 }
5688 return OK;
5689}
5690
5691#ifdef FEAT_CMDL_COMPL
5692/*
5693 * Function given to ExpandGeneric() to obtain the list of command names.
5694 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005696get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697{
5698 if (idx >= (int)CMD_SIZE)
5699# ifdef FEAT_USR_CMDS
5700 return get_user_command_name(idx);
5701# else
5702 return NULL;
5703# endif
5704 return cmdnames[idx].cmd_name;
5705}
5706#endif
5707
5708#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005709static 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);
5710static void uc_list(char_u *name, size_t name_len);
5711static 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);
5712static char_u *uc_split_args(char_u *arg, size_t *lenp);
5713static 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 +00005714
5715 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005716uc_add_command(
5717 char_u *name,
5718 size_t name_len,
5719 char_u *rep,
5720 long argt,
5721 long def,
5722 int flags,
5723 int compl,
5724 char_u *compl_arg,
5725 int addr_type,
5726 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727{
5728 ucmd_T *cmd = NULL;
5729 char_u *p;
5730 int i;
5731 int cmp = 1;
5732 char_u *rep_buf = NULL;
5733 garray_T *gap;
5734
Bram Moolenaar9c102382006-05-03 21:26:49 +00005735 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 if (rep_buf == NULL)
5737 {
5738 /* Can't replace termcodes - try using the string as is */
5739 rep_buf = vim_strsave(rep);
5740
5741 /* Give up if out of memory */
5742 if (rep_buf == NULL)
5743 return FAIL;
5744 }
5745
5746 /* get address of growarray: global or in curbuf */
5747 if (flags & UC_BUFFER)
5748 {
5749 gap = &curbuf->b_ucmds;
5750 if (gap->ga_itemsize == 0)
5751 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5752 }
5753 else
5754 gap = &ucmds;
5755
5756 /* Search for the command in the already defined commands. */
5757 for (i = 0; i < gap->ga_len; ++i)
5758 {
5759 size_t len;
5760
5761 cmd = USER_CMD_GA(gap, i);
5762 len = STRLEN(cmd->uc_name);
5763 cmp = STRNCMP(name, cmd->uc_name, name_len);
5764 if (cmp == 0)
5765 {
5766 if (name_len < len)
5767 cmp = -1;
5768 else if (name_len > len)
5769 cmp = 1;
5770 }
5771
5772 if (cmp == 0)
5773 {
5774 if (!force)
5775 {
5776 EMSG(_("E174: Command already exists: add ! to replace it"));
5777 goto fail;
5778 }
5779
5780 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005781 cmd->uc_rep = NULL;
5782#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5783 vim_free(cmd->uc_compl_arg);
5784 cmd->uc_compl_arg = NULL;
5785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 break;
5787 }
5788
5789 /* Stop as soon as we pass the name to add */
5790 if (cmp < 0)
5791 break;
5792 }
5793
5794 /* Extend the array unless we're replacing an existing command */
5795 if (cmp != 0)
5796 {
5797 if (ga_grow(gap, 1) != OK)
5798 goto fail;
5799 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5800 goto fail;
5801
5802 cmd = USER_CMD_GA(gap, i);
5803 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5804
5805 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806
5807 cmd->uc_name = p;
5808 }
5809
5810 cmd->uc_rep = rep_buf;
5811 cmd->uc_argt = argt;
5812 cmd->uc_def = def;
5813 cmd->uc_compl = compl;
5814#ifdef FEAT_EVAL
5815 cmd->uc_scriptID = current_SID;
5816# ifdef FEAT_CMDL_COMPL
5817 cmd->uc_compl_arg = compl_arg;
5818# endif
5819#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005820 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821
5822 return OK;
5823
5824fail:
5825 vim_free(rep_buf);
5826#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5827 vim_free(compl_arg);
5828#endif
5829 return FAIL;
5830}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005833#if defined(FEAT_USR_CMDS)
5834static struct
5835{
5836 int expand;
5837 char *name;
5838} addr_type_complete[] =
5839{
5840 {ADDR_ARGUMENTS, "arguments"},
5841 {ADDR_LINES, "lines"},
5842 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5843 {ADDR_TABS, "tabs"},
5844 {ADDR_BUFFERS, "buffers"},
5845 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005846 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005847 {-1, NULL}
5848};
5849#endif
5850
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005851#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852/*
5853 * List of names for completion for ":command" with the EXPAND_ flag.
5854 * Must be alphabetical for completion.
5855 */
5856static struct
5857{
5858 int expand;
5859 char *name;
5860} command_complete[] =
5861{
5862 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005863 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005865 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005867 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005868#if defined(FEAT_CSCOPE)
5869 {EXPAND_CSCOPE, "cscope"},
5870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5872 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005873 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874#endif
5875 {EXPAND_DIRECTORIES, "dir"},
5876 {EXPAND_ENV_VARS, "environment"},
5877 {EXPAND_EVENTS, "event"},
5878 {EXPAND_EXPRESSION, "expression"},
5879 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005880 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005881 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 {EXPAND_FUNCTIONS, "function"},
5883 {EXPAND_HELP, "help"},
5884 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005885#if defined(FEAT_CMDHIST)
5886 {EXPAND_HISTORY, "history"},
5887#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005888#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005889 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005890 {EXPAND_LOCALES, "locale"},
5891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 {EXPAND_MAPPINGS, "mapping"},
5893 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005894 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005895#if defined(FEAT_PROFILE)
5896 {EXPAND_SYNTIME, "syntime"},
5897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005899 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005900 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005901#if defined(FEAT_SIGNS)
5902 {EXPAND_SIGN, "sign"},
5903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 {EXPAND_TAGS, "tag"},
5905 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005906 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 {EXPAND_USER_VARS, "var"},
5908 {0, NULL}
5909};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005912#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005914uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915{
5916 int i, j;
5917 int found = FALSE;
5918 ucmd_T *cmd;
5919 int len;
5920 long a;
5921 garray_T *gap;
5922
5923 gap = &curbuf->b_ucmds;
5924 for (;;)
5925 {
5926 for (i = 0; i < gap->ga_len; ++i)
5927 {
5928 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005929 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930
5931 /* Skip commands which don't match the requested prefix */
5932 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5933 continue;
5934
5935 /* Put out the title first time */
5936 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005937 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 found = TRUE;
5939 msg_putchar('\n');
5940 if (got_int)
5941 break;
5942
5943 /* Special cases */
5944 msg_putchar(a & BANG ? '!' : ' ');
5945 msg_putchar(a & REGSTR ? '"' : ' ');
5946 msg_putchar(gap != &ucmds ? 'b' : ' ');
5947 msg_putchar(' ');
5948
5949 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5950 len = (int)STRLEN(cmd->uc_name) + 4;
5951
5952 do {
5953 msg_putchar(' ');
5954 ++len;
5955 } while (len < 16);
5956
5957 len = 0;
5958
5959 /* Arguments */
5960 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5961 {
5962 case 0: IObuff[len++] = '0'; break;
5963 case (EXTRA): IObuff[len++] = '*'; break;
5964 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5965 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5966 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5967 }
5968
5969 do {
5970 IObuff[len++] = ' ';
5971 } while (len < 5);
5972
5973 /* Range */
5974 if (a & (RANGE|COUNT))
5975 {
5976 if (a & COUNT)
5977 {
5978 /* -count=N */
5979 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5980 len += (int)STRLEN(IObuff + len);
5981 }
5982 else if (a & DFLALL)
5983 IObuff[len++] = '%';
5984 else if (cmd->uc_def >= 0)
5985 {
5986 /* -range=N */
5987 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5988 len += (int)STRLEN(IObuff + len);
5989 }
5990 else
5991 IObuff[len++] = '.';
5992 }
5993
5994 do {
5995 IObuff[len++] = ' ';
5996 } while (len < 11);
5997
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005998 /* Address Type */
5999 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6000 if (addr_type_complete[j].expand != ADDR_LINES
6001 && addr_type_complete[j].expand == cmd->uc_addr_type)
6002 {
6003 STRCPY(IObuff + len, addr_type_complete[j].name);
6004 len += (int)STRLEN(IObuff + len);
6005 break;
6006 }
6007
6008 do {
6009 IObuff[len++] = ' ';
6010 } while (len < 21);
6011
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 /* Completion */
6013 for (j = 0; command_complete[j].expand != 0; ++j)
6014 if (command_complete[j].expand == cmd->uc_compl)
6015 {
6016 STRCPY(IObuff + len, command_complete[j].name);
6017 len += (int)STRLEN(IObuff + len);
6018 break;
6019 }
6020
6021 do {
6022 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006023 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024
6025 IObuff[len] = '\0';
6026 msg_outtrans(IObuff);
6027
6028 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006029#ifdef FEAT_EVAL
6030 if (p_verbose > 0)
6031 last_set_msg(cmd->uc_scriptID);
6032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 out_flush();
6034 ui_breakcheck();
6035 if (got_int)
6036 break;
6037 }
6038 if (gap == &ucmds || i < gap->ga_len)
6039 break;
6040 gap = &ucmds;
6041 }
6042
6043 if (!found)
6044 MSG(_("No user-defined commands found"));
6045}
6046
6047 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006048uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049{
6050 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6051 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6052 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6053 0xb9, 0x7f, 0};
6054 int i;
6055
6056 for (i = 0; fcmd[i]; ++i)
6057 IObuff[i] = fcmd[i] - 0x40;
6058 IObuff[i] = 0;
6059 return IObuff;
6060}
6061
6062 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006063uc_scan_attr(
6064 char_u *attr,
6065 size_t len,
6066 long *argt,
6067 long *def,
6068 int *flags,
6069 int *compl,
6070 char_u **compl_arg,
6071 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072{
6073 char_u *p;
6074
6075 if (len == 0)
6076 {
6077 EMSG(_("E175: No attribute specified"));
6078 return FAIL;
6079 }
6080
6081 /* First, try the simple attributes (no arguments) */
6082 if (STRNICMP(attr, "bang", len) == 0)
6083 *argt |= BANG;
6084 else if (STRNICMP(attr, "buffer", len) == 0)
6085 *flags |= UC_BUFFER;
6086 else if (STRNICMP(attr, "register", len) == 0)
6087 *argt |= REGSTR;
6088 else if (STRNICMP(attr, "bar", len) == 0)
6089 *argt |= TRLBAR;
6090 else
6091 {
6092 int i;
6093 char_u *val = NULL;
6094 size_t vallen = 0;
6095 size_t attrlen = len;
6096
6097 /* Look for the attribute name - which is the part before any '=' */
6098 for (i = 0; i < (int)len; ++i)
6099 {
6100 if (attr[i] == '=')
6101 {
6102 val = &attr[i + 1];
6103 vallen = len - i - 1;
6104 attrlen = i;
6105 break;
6106 }
6107 }
6108
6109 if (STRNICMP(attr, "nargs", attrlen) == 0)
6110 {
6111 if (vallen == 1)
6112 {
6113 if (*val == '0')
6114 /* Do nothing - this is the default */;
6115 else if (*val == '1')
6116 *argt |= (EXTRA | NOSPC | NEEDARG);
6117 else if (*val == '*')
6118 *argt |= EXTRA;
6119 else if (*val == '?')
6120 *argt |= (EXTRA | NOSPC);
6121 else if (*val == '+')
6122 *argt |= (EXTRA | NEEDARG);
6123 else
6124 goto wrong_nargs;
6125 }
6126 else
6127 {
6128wrong_nargs:
6129 EMSG(_("E176: Invalid number of arguments"));
6130 return FAIL;
6131 }
6132 }
6133 else if (STRNICMP(attr, "range", attrlen) == 0)
6134 {
6135 *argt |= RANGE;
6136 if (vallen == 1 && *val == '%')
6137 *argt |= DFLALL;
6138 else if (val != NULL)
6139 {
6140 p = val;
6141 if (*def >= 0)
6142 {
6143two_count:
6144 EMSG(_("E177: Count cannot be specified twice"));
6145 return FAIL;
6146 }
6147
6148 *def = getdigits(&p);
6149 *argt |= (ZEROR | NOTADR);
6150
6151 if (p != val + vallen || vallen == 0)
6152 {
6153invalid_count:
6154 EMSG(_("E178: Invalid default value for count"));
6155 return FAIL;
6156 }
6157 }
6158 }
6159 else if (STRNICMP(attr, "count", attrlen) == 0)
6160 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006161 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162
6163 if (val != NULL)
6164 {
6165 p = val;
6166 if (*def >= 0)
6167 goto two_count;
6168
6169 *def = getdigits(&p);
6170
6171 if (p != val + vallen)
6172 goto invalid_count;
6173 }
6174
6175 if (*def < 0)
6176 *def = 0;
6177 }
6178 else if (STRNICMP(attr, "complete", attrlen) == 0)
6179 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 if (val == NULL)
6181 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006182 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 return FAIL;
6184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006186 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6187 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006190 else if (STRNICMP(attr, "addr", attrlen) == 0)
6191 {
6192 *argt |= RANGE;
6193 if (val == NULL)
6194 {
6195 EMSG(_("E179: argument required for -addr"));
6196 return FAIL;
6197 }
6198 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6199 == FAIL)
6200 return FAIL;
6201 if (addr_type_arg != ADDR_LINES)
6202 *argt |= (ZEROR | NOTADR) ;
6203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 else
6205 {
6206 char_u ch = attr[len];
6207 attr[len] = '\0';
6208 EMSG2(_("E181: Invalid attribute: %s"), attr);
6209 attr[len] = ch;
6210 return FAIL;
6211 }
6212 }
6213
6214 return OK;
6215}
6216
Bram Moolenaara850a712009-01-28 14:42:59 +00006217/*
6218 * ":command ..."
6219 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006221ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222{
6223 char_u *name;
6224 char_u *end;
6225 char_u *p;
6226 long argt = 0;
6227 long def = -1;
6228 int flags = 0;
6229 int compl = EXPAND_NOTHING;
6230 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006231 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006233 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234
6235 p = eap->arg;
6236
6237 /* Check for attributes */
6238 while (*p == '-')
6239 {
6240 ++p;
6241 end = skiptowhite(p);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006242 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 == FAIL)
6244 return;
6245 p = skipwhite(end);
6246 }
6247
6248 /* Get the name (if any) and skip to the following argument */
6249 name = p;
6250 if (ASCII_ISALPHA(*p))
6251 while (ASCII_ISALNUM(*p))
6252 ++p;
6253 if (!ends_excmd(*p) && !vim_iswhite(*p))
6254 {
6255 EMSG(_("E182: Invalid command name"));
6256 return;
6257 }
6258 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006259 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260
6261 /* If there is nothing after the name, and no attributes were specified,
6262 * we are listing commands
6263 */
6264 p = skipwhite(end);
6265 if (!has_attr && ends_excmd(*p))
6266 {
6267 uc_list(name, end - name);
6268 }
6269 else if (!ASCII_ISUPPER(*name))
6270 {
6271 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6272 return;
6273 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006274 else if ((name_len == 1 && *name == 'X')
6275 || (name_len <= 4
6276 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6277 {
6278 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6279 return;
6280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 else
6282 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006283 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284}
6285
6286/*
6287 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 * Clear all user commands, global and for current buffer.
6289 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006290 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006291ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292{
6293 uc_clear(&ucmds);
6294 uc_clear(&curbuf->b_ucmds);
6295}
6296
6297/*
6298 * Clear all user commands for "gap".
6299 */
6300 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006301uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302{
6303 int i;
6304 ucmd_T *cmd;
6305
6306 for (i = 0; i < gap->ga_len; ++i)
6307 {
6308 cmd = USER_CMD_GA(gap, i);
6309 vim_free(cmd->uc_name);
6310 vim_free(cmd->uc_rep);
6311# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6312 vim_free(cmd->uc_compl_arg);
6313# endif
6314 }
6315 ga_clear(gap);
6316}
6317
6318 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006319ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320{
6321 int i = 0;
6322 ucmd_T *cmd = NULL;
6323 int cmp = -1;
6324 garray_T *gap;
6325
6326 gap = &curbuf->b_ucmds;
6327 for (;;)
6328 {
6329 for (i = 0; i < gap->ga_len; ++i)
6330 {
6331 cmd = USER_CMD_GA(gap, i);
6332 cmp = STRCMP(eap->arg, cmd->uc_name);
6333 if (cmp <= 0)
6334 break;
6335 }
6336 if (gap == &ucmds || cmp == 0)
6337 break;
6338 gap = &ucmds;
6339 }
6340
6341 if (cmp != 0)
6342 {
6343 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6344 return;
6345 }
6346
6347 vim_free(cmd->uc_name);
6348 vim_free(cmd->uc_rep);
6349# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6350 vim_free(cmd->uc_compl_arg);
6351# endif
6352
6353 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354
6355 if (i < gap->ga_len)
6356 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6357}
6358
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006359/*
6360 * split and quote args for <f-args>
6361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006363uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364{
6365 char_u *buf;
6366 char_u *p;
6367 char_u *q;
6368 int len;
6369
6370 /* Precalculate length */
6371 p = arg;
6372 len = 2; /* Initial and final quotes */
6373
6374 while (*p)
6375 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006376 if (p[0] == '\\' && p[1] == '\\')
6377 {
6378 len += 2;
6379 p += 2;
6380 }
6381 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 {
6383 len += 1;
6384 p += 2;
6385 }
6386 else if (*p == '\\' || *p == '"')
6387 {
6388 len += 2;
6389 p += 1;
6390 }
6391 else if (vim_iswhite(*p))
6392 {
6393 p = skipwhite(p);
6394 if (*p == NUL)
6395 break;
6396 len += 3; /* "," */
6397 }
6398 else
6399 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006400#ifdef FEAT_MBYTE
6401 int charlen = (*mb_ptr2len)(p);
6402 len += charlen;
6403 p += charlen;
6404#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 ++len;
6406 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 }
6409 }
6410
6411 buf = alloc(len + 1);
6412 if (buf == NULL)
6413 {
6414 *lenp = 0;
6415 return buf;
6416 }
6417
6418 p = arg;
6419 q = buf;
6420 *q++ = '"';
6421 while (*p)
6422 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006423 if (p[0] == '\\' && p[1] == '\\')
6424 {
6425 *q++ = '\\';
6426 *q++ = '\\';
6427 p += 2;
6428 }
6429 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 {
6431 *q++ = p[1];
6432 p += 2;
6433 }
6434 else if (*p == '\\' || *p == '"')
6435 {
6436 *q++ = '\\';
6437 *q++ = *p++;
6438 }
6439 else if (vim_iswhite(*p))
6440 {
6441 p = skipwhite(p);
6442 if (*p == NUL)
6443 break;
6444 *q++ = '"';
6445 *q++ = ',';
6446 *q++ = '"';
6447 }
6448 else
6449 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006450 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 }
6452 }
6453 *q++ = '"';
6454 *q = 0;
6455
6456 *lenp = len;
6457 return buf;
6458}
6459
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006460 static size_t
6461add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6462{
6463 size_t result;
6464
6465 result = STRLEN(mod_str);
6466 if (*multi_mods)
6467 result += 1;
6468 if (buf != NULL)
6469 {
6470 if (*multi_mods)
6471 STRCAT(buf, " ");
6472 STRCAT(buf, mod_str);
6473 }
6474
6475 *multi_mods = 1;
6476
6477 return result;
6478}
6479
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480/*
6481 * Check for a <> code in a user command.
6482 * "code" points to the '<'. "len" the length of the <> (inclusive).
6483 * "buf" is where the result is to be added.
6484 * "split_buf" points to a buffer used for splitting, caller should free it.
6485 * "split_len" is the length of what "split_buf" contains.
6486 * Returns the length of the replacement, which has been added to "buf".
6487 * Returns -1 if there was no match, and only the "<" has been copied.
6488 */
6489 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006490uc_check_code(
6491 char_u *code,
6492 size_t len,
6493 char_u *buf,
6494 ucmd_T *cmd, /* the user command we're expanding */
6495 exarg_T *eap, /* ex arguments */
6496 char_u **split_buf,
6497 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498{
6499 size_t result = 0;
6500 char_u *p = code + 1;
6501 size_t l = len - 2;
6502 int quote = 0;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006503 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_MODS,
6504 ct_REGISTER, ct_LT, ct_NONE } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505
6506 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6507 {
6508 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6509 p += 2;
6510 l -= 2;
6511 }
6512
Bram Moolenaar371d5402006-03-20 21:47:49 +00006513 ++l;
6514 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006516 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006518 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006520 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006522 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006524 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006526 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006528 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006530 else if (STRNICMP(p, "mods>", l) == 0)
6531 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532
6533 switch (type)
6534 {
6535 case ct_ARGS:
6536 /* Simple case first */
6537 if (*eap->arg == NUL)
6538 {
6539 if (quote == 1)
6540 {
6541 result = 2;
6542 if (buf != NULL)
6543 STRCPY(buf, "''");
6544 }
6545 else
6546 result = 0;
6547 break;
6548 }
6549
6550 /* When specified there is a single argument don't split it.
6551 * Works for ":Cmd %" when % is "a b c". */
6552 if ((eap->argt & NOSPC) && quote == 2)
6553 quote = 1;
6554
6555 switch (quote)
6556 {
6557 case 0: /* No quoting, no splitting */
6558 result = STRLEN(eap->arg);
6559 if (buf != NULL)
6560 STRCPY(buf, eap->arg);
6561 break;
6562 case 1: /* Quote, but don't split */
6563 result = STRLEN(eap->arg) + 2;
6564 for (p = eap->arg; *p; ++p)
6565 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006566#ifdef FEAT_MBYTE
6567 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6568 /* DBCS can contain \ in a trail byte, skip the
6569 * double-byte character. */
6570 ++p;
6571 else
6572#endif
6573 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 ++result;
6575 }
6576
6577 if (buf != NULL)
6578 {
6579 *buf++ = '"';
6580 for (p = eap->arg; *p; ++p)
6581 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006582#ifdef FEAT_MBYTE
6583 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6584 /* DBCS can contain \ in a trail byte, copy the
6585 * double-byte character to avoid escaping. */
6586 *buf++ = *p++;
6587 else
6588#endif
6589 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 *buf++ = '\\';
6591 *buf++ = *p;
6592 }
6593 *buf = '"';
6594 }
6595
6596 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006597 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 /* This is hard, so only do it once, and cache the result */
6599 if (*split_buf == NULL)
6600 *split_buf = uc_split_args(eap->arg, split_len);
6601
6602 result = *split_len;
6603 if (buf != NULL && result != 0)
6604 STRCPY(buf, *split_buf);
6605
6606 break;
6607 }
6608 break;
6609
6610 case ct_BANG:
6611 result = eap->forceit ? 1 : 0;
6612 if (quote)
6613 result += 2;
6614 if (buf != NULL)
6615 {
6616 if (quote)
6617 *buf++ = '"';
6618 if (eap->forceit)
6619 *buf++ = '!';
6620 if (quote)
6621 *buf = '"';
6622 }
6623 break;
6624
6625 case ct_LINE1:
6626 case ct_LINE2:
6627 case ct_COUNT:
6628 {
6629 char num_buf[20];
6630 long num = (type == ct_LINE1) ? eap->line1 :
6631 (type == ct_LINE2) ? eap->line2 :
6632 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6633 size_t num_len;
6634
6635 sprintf(num_buf, "%ld", num);
6636 num_len = STRLEN(num_buf);
6637 result = num_len;
6638
6639 if (quote)
6640 result += 2;
6641
6642 if (buf != NULL)
6643 {
6644 if (quote)
6645 *buf++ = '"';
6646 STRCPY(buf, num_buf);
6647 buf += num_len;
6648 if (quote)
6649 *buf = '"';
6650 }
6651
6652 break;
6653 }
6654
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006655 case ct_MODS:
6656 {
6657 int multi_mods = 0;
6658 typedef struct {
6659 int *varp;
6660 char *name;
6661 } mod_entry_T;
6662 static mod_entry_T mod_entries[] = {
6663#ifdef FEAT_BROWSE_CMD
6664 {&cmdmod.browse, "browse"},
6665#endif
6666#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6667 {&cmdmod.confirm, "confirm"},
6668#endif
6669 {&cmdmod.hide, "hide"},
6670 {&cmdmod.keepalt, "keepalt"},
6671 {&cmdmod.keepjumps, "keepjumps"},
6672 {&cmdmod.keepmarks, "keepmarks"},
6673 {&cmdmod.keeppatterns, "keeppatterns"},
6674 {&cmdmod.lockmarks, "lockmarks"},
6675 {&cmdmod.noswapfile, "noswapfile"},
6676 {NULL, NULL}
6677 };
6678 int i;
6679
6680 result = quote ? 2 : 0;
6681 if (buf != NULL)
6682 {
6683 if (quote)
6684 *buf++ = '"';
6685 *buf = '\0';
6686 }
6687
6688#ifdef FEAT_WINDOWS
6689 /* :aboveleft and :leftabove */
6690 if (cmdmod.split & WSP_ABOVE)
6691 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6692 /* :belowright and :rightbelow */
6693 if (cmdmod.split & WSP_BELOW)
6694 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6695 /* :botright */
6696 if (cmdmod.split & WSP_BOT)
6697 result += add_cmd_modifier(buf, "botright", &multi_mods);
6698#endif
6699
6700 /* the modifiers that are simple flags */
6701 for (i = 0; mod_entries[i].varp != NULL; ++i)
6702 if (*mod_entries[i].varp)
6703 result += add_cmd_modifier(buf, mod_entries[i].name,
6704 &multi_mods);
6705
6706 /* TODO: How to support :noautocmd? */
6707#ifdef HAVE_SANDBOX
6708 /* TODO: How to support :sandbox?*/
6709#endif
6710 /* :silent */
6711 if (msg_silent > 0)
6712 result += add_cmd_modifier(buf,
6713 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
6714#ifdef FEAT_WINDOWS
6715 /* :tab */
6716 if (cmdmod.tab > 0)
6717 result += add_cmd_modifier(buf, "tab", &multi_mods);
6718 /* :topleft */
6719 if (cmdmod.split & WSP_TOP)
6720 result += add_cmd_modifier(buf, "topleft", &multi_mods);
6721#endif
6722 /* TODO: How to support :unsilent?*/
6723 /* :verbose */
6724 if (p_verbose > 0)
6725 result += add_cmd_modifier(buf, "verbose", &multi_mods);
6726#ifdef FEAT_WINDOWS
6727 /* :vertical */
6728 if (cmdmod.split & WSP_VERT)
6729 result += add_cmd_modifier(buf, "vertical", &multi_mods);
6730#endif
6731 if (quote && buf != NULL)
6732 {
6733 buf += result - 2;
6734 *buf = '"';
6735 }
6736 break;
6737 }
6738
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 case ct_REGISTER:
6740 result = eap->regname ? 1 : 0;
6741 if (quote)
6742 result += 2;
6743 if (buf != NULL)
6744 {
6745 if (quote)
6746 *buf++ = '\'';
6747 if (eap->regname)
6748 *buf++ = eap->regname;
6749 if (quote)
6750 *buf = '\'';
6751 }
6752 break;
6753
6754 case ct_LT:
6755 result = 1;
6756 if (buf != NULL)
6757 *buf = '<';
6758 break;
6759
6760 default:
6761 /* Not recognized: just copy the '<' and return -1. */
6762 result = (size_t)-1;
6763 if (buf != NULL)
6764 *buf = '<';
6765 break;
6766 }
6767
6768 return result;
6769}
6770
6771 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006772do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773{
6774 char_u *buf;
6775 char_u *p;
6776 char_u *q;
6777
6778 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006779 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006780 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 size_t len, totlen;
6782
6783 size_t split_len = 0;
6784 char_u *split_buf = NULL;
6785 ucmd_T *cmd;
6786#ifdef FEAT_EVAL
6787 scid_T save_current_SID = current_SID;
6788#endif
6789
6790 if (eap->cmdidx == CMD_USER)
6791 cmd = USER_CMD(eap->useridx);
6792 else
6793 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6794
6795 /*
6796 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006797 * First round: "buf" is NULL, compute length, allocate "buf".
6798 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 */
6800 buf = NULL;
6801 for (;;)
6802 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006803 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006804 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006806
6807 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006809 start = vim_strchr(p, '<');
6810 if (start != NULL)
6811 end = vim_strchr(start + 1, '>');
6812 if (buf != NULL)
6813 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006814 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6815 ;
6816 if (*ksp == K_SPECIAL
6817 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006818 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6819# ifdef FEAT_GUI
6820 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6821# endif
6822 ))
6823 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006824 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006825 * KS_SPECIAL KE_FILLER, like for mappings, but
6826 * do_cmdline() doesn't handle that, so convert it back.
6827 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6828 len = ksp - p;
6829 if (len > 0)
6830 {
6831 mch_memmove(q, p, len);
6832 q += len;
6833 }
6834 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6835 p = ksp + 3;
6836 continue;
6837 }
6838 }
6839
6840 /* break if there no <item> is found */
6841 if (start == NULL || end == NULL)
6842 break;
6843
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 /* Include the '>' */
6845 ++end;
6846
6847 /* Take everything up to the '<' */
6848 len = start - p;
6849 if (buf == NULL)
6850 totlen += len;
6851 else
6852 {
6853 mch_memmove(q, p, len);
6854 q += len;
6855 }
6856
6857 len = uc_check_code(start, end - start, q, cmd, eap,
6858 &split_buf, &split_len);
6859 if (len == (size_t)-1)
6860 {
6861 /* no match, continue after '<' */
6862 p = start + 1;
6863 len = 1;
6864 }
6865 else
6866 p = end;
6867 if (buf == NULL)
6868 totlen += len;
6869 else
6870 q += len;
6871 }
6872 if (buf != NULL) /* second time here, finished */
6873 {
6874 STRCPY(q, p);
6875 break;
6876 }
6877
6878 totlen += STRLEN(p); /* Add on the trailing characters */
6879 buf = alloc((unsigned)(totlen + 1));
6880 if (buf == NULL)
6881 {
6882 vim_free(split_buf);
6883 return;
6884 }
6885 }
6886
6887#ifdef FEAT_EVAL
6888 current_SID = cmd->uc_scriptID;
6889#endif
6890 (void)do_cmdline(buf, eap->getline, eap->cookie,
6891 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6892#ifdef FEAT_EVAL
6893 current_SID = save_current_SID;
6894#endif
6895 vim_free(buf);
6896 vim_free(split_buf);
6897}
6898
6899# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6900 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006901get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902{
6903 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6904}
6905
6906/*
6907 * Function given to ExpandGeneric() to obtain the list of user command names.
6908 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006910get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911{
6912 if (idx < curbuf->b_ucmds.ga_len)
6913 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6914 idx -= curbuf->b_ucmds.ga_len;
6915 if (idx < ucmds.ga_len)
6916 return USER_CMD(idx)->uc_name;
6917 return NULL;
6918}
6919
6920/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006921 * Function given to ExpandGeneric() to obtain the list of user address type names.
6922 */
6923 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006924get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006925{
6926 return (char_u *)addr_type_complete[idx].name;
6927}
6928
6929/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 * Function given to ExpandGeneric() to obtain the list of user command
6931 * attributes.
6932 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006934get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935{
6936 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006937 {"addr", "bang", "bar", "buffer", "complete",
6938 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006940 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 return NULL;
6942 return (char_u *)user_cmd_flags[idx];
6943}
6944
6945/*
6946 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6947 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006949get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950{
6951 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6952
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006953 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 return NULL;
6955 return (char_u *)user_cmd_nargs[idx];
6956}
6957
6958/*
6959 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6960 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006962get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963{
6964 return (char_u *)command_complete[idx].name;
6965}
6966# endif /* FEAT_CMDL_COMPL */
6967
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006968/*
6969 * Parse address type argument
6970 */
6971 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006972parse_addr_type_arg(
6973 char_u *value,
6974 int vallen,
6975 long *argt,
6976 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006977{
6978 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01006979
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006980 for (i = 0; addr_type_complete[i].expand != -1; ++i)
6981 {
6982 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
6983 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
6984 if (a && b)
6985 {
6986 *addr_type_arg = addr_type_complete[i].expand;
6987 break;
6988 }
6989 }
6990
6991 if (addr_type_complete[i].expand == -1)
6992 {
6993 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01006994
6995 for (i = 0; err[i] != NUL && !vim_iswhite(err[i]); i++)
6996 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006997 err[i] = NUL;
6998 EMSG2(_("E180: Invalid address type value: %s"), err);
6999 return FAIL;
7000 }
7001
7002 if (*addr_type_arg != ADDR_LINES)
7003 *argt |= NOTADR;
7004
7005 return OK;
7006}
7007
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008#endif /* FEAT_USR_CMDS */
7009
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007010#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7011/*
7012 * Parse a completion argument "value[vallen]".
7013 * The detected completion goes in "*complp", argument type in "*argt".
7014 * When there is an argument, for function and user defined completion, it's
7015 * copied to allocated memory and stored in "*compl_arg".
7016 * Returns FAIL if something is wrong.
7017 */
7018 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007019parse_compl_arg(
7020 char_u *value,
7021 int vallen,
7022 int *complp,
7023 long *argt,
7024 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007025{
7026 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007027# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007028 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007029# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007030 int i;
7031 int valend = vallen;
7032
7033 /* Look for any argument part - which is the part after any ',' */
7034 for (i = 0; i < vallen; ++i)
7035 {
7036 if (value[i] == ',')
7037 {
7038 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007039# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007040 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007041# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007042 valend = i;
7043 break;
7044 }
7045 }
7046
7047 for (i = 0; command_complete[i].expand != 0; ++i)
7048 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007049 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007050 && STRNCMP(value, command_complete[i].name, valend) == 0)
7051 {
7052 *complp = command_complete[i].expand;
7053 if (command_complete[i].expand == EXPAND_BUFFERS)
7054 *argt |= BUFNAME;
7055 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7056 || command_complete[i].expand == EXPAND_FILES)
7057 *argt |= XFILE;
7058 break;
7059 }
7060 }
7061
7062 if (command_complete[i].expand == 0)
7063 {
7064 EMSG2(_("E180: Invalid complete value: %s"), value);
7065 return FAIL;
7066 }
7067
7068# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7069 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7070 && arg != NULL)
7071# else
7072 if (arg != NULL)
7073# endif
7074 {
7075 EMSG(_("E468: Completion argument only allowed for custom completion"));
7076 return FAIL;
7077 }
7078
7079# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7080 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7081 && arg == NULL)
7082 {
7083 EMSG(_("E467: Custom completion requires a function argument"));
7084 return FAIL;
7085 }
7086
7087 if (arg != NULL)
7088 *compl_arg = vim_strnsave(arg, (int)arglen);
7089# endif
7090 return OK;
7091}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007092
7093 int
7094cmdcomplete_str_to_type(char_u *complete_str)
7095{
7096 int i;
7097
7098 for (i = 0; command_complete[i].expand != 0; ++i)
7099 if (STRCMP(complete_str, command_complete[i].name) == 0)
7100 return command_complete[i].expand;
7101
7102 return EXPAND_NOTHING;
7103}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007104#endif
7105
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007107ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108{
Bram Moolenaare6850792010-05-14 15:28:44 +02007109 if (*eap->arg == NUL)
7110 {
7111#ifdef FEAT_EVAL
7112 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7113 char_u *p = NULL;
7114
7115 if (expr != NULL)
7116 {
7117 ++emsg_off;
7118 p = eval_to_string(expr, NULL, FALSE);
7119 --emsg_off;
7120 vim_free(expr);
7121 }
7122 if (p != NULL)
7123 {
7124 MSG(p);
7125 vim_free(p);
7126 }
7127 else
7128 MSG("default");
7129#else
7130 MSG(_("unknown"));
7131#endif
7132 }
7133 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007134 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135}
7136
7137 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007138ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139{
7140 if (*eap->arg == NUL && eap->cmd[2] == '!')
7141 MSG(_("Greetings, Vim user!"));
7142 do_highlight(eap->arg, eap->forceit, FALSE);
7143}
7144
7145
7146/*
7147 * Call this function if we thought we were going to exit, but we won't
7148 * (because of an error). May need to restore the terminal mode.
7149 */
7150 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007151not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152{
7153 exiting = FALSE;
7154 settmode(TMODE_RAW);
7155}
7156
7157/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007158 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 */
7160 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007161ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162{
Bram Moolenaarf240e182014-11-27 18:33:02 +01007163#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007164 win_T *wp;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007165#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007166
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167#ifdef FEAT_CMDWIN
7168 if (cmdwin_type != 0)
7169 {
7170 cmdwin_result = Ctrl_C;
7171 return;
7172 }
7173#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007174 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007175 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007176 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007177 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007178 return;
7179 }
Bram Moolenaarf240e182014-11-27 18:33:02 +01007180#ifdef FEAT_WINDOWS
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007181 if (eap->addr_count > 0)
7182 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007183 int wnr = eap->line2;
7184
7185 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7186 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007187 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007188 }
7189 else
Bram Moolenaarf240e182014-11-27 18:33:02 +01007190#endif
7191#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007192 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007193#endif
7194
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007195#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02007196 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007197 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02007198 * being closed (can only happen in autocommands). */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007199 if (curbuf_locked() || (wp->w_buffer->b_nwindows == 1
7200 && wp->w_buffer->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007201 return;
7202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203
7204#ifdef FEAT_NETBEANS_INTG
7205 netbeansForcedQuit = eap->forceit;
7206#endif
7207
7208 /*
7209 * If there are more files or windows we won't exit.
7210 */
7211 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7212 exiting = TRUE;
7213 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007214 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7215 | (eap->forceit ? CCGD_FORCEIT : 0)
7216 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007218 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 {
7220 not_exiting();
7221 }
7222 else
7223 {
7224#ifdef FEAT_WINDOWS
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007225 /* quit last window
7226 * Note: only_one_window() returns true, even so a help window is
7227 * still open. In that case only quit, if no address has been
7228 * specified. Example:
7229 * :h|wincmd w|1q - don't quit
7230 * :h|wincmd w|q - quit
7231 */
7232 if (only_one_window() && (firstwin == lastwin || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233#endif
7234 getout(0);
7235#ifdef FEAT_WINDOWS
7236# ifdef FEAT_GUI
7237 need_mouse_correct = TRUE;
7238# endif
7239 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007240 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241#endif
7242 }
7243}
7244
7245/*
7246 * ":cquit".
7247 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007249ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250{
7251 getout(1); /* this does not always pass on the exit code to the Manx
7252 compiler. why? */
7253}
7254
7255/*
7256 * ":qall": try to quit all windows
7257 */
7258 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007259ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260{
7261# ifdef FEAT_CMDWIN
7262 if (cmdwin_type != 0)
7263 {
7264 if (eap->forceit)
7265 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7266 else
7267 cmdwin_result = K_XF2;
7268 return;
7269 }
7270# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007271
7272 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007273 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007274 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007275 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007276 return;
7277 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007278#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007279 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7280 /* Refuse to quit when locked or when the buffer in the last window is
7281 * being closed (can only happen in autocommands). */
7282 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007283 return;
7284#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007285
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007287 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 getout(0);
7289 not_exiting();
7290}
7291
Bram Moolenaard9967712006-03-11 21:18:15 +00007292#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293/*
7294 * ":close": close current window, unless it is the last one
7295 */
7296 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007297ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007299 win_T *win;
7300 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301# ifdef FEAT_CMDWIN
7302 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007303 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 else
7305# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007306 if (!text_locked()
7307#ifdef FEAT_AUTOCMD
7308 && !curbuf_locked()
7309#endif
7310 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007311 {
7312 if (eap->addr_count == 0)
7313 ex_win_close(eap->forceit, curwin, NULL);
7314 else {
Bram Moolenaar29323592016-07-24 22:04:11 +02007315 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007316 {
7317 winnr++;
7318 if (winnr == eap->line2)
7319 break;
7320 }
7321 if (win == NULL)
7322 win = lastwin;
7323 ex_win_close(eap->forceit, win, NULL);
7324 }
7325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326}
7327
Bram Moolenaard9967712006-03-11 21:18:15 +00007328# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007329/*
7330 * ":pclose": Close any preview window.
7331 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007333ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007334{
7335 win_T *win;
7336
Bram Moolenaar29323592016-07-24 22:04:11 +02007337 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007338 if (win->w_p_pvw)
7339 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007340 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007341 break;
7342 }
7343}
Bram Moolenaard9967712006-03-11 21:18:15 +00007344# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007345
Bram Moolenaarf740b292006-02-16 22:11:02 +00007346/*
7347 * Close window "win" and take care of handling closing the last window for a
7348 * modified buffer.
7349 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007350 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007351ex_win_close(
7352 int forceit,
7353 win_T *win,
7354 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355{
7356 int need_hide;
7357 buf_T *buf = win->w_buffer;
7358
7359 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007360 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007362# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 if ((p_confirm || cmdmod.confirm) && p_write)
7364 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007365 bufref_T bufref;
7366
7367 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007369 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 return;
7371 need_hide = FALSE;
7372 }
7373 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007374# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 {
7376 EMSG(_(e_nowrtmsg));
7377 return;
7378 }
7379 }
7380
Bram Moolenaard9967712006-03-11 21:18:15 +00007381# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007383# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007384
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007386 if (tp == NULL)
7387 win_close(win, !need_hide && !P_HID(buf));
7388 else
7389 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390}
7391
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007393 * ":tabclose": close current tab page, unless it is the last one.
7394 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 */
7396 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007397ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007399 tabpage_T *tp;
7400
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007401# ifdef FEAT_CMDWIN
7402 if (cmdwin_type != 0)
7403 cmdwin_result = K_IGNORE;
7404 else
7405# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007406 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007407 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007408 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007410 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007411 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007412 tp = find_tabpage((int)eap->line2);
7413 if (tp == NULL)
7414 {
7415 beep_flush();
7416 return;
7417 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007418 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007419 {
7420 tabpage_close_other(tp, eap->forceit);
7421 return;
7422 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007423 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007424 if (!text_locked()
7425#ifdef FEAT_AUTOCMD
7426 && !curbuf_locked()
7427#endif
7428 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00007429 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007431}
7432
7433/*
7434 * ":tabonly": close all tab pages except the current one
7435 */
7436 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007437ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007438{
7439 tabpage_T *tp;
7440 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007441
7442# ifdef FEAT_CMDWIN
7443 if (cmdwin_type != 0)
7444 cmdwin_result = K_IGNORE;
7445 else
7446# endif
7447 if (first_tabpage->tp_next == NULL)
7448 MSG(_("Already only one tab page"));
7449 else
7450 {
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007451 if (eap->addr_count > 0)
7452 goto_tabpage(eap->line2);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007453 /* Repeat this up to a 1000 times, because autocommands may mess
7454 * up the lists. */
7455 for (done = 0; done < 1000; ++done)
7456 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007457 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007458 if (tp->tp_topframe != topframe)
7459 {
7460 tabpage_close_other(tp, eap->forceit);
7461 /* if we failed to close it quit */
7462 if (valid_tabpage(tp))
7463 done = 1000;
7464 /* start over, "tp" is now invalid */
7465 break;
7466 }
7467 if (first_tabpage->tp_next == NULL)
7468 break;
7469 }
7470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472
7473/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007474 * Close the current tab page.
7475 */
7476 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007477tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007478{
7479 /* First close all the windows but the current one. If that worked then
7480 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007481 if (lastwin != firstwin)
7482 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007483 if (lastwin == firstwin)
7484 ex_win_close(forceit, curwin, NULL);
7485# ifdef FEAT_GUI
7486 need_mouse_correct = TRUE;
7487# endif
7488}
7489
7490/*
7491 * Close tab page "tp", which is not the current tab page.
7492 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007493 * Also takes care of the tab pages line disappearing when closing the
7494 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007495 */
7496 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007497tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007498{
7499 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007500 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007501 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007502
7503 /* Limit to 1000 windows, autocommands may add a window while we close
7504 * one. OK, so I'm paranoid... */
7505 while (++done < 1000)
7506 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007507 wp = tp->tp_firstwin;
7508 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007509
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007510 /* Autocommands may delete the tab page under our fingers and we may
7511 * fail to close a window with a modified buffer. */
7512 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007513 break;
7514 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007515
Bram Moolenaar29323592016-07-24 22:04:11 +02007516#ifdef FEAT_AUTOCMD
7517 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
7518#endif
7519
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007520 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007521 if (h != tabline_height())
7522 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007523}
7524
7525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 * ":only".
7527 */
7528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007529ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007531 win_T *wp;
7532 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533# ifdef FEAT_GUI
7534 need_mouse_correct = TRUE;
7535# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007536 if (eap->addr_count > 0)
7537 {
7538 wnr = eap->line2;
7539 for (wp = firstwin; --wnr > 0; )
7540 {
7541 if (wp->w_next == NULL)
7542 break;
7543 else
7544 wp = wp->w_next;
7545 }
7546 win_goto(wp);
7547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 close_others(TRUE, eap->forceit);
7549}
7550
7551/*
7552 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007553 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007555 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007556ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557{
7558 if (eap->addr_count == 0)
7559 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007560 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561}
7562#endif /* FEAT_WINDOWS */
7563
7564 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007565ex_hide(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566{
7567 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
7568 eap->errmsg = e_invarg;
7569 else
7570 {
7571 /* ":hide" or ":hide | cmd": hide current window */
7572 eap->nextcmd = check_nextcmd(eap->arg);
7573#ifdef FEAT_WINDOWS
7574 if (!eap->skip)
7575 {
7576# ifdef FEAT_GUI
7577 need_mouse_correct = TRUE;
7578# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007579 if (eap->addr_count == 0)
7580 win_close(curwin, FALSE); /* don't free buffer */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007581 else
7582 {
7583 int winnr = 0;
7584 win_T *win;
7585
Bram Moolenaar29323592016-07-24 22:04:11 +02007586 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007587 {
7588 winnr++;
7589 if (winnr == eap->line2)
7590 break;
7591 }
7592 if (win == NULL)
7593 win = lastwin;
7594 win_close(win, FALSE);
7595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 }
7597#endif
7598 }
7599}
7600
7601/*
7602 * ":stop" and ":suspend": Suspend Vim.
7603 */
7604 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007605ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606{
7607 /*
7608 * Disallow suspending for "rvim".
7609 */
7610 if (!check_restricted()
7611#ifdef WIN3264
7612 /*
7613 * Check if external commands are allowed now.
7614 */
7615 && can_end_termcap_mode(TRUE)
7616#endif
7617 )
7618 {
7619 if (!eap->forceit)
7620 autowrite_all();
7621 windgoto((int)Rows - 1, 0);
7622 out_char('\n');
7623 out_flush();
7624 stoptermcap();
7625 out_flush(); /* needed for SUN to restore xterm buffer */
7626#ifdef FEAT_TITLE
7627 mch_restore_title(3); /* restore window titles */
7628#endif
7629 ui_suspend(); /* call machine specific function */
7630#ifdef FEAT_TITLE
7631 maketitle();
7632 resettitle(); /* force updating the title */
7633#endif
7634 starttermcap();
7635 scroll_start(); /* scroll screen before redrawing */
7636 redraw_later_clear();
7637 shell_resized(); /* may have resized window */
7638 }
7639}
7640
7641/*
7642 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7643 */
7644 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007645ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646{
7647#ifdef FEAT_CMDWIN
7648 if (cmdwin_type != 0)
7649 {
7650 cmdwin_result = Ctrl_C;
7651 return;
7652 }
7653#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007654 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007655 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007656 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007657 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007658 return;
7659 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007660#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007661 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7662 /* Refuse to quit when locked or when the buffer in the last window is
7663 * being closed (can only happen in autocommands). */
7664 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007665 return;
7666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667
7668 /*
7669 * if more files or windows we won't exit
7670 */
7671 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7672 exiting = TRUE;
7673 if ( ((eap->cmdidx == CMD_wq
7674 || curbufIsChanged())
7675 && do_write(eap) == FAIL)
7676 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007677 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 {
7679 not_exiting();
7680 }
7681 else
7682 {
7683#ifdef FEAT_WINDOWS
7684 if (only_one_window()) /* quit last window, exit Vim */
7685#endif
7686 getout(0);
7687#ifdef FEAT_WINDOWS
7688# ifdef FEAT_GUI
7689 need_mouse_correct = TRUE;
7690# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007691 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 win_close(curwin, !P_HID(curwin->w_buffer));
7693#endif
7694 }
7695}
7696
7697/*
7698 * ":print", ":list", ":number".
7699 */
7700 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007701ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007703 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7704 EMSG(_(e_emptybuf));
7705 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007707 for ( ;!got_int; ui_breakcheck())
7708 {
7709 print_line(eap->line1,
7710 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7711 || (eap->flags & EXFLAG_NR)),
7712 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7713 if (++eap->line1 > eap->line2)
7714 break;
7715 out_flush(); /* show one line at a time */
7716 }
7717 setpcmark();
7718 /* put cursor at last line */
7719 curwin->w_cursor.lnum = eap->line2;
7720 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 }
7722
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724}
7725
7726#ifdef FEAT_BYTEOFF
7727 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007728ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729{
7730 goto_byte(eap->line2);
7731}
7732#endif
7733
7734/*
7735 * ":shell".
7736 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007738ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739{
7740 do_shell(NULL, 0);
7741}
7742
7743#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7744 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007745 || defined(FEAT_GUI_MSWIN) \
7746 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 || defined(PROTO)
7748
7749/*
7750 * Handle a file drop. The code is here because a drop is *nearly* like an
7751 * :args command, but not quite (we have a list of exact filenames, so we
7752 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7753 * code is very similar to :args and hence needs access to a lot of the static
7754 * functions in this file.
7755 *
7756 * The list should be allocated using alloc(), as should each item in the
7757 * list. This function takes over responsibility for freeing the list.
7758 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007759 * XXX The list is made into the argument list. This is freed using
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007760 * FreeWild(), which does a series of vim_free() calls.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 */
7762 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007763handle_drop(
7764 int filec, /* the number of files dropped */
7765 char_u **filev, /* the list of files dropped */
7766 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767{
7768 exarg_T ea;
7769 int save_msg_scroll = msg_scroll;
7770
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007771 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007772 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007774#ifdef FEAT_AUTOCMD
7775 if (curbuf_locked())
7776 return;
7777#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007778 /* When the screen is being updated we should not change buffers and
7779 * windows structures, it may cause freed memory to be used. */
7780 if (updating_screen)
7781 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782
7783 /* Check whether the current buffer is changed. If so, we will need
7784 * to split the current window or data could be lost.
7785 * We don't need to check if the 'hidden' option is set, as in this
7786 * case the buffer won't be lost.
7787 */
7788 if (!P_HID(curbuf) && !split)
7789 {
7790 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007791 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 --emsg_off;
7793 }
7794 if (split)
7795 {
7796# ifdef FEAT_WINDOWS
7797 if (win_split(0, 0) == FAIL)
7798 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007799 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800
7801 /* When splitting the window, create a new alist. Otherwise the
7802 * existing one is overwritten. */
7803 alist_unlink(curwin->w_alist);
7804 alist_new();
7805# else
7806 return; /* can't split, always fail */
7807# endif
7808 }
7809
7810 /*
7811 * Set up the new argument list.
7812 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007813 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814
7815 /*
7816 * Move to the first file.
7817 */
7818 /* Fake up a minimal "next" command for do_argfile() */
7819 vim_memset(&ea, 0, sizeof(ea));
7820 ea.cmd = (char_u *)"next";
7821 do_argfile(&ea, 0);
7822
7823 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7824 * mode that is not needed here. */
7825 need_start_insertmode = FALSE;
7826
7827 /* Restore msg_scroll, otherwise a following command may cause scrolling
7828 * unexpectedly. The screen will be redrawn by the caller, thus
7829 * msg_scroll being set by displaying a message is irrelevant. */
7830 msg_scroll = save_msg_scroll;
7831}
7832#endif
7833
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834/*
7835 * Clear an argument list: free all file names and reset it to zero entries.
7836 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007837 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007838alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839{
7840 while (--al->al_ga.ga_len >= 0)
7841 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7842 ga_clear(&al->al_ga);
7843}
7844
7845/*
7846 * Init an argument list.
7847 */
7848 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007849alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850{
7851 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7852}
7853
7854#if defined(FEAT_WINDOWS) || defined(PROTO)
7855
7856/*
7857 * Remove a reference from an argument list.
7858 * Ignored when the argument list is the global one.
7859 * If the argument list is no longer used by any window, free it.
7860 */
7861 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007862alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863{
7864 if (al != &global_alist && --al->al_refcount <= 0)
7865 {
7866 alist_clear(al);
7867 vim_free(al);
7868 }
7869}
7870
7871# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7872/*
7873 * Create a new argument list and use it for the current window.
7874 */
7875 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007876alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877{
7878 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7879 if (curwin->w_alist == NULL)
7880 {
7881 curwin->w_alist = &global_alist;
7882 ++global_alist.al_refcount;
7883 }
7884 else
7885 {
7886 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007887 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 alist_init(curwin->w_alist);
7889 }
7890}
7891# endif
7892#endif
7893
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007894#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895/*
7896 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007897 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7898 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 */
7900 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007901alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902{
7903 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007904 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 char_u **new_arg_files;
7906 int new_arg_file_count;
7907 char_u *save_p_su = p_su;
7908 int i;
7909
7910 /* Don't use 'suffixes' here. This should work like the shell did the
7911 * expansion. Also, the vimrc file isn't read yet, thus the user
7912 * can't set the options. */
7913 p_su = empty_option;
7914 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7915 if (old_arg_files != NULL)
7916 {
7917 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007918 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7919 old_arg_count = GARGCOUNT;
7920 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007922 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 && new_arg_file_count > 0)
7924 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007925 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7926 TRUE, fnum_list, fnum_len);
7927 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 }
7930 p_su = save_p_su;
7931}
7932#endif
7933
7934/*
7935 * Set the argument list for the current window.
7936 * Takes over the allocated files[] and the allocated fnames in it.
7937 */
7938 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007939alist_set(
7940 alist_T *al,
7941 int count,
7942 char_u **files,
7943 int use_curbuf,
7944 int *fnum_list,
7945 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946{
7947 int i;
7948
7949 alist_clear(al);
7950 if (ga_grow(&al->al_ga, count) == OK)
7951 {
7952 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007953 {
7954 if (got_int)
7955 {
7956 /* When adding many buffers this can take a long time. Allow
7957 * interrupting here. */
7958 while (i < count)
7959 vim_free(files[i++]);
7960 break;
7961 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007962
7963 /* May set buffer name of a buffer previously used for the
7964 * argument list, so that it's re-used by alist_add. */
7965 if (fnum_list != NULL && i < fnum_len)
7966 buf_set_name(fnum_list[i], files[i]);
7967
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007969 ui_breakcheck();
7970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 vim_free(files);
7972 }
7973 else
7974 FreeWild(count, files);
7975#ifdef FEAT_WINDOWS
7976 if (al == &global_alist)
7977#endif
7978 arg_had_last = FALSE;
7979}
7980
7981/*
7982 * Add file "fname" to argument list "al".
7983 * "fname" must have been allocated and "al" must have been checked for room.
7984 */
7985 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007986alist_add(
7987 alist_T *al,
7988 char_u *fname,
7989 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990{
7991 if (fname == NULL) /* don't add NULL file names */
7992 return;
7993#ifdef BACKSLASH_IN_FILENAME
7994 slash_adjust(fname);
7995#endif
7996 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7997 if (set_fnum > 0)
7998 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7999 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8000 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001}
8002
8003#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8004/*
8005 * Adjust slashes in file names. Called after 'shellslash' was set.
8006 */
8007 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008008alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009{
8010 int i;
8011# ifdef FEAT_WINDOWS
8012 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008013 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014# endif
8015
8016 for (i = 0; i < GARGCOUNT; ++i)
8017 if (GARGLIST[i].ae_fname != NULL)
8018 slash_adjust(GARGLIST[i].ae_fname);
8019# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00008020 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 if (wp->w_alist != &global_alist)
8022 for (i = 0; i < WARGCOUNT(wp); ++i)
8023 if (WARGLIST(wp)[i].ae_fname != NULL)
8024 slash_adjust(WARGLIST(wp)[i].ae_fname);
8025# endif
8026}
8027#endif
8028
8029/*
8030 * ":preserve".
8031 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008033ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008035 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 ml_preserve(curbuf, TRUE);
8037}
8038
8039/*
8040 * ":recover".
8041 */
8042 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008043ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044{
8045 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8046 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008047 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8048 | CCGD_MULTWIN
8049 | (eap->forceit ? CCGD_FORCEIT : 0)
8050 | CCGD_EXCMD)
8051
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 && (*eap->arg == NUL
8053 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8054 ml_recover();
8055 recoverymode = FALSE;
8056}
8057
8058/*
8059 * Command modifier used in a wrong way.
8060 */
8061 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008062ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063{
8064 eap->errmsg = e_invcmd;
8065}
8066
8067#ifdef FEAT_WINDOWS
8068/*
8069 * :sview [+command] file split window with new file, read-only
8070 * :split [[+command] file] split window with current or new file
8071 * :vsplit [[+command] file] split window vertically with current or new file
8072 * :new [[+command] file] split window with no or new file
8073 * :vnew [[+command] file] split vertically window with no or new file
8074 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008075 *
8076 * :tabedit open new Tab page with empty window
8077 * :tabedit [+command] file open new Tab page and edit "file"
8078 * :tabnew [[+command] file] just like :tabedit
8079 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 */
8081 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008082ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008084 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008085# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008087# endif
8088# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008090# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008092# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008094# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008096# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008098 * quickfix windows. But it's OK when doing ":tab split". */
8099 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 {
8101 if (eap->cmdidx == CMD_split)
8102 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 if (eap->cmdidx == CMD_vsplit)
8104 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008106# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008108# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008109 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 {
8111 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8112 FNAME_MESS, TRUE, curbuf->b_ffname);
8113 if (fname == NULL)
8114 goto theend;
8115 eap->arg = fname;
8116 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008117# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008119# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008121# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 && eap->cmdidx != CMD_new)
8125 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008126# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008127 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008128# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008129 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008130# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008131 au_has_group((char_u *)"FileExplorer"))
8132 {
8133 /* No browsing supported but we do have the file explorer:
8134 * Edit the directory. */
8135 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8136 eap->arg = (char_u *)".";
8137 }
8138 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008139# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008140 {
8141 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008143 if (fname == NULL)
8144 goto theend;
8145 eap->arg = fname;
8146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 }
8148 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008149# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008151 /*
8152 * Either open new tab page or split the window.
8153 */
8154 if (eap->cmdidx == CMD_tabedit
8155 || eap->cmdidx == CMD_tabfind
8156 || eap->cmdidx == CMD_tabnew)
8157 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008158 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8159 : eap->addr_count == 0 ? 0
8160 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008161 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008162 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008163
8164 /* set the alternate buffer for the window we came from */
8165 if (curwin != old_curwin
8166 && win_valid(old_curwin)
8167 && old_curwin->w_buffer != curbuf
8168 && !cmdmod.keepalt)
8169 old_curwin->w_alt_fnum = curbuf->b_fnum;
8170 }
8171 }
8172 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8174 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008175# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 /* Reset 'scrollbind' when editing another file, but keep it when
8177 * doing ":split" without arguments. */
8178 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008179# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008181# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008183 {
8184 RESET_BINDING(curwin);
8185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 else
8187 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008188# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 do_exedit(eap, old_curwin);
8190 }
8191
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008192# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008194# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008196# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197theend:
8198 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008199# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008201
8202/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008203 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008204 */
8205 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008206tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008207{
8208 exarg_T ea;
8209
8210 vim_memset(&ea, 0, sizeof(ea));
8211 ea.cmdidx = CMD_tabnew;
8212 ea.cmd = (char_u *)"tabn";
8213 ea.arg = (char_u *)"";
8214 ex_splitview(&ea);
8215}
8216
8217/*
8218 * :tabnext command
8219 */
8220 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008221ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008222{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008223 switch (eap->cmdidx)
8224 {
8225 case CMD_tabfirst:
8226 case CMD_tabrewind:
8227 goto_tabpage(1);
8228 break;
8229 case CMD_tablast:
8230 goto_tabpage(9999);
8231 break;
8232 case CMD_tabprevious:
8233 case CMD_tabNext:
8234 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
8235 break;
8236 default: /* CMD_tabnext */
8237 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
8238 break;
8239 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008240}
8241
8242/*
8243 * :tabmove command
8244 */
8245 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008246ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008247{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008248 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008249
8250 if (eap->arg && *eap->arg != NUL)
8251 {
8252 char_u *p = eap->arg;
8253 int relative = 0; /* argument +N/-N means: move N places to the
8254 * right/left relative to the current position. */
8255
8256 if (*eap->arg == '-')
8257 {
8258 relative = -1;
8259 p = eap->arg + 1;
8260 }
8261 else if (*eap->arg == '+')
8262 {
8263 relative = 1;
8264 p = eap->arg + 1;
8265 }
8266 else
8267 p = eap->arg;
8268
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008269 if (relative == 0)
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008270 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008271 if (STRCMP(p, "$") == 0)
8272 tab_number = LAST_TAB_NR;
8273 else if (p == skipdigits(p))
8274 {
8275 /* No numbers as argument. */
8276 eap->errmsg = e_invarg;
8277 return;
8278 }
8279 else
8280 tab_number = getdigits(&p);
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008281 }
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008282 else
8283 {
8284 if (*p != NUL)
8285 tab_number = getdigits(&p);
8286 else
8287 tab_number = 1;
8288 tab_number = tab_number * relative + tabpage_index(curtab);
8289 if (relative == -1)
8290 --tab_number;
8291 }
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008292 }
8293 else if (eap->addr_count != 0)
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008294 {
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008295 tab_number = eap->line2;
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008296 if (**eap->cmdlinep == '-')
8297 --tab_number;
8298 }
8299 else
8300 tab_number = LAST_TAB_NR;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008301
8302 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008303}
8304
8305/*
8306 * :tabs command: List tabs and their contents.
8307 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008308 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008309ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008310{
8311 tabpage_T *tp;
8312 win_T *wp;
8313 int tabcount = 1;
8314
8315 msg_start();
8316 msg_scroll = TRUE;
8317 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8318 {
8319 msg_putchar('\n');
8320 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
8321 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
8322 out_flush(); /* output one line at a time */
8323 ui_breakcheck();
8324
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008325 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008326 wp = firstwin;
8327 else
8328 wp = tp->tp_firstwin;
8329 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8330 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008331 msg_putchar('\n');
8332 msg_putchar(wp == curwin ? '>' : ' ');
8333 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008334 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8335 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008336 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008337 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008338 else
8339 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8340 IObuff, IOSIZE, TRUE);
8341 msg_outtrans(IObuff);
8342 out_flush(); /* output one line at a time */
8343 ui_breakcheck();
8344 }
8345 }
8346}
8347
8348#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349
8350/*
8351 * ":mode": Set screen mode.
8352 * If no argument given, just get the screen size and redraw.
8353 */
8354 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008355ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356{
8357 if (*eap->arg == NUL)
8358 shell_resized();
8359 else
8360 mch_screenmode(eap->arg);
8361}
8362
8363#ifdef FEAT_WINDOWS
8364/*
8365 * ":resize".
8366 * set, increment or decrement current window height
8367 */
8368 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008369ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370{
8371 int n;
8372 win_T *wp = curwin;
8373
8374 if (eap->addr_count > 0)
8375 {
8376 n = eap->line2;
8377 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8378 ;
8379 }
8380
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008381# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008383# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 if (cmdmod.split & WSP_VERT)
8386 {
8387 if (*eap->arg == '-' || *eap->arg == '+')
8388 n += W_WIDTH(curwin);
8389 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8390 n = 9999;
8391 win_setwidth_win((int)n, wp);
8392 }
8393 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 {
8395 if (*eap->arg == '-' || *eap->arg == '+')
8396 n += curwin->w_height;
8397 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8398 n = 9999;
8399 win_setheight_win((int)n, wp);
8400 }
8401}
8402#endif
8403
8404/*
8405 * ":find [+command] <file>" command.
8406 */
8407 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008408ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409{
8410#ifdef FEAT_SEARCHPATH
8411 char_u *fname;
8412 int count;
8413
8414 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8415 TRUE, curbuf->b_ffname);
8416 if (eap->addr_count > 0)
8417 {
8418 /* Repeat finding the file "count" times. This matters when it
8419 * appears several times in the path. */
8420 count = eap->line2;
8421 while (fname != NULL && --count > 0)
8422 {
8423 vim_free(fname);
8424 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8425 FALSE, curbuf->b_ffname);
8426 }
8427 }
8428
8429 if (fname != NULL)
8430 {
8431 eap->arg = fname;
8432#endif
8433 do_exedit(eap, NULL);
8434#ifdef FEAT_SEARCHPATH
8435 vim_free(fname);
8436 }
8437#endif
8438}
8439
8440/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008441 * ":open" simulation: for now just work like ":visual".
8442 */
8443 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008444ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008445{
8446 regmatch_T regmatch;
8447 char_u *p;
8448
8449 curwin->w_cursor.lnum = eap->line2;
8450 beginline(BL_SOL | BL_FIX);
8451 if (*eap->arg == '/')
8452 {
8453 /* ":open /pattern/": put cursor in column found with pattern */
8454 ++eap->arg;
8455 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8456 *p = NUL;
8457 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8458 if (regmatch.regprog != NULL)
8459 {
8460 regmatch.rm_ic = p_ic;
8461 p = ml_get_curline();
8462 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008463 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008464 else
8465 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008466 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008467 }
8468 /* Move to the NUL, ignore any other arguments. */
8469 eap->arg += STRLEN(eap->arg);
8470 }
8471 check_cursor();
8472
8473 eap->cmdidx = CMD_visual;
8474 do_exedit(eap, NULL);
8475}
8476
8477/*
8478 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 */
8480 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008481ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482{
8483 do_exedit(eap, NULL);
8484}
8485
8486/*
8487 * ":edit <file>" command and alikes.
8488 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008490do_exedit(
8491 exarg_T *eap,
8492 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493{
8494 int n;
8495#ifdef FEAT_WINDOWS
8496 int need_hide;
8497#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008498 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499
8500 /*
8501 * ":vi" command ends Ex mode.
8502 */
8503 if (exmode_active && (eap->cmdidx == CMD_visual
8504 || eap->cmdidx == CMD_view))
8505 {
8506 exmode_active = FALSE;
8507 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008508 {
8509 /* Special case: ":global/pat/visual\NLvi-commands" */
8510 if (global_busy)
8511 {
8512 int rd = RedrawingDisabled;
8513 int nwr = no_wait_return;
8514 int ms = msg_scroll;
8515#ifdef FEAT_GUI
8516 int he = hold_gui_events;
8517#endif
8518
8519 if (eap->nextcmd != NULL)
8520 {
8521 stuffReadbuff(eap->nextcmd);
8522 eap->nextcmd = NULL;
8523 }
8524
8525 if (exmode_was != EXMODE_VIM)
8526 settmode(TMODE_RAW);
8527 RedrawingDisabled = 0;
8528 no_wait_return = 0;
8529 need_wait_return = FALSE;
8530 msg_scroll = 0;
8531#ifdef FEAT_GUI
8532 hold_gui_events = 0;
8533#endif
8534 must_redraw = CLEAR;
8535
8536 main_loop(FALSE, TRUE);
8537
8538 RedrawingDisabled = rd;
8539 no_wait_return = nwr;
8540 msg_scroll = ms;
8541#ifdef FEAT_GUI
8542 hold_gui_events = he;
8543#endif
8544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 }
8548
8549 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008550 || eap->cmdidx == CMD_tabnew
8551 || eap->cmdidx == CMD_tabedit
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008552#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 || eap->cmdidx == CMD_vnew
8554#endif
8555 ) && *eap->arg == NUL)
8556 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008557 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 setpcmark();
8559 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008560 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8561 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 }
8563 else if ((eap->cmdidx != CMD_split
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008564#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 && eap->cmdidx != CMD_vsplit
8566#endif
8567 )
8568 || *eap->arg != NUL
8569#ifdef FEAT_BROWSE
8570 || cmdmod.browse
8571#endif
8572 )
8573 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008574#ifdef FEAT_AUTOCMD
8575 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8576 * can bring us here, others are stopped earlier. */
8577 if (*eap->arg != NUL && curbuf_locked())
8578 return;
8579#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 n = readonlymode;
8581 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8582 readonlymode = TRUE;
8583 else if (eap->cmdidx == CMD_enew)
8584 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8585 empty buffer */
8586 setpcmark();
8587 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8588 NULL, eap,
8589 /* ":edit" goes to first line if Vi compatible */
8590 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8591 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8592 ? ECMD_ONE : eap->do_ecmd_lnum,
8593 (P_HID(curbuf) ? ECMD_HIDE : 0)
8594 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008595 /* after a split we can use an existing buffer */
8596 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597#ifdef FEAT_LISTCMDS
8598 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8599#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008600 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 {
8602 /* Editing the file failed. If the window was split, close it. */
8603#ifdef FEAT_WINDOWS
8604 if (old_curwin != NULL)
8605 {
8606 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8607 if (!need_hide || P_HID(curbuf))
8608 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008609# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8610 cleanup_T cs;
8611
8612 /* Reset the error/interrupt/exception state here so that
8613 * aborting() returns FALSE when closing a window. */
8614 enter_cleanup(&cs);
8615# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616# ifdef FEAT_GUI
8617 need_mouse_correct = TRUE;
8618# endif
8619 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008620
8621# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8622 /* Restore the error/interrupt/exception state if not
8623 * discarded by a new aborting error, interrupt, or
8624 * uncaught exception. */
8625 leave_cleanup(&cs);
8626# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 }
8628 }
8629#endif
8630 }
8631 else if (readonlymode && curbuf->b_nwindows == 1)
8632 {
8633 /* When editing an already visited buffer, 'readonly' won't be set
8634 * but the previous value is kept. With ":view" and ":sview" we
8635 * want the file to be readonly, except when another window is
8636 * editing the same buffer. */
8637 curbuf->b_p_ro = TRUE;
8638 }
8639 readonlymode = n;
8640 }
8641 else
8642 {
8643 if (eap->do_ecmd_cmd != NULL)
8644 do_cmdline_cmd(eap->do_ecmd_cmd);
8645#ifdef FEAT_TITLE
8646 n = curwin->w_arg_idx_invalid;
8647#endif
8648 check_arg_idx(curwin);
8649#ifdef FEAT_TITLE
8650 if (n != curwin->w_arg_idx_invalid)
8651 maketitle();
8652#endif
8653 }
8654
8655#ifdef FEAT_WINDOWS
8656 /*
8657 * if ":split file" worked, set alternate file name in old window to new
8658 * file
8659 */
8660 if (old_curwin != NULL
8661 && *eap->arg != NUL
8662 && curwin != old_curwin
8663 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008664 && old_curwin->w_buffer != curbuf
8665 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 old_curwin->w_alt_fnum = curbuf->b_fnum;
8667#endif
8668
8669 ex_no_reprint = TRUE;
8670}
8671
8672#ifndef FEAT_GUI
8673/*
8674 * ":gui" and ":gvim" when there is no GUI.
8675 */
8676 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008677ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678{
8679 eap->errmsg = e_nogvim;
8680}
8681#endif
8682
8683#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8684 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008685ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686{
8687 gui_make_tearoff(eap->arg);
8688}
8689#endif
8690
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008691#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008693ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008695 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696}
8697#endif
8698
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008700ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701{
8702 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8703 MSG(_("No swap file"));
8704 else
8705 msg(curbuf->b_ml.ml_mfp->mf_fname);
8706}
8707
8708/*
8709 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8710 * offset.
8711 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8712 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008714ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715{
8716#ifdef FEAT_SCROLLBIND
8717 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008718 win_T *save_curwin = curwin;
8719 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 long topline;
8721 long y;
8722 linenr_T old_linenr = curwin->w_cursor.lnum;
8723
8724 setpcmark();
8725
8726 /*
8727 * determine max topline
8728 */
8729 if (curwin->w_p_scb)
8730 {
8731 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008732 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 {
8734 if (wp->w_p_scb && wp->w_buffer)
8735 {
8736 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8737 if (topline > y)
8738 topline = y;
8739 }
8740 }
8741 if (topline < 1)
8742 topline = 1;
8743 }
8744 else
8745 {
8746 topline = 1;
8747 }
8748
8749
8750 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008751 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008753 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 {
8755 if (curwin->w_p_scb)
8756 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008757 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 y = topline - curwin->w_topline;
8759 if (y > 0)
8760 scrollup(y, TRUE);
8761 else
8762 scrolldown(-y, TRUE);
8763 curwin->w_scbind_pos = topline;
8764 redraw_later(VALID);
8765 cursor_correct();
8766#ifdef FEAT_WINDOWS
8767 curwin->w_redr_status = TRUE;
8768#endif
8769 }
8770 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008771 curwin = save_curwin;
8772 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 if (curwin->w_p_scb)
8774 {
8775 did_syncbind = TRUE;
8776 checkpcmark();
8777 if (old_linenr != curwin->w_cursor.lnum)
8778 {
8779 char_u ctrl_o[2];
8780
8781 ctrl_o[0] = Ctrl_O;
8782 ctrl_o[1] = 0;
8783 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8784 }
8785 }
8786#endif
8787}
8788
8789
8790 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008791ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008793 int i;
8794 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8795 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796
8797 if (eap->usefilter) /* :r!cmd */
8798 do_bang(1, eap, FALSE, FALSE, TRUE);
8799 else
8800 {
8801 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8802 return;
8803
8804#ifdef FEAT_BROWSE
8805 if (cmdmod.browse)
8806 {
8807 char_u *browseFile;
8808
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008809 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 NULL, NULL, NULL, curbuf);
8811 if (browseFile != NULL)
8812 {
8813 i = readfile(browseFile, NULL,
8814 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8815 vim_free(browseFile);
8816 }
8817 else
8818 i = OK;
8819 }
8820 else
8821#endif
8822 if (*eap->arg == NUL)
8823 {
8824 if (check_fname() == FAIL) /* check for no file name */
8825 return;
8826 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8827 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8828 }
8829 else
8830 {
8831 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8832 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8833 i = readfile(eap->arg, NULL,
8834 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8835
8836 }
8837 if (i == FAIL)
8838 {
8839#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8840 if (!aborting())
8841#endif
8842 EMSG2(_(e_notopen), eap->arg);
8843 }
8844 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008845 {
8846 if (empty && exmode_active)
8847 {
8848 /* Delete the empty line that remains. Historically ex does
8849 * this but vi doesn't. */
8850 if (eap->line2 == 0)
8851 lnum = curbuf->b_ml.ml_line_count;
8852 else
8853 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008854 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008855 {
8856 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008857 if (curwin->w_cursor.lnum > 1
8858 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008859 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008860 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008861 }
8862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 }
8866}
8867
Bram Moolenaarea408852005-06-25 22:49:46 +00008868static char_u *prev_dir = NULL;
8869
8870#if defined(EXITFREE) || defined(PROTO)
8871 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008872free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00008873{
8874 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008875 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008876
8877 vim_free(globaldir);
8878 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008879}
8880#endif
8881
Bram Moolenaarf4258302013-06-02 18:20:17 +02008882/*
8883 * Deal with the side effects of changing the current directory.
8884 * When "local" is TRUE then this was after an ":lcd" command.
8885 */
8886 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008887post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008888{
8889 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008890 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008891 if (local)
8892 {
8893 /* If still in global directory, need to remember current
8894 * directory as global directory. */
8895 if (globaldir == NULL && prev_dir != NULL)
8896 globaldir = vim_strsave(prev_dir);
8897 /* Remember this local directory for the window. */
8898 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8899 curwin->w_localdir = vim_strsave(NameBuff);
8900 }
8901 else
8902 {
8903 /* We are now in the global directory, no need to remember its
8904 * name. */
8905 vim_free(globaldir);
8906 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008907 }
8908
8909 shorten_fnames(TRUE);
8910}
8911
Bram Moolenaarea408852005-06-25 22:49:46 +00008912
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913/*
8914 * ":cd", ":lcd", ":chdir" and ":lchdir".
8915 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008916 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008917ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 char_u *new_dir;
8920 char_u *tofree;
8921
8922 new_dir = eap->arg;
8923#if !defined(UNIX) && !defined(VMS)
8924 /* for non-UNIX ":cd" means: print current directory */
8925 if (*new_dir == NUL)
8926 ex_pwd(NULL);
8927 else
8928#endif
8929 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008930#ifdef FEAT_AUTOCMD
8931 if (allbuf_locked())
8932 return;
8933#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008934 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8935 && !eap->forceit)
8936 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008937 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008938 return;
8939 }
8940
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 /* ":cd -": Change to previous directory */
8942 if (STRCMP(new_dir, "-") == 0)
8943 {
8944 if (prev_dir == NULL)
8945 {
8946 EMSG(_("E186: No previous directory"));
8947 return;
8948 }
8949 new_dir = prev_dir;
8950 }
8951
8952 /* Save current directory for next ":cd -" */
8953 tofree = prev_dir;
8954 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8955 prev_dir = vim_strsave(NameBuff);
8956 else
8957 prev_dir = NULL;
8958
8959#if defined(UNIX) || defined(VMS)
8960 /* for UNIX ":cd" means: go to home directory */
8961 if (*new_dir == NUL)
8962 {
8963 /* use NameBuff for home directory name */
8964# ifdef VMS
8965 char_u *p;
8966
8967 p = mch_getenv((char_u *)"SYS$LOGIN");
8968 if (p == NULL || *p == NUL) /* empty is the same as not set */
8969 NameBuff[0] = NUL;
8970 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008971 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972# else
8973 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8974# endif
8975 new_dir = NameBuff;
8976 }
8977#endif
8978 if (new_dir == NULL || vim_chdir(new_dir))
8979 EMSG(_(e_failed));
8980 else
8981 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008982 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983
8984 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008985 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986 ex_pwd(eap);
8987 }
8988 vim_free(tofree);
8989 }
8990}
8991
8992/*
8993 * ":pwd".
8994 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008996ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997{
8998 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8999 {
9000#ifdef BACKSLASH_IN_FILENAME
9001 slash_adjust(NameBuff);
9002#endif
9003 msg(NameBuff);
9004 }
9005 else
9006 EMSG(_("E187: Unknown"));
9007}
9008
9009/*
9010 * ":=".
9011 */
9012 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009013ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014{
9015 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009016 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017}
9018
9019 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009020ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009022 int n;
9023 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024
9025 if (cursor_valid())
9026 {
9027 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9028 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01009029 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009031
9032 len = eap->line2;
9033 switch (*eap->arg)
9034 {
9035 case 'm': break;
9036 case NUL: len *= 1000L; break;
9037 default: EMSG2(_(e_invarg2), eap->arg); return;
9038 }
9039 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040}
9041
9042/*
9043 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9044 */
9045 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009046do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047{
9048 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009049 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050
9051 cursor_on();
9052 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009053 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009055 wait_now = msec - done > 1000L ? 1000L : msec - done;
9056#ifdef FEAT_TIMERS
9057 {
9058 long due_time = check_due_timer();
9059
9060 if (due_time > 0 && due_time < wait_now)
9061 wait_now = due_time;
9062 }
9063#endif
9064 ui_delay(wait_now, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009066#ifdef MESSAGE_QUEUE
9067 /* Process the netbeans and clientserver messages that may have been
9068 * received in the call to ui_breakcheck() when the GUI is in use. This
9069 * may occur when running a test case. */
9070 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 }
9073}
9074
9075 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009076do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077{
9078 int mode;
9079 char_u *cmdp;
9080
9081 cmdp = eap->cmd;
9082 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9083
9084 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9085 eap->arg, mode, isabbrev))
9086 {
9087 case 1: EMSG(_(e_invarg));
9088 break;
9089 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9090 break;
9091 }
9092}
9093
9094/*
9095 * ":winsize" command (obsolete).
9096 */
9097 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009098ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099{
9100 int w, h;
9101 char_u *arg = eap->arg;
9102 char_u *p;
9103
9104 w = getdigits(&arg);
9105 arg = skipwhite(arg);
9106 p = arg;
9107 h = getdigits(&arg);
9108 if (*p != NUL && *arg == NUL)
9109 set_shellsize(w, h, TRUE);
9110 else
9111 EMSG(_("E465: :winsize requires two number arguments"));
9112}
9113
9114#ifdef FEAT_WINDOWS
9115 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009116ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117{
9118 int xchar = NUL;
9119 char_u *p;
9120
9121 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9122 {
9123 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9124 if (eap->arg[1] == NUL)
9125 {
9126 EMSG(_(e_invarg));
9127 return;
9128 }
9129 xchar = eap->arg[1];
9130 p = eap->arg + 2;
9131 }
9132 else
9133 p = eap->arg + 1;
9134
9135 eap->nextcmd = check_nextcmd(p);
9136 p = skipwhite(p);
9137 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9138 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009139 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140 {
9141 /* Pass flags on for ":vertical wincmd ]". */
9142 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009143 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9145 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009146 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 }
9148}
9149#endif
9150
Bram Moolenaar843ee412004-06-30 16:16:41 +00009151#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152/*
9153 * ":winpos".
9154 */
9155 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009156ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157{
9158 int x, y;
9159 char_u *arg = eap->arg;
9160 char_u *p;
9161
9162 if (*arg == NUL)
9163 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009164# if defined(FEAT_GUI) || defined(MSWIN)
9165# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009167# else
9168 if (mch_get_winpos(&x, &y) != FAIL)
9169# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 {
9171 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9172 msg(IObuff);
9173 }
9174 else
9175# endif
9176 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9177 }
9178 else
9179 {
9180 x = getdigits(&arg);
9181 arg = skipwhite(arg);
9182 p = arg;
9183 y = getdigits(&arg);
9184 if (*p == NUL || *arg != NUL)
9185 {
9186 EMSG(_("E466: :winpos requires two number arguments"));
9187 return;
9188 }
9189# ifdef FEAT_GUI
9190 if (gui.in_use)
9191 gui_mch_set_winpos(x, y);
9192 else if (gui.starting)
9193 {
9194 /* Remember the coordinates for when the window is opened. */
9195 gui_win_x = x;
9196 gui_win_y = y;
9197 }
9198# ifdef HAVE_TGETENT
9199 else
9200# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009201# else
9202# ifdef MSWIN
9203 mch_set_winpos(x, y);
9204# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205# endif
9206# ifdef HAVE_TGETENT
9207 if (*T_CWP)
9208 term_set_winpos(x, y);
9209# endif
9210 }
9211}
9212#endif
9213
9214/*
9215 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9216 */
9217 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009218ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219{
9220 oparg_T oa;
9221
9222 clear_oparg(&oa);
9223 oa.regname = eap->regname;
9224 oa.start.lnum = eap->line1;
9225 oa.end.lnum = eap->line2;
9226 oa.line_count = eap->line2 - eap->line1 + 1;
9227 oa.motion_type = MLINE;
9228#ifdef FEAT_VIRTUALEDIT
9229 virtual_op = FALSE;
9230#endif
9231 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9232 {
9233 setpcmark();
9234 curwin->w_cursor.lnum = eap->line1;
9235 beginline(BL_SOL | BL_FIX);
9236 }
9237
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009238 if (VIsual_active)
9239 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009240
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 switch (eap->cmdidx)
9242 {
9243 case CMD_delete:
9244 oa.op_type = OP_DELETE;
9245 op_delete(&oa);
9246 break;
9247
9248 case CMD_yank:
9249 oa.op_type = OP_YANK;
9250 (void)op_yank(&oa, FALSE, TRUE);
9251 break;
9252
9253 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009254 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009256 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9257#else
9258 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009260 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261 oa.op_type = OP_RSHIFT;
9262 else
9263 oa.op_type = OP_LSHIFT;
9264 op_shift(&oa, FALSE, eap->amount);
9265 break;
9266 }
9267#ifdef FEAT_VIRTUALEDIT
9268 virtual_op = MAYBE;
9269#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009270 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271}
9272
9273/*
9274 * ":put".
9275 */
9276 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009277ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278{
9279 /* ":0put" works like ":1put!". */
9280 if (eap->line2 == 0)
9281 {
9282 eap->line2 = 1;
9283 eap->forceit = TRUE;
9284 }
9285 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009286 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9287 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288}
9289
9290/*
9291 * Handle ":copy" and ":move".
9292 */
9293 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009294ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295{
9296 long n;
9297
Bram Moolenaaraa23b372015-09-08 18:46:31 +02009298 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299 if (eap->arg == NULL) /* error detected */
9300 {
9301 eap->nextcmd = NULL;
9302 return;
9303 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009304 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305
9306 /*
9307 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9308 */
9309 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9310 {
9311 EMSG(_(e_invaddr));
9312 return;
9313 }
9314
9315 if (eap->cmdidx == CMD_move)
9316 {
9317 if (do_move(eap->line1, eap->line2, n) == FAIL)
9318 return;
9319 }
9320 else
9321 ex_copy(eap->line1, eap->line2, n);
9322 u_clearline();
9323 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009324 ex_may_print(eap);
9325}
9326
9327/*
9328 * Print the current line if flags were given to the Ex command.
9329 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009330 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009331ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009332{
9333 if (eap->flags != 0)
9334 {
9335 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9336 (eap->flags & EXFLAG_LIST));
9337 ex_no_reprint = TRUE;
9338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339}
9340
9341/*
9342 * ":smagic" and ":snomagic".
9343 */
9344 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009345ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346{
9347 int magic_save = p_magic;
9348
9349 p_magic = (eap->cmdidx == CMD_smagic);
9350 do_sub(eap);
9351 p_magic = magic_save;
9352}
9353
9354/*
9355 * ":join".
9356 */
9357 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009358ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359{
9360 curwin->w_cursor.lnum = eap->line1;
9361 if (eap->line1 == eap->line2)
9362 {
9363 if (eap->addr_count >= 2) /* :2,2join does nothing */
9364 return;
9365 if (eap->line2 == curbuf->b_ml.ml_line_count)
9366 {
9367 beep_flush();
9368 return;
9369 }
9370 ++eap->line2;
9371 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009372 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009374 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375}
9376
9377/*
9378 * ":[addr]@r" or ":[addr]*r": execute register
9379 */
9380 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009381ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382{
9383 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009384 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385
9386 curwin->w_cursor.lnum = eap->line2;
9387
9388#ifdef USE_ON_FLY_SCROLL
9389 dont_scroll = TRUE; /* disallow scrolling here */
9390#endif
9391
9392 /* get the register name. No name means to use the previous one */
9393 c = *eap->arg;
9394 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9395 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009396 /* Put the register in the typeahead buffer with the "silent" flag. */
9397 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9398 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009399 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402 else
9403 {
9404 int save_efr = exec_from_reg;
9405
9406 exec_from_reg = TRUE;
9407
9408 /*
9409 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009410 * Continue until the stuff buffer is empty and all added characters
9411 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009413 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9415
9416 exec_from_reg = save_efr;
9417 }
9418}
9419
9420/*
9421 * ":!".
9422 */
9423 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009424ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425{
9426 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9427}
9428
9429/*
9430 * ":undo".
9431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009433ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009435 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009436 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009437 else
9438 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439}
9440
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009441#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009442 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009443ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009444{
9445 char_u hash[UNDO_HASH_SIZE];
9446
9447 u_compute_hash(hash);
9448 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9449}
9450
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009451 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009452ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009453{
9454 char_u hash[UNDO_HASH_SIZE];
9455
9456 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009457 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009458}
9459#endif
9460
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461/*
9462 * ":redo".
9463 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009465ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466{
9467 u_redo(1);
9468}
9469
9470/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009471 * ":earlier" and ":later".
9472 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009473 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009474ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009475{
9476 long count = 0;
9477 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009478 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009479 char_u *p = eap->arg;
9480
9481 if (*p == NUL)
9482 count = 1;
9483 else if (isdigit(*p))
9484 {
9485 count = getdigits(&p);
9486 switch (*p)
9487 {
9488 case 's': ++p; sec = TRUE; break;
9489 case 'm': ++p; sec = TRUE; count *= 60; break;
9490 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009491 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9492 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009493 }
9494 }
9495
9496 if (*p != NUL)
9497 EMSG2(_(e_invarg2), eap->arg);
9498 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009499 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9500 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009501}
9502
9503/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 * ":redir": start/stop redirection.
9505 */
9506 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009507ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508{
9509 char *mode;
9510 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009511 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512
Bram Moolenaarba768492016-07-08 15:32:54 +02009513#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009514 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009515 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009516 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009517 return;
9518 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009519#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009520
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 if (STRICMP(eap->arg, "END") == 0)
9522 close_redir();
9523 else
9524 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009525 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009527 ++arg;
9528 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009530 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 mode = "a";
9532 }
9533 else
9534 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009535 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536
9537 close_redir();
9538
9539 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009540 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 if (fname == NULL)
9542 return;
9543#ifdef FEAT_BROWSE
9544 if (cmdmod.browse)
9545 {
9546 char_u *browseFile;
9547
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009548 browseFile = do_browse(BROWSE_SAVE,
9549 (char_u *)_("Save Redirection"),
9550 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 if (browseFile == NULL)
9552 return; /* operation cancelled */
9553 vim_free(fname);
9554 fname = browseFile;
9555 eap->forceit = TRUE; /* since dialog already asked */
9556 }
9557#endif
9558
9559 redir_fd = open_exfile(fname, eap->forceit, mode);
9560 vim_free(fname);
9561 }
9562#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009563 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 {
9565 /* redirect to a register a-z (resp. A-Z for appending) */
9566 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009567 ++arg;
9568 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009570 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009571 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009573 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009575 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009576 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009577 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009578 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009580 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009581 if (*arg == '>')
9582 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009583 /* Make register empty when not using @A-@Z and the
9584 * command is valid. */
9585 if (*arg == NUL && !isupper(redir_reg))
9586 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009588 }
9589 if (*arg != NUL)
9590 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009591 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009592 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009593 }
9594 }
9595 else if (*arg == '=' && arg[1] == '>')
9596 {
9597 int append;
9598
9599 /* redirect to a variable */
9600 close_redir();
9601 arg += 2;
9602
9603 if (*arg == '>')
9604 {
9605 ++arg;
9606 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 }
9608 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009609 append = FALSE;
9610
9611 if (var_redir_start(skipwhite(arg), append) == OK)
9612 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 }
9614#endif
9615
9616 /* TODO: redirect to a buffer */
9617
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009619 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009621
9622 /* Make sure redirection is not off. Can happen for cmdline completion
9623 * that indirectly invokes a command to catch its output. */
9624 if (redir_fd != NULL
9625#ifdef FEAT_EVAL
9626 || redir_reg || redir_vname
9627#endif
9628 )
9629 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630}
9631
9632/*
9633 * ":redraw": force redraw
9634 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009635 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009636ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637{
9638 int r = RedrawingDisabled;
9639 int p = p_lz;
9640
9641 RedrawingDisabled = 0;
9642 p_lz = FALSE;
9643 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009644 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645#ifdef FEAT_TITLE
9646 if (need_maketitle)
9647 maketitle();
9648#endif
9649 RedrawingDisabled = r;
9650 p_lz = p;
9651
9652 /* Reset msg_didout, so that a message that's there is overwritten. */
9653 msg_didout = FALSE;
9654 msg_col = 0;
9655
Bram Moolenaar56667a52013-07-17 11:54:28 +02009656 /* No need to wait after an intentional redraw. */
9657 need_wait_return = FALSE;
9658
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 out_flush();
9660}
9661
9662/*
9663 * ":redrawstatus": force redraw of status line(s)
9664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009666ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667{
9668#if defined(FEAT_WINDOWS)
9669 int r = RedrawingDisabled;
9670 int p = p_lz;
9671
9672 RedrawingDisabled = 0;
9673 p_lz = FALSE;
9674 if (eap->forceit)
9675 status_redraw_all();
9676 else
9677 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009678 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 RedrawingDisabled = r;
9680 p_lz = p;
9681 out_flush();
9682#endif
9683}
9684
9685 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009686close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687{
9688 if (redir_fd != NULL)
9689 {
9690 fclose(redir_fd);
9691 redir_fd = NULL;
9692 }
9693#ifdef FEAT_EVAL
9694 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009695 if (redir_vname)
9696 {
9697 var_redir_stop();
9698 redir_vname = 0;
9699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700#endif
9701}
9702
9703#if defined(FEAT_SESSION) && defined(USE_CRNL)
9704# define MKSESSION_NL
9705static int mksession_nl = FALSE; /* use NL only in put_eol() */
9706#endif
9707
9708/*
9709 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9710 */
9711 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009712ex_mkrc(
9713 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714{
9715 FILE *fd;
9716 int failed = FALSE;
9717 char_u *fname;
9718#ifdef FEAT_BROWSE
9719 char_u *browseFile = NULL;
9720#endif
9721#ifdef FEAT_SESSION
9722 int view_session = FALSE;
9723 int using_vdir = FALSE; /* using 'viewdir'? */
9724 char_u *viewFile = NULL;
9725 unsigned *flagp;
9726#endif
9727
9728 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9729 {
9730#ifdef FEAT_SESSION
9731 view_session = TRUE;
9732#else
9733 ex_ni(eap);
9734 return;
9735#endif
9736 }
9737
9738#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009739 /* Use the short file name until ":lcd" is used. We also don't use the
9740 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009741 did_lcd = FALSE;
9742
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9744 if (eap->cmdidx == CMD_mkview
9745 && (*eap->arg == NUL
9746 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9747 {
9748 eap->forceit = TRUE;
9749 fname = get_view_file(*eap->arg);
9750 if (fname == NULL)
9751 return;
9752 viewFile = fname;
9753 using_vdir = TRUE;
9754 }
9755 else
9756#endif
9757 if (*eap->arg != NUL)
9758 fname = eap->arg;
9759 else if (eap->cmdidx == CMD_mkvimrc)
9760 fname = (char_u *)VIMRC_FILE;
9761#ifdef FEAT_SESSION
9762 else if (eap->cmdidx == CMD_mksession)
9763 fname = (char_u *)SESSION_FILE;
9764#endif
9765 else
9766 fname = (char_u *)EXRC_FILE;
9767
9768#ifdef FEAT_BROWSE
9769 if (cmdmod.browse)
9770 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009771 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772# ifdef FEAT_SESSION
9773 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9774 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9775# endif
9776 (char_u *)_("Save Setup"),
9777 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9778 if (browseFile == NULL)
9779 goto theend;
9780 fname = browseFile;
9781 eap->forceit = TRUE; /* since dialog already asked */
9782 }
9783#endif
9784
9785#if defined(FEAT_SESSION) && defined(vim_mkdir)
9786 /* When using 'viewdir' may have to create the directory. */
9787 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009788 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789#endif
9790
9791 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9792 if (fd != NULL)
9793 {
9794#ifdef FEAT_SESSION
9795 if (eap->cmdidx == CMD_mkview)
9796 flagp = &vop_flags;
9797 else
9798 flagp = &ssop_flags;
9799#endif
9800
9801#ifdef MKSESSION_NL
9802 /* "unix" in 'sessionoptions': use NL line separator */
9803 if (view_session && (*flagp & SSOP_UNIX))
9804 mksession_nl = TRUE;
9805#endif
9806
9807 /* Write the version command for :mkvimrc */
9808 if (eap->cmdidx == CMD_mkvimrc)
9809 (void)put_line(fd, "version 6.0");
9810
9811#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009812 if (eap->cmdidx == CMD_mksession)
9813 {
9814 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9815 failed = TRUE;
9816 }
9817
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 if (eap->cmdidx != CMD_mkview)
9819#endif
9820 {
9821 /* Write setting 'compatible' first, because it has side effects.
9822 * For that same reason only do it when needed. */
9823 if (p_cp)
9824 (void)put_line(fd, "if !&cp | set cp | endif");
9825 else
9826 (void)put_line(fd, "if &cp | set nocp | endif");
9827 }
9828
9829#ifdef FEAT_SESSION
9830 if (!view_session
9831 || (eap->cmdidx == CMD_mksession
9832 && (*flagp & SSOP_OPTIONS)))
9833#endif
9834 failed |= (makemap(fd, NULL) == FAIL
9835 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9836
9837#ifdef FEAT_SESSION
9838 if (!failed && view_session)
9839 {
9840 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9841 failed = TRUE;
9842 if (eap->cmdidx == CMD_mksession)
9843 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009844 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845
Bram Moolenaard9462e32011-04-11 21:35:11 +02009846 dirnow = alloc(MAXPATHL);
9847 if (dirnow == NULL)
9848 failed = TRUE;
9849 else
9850 {
9851 /*
9852 * Change to session file's dir.
9853 */
9854 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009856 *dirnow = NUL;
9857 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9858 {
9859 if (vim_chdirfile(fname) == OK)
9860 shorten_fnames(TRUE);
9861 }
9862 else if (*dirnow != NUL
9863 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9864 {
9865 if (mch_chdir((char *)globaldir) == 0)
9866 shorten_fnames(TRUE);
9867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868
Bram Moolenaard9462e32011-04-11 21:35:11 +02009869 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870
Bram Moolenaard9462e32011-04-11 21:35:11 +02009871 /* restore original dir */
9872 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009874 {
9875 if (mch_chdir((char *)dirnow) != 0)
9876 EMSG(_(e_prev_dir));
9877 shorten_fnames(TRUE);
9878 }
9879 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 }
9881 }
9882 else
9883 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009884 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9885 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 }
9887 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9888 == FAIL)
9889 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009890 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9891 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009892 if (eap->cmdidx == CMD_mksession)
9893 {
9894 if (put_line(fd, "unlet SessionLoad") == FAIL)
9895 failed = TRUE;
9896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 }
9898#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009899 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9900 failed = TRUE;
9901
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902 failed |= fclose(fd);
9903
9904 if (failed)
9905 EMSG(_(e_write));
9906#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9907 else if (eap->cmdidx == CMD_mksession)
9908 {
9909 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009910 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911
Bram Moolenaard9462e32011-04-11 21:35:11 +02009912 tbuf = alloc(MAXPATHL);
9913 if (tbuf != NULL)
9914 {
9915 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9916 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9917 vim_free(tbuf);
9918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 }
9920#endif
9921#ifdef MKSESSION_NL
9922 mksession_nl = FALSE;
9923#endif
9924 }
9925
9926#ifdef FEAT_BROWSE
9927theend:
9928 vim_free(browseFile);
9929#endif
9930#ifdef FEAT_SESSION
9931 vim_free(viewFile);
9932#endif
9933}
9934
Bram Moolenaardf177f62005-02-22 08:39:57 +00009935#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9936 || defined(PROTO)
9937 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009938vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009939{
9940 if (vim_mkdir(name, prot) != 0)
9941 {
9942 EMSG2(_("E739: Cannot create directory: %s"), name);
9943 return FAIL;
9944 }
9945 return OK;
9946}
9947#endif
9948
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949/*
9950 * Open a file for writing for an Ex command, with some checks.
9951 * Return file descriptor, or NULL on failure.
9952 */
9953 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009954open_exfile(
9955 char_u *fname,
9956 int forceit,
9957 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958{
9959 FILE *fd;
9960
9961#ifdef UNIX
9962 /* with Unix it is possible to open a directory */
9963 if (mch_isdir(fname))
9964 {
9965 EMSG2(_(e_isadir2), fname);
9966 return NULL;
9967 }
9968#endif
9969 if (!forceit && *mode != 'a' && vim_fexists(fname))
9970 {
9971 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9972 return NULL;
9973 }
9974
9975 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9976 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9977
9978 return fd;
9979}
9980
9981/*
9982 * ":mark" and ":k".
9983 */
9984 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009985ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986{
9987 pos_T pos;
9988
9989 if (*eap->arg == NUL) /* No argument? */
9990 EMSG(_(e_argreq));
9991 else if (eap->arg[1] != NUL) /* more than one character? */
9992 EMSG(_(e_trailing));
9993 else
9994 {
9995 pos = curwin->w_cursor; /* save curwin->w_cursor */
9996 curwin->w_cursor.lnum = eap->line2;
9997 beginline(BL_WHITE | BL_FIX);
9998 if (setmark(*eap->arg) == FAIL) /* set mark */
9999 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10000 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10001 }
10002}
10003
10004/*
10005 * Update w_topline, w_leftcol and the cursor position.
10006 */
10007 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010008update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009{
10010 check_cursor(); /* put cursor on valid line */
10011 update_topline();
10012 if (!curwin->w_p_wrap)
10013 validate_cursor();
10014 update_curswant();
10015}
10016
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017/*
10018 * ":normal[!] {commands}": Execute normal mode commands.
10019 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010020 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010021ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023 int save_msg_scroll = msg_scroll;
10024 int save_restart_edit = restart_edit;
10025 int save_msg_didout = msg_didout;
10026 int save_State = State;
10027 tasave_T tabuf;
10028 int save_insertmode = p_im;
10029 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010030 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031#ifdef FEAT_MBYTE
10032 char_u *arg = NULL;
10033 int l;
10034 char_u *p;
10035#endif
10036
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010037 if (ex_normal_lock > 0)
10038 {
10039 EMSG(_(e_secure));
10040 return;
10041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 if (ex_normal_busy >= p_mmd)
10043 {
10044 EMSG(_("E192: Recursive use of :normal too deep"));
10045 return;
10046 }
10047 ++ex_normal_busy;
10048
10049 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10050 restart_edit = 0; /* don't go to Insert mode */
10051 p_im = FALSE; /* don't use 'insertmode' */
10052
10053#ifdef FEAT_MBYTE
10054 /*
10055 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10056 * this for the K_SPECIAL leading byte, otherwise special keys will not
10057 * work.
10058 */
10059 if (has_mbyte)
10060 {
10061 int len = 0;
10062
10063 /* Count the number of characters to be escaped. */
10064 for (p = eap->arg; *p != NUL; ++p)
10065 {
10066# ifdef FEAT_GUI
10067 if (*p == CSI) /* leadbyte CSI */
10068 len += 2;
10069# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010070 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10072# ifdef FEAT_GUI
10073 || *p == CSI
10074# endif
10075 )
10076 len += 2;
10077 }
10078 if (len > 0)
10079 {
10080 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10081 if (arg != NULL)
10082 {
10083 len = 0;
10084 for (p = eap->arg; *p != NUL; ++p)
10085 {
10086 arg[len++] = *p;
10087# ifdef FEAT_GUI
10088 if (*p == CSI)
10089 {
10090 arg[len++] = KS_EXTRA;
10091 arg[len++] = (int)KE_CSI;
10092 }
10093# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010094 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 {
10096 arg[len++] = *++p;
10097 if (*p == K_SPECIAL)
10098 {
10099 arg[len++] = KS_SPECIAL;
10100 arg[len++] = KE_FILLER;
10101 }
10102# ifdef FEAT_GUI
10103 else if (*p == CSI)
10104 {
10105 arg[len++] = KS_EXTRA;
10106 arg[len++] = (int)KE_CSI;
10107 }
10108# endif
10109 }
10110 arg[len] = NUL;
10111 }
10112 }
10113 }
10114 }
10115#endif
10116
10117 /*
10118 * Save the current typeahead. This is required to allow using ":normal"
10119 * from an event handler and makes sure we don't hang when the argument
10120 * ends with half a command.
10121 */
10122 save_typeahead(&tabuf);
10123 if (tabuf.typebuf_valid)
10124 {
10125 /*
10126 * Repeat the :normal command for each line in the range. When no
10127 * range given, execute it just once, without positioning the cursor
10128 * first.
10129 */
10130 do
10131 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132 if (eap->addr_count != 0)
10133 {
10134 curwin->w_cursor.lnum = eap->line1++;
10135 curwin->w_cursor.col = 0;
10136 }
10137
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010138 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139#ifdef FEAT_MBYTE
10140 arg != NULL ? arg :
10141#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010142 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 }
10144 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10145 }
10146
10147 /* Might not return to the main loop when in an event handler. */
10148 update_topline_cursor();
10149
10150 /* Restore the previous typeahead. */
10151 restore_typeahead(&tabuf);
10152
10153 --ex_normal_busy;
10154 msg_scroll = save_msg_scroll;
10155 restart_edit = save_restart_edit;
10156 p_im = save_insertmode;
10157 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010158 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
10160
10161 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +010010162 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +010010164#ifdef FEAT_MOUSE
10165 setmouse();
10166#endif
10167#ifdef CURSOR_SHAPE
10168 ui_cursor_shape(); /* may show different cursor shape */
10169#endif
10170
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171#ifdef FEAT_MBYTE
10172 vim_free(arg);
10173#endif
10174}
10175
10176/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010177 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178 */
10179 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010180ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010182 if (eap->forceit)
10183 {
10184 coladvance((colnr_T)MAXCOL);
10185 curwin->w_curswant = MAXCOL;
10186 curwin->w_set_curswant = FALSE;
10187 }
10188
Bram Moolenaara40c5002005-01-09 21:16:21 +000010189 /* Ignore the command when already in Insert mode. Inserting an
10190 * expression register that invokes a function can do this. */
10191 if (State & INSERT)
10192 return;
10193
Bram Moolenaar64969662005-12-14 21:59:55 +000010194 if (eap->cmdidx == CMD_startinsert)
10195 restart_edit = 'a';
10196 else if (eap->cmdidx == CMD_startreplace)
10197 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010199 restart_edit = 'V';
10200
10201 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010203 if (eap->cmdidx == CMD_startinsert)
10204 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205 curwin->w_curswant = 0; /* avoid MAXCOL */
10206 }
10207}
10208
10209/*
10210 * ":stopinsert"
10211 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010213ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214{
10215 restart_edit = 0;
10216 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010217 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010220/*
10221 * Execute normal mode command "cmd".
10222 * "remap" can be REMAP_NONE or REMAP_YES.
10223 */
10224 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010225exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010226{
Bram Moolenaar25281632016-01-21 23:32:32 +010010227 /* Stuff the argument into the typeahead buffer. */
10228 ins_typebuf(cmd, remap, 0, TRUE, silent);
10229 exec_normal(FALSE);
10230}
Bram Moolenaar25281632016-01-21 23:32:32 +010010231
Bram Moolenaar25281632016-01-21 23:32:32 +010010232/*
10233 * Execute normal_cmd() until there is no typeahead left.
10234 */
10235 void
10236exec_normal(int was_typed)
10237{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010238 oparg_T oa;
10239
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010240 clear_oparg(&oa);
10241 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010242 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10243 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010244 {
10245 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010246 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010247 }
10248}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010249
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250#ifdef FEAT_FIND_ID
10251 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010252ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253{
10254 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10255 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10256 (linenr_T)1, (linenr_T)MAXLNUM);
10257}
10258
10259#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
10260/*
10261 * ":psearch"
10262 */
10263 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010264ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265{
10266 g_do_tagpreview = p_pvh;
10267 ex_findpat(eap);
10268 g_do_tagpreview = 0;
10269}
10270#endif
10271
10272 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010273ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274{
10275 int whole = TRUE;
10276 long n;
10277 char_u *p;
10278 int action;
10279
10280 switch (cmdnames[eap->cmdidx].cmd_name[2])
10281 {
10282 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10283 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10284 action = ACTION_GOTO;
10285 else
10286 action = ACTION_SHOW;
10287 break;
10288 case 'i': /* ":ilist" and ":dlist" */
10289 action = ACTION_SHOW_ALL;
10290 break;
10291 case 'u': /* ":ijump" and ":djump" */
10292 action = ACTION_GOTO;
10293 break;
10294 default: /* ":isplit" and ":dsplit" */
10295 action = ACTION_SPLIT;
10296 break;
10297 }
10298
10299 n = 1;
10300 if (vim_isdigit(*eap->arg)) /* get count */
10301 {
10302 n = getdigits(&eap->arg);
10303 eap->arg = skipwhite(eap->arg);
10304 }
10305 if (*eap->arg == '/') /* Match regexp, not just whole words */
10306 {
10307 whole = FALSE;
10308 ++eap->arg;
10309 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10310 if (*p)
10311 {
10312 *p++ = NUL;
10313 p = skipwhite(p);
10314
10315 /* Check for trailing illegal characters */
10316 if (!ends_excmd(*p))
10317 eap->errmsg = e_trailing;
10318 else
10319 eap->nextcmd = check_nextcmd(p);
10320 }
10321 }
10322 if (!eap->skip)
10323 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10324 whole, !eap->forceit,
10325 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10326 n, action, eap->line1, eap->line2);
10327}
10328#endif
10329
10330#ifdef FEAT_WINDOWS
10331
10332# ifdef FEAT_QUICKFIX
10333/*
10334 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10335 */
10336 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010337ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010339 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10341}
10342
10343/*
10344 * ":pedit"
10345 */
10346 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010347ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348{
10349 win_T *curwin_save = curwin;
10350
10351 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010352 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 keep_help_flag = curwin_save->w_buffer->b_help;
10354 do_exedit(eap, NULL);
10355 keep_help_flag = FALSE;
10356 if (curwin != curwin_save && win_valid(curwin_save))
10357 {
10358 /* Return cursor to where we were */
10359 validate_cursor();
10360 redraw_later(VALID);
10361 win_enter(curwin_save, TRUE);
10362 }
10363 g_do_tagpreview = 0;
10364}
10365# endif
10366
10367/*
10368 * ":stag", ":stselect" and ":stjump".
10369 */
10370 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010371ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372{
10373 postponed_split = -1;
10374 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010375 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10377 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010378 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379}
10380#endif
10381
10382/*
10383 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10384 */
10385 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010386ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387{
10388 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10389}
10390
10391 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010392ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393{
10394 int cmd;
10395
10396 switch (name[1])
10397 {
10398 case 'j': cmd = DT_JUMP; /* ":tjump" */
10399 break;
10400 case 's': cmd = DT_SELECT; /* ":tselect" */
10401 break;
10402 case 'p': cmd = DT_PREV; /* ":tprevious" */
10403 break;
10404 case 'N': cmd = DT_PREV; /* ":tNext" */
10405 break;
10406 case 'n': cmd = DT_NEXT; /* ":tnext" */
10407 break;
10408 case 'o': cmd = DT_POP; /* ":pop" */
10409 break;
10410 case 'f': /* ":tfirst" */
10411 case 'r': cmd = DT_FIRST; /* ":trewind" */
10412 break;
10413 case 'l': cmd = DT_LAST; /* ":tlast" */
10414 break;
10415 default: /* ":tag" */
10416#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010417 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418 {
10419 do_cstag(eap);
10420 return;
10421 }
10422#endif
10423 cmd = DT_TAG;
10424 break;
10425 }
10426
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010427 if (name[0] == 'l')
10428 {
10429#ifndef FEAT_QUICKFIX
10430 ex_ni(eap);
10431 return;
10432#else
10433 cmd = DT_LTAG;
10434#endif
10435 }
10436
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10438 eap->forceit, TRUE);
10439}
10440
10441/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010442 * Check "str" for starting with a special cmdline variable.
10443 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10444 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10445 */
10446 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010447find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010448{
10449 int len;
10450 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010451 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010452 "%",
10453#define SPEC_PERC 0
10454 "#",
10455#define SPEC_HASH 1
10456 "<cword>", /* cursor word */
10457#define SPEC_CWORD 2
10458 "<cWORD>", /* cursor WORD */
10459#define SPEC_CCWORD 3
10460 "<cfile>", /* cursor path name */
10461#define SPEC_CFILE 4
10462 "<sfile>", /* ":so" file name */
10463#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010464 "<slnum>", /* ":so" file line number */
10465#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010466#ifdef FEAT_AUTOCMD
10467 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010468# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010469 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010470# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010471 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010472# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010473#endif
10474#ifdef FEAT_CLIENTSERVER
10475 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010476# ifdef FEAT_AUTOCMD
10477# define SPEC_CLIENT 10
10478# else
10479# define SPEC_CLIENT 7
10480# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010481#endif
10482 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010483
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010484 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010485 {
10486 len = (int)STRLEN(spec_str[i]);
10487 if (STRNCMP(src, spec_str[i], len) == 0)
10488 {
10489 *usedlen = len;
10490 return i;
10491 }
10492 }
10493 return -1;
10494}
10495
10496/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497 * Evaluate cmdline variables.
10498 *
10499 * change '%' to curbuf->b_ffname
10500 * '#' to curwin->w_altfile
10501 * '<cword>' to word under the cursor
10502 * '<cWORD>' to WORD under the cursor
10503 * '<cfile>' to path name under the cursor
10504 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010505 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506 * '<afile>' to file name for autocommand
10507 * '<abuf>' to buffer number for autocommand
10508 * '<amatch>' to matching name for autocommand
10509 *
10510 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10511 * "" for error without a message) and NULL is returned.
10512 * Returns an allocated string if a valid match was found.
10513 * Returns NULL if no match was found. "usedlen" then still contains the
10514 * number of characters to skip.
10515 */
10516 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010517eval_vars(
10518 char_u *src, /* pointer into commandline */
10519 char_u *srcstart, /* beginning of valid memory for src */
10520 int *usedlen, /* characters after src that are used */
10521 linenr_T *lnump, /* line number for :e command, or NULL */
10522 char_u **errormsg, /* pointer to error message */
10523 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010524 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525{
10526 int i;
10527 char_u *s;
10528 char_u *result;
10529 char_u *resultbuf = NULL;
10530 int resultlen;
10531 buf_T *buf;
10532 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10533 int spec_idx;
10534#ifdef FEAT_MODIFY_FNAME
10535 int skip_mod = FALSE;
10536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538
10539 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010540 if (escaped != NULL)
10541 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542
10543 /*
10544 * Check if there is something to do.
10545 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010546 spec_idx = find_cmdline_var(src, usedlen);
10547 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548 {
10549 *usedlen = 1;
10550 return NULL;
10551 }
10552
10553 /*
10554 * Skip when preceded with a backslash "\%" and "\#".
10555 * Note: In "\\%" the % is also not recognized!
10556 */
10557 if (src > srcstart && src[-1] == '\\')
10558 {
10559 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010560 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561 return NULL;
10562 }
10563
10564 /*
10565 * word or WORD under cursor
10566 */
10567 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10568 {
10569 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10570 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10571 if (resultlen == 0)
10572 {
10573 *errormsg = (char_u *)"";
10574 return NULL;
10575 }
10576 }
10577
10578 /*
10579 * '#': Alternate file name
10580 * '%': Current file name
10581 * File name under the cursor
10582 * File name for autocommand
10583 * and following modifiers
10584 */
10585 else
10586 {
10587 switch (spec_idx)
10588 {
10589 case SPEC_PERC: /* '%': current file */
10590 if (curbuf->b_fname == NULL)
10591 {
10592 result = (char_u *)"";
10593 valid = 0; /* Must have ":p:h" to be valid */
10594 }
10595 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597 break;
10598
10599 case SPEC_HASH: /* '#' or "#99": alternate file */
10600 if (src[1] == '#') /* "##": the argument list */
10601 {
10602 result = arg_all();
10603 resultbuf = result;
10604 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010605 if (escaped != NULL)
10606 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607#ifdef FEAT_MODIFY_FNAME
10608 skip_mod = TRUE;
10609#endif
10610 break;
10611 }
10612 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010613 if (*s == '<') /* "#<99" uses v:oldfiles */
10614 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615 i = (int)getdigits(&s);
10616 *usedlen = (int)(s - src); /* length of what we expand */
10617
Bram Moolenaard812df62008-11-09 12:46:09 +000010618 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010620 if (*usedlen < 2)
10621 {
10622 /* Should we give an error message for #<text? */
10623 *usedlen = 1;
10624 return NULL;
10625 }
10626#ifdef FEAT_EVAL
10627 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10628 (long)i);
10629 if (result == NULL)
10630 {
10631 *errormsg = (char_u *)"";
10632 return NULL;
10633 }
10634#else
10635 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638 }
10639 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010640 {
10641 buf = buflist_findnr(i);
10642 if (buf == NULL)
10643 {
10644 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10645 return NULL;
10646 }
10647 if (lnump != NULL)
10648 *lnump = ECMD_LAST;
10649 if (buf->b_fname == NULL)
10650 {
10651 result = (char_u *)"";
10652 valid = 0; /* Must have ":p:h" to be valid */
10653 }
10654 else
10655 result = buf->b_fname;
10656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657 break;
10658
10659#ifdef FEAT_SEARCHPATH
10660 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010661 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 if (result == NULL)
10663 {
10664 *errormsg = (char_u *)"";
10665 return NULL;
10666 }
10667 resultbuf = result; /* remember allocated string */
10668 break;
10669#endif
10670
10671#ifdef FEAT_AUTOCMD
10672 case SPEC_AFILE: /* file name for autocommand */
10673 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010674 if (result != NULL && !autocmd_fname_full)
10675 {
10676 /* Still need to turn the fname into a full path. It is
10677 * postponed to avoid a delay when <afile> is not used. */
10678 autocmd_fname_full = TRUE;
10679 result = FullName_save(autocmd_fname, FALSE);
10680 vim_free(autocmd_fname);
10681 autocmd_fname = result;
10682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683 if (result == NULL)
10684 {
10685 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10686 return NULL;
10687 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010688 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689 break;
10690
10691 case SPEC_ABUF: /* buffer number for autocommand */
10692 if (autocmd_bufnr <= 0)
10693 {
10694 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10695 return NULL;
10696 }
10697 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10698 result = strbuf;
10699 break;
10700
10701 case SPEC_AMATCH: /* match name for autocommand */
10702 result = autocmd_match;
10703 if (result == NULL)
10704 {
10705 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10706 return NULL;
10707 }
10708 break;
10709
10710#endif
10711 case SPEC_SFILE: /* file name for ":so" command */
10712 result = sourcing_name;
10713 if (result == NULL)
10714 {
10715 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10716 return NULL;
10717 }
10718 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010719 case SPEC_SLNUM: /* line in file for ":so" command */
10720 if (sourcing_name == NULL || sourcing_lnum == 0)
10721 {
10722 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10723 return NULL;
10724 }
10725 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10726 result = strbuf;
10727 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728#if defined(FEAT_CLIENTSERVER)
10729 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010730 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10731 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732 result = strbuf;
10733 break;
10734#endif
10735 }
10736
10737 resultlen = (int)STRLEN(result); /* length of new string */
10738 if (src[*usedlen] == '<') /* remove the file name extension */
10739 {
10740 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742 resultlen = (int)(s - result);
10743 }
10744#ifdef FEAT_MODIFY_FNAME
10745 else if (!skip_mod)
10746 {
10747 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10748 &resultlen);
10749 if (result == NULL)
10750 {
10751 *errormsg = (char_u *)"";
10752 return NULL;
10753 }
10754 }
10755#endif
10756 }
10757
10758 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10759 {
10760 if (valid != VALID_HEAD + VALID_PATH)
10761 /* xgettext:no-c-format */
10762 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10763 else
10764 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10765 result = NULL;
10766 }
10767 else
10768 result = vim_strnsave(result, resultlen);
10769 vim_free(resultbuf);
10770 return result;
10771}
10772
10773/*
10774 * Concatenate all files in the argument list, separated by spaces, and return
10775 * it in one allocated string.
10776 * Spaces and backslashes in the file names are escaped with a backslash.
10777 * Returns NULL when out of memory.
10778 */
10779 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010780arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781{
10782 int len;
10783 int idx;
10784 char_u *retval = NULL;
10785 char_u *p;
10786
10787 /*
10788 * Do this loop two times:
10789 * first time: compute the total length
10790 * second time: concatenate the names
10791 */
10792 for (;;)
10793 {
10794 len = 0;
10795 for (idx = 0; idx < ARGCOUNT; ++idx)
10796 {
10797 p = alist_name(&ARGLIST[idx]);
10798 if (p != NULL)
10799 {
10800 if (len > 0)
10801 {
10802 /* insert a space in between names */
10803 if (retval != NULL)
10804 retval[len] = ' ';
10805 ++len;
10806 }
10807 for ( ; *p != NUL; ++p)
10808 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010809 if (*p == ' '
10810#ifndef BACKSLASH_IN_FILENAME
10811 || *p == '\\'
10812#endif
10813 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814 {
10815 /* insert a backslash */
10816 if (retval != NULL)
10817 retval[len] = '\\';
10818 ++len;
10819 }
10820 if (retval != NULL)
10821 retval[len] = *p;
10822 ++len;
10823 }
10824 }
10825 }
10826
10827 /* second time: break here */
10828 if (retval != NULL)
10829 {
10830 retval[len] = NUL;
10831 break;
10832 }
10833
10834 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010835 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836 if (retval == NULL)
10837 break;
10838 }
10839
10840 return retval;
10841}
10842
10843#if defined(FEAT_AUTOCMD) || defined(PROTO)
10844/*
10845 * Expand the <sfile> string in "arg".
10846 *
10847 * Returns an allocated string, or NULL for any error.
10848 */
10849 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010850expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851{
10852 char_u *errormsg;
10853 int len;
10854 char_u *result;
10855 char_u *newres;
10856 char_u *repl;
10857 int srclen;
10858 char_u *p;
10859
10860 result = vim_strsave(arg);
10861 if (result == NULL)
10862 return NULL;
10863
10864 for (p = result; *p; )
10865 {
10866 if (STRNCMP(p, "<sfile>", 7) != 0)
10867 ++p;
10868 else
10869 {
10870 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010871 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872 if (errormsg != NULL)
10873 {
10874 if (*errormsg)
10875 emsg(errormsg);
10876 vim_free(result);
10877 return NULL;
10878 }
10879 if (repl == NULL) /* no match (cannot happen) */
10880 {
10881 p += srclen;
10882 continue;
10883 }
10884 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10885 newres = alloc(len);
10886 if (newres == NULL)
10887 {
10888 vim_free(repl);
10889 vim_free(result);
10890 return NULL;
10891 }
10892 mch_memmove(newres, result, (size_t)(p - result));
10893 STRCPY(newres + (p - result), repl);
10894 len = (int)STRLEN(newres);
10895 STRCAT(newres, p + srclen);
10896 vim_free(repl);
10897 vim_free(result);
10898 result = newres;
10899 p = newres + len; /* continue after the match */
10900 }
10901 }
10902
10903 return result;
10904}
10905#endif
10906
10907#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010010908static int ses_winsizes(FILE *fd, int restore_size,
10909 win_T *tab_firstwin);
10910static int ses_win_rec(FILE *fd, frame_T *fr);
10911static frame_T *ses_skipframe(frame_T *fr);
10912static int ses_do_frame(frame_T *fr);
10913static int ses_do_win(win_T *wp);
10914static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
10915static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
10916static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917
10918/*
10919 * Write openfile commands for the current buffers to an .exrc file.
10920 * Return FAIL on error, OK otherwise.
10921 */
10922 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010923makeopens(
10924 FILE *fd,
10925 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926{
10927 buf_T *buf;
10928 int only_save_windows = TRUE;
10929 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930 int restore_size = TRUE;
10931 win_T *wp;
10932 char_u *sname;
10933 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010934 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010935 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010936 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010937 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010938 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010939 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940
10941 if (ssop_flags & SSOP_BUFFERS)
10942 only_save_windows = FALSE; /* Save ALL buffers */
10943
10944 /*
10945 * Begin by setting the this_session variable, and then other
10946 * sessionable variables.
10947 */
10948#ifdef FEAT_EVAL
10949 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10950 return FAIL;
10951 if (ssop_flags & SSOP_GLOBALS)
10952 if (store_session_globals(fd) == FAIL)
10953 return FAIL;
10954#endif
10955
10956 /*
10957 * Close all windows but one.
10958 */
10959 if (put_line(fd, "silent only") == FAIL)
10960 return FAIL;
10961
10962 /*
10963 * Now a :cd command to the session directory or the current directory
10964 */
10965 if (ssop_flags & SSOP_SESDIR)
10966 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010967 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10968 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969 return FAIL;
10970 }
10971 else if (ssop_flags & SSOP_CURDIR)
10972 {
10973 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10974 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010975 || fputs("cd ", fd) < 0
10976 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10977 || put_eol(fd) == FAIL)
10978 {
10979 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 vim_free(sname);
10983 }
10984
10985 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010986 * If there is an empty, unnamed buffer we will wipe it out later.
10987 * Remember the buffer number.
10988 */
10989 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10990 return FAIL;
10991 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10992 return FAIL;
10993 if (put_line(fd, "endif") == FAIL)
10994 return FAIL;
10995
10996 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997 * Now save the current files, current buffer first.
10998 */
10999 if (put_line(fd, "set shortmess=aoO") == FAIL)
11000 return FAIL;
11001
11002 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011003 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011004 {
11005 if (!(only_save_windows && buf->b_nwindows == 0)
11006 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
11007 && buf->b_fname != NULL
11008 && buf->b_p_bl)
11009 {
11010 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11011 : buf->b_wininfo->wi_fpos.lnum) < 0
11012 || ses_fname(fd, buf, &ssop_flags) == FAIL)
11013 return FAIL;
11014 }
11015 }
11016
11017 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011018 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11020 return FAIL;
11021
11022 if (ssop_flags & SSOP_RESIZE)
11023 {
11024 /* Note: after the restore we still check it worked!*/
11025 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11026 || put_eol(fd) == FAIL)
11027 return FAIL;
11028 }
11029
11030#ifdef FEAT_GUI
11031 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11032 {
11033 int x, y;
11034
11035 if (gui_mch_get_winpos(&x, &y) == OK)
11036 {
11037 /* Note: after the restore we still check it worked!*/
11038 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11039 return FAIL;
11040 }
11041 }
11042#endif
11043
11044 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011045 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11046 * will be displayed when creating the next tab. That resizes the windows
11047 * in the first tab, which may cause problems. Set 'showtabline' to 2
11048 * temporarily to avoid that.
11049 */
11050 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11051 {
11052 if (put_line(fd, "set stal=2") == FAIL)
11053 return FAIL;
11054 restore_stal = TRUE;
11055 }
11056
11057 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011058 * May repeat putting Windows for each tab, when "tabpages" is in
11059 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011060 * Don't use goto_tabpage(), it may change directory and trigger
11061 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011063 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011064 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011065 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011067 int need_tabnew = FALSE;
11068 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011069
Bram Moolenaar18144c82006-04-12 21:52:12 +000011070 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011072 tabpage_T *tp = find_tabpage(tabnr);
11073
11074 if (tp == NULL)
11075 break; /* done all tab pages */
11076 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011077 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011078 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011079 tab_topframe = topframe;
11080 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011081 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011082 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011083 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011084 tab_topframe = tp->tp_topframe;
11085 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011086 if (tabnr > 1)
11087 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011089
Bram Moolenaar18144c82006-04-12 21:52:12 +000011090 /*
11091 * Before creating the window layout, try loading one file. If this
11092 * is aborted we don't end up with a number of useless windows.
11093 * This may have side effects! (e.g., compressed or network file).
11094 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011095 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011096 {
11097 if (ses_do_win(wp)
11098 && wp->w_buffer->b_ffname != NULL
11099 && !wp->w_buffer->b_help
11100#ifdef FEAT_QUICKFIX
11101 && !bt_nofile(wp->w_buffer)
11102#endif
11103 )
11104 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011105 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000011106 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
11107 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011108 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011109 if (!wp->w_arg_idx_invalid)
11110 edited_win = wp;
11111 break;
11112 }
11113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011115 /* If no file got edited create an empty tab page. */
11116 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11117 return FAIL;
11118
Bram Moolenaar18144c82006-04-12 21:52:12 +000011119 /*
11120 * Save current window layout.
11121 */
11122 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011124 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011126 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11127 return FAIL;
11128 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11129 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130
Bram Moolenaar18144c82006-04-12 21:52:12 +000011131 /*
11132 * Check if window sizes can be restored (no windows omitted).
11133 * Remember the window number of the current window after restoring.
11134 */
11135 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011136 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011137 {
11138 if (ses_do_win(wp))
11139 ++nr;
11140 else
11141 restore_size = FALSE;
11142 if (curwin == wp)
11143 cnr = nr;
11144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145
Bram Moolenaar18144c82006-04-12 21:52:12 +000011146 /* Go to the first window. */
11147 if (put_line(fd, "wincmd t") == FAIL)
11148 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011149
Bram Moolenaar18144c82006-04-12 21:52:12 +000011150 /*
11151 * If more than one window, see if sizes can be restored.
11152 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11153 * resized when moving between windows.
11154 * Do this before restoring the view, so that the topline and the
11155 * cursor can be set. This is done again below.
11156 */
11157 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
11158 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011159 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011160 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161
Bram Moolenaar18144c82006-04-12 21:52:12 +000011162 /*
11163 * Restore the view of the window (options, file, cursor, etc.).
11164 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011165 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011166 {
11167 if (!ses_do_win(wp))
11168 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011169 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11170 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011171 return FAIL;
11172 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11173 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011174 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011175 }
11176
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011177 /* The argument index in the first tab page is zero, need to set it in
11178 * each window. For further tab pages it's the window where we do
11179 * "tabedit". */
11180 cur_arg_idx = next_arg_idx;
11181
Bram Moolenaar18144c82006-04-12 21:52:12 +000011182 /*
11183 * Restore cursor to the current window if it's not the first one.
11184 */
11185 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11186 || put_eol(fd) == FAIL))
11187 return FAIL;
11188
11189 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011190 * Restore window sizes again after jumping around in windows, because
11191 * the current window has a minimum size while others may not.
11192 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011193 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011194 return FAIL;
11195
Bram Moolenaar18144c82006-04-12 21:52:12 +000011196 /* Don't continue in another tab page when doing only the current one
11197 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011198 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011199 break;
11200 }
11201
11202 if (ssop_flags & SSOP_TABPAGES)
11203 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011204 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11205 || put_eol(fd) == FAIL)
11206 return FAIL;
11207 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011208 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11209 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011210
Bram Moolenaar9c102382006-05-03 21:26:49 +000011211 /*
11212 * Wipe out an empty unnamed buffer we started in.
11213 */
11214 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11215 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011216 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011217 return FAIL;
11218 if (put_line(fd, "endif") == FAIL)
11219 return FAIL;
11220 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11221 return FAIL;
11222
11223 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11224 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11225 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11226 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011227
11228 /*
11229 * Lastly, execute the x.vim file if it exists.
11230 */
11231 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11232 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011233 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234 || put_line(fd, "endif") == FAIL)
11235 return FAIL;
11236
11237 return OK;
11238}
11239
11240 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011241ses_winsizes(
11242 FILE *fd,
11243 int restore_size,
11244 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011245{
11246 int n = 0;
11247 win_T *wp;
11248
11249 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11250 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011251 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252 {
11253 if (!ses_do_win(wp))
11254 continue;
11255 ++n;
11256
11257 /* restore height when not full height */
11258 if (wp->w_height + wp->w_status_height < topframe->fr_height
11259 && (fprintf(fd,
11260 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11261 n, (long)wp->w_height, Rows / 2, Rows) < 0
11262 || put_eol(fd) == FAIL))
11263 return FAIL;
11264
11265 /* restore width when not full width */
11266 if (wp->w_width < Columns && (fprintf(fd,
11267 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11268 n, (long)wp->w_width, Columns / 2, Columns) < 0
11269 || put_eol(fd) == FAIL))
11270 return FAIL;
11271 }
11272 }
11273 else
11274 {
11275 /* Just equalise window sizes */
11276 if (put_line(fd, "wincmd =") == FAIL)
11277 return FAIL;
11278 }
11279 return OK;
11280}
11281
11282/*
11283 * Write commands to "fd" to recursively create windows for frame "fr",
11284 * horizontally and vertically split.
11285 * After the commands the last window in the frame is the current window.
11286 * Returns FAIL when writing the commands to "fd" fails.
11287 */
11288 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011289ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290{
11291 frame_T *frc;
11292 int count = 0;
11293
11294 if (fr->fr_layout != FR_LEAF)
11295 {
11296 /* Find first frame that's not skipped and then create a window for
11297 * each following one (first frame is already there). */
11298 frc = ses_skipframe(fr->fr_child);
11299 if (frc != NULL)
11300 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11301 {
11302 /* Make window as big as possible so that we have lots of room
11303 * to split. */
11304 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11305 || put_line(fd, fr->fr_layout == FR_COL
11306 ? "split" : "vsplit") == FAIL)
11307 return FAIL;
11308 ++count;
11309 }
11310
11311 /* Go back to the first window. */
11312 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11313 ? "%dwincmd k" : "%dwincmd h", count) < 0
11314 || put_eol(fd) == FAIL))
11315 return FAIL;
11316
11317 /* Recursively create frames/windows in each window of this column or
11318 * row. */
11319 frc = ses_skipframe(fr->fr_child);
11320 while (frc != NULL)
11321 {
11322 ses_win_rec(fd, frc);
11323 frc = ses_skipframe(frc->fr_next);
11324 /* Go to next window. */
11325 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11326 return FAIL;
11327 }
11328 }
11329 return OK;
11330}
11331
11332/*
11333 * Skip frames that don't contain windows we want to save in the Session.
11334 * Returns NULL when there none.
11335 */
11336 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011337ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338{
11339 frame_T *frc;
11340
11341 for (frc = fr; frc != NULL; frc = frc->fr_next)
11342 if (ses_do_frame(frc))
11343 break;
11344 return frc;
11345}
11346
11347/*
11348 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11349 * the Session.
11350 */
11351 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011352ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353{
11354 frame_T *frc;
11355
11356 if (fr->fr_layout == FR_LEAF)
11357 return ses_do_win(fr->fr_win);
11358 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11359 if (ses_do_frame(frc))
11360 return TRUE;
11361 return FALSE;
11362}
11363
11364/*
11365 * Return non-zero if window "wp" is to be stored in the Session.
11366 */
11367 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011368ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369{
11370 if (wp->w_buffer->b_fname == NULL
11371#ifdef FEAT_QUICKFIX
11372 /* When 'buftype' is "nofile" can't restore the window contents. */
11373 || bt_nofile(wp->w_buffer)
11374#endif
11375 )
11376 return (ssop_flags & SSOP_BLANK);
11377 if (wp->w_buffer->b_help)
11378 return (ssop_flags & SSOP_HELP);
11379 return TRUE;
11380}
11381
11382/*
11383 * Write commands to "fd" to restore the view of a window.
11384 * Caller must make sure 'scrolloff' is zero.
11385 */
11386 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011387put_view(
11388 FILE *fd,
11389 win_T *wp,
11390 int add_edit, /* add ":edit" command to view */
11391 unsigned *flagp, /* vop_flags or ssop_flags */
11392 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011393 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394{
11395 win_T *save_curwin;
11396 int f;
11397 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011398 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399
11400 /* Always restore cursor position for ":mksession". For ":mkview" only
11401 * when 'viewoptions' contains "cursor". */
11402 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11403
11404 /*
11405 * Local argument list.
11406 */
11407 if (wp->w_alist == &global_alist)
11408 {
11409 if (put_line(fd, "argglobal") == FAIL)
11410 return FAIL;
11411 }
11412 else
11413 {
11414 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11415 flagp == &vop_flags
11416 || !(*flagp & SSOP_CURDIR)
11417 || wp->w_localdir != NULL, flagp) == FAIL)
11418 return FAIL;
11419 }
11420
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011421 /* Only when part of a session: restore the argument index. Some
11422 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011423 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011424 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011425 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011426 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011427 || put_eol(fd) == FAIL)
11428 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011429 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430 }
11431
11432 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011433 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434 {
11435 /*
11436 * Load the file.
11437 */
11438 if (wp->w_buffer->b_ffname != NULL
11439#ifdef FEAT_QUICKFIX
11440 && !bt_nofile(wp->w_buffer)
11441#endif
11442 )
11443 {
11444 /*
11445 * Editing a file in this buffer: use ":edit file".
11446 * This may have side effects! (e.g., compressed or network file).
11447 */
11448 if (fputs("edit ", fd) < 0
11449 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11450 return FAIL;
11451 }
11452 else
11453 {
11454 /* No file in this buffer, just make it empty. */
11455 if (put_line(fd, "enew") == FAIL)
11456 return FAIL;
11457#ifdef FEAT_QUICKFIX
11458 if (wp->w_buffer->b_ffname != NULL)
11459 {
11460 /* The buffer does have a name, but it's not a file name. */
11461 if (fputs("file ", fd) < 0
11462 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11463 return FAIL;
11464 }
11465#endif
11466 do_cursor = FALSE;
11467 }
11468 }
11469
11470 /*
11471 * Local mappings and abbreviations.
11472 */
11473 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11474 && makemap(fd, wp->w_buffer) == FAIL)
11475 return FAIL;
11476
11477 /*
11478 * Local options. Need to go to the window temporarily.
11479 * Store only local values when using ":mkview" and when ":mksession" is
11480 * used and 'sessionoptions' doesn't include "options".
11481 * Some folding options are always stored when "folds" is included,
11482 * otherwise the folds would not be restored correctly.
11483 */
11484 save_curwin = curwin;
11485 curwin = wp;
11486 curbuf = curwin->w_buffer;
11487 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11488 f = makeset(fd, OPT_LOCAL,
11489 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11490#ifdef FEAT_FOLDING
11491 else if (*flagp & SSOP_FOLDS)
11492 f = makefoldset(fd);
11493#endif
11494 else
11495 f = OK;
11496 curwin = save_curwin;
11497 curbuf = curwin->w_buffer;
11498 if (f == FAIL)
11499 return FAIL;
11500
11501#ifdef FEAT_FOLDING
11502 /*
11503 * Save Folds when 'buftype' is empty and for help files.
11504 */
11505 if ((*flagp & SSOP_FOLDS)
11506 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011507# ifdef FEAT_QUICKFIX
11508 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11509# endif
11510 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011511 {
11512 if (put_folds(fd, wp) == FAIL)
11513 return FAIL;
11514 }
11515#endif
11516
11517 /*
11518 * Set the cursor after creating folds, since that moves the cursor.
11519 */
11520 if (do_cursor)
11521 {
11522
11523 /* Restore the cursor line in the file and relatively in the
11524 * window. Don't use "G", it changes the jumplist. */
11525 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11526 (long)wp->w_cursor.lnum,
11527 (long)(wp->w_cursor.lnum - wp->w_topline),
11528 (long)wp->w_height / 2, (long)wp->w_height) < 0
11529 || put_eol(fd) == FAIL
11530 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11531 || put_line(fd, "exe s:l") == FAIL
11532 || put_line(fd, "normal! zt") == FAIL
11533 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11534 || put_eol(fd) == FAIL)
11535 return FAIL;
11536 /* Restore the cursor column and left offset when not wrapping. */
11537 if (wp->w_cursor.col == 0)
11538 {
11539 if (put_line(fd, "normal! 0") == FAIL)
11540 return FAIL;
11541 }
11542 else
11543 {
11544 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11545 {
11546 if (fprintf(fd,
11547 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011548 (long)wp->w_virtcol + 1,
11549 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550 (long)wp->w_width / 2, (long)wp->w_width) < 0
11551 || put_eol(fd) == FAIL
11552 || put_line(fd, "if s:c > 0") == FAIL
11553 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011554 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11555 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011556 || put_eol(fd) == FAIL
11557 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011558 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011559 || put_eol(fd) == FAIL
11560 || put_line(fd, "endif") == FAIL)
11561 return FAIL;
11562 }
11563 else
11564 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011565 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566 || put_eol(fd) == FAIL)
11567 return FAIL;
11568 }
11569 }
11570 }
11571
11572 /*
11573 * Local directory.
11574 */
11575 if (wp->w_localdir != NULL)
11576 {
11577 if (fputs("lcd ", fd) < 0
11578 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11579 || put_eol(fd) == FAIL)
11580 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011581 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011582 }
11583
11584 return OK;
11585}
11586
11587/*
11588 * Write an argument list to the session file.
11589 * Returns FAIL if writing fails.
11590 */
11591 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011592ses_arglist(
11593 FILE *fd,
11594 char *cmd,
11595 garray_T *gap,
11596 int fullname, /* TRUE: use full path name */
11597 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011598{
11599 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011600 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011601 char_u *s;
11602
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011603 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11604 return FAIL;
11605 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011606 return FAIL;
11607 for (i = 0; i < gap->ga_len; ++i)
11608 {
11609 /* NULL file names are skipped (only happens when out of memory). */
11610 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11611 if (s != NULL)
11612 {
11613 if (fullname)
11614 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011615 buf = alloc(MAXPATHL);
11616 if (buf != NULL)
11617 {
11618 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11619 s = buf;
11620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011621 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011622 if (fputs("argadd ", fd) < 0
11623 || ses_put_fname(fd, s, flagp) == FAIL
11624 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011625 {
11626 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011627 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011628 }
11629 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011630 }
11631 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011632 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011633}
11634
11635/*
11636 * Write a buffer name to the session file.
11637 * Also ends the line.
11638 * Returns FAIL if writing fails.
11639 */
11640 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011641ses_fname(FILE *fd, buf_T *buf, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011642{
11643 char_u *name;
11644
11645 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011646 * the session file will be sourced.
11647 * Don't do this for ":mkview", we don't know the current directory.
11648 * Don't do this after ":lcd", we don't keep track of what the current
11649 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011650 if (buf->b_sfname != NULL
11651 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011652 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011653#ifdef FEAT_AUTOCHDIR
11654 && !p_acd
11655#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011656 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011657 name = buf->b_sfname;
11658 else
11659 name = buf->b_ffname;
11660 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11661 return FAIL;
11662 return OK;
11663}
11664
11665/*
11666 * Write a file name to the session file.
11667 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11668 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011669 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011670 */
11671 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011672ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011673{
11674 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011675 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011676 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677
11678 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011679 if (sname == NULL)
11680 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011681
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011682 if (*flagp & SSOP_SLASH)
11683 {
11684 /* change all backslashes to forward slashes */
11685 for (p = sname; *p != NUL; mb_ptr_adv(p))
11686 if (*p == '\\')
11687 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011689
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011690 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011691 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011692 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011693 if (p == NULL)
11694 return FAIL;
11695
11696 /* write the result */
11697 if (fputs((char *)p, fd) < 0)
11698 retval = FAIL;
11699
11700 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011701 return retval;
11702}
11703
11704/*
11705 * ":loadview [nr]"
11706 */
11707 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011708ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709{
11710 char_u *fname;
11711
11712 fname = get_view_file(*eap->arg);
11713 if (fname != NULL)
11714 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011715 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011716 vim_free(fname);
11717 }
11718}
11719
11720/*
11721 * Get the name of the view file for the current buffer.
11722 */
11723 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011724get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011725{
11726 int len = 0;
11727 char_u *p, *s;
11728 char_u *retval;
11729 char_u *sname;
11730
11731 if (curbuf->b_ffname == NULL)
11732 {
11733 EMSG(_(e_noname));
11734 return NULL;
11735 }
11736 sname = home_replace_save(NULL, curbuf->b_ffname);
11737 if (sname == NULL)
11738 return NULL;
11739
11740 /*
11741 * We want a file name without separators, because we're not going to make
11742 * a directory.
11743 * "normal" path separator -> "=+"
11744 * "=" -> "=="
11745 * ":" path separator -> "=-"
11746 */
11747 for (p = sname; *p; ++p)
11748 if (*p == '=' || vim_ispathsep(*p))
11749 ++len;
11750 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11751 if (retval != NULL)
11752 {
11753 STRCPY(retval, p_vdir);
11754 add_pathsep(retval);
11755 s = retval + STRLEN(retval);
11756 for (p = sname; *p; ++p)
11757 {
11758 if (*p == '=')
11759 {
11760 *s++ = '=';
11761 *s++ = '=';
11762 }
11763 else if (vim_ispathsep(*p))
11764 {
11765 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011766#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011767 if (*p == ':')
11768 *s++ = '-';
11769 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011770#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011771 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772 }
11773 else
11774 *s++ = *p;
11775 }
11776 *s++ = '=';
11777 *s++ = c;
11778 STRCPY(s, ".vim");
11779 }
11780
11781 vim_free(sname);
11782 return retval;
11783}
11784
11785#endif /* FEAT_SESSION */
11786
11787/*
11788 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11789 * Return FAIL for a write error.
11790 */
11791 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011792put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011793{
11794 if (
11795#ifdef USE_CRNL
11796 (
11797# ifdef MKSESSION_NL
11798 !mksession_nl &&
11799# endif
11800 (putc('\r', fd) < 0)) ||
11801#endif
11802 (putc('\n', fd) < 0))
11803 return FAIL;
11804 return OK;
11805}
11806
11807/*
11808 * Write a line to "fd".
11809 * Return FAIL for a write error.
11810 */
11811 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011812put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011813{
11814 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11815 return FAIL;
11816 return OK;
11817}
11818
11819#ifdef FEAT_VIMINFO
11820/*
11821 * ":rviminfo" and ":wviminfo".
11822 */
11823 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011824ex_viminfo(
11825 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011826{
11827 char_u *save_viminfo;
11828
11829 save_viminfo = p_viminfo;
11830 if (*p_viminfo == NUL)
11831 p_viminfo = (char_u *)"'100";
11832 if (eap->cmdidx == CMD_rviminfo)
11833 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011834 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11835 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011836 EMSG(_("E195: Cannot open viminfo file for reading"));
11837 }
11838 else
11839 write_viminfo(eap->arg, eap->forceit);
11840 p_viminfo = save_viminfo;
11841}
11842#endif
11843
11844#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011845/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011846 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011847 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011848 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011850dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011851{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852 if (fname == NULL)
11853 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011854 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855}
11856#endif
11857
11858/*
11859 * ":behave {mswin,xterm}"
11860 */
11861 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011862ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863{
11864 if (STRCMP(eap->arg, "mswin") == 0)
11865 {
11866 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11867 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11868 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11869 set_option_value((char_u *)"keymodel", 0L,
11870 (char_u *)"startsel,stopsel", 0);
11871 }
11872 else if (STRCMP(eap->arg, "xterm") == 0)
11873 {
11874 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11875 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11876 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11877 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11878 }
11879 else
11880 EMSG2(_(e_invarg2), eap->arg);
11881}
11882
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011883#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11884/*
11885 * Function given to ExpandGeneric() to obtain the possible arguments of the
11886 * ":behave {mswin,xterm}" command.
11887 */
11888 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011889get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011890{
11891 if (idx == 0)
11892 return (char_u *)"mswin";
11893 if (idx == 1)
11894 return (char_u *)"xterm";
11895 return NULL;
11896}
11897#endif
11898
Bram Moolenaar071d4272004-06-13 20:20:40 +000011899#ifdef FEAT_AUTOCMD
11900static int filetype_detect = FALSE;
11901static int filetype_plugin = FALSE;
11902static int filetype_indent = FALSE;
11903
11904/*
11905 * ":filetype [plugin] [indent] {on,off,detect}"
11906 * on: Load the filetype.vim file to install autocommands for file types.
11907 * off: Load the ftoff.vim file to remove all autocommands for file types.
11908 * plugin on: load filetype.vim and ftplugin.vim
11909 * plugin off: load ftplugof.vim
11910 * indent on: load filetype.vim and indent.vim
11911 * indent off: load indoff.vim
11912 */
11913 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011914ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011915{
11916 char_u *arg = eap->arg;
11917 int plugin = FALSE;
11918 int indent = FALSE;
11919
11920 if (*eap->arg == NUL)
11921 {
11922 /* Print current status. */
11923 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11924 filetype_detect ? "ON" : "OFF",
11925 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11926 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11927 return;
11928 }
11929
11930 /* Accept "plugin" and "indent" in any order. */
11931 for (;;)
11932 {
11933 if (STRNCMP(arg, "plugin", 6) == 0)
11934 {
11935 plugin = TRUE;
11936 arg = skipwhite(arg + 6);
11937 continue;
11938 }
11939 if (STRNCMP(arg, "indent", 6) == 0)
11940 {
11941 indent = TRUE;
11942 arg = skipwhite(arg + 6);
11943 continue;
11944 }
11945 break;
11946 }
11947 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11948 {
11949 if (*arg == 'o' || !filetype_detect)
11950 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011951 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011952 filetype_detect = TRUE;
11953 if (plugin)
11954 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011955 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011956 filetype_plugin = TRUE;
11957 }
11958 if (indent)
11959 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011960 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961 filetype_indent = TRUE;
11962 }
11963 }
11964 if (*arg == 'd')
11965 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020011966 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011967 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011968 }
11969 }
11970 else if (STRCMP(arg, "off") == 0)
11971 {
11972 if (plugin || indent)
11973 {
11974 if (plugin)
11975 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011976 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977 filetype_plugin = FALSE;
11978 }
11979 if (indent)
11980 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011981 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011982 filetype_indent = FALSE;
11983 }
11984 }
11985 else
11986 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011987 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988 filetype_detect = FALSE;
11989 }
11990 }
11991 else
11992 EMSG2(_(e_invarg2), arg);
11993}
11994
11995/*
11996 * ":setfiletype {name}"
11997 */
11998 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011999ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000{
12001 if (!did_filetype)
12002 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
12003}
12004#endif
12005
Bram Moolenaar071d4272004-06-13 20:20:40 +000012006 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012007ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008{
12009#ifdef FEAT_DIGRAPHS
12010 if (*eap->arg != NUL)
12011 putdigraph(eap->arg);
12012 else
12013 listdigraphs();
12014#else
12015 EMSG(_("E196: No digraphs in this version"));
12016#endif
12017}
12018
12019 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012020ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012021{
12022 int flags = 0;
12023
12024 if (eap->cmdidx == CMD_setlocal)
12025 flags = OPT_LOCAL;
12026 else if (eap->cmdidx == CMD_setglobal)
12027 flags = OPT_GLOBAL;
12028#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
12029 if (cmdmod.browse && flags == 0)
12030 ex_options(eap);
12031 else
12032#endif
12033 (void)do_set(eap->arg, flags);
12034}
12035
12036#ifdef FEAT_SEARCH_EXTRA
12037/*
12038 * ":nohlsearch"
12039 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012040 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012041ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012042{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010012043 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012044 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012045}
12046
12047/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012048 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012049 * Sets nextcmd to the start of the next command, if any. Also called when
12050 * skipping commands to find the next command.
12051 */
12052 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012053ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054{
12055 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012056 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057 char_u *end;
12058 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012059 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012060
12061 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012062 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012063 else
12064 {
12065 EMSG(e_invcmd);
12066 return;
12067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012068
12069 /* First clear any old pattern. */
12070 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012071 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012072
12073 if (ends_excmd(*eap->arg))
12074 end = eap->arg;
12075 else if ((STRNICMP(eap->arg, "none", 4) == 0
12076 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
12077 end = eap->arg + 4;
12078 else
12079 {
12080 p = skiptowhite(eap->arg);
12081 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012082 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083 p = skipwhite(p);
12084 if (*p == NUL)
12085 {
12086 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012087 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088 EMSG2(_(e_invarg2), eap->arg);
12089 return;
12090 }
12091 end = skip_regexp(p + 1, *p, TRUE, NULL);
12092 if (!eap->skip)
12093 {
12094 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12095 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012096 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012097 eap->errmsg = e_trailing;
12098 return;
12099 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012100 if (*end != *p)
12101 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012102 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012103 EMSG2(_(e_invarg2), p);
12104 return;
12105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012106
12107 c = *end;
12108 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012109 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012110 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012111 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012112 }
12113 }
12114 eap->nextcmd = find_nextcmd(end);
12115}
12116#endif
12117
12118#ifdef FEAT_CRYPT
12119/*
12120 * ":X": Get crypt key
12121 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012122 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012123ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012124{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012125 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012126 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012127}
12128#endif
12129
12130#ifdef FEAT_FOLDING
12131 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012132ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012133{
12134 if (foldManualAllowed(TRUE))
12135 foldCreate(eap->line1, eap->line2);
12136}
12137
12138 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012139ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012140{
12141 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12142 eap->forceit, FALSE);
12143}
12144
12145 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012146ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012147{
12148 linenr_T lnum;
12149
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012150#ifdef FEAT_CLIPBOARD
12151 start_global_changes();
12152#endif
12153
Bram Moolenaar071d4272004-06-13 20:20:40 +000012154 /* First set the marks for all lines closed/open. */
12155 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12156 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12157 ml_setmarked(lnum);
12158
12159 /* Execute the command on the marked lines. */
12160 global_exe(eap->arg);
12161 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012162#ifdef FEAT_CLIPBOARD
12163 end_global_changes();
12164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165}
12166#endif