blob: 192f45d194b838d506f1c7b88270760e5d3fd15c [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
22#ifdef FEAT_USR_CMDS
23typedef struct ucmd
24{
25 char_u *uc_name; /* The command name */
26 long_u uc_argt; /* The argument type */
27 char_u *uc_rep; /* The command's replacement string */
28 long uc_def; /* The default value for a range/count */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int uc_compl; /* completion type */
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +010030 int uc_addr_type; /* The command's address type */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010031# ifdef FEAT_EVAL
32 scid_T uc_scriptID; /* SID where the command was defined */
33# ifdef FEAT_CMDL_COMPL
Bram Moolenaar071d4272004-06-13 20:20:40 +000034 char_u *uc_compl_arg; /* completion argument if any */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010035# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000036# endif
37} ucmd_T;
38
39#define UC_BUFFER 1 /* -buffer: local to current buffer */
40
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000041static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
43#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
44#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
45
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010046static void do_ucmd(exarg_T *eap);
47static void ex_command(exarg_T *eap);
48static void ex_delcommand(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# ifdef FEAT_CMDL_COMPL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010050static char_u *get_user_command_name(int idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051# endif
52
Bram Moolenaar958636c2014-10-21 20:01:58 +020053/* Wether a command index indicates a user command. */
54# define IS_USER_CMDIDX(idx) ((int)(idx) < 0)
55
Bram Moolenaar071d4272004-06-13 20:20:40 +000056#else
57# define ex_command ex_ni
58# define ex_comclear ex_ni
59# define ex_delcommand ex_ni
Bram Moolenaar958636c2014-10-21 20:01:58 +020060/* Wether a command index indicates a user command. */
61# define IS_USER_CMDIDX(idx) (FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000062#endif
63
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010064static int compute_buffer_local_count(int addr_type, int lnum, int local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000065#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010066static char_u *do_one_cmd(char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000067#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010068static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000069static int if_level = 0; /* depth in :if */
70#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010071static void append_command(char_u *cmd);
72static char_u *find_command(exarg_T *eap, int *full);
Bram Moolenaar071d4272004-06-13 20:20:40 +000073
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010074static void ex_abbreviate(exarg_T *eap);
75static void ex_map(exarg_T *eap);
76static void ex_unmap(exarg_T *eap);
77static void ex_mapclear(exarg_T *eap);
78static void ex_abclear(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#ifndef FEAT_MENU
80# define ex_emenu ex_ni
81# define ex_menu ex_ni
82# define ex_menutranslate ex_ni
83#endif
84#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static void ex_autocmd(exarg_T *eap);
86static void ex_doautocmd(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#else
88# define ex_autocmd ex_ni
89# define ex_doautocmd ex_ni
90# define ex_doautoall ex_ni
91#endif
92#ifdef FEAT_LISTCMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010093static void ex_bunload(exarg_T *eap);
94static void ex_buffer(exarg_T *eap);
95static void ex_bmodified(exarg_T *eap);
96static void ex_bnext(exarg_T *eap);
97static void ex_bprevious(exarg_T *eap);
98static void ex_brewind(exarg_T *eap);
99static void ex_blast(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#else
101# define ex_bunload ex_ni
102# define ex_buffer ex_ni
103# define ex_bmodified ex_ni
104# define ex_bnext ex_ni
105# define ex_bprevious ex_ni
106# define ex_brewind ex_ni
107# define ex_blast ex_ni
108# define buflist_list ex_ni
109# define ex_checktime ex_ni
110#endif
111#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
112# define ex_buffer_all ex_ni
113#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100114static char_u *getargcmd(char_u **);
115static char_u *skip_cmd_arg(char_u *p, int rembs);
116static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#ifndef FEAT_QUICKFIX
118# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000119# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120# define ex_cc ex_ni
121# define ex_cnext ex_ni
122# define ex_cfile ex_ni
123# define qf_list ex_ni
124# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200125# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000127# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#endif
129#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
130# define ex_cclose ex_ni
131# define ex_copen ex_ni
132# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +0200133# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000135#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
136# define ex_cexpr ex_ni
137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100139static int check_more(int, int);
Bram Moolenaarded27822017-01-02 14:27:34 +0100140static linenr_T get_address(exarg_T *, char_u **, int addr_type, int skip, int to_other_file, int address_count);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100141static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200142#if !defined(FEAT_PERL) \
143 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
144 || !defined(FEAT_TCL) \
145 || !defined(FEAT_RUBY) \
146 || !defined(FEAT_LUA) \
147 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000148# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100149static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100151static char_u *invalid_range(exarg_T *eap);
152static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000153#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100154static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000155#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100156static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
157static void ex_highlight(exarg_T *eap);
158static void ex_colorscheme(exarg_T *eap);
159static void ex_quit(exarg_T *eap);
160static void ex_cquit(exarg_T *eap);
161static void ex_quit_all(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100163static void ex_close(exarg_T *eap);
164static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
165static void ex_only(exarg_T *eap);
166static void ex_resize(exarg_T *eap);
167static void ex_stag(exarg_T *eap);
168static void ex_tabclose(exarg_T *eap);
169static void ex_tabonly(exarg_T *eap);
170static void ex_tabnext(exarg_T *eap);
171static void ex_tabmove(exarg_T *eap);
172static void ex_tabs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173#else
174# define ex_close ex_ni
175# define ex_only ex_ni
176# define ex_all ex_ni
177# define ex_resize ex_ni
178# define ex_splitview ex_ni
179# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000180# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000181# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000182# define ex_tabs ex_ni
183# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000184# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185#endif
186#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100187static void ex_pclose(exarg_T *eap);
188static void ex_ptag(exarg_T *eap);
189static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190#else
191# define ex_pclose ex_ni
192# define ex_ptag ex_ni
193# define ex_pedit ex_ni
194#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100195static void ex_hide(exarg_T *eap);
196static void ex_stop(exarg_T *eap);
197static void ex_exit(exarg_T *eap);
198static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100200static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#else
202# define ex_goto ex_ni
203#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100204static void ex_shell(exarg_T *eap);
205static void ex_preserve(exarg_T *eap);
206static void ex_recover(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207#ifndef FEAT_LISTCMDS
208# define ex_argedit ex_ni
209# define ex_argadd ex_ni
210# define ex_argdelete ex_ni
211# define ex_listdo ex_ni
212#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100213static void ex_mode(exarg_T *eap);
214static void ex_wrongmodifier(exarg_T *eap);
215static void ex_find(exarg_T *eap);
216static void ex_open(exarg_T *eap);
217static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
219# define ex_drop ex_ni
220#endif
221#ifndef FEAT_GUI
222# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#endif
225#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100226static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227#else
228# define ex_tearoff ex_ni
229#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000230#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100231static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232#else
233# define ex_popup ex_ni
234#endif
235#ifndef FEAT_GUI_MSWIN
236# define ex_simalt ex_ni
237#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000238#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239# define gui_mch_find_dialog ex_ni
240# define gui_mch_replace_dialog ex_ni
241#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000242#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243# define ex_helpfind ex_ni
244#endif
245#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100246# define ex_cscope ex_ni
247# define ex_scscope ex_ni
248# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249#endif
250#ifndef FEAT_SYN_HL
251# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200252# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000253#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200254#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200255# define ex_syntime ex_ni
256#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000257#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000258# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000259# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000260# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000261# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000262# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000263#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200264#ifndef FEAT_PERSISTENT_UNDO
265# define ex_rundo ex_ni
266# define ex_wundo ex_ni
267#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200268#ifndef FEAT_LUA
269# define ex_lua ex_script_ni
270# define ex_luado ex_ni
271# define ex_luafile ex_ni
272#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000273#ifndef FEAT_MZSCHEME
274# define ex_mzscheme ex_script_ni
275# define ex_mzfile ex_ni
276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#ifndef FEAT_PERL
278# define ex_perl ex_script_ni
279# define ex_perldo ex_ni
280#endif
281#ifndef FEAT_PYTHON
282# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200283# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284# define ex_pyfile ex_ni
285#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200286#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200287# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200288# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200289# define ex_py3file ex_ni
290#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100291#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
292# define ex_pyx ex_script_ni
293# define ex_pyxdo ex_ni
294# define ex_pyxfile ex_ni
295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296#ifndef FEAT_TCL
297# define ex_tcl ex_script_ni
298# define ex_tcldo ex_ni
299# define ex_tclfile ex_ni
300#endif
301#ifndef FEAT_RUBY
302# define ex_ruby ex_script_ni
303# define ex_rubydo ex_ni
304# define ex_rubyfile ex_ni
305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306#ifndef FEAT_KEYMAP
307# define ex_loadkeymap ex_ni
308#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100309static void ex_swapname(exarg_T *eap);
310static void ex_syncbind(exarg_T *eap);
311static void ex_read(exarg_T *eap);
312static void ex_pwd(exarg_T *eap);
313static void ex_equal(exarg_T *eap);
314static void ex_sleep(exarg_T *eap);
315static void do_exmap(exarg_T *eap, int isabbrev);
316static void ex_winsize(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_wincmd(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319#else
320# define ex_wincmd ex_ni
321#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000322#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100323static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324#else
325# define ex_winpos ex_ni
326#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100327static void ex_operators(exarg_T *eap);
328static void ex_put(exarg_T *eap);
329static void ex_copymove(exarg_T *eap);
330static void ex_submagic(exarg_T *eap);
331static void ex_join(exarg_T *eap);
332static void ex_at(exarg_T *eap);
333static void ex_bang(exarg_T *eap);
334static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200335#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100336static void ex_wundo(exarg_T *eap);
337static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200338#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100339static void ex_redo(exarg_T *eap);
340static void ex_later(exarg_T *eap);
341static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100342static void ex_redrawstatus(exarg_T *eap);
343static void close_redir(void);
344static void ex_mkrc(exarg_T *eap);
345static void ex_mark(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346#ifdef FEAT_USR_CMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100347static char_u *uc_fun_cmd(void);
348static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100350static void ex_startinsert(exarg_T *eap);
351static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100353static void ex_checkpath(exarg_T *eap);
354static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355#else
356# define ex_findpat ex_ni
357# define ex_checkpath ex_ni
358#endif
359#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100360static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361#else
362# define ex_psearch ex_ni
363#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100364static void ex_tag(exarg_T *eap);
365static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366#ifndef FEAT_EVAL
367# define ex_scriptnames ex_ni
368# define ex_finish ex_ni
369# define ex_echo ex_ni
370# define ex_echohl ex_ni
371# define ex_execute ex_ni
372# define ex_call ex_ni
373# define ex_if ex_ni
374# define ex_endif ex_ni
375# define ex_else ex_ni
376# define ex_while ex_ni
377# define ex_continue ex_ni
378# define ex_break ex_ni
379# define ex_endwhile ex_ni
380# define ex_throw ex_ni
381# define ex_try ex_ni
382# define ex_catch ex_ni
383# define ex_finally ex_ni
384# define ex_endtry ex_ni
385# define ex_endfunction ex_ni
386# define ex_let ex_ni
387# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000388# define ex_lockvar ex_ni
389# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390# define ex_function ex_ni
391# define ex_delfunction ex_ni
392# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000393# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100395static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100397static int makeopens(FILE *fd, char_u *dirnow);
398static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
399static void ex_loadview(exarg_T *eap);
400static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000401static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402#else
403# define ex_loadview ex_ni
404#endif
405#ifndef FEAT_EVAL
406# define ex_compiler ex_ni
407#endif
408#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100409static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410#else
411# define ex_viminfo ex_ni
412#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100413static void ex_behave(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100415static void ex_filetype(exarg_T *eap);
416static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417#else
418# define ex_filetype ex_ni
419# define ex_setfiletype ex_ni
420#endif
421#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000422# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423# define ex_diffpatch ex_ni
424# define ex_diffgetput ex_ni
425# define ex_diffsplit ex_ni
426# define ex_diffthis ex_ni
427# define ex_diffupdate ex_ni
428#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100429static void ex_digraphs(exarg_T *eap);
430static void ex_set(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
432# define ex_options ex_ni
433#endif
434#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100435static void ex_nohlsearch(exarg_T *eap);
436static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437#else
438# define ex_nohlsearch ex_ni
439# define ex_match ex_ni
440#endif
441#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100442static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443#else
444# define ex_X ex_ni
445#endif
446#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100447static void ex_fold(exarg_T *eap);
448static void ex_foldopen(exarg_T *eap);
449static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450#else
451# define ex_fold ex_ni
452# define ex_foldopen ex_ni
453# define ex_folddo ex_ni
454#endif
455#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
456 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
457# define ex_language ex_ni
458#endif
459#ifndef FEAT_SIGNS
460# define ex_sign ex_ni
461#endif
462#ifndef FEAT_SUN_WORKSHOP
463# define ex_wsverb ex_ni
464#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000465#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200466# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000467# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200468# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470
471#ifndef FEAT_EVAL
472# define ex_debug ex_ni
473# define ex_breakadd ex_ni
474# define ex_debuggreedy ex_ni
475# define ex_breakdel ex_ni
476# define ex_breaklist ex_ni
477#endif
478
479#ifndef FEAT_CMDHIST
480# define ex_history ex_ni
481#endif
482#ifndef FEAT_JUMPLIST
483# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200484# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485# define ex_changes ex_ni
486#endif
487
Bram Moolenaar05159a02005-02-26 23:04:13 +0000488#ifndef FEAT_PROFILE
489# define ex_profile ex_ni
490#endif
491
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492/*
493 * Declare cmdnames[].
494 */
495#define DO_DECLARE_EXCMD
496#include "ex_cmds.h"
497
498/*
499 * Table used to quickly search for a command, based on its first character.
500 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000501static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502{
503 CMD_append,
504 CMD_buffer,
505 CMD_change,
506 CMD_delete,
507 CMD_edit,
508 CMD_file,
509 CMD_global,
510 CMD_help,
511 CMD_insert,
512 CMD_join,
513 CMD_k,
514 CMD_list,
515 CMD_move,
516 CMD_next,
517 CMD_open,
518 CMD_print,
519 CMD_quit,
520 CMD_read,
521 CMD_substitute,
522 CMD_t,
523 CMD_undo,
524 CMD_vglobal,
525 CMD_write,
526 CMD_xit,
527 CMD_yank,
528 CMD_z,
529 CMD_bang
530};
531
532static char_u dollar_command[2] = {'$', 0};
533
534
535#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000536/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537typedef struct
538{
539 char_u *line; /* command line */
540 linenr_T lnum; /* sourcing_lnum of the line */
541} wcmd_T;
542
543/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000544 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000546 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000548struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549{
550 garray_T *lines_gap; /* growarray with line info */
551 int current_line; /* last read line from growarray */
552 int repeating; /* TRUE when looping a second time */
553 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100554 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 void *cookie;
556};
557
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100558static char_u *get_loop_line(int c, void *cookie, int indent);
559static int store_loop_line(garray_T *gap, char_u *line);
560static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000561
562/* Struct to save a few things while debugging. Used in do_cmdline() only. */
563struct dbg_stuff
564{
565 int trylevel;
566 int force_abort;
567 except_T *caught_stack;
568 char_u *vv_exception;
569 char_u *vv_throwpoint;
570 int did_emsg;
571 int got_int;
572 int did_throw;
573 int need_rethrow;
574 int check_cstack;
575 except_T *current_exception;
576};
577
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100578static void save_dbg_stuff(struct dbg_stuff *dsp);
579static void restore_dbg_stuff(struct dbg_stuff *dsp);
Bram Moolenaared203462004-06-16 11:19:22 +0000580
581 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100582save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000583{
584 dsp->trylevel = trylevel; trylevel = 0;
585 dsp->force_abort = force_abort; force_abort = FALSE;
586 dsp->caught_stack = caught_stack; caught_stack = NULL;
587 dsp->vv_exception = v_exception(NULL);
588 dsp->vv_throwpoint = v_throwpoint(NULL);
589
590 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
591 dsp->did_emsg = did_emsg; did_emsg = FALSE;
592 dsp->got_int = got_int; got_int = FALSE;
593 dsp->did_throw = did_throw; did_throw = FALSE;
594 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
595 dsp->check_cstack = check_cstack; check_cstack = FALSE;
596 dsp->current_exception = current_exception; current_exception = NULL;
597}
598
599 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100600restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000601{
602 suppress_errthrow = FALSE;
603 trylevel = dsp->trylevel;
604 force_abort = dsp->force_abort;
605 caught_stack = dsp->caught_stack;
606 (void)v_exception(dsp->vv_exception);
607 (void)v_throwpoint(dsp->vv_throwpoint);
608 did_emsg = dsp->did_emsg;
609 got_int = dsp->got_int;
610 did_throw = dsp->did_throw;
611 need_rethrow = dsp->need_rethrow;
612 check_cstack = dsp->check_cstack;
613 current_exception = dsp->current_exception;
614}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615#endif
616
617
618/*
619 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
620 * command is given.
621 */
622 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100623do_exmode(
624 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625{
626 int save_msg_scroll;
627 int prev_msg_row;
628 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000629 int changedtick;
630
631 if (improved)
632 exmode_active = EXMODE_VIM;
633 else
634 exmode_active = EXMODE_NORMAL;
635 State = NORMAL;
636
637 /* When using ":global /pat/ visual" and then "Q" we return to continue
638 * the :global command. */
639 if (global_busy)
640 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641
642 save_msg_scroll = msg_scroll;
643 ++RedrawingDisabled; /* don't redisplay the window */
644 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645#ifdef FEAT_GUI
646 /* Ignore scrollbar and mouse events in Ex mode */
647 ++hold_gui_events;
648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
650 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
651 while (exmode_active)
652 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000653 /* Check for a ":normal" command and no more characters left. */
654 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
655 {
656 exmode_active = FALSE;
657 break;
658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 msg_scroll = TRUE;
660 need_wait_return = FALSE;
661 ex_pressedreturn = FALSE;
662 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000663 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 prev_msg_row = msg_row;
665 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 if (improved)
667 {
668 cmdline_row = msg_row;
669 do_cmdline(NULL, getexline, NULL, 0);
670 }
671 else
672 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
673 lines_left = Rows - 1;
674
Bram Moolenaardf177f62005-02-22 08:39:57 +0000675 if ((prev_line != curwin->w_cursor.lnum
676 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000678 if (curbuf->b_ml.ml_flags & ML_EMPTY)
679 EMSG(_(e_emptybuf));
680 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000682 if (ex_pressedreturn)
683 {
684 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000685 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000686 msg_row = prev_msg_row;
687 if (prev_msg_row == Rows - 1)
688 msg_row--;
689 }
690 msg_col = 0;
691 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
692 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000695 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
696 {
697 if (curbuf->b_ml.ml_flags & ML_EMPTY)
698 EMSG(_(e_emptybuf));
699 else
700 EMSG(_("E501: At end-of-file"));
701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 }
703
704#ifdef FEAT_GUI
705 --hold_gui_events;
706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 --RedrawingDisabled;
708 --no_wait_return;
709 update_screen(CLEAR);
710 need_wait_return = FALSE;
711 msg_scroll = save_msg_scroll;
712}
713
714/*
715 * Execute a simple command line. Used for translated commands like "*".
716 */
717 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100718do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719{
720 return do_cmdline(cmd, NULL, NULL,
721 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
722}
723
724/*
725 * do_cmdline(): execute one Ex command line
726 *
727 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100728 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 * 2. Split up in parts separated with '|'.
730 *
731 * This function can be called recursively!
732 *
733 * flags:
734 * DOCMD_VERBOSE - The command will be included in the error message.
735 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100736 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 * DOCMD_KEYTYPED - Don't reset KeyTyped.
738 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
739 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
740 *
741 * return FAIL if cmdline could not be executed, OK otherwise
742 */
743 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100744do_cmdline(
745 char_u *cmdline,
746 char_u *(*fgetline)(int, void *, int),
747 void *cookie, /* argument for fgetline() */
748 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749{
750 char_u *next_cmdline; /* next cmd to execute */
751 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100752 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 static int recursive = 0; /* recursive depth */
754 int msg_didout_before_start = 0;
755 int count = 0; /* line number count */
756 int did_inc = FALSE; /* incremented RedrawingDisabled */
757 int retval = OK;
758#ifdef FEAT_EVAL
759 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000760 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 int current_line = 0; /* active line in lines_ga */
762 char_u *fname = NULL; /* function or script name */
763 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
764 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000765 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 int initial_trylevel;
767 struct msglist **saved_msg_list = NULL;
768 struct msglist *private_msg_list;
769
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100770 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100771 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000773 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000775 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100777# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778# define cmd_cookie cookie
779#endif
780 static int call_depth = 0; /* recursiveness */
781
782#ifdef FEAT_EVAL
783 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
784 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000785 * This ensures that the do_errthrow() call in do_one_cmd() does not
786 * combine the messages stored by an earlier invocation of do_one_cmd()
787 * with the command name of the later one. This would happen when
788 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 saved_msg_list = msg_list;
790 msg_list = &private_msg_list;
791 private_msg_list = NULL;
792#endif
793
794 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100795 * here. The value of 200 allows nested function calls, ":source", etc.
796 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100797 if (call_depth >= 200
798#ifdef FEAT_EVAL
799 && call_depth >= p_mfd
800#endif
801 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 {
803 EMSG(_("E169: Command too recursive"));
804#ifdef FEAT_EVAL
805 /* When converting to an exception, we do not include the command name
806 * since this is not an error of the specific command. */
807 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
808 msg_list = saved_msg_list;
809#endif
810 return FAIL;
811 }
812 ++call_depth;
813
814#ifdef FEAT_EVAL
815 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000816 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 cstack.cs_trylevel = 0;
818 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000819 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
821
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100822 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823
824 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100825 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000826 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 ++ex_nesting_level;
828
829 /* Get the function or script name and the address where the next breakpoint
830 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000831 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 {
833 fname = func_name(real_cookie);
834 breakpoint = func_breakpoint(real_cookie);
835 dbg_tick = func_dbg_tick(real_cookie);
836 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100837 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 {
839 fname = sourcing_name;
840 breakpoint = source_breakpoint(real_cookie);
841 dbg_tick = source_dbg_tick(real_cookie);
842 }
843
844 /*
845 * Initialize "force_abort" and "suppress_errthrow" at the top level.
846 */
847 if (!recursive)
848 {
849 force_abort = FALSE;
850 suppress_errthrow = FALSE;
851 }
852
853 /*
854 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000855 * exception handling (used when debugging). Otherwise clear it to avoid
856 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000858 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000859 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000860 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200861 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862
863 initial_trylevel = trylevel;
864
865 /*
866 * "did_throw" will be set to TRUE when an exception is being thrown.
867 */
868 did_throw = FALSE;
869#endif
870 /*
871 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000872 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 * If force_abort is set, we cancel everything.
874 */
875 did_emsg = FALSE;
876
877 /*
878 * KeyTyped is only set when calling vgetc(). Reset it here when not
879 * calling vgetc() (sourced command lines).
880 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100881 if (!(flags & DOCMD_KEYTYPED)
882 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 KeyTyped = FALSE;
884
885 /*
886 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000887 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 * - for multiple commands on one line, separated with '|'
889 * - when repeating until there are no more lines (for ":source")
890 */
891 next_cmdline = cmdline;
892 do
893 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000894#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100895 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000896#endif
897
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000898 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 if (next_cmdline == NULL
900#ifdef FEAT_EVAL
901 && !force_abort
902 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000903 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904#endif
905 )
906 did_emsg = FALSE;
907
908 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000909 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100910 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 * 3. If a line is given: Make a copy, so we can mess with it.
912 */
913
914#ifdef FEAT_EVAL
915 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000916 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 {
918 /* Each '|' separated command is stored separately in lines_ga, to
919 * be able to jump to it. Don't use next_cmdline now. */
920 vim_free(cmdline_copy);
921 cmdline_copy = NULL;
922
923 /* Check if a function has returned or, unless it has an unclosed
924 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000925 if (getline_is_func)
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 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000929 func_line_end(real_cookie);
930# endif
931 if (func_has_ended(real_cookie))
932 {
933 retval = FAIL;
934 break;
935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000937#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000938 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100939 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000940 script_line_end();
941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942
943 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100944 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 {
946 retval = FAIL;
947 break;
948 }
949
950 /* If breakpoints have been added/deleted need to check for it. */
951 if (breakpoint != NULL && dbg_tick != NULL
952 && *dbg_tick != debug_tick)
953 {
954 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100955 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 fname, sourcing_lnum);
957 *dbg_tick = debug_tick;
958 }
959
960 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
961 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
962
963 /* Did we encounter a breakpoint? */
964 if (breakpoint != NULL && *breakpoint != 0
965 && *breakpoint <= sourcing_lnum)
966 {
967 dbg_breakpoint(fname, sourcing_lnum);
968 /* Find next breakpoint. */
969 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100970 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 fname, sourcing_lnum);
972 *dbg_tick = debug_tick;
973 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000974# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000975 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000976 {
977 if (getline_is_func)
978 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100979 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000980 script_line_start();
981 }
982# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 }
984
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000985 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000987 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100988 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 * below, so that it stores lines in or reads them from
990 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000991 * while/for loop. */
992 cmd_getline = get_loop_line;
993 cmd_cookie = (void *)&cmd_loop_cookie;
994 cmd_loop_cookie.lines_gap = &lines_ga;
995 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100996 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000997 cmd_loop_cookie.cookie = cookie;
998 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 }
1000 else
1001 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001002 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 cmd_cookie = cookie;
1004 }
1005#endif
1006
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001007 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 if (next_cmdline == NULL)
1009 {
1010 /*
1011 * Need to set msg_didout for the first line after an ":if",
1012 * otherwise the ":if" will be overwritten.
1013 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001014 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001016 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017#ifdef FEAT_EVAL
1018 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1019#else
1020 0
1021#endif
1022 )) == NULL)
1023 {
1024 /* Don't call wait_return for aborted command line. The NULL
1025 * returned for the end of a sourced file or executed function
1026 * doesn't do this. */
1027 if (KeyTyped && !(flags & DOCMD_REPEAT))
1028 need_wait_return = FALSE;
1029 retval = FAIL;
1030 break;
1031 }
1032 used_getline = TRUE;
1033
1034 /*
1035 * Keep the first typed line. Clear it when more lines are typed.
1036 */
1037 if (flags & DOCMD_KEEPLINE)
1038 {
1039 vim_free(repeat_cmdline);
1040 if (count == 0)
1041 repeat_cmdline = vim_strsave(next_cmdline);
1042 else
1043 repeat_cmdline = NULL;
1044 }
1045 }
1046
1047 /* 3. Make a copy of the command so we can mess with it. */
1048 else if (cmdline_copy == NULL)
1049 {
1050 next_cmdline = vim_strsave(next_cmdline);
1051 if (next_cmdline == NULL)
1052 {
1053 EMSG(_(e_outofmem));
1054 retval = FAIL;
1055 break;
1056 }
1057 }
1058 cmdline_copy = next_cmdline;
1059
1060#ifdef FEAT_EVAL
1061 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001062 * Save the current line when inside a ":while" or ":for", and when
1063 * the command looks like a ":while" or ":for", because we may need it
1064 * later. When there is a '|' and another command, it is stored
1065 * separately, because we need to be able to jump back to it from an
1066 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001068 if (current_line == lines_ga.ga_len
1069 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001071 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 {
1073 retval = FAIL;
1074 break;
1075 }
1076 }
1077 did_endif = FALSE;
1078#endif
1079
1080 if (count++ == 0)
1081 {
1082 /*
1083 * All output from the commands is put below each other, without
1084 * waiting for a return. Don't do this when executing commands
1085 * from a script or when being called recursive (e.g. for ":e
1086 * +command file").
1087 */
1088 if (!(flags & DOCMD_NOWAIT) && !recursive)
1089 {
1090 msg_didout_before_start = msg_didout;
1091 msg_didany = FALSE; /* no output yet */
1092 msg_start();
1093 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001094 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 ++RedrawingDisabled;
1096 did_inc = TRUE;
1097 }
1098 }
1099
1100 if (p_verbose >= 15 && sourcing_name != NULL)
1101 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001103 verbose_enter_scroll();
1104
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 smsg((char_u *)_("line %ld: %s"),
1106 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001107 if (msg_silent == 0)
1108 msg_puts((char_u *)"\n"); /* don't overwrite this */
1109
1110 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 --no_wait_return;
1112 }
1113
1114 /*
1115 * 2. Execute one '|' separated command.
1116 * do_one_cmd() will return NULL if there is no trailing '|'.
1117 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1118 */
1119 ++recursive;
1120 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1121#ifdef FEAT_EVAL
1122 &cstack,
1123#endif
1124 cmd_getline, cmd_cookie);
1125 --recursive;
1126
1127#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001128 if (cmd_cookie == (void *)&cmd_loop_cookie)
1129 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001131 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132#endif
1133
1134 if (next_cmdline == NULL)
1135 {
1136 vim_free(cmdline_copy);
1137 cmdline_copy = NULL;
1138#ifdef FEAT_CMDHIST
1139 /*
1140 * If the command was typed, remember it for the ':' register.
1141 * Do this AFTER executing the command to make :@: work.
1142 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001143 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 && new_last_cmdline != NULL)
1145 {
1146 vim_free(last_cmdline);
1147 last_cmdline = new_last_cmdline;
1148 new_last_cmdline = NULL;
1149 }
1150#endif
1151 }
1152 else
1153 {
1154 /* need to copy the command after the '|' to cmdline_copy, for the
1155 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001156 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 next_cmdline = cmdline_copy;
1158 }
1159
1160
1161#ifdef FEAT_EVAL
1162 /* reset did_emsg for a function that is not aborted by an error */
1163 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001164 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 && !func_has_abort(real_cookie))
1166 did_emsg = FALSE;
1167
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001168 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 {
1170 ++current_line;
1171
1172 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001173 * An ":endwhile", ":endfor" and ":continue" is handled here.
1174 * If we were executing commands, jump back to the ":while" or
1175 * ":for".
1176 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001178 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001180 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001182 /* Jump back to the matching ":while" or ":for". Be careful
1183 * not to use a cs_line[] from an entry that isn't a ":while"
1184 * or ":for": It would make "current_line" invalid and can
1185 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 if (!did_emsg && !got_int && !did_throw
1187 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001188 && (cstack.cs_flags[cstack.cs_idx]
1189 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 && cstack.cs_line[cstack.cs_idx] >= 0
1191 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1192 {
1193 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001194 /* remember we jumped there */
1195 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 line_breakcheck(); /* check if CTRL-C typed */
1197
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001198 /* Check for the next breakpoint at or after the ":while"
1199 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 if (breakpoint != NULL)
1201 {
1202 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001203 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 fname,
1205 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1206 *dbg_tick = debug_tick;
1207 }
1208 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001209 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001211 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001213 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1214 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 }
1216 }
1217
1218 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001219 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001221 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001223 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1225 }
1226 }
1227
1228 /*
1229 * When not inside any ":while" loop, clear remembered lines.
1230 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001231 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 {
1233 if (lines_ga.ga_len > 0)
1234 {
1235 sourcing_lnum =
1236 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1237 free_cmdlines(&lines_ga);
1238 }
1239 current_line = 0;
1240 }
1241
1242 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001243 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1244 * being restored at the ":endtry". Reset them here and set the
1245 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1246 * This includes the case where a missing ":endif", ":endwhile" or
1247 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001249 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001251 cstack.cs_lflags &= ~CSL_HAD_FINA;
1252 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1253 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 did_throw ? (void *)current_exception : NULL);
1255 did_emsg = got_int = did_throw = FALSE;
1256 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1257 }
1258
1259 /* Update global "trylevel" for recursive calls to do_cmdline() from
1260 * within this loop. */
1261 trylevel = initial_trylevel + cstack.cs_trylevel;
1262
1263 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001264 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 * files) is aborted because of an error, an interrupt, or an uncaught
1266 * exception, cancel everything. If it is left normally, reset
1267 * force_abort to get the non-EH compatible abortion behavior for
1268 * the rest of the script.
1269 */
1270 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1271 force_abort = FALSE;
1272
1273 /* Convert an interrupt to an exception if appropriate. */
1274 (void)do_intthrow(&cstack);
1275#endif /* FEAT_EVAL */
1276
1277 }
1278 /*
1279 * Continue executing command lines when:
1280 * - no CTRL-C typed, no aborting error, no exception thrown or try
1281 * conditionals need to be checked for executing finally clauses or
1282 * catching an interrupt exception
1283 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001284 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 * looping for ":source" command or function call.
1286 */
1287 while (!((got_int
1288#ifdef FEAT_EVAL
1289 || (did_emsg && force_abort) || did_throw
1290#endif
1291 )
1292#ifdef FEAT_EVAL
1293 && cstack.cs_trylevel == 0
1294#endif
1295 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001296 && !(did_emsg
1297#ifdef FEAT_EVAL
1298 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001299 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001300 * the :endtry to be missed. */
1301 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1302#endif
1303 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001304 && (getline_equal(fgetline, cookie, getexmodeline)
1305 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 && (next_cmdline != NULL
1307#ifdef FEAT_EVAL
1308 || cstack.cs_idx >= 0
1309#endif
1310 || (flags & DOCMD_REPEAT)));
1311
1312 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001313 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314#ifdef FEAT_EVAL
1315 free_cmdlines(&lines_ga);
1316 ga_clear(&lines_ga);
1317
1318 if (cstack.cs_idx >= 0)
1319 {
1320 /*
1321 * If a sourced file or executed function ran to its end, report the
1322 * unclosed conditional.
1323 */
1324 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001325 && ((getline_equal(fgetline, cookie, getsourceline)
1326 && !source_finished(fgetline, cookie))
1327 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 && !func_has_ended(real_cookie))))
1329 {
1330 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1331 EMSG(_(e_endtry));
1332 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1333 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001334 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1335 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 else
1337 EMSG(_(e_endif));
1338 }
1339
1340 /*
1341 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1342 * ":endtry" in a sourced file or executed function. If the try
1343 * conditional is in its finally clause, ignore anything pending.
1344 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001345 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 */
1347 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001348 {
1349 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1350
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001351 if (idx >= 0)
1352 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001353 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1354 &cstack.cs_looplevel);
1355 }
1356 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 trylevel = initial_trylevel;
1358 }
1359
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001360 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1361 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001363 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 ? (char_u *)"endfunction" : (char_u *)NULL);
1365
1366 if (trylevel == 0)
1367 {
1368 /*
1369 * When an exception is being thrown out of the outermost try
1370 * conditional, discard the uncaught exception, disable the conversion
1371 * of interrupts or errors to exceptions, and ensure that no more
1372 * commands are executed.
1373 */
1374 if (did_throw)
1375 {
1376 void *p = NULL;
1377 char_u *saved_sourcing_name;
1378 int saved_sourcing_lnum;
1379 struct msglist *messages = NULL, *next;
1380
1381 /*
1382 * If the uncaught exception is a user exception, report it as an
1383 * error. If it is an error exception, display the saved error
1384 * message now. For an interrupt exception, do nothing; the
1385 * interrupt message is given elsewhere.
1386 */
1387 switch (current_exception->type)
1388 {
1389 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001390 vim_snprintf((char *)IObuff, IOSIZE,
1391 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 current_exception->value);
1393 p = vim_strsave(IObuff);
1394 break;
1395 case ET_ERROR:
1396 messages = current_exception->messages;
1397 current_exception->messages = NULL;
1398 break;
1399 case ET_INTERRUPT:
1400 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 }
1402
1403 saved_sourcing_name = sourcing_name;
1404 saved_sourcing_lnum = sourcing_lnum;
1405 sourcing_name = current_exception->throw_name;
1406 sourcing_lnum = current_exception->throw_lnum;
1407 current_exception->throw_name = NULL;
1408
1409 discard_current_exception(); /* uses IObuff if 'verbose' */
1410 suppress_errthrow = TRUE;
1411 force_abort = TRUE;
1412
1413 if (messages != NULL)
1414 {
1415 do
1416 {
1417 next = messages->next;
1418 emsg(messages->msg);
1419 vim_free(messages->msg);
1420 vim_free(messages);
1421 messages = next;
1422 }
1423 while (messages != NULL);
1424 }
1425 else if (p != NULL)
1426 {
1427 emsg(p);
1428 vim_free(p);
1429 }
1430 vim_free(sourcing_name);
1431 sourcing_name = saved_sourcing_name;
1432 sourcing_lnum = saved_sourcing_lnum;
1433 }
1434
1435 /*
1436 * On an interrupt or an aborting error not converted to an exception,
1437 * disable the conversion of errors to exceptions. (Interrupts are not
1438 * converted any more, here.) This enables also the interrupt message
1439 * when force_abort is set and did_emsg unset in case of an interrupt
1440 * from a finally clause after an error.
1441 */
1442 else if (got_int || (did_emsg && force_abort))
1443 suppress_errthrow = TRUE;
1444 }
1445
1446 /*
1447 * The current cstack will be freed when do_cmdline() returns. An uncaught
1448 * exception will have to be rethrown in the previous cstack. If a function
1449 * has just returned or a script file was just finished and the previous
1450 * cstack belongs to the same function or, respectively, script file, it
1451 * will have to be checked for finally clauses to be executed due to the
1452 * ":return" or ":finish". This is done in do_one_cmd().
1453 */
1454 if (did_throw)
1455 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001456 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001458 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 && ex_nesting_level > func_level(real_cookie) + 1))
1460 {
1461 if (!did_throw)
1462 check_cstack = TRUE;
1463 }
1464 else
1465 {
1466 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001467 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 --ex_nesting_level;
1469 /*
1470 * Go to debug mode when returning from a function in which we are
1471 * single-stepping.
1472 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001473 if ((getline_equal(fgetline, cookie, getsourceline)
1474 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001476 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 ? (char_u *)_("End of sourced file")
1478 : (char_u *)_("End of function"));
1479 }
1480
1481 /*
1482 * Restore the exception environment (done after returning from the
1483 * debugger).
1484 */
1485 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001486 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487
1488 msg_list = saved_msg_list;
1489#endif /* FEAT_EVAL */
1490
1491 /*
1492 * If there was too much output to fit on the command line, ask the user to
1493 * hit return before redrawing the screen. With the ":global" command we do
1494 * this only once after the command is finished.
1495 */
1496 if (did_inc)
1497 {
1498 --RedrawingDisabled;
1499 --no_wait_return;
1500 msg_scroll = FALSE;
1501
1502 /*
1503 * When just finished an ":if"-":else" which was typed, no need to
1504 * wait for hit-return. Also for an error situation.
1505 */
1506 if (retval == FAIL
1507#ifdef FEAT_EVAL
1508 || (did_endif && KeyTyped && !did_emsg)
1509#endif
1510 )
1511 {
1512 need_wait_return = FALSE;
1513 msg_didany = FALSE; /* don't wait when restarting edit */
1514 }
1515 else if (need_wait_return)
1516 {
1517 /*
1518 * The msg_start() above clears msg_didout. The wait_return we do
1519 * here should not overwrite the command that may be shown before
1520 * doing that.
1521 */
1522 msg_didout |= msg_didout_before_start;
1523 wait_return(FALSE);
1524 }
1525 }
1526
Bram Moolenaar2a942252012-11-28 23:03:07 +01001527#ifdef FEAT_EVAL
1528 did_endif = FALSE; /* in case do_cmdline used recursively */
1529#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 /*
1531 * Reset if_level, in case a sourced script file contains more ":if" than
1532 * ":endif" (could be ":if x | foo | endif").
1533 */
1534 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001535#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001536
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 --call_depth;
1538 return retval;
1539}
1540
1541#ifdef FEAT_EVAL
1542/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001543 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 */
1545 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001546get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001548 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 wcmd_T *wp;
1550 char_u *line;
1551
1552 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1553 {
1554 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001555 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001557 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 if (cp->getline == NULL)
1559 line = getcmdline(c, 0L, indent);
1560 else
1561 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001562 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 ++cp->current_line;
1564
1565 return line;
1566 }
1567
1568 KeyTyped = FALSE;
1569 ++cp->current_line;
1570 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1571 sourcing_lnum = wp->lnum;
1572 return vim_strsave(wp->line);
1573}
1574
1575/*
1576 * Store a line in "gap" so that a ":while" loop can execute it again.
1577 */
1578 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001579store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580{
1581 if (ga_grow(gap, 1) == FAIL)
1582 return FAIL;
1583 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1584 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1585 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 return OK;
1587}
1588
1589/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001590 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 */
1592 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001593free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594{
1595 while (gap->ga_len > 0)
1596 {
1597 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1598 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 }
1600}
1601#endif
1602
1603/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001604 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1605 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001608getline_equal(
1609 char_u *(*fgetline)(int, void *, int),
1610 void *cookie UNUSED, /* argument for fgetline() */
1611 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612{
1613#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001614 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001615 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616
Bram Moolenaar89d40322006-08-29 15:30:07 +00001617 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001618 * function that's originally used to obtain the lines. This may be
1619 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001620 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001621 cp = (struct loop_cookie *)cookie;
1622 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 {
1624 gp = cp->getline;
1625 cp = cp->cookie;
1626 }
1627 return gp == func;
1628#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001629 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630#endif
1631}
1632
1633#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1634/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001635 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 * getline function. Otherwise return "cookie".
1637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001639getline_cookie(
1640 char_u *(*fgetline)(int, void *, int) UNUSED,
1641 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642{
1643# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001644 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001645 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646
Bram Moolenaar89d40322006-08-29 15:30:07 +00001647 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001648 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001650 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001651 cp = (struct loop_cookie *)cookie;
1652 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 {
1654 gp = cp->getline;
1655 cp = cp->cookie;
1656 }
1657 return cp;
1658# else
1659 return cookie;
1660# endif
1661}
1662#endif
1663
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001664
1665/*
1666 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1667 * ":bwipeout", etc.
1668 * Returns the buffer number.
1669 */
1670 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001671compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001672{
1673 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001674 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001675 int count = offset;
1676
1677 buf = firstbuf;
1678 while (buf->b_next != NULL && buf->b_fnum < lnum)
1679 buf = buf->b_next;
1680 while (count != 0)
1681 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001682 count += (offset < 0) ? 1 : -1;
1683 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1684 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001685 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001686 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001687 if (addr_type == ADDR_LOADED_BUFFERS)
1688 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001689 while (buf->b_ml.ml_mfp == NULL)
1690 {
1691 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1692 if (nextbuf == NULL)
1693 break;
1694 buf = nextbuf;
1695 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001696 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001697 /* we might have gone too far, last buffer is not loadedd */
1698 if (addr_type == ADDR_LOADED_BUFFERS)
1699 while (buf->b_ml.ml_mfp == NULL)
1700 {
1701 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1702 if (nextbuf == NULL)
1703 break;
1704 buf = nextbuf;
1705 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001706 return buf->b_fnum;
1707}
1708
Bram Moolenaarf240e182014-11-27 18:33:02 +01001709#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001710static int current_win_nr(win_T *win);
1711static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001712
1713 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001714current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001715{
1716 win_T *wp;
1717 int nr = 0;
1718
Bram Moolenaar29323592016-07-24 22:04:11 +02001719 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001720 {
1721 ++nr;
1722 if (wp == win)
1723 break;
1724 }
1725 return nr;
1726}
1727
1728 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001729current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001730{
1731 tabpage_T *tp;
1732 int nr = 0;
1733
Bram Moolenaar29323592016-07-24 22:04:11 +02001734 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001735 {
1736 ++nr;
1737 if (tp == tab)
1738 break;
1739 }
1740 return nr;
1741}
1742
1743# define CURRENT_WIN_NR current_win_nr(curwin)
1744# define LAST_WIN_NR current_win_nr(NULL)
1745# define CURRENT_TAB_NR current_tab_nr(curtab)
1746# define LAST_TAB_NR current_tab_nr(NULL)
1747#else
1748# define CURRENT_WIN_NR 1
1749# define LAST_WIN_NR 1
1750# define CURRENT_TAB_NR 1
1751# define LAST_TAB_NR 1
1752#endif
1753
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001754
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755/*
1756 * Execute one Ex command.
1757 *
1758 * If 'sourcing' is TRUE, the command will be included in the error message.
1759 *
1760 * 1. skip comment lines and leading space
1761 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001762 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001763 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001764 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001765 * 6. parse arguments
1766 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001768 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 *
1770 * This function may be called recursively!
1771 */
1772#if (_MSC_VER == 1200)
1773/*
Bram Moolenaared203462004-06-16 11:19:22 +00001774 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001776 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777#endif
1778 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001779do_one_cmd(
1780 char_u **cmdlinep,
1781 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001783 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001785 char_u *(*fgetline)(int, void *, int),
1786 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787{
1788 char_u *p;
1789 linenr_T lnum;
1790 long n;
1791 char_u *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001792 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 exarg_T ea; /* Ex command arguments */
1794 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001795 int save_msg_scroll = msg_scroll;
1796 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001798#ifdef HAVE_SANDBOX
1799 int did_sandbox = FALSE;
1800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 cmdmod_T save_cmdmod;
1802 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001803 char_u *cmd;
Bram Moolenaarded27822017-01-02 14:27:34 +01001804 int address_count = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805
1806 vim_memset(&ea, 0, sizeof(ea));
1807 ea.line1 = 1;
1808 ea.line2 = 1;
1809#ifdef FEAT_EVAL
1810 ++ex_nesting_level;
1811#endif
1812
Bram Moolenaar21691f82012-12-05 19:13:18 +01001813 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 if (quitmore
1815#ifdef FEAT_EVAL
1816 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001817 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001818#endif
1819#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001820 /* avoid that an autocommand, e.g. QuitPre, does this */
1821 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822#endif
1823 )
1824 --quitmore;
1825
1826 /*
1827 * Reset browse, confirm, etc.. They are restored when returning, for
1828 * recursive calls.
1829 */
1830 save_cmdmod = cmdmod;
1831 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1832
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001833 /* "#!anything" is handled like a comment. */
1834 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1835 goto doend;
1836
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 /*
1838 * Repeat until no more command modifiers are found.
1839 */
1840 ea.cmd = *cmdlinep;
1841 for (;;)
1842 {
1843/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001844 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 */
1846 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1847 ++ea.cmd;
1848
1849 /* in ex mode, an empty line works like :+ */
1850 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001851 && (getline_equal(fgetline, cookie, getexmodeline)
1852 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001853 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 {
1855 ea.cmd = (char_u *)"+";
1856 ex_pressedreturn = TRUE;
1857 }
1858
1859 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001860 if (*ea.cmd == '"')
1861 goto doend;
1862 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001863 {
1864 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867
1868/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001869 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 */
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001871 p = skip_range(ea.cmd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 switch (*p)
1873 {
1874 /* When adding an entry, also modify cmd_exists(). */
1875 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1876 break;
1877#ifdef FEAT_WINDOWS
1878 cmdmod.split |= WSP_ABOVE;
1879#endif
1880 continue;
1881
1882 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1883 {
1884#ifdef FEAT_WINDOWS
1885 cmdmod.split |= WSP_BELOW;
1886#endif
1887 continue;
1888 }
1889 if (checkforcmd(&ea.cmd, "browse", 3))
1890 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001891#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 cmdmod.browse = TRUE;
1893#endif
1894 continue;
1895 }
1896 if (!checkforcmd(&ea.cmd, "botright", 2))
1897 break;
1898#ifdef FEAT_WINDOWS
1899 cmdmod.split |= WSP_BOT;
1900#endif
1901 continue;
1902
1903 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1904 break;
1905#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1906 cmdmod.confirm = TRUE;
1907#endif
1908 continue;
1909
1910 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1911 {
1912 cmdmod.keepmarks = TRUE;
1913 continue;
1914 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001915 if (checkforcmd(&ea.cmd, "keepalt", 5))
1916 {
1917 cmdmod.keepalt = TRUE;
1918 continue;
1919 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001920 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1921 {
1922 cmdmod.keeppatterns = TRUE;
1923 continue;
1924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1926 break;
1927 cmdmod.keepjumps = TRUE;
1928 continue;
1929
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001930 case 'f': /* only accept ":filter {pat} cmd" */
1931 {
1932 char_u *reg_pat;
1933
1934 if (!checkforcmd(&p, "filter", 4)
1935 || *p == NUL || ends_excmd(*p))
1936 break;
Bram Moolenaard29459b2016-08-26 22:29:11 +02001937 if (*p == '!')
1938 {
1939 cmdmod.filter_force = TRUE;
1940 p = skipwhite(p + 1);
1941 if (*p == NUL || ends_excmd(*p))
1942 break;
1943 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001944 p = skip_vimgrep_pat(p, &reg_pat, NULL);
1945 if (p == NULL || *p == NUL)
1946 break;
1947 cmdmod.filter_regmatch.regprog =
1948 vim_regcomp(reg_pat, RE_MAGIC);
1949 if (cmdmod.filter_regmatch.regprog == NULL)
1950 break;
1951 ea.cmd = p;
1952 continue;
1953 }
1954
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 /* ":hide" and ":hide | cmd" are not modifiers */
1956 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1957 || *p == NUL || ends_excmd(*p))
1958 break;
1959 ea.cmd = p;
1960 cmdmod.hide = TRUE;
1961 continue;
1962
1963 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1964 {
1965 cmdmod.lockmarks = TRUE;
1966 continue;
1967 }
1968
1969 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1970 break;
1971#ifdef FEAT_WINDOWS
1972 cmdmod.split |= WSP_ABOVE;
1973#endif
1974 continue;
1975
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001976 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001977 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001978#ifdef FEAT_AUTOCMD
1979 if (cmdmod.save_ei == NULL)
1980 {
1981 /* Set 'eventignore' to "all". Restore the
1982 * existing option value later. */
1983 cmdmod.save_ei = vim_strsave(p_ei);
1984 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001985 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001986 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001987#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001988 continue;
1989 }
Bram Moolenaar3bcfca32016-07-30 19:39:29 +02001990 if (!checkforcmd(&ea.cmd, "noswapfile", 3))
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001991 break;
1992 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001993 continue;
1994
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1996 break;
1997#ifdef FEAT_WINDOWS
1998 cmdmod.split |= WSP_BELOW;
1999#endif
2000 continue;
2001
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002002 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
2003 {
2004#ifdef HAVE_SANDBOX
2005 if (!did_sandbox)
2006 ++sandbox;
2007 did_sandbox = TRUE;
2008#endif
2009 continue;
2010 }
2011 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002013 if (save_msg_silent == -1)
2014 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
2017 {
2018 /* ":silent!", but not "silent !cmd" */
2019 ea.cmd = skipwhite(ea.cmd + 1);
2020 ++emsg_silent;
2021 ++did_esilent;
2022 }
2023 continue;
2024
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002025 case 't': if (checkforcmd(&p, "tab", 3))
2026 {
2027#ifdef FEAT_WINDOWS
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02002028 long tabnr = get_address(&ea, &ea.cmd, ADDR_TABS,
Bram Moolenaarded27822017-01-02 14:27:34 +01002029 ea.skip, FALSE, 1);
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02002030 if (tabnr == MAXLNUM)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002031 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02002032 else
2033 {
2034 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2035 {
2036 errormsg = (char_u *)_(e_invrange);
2037 goto doend;
2038 }
2039 cmdmod.tab = tabnr + 1;
2040 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002041 ea.cmd = p;
2042#endif
2043 continue;
2044 }
2045 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 break;
2047#ifdef FEAT_WINDOWS
2048 cmdmod.split |= WSP_TOP;
2049#endif
2050 continue;
2051
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002052 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
2053 break;
2054 if (save_msg_silent == -1)
2055 save_msg_silent = msg_silent;
2056 msg_silent = 0;
2057 continue;
2058
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
2060 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002061#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 cmdmod.split |= WSP_VERT;
2063#endif
2064 continue;
2065 }
2066 if (!checkforcmd(&p, "verbose", 4))
2067 break;
2068 if (verbose_save < 0)
2069 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00002070 if (vim_isdigit(*ea.cmd))
2071 p_verbose = atoi((char *)ea.cmd);
2072 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 p_verbose = 1;
2074 ea.cmd = p;
2075 continue;
2076 }
2077 break;
2078 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002079 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080
2081#ifdef FEAT_EVAL
2082 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2083 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2084#else
2085 ea.skip = (if_level > 0);
2086#endif
2087
2088#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002089# ifdef FEAT_PROFILE
2090 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002091 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002092 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002093 if (getline_equal(fgetline, cookie, get_func_line))
2094 func_line_exec(getline_cookie(fgetline, cookie));
2095 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002096 script_line_exec();
2097 }
2098#endif
2099
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 /* May go to debug mode. If this happens and the ">quit" debug command is
2101 * used, throw an interrupt exception and skip the next command. */
2102 dbg_check_breakpoint(&ea);
2103 if (!ea.skip && got_int)
2104 {
2105 ea.skip = TRUE;
2106 (void)do_intthrow(cstack);
2107 }
2108#endif
2109
2110/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002111 * 3. Skip over the range to find the command. Let "p" point to after it.
2112 *
2113 * We need the command to know what kind of range it uses.
2114 */
2115 cmd = ea.cmd;
2116 ea.cmd = skip_range(ea.cmd, NULL);
2117 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2118 ea.cmd = skipwhite(ea.cmd + 1);
2119 p = find_command(&ea, NULL);
2120
2121/*
2122 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 *
2124 * where 'addr' is:
2125 *
2126 * % (entire file)
2127 * $ [+-NUM]
2128 * 'x [+-NUM] (where x denotes a currently defined mark)
2129 * . [+-NUM]
2130 * [+-NUM]..
2131 * NUM
2132 *
2133 * The ea.cmd pointer is updated to point to the first character following the
2134 * range spec. If an initial address is found, but no second, the upper bound
2135 * is equal to the lower.
2136 */
2137
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002138 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002139 if (!IS_USER_CMDIDX(ea.cmdidx))
2140 {
2141 if (ea.cmdidx != CMD_SIZE)
2142 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2143 else
2144 ea.addr_type = ADDR_LINES;
2145
2146#ifdef FEAT_WINDOWS
2147 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002148 if (ea.cmdidx == CMD_wincmd && p != NULL)
2149 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002150#endif
2151 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002152
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002154 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 for (;;)
2156 {
2157 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002158 switch (ea.addr_type)
2159 {
2160 case ADDR_LINES:
2161 /* default is current line number */
2162 ea.line2 = curwin->w_cursor.lnum;
2163 break;
2164 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002165 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002166 ea.line2 = lnum;
2167 break;
2168 case ADDR_ARGUMENTS:
2169 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002170 if (ea.line2 > ARGCOUNT)
2171 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002172 break;
2173 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002174 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002175 ea.line2 = curbuf->b_fnum;
2176 break;
2177 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002178 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002179 ea.line2 = lnum;
2180 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002181#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002182 case ADDR_QUICKFIX:
2183 ea.line2 = qf_get_cur_valid_idx(&ea);
2184 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002185#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002188 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip,
Bram Moolenaarded27822017-01-02 14:27:34 +01002189 ea.addr_count == 0, address_count++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 if (ea.cmd == NULL) /* error detected */
2191 goto doend;
2192 if (lnum == MAXLNUM)
2193 {
2194 if (*ea.cmd == '%') /* '%' - all lines */
2195 {
2196 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002197 switch (ea.addr_type)
2198 {
2199 case ADDR_LINES:
2200 ea.line1 = 1;
2201 ea.line2 = curbuf->b_ml.ml_line_count;
2202 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002203 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002204 {
2205 buf_T *buf = firstbuf;
2206
2207 while (buf->b_next != NULL
2208 && buf->b_ml.ml_mfp == NULL)
2209 buf = buf->b_next;
2210 ea.line1 = buf->b_fnum;
2211 buf = lastbuf;
2212 while (buf->b_prev != NULL
2213 && buf->b_ml.ml_mfp == NULL)
2214 buf = buf->b_prev;
2215 ea.line2 = buf->b_fnum;
2216 break;
2217 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002218 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002219 ea.line1 = firstbuf->b_fnum;
2220 ea.line2 = lastbuf->b_fnum;
2221 break;
2222 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002223 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002224 if (IS_USER_CMDIDX(ea.cmdidx))
2225 {
2226 ea.line1 = 1;
2227 ea.line2 = ea.addr_type == ADDR_WINDOWS
2228 ? LAST_WIN_NR : LAST_TAB_NR;
2229 }
2230 else
2231 {
2232 /* there is no Vim command which uses '%' and
2233 * ADDR_WINDOWS or ADDR_TABS */
2234 errormsg = (char_u *)_(e_invrange);
2235 goto doend;
2236 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002237 break;
2238 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002239 if (ARGCOUNT == 0)
2240 ea.line1 = ea.line2 = 0;
2241 else
2242 {
2243 ea.line1 = 1;
2244 ea.line2 = ARGCOUNT;
2245 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002246 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002247#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002248 case ADDR_QUICKFIX:
2249 ea.line1 = 1;
2250 ea.line2 = qf_get_size(&ea);
2251 if (ea.line2 == 0)
2252 ea.line2 = 1;
2253 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002254#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 ++ea.addr_count;
2257 }
2258 /* '*' - visual area */
2259 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2260 {
2261 pos_T *fp;
2262
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002263 if (ea.addr_type != ADDR_LINES)
2264 {
2265 errormsg = (char_u *)_(e_invrange);
2266 goto doend;
2267 }
2268
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 ++ea.cmd;
2270 if (!ea.skip)
2271 {
2272 fp = getmark('<', FALSE);
2273 if (check_mark(fp) == FAIL)
2274 goto doend;
2275 ea.line1 = fp->lnum;
2276 fp = getmark('>', FALSE);
2277 if (check_mark(fp) == FAIL)
2278 goto doend;
2279 ea.line2 = fp->lnum;
2280 ++ea.addr_count;
2281 }
2282 }
2283 }
2284 else
2285 ea.line2 = lnum;
2286 ea.addr_count++;
2287
2288 if (*ea.cmd == ';')
2289 {
2290 if (!ea.skip)
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002291 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 curwin->w_cursor.lnum = ea.line2;
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002293 /* don't leave the cursor on an illegal line */
2294 check_cursor_lnum();
2295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 }
2297 else if (*ea.cmd != ',')
2298 break;
2299 ++ea.cmd;
2300 }
2301
2302 /* One address given: set start and end lines */
2303 if (ea.addr_count == 1)
2304 {
2305 ea.line1 = ea.line2;
2306 /* ... but only implicit: really no address given */
2307 if (lnum == MAXLNUM)
2308 ea.addr_count = 0;
2309 }
2310
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002312 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 */
2314
2315 /*
2316 * Skip ':' and any white space
2317 */
2318 ea.cmd = skipwhite(ea.cmd);
2319 while (*ea.cmd == ':')
2320 ea.cmd = skipwhite(ea.cmd + 1);
2321
2322 /*
2323 * If we got a line, but no command, then go to the line.
2324 * If we find a '|' or '\n' we set ea.nextcmd.
2325 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002326 if (*ea.cmd == NUL || *ea.cmd == '"'
2327 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 {
2329 /*
2330 * strange vi behaviour:
2331 * ":3" jumps to line 3
2332 * ":3|..." prints line 3
2333 * ":|" prints current line
2334 */
2335 if (ea.skip) /* skip this if inside :if */
2336 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002337 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 {
2339 ea.cmdidx = CMD_print;
2340 ea.argt = RANGE+COUNT+TRLBAR;
2341 if ((errormsg = invalid_range(&ea)) == NULL)
2342 {
2343 correct_range(&ea);
2344 ex_print(&ea);
2345 }
2346 }
2347 else if (ea.addr_count != 0)
2348 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002349 if (ea.line2 > curbuf->b_ml.ml_line_count)
2350 {
2351 /* With '-' in 'cpoptions' a line number past the file is an
2352 * error, otherwise put it at the end of the file. */
2353 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2354 ea.line2 = -1;
2355 else
2356 ea.line2 = curbuf->b_ml.ml_line_count;
2357 }
2358
2359 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002360 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 else
2362 {
2363 if (ea.line2 == 0)
2364 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 else
2366 curwin->w_cursor.lnum = ea.line2;
2367 beginline(BL_SOL | BL_FIX);
2368 }
2369 }
2370 goto doend;
2371 }
2372
Bram Moolenaard5005162014-08-22 23:05:54 +02002373#ifdef FEAT_AUTOCMD
2374 /* If this looks like an undefined user command and there are CmdUndefined
2375 * autocommands defined, trigger the matching autocommands. */
2376 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2377 && ASCII_ISUPPER(*ea.cmd)
2378 && has_cmdundefined())
2379 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002380 int ret;
2381
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002382 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002383 while (ASCII_ISALNUM(*p))
2384 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002385 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002386 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2387 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002388 /* If the autocommands did something and didn't cause an error, try
2389 * finding the command again. */
Bram Moolenaareac784e2016-07-28 22:08:24 +02002390 p = (ret && !aborting()) ? find_command(&ea, NULL) : ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002391 }
2392#endif
2393
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394#ifdef FEAT_USR_CMDS
2395 if (p == NULL)
2396 {
2397 if (!ea.skip)
2398 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2399 goto doend;
2400 }
2401 /* Check for wrong commands. */
2402 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2403 {
2404 errormsg = uc_fun_cmd();
2405 goto doend;
2406 }
2407#endif
2408 if (ea.cmdidx == CMD_SIZE)
2409 {
2410 if (!ea.skip)
2411 {
2412 STRCPY(IObuff, _("E492: Not an editor command"));
2413 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002414 {
2415 /* If the modifier was parsed OK the error must be in the
2416 * following command */
2417 if (after_modifier != NULL)
2418 append_command(after_modifier);
2419 else
2420 append_command(*cmdlinep);
2421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002423 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 }
2425 goto doend;
2426 }
2427
Bram Moolenaar958636c2014-10-21 20:01:58 +02002428 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2429 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002430#ifdef HAVE_EX_SCRIPT_NI
2431 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2432#endif
2433 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434
2435#ifndef FEAT_EVAL
2436 /*
2437 * When the expression evaluation is disabled, recognize the ":if" and
2438 * ":endif" commands and ignore everything in between it.
2439 */
2440 if (ea.cmdidx == CMD_if)
2441 ++if_level;
2442 if (if_level)
2443 {
2444 if (ea.cmdidx == CMD_endif)
2445 --if_level;
2446 goto doend;
2447 }
2448
2449#endif
2450
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002451 /* forced commands */
2452 if (*p == '!' && ea.cmdidx != CMD_substitute
2453 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 {
2455 ++p;
2456 ea.forceit = TRUE;
2457 }
2458 else
2459 ea.forceit = FALSE;
2460
2461/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002462 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002464 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002465 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466
2467 if (!ea.skip)
2468 {
2469#ifdef HAVE_SANDBOX
2470 if (sandbox != 0 && !(ea.argt & SBOXOK))
2471 {
2472 /* Command not allowed in sandbox. */
2473 errormsg = (char_u *)_(e_sandbox);
2474 goto doend;
2475 }
2476#endif
2477 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2478 {
2479 /* Command not allowed in non-'modifiable' buffer */
2480 errormsg = (char_u *)_(e_modifiable);
2481 goto doend;
2482 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002483
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002484 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002485 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002487 /* Command not allowed when editing the command line. */
Bram Moolenaar5a497892016-09-03 16:29:04 +02002488 errormsg = get_text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 goto doend;
2490 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002491#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002492 /* Disallow editing another buffer when "curbuf_lock" is set.
2493 * Do allow ":edit" (check for argument later).
2494 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002495 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002496 && ea.cmdidx != CMD_edit
2497 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002498 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002499 && curbuf_locked())
2500 goto doend;
2501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502
2503 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2504 {
2505 /* no range allowed */
2506 errormsg = (char_u *)_(e_norange);
2507 goto doend;
2508 }
2509 }
2510
2511 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2512 {
2513 errormsg = (char_u *)_(e_nobang);
2514 goto doend;
2515 }
2516
2517 /*
2518 * Don't complain about the range if it is not used
2519 * (could happen if line_count is accidentally set to 0).
2520 */
2521 if (!ea.skip && !ni)
2522 {
2523 /*
2524 * If the range is backwards, ask for confirmation and, if given, swap
2525 * ea.line1 & ea.line2 so it's forwards again.
2526 * When global command is busy, don't ask, will fail below.
2527 */
2528 if (!global_busy && ea.line1 > ea.line2)
2529 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002530 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002532 if (sourcing || exmode_active)
2533 {
2534 errormsg = (char_u *)_("E493: Backwards range given");
2535 goto doend;
2536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 if (ask_yesno((char_u *)
2538 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002539 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 }
2541 lnum = ea.line1;
2542 ea.line1 = ea.line2;
2543 ea.line2 = lnum;
2544 }
2545 if ((errormsg = invalid_range(&ea)) != NULL)
2546 goto doend;
2547 }
2548
2549 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2550 ea.line2 = 1;
2551
2552 correct_range(&ea);
2553
2554#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002555 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2556 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 {
2558 /* Put the first line at the start of a closed fold, put the last line
2559 * at the end of a closed fold. */
2560 (void)hasFolding(ea.line1, &ea.line1, NULL);
2561 (void)hasFolding(ea.line2, NULL, &ea.line2);
2562 }
2563#endif
2564
2565#ifdef FEAT_QUICKFIX
2566 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002567 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 * option here, so things like % get expanded.
2569 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002570 p = replace_makeprg(&ea, p, cmdlinep);
2571 if (p == NULL)
2572 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573#endif
2574
2575 /*
2576 * Skip to start of argument.
2577 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2578 */
2579 if (ea.cmdidx == CMD_bang)
2580 ea.arg = p;
2581 else
2582 ea.arg = skipwhite(p);
2583
2584 /*
2585 * Check for "++opt=val" argument.
2586 * Must be first, allow ":w ++enc=utf8 !cmd"
2587 */
2588 if (ea.argt & ARGOPT)
2589 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2590 if (getargopt(&ea) == FAIL && !ni)
2591 {
2592 errormsg = (char_u *)_(e_invarg);
2593 goto doend;
2594 }
2595
2596 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2597 {
2598 if (*ea.arg == '>') /* append */
2599 {
2600 if (*++ea.arg != '>') /* typed wrong */
2601 {
2602 errormsg = (char_u *)_("E494: Use w or w>>");
2603 goto doend;
2604 }
2605 ea.arg = skipwhite(ea.arg + 1);
2606 ea.append = TRUE;
2607 }
2608 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2609 {
2610 ++ea.arg;
2611 ea.usefilter = TRUE;
2612 }
2613 }
2614
2615 if (ea.cmdidx == CMD_read)
2616 {
2617 if (ea.forceit)
2618 {
2619 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2620 ea.forceit = FALSE;
2621 }
2622 else if (*ea.arg == '!') /* :r !filter */
2623 {
2624 ++ea.arg;
2625 ea.usefilter = TRUE;
2626 }
2627 }
2628
2629 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2630 {
2631 ea.amount = 1;
2632 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2633 {
2634 ++ea.arg;
2635 ++ea.amount;
2636 }
2637 ea.arg = skipwhite(ea.arg);
2638 }
2639
2640 /*
2641 * Check for "+command" argument, before checking for next command.
2642 * Don't do this for ":read !cmd" and ":write !cmd".
2643 */
2644 if ((ea.argt & EDITCMD) && !ea.usefilter)
2645 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2646
2647 /*
2648 * Check for '|' to separate commands and '"' to start comments.
2649 * Don't do this for ":read !cmd" and ":write !cmd".
2650 */
2651 if ((ea.argt & TRLBAR) && !ea.usefilter)
2652 separate_nextcmd(&ea);
2653
2654 /*
2655 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002656 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2657 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002659 else if (ea.cmdidx == CMD_bang
2660 || ea.cmdidx == CMD_global
2661 || ea.cmdidx == CMD_vglobal
2662 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 {
2664 for (p = ea.arg; *p; ++p)
2665 {
2666 /* Remove one backslash before a newline, so that it's possible to
2667 * pass a newline to the shell and also a newline that is preceded
2668 * with a backslash. This makes it impossible to end a shell
2669 * command in a backslash, but that doesn't appear useful.
2670 * Halving the number of backslashes is incompatible with previous
2671 * versions. */
2672 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002673 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 else if (*p == '\n')
2675 {
2676 ea.nextcmd = p + 1;
2677 *p = NUL;
2678 break;
2679 }
2680 }
2681 }
2682
2683 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2684 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002685 buf_T *buf;
2686
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002688 switch (ea.addr_type)
2689 {
2690 case ADDR_LINES:
2691 ea.line2 = curbuf->b_ml.ml_line_count;
2692 break;
2693 case ADDR_LOADED_BUFFERS:
2694 buf = firstbuf;
2695 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2696 buf = buf->b_next;
2697 ea.line1 = buf->b_fnum;
2698 buf = lastbuf;
2699 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2700 buf = buf->b_prev;
2701 ea.line2 = buf->b_fnum;
2702 break;
2703 case ADDR_BUFFERS:
2704 ea.line1 = firstbuf->b_fnum;
2705 ea.line2 = lastbuf->b_fnum;
2706 break;
2707 case ADDR_WINDOWS:
2708 ea.line2 = LAST_WIN_NR;
2709 break;
2710 case ADDR_TABS:
2711 ea.line2 = LAST_TAB_NR;
2712 break;
2713 case ADDR_ARGUMENTS:
2714 if (ARGCOUNT == 0)
2715 ea.line1 = ea.line2 = 0;
2716 else
2717 ea.line2 = ARGCOUNT;
2718 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002719#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002720 case ADDR_QUICKFIX:
2721 ea.line2 = qf_get_size(&ea);
2722 if (ea.line2 == 0)
2723 ea.line2 = 1;
2724 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002725#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 }
2728
2729 /* accept numbered register only when no count allowed (:put) */
2730 if ( (ea.argt & REGSTR)
2731 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002732 /* Do not allow register = for user commands */
2733 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2735 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002736#ifndef FEAT_CLIPBOARD
2737 /* check these explicitly for a more specific error message */
2738 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002740 errormsg = (char_u *)_(e_invalidreg);
2741 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 }
2743#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002744 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2745 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002746 {
2747 ea.regname = *ea.arg++;
2748#ifdef FEAT_EVAL
2749 /* for '=' register: accept the rest of the line as an expression */
2750 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2751 {
2752 set_expr_line(vim_strsave(ea.arg));
2753 ea.arg += STRLEN(ea.arg);
2754 }
2755#endif
2756 ea.arg = skipwhite(ea.arg);
2757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 }
2759
2760 /*
2761 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2762 * count, it's a buffer name.
2763 */
2764 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2765 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2766 || vim_iswhite(*p)))
2767 {
2768 n = getdigits(&ea.arg);
2769 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002770 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 {
2772 errormsg = (char_u *)_(e_zerocount);
2773 goto doend;
2774 }
2775 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2776 {
2777 ea.line2 = n;
2778 if (ea.addr_count == 0)
2779 ea.addr_count = 1;
2780 }
2781 else
2782 {
2783 ea.line1 = ea.line2;
2784 ea.line2 += n - 1;
2785 ++ea.addr_count;
2786 /*
2787 * Be vi compatible: no error message for out of range.
2788 */
2789 if (ea.line2 > curbuf->b_ml.ml_line_count)
2790 ea.line2 = curbuf->b_ml.ml_line_count;
2791 }
2792 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002793
2794 /*
2795 * Check for flags: 'l', 'p' and '#'.
2796 */
2797 if (ea.argt & EXFLAGS)
2798 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002800 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002801 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 {
2803 errormsg = (char_u *)_(e_trailing);
2804 goto doend;
2805 }
2806
2807 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2808 {
2809 errormsg = (char_u *)_(e_argreq);
2810 goto doend;
2811 }
2812
2813#ifdef FEAT_EVAL
2814 /*
2815 * Skip the command when it's not going to be executed.
2816 * The commands like :if, :endif, etc. always need to be executed.
2817 * Also make an exception for commands that handle a trailing command
2818 * themselves.
2819 */
2820 if (ea.skip)
2821 {
2822 switch (ea.cmdidx)
2823 {
2824 /* commands that need evaluation */
2825 case CMD_while:
2826 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002827 case CMD_for:
2828 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 case CMD_if:
2830 case CMD_elseif:
2831 case CMD_else:
2832 case CMD_endif:
2833 case CMD_try:
2834 case CMD_catch:
2835 case CMD_finally:
2836 case CMD_endtry:
2837 case CMD_function:
2838 break;
2839
2840 /* Commands that handle '|' themselves. Check: A command should
2841 * either have the TRLBAR flag, appear in this list or appear in
2842 * the list at ":help :bar". */
2843 case CMD_aboveleft:
2844 case CMD_and:
2845 case CMD_belowright:
2846 case CMD_botright:
2847 case CMD_browse:
2848 case CMD_call:
2849 case CMD_confirm:
2850 case CMD_delfunction:
2851 case CMD_djump:
2852 case CMD_dlist:
2853 case CMD_dsearch:
2854 case CMD_dsplit:
2855 case CMD_echo:
2856 case CMD_echoerr:
2857 case CMD_echomsg:
2858 case CMD_echon:
2859 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002860 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 case CMD_help:
2862 case CMD_hide:
2863 case CMD_ijump:
2864 case CMD_ilist:
2865 case CMD_isearch:
2866 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002867 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 case CMD_keepjumps:
2869 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002870 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 case CMD_leftabove:
2872 case CMD_let:
2873 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002874 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002876 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002877 case CMD_noautocmd:
2878 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 case CMD_perl:
2880 case CMD_psearch:
2881 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002882 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002883 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 case CMD_return:
2885 case CMD_rightbelow:
2886 case CMD_ruby:
2887 case CMD_silent:
2888 case CMD_smagic:
2889 case CMD_snomagic:
2890 case CMD_substitute:
2891 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002892 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 case CMD_tcl:
2894 case CMD_throw:
2895 case CMD_tilde:
2896 case CMD_topleft:
2897 case CMD_unlet:
2898 case CMD_verbose:
2899 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002900 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 break;
2902
2903 default: goto doend;
2904 }
2905 }
2906#endif
2907
2908 if (ea.argt & XFILE)
2909 {
2910 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2911 goto doend;
2912 }
2913
2914#ifdef FEAT_LISTCMDS
2915 /*
2916 * Accept buffer name. Cannot be used at the same time with a buffer
2917 * number. Don't do this for a user command.
2918 */
2919 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002920 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 {
2922 /*
2923 * :bdelete, :bwipeout and :bunload take several arguments, separated
2924 * by spaces: find next space (skipping over escaped characters).
2925 * The others take one argument: ignore trailing spaces.
2926 */
2927 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2928 || ea.cmdidx == CMD_bunload)
2929 p = skiptowhite_esc(ea.arg);
2930 else
2931 {
2932 p = ea.arg + STRLEN(ea.arg);
2933 while (p > ea.arg && vim_iswhite(p[-1]))
2934 --p;
2935 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002936 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2937 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 if (ea.line2 < 0) /* failed */
2939 goto doend;
2940 ea.addr_count = 1;
2941 ea.arg = skipwhite(p);
2942 }
2943#endif
2944
2945/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002946 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 *
2948 * The "ea" structure holds the arguments that can be used.
2949 */
2950 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002951 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 ea.cookie = cookie;
2953#ifdef FEAT_EVAL
2954 ea.cstack = cstack;
2955#endif
2956
2957#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002958 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 {
2960 /*
2961 * Execute a user-defined command.
2962 */
2963 do_ucmd(&ea);
2964 }
2965 else
2966#endif
2967 {
2968 /*
2969 * Call the function to execute the command.
2970 */
2971 ea.errmsg = NULL;
2972 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2973 if (ea.errmsg != NULL)
2974 errormsg = (char_u *)_(ea.errmsg);
2975 }
2976
2977#ifdef FEAT_EVAL
2978 /*
2979 * If the command just executed called do_cmdline(), any throw or ":return"
2980 * or ":finish" encountered there must also check the cstack of the still
2981 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2982 * exception, or reanimate a returned function or finished script file and
2983 * return or finish it again.
2984 */
2985 if (need_rethrow)
2986 do_throw(cstack);
2987 else if (check_cstack)
2988 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002989 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002991 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 && current_func_returned())
2993 do_return(&ea, TRUE, FALSE, NULL);
2994 }
2995 need_rethrow = check_cstack = FALSE;
2996#endif
2997
2998doend:
2999 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
3000 curwin->w_cursor.lnum = 1;
3001
3002 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
3003 {
3004 if (sourcing)
3005 {
3006 if (errormsg != IObuff)
3007 {
3008 STRCPY(IObuff, errormsg);
3009 errormsg = IObuff;
3010 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003011 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 }
3013 emsg(errormsg);
3014 }
3015#ifdef FEAT_EVAL
3016 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02003017 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
3018 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019#endif
3020
3021 if (verbose_save >= 0)
3022 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00003023#ifdef FEAT_AUTOCMD
3024 if (cmdmod.save_ei != NULL)
3025 {
3026 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003027 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
3028 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00003029 free_string_option(cmdmod.save_ei);
3030 }
3031#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003032 if (cmdmod.filter_regmatch.regprog != NULL)
3033 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034
3035 cmdmod = save_cmdmod;
3036
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003037 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 {
3039 /* messages could be enabled for a serious error, need to check if the
3040 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00003041 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003042 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 emsg_silent -= did_esilent;
3044 if (emsg_silent < 0)
3045 emsg_silent = 0;
3046 /* Restore msg_scroll, it's set by file I/O commands, even when no
3047 * message is actually displayed. */
3048 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003049
3050 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
3051 * somewhere in the line. Put it back in the first column. */
3052 if (redirecting())
3053 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 }
3055
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003056#ifdef HAVE_SANDBOX
3057 if (did_sandbox)
3058 --sandbox;
3059#endif
3060
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3062 ea.nextcmd = NULL;
3063
3064#ifdef FEAT_EVAL
3065 --ex_nesting_level;
3066#endif
3067
3068 return ea.nextcmd;
3069}
3070#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003071 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072#endif
3073
3074/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003075 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 * If there is a match advance "pp" to the argument and return TRUE.
3077 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003078 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003079checkforcmd(
3080 char_u **pp, /* start of command */
3081 char *cmd, /* name of command */
3082 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083{
3084 int i;
3085
3086 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003087 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 break;
3089 if (i >= len && !isalpha((*pp)[i]))
3090 {
3091 *pp = skipwhite(*pp + i);
3092 return TRUE;
3093 }
3094 return FALSE;
3095}
3096
3097/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003098 * Append "cmd" to the error message in IObuff.
3099 * Takes care of limiting the length and handling 0xa0, which would be
3100 * invisible otherwise.
3101 */
3102 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003103append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003104{
3105 char_u *s = cmd;
3106 char_u *d;
3107
3108 STRCAT(IObuff, ": ");
3109 d = IObuff + STRLEN(IObuff);
3110 while (*s != NUL && d - IObuff < IOSIZE - 7)
3111 {
3112 if (
3113#ifdef FEAT_MBYTE
3114 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3115#endif
3116 *s == 0xa0)
3117 {
3118 s +=
3119#ifdef FEAT_MBYTE
3120 enc_utf8 ? 2 :
3121#endif
3122 1;
3123 STRCPY(d, "<a0>");
3124 d += 4;
3125 }
3126 else
3127 MB_COPY_CHAR(s, d);
3128 }
3129 *d = NUL;
3130}
3131
3132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003134 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003136 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 * Returns NULL for an ambiguous user command.
3138 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003140find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141{
3142 int len;
3143 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003144 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145
3146 /*
3147 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003148 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 * - the 'k' command can directly be followed by any character.
3150 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003151 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003153 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 */
3155 p = eap->cmd;
3156 if (*p == 'k')
3157 {
3158 eap->cmdidx = CMD_k;
3159 ++p;
3160 }
3161 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003162 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3163 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 || p[1] == 'g'
3165 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3166 || p[1] == 'I'
3167 || (p[1] == 'r' && p[2] != 'e')))
3168 {
3169 eap->cmdidx = CMD_substitute;
3170 ++p;
3171 }
3172 else
3173 {
3174 while (ASCII_ISALPHA(*p))
3175 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003176 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003177 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003178 while (ASCII_ISALNUM(*p))
3179 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003180
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 /* check for non-alpha command */
3182 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3183 ++p;
3184 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003185 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3186 {
3187 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3188 * :delete with the 'l' flag. Same for 'p'. */
3189 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003190 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003191 break;
3192 if (i == len - 1)
3193 {
3194 --len;
3195 if (p[-1] == 'l')
3196 eap->flags |= EXFLAG_LIST;
3197 else
3198 eap->flags |= EXFLAG_PRINT;
3199 }
3200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201
3202 if (ASCII_ISLOWER(*eap->cmd))
3203 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
3204 else
3205 eap->cmdidx = cmdidxs[26];
3206
3207 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3208 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3209 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3210 (size_t)len) == 0)
3211 {
3212#ifdef FEAT_EVAL
3213 if (full != NULL
3214 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3215 *full = TRUE;
3216#endif
3217 break;
3218 }
3219
3220#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003221 /* Look for a user defined command as a last resort. Let ":Print" be
3222 * overruled by a user defined command. */
3223 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3224 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003226 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 while (ASCII_ISALNUM(*p))
3228 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003229 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 }
3231#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003232 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 eap->cmdidx = CMD_SIZE;
3234 }
3235
3236 return p;
3237}
3238
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003239#ifdef FEAT_USR_CMDS
3240/*
3241 * Search for a user command that matches "eap->cmd".
3242 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3243 * Return a pointer to just after the command.
3244 * Return NULL if there is no matching command.
3245 */
3246 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003247find_ucmd(
3248 exarg_T *eap,
3249 char_u *p, /* end of the command (possibly including count) */
3250 int *full, /* set to TRUE for a full match */
3251 expand_T *xp, /* used for completion, NULL otherwise */
3252 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003253{
3254 int len = (int)(p - eap->cmd);
3255 int j, k, matchlen = 0;
3256 ucmd_T *uc;
3257 int found = FALSE;
3258 int possible = FALSE;
3259 char_u *cp, *np; /* Point into typed cmd and test name */
3260 garray_T *gap;
3261 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3262 only full match global is accepted. */
3263
3264 /*
3265 * Look for buffer-local user commands first, then global ones.
3266 */
3267 gap = &curbuf->b_ucmds;
3268 for (;;)
3269 {
3270 for (j = 0; j < gap->ga_len; ++j)
3271 {
3272 uc = USER_CMD_GA(gap, j);
3273 cp = eap->cmd;
3274 np = uc->uc_name;
3275 k = 0;
3276 while (k < len && *np != NUL && *cp++ == *np++)
3277 k++;
3278 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3279 {
3280 /* If finding a second match, the command is ambiguous. But
3281 * not if a buffer-local command wasn't a full match and a
3282 * global command is a full match. */
3283 if (k == len && found && *np != NUL)
3284 {
3285 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003286 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003287 amb_local = TRUE;
3288 }
3289
3290 if (!found || (k == len && *np == NUL))
3291 {
3292 /* If we matched up to a digit, then there could
3293 * be another command including the digit that we
3294 * should use instead.
3295 */
3296 if (k == len)
3297 found = TRUE;
3298 else
3299 possible = TRUE;
3300
3301 if (gap == &ucmds)
3302 eap->cmdidx = CMD_USER;
3303 else
3304 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003305 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003306 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003307 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003308
3309# ifdef FEAT_CMDL_COMPL
3310 if (compl != NULL)
3311 *compl = uc->uc_compl;
3312# ifdef FEAT_EVAL
3313 if (xp != NULL)
3314 {
3315 xp->xp_arg = uc->uc_compl_arg;
3316 xp->xp_scriptID = uc->uc_scriptID;
3317 }
3318# endif
3319# endif
3320 /* Do not search for further abbreviations
3321 * if this is an exact match. */
3322 matchlen = k;
3323 if (k == len && *np == NUL)
3324 {
3325 if (full != NULL)
3326 *full = TRUE;
3327 amb_local = FALSE;
3328 break;
3329 }
3330 }
3331 }
3332 }
3333
3334 /* Stop if we found a full match or searched all. */
3335 if (j < gap->ga_len || gap == &ucmds)
3336 break;
3337 gap = &ucmds;
3338 }
3339
3340 /* Only found ambiguous matches. */
3341 if (amb_local)
3342 {
3343 if (xp != NULL)
3344 xp->xp_context = EXPAND_UNSUCCESSFUL;
3345 return NULL;
3346 }
3347
3348 /* The match we found may be followed immediately by a number. Move "p"
3349 * back to point to it. */
3350 if (found || possible)
3351 return p + (matchlen - len);
3352 return p;
3353}
3354#endif
3355
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003357static struct cmdmod
3358{
3359 char *name;
3360 int minlen;
3361 int has_count; /* :123verbose :3tab */
3362} cmdmods[] = {
3363 {"aboveleft", 3, FALSE},
3364 {"belowright", 3, FALSE},
3365 {"botright", 2, FALSE},
3366 {"browse", 3, FALSE},
3367 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003368 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003369 {"hide", 3, FALSE},
3370 {"keepalt", 5, FALSE},
3371 {"keepjumps", 5, FALSE},
3372 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003373 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003374 {"leftabove", 5, FALSE},
3375 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003376 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003377 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003378 {"rightbelow", 6, FALSE},
3379 {"sandbox", 3, FALSE},
3380 {"silent", 3, FALSE},
3381 {"tab", 3, TRUE},
3382 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003383 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003384 {"verbose", 4, TRUE},
3385 {"vertical", 4, FALSE},
3386};
3387
3388/*
3389 * Return length of a command modifier (including optional count).
3390 * Return zero when it's not a modifier.
3391 */
3392 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003393modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003394{
3395 int i, j;
3396 char_u *p = cmd;
3397
3398 if (VIM_ISDIGIT(*cmd))
3399 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003400 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003401 {
3402 for (j = 0; p[j] != NUL; ++j)
3403 if (p[j] != cmdmods[i].name[j])
3404 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003405 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003406 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003407 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003408 }
3409 return 0;
3410}
3411
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412/*
3413 * Return > 0 if an Ex command "name" exists.
3414 * Return 2 if there is an exact match.
3415 * Return 3 if there is an ambiguous match.
3416 */
3417 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003418cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419{
3420 exarg_T ea;
3421 int full = FALSE;
3422 int i;
3423 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003424 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425
3426 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003427 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 {
3429 for (j = 0; name[j] != NUL; ++j)
3430 if (name[j] != cmdmods[i].name[j])
3431 break;
3432 if (name[j] == NUL && j >= cmdmods[i].minlen)
3433 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3434 }
3435
Bram Moolenaara9587612006-05-04 21:47:50 +00003436 /* Check built-in commands and user defined commands.
3437 * For ":2match" and ":3match" we need to skip the number. */
3438 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003440 p = find_command(&ea, &full);
3441 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003443 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3444 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003445 if (*skipwhite(p) != NUL)
3446 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3448}
3449#endif
3450
3451/*
3452 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3453 * we don't need/want deleted. Maybe this could be done better if we didn't
3454 * repeat all this stuff. The only problem is that they may not stay
3455 * perfectly compatible with each other, but then the command line syntax
3456 * probably won't change that much -- webb.
3457 */
3458 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003459set_one_cmd_context(
3460 expand_T *xp,
3461 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462{
3463 char_u *p;
3464 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003465 int len = 0;
3466 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3468 int compl = EXPAND_NOTHING;
3469#endif
3470#ifdef FEAT_CMDL_COMPL
3471 int delim;
3472#endif
3473 int forceit = FALSE;
3474 int usefilter = FALSE; /* filter instead of file name */
3475
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003476 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 xp->xp_pattern = buff;
3478 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003479 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480
3481/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003482 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 */
3484 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3485 ;
3486 xp->xp_pattern = cmd;
3487
3488 if (*cmd == NUL)
3489 return NULL;
3490 if (*cmd == '"') /* ignore comment lines */
3491 {
3492 xp->xp_context = EXPAND_NOTHING;
3493 return NULL;
3494 }
3495
3496/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003497 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 */
3499 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 xp->xp_pattern = cmd;
3501 if (*cmd == NUL)
3502 return NULL;
3503 if (*cmd == '"')
3504 {
3505 xp->xp_context = EXPAND_NOTHING;
3506 return NULL;
3507 }
3508
3509 if (*cmd == '|' || *cmd == '\n')
3510 return cmd + 1; /* There's another command */
3511
3512 /*
3513 * Isolate the command and search for it in the command table.
3514 * Exceptions:
3515 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003516 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3518 */
3519 if (*cmd == 'k' && cmd[1] != 'e')
3520 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003521 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 p = cmd + 1;
3523 }
3524 else
3525 {
3526 p = cmd;
3527 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3528 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003529 /* a user command may contain digits */
3530 if (ASCII_ISUPPER(cmd[0]))
3531 while (ASCII_ISALNUM(*p) || *p == '*')
3532 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003533 /* for python 3.x: ":py3*" commands completion */
3534 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003535 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003536 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003537 while (ASCII_ISALPHA(*p) || *p == '*')
3538 ++p;
3539 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003540 /* check for non-alpha command */
3541 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3542 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003543 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003545 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 {
3547 xp->xp_context = EXPAND_UNSUCCESSFUL;
3548 return NULL;
3549 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003550 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003551 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3552 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3553 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 break;
3555
3556#ifdef FEAT_USR_CMDS
3557 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3559 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560#endif
3561 }
3562
3563 /*
3564 * If the cursor is touching the command, and it ends in an alpha-numeric
3565 * character, complete the command name.
3566 */
3567 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3568 return NULL;
3569
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003570 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
3572 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3573 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003574 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 p = cmd + 1;
3576 }
3577#ifdef FEAT_USR_CMDS
3578 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3579 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003580 ea.cmd = cmd;
3581 p = find_ucmd(&ea, p, NULL, xp,
3582# if defined(FEAT_CMDL_COMPL)
3583 &compl
3584# else
3585 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003587 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003588 if (p == NULL)
3589 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 }
3591#endif
3592 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003593 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 {
3595 /* Not still touching the command and it was an illegal one */
3596 xp->xp_context = EXPAND_UNSUCCESSFUL;
3597 return NULL;
3598 }
3599
3600 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3601
3602 if (*p == '!') /* forced commands */
3603 {
3604 forceit = TRUE;
3605 ++p;
3606 }
3607
3608/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003609 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003611 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003612 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613
3614 arg = skipwhite(p);
3615
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003616 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 {
3618 if (*arg == '>') /* append */
3619 {
3620 if (*++arg == '>')
3621 ++arg;
3622 arg = skipwhite(arg);
3623 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003624 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 {
3626 ++arg;
3627 usefilter = TRUE;
3628 }
3629 }
3630
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003631 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 {
3633 usefilter = forceit; /* :r! filter if forced */
3634 if (*arg == '!') /* :r !filter */
3635 {
3636 ++arg;
3637 usefilter = TRUE;
3638 }
3639 }
3640
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003641 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 {
3643 while (*arg == *cmd) /* allow any number of '>' or '<' */
3644 ++arg;
3645 arg = skipwhite(arg);
3646 }
3647
3648 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003649 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 {
3651 /* Check if we're in the +command */
3652 p = arg + 1;
3653 arg = skip_cmd_arg(arg, FALSE);
3654
3655 /* Still touching the command after '+'? */
3656 if (*arg == NUL)
3657 return p;
3658
3659 /* Skip space(s) after +command to get to the real argument */
3660 arg = skipwhite(arg);
3661 }
3662
3663 /*
3664 * Check for '|' to separate commands and '"' to start comments.
3665 * Don't do this for ":read !cmd" and ":write !cmd".
3666 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003667 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 {
3669 p = arg;
3670 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003671 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 p += 2;
3673 while (*p)
3674 {
3675 if (*p == Ctrl_V)
3676 {
3677 if (p[1] != NUL)
3678 ++p;
3679 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003680 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 || *p == '|' || *p == '\n')
3682 {
3683 if (*(p - 1) != '\\')
3684 {
3685 if (*p == '|' || *p == '\n')
3686 return p + 1;
3687 return NULL; /* It's a comment */
3688 }
3689 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003690 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 }
3692 }
3693
3694 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003695 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 vim_strchr((char_u *)"|\"", *arg) == NULL)
3697 return NULL;
3698
3699 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003700 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003702 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003703 while (*p && p < buff + len)
3704 {
3705 if (*p == ' ' || *p == TAB)
3706 {
3707 /* argument starts after a space */
3708 xp->xp_pattern = ++p;
3709 }
3710 else
3711 {
3712 if (*p == '\\' && *(p + 1) != NUL)
3713 ++p; /* skip over escaped character */
3714 mb_ptr_adv(p);
3715 }
3716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003718 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003720 int c;
3721 int in_quote = FALSE;
3722 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723
3724 /*
3725 * Allow spaces within back-quotes to count as part of the argument
3726 * being expanded.
3727 */
3728 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003729 p = xp->xp_pattern;
3730 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003732#ifdef FEAT_MBYTE
3733 if (has_mbyte)
3734 c = mb_ptr2char(p);
3735 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003737 c = *p;
3738 if (c == '\\' && p[1] != NUL)
3739 ++p;
3740 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 {
3742 if (!in_quote)
3743 {
3744 xp->xp_pattern = p;
3745 bow = p + 1;
3746 }
3747 in_quote = !in_quote;
3748 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003749 /* An argument can contain just about everything, except
3750 * characters that end the command and white space. */
3751 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003752#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003753 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003754#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003755 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003756 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003757 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003758 while (*p != NUL)
3759 {
3760#ifdef FEAT_MBYTE
3761 if (has_mbyte)
3762 c = mb_ptr2char(p);
3763 else
3764#endif
3765 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003766 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003767 break;
3768#ifdef FEAT_MBYTE
3769 if (has_mbyte)
3770 len = (*mb_ptr2len)(p);
3771 else
3772#endif
3773 len = 1;
3774 mb_ptr_adv(p);
3775 }
3776 if (in_quote)
3777 bow = p;
3778 else
3779 xp->xp_pattern = p;
3780 p -= len;
3781 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003782 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 }
3784
3785 /*
3786 * If we are still inside the quotes, and we passed a space, just
3787 * expand from there.
3788 */
3789 if (bow != NULL && in_quote)
3790 xp->xp_pattern = bow;
3791 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003792
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003793 /* For a shell command more chars need to be escaped. */
3794 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003795 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003796#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003797 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003798#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003799 /* When still after the command name expand executables. */
3800 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003801 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803
3804 /* Check for environment variable */
3805 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003806#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 || *xp->xp_pattern == '%'
3808#endif
3809 )
3810 {
3811 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3812 if (!vim_isIDc(*p))
3813 break;
3814 if (*p == NUL)
3815 {
3816 xp->xp_context = EXPAND_ENV_VARS;
3817 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003818#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3819 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003820 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003821 compl = EXPAND_ENV_VARS;
3822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 }
3824 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003825#if defined(FEAT_CMDL_COMPL)
3826 /* Check for user names */
3827 if (*xp->xp_pattern == '~')
3828 {
3829 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3830 ;
3831 /* Complete ~user only if it partially matches a user name.
3832 * A full match ~user<Tab> will be replaced by user's home
3833 * directory i.e. something like ~user<Tab> -> /home/user/ */
3834 if (*p == NUL && p > xp->xp_pattern + 1
3835 && match_user(xp->xp_pattern + 1) == 1)
3836 {
3837 xp->xp_context = EXPAND_USER;
3838 ++xp->xp_pattern;
3839 }
3840 }
3841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 }
3843
3844/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003845 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003847 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003849 case CMD_find:
3850 case CMD_sfind:
3851 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003852 if (xp->xp_context == EXPAND_FILES)
3853 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003854 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 case CMD_cd:
3856 case CMD_chdir:
3857 case CMD_lcd:
3858 case CMD_lchdir:
3859 if (xp->xp_context == EXPAND_FILES)
3860 xp->xp_context = EXPAND_DIRECTORIES;
3861 break;
3862 case CMD_help:
3863 xp->xp_context = EXPAND_HELP;
3864 xp->xp_pattern = arg;
3865 break;
3866
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003867 /* Command modifiers: return the argument.
3868 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003870 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 case CMD_belowright:
3872 case CMD_botright:
3873 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003874 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003875 case CMD_cdo:
3876 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003878 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 case CMD_folddoclosed:
3880 case CMD_folddoopen:
3881 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003882 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 case CMD_keepjumps:
3884 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003885 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003886 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003888 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003890 case CMD_noautocmd:
3891 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003893 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003895 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003896 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 case CMD_topleft:
3898 case CMD_verbose:
3899 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003900 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 return arg;
3902
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003903 case CMD_filter:
3904 if (*arg != NUL)
3905 arg = skip_vimgrep_pat(arg, NULL, NULL);
3906 if (arg == NULL || *arg == NUL)
3907 {
3908 xp->xp_context = EXPAND_NOTHING;
3909 return NULL;
3910 }
3911 return skipwhite(arg);
3912
Bram Moolenaar4f688582007-07-24 12:34:30 +00003913#ifdef FEAT_CMDL_COMPL
3914# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 case CMD_match:
3916 if (*arg == NUL || !ends_excmd(*arg))
3917 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003918 /* also complete "None" */
3919 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 arg = skipwhite(skiptowhite(arg));
3921 if (*arg != NUL)
3922 {
3923 xp->xp_context = EXPAND_NOTHING;
3924 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3925 }
3926 }
3927 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003928# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930/*
3931 * All completion for the +cmdline_compl feature goes here.
3932 */
3933
3934# ifdef FEAT_USR_CMDS
3935 case CMD_command:
3936 /* Check for attributes */
3937 while (*arg == '-')
3938 {
3939 arg++; /* Skip "-" */
3940 p = skiptowhite(arg);
3941 if (*p == NUL)
3942 {
3943 /* Cursor is still in the attribute */
3944 p = vim_strchr(arg, '=');
3945 if (p == NULL)
3946 {
3947 /* No "=", so complete attribute names */
3948 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3949 xp->xp_pattern = arg;
3950 return NULL;
3951 }
3952
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003953 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 * their arguments as well.
3955 */
3956 if (STRNICMP(arg, "complete", p - arg) == 0)
3957 {
3958 xp->xp_context = EXPAND_USER_COMPLETE;
3959 xp->xp_pattern = p + 1;
3960 return NULL;
3961 }
3962 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3963 {
3964 xp->xp_context = EXPAND_USER_NARGS;
3965 xp->xp_pattern = p + 1;
3966 return NULL;
3967 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003968 else if (STRNICMP(arg, "addr", p - arg) == 0)
3969 {
3970 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3971 xp->xp_pattern = p + 1;
3972 return NULL;
3973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 return NULL;
3975 }
3976 arg = skipwhite(p);
3977 }
3978
3979 /* After the attributes comes the new command name */
3980 p = skiptowhite(arg);
3981 if (*p == NUL)
3982 {
3983 xp->xp_context = EXPAND_USER_COMMANDS;
3984 xp->xp_pattern = arg;
3985 break;
3986 }
3987
3988 /* And finally comes a normal command */
3989 return skipwhite(p);
3990
3991 case CMD_delcommand:
3992 xp->xp_context = EXPAND_USER_COMMANDS;
3993 xp->xp_pattern = arg;
3994 break;
3995# endif
3996
3997 case CMD_global:
3998 case CMD_vglobal:
3999 delim = *arg; /* get the delimiter */
4000 if (delim)
4001 ++arg; /* skip delimiter if there is one */
4002
4003 while (arg[0] != NUL && arg[0] != delim)
4004 {
4005 if (arg[0] == '\\' && arg[1] != NUL)
4006 ++arg;
4007 ++arg;
4008 }
4009 if (arg[0] != NUL)
4010 return arg + 1;
4011 break;
4012 case CMD_and:
4013 case CMD_substitute:
4014 delim = *arg;
4015 if (delim)
4016 {
4017 /* skip "from" part */
4018 ++arg;
4019 arg = skip_regexp(arg, delim, p_magic, NULL);
4020 }
4021 /* skip "to" part */
4022 while (arg[0] != NUL && arg[0] != delim)
4023 {
4024 if (arg[0] == '\\' && arg[1] != NUL)
4025 ++arg;
4026 ++arg;
4027 }
4028 if (arg[0] != NUL) /* skip delimiter */
4029 ++arg;
4030 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
4031 ++arg;
4032 if (arg[0] != NUL)
4033 return arg;
4034 break;
4035 case CMD_isearch:
4036 case CMD_dsearch:
4037 case CMD_ilist:
4038 case CMD_dlist:
4039 case CMD_ijump:
4040 case CMD_psearch:
4041 case CMD_djump:
4042 case CMD_isplit:
4043 case CMD_dsplit:
4044 arg = skipwhite(skipdigits(arg)); /* skip count */
4045 if (*arg == '/') /* Match regexp, not just whole words */
4046 {
4047 for (++arg; *arg && *arg != '/'; arg++)
4048 if (*arg == '\\' && arg[1] != NUL)
4049 arg++;
4050 if (*arg)
4051 {
4052 arg = skipwhite(arg + 1);
4053
4054 /* Check for trailing illegal characters */
4055 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4056 xp->xp_context = EXPAND_NOTHING;
4057 else
4058 return arg;
4059 }
4060 }
4061 break;
4062#ifdef FEAT_AUTOCMD
4063 case CMD_autocmd:
4064 return set_context_in_autocmd(xp, arg, FALSE);
4065
4066 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004067 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 return set_context_in_autocmd(xp, arg, TRUE);
4069#endif
4070 case CMD_set:
4071 set_context_in_set_cmd(xp, arg, 0);
4072 break;
4073 case CMD_setglobal:
4074 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4075 break;
4076 case CMD_setlocal:
4077 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4078 break;
4079 case CMD_tag:
4080 case CMD_stag:
4081 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004082 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 case CMD_tselect:
4084 case CMD_stselect:
4085 case CMD_ptselect:
4086 case CMD_tjump:
4087 case CMD_stjump:
4088 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004089 if (*p_wop != NUL)
4090 xp->xp_context = EXPAND_TAGS_LISTFILES;
4091 else
4092 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 xp->xp_pattern = arg;
4094 break;
4095 case CMD_augroup:
4096 xp->xp_context = EXPAND_AUGROUP;
4097 xp->xp_pattern = arg;
4098 break;
4099#ifdef FEAT_SYN_HL
4100 case CMD_syntax:
4101 set_context_in_syntax_cmd(xp, arg);
4102 break;
4103#endif
4104#ifdef FEAT_EVAL
4105 case CMD_let:
4106 case CMD_if:
4107 case CMD_elseif:
4108 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004109 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 case CMD_echo:
4111 case CMD_echon:
4112 case CMD_execute:
4113 case CMD_echomsg:
4114 case CMD_echoerr:
4115 case CMD_call:
4116 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004117 case CMD_cexpr:
4118 case CMD_caddexpr:
4119 case CMD_cgetexpr:
4120 case CMD_lexpr:
4121 case CMD_laddexpr:
4122 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004123 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 break;
4125
4126 case CMD_unlet:
4127 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4128 arg = xp->xp_pattern + 1;
4129 xp->xp_context = EXPAND_USER_VARS;
4130 xp->xp_pattern = arg;
4131 break;
4132
4133 case CMD_function:
4134 case CMD_delfunction:
4135 xp->xp_context = EXPAND_USER_FUNC;
4136 xp->xp_pattern = arg;
4137 break;
4138
4139 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004140 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 break;
4142#endif
4143 case CMD_highlight:
4144 set_context_in_highlight_cmd(xp, arg);
4145 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004146#ifdef FEAT_CSCOPE
4147 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004148 case CMD_lcscope:
4149 case CMD_scscope:
4150 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004151 break;
4152#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004153#ifdef FEAT_SIGNS
4154 case CMD_sign:
4155 set_context_in_sign_cmd(xp, arg);
4156 break;
4157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158#ifdef FEAT_LISTCMDS
4159 case CMD_bdelete:
4160 case CMD_bwipeout:
4161 case CMD_bunload:
4162 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4163 arg = xp->xp_pattern + 1;
4164 /*FALLTHROUGH*/
4165 case CMD_buffer:
4166 case CMD_sbuffer:
4167 case CMD_checktime:
4168 xp->xp_context = EXPAND_BUFFERS;
4169 xp->xp_pattern = arg;
4170 break;
4171#endif
4172#ifdef FEAT_USR_CMDS
4173 case CMD_USER:
4174 case CMD_USER_BUF:
4175 if (compl != EXPAND_NOTHING)
4176 {
4177 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004178 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 {
4180# ifdef FEAT_MENU
4181 if (compl == EXPAND_MENUS)
4182 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4183# endif
4184 if (compl == EXPAND_COMMANDS)
4185 return arg;
4186 if (compl == EXPAND_MAPPINGS)
4187 return set_context_in_map_cmd(xp, (char_u *)"map",
4188 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004189 /* Find start of last argument. */
4190 p = arg;
4191 while (*p)
4192 {
4193 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004194 /* argument starts after a space */
4195 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004196 else if (*p == '\\' && *(p + 1) != NUL)
4197 ++p; /* skip over escaped character */
4198 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 xp->xp_pattern = arg;
4201 }
4202 xp->xp_context = compl;
4203 }
4204 break;
4205#endif
4206 case CMD_map: case CMD_noremap:
4207 case CMD_nmap: case CMD_nnoremap:
4208 case CMD_vmap: case CMD_vnoremap:
4209 case CMD_omap: case CMD_onoremap:
4210 case CMD_imap: case CMD_inoremap:
4211 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004212 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004213 case CMD_smap: case CMD_snoremap:
4214 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004216 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 case CMD_unmap:
4218 case CMD_nunmap:
4219 case CMD_vunmap:
4220 case CMD_ounmap:
4221 case CMD_iunmap:
4222 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004223 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004224 case CMD_sunmap:
4225 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004227 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 case CMD_abbreviate: case CMD_noreabbrev:
4229 case CMD_cabbrev: case CMD_cnoreabbrev:
4230 case CMD_iabbrev: case CMD_inoreabbrev:
4231 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004232 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 case CMD_unabbreviate:
4234 case CMD_cunabbrev:
4235 case CMD_iunabbrev:
4236 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004237 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238#ifdef FEAT_MENU
4239 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4240 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4241 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4242 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4243 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4244 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4245 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4246 case CMD_tmenu: case CMD_tunmenu:
4247 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4248 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4249#endif
4250
4251 case CMD_colorscheme:
4252 xp->xp_context = EXPAND_COLORS;
4253 xp->xp_pattern = arg;
4254 break;
4255
4256 case CMD_compiler:
4257 xp->xp_context = EXPAND_COMPILER;
4258 xp->xp_pattern = arg;
4259 break;
4260
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004261 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004262 xp->xp_context = EXPAND_OWNSYNTAX;
4263 xp->xp_pattern = arg;
4264 break;
4265
4266 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004267 xp->xp_context = EXPAND_FILETYPE;
4268 xp->xp_pattern = arg;
4269 break;
4270
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004271 case CMD_packadd:
4272 xp->xp_context = EXPAND_PACKADD;
4273 xp->xp_pattern = arg;
4274 break;
4275
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4277 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4278 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004279 p = skiptowhite(arg);
4280 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 {
4282 xp->xp_context = EXPAND_LANGUAGE;
4283 xp->xp_pattern = arg;
4284 }
4285 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004286 {
4287 if ( STRNCMP(arg, "messages", p - arg) == 0
4288 || STRNCMP(arg, "ctype", p - arg) == 0
4289 || STRNCMP(arg, "time", p - arg) == 0)
4290 {
4291 xp->xp_context = EXPAND_LOCALES;
4292 xp->xp_pattern = skipwhite(p);
4293 }
4294 else
4295 xp->xp_context = EXPAND_NOTHING;
4296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 break;
4298#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004299#if defined(FEAT_PROFILE)
4300 case CMD_profile:
4301 set_context_in_profile_cmd(xp, arg);
4302 break;
4303#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004304 case CMD_behave:
4305 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004306 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004307 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004309 case CMD_messages:
4310 xp->xp_context = EXPAND_MESSAGES;
4311 xp->xp_pattern = arg;
4312 break;
4313
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004314#if defined(FEAT_CMDHIST)
4315 case CMD_history:
4316 xp->xp_context = EXPAND_HISTORY;
4317 xp->xp_pattern = arg;
4318 break;
4319#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004320#if defined(FEAT_PROFILE)
4321 case CMD_syntime:
4322 xp->xp_context = EXPAND_SYNTIME;
4323 xp->xp_pattern = arg;
4324 break;
4325#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004326
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327#endif /* FEAT_CMDL_COMPL */
4328
4329 default:
4330 break;
4331 }
4332 return NULL;
4333}
4334
4335/*
4336 * skip a range specifier of the form: addr [,addr] [;addr] ..
4337 *
4338 * Backslashed delimiters after / or ? will be skipped, and commands will
4339 * not be expanded between /'s and ?'s or after "'".
4340 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004341 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 * Returns the "cmd" pointer advanced to beyond the range.
4343 */
4344 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004345skip_range(
4346 char_u *cmd,
4347 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004349 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350
Bram Moolenaardf177f62005-02-22 08:39:57 +00004351 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 {
4353 if (*cmd == '\'')
4354 {
4355 if (*++cmd == NUL && ctx != NULL)
4356 *ctx = EXPAND_NOTHING;
4357 }
4358 else if (*cmd == '/' || *cmd == '?')
4359 {
4360 delim = *cmd++;
4361 while (*cmd != NUL && *cmd != delim)
4362 if (*cmd++ == '\\' && *cmd != NUL)
4363 ++cmd;
4364 if (*cmd == NUL && ctx != NULL)
4365 *ctx = EXPAND_NOTHING;
4366 }
4367 if (*cmd != NUL)
4368 ++cmd;
4369 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004370
4371 /* Skip ":" and white space. */
4372 while (*cmd == ':')
4373 cmd = skipwhite(cmd + 1);
4374
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 return cmd;
4376}
4377
4378/*
4379 * get a single EX address
4380 *
4381 * Set ptr to the next character after the part that was interpreted.
4382 * Set ptr to NULL when an error is encountered.
4383 *
4384 * Return MAXLNUM when no Ex address was found.
4385 */
4386 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004387get_address(
4388 exarg_T *eap UNUSED,
4389 char_u **ptr,
4390 int addr_type, /* flag: one of ADDR_LINES, ... */
4391 int skip, /* only skip the address, don't use it */
Bram Moolenaarded27822017-01-02 14:27:34 +01004392 int to_other_file, /* flag: may jump to other file */
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01004393 int address_count UNUSED) /* 1 for first address, >1 after comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394{
4395 int c;
4396 int i;
4397 long n;
4398 char_u *cmd;
4399 pos_T pos;
4400 pos_T *fp;
4401 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004402 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403
4404 cmd = skipwhite(*ptr);
4405 lnum = MAXLNUM;
4406 do
4407 {
4408 switch (*cmd)
4409 {
4410 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004411 ++cmd;
4412 switch (addr_type)
4413 {
4414 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 lnum = curwin->w_cursor.lnum;
4416 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004417 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004418 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004419 break;
4420 case ADDR_ARGUMENTS:
4421 lnum = curwin->w_arg_idx + 1;
4422 break;
4423 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004424 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004425 lnum = curbuf->b_fnum;
4426 break;
4427 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004428 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004429 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004430#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004431 case ADDR_QUICKFIX:
4432 lnum = qf_get_cur_valid_idx(eap);
4433 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004434#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004435 }
4436 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437
4438 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004439 ++cmd;
4440 switch (addr_type)
4441 {
4442 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 lnum = curbuf->b_ml.ml_line_count;
4444 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004445 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004446 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004447 break;
4448 case ADDR_ARGUMENTS:
4449 lnum = ARGCOUNT;
4450 break;
4451 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004452 buf = lastbuf;
4453 while (buf->b_ml.ml_mfp == NULL)
4454 {
4455 if (buf->b_prev == NULL)
4456 break;
4457 buf = buf->b_prev;
4458 }
4459 lnum = buf->b_fnum;
4460 break;
4461 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004462 lnum = lastbuf->b_fnum;
4463 break;
4464 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004465 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004466 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004467#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004468 case ADDR_QUICKFIX:
4469 lnum = qf_get_size(eap);
4470 if (lnum == 0)
4471 lnum = 1;
4472 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004473#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004474 }
4475 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476
4477 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004478 if (*++cmd == NUL)
4479 {
4480 cmd = NULL;
4481 goto error;
4482 }
4483 if (addr_type != ADDR_LINES)
4484 {
4485 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004486 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004487 goto error;
4488 }
4489 if (skip)
4490 ++cmd;
4491 else
4492 {
4493 /* Only accept a mark in another file when it is
4494 * used by itself: ":'M". */
4495 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4496 ++cmd;
4497 if (fp == (pos_T *)-1)
4498 /* Jumped to another file. */
4499 lnum = curwin->w_cursor.lnum;
4500 else
4501 {
4502 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 {
4504 cmd = NULL;
4505 goto error;
4506 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004507 lnum = fp->lnum;
4508 }
4509 }
4510 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511
4512 case '/':
4513 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004514 c = *cmd++;
4515 if (addr_type != ADDR_LINES)
4516 {
4517 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004518 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004519 goto error;
4520 }
4521 if (skip) /* skip "/pat/" */
4522 {
4523 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4524 if (*cmd == c)
4525 ++cmd;
4526 }
4527 else
4528 {
4529 pos = curwin->w_cursor; /* save curwin->w_cursor */
4530 /*
4531 * When '/' or '?' follows another address, start
4532 * from there.
4533 */
4534 if (lnum != MAXLNUM)
4535 curwin->w_cursor.lnum = lnum;
4536 /*
4537 * Start a forward search at the end of the line.
4538 * Start a backward search at the start of the line.
4539 * This makes sure we never match in the current
4540 * line, and can match anywhere in the
4541 * next/previous line.
4542 */
4543 if (c == '/')
4544 curwin->w_cursor.col = MAXCOL;
4545 else
4546 curwin->w_cursor.col = 0;
4547 searchcmdlen = 0;
4548 if (!do_search(NULL, c, cmd, 1L,
4549 SEARCH_HIS | SEARCH_MSG, NULL))
4550 {
4551 curwin->w_cursor = pos;
4552 cmd = NULL;
4553 goto error;
4554 }
4555 lnum = curwin->w_cursor.lnum;
4556 curwin->w_cursor = pos;
4557 /* adjust command string pointer */
4558 cmd += searchcmdlen;
4559 }
4560 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561
4562 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004563 ++cmd;
4564 if (addr_type != ADDR_LINES)
4565 {
4566 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004567 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004568 goto error;
4569 }
4570 if (*cmd == '&')
4571 i = RE_SUBST;
4572 else if (*cmd == '?' || *cmd == '/')
4573 i = RE_SEARCH;
4574 else
4575 {
4576 EMSG(_(e_backslash));
4577 cmd = NULL;
4578 goto error;
4579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004581 if (!skip)
4582 {
4583 /*
4584 * When search follows another address, start from
4585 * there.
4586 */
4587 if (lnum != MAXLNUM)
4588 pos.lnum = lnum;
4589 else
4590 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004592 /*
4593 * Start the search just like for the above
4594 * do_search().
4595 */
4596 if (*cmd != '?')
4597 pos.col = MAXCOL;
4598 else
4599 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004600#ifdef FEAT_VIRTUALEDIT
4601 pos.coladd = 0;
4602#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004603 if (searchit(curwin, curbuf, &pos,
4604 *cmd == '?' ? BACKWARD : FORWARD,
4605 (char_u *)"", 1L, SEARCH_MSG,
4606 i, (linenr_T)0, NULL) != FAIL)
4607 lnum = pos.lnum;
4608 else
4609 {
4610 cmd = NULL;
4611 goto error;
4612 }
4613 }
4614 ++cmd;
4615 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616
4617 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004618 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4619 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 }
4621
4622 for (;;)
4623 {
4624 cmd = skipwhite(cmd);
4625 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4626 break;
4627
4628 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004629 {
4630 switch (addr_type)
4631 {
4632 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004633 /* "+1" is same as ".+1" */
4634 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004635 break;
4636 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004637 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004638 break;
4639 case ADDR_ARGUMENTS:
4640 lnum = curwin->w_arg_idx + 1;
4641 break;
4642 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004643 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004644 lnum = curbuf->b_fnum;
4645 break;
4646 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004647 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004648 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004649#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004650 case ADDR_QUICKFIX:
4651 lnum = qf_get_cur_valid_idx(eap);
4652 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004653#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004654 }
4655 }
4656
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 if (VIM_ISDIGIT(*cmd))
4658 i = '+'; /* "number" is same as "+number" */
4659 else
4660 i = *cmd++;
4661 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4662 n = 1;
4663 else
4664 n = getdigits(&cmd);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004665 if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004666 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004667 lnum = compute_buffer_local_count(
4668 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004670 {
4671#ifdef FEAT_FOLDING
4672 /* Relative line addressing, need to adjust for folded lines
4673 * now, but only do it after the first address. */
4674 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4675 && address_count >= 2)
4676 (void)hasFolding(lnum, NULL, &lnum);
4677#endif
4678 if (i == '-')
4679 lnum -= n;
4680 else
4681 lnum += n;
4682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 }
4684 } while (*cmd == '/' || *cmd == '?');
4685
4686error:
4687 *ptr = cmd;
4688 return lnum;
4689}
4690
4691/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004692 * Get flags from an Ex command argument.
4693 */
4694 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004695get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004696{
4697 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4698 {
4699 if (*eap->arg == 'l')
4700 eap->flags |= EXFLAG_LIST;
4701 else if (*eap->arg == 'p')
4702 eap->flags |= EXFLAG_PRINT;
4703 else
4704 eap->flags |= EXFLAG_NR;
4705 eap->arg = skipwhite(eap->arg + 1);
4706 }
4707}
4708
4709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 * Function called for command which is Not Implemented. NI!
4711 */
4712 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004713ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714{
4715 if (!eap->skip)
4716 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4717}
4718
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004719#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720/*
4721 * Function called for script command which is Not Implemented. NI!
4722 * Skips over ":perl <<EOF" constructs.
4723 */
4724 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004725ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726{
4727 if (!eap->skip)
4728 ex_ni(eap);
4729 else
4730 vim_free(script_get(eap, eap->arg));
4731}
4732#endif
4733
4734/*
4735 * Check range in Ex command for validity.
4736 * Return NULL when valid, error message when invalid.
4737 */
4738 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004739invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004741 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 if ( eap->line1 < 0
4743 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004744 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004746
4747 if (eap->argt & RANGE)
4748 {
4749 switch(eap->addr_type)
4750 {
4751 case ADDR_LINES:
4752 if (!(eap->argt & NOTADR)
4753 && eap->line2 > curbuf->b_ml.ml_line_count
4754#ifdef FEAT_DIFF
4755 + (eap->cmdidx == CMD_diffget)
4756#endif
4757 )
4758 return (char_u *)_(e_invrange);
4759 break;
4760 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004761 /* add 1 if ARGCOUNT is 0 */
4762 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004763 return (char_u *)_(e_invrange);
4764 break;
4765 case ADDR_BUFFERS:
4766 if (eap->line1 < firstbuf->b_fnum
4767 || eap->line2 > lastbuf->b_fnum)
4768 return (char_u *)_(e_invrange);
4769 break;
4770 case ADDR_LOADED_BUFFERS:
4771 buf = firstbuf;
4772 while (buf->b_ml.ml_mfp == NULL)
4773 {
4774 if (buf->b_next == NULL)
4775 return (char_u *)_(e_invrange);
4776 buf = buf->b_next;
4777 }
4778 if (eap->line1 < buf->b_fnum)
4779 return (char_u *)_(e_invrange);
4780 buf = lastbuf;
4781 while (buf->b_ml.ml_mfp == NULL)
4782 {
4783 if (buf->b_prev == NULL)
4784 return (char_u *)_(e_invrange);
4785 buf = buf->b_prev;
4786 }
4787 if (eap->line2 > buf->b_fnum)
4788 return (char_u *)_(e_invrange);
4789 break;
4790 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004791 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004792 return (char_u *)_(e_invrange);
4793 break;
4794 case ADDR_TABS:
4795 if (eap->line2 > LAST_TAB_NR)
4796 return (char_u *)_(e_invrange);
4797 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004798#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004799 case ADDR_QUICKFIX:
4800 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4801 return (char_u *)_(e_invrange);
4802 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004803#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004804 }
4805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 return NULL;
4807}
4808
4809/*
4810 * Correct the range for zero line number, if required.
4811 */
4812 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004813correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814{
4815 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4816 {
4817 if (eap->line1 == 0)
4818 eap->line1 = 1;
4819 if (eap->line2 == 0)
4820 eap->line2 = 1;
4821 }
4822}
4823
Bram Moolenaar748bf032005-02-02 23:04:36 +00004824#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004825static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004826
4827/*
4828 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4829 * pattern. Otherwise return eap->arg.
4830 */
4831 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004832skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004833{
4834 char_u *p = eap->arg;
4835
Bram Moolenaara37420f2006-02-04 22:37:47 +00004836 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4837 || eap->cmdidx == CMD_vimgrepadd
4838 || eap->cmdidx == CMD_lvimgrepadd
4839 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004840 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004841 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004842 if (p == NULL)
4843 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004844 }
4845 return p;
4846}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004847
4848/*
4849 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4850 * in the command line, so that things like % get expanded.
4851 */
4852 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004853replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004854{
4855 char_u *new_cmdline;
4856 char_u *program;
4857 char_u *pos;
4858 char_u *ptr;
4859 int len;
4860 int i;
4861
4862 /*
4863 * Don't do it when ":vimgrep" is used for ":grep".
4864 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004865 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4866 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4867 || eap->cmdidx == CMD_grepadd
4868 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004869 && !grep_internal(eap->cmdidx))
4870 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004871 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4872 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004873 {
4874 if (*curbuf->b_p_gp == NUL)
4875 program = p_gp;
4876 else
4877 program = curbuf->b_p_gp;
4878 }
4879 else
4880 {
4881 if (*curbuf->b_p_mp == NUL)
4882 program = p_mp;
4883 else
4884 program = curbuf->b_p_mp;
4885 }
4886
4887 p = skipwhite(p);
4888
4889 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4890 {
4891 /* replace $* by given arguments */
4892 i = 1;
4893 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4894 ++i;
4895 len = (int)STRLEN(p);
4896 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4897 if (new_cmdline == NULL)
4898 return NULL; /* out of memory */
4899 ptr = new_cmdline;
4900 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4901 {
4902 i = (int)(pos - program);
4903 STRNCPY(ptr, program, i);
4904 STRCPY(ptr += i, p);
4905 ptr += len;
4906 program = pos + 2;
4907 }
4908 STRCPY(ptr, program);
4909 }
4910 else
4911 {
4912 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4913 if (new_cmdline == NULL)
4914 return NULL; /* out of memory */
4915 STRCPY(new_cmdline, program);
4916 STRCAT(new_cmdline, " ");
4917 STRCAT(new_cmdline, p);
4918 }
4919 msg_make(p);
4920
4921 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4922 vim_free(*cmdlinep);
4923 *cmdlinep = new_cmdline;
4924 p = new_cmdline;
4925 }
4926 return p;
4927}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004928#endif
4929
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930/*
4931 * Expand file name in Ex command argument.
4932 * Return FAIL for failure, OK otherwise.
4933 */
4934 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004935expand_filename(
4936 exarg_T *eap,
4937 char_u **cmdlinep,
4938 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939{
4940 int has_wildcards; /* need to expand wildcards */
4941 char_u *repl;
4942 int srclen;
4943 char_u *p;
4944 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004945 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946
Bram Moolenaar748bf032005-02-02 23:04:36 +00004947#ifdef FEAT_QUICKFIX
4948 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4949 p = skip_grep_pat(eap);
4950#else
4951 p = eap->arg;
4952#endif
4953
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 /*
4955 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4956 * the file name contains a wildcard it should not cause expanding.
4957 * (it will be expanded anyway if there is a wildcard before replacing).
4958 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004959 has_wildcards = mch_has_wildcard(p);
4960 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004962#ifdef FEAT_EVAL
4963 /* Skip over `=expr`, wildcards in it are not expanded. */
4964 if (p[0] == '`' && p[1] == '=')
4965 {
4966 p += 2;
4967 (void)skip_expr(&p);
4968 if (*p == '`')
4969 ++p;
4970 continue;
4971 }
4972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 /*
4974 * Quick check if this cannot be the start of a special string.
4975 * Also removes backslash before '%', '#' and '<'.
4976 */
4977 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4978 {
4979 ++p;
4980 continue;
4981 }
4982
4983 /*
4984 * Try to find a match at this position.
4985 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004986 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4987 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 if (*errormsgp != NULL) /* error detected */
4989 return FAIL;
4990 if (repl == NULL) /* no match found */
4991 {
4992 p += srclen;
4993 continue;
4994 }
4995
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004996 /* Wildcards won't be expanded below, the replacement is taken
4997 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4998 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4999 {
5000 char_u *l = repl;
5001
5002 repl = expand_env_save(repl);
5003 vim_free(l);
5004 }
5005
Bram Moolenaar63b92542007-03-27 14:57:09 +00005006 /* Need to escape white space et al. with a backslash.
5007 * Don't do this for:
5008 * - replacement that already has been escaped: "##"
5009 * - shell commands (may have to use quotes instead).
5010 * - non-unix systems when there is a single argument (spaces don't
5011 * separate arguments then).
5012 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005014 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 && eap->cmdidx != CMD_bang
5016 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00005017 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00005019 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00005021 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022#ifndef UNIX
5023 && !(eap->argt & NOSPC)
5024#endif
5025 )
5026 {
5027 char_u *l;
5028#ifdef BACKSLASH_IN_FILENAME
5029 /* Don't escape a backslash here, because rem_backslash() doesn't
5030 * remove it later. */
5031 static char_u *nobslash = (char_u *)" \t\"|";
5032# define ESCAPE_CHARS nobslash
5033#else
5034# define ESCAPE_CHARS escape_chars
5035#endif
5036
5037 for (l = repl; *l; ++l)
5038 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5039 {
5040 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5041 if (l != NULL)
5042 {
5043 vim_free(repl);
5044 repl = l;
5045 }
5046 break;
5047 }
5048 }
5049
5050 /* For a shell command a '!' must be escaped. */
5051 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005052 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 {
5054 char_u *l;
5055
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005056 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 if (l != NULL)
5058 {
5059 vim_free(repl);
5060 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 }
5062 }
5063
5064 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5065 vim_free(repl);
5066 if (p == NULL)
5067 return FAIL;
5068 }
5069
5070 /*
5071 * One file argument: Expand wildcards.
5072 * Don't do this with ":r !command" or ":w !command".
5073 */
5074 if ((eap->argt & NOSPC) && !eap->usefilter)
5075 {
5076 /*
5077 * May do this twice:
5078 * 1. Replace environment variables.
5079 * 2. Replace any other wildcards, remove backslashes.
5080 */
5081 for (n = 1; n <= 2; ++n)
5082 {
5083 if (n == 2)
5084 {
5085#ifdef UNIX
5086 /*
5087 * Only for Unix we check for more than one file name.
5088 * For other systems spaces are considered to be part
5089 * of the file name.
5090 * Only check here if there is no wildcard, otherwise
5091 * ExpandOne() will check for errors. This allows
5092 * ":e `ls ve*.c`" on Unix.
5093 */
5094 if (!has_wildcards)
5095 for (p = eap->arg; *p; ++p)
5096 {
5097 /* skip escaped characters */
5098 if (p[1] && (*p == '\\' || *p == Ctrl_V))
5099 ++p;
5100 else if (vim_iswhite(*p))
5101 {
5102 *errormsgp = (char_u *)_("E172: Only one file name allowed");
5103 return FAIL;
5104 }
5105 }
5106#endif
5107
5108 /*
5109 * Halve the number of backslashes (this is Vi compatible).
5110 * For Unix and OS/2, when wildcards are expanded, this is
5111 * done by ExpandOne() below.
5112 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005113#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 if (!has_wildcards)
5115#endif
5116 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 }
5118
5119 if (has_wildcards)
5120 {
5121 if (n == 1)
5122 {
5123 /*
5124 * First loop: May expand environment variables. This
5125 * can be done much faster with expand_env() than with
5126 * something else (e.g., calling a shell).
5127 * After expanding environment variables, check again
5128 * if there are still wildcards present.
5129 */
5130 if (vim_strchr(eap->arg, '$') != NULL
5131 || vim_strchr(eap->arg, '~') != NULL)
5132 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005133 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005134 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 has_wildcards = mch_has_wildcard(NameBuff);
5136 p = NameBuff;
5137 }
5138 else
5139 p = NULL;
5140 }
5141 else /* n == 2 */
5142 {
5143 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005144 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145
5146 ExpandInit(&xpc);
5147 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005148 if (p_wic)
5149 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005151 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 if (p == NULL)
5153 return FAIL;
5154 }
5155 if (p != NULL)
5156 {
5157 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5158 p, cmdlinep);
5159 if (n == 2) /* p came from ExpandOne() */
5160 vim_free(p);
5161 }
5162 }
5163 }
5164 }
5165 return OK;
5166}
5167
5168/*
5169 * Replace part of the command line, keeping eap->cmd, eap->arg and
5170 * eap->nextcmd correct.
5171 * "src" points to the part that is to be replaced, of length "srclen".
5172 * "repl" is the replacement string.
5173 * Returns a pointer to the character after the replaced string.
5174 * Returns NULL for failure.
5175 */
5176 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005177repl_cmdline(
5178 exarg_T *eap,
5179 char_u *src,
5180 int srclen,
5181 char_u *repl,
5182 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183{
5184 int len;
5185 int i;
5186 char_u *new_cmdline;
5187
5188 /*
5189 * The new command line is build in new_cmdline[].
5190 * First allocate it.
5191 * Careful: a "+cmd" argument may have been NUL terminated.
5192 */
5193 len = (int)STRLEN(repl);
5194 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005195 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5197 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5198 return NULL; /* out of memory! */
5199
5200 /*
5201 * Copy the stuff before the expanded part.
5202 * Copy the expanded stuff.
5203 * Copy what came after the expanded part.
5204 * Copy the next commands, if there are any.
5205 */
5206 i = (int)(src - *cmdlinep); /* length of part before match */
5207 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005208
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 mch_memmove(new_cmdline + i, repl, (size_t)len);
5210 i += len; /* remember the end of the string */
5211 STRCPY(new_cmdline + i, src + srclen);
5212 src = new_cmdline + i; /* remember where to continue */
5213
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005214 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 {
5216 i = (int)STRLEN(new_cmdline) + 1;
5217 STRCPY(new_cmdline + i, eap->nextcmd);
5218 eap->nextcmd = new_cmdline + i;
5219 }
5220 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5221 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5222 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5223 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5224 vim_free(*cmdlinep);
5225 *cmdlinep = new_cmdline;
5226
5227 return src;
5228}
5229
5230/*
5231 * Check for '|' to separate commands and '"' to start comments.
5232 */
5233 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005234separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235{
5236 char_u *p;
5237
Bram Moolenaar86b68352004-12-27 21:59:20 +00005238#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005239 p = skip_grep_pat(eap);
5240#else
5241 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005242#endif
5243
5244 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 {
5246 if (*p == Ctrl_V)
5247 {
5248 if (eap->argt & (USECTRLV | XFILE))
5249 ++p; /* skip CTRL-V and next char */
5250 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005251 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005252 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 if (*p == NUL) /* stop at NUL after CTRL-V */
5254 break;
5255 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005256
5257#ifdef FEAT_EVAL
5258 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005259 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005260 {
5261 p += 2;
5262 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005263 }
5264#endif
5265
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 /* Check for '"': start of comment or '|': next command */
5267 /* :@" and :*" do not start a comment!
5268 * :redir @" doesn't either. */
5269 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5270 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5271 || p != eap->arg)
5272 && (eap->cmdidx != CMD_redir
5273 || p != eap->arg + 1 || p[-1] != '@'))
5274 || *p == '|' || *p == '\n')
5275 {
5276 /*
5277 * We remove the '\' before the '|', unless USECTRLV is used
5278 * AND 'b' is present in 'cpoptions'.
5279 */
5280 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5281 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5282 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005283 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 --p;
5285 }
5286 else
5287 {
5288 eap->nextcmd = check_nextcmd(p);
5289 *p = NUL;
5290 break;
5291 }
5292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005294
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5296 del_trailing_spaces(eap->arg);
5297}
5298
5299/*
5300 * get + command from ex argument
5301 */
5302 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005303getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304{
5305 char_u *arg = *argp;
5306 char_u *command = NULL;
5307
5308 if (*arg == '+') /* +[command] */
5309 {
5310 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005311 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 command = dollar_command;
5313 else
5314 {
5315 command = arg;
5316 arg = skip_cmd_arg(command, TRUE);
5317 if (*arg != NUL)
5318 *arg++ = NUL; /* terminate command with NUL */
5319 }
5320
5321 arg = skipwhite(arg); /* skip over spaces */
5322 *argp = arg;
5323 }
5324 return command;
5325}
5326
5327/*
5328 * Find end of "+command" argument. Skip over "\ " and "\\".
5329 */
5330 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005331skip_cmd_arg(
5332 char_u *p,
5333 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334{
5335 while (*p && !vim_isspace(*p))
5336 {
5337 if (*p == '\\' && p[1] != NUL)
5338 {
5339 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005340 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 else
5342 ++p;
5343 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005344 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 }
5346 return p;
5347}
5348
5349/*
5350 * Get "++opt=arg" argument.
5351 * Return FAIL or OK.
5352 */
5353 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005354getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355{
5356 char_u *arg = eap->arg + 2;
5357 int *pp = NULL;
5358#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005359 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 char_u *p;
5361#endif
5362
5363 /* ":edit ++[no]bin[ary] file" */
5364 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5365 {
5366 if (*arg == 'n')
5367 {
5368 arg += 2;
5369 eap->force_bin = FORCE_NOBIN;
5370 }
5371 else
5372 eap->force_bin = FORCE_BIN;
5373 if (!checkforcmd(&arg, "binary", 3))
5374 return FAIL;
5375 eap->arg = skipwhite(arg);
5376 return OK;
5377 }
5378
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005379 /* ":read ++edit file" */
5380 if (STRNCMP(arg, "edit", 4) == 0)
5381 {
5382 eap->read_edit = TRUE;
5383 eap->arg = skipwhite(arg + 4);
5384 return OK;
5385 }
5386
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 if (STRNCMP(arg, "ff", 2) == 0)
5388 {
5389 arg += 2;
5390 pp = &eap->force_ff;
5391 }
5392 else if (STRNCMP(arg, "fileformat", 10) == 0)
5393 {
5394 arg += 10;
5395 pp = &eap->force_ff;
5396 }
5397#ifdef FEAT_MBYTE
5398 else if (STRNCMP(arg, "enc", 3) == 0)
5399 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005400 if (STRNCMP(arg, "encoding", 8) == 0)
5401 arg += 8;
5402 else
5403 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 pp = &eap->force_enc;
5405 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005406 else if (STRNCMP(arg, "bad", 3) == 0)
5407 {
5408 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005409 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411#endif
5412
5413 if (pp == NULL || *arg != '=')
5414 return FAIL;
5415
5416 ++arg;
5417 *pp = (int)(arg - eap->cmd);
5418 arg = skip_cmd_arg(arg, FALSE);
5419 eap->arg = skipwhite(arg);
5420 *arg = NUL;
5421
5422#ifdef FEAT_MBYTE
5423 if (pp == &eap->force_ff)
5424 {
5425#endif
5426 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5427 return FAIL;
5428#ifdef FEAT_MBYTE
5429 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005430 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 {
5432 /* Make 'fileencoding' lower case. */
5433 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5434 *p = TOLOWER_ASC(*p);
5435 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005436 else
5437 {
5438 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5439 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005440 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005441 if (STRICMP(p, "keep") == 0)
5442 eap->bad_char = BAD_KEEP;
5443 else if (STRICMP(p, "drop") == 0)
5444 eap->bad_char = BAD_DROP;
5445 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5446 eap->bad_char = *p;
5447 else
5448 return FAIL;
5449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450#endif
5451
5452 return OK;
5453}
5454
5455/*
5456 * ":abbreviate" and friends.
5457 */
5458 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005459ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460{
5461 do_exmap(eap, TRUE); /* almost the same as mapping */
5462}
5463
5464/*
5465 * ":map" and friends.
5466 */
5467 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005468ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469{
5470 /*
5471 * If we are sourcing .exrc or .vimrc in current directory we
5472 * print the mappings for security reasons.
5473 */
5474 if (secure)
5475 {
5476 secure = 2;
5477 msg_outtrans(eap->cmd);
5478 msg_putchar('\n');
5479 }
5480 do_exmap(eap, FALSE);
5481}
5482
5483/*
5484 * ":unmap" and friends.
5485 */
5486 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005487ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488{
5489 do_exmap(eap, FALSE);
5490}
5491
5492/*
5493 * ":mapclear" and friends.
5494 */
5495 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005496ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497{
5498 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5499}
5500
5501/*
5502 * ":abclear" and friends.
5503 */
5504 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005505ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506{
5507 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5508}
5509
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005510#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005512ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513{
5514 /*
5515 * Disallow auto commands from .exrc and .vimrc in current
5516 * directory for security reasons.
5517 */
5518 if (secure)
5519 {
5520 secure = 2;
5521 eap->errmsg = e_curdir;
5522 }
5523 else if (eap->cmdidx == CMD_autocmd)
5524 do_autocmd(eap->arg, eap->forceit);
5525 else
5526 do_augroup(eap->arg, eap->forceit);
5527}
5528
5529/*
5530 * ":doautocmd": Apply the automatic commands to the current buffer.
5531 */
5532 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005533ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005535 char_u *arg = eap->arg;
5536 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005537 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005538
Bram Moolenaar1610d052016-06-09 22:53:01 +02005539 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5540 /* Only when there is no <nomodeline>. */
5541 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005542 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543}
5544#endif
5545
5546#ifdef FEAT_LISTCMDS
5547/*
5548 * :[N]bunload[!] [N] [bufname] unload buffer
5549 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5550 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5551 */
5552 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005553ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554{
5555 eap->errmsg = do_bufdel(
5556 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5557 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5558 : DOBUF_UNLOAD, eap->arg,
5559 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5560}
5561
5562/*
5563 * :[N]buffer [N] to buffer N
5564 * :[N]sbuffer [N] to buffer N
5565 */
5566 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005567ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568{
5569 if (*eap->arg)
5570 eap->errmsg = e_trailing;
5571 else
5572 {
5573 if (eap->addr_count == 0) /* default is current buffer */
5574 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5575 else
5576 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005577 if (eap->do_ecmd_cmd != NULL)
5578 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 }
5580}
5581
5582/*
5583 * :[N]bmodified [N] to next mod. buffer
5584 * :[N]sbmodified [N] to next mod. buffer
5585 */
5586 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005587ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588{
5589 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005590 if (eap->do_ecmd_cmd != NULL)
5591 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592}
5593
5594/*
5595 * :[N]bnext [N] to next buffer
5596 * :[N]sbnext [N] split and to next buffer
5597 */
5598 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005599ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600{
5601 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005602 if (eap->do_ecmd_cmd != NULL)
5603 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604}
5605
5606/*
5607 * :[N]bNext [N] to previous buffer
5608 * :[N]bprevious [N] to previous buffer
5609 * :[N]sbNext [N] split and to previous buffer
5610 * :[N]sbprevious [N] split and to previous buffer
5611 */
5612 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005613ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614{
5615 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005616 if (eap->do_ecmd_cmd != NULL)
5617 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618}
5619
5620/*
5621 * :brewind to first buffer
5622 * :bfirst to first buffer
5623 * :sbrewind split and to first buffer
5624 * :sbfirst split and to first buffer
5625 */
5626 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005627ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628{
5629 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005630 if (eap->do_ecmd_cmd != NULL)
5631 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632}
5633
5634/*
5635 * :blast to last buffer
5636 * :sblast split and to last buffer
5637 */
5638 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005639ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640{
5641 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005642 if (eap->do_ecmd_cmd != NULL)
5643 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644}
5645#endif
5646
5647 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005648ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649{
5650 return (c == NUL || c == '|' || c == '"' || c == '\n');
5651}
5652
5653#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5654 || defined(PROTO)
5655/*
5656 * Return the next command, after the first '|' or '\n'.
5657 * Return NULL if not found.
5658 */
5659 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005660find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661{
5662 while (*p != '|' && *p != '\n')
5663 {
5664 if (*p == NUL)
5665 return NULL;
5666 ++p;
5667 }
5668 return (p + 1);
5669}
5670#endif
5671
5672/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005673 * Check if *p is a separator between Ex commands, skipping over white space.
5674 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 */
5676 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005677check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005679 char_u *s = skipwhite(p);
5680
5681 if (*s == '|' || *s == '\n')
5682 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 else
5684 return NULL;
5685}
5686
5687/*
5688 * - if there are more files to edit
5689 * - and this is the last window
5690 * - and forceit not used
5691 * - and not repeated twice on a row
5692 * return FAIL and give error message if 'message' TRUE
5693 * return OK otherwise
5694 */
5695 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005696check_more(
5697 int message, /* when FALSE check only, no messages */
5698 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699{
5700 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5701
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005702 if (!forceit && only_one_window()
5703 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 {
5705 if (message)
5706 {
5707#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5708 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5709 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005710 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711
5712 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005713 vim_strncpy(buff,
5714 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005715 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005717 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 _("%d more files to edit. Quit anyway?"), n);
5719 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5720 return OK;
5721 return FAIL;
5722 }
5723#endif
5724 if (n == 1)
5725 EMSG(_("E173: 1 more file to edit"));
5726 else
5727 EMSGN(_("E173: %ld more files to edit"), n);
5728 quitmore = 2; /* next try to quit is allowed */
5729 }
5730 return FAIL;
5731 }
5732 return OK;
5733}
5734
5735#ifdef FEAT_CMDL_COMPL
5736/*
5737 * Function given to ExpandGeneric() to obtain the list of command names.
5738 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005740get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741{
5742 if (idx >= (int)CMD_SIZE)
5743# ifdef FEAT_USR_CMDS
5744 return get_user_command_name(idx);
5745# else
5746 return NULL;
5747# endif
5748 return cmdnames[idx].cmd_name;
5749}
5750#endif
5751
5752#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005753static 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);
5754static void uc_list(char_u *name, size_t name_len);
5755static 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);
5756static char_u *uc_split_args(char_u *arg, size_t *lenp);
5757static 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 +00005758
5759 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005760uc_add_command(
5761 char_u *name,
5762 size_t name_len,
5763 char_u *rep,
5764 long argt,
5765 long def,
5766 int flags,
5767 int compl,
5768 char_u *compl_arg,
5769 int addr_type,
5770 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771{
5772 ucmd_T *cmd = NULL;
5773 char_u *p;
5774 int i;
5775 int cmp = 1;
5776 char_u *rep_buf = NULL;
5777 garray_T *gap;
5778
Bram Moolenaar9c102382006-05-03 21:26:49 +00005779 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 if (rep_buf == NULL)
5781 {
5782 /* Can't replace termcodes - try using the string as is */
5783 rep_buf = vim_strsave(rep);
5784
5785 /* Give up if out of memory */
5786 if (rep_buf == NULL)
5787 return FAIL;
5788 }
5789
5790 /* get address of growarray: global or in curbuf */
5791 if (flags & UC_BUFFER)
5792 {
5793 gap = &curbuf->b_ucmds;
5794 if (gap->ga_itemsize == 0)
5795 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5796 }
5797 else
5798 gap = &ucmds;
5799
5800 /* Search for the command in the already defined commands. */
5801 for (i = 0; i < gap->ga_len; ++i)
5802 {
5803 size_t len;
5804
5805 cmd = USER_CMD_GA(gap, i);
5806 len = STRLEN(cmd->uc_name);
5807 cmp = STRNCMP(name, cmd->uc_name, name_len);
5808 if (cmp == 0)
5809 {
5810 if (name_len < len)
5811 cmp = -1;
5812 else if (name_len > len)
5813 cmp = 1;
5814 }
5815
5816 if (cmp == 0)
5817 {
5818 if (!force)
5819 {
5820 EMSG(_("E174: Command already exists: add ! to replace it"));
5821 goto fail;
5822 }
5823
5824 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005825 cmd->uc_rep = NULL;
5826#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5827 vim_free(cmd->uc_compl_arg);
5828 cmd->uc_compl_arg = NULL;
5829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 break;
5831 }
5832
5833 /* Stop as soon as we pass the name to add */
5834 if (cmp < 0)
5835 break;
5836 }
5837
5838 /* Extend the array unless we're replacing an existing command */
5839 if (cmp != 0)
5840 {
5841 if (ga_grow(gap, 1) != OK)
5842 goto fail;
5843 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5844 goto fail;
5845
5846 cmd = USER_CMD_GA(gap, i);
5847 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5848
5849 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850
5851 cmd->uc_name = p;
5852 }
5853
5854 cmd->uc_rep = rep_buf;
5855 cmd->uc_argt = argt;
5856 cmd->uc_def = def;
5857 cmd->uc_compl = compl;
5858#ifdef FEAT_EVAL
5859 cmd->uc_scriptID = current_SID;
5860# ifdef FEAT_CMDL_COMPL
5861 cmd->uc_compl_arg = compl_arg;
5862# endif
5863#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005864 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865
5866 return OK;
5867
5868fail:
5869 vim_free(rep_buf);
5870#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5871 vim_free(compl_arg);
5872#endif
5873 return FAIL;
5874}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005877#if defined(FEAT_USR_CMDS)
5878static struct
5879{
5880 int expand;
5881 char *name;
5882} addr_type_complete[] =
5883{
5884 {ADDR_ARGUMENTS, "arguments"},
5885 {ADDR_LINES, "lines"},
5886 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5887 {ADDR_TABS, "tabs"},
5888 {ADDR_BUFFERS, "buffers"},
5889 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005890 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005891 {-1, NULL}
5892};
5893#endif
5894
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005895#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896/*
5897 * List of names for completion for ":command" with the EXPAND_ flag.
5898 * Must be alphabetical for completion.
5899 */
5900static struct
5901{
5902 int expand;
5903 char *name;
5904} command_complete[] =
5905{
5906 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005907 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005909 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005911 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005912#if defined(FEAT_CSCOPE)
5913 {EXPAND_CSCOPE, "cscope"},
5914#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5916 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005917 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918#endif
5919 {EXPAND_DIRECTORIES, "dir"},
5920 {EXPAND_ENV_VARS, "environment"},
5921 {EXPAND_EVENTS, "event"},
5922 {EXPAND_EXPRESSION, "expression"},
5923 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005924 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005925 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 {EXPAND_FUNCTIONS, "function"},
5927 {EXPAND_HELP, "help"},
5928 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005929#if defined(FEAT_CMDHIST)
5930 {EXPAND_HISTORY, "history"},
5931#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005932#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005933 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005934 {EXPAND_LOCALES, "locale"},
5935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 {EXPAND_MAPPINGS, "mapping"},
5937 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005938 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005939 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005940#if defined(FEAT_PROFILE)
5941 {EXPAND_SYNTIME, "syntime"},
5942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005944 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005945 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005946#if defined(FEAT_SIGNS)
5947 {EXPAND_SIGN, "sign"},
5948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 {EXPAND_TAGS, "tag"},
5950 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005951 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 {EXPAND_USER_VARS, "var"},
5953 {0, NULL}
5954};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005957#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005959uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960{
5961 int i, j;
5962 int found = FALSE;
5963 ucmd_T *cmd;
5964 int len;
5965 long a;
5966 garray_T *gap;
5967
5968 gap = &curbuf->b_ucmds;
5969 for (;;)
5970 {
5971 for (i = 0; i < gap->ga_len; ++i)
5972 {
5973 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005974 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975
Bram Moolenaard29459b2016-08-26 22:29:11 +02005976 /* Skip commands which don't match the requested prefix and
5977 * commands filtered out. */
5978 if (STRNCMP(name, cmd->uc_name, name_len) != 0
5979 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 continue;
5981
5982 /* Put out the title first time */
5983 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005984 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 found = TRUE;
5986 msg_putchar('\n');
5987 if (got_int)
5988 break;
5989
5990 /* Special cases */
5991 msg_putchar(a & BANG ? '!' : ' ');
5992 msg_putchar(a & REGSTR ? '"' : ' ');
5993 msg_putchar(gap != &ucmds ? 'b' : ' ');
5994 msg_putchar(' ');
5995
5996 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5997 len = (int)STRLEN(cmd->uc_name) + 4;
5998
5999 do {
6000 msg_putchar(' ');
6001 ++len;
6002 } while (len < 16);
6003
6004 len = 0;
6005
6006 /* Arguments */
6007 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
6008 {
6009 case 0: IObuff[len++] = '0'; break;
6010 case (EXTRA): IObuff[len++] = '*'; break;
6011 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
6012 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
6013 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
6014 }
6015
6016 do {
6017 IObuff[len++] = ' ';
6018 } while (len < 5);
6019
6020 /* Range */
6021 if (a & (RANGE|COUNT))
6022 {
6023 if (a & COUNT)
6024 {
6025 /* -count=N */
6026 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6027 len += (int)STRLEN(IObuff + len);
6028 }
6029 else if (a & DFLALL)
6030 IObuff[len++] = '%';
6031 else if (cmd->uc_def >= 0)
6032 {
6033 /* -range=N */
6034 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6035 len += (int)STRLEN(IObuff + len);
6036 }
6037 else
6038 IObuff[len++] = '.';
6039 }
6040
6041 do {
6042 IObuff[len++] = ' ';
6043 } while (len < 11);
6044
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006045 /* Address Type */
6046 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6047 if (addr_type_complete[j].expand != ADDR_LINES
6048 && addr_type_complete[j].expand == cmd->uc_addr_type)
6049 {
6050 STRCPY(IObuff + len, addr_type_complete[j].name);
6051 len += (int)STRLEN(IObuff + len);
6052 break;
6053 }
6054
6055 do {
6056 IObuff[len++] = ' ';
6057 } while (len < 21);
6058
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 /* Completion */
6060 for (j = 0; command_complete[j].expand != 0; ++j)
6061 if (command_complete[j].expand == cmd->uc_compl)
6062 {
6063 STRCPY(IObuff + len, command_complete[j].name);
6064 len += (int)STRLEN(IObuff + len);
6065 break;
6066 }
6067
6068 do {
6069 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006070 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071
6072 IObuff[len] = '\0';
6073 msg_outtrans(IObuff);
6074
6075 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006076#ifdef FEAT_EVAL
6077 if (p_verbose > 0)
6078 last_set_msg(cmd->uc_scriptID);
6079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 out_flush();
6081 ui_breakcheck();
6082 if (got_int)
6083 break;
6084 }
6085 if (gap == &ucmds || i < gap->ga_len)
6086 break;
6087 gap = &ucmds;
6088 }
6089
6090 if (!found)
6091 MSG(_("No user-defined commands found"));
6092}
6093
6094 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006095uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096{
6097 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6098 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6099 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6100 0xb9, 0x7f, 0};
6101 int i;
6102
6103 for (i = 0; fcmd[i]; ++i)
6104 IObuff[i] = fcmd[i] - 0x40;
6105 IObuff[i] = 0;
6106 return IObuff;
6107}
6108
6109 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006110uc_scan_attr(
6111 char_u *attr,
6112 size_t len,
6113 long *argt,
6114 long *def,
6115 int *flags,
6116 int *compl,
6117 char_u **compl_arg,
6118 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119{
6120 char_u *p;
6121
6122 if (len == 0)
6123 {
6124 EMSG(_("E175: No attribute specified"));
6125 return FAIL;
6126 }
6127
6128 /* First, try the simple attributes (no arguments) */
6129 if (STRNICMP(attr, "bang", len) == 0)
6130 *argt |= BANG;
6131 else if (STRNICMP(attr, "buffer", len) == 0)
6132 *flags |= UC_BUFFER;
6133 else if (STRNICMP(attr, "register", len) == 0)
6134 *argt |= REGSTR;
6135 else if (STRNICMP(attr, "bar", len) == 0)
6136 *argt |= TRLBAR;
6137 else
6138 {
6139 int i;
6140 char_u *val = NULL;
6141 size_t vallen = 0;
6142 size_t attrlen = len;
6143
6144 /* Look for the attribute name - which is the part before any '=' */
6145 for (i = 0; i < (int)len; ++i)
6146 {
6147 if (attr[i] == '=')
6148 {
6149 val = &attr[i + 1];
6150 vallen = len - i - 1;
6151 attrlen = i;
6152 break;
6153 }
6154 }
6155
6156 if (STRNICMP(attr, "nargs", attrlen) == 0)
6157 {
6158 if (vallen == 1)
6159 {
6160 if (*val == '0')
6161 /* Do nothing - this is the default */;
6162 else if (*val == '1')
6163 *argt |= (EXTRA | NOSPC | NEEDARG);
6164 else if (*val == '*')
6165 *argt |= EXTRA;
6166 else if (*val == '?')
6167 *argt |= (EXTRA | NOSPC);
6168 else if (*val == '+')
6169 *argt |= (EXTRA | NEEDARG);
6170 else
6171 goto wrong_nargs;
6172 }
6173 else
6174 {
6175wrong_nargs:
6176 EMSG(_("E176: Invalid number of arguments"));
6177 return FAIL;
6178 }
6179 }
6180 else if (STRNICMP(attr, "range", attrlen) == 0)
6181 {
6182 *argt |= RANGE;
6183 if (vallen == 1 && *val == '%')
6184 *argt |= DFLALL;
6185 else if (val != NULL)
6186 {
6187 p = val;
6188 if (*def >= 0)
6189 {
6190two_count:
6191 EMSG(_("E177: Count cannot be specified twice"));
6192 return FAIL;
6193 }
6194
6195 *def = getdigits(&p);
6196 *argt |= (ZEROR | NOTADR);
6197
6198 if (p != val + vallen || vallen == 0)
6199 {
6200invalid_count:
6201 EMSG(_("E178: Invalid default value for count"));
6202 return FAIL;
6203 }
6204 }
6205 }
6206 else if (STRNICMP(attr, "count", attrlen) == 0)
6207 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006208 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209
6210 if (val != NULL)
6211 {
6212 p = val;
6213 if (*def >= 0)
6214 goto two_count;
6215
6216 *def = getdigits(&p);
6217
6218 if (p != val + vallen)
6219 goto invalid_count;
6220 }
6221
6222 if (*def < 0)
6223 *def = 0;
6224 }
6225 else if (STRNICMP(attr, "complete", attrlen) == 0)
6226 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 if (val == NULL)
6228 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006229 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 return FAIL;
6231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006233 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6234 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006237 else if (STRNICMP(attr, "addr", attrlen) == 0)
6238 {
6239 *argt |= RANGE;
6240 if (val == NULL)
6241 {
6242 EMSG(_("E179: argument required for -addr"));
6243 return FAIL;
6244 }
6245 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6246 == FAIL)
6247 return FAIL;
6248 if (addr_type_arg != ADDR_LINES)
6249 *argt |= (ZEROR | NOTADR) ;
6250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 else
6252 {
6253 char_u ch = attr[len];
6254 attr[len] = '\0';
6255 EMSG2(_("E181: Invalid attribute: %s"), attr);
6256 attr[len] = ch;
6257 return FAIL;
6258 }
6259 }
6260
6261 return OK;
6262}
6263
Bram Moolenaara850a712009-01-28 14:42:59 +00006264/*
6265 * ":command ..."
6266 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006268ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269{
6270 char_u *name;
6271 char_u *end;
6272 char_u *p;
6273 long argt = 0;
6274 long def = -1;
6275 int flags = 0;
6276 int compl = EXPAND_NOTHING;
6277 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006278 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006280 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281
6282 p = eap->arg;
6283
6284 /* Check for attributes */
6285 while (*p == '-')
6286 {
6287 ++p;
6288 end = skiptowhite(p);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006289 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 == FAIL)
6291 return;
6292 p = skipwhite(end);
6293 }
6294
6295 /* Get the name (if any) and skip to the following argument */
6296 name = p;
6297 if (ASCII_ISALPHA(*p))
6298 while (ASCII_ISALNUM(*p))
6299 ++p;
6300 if (!ends_excmd(*p) && !vim_iswhite(*p))
6301 {
6302 EMSG(_("E182: Invalid command name"));
6303 return;
6304 }
6305 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006306 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307
6308 /* If there is nothing after the name, and no attributes were specified,
6309 * we are listing commands
6310 */
6311 p = skipwhite(end);
6312 if (!has_attr && ends_excmd(*p))
6313 {
6314 uc_list(name, end - name);
6315 }
6316 else if (!ASCII_ISUPPER(*name))
6317 {
6318 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6319 return;
6320 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006321 else if ((name_len == 1 && *name == 'X')
6322 || (name_len <= 4
6323 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6324 {
6325 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6326 return;
6327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 else
6329 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006330 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331}
6332
6333/*
6334 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 * Clear all user commands, global and for current buffer.
6336 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006337 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006338ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339{
6340 uc_clear(&ucmds);
6341 uc_clear(&curbuf->b_ucmds);
6342}
6343
6344/*
6345 * Clear all user commands for "gap".
6346 */
6347 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006348uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349{
6350 int i;
6351 ucmd_T *cmd;
6352
6353 for (i = 0; i < gap->ga_len; ++i)
6354 {
6355 cmd = USER_CMD_GA(gap, i);
6356 vim_free(cmd->uc_name);
6357 vim_free(cmd->uc_rep);
6358# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6359 vim_free(cmd->uc_compl_arg);
6360# endif
6361 }
6362 ga_clear(gap);
6363}
6364
6365 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006366ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367{
6368 int i = 0;
6369 ucmd_T *cmd = NULL;
6370 int cmp = -1;
6371 garray_T *gap;
6372
6373 gap = &curbuf->b_ucmds;
6374 for (;;)
6375 {
6376 for (i = 0; i < gap->ga_len; ++i)
6377 {
6378 cmd = USER_CMD_GA(gap, i);
6379 cmp = STRCMP(eap->arg, cmd->uc_name);
6380 if (cmp <= 0)
6381 break;
6382 }
6383 if (gap == &ucmds || cmp == 0)
6384 break;
6385 gap = &ucmds;
6386 }
6387
6388 if (cmp != 0)
6389 {
6390 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6391 return;
6392 }
6393
6394 vim_free(cmd->uc_name);
6395 vim_free(cmd->uc_rep);
6396# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6397 vim_free(cmd->uc_compl_arg);
6398# endif
6399
6400 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401
6402 if (i < gap->ga_len)
6403 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6404}
6405
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006406/*
6407 * split and quote args for <f-args>
6408 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006410uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411{
6412 char_u *buf;
6413 char_u *p;
6414 char_u *q;
6415 int len;
6416
6417 /* Precalculate length */
6418 p = arg;
6419 len = 2; /* Initial and final quotes */
6420
6421 while (*p)
6422 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006423 if (p[0] == '\\' && p[1] == '\\')
6424 {
6425 len += 2;
6426 p += 2;
6427 }
6428 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 {
6430 len += 1;
6431 p += 2;
6432 }
6433 else if (*p == '\\' || *p == '"')
6434 {
6435 len += 2;
6436 p += 1;
6437 }
6438 else if (vim_iswhite(*p))
6439 {
6440 p = skipwhite(p);
6441 if (*p == NUL)
6442 break;
6443 len += 3; /* "," */
6444 }
6445 else
6446 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006447#ifdef FEAT_MBYTE
6448 int charlen = (*mb_ptr2len)(p);
6449 len += charlen;
6450 p += charlen;
6451#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 ++len;
6453 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 }
6456 }
6457
6458 buf = alloc(len + 1);
6459 if (buf == NULL)
6460 {
6461 *lenp = 0;
6462 return buf;
6463 }
6464
6465 p = arg;
6466 q = buf;
6467 *q++ = '"';
6468 while (*p)
6469 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006470 if (p[0] == '\\' && p[1] == '\\')
6471 {
6472 *q++ = '\\';
6473 *q++ = '\\';
6474 p += 2;
6475 }
6476 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 {
6478 *q++ = p[1];
6479 p += 2;
6480 }
6481 else if (*p == '\\' || *p == '"')
6482 {
6483 *q++ = '\\';
6484 *q++ = *p++;
6485 }
6486 else if (vim_iswhite(*p))
6487 {
6488 p = skipwhite(p);
6489 if (*p == NUL)
6490 break;
6491 *q++ = '"';
6492 *q++ = ',';
6493 *q++ = '"';
6494 }
6495 else
6496 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006497 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 }
6499 }
6500 *q++ = '"';
6501 *q = 0;
6502
6503 *lenp = len;
6504 return buf;
6505}
6506
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006507 static size_t
6508add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6509{
6510 size_t result;
6511
6512 result = STRLEN(mod_str);
6513 if (*multi_mods)
6514 result += 1;
6515 if (buf != NULL)
6516 {
6517 if (*multi_mods)
6518 STRCAT(buf, " ");
6519 STRCAT(buf, mod_str);
6520 }
6521
6522 *multi_mods = 1;
6523
6524 return result;
6525}
6526
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527/*
6528 * Check for a <> code in a user command.
6529 * "code" points to the '<'. "len" the length of the <> (inclusive).
6530 * "buf" is where the result is to be added.
6531 * "split_buf" points to a buffer used for splitting, caller should free it.
6532 * "split_len" is the length of what "split_buf" contains.
6533 * Returns the length of the replacement, which has been added to "buf".
6534 * Returns -1 if there was no match, and only the "<" has been copied.
6535 */
6536 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006537uc_check_code(
6538 char_u *code,
6539 size_t len,
6540 char_u *buf,
6541 ucmd_T *cmd, /* the user command we're expanding */
6542 exarg_T *eap, /* ex arguments */
6543 char_u **split_buf,
6544 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545{
6546 size_t result = 0;
6547 char_u *p = code + 1;
6548 size_t l = len - 2;
6549 int quote = 0;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006550 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_MODS,
6551 ct_REGISTER, ct_LT, ct_NONE } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552
6553 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6554 {
6555 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6556 p += 2;
6557 l -= 2;
6558 }
6559
Bram Moolenaar371d5402006-03-20 21:47:49 +00006560 ++l;
6561 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006563 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006565 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006567 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006569 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006571 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006573 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006575 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006577 else if (STRNICMP(p, "mods>", l) == 0)
6578 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579
6580 switch (type)
6581 {
6582 case ct_ARGS:
6583 /* Simple case first */
6584 if (*eap->arg == NUL)
6585 {
6586 if (quote == 1)
6587 {
6588 result = 2;
6589 if (buf != NULL)
6590 STRCPY(buf, "''");
6591 }
6592 else
6593 result = 0;
6594 break;
6595 }
6596
6597 /* When specified there is a single argument don't split it.
6598 * Works for ":Cmd %" when % is "a b c". */
6599 if ((eap->argt & NOSPC) && quote == 2)
6600 quote = 1;
6601
6602 switch (quote)
6603 {
6604 case 0: /* No quoting, no splitting */
6605 result = STRLEN(eap->arg);
6606 if (buf != NULL)
6607 STRCPY(buf, eap->arg);
6608 break;
6609 case 1: /* Quote, but don't split */
6610 result = STRLEN(eap->arg) + 2;
6611 for (p = eap->arg; *p; ++p)
6612 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006613#ifdef FEAT_MBYTE
6614 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6615 /* DBCS can contain \ in a trail byte, skip the
6616 * double-byte character. */
6617 ++p;
6618 else
6619#endif
6620 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 ++result;
6622 }
6623
6624 if (buf != NULL)
6625 {
6626 *buf++ = '"';
6627 for (p = eap->arg; *p; ++p)
6628 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006629#ifdef FEAT_MBYTE
6630 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6631 /* DBCS can contain \ in a trail byte, copy the
6632 * double-byte character to avoid escaping. */
6633 *buf++ = *p++;
6634 else
6635#endif
6636 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 *buf++ = '\\';
6638 *buf++ = *p;
6639 }
6640 *buf = '"';
6641 }
6642
6643 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006644 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 /* This is hard, so only do it once, and cache the result */
6646 if (*split_buf == NULL)
6647 *split_buf = uc_split_args(eap->arg, split_len);
6648
6649 result = *split_len;
6650 if (buf != NULL && result != 0)
6651 STRCPY(buf, *split_buf);
6652
6653 break;
6654 }
6655 break;
6656
6657 case ct_BANG:
6658 result = eap->forceit ? 1 : 0;
6659 if (quote)
6660 result += 2;
6661 if (buf != NULL)
6662 {
6663 if (quote)
6664 *buf++ = '"';
6665 if (eap->forceit)
6666 *buf++ = '!';
6667 if (quote)
6668 *buf = '"';
6669 }
6670 break;
6671
6672 case ct_LINE1:
6673 case ct_LINE2:
6674 case ct_COUNT:
6675 {
6676 char num_buf[20];
6677 long num = (type == ct_LINE1) ? eap->line1 :
6678 (type == ct_LINE2) ? eap->line2 :
6679 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6680 size_t num_len;
6681
6682 sprintf(num_buf, "%ld", num);
6683 num_len = STRLEN(num_buf);
6684 result = num_len;
6685
6686 if (quote)
6687 result += 2;
6688
6689 if (buf != NULL)
6690 {
6691 if (quote)
6692 *buf++ = '"';
6693 STRCPY(buf, num_buf);
6694 buf += num_len;
6695 if (quote)
6696 *buf = '"';
6697 }
6698
6699 break;
6700 }
6701
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006702 case ct_MODS:
6703 {
6704 int multi_mods = 0;
6705 typedef struct {
6706 int *varp;
6707 char *name;
6708 } mod_entry_T;
6709 static mod_entry_T mod_entries[] = {
6710#ifdef FEAT_BROWSE_CMD
6711 {&cmdmod.browse, "browse"},
6712#endif
6713#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6714 {&cmdmod.confirm, "confirm"},
6715#endif
6716 {&cmdmod.hide, "hide"},
6717 {&cmdmod.keepalt, "keepalt"},
6718 {&cmdmod.keepjumps, "keepjumps"},
6719 {&cmdmod.keepmarks, "keepmarks"},
6720 {&cmdmod.keeppatterns, "keeppatterns"},
6721 {&cmdmod.lockmarks, "lockmarks"},
6722 {&cmdmod.noswapfile, "noswapfile"},
6723 {NULL, NULL}
6724 };
6725 int i;
6726
6727 result = quote ? 2 : 0;
6728 if (buf != NULL)
6729 {
6730 if (quote)
6731 *buf++ = '"';
6732 *buf = '\0';
6733 }
6734
6735#ifdef FEAT_WINDOWS
6736 /* :aboveleft and :leftabove */
6737 if (cmdmod.split & WSP_ABOVE)
6738 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6739 /* :belowright and :rightbelow */
6740 if (cmdmod.split & WSP_BELOW)
6741 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6742 /* :botright */
6743 if (cmdmod.split & WSP_BOT)
6744 result += add_cmd_modifier(buf, "botright", &multi_mods);
6745#endif
6746
6747 /* the modifiers that are simple flags */
6748 for (i = 0; mod_entries[i].varp != NULL; ++i)
6749 if (*mod_entries[i].varp)
6750 result += add_cmd_modifier(buf, mod_entries[i].name,
6751 &multi_mods);
6752
6753 /* TODO: How to support :noautocmd? */
6754#ifdef HAVE_SANDBOX
6755 /* TODO: How to support :sandbox?*/
6756#endif
6757 /* :silent */
6758 if (msg_silent > 0)
6759 result += add_cmd_modifier(buf,
6760 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
6761#ifdef FEAT_WINDOWS
6762 /* :tab */
6763 if (cmdmod.tab > 0)
6764 result += add_cmd_modifier(buf, "tab", &multi_mods);
6765 /* :topleft */
6766 if (cmdmod.split & WSP_TOP)
6767 result += add_cmd_modifier(buf, "topleft", &multi_mods);
6768#endif
6769 /* TODO: How to support :unsilent?*/
6770 /* :verbose */
6771 if (p_verbose > 0)
6772 result += add_cmd_modifier(buf, "verbose", &multi_mods);
6773#ifdef FEAT_WINDOWS
6774 /* :vertical */
6775 if (cmdmod.split & WSP_VERT)
6776 result += add_cmd_modifier(buf, "vertical", &multi_mods);
6777#endif
6778 if (quote && buf != NULL)
6779 {
6780 buf += result - 2;
6781 *buf = '"';
6782 }
6783 break;
6784 }
6785
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 case ct_REGISTER:
6787 result = eap->regname ? 1 : 0;
6788 if (quote)
6789 result += 2;
6790 if (buf != NULL)
6791 {
6792 if (quote)
6793 *buf++ = '\'';
6794 if (eap->regname)
6795 *buf++ = eap->regname;
6796 if (quote)
6797 *buf = '\'';
6798 }
6799 break;
6800
6801 case ct_LT:
6802 result = 1;
6803 if (buf != NULL)
6804 *buf = '<';
6805 break;
6806
6807 default:
6808 /* Not recognized: just copy the '<' and return -1. */
6809 result = (size_t)-1;
6810 if (buf != NULL)
6811 *buf = '<';
6812 break;
6813 }
6814
6815 return result;
6816}
6817
6818 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006819do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820{
6821 char_u *buf;
6822 char_u *p;
6823 char_u *q;
6824
6825 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006826 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006827 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 size_t len, totlen;
6829
6830 size_t split_len = 0;
6831 char_u *split_buf = NULL;
6832 ucmd_T *cmd;
6833#ifdef FEAT_EVAL
6834 scid_T save_current_SID = current_SID;
6835#endif
6836
6837 if (eap->cmdidx == CMD_USER)
6838 cmd = USER_CMD(eap->useridx);
6839 else
6840 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6841
6842 /*
6843 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006844 * First round: "buf" is NULL, compute length, allocate "buf".
6845 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 */
6847 buf = NULL;
6848 for (;;)
6849 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006850 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006851 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006853
6854 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006856 start = vim_strchr(p, '<');
6857 if (start != NULL)
6858 end = vim_strchr(start + 1, '>');
6859 if (buf != NULL)
6860 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006861 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6862 ;
6863 if (*ksp == K_SPECIAL
6864 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006865 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6866# ifdef FEAT_GUI
6867 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6868# endif
6869 ))
6870 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006871 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006872 * KS_SPECIAL KE_FILLER, like for mappings, but
6873 * do_cmdline() doesn't handle that, so convert it back.
6874 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6875 len = ksp - p;
6876 if (len > 0)
6877 {
6878 mch_memmove(q, p, len);
6879 q += len;
6880 }
6881 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6882 p = ksp + 3;
6883 continue;
6884 }
6885 }
6886
6887 /* break if there no <item> is found */
6888 if (start == NULL || end == NULL)
6889 break;
6890
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 /* Include the '>' */
6892 ++end;
6893
6894 /* Take everything up to the '<' */
6895 len = start - p;
6896 if (buf == NULL)
6897 totlen += len;
6898 else
6899 {
6900 mch_memmove(q, p, len);
6901 q += len;
6902 }
6903
6904 len = uc_check_code(start, end - start, q, cmd, eap,
6905 &split_buf, &split_len);
6906 if (len == (size_t)-1)
6907 {
6908 /* no match, continue after '<' */
6909 p = start + 1;
6910 len = 1;
6911 }
6912 else
6913 p = end;
6914 if (buf == NULL)
6915 totlen += len;
6916 else
6917 q += len;
6918 }
6919 if (buf != NULL) /* second time here, finished */
6920 {
6921 STRCPY(q, p);
6922 break;
6923 }
6924
6925 totlen += STRLEN(p); /* Add on the trailing characters */
6926 buf = alloc((unsigned)(totlen + 1));
6927 if (buf == NULL)
6928 {
6929 vim_free(split_buf);
6930 return;
6931 }
6932 }
6933
6934#ifdef FEAT_EVAL
6935 current_SID = cmd->uc_scriptID;
6936#endif
6937 (void)do_cmdline(buf, eap->getline, eap->cookie,
6938 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6939#ifdef FEAT_EVAL
6940 current_SID = save_current_SID;
6941#endif
6942 vim_free(buf);
6943 vim_free(split_buf);
6944}
6945
6946# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6947 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006948get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949{
6950 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6951}
6952
6953/*
6954 * Function given to ExpandGeneric() to obtain the list of user command names.
6955 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006957get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958{
6959 if (idx < curbuf->b_ucmds.ga_len)
6960 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6961 idx -= curbuf->b_ucmds.ga_len;
6962 if (idx < ucmds.ga_len)
6963 return USER_CMD(idx)->uc_name;
6964 return NULL;
6965}
6966
6967/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006968 * Function given to ExpandGeneric() to obtain the list of user address type names.
6969 */
6970 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006971get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006972{
6973 return (char_u *)addr_type_complete[idx].name;
6974}
6975
6976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 * Function given to ExpandGeneric() to obtain the list of user command
6978 * attributes.
6979 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006981get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982{
6983 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006984 {"addr", "bang", "bar", "buffer", "complete",
6985 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006987 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 return NULL;
6989 return (char_u *)user_cmd_flags[idx];
6990}
6991
6992/*
6993 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6994 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006996get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997{
6998 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6999
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007000 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 return NULL;
7002 return (char_u *)user_cmd_nargs[idx];
7003}
7004
7005/*
7006 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7007 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007009get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010{
7011 return (char_u *)command_complete[idx].name;
7012}
7013# endif /* FEAT_CMDL_COMPL */
7014
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007015/*
7016 * Parse address type argument
7017 */
7018 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007019parse_addr_type_arg(
7020 char_u *value,
7021 int vallen,
7022 long *argt,
7023 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007024{
7025 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007026
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007027 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7028 {
7029 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7030 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7031 if (a && b)
7032 {
7033 *addr_type_arg = addr_type_complete[i].expand;
7034 break;
7035 }
7036 }
7037
7038 if (addr_type_complete[i].expand == -1)
7039 {
7040 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007041
7042 for (i = 0; err[i] != NUL && !vim_iswhite(err[i]); i++)
7043 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007044 err[i] = NUL;
7045 EMSG2(_("E180: Invalid address type value: %s"), err);
7046 return FAIL;
7047 }
7048
7049 if (*addr_type_arg != ADDR_LINES)
7050 *argt |= NOTADR;
7051
7052 return OK;
7053}
7054
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055#endif /* FEAT_USR_CMDS */
7056
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007057#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7058/*
7059 * Parse a completion argument "value[vallen]".
7060 * The detected completion goes in "*complp", argument type in "*argt".
7061 * When there is an argument, for function and user defined completion, it's
7062 * copied to allocated memory and stored in "*compl_arg".
7063 * Returns FAIL if something is wrong.
7064 */
7065 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007066parse_compl_arg(
7067 char_u *value,
7068 int vallen,
7069 int *complp,
7070 long *argt,
7071 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007072{
7073 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007074# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007075 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007076# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007077 int i;
7078 int valend = vallen;
7079
7080 /* Look for any argument part - which is the part after any ',' */
7081 for (i = 0; i < vallen; ++i)
7082 {
7083 if (value[i] == ',')
7084 {
7085 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007086# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007087 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007088# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007089 valend = i;
7090 break;
7091 }
7092 }
7093
7094 for (i = 0; command_complete[i].expand != 0; ++i)
7095 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007096 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007097 && STRNCMP(value, command_complete[i].name, valend) == 0)
7098 {
7099 *complp = command_complete[i].expand;
7100 if (command_complete[i].expand == EXPAND_BUFFERS)
7101 *argt |= BUFNAME;
7102 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7103 || command_complete[i].expand == EXPAND_FILES)
7104 *argt |= XFILE;
7105 break;
7106 }
7107 }
7108
7109 if (command_complete[i].expand == 0)
7110 {
7111 EMSG2(_("E180: Invalid complete value: %s"), value);
7112 return FAIL;
7113 }
7114
7115# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7116 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7117 && arg != NULL)
7118# else
7119 if (arg != NULL)
7120# endif
7121 {
7122 EMSG(_("E468: Completion argument only allowed for custom completion"));
7123 return FAIL;
7124 }
7125
7126# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7127 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7128 && arg == NULL)
7129 {
7130 EMSG(_("E467: Custom completion requires a function argument"));
7131 return FAIL;
7132 }
7133
7134 if (arg != NULL)
7135 *compl_arg = vim_strnsave(arg, (int)arglen);
7136# endif
7137 return OK;
7138}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007139
7140 int
7141cmdcomplete_str_to_type(char_u *complete_str)
7142{
7143 int i;
7144
7145 for (i = 0; command_complete[i].expand != 0; ++i)
7146 if (STRCMP(complete_str, command_complete[i].name) == 0)
7147 return command_complete[i].expand;
7148
7149 return EXPAND_NOTHING;
7150}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007151#endif
7152
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007154ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155{
Bram Moolenaare6850792010-05-14 15:28:44 +02007156 if (*eap->arg == NUL)
7157 {
7158#ifdef FEAT_EVAL
7159 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7160 char_u *p = NULL;
7161
7162 if (expr != NULL)
7163 {
7164 ++emsg_off;
7165 p = eval_to_string(expr, NULL, FALSE);
7166 --emsg_off;
7167 vim_free(expr);
7168 }
7169 if (p != NULL)
7170 {
7171 MSG(p);
7172 vim_free(p);
7173 }
7174 else
7175 MSG("default");
7176#else
7177 MSG(_("unknown"));
7178#endif
7179 }
7180 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007181 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182}
7183
7184 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007185ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186{
7187 if (*eap->arg == NUL && eap->cmd[2] == '!')
7188 MSG(_("Greetings, Vim user!"));
7189 do_highlight(eap->arg, eap->forceit, FALSE);
7190}
7191
7192
7193/*
7194 * Call this function if we thought we were going to exit, but we won't
7195 * (because of an error). May need to restore the terminal mode.
7196 */
7197 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007198not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199{
7200 exiting = FALSE;
7201 settmode(TMODE_RAW);
7202}
7203
7204/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007205 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 */
7207 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007208ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209{
Bram Moolenaarf240e182014-11-27 18:33:02 +01007210#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007211 win_T *wp;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007212#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007213
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214#ifdef FEAT_CMDWIN
7215 if (cmdwin_type != 0)
7216 {
7217 cmdwin_result = Ctrl_C;
7218 return;
7219 }
7220#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007221 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007222 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007223 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007224 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007225 return;
7226 }
Bram Moolenaarf240e182014-11-27 18:33:02 +01007227#ifdef FEAT_WINDOWS
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007228 if (eap->addr_count > 0)
7229 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007230 int wnr = eap->line2;
7231
7232 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7233 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007234 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007235 }
7236 else
Bram Moolenaarf240e182014-11-27 18:33:02 +01007237#endif
7238#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007239 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007240#endif
7241
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007242#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02007243 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007244 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02007245 * being closed (can only happen in autocommands). */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007246 if (curbuf_locked() || (wp->w_buffer->b_nwindows == 1
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007247 && wp->w_buffer->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007248 return;
7249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250
7251#ifdef FEAT_NETBEANS_INTG
7252 netbeansForcedQuit = eap->forceit;
7253#endif
7254
7255 /*
7256 * If there are more files or windows we won't exit.
7257 */
7258 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7259 exiting = TRUE;
7260 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007261 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7262 | (eap->forceit ? CCGD_FORCEIT : 0)
7263 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007265 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 {
7267 not_exiting();
7268 }
7269 else
7270 {
7271#ifdef FEAT_WINDOWS
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007272 /* quit last window
7273 * Note: only_one_window() returns true, even so a help window is
7274 * still open. In that case only quit, if no address has been
7275 * specified. Example:
7276 * :h|wincmd w|1q - don't quit
7277 * :h|wincmd w|q - quit
7278 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007279 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280#endif
7281 getout(0);
7282#ifdef FEAT_WINDOWS
7283# ifdef FEAT_GUI
7284 need_mouse_correct = TRUE;
7285# endif
7286 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007287 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288#endif
7289 }
7290}
7291
7292/*
7293 * ":cquit".
7294 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007296ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297{
7298 getout(1); /* this does not always pass on the exit code to the Manx
7299 compiler. why? */
7300}
7301
7302/*
7303 * ":qall": try to quit all windows
7304 */
7305 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007306ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307{
7308# ifdef FEAT_CMDWIN
7309 if (cmdwin_type != 0)
7310 {
7311 if (eap->forceit)
7312 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7313 else
7314 cmdwin_result = K_XF2;
7315 return;
7316 }
7317# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007318
7319 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007320 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007321 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007322 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007323 return;
7324 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007325#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007326 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7327 /* Refuse to quit when locked or when the buffer in the last window is
7328 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007329 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007330 return;
7331#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007332
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007334 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 getout(0);
7336 not_exiting();
7337}
7338
Bram Moolenaard9967712006-03-11 21:18:15 +00007339#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340/*
7341 * ":close": close current window, unless it is the last one
7342 */
7343 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007344ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007346 win_T *win;
7347 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348# ifdef FEAT_CMDWIN
7349 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007350 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 else
7352# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007353 if (!text_locked()
7354#ifdef FEAT_AUTOCMD
7355 && !curbuf_locked()
7356#endif
7357 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007358 {
7359 if (eap->addr_count == 0)
7360 ex_win_close(eap->forceit, curwin, NULL);
7361 else {
Bram Moolenaar29323592016-07-24 22:04:11 +02007362 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007363 {
7364 winnr++;
7365 if (winnr == eap->line2)
7366 break;
7367 }
7368 if (win == NULL)
7369 win = lastwin;
7370 ex_win_close(eap->forceit, win, NULL);
7371 }
7372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373}
7374
Bram Moolenaard9967712006-03-11 21:18:15 +00007375# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007376/*
7377 * ":pclose": Close any preview window.
7378 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007380ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007381{
7382 win_T *win;
7383
Bram Moolenaar29323592016-07-24 22:04:11 +02007384 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007385 if (win->w_p_pvw)
7386 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007387 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007388 break;
7389 }
7390}
Bram Moolenaard9967712006-03-11 21:18:15 +00007391# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007392
Bram Moolenaarf740b292006-02-16 22:11:02 +00007393/*
7394 * Close window "win" and take care of handling closing the last window for a
7395 * modified buffer.
7396 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007397 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007398ex_win_close(
7399 int forceit,
7400 win_T *win,
7401 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402{
7403 int need_hide;
7404 buf_T *buf = win->w_buffer;
7405
7406 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007407 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007409# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 if ((p_confirm || cmdmod.confirm) && p_write)
7411 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007412 bufref_T bufref;
7413
7414 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007416 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 return;
7418 need_hide = FALSE;
7419 }
7420 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007421# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 {
7423 EMSG(_(e_nowrtmsg));
7424 return;
7425 }
7426 }
7427
Bram Moolenaard9967712006-03-11 21:18:15 +00007428# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007430# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007431
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007433 if (tp == NULL)
7434 win_close(win, !need_hide && !P_HID(buf));
7435 else
7436 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437}
7438
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007440 * ":tabclose": close current tab page, unless it is the last one.
7441 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 */
7443 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007444ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007446 tabpage_T *tp;
7447
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007448# ifdef FEAT_CMDWIN
7449 if (cmdwin_type != 0)
7450 cmdwin_result = K_IGNORE;
7451 else
7452# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007453 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007454 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007455 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007457 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007458 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007459 tp = find_tabpage((int)eap->line2);
7460 if (tp == NULL)
7461 {
7462 beep_flush();
7463 return;
7464 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007465 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007466 {
7467 tabpage_close_other(tp, eap->forceit);
7468 return;
7469 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007470 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007471 if (!text_locked()
7472#ifdef FEAT_AUTOCMD
7473 && !curbuf_locked()
7474#endif
7475 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00007476 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007478}
7479
7480/*
7481 * ":tabonly": close all tab pages except the current one
7482 */
7483 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007484ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007485{
7486 tabpage_T *tp;
7487 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007488
7489# ifdef FEAT_CMDWIN
7490 if (cmdwin_type != 0)
7491 cmdwin_result = K_IGNORE;
7492 else
7493# endif
7494 if (first_tabpage->tp_next == NULL)
7495 MSG(_("Already only one tab page"));
7496 else
7497 {
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007498 if (eap->addr_count > 0)
7499 goto_tabpage(eap->line2);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007500 /* Repeat this up to a 1000 times, because autocommands may mess
7501 * up the lists. */
7502 for (done = 0; done < 1000; ++done)
7503 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007504 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007505 if (tp->tp_topframe != topframe)
7506 {
7507 tabpage_close_other(tp, eap->forceit);
7508 /* if we failed to close it quit */
7509 if (valid_tabpage(tp))
7510 done = 1000;
7511 /* start over, "tp" is now invalid */
7512 break;
7513 }
7514 if (first_tabpage->tp_next == NULL)
7515 break;
7516 }
7517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519
7520/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007521 * Close the current tab page.
7522 */
7523 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007524tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007525{
7526 /* First close all the windows but the current one. If that worked then
7527 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007528 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007529 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007530 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007531 ex_win_close(forceit, curwin, NULL);
7532# ifdef FEAT_GUI
7533 need_mouse_correct = TRUE;
7534# endif
7535}
7536
7537/*
7538 * Close tab page "tp", which is not the current tab page.
7539 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007540 * Also takes care of the tab pages line disappearing when closing the
7541 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007542 */
7543 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007544tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007545{
7546 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007547 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007548 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007549
7550 /* Limit to 1000 windows, autocommands may add a window while we close
7551 * one. OK, so I'm paranoid... */
7552 while (++done < 1000)
7553 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007554 wp = tp->tp_firstwin;
7555 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007556
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007557 /* Autocommands may delete the tab page under our fingers and we may
7558 * fail to close a window with a modified buffer. */
7559 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007560 break;
7561 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007562
Bram Moolenaar29323592016-07-24 22:04:11 +02007563#ifdef FEAT_AUTOCMD
7564 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
7565#endif
7566
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007567 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007568 if (h != tabline_height())
7569 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007570}
7571
7572/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 * ":only".
7574 */
7575 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007576ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007578 win_T *wp;
7579 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580# ifdef FEAT_GUI
7581 need_mouse_correct = TRUE;
7582# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007583 if (eap->addr_count > 0)
7584 {
7585 wnr = eap->line2;
7586 for (wp = firstwin; --wnr > 0; )
7587 {
7588 if (wp->w_next == NULL)
7589 break;
7590 else
7591 wp = wp->w_next;
7592 }
7593 win_goto(wp);
7594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 close_others(TRUE, eap->forceit);
7596}
7597
7598/*
7599 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007600 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007602 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007603ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604{
7605 if (eap->addr_count == 0)
7606 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007607 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608}
7609#endif /* FEAT_WINDOWS */
7610
7611 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007612ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007614 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615#ifdef FEAT_WINDOWS
Bram Moolenaar2256c992016-11-15 21:17:07 +01007616 if (!eap->skip)
7617 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618# ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007619 need_mouse_correct = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620# endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007621 if (eap->addr_count == 0)
7622 win_close(curwin, FALSE); /* don't free buffer */
7623 else
7624 {
7625 int winnr = 0;
7626 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007627
Bram Moolenaar2256c992016-11-15 21:17:07 +01007628 FOR_ALL_WINDOWS(win)
7629 {
7630 winnr++;
7631 if (winnr == eap->line2)
7632 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007633 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007634 if (win == NULL)
7635 win = lastwin;
7636 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640}
7641
7642/*
7643 * ":stop" and ":suspend": Suspend Vim.
7644 */
7645 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007646ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647{
7648 /*
7649 * Disallow suspending for "rvim".
7650 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007651 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 {
7653 if (!eap->forceit)
7654 autowrite_all();
7655 windgoto((int)Rows - 1, 0);
7656 out_char('\n');
7657 out_flush();
7658 stoptermcap();
7659 out_flush(); /* needed for SUN to restore xterm buffer */
7660#ifdef FEAT_TITLE
7661 mch_restore_title(3); /* restore window titles */
7662#endif
7663 ui_suspend(); /* call machine specific function */
7664#ifdef FEAT_TITLE
7665 maketitle();
7666 resettitle(); /* force updating the title */
7667#endif
7668 starttermcap();
7669 scroll_start(); /* scroll screen before redrawing */
7670 redraw_later_clear();
7671 shell_resized(); /* may have resized window */
7672 }
7673}
7674
7675/*
7676 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7677 */
7678 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007679ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680{
7681#ifdef FEAT_CMDWIN
7682 if (cmdwin_type != 0)
7683 {
7684 cmdwin_result = Ctrl_C;
7685 return;
7686 }
7687#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007688 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007689 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007690 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007691 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007692 return;
7693 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007694#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007695 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7696 /* Refuse to quit when locked or when the buffer in the last window is
7697 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007698 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007699 return;
7700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701
7702 /*
7703 * if more files or windows we won't exit
7704 */
7705 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7706 exiting = TRUE;
7707 if ( ((eap->cmdidx == CMD_wq
7708 || curbufIsChanged())
7709 && do_write(eap) == FAIL)
7710 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007711 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 {
7713 not_exiting();
7714 }
7715 else
7716 {
7717#ifdef FEAT_WINDOWS
7718 if (only_one_window()) /* quit last window, exit Vim */
7719#endif
7720 getout(0);
7721#ifdef FEAT_WINDOWS
7722# ifdef FEAT_GUI
7723 need_mouse_correct = TRUE;
7724# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007725 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 win_close(curwin, !P_HID(curwin->w_buffer));
7727#endif
7728 }
7729}
7730
7731/*
7732 * ":print", ":list", ":number".
7733 */
7734 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007735ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007737 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7738 EMSG(_(e_emptybuf));
7739 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007741 for ( ;!got_int; ui_breakcheck())
7742 {
7743 print_line(eap->line1,
7744 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7745 || (eap->flags & EXFLAG_NR)),
7746 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7747 if (++eap->line1 > eap->line2)
7748 break;
7749 out_flush(); /* show one line at a time */
7750 }
7751 setpcmark();
7752 /* put cursor at last line */
7753 curwin->w_cursor.lnum = eap->line2;
7754 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 }
7756
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758}
7759
7760#ifdef FEAT_BYTEOFF
7761 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007762ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763{
7764 goto_byte(eap->line2);
7765}
7766#endif
7767
7768/*
7769 * ":shell".
7770 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007772ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773{
7774 do_shell(NULL, 0);
7775}
7776
7777#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7778 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007779 || defined(FEAT_GUI_MSWIN) \
7780 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 || defined(PROTO)
7782
7783/*
7784 * Handle a file drop. The code is here because a drop is *nearly* like an
7785 * :args command, but not quite (we have a list of exact filenames, so we
7786 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7787 * code is very similar to :args and hence needs access to a lot of the static
7788 * functions in this file.
7789 *
7790 * The list should be allocated using alloc(), as should each item in the
7791 * list. This function takes over responsibility for freeing the list.
7792 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007793 * XXX The list is made into the argument list. This is freed using
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007794 * FreeWild(), which does a series of vim_free() calls.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 */
7796 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007797handle_drop(
7798 int filec, /* the number of files dropped */
7799 char_u **filev, /* the list of files dropped */
7800 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801{
7802 exarg_T ea;
7803 int save_msg_scroll = msg_scroll;
7804
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007805 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007806 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007808#ifdef FEAT_AUTOCMD
7809 if (curbuf_locked())
7810 return;
7811#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007812 /* When the screen is being updated we should not change buffers and
7813 * windows structures, it may cause freed memory to be used. */
7814 if (updating_screen)
7815 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816
7817 /* Check whether the current buffer is changed. If so, we will need
7818 * to split the current window or data could be lost.
7819 * We don't need to check if the 'hidden' option is set, as in this
7820 * case the buffer won't be lost.
7821 */
7822 if (!P_HID(curbuf) && !split)
7823 {
7824 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007825 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 --emsg_off;
7827 }
7828 if (split)
7829 {
7830# ifdef FEAT_WINDOWS
7831 if (win_split(0, 0) == FAIL)
7832 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007833 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834
7835 /* When splitting the window, create a new alist. Otherwise the
7836 * existing one is overwritten. */
7837 alist_unlink(curwin->w_alist);
7838 alist_new();
7839# else
7840 return; /* can't split, always fail */
7841# endif
7842 }
7843
7844 /*
7845 * Set up the new argument list.
7846 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007847 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848
7849 /*
7850 * Move to the first file.
7851 */
7852 /* Fake up a minimal "next" command for do_argfile() */
7853 vim_memset(&ea, 0, sizeof(ea));
7854 ea.cmd = (char_u *)"next";
7855 do_argfile(&ea, 0);
7856
7857 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7858 * mode that is not needed here. */
7859 need_start_insertmode = FALSE;
7860
7861 /* Restore msg_scroll, otherwise a following command may cause scrolling
7862 * unexpectedly. The screen will be redrawn by the caller, thus
7863 * msg_scroll being set by displaying a message is irrelevant. */
7864 msg_scroll = save_msg_scroll;
7865}
7866#endif
7867
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868/*
7869 * Clear an argument list: free all file names and reset it to zero entries.
7870 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007871 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007872alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873{
7874 while (--al->al_ga.ga_len >= 0)
7875 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7876 ga_clear(&al->al_ga);
7877}
7878
7879/*
7880 * Init an argument list.
7881 */
7882 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007883alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884{
7885 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7886}
7887
7888#if defined(FEAT_WINDOWS) || defined(PROTO)
7889
7890/*
7891 * Remove a reference from an argument list.
7892 * Ignored when the argument list is the global one.
7893 * If the argument list is no longer used by any window, free it.
7894 */
7895 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007896alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897{
7898 if (al != &global_alist && --al->al_refcount <= 0)
7899 {
7900 alist_clear(al);
7901 vim_free(al);
7902 }
7903}
7904
7905# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7906/*
7907 * Create a new argument list and use it for the current window.
7908 */
7909 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007910alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911{
7912 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7913 if (curwin->w_alist == NULL)
7914 {
7915 curwin->w_alist = &global_alist;
7916 ++global_alist.al_refcount;
7917 }
7918 else
7919 {
7920 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007921 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 alist_init(curwin->w_alist);
7923 }
7924}
7925# endif
7926#endif
7927
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007928#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929/*
7930 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007931 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7932 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 */
7934 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007935alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936{
7937 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007938 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 char_u **new_arg_files;
7940 int new_arg_file_count;
7941 char_u *save_p_su = p_su;
7942 int i;
7943
7944 /* Don't use 'suffixes' here. This should work like the shell did the
7945 * expansion. Also, the vimrc file isn't read yet, thus the user
7946 * can't set the options. */
7947 p_su = empty_option;
7948 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7949 if (old_arg_files != NULL)
7950 {
7951 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007952 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7953 old_arg_count = GARGCOUNT;
7954 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007956 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 && new_arg_file_count > 0)
7958 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007959 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7960 TRUE, fnum_list, fnum_len);
7961 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 }
7964 p_su = save_p_su;
7965}
7966#endif
7967
7968/*
7969 * Set the argument list for the current window.
7970 * Takes over the allocated files[] and the allocated fnames in it.
7971 */
7972 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007973alist_set(
7974 alist_T *al,
7975 int count,
7976 char_u **files,
7977 int use_curbuf,
7978 int *fnum_list,
7979 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980{
7981 int i;
7982
7983 alist_clear(al);
7984 if (ga_grow(&al->al_ga, count) == OK)
7985 {
7986 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007987 {
7988 if (got_int)
7989 {
7990 /* When adding many buffers this can take a long time. Allow
7991 * interrupting here. */
7992 while (i < count)
7993 vim_free(files[i++]);
7994 break;
7995 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007996
7997 /* May set buffer name of a buffer previously used for the
7998 * argument list, so that it's re-used by alist_add. */
7999 if (fnum_list != NULL && i < fnum_len)
8000 buf_set_name(fnum_list[i], files[i]);
8001
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008003 ui_breakcheck();
8004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 vim_free(files);
8006 }
8007 else
8008 FreeWild(count, files);
8009#ifdef FEAT_WINDOWS
8010 if (al == &global_alist)
8011#endif
8012 arg_had_last = FALSE;
8013}
8014
8015/*
8016 * Add file "fname" to argument list "al".
8017 * "fname" must have been allocated and "al" must have been checked for room.
8018 */
8019 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008020alist_add(
8021 alist_T *al,
8022 char_u *fname,
8023 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024{
8025 if (fname == NULL) /* don't add NULL file names */
8026 return;
8027#ifdef BACKSLASH_IN_FILENAME
8028 slash_adjust(fname);
8029#endif
8030 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8031 if (set_fnum > 0)
8032 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8033 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8034 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035}
8036
8037#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8038/*
8039 * Adjust slashes in file names. Called after 'shellslash' was set.
8040 */
8041 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008042alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043{
8044 int i;
8045# ifdef FEAT_WINDOWS
8046 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008047 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048# endif
8049
8050 for (i = 0; i < GARGCOUNT; ++i)
8051 if (GARGLIST[i].ae_fname != NULL)
8052 slash_adjust(GARGLIST[i].ae_fname);
8053# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00008054 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 if (wp->w_alist != &global_alist)
8056 for (i = 0; i < WARGCOUNT(wp); ++i)
8057 if (WARGLIST(wp)[i].ae_fname != NULL)
8058 slash_adjust(WARGLIST(wp)[i].ae_fname);
8059# endif
8060}
8061#endif
8062
8063/*
8064 * ":preserve".
8065 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008067ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008069 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 ml_preserve(curbuf, TRUE);
8071}
8072
8073/*
8074 * ":recover".
8075 */
8076 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008077ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078{
8079 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8080 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008081 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8082 | CCGD_MULTWIN
8083 | (eap->forceit ? CCGD_FORCEIT : 0)
8084 | CCGD_EXCMD)
8085
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 && (*eap->arg == NUL
8087 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8088 ml_recover();
8089 recoverymode = FALSE;
8090}
8091
8092/*
8093 * Command modifier used in a wrong way.
8094 */
8095 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008096ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097{
8098 eap->errmsg = e_invcmd;
8099}
8100
8101#ifdef FEAT_WINDOWS
8102/*
8103 * :sview [+command] file split window with new file, read-only
8104 * :split [[+command] file] split window with current or new file
8105 * :vsplit [[+command] file] split window vertically with current or new file
8106 * :new [[+command] file] split window with no or new file
8107 * :vnew [[+command] file] split vertically window with no or new file
8108 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008109 *
8110 * :tabedit open new Tab page with empty window
8111 * :tabedit [+command] file open new Tab page and edit "file"
8112 * :tabnew [[+command] file] just like :tabedit
8113 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 */
8115 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008116ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008118 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008119# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008121# endif
8122# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008124# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008126# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008128# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008130# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008132 * quickfix windows. But it's OK when doing ":tab split". */
8133 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 {
8135 if (eap->cmdidx == CMD_split)
8136 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 if (eap->cmdidx == CMD_vsplit)
8138 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008140# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008142# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008143 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {
8145 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8146 FNAME_MESS, TRUE, curbuf->b_ffname);
8147 if (fname == NULL)
8148 goto theend;
8149 eap->arg = fname;
8150 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008151# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008153# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008155# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 && eap->cmdidx != CMD_new)
8159 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008160# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008161 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008162# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008163 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008164# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008165 au_has_group((char_u *)"FileExplorer"))
8166 {
8167 /* No browsing supported but we do have the file explorer:
8168 * Edit the directory. */
8169 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8170 eap->arg = (char_u *)".";
8171 }
8172 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008173# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008174 {
8175 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008177 if (fname == NULL)
8178 goto theend;
8179 eap->arg = fname;
8180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 }
8182 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008183# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008185 /*
8186 * Either open new tab page or split the window.
8187 */
8188 if (eap->cmdidx == CMD_tabedit
8189 || eap->cmdidx == CMD_tabfind
8190 || eap->cmdidx == CMD_tabnew)
8191 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008192 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8193 : eap->addr_count == 0 ? 0
8194 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008195 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008196 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008197
8198 /* set the alternate buffer for the window we came from */
8199 if (curwin != old_curwin
8200 && win_valid(old_curwin)
8201 && old_curwin->w_buffer != curbuf
8202 && !cmdmod.keepalt)
8203 old_curwin->w_alt_fnum = curbuf->b_fnum;
8204 }
8205 }
8206 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8208 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008209# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 /* Reset 'scrollbind' when editing another file, but keep it when
8211 * doing ":split" without arguments. */
8212 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008213# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008215# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008217 {
8218 RESET_BINDING(curwin);
8219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 else
8221 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008222# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 do_exedit(eap, old_curwin);
8224 }
8225
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008226# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008228# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008230# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231theend:
8232 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008233# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008235
8236/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008237 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008238 */
8239 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008240tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008241{
8242 exarg_T ea;
8243
8244 vim_memset(&ea, 0, sizeof(ea));
8245 ea.cmdidx = CMD_tabnew;
8246 ea.cmd = (char_u *)"tabn";
8247 ea.arg = (char_u *)"";
8248 ex_splitview(&ea);
8249}
8250
8251/*
8252 * :tabnext command
8253 */
8254 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008255ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008256{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008257 switch (eap->cmdidx)
8258 {
8259 case CMD_tabfirst:
8260 case CMD_tabrewind:
8261 goto_tabpage(1);
8262 break;
8263 case CMD_tablast:
8264 goto_tabpage(9999);
8265 break;
8266 case CMD_tabprevious:
8267 case CMD_tabNext:
8268 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
8269 break;
8270 default: /* CMD_tabnext */
8271 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
8272 break;
8273 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008274}
8275
8276/*
8277 * :tabmove command
8278 */
8279 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008280ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008281{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008282 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008283
8284 if (eap->arg && *eap->arg != NUL)
8285 {
8286 char_u *p = eap->arg;
8287 int relative = 0; /* argument +N/-N means: move N places to the
8288 * right/left relative to the current position. */
8289
8290 if (*eap->arg == '-')
8291 {
8292 relative = -1;
8293 p = eap->arg + 1;
8294 }
8295 else if (*eap->arg == '+')
8296 {
8297 relative = 1;
8298 p = eap->arg + 1;
8299 }
8300 else
8301 p = eap->arg;
8302
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008303 if (relative == 0)
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008304 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008305 if (STRCMP(p, "$") == 0)
8306 tab_number = LAST_TAB_NR;
8307 else if (p == skipdigits(p))
8308 {
8309 /* No numbers as argument. */
8310 eap->errmsg = e_invarg;
8311 return;
8312 }
8313 else
8314 tab_number = getdigits(&p);
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008315 }
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008316 else
8317 {
8318 if (*p != NUL)
8319 tab_number = getdigits(&p);
8320 else
8321 tab_number = 1;
8322 tab_number = tab_number * relative + tabpage_index(curtab);
8323 if (relative == -1)
8324 --tab_number;
8325 }
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008326 }
8327 else if (eap->addr_count != 0)
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008328 {
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008329 tab_number = eap->line2;
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008330 if (**eap->cmdlinep == '-')
8331 --tab_number;
8332 }
8333 else
8334 tab_number = LAST_TAB_NR;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008335
8336 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008337}
8338
8339/*
8340 * :tabs command: List tabs and their contents.
8341 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008342 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008343ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008344{
8345 tabpage_T *tp;
8346 win_T *wp;
8347 int tabcount = 1;
8348
8349 msg_start();
8350 msg_scroll = TRUE;
8351 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8352 {
8353 msg_putchar('\n');
8354 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
8355 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
8356 out_flush(); /* output one line at a time */
8357 ui_breakcheck();
8358
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008359 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008360 wp = firstwin;
8361 else
8362 wp = tp->tp_firstwin;
8363 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8364 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008365 msg_putchar('\n');
8366 msg_putchar(wp == curwin ? '>' : ' ');
8367 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008368 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8369 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008370 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008371 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008372 else
8373 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8374 IObuff, IOSIZE, TRUE);
8375 msg_outtrans(IObuff);
8376 out_flush(); /* output one line at a time */
8377 ui_breakcheck();
8378 }
8379 }
8380}
8381
8382#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383
8384/*
8385 * ":mode": Set screen mode.
8386 * If no argument given, just get the screen size and redraw.
8387 */
8388 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008389ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390{
8391 if (*eap->arg == NUL)
8392 shell_resized();
8393 else
8394 mch_screenmode(eap->arg);
8395}
8396
8397#ifdef FEAT_WINDOWS
8398/*
8399 * ":resize".
8400 * set, increment or decrement current window height
8401 */
8402 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008403ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404{
8405 int n;
8406 win_T *wp = curwin;
8407
8408 if (eap->addr_count > 0)
8409 {
8410 n = eap->line2;
8411 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8412 ;
8413 }
8414
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008415# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008417# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 if (cmdmod.split & WSP_VERT)
8420 {
8421 if (*eap->arg == '-' || *eap->arg == '+')
8422 n += W_WIDTH(curwin);
8423 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8424 n = 9999;
8425 win_setwidth_win((int)n, wp);
8426 }
8427 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 {
8429 if (*eap->arg == '-' || *eap->arg == '+')
8430 n += curwin->w_height;
8431 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8432 n = 9999;
8433 win_setheight_win((int)n, wp);
8434 }
8435}
8436#endif
8437
8438/*
8439 * ":find [+command] <file>" command.
8440 */
8441 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008442ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443{
8444#ifdef FEAT_SEARCHPATH
8445 char_u *fname;
8446 int count;
8447
8448 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8449 TRUE, curbuf->b_ffname);
8450 if (eap->addr_count > 0)
8451 {
8452 /* Repeat finding the file "count" times. This matters when it
8453 * appears several times in the path. */
8454 count = eap->line2;
8455 while (fname != NULL && --count > 0)
8456 {
8457 vim_free(fname);
8458 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8459 FALSE, curbuf->b_ffname);
8460 }
8461 }
8462
8463 if (fname != NULL)
8464 {
8465 eap->arg = fname;
8466#endif
8467 do_exedit(eap, NULL);
8468#ifdef FEAT_SEARCHPATH
8469 vim_free(fname);
8470 }
8471#endif
8472}
8473
8474/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008475 * ":open" simulation: for now just work like ":visual".
8476 */
8477 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008478ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008479{
8480 regmatch_T regmatch;
8481 char_u *p;
8482
8483 curwin->w_cursor.lnum = eap->line2;
8484 beginline(BL_SOL | BL_FIX);
8485 if (*eap->arg == '/')
8486 {
8487 /* ":open /pattern/": put cursor in column found with pattern */
8488 ++eap->arg;
8489 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8490 *p = NUL;
8491 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8492 if (regmatch.regprog != NULL)
8493 {
8494 regmatch.rm_ic = p_ic;
8495 p = ml_get_curline();
8496 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008497 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008498 else
8499 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008500 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008501 }
8502 /* Move to the NUL, ignore any other arguments. */
8503 eap->arg += STRLEN(eap->arg);
8504 }
8505 check_cursor();
8506
8507 eap->cmdidx = CMD_visual;
8508 do_exedit(eap, NULL);
8509}
8510
8511/*
8512 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 */
8514 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008515ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516{
8517 do_exedit(eap, NULL);
8518}
8519
8520/*
8521 * ":edit <file>" command and alikes.
8522 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008524do_exedit(
8525 exarg_T *eap,
8526 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527{
8528 int n;
8529#ifdef FEAT_WINDOWS
8530 int need_hide;
8531#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008532 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533
8534 /*
8535 * ":vi" command ends Ex mode.
8536 */
8537 if (exmode_active && (eap->cmdidx == CMD_visual
8538 || eap->cmdidx == CMD_view))
8539 {
8540 exmode_active = FALSE;
8541 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008542 {
8543 /* Special case: ":global/pat/visual\NLvi-commands" */
8544 if (global_busy)
8545 {
8546 int rd = RedrawingDisabled;
8547 int nwr = no_wait_return;
8548 int ms = msg_scroll;
8549#ifdef FEAT_GUI
8550 int he = hold_gui_events;
8551#endif
8552
8553 if (eap->nextcmd != NULL)
8554 {
8555 stuffReadbuff(eap->nextcmd);
8556 eap->nextcmd = NULL;
8557 }
8558
8559 if (exmode_was != EXMODE_VIM)
8560 settmode(TMODE_RAW);
8561 RedrawingDisabled = 0;
8562 no_wait_return = 0;
8563 need_wait_return = FALSE;
8564 msg_scroll = 0;
8565#ifdef FEAT_GUI
8566 hold_gui_events = 0;
8567#endif
8568 must_redraw = CLEAR;
8569
8570 main_loop(FALSE, TRUE);
8571
8572 RedrawingDisabled = rd;
8573 no_wait_return = nwr;
8574 msg_scroll = ms;
8575#ifdef FEAT_GUI
8576 hold_gui_events = he;
8577#endif
8578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 }
8582
8583 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008584 || eap->cmdidx == CMD_tabnew
8585 || eap->cmdidx == CMD_tabedit
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008586#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 || eap->cmdidx == CMD_vnew
8588#endif
8589 ) && *eap->arg == NUL)
8590 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008591 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 setpcmark();
8593 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008594 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8595 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 }
8597 else if ((eap->cmdidx != CMD_split
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008598#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 && eap->cmdidx != CMD_vsplit
8600#endif
8601 )
8602 || *eap->arg != NUL
8603#ifdef FEAT_BROWSE
8604 || cmdmod.browse
8605#endif
8606 )
8607 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008608#ifdef FEAT_AUTOCMD
8609 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8610 * can bring us here, others are stopped earlier. */
8611 if (*eap->arg != NUL && curbuf_locked())
8612 return;
8613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 n = readonlymode;
8615 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8616 readonlymode = TRUE;
8617 else if (eap->cmdidx == CMD_enew)
8618 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8619 empty buffer */
8620 setpcmark();
8621 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8622 NULL, eap,
8623 /* ":edit" goes to first line if Vi compatible */
8624 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8625 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8626 ? ECMD_ONE : eap->do_ecmd_lnum,
8627 (P_HID(curbuf) ? ECMD_HIDE : 0)
8628 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008629 /* after a split we can use an existing buffer */
8630 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631#ifdef FEAT_LISTCMDS
8632 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8633#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008634 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 {
8636 /* Editing the file failed. If the window was split, close it. */
8637#ifdef FEAT_WINDOWS
8638 if (old_curwin != NULL)
8639 {
8640 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8641 if (!need_hide || P_HID(curbuf))
8642 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008643# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8644 cleanup_T cs;
8645
8646 /* Reset the error/interrupt/exception state here so that
8647 * aborting() returns FALSE when closing a window. */
8648 enter_cleanup(&cs);
8649# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650# ifdef FEAT_GUI
8651 need_mouse_correct = TRUE;
8652# endif
8653 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008654
8655# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8656 /* Restore the error/interrupt/exception state if not
8657 * discarded by a new aborting error, interrupt, or
8658 * uncaught exception. */
8659 leave_cleanup(&cs);
8660# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 }
8662 }
8663#endif
8664 }
8665 else if (readonlymode && curbuf->b_nwindows == 1)
8666 {
8667 /* When editing an already visited buffer, 'readonly' won't be set
8668 * but the previous value is kept. With ":view" and ":sview" we
8669 * want the file to be readonly, except when another window is
8670 * editing the same buffer. */
8671 curbuf->b_p_ro = TRUE;
8672 }
8673 readonlymode = n;
8674 }
8675 else
8676 {
8677 if (eap->do_ecmd_cmd != NULL)
8678 do_cmdline_cmd(eap->do_ecmd_cmd);
8679#ifdef FEAT_TITLE
8680 n = curwin->w_arg_idx_invalid;
8681#endif
8682 check_arg_idx(curwin);
8683#ifdef FEAT_TITLE
8684 if (n != curwin->w_arg_idx_invalid)
8685 maketitle();
8686#endif
8687 }
8688
8689#ifdef FEAT_WINDOWS
8690 /*
8691 * if ":split file" worked, set alternate file name in old window to new
8692 * file
8693 */
8694 if (old_curwin != NULL
8695 && *eap->arg != NUL
8696 && curwin != old_curwin
8697 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008698 && old_curwin->w_buffer != curbuf
8699 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 old_curwin->w_alt_fnum = curbuf->b_fnum;
8701#endif
8702
8703 ex_no_reprint = TRUE;
8704}
8705
8706#ifndef FEAT_GUI
8707/*
8708 * ":gui" and ":gvim" when there is no GUI.
8709 */
8710 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008711ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712{
8713 eap->errmsg = e_nogvim;
8714}
8715#endif
8716
8717#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8718 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008719ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720{
8721 gui_make_tearoff(eap->arg);
8722}
8723#endif
8724
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008725#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008727ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008729 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730}
8731#endif
8732
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008734ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735{
8736 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8737 MSG(_("No swap file"));
8738 else
8739 msg(curbuf->b_ml.ml_mfp->mf_fname);
8740}
8741
8742/*
8743 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8744 * offset.
8745 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8746 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008748ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749{
8750#ifdef FEAT_SCROLLBIND
8751 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008752 win_T *save_curwin = curwin;
8753 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 long topline;
8755 long y;
8756 linenr_T old_linenr = curwin->w_cursor.lnum;
8757
8758 setpcmark();
8759
8760 /*
8761 * determine max topline
8762 */
8763 if (curwin->w_p_scb)
8764 {
8765 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008766 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 {
8768 if (wp->w_p_scb && wp->w_buffer)
8769 {
8770 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8771 if (topline > y)
8772 topline = y;
8773 }
8774 }
8775 if (topline < 1)
8776 topline = 1;
8777 }
8778 else
8779 {
8780 topline = 1;
8781 }
8782
8783
8784 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008785 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008787 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 {
8789 if (curwin->w_p_scb)
8790 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008791 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 y = topline - curwin->w_topline;
8793 if (y > 0)
8794 scrollup(y, TRUE);
8795 else
8796 scrolldown(-y, TRUE);
8797 curwin->w_scbind_pos = topline;
8798 redraw_later(VALID);
8799 cursor_correct();
8800#ifdef FEAT_WINDOWS
8801 curwin->w_redr_status = TRUE;
8802#endif
8803 }
8804 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008805 curwin = save_curwin;
8806 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 if (curwin->w_p_scb)
8808 {
8809 did_syncbind = TRUE;
8810 checkpcmark();
8811 if (old_linenr != curwin->w_cursor.lnum)
8812 {
8813 char_u ctrl_o[2];
8814
8815 ctrl_o[0] = Ctrl_O;
8816 ctrl_o[1] = 0;
8817 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8818 }
8819 }
8820#endif
8821}
8822
8823
8824 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008825ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008827 int i;
8828 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8829 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830
8831 if (eap->usefilter) /* :r!cmd */
8832 do_bang(1, eap, FALSE, FALSE, TRUE);
8833 else
8834 {
8835 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8836 return;
8837
8838#ifdef FEAT_BROWSE
8839 if (cmdmod.browse)
8840 {
8841 char_u *browseFile;
8842
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008843 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 NULL, NULL, NULL, curbuf);
8845 if (browseFile != NULL)
8846 {
8847 i = readfile(browseFile, NULL,
8848 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8849 vim_free(browseFile);
8850 }
8851 else
8852 i = OK;
8853 }
8854 else
8855#endif
8856 if (*eap->arg == NUL)
8857 {
8858 if (check_fname() == FAIL) /* check for no file name */
8859 return;
8860 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8861 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8862 }
8863 else
8864 {
8865 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8866 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8867 i = readfile(eap->arg, NULL,
8868 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8869
8870 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01008871 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 {
8873#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8874 if (!aborting())
8875#endif
8876 EMSG2(_(e_notopen), eap->arg);
8877 }
8878 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008879 {
8880 if (empty && exmode_active)
8881 {
8882 /* Delete the empty line that remains. Historically ex does
8883 * this but vi doesn't. */
8884 if (eap->line2 == 0)
8885 lnum = curbuf->b_ml.ml_line_count;
8886 else
8887 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008888 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008889 {
8890 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008891 if (curwin->w_cursor.lnum > 1
8892 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008893 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008894 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008895 }
8896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 }
8900}
8901
Bram Moolenaarea408852005-06-25 22:49:46 +00008902static char_u *prev_dir = NULL;
8903
8904#if defined(EXITFREE) || defined(PROTO)
8905 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008906free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00008907{
8908 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008909 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008910
8911 vim_free(globaldir);
8912 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008913}
8914#endif
8915
Bram Moolenaarf4258302013-06-02 18:20:17 +02008916/*
8917 * Deal with the side effects of changing the current directory.
8918 * When "local" is TRUE then this was after an ":lcd" command.
8919 */
8920 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008921post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008922{
8923 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008924 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008925 if (local)
8926 {
8927 /* If still in global directory, need to remember current
8928 * directory as global directory. */
8929 if (globaldir == NULL && prev_dir != NULL)
8930 globaldir = vim_strsave(prev_dir);
8931 /* Remember this local directory for the window. */
8932 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8933 curwin->w_localdir = vim_strsave(NameBuff);
8934 }
8935 else
8936 {
8937 /* We are now in the global directory, no need to remember its
8938 * name. */
8939 vim_free(globaldir);
8940 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008941 }
8942
8943 shorten_fnames(TRUE);
8944}
8945
Bram Moolenaarea408852005-06-25 22:49:46 +00008946
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947/*
8948 * ":cd", ":lcd", ":chdir" and ":lchdir".
8949 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008950 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008951ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 char_u *new_dir;
8954 char_u *tofree;
8955
8956 new_dir = eap->arg;
8957#if !defined(UNIX) && !defined(VMS)
8958 /* for non-UNIX ":cd" means: print current directory */
8959 if (*new_dir == NUL)
8960 ex_pwd(NULL);
8961 else
8962#endif
8963 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008964#ifdef FEAT_AUTOCMD
8965 if (allbuf_locked())
8966 return;
8967#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008968 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8969 && !eap->forceit)
8970 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008971 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008972 return;
8973 }
8974
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 /* ":cd -": Change to previous directory */
8976 if (STRCMP(new_dir, "-") == 0)
8977 {
8978 if (prev_dir == NULL)
8979 {
8980 EMSG(_("E186: No previous directory"));
8981 return;
8982 }
8983 new_dir = prev_dir;
8984 }
8985
8986 /* Save current directory for next ":cd -" */
8987 tofree = prev_dir;
8988 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8989 prev_dir = vim_strsave(NameBuff);
8990 else
8991 prev_dir = NULL;
8992
8993#if defined(UNIX) || defined(VMS)
8994 /* for UNIX ":cd" means: go to home directory */
8995 if (*new_dir == NUL)
8996 {
8997 /* use NameBuff for home directory name */
8998# ifdef VMS
8999 char_u *p;
9000
9001 p = mch_getenv((char_u *)"SYS$LOGIN");
9002 if (p == NULL || *p == NUL) /* empty is the same as not set */
9003 NameBuff[0] = NUL;
9004 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009005 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006# else
9007 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9008# endif
9009 new_dir = NameBuff;
9010 }
9011#endif
9012 if (new_dir == NULL || vim_chdir(new_dir))
9013 EMSG(_(e_failed));
9014 else
9015 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02009016 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017
9018 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009019 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 ex_pwd(eap);
9021 }
9022 vim_free(tofree);
9023 }
9024}
9025
9026/*
9027 * ":pwd".
9028 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009030ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031{
9032 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9033 {
9034#ifdef BACKSLASH_IN_FILENAME
9035 slash_adjust(NameBuff);
9036#endif
9037 msg(NameBuff);
9038 }
9039 else
9040 EMSG(_("E187: Unknown"));
9041}
9042
9043/*
9044 * ":=".
9045 */
9046 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009047ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048{
9049 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009050 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051}
9052
9053 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009054ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009056 int n;
9057 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058
9059 if (cursor_valid())
9060 {
9061 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9062 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01009063 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009065
9066 len = eap->line2;
9067 switch (*eap->arg)
9068 {
9069 case 'm': break;
9070 case NUL: len *= 1000L; break;
9071 default: EMSG2(_(e_invarg2), eap->arg); return;
9072 }
9073 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074}
9075
9076/*
9077 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9078 */
9079 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009080do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081{
9082 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009083 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084
9085 cursor_on();
9086 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009087 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009089 wait_now = msec - done > 1000L ? 1000L : msec - done;
9090#ifdef FEAT_TIMERS
9091 {
9092 long due_time = check_due_timer();
9093
9094 if (due_time > 0 && due_time < wait_now)
9095 wait_now = due_time;
9096 }
9097#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009098#ifdef FEAT_JOB_CHANNEL
9099 if (has_any_channel() && wait_now > 100L)
9100 wait_now = 100L;
9101#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009102 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009103#ifdef FEAT_JOB_CHANNEL
9104 if (has_any_channel())
9105 ui_breakcheck_force(TRUE);
9106 else
9107#endif
9108 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009109#ifdef MESSAGE_QUEUE
9110 /* Process the netbeans and clientserver messages that may have been
9111 * received in the call to ui_breakcheck() when the GUI is in use. This
9112 * may occur when running a test case. */
9113 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 }
9116}
9117
9118 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009119do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120{
9121 int mode;
9122 char_u *cmdp;
9123
9124 cmdp = eap->cmd;
9125 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9126
9127 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9128 eap->arg, mode, isabbrev))
9129 {
9130 case 1: EMSG(_(e_invarg));
9131 break;
9132 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9133 break;
9134 }
9135}
9136
9137/*
9138 * ":winsize" command (obsolete).
9139 */
9140 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009141ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142{
9143 int w, h;
9144 char_u *arg = eap->arg;
9145 char_u *p;
9146
9147 w = getdigits(&arg);
9148 arg = skipwhite(arg);
9149 p = arg;
9150 h = getdigits(&arg);
9151 if (*p != NUL && *arg == NUL)
9152 set_shellsize(w, h, TRUE);
9153 else
9154 EMSG(_("E465: :winsize requires two number arguments"));
9155}
9156
9157#ifdef FEAT_WINDOWS
9158 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009159ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160{
9161 int xchar = NUL;
9162 char_u *p;
9163
9164 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9165 {
9166 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9167 if (eap->arg[1] == NUL)
9168 {
9169 EMSG(_(e_invarg));
9170 return;
9171 }
9172 xchar = eap->arg[1];
9173 p = eap->arg + 2;
9174 }
9175 else
9176 p = eap->arg + 1;
9177
9178 eap->nextcmd = check_nextcmd(p);
9179 p = skipwhite(p);
9180 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9181 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009182 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 {
9184 /* Pass flags on for ":vertical wincmd ]". */
9185 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009186 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9188 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009189 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 }
9191}
9192#endif
9193
Bram Moolenaar843ee412004-06-30 16:16:41 +00009194#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195/*
9196 * ":winpos".
9197 */
9198 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009199ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200{
9201 int x, y;
9202 char_u *arg = eap->arg;
9203 char_u *p;
9204
9205 if (*arg == NUL)
9206 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009207# if defined(FEAT_GUI) || defined(MSWIN)
9208# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009210# else
9211 if (mch_get_winpos(&x, &y) != FAIL)
9212# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213 {
9214 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9215 msg(IObuff);
9216 }
9217 else
9218# endif
9219 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9220 }
9221 else
9222 {
9223 x = getdigits(&arg);
9224 arg = skipwhite(arg);
9225 p = arg;
9226 y = getdigits(&arg);
9227 if (*p == NUL || *arg != NUL)
9228 {
9229 EMSG(_("E466: :winpos requires two number arguments"));
9230 return;
9231 }
9232# ifdef FEAT_GUI
9233 if (gui.in_use)
9234 gui_mch_set_winpos(x, y);
9235 else if (gui.starting)
9236 {
9237 /* Remember the coordinates for when the window is opened. */
9238 gui_win_x = x;
9239 gui_win_y = y;
9240 }
9241# ifdef HAVE_TGETENT
9242 else
9243# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009244# else
9245# ifdef MSWIN
9246 mch_set_winpos(x, y);
9247# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248# endif
9249# ifdef HAVE_TGETENT
9250 if (*T_CWP)
9251 term_set_winpos(x, y);
9252# endif
9253 }
9254}
9255#endif
9256
9257/*
9258 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9259 */
9260 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009261ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262{
9263 oparg_T oa;
9264
9265 clear_oparg(&oa);
9266 oa.regname = eap->regname;
9267 oa.start.lnum = eap->line1;
9268 oa.end.lnum = eap->line2;
9269 oa.line_count = eap->line2 - eap->line1 + 1;
9270 oa.motion_type = MLINE;
9271#ifdef FEAT_VIRTUALEDIT
9272 virtual_op = FALSE;
9273#endif
9274 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9275 {
9276 setpcmark();
9277 curwin->w_cursor.lnum = eap->line1;
9278 beginline(BL_SOL | BL_FIX);
9279 }
9280
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009281 if (VIsual_active)
9282 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009283
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284 switch (eap->cmdidx)
9285 {
9286 case CMD_delete:
9287 oa.op_type = OP_DELETE;
9288 op_delete(&oa);
9289 break;
9290
9291 case CMD_yank:
9292 oa.op_type = OP_YANK;
9293 (void)op_yank(&oa, FALSE, TRUE);
9294 break;
9295
9296 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009297 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009299 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9300#else
9301 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009303 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 oa.op_type = OP_RSHIFT;
9305 else
9306 oa.op_type = OP_LSHIFT;
9307 op_shift(&oa, FALSE, eap->amount);
9308 break;
9309 }
9310#ifdef FEAT_VIRTUALEDIT
9311 virtual_op = MAYBE;
9312#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009313 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314}
9315
9316/*
9317 * ":put".
9318 */
9319 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009320ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321{
9322 /* ":0put" works like ":1put!". */
9323 if (eap->line2 == 0)
9324 {
9325 eap->line2 = 1;
9326 eap->forceit = TRUE;
9327 }
9328 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009329 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9330 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331}
9332
9333/*
9334 * Handle ":copy" and ":move".
9335 */
9336 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009337ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338{
9339 long n;
9340
Bram Moolenaarded27822017-01-02 14:27:34 +01009341 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342 if (eap->arg == NULL) /* error detected */
9343 {
9344 eap->nextcmd = NULL;
9345 return;
9346 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009347 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348
9349 /*
9350 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9351 */
9352 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9353 {
9354 EMSG(_(e_invaddr));
9355 return;
9356 }
9357
9358 if (eap->cmdidx == CMD_move)
9359 {
9360 if (do_move(eap->line1, eap->line2, n) == FAIL)
9361 return;
9362 }
9363 else
9364 ex_copy(eap->line1, eap->line2, n);
9365 u_clearline();
9366 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009367 ex_may_print(eap);
9368}
9369
9370/*
9371 * Print the current line if flags were given to the Ex command.
9372 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009373 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009374ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009375{
9376 if (eap->flags != 0)
9377 {
9378 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9379 (eap->flags & EXFLAG_LIST));
9380 ex_no_reprint = TRUE;
9381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382}
9383
9384/*
9385 * ":smagic" and ":snomagic".
9386 */
9387 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009388ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389{
9390 int magic_save = p_magic;
9391
9392 p_magic = (eap->cmdidx == CMD_smagic);
9393 do_sub(eap);
9394 p_magic = magic_save;
9395}
9396
9397/*
9398 * ":join".
9399 */
9400 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009401ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402{
9403 curwin->w_cursor.lnum = eap->line1;
9404 if (eap->line1 == eap->line2)
9405 {
9406 if (eap->addr_count >= 2) /* :2,2join does nothing */
9407 return;
9408 if (eap->line2 == curbuf->b_ml.ml_line_count)
9409 {
9410 beep_flush();
9411 return;
9412 }
9413 ++eap->line2;
9414 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009415 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009417 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418}
9419
9420/*
9421 * ":[addr]@r" or ":[addr]*r": execute register
9422 */
9423 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009424ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425{
9426 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009427 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428
9429 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009430 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431
9432#ifdef USE_ON_FLY_SCROLL
9433 dont_scroll = TRUE; /* disallow scrolling here */
9434#endif
9435
9436 /* get the register name. No name means to use the previous one */
9437 c = *eap->arg;
9438 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9439 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009440 /* Put the register in the typeahead buffer with the "silent" flag. */
9441 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9442 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009443 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 else
9447 {
9448 int save_efr = exec_from_reg;
9449
9450 exec_from_reg = TRUE;
9451
9452 /*
9453 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009454 * Continue until the stuff buffer is empty and all added characters
9455 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009457 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9459
9460 exec_from_reg = save_efr;
9461 }
9462}
9463
9464/*
9465 * ":!".
9466 */
9467 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009468ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469{
9470 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9471}
9472
9473/*
9474 * ":undo".
9475 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009477ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009479 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009480 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009481 else
9482 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483}
9484
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009485#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009486 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009487ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009488{
9489 char_u hash[UNDO_HASH_SIZE];
9490
9491 u_compute_hash(hash);
9492 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9493}
9494
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009495 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009496ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009497{
9498 char_u hash[UNDO_HASH_SIZE];
9499
9500 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009501 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009502}
9503#endif
9504
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505/*
9506 * ":redo".
9507 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009509ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510{
9511 u_redo(1);
9512}
9513
9514/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009515 * ":earlier" and ":later".
9516 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009517 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009518ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009519{
9520 long count = 0;
9521 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009522 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009523 char_u *p = eap->arg;
9524
9525 if (*p == NUL)
9526 count = 1;
9527 else if (isdigit(*p))
9528 {
9529 count = getdigits(&p);
9530 switch (*p)
9531 {
9532 case 's': ++p; sec = TRUE; break;
9533 case 'm': ++p; sec = TRUE; count *= 60; break;
9534 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009535 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9536 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009537 }
9538 }
9539
9540 if (*p != NUL)
9541 EMSG2(_(e_invarg2), eap->arg);
9542 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009543 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9544 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009545}
9546
9547/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548 * ":redir": start/stop redirection.
9549 */
9550 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009551ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552{
9553 char *mode;
9554 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009555 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556
Bram Moolenaarba768492016-07-08 15:32:54 +02009557#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009558 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009559 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009560 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009561 return;
9562 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009563#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009564
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 if (STRICMP(eap->arg, "END") == 0)
9566 close_redir();
9567 else
9568 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009569 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009571 ++arg;
9572 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009574 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575 mode = "a";
9576 }
9577 else
9578 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009579 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580
9581 close_redir();
9582
9583 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009584 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 if (fname == NULL)
9586 return;
9587#ifdef FEAT_BROWSE
9588 if (cmdmod.browse)
9589 {
9590 char_u *browseFile;
9591
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009592 browseFile = do_browse(BROWSE_SAVE,
9593 (char_u *)_("Save Redirection"),
9594 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 if (browseFile == NULL)
9596 return; /* operation cancelled */
9597 vim_free(fname);
9598 fname = browseFile;
9599 eap->forceit = TRUE; /* since dialog already asked */
9600 }
9601#endif
9602
9603 redir_fd = open_exfile(fname, eap->forceit, mode);
9604 vim_free(fname);
9605 }
9606#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009607 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 {
9609 /* redirect to a register a-z (resp. A-Z for appending) */
9610 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009611 ++arg;
9612 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009614 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009615 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009617 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009619 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009620 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009621 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009622 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009624 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009625 if (*arg == '>')
9626 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009627 /* Make register empty when not using @A-@Z and the
9628 * command is valid. */
9629 if (*arg == NUL && !isupper(redir_reg))
9630 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009632 }
9633 if (*arg != NUL)
9634 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009635 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009636 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009637 }
9638 }
9639 else if (*arg == '=' && arg[1] == '>')
9640 {
9641 int append;
9642
9643 /* redirect to a variable */
9644 close_redir();
9645 arg += 2;
9646
9647 if (*arg == '>')
9648 {
9649 ++arg;
9650 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 }
9652 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009653 append = FALSE;
9654
9655 if (var_redir_start(skipwhite(arg), append) == OK)
9656 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 }
9658#endif
9659
9660 /* TODO: redirect to a buffer */
9661
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009663 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009665
9666 /* Make sure redirection is not off. Can happen for cmdline completion
9667 * that indirectly invokes a command to catch its output. */
9668 if (redir_fd != NULL
9669#ifdef FEAT_EVAL
9670 || redir_reg || redir_vname
9671#endif
9672 )
9673 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674}
9675
9676/*
9677 * ":redraw": force redraw
9678 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009679 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009680ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681{
9682 int r = RedrawingDisabled;
9683 int p = p_lz;
9684
9685 RedrawingDisabled = 0;
9686 p_lz = FALSE;
9687 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009688 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689#ifdef FEAT_TITLE
9690 if (need_maketitle)
9691 maketitle();
9692#endif
9693 RedrawingDisabled = r;
9694 p_lz = p;
9695
9696 /* Reset msg_didout, so that a message that's there is overwritten. */
9697 msg_didout = FALSE;
9698 msg_col = 0;
9699
Bram Moolenaar56667a52013-07-17 11:54:28 +02009700 /* No need to wait after an intentional redraw. */
9701 need_wait_return = FALSE;
9702
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 out_flush();
9704}
9705
9706/*
9707 * ":redrawstatus": force redraw of status line(s)
9708 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009710ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711{
9712#if defined(FEAT_WINDOWS)
9713 int r = RedrawingDisabled;
9714 int p = p_lz;
9715
9716 RedrawingDisabled = 0;
9717 p_lz = FALSE;
9718 if (eap->forceit)
9719 status_redraw_all();
9720 else
9721 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009722 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723 RedrawingDisabled = r;
9724 p_lz = p;
9725 out_flush();
9726#endif
9727}
9728
9729 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009730close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731{
9732 if (redir_fd != NULL)
9733 {
9734 fclose(redir_fd);
9735 redir_fd = NULL;
9736 }
9737#ifdef FEAT_EVAL
9738 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009739 if (redir_vname)
9740 {
9741 var_redir_stop();
9742 redir_vname = 0;
9743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744#endif
9745}
9746
9747#if defined(FEAT_SESSION) && defined(USE_CRNL)
9748# define MKSESSION_NL
9749static int mksession_nl = FALSE; /* use NL only in put_eol() */
9750#endif
9751
9752/*
9753 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9754 */
9755 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009756ex_mkrc(
9757 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758{
9759 FILE *fd;
9760 int failed = FALSE;
9761 char_u *fname;
9762#ifdef FEAT_BROWSE
9763 char_u *browseFile = NULL;
9764#endif
9765#ifdef FEAT_SESSION
9766 int view_session = FALSE;
9767 int using_vdir = FALSE; /* using 'viewdir'? */
9768 char_u *viewFile = NULL;
9769 unsigned *flagp;
9770#endif
9771
9772 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9773 {
9774#ifdef FEAT_SESSION
9775 view_session = TRUE;
9776#else
9777 ex_ni(eap);
9778 return;
9779#endif
9780 }
9781
9782#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009783 /* Use the short file name until ":lcd" is used. We also don't use the
9784 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009785 did_lcd = FALSE;
9786
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9788 if (eap->cmdidx == CMD_mkview
9789 && (*eap->arg == NUL
9790 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9791 {
9792 eap->forceit = TRUE;
9793 fname = get_view_file(*eap->arg);
9794 if (fname == NULL)
9795 return;
9796 viewFile = fname;
9797 using_vdir = TRUE;
9798 }
9799 else
9800#endif
9801 if (*eap->arg != NUL)
9802 fname = eap->arg;
9803 else if (eap->cmdidx == CMD_mkvimrc)
9804 fname = (char_u *)VIMRC_FILE;
9805#ifdef FEAT_SESSION
9806 else if (eap->cmdidx == CMD_mksession)
9807 fname = (char_u *)SESSION_FILE;
9808#endif
9809 else
9810 fname = (char_u *)EXRC_FILE;
9811
9812#ifdef FEAT_BROWSE
9813 if (cmdmod.browse)
9814 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009815 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816# ifdef FEAT_SESSION
9817 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9818 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9819# endif
9820 (char_u *)_("Save Setup"),
9821 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9822 if (browseFile == NULL)
9823 goto theend;
9824 fname = browseFile;
9825 eap->forceit = TRUE; /* since dialog already asked */
9826 }
9827#endif
9828
9829#if defined(FEAT_SESSION) && defined(vim_mkdir)
9830 /* When using 'viewdir' may have to create the directory. */
9831 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009832 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833#endif
9834
9835 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9836 if (fd != NULL)
9837 {
9838#ifdef FEAT_SESSION
9839 if (eap->cmdidx == CMD_mkview)
9840 flagp = &vop_flags;
9841 else
9842 flagp = &ssop_flags;
9843#endif
9844
9845#ifdef MKSESSION_NL
9846 /* "unix" in 'sessionoptions': use NL line separator */
9847 if (view_session && (*flagp & SSOP_UNIX))
9848 mksession_nl = TRUE;
9849#endif
9850
9851 /* Write the version command for :mkvimrc */
9852 if (eap->cmdidx == CMD_mkvimrc)
9853 (void)put_line(fd, "version 6.0");
9854
9855#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009856 if (eap->cmdidx == CMD_mksession)
9857 {
9858 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9859 failed = TRUE;
9860 }
9861
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 if (eap->cmdidx != CMD_mkview)
9863#endif
9864 {
9865 /* Write setting 'compatible' first, because it has side effects.
9866 * For that same reason only do it when needed. */
9867 if (p_cp)
9868 (void)put_line(fd, "if !&cp | set cp | endif");
9869 else
9870 (void)put_line(fd, "if &cp | set nocp | endif");
9871 }
9872
9873#ifdef FEAT_SESSION
9874 if (!view_session
9875 || (eap->cmdidx == CMD_mksession
9876 && (*flagp & SSOP_OPTIONS)))
9877#endif
9878 failed |= (makemap(fd, NULL) == FAIL
9879 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9880
9881#ifdef FEAT_SESSION
9882 if (!failed && view_session)
9883 {
9884 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9885 failed = TRUE;
9886 if (eap->cmdidx == CMD_mksession)
9887 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009888 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889
Bram Moolenaard9462e32011-04-11 21:35:11 +02009890 dirnow = alloc(MAXPATHL);
9891 if (dirnow == NULL)
9892 failed = TRUE;
9893 else
9894 {
9895 /*
9896 * Change to session file's dir.
9897 */
9898 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009900 *dirnow = NUL;
9901 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9902 {
9903 if (vim_chdirfile(fname) == OK)
9904 shorten_fnames(TRUE);
9905 }
9906 else if (*dirnow != NUL
9907 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9908 {
9909 if (mch_chdir((char *)globaldir) == 0)
9910 shorten_fnames(TRUE);
9911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912
Bram Moolenaard9462e32011-04-11 21:35:11 +02009913 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914
Bram Moolenaard9462e32011-04-11 21:35:11 +02009915 /* restore original dir */
9916 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009918 {
9919 if (mch_chdir((char *)dirnow) != 0)
9920 EMSG(_(e_prev_dir));
9921 shorten_fnames(TRUE);
9922 }
9923 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 }
9925 }
9926 else
9927 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009928 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9929 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 }
9931 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9932 == FAIL)
9933 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009934 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9935 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009936 if (eap->cmdidx == CMD_mksession)
9937 {
9938 if (put_line(fd, "unlet SessionLoad") == FAIL)
9939 failed = TRUE;
9940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 }
9942#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009943 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9944 failed = TRUE;
9945
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 failed |= fclose(fd);
9947
9948 if (failed)
9949 EMSG(_(e_write));
9950#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9951 else if (eap->cmdidx == CMD_mksession)
9952 {
9953 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009954 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955
Bram Moolenaard9462e32011-04-11 21:35:11 +02009956 tbuf = alloc(MAXPATHL);
9957 if (tbuf != NULL)
9958 {
9959 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9960 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9961 vim_free(tbuf);
9962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963 }
9964#endif
9965#ifdef MKSESSION_NL
9966 mksession_nl = FALSE;
9967#endif
9968 }
9969
9970#ifdef FEAT_BROWSE
9971theend:
9972 vim_free(browseFile);
9973#endif
9974#ifdef FEAT_SESSION
9975 vim_free(viewFile);
9976#endif
9977}
9978
Bram Moolenaardf177f62005-02-22 08:39:57 +00009979#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9980 || defined(PROTO)
9981 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009982vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009983{
9984 if (vim_mkdir(name, prot) != 0)
9985 {
9986 EMSG2(_("E739: Cannot create directory: %s"), name);
9987 return FAIL;
9988 }
9989 return OK;
9990}
9991#endif
9992
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993/*
9994 * Open a file for writing for an Ex command, with some checks.
9995 * Return file descriptor, or NULL on failure.
9996 */
9997 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009998open_exfile(
9999 char_u *fname,
10000 int forceit,
10001 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002{
10003 FILE *fd;
10004
10005#ifdef UNIX
10006 /* with Unix it is possible to open a directory */
10007 if (mch_isdir(fname))
10008 {
10009 EMSG2(_(e_isadir2), fname);
10010 return NULL;
10011 }
10012#endif
10013 if (!forceit && *mode != 'a' && vim_fexists(fname))
10014 {
10015 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
10016 return NULL;
10017 }
10018
10019 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
10020 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
10021
10022 return fd;
10023}
10024
10025/*
10026 * ":mark" and ":k".
10027 */
10028 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010029ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030{
10031 pos_T pos;
10032
10033 if (*eap->arg == NUL) /* No argument? */
10034 EMSG(_(e_argreq));
10035 else if (eap->arg[1] != NUL) /* more than one character? */
10036 EMSG(_(e_trailing));
10037 else
10038 {
10039 pos = curwin->w_cursor; /* save curwin->w_cursor */
10040 curwin->w_cursor.lnum = eap->line2;
10041 beginline(BL_WHITE | BL_FIX);
10042 if (setmark(*eap->arg) == FAIL) /* set mark */
10043 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10044 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10045 }
10046}
10047
10048/*
10049 * Update w_topline, w_leftcol and the cursor position.
10050 */
10051 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010052update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053{
10054 check_cursor(); /* put cursor on valid line */
10055 update_topline();
10056 if (!curwin->w_p_wrap)
10057 validate_cursor();
10058 update_curswant();
10059}
10060
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061/*
10062 * ":normal[!] {commands}": Execute normal mode commands.
10063 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010064 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010065ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 int save_msg_scroll = msg_scroll;
10068 int save_restart_edit = restart_edit;
10069 int save_msg_didout = msg_didout;
10070 int save_State = State;
10071 tasave_T tabuf;
10072 int save_insertmode = p_im;
10073 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010074 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075#ifdef FEAT_MBYTE
10076 char_u *arg = NULL;
10077 int l;
10078 char_u *p;
10079#endif
10080
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010081 if (ex_normal_lock > 0)
10082 {
10083 EMSG(_(e_secure));
10084 return;
10085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086 if (ex_normal_busy >= p_mmd)
10087 {
10088 EMSG(_("E192: Recursive use of :normal too deep"));
10089 return;
10090 }
10091 ++ex_normal_busy;
10092
10093 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10094 restart_edit = 0; /* don't go to Insert mode */
10095 p_im = FALSE; /* don't use 'insertmode' */
10096
10097#ifdef FEAT_MBYTE
10098 /*
10099 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10100 * this for the K_SPECIAL leading byte, otherwise special keys will not
10101 * work.
10102 */
10103 if (has_mbyte)
10104 {
10105 int len = 0;
10106
10107 /* Count the number of characters to be escaped. */
10108 for (p = eap->arg; *p != NUL; ++p)
10109 {
10110# ifdef FEAT_GUI
10111 if (*p == CSI) /* leadbyte CSI */
10112 len += 2;
10113# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010114 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10116# ifdef FEAT_GUI
10117 || *p == CSI
10118# endif
10119 )
10120 len += 2;
10121 }
10122 if (len > 0)
10123 {
10124 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10125 if (arg != NULL)
10126 {
10127 len = 0;
10128 for (p = eap->arg; *p != NUL; ++p)
10129 {
10130 arg[len++] = *p;
10131# ifdef FEAT_GUI
10132 if (*p == CSI)
10133 {
10134 arg[len++] = KS_EXTRA;
10135 arg[len++] = (int)KE_CSI;
10136 }
10137# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010138 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 {
10140 arg[len++] = *++p;
10141 if (*p == K_SPECIAL)
10142 {
10143 arg[len++] = KS_SPECIAL;
10144 arg[len++] = KE_FILLER;
10145 }
10146# ifdef FEAT_GUI
10147 else if (*p == CSI)
10148 {
10149 arg[len++] = KS_EXTRA;
10150 arg[len++] = (int)KE_CSI;
10151 }
10152# endif
10153 }
10154 arg[len] = NUL;
10155 }
10156 }
10157 }
10158 }
10159#endif
10160
10161 /*
10162 * Save the current typeahead. This is required to allow using ":normal"
10163 * from an event handler and makes sure we don't hang when the argument
10164 * ends with half a command.
10165 */
10166 save_typeahead(&tabuf);
10167 if (tabuf.typebuf_valid)
10168 {
10169 /*
10170 * Repeat the :normal command for each line in the range. When no
10171 * range given, execute it just once, without positioning the cursor
10172 * first.
10173 */
10174 do
10175 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176 if (eap->addr_count != 0)
10177 {
10178 curwin->w_cursor.lnum = eap->line1++;
10179 curwin->w_cursor.col = 0;
10180 }
10181
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010182 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183#ifdef FEAT_MBYTE
10184 arg != NULL ? arg :
10185#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010186 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187 }
10188 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10189 }
10190
10191 /* Might not return to the main loop when in an event handler. */
10192 update_topline_cursor();
10193
10194 /* Restore the previous typeahead. */
10195 restore_typeahead(&tabuf);
10196
10197 --ex_normal_busy;
10198 msg_scroll = save_msg_scroll;
10199 restart_edit = save_restart_edit;
10200 p_im = save_insertmode;
10201 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010202 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
10204
10205 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +010010206 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +010010208#ifdef FEAT_MOUSE
10209 setmouse();
10210#endif
10211#ifdef CURSOR_SHAPE
10212 ui_cursor_shape(); /* may show different cursor shape */
10213#endif
10214
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215#ifdef FEAT_MBYTE
10216 vim_free(arg);
10217#endif
10218}
10219
10220/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010221 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 */
10223 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010224ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010226 if (eap->forceit)
10227 {
10228 coladvance((colnr_T)MAXCOL);
10229 curwin->w_curswant = MAXCOL;
10230 curwin->w_set_curswant = FALSE;
10231 }
10232
Bram Moolenaara40c5002005-01-09 21:16:21 +000010233 /* Ignore the command when already in Insert mode. Inserting an
10234 * expression register that invokes a function can do this. */
10235 if (State & INSERT)
10236 return;
10237
Bram Moolenaar64969662005-12-14 21:59:55 +000010238 if (eap->cmdidx == CMD_startinsert)
10239 restart_edit = 'a';
10240 else if (eap->cmdidx == CMD_startreplace)
10241 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010243 restart_edit = 'V';
10244
10245 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010247 if (eap->cmdidx == CMD_startinsert)
10248 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249 curwin->w_curswant = 0; /* avoid MAXCOL */
10250 }
10251}
10252
10253/*
10254 * ":stopinsert"
10255 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010257ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258{
10259 restart_edit = 0;
10260 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010261 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010264/*
10265 * Execute normal mode command "cmd".
10266 * "remap" can be REMAP_NONE or REMAP_YES.
10267 */
10268 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010269exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010270{
Bram Moolenaar25281632016-01-21 23:32:32 +010010271 /* Stuff the argument into the typeahead buffer. */
10272 ins_typebuf(cmd, remap, 0, TRUE, silent);
10273 exec_normal(FALSE);
10274}
Bram Moolenaar25281632016-01-21 23:32:32 +010010275
Bram Moolenaar25281632016-01-21 23:32:32 +010010276/*
10277 * Execute normal_cmd() until there is no typeahead left.
10278 */
10279 void
10280exec_normal(int was_typed)
10281{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010282 oparg_T oa;
10283
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010284 clear_oparg(&oa);
10285 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010286 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10287 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010288 {
10289 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010290 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010291 }
10292}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010293
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294#ifdef FEAT_FIND_ID
10295 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010296ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297{
10298 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10299 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10300 (linenr_T)1, (linenr_T)MAXLNUM);
10301}
10302
10303#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
10304/*
10305 * ":psearch"
10306 */
10307 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010308ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309{
10310 g_do_tagpreview = p_pvh;
10311 ex_findpat(eap);
10312 g_do_tagpreview = 0;
10313}
10314#endif
10315
10316 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010317ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318{
10319 int whole = TRUE;
10320 long n;
10321 char_u *p;
10322 int action;
10323
10324 switch (cmdnames[eap->cmdidx].cmd_name[2])
10325 {
10326 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10327 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10328 action = ACTION_GOTO;
10329 else
10330 action = ACTION_SHOW;
10331 break;
10332 case 'i': /* ":ilist" and ":dlist" */
10333 action = ACTION_SHOW_ALL;
10334 break;
10335 case 'u': /* ":ijump" and ":djump" */
10336 action = ACTION_GOTO;
10337 break;
10338 default: /* ":isplit" and ":dsplit" */
10339 action = ACTION_SPLIT;
10340 break;
10341 }
10342
10343 n = 1;
10344 if (vim_isdigit(*eap->arg)) /* get count */
10345 {
10346 n = getdigits(&eap->arg);
10347 eap->arg = skipwhite(eap->arg);
10348 }
10349 if (*eap->arg == '/') /* Match regexp, not just whole words */
10350 {
10351 whole = FALSE;
10352 ++eap->arg;
10353 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10354 if (*p)
10355 {
10356 *p++ = NUL;
10357 p = skipwhite(p);
10358
10359 /* Check for trailing illegal characters */
10360 if (!ends_excmd(*p))
10361 eap->errmsg = e_trailing;
10362 else
10363 eap->nextcmd = check_nextcmd(p);
10364 }
10365 }
10366 if (!eap->skip)
10367 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10368 whole, !eap->forceit,
10369 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10370 n, action, eap->line1, eap->line2);
10371}
10372#endif
10373
10374#ifdef FEAT_WINDOWS
10375
10376# ifdef FEAT_QUICKFIX
10377/*
10378 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10379 */
10380 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010381ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010383 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10385}
10386
10387/*
10388 * ":pedit"
10389 */
10390 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010391ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392{
10393 win_T *curwin_save = curwin;
10394
10395 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010396 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397 keep_help_flag = curwin_save->w_buffer->b_help;
10398 do_exedit(eap, NULL);
10399 keep_help_flag = FALSE;
10400 if (curwin != curwin_save && win_valid(curwin_save))
10401 {
10402 /* Return cursor to where we were */
10403 validate_cursor();
10404 redraw_later(VALID);
10405 win_enter(curwin_save, TRUE);
10406 }
10407 g_do_tagpreview = 0;
10408}
10409# endif
10410
10411/*
10412 * ":stag", ":stselect" and ":stjump".
10413 */
10414 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010415ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416{
10417 postponed_split = -1;
10418 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010419 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10421 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010422 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010423}
10424#endif
10425
10426/*
10427 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10428 */
10429 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010430ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431{
10432 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10433}
10434
10435 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010436ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437{
10438 int cmd;
10439
10440 switch (name[1])
10441 {
10442 case 'j': cmd = DT_JUMP; /* ":tjump" */
10443 break;
10444 case 's': cmd = DT_SELECT; /* ":tselect" */
10445 break;
10446 case 'p': cmd = DT_PREV; /* ":tprevious" */
10447 break;
10448 case 'N': cmd = DT_PREV; /* ":tNext" */
10449 break;
10450 case 'n': cmd = DT_NEXT; /* ":tnext" */
10451 break;
10452 case 'o': cmd = DT_POP; /* ":pop" */
10453 break;
10454 case 'f': /* ":tfirst" */
10455 case 'r': cmd = DT_FIRST; /* ":trewind" */
10456 break;
10457 case 'l': cmd = DT_LAST; /* ":tlast" */
10458 break;
10459 default: /* ":tag" */
10460#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010461 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010463 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464 return;
10465 }
10466#endif
10467 cmd = DT_TAG;
10468 break;
10469 }
10470
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010471 if (name[0] == 'l')
10472 {
10473#ifndef FEAT_QUICKFIX
10474 ex_ni(eap);
10475 return;
10476#else
10477 cmd = DT_LTAG;
10478#endif
10479 }
10480
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10482 eap->forceit, TRUE);
10483}
10484
10485/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010486 * Check "str" for starting with a special cmdline variable.
10487 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10488 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10489 */
10490 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010491find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010492{
10493 int len;
10494 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010495 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010496 "%",
10497#define SPEC_PERC 0
10498 "#",
10499#define SPEC_HASH 1
10500 "<cword>", /* cursor word */
10501#define SPEC_CWORD 2
10502 "<cWORD>", /* cursor WORD */
10503#define SPEC_CCWORD 3
10504 "<cfile>", /* cursor path name */
10505#define SPEC_CFILE 4
10506 "<sfile>", /* ":so" file name */
10507#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010508 "<slnum>", /* ":so" file line number */
10509#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010510#ifdef FEAT_AUTOCMD
10511 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010512# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010513 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010514# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010515 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010516# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010517#endif
10518#ifdef FEAT_CLIENTSERVER
10519 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010520# ifdef FEAT_AUTOCMD
10521# define SPEC_CLIENT 10
10522# else
10523# define SPEC_CLIENT 7
10524# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010525#endif
10526 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010527
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010528 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010529 {
10530 len = (int)STRLEN(spec_str[i]);
10531 if (STRNCMP(src, spec_str[i], len) == 0)
10532 {
10533 *usedlen = len;
10534 return i;
10535 }
10536 }
10537 return -1;
10538}
10539
10540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 * Evaluate cmdline variables.
10542 *
10543 * change '%' to curbuf->b_ffname
10544 * '#' to curwin->w_altfile
10545 * '<cword>' to word under the cursor
10546 * '<cWORD>' to WORD under the cursor
10547 * '<cfile>' to path name under the cursor
10548 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010549 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550 * '<afile>' to file name for autocommand
10551 * '<abuf>' to buffer number for autocommand
10552 * '<amatch>' to matching name for autocommand
10553 *
10554 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10555 * "" for error without a message) and NULL is returned.
10556 * Returns an allocated string if a valid match was found.
10557 * Returns NULL if no match was found. "usedlen" then still contains the
10558 * number of characters to skip.
10559 */
10560 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010561eval_vars(
10562 char_u *src, /* pointer into commandline */
10563 char_u *srcstart, /* beginning of valid memory for src */
10564 int *usedlen, /* characters after src that are used */
10565 linenr_T *lnump, /* line number for :e command, or NULL */
10566 char_u **errormsg, /* pointer to error message */
10567 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010568 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569{
10570 int i;
10571 char_u *s;
10572 char_u *result;
10573 char_u *resultbuf = NULL;
10574 int resultlen;
10575 buf_T *buf;
10576 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10577 int spec_idx;
10578#ifdef FEAT_MODIFY_FNAME
10579 int skip_mod = FALSE;
10580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582
10583 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010584 if (escaped != NULL)
10585 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586
10587 /*
10588 * Check if there is something to do.
10589 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010590 spec_idx = find_cmdline_var(src, usedlen);
10591 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592 {
10593 *usedlen = 1;
10594 return NULL;
10595 }
10596
10597 /*
10598 * Skip when preceded with a backslash "\%" and "\#".
10599 * Note: In "\\%" the % is also not recognized!
10600 */
10601 if (src > srcstart && src[-1] == '\\')
10602 {
10603 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010604 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605 return NULL;
10606 }
10607
10608 /*
10609 * word or WORD under cursor
10610 */
10611 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10612 {
10613 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10614 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10615 if (resultlen == 0)
10616 {
10617 *errormsg = (char_u *)"";
10618 return NULL;
10619 }
10620 }
10621
10622 /*
10623 * '#': Alternate file name
10624 * '%': Current file name
10625 * File name under the cursor
10626 * File name for autocommand
10627 * and following modifiers
10628 */
10629 else
10630 {
10631 switch (spec_idx)
10632 {
10633 case SPEC_PERC: /* '%': current file */
10634 if (curbuf->b_fname == NULL)
10635 {
10636 result = (char_u *)"";
10637 valid = 0; /* Must have ":p:h" to be valid */
10638 }
10639 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641 break;
10642
10643 case SPEC_HASH: /* '#' or "#99": alternate file */
10644 if (src[1] == '#') /* "##": the argument list */
10645 {
10646 result = arg_all();
10647 resultbuf = result;
10648 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010649 if (escaped != NULL)
10650 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010651#ifdef FEAT_MODIFY_FNAME
10652 skip_mod = TRUE;
10653#endif
10654 break;
10655 }
10656 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010657 if (*s == '<') /* "#<99" uses v:oldfiles */
10658 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659 i = (int)getdigits(&s);
10660 *usedlen = (int)(s - src); /* length of what we expand */
10661
Bram Moolenaard812df62008-11-09 12:46:09 +000010662 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010664 if (*usedlen < 2)
10665 {
10666 /* Should we give an error message for #<text? */
10667 *usedlen = 1;
10668 return NULL;
10669 }
10670#ifdef FEAT_EVAL
10671 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10672 (long)i);
10673 if (result == NULL)
10674 {
10675 *errormsg = (char_u *)"";
10676 return NULL;
10677 }
10678#else
10679 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682 }
10683 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010684 {
10685 buf = buflist_findnr(i);
10686 if (buf == NULL)
10687 {
10688 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10689 return NULL;
10690 }
10691 if (lnump != NULL)
10692 *lnump = ECMD_LAST;
10693 if (buf->b_fname == NULL)
10694 {
10695 result = (char_u *)"";
10696 valid = 0; /* Must have ":p:h" to be valid */
10697 }
10698 else
10699 result = buf->b_fname;
10700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701 break;
10702
10703#ifdef FEAT_SEARCHPATH
10704 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010705 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706 if (result == NULL)
10707 {
10708 *errormsg = (char_u *)"";
10709 return NULL;
10710 }
10711 resultbuf = result; /* remember allocated string */
10712 break;
10713#endif
10714
10715#ifdef FEAT_AUTOCMD
10716 case SPEC_AFILE: /* file name for autocommand */
10717 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010718 if (result != NULL && !autocmd_fname_full)
10719 {
10720 /* Still need to turn the fname into a full path. It is
10721 * postponed to avoid a delay when <afile> is not used. */
10722 autocmd_fname_full = TRUE;
10723 result = FullName_save(autocmd_fname, FALSE);
10724 vim_free(autocmd_fname);
10725 autocmd_fname = result;
10726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727 if (result == NULL)
10728 {
10729 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10730 return NULL;
10731 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010732 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733 break;
10734
10735 case SPEC_ABUF: /* buffer number for autocommand */
10736 if (autocmd_bufnr <= 0)
10737 {
10738 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10739 return NULL;
10740 }
10741 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10742 result = strbuf;
10743 break;
10744
10745 case SPEC_AMATCH: /* match name for autocommand */
10746 result = autocmd_match;
10747 if (result == NULL)
10748 {
10749 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10750 return NULL;
10751 }
10752 break;
10753
10754#endif
10755 case SPEC_SFILE: /* file name for ":so" command */
10756 result = sourcing_name;
10757 if (result == NULL)
10758 {
10759 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10760 return NULL;
10761 }
10762 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010763 case SPEC_SLNUM: /* line in file for ":so" command */
10764 if (sourcing_name == NULL || sourcing_lnum == 0)
10765 {
10766 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10767 return NULL;
10768 }
10769 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10770 result = strbuf;
10771 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772#if defined(FEAT_CLIENTSERVER)
10773 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010774 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10775 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 result = strbuf;
10777 break;
10778#endif
10779 }
10780
10781 resultlen = (int)STRLEN(result); /* length of new string */
10782 if (src[*usedlen] == '<') /* remove the file name extension */
10783 {
10784 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010786 resultlen = (int)(s - result);
10787 }
10788#ifdef FEAT_MODIFY_FNAME
10789 else if (!skip_mod)
10790 {
10791 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10792 &resultlen);
10793 if (result == NULL)
10794 {
10795 *errormsg = (char_u *)"";
10796 return NULL;
10797 }
10798 }
10799#endif
10800 }
10801
10802 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10803 {
10804 if (valid != VALID_HEAD + VALID_PATH)
10805 /* xgettext:no-c-format */
10806 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10807 else
10808 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10809 result = NULL;
10810 }
10811 else
10812 result = vim_strnsave(result, resultlen);
10813 vim_free(resultbuf);
10814 return result;
10815}
10816
10817/*
10818 * Concatenate all files in the argument list, separated by spaces, and return
10819 * it in one allocated string.
10820 * Spaces and backslashes in the file names are escaped with a backslash.
10821 * Returns NULL when out of memory.
10822 */
10823 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010824arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825{
10826 int len;
10827 int idx;
10828 char_u *retval = NULL;
10829 char_u *p;
10830
10831 /*
10832 * Do this loop two times:
10833 * first time: compute the total length
10834 * second time: concatenate the names
10835 */
10836 for (;;)
10837 {
10838 len = 0;
10839 for (idx = 0; idx < ARGCOUNT; ++idx)
10840 {
10841 p = alist_name(&ARGLIST[idx]);
10842 if (p != NULL)
10843 {
10844 if (len > 0)
10845 {
10846 /* insert a space in between names */
10847 if (retval != NULL)
10848 retval[len] = ' ';
10849 ++len;
10850 }
10851 for ( ; *p != NUL; ++p)
10852 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010853 if (*p == ' '
10854#ifndef BACKSLASH_IN_FILENAME
10855 || *p == '\\'
10856#endif
10857 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858 {
10859 /* insert a backslash */
10860 if (retval != NULL)
10861 retval[len] = '\\';
10862 ++len;
10863 }
10864 if (retval != NULL)
10865 retval[len] = *p;
10866 ++len;
10867 }
10868 }
10869 }
10870
10871 /* second time: break here */
10872 if (retval != NULL)
10873 {
10874 retval[len] = NUL;
10875 break;
10876 }
10877
10878 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010879 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 if (retval == NULL)
10881 break;
10882 }
10883
10884 return retval;
10885}
10886
10887#if defined(FEAT_AUTOCMD) || defined(PROTO)
10888/*
10889 * Expand the <sfile> string in "arg".
10890 *
10891 * Returns an allocated string, or NULL for any error.
10892 */
10893 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010894expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895{
10896 char_u *errormsg;
10897 int len;
10898 char_u *result;
10899 char_u *newres;
10900 char_u *repl;
10901 int srclen;
10902 char_u *p;
10903
10904 result = vim_strsave(arg);
10905 if (result == NULL)
10906 return NULL;
10907
10908 for (p = result; *p; )
10909 {
10910 if (STRNCMP(p, "<sfile>", 7) != 0)
10911 ++p;
10912 else
10913 {
10914 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010915 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 if (errormsg != NULL)
10917 {
10918 if (*errormsg)
10919 emsg(errormsg);
10920 vim_free(result);
10921 return NULL;
10922 }
10923 if (repl == NULL) /* no match (cannot happen) */
10924 {
10925 p += srclen;
10926 continue;
10927 }
10928 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10929 newres = alloc(len);
10930 if (newres == NULL)
10931 {
10932 vim_free(repl);
10933 vim_free(result);
10934 return NULL;
10935 }
10936 mch_memmove(newres, result, (size_t)(p - result));
10937 STRCPY(newres + (p - result), repl);
10938 len = (int)STRLEN(newres);
10939 STRCAT(newres, p + srclen);
10940 vim_free(repl);
10941 vim_free(result);
10942 result = newres;
10943 p = newres + len; /* continue after the match */
10944 }
10945 }
10946
10947 return result;
10948}
10949#endif
10950
10951#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010010952static int ses_winsizes(FILE *fd, int restore_size,
10953 win_T *tab_firstwin);
10954static int ses_win_rec(FILE *fd, frame_T *fr);
10955static frame_T *ses_skipframe(frame_T *fr);
10956static int ses_do_frame(frame_T *fr);
10957static int ses_do_win(win_T *wp);
10958static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
10959static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
10960static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961
10962/*
10963 * Write openfile commands for the current buffers to an .exrc file.
10964 * Return FAIL on error, OK otherwise.
10965 */
10966 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010967makeopens(
10968 FILE *fd,
10969 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970{
10971 buf_T *buf;
10972 int only_save_windows = TRUE;
10973 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974 int restore_size = TRUE;
10975 win_T *wp;
10976 char_u *sname;
10977 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010978 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010979 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010980 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010981 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010982 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010983 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984
10985 if (ssop_flags & SSOP_BUFFERS)
10986 only_save_windows = FALSE; /* Save ALL buffers */
10987
10988 /*
10989 * Begin by setting the this_session variable, and then other
10990 * sessionable variables.
10991 */
10992#ifdef FEAT_EVAL
10993 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10994 return FAIL;
10995 if (ssop_flags & SSOP_GLOBALS)
10996 if (store_session_globals(fd) == FAIL)
10997 return FAIL;
10998#endif
10999
11000 /*
11001 * Close all windows but one.
11002 */
11003 if (put_line(fd, "silent only") == FAIL)
11004 return FAIL;
11005
11006 /*
11007 * Now a :cd command to the session directory or the current directory
11008 */
11009 if (ssop_flags & SSOP_SESDIR)
11010 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011011 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11012 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013 return FAIL;
11014 }
11015 else if (ssop_flags & SSOP_CURDIR)
11016 {
11017 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11018 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011019 || fputs("cd ", fd) < 0
11020 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11021 || put_eol(fd) == FAIL)
11022 {
11023 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026 vim_free(sname);
11027 }
11028
11029 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011030 * If there is an empty, unnamed buffer we will wipe it out later.
11031 * Remember the buffer number.
11032 */
11033 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11034 return FAIL;
11035 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11036 return FAIL;
11037 if (put_line(fd, "endif") == FAIL)
11038 return FAIL;
11039
11040 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 * Now save the current files, current buffer first.
11042 */
11043 if (put_line(fd, "set shortmess=aoO") == FAIL)
11044 return FAIL;
11045
11046 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011047 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048 {
11049 if (!(only_save_windows && buf->b_nwindows == 0)
11050 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
11051 && buf->b_fname != NULL
11052 && buf->b_p_bl)
11053 {
11054 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11055 : buf->b_wininfo->wi_fpos.lnum) < 0
11056 || ses_fname(fd, buf, &ssop_flags) == FAIL)
11057 return FAIL;
11058 }
11059 }
11060
11061 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011062 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011063 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11064 return FAIL;
11065
11066 if (ssop_flags & SSOP_RESIZE)
11067 {
11068 /* Note: after the restore we still check it worked!*/
11069 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11070 || put_eol(fd) == FAIL)
11071 return FAIL;
11072 }
11073
11074#ifdef FEAT_GUI
11075 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11076 {
11077 int x, y;
11078
11079 if (gui_mch_get_winpos(&x, &y) == OK)
11080 {
11081 /* Note: after the restore we still check it worked!*/
11082 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11083 return FAIL;
11084 }
11085 }
11086#endif
11087
11088 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011089 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11090 * will be displayed when creating the next tab. That resizes the windows
11091 * in the first tab, which may cause problems. Set 'showtabline' to 2
11092 * temporarily to avoid that.
11093 */
11094 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11095 {
11096 if (put_line(fd, "set stal=2") == FAIL)
11097 return FAIL;
11098 restore_stal = TRUE;
11099 }
11100
11101 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011102 * May repeat putting Windows for each tab, when "tabpages" is in
11103 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011104 * Don't use goto_tabpage(), it may change directory and trigger
11105 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011107 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011108 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011109 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011111 int need_tabnew = FALSE;
11112 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011113
Bram Moolenaar18144c82006-04-12 21:52:12 +000011114 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011116 tabpage_T *tp = find_tabpage(tabnr);
11117
11118 if (tp == NULL)
11119 break; /* done all tab pages */
11120 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011121 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011122 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011123 tab_topframe = topframe;
11124 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011125 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011126 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011127 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011128 tab_topframe = tp->tp_topframe;
11129 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011130 if (tabnr > 1)
11131 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133
Bram Moolenaar18144c82006-04-12 21:52:12 +000011134 /*
11135 * Before creating the window layout, try loading one file. If this
11136 * is aborted we don't end up with a number of useless windows.
11137 * This may have side effects! (e.g., compressed or network file).
11138 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011139 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011140 {
11141 if (ses_do_win(wp)
11142 && wp->w_buffer->b_ffname != NULL
11143 && !wp->w_buffer->b_help
11144#ifdef FEAT_QUICKFIX
11145 && !bt_nofile(wp->w_buffer)
11146#endif
11147 )
11148 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011149 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000011150 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
11151 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011152 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011153 if (!wp->w_arg_idx_invalid)
11154 edited_win = wp;
11155 break;
11156 }
11157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011159 /* If no file got edited create an empty tab page. */
11160 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11161 return FAIL;
11162
Bram Moolenaar18144c82006-04-12 21:52:12 +000011163 /*
11164 * Save current window layout.
11165 */
11166 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011168 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011170 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11171 return FAIL;
11172 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11173 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174
Bram Moolenaar18144c82006-04-12 21:52:12 +000011175 /*
11176 * Check if window sizes can be restored (no windows omitted).
11177 * Remember the window number of the current window after restoring.
11178 */
11179 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011180 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011181 {
11182 if (ses_do_win(wp))
11183 ++nr;
11184 else
11185 restore_size = FALSE;
11186 if (curwin == wp)
11187 cnr = nr;
11188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011189
Bram Moolenaar18144c82006-04-12 21:52:12 +000011190 /* Go to the first window. */
11191 if (put_line(fd, "wincmd t") == FAIL)
11192 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011193
Bram Moolenaar18144c82006-04-12 21:52:12 +000011194 /*
11195 * If more than one window, see if sizes can be restored.
11196 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11197 * resized when moving between windows.
11198 * Do this before restoring the view, so that the topline and the
11199 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011200 * winminheight and winminwidth need to be set to avoid an error if the
11201 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011202 */
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011203 if (put_line(fd, "set winminheight=1 winheight=1 winminwidth=1 winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011204 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011205 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011206 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207
Bram Moolenaar18144c82006-04-12 21:52:12 +000011208 /*
11209 * Restore the view of the window (options, file, cursor, etc.).
11210 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011211 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011212 {
11213 if (!ses_do_win(wp))
11214 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011215 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11216 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011217 return FAIL;
11218 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11219 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011220 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011221 }
11222
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011223 /* The argument index in the first tab page is zero, need to set it in
11224 * each window. For further tab pages it's the window where we do
11225 * "tabedit". */
11226 cur_arg_idx = next_arg_idx;
11227
Bram Moolenaar18144c82006-04-12 21:52:12 +000011228 /*
11229 * Restore cursor to the current window if it's not the first one.
11230 */
11231 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11232 || put_eol(fd) == FAIL))
11233 return FAIL;
11234
11235 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011236 * Restore window sizes again after jumping around in windows, because
11237 * the current window has a minimum size while others may not.
11238 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011239 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011240 return FAIL;
11241
Bram Moolenaar18144c82006-04-12 21:52:12 +000011242 /* Don't continue in another tab page when doing only the current one
11243 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011244 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011245 break;
11246 }
11247
11248 if (ssop_flags & SSOP_TABPAGES)
11249 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011250 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11251 || put_eol(fd) == FAIL)
11252 return FAIL;
11253 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011254 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11255 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011256
Bram Moolenaar9c102382006-05-03 21:26:49 +000011257 /*
11258 * Wipe out an empty unnamed buffer we started in.
11259 */
11260 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11261 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011262 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011263 return FAIL;
11264 if (put_line(fd, "endif") == FAIL)
11265 return FAIL;
11266 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11267 return FAIL;
11268
11269 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11270 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11271 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11272 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011273 /* Re-apply 'winminheight' and 'winminwidth'. */
11274 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11275 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11276 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277
11278 /*
11279 * Lastly, execute the x.vim file if it exists.
11280 */
11281 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11282 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011283 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011284 || put_line(fd, "endif") == FAIL)
11285 return FAIL;
11286
11287 return OK;
11288}
11289
11290 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011291ses_winsizes(
11292 FILE *fd,
11293 int restore_size,
11294 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295{
11296 int n = 0;
11297 win_T *wp;
11298
11299 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11300 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011301 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302 {
11303 if (!ses_do_win(wp))
11304 continue;
11305 ++n;
11306
11307 /* restore height when not full height */
11308 if (wp->w_height + wp->w_status_height < topframe->fr_height
11309 && (fprintf(fd,
11310 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11311 n, (long)wp->w_height, Rows / 2, Rows) < 0
11312 || put_eol(fd) == FAIL))
11313 return FAIL;
11314
11315 /* restore width when not full width */
11316 if (wp->w_width < Columns && (fprintf(fd,
11317 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11318 n, (long)wp->w_width, Columns / 2, Columns) < 0
11319 || put_eol(fd) == FAIL))
11320 return FAIL;
11321 }
11322 }
11323 else
11324 {
11325 /* Just equalise window sizes */
11326 if (put_line(fd, "wincmd =") == FAIL)
11327 return FAIL;
11328 }
11329 return OK;
11330}
11331
11332/*
11333 * Write commands to "fd" to recursively create windows for frame "fr",
11334 * horizontally and vertically split.
11335 * After the commands the last window in the frame is the current window.
11336 * Returns FAIL when writing the commands to "fd" fails.
11337 */
11338 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011339ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340{
11341 frame_T *frc;
11342 int count = 0;
11343
11344 if (fr->fr_layout != FR_LEAF)
11345 {
11346 /* Find first frame that's not skipped and then create a window for
11347 * each following one (first frame is already there). */
11348 frc = ses_skipframe(fr->fr_child);
11349 if (frc != NULL)
11350 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11351 {
11352 /* Make window as big as possible so that we have lots of room
11353 * to split. */
11354 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11355 || put_line(fd, fr->fr_layout == FR_COL
11356 ? "split" : "vsplit") == FAIL)
11357 return FAIL;
11358 ++count;
11359 }
11360
11361 /* Go back to the first window. */
11362 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11363 ? "%dwincmd k" : "%dwincmd h", count) < 0
11364 || put_eol(fd) == FAIL))
11365 return FAIL;
11366
11367 /* Recursively create frames/windows in each window of this column or
11368 * row. */
11369 frc = ses_skipframe(fr->fr_child);
11370 while (frc != NULL)
11371 {
11372 ses_win_rec(fd, frc);
11373 frc = ses_skipframe(frc->fr_next);
11374 /* Go to next window. */
11375 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11376 return FAIL;
11377 }
11378 }
11379 return OK;
11380}
11381
11382/*
11383 * Skip frames that don't contain windows we want to save in the Session.
11384 * Returns NULL when there none.
11385 */
11386 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011387ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388{
11389 frame_T *frc;
11390
11391 for (frc = fr; frc != NULL; frc = frc->fr_next)
11392 if (ses_do_frame(frc))
11393 break;
11394 return frc;
11395}
11396
11397/*
11398 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11399 * the Session.
11400 */
11401 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011402ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011403{
11404 frame_T *frc;
11405
11406 if (fr->fr_layout == FR_LEAF)
11407 return ses_do_win(fr->fr_win);
11408 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11409 if (ses_do_frame(frc))
11410 return TRUE;
11411 return FALSE;
11412}
11413
11414/*
11415 * Return non-zero if window "wp" is to be stored in the Session.
11416 */
11417 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011418ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011419{
11420 if (wp->w_buffer->b_fname == NULL
11421#ifdef FEAT_QUICKFIX
11422 /* When 'buftype' is "nofile" can't restore the window contents. */
11423 || bt_nofile(wp->w_buffer)
11424#endif
11425 )
11426 return (ssop_flags & SSOP_BLANK);
11427 if (wp->w_buffer->b_help)
11428 return (ssop_flags & SSOP_HELP);
11429 return TRUE;
11430}
11431
11432/*
11433 * Write commands to "fd" to restore the view of a window.
11434 * Caller must make sure 'scrolloff' is zero.
11435 */
11436 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011437put_view(
11438 FILE *fd,
11439 win_T *wp,
11440 int add_edit, /* add ":edit" command to view */
11441 unsigned *flagp, /* vop_flags or ssop_flags */
11442 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011443 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444{
11445 win_T *save_curwin;
11446 int f;
11447 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011448 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449
11450 /* Always restore cursor position for ":mksession". For ":mkview" only
11451 * when 'viewoptions' contains "cursor". */
11452 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11453
11454 /*
11455 * Local argument list.
11456 */
11457 if (wp->w_alist == &global_alist)
11458 {
11459 if (put_line(fd, "argglobal") == FAIL)
11460 return FAIL;
11461 }
11462 else
11463 {
11464 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11465 flagp == &vop_flags
11466 || !(*flagp & SSOP_CURDIR)
11467 || wp->w_localdir != NULL, flagp) == FAIL)
11468 return FAIL;
11469 }
11470
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011471 /* Only when part of a session: restore the argument index. Some
11472 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011473 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011474 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011476 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477 || put_eol(fd) == FAIL)
11478 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011479 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480 }
11481
11482 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011483 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011484 {
11485 /*
11486 * Load the file.
11487 */
11488 if (wp->w_buffer->b_ffname != NULL
11489#ifdef FEAT_QUICKFIX
11490 && !bt_nofile(wp->w_buffer)
11491#endif
11492 )
11493 {
11494 /*
11495 * Editing a file in this buffer: use ":edit file".
11496 * This may have side effects! (e.g., compressed or network file).
11497 */
11498 if (fputs("edit ", fd) < 0
11499 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11500 return FAIL;
11501 }
11502 else
11503 {
11504 /* No file in this buffer, just make it empty. */
11505 if (put_line(fd, "enew") == FAIL)
11506 return FAIL;
11507#ifdef FEAT_QUICKFIX
11508 if (wp->w_buffer->b_ffname != NULL)
11509 {
11510 /* The buffer does have a name, but it's not a file name. */
11511 if (fputs("file ", fd) < 0
11512 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11513 return FAIL;
11514 }
11515#endif
11516 do_cursor = FALSE;
11517 }
11518 }
11519
11520 /*
11521 * Local mappings and abbreviations.
11522 */
11523 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11524 && makemap(fd, wp->w_buffer) == FAIL)
11525 return FAIL;
11526
11527 /*
11528 * Local options. Need to go to the window temporarily.
11529 * Store only local values when using ":mkview" and when ":mksession" is
11530 * used and 'sessionoptions' doesn't include "options".
11531 * Some folding options are always stored when "folds" is included,
11532 * otherwise the folds would not be restored correctly.
11533 */
11534 save_curwin = curwin;
11535 curwin = wp;
11536 curbuf = curwin->w_buffer;
11537 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11538 f = makeset(fd, OPT_LOCAL,
11539 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11540#ifdef FEAT_FOLDING
11541 else if (*flagp & SSOP_FOLDS)
11542 f = makefoldset(fd);
11543#endif
11544 else
11545 f = OK;
11546 curwin = save_curwin;
11547 curbuf = curwin->w_buffer;
11548 if (f == FAIL)
11549 return FAIL;
11550
11551#ifdef FEAT_FOLDING
11552 /*
11553 * Save Folds when 'buftype' is empty and for help files.
11554 */
11555 if ((*flagp & SSOP_FOLDS)
11556 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011557# ifdef FEAT_QUICKFIX
11558 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11559# endif
11560 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011561 {
11562 if (put_folds(fd, wp) == FAIL)
11563 return FAIL;
11564 }
11565#endif
11566
11567 /*
11568 * Set the cursor after creating folds, since that moves the cursor.
11569 */
11570 if (do_cursor)
11571 {
11572
11573 /* Restore the cursor line in the file and relatively in the
11574 * window. Don't use "G", it changes the jumplist. */
11575 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11576 (long)wp->w_cursor.lnum,
11577 (long)(wp->w_cursor.lnum - wp->w_topline),
11578 (long)wp->w_height / 2, (long)wp->w_height) < 0
11579 || put_eol(fd) == FAIL
11580 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11581 || put_line(fd, "exe s:l") == FAIL
11582 || put_line(fd, "normal! zt") == FAIL
11583 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11584 || put_eol(fd) == FAIL)
11585 return FAIL;
11586 /* Restore the cursor column and left offset when not wrapping. */
11587 if (wp->w_cursor.col == 0)
11588 {
11589 if (put_line(fd, "normal! 0") == FAIL)
11590 return FAIL;
11591 }
11592 else
11593 {
11594 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11595 {
11596 if (fprintf(fd,
11597 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011598 (long)wp->w_virtcol + 1,
11599 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600 (long)wp->w_width / 2, (long)wp->w_width) < 0
11601 || put_eol(fd) == FAIL
11602 || put_line(fd, "if s:c > 0") == FAIL
11603 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011604 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11605 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011606 || put_eol(fd) == FAIL
11607 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011608 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011609 || put_eol(fd) == FAIL
11610 || put_line(fd, "endif") == FAIL)
11611 return FAIL;
11612 }
11613 else
11614 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011615 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011616 || put_eol(fd) == FAIL)
11617 return FAIL;
11618 }
11619 }
11620 }
11621
11622 /*
11623 * Local directory.
11624 */
11625 if (wp->w_localdir != NULL)
11626 {
11627 if (fputs("lcd ", fd) < 0
11628 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11629 || put_eol(fd) == FAIL)
11630 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011631 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011632 }
11633
11634 return OK;
11635}
11636
11637/*
11638 * Write an argument list to the session file.
11639 * Returns FAIL if writing fails.
11640 */
11641 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011642ses_arglist(
11643 FILE *fd,
11644 char *cmd,
11645 garray_T *gap,
11646 int fullname, /* TRUE: use full path name */
11647 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011648{
11649 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011650 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011651 char_u *s;
11652
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011653 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11654 return FAIL;
11655 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011656 return FAIL;
11657 for (i = 0; i < gap->ga_len; ++i)
11658 {
11659 /* NULL file names are skipped (only happens when out of memory). */
11660 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11661 if (s != NULL)
11662 {
11663 if (fullname)
11664 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011665 buf = alloc(MAXPATHL);
11666 if (buf != NULL)
11667 {
11668 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11669 s = buf;
11670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011671 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011672 if (fputs("argadd ", fd) < 0
11673 || ses_put_fname(fd, s, flagp) == FAIL
11674 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011675 {
11676 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011678 }
11679 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680 }
11681 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011682 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011683}
11684
11685/*
11686 * Write a buffer name to the session file.
11687 * Also ends the line.
11688 * Returns FAIL if writing fails.
11689 */
11690 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011691ses_fname(FILE *fd, buf_T *buf, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011692{
11693 char_u *name;
11694
11695 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011696 * the session file will be sourced.
11697 * Don't do this for ":mkview", we don't know the current directory.
11698 * Don't do this after ":lcd", we don't keep track of what the current
11699 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011700 if (buf->b_sfname != NULL
11701 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011702 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011703#ifdef FEAT_AUTOCHDIR
11704 && !p_acd
11705#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011706 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011707 name = buf->b_sfname;
11708 else
11709 name = buf->b_ffname;
11710 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11711 return FAIL;
11712 return OK;
11713}
11714
11715/*
11716 * Write a file name to the session file.
11717 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11718 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011719 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011720 */
11721 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011722ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011723{
11724 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011725 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011726 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727
11728 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011729 if (sname == NULL)
11730 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011732 if (*flagp & SSOP_SLASH)
11733 {
11734 /* change all backslashes to forward slashes */
11735 for (p = sname; *p != NUL; mb_ptr_adv(p))
11736 if (*p == '\\')
11737 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011739
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011740 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011741 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011742 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011743 if (p == NULL)
11744 return FAIL;
11745
11746 /* write the result */
11747 if (fputs((char *)p, fd) < 0)
11748 retval = FAIL;
11749
11750 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751 return retval;
11752}
11753
11754/*
11755 * ":loadview [nr]"
11756 */
11757 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011758ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011759{
11760 char_u *fname;
11761
11762 fname = get_view_file(*eap->arg);
11763 if (fname != NULL)
11764 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011765 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766 vim_free(fname);
11767 }
11768}
11769
11770/*
11771 * Get the name of the view file for the current buffer.
11772 */
11773 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011774get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011775{
11776 int len = 0;
11777 char_u *p, *s;
11778 char_u *retval;
11779 char_u *sname;
11780
11781 if (curbuf->b_ffname == NULL)
11782 {
11783 EMSG(_(e_noname));
11784 return NULL;
11785 }
11786 sname = home_replace_save(NULL, curbuf->b_ffname);
11787 if (sname == NULL)
11788 return NULL;
11789
11790 /*
11791 * We want a file name without separators, because we're not going to make
11792 * a directory.
11793 * "normal" path separator -> "=+"
11794 * "=" -> "=="
11795 * ":" path separator -> "=-"
11796 */
11797 for (p = sname; *p; ++p)
11798 if (*p == '=' || vim_ispathsep(*p))
11799 ++len;
11800 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11801 if (retval != NULL)
11802 {
11803 STRCPY(retval, p_vdir);
11804 add_pathsep(retval);
11805 s = retval + STRLEN(retval);
11806 for (p = sname; *p; ++p)
11807 {
11808 if (*p == '=')
11809 {
11810 *s++ = '=';
11811 *s++ = '=';
11812 }
11813 else if (vim_ispathsep(*p))
11814 {
11815 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011816#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011817 if (*p == ':')
11818 *s++ = '-';
11819 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011820#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011821 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011822 }
11823 else
11824 *s++ = *p;
11825 }
11826 *s++ = '=';
11827 *s++ = c;
11828 STRCPY(s, ".vim");
11829 }
11830
11831 vim_free(sname);
11832 return retval;
11833}
11834
11835#endif /* FEAT_SESSION */
11836
11837/*
11838 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11839 * Return FAIL for a write error.
11840 */
11841 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011842put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011843{
11844 if (
11845#ifdef USE_CRNL
11846 (
11847# ifdef MKSESSION_NL
11848 !mksession_nl &&
11849# endif
11850 (putc('\r', fd) < 0)) ||
11851#endif
11852 (putc('\n', fd) < 0))
11853 return FAIL;
11854 return OK;
11855}
11856
11857/*
11858 * Write a line to "fd".
11859 * Return FAIL for a write error.
11860 */
11861 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011862put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863{
11864 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11865 return FAIL;
11866 return OK;
11867}
11868
11869#ifdef FEAT_VIMINFO
11870/*
11871 * ":rviminfo" and ":wviminfo".
11872 */
11873 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011874ex_viminfo(
11875 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011876{
11877 char_u *save_viminfo;
11878
11879 save_viminfo = p_viminfo;
11880 if (*p_viminfo == NUL)
11881 p_viminfo = (char_u *)"'100";
11882 if (eap->cmdidx == CMD_rviminfo)
11883 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011884 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11885 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011886 EMSG(_("E195: Cannot open viminfo file for reading"));
11887 }
11888 else
11889 write_viminfo(eap->arg, eap->forceit);
11890 p_viminfo = save_viminfo;
11891}
11892#endif
11893
11894#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011895/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011896 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011897 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011898 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011899 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011900dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011901{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011902 if (fname == NULL)
11903 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011904 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905}
11906#endif
11907
11908/*
11909 * ":behave {mswin,xterm}"
11910 */
11911 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011912ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011913{
11914 if (STRCMP(eap->arg, "mswin") == 0)
11915 {
11916 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11917 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11918 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11919 set_option_value((char_u *)"keymodel", 0L,
11920 (char_u *)"startsel,stopsel", 0);
11921 }
11922 else if (STRCMP(eap->arg, "xterm") == 0)
11923 {
11924 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11925 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11926 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11927 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11928 }
11929 else
11930 EMSG2(_(e_invarg2), eap->arg);
11931}
11932
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011933#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11934/*
11935 * Function given to ExpandGeneric() to obtain the possible arguments of the
11936 * ":behave {mswin,xterm}" command.
11937 */
11938 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011939get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011940{
11941 if (idx == 0)
11942 return (char_u *)"mswin";
11943 if (idx == 1)
11944 return (char_u *)"xterm";
11945 return NULL;
11946}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020011947
11948/*
11949 * Function given to ExpandGeneric() to obtain the possible arguments of the
11950 * ":messages {clear}" command.
11951 */
11952 char_u *
11953get_messages_arg(expand_T *xp UNUSED, int idx)
11954{
11955 if (idx == 0)
11956 return (char_u *)"clear";
11957 return NULL;
11958}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011959#endif
11960
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961#ifdef FEAT_AUTOCMD
11962static int filetype_detect = FALSE;
11963static int filetype_plugin = FALSE;
11964static int filetype_indent = FALSE;
11965
11966/*
11967 * ":filetype [plugin] [indent] {on,off,detect}"
11968 * on: Load the filetype.vim file to install autocommands for file types.
11969 * off: Load the ftoff.vim file to remove all autocommands for file types.
11970 * plugin on: load filetype.vim and ftplugin.vim
11971 * plugin off: load ftplugof.vim
11972 * indent on: load filetype.vim and indent.vim
11973 * indent off: load indoff.vim
11974 */
11975 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011976ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977{
11978 char_u *arg = eap->arg;
11979 int plugin = FALSE;
11980 int indent = FALSE;
11981
11982 if (*eap->arg == NUL)
11983 {
11984 /* Print current status. */
11985 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11986 filetype_detect ? "ON" : "OFF",
11987 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11988 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11989 return;
11990 }
11991
11992 /* Accept "plugin" and "indent" in any order. */
11993 for (;;)
11994 {
11995 if (STRNCMP(arg, "plugin", 6) == 0)
11996 {
11997 plugin = TRUE;
11998 arg = skipwhite(arg + 6);
11999 continue;
12000 }
12001 if (STRNCMP(arg, "indent", 6) == 0)
12002 {
12003 indent = TRUE;
12004 arg = skipwhite(arg + 6);
12005 continue;
12006 }
12007 break;
12008 }
12009 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12010 {
12011 if (*arg == 'o' || !filetype_detect)
12012 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012013 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012014 filetype_detect = TRUE;
12015 if (plugin)
12016 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012017 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012018 filetype_plugin = TRUE;
12019 }
12020 if (indent)
12021 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012022 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012023 filetype_indent = TRUE;
12024 }
12025 }
12026 if (*arg == 'd')
12027 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012028 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012029 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012030 }
12031 }
12032 else if (STRCMP(arg, "off") == 0)
12033 {
12034 if (plugin || indent)
12035 {
12036 if (plugin)
12037 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012038 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012039 filetype_plugin = FALSE;
12040 }
12041 if (indent)
12042 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012043 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044 filetype_indent = FALSE;
12045 }
12046 }
12047 else
12048 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012049 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050 filetype_detect = FALSE;
12051 }
12052 }
12053 else
12054 EMSG2(_(e_invarg2), arg);
12055}
12056
12057/*
12058 * ":setfiletype {name}"
12059 */
12060 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012061ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012062{
12063 if (!did_filetype)
12064 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
12065}
12066#endif
12067
Bram Moolenaar071d4272004-06-13 20:20:40 +000012068 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012069ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012070{
12071#ifdef FEAT_DIGRAPHS
12072 if (*eap->arg != NUL)
12073 putdigraph(eap->arg);
12074 else
12075 listdigraphs();
12076#else
12077 EMSG(_("E196: No digraphs in this version"));
12078#endif
12079}
12080
12081 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012082ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083{
12084 int flags = 0;
12085
12086 if (eap->cmdidx == CMD_setlocal)
12087 flags = OPT_LOCAL;
12088 else if (eap->cmdidx == CMD_setglobal)
12089 flags = OPT_GLOBAL;
12090#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
12091 if (cmdmod.browse && flags == 0)
12092 ex_options(eap);
12093 else
12094#endif
12095 (void)do_set(eap->arg, flags);
12096}
12097
12098#ifdef FEAT_SEARCH_EXTRA
12099/*
12100 * ":nohlsearch"
12101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012102 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012103ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012104{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010012105 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012106 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012107}
12108
12109/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012110 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012111 * Sets nextcmd to the start of the next command, if any. Also called when
12112 * skipping commands to find the next command.
12113 */
12114 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012115ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012116{
12117 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012118 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119 char_u *end;
12120 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012121 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012122
12123 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012124 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012125 else
12126 {
12127 EMSG(e_invcmd);
12128 return;
12129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012130
12131 /* First clear any old pattern. */
12132 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012133 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012134
12135 if (ends_excmd(*eap->arg))
12136 end = eap->arg;
12137 else if ((STRNICMP(eap->arg, "none", 4) == 0
12138 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
12139 end = eap->arg + 4;
12140 else
12141 {
12142 p = skiptowhite(eap->arg);
12143 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012144 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012145 p = skipwhite(p);
12146 if (*p == NUL)
12147 {
12148 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012149 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012150 EMSG2(_(e_invarg2), eap->arg);
12151 return;
12152 }
12153 end = skip_regexp(p + 1, *p, TRUE, NULL);
12154 if (!eap->skip)
12155 {
12156 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12157 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012158 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159 eap->errmsg = e_trailing;
12160 return;
12161 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012162 if (*end != *p)
12163 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012164 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012165 EMSG2(_(e_invarg2), p);
12166 return;
12167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012168
12169 c = *end;
12170 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012171 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012172 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012173 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012174 }
12175 }
12176 eap->nextcmd = find_nextcmd(end);
12177}
12178#endif
12179
12180#ifdef FEAT_CRYPT
12181/*
12182 * ":X": Get crypt key
12183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012185ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012187 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012188 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189}
12190#endif
12191
12192#ifdef FEAT_FOLDING
12193 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012194ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012195{
12196 if (foldManualAllowed(TRUE))
12197 foldCreate(eap->line1, eap->line2);
12198}
12199
12200 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012201ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012202{
12203 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12204 eap->forceit, FALSE);
12205}
12206
12207 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012208ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012209{
12210 linenr_T lnum;
12211
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012212#ifdef FEAT_CLIPBOARD
12213 start_global_changes();
12214#endif
12215
Bram Moolenaar071d4272004-06-13 20:20:40 +000012216 /* First set the marks for all lines closed/open. */
12217 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12218 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12219 ml_setmarked(lnum);
12220
12221 /* Execute the command on the marked lines. */
12222 global_exe(eap->arg);
12223 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012224#ifdef FEAT_CLIPBOARD
12225 end_global_changes();
12226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227}
12228#endif