blob: 13d5fe495ee0722828e26bed94e6d57fed8b581f [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"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200497#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100498
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499static char_u dollar_command[2] = {'$', 0};
500
501
502#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000503/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504typedef struct
505{
506 char_u *line; /* command line */
507 linenr_T lnum; /* sourcing_lnum of the line */
508} wcmd_T;
509
510/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000511 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000513 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000515struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516{
517 garray_T *lines_gap; /* growarray with line info */
518 int current_line; /* last read line from growarray */
519 int repeating; /* TRUE when looping a second time */
520 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100521 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 void *cookie;
523};
524
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100525static char_u *get_loop_line(int c, void *cookie, int indent);
526static int store_loop_line(garray_T *gap, char_u *line);
527static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000528
529/* Struct to save a few things while debugging. Used in do_cmdline() only. */
530struct dbg_stuff
531{
532 int trylevel;
533 int force_abort;
534 except_T *caught_stack;
535 char_u *vv_exception;
536 char_u *vv_throwpoint;
537 int did_emsg;
538 int got_int;
539 int did_throw;
540 int need_rethrow;
541 int check_cstack;
542 except_T *current_exception;
543};
544
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100545static void save_dbg_stuff(struct dbg_stuff *dsp);
546static void restore_dbg_stuff(struct dbg_stuff *dsp);
Bram Moolenaared203462004-06-16 11:19:22 +0000547
548 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100549save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000550{
551 dsp->trylevel = trylevel; trylevel = 0;
552 dsp->force_abort = force_abort; force_abort = FALSE;
553 dsp->caught_stack = caught_stack; caught_stack = NULL;
554 dsp->vv_exception = v_exception(NULL);
555 dsp->vv_throwpoint = v_throwpoint(NULL);
556
557 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
558 dsp->did_emsg = did_emsg; did_emsg = FALSE;
559 dsp->got_int = got_int; got_int = FALSE;
560 dsp->did_throw = did_throw; did_throw = FALSE;
561 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
562 dsp->check_cstack = check_cstack; check_cstack = FALSE;
563 dsp->current_exception = current_exception; current_exception = NULL;
564}
565
566 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100567restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000568{
569 suppress_errthrow = FALSE;
570 trylevel = dsp->trylevel;
571 force_abort = dsp->force_abort;
572 caught_stack = dsp->caught_stack;
573 (void)v_exception(dsp->vv_exception);
574 (void)v_throwpoint(dsp->vv_throwpoint);
575 did_emsg = dsp->did_emsg;
576 got_int = dsp->got_int;
577 did_throw = dsp->did_throw;
578 need_rethrow = dsp->need_rethrow;
579 check_cstack = dsp->check_cstack;
580 current_exception = dsp->current_exception;
581}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582#endif
583
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584/*
585 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
586 * command is given.
587 */
588 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100589do_exmode(
590 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591{
592 int save_msg_scroll;
593 int prev_msg_row;
594 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100595 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000596
597 if (improved)
598 exmode_active = EXMODE_VIM;
599 else
600 exmode_active = EXMODE_NORMAL;
601 State = NORMAL;
602
603 /* When using ":global /pat/ visual" and then "Q" we return to continue
604 * the :global command. */
605 if (global_busy)
606 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
608 save_msg_scroll = msg_scroll;
609 ++RedrawingDisabled; /* don't redisplay the window */
610 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611#ifdef FEAT_GUI
612 /* Ignore scrollbar and mouse events in Ex mode */
613 ++hold_gui_events;
614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615
616 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
617 while (exmode_active)
618 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000619 /* Check for a ":normal" command and no more characters left. */
620 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
621 {
622 exmode_active = FALSE;
623 break;
624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 msg_scroll = TRUE;
626 need_wait_return = FALSE;
627 ex_pressedreturn = FALSE;
628 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100629 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 prev_msg_row = msg_row;
631 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 if (improved)
633 {
634 cmdline_row = msg_row;
635 do_cmdline(NULL, getexline, NULL, 0);
636 }
637 else
638 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
639 lines_left = Rows - 1;
640
Bram Moolenaardf177f62005-02-22 08:39:57 +0000641 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100642 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000644 if (curbuf->b_ml.ml_flags & ML_EMPTY)
645 EMSG(_(e_emptybuf));
646 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000648 if (ex_pressedreturn)
649 {
650 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000651 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000652 msg_row = prev_msg_row;
653 if (prev_msg_row == Rows - 1)
654 msg_row--;
655 }
656 msg_col = 0;
657 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
658 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000661 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
662 {
663 if (curbuf->b_ml.ml_flags & ML_EMPTY)
664 EMSG(_(e_emptybuf));
665 else
666 EMSG(_("E501: At end-of-file"));
667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 }
669
670#ifdef FEAT_GUI
671 --hold_gui_events;
672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 --RedrawingDisabled;
674 --no_wait_return;
675 update_screen(CLEAR);
676 need_wait_return = FALSE;
677 msg_scroll = save_msg_scroll;
678}
679
680/*
681 * Execute a simple command line. Used for translated commands like "*".
682 */
683 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100684do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685{
686 return do_cmdline(cmd, NULL, NULL,
687 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
688}
689
690/*
691 * do_cmdline(): execute one Ex command line
692 *
693 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100694 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 * 2. Split up in parts separated with '|'.
696 *
697 * This function can be called recursively!
698 *
699 * flags:
700 * DOCMD_VERBOSE - The command will be included in the error message.
701 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100702 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 * DOCMD_KEYTYPED - Don't reset KeyTyped.
704 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
705 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
706 *
707 * return FAIL if cmdline could not be executed, OK otherwise
708 */
709 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100710do_cmdline(
711 char_u *cmdline,
712 char_u *(*fgetline)(int, void *, int),
713 void *cookie, /* argument for fgetline() */
714 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715{
716 char_u *next_cmdline; /* next cmd to execute */
717 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100718 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 static int recursive = 0; /* recursive depth */
720 int msg_didout_before_start = 0;
721 int count = 0; /* line number count */
722 int did_inc = FALSE; /* incremented RedrawingDisabled */
723 int retval = OK;
724#ifdef FEAT_EVAL
725 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000726 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 int current_line = 0; /* active line in lines_ga */
728 char_u *fname = NULL; /* function or script name */
729 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
730 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000731 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 int initial_trylevel;
733 struct msglist **saved_msg_list = NULL;
734 struct msglist *private_msg_list;
735
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100736 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100737 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000739 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000741 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100743# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744# define cmd_cookie cookie
745#endif
746 static int call_depth = 0; /* recursiveness */
747
748#ifdef FEAT_EVAL
749 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
750 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000751 * This ensures that the do_errthrow() call in do_one_cmd() does not
752 * combine the messages stored by an earlier invocation of do_one_cmd()
753 * with the command name of the later one. This would happen when
754 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 saved_msg_list = msg_list;
756 msg_list = &private_msg_list;
757 private_msg_list = NULL;
758#endif
759
760 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100761 * here. The value of 200 allows nested function calls, ":source", etc.
762 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100763 if (call_depth >= 200
764#ifdef FEAT_EVAL
765 && call_depth >= p_mfd
766#endif
767 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 {
769 EMSG(_("E169: Command too recursive"));
770#ifdef FEAT_EVAL
771 /* When converting to an exception, we do not include the command name
772 * since this is not an error of the specific command. */
773 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
774 msg_list = saved_msg_list;
775#endif
776 return FAIL;
777 }
778 ++call_depth;
779
780#ifdef FEAT_EVAL
781 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000782 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 cstack.cs_trylevel = 0;
784 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000785 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
787
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100788 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789
790 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100791 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000792 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 ++ex_nesting_level;
794
795 /* Get the function or script name and the address where the next breakpoint
796 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000797 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 {
799 fname = func_name(real_cookie);
800 breakpoint = func_breakpoint(real_cookie);
801 dbg_tick = func_dbg_tick(real_cookie);
802 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100803 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 {
805 fname = sourcing_name;
806 breakpoint = source_breakpoint(real_cookie);
807 dbg_tick = source_dbg_tick(real_cookie);
808 }
809
810 /*
811 * Initialize "force_abort" and "suppress_errthrow" at the top level.
812 */
813 if (!recursive)
814 {
815 force_abort = FALSE;
816 suppress_errthrow = FALSE;
817 }
818
819 /*
820 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000821 * exception handling (used when debugging). Otherwise clear it to avoid
822 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000824 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000825 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000826 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200827 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828
829 initial_trylevel = trylevel;
830
831 /*
832 * "did_throw" will be set to TRUE when an exception is being thrown.
833 */
834 did_throw = FALSE;
835#endif
836 /*
837 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000838 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 * If force_abort is set, we cancel everything.
840 */
841 did_emsg = FALSE;
842
843 /*
844 * KeyTyped is only set when calling vgetc(). Reset it here when not
845 * calling vgetc() (sourced command lines).
846 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100847 if (!(flags & DOCMD_KEYTYPED)
848 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 KeyTyped = FALSE;
850
851 /*
852 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000853 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 * - for multiple commands on one line, separated with '|'
855 * - when repeating until there are no more lines (for ":source")
856 */
857 next_cmdline = cmdline;
858 do
859 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000860#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100861 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000862#endif
863
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000864 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 if (next_cmdline == NULL
866#ifdef FEAT_EVAL
867 && !force_abort
868 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000869 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870#endif
871 )
872 did_emsg = FALSE;
873
874 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000875 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100876 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 * 3. If a line is given: Make a copy, so we can mess with it.
878 */
879
880#ifdef FEAT_EVAL
881 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000882 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 {
884 /* Each '|' separated command is stored separately in lines_ga, to
885 * be able to jump to it. Don't use next_cmdline now. */
886 vim_free(cmdline_copy);
887 cmdline_copy = NULL;
888
889 /* Check if a function has returned or, unless it has an unclosed
890 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000891 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000893# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000894 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000895 func_line_end(real_cookie);
896# endif
897 if (func_has_ended(real_cookie))
898 {
899 retval = FAIL;
900 break;
901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000903#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000904 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100905 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000906 script_line_end();
907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908
909 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100910 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 {
912 retval = FAIL;
913 break;
914 }
915
916 /* If breakpoints have been added/deleted need to check for it. */
917 if (breakpoint != NULL && dbg_tick != NULL
918 && *dbg_tick != debug_tick)
919 {
920 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100921 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 fname, sourcing_lnum);
923 *dbg_tick = debug_tick;
924 }
925
926 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
927 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
928
929 /* Did we encounter a breakpoint? */
930 if (breakpoint != NULL && *breakpoint != 0
931 && *breakpoint <= sourcing_lnum)
932 {
933 dbg_breakpoint(fname, sourcing_lnum);
934 /* Find next breakpoint. */
935 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100936 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 fname, sourcing_lnum);
938 *dbg_tick = debug_tick;
939 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000940# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000941 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000942 {
943 if (getline_is_func)
944 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100945 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000946 script_line_start();
947 }
948# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 }
950
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000951 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000953 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100954 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 * below, so that it stores lines in or reads them from
956 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000957 * while/for loop. */
958 cmd_getline = get_loop_line;
959 cmd_cookie = (void *)&cmd_loop_cookie;
960 cmd_loop_cookie.lines_gap = &lines_ga;
961 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100962 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000963 cmd_loop_cookie.cookie = cookie;
964 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 }
966 else
967 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100968 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 cmd_cookie = cookie;
970 }
971#endif
972
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100973 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 if (next_cmdline == NULL)
975 {
976 /*
977 * Need to set msg_didout for the first line after an ":if",
978 * otherwise the ":if" will be overwritten.
979 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100980 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100982 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983#ifdef FEAT_EVAL
984 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
985#else
986 0
987#endif
988 )) == NULL)
989 {
990 /* Don't call wait_return for aborted command line. The NULL
991 * returned for the end of a sourced file or executed function
992 * doesn't do this. */
993 if (KeyTyped && !(flags & DOCMD_REPEAT))
994 need_wait_return = FALSE;
995 retval = FAIL;
996 break;
997 }
998 used_getline = TRUE;
999
1000 /*
1001 * Keep the first typed line. Clear it when more lines are typed.
1002 */
1003 if (flags & DOCMD_KEEPLINE)
1004 {
1005 vim_free(repeat_cmdline);
1006 if (count == 0)
1007 repeat_cmdline = vim_strsave(next_cmdline);
1008 else
1009 repeat_cmdline = NULL;
1010 }
1011 }
1012
1013 /* 3. Make a copy of the command so we can mess with it. */
1014 else if (cmdline_copy == NULL)
1015 {
1016 next_cmdline = vim_strsave(next_cmdline);
1017 if (next_cmdline == NULL)
1018 {
1019 EMSG(_(e_outofmem));
1020 retval = FAIL;
1021 break;
1022 }
1023 }
1024 cmdline_copy = next_cmdline;
1025
1026#ifdef FEAT_EVAL
1027 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001028 * Save the current line when inside a ":while" or ":for", and when
1029 * the command looks like a ":while" or ":for", because we may need it
1030 * later. When there is a '|' and another command, it is stored
1031 * separately, because we need to be able to jump back to it from an
1032 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001034 if (current_line == lines_ga.ga_len
1035 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001037 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 {
1039 retval = FAIL;
1040 break;
1041 }
1042 }
1043 did_endif = FALSE;
1044#endif
1045
1046 if (count++ == 0)
1047 {
1048 /*
1049 * All output from the commands is put below each other, without
1050 * waiting for a return. Don't do this when executing commands
1051 * from a script or when being called recursive (e.g. for ":e
1052 * +command file").
1053 */
1054 if (!(flags & DOCMD_NOWAIT) && !recursive)
1055 {
1056 msg_didout_before_start = msg_didout;
1057 msg_didany = FALSE; /* no output yet */
1058 msg_start();
1059 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001060 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 ++RedrawingDisabled;
1062 did_inc = TRUE;
1063 }
1064 }
1065
1066 if (p_verbose >= 15 && sourcing_name != NULL)
1067 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001069 verbose_enter_scroll();
1070
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 smsg((char_u *)_("line %ld: %s"),
1072 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001073 if (msg_silent == 0)
1074 msg_puts((char_u *)"\n"); /* don't overwrite this */
1075
1076 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 --no_wait_return;
1078 }
1079
1080 /*
1081 * 2. Execute one '|' separated command.
1082 * do_one_cmd() will return NULL if there is no trailing '|'.
1083 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1084 */
1085 ++recursive;
1086 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1087#ifdef FEAT_EVAL
1088 &cstack,
1089#endif
1090 cmd_getline, cmd_cookie);
1091 --recursive;
1092
1093#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001094 if (cmd_cookie == (void *)&cmd_loop_cookie)
1095 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001097 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098#endif
1099
1100 if (next_cmdline == NULL)
1101 {
1102 vim_free(cmdline_copy);
1103 cmdline_copy = NULL;
1104#ifdef FEAT_CMDHIST
1105 /*
1106 * If the command was typed, remember it for the ':' register.
1107 * Do this AFTER executing the command to make :@: work.
1108 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001109 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 && new_last_cmdline != NULL)
1111 {
1112 vim_free(last_cmdline);
1113 last_cmdline = new_last_cmdline;
1114 new_last_cmdline = NULL;
1115 }
1116#endif
1117 }
1118 else
1119 {
1120 /* need to copy the command after the '|' to cmdline_copy, for the
1121 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001122 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 next_cmdline = cmdline_copy;
1124 }
1125
1126
1127#ifdef FEAT_EVAL
1128 /* reset did_emsg for a function that is not aborted by an error */
1129 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001130 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 && !func_has_abort(real_cookie))
1132 did_emsg = FALSE;
1133
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001134 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 {
1136 ++current_line;
1137
1138 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001139 * An ":endwhile", ":endfor" and ":continue" is handled here.
1140 * If we were executing commands, jump back to the ":while" or
1141 * ":for".
1142 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001144 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001146 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001148 /* Jump back to the matching ":while" or ":for". Be careful
1149 * not to use a cs_line[] from an entry that isn't a ":while"
1150 * or ":for": It would make "current_line" invalid and can
1151 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 if (!did_emsg && !got_int && !did_throw
1153 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001154 && (cstack.cs_flags[cstack.cs_idx]
1155 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 && cstack.cs_line[cstack.cs_idx] >= 0
1157 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1158 {
1159 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001160 /* remember we jumped there */
1161 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 line_breakcheck(); /* check if CTRL-C typed */
1163
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001164 /* Check for the next breakpoint at or after the ":while"
1165 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 if (breakpoint != NULL)
1167 {
1168 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001169 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 fname,
1171 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1172 *dbg_tick = debug_tick;
1173 }
1174 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001175 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001177 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001179 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1180 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 }
1182 }
1183
1184 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001185 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001187 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001189 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1191 }
1192 }
1193
1194 /*
1195 * When not inside any ":while" loop, clear remembered lines.
1196 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001197 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 {
1199 if (lines_ga.ga_len > 0)
1200 {
1201 sourcing_lnum =
1202 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1203 free_cmdlines(&lines_ga);
1204 }
1205 current_line = 0;
1206 }
1207
1208 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001209 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1210 * being restored at the ":endtry". Reset them here and set the
1211 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1212 * This includes the case where a missing ":endif", ":endwhile" or
1213 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001215 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001217 cstack.cs_lflags &= ~CSL_HAD_FINA;
1218 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1219 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 did_throw ? (void *)current_exception : NULL);
1221 did_emsg = got_int = did_throw = FALSE;
1222 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1223 }
1224
1225 /* Update global "trylevel" for recursive calls to do_cmdline() from
1226 * within this loop. */
1227 trylevel = initial_trylevel + cstack.cs_trylevel;
1228
1229 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001230 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 * files) is aborted because of an error, an interrupt, or an uncaught
1232 * exception, cancel everything. If it is left normally, reset
1233 * force_abort to get the non-EH compatible abortion behavior for
1234 * the rest of the script.
1235 */
1236 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1237 force_abort = FALSE;
1238
1239 /* Convert an interrupt to an exception if appropriate. */
1240 (void)do_intthrow(&cstack);
1241#endif /* FEAT_EVAL */
1242
1243 }
1244 /*
1245 * Continue executing command lines when:
1246 * - no CTRL-C typed, no aborting error, no exception thrown or try
1247 * conditionals need to be checked for executing finally clauses or
1248 * catching an interrupt exception
1249 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001250 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 * looping for ":source" command or function call.
1252 */
1253 while (!((got_int
1254#ifdef FEAT_EVAL
1255 || (did_emsg && force_abort) || did_throw
1256#endif
1257 )
1258#ifdef FEAT_EVAL
1259 && cstack.cs_trylevel == 0
1260#endif
1261 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001262 && !(did_emsg
1263#ifdef FEAT_EVAL
1264 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001265 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001266 * the :endtry to be missed. */
1267 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1268#endif
1269 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001270 && (getline_equal(fgetline, cookie, getexmodeline)
1271 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 && (next_cmdline != NULL
1273#ifdef FEAT_EVAL
1274 || cstack.cs_idx >= 0
1275#endif
1276 || (flags & DOCMD_REPEAT)));
1277
1278 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001279 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280#ifdef FEAT_EVAL
1281 free_cmdlines(&lines_ga);
1282 ga_clear(&lines_ga);
1283
1284 if (cstack.cs_idx >= 0)
1285 {
1286 /*
1287 * If a sourced file or executed function ran to its end, report the
1288 * unclosed conditional.
1289 */
1290 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001291 && ((getline_equal(fgetline, cookie, getsourceline)
1292 && !source_finished(fgetline, cookie))
1293 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 && !func_has_ended(real_cookie))))
1295 {
1296 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1297 EMSG(_(e_endtry));
1298 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1299 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001300 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1301 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 else
1303 EMSG(_(e_endif));
1304 }
1305
1306 /*
1307 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1308 * ":endtry" in a sourced file or executed function. If the try
1309 * conditional is in its finally clause, ignore anything pending.
1310 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001311 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 */
1313 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001314 {
1315 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1316
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001317 if (idx >= 0)
1318 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001319 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1320 &cstack.cs_looplevel);
1321 }
1322 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 trylevel = initial_trylevel;
1324 }
1325
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001326 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1327 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001329 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 ? (char_u *)"endfunction" : (char_u *)NULL);
1331
1332 if (trylevel == 0)
1333 {
1334 /*
1335 * When an exception is being thrown out of the outermost try
1336 * conditional, discard the uncaught exception, disable the conversion
1337 * of interrupts or errors to exceptions, and ensure that no more
1338 * commands are executed.
1339 */
1340 if (did_throw)
1341 {
1342 void *p = NULL;
1343 char_u *saved_sourcing_name;
1344 int saved_sourcing_lnum;
1345 struct msglist *messages = NULL, *next;
1346
1347 /*
1348 * If the uncaught exception is a user exception, report it as an
1349 * error. If it is an error exception, display the saved error
1350 * message now. For an interrupt exception, do nothing; the
1351 * interrupt message is given elsewhere.
1352 */
1353 switch (current_exception->type)
1354 {
1355 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001356 vim_snprintf((char *)IObuff, IOSIZE,
1357 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 current_exception->value);
1359 p = vim_strsave(IObuff);
1360 break;
1361 case ET_ERROR:
1362 messages = current_exception->messages;
1363 current_exception->messages = NULL;
1364 break;
1365 case ET_INTERRUPT:
1366 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 }
1368
1369 saved_sourcing_name = sourcing_name;
1370 saved_sourcing_lnum = sourcing_lnum;
1371 sourcing_name = current_exception->throw_name;
1372 sourcing_lnum = current_exception->throw_lnum;
1373 current_exception->throw_name = NULL;
1374
1375 discard_current_exception(); /* uses IObuff if 'verbose' */
1376 suppress_errthrow = TRUE;
1377 force_abort = TRUE;
1378
1379 if (messages != NULL)
1380 {
1381 do
1382 {
1383 next = messages->next;
1384 emsg(messages->msg);
1385 vim_free(messages->msg);
1386 vim_free(messages);
1387 messages = next;
1388 }
1389 while (messages != NULL);
1390 }
1391 else if (p != NULL)
1392 {
1393 emsg(p);
1394 vim_free(p);
1395 }
1396 vim_free(sourcing_name);
1397 sourcing_name = saved_sourcing_name;
1398 sourcing_lnum = saved_sourcing_lnum;
1399 }
1400
1401 /*
1402 * On an interrupt or an aborting error not converted to an exception,
1403 * disable the conversion of errors to exceptions. (Interrupts are not
1404 * converted any more, here.) This enables also the interrupt message
1405 * when force_abort is set and did_emsg unset in case of an interrupt
1406 * from a finally clause after an error.
1407 */
1408 else if (got_int || (did_emsg && force_abort))
1409 suppress_errthrow = TRUE;
1410 }
1411
1412 /*
1413 * The current cstack will be freed when do_cmdline() returns. An uncaught
1414 * exception will have to be rethrown in the previous cstack. If a function
1415 * has just returned or a script file was just finished and the previous
1416 * cstack belongs to the same function or, respectively, script file, it
1417 * will have to be checked for finally clauses to be executed due to the
1418 * ":return" or ":finish". This is done in do_one_cmd().
1419 */
1420 if (did_throw)
1421 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001422 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001424 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 && ex_nesting_level > func_level(real_cookie) + 1))
1426 {
1427 if (!did_throw)
1428 check_cstack = TRUE;
1429 }
1430 else
1431 {
1432 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001433 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 --ex_nesting_level;
1435 /*
1436 * Go to debug mode when returning from a function in which we are
1437 * single-stepping.
1438 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001439 if ((getline_equal(fgetline, cookie, getsourceline)
1440 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001442 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 ? (char_u *)_("End of sourced file")
1444 : (char_u *)_("End of function"));
1445 }
1446
1447 /*
1448 * Restore the exception environment (done after returning from the
1449 * debugger).
1450 */
1451 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001452 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453
1454 msg_list = saved_msg_list;
1455#endif /* FEAT_EVAL */
1456
1457 /*
1458 * If there was too much output to fit on the command line, ask the user to
1459 * hit return before redrawing the screen. With the ":global" command we do
1460 * this only once after the command is finished.
1461 */
1462 if (did_inc)
1463 {
1464 --RedrawingDisabled;
1465 --no_wait_return;
1466 msg_scroll = FALSE;
1467
1468 /*
1469 * When just finished an ":if"-":else" which was typed, no need to
1470 * wait for hit-return. Also for an error situation.
1471 */
1472 if (retval == FAIL
1473#ifdef FEAT_EVAL
1474 || (did_endif && KeyTyped && !did_emsg)
1475#endif
1476 )
1477 {
1478 need_wait_return = FALSE;
1479 msg_didany = FALSE; /* don't wait when restarting edit */
1480 }
1481 else if (need_wait_return)
1482 {
1483 /*
1484 * The msg_start() above clears msg_didout. The wait_return we do
1485 * here should not overwrite the command that may be shown before
1486 * doing that.
1487 */
1488 msg_didout |= msg_didout_before_start;
1489 wait_return(FALSE);
1490 }
1491 }
1492
Bram Moolenaar2a942252012-11-28 23:03:07 +01001493#ifdef FEAT_EVAL
1494 did_endif = FALSE; /* in case do_cmdline used recursively */
1495#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 /*
1497 * Reset if_level, in case a sourced script file contains more ":if" than
1498 * ":endif" (could be ":if x | foo | endif").
1499 */
1500 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001501#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001502
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 --call_depth;
1504 return retval;
1505}
1506
1507#ifdef FEAT_EVAL
1508/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001509 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 */
1511 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001512get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001514 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 wcmd_T *wp;
1516 char_u *line;
1517
1518 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1519 {
1520 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001521 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001523 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 if (cp->getline == NULL)
1525 line = getcmdline(c, 0L, indent);
1526 else
1527 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001528 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 ++cp->current_line;
1530
1531 return line;
1532 }
1533
1534 KeyTyped = FALSE;
1535 ++cp->current_line;
1536 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1537 sourcing_lnum = wp->lnum;
1538 return vim_strsave(wp->line);
1539}
1540
1541/*
1542 * Store a line in "gap" so that a ":while" loop can execute it again.
1543 */
1544 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001545store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546{
1547 if (ga_grow(gap, 1) == FAIL)
1548 return FAIL;
1549 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1550 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1551 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 return OK;
1553}
1554
1555/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001556 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 */
1558 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001559free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560{
1561 while (gap->ga_len > 0)
1562 {
1563 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1564 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 }
1566}
1567#endif
1568
1569/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001570 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1571 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001574getline_equal(
1575 char_u *(*fgetline)(int, void *, int),
1576 void *cookie UNUSED, /* argument for fgetline() */
1577 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578{
1579#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001580 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001581 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582
Bram Moolenaar89d40322006-08-29 15:30:07 +00001583 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001584 * function that's originally used to obtain the lines. This may be
1585 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001586 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001587 cp = (struct loop_cookie *)cookie;
1588 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 {
1590 gp = cp->getline;
1591 cp = cp->cookie;
1592 }
1593 return gp == func;
1594#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001595 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596#endif
1597}
1598
1599#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1600/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001601 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 * getline function. Otherwise return "cookie".
1603 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001605getline_cookie(
1606 char_u *(*fgetline)(int, void *, int) UNUSED,
1607 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608{
1609# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001610 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001611 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612
Bram Moolenaar89d40322006-08-29 15:30:07 +00001613 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001614 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001616 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001617 cp = (struct loop_cookie *)cookie;
1618 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 {
1620 gp = cp->getline;
1621 cp = cp->cookie;
1622 }
1623 return cp;
1624# else
1625 return cookie;
1626# endif
1627}
1628#endif
1629
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001630
1631/*
1632 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1633 * ":bwipeout", etc.
1634 * Returns the buffer number.
1635 */
1636 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001637compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001638{
1639 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001640 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001641 int count = offset;
1642
1643 buf = firstbuf;
1644 while (buf->b_next != NULL && buf->b_fnum < lnum)
1645 buf = buf->b_next;
1646 while (count != 0)
1647 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001648 count += (offset < 0) ? 1 : -1;
1649 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1650 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001651 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001652 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001653 if (addr_type == ADDR_LOADED_BUFFERS)
1654 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001655 while (buf->b_ml.ml_mfp == NULL)
1656 {
1657 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1658 if (nextbuf == NULL)
1659 break;
1660 buf = nextbuf;
1661 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001662 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001663 /* we might have gone too far, last buffer is not loadedd */
1664 if (addr_type == ADDR_LOADED_BUFFERS)
1665 while (buf->b_ml.ml_mfp == NULL)
1666 {
1667 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1668 if (nextbuf == NULL)
1669 break;
1670 buf = nextbuf;
1671 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001672 return buf->b_fnum;
1673}
1674
Bram Moolenaarf240e182014-11-27 18:33:02 +01001675#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001676static int current_win_nr(win_T *win);
1677static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001678
1679 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001680current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001681{
1682 win_T *wp;
1683 int nr = 0;
1684
Bram Moolenaar29323592016-07-24 22:04:11 +02001685 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001686 {
1687 ++nr;
1688 if (wp == win)
1689 break;
1690 }
1691 return nr;
1692}
1693
1694 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001695current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001696{
1697 tabpage_T *tp;
1698 int nr = 0;
1699
Bram Moolenaar29323592016-07-24 22:04:11 +02001700 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001701 {
1702 ++nr;
1703 if (tp == tab)
1704 break;
1705 }
1706 return nr;
1707}
1708
1709# define CURRENT_WIN_NR current_win_nr(curwin)
1710# define LAST_WIN_NR current_win_nr(NULL)
1711# define CURRENT_TAB_NR current_tab_nr(curtab)
1712# define LAST_TAB_NR current_tab_nr(NULL)
1713#else
1714# define CURRENT_WIN_NR 1
1715# define LAST_WIN_NR 1
1716# define CURRENT_TAB_NR 1
1717# define LAST_TAB_NR 1
1718#endif
1719
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001720
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721/*
1722 * Execute one Ex command.
1723 *
1724 * If 'sourcing' is TRUE, the command will be included in the error message.
1725 *
1726 * 1. skip comment lines and leading space
1727 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001728 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001729 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001730 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001731 * 6. parse arguments
1732 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001734 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 *
1736 * This function may be called recursively!
1737 */
1738#if (_MSC_VER == 1200)
1739/*
Bram Moolenaared203462004-06-16 11:19:22 +00001740 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001742 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743#endif
1744 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001745do_one_cmd(
1746 char_u **cmdlinep,
1747 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001749 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001751 char_u *(*fgetline)(int, void *, int),
1752 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753{
1754 char_u *p;
1755 linenr_T lnum;
1756 long n;
1757 char_u *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001758 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 exarg_T ea; /* Ex command arguments */
1760 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001761 int save_msg_scroll = msg_scroll;
1762 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001764#ifdef HAVE_SANDBOX
1765 int did_sandbox = FALSE;
1766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 cmdmod_T save_cmdmod;
1768 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001769 char_u *cmd;
Bram Moolenaarded27822017-01-02 14:27:34 +01001770 int address_count = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771
1772 vim_memset(&ea, 0, sizeof(ea));
1773 ea.line1 = 1;
1774 ea.line2 = 1;
1775#ifdef FEAT_EVAL
1776 ++ex_nesting_level;
1777#endif
1778
Bram Moolenaar21691f82012-12-05 19:13:18 +01001779 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 if (quitmore
1781#ifdef FEAT_EVAL
1782 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001783 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001784#endif
1785#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001786 /* avoid that an autocommand, e.g. QuitPre, does this */
1787 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788#endif
1789 )
1790 --quitmore;
1791
1792 /*
1793 * Reset browse, confirm, etc.. They are restored when returning, for
1794 * recursive calls.
1795 */
1796 save_cmdmod = cmdmod;
1797 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1798
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001799 /* "#!anything" is handled like a comment. */
1800 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1801 goto doend;
1802
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 /*
1804 * Repeat until no more command modifiers are found.
1805 */
1806 ea.cmd = *cmdlinep;
1807 for (;;)
1808 {
1809/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001810 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 */
1812 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1813 ++ea.cmd;
1814
1815 /* in ex mode, an empty line works like :+ */
1816 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001817 && (getline_equal(fgetline, cookie, getexmodeline)
1818 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001819 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 {
1821 ea.cmd = (char_u *)"+";
1822 ex_pressedreturn = TRUE;
1823 }
1824
1825 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001826 if (*ea.cmd == '"')
1827 goto doend;
1828 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001829 {
1830 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833
1834/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001835 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 */
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001837 p = skip_range(ea.cmd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 switch (*p)
1839 {
1840 /* When adding an entry, also modify cmd_exists(). */
1841 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1842 break;
1843#ifdef FEAT_WINDOWS
1844 cmdmod.split |= WSP_ABOVE;
1845#endif
1846 continue;
1847
1848 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1849 {
1850#ifdef FEAT_WINDOWS
1851 cmdmod.split |= WSP_BELOW;
1852#endif
1853 continue;
1854 }
1855 if (checkforcmd(&ea.cmd, "browse", 3))
1856 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001857#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 cmdmod.browse = TRUE;
1859#endif
1860 continue;
1861 }
1862 if (!checkforcmd(&ea.cmd, "botright", 2))
1863 break;
1864#ifdef FEAT_WINDOWS
1865 cmdmod.split |= WSP_BOT;
1866#endif
1867 continue;
1868
1869 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1870 break;
1871#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1872 cmdmod.confirm = TRUE;
1873#endif
1874 continue;
1875
1876 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1877 {
1878 cmdmod.keepmarks = TRUE;
1879 continue;
1880 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001881 if (checkforcmd(&ea.cmd, "keepalt", 5))
1882 {
1883 cmdmod.keepalt = TRUE;
1884 continue;
1885 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001886 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1887 {
1888 cmdmod.keeppatterns = TRUE;
1889 continue;
1890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1892 break;
1893 cmdmod.keepjumps = TRUE;
1894 continue;
1895
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001896 case 'f': /* only accept ":filter {pat} cmd" */
1897 {
1898 char_u *reg_pat;
1899
1900 if (!checkforcmd(&p, "filter", 4)
1901 || *p == NUL || ends_excmd(*p))
1902 break;
Bram Moolenaard29459b2016-08-26 22:29:11 +02001903 if (*p == '!')
1904 {
1905 cmdmod.filter_force = TRUE;
1906 p = skipwhite(p + 1);
1907 if (*p == NUL || ends_excmd(*p))
1908 break;
1909 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001910 p = skip_vimgrep_pat(p, &reg_pat, NULL);
1911 if (p == NULL || *p == NUL)
1912 break;
1913 cmdmod.filter_regmatch.regprog =
1914 vim_regcomp(reg_pat, RE_MAGIC);
1915 if (cmdmod.filter_regmatch.regprog == NULL)
1916 break;
1917 ea.cmd = p;
1918 continue;
1919 }
1920
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 /* ":hide" and ":hide | cmd" are not modifiers */
1922 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1923 || *p == NUL || ends_excmd(*p))
1924 break;
1925 ea.cmd = p;
1926 cmdmod.hide = TRUE;
1927 continue;
1928
1929 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1930 {
1931 cmdmod.lockmarks = TRUE;
1932 continue;
1933 }
1934
1935 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1936 break;
1937#ifdef FEAT_WINDOWS
1938 cmdmod.split |= WSP_ABOVE;
1939#endif
1940 continue;
1941
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001942 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001943 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001944#ifdef FEAT_AUTOCMD
1945 if (cmdmod.save_ei == NULL)
1946 {
1947 /* Set 'eventignore' to "all". Restore the
1948 * existing option value later. */
1949 cmdmod.save_ei = vim_strsave(p_ei);
1950 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001951 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001952 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001953#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001954 continue;
1955 }
Bram Moolenaar3bcfca32016-07-30 19:39:29 +02001956 if (!checkforcmd(&ea.cmd, "noswapfile", 3))
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001957 break;
1958 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001959 continue;
1960
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1962 break;
1963#ifdef FEAT_WINDOWS
1964 cmdmod.split |= WSP_BELOW;
1965#endif
1966 continue;
1967
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001968 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1969 {
1970#ifdef HAVE_SANDBOX
1971 if (!did_sandbox)
1972 ++sandbox;
1973 did_sandbox = TRUE;
1974#endif
1975 continue;
1976 }
1977 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001979 if (save_msg_silent == -1)
1980 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 ++msg_silent;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001982 if (*ea.cmd == '!' && !VIM_ISWHITE(ea.cmd[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 {
1984 /* ":silent!", but not "silent !cmd" */
1985 ea.cmd = skipwhite(ea.cmd + 1);
1986 ++emsg_silent;
1987 ++did_esilent;
1988 }
1989 continue;
1990
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001991 case 't': if (checkforcmd(&p, "tab", 3))
1992 {
1993#ifdef FEAT_WINDOWS
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001994 long tabnr = get_address(&ea, &ea.cmd, ADDR_TABS,
Bram Moolenaarded27822017-01-02 14:27:34 +01001995 ea.skip, FALSE, 1);
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001996 if (tabnr == MAXLNUM)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001997 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001998 else
1999 {
2000 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2001 {
2002 errormsg = (char_u *)_(e_invrange);
2003 goto doend;
2004 }
2005 cmdmod.tab = tabnr + 1;
2006 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002007 ea.cmd = p;
2008#endif
2009 continue;
2010 }
2011 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 break;
2013#ifdef FEAT_WINDOWS
2014 cmdmod.split |= WSP_TOP;
2015#endif
2016 continue;
2017
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002018 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
2019 break;
2020 if (save_msg_silent == -1)
2021 save_msg_silent = msg_silent;
2022 msg_silent = 0;
2023 continue;
2024
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
2026 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002027#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 cmdmod.split |= WSP_VERT;
2029#endif
2030 continue;
2031 }
2032 if (!checkforcmd(&p, "verbose", 4))
2033 break;
2034 if (verbose_save < 0)
2035 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00002036 if (vim_isdigit(*ea.cmd))
2037 p_verbose = atoi((char *)ea.cmd);
2038 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 p_verbose = 1;
2040 ea.cmd = p;
2041 continue;
2042 }
2043 break;
2044 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002045 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046
2047#ifdef FEAT_EVAL
2048 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2049 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2050#else
2051 ea.skip = (if_level > 0);
2052#endif
2053
2054#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002055# ifdef FEAT_PROFILE
2056 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002057 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002058 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002059 if (getline_equal(fgetline, cookie, get_func_line))
2060 func_line_exec(getline_cookie(fgetline, cookie));
2061 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002062 script_line_exec();
2063 }
2064#endif
2065
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 /* May go to debug mode. If this happens and the ">quit" debug command is
2067 * used, throw an interrupt exception and skip the next command. */
2068 dbg_check_breakpoint(&ea);
2069 if (!ea.skip && got_int)
2070 {
2071 ea.skip = TRUE;
2072 (void)do_intthrow(cstack);
2073 }
2074#endif
2075
2076/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002077 * 3. Skip over the range to find the command. Let "p" point to after it.
2078 *
2079 * We need the command to know what kind of range it uses.
2080 */
2081 cmd = ea.cmd;
2082 ea.cmd = skip_range(ea.cmd, NULL);
2083 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2084 ea.cmd = skipwhite(ea.cmd + 1);
2085 p = find_command(&ea, NULL);
2086
2087/*
2088 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 *
2090 * where 'addr' is:
2091 *
2092 * % (entire file)
2093 * $ [+-NUM]
2094 * 'x [+-NUM] (where x denotes a currently defined mark)
2095 * . [+-NUM]
2096 * [+-NUM]..
2097 * NUM
2098 *
2099 * The ea.cmd pointer is updated to point to the first character following the
2100 * range spec. If an initial address is found, but no second, the upper bound
2101 * is equal to the lower.
2102 */
2103
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002104 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002105 if (!IS_USER_CMDIDX(ea.cmdidx))
2106 {
2107 if (ea.cmdidx != CMD_SIZE)
2108 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2109 else
2110 ea.addr_type = ADDR_LINES;
2111
2112#ifdef FEAT_WINDOWS
2113 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002114 if (ea.cmdidx == CMD_wincmd && p != NULL)
2115 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002116#endif
2117 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002118
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002120 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 for (;;)
2122 {
2123 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002124 switch (ea.addr_type)
2125 {
2126 case ADDR_LINES:
2127 /* default is current line number */
2128 ea.line2 = curwin->w_cursor.lnum;
2129 break;
2130 case ADDR_WINDOWS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002131 ea.line2 = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002132 break;
2133 case ADDR_ARGUMENTS:
2134 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002135 if (ea.line2 > ARGCOUNT)
2136 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002137 break;
2138 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002139 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002140 ea.line2 = curbuf->b_fnum;
2141 break;
2142 case ADDR_TABS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002143 ea.line2 = CURRENT_TAB_NR;
2144 break;
2145 case ADDR_TABS_RELATIVE:
2146 ea.line2 = 1;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002147 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002148#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002149 case ADDR_QUICKFIX:
2150 ea.line2 = qf_get_cur_valid_idx(&ea);
2151 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002152#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002155 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip,
Bram Moolenaarded27822017-01-02 14:27:34 +01002156 ea.addr_count == 0, address_count++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 if (ea.cmd == NULL) /* error detected */
2158 goto doend;
2159 if (lnum == MAXLNUM)
2160 {
2161 if (*ea.cmd == '%') /* '%' - all lines */
2162 {
2163 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002164 switch (ea.addr_type)
2165 {
2166 case ADDR_LINES:
2167 ea.line1 = 1;
2168 ea.line2 = curbuf->b_ml.ml_line_count;
2169 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002170 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002171 {
2172 buf_T *buf = firstbuf;
2173
2174 while (buf->b_next != NULL
2175 && buf->b_ml.ml_mfp == NULL)
2176 buf = buf->b_next;
2177 ea.line1 = buf->b_fnum;
2178 buf = lastbuf;
2179 while (buf->b_prev != NULL
2180 && buf->b_ml.ml_mfp == NULL)
2181 buf = buf->b_prev;
2182 ea.line2 = buf->b_fnum;
2183 break;
2184 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002185 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002186 ea.line1 = firstbuf->b_fnum;
2187 ea.line2 = lastbuf->b_fnum;
2188 break;
2189 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002190 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002191 if (IS_USER_CMDIDX(ea.cmdidx))
2192 {
2193 ea.line1 = 1;
2194 ea.line2 = ea.addr_type == ADDR_WINDOWS
2195 ? LAST_WIN_NR : LAST_TAB_NR;
2196 }
2197 else
2198 {
2199 /* there is no Vim command which uses '%' and
2200 * ADDR_WINDOWS or ADDR_TABS */
2201 errormsg = (char_u *)_(e_invrange);
2202 goto doend;
2203 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002204 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002205 case ADDR_TABS_RELATIVE:
2206 errormsg = (char_u *)_(e_invrange);
2207 goto doend;
2208 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002209 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002210 if (ARGCOUNT == 0)
2211 ea.line1 = ea.line2 = 0;
2212 else
2213 {
2214 ea.line1 = 1;
2215 ea.line2 = ARGCOUNT;
2216 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002217 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002218#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002219 case ADDR_QUICKFIX:
2220 ea.line1 = 1;
2221 ea.line2 = qf_get_size(&ea);
2222 if (ea.line2 == 0)
2223 ea.line2 = 1;
2224 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002225#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 ++ea.addr_count;
2228 }
2229 /* '*' - visual area */
2230 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2231 {
2232 pos_T *fp;
2233
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002234 if (ea.addr_type != ADDR_LINES)
2235 {
2236 errormsg = (char_u *)_(e_invrange);
2237 goto doend;
2238 }
2239
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 ++ea.cmd;
2241 if (!ea.skip)
2242 {
2243 fp = getmark('<', FALSE);
2244 if (check_mark(fp) == FAIL)
2245 goto doend;
2246 ea.line1 = fp->lnum;
2247 fp = getmark('>', FALSE);
2248 if (check_mark(fp) == FAIL)
2249 goto doend;
2250 ea.line2 = fp->lnum;
2251 ++ea.addr_count;
2252 }
2253 }
2254 }
2255 else
2256 ea.line2 = lnum;
2257 ea.addr_count++;
2258
2259 if (*ea.cmd == ';')
2260 {
2261 if (!ea.skip)
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002262 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 curwin->w_cursor.lnum = ea.line2;
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +01002264 /* don't leave the cursor on an illegal line or column */
2265 check_cursor();
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 }
2268 else if (*ea.cmd != ',')
2269 break;
2270 ++ea.cmd;
2271 }
2272
2273 /* One address given: set start and end lines */
2274 if (ea.addr_count == 1)
2275 {
2276 ea.line1 = ea.line2;
2277 /* ... but only implicit: really no address given */
2278 if (lnum == MAXLNUM)
2279 ea.addr_count = 0;
2280 }
2281
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002283 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 */
2285
2286 /*
2287 * Skip ':' and any white space
2288 */
2289 ea.cmd = skipwhite(ea.cmd);
2290 while (*ea.cmd == ':')
2291 ea.cmd = skipwhite(ea.cmd + 1);
2292
2293 /*
2294 * If we got a line, but no command, then go to the line.
2295 * If we find a '|' or '\n' we set ea.nextcmd.
2296 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002297 if (*ea.cmd == NUL || *ea.cmd == '"'
2298 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 {
2300 /*
2301 * strange vi behaviour:
2302 * ":3" jumps to line 3
2303 * ":3|..." prints line 3
2304 * ":|" prints current line
2305 */
2306 if (ea.skip) /* skip this if inside :if */
2307 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002308 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
2310 ea.cmdidx = CMD_print;
2311 ea.argt = RANGE+COUNT+TRLBAR;
2312 if ((errormsg = invalid_range(&ea)) == NULL)
2313 {
2314 correct_range(&ea);
2315 ex_print(&ea);
2316 }
2317 }
2318 else if (ea.addr_count != 0)
2319 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002320 if (ea.line2 > curbuf->b_ml.ml_line_count)
2321 {
2322 /* With '-' in 'cpoptions' a line number past the file is an
2323 * error, otherwise put it at the end of the file. */
2324 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2325 ea.line2 = -1;
2326 else
2327 ea.line2 = curbuf->b_ml.ml_line_count;
2328 }
2329
2330 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002331 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 else
2333 {
2334 if (ea.line2 == 0)
2335 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 else
2337 curwin->w_cursor.lnum = ea.line2;
2338 beginline(BL_SOL | BL_FIX);
2339 }
2340 }
2341 goto doend;
2342 }
2343
Bram Moolenaard5005162014-08-22 23:05:54 +02002344#ifdef FEAT_AUTOCMD
2345 /* If this looks like an undefined user command and there are CmdUndefined
2346 * autocommands defined, trigger the matching autocommands. */
2347 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2348 && ASCII_ISUPPER(*ea.cmd)
2349 && has_cmdundefined())
2350 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002351 int ret;
2352
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002353 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002354 while (ASCII_ISALNUM(*p))
2355 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002356 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002357 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2358 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002359 /* If the autocommands did something and didn't cause an error, try
2360 * finding the command again. */
Bram Moolenaareac784e2016-07-28 22:08:24 +02002361 p = (ret && !aborting()) ? find_command(&ea, NULL) : ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002362 }
2363#endif
2364
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365#ifdef FEAT_USR_CMDS
2366 if (p == NULL)
2367 {
2368 if (!ea.skip)
2369 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2370 goto doend;
2371 }
2372 /* Check for wrong commands. */
2373 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2374 {
2375 errormsg = uc_fun_cmd();
2376 goto doend;
2377 }
2378#endif
2379 if (ea.cmdidx == CMD_SIZE)
2380 {
2381 if (!ea.skip)
2382 {
2383 STRCPY(IObuff, _("E492: Not an editor command"));
2384 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002385 {
2386 /* If the modifier was parsed OK the error must be in the
2387 * following command */
2388 if (after_modifier != NULL)
2389 append_command(after_modifier);
2390 else
2391 append_command(*cmdlinep);
2392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002394 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 }
2396 goto doend;
2397 }
2398
Bram Moolenaar958636c2014-10-21 20:01:58 +02002399 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2400 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002401#ifdef HAVE_EX_SCRIPT_NI
2402 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2403#endif
2404 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
2406#ifndef FEAT_EVAL
2407 /*
2408 * When the expression evaluation is disabled, recognize the ":if" and
2409 * ":endif" commands and ignore everything in between it.
2410 */
2411 if (ea.cmdidx == CMD_if)
2412 ++if_level;
2413 if (if_level)
2414 {
2415 if (ea.cmdidx == CMD_endif)
2416 --if_level;
2417 goto doend;
2418 }
2419
2420#endif
2421
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002422 /* forced commands */
2423 if (*p == '!' && ea.cmdidx != CMD_substitute
2424 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 {
2426 ++p;
2427 ea.forceit = TRUE;
2428 }
2429 else
2430 ea.forceit = FALSE;
2431
2432/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002433 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002435 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002436 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437
2438 if (!ea.skip)
2439 {
2440#ifdef HAVE_SANDBOX
2441 if (sandbox != 0 && !(ea.argt & SBOXOK))
2442 {
2443 /* Command not allowed in sandbox. */
2444 errormsg = (char_u *)_(e_sandbox);
2445 goto doend;
2446 }
2447#endif
2448 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2449 {
2450 /* Command not allowed in non-'modifiable' buffer */
2451 errormsg = (char_u *)_(e_modifiable);
2452 goto doend;
2453 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002454
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002455 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002456 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002458 /* Command not allowed when editing the command line. */
Bram Moolenaar75c19462017-02-12 18:34:05 +01002459 errormsg = (char_u *)_(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 goto doend;
2461 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002462#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002463 /* Disallow editing another buffer when "curbuf_lock" is set.
2464 * Do allow ":edit" (check for argument later).
2465 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002466 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002467 && ea.cmdidx != CMD_edit
2468 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002469 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002470 && curbuf_locked())
2471 goto doend;
2472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473
2474 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2475 {
2476 /* no range allowed */
2477 errormsg = (char_u *)_(e_norange);
2478 goto doend;
2479 }
2480 }
2481
2482 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2483 {
2484 errormsg = (char_u *)_(e_nobang);
2485 goto doend;
2486 }
2487
2488 /*
2489 * Don't complain about the range if it is not used
2490 * (could happen if line_count is accidentally set to 0).
2491 */
2492 if (!ea.skip && !ni)
2493 {
2494 /*
2495 * If the range is backwards, ask for confirmation and, if given, swap
2496 * ea.line1 & ea.line2 so it's forwards again.
2497 * When global command is busy, don't ask, will fail below.
2498 */
2499 if (!global_busy && ea.line1 > ea.line2)
2500 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002501 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002503 if (sourcing || exmode_active)
2504 {
2505 errormsg = (char_u *)_("E493: Backwards range given");
2506 goto doend;
2507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 if (ask_yesno((char_u *)
2509 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002510 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 }
2512 lnum = ea.line1;
2513 ea.line1 = ea.line2;
2514 ea.line2 = lnum;
2515 }
2516 if ((errormsg = invalid_range(&ea)) != NULL)
2517 goto doend;
2518 }
2519
2520 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2521 ea.line2 = 1;
2522
2523 correct_range(&ea);
2524
2525#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002526 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2527 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 {
2529 /* Put the first line at the start of a closed fold, put the last line
2530 * at the end of a closed fold. */
2531 (void)hasFolding(ea.line1, &ea.line1, NULL);
2532 (void)hasFolding(ea.line2, NULL, &ea.line2);
2533 }
2534#endif
2535
2536#ifdef FEAT_QUICKFIX
2537 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002538 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 * option here, so things like % get expanded.
2540 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002541 p = replace_makeprg(&ea, p, cmdlinep);
2542 if (p == NULL)
2543 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544#endif
2545
2546 /*
2547 * Skip to start of argument.
2548 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2549 */
2550 if (ea.cmdidx == CMD_bang)
2551 ea.arg = p;
2552 else
2553 ea.arg = skipwhite(p);
2554
2555 /*
2556 * Check for "++opt=val" argument.
2557 * Must be first, allow ":w ++enc=utf8 !cmd"
2558 */
2559 if (ea.argt & ARGOPT)
2560 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2561 if (getargopt(&ea) == FAIL && !ni)
2562 {
2563 errormsg = (char_u *)_(e_invarg);
2564 goto doend;
2565 }
2566
2567 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2568 {
2569 if (*ea.arg == '>') /* append */
2570 {
2571 if (*++ea.arg != '>') /* typed wrong */
2572 {
2573 errormsg = (char_u *)_("E494: Use w or w>>");
2574 goto doend;
2575 }
2576 ea.arg = skipwhite(ea.arg + 1);
2577 ea.append = TRUE;
2578 }
2579 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2580 {
2581 ++ea.arg;
2582 ea.usefilter = TRUE;
2583 }
2584 }
2585
2586 if (ea.cmdidx == CMD_read)
2587 {
2588 if (ea.forceit)
2589 {
2590 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2591 ea.forceit = FALSE;
2592 }
2593 else if (*ea.arg == '!') /* :r !filter */
2594 {
2595 ++ea.arg;
2596 ea.usefilter = TRUE;
2597 }
2598 }
2599
2600 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2601 {
2602 ea.amount = 1;
2603 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2604 {
2605 ++ea.arg;
2606 ++ea.amount;
2607 }
2608 ea.arg = skipwhite(ea.arg);
2609 }
2610
2611 /*
2612 * Check for "+command" argument, before checking for next command.
2613 * Don't do this for ":read !cmd" and ":write !cmd".
2614 */
2615 if ((ea.argt & EDITCMD) && !ea.usefilter)
2616 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2617
2618 /*
2619 * Check for '|' to separate commands and '"' to start comments.
2620 * Don't do this for ":read !cmd" and ":write !cmd".
2621 */
2622 if ((ea.argt & TRLBAR) && !ea.usefilter)
2623 separate_nextcmd(&ea);
2624
2625 /*
2626 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002627 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2628 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002630 else if (ea.cmdidx == CMD_bang
2631 || ea.cmdidx == CMD_global
2632 || ea.cmdidx == CMD_vglobal
2633 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 {
2635 for (p = ea.arg; *p; ++p)
2636 {
2637 /* Remove one backslash before a newline, so that it's possible to
2638 * pass a newline to the shell and also a newline that is preceded
2639 * with a backslash. This makes it impossible to end a shell
2640 * command in a backslash, but that doesn't appear useful.
2641 * Halving the number of backslashes is incompatible with previous
2642 * versions. */
2643 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002644 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 else if (*p == '\n')
2646 {
2647 ea.nextcmd = p + 1;
2648 *p = NUL;
2649 break;
2650 }
2651 }
2652 }
2653
2654 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2655 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002656 buf_T *buf;
2657
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002659 switch (ea.addr_type)
2660 {
2661 case ADDR_LINES:
2662 ea.line2 = curbuf->b_ml.ml_line_count;
2663 break;
2664 case ADDR_LOADED_BUFFERS:
2665 buf = firstbuf;
2666 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2667 buf = buf->b_next;
2668 ea.line1 = buf->b_fnum;
2669 buf = lastbuf;
2670 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2671 buf = buf->b_prev;
2672 ea.line2 = buf->b_fnum;
2673 break;
2674 case ADDR_BUFFERS:
2675 ea.line1 = firstbuf->b_fnum;
2676 ea.line2 = lastbuf->b_fnum;
2677 break;
2678 case ADDR_WINDOWS:
2679 ea.line2 = LAST_WIN_NR;
2680 break;
2681 case ADDR_TABS:
2682 ea.line2 = LAST_TAB_NR;
2683 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002684 case ADDR_TABS_RELATIVE:
2685 ea.line2 = 1;
2686 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002687 case ADDR_ARGUMENTS:
2688 if (ARGCOUNT == 0)
2689 ea.line1 = ea.line2 = 0;
2690 else
2691 ea.line2 = ARGCOUNT;
2692 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002693#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002694 case ADDR_QUICKFIX:
2695 ea.line2 = qf_get_size(&ea);
2696 if (ea.line2 == 0)
2697 ea.line2 = 1;
2698 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002699#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 }
2702
2703 /* accept numbered register only when no count allowed (:put) */
2704 if ( (ea.argt & REGSTR)
2705 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002706 /* Do not allow register = for user commands */
2707 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2709 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002710#ifndef FEAT_CLIPBOARD
2711 /* check these explicitly for a more specific error message */
2712 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002714 errormsg = (char_u *)_(e_invalidreg);
2715 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 }
2717#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002718 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2719 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002720 {
2721 ea.regname = *ea.arg++;
2722#ifdef FEAT_EVAL
2723 /* for '=' register: accept the rest of the line as an expression */
2724 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2725 {
2726 set_expr_line(vim_strsave(ea.arg));
2727 ea.arg += STRLEN(ea.arg);
2728 }
2729#endif
2730 ea.arg = skipwhite(ea.arg);
2731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 }
2733
2734 /*
2735 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2736 * count, it's a buffer name.
2737 */
2738 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2739 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002740 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 {
2742 n = getdigits(&ea.arg);
2743 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002744 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 {
2746 errormsg = (char_u *)_(e_zerocount);
2747 goto doend;
2748 }
2749 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2750 {
2751 ea.line2 = n;
2752 if (ea.addr_count == 0)
2753 ea.addr_count = 1;
2754 }
2755 else
2756 {
2757 ea.line1 = ea.line2;
2758 ea.line2 += n - 1;
2759 ++ea.addr_count;
2760 /*
2761 * Be vi compatible: no error message for out of range.
2762 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002763 if (ea.addr_type == ADDR_LINES
2764 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 ea.line2 = curbuf->b_ml.ml_line_count;
2766 }
2767 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002768
2769 /*
2770 * Check for flags: 'l', 'p' and '#'.
2771 */
2772 if (ea.argt & EXFLAGS)
2773 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002775 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002776 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 {
2778 errormsg = (char_u *)_(e_trailing);
2779 goto doend;
2780 }
2781
2782 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2783 {
2784 errormsg = (char_u *)_(e_argreq);
2785 goto doend;
2786 }
2787
2788#ifdef FEAT_EVAL
2789 /*
2790 * Skip the command when it's not going to be executed.
2791 * The commands like :if, :endif, etc. always need to be executed.
2792 * Also make an exception for commands that handle a trailing command
2793 * themselves.
2794 */
2795 if (ea.skip)
2796 {
2797 switch (ea.cmdidx)
2798 {
2799 /* commands that need evaluation */
2800 case CMD_while:
2801 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002802 case CMD_for:
2803 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 case CMD_if:
2805 case CMD_elseif:
2806 case CMD_else:
2807 case CMD_endif:
2808 case CMD_try:
2809 case CMD_catch:
2810 case CMD_finally:
2811 case CMD_endtry:
2812 case CMD_function:
2813 break;
2814
2815 /* Commands that handle '|' themselves. Check: A command should
2816 * either have the TRLBAR flag, appear in this list or appear in
2817 * the list at ":help :bar". */
2818 case CMD_aboveleft:
2819 case CMD_and:
2820 case CMD_belowright:
2821 case CMD_botright:
2822 case CMD_browse:
2823 case CMD_call:
2824 case CMD_confirm:
2825 case CMD_delfunction:
2826 case CMD_djump:
2827 case CMD_dlist:
2828 case CMD_dsearch:
2829 case CMD_dsplit:
2830 case CMD_echo:
2831 case CMD_echoerr:
2832 case CMD_echomsg:
2833 case CMD_echon:
2834 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002835 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 case CMD_help:
2837 case CMD_hide:
2838 case CMD_ijump:
2839 case CMD_ilist:
2840 case CMD_isearch:
2841 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002842 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 case CMD_keepjumps:
2844 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002845 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 case CMD_leftabove:
2847 case CMD_let:
2848 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002849 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002851 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002852 case CMD_noautocmd:
2853 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 case CMD_perl:
2855 case CMD_psearch:
2856 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002857 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002858 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 case CMD_return:
2860 case CMD_rightbelow:
2861 case CMD_ruby:
2862 case CMD_silent:
2863 case CMD_smagic:
2864 case CMD_snomagic:
2865 case CMD_substitute:
2866 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002867 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 case CMD_tcl:
2869 case CMD_throw:
2870 case CMD_tilde:
2871 case CMD_topleft:
2872 case CMD_unlet:
2873 case CMD_verbose:
2874 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002875 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 break;
2877
2878 default: goto doend;
2879 }
2880 }
2881#endif
2882
2883 if (ea.argt & XFILE)
2884 {
2885 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2886 goto doend;
2887 }
2888
2889#ifdef FEAT_LISTCMDS
2890 /*
2891 * Accept buffer name. Cannot be used at the same time with a buffer
2892 * number. Don't do this for a user command.
2893 */
2894 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002895 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 {
2897 /*
2898 * :bdelete, :bwipeout and :bunload take several arguments, separated
2899 * by spaces: find next space (skipping over escaped characters).
2900 * The others take one argument: ignore trailing spaces.
2901 */
2902 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2903 || ea.cmdidx == CMD_bunload)
2904 p = skiptowhite_esc(ea.arg);
2905 else
2906 {
2907 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002908 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 --p;
2910 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002911 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2912 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 if (ea.line2 < 0) /* failed */
2914 goto doend;
2915 ea.addr_count = 1;
2916 ea.arg = skipwhite(p);
2917 }
2918#endif
2919
2920/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002921 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 *
2923 * The "ea" structure holds the arguments that can be used.
2924 */
2925 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002926 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 ea.cookie = cookie;
2928#ifdef FEAT_EVAL
2929 ea.cstack = cstack;
2930#endif
2931
2932#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002933 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 {
2935 /*
2936 * Execute a user-defined command.
2937 */
2938 do_ucmd(&ea);
2939 }
2940 else
2941#endif
2942 {
2943 /*
2944 * Call the function to execute the command.
2945 */
2946 ea.errmsg = NULL;
2947 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2948 if (ea.errmsg != NULL)
2949 errormsg = (char_u *)_(ea.errmsg);
2950 }
2951
2952#ifdef FEAT_EVAL
2953 /*
2954 * If the command just executed called do_cmdline(), any throw or ":return"
2955 * or ":finish" encountered there must also check the cstack of the still
2956 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2957 * exception, or reanimate a returned function or finished script file and
2958 * return or finish it again.
2959 */
2960 if (need_rethrow)
2961 do_throw(cstack);
2962 else if (check_cstack)
2963 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002964 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002966 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 && current_func_returned())
2968 do_return(&ea, TRUE, FALSE, NULL);
2969 }
2970 need_rethrow = check_cstack = FALSE;
2971#endif
2972
2973doend:
2974 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002975 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002977 curwin->w_cursor.col = 0;
2978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979
2980 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2981 {
2982 if (sourcing)
2983 {
2984 if (errormsg != IObuff)
2985 {
2986 STRCPY(IObuff, errormsg);
2987 errormsg = IObuff;
2988 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002989 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 }
2991 emsg(errormsg);
2992 }
2993#ifdef FEAT_EVAL
2994 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002995 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2996 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997#endif
2998
2999 if (verbose_save >= 0)
3000 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00003001#ifdef FEAT_AUTOCMD
3002 if (cmdmod.save_ei != NULL)
3003 {
3004 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003005 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
3006 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00003007 free_string_option(cmdmod.save_ei);
3008 }
3009#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003010 if (cmdmod.filter_regmatch.regprog != NULL)
3011 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012
3013 cmdmod = save_cmdmod;
3014
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003015 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 {
3017 /* messages could be enabled for a serious error, need to check if the
3018 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00003019 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003020 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 emsg_silent -= did_esilent;
3022 if (emsg_silent < 0)
3023 emsg_silent = 0;
3024 /* Restore msg_scroll, it's set by file I/O commands, even when no
3025 * message is actually displayed. */
3026 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003027
3028 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
3029 * somewhere in the line. Put it back in the first column. */
3030 if (redirecting())
3031 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 }
3033
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003034#ifdef HAVE_SANDBOX
3035 if (did_sandbox)
3036 --sandbox;
3037#endif
3038
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3040 ea.nextcmd = NULL;
3041
3042#ifdef FEAT_EVAL
3043 --ex_nesting_level;
3044#endif
3045
3046 return ea.nextcmd;
3047}
3048#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003049 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050#endif
3051
3052/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003053 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 * If there is a match advance "pp" to the argument and return TRUE.
3055 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003056 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003057checkforcmd(
3058 char_u **pp, /* start of command */
3059 char *cmd, /* name of command */
3060 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061{
3062 int i;
3063
3064 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003065 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 break;
3067 if (i >= len && !isalpha((*pp)[i]))
3068 {
3069 *pp = skipwhite(*pp + i);
3070 return TRUE;
3071 }
3072 return FALSE;
3073}
3074
3075/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003076 * Append "cmd" to the error message in IObuff.
3077 * Takes care of limiting the length and handling 0xa0, which would be
3078 * invisible otherwise.
3079 */
3080 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003081append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003082{
3083 char_u *s = cmd;
3084 char_u *d;
3085
3086 STRCAT(IObuff, ": ");
3087 d = IObuff + STRLEN(IObuff);
3088 while (*s != NUL && d - IObuff < IOSIZE - 7)
3089 {
3090 if (
3091#ifdef FEAT_MBYTE
3092 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3093#endif
3094 *s == 0xa0)
3095 {
3096 s +=
3097#ifdef FEAT_MBYTE
3098 enc_utf8 ? 2 :
3099#endif
3100 1;
3101 STRCPY(d, "<a0>");
3102 d += 4;
3103 }
3104 else
3105 MB_COPY_CHAR(s, d);
3106 }
3107 *d = NUL;
3108}
3109
3110/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003112 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003114 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 * Returns NULL for an ambiguous user command.
3116 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003118find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119{
3120 int len;
3121 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003122 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123
3124 /*
3125 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003126 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 * - the 'k' command can directly be followed by any character.
3128 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003129 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003131 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 */
3133 p = eap->cmd;
3134 if (*p == 'k')
3135 {
3136 eap->cmdidx = CMD_k;
3137 ++p;
3138 }
3139 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003140 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3141 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 || p[1] == 'g'
3143 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3144 || p[1] == 'I'
3145 || (p[1] == 'r' && p[2] != 'e')))
3146 {
3147 eap->cmdidx = CMD_substitute;
3148 ++p;
3149 }
3150 else
3151 {
3152 while (ASCII_ISALPHA(*p))
3153 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003154 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003155 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003156 while (ASCII_ISALNUM(*p))
3157 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003158
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 /* check for non-alpha command */
3160 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3161 ++p;
3162 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003163 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3164 {
3165 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3166 * :delete with the 'l' flag. Same for 'p'. */
3167 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003168 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003169 break;
3170 if (i == len - 1)
3171 {
3172 --len;
3173 if (p[-1] == 'l')
3174 eap->flags |= EXFLAG_LIST;
3175 else
3176 eap->flags |= EXFLAG_PRINT;
3177 }
3178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003180 if (ASCII_ISLOWER(eap->cmd[0]))
3181 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003182 int c1 = eap->cmd[0];
3183 int c2 = eap->cmd[1];
3184
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003185 if (command_count != (int)CMD_SIZE)
3186 {
3187 iemsg((char_u *)_("E943: Command table needs to be updated, run 'make cmdidxs'"));
3188 getout(1);
3189 }
3190
3191 /* Use a precomputed index for fast look-up in cmdnames[]
3192 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003193 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3194 if (ASCII_ISLOWER(c2))
3195 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003198 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199
3200 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3201 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3202 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3203 (size_t)len) == 0)
3204 {
3205#ifdef FEAT_EVAL
3206 if (full != NULL
3207 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3208 *full = TRUE;
3209#endif
3210 break;
3211 }
3212
3213#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003214 /* Look for a user defined command as a last resort. Let ":Print" be
3215 * overruled by a user defined command. */
3216 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3217 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003219 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 while (ASCII_ISALNUM(*p))
3221 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003222 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 }
3224#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003225 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 eap->cmdidx = CMD_SIZE;
3227 }
3228
3229 return p;
3230}
3231
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003232#ifdef FEAT_USR_CMDS
3233/*
3234 * Search for a user command that matches "eap->cmd".
3235 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3236 * Return a pointer to just after the command.
3237 * Return NULL if there is no matching command.
3238 */
3239 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003240find_ucmd(
3241 exarg_T *eap,
3242 char_u *p, /* end of the command (possibly including count) */
3243 int *full, /* set to TRUE for a full match */
3244 expand_T *xp, /* used for completion, NULL otherwise */
3245 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003246{
3247 int len = (int)(p - eap->cmd);
3248 int j, k, matchlen = 0;
3249 ucmd_T *uc;
3250 int found = FALSE;
3251 int possible = FALSE;
3252 char_u *cp, *np; /* Point into typed cmd and test name */
3253 garray_T *gap;
3254 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3255 only full match global is accepted. */
3256
3257 /*
3258 * Look for buffer-local user commands first, then global ones.
3259 */
3260 gap = &curbuf->b_ucmds;
3261 for (;;)
3262 {
3263 for (j = 0; j < gap->ga_len; ++j)
3264 {
3265 uc = USER_CMD_GA(gap, j);
3266 cp = eap->cmd;
3267 np = uc->uc_name;
3268 k = 0;
3269 while (k < len && *np != NUL && *cp++ == *np++)
3270 k++;
3271 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3272 {
3273 /* If finding a second match, the command is ambiguous. But
3274 * not if a buffer-local command wasn't a full match and a
3275 * global command is a full match. */
3276 if (k == len && found && *np != NUL)
3277 {
3278 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003279 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003280 amb_local = TRUE;
3281 }
3282
3283 if (!found || (k == len && *np == NUL))
3284 {
3285 /* If we matched up to a digit, then there could
3286 * be another command including the digit that we
3287 * should use instead.
3288 */
3289 if (k == len)
3290 found = TRUE;
3291 else
3292 possible = TRUE;
3293
3294 if (gap == &ucmds)
3295 eap->cmdidx = CMD_USER;
3296 else
3297 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003298 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003299 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003300 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003301
3302# ifdef FEAT_CMDL_COMPL
3303 if (compl != NULL)
3304 *compl = uc->uc_compl;
3305# ifdef FEAT_EVAL
3306 if (xp != NULL)
3307 {
3308 xp->xp_arg = uc->uc_compl_arg;
3309 xp->xp_scriptID = uc->uc_scriptID;
3310 }
3311# endif
3312# endif
3313 /* Do not search for further abbreviations
3314 * if this is an exact match. */
3315 matchlen = k;
3316 if (k == len && *np == NUL)
3317 {
3318 if (full != NULL)
3319 *full = TRUE;
3320 amb_local = FALSE;
3321 break;
3322 }
3323 }
3324 }
3325 }
3326
3327 /* Stop if we found a full match or searched all. */
3328 if (j < gap->ga_len || gap == &ucmds)
3329 break;
3330 gap = &ucmds;
3331 }
3332
3333 /* Only found ambiguous matches. */
3334 if (amb_local)
3335 {
3336 if (xp != NULL)
3337 xp->xp_context = EXPAND_UNSUCCESSFUL;
3338 return NULL;
3339 }
3340
3341 /* The match we found may be followed immediately by a number. Move "p"
3342 * back to point to it. */
3343 if (found || possible)
3344 return p + (matchlen - len);
3345 return p;
3346}
3347#endif
3348
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003350static struct cmdmod
3351{
3352 char *name;
3353 int minlen;
3354 int has_count; /* :123verbose :3tab */
3355} cmdmods[] = {
3356 {"aboveleft", 3, FALSE},
3357 {"belowright", 3, FALSE},
3358 {"botright", 2, FALSE},
3359 {"browse", 3, FALSE},
3360 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003361 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003362 {"hide", 3, FALSE},
3363 {"keepalt", 5, FALSE},
3364 {"keepjumps", 5, FALSE},
3365 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003366 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003367 {"leftabove", 5, FALSE},
3368 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003369 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003370 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003371 {"rightbelow", 6, FALSE},
3372 {"sandbox", 3, FALSE},
3373 {"silent", 3, FALSE},
3374 {"tab", 3, TRUE},
3375 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003376 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003377 {"verbose", 4, TRUE},
3378 {"vertical", 4, FALSE},
3379};
3380
3381/*
3382 * Return length of a command modifier (including optional count).
3383 * Return zero when it's not a modifier.
3384 */
3385 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003386modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003387{
3388 int i, j;
3389 char_u *p = cmd;
3390
3391 if (VIM_ISDIGIT(*cmd))
3392 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003393 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003394 {
3395 for (j = 0; p[j] != NUL; ++j)
3396 if (p[j] != cmdmods[i].name[j])
3397 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003398 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003399 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003400 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003401 }
3402 return 0;
3403}
3404
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405/*
3406 * Return > 0 if an Ex command "name" exists.
3407 * Return 2 if there is an exact match.
3408 * Return 3 if there is an ambiguous match.
3409 */
3410 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003411cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412{
3413 exarg_T ea;
3414 int full = FALSE;
3415 int i;
3416 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003417 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418
3419 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003420 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 {
3422 for (j = 0; name[j] != NUL; ++j)
3423 if (name[j] != cmdmods[i].name[j])
3424 break;
3425 if (name[j] == NUL && j >= cmdmods[i].minlen)
3426 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3427 }
3428
Bram Moolenaara9587612006-05-04 21:47:50 +00003429 /* Check built-in commands and user defined commands.
3430 * For ":2match" and ":3match" we need to skip the number. */
3431 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003433 p = find_command(&ea, &full);
3434 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003436 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3437 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003438 if (*skipwhite(p) != NUL)
3439 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3441}
3442#endif
3443
3444/*
3445 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3446 * we don't need/want deleted. Maybe this could be done better if we didn't
3447 * repeat all this stuff. The only problem is that they may not stay
3448 * perfectly compatible with each other, but then the command line syntax
3449 * probably won't change that much -- webb.
3450 */
3451 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003452set_one_cmd_context(
3453 expand_T *xp,
3454 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455{
3456 char_u *p;
3457 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003458 int len = 0;
3459 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3461 int compl = EXPAND_NOTHING;
3462#endif
3463#ifdef FEAT_CMDL_COMPL
3464 int delim;
3465#endif
3466 int forceit = FALSE;
3467 int usefilter = FALSE; /* filter instead of file name */
3468
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003469 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 xp->xp_pattern = buff;
3471 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003472 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473
3474/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003475 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 */
3477 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3478 ;
3479 xp->xp_pattern = cmd;
3480
3481 if (*cmd == NUL)
3482 return NULL;
3483 if (*cmd == '"') /* ignore comment lines */
3484 {
3485 xp->xp_context = EXPAND_NOTHING;
3486 return NULL;
3487 }
3488
3489/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003490 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 */
3492 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 xp->xp_pattern = cmd;
3494 if (*cmd == NUL)
3495 return NULL;
3496 if (*cmd == '"')
3497 {
3498 xp->xp_context = EXPAND_NOTHING;
3499 return NULL;
3500 }
3501
3502 if (*cmd == '|' || *cmd == '\n')
3503 return cmd + 1; /* There's another command */
3504
3505 /*
3506 * Isolate the command and search for it in the command table.
3507 * Exceptions:
3508 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003509 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3511 */
3512 if (*cmd == 'k' && cmd[1] != 'e')
3513 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003514 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 p = cmd + 1;
3516 }
3517 else
3518 {
3519 p = cmd;
3520 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3521 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003522 /* a user command may contain digits */
3523 if (ASCII_ISUPPER(cmd[0]))
3524 while (ASCII_ISALNUM(*p) || *p == '*')
3525 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003526 /* for python 3.x: ":py3*" commands completion */
3527 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003528 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003529 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003530 while (ASCII_ISALPHA(*p) || *p == '*')
3531 ++p;
3532 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003533 /* check for non-alpha command */
3534 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3535 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003536 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003538 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 {
3540 xp->xp_context = EXPAND_UNSUCCESSFUL;
3541 return NULL;
3542 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003543 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003544 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3545 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3546 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 break;
3548
3549#ifdef FEAT_USR_CMDS
3550 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3552 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553#endif
3554 }
3555
3556 /*
3557 * If the cursor is touching the command, and it ends in an alpha-numeric
3558 * character, complete the command name.
3559 */
3560 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3561 return NULL;
3562
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003563 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 {
3565 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3566 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003567 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 p = cmd + 1;
3569 }
3570#ifdef FEAT_USR_CMDS
3571 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3572 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003573 ea.cmd = cmd;
3574 p = find_ucmd(&ea, p, NULL, xp,
3575# if defined(FEAT_CMDL_COMPL)
3576 &compl
3577# else
3578 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003580 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003581 if (p == NULL)
3582 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 }
3584#endif
3585 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003586 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 {
3588 /* Not still touching the command and it was an illegal one */
3589 xp->xp_context = EXPAND_UNSUCCESSFUL;
3590 return NULL;
3591 }
3592
3593 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3594
3595 if (*p == '!') /* forced commands */
3596 {
3597 forceit = TRUE;
3598 ++p;
3599 }
3600
3601/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003602 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003604 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003605 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606
3607 arg = skipwhite(p);
3608
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003609 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 {
3611 if (*arg == '>') /* append */
3612 {
3613 if (*++arg == '>')
3614 ++arg;
3615 arg = skipwhite(arg);
3616 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003617 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 {
3619 ++arg;
3620 usefilter = TRUE;
3621 }
3622 }
3623
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003624 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 {
3626 usefilter = forceit; /* :r! filter if forced */
3627 if (*arg == '!') /* :r !filter */
3628 {
3629 ++arg;
3630 usefilter = TRUE;
3631 }
3632 }
3633
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003634 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 {
3636 while (*arg == *cmd) /* allow any number of '>' or '<' */
3637 ++arg;
3638 arg = skipwhite(arg);
3639 }
3640
3641 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003642 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 {
3644 /* Check if we're in the +command */
3645 p = arg + 1;
3646 arg = skip_cmd_arg(arg, FALSE);
3647
3648 /* Still touching the command after '+'? */
3649 if (*arg == NUL)
3650 return p;
3651
3652 /* Skip space(s) after +command to get to the real argument */
3653 arg = skipwhite(arg);
3654 }
3655
3656 /*
3657 * Check for '|' to separate commands and '"' to start comments.
3658 * Don't do this for ":read !cmd" and ":write !cmd".
3659 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003660 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 {
3662 p = arg;
3663 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003664 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 p += 2;
3666 while (*p)
3667 {
3668 if (*p == Ctrl_V)
3669 {
3670 if (p[1] != NUL)
3671 ++p;
3672 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003673 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 || *p == '|' || *p == '\n')
3675 {
3676 if (*(p - 1) != '\\')
3677 {
3678 if (*p == '|' || *p == '\n')
3679 return p + 1;
3680 return NULL; /* It's a comment */
3681 }
3682 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003683 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 }
3685 }
3686
3687 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003688 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 vim_strchr((char_u *)"|\"", *arg) == NULL)
3690 return NULL;
3691
3692 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003693 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003695 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003696 while (*p && p < buff + len)
3697 {
3698 if (*p == ' ' || *p == TAB)
3699 {
3700 /* argument starts after a space */
3701 xp->xp_pattern = ++p;
3702 }
3703 else
3704 {
3705 if (*p == '\\' && *(p + 1) != NUL)
3706 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003707 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003708 }
3709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003711 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003713 int c;
3714 int in_quote = FALSE;
3715 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716
3717 /*
3718 * Allow spaces within back-quotes to count as part of the argument
3719 * being expanded.
3720 */
3721 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003722 p = xp->xp_pattern;
3723 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003725#ifdef FEAT_MBYTE
3726 if (has_mbyte)
3727 c = mb_ptr2char(p);
3728 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003730 c = *p;
3731 if (c == '\\' && p[1] != NUL)
3732 ++p;
3733 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 {
3735 if (!in_quote)
3736 {
3737 xp->xp_pattern = p;
3738 bow = p + 1;
3739 }
3740 in_quote = !in_quote;
3741 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003742 /* An argument can contain just about everything, except
3743 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003744 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003745#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003746 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003747#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003748 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003749 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003750 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003751 while (*p != NUL)
3752 {
3753#ifdef FEAT_MBYTE
3754 if (has_mbyte)
3755 c = mb_ptr2char(p);
3756 else
3757#endif
3758 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003759 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003760 break;
3761#ifdef FEAT_MBYTE
3762 if (has_mbyte)
3763 len = (*mb_ptr2len)(p);
3764 else
3765#endif
3766 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003767 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003768 }
3769 if (in_quote)
3770 bow = p;
3771 else
3772 xp->xp_pattern = p;
3773 p -= len;
3774 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003775 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 }
3777
3778 /*
3779 * If we are still inside the quotes, and we passed a space, just
3780 * expand from there.
3781 */
3782 if (bow != NULL && in_quote)
3783 xp->xp_pattern = bow;
3784 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003785
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003786 /* For a shell command more chars need to be escaped. */
3787 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003788 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003789#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003790 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003791#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003792 /* When still after the command name expand executables. */
3793 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003794 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796
3797 /* Check for environment variable */
3798 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003799#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 || *xp->xp_pattern == '%'
3801#endif
3802 )
3803 {
3804 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3805 if (!vim_isIDc(*p))
3806 break;
3807 if (*p == NUL)
3808 {
3809 xp->xp_context = EXPAND_ENV_VARS;
3810 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003811#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3812 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003813 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003814 compl = EXPAND_ENV_VARS;
3815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 }
3817 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003818#if defined(FEAT_CMDL_COMPL)
3819 /* Check for user names */
3820 if (*xp->xp_pattern == '~')
3821 {
3822 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3823 ;
3824 /* Complete ~user only if it partially matches a user name.
3825 * A full match ~user<Tab> will be replaced by user's home
3826 * directory i.e. something like ~user<Tab> -> /home/user/ */
3827 if (*p == NUL && p > xp->xp_pattern + 1
3828 && match_user(xp->xp_pattern + 1) == 1)
3829 {
3830 xp->xp_context = EXPAND_USER;
3831 ++xp->xp_pattern;
3832 }
3833 }
3834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 }
3836
3837/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003838 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003840 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003842 case CMD_find:
3843 case CMD_sfind:
3844 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003845 if (xp->xp_context == EXPAND_FILES)
3846 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003847 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 case CMD_cd:
3849 case CMD_chdir:
3850 case CMD_lcd:
3851 case CMD_lchdir:
3852 if (xp->xp_context == EXPAND_FILES)
3853 xp->xp_context = EXPAND_DIRECTORIES;
3854 break;
3855 case CMD_help:
3856 xp->xp_context = EXPAND_HELP;
3857 xp->xp_pattern = arg;
3858 break;
3859
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003860 /* Command modifiers: return the argument.
3861 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003863 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 case CMD_belowright:
3865 case CMD_botright:
3866 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003867 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003868 case CMD_cdo:
3869 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003871 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 case CMD_folddoclosed:
3873 case CMD_folddoopen:
3874 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003875 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 case CMD_keepjumps:
3877 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003878 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003879 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003881 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003883 case CMD_noautocmd:
3884 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003886 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003888 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003889 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 case CMD_topleft:
3891 case CMD_verbose:
3892 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003893 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 return arg;
3895
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003896 case CMD_filter:
3897 if (*arg != NUL)
3898 arg = skip_vimgrep_pat(arg, NULL, NULL);
3899 if (arg == NULL || *arg == NUL)
3900 {
3901 xp->xp_context = EXPAND_NOTHING;
3902 return NULL;
3903 }
3904 return skipwhite(arg);
3905
Bram Moolenaar4f688582007-07-24 12:34:30 +00003906#ifdef FEAT_CMDL_COMPL
3907# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 case CMD_match:
3909 if (*arg == NUL || !ends_excmd(*arg))
3910 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003911 /* also complete "None" */
3912 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 arg = skipwhite(skiptowhite(arg));
3914 if (*arg != NUL)
3915 {
3916 xp->xp_context = EXPAND_NOTHING;
3917 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3918 }
3919 }
3920 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003921# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923/*
3924 * All completion for the +cmdline_compl feature goes here.
3925 */
3926
3927# ifdef FEAT_USR_CMDS
3928 case CMD_command:
3929 /* Check for attributes */
3930 while (*arg == '-')
3931 {
3932 arg++; /* Skip "-" */
3933 p = skiptowhite(arg);
3934 if (*p == NUL)
3935 {
3936 /* Cursor is still in the attribute */
3937 p = vim_strchr(arg, '=');
3938 if (p == NULL)
3939 {
3940 /* No "=", so complete attribute names */
3941 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3942 xp->xp_pattern = arg;
3943 return NULL;
3944 }
3945
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003946 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 * their arguments as well.
3948 */
3949 if (STRNICMP(arg, "complete", p - arg) == 0)
3950 {
3951 xp->xp_context = EXPAND_USER_COMPLETE;
3952 xp->xp_pattern = p + 1;
3953 return NULL;
3954 }
3955 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3956 {
3957 xp->xp_context = EXPAND_USER_NARGS;
3958 xp->xp_pattern = p + 1;
3959 return NULL;
3960 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003961 else if (STRNICMP(arg, "addr", p - arg) == 0)
3962 {
3963 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3964 xp->xp_pattern = p + 1;
3965 return NULL;
3966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 return NULL;
3968 }
3969 arg = skipwhite(p);
3970 }
3971
3972 /* After the attributes comes the new command name */
3973 p = skiptowhite(arg);
3974 if (*p == NUL)
3975 {
3976 xp->xp_context = EXPAND_USER_COMMANDS;
3977 xp->xp_pattern = arg;
3978 break;
3979 }
3980
3981 /* And finally comes a normal command */
3982 return skipwhite(p);
3983
3984 case CMD_delcommand:
3985 xp->xp_context = EXPAND_USER_COMMANDS;
3986 xp->xp_pattern = arg;
3987 break;
3988# endif
3989
3990 case CMD_global:
3991 case CMD_vglobal:
3992 delim = *arg; /* get the delimiter */
3993 if (delim)
3994 ++arg; /* skip delimiter if there is one */
3995
3996 while (arg[0] != NUL && arg[0] != delim)
3997 {
3998 if (arg[0] == '\\' && arg[1] != NUL)
3999 ++arg;
4000 ++arg;
4001 }
4002 if (arg[0] != NUL)
4003 return arg + 1;
4004 break;
4005 case CMD_and:
4006 case CMD_substitute:
4007 delim = *arg;
4008 if (delim)
4009 {
4010 /* skip "from" part */
4011 ++arg;
4012 arg = skip_regexp(arg, delim, p_magic, NULL);
4013 }
4014 /* skip "to" part */
4015 while (arg[0] != NUL && arg[0] != delim)
4016 {
4017 if (arg[0] == '\\' && arg[1] != NUL)
4018 ++arg;
4019 ++arg;
4020 }
4021 if (arg[0] != NUL) /* skip delimiter */
4022 ++arg;
4023 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
4024 ++arg;
4025 if (arg[0] != NUL)
4026 return arg;
4027 break;
4028 case CMD_isearch:
4029 case CMD_dsearch:
4030 case CMD_ilist:
4031 case CMD_dlist:
4032 case CMD_ijump:
4033 case CMD_psearch:
4034 case CMD_djump:
4035 case CMD_isplit:
4036 case CMD_dsplit:
4037 arg = skipwhite(skipdigits(arg)); /* skip count */
4038 if (*arg == '/') /* Match regexp, not just whole words */
4039 {
4040 for (++arg; *arg && *arg != '/'; arg++)
4041 if (*arg == '\\' && arg[1] != NUL)
4042 arg++;
4043 if (*arg)
4044 {
4045 arg = skipwhite(arg + 1);
4046
4047 /* Check for trailing illegal characters */
4048 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4049 xp->xp_context = EXPAND_NOTHING;
4050 else
4051 return arg;
4052 }
4053 }
4054 break;
4055#ifdef FEAT_AUTOCMD
4056 case CMD_autocmd:
4057 return set_context_in_autocmd(xp, arg, FALSE);
4058
4059 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004060 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 return set_context_in_autocmd(xp, arg, TRUE);
4062#endif
4063 case CMD_set:
4064 set_context_in_set_cmd(xp, arg, 0);
4065 break;
4066 case CMD_setglobal:
4067 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4068 break;
4069 case CMD_setlocal:
4070 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4071 break;
4072 case CMD_tag:
4073 case CMD_stag:
4074 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004075 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 case CMD_tselect:
4077 case CMD_stselect:
4078 case CMD_ptselect:
4079 case CMD_tjump:
4080 case CMD_stjump:
4081 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004082 if (*p_wop != NUL)
4083 xp->xp_context = EXPAND_TAGS_LISTFILES;
4084 else
4085 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 xp->xp_pattern = arg;
4087 break;
4088 case CMD_augroup:
4089 xp->xp_context = EXPAND_AUGROUP;
4090 xp->xp_pattern = arg;
4091 break;
4092#ifdef FEAT_SYN_HL
4093 case CMD_syntax:
4094 set_context_in_syntax_cmd(xp, arg);
4095 break;
4096#endif
4097#ifdef FEAT_EVAL
4098 case CMD_let:
4099 case CMD_if:
4100 case CMD_elseif:
4101 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004102 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 case CMD_echo:
4104 case CMD_echon:
4105 case CMD_execute:
4106 case CMD_echomsg:
4107 case CMD_echoerr:
4108 case CMD_call:
4109 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004110 case CMD_cexpr:
4111 case CMD_caddexpr:
4112 case CMD_cgetexpr:
4113 case CMD_lexpr:
4114 case CMD_laddexpr:
4115 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004116 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 break;
4118
4119 case CMD_unlet:
4120 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4121 arg = xp->xp_pattern + 1;
4122 xp->xp_context = EXPAND_USER_VARS;
4123 xp->xp_pattern = arg;
4124 break;
4125
4126 case CMD_function:
4127 case CMD_delfunction:
4128 xp->xp_context = EXPAND_USER_FUNC;
4129 xp->xp_pattern = arg;
4130 break;
4131
4132 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004133 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 break;
4135#endif
4136 case CMD_highlight:
4137 set_context_in_highlight_cmd(xp, arg);
4138 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004139#ifdef FEAT_CSCOPE
4140 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004141 case CMD_lcscope:
4142 case CMD_scscope:
4143 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004144 break;
4145#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004146#ifdef FEAT_SIGNS
4147 case CMD_sign:
4148 set_context_in_sign_cmd(xp, arg);
4149 break;
4150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151#ifdef FEAT_LISTCMDS
4152 case CMD_bdelete:
4153 case CMD_bwipeout:
4154 case CMD_bunload:
4155 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4156 arg = xp->xp_pattern + 1;
4157 /*FALLTHROUGH*/
4158 case CMD_buffer:
4159 case CMD_sbuffer:
4160 case CMD_checktime:
4161 xp->xp_context = EXPAND_BUFFERS;
4162 xp->xp_pattern = arg;
4163 break;
4164#endif
4165#ifdef FEAT_USR_CMDS
4166 case CMD_USER:
4167 case CMD_USER_BUF:
4168 if (compl != EXPAND_NOTHING)
4169 {
4170 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004171 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 {
4173# ifdef FEAT_MENU
4174 if (compl == EXPAND_MENUS)
4175 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4176# endif
4177 if (compl == EXPAND_COMMANDS)
4178 return arg;
4179 if (compl == EXPAND_MAPPINGS)
4180 return set_context_in_map_cmd(xp, (char_u *)"map",
4181 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004182 /* Find start of last argument. */
4183 p = arg;
4184 while (*p)
4185 {
4186 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004187 /* argument starts after a space */
4188 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004189 else if (*p == '\\' && *(p + 1) != NUL)
4190 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004191 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 xp->xp_pattern = arg;
4194 }
4195 xp->xp_context = compl;
4196 }
4197 break;
4198#endif
4199 case CMD_map: case CMD_noremap:
4200 case CMD_nmap: case CMD_nnoremap:
4201 case CMD_vmap: case CMD_vnoremap:
4202 case CMD_omap: case CMD_onoremap:
4203 case CMD_imap: case CMD_inoremap:
4204 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004205 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004206 case CMD_smap: case CMD_snoremap:
4207 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004209 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 case CMD_unmap:
4211 case CMD_nunmap:
4212 case CMD_vunmap:
4213 case CMD_ounmap:
4214 case CMD_iunmap:
4215 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004216 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004217 case CMD_sunmap:
4218 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004220 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 case CMD_abbreviate: case CMD_noreabbrev:
4222 case CMD_cabbrev: case CMD_cnoreabbrev:
4223 case CMD_iabbrev: case CMD_inoreabbrev:
4224 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004225 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 case CMD_unabbreviate:
4227 case CMD_cunabbrev:
4228 case CMD_iunabbrev:
4229 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004230 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231#ifdef FEAT_MENU
4232 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4233 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4234 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4235 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4236 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4237 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4238 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4239 case CMD_tmenu: case CMD_tunmenu:
4240 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4241 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4242#endif
4243
4244 case CMD_colorscheme:
4245 xp->xp_context = EXPAND_COLORS;
4246 xp->xp_pattern = arg;
4247 break;
4248
4249 case CMD_compiler:
4250 xp->xp_context = EXPAND_COMPILER;
4251 xp->xp_pattern = arg;
4252 break;
4253
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004254 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004255 xp->xp_context = EXPAND_OWNSYNTAX;
4256 xp->xp_pattern = arg;
4257 break;
4258
4259 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004260 xp->xp_context = EXPAND_FILETYPE;
4261 xp->xp_pattern = arg;
4262 break;
4263
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004264 case CMD_packadd:
4265 xp->xp_context = EXPAND_PACKADD;
4266 xp->xp_pattern = arg;
4267 break;
4268
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4270 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4271 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004272 p = skiptowhite(arg);
4273 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 {
4275 xp->xp_context = EXPAND_LANGUAGE;
4276 xp->xp_pattern = arg;
4277 }
4278 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004279 {
4280 if ( STRNCMP(arg, "messages", p - arg) == 0
4281 || STRNCMP(arg, "ctype", p - arg) == 0
4282 || STRNCMP(arg, "time", p - arg) == 0)
4283 {
4284 xp->xp_context = EXPAND_LOCALES;
4285 xp->xp_pattern = skipwhite(p);
4286 }
4287 else
4288 xp->xp_context = EXPAND_NOTHING;
4289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 break;
4291#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004292#if defined(FEAT_PROFILE)
4293 case CMD_profile:
4294 set_context_in_profile_cmd(xp, arg);
4295 break;
4296#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004297 case CMD_behave:
4298 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004299 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004300 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004302 case CMD_messages:
4303 xp->xp_context = EXPAND_MESSAGES;
4304 xp->xp_pattern = arg;
4305 break;
4306
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004307#if defined(FEAT_CMDHIST)
4308 case CMD_history:
4309 xp->xp_context = EXPAND_HISTORY;
4310 xp->xp_pattern = arg;
4311 break;
4312#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004313#if defined(FEAT_PROFILE)
4314 case CMD_syntime:
4315 xp->xp_context = EXPAND_SYNTIME;
4316 xp->xp_pattern = arg;
4317 break;
4318#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004319
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320#endif /* FEAT_CMDL_COMPL */
4321
4322 default:
4323 break;
4324 }
4325 return NULL;
4326}
4327
4328/*
4329 * skip a range specifier of the form: addr [,addr] [;addr] ..
4330 *
4331 * Backslashed delimiters after / or ? will be skipped, and commands will
4332 * not be expanded between /'s and ?'s or after "'".
4333 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004334 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 * Returns the "cmd" pointer advanced to beyond the range.
4336 */
4337 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004338skip_range(
4339 char_u *cmd,
4340 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004342 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004344 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004346 if (*cmd == '\\')
4347 {
4348 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4349 ++cmd;
4350 else
4351 break;
4352 }
4353 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 {
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 Moolenaar2f72c702017-01-29 14:48:10 +01004430 case ADDR_TABS_RELATIVE:
4431 EMSG(_(e_invrange));
4432 cmd = NULL;
4433 goto error;
4434 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004435#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004436 case ADDR_QUICKFIX:
4437 lnum = qf_get_cur_valid_idx(eap);
4438 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004439#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004440 }
4441 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442
4443 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004444 ++cmd;
4445 switch (addr_type)
4446 {
4447 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 lnum = curbuf->b_ml.ml_line_count;
4449 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004450 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004451 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004452 break;
4453 case ADDR_ARGUMENTS:
4454 lnum = ARGCOUNT;
4455 break;
4456 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004457 buf = lastbuf;
4458 while (buf->b_ml.ml_mfp == NULL)
4459 {
4460 if (buf->b_prev == NULL)
4461 break;
4462 buf = buf->b_prev;
4463 }
4464 lnum = buf->b_fnum;
4465 break;
4466 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004467 lnum = lastbuf->b_fnum;
4468 break;
4469 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004470 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004471 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004472 case ADDR_TABS_RELATIVE:
4473 EMSG(_(e_invrange));
4474 cmd = NULL;
4475 goto error;
4476 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004477#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004478 case ADDR_QUICKFIX:
4479 lnum = qf_get_size(eap);
4480 if (lnum == 0)
4481 lnum = 1;
4482 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004483#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004484 }
4485 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486
4487 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004488 if (*++cmd == NUL)
4489 {
4490 cmd = NULL;
4491 goto error;
4492 }
4493 if (addr_type != ADDR_LINES)
4494 {
4495 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004496 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004497 goto error;
4498 }
4499 if (skip)
4500 ++cmd;
4501 else
4502 {
4503 /* Only accept a mark in another file when it is
4504 * used by itself: ":'M". */
4505 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4506 ++cmd;
4507 if (fp == (pos_T *)-1)
4508 /* Jumped to another file. */
4509 lnum = curwin->w_cursor.lnum;
4510 else
4511 {
4512 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 {
4514 cmd = NULL;
4515 goto error;
4516 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004517 lnum = fp->lnum;
4518 }
4519 }
4520 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521
4522 case '/':
4523 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004524 c = *cmd++;
4525 if (addr_type != ADDR_LINES)
4526 {
4527 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004528 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004529 goto error;
4530 }
4531 if (skip) /* skip "/pat/" */
4532 {
4533 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4534 if (*cmd == c)
4535 ++cmd;
4536 }
4537 else
4538 {
4539 pos = curwin->w_cursor; /* save curwin->w_cursor */
4540 /*
4541 * When '/' or '?' follows another address, start
4542 * from there.
4543 */
4544 if (lnum != MAXLNUM)
4545 curwin->w_cursor.lnum = lnum;
4546 /*
4547 * Start a forward search at the end of the line.
4548 * Start a backward search at the start of the line.
4549 * This makes sure we never match in the current
4550 * line, and can match anywhere in the
4551 * next/previous line.
4552 */
4553 if (c == '/')
4554 curwin->w_cursor.col = MAXCOL;
4555 else
4556 curwin->w_cursor.col = 0;
4557 searchcmdlen = 0;
4558 if (!do_search(NULL, c, cmd, 1L,
4559 SEARCH_HIS | SEARCH_MSG, NULL))
4560 {
4561 curwin->w_cursor = pos;
4562 cmd = NULL;
4563 goto error;
4564 }
4565 lnum = curwin->w_cursor.lnum;
4566 curwin->w_cursor = pos;
4567 /* adjust command string pointer */
4568 cmd += searchcmdlen;
4569 }
4570 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571
4572 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004573 ++cmd;
4574 if (addr_type != ADDR_LINES)
4575 {
4576 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004577 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004578 goto error;
4579 }
4580 if (*cmd == '&')
4581 i = RE_SUBST;
4582 else if (*cmd == '?' || *cmd == '/')
4583 i = RE_SEARCH;
4584 else
4585 {
4586 EMSG(_(e_backslash));
4587 cmd = NULL;
4588 goto error;
4589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004591 if (!skip)
4592 {
4593 /*
4594 * When search follows another address, start from
4595 * there.
4596 */
4597 if (lnum != MAXLNUM)
4598 pos.lnum = lnum;
4599 else
4600 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004602 /*
4603 * Start the search just like for the above
4604 * do_search().
4605 */
4606 if (*cmd != '?')
4607 pos.col = MAXCOL;
4608 else
4609 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004610#ifdef FEAT_VIRTUALEDIT
4611 pos.coladd = 0;
4612#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004613 if (searchit(curwin, curbuf, &pos,
4614 *cmd == '?' ? BACKWARD : FORWARD,
4615 (char_u *)"", 1L, SEARCH_MSG,
4616 i, (linenr_T)0, NULL) != FAIL)
4617 lnum = pos.lnum;
4618 else
4619 {
4620 cmd = NULL;
4621 goto error;
4622 }
4623 }
4624 ++cmd;
4625 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626
4627 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004628 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4629 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 }
4631
4632 for (;;)
4633 {
4634 cmd = skipwhite(cmd);
4635 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4636 break;
4637
4638 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004639 {
4640 switch (addr_type)
4641 {
4642 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004643 /* "+1" is same as ".+1" */
4644 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004645 break;
4646 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004647 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004648 break;
4649 case ADDR_ARGUMENTS:
4650 lnum = curwin->w_arg_idx + 1;
4651 break;
4652 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004653 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004654 lnum = curbuf->b_fnum;
4655 break;
4656 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004657 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004658 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004659 case ADDR_TABS_RELATIVE:
4660 lnum = 1;
4661 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004662#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004663 case ADDR_QUICKFIX:
4664 lnum = qf_get_cur_valid_idx(eap);
4665 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004666#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004667 }
4668 }
4669
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 if (VIM_ISDIGIT(*cmd))
4671 i = '+'; /* "number" is same as "+number" */
4672 else
4673 i = *cmd++;
4674 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4675 n = 1;
4676 else
4677 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004678
4679 if (addr_type == ADDR_TABS_RELATIVE)
4680 {
4681 EMSG(_(e_invrange));
4682 cmd = NULL;
4683 goto error;
4684 }
4685 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004686 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004687 lnum = compute_buffer_local_count(
4688 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004690 {
4691#ifdef FEAT_FOLDING
4692 /* Relative line addressing, need to adjust for folded lines
4693 * now, but only do it after the first address. */
4694 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4695 && address_count >= 2)
4696 (void)hasFolding(lnum, NULL, &lnum);
4697#endif
4698 if (i == '-')
4699 lnum -= n;
4700 else
4701 lnum += n;
4702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
4704 } while (*cmd == '/' || *cmd == '?');
4705
4706error:
4707 *ptr = cmd;
4708 return lnum;
4709}
4710
4711/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004712 * Get flags from an Ex command argument.
4713 */
4714 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004715get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004716{
4717 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4718 {
4719 if (*eap->arg == 'l')
4720 eap->flags |= EXFLAG_LIST;
4721 else if (*eap->arg == 'p')
4722 eap->flags |= EXFLAG_PRINT;
4723 else
4724 eap->flags |= EXFLAG_NR;
4725 eap->arg = skipwhite(eap->arg + 1);
4726 }
4727}
4728
4729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 * Function called for command which is Not Implemented. NI!
4731 */
4732 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004733ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734{
4735 if (!eap->skip)
4736 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4737}
4738
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004739#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740/*
4741 * Function called for script command which is Not Implemented. NI!
4742 * Skips over ":perl <<EOF" constructs.
4743 */
4744 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004745ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746{
4747 if (!eap->skip)
4748 ex_ni(eap);
4749 else
4750 vim_free(script_get(eap, eap->arg));
4751}
4752#endif
4753
4754/*
4755 * Check range in Ex command for validity.
4756 * Return NULL when valid, error message when invalid.
4757 */
4758 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004759invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004761 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 if ( eap->line1 < 0
4763 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004764 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004766
4767 if (eap->argt & RANGE)
4768 {
4769 switch(eap->addr_type)
4770 {
4771 case ADDR_LINES:
4772 if (!(eap->argt & NOTADR)
4773 && eap->line2 > curbuf->b_ml.ml_line_count
4774#ifdef FEAT_DIFF
4775 + (eap->cmdidx == CMD_diffget)
4776#endif
4777 )
4778 return (char_u *)_(e_invrange);
4779 break;
4780 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004781 /* add 1 if ARGCOUNT is 0 */
4782 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004783 return (char_u *)_(e_invrange);
4784 break;
4785 case ADDR_BUFFERS:
4786 if (eap->line1 < firstbuf->b_fnum
4787 || eap->line2 > lastbuf->b_fnum)
4788 return (char_u *)_(e_invrange);
4789 break;
4790 case ADDR_LOADED_BUFFERS:
4791 buf = firstbuf;
4792 while (buf->b_ml.ml_mfp == NULL)
4793 {
4794 if (buf->b_next == NULL)
4795 return (char_u *)_(e_invrange);
4796 buf = buf->b_next;
4797 }
4798 if (eap->line1 < buf->b_fnum)
4799 return (char_u *)_(e_invrange);
4800 buf = lastbuf;
4801 while (buf->b_ml.ml_mfp == NULL)
4802 {
4803 if (buf->b_prev == NULL)
4804 return (char_u *)_(e_invrange);
4805 buf = buf->b_prev;
4806 }
4807 if (eap->line2 > buf->b_fnum)
4808 return (char_u *)_(e_invrange);
4809 break;
4810 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004811 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004812 return (char_u *)_(e_invrange);
4813 break;
4814 case ADDR_TABS:
4815 if (eap->line2 > LAST_TAB_NR)
4816 return (char_u *)_(e_invrange);
4817 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004818 case ADDR_TABS_RELATIVE:
4819 /* Do nothing */
4820 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004821#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004822 case ADDR_QUICKFIX:
4823 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4824 return (char_u *)_(e_invrange);
4825 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004826#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004827 }
4828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 return NULL;
4830}
4831
4832/*
4833 * Correct the range for zero line number, if required.
4834 */
4835 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004836correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837{
4838 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4839 {
4840 if (eap->line1 == 0)
4841 eap->line1 = 1;
4842 if (eap->line2 == 0)
4843 eap->line2 = 1;
4844 }
4845}
4846
Bram Moolenaar748bf032005-02-02 23:04:36 +00004847#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004848static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004849
4850/*
4851 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4852 * pattern. Otherwise return eap->arg.
4853 */
4854 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004855skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004856{
4857 char_u *p = eap->arg;
4858
Bram Moolenaara37420f2006-02-04 22:37:47 +00004859 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4860 || eap->cmdidx == CMD_vimgrepadd
4861 || eap->cmdidx == CMD_lvimgrepadd
4862 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004863 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004864 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004865 if (p == NULL)
4866 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004867 }
4868 return p;
4869}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004870
4871/*
4872 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4873 * in the command line, so that things like % get expanded.
4874 */
4875 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004876replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004877{
4878 char_u *new_cmdline;
4879 char_u *program;
4880 char_u *pos;
4881 char_u *ptr;
4882 int len;
4883 int i;
4884
4885 /*
4886 * Don't do it when ":vimgrep" is used for ":grep".
4887 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004888 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4889 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4890 || eap->cmdidx == CMD_grepadd
4891 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004892 && !grep_internal(eap->cmdidx))
4893 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004894 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4895 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004896 {
4897 if (*curbuf->b_p_gp == NUL)
4898 program = p_gp;
4899 else
4900 program = curbuf->b_p_gp;
4901 }
4902 else
4903 {
4904 if (*curbuf->b_p_mp == NUL)
4905 program = p_mp;
4906 else
4907 program = curbuf->b_p_mp;
4908 }
4909
4910 p = skipwhite(p);
4911
4912 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4913 {
4914 /* replace $* by given arguments */
4915 i = 1;
4916 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4917 ++i;
4918 len = (int)STRLEN(p);
4919 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4920 if (new_cmdline == NULL)
4921 return NULL; /* out of memory */
4922 ptr = new_cmdline;
4923 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4924 {
4925 i = (int)(pos - program);
4926 STRNCPY(ptr, program, i);
4927 STRCPY(ptr += i, p);
4928 ptr += len;
4929 program = pos + 2;
4930 }
4931 STRCPY(ptr, program);
4932 }
4933 else
4934 {
4935 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4936 if (new_cmdline == NULL)
4937 return NULL; /* out of memory */
4938 STRCPY(new_cmdline, program);
4939 STRCAT(new_cmdline, " ");
4940 STRCAT(new_cmdline, p);
4941 }
4942 msg_make(p);
4943
4944 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4945 vim_free(*cmdlinep);
4946 *cmdlinep = new_cmdline;
4947 p = new_cmdline;
4948 }
4949 return p;
4950}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004951#endif
4952
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953/*
4954 * Expand file name in Ex command argument.
4955 * Return FAIL for failure, OK otherwise.
4956 */
4957 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004958expand_filename(
4959 exarg_T *eap,
4960 char_u **cmdlinep,
4961 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962{
4963 int has_wildcards; /* need to expand wildcards */
4964 char_u *repl;
4965 int srclen;
4966 char_u *p;
4967 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004968 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969
Bram Moolenaar748bf032005-02-02 23:04:36 +00004970#ifdef FEAT_QUICKFIX
4971 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4972 p = skip_grep_pat(eap);
4973#else
4974 p = eap->arg;
4975#endif
4976
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 /*
4978 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4979 * the file name contains a wildcard it should not cause expanding.
4980 * (it will be expanded anyway if there is a wildcard before replacing).
4981 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004982 has_wildcards = mch_has_wildcard(p);
4983 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004985#ifdef FEAT_EVAL
4986 /* Skip over `=expr`, wildcards in it are not expanded. */
4987 if (p[0] == '`' && p[1] == '=')
4988 {
4989 p += 2;
4990 (void)skip_expr(&p);
4991 if (*p == '`')
4992 ++p;
4993 continue;
4994 }
4995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 /*
4997 * Quick check if this cannot be the start of a special string.
4998 * Also removes backslash before '%', '#' and '<'.
4999 */
5000 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5001 {
5002 ++p;
5003 continue;
5004 }
5005
5006 /*
5007 * Try to find a match at this position.
5008 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005009 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
5010 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 if (*errormsgp != NULL) /* error detected */
5012 return FAIL;
5013 if (repl == NULL) /* no match found */
5014 {
5015 p += srclen;
5016 continue;
5017 }
5018
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005019 /* Wildcards won't be expanded below, the replacement is taken
5020 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
5021 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5022 {
5023 char_u *l = repl;
5024
5025 repl = expand_env_save(repl);
5026 vim_free(l);
5027 }
5028
Bram Moolenaar63b92542007-03-27 14:57:09 +00005029 /* Need to escape white space et al. with a backslash.
5030 * Don't do this for:
5031 * - replacement that already has been escaped: "##"
5032 * - shell commands (may have to use quotes instead).
5033 * - non-unix systems when there is a single argument (spaces don't
5034 * separate arguments then).
5035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005037 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 && eap->cmdidx != CMD_bang
5039 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00005040 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00005042 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00005044 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045#ifndef UNIX
5046 && !(eap->argt & NOSPC)
5047#endif
5048 )
5049 {
5050 char_u *l;
5051#ifdef BACKSLASH_IN_FILENAME
5052 /* Don't escape a backslash here, because rem_backslash() doesn't
5053 * remove it later. */
5054 static char_u *nobslash = (char_u *)" \t\"|";
5055# define ESCAPE_CHARS nobslash
5056#else
5057# define ESCAPE_CHARS escape_chars
5058#endif
5059
5060 for (l = repl; *l; ++l)
5061 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5062 {
5063 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5064 if (l != NULL)
5065 {
5066 vim_free(repl);
5067 repl = l;
5068 }
5069 break;
5070 }
5071 }
5072
5073 /* For a shell command a '!' must be escaped. */
5074 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005075 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 {
5077 char_u *l;
5078
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005079 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 if (l != NULL)
5081 {
5082 vim_free(repl);
5083 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 }
5085 }
5086
5087 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5088 vim_free(repl);
5089 if (p == NULL)
5090 return FAIL;
5091 }
5092
5093 /*
5094 * One file argument: Expand wildcards.
5095 * Don't do this with ":r !command" or ":w !command".
5096 */
5097 if ((eap->argt & NOSPC) && !eap->usefilter)
5098 {
5099 /*
5100 * May do this twice:
5101 * 1. Replace environment variables.
5102 * 2. Replace any other wildcards, remove backslashes.
5103 */
5104 for (n = 1; n <= 2; ++n)
5105 {
5106 if (n == 2)
5107 {
5108#ifdef UNIX
5109 /*
5110 * Only for Unix we check for more than one file name.
5111 * For other systems spaces are considered to be part
5112 * of the file name.
5113 * Only check here if there is no wildcard, otherwise
5114 * ExpandOne() will check for errors. This allows
5115 * ":e `ls ve*.c`" on Unix.
5116 */
5117 if (!has_wildcards)
5118 for (p = eap->arg; *p; ++p)
5119 {
5120 /* skip escaped characters */
5121 if (p[1] && (*p == '\\' || *p == Ctrl_V))
5122 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005123 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 {
5125 *errormsgp = (char_u *)_("E172: Only one file name allowed");
5126 return FAIL;
5127 }
5128 }
5129#endif
5130
5131 /*
5132 * Halve the number of backslashes (this is Vi compatible).
5133 * For Unix and OS/2, when wildcards are expanded, this is
5134 * done by ExpandOne() below.
5135 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005136#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 if (!has_wildcards)
5138#endif
5139 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 }
5141
5142 if (has_wildcards)
5143 {
5144 if (n == 1)
5145 {
5146 /*
5147 * First loop: May expand environment variables. This
5148 * can be done much faster with expand_env() than with
5149 * something else (e.g., calling a shell).
5150 * After expanding environment variables, check again
5151 * if there are still wildcards present.
5152 */
5153 if (vim_strchr(eap->arg, '$') != NULL
5154 || vim_strchr(eap->arg, '~') != NULL)
5155 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005156 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005157 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 has_wildcards = mch_has_wildcard(NameBuff);
5159 p = NameBuff;
5160 }
5161 else
5162 p = NULL;
5163 }
5164 else /* n == 2 */
5165 {
5166 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005167 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168
5169 ExpandInit(&xpc);
5170 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005171 if (p_wic)
5172 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005174 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 if (p == NULL)
5176 return FAIL;
5177 }
5178 if (p != NULL)
5179 {
5180 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5181 p, cmdlinep);
5182 if (n == 2) /* p came from ExpandOne() */
5183 vim_free(p);
5184 }
5185 }
5186 }
5187 }
5188 return OK;
5189}
5190
5191/*
5192 * Replace part of the command line, keeping eap->cmd, eap->arg and
5193 * eap->nextcmd correct.
5194 * "src" points to the part that is to be replaced, of length "srclen".
5195 * "repl" is the replacement string.
5196 * Returns a pointer to the character after the replaced string.
5197 * Returns NULL for failure.
5198 */
5199 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005200repl_cmdline(
5201 exarg_T *eap,
5202 char_u *src,
5203 int srclen,
5204 char_u *repl,
5205 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206{
5207 int len;
5208 int i;
5209 char_u *new_cmdline;
5210
5211 /*
5212 * The new command line is build in new_cmdline[].
5213 * First allocate it.
5214 * Careful: a "+cmd" argument may have been NUL terminated.
5215 */
5216 len = (int)STRLEN(repl);
5217 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005218 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5220 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5221 return NULL; /* out of memory! */
5222
5223 /*
5224 * Copy the stuff before the expanded part.
5225 * Copy the expanded stuff.
5226 * Copy what came after the expanded part.
5227 * Copy the next commands, if there are any.
5228 */
5229 i = (int)(src - *cmdlinep); /* length of part before match */
5230 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005231
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 mch_memmove(new_cmdline + i, repl, (size_t)len);
5233 i += len; /* remember the end of the string */
5234 STRCPY(new_cmdline + i, src + srclen);
5235 src = new_cmdline + i; /* remember where to continue */
5236
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005237 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 {
5239 i = (int)STRLEN(new_cmdline) + 1;
5240 STRCPY(new_cmdline + i, eap->nextcmd);
5241 eap->nextcmd = new_cmdline + i;
5242 }
5243 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5244 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5245 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5246 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5247 vim_free(*cmdlinep);
5248 *cmdlinep = new_cmdline;
5249
5250 return src;
5251}
5252
5253/*
5254 * Check for '|' to separate commands and '"' to start comments.
5255 */
5256 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005257separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258{
5259 char_u *p;
5260
Bram Moolenaar86b68352004-12-27 21:59:20 +00005261#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005262 p = skip_grep_pat(eap);
5263#else
5264 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005265#endif
5266
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005267 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 {
5269 if (*p == Ctrl_V)
5270 {
5271 if (eap->argt & (USECTRLV | XFILE))
5272 ++p; /* skip CTRL-V and next char */
5273 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005274 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005275 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 if (*p == NUL) /* stop at NUL after CTRL-V */
5277 break;
5278 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005279
5280#ifdef FEAT_EVAL
5281 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005282 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005283 {
5284 p += 2;
5285 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005286 }
5287#endif
5288
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 /* Check for '"': start of comment or '|': next command */
5290 /* :@" and :*" do not start a comment!
5291 * :redir @" doesn't either. */
5292 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5293 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5294 || p != eap->arg)
5295 && (eap->cmdidx != CMD_redir
5296 || p != eap->arg + 1 || p[-1] != '@'))
5297 || *p == '|' || *p == '\n')
5298 {
5299 /*
5300 * We remove the '\' before the '|', unless USECTRLV is used
5301 * AND 'b' is present in 'cpoptions'.
5302 */
5303 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5304 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5305 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005306 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 --p;
5308 }
5309 else
5310 {
5311 eap->nextcmd = check_nextcmd(p);
5312 *p = NUL;
5313 break;
5314 }
5315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005317
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5319 del_trailing_spaces(eap->arg);
5320}
5321
5322/*
5323 * get + command from ex argument
5324 */
5325 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005326getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327{
5328 char_u *arg = *argp;
5329 char_u *command = NULL;
5330
5331 if (*arg == '+') /* +[command] */
5332 {
5333 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005334 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 command = dollar_command;
5336 else
5337 {
5338 command = arg;
5339 arg = skip_cmd_arg(command, TRUE);
5340 if (*arg != NUL)
5341 *arg++ = NUL; /* terminate command with NUL */
5342 }
5343
5344 arg = skipwhite(arg); /* skip over spaces */
5345 *argp = arg;
5346 }
5347 return command;
5348}
5349
5350/*
5351 * Find end of "+command" argument. Skip over "\ " and "\\".
5352 */
5353 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005354skip_cmd_arg(
5355 char_u *p,
5356 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357{
5358 while (*p && !vim_isspace(*p))
5359 {
5360 if (*p == '\\' && p[1] != NUL)
5361 {
5362 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005363 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 else
5365 ++p;
5366 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005367 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 }
5369 return p;
5370}
5371
5372/*
5373 * Get "++opt=arg" argument.
5374 * Return FAIL or OK.
5375 */
5376 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005377getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378{
5379 char_u *arg = eap->arg + 2;
5380 int *pp = NULL;
5381#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005382 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 char_u *p;
5384#endif
5385
5386 /* ":edit ++[no]bin[ary] file" */
5387 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5388 {
5389 if (*arg == 'n')
5390 {
5391 arg += 2;
5392 eap->force_bin = FORCE_NOBIN;
5393 }
5394 else
5395 eap->force_bin = FORCE_BIN;
5396 if (!checkforcmd(&arg, "binary", 3))
5397 return FAIL;
5398 eap->arg = skipwhite(arg);
5399 return OK;
5400 }
5401
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005402 /* ":read ++edit file" */
5403 if (STRNCMP(arg, "edit", 4) == 0)
5404 {
5405 eap->read_edit = TRUE;
5406 eap->arg = skipwhite(arg + 4);
5407 return OK;
5408 }
5409
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 if (STRNCMP(arg, "ff", 2) == 0)
5411 {
5412 arg += 2;
5413 pp = &eap->force_ff;
5414 }
5415 else if (STRNCMP(arg, "fileformat", 10) == 0)
5416 {
5417 arg += 10;
5418 pp = &eap->force_ff;
5419 }
5420#ifdef FEAT_MBYTE
5421 else if (STRNCMP(arg, "enc", 3) == 0)
5422 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005423 if (STRNCMP(arg, "encoding", 8) == 0)
5424 arg += 8;
5425 else
5426 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 pp = &eap->force_enc;
5428 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005429 else if (STRNCMP(arg, "bad", 3) == 0)
5430 {
5431 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005432 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434#endif
5435
5436 if (pp == NULL || *arg != '=')
5437 return FAIL;
5438
5439 ++arg;
5440 *pp = (int)(arg - eap->cmd);
5441 arg = skip_cmd_arg(arg, FALSE);
5442 eap->arg = skipwhite(arg);
5443 *arg = NUL;
5444
5445#ifdef FEAT_MBYTE
5446 if (pp == &eap->force_ff)
5447 {
5448#endif
5449 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5450 return FAIL;
5451#ifdef FEAT_MBYTE
5452 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005453 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 {
5455 /* Make 'fileencoding' lower case. */
5456 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5457 *p = TOLOWER_ASC(*p);
5458 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005459 else
5460 {
5461 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5462 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005463 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005464 if (STRICMP(p, "keep") == 0)
5465 eap->bad_char = BAD_KEEP;
5466 else if (STRICMP(p, "drop") == 0)
5467 eap->bad_char = BAD_DROP;
5468 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5469 eap->bad_char = *p;
5470 else
5471 return FAIL;
5472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473#endif
5474
5475 return OK;
5476}
5477
5478/*
5479 * ":abbreviate" and friends.
5480 */
5481 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005482ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483{
5484 do_exmap(eap, TRUE); /* almost the same as mapping */
5485}
5486
5487/*
5488 * ":map" and friends.
5489 */
5490 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005491ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492{
5493 /*
5494 * If we are sourcing .exrc or .vimrc in current directory we
5495 * print the mappings for security reasons.
5496 */
5497 if (secure)
5498 {
5499 secure = 2;
5500 msg_outtrans(eap->cmd);
5501 msg_putchar('\n');
5502 }
5503 do_exmap(eap, FALSE);
5504}
5505
5506/*
5507 * ":unmap" and friends.
5508 */
5509 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005510ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511{
5512 do_exmap(eap, FALSE);
5513}
5514
5515/*
5516 * ":mapclear" and friends.
5517 */
5518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005519ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520{
5521 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5522}
5523
5524/*
5525 * ":abclear" and friends.
5526 */
5527 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005528ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529{
5530 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5531}
5532
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005533#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005535ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536{
5537 /*
5538 * Disallow auto commands from .exrc and .vimrc in current
5539 * directory for security reasons.
5540 */
5541 if (secure)
5542 {
5543 secure = 2;
5544 eap->errmsg = e_curdir;
5545 }
5546 else if (eap->cmdidx == CMD_autocmd)
5547 do_autocmd(eap->arg, eap->forceit);
5548 else
5549 do_augroup(eap->arg, eap->forceit);
5550}
5551
5552/*
5553 * ":doautocmd": Apply the automatic commands to the current buffer.
5554 */
5555 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005556ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005558 char_u *arg = eap->arg;
5559 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005560 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005561
Bram Moolenaar1610d052016-06-09 22:53:01 +02005562 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5563 /* Only when there is no <nomodeline>. */
5564 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005565 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566}
5567#endif
5568
5569#ifdef FEAT_LISTCMDS
5570/*
5571 * :[N]bunload[!] [N] [bufname] unload buffer
5572 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5573 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5574 */
5575 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005576ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577{
5578 eap->errmsg = do_bufdel(
5579 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5580 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5581 : DOBUF_UNLOAD, eap->arg,
5582 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5583}
5584
5585/*
5586 * :[N]buffer [N] to buffer N
5587 * :[N]sbuffer [N] to buffer N
5588 */
5589 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005590ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591{
5592 if (*eap->arg)
5593 eap->errmsg = e_trailing;
5594 else
5595 {
5596 if (eap->addr_count == 0) /* default is current buffer */
5597 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5598 else
5599 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005600 if (eap->do_ecmd_cmd != NULL)
5601 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 }
5603}
5604
5605/*
5606 * :[N]bmodified [N] to next mod. buffer
5607 * :[N]sbmodified [N] to next mod. buffer
5608 */
5609 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005610ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611{
5612 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005613 if (eap->do_ecmd_cmd != NULL)
5614 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615}
5616
5617/*
5618 * :[N]bnext [N] to next buffer
5619 * :[N]sbnext [N] split and to next buffer
5620 */
5621 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005622ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623{
5624 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005625 if (eap->do_ecmd_cmd != NULL)
5626 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627}
5628
5629/*
5630 * :[N]bNext [N] to previous buffer
5631 * :[N]bprevious [N] to previous buffer
5632 * :[N]sbNext [N] split and to previous buffer
5633 * :[N]sbprevious [N] split and to previous buffer
5634 */
5635 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005636ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637{
5638 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005639 if (eap->do_ecmd_cmd != NULL)
5640 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641}
5642
5643/*
5644 * :brewind to first buffer
5645 * :bfirst to first buffer
5646 * :sbrewind split and to first buffer
5647 * :sbfirst split and to first buffer
5648 */
5649 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005650ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651{
5652 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005653 if (eap->do_ecmd_cmd != NULL)
5654 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655}
5656
5657/*
5658 * :blast to last buffer
5659 * :sblast split and to last buffer
5660 */
5661 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005662ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663{
5664 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005665 if (eap->do_ecmd_cmd != NULL)
5666 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667}
5668#endif
5669
5670 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005671ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672{
5673 return (c == NUL || c == '|' || c == '"' || c == '\n');
5674}
5675
5676#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5677 || defined(PROTO)
5678/*
5679 * Return the next command, after the first '|' or '\n'.
5680 * Return NULL if not found.
5681 */
5682 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005683find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684{
5685 while (*p != '|' && *p != '\n')
5686 {
5687 if (*p == NUL)
5688 return NULL;
5689 ++p;
5690 }
5691 return (p + 1);
5692}
5693#endif
5694
5695/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005696 * Check if *p is a separator between Ex commands, skipping over white space.
5697 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 */
5699 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005700check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005702 char_u *s = skipwhite(p);
5703
5704 if (*s == '|' || *s == '\n')
5705 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 else
5707 return NULL;
5708}
5709
5710/*
5711 * - if there are more files to edit
5712 * - and this is the last window
5713 * - and forceit not used
5714 * - and not repeated twice on a row
5715 * return FAIL and give error message if 'message' TRUE
5716 * return OK otherwise
5717 */
5718 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005719check_more(
5720 int message, /* when FALSE check only, no messages */
5721 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722{
5723 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5724
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005725 if (!forceit && only_one_window()
5726 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 {
5728 if (message)
5729 {
5730#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5731 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5732 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005733 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734
5735 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005736 vim_strncpy(buff,
5737 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005738 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005740 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 _("%d more files to edit. Quit anyway?"), n);
5742 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5743 return OK;
5744 return FAIL;
5745 }
5746#endif
5747 if (n == 1)
5748 EMSG(_("E173: 1 more file to edit"));
5749 else
5750 EMSGN(_("E173: %ld more files to edit"), n);
5751 quitmore = 2; /* next try to quit is allowed */
5752 }
5753 return FAIL;
5754 }
5755 return OK;
5756}
5757
5758#ifdef FEAT_CMDL_COMPL
5759/*
5760 * Function given to ExpandGeneric() to obtain the list of command names.
5761 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005763get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764{
5765 if (idx >= (int)CMD_SIZE)
5766# ifdef FEAT_USR_CMDS
5767 return get_user_command_name(idx);
5768# else
5769 return NULL;
5770# endif
5771 return cmdnames[idx].cmd_name;
5772}
5773#endif
5774
5775#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005776static 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);
5777static void uc_list(char_u *name, size_t name_len);
5778static 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);
5779static char_u *uc_split_args(char_u *arg, size_t *lenp);
5780static 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 +00005781
5782 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005783uc_add_command(
5784 char_u *name,
5785 size_t name_len,
5786 char_u *rep,
5787 long argt,
5788 long def,
5789 int flags,
5790 int compl,
5791 char_u *compl_arg,
5792 int addr_type,
5793 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794{
5795 ucmd_T *cmd = NULL;
5796 char_u *p;
5797 int i;
5798 int cmp = 1;
5799 char_u *rep_buf = NULL;
5800 garray_T *gap;
5801
Bram Moolenaar9c102382006-05-03 21:26:49 +00005802 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 if (rep_buf == NULL)
5804 {
5805 /* Can't replace termcodes - try using the string as is */
5806 rep_buf = vim_strsave(rep);
5807
5808 /* Give up if out of memory */
5809 if (rep_buf == NULL)
5810 return FAIL;
5811 }
5812
5813 /* get address of growarray: global or in curbuf */
5814 if (flags & UC_BUFFER)
5815 {
5816 gap = &curbuf->b_ucmds;
5817 if (gap->ga_itemsize == 0)
5818 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5819 }
5820 else
5821 gap = &ucmds;
5822
5823 /* Search for the command in the already defined commands. */
5824 for (i = 0; i < gap->ga_len; ++i)
5825 {
5826 size_t len;
5827
5828 cmd = USER_CMD_GA(gap, i);
5829 len = STRLEN(cmd->uc_name);
5830 cmp = STRNCMP(name, cmd->uc_name, name_len);
5831 if (cmp == 0)
5832 {
5833 if (name_len < len)
5834 cmp = -1;
5835 else if (name_len > len)
5836 cmp = 1;
5837 }
5838
5839 if (cmp == 0)
5840 {
5841 if (!force)
5842 {
5843 EMSG(_("E174: Command already exists: add ! to replace it"));
5844 goto fail;
5845 }
5846
5847 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005848 cmd->uc_rep = NULL;
5849#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5850 vim_free(cmd->uc_compl_arg);
5851 cmd->uc_compl_arg = NULL;
5852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 break;
5854 }
5855
5856 /* Stop as soon as we pass the name to add */
5857 if (cmp < 0)
5858 break;
5859 }
5860
5861 /* Extend the array unless we're replacing an existing command */
5862 if (cmp != 0)
5863 {
5864 if (ga_grow(gap, 1) != OK)
5865 goto fail;
5866 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5867 goto fail;
5868
5869 cmd = USER_CMD_GA(gap, i);
5870 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5871
5872 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873
5874 cmd->uc_name = p;
5875 }
5876
5877 cmd->uc_rep = rep_buf;
5878 cmd->uc_argt = argt;
5879 cmd->uc_def = def;
5880 cmd->uc_compl = compl;
5881#ifdef FEAT_EVAL
5882 cmd->uc_scriptID = current_SID;
5883# ifdef FEAT_CMDL_COMPL
5884 cmd->uc_compl_arg = compl_arg;
5885# endif
5886#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005887 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888
5889 return OK;
5890
5891fail:
5892 vim_free(rep_buf);
5893#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5894 vim_free(compl_arg);
5895#endif
5896 return FAIL;
5897}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005900#if defined(FEAT_USR_CMDS)
5901static struct
5902{
5903 int expand;
5904 char *name;
5905} addr_type_complete[] =
5906{
5907 {ADDR_ARGUMENTS, "arguments"},
5908 {ADDR_LINES, "lines"},
5909 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5910 {ADDR_TABS, "tabs"},
5911 {ADDR_BUFFERS, "buffers"},
5912 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005913 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005914 {-1, NULL}
5915};
5916#endif
5917
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005918#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919/*
5920 * List of names for completion for ":command" with the EXPAND_ flag.
5921 * Must be alphabetical for completion.
5922 */
5923static struct
5924{
5925 int expand;
5926 char *name;
5927} command_complete[] =
5928{
5929 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005930 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005932 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005934 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005935#if defined(FEAT_CSCOPE)
5936 {EXPAND_CSCOPE, "cscope"},
5937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5939 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005940 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941#endif
5942 {EXPAND_DIRECTORIES, "dir"},
5943 {EXPAND_ENV_VARS, "environment"},
5944 {EXPAND_EVENTS, "event"},
5945 {EXPAND_EXPRESSION, "expression"},
5946 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005947 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005948 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 {EXPAND_FUNCTIONS, "function"},
5950 {EXPAND_HELP, "help"},
5951 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005952#if defined(FEAT_CMDHIST)
5953 {EXPAND_HISTORY, "history"},
5954#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005955#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005956 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005957 {EXPAND_LOCALES, "locale"},
5958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 {EXPAND_MAPPINGS, "mapping"},
5960 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005961 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005962 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005963#if defined(FEAT_PROFILE)
5964 {EXPAND_SYNTIME, "syntime"},
5965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005967 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005968 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005969#if defined(FEAT_SIGNS)
5970 {EXPAND_SIGN, "sign"},
5971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 {EXPAND_TAGS, "tag"},
5973 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005974 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 {EXPAND_USER_VARS, "var"},
5976 {0, NULL}
5977};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005980#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005982uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983{
5984 int i, j;
5985 int found = FALSE;
5986 ucmd_T *cmd;
5987 int len;
5988 long a;
5989 garray_T *gap;
5990
5991 gap = &curbuf->b_ucmds;
5992 for (;;)
5993 {
5994 for (i = 0; i < gap->ga_len; ++i)
5995 {
5996 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005997 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998
Bram Moolenaard29459b2016-08-26 22:29:11 +02005999 /* Skip commands which don't match the requested prefix and
6000 * commands filtered out. */
6001 if (STRNCMP(name, cmd->uc_name, name_len) != 0
6002 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 continue;
6004
6005 /* Put out the title first time */
6006 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006007 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 found = TRUE;
6009 msg_putchar('\n');
6010 if (got_int)
6011 break;
6012
6013 /* Special cases */
6014 msg_putchar(a & BANG ? '!' : ' ');
6015 msg_putchar(a & REGSTR ? '"' : ' ');
6016 msg_putchar(gap != &ucmds ? 'b' : ' ');
6017 msg_putchar(' ');
6018
Bram Moolenaar8820b482017-03-16 17:23:31 +01006019 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 len = (int)STRLEN(cmd->uc_name) + 4;
6021
6022 do {
6023 msg_putchar(' ');
6024 ++len;
6025 } while (len < 16);
6026
6027 len = 0;
6028
6029 /* Arguments */
6030 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
6031 {
6032 case 0: IObuff[len++] = '0'; break;
6033 case (EXTRA): IObuff[len++] = '*'; break;
6034 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
6035 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
6036 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
6037 }
6038
6039 do {
6040 IObuff[len++] = ' ';
6041 } while (len < 5);
6042
6043 /* Range */
6044 if (a & (RANGE|COUNT))
6045 {
6046 if (a & COUNT)
6047 {
6048 /* -count=N */
6049 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6050 len += (int)STRLEN(IObuff + len);
6051 }
6052 else if (a & DFLALL)
6053 IObuff[len++] = '%';
6054 else if (cmd->uc_def >= 0)
6055 {
6056 /* -range=N */
6057 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6058 len += (int)STRLEN(IObuff + len);
6059 }
6060 else
6061 IObuff[len++] = '.';
6062 }
6063
6064 do {
6065 IObuff[len++] = ' ';
6066 } while (len < 11);
6067
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006068 /* Address Type */
6069 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6070 if (addr_type_complete[j].expand != ADDR_LINES
6071 && addr_type_complete[j].expand == cmd->uc_addr_type)
6072 {
6073 STRCPY(IObuff + len, addr_type_complete[j].name);
6074 len += (int)STRLEN(IObuff + len);
6075 break;
6076 }
6077
6078 do {
6079 IObuff[len++] = ' ';
6080 } while (len < 21);
6081
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 /* Completion */
6083 for (j = 0; command_complete[j].expand != 0; ++j)
6084 if (command_complete[j].expand == cmd->uc_compl)
6085 {
6086 STRCPY(IObuff + len, command_complete[j].name);
6087 len += (int)STRLEN(IObuff + len);
6088 break;
6089 }
6090
6091 do {
6092 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006093 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094
6095 IObuff[len] = '\0';
6096 msg_outtrans(IObuff);
6097
6098 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006099#ifdef FEAT_EVAL
6100 if (p_verbose > 0)
6101 last_set_msg(cmd->uc_scriptID);
6102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 out_flush();
6104 ui_breakcheck();
6105 if (got_int)
6106 break;
6107 }
6108 if (gap == &ucmds || i < gap->ga_len)
6109 break;
6110 gap = &ucmds;
6111 }
6112
6113 if (!found)
6114 MSG(_("No user-defined commands found"));
6115}
6116
6117 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006118uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119{
6120 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6121 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6122 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6123 0xb9, 0x7f, 0};
6124 int i;
6125
6126 for (i = 0; fcmd[i]; ++i)
6127 IObuff[i] = fcmd[i] - 0x40;
6128 IObuff[i] = 0;
6129 return IObuff;
6130}
6131
6132 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006133uc_scan_attr(
6134 char_u *attr,
6135 size_t len,
6136 long *argt,
6137 long *def,
6138 int *flags,
6139 int *compl,
6140 char_u **compl_arg,
6141 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142{
6143 char_u *p;
6144
6145 if (len == 0)
6146 {
6147 EMSG(_("E175: No attribute specified"));
6148 return FAIL;
6149 }
6150
6151 /* First, try the simple attributes (no arguments) */
6152 if (STRNICMP(attr, "bang", len) == 0)
6153 *argt |= BANG;
6154 else if (STRNICMP(attr, "buffer", len) == 0)
6155 *flags |= UC_BUFFER;
6156 else if (STRNICMP(attr, "register", len) == 0)
6157 *argt |= REGSTR;
6158 else if (STRNICMP(attr, "bar", len) == 0)
6159 *argt |= TRLBAR;
6160 else
6161 {
6162 int i;
6163 char_u *val = NULL;
6164 size_t vallen = 0;
6165 size_t attrlen = len;
6166
6167 /* Look for the attribute name - which is the part before any '=' */
6168 for (i = 0; i < (int)len; ++i)
6169 {
6170 if (attr[i] == '=')
6171 {
6172 val = &attr[i + 1];
6173 vallen = len - i - 1;
6174 attrlen = i;
6175 break;
6176 }
6177 }
6178
6179 if (STRNICMP(attr, "nargs", attrlen) == 0)
6180 {
6181 if (vallen == 1)
6182 {
6183 if (*val == '0')
6184 /* Do nothing - this is the default */;
6185 else if (*val == '1')
6186 *argt |= (EXTRA | NOSPC | NEEDARG);
6187 else if (*val == '*')
6188 *argt |= EXTRA;
6189 else if (*val == '?')
6190 *argt |= (EXTRA | NOSPC);
6191 else if (*val == '+')
6192 *argt |= (EXTRA | NEEDARG);
6193 else
6194 goto wrong_nargs;
6195 }
6196 else
6197 {
6198wrong_nargs:
6199 EMSG(_("E176: Invalid number of arguments"));
6200 return FAIL;
6201 }
6202 }
6203 else if (STRNICMP(attr, "range", attrlen) == 0)
6204 {
6205 *argt |= RANGE;
6206 if (vallen == 1 && *val == '%')
6207 *argt |= DFLALL;
6208 else if (val != NULL)
6209 {
6210 p = val;
6211 if (*def >= 0)
6212 {
6213two_count:
6214 EMSG(_("E177: Count cannot be specified twice"));
6215 return FAIL;
6216 }
6217
6218 *def = getdigits(&p);
6219 *argt |= (ZEROR | NOTADR);
6220
6221 if (p != val + vallen || vallen == 0)
6222 {
6223invalid_count:
6224 EMSG(_("E178: Invalid default value for count"));
6225 return FAIL;
6226 }
6227 }
6228 }
6229 else if (STRNICMP(attr, "count", attrlen) == 0)
6230 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006231 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232
6233 if (val != NULL)
6234 {
6235 p = val;
6236 if (*def >= 0)
6237 goto two_count;
6238
6239 *def = getdigits(&p);
6240
6241 if (p != val + vallen)
6242 goto invalid_count;
6243 }
6244
6245 if (*def < 0)
6246 *def = 0;
6247 }
6248 else if (STRNICMP(attr, "complete", attrlen) == 0)
6249 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 if (val == NULL)
6251 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006252 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 return FAIL;
6254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006256 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6257 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006260 else if (STRNICMP(attr, "addr", attrlen) == 0)
6261 {
6262 *argt |= RANGE;
6263 if (val == NULL)
6264 {
6265 EMSG(_("E179: argument required for -addr"));
6266 return FAIL;
6267 }
6268 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6269 == FAIL)
6270 return FAIL;
6271 if (addr_type_arg != ADDR_LINES)
6272 *argt |= (ZEROR | NOTADR) ;
6273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 else
6275 {
6276 char_u ch = attr[len];
6277 attr[len] = '\0';
6278 EMSG2(_("E181: Invalid attribute: %s"), attr);
6279 attr[len] = ch;
6280 return FAIL;
6281 }
6282 }
6283
6284 return OK;
6285}
6286
Bram Moolenaara850a712009-01-28 14:42:59 +00006287/*
6288 * ":command ..."
6289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006291ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292{
6293 char_u *name;
6294 char_u *end;
6295 char_u *p;
6296 long argt = 0;
6297 long def = -1;
6298 int flags = 0;
6299 int compl = EXPAND_NOTHING;
6300 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006301 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006303 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304
6305 p = eap->arg;
6306
6307 /* Check for attributes */
6308 while (*p == '-')
6309 {
6310 ++p;
6311 end = skiptowhite(p);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006312 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 == FAIL)
6314 return;
6315 p = skipwhite(end);
6316 }
6317
6318 /* Get the name (if any) and skip to the following argument */
6319 name = p;
6320 if (ASCII_ISALPHA(*p))
6321 while (ASCII_ISALNUM(*p))
6322 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006323 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 {
6325 EMSG(_("E182: Invalid command name"));
6326 return;
6327 }
6328 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006329 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330
6331 /* If there is nothing after the name, and no attributes were specified,
6332 * we are listing commands
6333 */
6334 p = skipwhite(end);
6335 if (!has_attr && ends_excmd(*p))
6336 {
6337 uc_list(name, end - name);
6338 }
6339 else if (!ASCII_ISUPPER(*name))
6340 {
6341 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6342 return;
6343 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006344 else if ((name_len == 1 && *name == 'X')
6345 || (name_len <= 4
6346 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6347 {
6348 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6349 return;
6350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 else
6352 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006353 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354}
6355
6356/*
6357 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 * Clear all user commands, global and for current buffer.
6359 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006360 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006361ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362{
6363 uc_clear(&ucmds);
6364 uc_clear(&curbuf->b_ucmds);
6365}
6366
6367/*
6368 * Clear all user commands for "gap".
6369 */
6370 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006371uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372{
6373 int i;
6374 ucmd_T *cmd;
6375
6376 for (i = 0; i < gap->ga_len; ++i)
6377 {
6378 cmd = USER_CMD_GA(gap, i);
6379 vim_free(cmd->uc_name);
6380 vim_free(cmd->uc_rep);
6381# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6382 vim_free(cmd->uc_compl_arg);
6383# endif
6384 }
6385 ga_clear(gap);
6386}
6387
6388 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006389ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390{
6391 int i = 0;
6392 ucmd_T *cmd = NULL;
6393 int cmp = -1;
6394 garray_T *gap;
6395
6396 gap = &curbuf->b_ucmds;
6397 for (;;)
6398 {
6399 for (i = 0; i < gap->ga_len; ++i)
6400 {
6401 cmd = USER_CMD_GA(gap, i);
6402 cmp = STRCMP(eap->arg, cmd->uc_name);
6403 if (cmp <= 0)
6404 break;
6405 }
6406 if (gap == &ucmds || cmp == 0)
6407 break;
6408 gap = &ucmds;
6409 }
6410
6411 if (cmp != 0)
6412 {
6413 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6414 return;
6415 }
6416
6417 vim_free(cmd->uc_name);
6418 vim_free(cmd->uc_rep);
6419# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6420 vim_free(cmd->uc_compl_arg);
6421# endif
6422
6423 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424
6425 if (i < gap->ga_len)
6426 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6427}
6428
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006429/*
6430 * split and quote args for <f-args>
6431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006433uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434{
6435 char_u *buf;
6436 char_u *p;
6437 char_u *q;
6438 int len;
6439
6440 /* Precalculate length */
6441 p = arg;
6442 len = 2; /* Initial and final quotes */
6443
6444 while (*p)
6445 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006446 if (p[0] == '\\' && p[1] == '\\')
6447 {
6448 len += 2;
6449 p += 2;
6450 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006451 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 {
6453 len += 1;
6454 p += 2;
6455 }
6456 else if (*p == '\\' || *p == '"')
6457 {
6458 len += 2;
6459 p += 1;
6460 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006461 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 {
6463 p = skipwhite(p);
6464 if (*p == NUL)
6465 break;
6466 len += 3; /* "," */
6467 }
6468 else
6469 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006470#ifdef FEAT_MBYTE
6471 int charlen = (*mb_ptr2len)(p);
6472 len += charlen;
6473 p += charlen;
6474#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 ++len;
6476 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 }
6479 }
6480
6481 buf = alloc(len + 1);
6482 if (buf == NULL)
6483 {
6484 *lenp = 0;
6485 return buf;
6486 }
6487
6488 p = arg;
6489 q = buf;
6490 *q++ = '"';
6491 while (*p)
6492 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006493 if (p[0] == '\\' && p[1] == '\\')
6494 {
6495 *q++ = '\\';
6496 *q++ = '\\';
6497 p += 2;
6498 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006499 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 {
6501 *q++ = p[1];
6502 p += 2;
6503 }
6504 else if (*p == '\\' || *p == '"')
6505 {
6506 *q++ = '\\';
6507 *q++ = *p++;
6508 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006509 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 {
6511 p = skipwhite(p);
6512 if (*p == NUL)
6513 break;
6514 *q++ = '"';
6515 *q++ = ',';
6516 *q++ = '"';
6517 }
6518 else
6519 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006520 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 }
6522 }
6523 *q++ = '"';
6524 *q = 0;
6525
6526 *lenp = len;
6527 return buf;
6528}
6529
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006530 static size_t
6531add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6532{
6533 size_t result;
6534
6535 result = STRLEN(mod_str);
6536 if (*multi_mods)
6537 result += 1;
6538 if (buf != NULL)
6539 {
6540 if (*multi_mods)
6541 STRCAT(buf, " ");
6542 STRCAT(buf, mod_str);
6543 }
6544
6545 *multi_mods = 1;
6546
6547 return result;
6548}
6549
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550/*
6551 * Check for a <> code in a user command.
6552 * "code" points to the '<'. "len" the length of the <> (inclusive).
6553 * "buf" is where the result is to be added.
6554 * "split_buf" points to a buffer used for splitting, caller should free it.
6555 * "split_len" is the length of what "split_buf" contains.
6556 * Returns the length of the replacement, which has been added to "buf".
6557 * Returns -1 if there was no match, and only the "<" has been copied.
6558 */
6559 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006560uc_check_code(
6561 char_u *code,
6562 size_t len,
6563 char_u *buf,
6564 ucmd_T *cmd, /* the user command we're expanding */
6565 exarg_T *eap, /* ex arguments */
6566 char_u **split_buf,
6567 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568{
6569 size_t result = 0;
6570 char_u *p = code + 1;
6571 size_t l = len - 2;
6572 int quote = 0;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006573 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_MODS,
6574 ct_REGISTER, ct_LT, ct_NONE } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575
6576 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6577 {
6578 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6579 p += 2;
6580 l -= 2;
6581 }
6582
Bram Moolenaar371d5402006-03-20 21:47:49 +00006583 ++l;
6584 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006586 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006588 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006590 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006592 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006594 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006596 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006598 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006600 else if (STRNICMP(p, "mods>", l) == 0)
6601 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602
6603 switch (type)
6604 {
6605 case ct_ARGS:
6606 /* Simple case first */
6607 if (*eap->arg == NUL)
6608 {
6609 if (quote == 1)
6610 {
6611 result = 2;
6612 if (buf != NULL)
6613 STRCPY(buf, "''");
6614 }
6615 else
6616 result = 0;
6617 break;
6618 }
6619
6620 /* When specified there is a single argument don't split it.
6621 * Works for ":Cmd %" when % is "a b c". */
6622 if ((eap->argt & NOSPC) && quote == 2)
6623 quote = 1;
6624
6625 switch (quote)
6626 {
6627 case 0: /* No quoting, no splitting */
6628 result = STRLEN(eap->arg);
6629 if (buf != NULL)
6630 STRCPY(buf, eap->arg);
6631 break;
6632 case 1: /* Quote, but don't split */
6633 result = STRLEN(eap->arg) + 2;
6634 for (p = eap->arg; *p; ++p)
6635 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006636#ifdef FEAT_MBYTE
6637 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6638 /* DBCS can contain \ in a trail byte, skip the
6639 * double-byte character. */
6640 ++p;
6641 else
6642#endif
6643 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 ++result;
6645 }
6646
6647 if (buf != NULL)
6648 {
6649 *buf++ = '"';
6650 for (p = eap->arg; *p; ++p)
6651 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006652#ifdef FEAT_MBYTE
6653 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6654 /* DBCS can contain \ in a trail byte, copy the
6655 * double-byte character to avoid escaping. */
6656 *buf++ = *p++;
6657 else
6658#endif
6659 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 *buf++ = '\\';
6661 *buf++ = *p;
6662 }
6663 *buf = '"';
6664 }
6665
6666 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006667 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 /* This is hard, so only do it once, and cache the result */
6669 if (*split_buf == NULL)
6670 *split_buf = uc_split_args(eap->arg, split_len);
6671
6672 result = *split_len;
6673 if (buf != NULL && result != 0)
6674 STRCPY(buf, *split_buf);
6675
6676 break;
6677 }
6678 break;
6679
6680 case ct_BANG:
6681 result = eap->forceit ? 1 : 0;
6682 if (quote)
6683 result += 2;
6684 if (buf != NULL)
6685 {
6686 if (quote)
6687 *buf++ = '"';
6688 if (eap->forceit)
6689 *buf++ = '!';
6690 if (quote)
6691 *buf = '"';
6692 }
6693 break;
6694
6695 case ct_LINE1:
6696 case ct_LINE2:
6697 case ct_COUNT:
6698 {
6699 char num_buf[20];
6700 long num = (type == ct_LINE1) ? eap->line1 :
6701 (type == ct_LINE2) ? eap->line2 :
6702 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6703 size_t num_len;
6704
6705 sprintf(num_buf, "%ld", num);
6706 num_len = STRLEN(num_buf);
6707 result = num_len;
6708
6709 if (quote)
6710 result += 2;
6711
6712 if (buf != NULL)
6713 {
6714 if (quote)
6715 *buf++ = '"';
6716 STRCPY(buf, num_buf);
6717 buf += num_len;
6718 if (quote)
6719 *buf = '"';
6720 }
6721
6722 break;
6723 }
6724
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006725 case ct_MODS:
6726 {
6727 int multi_mods = 0;
6728 typedef struct {
6729 int *varp;
6730 char *name;
6731 } mod_entry_T;
6732 static mod_entry_T mod_entries[] = {
6733#ifdef FEAT_BROWSE_CMD
6734 {&cmdmod.browse, "browse"},
6735#endif
6736#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6737 {&cmdmod.confirm, "confirm"},
6738#endif
6739 {&cmdmod.hide, "hide"},
6740 {&cmdmod.keepalt, "keepalt"},
6741 {&cmdmod.keepjumps, "keepjumps"},
6742 {&cmdmod.keepmarks, "keepmarks"},
6743 {&cmdmod.keeppatterns, "keeppatterns"},
6744 {&cmdmod.lockmarks, "lockmarks"},
6745 {&cmdmod.noswapfile, "noswapfile"},
6746 {NULL, NULL}
6747 };
6748 int i;
6749
6750 result = quote ? 2 : 0;
6751 if (buf != NULL)
6752 {
6753 if (quote)
6754 *buf++ = '"';
6755 *buf = '\0';
6756 }
6757
6758#ifdef FEAT_WINDOWS
6759 /* :aboveleft and :leftabove */
6760 if (cmdmod.split & WSP_ABOVE)
6761 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6762 /* :belowright and :rightbelow */
6763 if (cmdmod.split & WSP_BELOW)
6764 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6765 /* :botright */
6766 if (cmdmod.split & WSP_BOT)
6767 result += add_cmd_modifier(buf, "botright", &multi_mods);
6768#endif
6769
6770 /* the modifiers that are simple flags */
6771 for (i = 0; mod_entries[i].varp != NULL; ++i)
6772 if (*mod_entries[i].varp)
6773 result += add_cmd_modifier(buf, mod_entries[i].name,
6774 &multi_mods);
6775
6776 /* TODO: How to support :noautocmd? */
6777#ifdef HAVE_SANDBOX
6778 /* TODO: How to support :sandbox?*/
6779#endif
6780 /* :silent */
6781 if (msg_silent > 0)
6782 result += add_cmd_modifier(buf,
6783 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
6784#ifdef FEAT_WINDOWS
6785 /* :tab */
6786 if (cmdmod.tab > 0)
6787 result += add_cmd_modifier(buf, "tab", &multi_mods);
6788 /* :topleft */
6789 if (cmdmod.split & WSP_TOP)
6790 result += add_cmd_modifier(buf, "topleft", &multi_mods);
6791#endif
6792 /* TODO: How to support :unsilent?*/
6793 /* :verbose */
6794 if (p_verbose > 0)
6795 result += add_cmd_modifier(buf, "verbose", &multi_mods);
6796#ifdef FEAT_WINDOWS
6797 /* :vertical */
6798 if (cmdmod.split & WSP_VERT)
6799 result += add_cmd_modifier(buf, "vertical", &multi_mods);
6800#endif
6801 if (quote && buf != NULL)
6802 {
6803 buf += result - 2;
6804 *buf = '"';
6805 }
6806 break;
6807 }
6808
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 case ct_REGISTER:
6810 result = eap->regname ? 1 : 0;
6811 if (quote)
6812 result += 2;
6813 if (buf != NULL)
6814 {
6815 if (quote)
6816 *buf++ = '\'';
6817 if (eap->regname)
6818 *buf++ = eap->regname;
6819 if (quote)
6820 *buf = '\'';
6821 }
6822 break;
6823
6824 case ct_LT:
6825 result = 1;
6826 if (buf != NULL)
6827 *buf = '<';
6828 break;
6829
6830 default:
6831 /* Not recognized: just copy the '<' and return -1. */
6832 result = (size_t)-1;
6833 if (buf != NULL)
6834 *buf = '<';
6835 break;
6836 }
6837
6838 return result;
6839}
6840
6841 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006842do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843{
6844 char_u *buf;
6845 char_u *p;
6846 char_u *q;
6847
6848 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006849 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006850 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 size_t len, totlen;
6852
6853 size_t split_len = 0;
6854 char_u *split_buf = NULL;
6855 ucmd_T *cmd;
6856#ifdef FEAT_EVAL
6857 scid_T save_current_SID = current_SID;
6858#endif
6859
6860 if (eap->cmdidx == CMD_USER)
6861 cmd = USER_CMD(eap->useridx);
6862 else
6863 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6864
6865 /*
6866 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006867 * First round: "buf" is NULL, compute length, allocate "buf".
6868 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 */
6870 buf = NULL;
6871 for (;;)
6872 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006873 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006874 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006876
6877 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006879 start = vim_strchr(p, '<');
6880 if (start != NULL)
6881 end = vim_strchr(start + 1, '>');
6882 if (buf != NULL)
6883 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006884 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6885 ;
6886 if (*ksp == K_SPECIAL
6887 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006888 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6889# ifdef FEAT_GUI
6890 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6891# endif
6892 ))
6893 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006894 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006895 * KS_SPECIAL KE_FILLER, like for mappings, but
6896 * do_cmdline() doesn't handle that, so convert it back.
6897 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6898 len = ksp - p;
6899 if (len > 0)
6900 {
6901 mch_memmove(q, p, len);
6902 q += len;
6903 }
6904 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6905 p = ksp + 3;
6906 continue;
6907 }
6908 }
6909
6910 /* break if there no <item> is found */
6911 if (start == NULL || end == NULL)
6912 break;
6913
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 /* Include the '>' */
6915 ++end;
6916
6917 /* Take everything up to the '<' */
6918 len = start - p;
6919 if (buf == NULL)
6920 totlen += len;
6921 else
6922 {
6923 mch_memmove(q, p, len);
6924 q += len;
6925 }
6926
6927 len = uc_check_code(start, end - start, q, cmd, eap,
6928 &split_buf, &split_len);
6929 if (len == (size_t)-1)
6930 {
6931 /* no match, continue after '<' */
6932 p = start + 1;
6933 len = 1;
6934 }
6935 else
6936 p = end;
6937 if (buf == NULL)
6938 totlen += len;
6939 else
6940 q += len;
6941 }
6942 if (buf != NULL) /* second time here, finished */
6943 {
6944 STRCPY(q, p);
6945 break;
6946 }
6947
6948 totlen += STRLEN(p); /* Add on the trailing characters */
6949 buf = alloc((unsigned)(totlen + 1));
6950 if (buf == NULL)
6951 {
6952 vim_free(split_buf);
6953 return;
6954 }
6955 }
6956
6957#ifdef FEAT_EVAL
6958 current_SID = cmd->uc_scriptID;
6959#endif
6960 (void)do_cmdline(buf, eap->getline, eap->cookie,
6961 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6962#ifdef FEAT_EVAL
6963 current_SID = save_current_SID;
6964#endif
6965 vim_free(buf);
6966 vim_free(split_buf);
6967}
6968
6969# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6970 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006971get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972{
6973 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6974}
6975
6976/*
6977 * Function given to ExpandGeneric() to obtain the list of user command names.
6978 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006980get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981{
6982 if (idx < curbuf->b_ucmds.ga_len)
6983 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6984 idx -= curbuf->b_ucmds.ga_len;
6985 if (idx < ucmds.ga_len)
6986 return USER_CMD(idx)->uc_name;
6987 return NULL;
6988}
6989
6990/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006991 * Function given to ExpandGeneric() to obtain the list of user address type names.
6992 */
6993 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006994get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006995{
6996 return (char_u *)addr_type_complete[idx].name;
6997}
6998
6999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 * Function given to ExpandGeneric() to obtain the list of user command
7001 * attributes.
7002 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007004get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005{
7006 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007007 {"addr", "bang", "bar", "buffer", "complete",
7008 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007010 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 return NULL;
7012 return (char_u *)user_cmd_flags[idx];
7013}
7014
7015/*
7016 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
7017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007019get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020{
7021 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
7022
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007023 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 return NULL;
7025 return (char_u *)user_cmd_nargs[idx];
7026}
7027
7028/*
7029 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7030 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007032get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033{
7034 return (char_u *)command_complete[idx].name;
7035}
7036# endif /* FEAT_CMDL_COMPL */
7037
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007038/*
7039 * Parse address type argument
7040 */
7041 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007042parse_addr_type_arg(
7043 char_u *value,
7044 int vallen,
7045 long *argt,
7046 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007047{
7048 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007049
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007050 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7051 {
7052 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7053 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7054 if (a && b)
7055 {
7056 *addr_type_arg = addr_type_complete[i].expand;
7057 break;
7058 }
7059 }
7060
7061 if (addr_type_complete[i].expand == -1)
7062 {
7063 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007064
Bram Moolenaar1c465442017-03-12 20:10:05 +01007065 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007066 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007067 err[i] = NUL;
7068 EMSG2(_("E180: Invalid address type value: %s"), err);
7069 return FAIL;
7070 }
7071
7072 if (*addr_type_arg != ADDR_LINES)
7073 *argt |= NOTADR;
7074
7075 return OK;
7076}
7077
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078#endif /* FEAT_USR_CMDS */
7079
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007080#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7081/*
7082 * Parse a completion argument "value[vallen]".
7083 * The detected completion goes in "*complp", argument type in "*argt".
7084 * When there is an argument, for function and user defined completion, it's
7085 * copied to allocated memory and stored in "*compl_arg".
7086 * Returns FAIL if something is wrong.
7087 */
7088 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007089parse_compl_arg(
7090 char_u *value,
7091 int vallen,
7092 int *complp,
7093 long *argt,
7094 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007095{
7096 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007097# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007098 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007099# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007100 int i;
7101 int valend = vallen;
7102
7103 /* Look for any argument part - which is the part after any ',' */
7104 for (i = 0; i < vallen; ++i)
7105 {
7106 if (value[i] == ',')
7107 {
7108 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007109# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007110 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007111# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007112 valend = i;
7113 break;
7114 }
7115 }
7116
7117 for (i = 0; command_complete[i].expand != 0; ++i)
7118 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007119 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007120 && STRNCMP(value, command_complete[i].name, valend) == 0)
7121 {
7122 *complp = command_complete[i].expand;
7123 if (command_complete[i].expand == EXPAND_BUFFERS)
7124 *argt |= BUFNAME;
7125 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7126 || command_complete[i].expand == EXPAND_FILES)
7127 *argt |= XFILE;
7128 break;
7129 }
7130 }
7131
7132 if (command_complete[i].expand == 0)
7133 {
7134 EMSG2(_("E180: Invalid complete value: %s"), value);
7135 return FAIL;
7136 }
7137
7138# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7139 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7140 && arg != NULL)
7141# else
7142 if (arg != NULL)
7143# endif
7144 {
7145 EMSG(_("E468: Completion argument only allowed for custom completion"));
7146 return FAIL;
7147 }
7148
7149# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7150 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7151 && arg == NULL)
7152 {
7153 EMSG(_("E467: Custom completion requires a function argument"));
7154 return FAIL;
7155 }
7156
7157 if (arg != NULL)
7158 *compl_arg = vim_strnsave(arg, (int)arglen);
7159# endif
7160 return OK;
7161}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007162
7163 int
7164cmdcomplete_str_to_type(char_u *complete_str)
7165{
7166 int i;
7167
7168 for (i = 0; command_complete[i].expand != 0; ++i)
7169 if (STRCMP(complete_str, command_complete[i].name) == 0)
7170 return command_complete[i].expand;
7171
7172 return EXPAND_NOTHING;
7173}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007174#endif
7175
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007177ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178{
Bram Moolenaare6850792010-05-14 15:28:44 +02007179 if (*eap->arg == NUL)
7180 {
7181#ifdef FEAT_EVAL
7182 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7183 char_u *p = NULL;
7184
7185 if (expr != NULL)
7186 {
7187 ++emsg_off;
7188 p = eval_to_string(expr, NULL, FALSE);
7189 --emsg_off;
7190 vim_free(expr);
7191 }
7192 if (p != NULL)
7193 {
7194 MSG(p);
7195 vim_free(p);
7196 }
7197 else
7198 MSG("default");
7199#else
7200 MSG(_("unknown"));
7201#endif
7202 }
7203 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007204 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205}
7206
7207 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007208ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209{
7210 if (*eap->arg == NUL && eap->cmd[2] == '!')
7211 MSG(_("Greetings, Vim user!"));
7212 do_highlight(eap->arg, eap->forceit, FALSE);
7213}
7214
7215
7216/*
7217 * Call this function if we thought we were going to exit, but we won't
7218 * (because of an error). May need to restore the terminal mode.
7219 */
7220 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007221not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222{
7223 exiting = FALSE;
7224 settmode(TMODE_RAW);
7225}
7226
7227/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007228 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 */
7230 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007231ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232{
Bram Moolenaarf240e182014-11-27 18:33:02 +01007233#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007234 win_T *wp;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007235#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007236
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237#ifdef FEAT_CMDWIN
7238 if (cmdwin_type != 0)
7239 {
7240 cmdwin_result = Ctrl_C;
7241 return;
7242 }
7243#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007244 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007245 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007246 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007247 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007248 return;
7249 }
Bram Moolenaarf240e182014-11-27 18:33:02 +01007250#ifdef FEAT_WINDOWS
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007251 if (eap->addr_count > 0)
7252 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007253 int wnr = eap->line2;
7254
7255 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7256 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007257 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007258 }
7259 else
Bram Moolenaarf240e182014-11-27 18:33:02 +01007260#endif
7261#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007262 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007263#endif
7264
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007265#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02007266 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007267 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02007268 * being closed (can only happen in autocommands). */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007269 if (curbuf_locked() || (wp->w_buffer->b_nwindows == 1
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007270 && wp->w_buffer->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007271 return;
7272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273
7274#ifdef FEAT_NETBEANS_INTG
7275 netbeansForcedQuit = eap->forceit;
7276#endif
7277
7278 /*
7279 * If there are more files or windows we won't exit.
7280 */
7281 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7282 exiting = TRUE;
7283 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007284 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7285 | (eap->forceit ? CCGD_FORCEIT : 0)
7286 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007288 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 {
7290 not_exiting();
7291 }
7292 else
7293 {
7294#ifdef FEAT_WINDOWS
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007295 /* quit last window
7296 * Note: only_one_window() returns true, even so a help window is
7297 * still open. In that case only quit, if no address has been
7298 * specified. Example:
7299 * :h|wincmd w|1q - don't quit
7300 * :h|wincmd w|q - quit
7301 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007302 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303#endif
7304 getout(0);
7305#ifdef FEAT_WINDOWS
7306# ifdef FEAT_GUI
7307 need_mouse_correct = TRUE;
7308# endif
7309 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007310 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311#endif
7312 }
7313}
7314
7315/*
7316 * ":cquit".
7317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007319ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320{
7321 getout(1); /* this does not always pass on the exit code to the Manx
7322 compiler. why? */
7323}
7324
7325/*
7326 * ":qall": try to quit all windows
7327 */
7328 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007329ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330{
7331# ifdef FEAT_CMDWIN
7332 if (cmdwin_type != 0)
7333 {
7334 if (eap->forceit)
7335 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7336 else
7337 cmdwin_result = K_XF2;
7338 return;
7339 }
7340# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007341
7342 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007343 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007344 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007345 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007346 return;
7347 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007348#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007349 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7350 /* Refuse to quit when locked or when the buffer in the last window is
7351 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007352 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007353 return;
7354#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007355
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007357 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 getout(0);
7359 not_exiting();
7360}
7361
Bram Moolenaard9967712006-03-11 21:18:15 +00007362#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363/*
7364 * ":close": close current window, unless it is the last one
7365 */
7366 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007367ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007369 win_T *win;
7370 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371# ifdef FEAT_CMDWIN
7372 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007373 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 else
7375# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007376 if (!text_locked()
7377#ifdef FEAT_AUTOCMD
7378 && !curbuf_locked()
7379#endif
7380 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007381 {
7382 if (eap->addr_count == 0)
7383 ex_win_close(eap->forceit, curwin, NULL);
7384 else {
Bram Moolenaar29323592016-07-24 22:04:11 +02007385 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007386 {
7387 winnr++;
7388 if (winnr == eap->line2)
7389 break;
7390 }
7391 if (win == NULL)
7392 win = lastwin;
7393 ex_win_close(eap->forceit, win, NULL);
7394 }
7395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396}
7397
Bram Moolenaard9967712006-03-11 21:18:15 +00007398# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007399/*
7400 * ":pclose": Close any preview window.
7401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007403ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007404{
7405 win_T *win;
7406
Bram Moolenaar29323592016-07-24 22:04:11 +02007407 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007408 if (win->w_p_pvw)
7409 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007410 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007411 break;
7412 }
7413}
Bram Moolenaard9967712006-03-11 21:18:15 +00007414# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007415
Bram Moolenaarf740b292006-02-16 22:11:02 +00007416/*
7417 * Close window "win" and take care of handling closing the last window for a
7418 * modified buffer.
7419 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007420 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007421ex_win_close(
7422 int forceit,
7423 win_T *win,
7424 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425{
7426 int need_hide;
7427 buf_T *buf = win->w_buffer;
7428
7429 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007430 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007432# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 if ((p_confirm || cmdmod.confirm) && p_write)
7434 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007435 bufref_T bufref;
7436
7437 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007439 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 return;
7441 need_hide = FALSE;
7442 }
7443 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007444# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 {
7446 EMSG(_(e_nowrtmsg));
7447 return;
7448 }
7449 }
7450
Bram Moolenaard9967712006-03-11 21:18:15 +00007451# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007453# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007454
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007456 if (tp == NULL)
7457 win_close(win, !need_hide && !P_HID(buf));
7458 else
7459 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460}
7461
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007463 * Handle the argument for a tabpage related ex command.
7464 * Returns a tabpage number.
7465 * When an error is encountered then eap->errmsg is set.
7466 */
7467 static int
7468get_tabpage_arg(exarg_T *eap)
7469{
7470 int tab_number;
7471 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7472
7473 if (eap->arg && *eap->arg != NUL)
7474 {
7475 char_u *p = eap->arg;
7476 char_u *p_save;
7477 int relative = 0; /* argument +N/-N means: go to N places to the
7478 * right/left relative to the current position. */
7479
7480 if (*p == '-')
7481 {
7482 relative = -1;
7483 p++;
7484 }
7485 else if (*p == '+')
7486 {
7487 relative = 1;
7488 p++;
7489 }
7490
7491 p_save = p;
7492 tab_number = getdigits(&p);
7493
7494 if (relative == 0)
7495 {
7496 if (STRCMP(p, "$") == 0)
7497 tab_number = LAST_TAB_NR;
7498 else if (p == p_save || *p_save == '-' || *p != NUL
7499 || tab_number > LAST_TAB_NR)
7500 {
7501 /* No numbers as argument. */
7502 eap->errmsg = e_invarg;
7503 goto theend;
7504 }
7505 }
7506 else
7507 {
7508 if (*p_save == NUL)
7509 tab_number = 1;
7510 else if (p == p_save || *p_save == '-' || *p != NUL
7511 || tab_number == 0)
7512 {
7513 /* No numbers as argument. */
7514 eap->errmsg = e_invarg;
7515 goto theend;
7516 }
7517 tab_number = tab_number * relative + tabpage_index(curtab);
7518 if (!unaccept_arg0 && relative == -1)
7519 --tab_number;
7520 }
7521 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7522 eap->errmsg = e_invarg;
7523 }
7524 else if (eap->addr_count > 0)
7525 {
7526 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007527 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007528 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007529 tab_number = 0;
7530 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007531 else
7532 {
7533 tab_number = eap->line2;
7534 if (!unaccept_arg0 && **eap->cmdlinep == '-')
7535 {
7536 --tab_number;
7537 if (tab_number < unaccept_arg0)
7538 eap->errmsg = e_invarg;
7539 }
7540 }
7541 }
7542 else
7543 {
7544 switch (eap->cmdidx)
7545 {
7546 case CMD_tabnext:
7547 tab_number = tabpage_index(curtab) + 1;
7548 if (tab_number > LAST_TAB_NR)
7549 tab_number = 1;
7550 break;
7551 case CMD_tabmove:
7552 tab_number = LAST_TAB_NR;
7553 break;
7554 default:
7555 tab_number = tabpage_index(curtab);
7556 }
7557 }
7558
7559theend:
7560 return tab_number;
7561}
7562
7563/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007564 * ":tabclose": close current tab page, unless it is the last one.
7565 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 */
7567 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007568ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007570 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007571 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007572
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007573# ifdef FEAT_CMDWIN
7574 if (cmdwin_type != 0)
7575 cmdwin_result = K_IGNORE;
7576 else
7577# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007578 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007579 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007580 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007582 tab_number = get_tabpage_arg(eap);
7583 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007584 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007585 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007586 if (tp == NULL)
7587 {
7588 beep_flush();
7589 return;
7590 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007591 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007592 {
7593 tabpage_close_other(tp, eap->forceit);
7594 return;
7595 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007596 else if (!text_locked()
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007597#ifdef FEAT_AUTOCMD
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007598 && !curbuf_locked()
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007599#endif
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007600 )
7601 tabpage_close(eap->forceit);
7602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007604}
7605
7606/*
7607 * ":tabonly": close all tab pages except the current one
7608 */
7609 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007610ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007611{
7612 tabpage_T *tp;
7613 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007614 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007615
7616# ifdef FEAT_CMDWIN
7617 if (cmdwin_type != 0)
7618 cmdwin_result = K_IGNORE;
7619 else
7620# endif
7621 if (first_tabpage->tp_next == NULL)
7622 MSG(_("Already only one tab page"));
7623 else
7624 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007625 tab_number = get_tabpage_arg(eap);
7626 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007627 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007628 goto_tabpage(tab_number);
7629 /* Repeat this up to a 1000 times, because autocommands may
7630 * mess up the lists. */
7631 for (done = 0; done < 1000; ++done)
7632 {
7633 FOR_ALL_TABPAGES(tp)
7634 if (tp->tp_topframe != topframe)
7635 {
7636 tabpage_close_other(tp, eap->forceit);
7637 /* if we failed to close it quit */
7638 if (valid_tabpage(tp))
7639 done = 1000;
7640 /* start over, "tp" is now invalid */
7641 break;
7642 }
7643 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007644 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007645 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007646 }
7647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649
7650/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007651 * Close the current tab page.
7652 */
7653 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007654tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007655{
7656 /* First close all the windows but the current one. If that worked then
7657 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007658 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007659 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007660 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007661 ex_win_close(forceit, curwin, NULL);
7662# ifdef FEAT_GUI
7663 need_mouse_correct = TRUE;
7664# endif
7665}
7666
7667/*
7668 * Close tab page "tp", which is not the current tab page.
7669 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007670 * Also takes care of the tab pages line disappearing when closing the
7671 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007672 */
7673 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007674tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007675{
7676 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007677 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007678 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007679
7680 /* Limit to 1000 windows, autocommands may add a window while we close
7681 * one. OK, so I'm paranoid... */
7682 while (++done < 1000)
7683 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007684 wp = tp->tp_firstwin;
7685 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007686
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007687 /* Autocommands may delete the tab page under our fingers and we may
7688 * fail to close a window with a modified buffer. */
7689 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007690 break;
7691 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007692
Bram Moolenaar29323592016-07-24 22:04:11 +02007693#ifdef FEAT_AUTOCMD
7694 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
7695#endif
7696
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007697 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007698 if (h != tabline_height())
7699 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007700}
7701
7702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 * ":only".
7704 */
7705 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007706ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007708 win_T *wp;
7709 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710# ifdef FEAT_GUI
7711 need_mouse_correct = TRUE;
7712# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007713 if (eap->addr_count > 0)
7714 {
7715 wnr = eap->line2;
7716 for (wp = firstwin; --wnr > 0; )
7717 {
7718 if (wp->w_next == NULL)
7719 break;
7720 else
7721 wp = wp->w_next;
7722 }
7723 win_goto(wp);
7724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 close_others(TRUE, eap->forceit);
7726}
7727
7728/*
7729 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007730 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007732 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007733ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734{
7735 if (eap->addr_count == 0)
7736 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007737 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738}
7739#endif /* FEAT_WINDOWS */
7740
7741 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007742ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007744 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745#ifdef FEAT_WINDOWS
Bram Moolenaar2256c992016-11-15 21:17:07 +01007746 if (!eap->skip)
7747 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748# ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007749 need_mouse_correct = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750# endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007751 if (eap->addr_count == 0)
7752 win_close(curwin, FALSE); /* don't free buffer */
7753 else
7754 {
7755 int winnr = 0;
7756 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007757
Bram Moolenaar2256c992016-11-15 21:17:07 +01007758 FOR_ALL_WINDOWS(win)
7759 {
7760 winnr++;
7761 if (winnr == eap->line2)
7762 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007763 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007764 if (win == NULL)
7765 win = lastwin;
7766 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770}
7771
7772/*
7773 * ":stop" and ":suspend": Suspend Vim.
7774 */
7775 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007776ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777{
7778 /*
7779 * Disallow suspending for "rvim".
7780 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007781 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 {
7783 if (!eap->forceit)
7784 autowrite_all();
7785 windgoto((int)Rows - 1, 0);
7786 out_char('\n');
7787 out_flush();
7788 stoptermcap();
7789 out_flush(); /* needed for SUN to restore xterm buffer */
7790#ifdef FEAT_TITLE
7791 mch_restore_title(3); /* restore window titles */
7792#endif
7793 ui_suspend(); /* call machine specific function */
7794#ifdef FEAT_TITLE
7795 maketitle();
7796 resettitle(); /* force updating the title */
7797#endif
7798 starttermcap();
7799 scroll_start(); /* scroll screen before redrawing */
7800 redraw_later_clear();
7801 shell_resized(); /* may have resized window */
7802 }
7803}
7804
7805/*
7806 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7807 */
7808 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007809ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810{
7811#ifdef FEAT_CMDWIN
7812 if (cmdwin_type != 0)
7813 {
7814 cmdwin_result = Ctrl_C;
7815 return;
7816 }
7817#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007818 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007819 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007820 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007821 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007822 return;
7823 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007824#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007825 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7826 /* Refuse to quit when locked or when the buffer in the last window is
7827 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007828 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007829 return;
7830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831
7832 /*
7833 * if more files or windows we won't exit
7834 */
7835 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7836 exiting = TRUE;
7837 if ( ((eap->cmdidx == CMD_wq
7838 || curbufIsChanged())
7839 && do_write(eap) == FAIL)
7840 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007841 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 {
7843 not_exiting();
7844 }
7845 else
7846 {
7847#ifdef FEAT_WINDOWS
7848 if (only_one_window()) /* quit last window, exit Vim */
7849#endif
7850 getout(0);
7851#ifdef FEAT_WINDOWS
7852# ifdef FEAT_GUI
7853 need_mouse_correct = TRUE;
7854# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007855 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 win_close(curwin, !P_HID(curwin->w_buffer));
7857#endif
7858 }
7859}
7860
7861/*
7862 * ":print", ":list", ":number".
7863 */
7864 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007865ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007867 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7868 EMSG(_(e_emptybuf));
7869 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007871 for ( ;!got_int; ui_breakcheck())
7872 {
7873 print_line(eap->line1,
7874 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7875 || (eap->flags & EXFLAG_NR)),
7876 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7877 if (++eap->line1 > eap->line2)
7878 break;
7879 out_flush(); /* show one line at a time */
7880 }
7881 setpcmark();
7882 /* put cursor at last line */
7883 curwin->w_cursor.lnum = eap->line2;
7884 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 }
7886
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888}
7889
7890#ifdef FEAT_BYTEOFF
7891 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007892ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893{
7894 goto_byte(eap->line2);
7895}
7896#endif
7897
7898/*
7899 * ":shell".
7900 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007902ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903{
7904 do_shell(NULL, 0);
7905}
7906
7907#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7908 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007909 || defined(FEAT_GUI_MSWIN) \
7910 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 || defined(PROTO)
7912
7913/*
7914 * Handle a file drop. The code is here because a drop is *nearly* like an
7915 * :args command, but not quite (we have a list of exact filenames, so we
7916 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7917 * code is very similar to :args and hence needs access to a lot of the static
7918 * functions in this file.
7919 *
7920 * The list should be allocated using alloc(), as should each item in the
7921 * list. This function takes over responsibility for freeing the list.
7922 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007923 * XXX The list is made into the argument list. This is freed using
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007924 * FreeWild(), which does a series of vim_free() calls.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 */
7926 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007927handle_drop(
7928 int filec, /* the number of files dropped */
7929 char_u **filev, /* the list of files dropped */
7930 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931{
7932 exarg_T ea;
7933 int save_msg_scroll = msg_scroll;
7934
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007935 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007936 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007938#ifdef FEAT_AUTOCMD
7939 if (curbuf_locked())
7940 return;
7941#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007942 /* When the screen is being updated we should not change buffers and
7943 * windows structures, it may cause freed memory to be used. */
7944 if (updating_screen)
7945 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946
7947 /* Check whether the current buffer is changed. If so, we will need
7948 * to split the current window or data could be lost.
7949 * We don't need to check if the 'hidden' option is set, as in this
7950 * case the buffer won't be lost.
7951 */
7952 if (!P_HID(curbuf) && !split)
7953 {
7954 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007955 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 --emsg_off;
7957 }
7958 if (split)
7959 {
7960# ifdef FEAT_WINDOWS
7961 if (win_split(0, 0) == FAIL)
7962 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007963 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964
7965 /* When splitting the window, create a new alist. Otherwise the
7966 * existing one is overwritten. */
7967 alist_unlink(curwin->w_alist);
7968 alist_new();
7969# else
7970 return; /* can't split, always fail */
7971# endif
7972 }
7973
7974 /*
7975 * Set up the new argument list.
7976 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007977 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978
7979 /*
7980 * Move to the first file.
7981 */
7982 /* Fake up a minimal "next" command for do_argfile() */
7983 vim_memset(&ea, 0, sizeof(ea));
7984 ea.cmd = (char_u *)"next";
7985 do_argfile(&ea, 0);
7986
7987 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7988 * mode that is not needed here. */
7989 need_start_insertmode = FALSE;
7990
7991 /* Restore msg_scroll, otherwise a following command may cause scrolling
7992 * unexpectedly. The screen will be redrawn by the caller, thus
7993 * msg_scroll being set by displaying a message is irrelevant. */
7994 msg_scroll = save_msg_scroll;
7995}
7996#endif
7997
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998/*
7999 * Clear an argument list: free all file names and reset it to zero entries.
8000 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008001 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008002alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003{
8004 while (--al->al_ga.ga_len >= 0)
8005 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
8006 ga_clear(&al->al_ga);
8007}
8008
8009/*
8010 * Init an argument list.
8011 */
8012 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008013alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014{
8015 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
8016}
8017
8018#if defined(FEAT_WINDOWS) || defined(PROTO)
8019
8020/*
8021 * Remove a reference from an argument list.
8022 * Ignored when the argument list is the global one.
8023 * If the argument list is no longer used by any window, free it.
8024 */
8025 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008026alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027{
8028 if (al != &global_alist && --al->al_refcount <= 0)
8029 {
8030 alist_clear(al);
8031 vim_free(al);
8032 }
8033}
8034
8035# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
8036/*
8037 * Create a new argument list and use it for the current window.
8038 */
8039 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008040alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041{
8042 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
8043 if (curwin->w_alist == NULL)
8044 {
8045 curwin->w_alist = &global_alist;
8046 ++global_alist.al_refcount;
8047 }
8048 else
8049 {
8050 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008051 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 alist_init(curwin->w_alist);
8053 }
8054}
8055# endif
8056#endif
8057
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008058#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059/*
8060 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008061 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8062 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 */
8064 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008065alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066{
8067 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008068 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 char_u **new_arg_files;
8070 int new_arg_file_count;
8071 char_u *save_p_su = p_su;
8072 int i;
8073
8074 /* Don't use 'suffixes' here. This should work like the shell did the
8075 * expansion. Also, the vimrc file isn't read yet, thus the user
8076 * can't set the options. */
8077 p_su = empty_option;
8078 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8079 if (old_arg_files != NULL)
8080 {
8081 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008082 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8083 old_arg_count = GARGCOUNT;
8084 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008086 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 && new_arg_file_count > 0)
8088 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008089 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8090 TRUE, fnum_list, fnum_len);
8091 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 }
8094 p_su = save_p_su;
8095}
8096#endif
8097
8098/*
8099 * Set the argument list for the current window.
8100 * Takes over the allocated files[] and the allocated fnames in it.
8101 */
8102 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008103alist_set(
8104 alist_T *al,
8105 int count,
8106 char_u **files,
8107 int use_curbuf,
8108 int *fnum_list,
8109 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110{
8111 int i;
8112
8113 alist_clear(al);
8114 if (ga_grow(&al->al_ga, count) == OK)
8115 {
8116 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008117 {
8118 if (got_int)
8119 {
8120 /* When adding many buffers this can take a long time. Allow
8121 * interrupting here. */
8122 while (i < count)
8123 vim_free(files[i++]);
8124 break;
8125 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008126
8127 /* May set buffer name of a buffer previously used for the
8128 * argument list, so that it's re-used by alist_add. */
8129 if (fnum_list != NULL && i < fnum_len)
8130 buf_set_name(fnum_list[i], files[i]);
8131
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008133 ui_breakcheck();
8134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 vim_free(files);
8136 }
8137 else
8138 FreeWild(count, files);
8139#ifdef FEAT_WINDOWS
8140 if (al == &global_alist)
8141#endif
8142 arg_had_last = FALSE;
8143}
8144
8145/*
8146 * Add file "fname" to argument list "al".
8147 * "fname" must have been allocated and "al" must have been checked for room.
8148 */
8149 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008150alist_add(
8151 alist_T *al,
8152 char_u *fname,
8153 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154{
8155 if (fname == NULL) /* don't add NULL file names */
8156 return;
8157#ifdef BACKSLASH_IN_FILENAME
8158 slash_adjust(fname);
8159#endif
8160 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8161 if (set_fnum > 0)
8162 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8163 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8164 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165}
8166
8167#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8168/*
8169 * Adjust slashes in file names. Called after 'shellslash' was set.
8170 */
8171 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008172alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173{
8174 int i;
8175# ifdef FEAT_WINDOWS
8176 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008177 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178# endif
8179
8180 for (i = 0; i < GARGCOUNT; ++i)
8181 if (GARGLIST[i].ae_fname != NULL)
8182 slash_adjust(GARGLIST[i].ae_fname);
8183# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00008184 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 if (wp->w_alist != &global_alist)
8186 for (i = 0; i < WARGCOUNT(wp); ++i)
8187 if (WARGLIST(wp)[i].ae_fname != NULL)
8188 slash_adjust(WARGLIST(wp)[i].ae_fname);
8189# endif
8190}
8191#endif
8192
8193/*
8194 * ":preserve".
8195 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008197ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008199 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 ml_preserve(curbuf, TRUE);
8201}
8202
8203/*
8204 * ":recover".
8205 */
8206 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008207ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208{
8209 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8210 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008211 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8212 | CCGD_MULTWIN
8213 | (eap->forceit ? CCGD_FORCEIT : 0)
8214 | CCGD_EXCMD)
8215
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 && (*eap->arg == NUL
8217 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8218 ml_recover();
8219 recoverymode = FALSE;
8220}
8221
8222/*
8223 * Command modifier used in a wrong way.
8224 */
8225 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008226ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227{
8228 eap->errmsg = e_invcmd;
8229}
8230
8231#ifdef FEAT_WINDOWS
8232/*
8233 * :sview [+command] file split window with new file, read-only
8234 * :split [[+command] file] split window with current or new file
8235 * :vsplit [[+command] file] split window vertically with current or new file
8236 * :new [[+command] file] split window with no or new file
8237 * :vnew [[+command] file] split vertically window with no or new file
8238 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008239 *
8240 * :tabedit open new Tab page with empty window
8241 * :tabedit [+command] file open new Tab page and edit "file"
8242 * :tabnew [[+command] file] just like :tabedit
8243 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 */
8245 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008246ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008248 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008249# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008251# endif
8252# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008254# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008256# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008258# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008260# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008262 * quickfix windows. But it's OK when doing ":tab split". */
8263 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 {
8265 if (eap->cmdidx == CMD_split)
8266 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 if (eap->cmdidx == CMD_vsplit)
8268 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008270# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008272# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008273 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 {
8275 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8276 FNAME_MESS, TRUE, curbuf->b_ffname);
8277 if (fname == NULL)
8278 goto theend;
8279 eap->arg = fname;
8280 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008281# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008283# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008285# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 && eap->cmdidx != CMD_new)
8289 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008290# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008291 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008292# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008293 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008294# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008295 au_has_group((char_u *)"FileExplorer"))
8296 {
8297 /* No browsing supported but we do have the file explorer:
8298 * Edit the directory. */
8299 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8300 eap->arg = (char_u *)".";
8301 }
8302 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008303# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008304 {
8305 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008307 if (fname == NULL)
8308 goto theend;
8309 eap->arg = fname;
8310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 }
8312 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008313# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008315 /*
8316 * Either open new tab page or split the window.
8317 */
8318 if (eap->cmdidx == CMD_tabedit
8319 || eap->cmdidx == CMD_tabfind
8320 || eap->cmdidx == CMD_tabnew)
8321 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008322 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8323 : eap->addr_count == 0 ? 0
8324 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008325 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008326 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008327
8328 /* set the alternate buffer for the window we came from */
8329 if (curwin != old_curwin
8330 && win_valid(old_curwin)
8331 && old_curwin->w_buffer != curbuf
8332 && !cmdmod.keepalt)
8333 old_curwin->w_alt_fnum = curbuf->b_fnum;
8334 }
8335 }
8336 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8338 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008339# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 /* Reset 'scrollbind' when editing another file, but keep it when
8341 * doing ":split" without arguments. */
8342 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008343# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008345# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008347 {
8348 RESET_BINDING(curwin);
8349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 else
8351 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008352# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 do_exedit(eap, old_curwin);
8354 }
8355
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008356# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008358# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008360# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361theend:
8362 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008363# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008365
8366/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008367 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008368 */
8369 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008370tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008371{
8372 exarg_T ea;
8373
8374 vim_memset(&ea, 0, sizeof(ea));
8375 ea.cmdidx = CMD_tabnew;
8376 ea.cmd = (char_u *)"tabn";
8377 ea.arg = (char_u *)"";
8378 ex_splitview(&ea);
8379}
8380
8381/*
8382 * :tabnext command
8383 */
8384 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008385ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008386{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008387 int tab_number;
8388
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008389 switch (eap->cmdidx)
8390 {
8391 case CMD_tabfirst:
8392 case CMD_tabrewind:
8393 goto_tabpage(1);
8394 break;
8395 case CMD_tablast:
8396 goto_tabpage(9999);
8397 break;
8398 case CMD_tabprevious:
8399 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008400 if (eap->arg && *eap->arg != NUL)
8401 {
8402 char_u *p = eap->arg;
8403 char_u *p_save = p;
8404
8405 tab_number = getdigits(&p);
8406 if (p == p_save || *p_save == '-' || *p != NUL
8407 || tab_number == 0)
8408 {
8409 /* No numbers as argument. */
8410 eap->errmsg = e_invarg;
8411 return;
8412 }
8413 }
8414 else
8415 {
8416 if (eap->addr_count == 0)
8417 tab_number = 1;
8418 else
8419 {
8420 tab_number = eap->line2;
8421 if (tab_number < 1)
8422 {
8423 eap->errmsg = e_invrange;
8424 return;
8425 }
8426 }
8427 }
8428 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008429 break;
8430 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008431 tab_number = get_tabpage_arg(eap);
8432 if (eap->errmsg == NULL)
8433 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008434 break;
8435 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008436}
8437
8438/*
8439 * :tabmove command
8440 */
8441 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008442ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008443{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008444 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008445
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008446 tab_number = get_tabpage_arg(eap);
8447 if (eap->errmsg == NULL)
8448 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008449}
8450
8451/*
8452 * :tabs command: List tabs and their contents.
8453 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008454 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008455ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008456{
8457 tabpage_T *tp;
8458 win_T *wp;
8459 int tabcount = 1;
8460
8461 msg_start();
8462 msg_scroll = TRUE;
8463 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8464 {
8465 msg_putchar('\n');
8466 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008467 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008468 out_flush(); /* output one line at a time */
8469 ui_breakcheck();
8470
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008471 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008472 wp = firstwin;
8473 else
8474 wp = tp->tp_firstwin;
8475 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8476 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008477 msg_putchar('\n');
8478 msg_putchar(wp == curwin ? '>' : ' ');
8479 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008480 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8481 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008482 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008483 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008484 else
8485 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8486 IObuff, IOSIZE, TRUE);
8487 msg_outtrans(IObuff);
8488 out_flush(); /* output one line at a time */
8489 ui_breakcheck();
8490 }
8491 }
8492}
8493
8494#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495
8496/*
8497 * ":mode": Set screen mode.
8498 * If no argument given, just get the screen size and redraw.
8499 */
8500 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008501ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502{
8503 if (*eap->arg == NUL)
8504 shell_resized();
8505 else
8506 mch_screenmode(eap->arg);
8507}
8508
8509#ifdef FEAT_WINDOWS
8510/*
8511 * ":resize".
8512 * set, increment or decrement current window height
8513 */
8514 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008515ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516{
8517 int n;
8518 win_T *wp = curwin;
8519
8520 if (eap->addr_count > 0)
8521 {
8522 n = eap->line2;
8523 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8524 ;
8525 }
8526
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008527# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008529# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 if (cmdmod.split & WSP_VERT)
8532 {
8533 if (*eap->arg == '-' || *eap->arg == '+')
8534 n += W_WIDTH(curwin);
8535 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8536 n = 9999;
8537 win_setwidth_win((int)n, wp);
8538 }
8539 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 {
8541 if (*eap->arg == '-' || *eap->arg == '+')
8542 n += curwin->w_height;
8543 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8544 n = 9999;
8545 win_setheight_win((int)n, wp);
8546 }
8547}
8548#endif
8549
8550/*
8551 * ":find [+command] <file>" command.
8552 */
8553 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008554ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555{
8556#ifdef FEAT_SEARCHPATH
8557 char_u *fname;
8558 int count;
8559
8560 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8561 TRUE, curbuf->b_ffname);
8562 if (eap->addr_count > 0)
8563 {
8564 /* Repeat finding the file "count" times. This matters when it
8565 * appears several times in the path. */
8566 count = eap->line2;
8567 while (fname != NULL && --count > 0)
8568 {
8569 vim_free(fname);
8570 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8571 FALSE, curbuf->b_ffname);
8572 }
8573 }
8574
8575 if (fname != NULL)
8576 {
8577 eap->arg = fname;
8578#endif
8579 do_exedit(eap, NULL);
8580#ifdef FEAT_SEARCHPATH
8581 vim_free(fname);
8582 }
8583#endif
8584}
8585
8586/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008587 * ":open" simulation: for now just work like ":visual".
8588 */
8589 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008590ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008591{
8592 regmatch_T regmatch;
8593 char_u *p;
8594
8595 curwin->w_cursor.lnum = eap->line2;
8596 beginline(BL_SOL | BL_FIX);
8597 if (*eap->arg == '/')
8598 {
8599 /* ":open /pattern/": put cursor in column found with pattern */
8600 ++eap->arg;
8601 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8602 *p = NUL;
8603 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8604 if (regmatch.regprog != NULL)
8605 {
8606 regmatch.rm_ic = p_ic;
8607 p = ml_get_curline();
8608 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008609 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008610 else
8611 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008612 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008613 }
8614 /* Move to the NUL, ignore any other arguments. */
8615 eap->arg += STRLEN(eap->arg);
8616 }
8617 check_cursor();
8618
8619 eap->cmdidx = CMD_visual;
8620 do_exedit(eap, NULL);
8621}
8622
8623/*
8624 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 */
8626 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008627ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628{
8629 do_exedit(eap, NULL);
8630}
8631
8632/*
8633 * ":edit <file>" command and alikes.
8634 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008636do_exedit(
8637 exarg_T *eap,
8638 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639{
8640 int n;
8641#ifdef FEAT_WINDOWS
8642 int need_hide;
8643#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008644 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645
8646 /*
8647 * ":vi" command ends Ex mode.
8648 */
8649 if (exmode_active && (eap->cmdidx == CMD_visual
8650 || eap->cmdidx == CMD_view))
8651 {
8652 exmode_active = FALSE;
8653 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008654 {
8655 /* Special case: ":global/pat/visual\NLvi-commands" */
8656 if (global_busy)
8657 {
8658 int rd = RedrawingDisabled;
8659 int nwr = no_wait_return;
8660 int ms = msg_scroll;
8661#ifdef FEAT_GUI
8662 int he = hold_gui_events;
8663#endif
8664
8665 if (eap->nextcmd != NULL)
8666 {
8667 stuffReadbuff(eap->nextcmd);
8668 eap->nextcmd = NULL;
8669 }
8670
8671 if (exmode_was != EXMODE_VIM)
8672 settmode(TMODE_RAW);
8673 RedrawingDisabled = 0;
8674 no_wait_return = 0;
8675 need_wait_return = FALSE;
8676 msg_scroll = 0;
8677#ifdef FEAT_GUI
8678 hold_gui_events = 0;
8679#endif
8680 must_redraw = CLEAR;
8681
8682 main_loop(FALSE, TRUE);
8683
8684 RedrawingDisabled = rd;
8685 no_wait_return = nwr;
8686 msg_scroll = ms;
8687#ifdef FEAT_GUI
8688 hold_gui_events = he;
8689#endif
8690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 }
8694
8695 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008696 || eap->cmdidx == CMD_tabnew
8697 || eap->cmdidx == CMD_tabedit
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008698#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 || eap->cmdidx == CMD_vnew
8700#endif
8701 ) && *eap->arg == NUL)
8702 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008703 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 setpcmark();
8705 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008706 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8707 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 }
8709 else if ((eap->cmdidx != CMD_split
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008710#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 && eap->cmdidx != CMD_vsplit
8712#endif
8713 )
8714 || *eap->arg != NUL
8715#ifdef FEAT_BROWSE
8716 || cmdmod.browse
8717#endif
8718 )
8719 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008720#ifdef FEAT_AUTOCMD
8721 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8722 * can bring us here, others are stopped earlier. */
8723 if (*eap->arg != NUL && curbuf_locked())
8724 return;
8725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 n = readonlymode;
8727 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8728 readonlymode = TRUE;
8729 else if (eap->cmdidx == CMD_enew)
8730 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8731 empty buffer */
8732 setpcmark();
8733 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8734 NULL, eap,
8735 /* ":edit" goes to first line if Vi compatible */
8736 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8737 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8738 ? ECMD_ONE : eap->do_ecmd_lnum,
8739 (P_HID(curbuf) ? ECMD_HIDE : 0)
8740 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008741 /* after a split we can use an existing buffer */
8742 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743#ifdef FEAT_LISTCMDS
8744 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8745#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008746 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 {
8748 /* Editing the file failed. If the window was split, close it. */
8749#ifdef FEAT_WINDOWS
8750 if (old_curwin != NULL)
8751 {
8752 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8753 if (!need_hide || P_HID(curbuf))
8754 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008755# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8756 cleanup_T cs;
8757
8758 /* Reset the error/interrupt/exception state here so that
8759 * aborting() returns FALSE when closing a window. */
8760 enter_cleanup(&cs);
8761# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762# ifdef FEAT_GUI
8763 need_mouse_correct = TRUE;
8764# endif
8765 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008766
8767# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8768 /* Restore the error/interrupt/exception state if not
8769 * discarded by a new aborting error, interrupt, or
8770 * uncaught exception. */
8771 leave_cleanup(&cs);
8772# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 }
8774 }
8775#endif
8776 }
8777 else if (readonlymode && curbuf->b_nwindows == 1)
8778 {
8779 /* When editing an already visited buffer, 'readonly' won't be set
8780 * but the previous value is kept. With ":view" and ":sview" we
8781 * want the file to be readonly, except when another window is
8782 * editing the same buffer. */
8783 curbuf->b_p_ro = TRUE;
8784 }
8785 readonlymode = n;
8786 }
8787 else
8788 {
8789 if (eap->do_ecmd_cmd != NULL)
8790 do_cmdline_cmd(eap->do_ecmd_cmd);
8791#ifdef FEAT_TITLE
8792 n = curwin->w_arg_idx_invalid;
8793#endif
8794 check_arg_idx(curwin);
8795#ifdef FEAT_TITLE
8796 if (n != curwin->w_arg_idx_invalid)
8797 maketitle();
8798#endif
8799 }
8800
8801#ifdef FEAT_WINDOWS
8802 /*
8803 * if ":split file" worked, set alternate file name in old window to new
8804 * file
8805 */
8806 if (old_curwin != NULL
8807 && *eap->arg != NUL
8808 && curwin != old_curwin
8809 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008810 && old_curwin->w_buffer != curbuf
8811 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 old_curwin->w_alt_fnum = curbuf->b_fnum;
8813#endif
8814
8815 ex_no_reprint = TRUE;
8816}
8817
8818#ifndef FEAT_GUI
8819/*
8820 * ":gui" and ":gvim" when there is no GUI.
8821 */
8822 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008823ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824{
8825 eap->errmsg = e_nogvim;
8826}
8827#endif
8828
8829#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8830 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008831ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832{
8833 gui_make_tearoff(eap->arg);
8834}
8835#endif
8836
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008837#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008839ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008841 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842}
8843#endif
8844
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008846ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847{
8848 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8849 MSG(_("No swap file"));
8850 else
8851 msg(curbuf->b_ml.ml_mfp->mf_fname);
8852}
8853
8854/*
8855 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8856 * offset.
8857 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8858 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008860ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861{
8862#ifdef FEAT_SCROLLBIND
8863 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008864 win_T *save_curwin = curwin;
8865 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 long topline;
8867 long y;
8868 linenr_T old_linenr = curwin->w_cursor.lnum;
8869
8870 setpcmark();
8871
8872 /*
8873 * determine max topline
8874 */
8875 if (curwin->w_p_scb)
8876 {
8877 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008878 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 {
8880 if (wp->w_p_scb && wp->w_buffer)
8881 {
8882 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8883 if (topline > y)
8884 topline = y;
8885 }
8886 }
8887 if (topline < 1)
8888 topline = 1;
8889 }
8890 else
8891 {
8892 topline = 1;
8893 }
8894
8895
8896 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008897 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008899 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 {
8901 if (curwin->w_p_scb)
8902 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008903 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 y = topline - curwin->w_topline;
8905 if (y > 0)
8906 scrollup(y, TRUE);
8907 else
8908 scrolldown(-y, TRUE);
8909 curwin->w_scbind_pos = topline;
8910 redraw_later(VALID);
8911 cursor_correct();
8912#ifdef FEAT_WINDOWS
8913 curwin->w_redr_status = TRUE;
8914#endif
8915 }
8916 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008917 curwin = save_curwin;
8918 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 if (curwin->w_p_scb)
8920 {
8921 did_syncbind = TRUE;
8922 checkpcmark();
8923 if (old_linenr != curwin->w_cursor.lnum)
8924 {
8925 char_u ctrl_o[2];
8926
8927 ctrl_o[0] = Ctrl_O;
8928 ctrl_o[1] = 0;
8929 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8930 }
8931 }
8932#endif
8933}
8934
8935
8936 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008937ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008939 int i;
8940 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8941 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942
8943 if (eap->usefilter) /* :r!cmd */
8944 do_bang(1, eap, FALSE, FALSE, TRUE);
8945 else
8946 {
8947 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8948 return;
8949
8950#ifdef FEAT_BROWSE
8951 if (cmdmod.browse)
8952 {
8953 char_u *browseFile;
8954
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008955 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 NULL, NULL, NULL, curbuf);
8957 if (browseFile != NULL)
8958 {
8959 i = readfile(browseFile, NULL,
8960 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8961 vim_free(browseFile);
8962 }
8963 else
8964 i = OK;
8965 }
8966 else
8967#endif
8968 if (*eap->arg == NUL)
8969 {
8970 if (check_fname() == FAIL) /* check for no file name */
8971 return;
8972 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8973 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8974 }
8975 else
8976 {
8977 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8978 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8979 i = readfile(eap->arg, NULL,
8980 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8981
8982 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01008983 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 {
8985#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8986 if (!aborting())
8987#endif
8988 EMSG2(_(e_notopen), eap->arg);
8989 }
8990 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008991 {
8992 if (empty && exmode_active)
8993 {
8994 /* Delete the empty line that remains. Historically ex does
8995 * this but vi doesn't. */
8996 if (eap->line2 == 0)
8997 lnum = curbuf->b_ml.ml_line_count;
8998 else
8999 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009000 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009001 {
9002 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009003 if (curwin->w_cursor.lnum > 1
9004 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009005 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00009006 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009007 }
9008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 }
9012}
9013
Bram Moolenaarea408852005-06-25 22:49:46 +00009014static char_u *prev_dir = NULL;
9015
9016#if defined(EXITFREE) || defined(PROTO)
9017 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009018free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00009019{
9020 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00009021 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00009022
9023 vim_free(globaldir);
9024 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00009025}
9026#endif
9027
Bram Moolenaarf4258302013-06-02 18:20:17 +02009028/*
9029 * Deal with the side effects of changing the current directory.
9030 * When "local" is TRUE then this was after an ":lcd" command.
9031 */
9032 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009033post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02009034{
9035 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01009036 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02009037 if (local)
9038 {
9039 /* If still in global directory, need to remember current
9040 * directory as global directory. */
9041 if (globaldir == NULL && prev_dir != NULL)
9042 globaldir = vim_strsave(prev_dir);
9043 /* Remember this local directory for the window. */
9044 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9045 curwin->w_localdir = vim_strsave(NameBuff);
9046 }
9047 else
9048 {
9049 /* We are now in the global directory, no need to remember its
9050 * name. */
9051 vim_free(globaldir);
9052 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02009053 }
9054
9055 shorten_fnames(TRUE);
9056}
9057
Bram Moolenaarea408852005-06-25 22:49:46 +00009058
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059/*
9060 * ":cd", ":lcd", ":chdir" and ":lchdir".
9061 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00009062 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009063ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 char_u *new_dir;
9066 char_u *tofree;
9067
9068 new_dir = eap->arg;
9069#if !defined(UNIX) && !defined(VMS)
9070 /* for non-UNIX ":cd" means: print current directory */
9071 if (*new_dir == NUL)
9072 ex_pwd(NULL);
9073 else
9074#endif
9075 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009076#ifdef FEAT_AUTOCMD
9077 if (allbuf_locked())
9078 return;
9079#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009080 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9081 && !eap->forceit)
9082 {
Bram Moolenaar81870892007-11-11 18:17:28 +00009083 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009084 return;
9085 }
9086
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 /* ":cd -": Change to previous directory */
9088 if (STRCMP(new_dir, "-") == 0)
9089 {
9090 if (prev_dir == NULL)
9091 {
9092 EMSG(_("E186: No previous directory"));
9093 return;
9094 }
9095 new_dir = prev_dir;
9096 }
9097
9098 /* Save current directory for next ":cd -" */
9099 tofree = prev_dir;
9100 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9101 prev_dir = vim_strsave(NameBuff);
9102 else
9103 prev_dir = NULL;
9104
9105#if defined(UNIX) || defined(VMS)
9106 /* for UNIX ":cd" means: go to home directory */
9107 if (*new_dir == NUL)
9108 {
9109 /* use NameBuff for home directory name */
9110# ifdef VMS
9111 char_u *p;
9112
9113 p = mch_getenv((char_u *)"SYS$LOGIN");
9114 if (p == NULL || *p == NUL) /* empty is the same as not set */
9115 NameBuff[0] = NUL;
9116 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009117 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118# else
9119 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9120# endif
9121 new_dir = NameBuff;
9122 }
9123#endif
9124 if (new_dir == NULL || vim_chdir(new_dir))
9125 EMSG(_(e_failed));
9126 else
9127 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02009128 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129
9130 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009131 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 ex_pwd(eap);
9133 }
9134 vim_free(tofree);
9135 }
9136}
9137
9138/*
9139 * ":pwd".
9140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009142ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143{
9144 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9145 {
9146#ifdef BACKSLASH_IN_FILENAME
9147 slash_adjust(NameBuff);
9148#endif
9149 msg(NameBuff);
9150 }
9151 else
9152 EMSG(_("E187: Unknown"));
9153}
9154
9155/*
9156 * ":=".
9157 */
9158 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009159ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160{
9161 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009162 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163}
9164
9165 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009166ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009168 int n;
9169 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170
9171 if (cursor_valid())
9172 {
9173 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9174 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01009175 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009177
9178 len = eap->line2;
9179 switch (*eap->arg)
9180 {
9181 case 'm': break;
9182 case NUL: len *= 1000L; break;
9183 default: EMSG2(_(e_invarg2), eap->arg); return;
9184 }
9185 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186}
9187
9188/*
9189 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9190 */
9191 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009192do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193{
9194 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009195 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196
9197 cursor_on();
9198 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009199 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009201 wait_now = msec - done > 1000L ? 1000L : msec - done;
9202#ifdef FEAT_TIMERS
9203 {
9204 long due_time = check_due_timer();
9205
9206 if (due_time > 0 && due_time < wait_now)
9207 wait_now = due_time;
9208 }
9209#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009210#ifdef FEAT_JOB_CHANNEL
9211 if (has_any_channel() && wait_now > 100L)
9212 wait_now = 100L;
9213#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009214 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009215#ifdef FEAT_JOB_CHANNEL
9216 if (has_any_channel())
9217 ui_breakcheck_force(TRUE);
9218 else
9219#endif
9220 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009221#ifdef MESSAGE_QUEUE
9222 /* Process the netbeans and clientserver messages that may have been
9223 * received in the call to ui_breakcheck() when the GUI is in use. This
9224 * may occur when running a test case. */
9225 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 }
9228}
9229
9230 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009231do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232{
9233 int mode;
9234 char_u *cmdp;
9235
9236 cmdp = eap->cmd;
9237 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9238
9239 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9240 eap->arg, mode, isabbrev))
9241 {
9242 case 1: EMSG(_(e_invarg));
9243 break;
9244 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9245 break;
9246 }
9247}
9248
9249/*
9250 * ":winsize" command (obsolete).
9251 */
9252 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009253ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254{
9255 int w, h;
9256 char_u *arg = eap->arg;
9257 char_u *p;
9258
9259 w = getdigits(&arg);
9260 arg = skipwhite(arg);
9261 p = arg;
9262 h = getdigits(&arg);
9263 if (*p != NUL && *arg == NUL)
9264 set_shellsize(w, h, TRUE);
9265 else
9266 EMSG(_("E465: :winsize requires two number arguments"));
9267}
9268
9269#ifdef FEAT_WINDOWS
9270 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009271ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272{
9273 int xchar = NUL;
9274 char_u *p;
9275
9276 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9277 {
9278 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9279 if (eap->arg[1] == NUL)
9280 {
9281 EMSG(_(e_invarg));
9282 return;
9283 }
9284 xchar = eap->arg[1];
9285 p = eap->arg + 2;
9286 }
9287 else
9288 p = eap->arg + 1;
9289
9290 eap->nextcmd = check_nextcmd(p);
9291 p = skipwhite(p);
9292 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9293 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009294 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295 {
9296 /* Pass flags on for ":vertical wincmd ]". */
9297 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009298 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9300 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009301 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 }
9303}
9304#endif
9305
Bram Moolenaar843ee412004-06-30 16:16:41 +00009306#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307/*
9308 * ":winpos".
9309 */
9310 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009311ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312{
9313 int x, y;
9314 char_u *arg = eap->arg;
9315 char_u *p;
9316
9317 if (*arg == NUL)
9318 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009319# if defined(FEAT_GUI) || defined(MSWIN)
9320# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009322# else
9323 if (mch_get_winpos(&x, &y) != FAIL)
9324# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 {
9326 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9327 msg(IObuff);
9328 }
9329 else
9330# endif
9331 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9332 }
9333 else
9334 {
9335 x = getdigits(&arg);
9336 arg = skipwhite(arg);
9337 p = arg;
9338 y = getdigits(&arg);
9339 if (*p == NUL || *arg != NUL)
9340 {
9341 EMSG(_("E466: :winpos requires two number arguments"));
9342 return;
9343 }
9344# ifdef FEAT_GUI
9345 if (gui.in_use)
9346 gui_mch_set_winpos(x, y);
9347 else if (gui.starting)
9348 {
9349 /* Remember the coordinates for when the window is opened. */
9350 gui_win_x = x;
9351 gui_win_y = y;
9352 }
9353# ifdef HAVE_TGETENT
9354 else
9355# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009356# else
9357# ifdef MSWIN
9358 mch_set_winpos(x, y);
9359# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360# endif
9361# ifdef HAVE_TGETENT
9362 if (*T_CWP)
9363 term_set_winpos(x, y);
9364# endif
9365 }
9366}
9367#endif
9368
9369/*
9370 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9371 */
9372 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009373ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374{
9375 oparg_T oa;
9376
9377 clear_oparg(&oa);
9378 oa.regname = eap->regname;
9379 oa.start.lnum = eap->line1;
9380 oa.end.lnum = eap->line2;
9381 oa.line_count = eap->line2 - eap->line1 + 1;
9382 oa.motion_type = MLINE;
9383#ifdef FEAT_VIRTUALEDIT
9384 virtual_op = FALSE;
9385#endif
9386 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9387 {
9388 setpcmark();
9389 curwin->w_cursor.lnum = eap->line1;
9390 beginline(BL_SOL | BL_FIX);
9391 }
9392
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009393 if (VIsual_active)
9394 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009395
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 switch (eap->cmdidx)
9397 {
9398 case CMD_delete:
9399 oa.op_type = OP_DELETE;
9400 op_delete(&oa);
9401 break;
9402
9403 case CMD_yank:
9404 oa.op_type = OP_YANK;
9405 (void)op_yank(&oa, FALSE, TRUE);
9406 break;
9407
9408 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009409 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009411 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9412#else
9413 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009415 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 oa.op_type = OP_RSHIFT;
9417 else
9418 oa.op_type = OP_LSHIFT;
9419 op_shift(&oa, FALSE, eap->amount);
9420 break;
9421 }
9422#ifdef FEAT_VIRTUALEDIT
9423 virtual_op = MAYBE;
9424#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009425 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426}
9427
9428/*
9429 * ":put".
9430 */
9431 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009432ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433{
9434 /* ":0put" works like ":1put!". */
9435 if (eap->line2 == 0)
9436 {
9437 eap->line2 = 1;
9438 eap->forceit = TRUE;
9439 }
9440 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009441 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9442 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443}
9444
9445/*
9446 * Handle ":copy" and ":move".
9447 */
9448 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009449ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450{
9451 long n;
9452
Bram Moolenaarded27822017-01-02 14:27:34 +01009453 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 if (eap->arg == NULL) /* error detected */
9455 {
9456 eap->nextcmd = NULL;
9457 return;
9458 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009459 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460
9461 /*
9462 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9463 */
9464 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9465 {
9466 EMSG(_(e_invaddr));
9467 return;
9468 }
9469
9470 if (eap->cmdidx == CMD_move)
9471 {
9472 if (do_move(eap->line1, eap->line2, n) == FAIL)
9473 return;
9474 }
9475 else
9476 ex_copy(eap->line1, eap->line2, n);
9477 u_clearline();
9478 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009479 ex_may_print(eap);
9480}
9481
9482/*
9483 * Print the current line if flags were given to the Ex command.
9484 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009485 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009486ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009487{
9488 if (eap->flags != 0)
9489 {
9490 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9491 (eap->flags & EXFLAG_LIST));
9492 ex_no_reprint = TRUE;
9493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494}
9495
9496/*
9497 * ":smagic" and ":snomagic".
9498 */
9499 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009500ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501{
9502 int magic_save = p_magic;
9503
9504 p_magic = (eap->cmdidx == CMD_smagic);
9505 do_sub(eap);
9506 p_magic = magic_save;
9507}
9508
9509/*
9510 * ":join".
9511 */
9512 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009513ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514{
9515 curwin->w_cursor.lnum = eap->line1;
9516 if (eap->line1 == eap->line2)
9517 {
9518 if (eap->addr_count >= 2) /* :2,2join does nothing */
9519 return;
9520 if (eap->line2 == curbuf->b_ml.ml_line_count)
9521 {
9522 beep_flush();
9523 return;
9524 }
9525 ++eap->line2;
9526 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009527 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009529 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530}
9531
9532/*
9533 * ":[addr]@r" or ":[addr]*r": execute register
9534 */
9535 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009536ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537{
9538 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009539 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540
9541 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009542 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543
9544#ifdef USE_ON_FLY_SCROLL
9545 dont_scroll = TRUE; /* disallow scrolling here */
9546#endif
9547
9548 /* get the register name. No name means to use the previous one */
9549 c = *eap->arg;
9550 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9551 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009552 /* Put the register in the typeahead buffer with the "silent" flag. */
9553 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9554 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009555 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 else
9559 {
9560 int save_efr = exec_from_reg;
9561
9562 exec_from_reg = TRUE;
9563
9564 /*
9565 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009566 * Continue until the stuff buffer is empty and all added characters
9567 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009569 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9571
9572 exec_from_reg = save_efr;
9573 }
9574}
9575
9576/*
9577 * ":!".
9578 */
9579 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009580ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581{
9582 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9583}
9584
9585/*
9586 * ":undo".
9587 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009589ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009591 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009592 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009593 else
9594 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595}
9596
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009597#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009598 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009599ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009600{
9601 char_u hash[UNDO_HASH_SIZE];
9602
9603 u_compute_hash(hash);
9604 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9605}
9606
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009607 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009608ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009609{
9610 char_u hash[UNDO_HASH_SIZE];
9611
9612 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009613 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009614}
9615#endif
9616
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617/*
9618 * ":redo".
9619 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009621ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622{
9623 u_redo(1);
9624}
9625
9626/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009627 * ":earlier" and ":later".
9628 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009629 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009630ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009631{
9632 long count = 0;
9633 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009634 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009635 char_u *p = eap->arg;
9636
9637 if (*p == NUL)
9638 count = 1;
9639 else if (isdigit(*p))
9640 {
9641 count = getdigits(&p);
9642 switch (*p)
9643 {
9644 case 's': ++p; sec = TRUE; break;
9645 case 'm': ++p; sec = TRUE; count *= 60; break;
9646 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009647 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9648 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009649 }
9650 }
9651
9652 if (*p != NUL)
9653 EMSG2(_(e_invarg2), eap->arg);
9654 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009655 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9656 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009657}
9658
9659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660 * ":redir": start/stop redirection.
9661 */
9662 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009663ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664{
9665 char *mode;
9666 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009667 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668
Bram Moolenaarba768492016-07-08 15:32:54 +02009669#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009670 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009671 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009672 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009673 return;
9674 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009675#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009676
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 if (STRICMP(eap->arg, "END") == 0)
9678 close_redir();
9679 else
9680 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009681 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009683 ++arg;
9684 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009686 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 mode = "a";
9688 }
9689 else
9690 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009691 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692
9693 close_redir();
9694
9695 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009696 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 if (fname == NULL)
9698 return;
9699#ifdef FEAT_BROWSE
9700 if (cmdmod.browse)
9701 {
9702 char_u *browseFile;
9703
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009704 browseFile = do_browse(BROWSE_SAVE,
9705 (char_u *)_("Save Redirection"),
9706 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707 if (browseFile == NULL)
9708 return; /* operation cancelled */
9709 vim_free(fname);
9710 fname = browseFile;
9711 eap->forceit = TRUE; /* since dialog already asked */
9712 }
9713#endif
9714
9715 redir_fd = open_exfile(fname, eap->forceit, mode);
9716 vim_free(fname);
9717 }
9718#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009719 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 {
9721 /* redirect to a register a-z (resp. A-Z for appending) */
9722 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009723 ++arg;
9724 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009726 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009727 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009729 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009731 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009732 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009733 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009734 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009736 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009737 if (*arg == '>')
9738 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009739 /* Make register empty when not using @A-@Z and the
9740 * command is valid. */
9741 if (*arg == NUL && !isupper(redir_reg))
9742 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009744 }
9745 if (*arg != NUL)
9746 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009747 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009748 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009749 }
9750 }
9751 else if (*arg == '=' && arg[1] == '>')
9752 {
9753 int append;
9754
9755 /* redirect to a variable */
9756 close_redir();
9757 arg += 2;
9758
9759 if (*arg == '>')
9760 {
9761 ++arg;
9762 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 }
9764 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009765 append = FALSE;
9766
9767 if (var_redir_start(skipwhite(arg), append) == OK)
9768 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 }
9770#endif
9771
9772 /* TODO: redirect to a buffer */
9773
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009775 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009777
9778 /* Make sure redirection is not off. Can happen for cmdline completion
9779 * that indirectly invokes a command to catch its output. */
9780 if (redir_fd != NULL
9781#ifdef FEAT_EVAL
9782 || redir_reg || redir_vname
9783#endif
9784 )
9785 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786}
9787
9788/*
9789 * ":redraw": force redraw
9790 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009791 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009792ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793{
9794 int r = RedrawingDisabled;
9795 int p = p_lz;
9796
9797 RedrawingDisabled = 0;
9798 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009799 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009801 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802#ifdef FEAT_TITLE
9803 if (need_maketitle)
9804 maketitle();
9805#endif
9806 RedrawingDisabled = r;
9807 p_lz = p;
9808
9809 /* Reset msg_didout, so that a message that's there is overwritten. */
9810 msg_didout = FALSE;
9811 msg_col = 0;
9812
Bram Moolenaar56667a52013-07-17 11:54:28 +02009813 /* No need to wait after an intentional redraw. */
9814 need_wait_return = FALSE;
9815
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 out_flush();
9817}
9818
9819/*
9820 * ":redrawstatus": force redraw of status line(s)
9821 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009823ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824{
9825#if defined(FEAT_WINDOWS)
9826 int r = RedrawingDisabled;
9827 int p = p_lz;
9828
9829 RedrawingDisabled = 0;
9830 p_lz = FALSE;
9831 if (eap->forceit)
9832 status_redraw_all();
9833 else
9834 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009835 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 RedrawingDisabled = r;
9837 p_lz = p;
9838 out_flush();
9839#endif
9840}
9841
9842 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009843close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844{
9845 if (redir_fd != NULL)
9846 {
9847 fclose(redir_fd);
9848 redir_fd = NULL;
9849 }
9850#ifdef FEAT_EVAL
9851 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009852 if (redir_vname)
9853 {
9854 var_redir_stop();
9855 redir_vname = 0;
9856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857#endif
9858}
9859
9860#if defined(FEAT_SESSION) && defined(USE_CRNL)
9861# define MKSESSION_NL
9862static int mksession_nl = FALSE; /* use NL only in put_eol() */
9863#endif
9864
9865/*
9866 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9867 */
9868 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009869ex_mkrc(
9870 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871{
9872 FILE *fd;
9873 int failed = FALSE;
9874 char_u *fname;
9875#ifdef FEAT_BROWSE
9876 char_u *browseFile = NULL;
9877#endif
9878#ifdef FEAT_SESSION
9879 int view_session = FALSE;
9880 int using_vdir = FALSE; /* using 'viewdir'? */
9881 char_u *viewFile = NULL;
9882 unsigned *flagp;
9883#endif
9884
9885 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9886 {
9887#ifdef FEAT_SESSION
9888 view_session = TRUE;
9889#else
9890 ex_ni(eap);
9891 return;
9892#endif
9893 }
9894
9895#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009896 /* Use the short file name until ":lcd" is used. We also don't use the
9897 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009898 did_lcd = FALSE;
9899
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9901 if (eap->cmdidx == CMD_mkview
9902 && (*eap->arg == NUL
9903 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9904 {
9905 eap->forceit = TRUE;
9906 fname = get_view_file(*eap->arg);
9907 if (fname == NULL)
9908 return;
9909 viewFile = fname;
9910 using_vdir = TRUE;
9911 }
9912 else
9913#endif
9914 if (*eap->arg != NUL)
9915 fname = eap->arg;
9916 else if (eap->cmdidx == CMD_mkvimrc)
9917 fname = (char_u *)VIMRC_FILE;
9918#ifdef FEAT_SESSION
9919 else if (eap->cmdidx == CMD_mksession)
9920 fname = (char_u *)SESSION_FILE;
9921#endif
9922 else
9923 fname = (char_u *)EXRC_FILE;
9924
9925#ifdef FEAT_BROWSE
9926 if (cmdmod.browse)
9927 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009928 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929# ifdef FEAT_SESSION
9930 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9931 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9932# endif
9933 (char_u *)_("Save Setup"),
9934 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9935 if (browseFile == NULL)
9936 goto theend;
9937 fname = browseFile;
9938 eap->forceit = TRUE; /* since dialog already asked */
9939 }
9940#endif
9941
9942#if defined(FEAT_SESSION) && defined(vim_mkdir)
9943 /* When using 'viewdir' may have to create the directory. */
9944 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009945 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946#endif
9947
9948 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9949 if (fd != NULL)
9950 {
9951#ifdef FEAT_SESSION
9952 if (eap->cmdidx == CMD_mkview)
9953 flagp = &vop_flags;
9954 else
9955 flagp = &ssop_flags;
9956#endif
9957
9958#ifdef MKSESSION_NL
9959 /* "unix" in 'sessionoptions': use NL line separator */
9960 if (view_session && (*flagp & SSOP_UNIX))
9961 mksession_nl = TRUE;
9962#endif
9963
9964 /* Write the version command for :mkvimrc */
9965 if (eap->cmdidx == CMD_mkvimrc)
9966 (void)put_line(fd, "version 6.0");
9967
9968#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009969 if (eap->cmdidx == CMD_mksession)
9970 {
9971 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9972 failed = TRUE;
9973 }
9974
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 if (eap->cmdidx != CMD_mkview)
9976#endif
9977 {
9978 /* Write setting 'compatible' first, because it has side effects.
9979 * For that same reason only do it when needed. */
9980 if (p_cp)
9981 (void)put_line(fd, "if !&cp | set cp | endif");
9982 else
9983 (void)put_line(fd, "if &cp | set nocp | endif");
9984 }
9985
9986#ifdef FEAT_SESSION
9987 if (!view_session
9988 || (eap->cmdidx == CMD_mksession
9989 && (*flagp & SSOP_OPTIONS)))
9990#endif
9991 failed |= (makemap(fd, NULL) == FAIL
9992 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9993
9994#ifdef FEAT_SESSION
9995 if (!failed && view_session)
9996 {
9997 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9998 failed = TRUE;
9999 if (eap->cmdidx == CMD_mksession)
10000 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010001 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002
Bram Moolenaard9462e32011-04-11 21:35:11 +020010003 dirnow = alloc(MAXPATHL);
10004 if (dirnow == NULL)
10005 failed = TRUE;
10006 else
10007 {
10008 /*
10009 * Change to session file's dir.
10010 */
10011 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010013 *dirnow = NUL;
10014 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
10015 {
10016 if (vim_chdirfile(fname) == OK)
10017 shorten_fnames(TRUE);
10018 }
10019 else if (*dirnow != NUL
10020 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
10021 {
10022 if (mch_chdir((char *)globaldir) == 0)
10023 shorten_fnames(TRUE);
10024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025
Bram Moolenaard9462e32011-04-11 21:35:11 +020010026 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027
Bram Moolenaard9462e32011-04-11 21:35:11 +020010028 /* restore original dir */
10029 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +020010031 {
10032 if (mch_chdir((char *)dirnow) != 0)
10033 EMSG(_(e_prev_dir));
10034 shorten_fnames(TRUE);
10035 }
10036 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037 }
10038 }
10039 else
10040 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010041 failed |= (put_view(fd, curwin, !using_vdir, flagp,
10042 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 }
10044 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
10045 == FAIL)
10046 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010047 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
10048 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010049 if (eap->cmdidx == CMD_mksession)
10050 {
10051 if (put_line(fd, "unlet SessionLoad") == FAIL)
10052 failed = TRUE;
10053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 }
10055#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010056 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
10057 failed = TRUE;
10058
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 failed |= fclose(fd);
10060
10061 if (failed)
10062 EMSG(_(e_write));
10063#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
10064 else if (eap->cmdidx == CMD_mksession)
10065 {
10066 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010067 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068
Bram Moolenaard9462e32011-04-11 21:35:11 +020010069 tbuf = alloc(MAXPATHL);
10070 if (tbuf != NULL)
10071 {
10072 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10073 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10074 vim_free(tbuf);
10075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 }
10077#endif
10078#ifdef MKSESSION_NL
10079 mksession_nl = FALSE;
10080#endif
10081 }
10082
10083#ifdef FEAT_BROWSE
10084theend:
10085 vim_free(browseFile);
10086#endif
10087#ifdef FEAT_SESSION
10088 vim_free(viewFile);
10089#endif
10090}
10091
Bram Moolenaardf177f62005-02-22 08:39:57 +000010092#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10093 || defined(PROTO)
10094 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010095vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010096{
10097 if (vim_mkdir(name, prot) != 0)
10098 {
10099 EMSG2(_("E739: Cannot create directory: %s"), name);
10100 return FAIL;
10101 }
10102 return OK;
10103}
10104#endif
10105
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106/*
10107 * Open a file for writing for an Ex command, with some checks.
10108 * Return file descriptor, or NULL on failure.
10109 */
10110 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010111open_exfile(
10112 char_u *fname,
10113 int forceit,
10114 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115{
10116 FILE *fd;
10117
10118#ifdef UNIX
10119 /* with Unix it is possible to open a directory */
10120 if (mch_isdir(fname))
10121 {
10122 EMSG2(_(e_isadir2), fname);
10123 return NULL;
10124 }
10125#endif
10126 if (!forceit && *mode != 'a' && vim_fexists(fname))
10127 {
10128 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
10129 return NULL;
10130 }
10131
10132 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
10133 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
10134
10135 return fd;
10136}
10137
10138/*
10139 * ":mark" and ":k".
10140 */
10141 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010142ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143{
10144 pos_T pos;
10145
10146 if (*eap->arg == NUL) /* No argument? */
10147 EMSG(_(e_argreq));
10148 else if (eap->arg[1] != NUL) /* more than one character? */
10149 EMSG(_(e_trailing));
10150 else
10151 {
10152 pos = curwin->w_cursor; /* save curwin->w_cursor */
10153 curwin->w_cursor.lnum = eap->line2;
10154 beginline(BL_WHITE | BL_FIX);
10155 if (setmark(*eap->arg) == FAIL) /* set mark */
10156 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10157 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10158 }
10159}
10160
10161/*
10162 * Update w_topline, w_leftcol and the cursor position.
10163 */
10164 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010165update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166{
10167 check_cursor(); /* put cursor on valid line */
10168 update_topline();
10169 if (!curwin->w_p_wrap)
10170 validate_cursor();
10171 update_curswant();
10172}
10173
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174/*
10175 * ":normal[!] {commands}": Execute normal mode commands.
10176 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010177 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010178ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180 int save_msg_scroll = msg_scroll;
10181 int save_restart_edit = restart_edit;
10182 int save_msg_didout = msg_didout;
10183 int save_State = State;
10184 tasave_T tabuf;
10185 int save_insertmode = p_im;
10186 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010187 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188#ifdef FEAT_MBYTE
10189 char_u *arg = NULL;
10190 int l;
10191 char_u *p;
10192#endif
10193
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010194 if (ex_normal_lock > 0)
10195 {
10196 EMSG(_(e_secure));
10197 return;
10198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199 if (ex_normal_busy >= p_mmd)
10200 {
10201 EMSG(_("E192: Recursive use of :normal too deep"));
10202 return;
10203 }
10204 ++ex_normal_busy;
10205
10206 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10207 restart_edit = 0; /* don't go to Insert mode */
10208 p_im = FALSE; /* don't use 'insertmode' */
10209
10210#ifdef FEAT_MBYTE
10211 /*
10212 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10213 * this for the K_SPECIAL leading byte, otherwise special keys will not
10214 * work.
10215 */
10216 if (has_mbyte)
10217 {
10218 int len = 0;
10219
10220 /* Count the number of characters to be escaped. */
10221 for (p = eap->arg; *p != NUL; ++p)
10222 {
10223# ifdef FEAT_GUI
10224 if (*p == CSI) /* leadbyte CSI */
10225 len += 2;
10226# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010227 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10229# ifdef FEAT_GUI
10230 || *p == CSI
10231# endif
10232 )
10233 len += 2;
10234 }
10235 if (len > 0)
10236 {
10237 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10238 if (arg != NULL)
10239 {
10240 len = 0;
10241 for (p = eap->arg; *p != NUL; ++p)
10242 {
10243 arg[len++] = *p;
10244# ifdef FEAT_GUI
10245 if (*p == CSI)
10246 {
10247 arg[len++] = KS_EXTRA;
10248 arg[len++] = (int)KE_CSI;
10249 }
10250# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010251 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 {
10253 arg[len++] = *++p;
10254 if (*p == K_SPECIAL)
10255 {
10256 arg[len++] = KS_SPECIAL;
10257 arg[len++] = KE_FILLER;
10258 }
10259# ifdef FEAT_GUI
10260 else if (*p == CSI)
10261 {
10262 arg[len++] = KS_EXTRA;
10263 arg[len++] = (int)KE_CSI;
10264 }
10265# endif
10266 }
10267 arg[len] = NUL;
10268 }
10269 }
10270 }
10271 }
10272#endif
10273
10274 /*
10275 * Save the current typeahead. This is required to allow using ":normal"
10276 * from an event handler and makes sure we don't hang when the argument
10277 * ends with half a command.
10278 */
10279 save_typeahead(&tabuf);
10280 if (tabuf.typebuf_valid)
10281 {
10282 /*
10283 * Repeat the :normal command for each line in the range. When no
10284 * range given, execute it just once, without positioning the cursor
10285 * first.
10286 */
10287 do
10288 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 if (eap->addr_count != 0)
10290 {
10291 curwin->w_cursor.lnum = eap->line1++;
10292 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010293 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 }
10295
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010296 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297#ifdef FEAT_MBYTE
10298 arg != NULL ? arg :
10299#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010300 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301 }
10302 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10303 }
10304
10305 /* Might not return to the main loop when in an event handler. */
10306 update_topline_cursor();
10307
10308 /* Restore the previous typeahead. */
10309 restore_typeahead(&tabuf);
10310
10311 --ex_normal_busy;
10312 msg_scroll = save_msg_scroll;
10313 restart_edit = save_restart_edit;
10314 p_im = save_insertmode;
10315 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010316 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
10318
10319 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +010010320 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +010010322#ifdef FEAT_MOUSE
10323 setmouse();
10324#endif
10325#ifdef CURSOR_SHAPE
10326 ui_cursor_shape(); /* may show different cursor shape */
10327#endif
10328
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329#ifdef FEAT_MBYTE
10330 vim_free(arg);
10331#endif
10332}
10333
10334/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010335 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 */
10337 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010338ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010340 if (eap->forceit)
10341 {
10342 coladvance((colnr_T)MAXCOL);
10343 curwin->w_curswant = MAXCOL;
10344 curwin->w_set_curswant = FALSE;
10345 }
10346
Bram Moolenaara40c5002005-01-09 21:16:21 +000010347 /* Ignore the command when already in Insert mode. Inserting an
10348 * expression register that invokes a function can do this. */
10349 if (State & INSERT)
10350 return;
10351
Bram Moolenaar64969662005-12-14 21:59:55 +000010352 if (eap->cmdidx == CMD_startinsert)
10353 restart_edit = 'a';
10354 else if (eap->cmdidx == CMD_startreplace)
10355 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010357 restart_edit = 'V';
10358
10359 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010361 if (eap->cmdidx == CMD_startinsert)
10362 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 curwin->w_curswant = 0; /* avoid MAXCOL */
10364 }
10365}
10366
10367/*
10368 * ":stopinsert"
10369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010371ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372{
10373 restart_edit = 0;
10374 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010375 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010378/*
10379 * Execute normal mode command "cmd".
10380 * "remap" can be REMAP_NONE or REMAP_YES.
10381 */
10382 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010383exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010384{
Bram Moolenaar25281632016-01-21 23:32:32 +010010385 /* Stuff the argument into the typeahead buffer. */
10386 ins_typebuf(cmd, remap, 0, TRUE, silent);
10387 exec_normal(FALSE);
10388}
Bram Moolenaar25281632016-01-21 23:32:32 +010010389
Bram Moolenaar25281632016-01-21 23:32:32 +010010390/*
10391 * Execute normal_cmd() until there is no typeahead left.
10392 */
10393 void
10394exec_normal(int was_typed)
10395{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010396 oparg_T oa;
10397
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010398 clear_oparg(&oa);
10399 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010400 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10401 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010402 {
10403 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010404 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010405 }
10406}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010407
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408#ifdef FEAT_FIND_ID
10409 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010410ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411{
10412 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10413 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10414 (linenr_T)1, (linenr_T)MAXLNUM);
10415}
10416
10417#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
10418/*
10419 * ":psearch"
10420 */
10421 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010422ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010423{
10424 g_do_tagpreview = p_pvh;
10425 ex_findpat(eap);
10426 g_do_tagpreview = 0;
10427}
10428#endif
10429
10430 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010431ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432{
10433 int whole = TRUE;
10434 long n;
10435 char_u *p;
10436 int action;
10437
10438 switch (cmdnames[eap->cmdidx].cmd_name[2])
10439 {
10440 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10441 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10442 action = ACTION_GOTO;
10443 else
10444 action = ACTION_SHOW;
10445 break;
10446 case 'i': /* ":ilist" and ":dlist" */
10447 action = ACTION_SHOW_ALL;
10448 break;
10449 case 'u': /* ":ijump" and ":djump" */
10450 action = ACTION_GOTO;
10451 break;
10452 default: /* ":isplit" and ":dsplit" */
10453 action = ACTION_SPLIT;
10454 break;
10455 }
10456
10457 n = 1;
10458 if (vim_isdigit(*eap->arg)) /* get count */
10459 {
10460 n = getdigits(&eap->arg);
10461 eap->arg = skipwhite(eap->arg);
10462 }
10463 if (*eap->arg == '/') /* Match regexp, not just whole words */
10464 {
10465 whole = FALSE;
10466 ++eap->arg;
10467 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10468 if (*p)
10469 {
10470 *p++ = NUL;
10471 p = skipwhite(p);
10472
10473 /* Check for trailing illegal characters */
10474 if (!ends_excmd(*p))
10475 eap->errmsg = e_trailing;
10476 else
10477 eap->nextcmd = check_nextcmd(p);
10478 }
10479 }
10480 if (!eap->skip)
10481 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10482 whole, !eap->forceit,
10483 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10484 n, action, eap->line1, eap->line2);
10485}
10486#endif
10487
10488#ifdef FEAT_WINDOWS
10489
10490# ifdef FEAT_QUICKFIX
10491/*
10492 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10493 */
10494 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010495ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010497 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10499}
10500
10501/*
10502 * ":pedit"
10503 */
10504 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010505ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506{
10507 win_T *curwin_save = curwin;
10508
10509 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010510 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511 keep_help_flag = curwin_save->w_buffer->b_help;
10512 do_exedit(eap, NULL);
10513 keep_help_flag = FALSE;
10514 if (curwin != curwin_save && win_valid(curwin_save))
10515 {
10516 /* Return cursor to where we were */
10517 validate_cursor();
10518 redraw_later(VALID);
10519 win_enter(curwin_save, TRUE);
10520 }
10521 g_do_tagpreview = 0;
10522}
10523# endif
10524
10525/*
10526 * ":stag", ":stselect" and ":stjump".
10527 */
10528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010529ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530{
10531 postponed_split = -1;
10532 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010533 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10535 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010536 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537}
10538#endif
10539
10540/*
10541 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10542 */
10543 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010544ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545{
10546 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10547}
10548
10549 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010550ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551{
10552 int cmd;
10553
10554 switch (name[1])
10555 {
10556 case 'j': cmd = DT_JUMP; /* ":tjump" */
10557 break;
10558 case 's': cmd = DT_SELECT; /* ":tselect" */
10559 break;
10560 case 'p': cmd = DT_PREV; /* ":tprevious" */
10561 break;
10562 case 'N': cmd = DT_PREV; /* ":tNext" */
10563 break;
10564 case 'n': cmd = DT_NEXT; /* ":tnext" */
10565 break;
10566 case 'o': cmd = DT_POP; /* ":pop" */
10567 break;
10568 case 'f': /* ":tfirst" */
10569 case 'r': cmd = DT_FIRST; /* ":trewind" */
10570 break;
10571 case 'l': cmd = DT_LAST; /* ":tlast" */
10572 break;
10573 default: /* ":tag" */
10574#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010575 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010577 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578 return;
10579 }
10580#endif
10581 cmd = DT_TAG;
10582 break;
10583 }
10584
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010585 if (name[0] == 'l')
10586 {
10587#ifndef FEAT_QUICKFIX
10588 ex_ni(eap);
10589 return;
10590#else
10591 cmd = DT_LTAG;
10592#endif
10593 }
10594
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10596 eap->forceit, TRUE);
10597}
10598
10599/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010600 * Check "str" for starting with a special cmdline variable.
10601 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10602 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10603 */
10604 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010605find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010606{
10607 int len;
10608 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010609 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010610 "%",
10611#define SPEC_PERC 0
10612 "#",
10613#define SPEC_HASH 1
10614 "<cword>", /* cursor word */
10615#define SPEC_CWORD 2
10616 "<cWORD>", /* cursor WORD */
10617#define SPEC_CCWORD 3
10618 "<cfile>", /* cursor path name */
10619#define SPEC_CFILE 4
10620 "<sfile>", /* ":so" file name */
10621#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010622 "<slnum>", /* ":so" file line number */
10623#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010624#ifdef FEAT_AUTOCMD
10625 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010626# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010627 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010628# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010629 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010630# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010631#endif
10632#ifdef FEAT_CLIENTSERVER
10633 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010634# ifdef FEAT_AUTOCMD
10635# define SPEC_CLIENT 10
10636# else
10637# define SPEC_CLIENT 7
10638# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010639#endif
10640 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010641
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010642 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010643 {
10644 len = (int)STRLEN(spec_str[i]);
10645 if (STRNCMP(src, spec_str[i], len) == 0)
10646 {
10647 *usedlen = len;
10648 return i;
10649 }
10650 }
10651 return -1;
10652}
10653
10654/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655 * Evaluate cmdline variables.
10656 *
10657 * change '%' to curbuf->b_ffname
10658 * '#' to curwin->w_altfile
10659 * '<cword>' to word under the cursor
10660 * '<cWORD>' to WORD under the cursor
10661 * '<cfile>' to path name under the cursor
10662 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010663 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664 * '<afile>' to file name for autocommand
10665 * '<abuf>' to buffer number for autocommand
10666 * '<amatch>' to matching name for autocommand
10667 *
10668 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10669 * "" for error without a message) and NULL is returned.
10670 * Returns an allocated string if a valid match was found.
10671 * Returns NULL if no match was found. "usedlen" then still contains the
10672 * number of characters to skip.
10673 */
10674 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010675eval_vars(
10676 char_u *src, /* pointer into commandline */
10677 char_u *srcstart, /* beginning of valid memory for src */
10678 int *usedlen, /* characters after src that are used */
10679 linenr_T *lnump, /* line number for :e command, or NULL */
10680 char_u **errormsg, /* pointer to error message */
10681 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010682 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683{
10684 int i;
10685 char_u *s;
10686 char_u *result;
10687 char_u *resultbuf = NULL;
10688 int resultlen;
10689 buf_T *buf;
10690 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10691 int spec_idx;
10692#ifdef FEAT_MODIFY_FNAME
10693 int skip_mod = FALSE;
10694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696
10697 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010698 if (escaped != NULL)
10699 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700
10701 /*
10702 * Check if there is something to do.
10703 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010704 spec_idx = find_cmdline_var(src, usedlen);
10705 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706 {
10707 *usedlen = 1;
10708 return NULL;
10709 }
10710
10711 /*
10712 * Skip when preceded with a backslash "\%" and "\#".
10713 * Note: In "\\%" the % is also not recognized!
10714 */
10715 if (src > srcstart && src[-1] == '\\')
10716 {
10717 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010718 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719 return NULL;
10720 }
10721
10722 /*
10723 * word or WORD under cursor
10724 */
10725 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10726 {
10727 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10728 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10729 if (resultlen == 0)
10730 {
10731 *errormsg = (char_u *)"";
10732 return NULL;
10733 }
10734 }
10735
10736 /*
10737 * '#': Alternate file name
10738 * '%': Current file name
10739 * File name under the cursor
10740 * File name for autocommand
10741 * and following modifiers
10742 */
10743 else
10744 {
10745 switch (spec_idx)
10746 {
10747 case SPEC_PERC: /* '%': current file */
10748 if (curbuf->b_fname == NULL)
10749 {
10750 result = (char_u *)"";
10751 valid = 0; /* Must have ":p:h" to be valid */
10752 }
10753 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755 break;
10756
10757 case SPEC_HASH: /* '#' or "#99": alternate file */
10758 if (src[1] == '#') /* "##": the argument list */
10759 {
10760 result = arg_all();
10761 resultbuf = result;
10762 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010763 if (escaped != NULL)
10764 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765#ifdef FEAT_MODIFY_FNAME
10766 skip_mod = TRUE;
10767#endif
10768 break;
10769 }
10770 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010771 if (*s == '<') /* "#<99" uses v:oldfiles */
10772 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 i = (int)getdigits(&s);
10774 *usedlen = (int)(s - src); /* length of what we expand */
10775
Bram Moolenaard812df62008-11-09 12:46:09 +000010776 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010778 if (*usedlen < 2)
10779 {
10780 /* Should we give an error message for #<text? */
10781 *usedlen = 1;
10782 return NULL;
10783 }
10784#ifdef FEAT_EVAL
10785 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10786 (long)i);
10787 if (result == NULL)
10788 {
10789 *errormsg = (char_u *)"";
10790 return NULL;
10791 }
10792#else
10793 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796 }
10797 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010798 {
10799 buf = buflist_findnr(i);
10800 if (buf == NULL)
10801 {
10802 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10803 return NULL;
10804 }
10805 if (lnump != NULL)
10806 *lnump = ECMD_LAST;
10807 if (buf->b_fname == NULL)
10808 {
10809 result = (char_u *)"";
10810 valid = 0; /* Must have ":p:h" to be valid */
10811 }
10812 else
10813 result = buf->b_fname;
10814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815 break;
10816
10817#ifdef FEAT_SEARCHPATH
10818 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010819 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 if (result == NULL)
10821 {
10822 *errormsg = (char_u *)"";
10823 return NULL;
10824 }
10825 resultbuf = result; /* remember allocated string */
10826 break;
10827#endif
10828
10829#ifdef FEAT_AUTOCMD
10830 case SPEC_AFILE: /* file name for autocommand */
10831 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010832 if (result != NULL && !autocmd_fname_full)
10833 {
10834 /* Still need to turn the fname into a full path. It is
10835 * postponed to avoid a delay when <afile> is not used. */
10836 autocmd_fname_full = TRUE;
10837 result = FullName_save(autocmd_fname, FALSE);
10838 vim_free(autocmd_fname);
10839 autocmd_fname = result;
10840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841 if (result == NULL)
10842 {
10843 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10844 return NULL;
10845 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010846 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847 break;
10848
10849 case SPEC_ABUF: /* buffer number for autocommand */
10850 if (autocmd_bufnr <= 0)
10851 {
10852 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10853 return NULL;
10854 }
10855 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10856 result = strbuf;
10857 break;
10858
10859 case SPEC_AMATCH: /* match name for autocommand */
10860 result = autocmd_match;
10861 if (result == NULL)
10862 {
10863 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10864 return NULL;
10865 }
10866 break;
10867
10868#endif
10869 case SPEC_SFILE: /* file name for ":so" command */
10870 result = sourcing_name;
10871 if (result == NULL)
10872 {
10873 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10874 return NULL;
10875 }
10876 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010877 case SPEC_SLNUM: /* line in file for ":so" command */
10878 if (sourcing_name == NULL || sourcing_lnum == 0)
10879 {
10880 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10881 return NULL;
10882 }
10883 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10884 result = strbuf;
10885 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886#if defined(FEAT_CLIENTSERVER)
10887 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010888 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10889 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890 result = strbuf;
10891 break;
10892#endif
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020010893 default:
10894 result = (char_u *)""; /* avoid gcc warning */
10895 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896 }
10897
10898 resultlen = (int)STRLEN(result); /* length of new string */
10899 if (src[*usedlen] == '<') /* remove the file name extension */
10900 {
10901 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903 resultlen = (int)(s - result);
10904 }
10905#ifdef FEAT_MODIFY_FNAME
10906 else if (!skip_mod)
10907 {
10908 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10909 &resultlen);
10910 if (result == NULL)
10911 {
10912 *errormsg = (char_u *)"";
10913 return NULL;
10914 }
10915 }
10916#endif
10917 }
10918
10919 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10920 {
10921 if (valid != VALID_HEAD + VALID_PATH)
10922 /* xgettext:no-c-format */
10923 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10924 else
10925 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10926 result = NULL;
10927 }
10928 else
10929 result = vim_strnsave(result, resultlen);
10930 vim_free(resultbuf);
10931 return result;
10932}
10933
10934/*
10935 * Concatenate all files in the argument list, separated by spaces, and return
10936 * it in one allocated string.
10937 * Spaces and backslashes in the file names are escaped with a backslash.
10938 * Returns NULL when out of memory.
10939 */
10940 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010941arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942{
10943 int len;
10944 int idx;
10945 char_u *retval = NULL;
10946 char_u *p;
10947
10948 /*
10949 * Do this loop two times:
10950 * first time: compute the total length
10951 * second time: concatenate the names
10952 */
10953 for (;;)
10954 {
10955 len = 0;
10956 for (idx = 0; idx < ARGCOUNT; ++idx)
10957 {
10958 p = alist_name(&ARGLIST[idx]);
10959 if (p != NULL)
10960 {
10961 if (len > 0)
10962 {
10963 /* insert a space in between names */
10964 if (retval != NULL)
10965 retval[len] = ' ';
10966 ++len;
10967 }
10968 for ( ; *p != NUL; ++p)
10969 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010970 if (*p == ' '
10971#ifndef BACKSLASH_IN_FILENAME
10972 || *p == '\\'
10973#endif
10974 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975 {
10976 /* insert a backslash */
10977 if (retval != NULL)
10978 retval[len] = '\\';
10979 ++len;
10980 }
10981 if (retval != NULL)
10982 retval[len] = *p;
10983 ++len;
10984 }
10985 }
10986 }
10987
10988 /* second time: break here */
10989 if (retval != NULL)
10990 {
10991 retval[len] = NUL;
10992 break;
10993 }
10994
10995 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010996 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997 if (retval == NULL)
10998 break;
10999 }
11000
11001 return retval;
11002}
11003
11004#if defined(FEAT_AUTOCMD) || defined(PROTO)
11005/*
11006 * Expand the <sfile> string in "arg".
11007 *
11008 * Returns an allocated string, or NULL for any error.
11009 */
11010 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011011expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012{
11013 char_u *errormsg;
11014 int len;
11015 char_u *result;
11016 char_u *newres;
11017 char_u *repl;
11018 int srclen;
11019 char_u *p;
11020
11021 result = vim_strsave(arg);
11022 if (result == NULL)
11023 return NULL;
11024
11025 for (p = result; *p; )
11026 {
11027 if (STRNCMP(p, "<sfile>", 7) != 0)
11028 ++p;
11029 else
11030 {
11031 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000011032 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033 if (errormsg != NULL)
11034 {
11035 if (*errormsg)
11036 emsg(errormsg);
11037 vim_free(result);
11038 return NULL;
11039 }
11040 if (repl == NULL) /* no match (cannot happen) */
11041 {
11042 p += srclen;
11043 continue;
11044 }
11045 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11046 newres = alloc(len);
11047 if (newres == NULL)
11048 {
11049 vim_free(repl);
11050 vim_free(result);
11051 return NULL;
11052 }
11053 mch_memmove(newres, result, (size_t)(p - result));
11054 STRCPY(newres + (p - result), repl);
11055 len = (int)STRLEN(newres);
11056 STRCAT(newres, p + srclen);
11057 vim_free(repl);
11058 vim_free(result);
11059 result = newres;
11060 p = newres + len; /* continue after the match */
11061 }
11062 }
11063
11064 return result;
11065}
11066#endif
11067
11068#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011069static int ses_winsizes(FILE *fd, int restore_size,
11070 win_T *tab_firstwin);
11071static int ses_win_rec(FILE *fd, frame_T *fr);
11072static frame_T *ses_skipframe(frame_T *fr);
11073static int ses_do_frame(frame_T *fr);
11074static int ses_do_win(win_T *wp);
11075static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11076static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
11077static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078
11079/*
11080 * Write openfile commands for the current buffers to an .exrc file.
11081 * Return FAIL on error, OK otherwise.
11082 */
11083 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011084makeopens(
11085 FILE *fd,
11086 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011087{
11088 buf_T *buf;
11089 int only_save_windows = TRUE;
11090 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091 int restore_size = TRUE;
11092 win_T *wp;
11093 char_u *sname;
11094 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011095 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011096 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011097 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011098 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011099 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011100 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101
11102 if (ssop_flags & SSOP_BUFFERS)
11103 only_save_windows = FALSE; /* Save ALL buffers */
11104
11105 /*
11106 * Begin by setting the this_session variable, and then other
11107 * sessionable variables.
11108 */
11109#ifdef FEAT_EVAL
11110 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11111 return FAIL;
11112 if (ssop_flags & SSOP_GLOBALS)
11113 if (store_session_globals(fd) == FAIL)
11114 return FAIL;
11115#endif
11116
11117 /*
11118 * Close all windows but one.
11119 */
11120 if (put_line(fd, "silent only") == FAIL)
11121 return FAIL;
11122
11123 /*
11124 * Now a :cd command to the session directory or the current directory
11125 */
11126 if (ssop_flags & SSOP_SESDIR)
11127 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011128 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11129 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130 return FAIL;
11131 }
11132 else if (ssop_flags & SSOP_CURDIR)
11133 {
11134 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11135 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011136 || fputs("cd ", fd) < 0
11137 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11138 || put_eol(fd) == FAIL)
11139 {
11140 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143 vim_free(sname);
11144 }
11145
11146 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011147 * If there is an empty, unnamed buffer we will wipe it out later.
11148 * Remember the buffer number.
11149 */
11150 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11151 return FAIL;
11152 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11153 return FAIL;
11154 if (put_line(fd, "endif") == FAIL)
11155 return FAIL;
11156
11157 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158 * Now save the current files, current buffer first.
11159 */
11160 if (put_line(fd, "set shortmess=aoO") == FAIL)
11161 return FAIL;
11162
11163 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011164 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165 {
11166 if (!(only_save_windows && buf->b_nwindows == 0)
11167 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
11168 && buf->b_fname != NULL
11169 && buf->b_p_bl)
11170 {
11171 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11172 : buf->b_wininfo->wi_fpos.lnum) < 0
11173 || ses_fname(fd, buf, &ssop_flags) == FAIL)
11174 return FAIL;
11175 }
11176 }
11177
11178 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011179 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011180 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11181 return FAIL;
11182
11183 if (ssop_flags & SSOP_RESIZE)
11184 {
11185 /* Note: after the restore we still check it worked!*/
11186 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11187 || put_eol(fd) == FAIL)
11188 return FAIL;
11189 }
11190
11191#ifdef FEAT_GUI
11192 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11193 {
11194 int x, y;
11195
11196 if (gui_mch_get_winpos(&x, &y) == OK)
11197 {
11198 /* Note: after the restore we still check it worked!*/
11199 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11200 return FAIL;
11201 }
11202 }
11203#endif
11204
11205 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011206 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11207 * will be displayed when creating the next tab. That resizes the windows
11208 * in the first tab, which may cause problems. Set 'showtabline' to 2
11209 * temporarily to avoid that.
11210 */
11211 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11212 {
11213 if (put_line(fd, "set stal=2") == FAIL)
11214 return FAIL;
11215 restore_stal = TRUE;
11216 }
11217
11218 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011219 * May repeat putting Windows for each tab, when "tabpages" is in
11220 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011221 * Don't use goto_tabpage(), it may change directory and trigger
11222 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011223 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011224 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011225 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011226 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011227 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011228 int need_tabnew = FALSE;
11229 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011230
Bram Moolenaar18144c82006-04-12 21:52:12 +000011231 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011232 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011233 tabpage_T *tp = find_tabpage(tabnr);
11234
11235 if (tp == NULL)
11236 break; /* done all tab pages */
11237 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011238 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011239 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011240 tab_topframe = topframe;
11241 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011242 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011243 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011244 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011245 tab_topframe = tp->tp_topframe;
11246 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011247 if (tabnr > 1)
11248 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250
Bram Moolenaar18144c82006-04-12 21:52:12 +000011251 /*
11252 * Before creating the window layout, try loading one file. If this
11253 * is aborted we don't end up with a number of useless windows.
11254 * This may have side effects! (e.g., compressed or network file).
11255 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011256 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011257 {
11258 if (ses_do_win(wp)
11259 && wp->w_buffer->b_ffname != NULL
11260 && !wp->w_buffer->b_help
11261#ifdef FEAT_QUICKFIX
11262 && !bt_nofile(wp->w_buffer)
11263#endif
11264 )
11265 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011266 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000011267 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
11268 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011269 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011270 if (!wp->w_arg_idx_invalid)
11271 edited_win = wp;
11272 break;
11273 }
11274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011276 /* If no file got edited create an empty tab page. */
11277 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11278 return FAIL;
11279
Bram Moolenaar18144c82006-04-12 21:52:12 +000011280 /*
11281 * Save current window layout.
11282 */
11283 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011284 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011285 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011287 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11288 return FAIL;
11289 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11290 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291
Bram Moolenaar18144c82006-04-12 21:52:12 +000011292 /*
11293 * Check if window sizes can be restored (no windows omitted).
11294 * Remember the window number of the current window after restoring.
11295 */
11296 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011297 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011298 {
11299 if (ses_do_win(wp))
11300 ++nr;
11301 else
11302 restore_size = FALSE;
11303 if (curwin == wp)
11304 cnr = nr;
11305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011306
Bram Moolenaar18144c82006-04-12 21:52:12 +000011307 /* Go to the first window. */
11308 if (put_line(fd, "wincmd t") == FAIL)
11309 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011310
Bram Moolenaar18144c82006-04-12 21:52:12 +000011311 /*
11312 * If more than one window, see if sizes can be restored.
11313 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11314 * resized when moving between windows.
11315 * Do this before restoring the view, so that the topline and the
11316 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011317 * winminheight and winminwidth need to be set to avoid an error if the
11318 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011319 */
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011320 if (put_line(fd, "set winminheight=1 winheight=1 winminwidth=1 winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011321 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011322 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011323 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011324
Bram Moolenaar18144c82006-04-12 21:52:12 +000011325 /*
11326 * Restore the view of the window (options, file, cursor, etc.).
11327 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011328 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011329 {
11330 if (!ses_do_win(wp))
11331 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011332 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11333 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011334 return FAIL;
11335 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11336 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011337 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011338 }
11339
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011340 /* The argument index in the first tab page is zero, need to set it in
11341 * each window. For further tab pages it's the window where we do
11342 * "tabedit". */
11343 cur_arg_idx = next_arg_idx;
11344
Bram Moolenaar18144c82006-04-12 21:52:12 +000011345 /*
11346 * Restore cursor to the current window if it's not the first one.
11347 */
11348 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11349 || put_eol(fd) == FAIL))
11350 return FAIL;
11351
11352 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011353 * Restore window sizes again after jumping around in windows, because
11354 * the current window has a minimum size while others may not.
11355 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011356 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011357 return FAIL;
11358
Bram Moolenaar18144c82006-04-12 21:52:12 +000011359 /* Don't continue in another tab page when doing only the current one
11360 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011361 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011362 break;
11363 }
11364
11365 if (ssop_flags & SSOP_TABPAGES)
11366 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011367 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11368 || put_eol(fd) == FAIL)
11369 return FAIL;
11370 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011371 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11372 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011373
Bram Moolenaar9c102382006-05-03 21:26:49 +000011374 /*
11375 * Wipe out an empty unnamed buffer we started in.
11376 */
11377 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11378 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011379 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011380 return FAIL;
11381 if (put_line(fd, "endif") == FAIL)
11382 return FAIL;
11383 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11384 return FAIL;
11385
11386 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11387 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11388 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11389 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011390 /* Re-apply 'winminheight' and 'winminwidth'. */
11391 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11392 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11393 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394
11395 /*
11396 * Lastly, execute the x.vim file if it exists.
11397 */
11398 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11399 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011400 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401 || put_line(fd, "endif") == FAIL)
11402 return FAIL;
11403
11404 return OK;
11405}
11406
11407 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011408ses_winsizes(
11409 FILE *fd,
11410 int restore_size,
11411 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412{
11413 int n = 0;
11414 win_T *wp;
11415
11416 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11417 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011418 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011419 {
11420 if (!ses_do_win(wp))
11421 continue;
11422 ++n;
11423
11424 /* restore height when not full height */
11425 if (wp->w_height + wp->w_status_height < topframe->fr_height
11426 && (fprintf(fd,
11427 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11428 n, (long)wp->w_height, Rows / 2, Rows) < 0
11429 || put_eol(fd) == FAIL))
11430 return FAIL;
11431
11432 /* restore width when not full width */
11433 if (wp->w_width < Columns && (fprintf(fd,
11434 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11435 n, (long)wp->w_width, Columns / 2, Columns) < 0
11436 || put_eol(fd) == FAIL))
11437 return FAIL;
11438 }
11439 }
11440 else
11441 {
11442 /* Just equalise window sizes */
11443 if (put_line(fd, "wincmd =") == FAIL)
11444 return FAIL;
11445 }
11446 return OK;
11447}
11448
11449/*
11450 * Write commands to "fd" to recursively create windows for frame "fr",
11451 * horizontally and vertically split.
11452 * After the commands the last window in the frame is the current window.
11453 * Returns FAIL when writing the commands to "fd" fails.
11454 */
11455 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011456ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457{
11458 frame_T *frc;
11459 int count = 0;
11460
11461 if (fr->fr_layout != FR_LEAF)
11462 {
11463 /* Find first frame that's not skipped and then create a window for
11464 * each following one (first frame is already there). */
11465 frc = ses_skipframe(fr->fr_child);
11466 if (frc != NULL)
11467 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11468 {
11469 /* Make window as big as possible so that we have lots of room
11470 * to split. */
11471 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11472 || put_line(fd, fr->fr_layout == FR_COL
11473 ? "split" : "vsplit") == FAIL)
11474 return FAIL;
11475 ++count;
11476 }
11477
11478 /* Go back to the first window. */
11479 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11480 ? "%dwincmd k" : "%dwincmd h", count) < 0
11481 || put_eol(fd) == FAIL))
11482 return FAIL;
11483
11484 /* Recursively create frames/windows in each window of this column or
11485 * row. */
11486 frc = ses_skipframe(fr->fr_child);
11487 while (frc != NULL)
11488 {
11489 ses_win_rec(fd, frc);
11490 frc = ses_skipframe(frc->fr_next);
11491 /* Go to next window. */
11492 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11493 return FAIL;
11494 }
11495 }
11496 return OK;
11497}
11498
11499/*
11500 * Skip frames that don't contain windows we want to save in the Session.
11501 * Returns NULL when there none.
11502 */
11503 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011504ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505{
11506 frame_T *frc;
11507
11508 for (frc = fr; frc != NULL; frc = frc->fr_next)
11509 if (ses_do_frame(frc))
11510 break;
11511 return frc;
11512}
11513
11514/*
11515 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11516 * the Session.
11517 */
11518 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011519ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011520{
11521 frame_T *frc;
11522
11523 if (fr->fr_layout == FR_LEAF)
11524 return ses_do_win(fr->fr_win);
11525 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11526 if (ses_do_frame(frc))
11527 return TRUE;
11528 return FALSE;
11529}
11530
11531/*
11532 * Return non-zero if window "wp" is to be stored in the Session.
11533 */
11534 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011535ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011536{
11537 if (wp->w_buffer->b_fname == NULL
11538#ifdef FEAT_QUICKFIX
11539 /* When 'buftype' is "nofile" can't restore the window contents. */
11540 || bt_nofile(wp->w_buffer)
11541#endif
11542 )
11543 return (ssop_flags & SSOP_BLANK);
11544 if (wp->w_buffer->b_help)
11545 return (ssop_flags & SSOP_HELP);
11546 return TRUE;
11547}
11548
11549/*
11550 * Write commands to "fd" to restore the view of a window.
11551 * Caller must make sure 'scrolloff' is zero.
11552 */
11553 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011554put_view(
11555 FILE *fd,
11556 win_T *wp,
11557 int add_edit, /* add ":edit" command to view */
11558 unsigned *flagp, /* vop_flags or ssop_flags */
11559 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011560 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011561{
11562 win_T *save_curwin;
11563 int f;
11564 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011565 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566
11567 /* Always restore cursor position for ":mksession". For ":mkview" only
11568 * when 'viewoptions' contains "cursor". */
11569 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11570
11571 /*
11572 * Local argument list.
11573 */
11574 if (wp->w_alist == &global_alist)
11575 {
11576 if (put_line(fd, "argglobal") == FAIL)
11577 return FAIL;
11578 }
11579 else
11580 {
11581 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11582 flagp == &vop_flags
11583 || !(*flagp & SSOP_CURDIR)
11584 || wp->w_localdir != NULL, flagp) == FAIL)
11585 return FAIL;
11586 }
11587
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011588 /* Only when part of a session: restore the argument index. Some
11589 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011590 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011591 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011592 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011593 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011594 || put_eol(fd) == FAIL)
11595 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011596 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011597 }
11598
11599 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011600 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011601 {
11602 /*
11603 * Load the file.
11604 */
11605 if (wp->w_buffer->b_ffname != NULL
11606#ifdef FEAT_QUICKFIX
11607 && !bt_nofile(wp->w_buffer)
11608#endif
11609 )
11610 {
11611 /*
11612 * Editing a file in this buffer: use ":edit file".
11613 * This may have side effects! (e.g., compressed or network file).
11614 */
11615 if (fputs("edit ", fd) < 0
11616 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11617 return FAIL;
11618 }
11619 else
11620 {
11621 /* No file in this buffer, just make it empty. */
11622 if (put_line(fd, "enew") == FAIL)
11623 return FAIL;
11624#ifdef FEAT_QUICKFIX
11625 if (wp->w_buffer->b_ffname != NULL)
11626 {
11627 /* The buffer does have a name, but it's not a file name. */
11628 if (fputs("file ", fd) < 0
11629 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11630 return FAIL;
11631 }
11632#endif
11633 do_cursor = FALSE;
11634 }
11635 }
11636
11637 /*
11638 * Local mappings and abbreviations.
11639 */
11640 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11641 && makemap(fd, wp->w_buffer) == FAIL)
11642 return FAIL;
11643
11644 /*
11645 * Local options. Need to go to the window temporarily.
11646 * Store only local values when using ":mkview" and when ":mksession" is
11647 * used and 'sessionoptions' doesn't include "options".
11648 * Some folding options are always stored when "folds" is included,
11649 * otherwise the folds would not be restored correctly.
11650 */
11651 save_curwin = curwin;
11652 curwin = wp;
11653 curbuf = curwin->w_buffer;
11654 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11655 f = makeset(fd, OPT_LOCAL,
11656 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11657#ifdef FEAT_FOLDING
11658 else if (*flagp & SSOP_FOLDS)
11659 f = makefoldset(fd);
11660#endif
11661 else
11662 f = OK;
11663 curwin = save_curwin;
11664 curbuf = curwin->w_buffer;
11665 if (f == FAIL)
11666 return FAIL;
11667
11668#ifdef FEAT_FOLDING
11669 /*
11670 * Save Folds when 'buftype' is empty and for help files.
11671 */
11672 if ((*flagp & SSOP_FOLDS)
11673 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011674# ifdef FEAT_QUICKFIX
11675 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11676# endif
11677 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011678 {
11679 if (put_folds(fd, wp) == FAIL)
11680 return FAIL;
11681 }
11682#endif
11683
11684 /*
11685 * Set the cursor after creating folds, since that moves the cursor.
11686 */
11687 if (do_cursor)
11688 {
11689
11690 /* Restore the cursor line in the file and relatively in the
11691 * window. Don't use "G", it changes the jumplist. */
11692 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11693 (long)wp->w_cursor.lnum,
11694 (long)(wp->w_cursor.lnum - wp->w_topline),
11695 (long)wp->w_height / 2, (long)wp->w_height) < 0
11696 || put_eol(fd) == FAIL
11697 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11698 || put_line(fd, "exe s:l") == FAIL
11699 || put_line(fd, "normal! zt") == FAIL
11700 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11701 || put_eol(fd) == FAIL)
11702 return FAIL;
11703 /* Restore the cursor column and left offset when not wrapping. */
11704 if (wp->w_cursor.col == 0)
11705 {
11706 if (put_line(fd, "normal! 0") == FAIL)
11707 return FAIL;
11708 }
11709 else
11710 {
11711 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11712 {
11713 if (fprintf(fd,
11714 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011715 (long)wp->w_virtcol + 1,
11716 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011717 (long)wp->w_width / 2, (long)wp->w_width) < 0
11718 || put_eol(fd) == FAIL
11719 || put_line(fd, "if s:c > 0") == FAIL
11720 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011721 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11722 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011723 || put_eol(fd) == FAIL
11724 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011725 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011726 || put_eol(fd) == FAIL
11727 || put_line(fd, "endif") == FAIL)
11728 return FAIL;
11729 }
11730 else
11731 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011732 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011733 || put_eol(fd) == FAIL)
11734 return FAIL;
11735 }
11736 }
11737 }
11738
11739 /*
11740 * Local directory.
11741 */
11742 if (wp->w_localdir != NULL)
11743 {
11744 if (fputs("lcd ", fd) < 0
11745 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11746 || put_eol(fd) == FAIL)
11747 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011748 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011749 }
11750
11751 return OK;
11752}
11753
11754/*
11755 * Write an argument list to the session file.
11756 * Returns FAIL if writing fails.
11757 */
11758 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011759ses_arglist(
11760 FILE *fd,
11761 char *cmd,
11762 garray_T *gap,
11763 int fullname, /* TRUE: use full path name */
11764 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011765{
11766 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011767 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011768 char_u *s;
11769
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011770 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11771 return FAIL;
11772 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011773 return FAIL;
11774 for (i = 0; i < gap->ga_len; ++i)
11775 {
11776 /* NULL file names are skipped (only happens when out of memory). */
11777 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11778 if (s != NULL)
11779 {
11780 if (fullname)
11781 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011782 buf = alloc(MAXPATHL);
11783 if (buf != NULL)
11784 {
11785 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11786 s = buf;
11787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011788 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010011789 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011790 || ses_put_fname(fd, s, flagp) == FAIL
11791 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011792 {
11793 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011794 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011795 }
11796 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797 }
11798 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011799 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011800}
11801
11802/*
11803 * Write a buffer name to the session file.
11804 * Also ends the line.
11805 * Returns FAIL if writing fails.
11806 */
11807 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011808ses_fname(FILE *fd, buf_T *buf, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809{
11810 char_u *name;
11811
11812 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011813 * the session file will be sourced.
11814 * Don't do this for ":mkview", we don't know the current directory.
11815 * Don't do this after ":lcd", we don't keep track of what the current
11816 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011817 if (buf->b_sfname != NULL
11818 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011819 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011820#ifdef FEAT_AUTOCHDIR
11821 && !p_acd
11822#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011823 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011824 name = buf->b_sfname;
11825 else
11826 name = buf->b_ffname;
11827 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11828 return FAIL;
11829 return OK;
11830}
11831
11832/*
11833 * Write a file name to the session file.
11834 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11835 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011836 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011837 */
11838 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011839ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011840{
11841 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011842 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011843 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011844
11845 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011846 if (sname == NULL)
11847 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011848
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011849 if (*flagp & SSOP_SLASH)
11850 {
11851 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011852 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011853 if (*p == '\\')
11854 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011856
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011857 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011858 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011860 if (p == NULL)
11861 return FAIL;
11862
11863 /* write the result */
11864 if (fputs((char *)p, fd) < 0)
11865 retval = FAIL;
11866
11867 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868 return retval;
11869}
11870
11871/*
11872 * ":loadview [nr]"
11873 */
11874 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011875ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011876{
11877 char_u *fname;
11878
11879 fname = get_view_file(*eap->arg);
11880 if (fname != NULL)
11881 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011882 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011883 vim_free(fname);
11884 }
11885}
11886
11887/*
11888 * Get the name of the view file for the current buffer.
11889 */
11890 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011891get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011892{
11893 int len = 0;
11894 char_u *p, *s;
11895 char_u *retval;
11896 char_u *sname;
11897
11898 if (curbuf->b_ffname == NULL)
11899 {
11900 EMSG(_(e_noname));
11901 return NULL;
11902 }
11903 sname = home_replace_save(NULL, curbuf->b_ffname);
11904 if (sname == NULL)
11905 return NULL;
11906
11907 /*
11908 * We want a file name without separators, because we're not going to make
11909 * a directory.
11910 * "normal" path separator -> "=+"
11911 * "=" -> "=="
11912 * ":" path separator -> "=-"
11913 */
11914 for (p = sname; *p; ++p)
11915 if (*p == '=' || vim_ispathsep(*p))
11916 ++len;
11917 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11918 if (retval != NULL)
11919 {
11920 STRCPY(retval, p_vdir);
11921 add_pathsep(retval);
11922 s = retval + STRLEN(retval);
11923 for (p = sname; *p; ++p)
11924 {
11925 if (*p == '=')
11926 {
11927 *s++ = '=';
11928 *s++ = '=';
11929 }
11930 else if (vim_ispathsep(*p))
11931 {
11932 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011933#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011934 if (*p == ':')
11935 *s++ = '-';
11936 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011937#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011938 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011939 }
11940 else
11941 *s++ = *p;
11942 }
11943 *s++ = '=';
11944 *s++ = c;
11945 STRCPY(s, ".vim");
11946 }
11947
11948 vim_free(sname);
11949 return retval;
11950}
11951
11952#endif /* FEAT_SESSION */
11953
11954/*
11955 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11956 * Return FAIL for a write error.
11957 */
11958 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011959put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011960{
11961 if (
11962#ifdef USE_CRNL
11963 (
11964# ifdef MKSESSION_NL
11965 !mksession_nl &&
11966# endif
11967 (putc('\r', fd) < 0)) ||
11968#endif
11969 (putc('\n', fd) < 0))
11970 return FAIL;
11971 return OK;
11972}
11973
11974/*
11975 * Write a line to "fd".
11976 * Return FAIL for a write error.
11977 */
11978 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011979put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011980{
11981 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11982 return FAIL;
11983 return OK;
11984}
11985
11986#ifdef FEAT_VIMINFO
11987/*
11988 * ":rviminfo" and ":wviminfo".
11989 */
11990 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011991ex_viminfo(
11992 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011993{
11994 char_u *save_viminfo;
11995
11996 save_viminfo = p_viminfo;
11997 if (*p_viminfo == NUL)
11998 p_viminfo = (char_u *)"'100";
11999 if (eap->cmdidx == CMD_rviminfo)
12000 {
Bram Moolenaard812df62008-11-09 12:46:09 +000012001 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
12002 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003 EMSG(_("E195: Cannot open viminfo file for reading"));
12004 }
12005 else
12006 write_viminfo(eap->arg, eap->forceit);
12007 p_viminfo = save_viminfo;
12008}
12009#endif
12010
12011#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012012/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020012013 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000012014 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012015 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012016 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012017dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012018{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019 if (fname == NULL)
12020 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012021 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022}
12023#endif
12024
12025/*
12026 * ":behave {mswin,xterm}"
12027 */
12028 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012029ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012030{
12031 if (STRCMP(eap->arg, "mswin") == 0)
12032 {
12033 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12034 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12035 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12036 set_option_value((char_u *)"keymodel", 0L,
12037 (char_u *)"startsel,stopsel", 0);
12038 }
12039 else if (STRCMP(eap->arg, "xterm") == 0)
12040 {
12041 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12042 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12043 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12044 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12045 }
12046 else
12047 EMSG2(_(e_invarg2), eap->arg);
12048}
12049
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012050#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12051/*
12052 * Function given to ExpandGeneric() to obtain the possible arguments of the
12053 * ":behave {mswin,xterm}" command.
12054 */
12055 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012056get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012057{
12058 if (idx == 0)
12059 return (char_u *)"mswin";
12060 if (idx == 1)
12061 return (char_u *)"xterm";
12062 return NULL;
12063}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012064
12065/*
12066 * Function given to ExpandGeneric() to obtain the possible arguments of the
12067 * ":messages {clear}" command.
12068 */
12069 char_u *
12070get_messages_arg(expand_T *xp UNUSED, int idx)
12071{
12072 if (idx == 0)
12073 return (char_u *)"clear";
12074 return NULL;
12075}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012076#endif
12077
Bram Moolenaar071d4272004-06-13 20:20:40 +000012078#ifdef FEAT_AUTOCMD
12079static int filetype_detect = FALSE;
12080static int filetype_plugin = FALSE;
12081static int filetype_indent = FALSE;
12082
12083/*
12084 * ":filetype [plugin] [indent] {on,off,detect}"
12085 * on: Load the filetype.vim file to install autocommands for file types.
12086 * off: Load the ftoff.vim file to remove all autocommands for file types.
12087 * plugin on: load filetype.vim and ftplugin.vim
12088 * plugin off: load ftplugof.vim
12089 * indent on: load filetype.vim and indent.vim
12090 * indent off: load indoff.vim
12091 */
12092 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012093ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012094{
12095 char_u *arg = eap->arg;
12096 int plugin = FALSE;
12097 int indent = FALSE;
12098
12099 if (*eap->arg == NUL)
12100 {
12101 /* Print current status. */
12102 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
12103 filetype_detect ? "ON" : "OFF",
12104 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12105 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12106 return;
12107 }
12108
12109 /* Accept "plugin" and "indent" in any order. */
12110 for (;;)
12111 {
12112 if (STRNCMP(arg, "plugin", 6) == 0)
12113 {
12114 plugin = TRUE;
12115 arg = skipwhite(arg + 6);
12116 continue;
12117 }
12118 if (STRNCMP(arg, "indent", 6) == 0)
12119 {
12120 indent = TRUE;
12121 arg = skipwhite(arg + 6);
12122 continue;
12123 }
12124 break;
12125 }
12126 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12127 {
12128 if (*arg == 'o' || !filetype_detect)
12129 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012130 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012131 filetype_detect = TRUE;
12132 if (plugin)
12133 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012134 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012135 filetype_plugin = TRUE;
12136 }
12137 if (indent)
12138 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012139 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012140 filetype_indent = TRUE;
12141 }
12142 }
12143 if (*arg == 'd')
12144 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012145 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012146 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012147 }
12148 }
12149 else if (STRCMP(arg, "off") == 0)
12150 {
12151 if (plugin || indent)
12152 {
12153 if (plugin)
12154 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012155 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012156 filetype_plugin = FALSE;
12157 }
12158 if (indent)
12159 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012160 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161 filetype_indent = FALSE;
12162 }
12163 }
12164 else
12165 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012166 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012167 filetype_detect = FALSE;
12168 }
12169 }
12170 else
12171 EMSG2(_(e_invarg2), arg);
12172}
12173
12174/*
12175 * ":setfiletype {name}"
12176 */
12177 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012178ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179{
12180 if (!did_filetype)
12181 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
12182}
12183#endif
12184
Bram Moolenaar071d4272004-06-13 20:20:40 +000012185 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012186ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012187{
12188#ifdef FEAT_DIGRAPHS
12189 if (*eap->arg != NUL)
12190 putdigraph(eap->arg);
12191 else
12192 listdigraphs();
12193#else
12194 EMSG(_("E196: No digraphs in this version"));
12195#endif
12196}
12197
12198 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012199ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200{
12201 int flags = 0;
12202
12203 if (eap->cmdidx == CMD_setlocal)
12204 flags = OPT_LOCAL;
12205 else if (eap->cmdidx == CMD_setglobal)
12206 flags = OPT_GLOBAL;
12207#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
12208 if (cmdmod.browse && flags == 0)
12209 ex_options(eap);
12210 else
12211#endif
12212 (void)do_set(eap->arg, flags);
12213}
12214
12215#ifdef FEAT_SEARCH_EXTRA
12216/*
12217 * ":nohlsearch"
12218 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012219 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012220ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012221{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010012222 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012223 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012224}
12225
12226/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012227 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012228 * Sets nextcmd to the start of the next command, if any. Also called when
12229 * skipping commands to find the next command.
12230 */
12231 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012232ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012233{
12234 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012235 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012236 char_u *end;
12237 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012238 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012239
12240 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012241 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012242 else
12243 {
12244 EMSG(e_invcmd);
12245 return;
12246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012247
12248 /* First clear any old pattern. */
12249 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012250 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012251
12252 if (ends_excmd(*eap->arg))
12253 end = eap->arg;
12254 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012255 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256 end = eap->arg + 4;
12257 else
12258 {
12259 p = skiptowhite(eap->arg);
12260 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012261 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262 p = skipwhite(p);
12263 if (*p == NUL)
12264 {
12265 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012266 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012267 EMSG2(_(e_invarg2), eap->arg);
12268 return;
12269 }
12270 end = skip_regexp(p + 1, *p, TRUE, NULL);
12271 if (!eap->skip)
12272 {
12273 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12274 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012275 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012276 eap->errmsg = e_trailing;
12277 return;
12278 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012279 if (*end != *p)
12280 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012281 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012282 EMSG2(_(e_invarg2), p);
12283 return;
12284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012285
12286 c = *end;
12287 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012288 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012289 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012290 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291 }
12292 }
12293 eap->nextcmd = find_nextcmd(end);
12294}
12295#endif
12296
12297#ifdef FEAT_CRYPT
12298/*
12299 * ":X": Get crypt key
12300 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012302ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012303{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012304 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012305 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012306}
12307#endif
12308
12309#ifdef FEAT_FOLDING
12310 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012311ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312{
12313 if (foldManualAllowed(TRUE))
12314 foldCreate(eap->line1, eap->line2);
12315}
12316
12317 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012318ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012319{
12320 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12321 eap->forceit, FALSE);
12322}
12323
12324 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012325ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012326{
12327 linenr_T lnum;
12328
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012329#ifdef FEAT_CLIPBOARD
12330 start_global_changes();
12331#endif
12332
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333 /* First set the marks for all lines closed/open. */
12334 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12335 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12336 ml_setmarked(lnum);
12337
12338 /* Execute the command on the marked lines. */
12339 global_exe(eap->arg);
12340 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012341#ifdef FEAT_CLIPBOARD
12342 end_global_changes();
12343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344}
12345#endif