blob: 9dac9bf3a7243c39b04c663062bf2c47130744e0 [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,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004559 SEARCH_HIS | SEARCH_MSG, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004560 {
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,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004616 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004617 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 Moolenaarbf15b8d2017-06-04 20:43:48 +02005045 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046#ifndef UNIX
5047 && !(eap->argt & NOSPC)
5048#endif
5049 )
5050 {
5051 char_u *l;
5052#ifdef BACKSLASH_IN_FILENAME
5053 /* Don't escape a backslash here, because rem_backslash() doesn't
5054 * remove it later. */
5055 static char_u *nobslash = (char_u *)" \t\"|";
5056# define ESCAPE_CHARS nobslash
5057#else
5058# define ESCAPE_CHARS escape_chars
5059#endif
5060
5061 for (l = repl; *l; ++l)
5062 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5063 {
5064 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5065 if (l != NULL)
5066 {
5067 vim_free(repl);
5068 repl = l;
5069 }
5070 break;
5071 }
5072 }
5073
5074 /* For a shell command a '!' must be escaped. */
5075 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005076 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 {
5078 char_u *l;
5079
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005080 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 if (l != NULL)
5082 {
5083 vim_free(repl);
5084 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 }
5086 }
5087
5088 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5089 vim_free(repl);
5090 if (p == NULL)
5091 return FAIL;
5092 }
5093
5094 /*
5095 * One file argument: Expand wildcards.
5096 * Don't do this with ":r !command" or ":w !command".
5097 */
5098 if ((eap->argt & NOSPC) && !eap->usefilter)
5099 {
5100 /*
5101 * May do this twice:
5102 * 1. Replace environment variables.
5103 * 2. Replace any other wildcards, remove backslashes.
5104 */
5105 for (n = 1; n <= 2; ++n)
5106 {
5107 if (n == 2)
5108 {
5109#ifdef UNIX
5110 /*
5111 * Only for Unix we check for more than one file name.
5112 * For other systems spaces are considered to be part
5113 * of the file name.
5114 * Only check here if there is no wildcard, otherwise
5115 * ExpandOne() will check for errors. This allows
5116 * ":e `ls ve*.c`" on Unix.
5117 */
5118 if (!has_wildcards)
5119 for (p = eap->arg; *p; ++p)
5120 {
5121 /* skip escaped characters */
5122 if (p[1] && (*p == '\\' || *p == Ctrl_V))
5123 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005124 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 {
5126 *errormsgp = (char_u *)_("E172: Only one file name allowed");
5127 return FAIL;
5128 }
5129 }
5130#endif
5131
5132 /*
5133 * Halve the number of backslashes (this is Vi compatible).
5134 * For Unix and OS/2, when wildcards are expanded, this is
5135 * done by ExpandOne() below.
5136 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005137#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 if (!has_wildcards)
5139#endif
5140 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 }
5142
5143 if (has_wildcards)
5144 {
5145 if (n == 1)
5146 {
5147 /*
5148 * First loop: May expand environment variables. This
5149 * can be done much faster with expand_env() than with
5150 * something else (e.g., calling a shell).
5151 * After expanding environment variables, check again
5152 * if there are still wildcards present.
5153 */
5154 if (vim_strchr(eap->arg, '$') != NULL
5155 || vim_strchr(eap->arg, '~') != NULL)
5156 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005157 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005158 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 has_wildcards = mch_has_wildcard(NameBuff);
5160 p = NameBuff;
5161 }
5162 else
5163 p = NULL;
5164 }
5165 else /* n == 2 */
5166 {
5167 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005168 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169
5170 ExpandInit(&xpc);
5171 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005172 if (p_wic)
5173 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005175 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 if (p == NULL)
5177 return FAIL;
5178 }
5179 if (p != NULL)
5180 {
5181 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5182 p, cmdlinep);
5183 if (n == 2) /* p came from ExpandOne() */
5184 vim_free(p);
5185 }
5186 }
5187 }
5188 }
5189 return OK;
5190}
5191
5192/*
5193 * Replace part of the command line, keeping eap->cmd, eap->arg and
5194 * eap->nextcmd correct.
5195 * "src" points to the part that is to be replaced, of length "srclen".
5196 * "repl" is the replacement string.
5197 * Returns a pointer to the character after the replaced string.
5198 * Returns NULL for failure.
5199 */
5200 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005201repl_cmdline(
5202 exarg_T *eap,
5203 char_u *src,
5204 int srclen,
5205 char_u *repl,
5206 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207{
5208 int len;
5209 int i;
5210 char_u *new_cmdline;
5211
5212 /*
5213 * The new command line is build in new_cmdline[].
5214 * First allocate it.
5215 * Careful: a "+cmd" argument may have been NUL terminated.
5216 */
5217 len = (int)STRLEN(repl);
5218 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005219 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5221 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5222 return NULL; /* out of memory! */
5223
5224 /*
5225 * Copy the stuff before the expanded part.
5226 * Copy the expanded stuff.
5227 * Copy what came after the expanded part.
5228 * Copy the next commands, if there are any.
5229 */
5230 i = (int)(src - *cmdlinep); /* length of part before match */
5231 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005232
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 mch_memmove(new_cmdline + i, repl, (size_t)len);
5234 i += len; /* remember the end of the string */
5235 STRCPY(new_cmdline + i, src + srclen);
5236 src = new_cmdline + i; /* remember where to continue */
5237
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005238 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 {
5240 i = (int)STRLEN(new_cmdline) + 1;
5241 STRCPY(new_cmdline + i, eap->nextcmd);
5242 eap->nextcmd = new_cmdline + i;
5243 }
5244 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5245 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5246 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5247 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5248 vim_free(*cmdlinep);
5249 *cmdlinep = new_cmdline;
5250
5251 return src;
5252}
5253
5254/*
5255 * Check for '|' to separate commands and '"' to start comments.
5256 */
5257 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005258separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259{
5260 char_u *p;
5261
Bram Moolenaar86b68352004-12-27 21:59:20 +00005262#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005263 p = skip_grep_pat(eap);
5264#else
5265 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005266#endif
5267
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005268 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 {
5270 if (*p == Ctrl_V)
5271 {
5272 if (eap->argt & (USECTRLV | XFILE))
5273 ++p; /* skip CTRL-V and next char */
5274 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005275 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005276 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 if (*p == NUL) /* stop at NUL after CTRL-V */
5278 break;
5279 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005280
5281#ifdef FEAT_EVAL
5282 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005283 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005284 {
5285 p += 2;
5286 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005287 }
5288#endif
5289
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 /* Check for '"': start of comment or '|': next command */
5291 /* :@" and :*" do not start a comment!
5292 * :redir @" doesn't either. */
5293 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5294 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5295 || p != eap->arg)
5296 && (eap->cmdidx != CMD_redir
5297 || p != eap->arg + 1 || p[-1] != '@'))
5298 || *p == '|' || *p == '\n')
5299 {
5300 /*
5301 * We remove the '\' before the '|', unless USECTRLV is used
5302 * AND 'b' is present in 'cpoptions'.
5303 */
5304 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5305 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5306 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005307 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 --p;
5309 }
5310 else
5311 {
5312 eap->nextcmd = check_nextcmd(p);
5313 *p = NUL;
5314 break;
5315 }
5316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005318
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5320 del_trailing_spaces(eap->arg);
5321}
5322
5323/*
5324 * get + command from ex argument
5325 */
5326 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005327getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328{
5329 char_u *arg = *argp;
5330 char_u *command = NULL;
5331
5332 if (*arg == '+') /* +[command] */
5333 {
5334 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005335 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 command = dollar_command;
5337 else
5338 {
5339 command = arg;
5340 arg = skip_cmd_arg(command, TRUE);
5341 if (*arg != NUL)
5342 *arg++ = NUL; /* terminate command with NUL */
5343 }
5344
5345 arg = skipwhite(arg); /* skip over spaces */
5346 *argp = arg;
5347 }
5348 return command;
5349}
5350
5351/*
5352 * Find end of "+command" argument. Skip over "\ " and "\\".
5353 */
5354 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005355skip_cmd_arg(
5356 char_u *p,
5357 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358{
5359 while (*p && !vim_isspace(*p))
5360 {
5361 if (*p == '\\' && p[1] != NUL)
5362 {
5363 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005364 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 else
5366 ++p;
5367 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005368 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 }
5370 return p;
5371}
5372
5373/*
5374 * Get "++opt=arg" argument.
5375 * Return FAIL or OK.
5376 */
5377 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005378getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379{
5380 char_u *arg = eap->arg + 2;
5381 int *pp = NULL;
5382#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005383 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 char_u *p;
5385#endif
5386
5387 /* ":edit ++[no]bin[ary] file" */
5388 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5389 {
5390 if (*arg == 'n')
5391 {
5392 arg += 2;
5393 eap->force_bin = FORCE_NOBIN;
5394 }
5395 else
5396 eap->force_bin = FORCE_BIN;
5397 if (!checkforcmd(&arg, "binary", 3))
5398 return FAIL;
5399 eap->arg = skipwhite(arg);
5400 return OK;
5401 }
5402
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005403 /* ":read ++edit file" */
5404 if (STRNCMP(arg, "edit", 4) == 0)
5405 {
5406 eap->read_edit = TRUE;
5407 eap->arg = skipwhite(arg + 4);
5408 return OK;
5409 }
5410
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 if (STRNCMP(arg, "ff", 2) == 0)
5412 {
5413 arg += 2;
5414 pp = &eap->force_ff;
5415 }
5416 else if (STRNCMP(arg, "fileformat", 10) == 0)
5417 {
5418 arg += 10;
5419 pp = &eap->force_ff;
5420 }
5421#ifdef FEAT_MBYTE
5422 else if (STRNCMP(arg, "enc", 3) == 0)
5423 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005424 if (STRNCMP(arg, "encoding", 8) == 0)
5425 arg += 8;
5426 else
5427 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 pp = &eap->force_enc;
5429 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005430 else if (STRNCMP(arg, "bad", 3) == 0)
5431 {
5432 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005433 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435#endif
5436
5437 if (pp == NULL || *arg != '=')
5438 return FAIL;
5439
5440 ++arg;
5441 *pp = (int)(arg - eap->cmd);
5442 arg = skip_cmd_arg(arg, FALSE);
5443 eap->arg = skipwhite(arg);
5444 *arg = NUL;
5445
5446#ifdef FEAT_MBYTE
5447 if (pp == &eap->force_ff)
5448 {
5449#endif
5450 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5451 return FAIL;
5452#ifdef FEAT_MBYTE
5453 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005454 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 {
5456 /* Make 'fileencoding' lower case. */
5457 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5458 *p = TOLOWER_ASC(*p);
5459 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005460 else
5461 {
5462 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5463 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005464 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005465 if (STRICMP(p, "keep") == 0)
5466 eap->bad_char = BAD_KEEP;
5467 else if (STRICMP(p, "drop") == 0)
5468 eap->bad_char = BAD_DROP;
5469 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5470 eap->bad_char = *p;
5471 else
5472 return FAIL;
5473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474#endif
5475
5476 return OK;
5477}
5478
5479/*
5480 * ":abbreviate" and friends.
5481 */
5482 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005483ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484{
5485 do_exmap(eap, TRUE); /* almost the same as mapping */
5486}
5487
5488/*
5489 * ":map" and friends.
5490 */
5491 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005492ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493{
5494 /*
5495 * If we are sourcing .exrc or .vimrc in current directory we
5496 * print the mappings for security reasons.
5497 */
5498 if (secure)
5499 {
5500 secure = 2;
5501 msg_outtrans(eap->cmd);
5502 msg_putchar('\n');
5503 }
5504 do_exmap(eap, FALSE);
5505}
5506
5507/*
5508 * ":unmap" and friends.
5509 */
5510 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005511ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512{
5513 do_exmap(eap, FALSE);
5514}
5515
5516/*
5517 * ":mapclear" and friends.
5518 */
5519 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005520ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521{
5522 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5523}
5524
5525/*
5526 * ":abclear" and friends.
5527 */
5528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005529ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530{
5531 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5532}
5533
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005534#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005536ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537{
5538 /*
5539 * Disallow auto commands from .exrc and .vimrc in current
5540 * directory for security reasons.
5541 */
5542 if (secure)
5543 {
5544 secure = 2;
5545 eap->errmsg = e_curdir;
5546 }
5547 else if (eap->cmdidx == CMD_autocmd)
5548 do_autocmd(eap->arg, eap->forceit);
5549 else
5550 do_augroup(eap->arg, eap->forceit);
5551}
5552
5553/*
5554 * ":doautocmd": Apply the automatic commands to the current buffer.
5555 */
5556 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005557ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005559 char_u *arg = eap->arg;
5560 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005561 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005562
Bram Moolenaar1610d052016-06-09 22:53:01 +02005563 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5564 /* Only when there is no <nomodeline>. */
5565 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005566 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567}
5568#endif
5569
5570#ifdef FEAT_LISTCMDS
5571/*
5572 * :[N]bunload[!] [N] [bufname] unload buffer
5573 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5574 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5575 */
5576 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005577ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578{
5579 eap->errmsg = do_bufdel(
5580 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5581 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5582 : DOBUF_UNLOAD, eap->arg,
5583 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5584}
5585
5586/*
5587 * :[N]buffer [N] to buffer N
5588 * :[N]sbuffer [N] to buffer N
5589 */
5590 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005591ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592{
5593 if (*eap->arg)
5594 eap->errmsg = e_trailing;
5595 else
5596 {
5597 if (eap->addr_count == 0) /* default is current buffer */
5598 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5599 else
5600 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005601 if (eap->do_ecmd_cmd != NULL)
5602 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 }
5604}
5605
5606/*
5607 * :[N]bmodified [N] to next mod. buffer
5608 * :[N]sbmodified [N] to next mod. buffer
5609 */
5610 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005611ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612{
5613 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005614 if (eap->do_ecmd_cmd != NULL)
5615 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616}
5617
5618/*
5619 * :[N]bnext [N] to next buffer
5620 * :[N]sbnext [N] split and to next buffer
5621 */
5622 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005623ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624{
5625 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005626 if (eap->do_ecmd_cmd != NULL)
5627 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628}
5629
5630/*
5631 * :[N]bNext [N] to previous buffer
5632 * :[N]bprevious [N] to previous buffer
5633 * :[N]sbNext [N] split and to previous buffer
5634 * :[N]sbprevious [N] split and to previous buffer
5635 */
5636 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005637ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638{
5639 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005640 if (eap->do_ecmd_cmd != NULL)
5641 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642}
5643
5644/*
5645 * :brewind to first buffer
5646 * :bfirst to first buffer
5647 * :sbrewind split and to first buffer
5648 * :sbfirst split and to first buffer
5649 */
5650 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005651ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652{
5653 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005654 if (eap->do_ecmd_cmd != NULL)
5655 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656}
5657
5658/*
5659 * :blast to last buffer
5660 * :sblast split and to last buffer
5661 */
5662 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005663ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664{
5665 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005666 if (eap->do_ecmd_cmd != NULL)
5667 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668}
5669#endif
5670
5671 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005672ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673{
5674 return (c == NUL || c == '|' || c == '"' || c == '\n');
5675}
5676
5677#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5678 || defined(PROTO)
5679/*
5680 * Return the next command, after the first '|' or '\n'.
5681 * Return NULL if not found.
5682 */
5683 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005684find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685{
5686 while (*p != '|' && *p != '\n')
5687 {
5688 if (*p == NUL)
5689 return NULL;
5690 ++p;
5691 }
5692 return (p + 1);
5693}
5694#endif
5695
5696/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005697 * Check if *p is a separator between Ex commands, skipping over white space.
5698 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 */
5700 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005701check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005703 char_u *s = skipwhite(p);
5704
5705 if (*s == '|' || *s == '\n')
5706 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 else
5708 return NULL;
5709}
5710
5711/*
5712 * - if there are more files to edit
5713 * - and this is the last window
5714 * - and forceit not used
5715 * - and not repeated twice on a row
5716 * return FAIL and give error message if 'message' TRUE
5717 * return OK otherwise
5718 */
5719 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005720check_more(
5721 int message, /* when FALSE check only, no messages */
5722 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723{
5724 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5725
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005726 if (!forceit && only_one_window()
5727 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 {
5729 if (message)
5730 {
5731#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5732 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5733 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005734 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735
5736 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005737 vim_strncpy(buff,
5738 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005739 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005741 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 _("%d more files to edit. Quit anyway?"), n);
5743 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5744 return OK;
5745 return FAIL;
5746 }
5747#endif
5748 if (n == 1)
5749 EMSG(_("E173: 1 more file to edit"));
5750 else
5751 EMSGN(_("E173: %ld more files to edit"), n);
5752 quitmore = 2; /* next try to quit is allowed */
5753 }
5754 return FAIL;
5755 }
5756 return OK;
5757}
5758
5759#ifdef FEAT_CMDL_COMPL
5760/*
5761 * Function given to ExpandGeneric() to obtain the list of command names.
5762 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005764get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765{
5766 if (idx >= (int)CMD_SIZE)
5767# ifdef FEAT_USR_CMDS
5768 return get_user_command_name(idx);
5769# else
5770 return NULL;
5771# endif
5772 return cmdnames[idx].cmd_name;
5773}
5774#endif
5775
5776#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005777static 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);
5778static void uc_list(char_u *name, size_t name_len);
5779static 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);
5780static char_u *uc_split_args(char_u *arg, size_t *lenp);
5781static 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 +00005782
5783 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005784uc_add_command(
5785 char_u *name,
5786 size_t name_len,
5787 char_u *rep,
5788 long argt,
5789 long def,
5790 int flags,
5791 int compl,
5792 char_u *compl_arg,
5793 int addr_type,
5794 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795{
5796 ucmd_T *cmd = NULL;
5797 char_u *p;
5798 int i;
5799 int cmp = 1;
5800 char_u *rep_buf = NULL;
5801 garray_T *gap;
5802
Bram Moolenaar9c102382006-05-03 21:26:49 +00005803 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 if (rep_buf == NULL)
5805 {
5806 /* Can't replace termcodes - try using the string as is */
5807 rep_buf = vim_strsave(rep);
5808
5809 /* Give up if out of memory */
5810 if (rep_buf == NULL)
5811 return FAIL;
5812 }
5813
5814 /* get address of growarray: global or in curbuf */
5815 if (flags & UC_BUFFER)
5816 {
5817 gap = &curbuf->b_ucmds;
5818 if (gap->ga_itemsize == 0)
5819 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5820 }
5821 else
5822 gap = &ucmds;
5823
5824 /* Search for the command in the already defined commands. */
5825 for (i = 0; i < gap->ga_len; ++i)
5826 {
5827 size_t len;
5828
5829 cmd = USER_CMD_GA(gap, i);
5830 len = STRLEN(cmd->uc_name);
5831 cmp = STRNCMP(name, cmd->uc_name, name_len);
5832 if (cmp == 0)
5833 {
5834 if (name_len < len)
5835 cmp = -1;
5836 else if (name_len > len)
5837 cmp = 1;
5838 }
5839
5840 if (cmp == 0)
5841 {
5842 if (!force)
5843 {
5844 EMSG(_("E174: Command already exists: add ! to replace it"));
5845 goto fail;
5846 }
5847
5848 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005849 cmd->uc_rep = NULL;
5850#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5851 vim_free(cmd->uc_compl_arg);
5852 cmd->uc_compl_arg = NULL;
5853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 break;
5855 }
5856
5857 /* Stop as soon as we pass the name to add */
5858 if (cmp < 0)
5859 break;
5860 }
5861
5862 /* Extend the array unless we're replacing an existing command */
5863 if (cmp != 0)
5864 {
5865 if (ga_grow(gap, 1) != OK)
5866 goto fail;
5867 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5868 goto fail;
5869
5870 cmd = USER_CMD_GA(gap, i);
5871 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5872
5873 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874
5875 cmd->uc_name = p;
5876 }
5877
5878 cmd->uc_rep = rep_buf;
5879 cmd->uc_argt = argt;
5880 cmd->uc_def = def;
5881 cmd->uc_compl = compl;
5882#ifdef FEAT_EVAL
5883 cmd->uc_scriptID = current_SID;
5884# ifdef FEAT_CMDL_COMPL
5885 cmd->uc_compl_arg = compl_arg;
5886# endif
5887#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005888 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889
5890 return OK;
5891
5892fail:
5893 vim_free(rep_buf);
5894#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5895 vim_free(compl_arg);
5896#endif
5897 return FAIL;
5898}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005901#if defined(FEAT_USR_CMDS)
5902static struct
5903{
5904 int expand;
5905 char *name;
5906} addr_type_complete[] =
5907{
5908 {ADDR_ARGUMENTS, "arguments"},
5909 {ADDR_LINES, "lines"},
5910 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5911 {ADDR_TABS, "tabs"},
5912 {ADDR_BUFFERS, "buffers"},
5913 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005914 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005915 {-1, NULL}
5916};
5917#endif
5918
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005919#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920/*
5921 * List of names for completion for ":command" with the EXPAND_ flag.
5922 * Must be alphabetical for completion.
5923 */
5924static struct
5925{
5926 int expand;
5927 char *name;
5928} command_complete[] =
5929{
5930 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005931 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005933 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005935 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005936#if defined(FEAT_CSCOPE)
5937 {EXPAND_CSCOPE, "cscope"},
5938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5940 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005941 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942#endif
5943 {EXPAND_DIRECTORIES, "dir"},
5944 {EXPAND_ENV_VARS, "environment"},
5945 {EXPAND_EVENTS, "event"},
5946 {EXPAND_EXPRESSION, "expression"},
5947 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005948 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005949 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 {EXPAND_FUNCTIONS, "function"},
5951 {EXPAND_HELP, "help"},
5952 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005953#if defined(FEAT_CMDHIST)
5954 {EXPAND_HISTORY, "history"},
5955#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005956#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005957 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005958 {EXPAND_LOCALES, "locale"},
5959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 {EXPAND_MAPPINGS, "mapping"},
5961 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005962 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005963 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005964#if defined(FEAT_PROFILE)
5965 {EXPAND_SYNTIME, "syntime"},
5966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005968 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005969 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005970#if defined(FEAT_SIGNS)
5971 {EXPAND_SIGN, "sign"},
5972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 {EXPAND_TAGS, "tag"},
5974 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005975 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 {EXPAND_USER_VARS, "var"},
5977 {0, NULL}
5978};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005981#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005983uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984{
5985 int i, j;
5986 int found = FALSE;
5987 ucmd_T *cmd;
5988 int len;
5989 long a;
5990 garray_T *gap;
5991
5992 gap = &curbuf->b_ucmds;
5993 for (;;)
5994 {
5995 for (i = 0; i < gap->ga_len; ++i)
5996 {
5997 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005998 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999
Bram Moolenaard29459b2016-08-26 22:29:11 +02006000 /* Skip commands which don't match the requested prefix and
6001 * commands filtered out. */
6002 if (STRNCMP(name, cmd->uc_name, name_len) != 0
6003 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 continue;
6005
6006 /* Put out the title first time */
6007 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006008 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 found = TRUE;
6010 msg_putchar('\n');
6011 if (got_int)
6012 break;
6013
6014 /* Special cases */
6015 msg_putchar(a & BANG ? '!' : ' ');
6016 msg_putchar(a & REGSTR ? '"' : ' ');
6017 msg_putchar(gap != &ucmds ? 'b' : ' ');
6018 msg_putchar(' ');
6019
Bram Moolenaar8820b482017-03-16 17:23:31 +01006020 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 len = (int)STRLEN(cmd->uc_name) + 4;
6022
6023 do {
6024 msg_putchar(' ');
6025 ++len;
6026 } while (len < 16);
6027
6028 len = 0;
6029
6030 /* Arguments */
6031 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
6032 {
6033 case 0: IObuff[len++] = '0'; break;
6034 case (EXTRA): IObuff[len++] = '*'; break;
6035 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
6036 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
6037 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
6038 }
6039
6040 do {
6041 IObuff[len++] = ' ';
6042 } while (len < 5);
6043
6044 /* Range */
6045 if (a & (RANGE|COUNT))
6046 {
6047 if (a & COUNT)
6048 {
6049 /* -count=N */
6050 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6051 len += (int)STRLEN(IObuff + len);
6052 }
6053 else if (a & DFLALL)
6054 IObuff[len++] = '%';
6055 else if (cmd->uc_def >= 0)
6056 {
6057 /* -range=N */
6058 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6059 len += (int)STRLEN(IObuff + len);
6060 }
6061 else
6062 IObuff[len++] = '.';
6063 }
6064
6065 do {
6066 IObuff[len++] = ' ';
6067 } while (len < 11);
6068
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006069 /* Address Type */
6070 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6071 if (addr_type_complete[j].expand != ADDR_LINES
6072 && addr_type_complete[j].expand == cmd->uc_addr_type)
6073 {
6074 STRCPY(IObuff + len, addr_type_complete[j].name);
6075 len += (int)STRLEN(IObuff + len);
6076 break;
6077 }
6078
6079 do {
6080 IObuff[len++] = ' ';
6081 } while (len < 21);
6082
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 /* Completion */
6084 for (j = 0; command_complete[j].expand != 0; ++j)
6085 if (command_complete[j].expand == cmd->uc_compl)
6086 {
6087 STRCPY(IObuff + len, command_complete[j].name);
6088 len += (int)STRLEN(IObuff + len);
6089 break;
6090 }
6091
6092 do {
6093 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006094 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095
6096 IObuff[len] = '\0';
6097 msg_outtrans(IObuff);
6098
6099 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006100#ifdef FEAT_EVAL
6101 if (p_verbose > 0)
6102 last_set_msg(cmd->uc_scriptID);
6103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 out_flush();
6105 ui_breakcheck();
6106 if (got_int)
6107 break;
6108 }
6109 if (gap == &ucmds || i < gap->ga_len)
6110 break;
6111 gap = &ucmds;
6112 }
6113
6114 if (!found)
6115 MSG(_("No user-defined commands found"));
6116}
6117
6118 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006119uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120{
6121 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6122 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6123 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6124 0xb9, 0x7f, 0};
6125 int i;
6126
6127 for (i = 0; fcmd[i]; ++i)
6128 IObuff[i] = fcmd[i] - 0x40;
6129 IObuff[i] = 0;
6130 return IObuff;
6131}
6132
6133 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006134uc_scan_attr(
6135 char_u *attr,
6136 size_t len,
6137 long *argt,
6138 long *def,
6139 int *flags,
6140 int *compl,
6141 char_u **compl_arg,
6142 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143{
6144 char_u *p;
6145
6146 if (len == 0)
6147 {
6148 EMSG(_("E175: No attribute specified"));
6149 return FAIL;
6150 }
6151
6152 /* First, try the simple attributes (no arguments) */
6153 if (STRNICMP(attr, "bang", len) == 0)
6154 *argt |= BANG;
6155 else if (STRNICMP(attr, "buffer", len) == 0)
6156 *flags |= UC_BUFFER;
6157 else if (STRNICMP(attr, "register", len) == 0)
6158 *argt |= REGSTR;
6159 else if (STRNICMP(attr, "bar", len) == 0)
6160 *argt |= TRLBAR;
6161 else
6162 {
6163 int i;
6164 char_u *val = NULL;
6165 size_t vallen = 0;
6166 size_t attrlen = len;
6167
6168 /* Look for the attribute name - which is the part before any '=' */
6169 for (i = 0; i < (int)len; ++i)
6170 {
6171 if (attr[i] == '=')
6172 {
6173 val = &attr[i + 1];
6174 vallen = len - i - 1;
6175 attrlen = i;
6176 break;
6177 }
6178 }
6179
6180 if (STRNICMP(attr, "nargs", attrlen) == 0)
6181 {
6182 if (vallen == 1)
6183 {
6184 if (*val == '0')
6185 /* Do nothing - this is the default */;
6186 else if (*val == '1')
6187 *argt |= (EXTRA | NOSPC | NEEDARG);
6188 else if (*val == '*')
6189 *argt |= EXTRA;
6190 else if (*val == '?')
6191 *argt |= (EXTRA | NOSPC);
6192 else if (*val == '+')
6193 *argt |= (EXTRA | NEEDARG);
6194 else
6195 goto wrong_nargs;
6196 }
6197 else
6198 {
6199wrong_nargs:
6200 EMSG(_("E176: Invalid number of arguments"));
6201 return FAIL;
6202 }
6203 }
6204 else if (STRNICMP(attr, "range", attrlen) == 0)
6205 {
6206 *argt |= RANGE;
6207 if (vallen == 1 && *val == '%')
6208 *argt |= DFLALL;
6209 else if (val != NULL)
6210 {
6211 p = val;
6212 if (*def >= 0)
6213 {
6214two_count:
6215 EMSG(_("E177: Count cannot be specified twice"));
6216 return FAIL;
6217 }
6218
6219 *def = getdigits(&p);
6220 *argt |= (ZEROR | NOTADR);
6221
6222 if (p != val + vallen || vallen == 0)
6223 {
6224invalid_count:
6225 EMSG(_("E178: Invalid default value for count"));
6226 return FAIL;
6227 }
6228 }
6229 }
6230 else if (STRNICMP(attr, "count", attrlen) == 0)
6231 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006232 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233
6234 if (val != NULL)
6235 {
6236 p = val;
6237 if (*def >= 0)
6238 goto two_count;
6239
6240 *def = getdigits(&p);
6241
6242 if (p != val + vallen)
6243 goto invalid_count;
6244 }
6245
6246 if (*def < 0)
6247 *def = 0;
6248 }
6249 else if (STRNICMP(attr, "complete", attrlen) == 0)
6250 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 if (val == NULL)
6252 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006253 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 return FAIL;
6255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006257 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6258 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006261 else if (STRNICMP(attr, "addr", attrlen) == 0)
6262 {
6263 *argt |= RANGE;
6264 if (val == NULL)
6265 {
6266 EMSG(_("E179: argument required for -addr"));
6267 return FAIL;
6268 }
6269 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6270 == FAIL)
6271 return FAIL;
6272 if (addr_type_arg != ADDR_LINES)
6273 *argt |= (ZEROR | NOTADR) ;
6274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 else
6276 {
6277 char_u ch = attr[len];
6278 attr[len] = '\0';
6279 EMSG2(_("E181: Invalid attribute: %s"), attr);
6280 attr[len] = ch;
6281 return FAIL;
6282 }
6283 }
6284
6285 return OK;
6286}
6287
Bram Moolenaara850a712009-01-28 14:42:59 +00006288/*
6289 * ":command ..."
6290 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006292ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293{
6294 char_u *name;
6295 char_u *end;
6296 char_u *p;
6297 long argt = 0;
6298 long def = -1;
6299 int flags = 0;
6300 int compl = EXPAND_NOTHING;
6301 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006302 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006304 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305
6306 p = eap->arg;
6307
6308 /* Check for attributes */
6309 while (*p == '-')
6310 {
6311 ++p;
6312 end = skiptowhite(p);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006313 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 == FAIL)
6315 return;
6316 p = skipwhite(end);
6317 }
6318
6319 /* Get the name (if any) and skip to the following argument */
6320 name = p;
6321 if (ASCII_ISALPHA(*p))
6322 while (ASCII_ISALNUM(*p))
6323 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006324 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 {
6326 EMSG(_("E182: Invalid command name"));
6327 return;
6328 }
6329 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006330 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331
6332 /* If there is nothing after the name, and no attributes were specified,
6333 * we are listing commands
6334 */
6335 p = skipwhite(end);
6336 if (!has_attr && ends_excmd(*p))
6337 {
6338 uc_list(name, end - name);
6339 }
6340 else if (!ASCII_ISUPPER(*name))
6341 {
6342 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6343 return;
6344 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006345 else if ((name_len == 1 && *name == 'X')
6346 || (name_len <= 4
6347 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6348 {
6349 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6350 return;
6351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 else
6353 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006354 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355}
6356
6357/*
6358 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 * Clear all user commands, global and for current buffer.
6360 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006361 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006362ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363{
6364 uc_clear(&ucmds);
6365 uc_clear(&curbuf->b_ucmds);
6366}
6367
6368/*
6369 * Clear all user commands for "gap".
6370 */
6371 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006372uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373{
6374 int i;
6375 ucmd_T *cmd;
6376
6377 for (i = 0; i < gap->ga_len; ++i)
6378 {
6379 cmd = USER_CMD_GA(gap, i);
6380 vim_free(cmd->uc_name);
6381 vim_free(cmd->uc_rep);
6382# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6383 vim_free(cmd->uc_compl_arg);
6384# endif
6385 }
6386 ga_clear(gap);
6387}
6388
6389 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006390ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391{
6392 int i = 0;
6393 ucmd_T *cmd = NULL;
6394 int cmp = -1;
6395 garray_T *gap;
6396
6397 gap = &curbuf->b_ucmds;
6398 for (;;)
6399 {
6400 for (i = 0; i < gap->ga_len; ++i)
6401 {
6402 cmd = USER_CMD_GA(gap, i);
6403 cmp = STRCMP(eap->arg, cmd->uc_name);
6404 if (cmp <= 0)
6405 break;
6406 }
6407 if (gap == &ucmds || cmp == 0)
6408 break;
6409 gap = &ucmds;
6410 }
6411
6412 if (cmp != 0)
6413 {
6414 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6415 return;
6416 }
6417
6418 vim_free(cmd->uc_name);
6419 vim_free(cmd->uc_rep);
6420# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6421 vim_free(cmd->uc_compl_arg);
6422# endif
6423
6424 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425
6426 if (i < gap->ga_len)
6427 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6428}
6429
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006430/*
6431 * split and quote args for <f-args>
6432 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006434uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435{
6436 char_u *buf;
6437 char_u *p;
6438 char_u *q;
6439 int len;
6440
6441 /* Precalculate length */
6442 p = arg;
6443 len = 2; /* Initial and final quotes */
6444
6445 while (*p)
6446 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006447 if (p[0] == '\\' && p[1] == '\\')
6448 {
6449 len += 2;
6450 p += 2;
6451 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006452 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 {
6454 len += 1;
6455 p += 2;
6456 }
6457 else if (*p == '\\' || *p == '"')
6458 {
6459 len += 2;
6460 p += 1;
6461 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006462 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 {
6464 p = skipwhite(p);
6465 if (*p == NUL)
6466 break;
6467 len += 3; /* "," */
6468 }
6469 else
6470 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006471#ifdef FEAT_MBYTE
6472 int charlen = (*mb_ptr2len)(p);
6473 len += charlen;
6474 p += charlen;
6475#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 ++len;
6477 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 }
6480 }
6481
6482 buf = alloc(len + 1);
6483 if (buf == NULL)
6484 {
6485 *lenp = 0;
6486 return buf;
6487 }
6488
6489 p = arg;
6490 q = buf;
6491 *q++ = '"';
6492 while (*p)
6493 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006494 if (p[0] == '\\' && p[1] == '\\')
6495 {
6496 *q++ = '\\';
6497 *q++ = '\\';
6498 p += 2;
6499 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006500 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 {
6502 *q++ = p[1];
6503 p += 2;
6504 }
6505 else if (*p == '\\' || *p == '"')
6506 {
6507 *q++ = '\\';
6508 *q++ = *p++;
6509 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006510 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 {
6512 p = skipwhite(p);
6513 if (*p == NUL)
6514 break;
6515 *q++ = '"';
6516 *q++ = ',';
6517 *q++ = '"';
6518 }
6519 else
6520 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006521 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 }
6523 }
6524 *q++ = '"';
6525 *q = 0;
6526
6527 *lenp = len;
6528 return buf;
6529}
6530
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006531 static size_t
6532add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6533{
6534 size_t result;
6535
6536 result = STRLEN(mod_str);
6537 if (*multi_mods)
6538 result += 1;
6539 if (buf != NULL)
6540 {
6541 if (*multi_mods)
6542 STRCAT(buf, " ");
6543 STRCAT(buf, mod_str);
6544 }
6545
6546 *multi_mods = 1;
6547
6548 return result;
6549}
6550
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551/*
6552 * Check for a <> code in a user command.
6553 * "code" points to the '<'. "len" the length of the <> (inclusive).
6554 * "buf" is where the result is to be added.
6555 * "split_buf" points to a buffer used for splitting, caller should free it.
6556 * "split_len" is the length of what "split_buf" contains.
6557 * Returns the length of the replacement, which has been added to "buf".
6558 * Returns -1 if there was no match, and only the "<" has been copied.
6559 */
6560 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006561uc_check_code(
6562 char_u *code,
6563 size_t len,
6564 char_u *buf,
6565 ucmd_T *cmd, /* the user command we're expanding */
6566 exarg_T *eap, /* ex arguments */
6567 char_u **split_buf,
6568 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569{
6570 size_t result = 0;
6571 char_u *p = code + 1;
6572 size_t l = len - 2;
6573 int quote = 0;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006574 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_MODS,
6575 ct_REGISTER, ct_LT, ct_NONE } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576
6577 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6578 {
6579 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6580 p += 2;
6581 l -= 2;
6582 }
6583
Bram Moolenaar371d5402006-03-20 21:47:49 +00006584 ++l;
6585 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006587 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006589 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006591 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006593 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006595 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006597 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006599 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006601 else if (STRNICMP(p, "mods>", l) == 0)
6602 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603
6604 switch (type)
6605 {
6606 case ct_ARGS:
6607 /* Simple case first */
6608 if (*eap->arg == NUL)
6609 {
6610 if (quote == 1)
6611 {
6612 result = 2;
6613 if (buf != NULL)
6614 STRCPY(buf, "''");
6615 }
6616 else
6617 result = 0;
6618 break;
6619 }
6620
6621 /* When specified there is a single argument don't split it.
6622 * Works for ":Cmd %" when % is "a b c". */
6623 if ((eap->argt & NOSPC) && quote == 2)
6624 quote = 1;
6625
6626 switch (quote)
6627 {
6628 case 0: /* No quoting, no splitting */
6629 result = STRLEN(eap->arg);
6630 if (buf != NULL)
6631 STRCPY(buf, eap->arg);
6632 break;
6633 case 1: /* Quote, but don't split */
6634 result = STRLEN(eap->arg) + 2;
6635 for (p = eap->arg; *p; ++p)
6636 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006637#ifdef FEAT_MBYTE
6638 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6639 /* DBCS can contain \ in a trail byte, skip the
6640 * double-byte character. */
6641 ++p;
6642 else
6643#endif
6644 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 ++result;
6646 }
6647
6648 if (buf != NULL)
6649 {
6650 *buf++ = '"';
6651 for (p = eap->arg; *p; ++p)
6652 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006653#ifdef FEAT_MBYTE
6654 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6655 /* DBCS can contain \ in a trail byte, copy the
6656 * double-byte character to avoid escaping. */
6657 *buf++ = *p++;
6658 else
6659#endif
6660 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 *buf++ = '\\';
6662 *buf++ = *p;
6663 }
6664 *buf = '"';
6665 }
6666
6667 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006668 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 /* This is hard, so only do it once, and cache the result */
6670 if (*split_buf == NULL)
6671 *split_buf = uc_split_args(eap->arg, split_len);
6672
6673 result = *split_len;
6674 if (buf != NULL && result != 0)
6675 STRCPY(buf, *split_buf);
6676
6677 break;
6678 }
6679 break;
6680
6681 case ct_BANG:
6682 result = eap->forceit ? 1 : 0;
6683 if (quote)
6684 result += 2;
6685 if (buf != NULL)
6686 {
6687 if (quote)
6688 *buf++ = '"';
6689 if (eap->forceit)
6690 *buf++ = '!';
6691 if (quote)
6692 *buf = '"';
6693 }
6694 break;
6695
6696 case ct_LINE1:
6697 case ct_LINE2:
6698 case ct_COUNT:
6699 {
6700 char num_buf[20];
6701 long num = (type == ct_LINE1) ? eap->line1 :
6702 (type == ct_LINE2) ? eap->line2 :
6703 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6704 size_t num_len;
6705
6706 sprintf(num_buf, "%ld", num);
6707 num_len = STRLEN(num_buf);
6708 result = num_len;
6709
6710 if (quote)
6711 result += 2;
6712
6713 if (buf != NULL)
6714 {
6715 if (quote)
6716 *buf++ = '"';
6717 STRCPY(buf, num_buf);
6718 buf += num_len;
6719 if (quote)
6720 *buf = '"';
6721 }
6722
6723 break;
6724 }
6725
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006726 case ct_MODS:
6727 {
6728 int multi_mods = 0;
6729 typedef struct {
6730 int *varp;
6731 char *name;
6732 } mod_entry_T;
6733 static mod_entry_T mod_entries[] = {
6734#ifdef FEAT_BROWSE_CMD
6735 {&cmdmod.browse, "browse"},
6736#endif
6737#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6738 {&cmdmod.confirm, "confirm"},
6739#endif
6740 {&cmdmod.hide, "hide"},
6741 {&cmdmod.keepalt, "keepalt"},
6742 {&cmdmod.keepjumps, "keepjumps"},
6743 {&cmdmod.keepmarks, "keepmarks"},
6744 {&cmdmod.keeppatterns, "keeppatterns"},
6745 {&cmdmod.lockmarks, "lockmarks"},
6746 {&cmdmod.noswapfile, "noswapfile"},
6747 {NULL, NULL}
6748 };
6749 int i;
6750
6751 result = quote ? 2 : 0;
6752 if (buf != NULL)
6753 {
6754 if (quote)
6755 *buf++ = '"';
6756 *buf = '\0';
6757 }
6758
6759#ifdef FEAT_WINDOWS
6760 /* :aboveleft and :leftabove */
6761 if (cmdmod.split & WSP_ABOVE)
6762 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6763 /* :belowright and :rightbelow */
6764 if (cmdmod.split & WSP_BELOW)
6765 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6766 /* :botright */
6767 if (cmdmod.split & WSP_BOT)
6768 result += add_cmd_modifier(buf, "botright", &multi_mods);
6769#endif
6770
6771 /* the modifiers that are simple flags */
6772 for (i = 0; mod_entries[i].varp != NULL; ++i)
6773 if (*mod_entries[i].varp)
6774 result += add_cmd_modifier(buf, mod_entries[i].name,
6775 &multi_mods);
6776
6777 /* TODO: How to support :noautocmd? */
6778#ifdef HAVE_SANDBOX
6779 /* TODO: How to support :sandbox?*/
6780#endif
6781 /* :silent */
6782 if (msg_silent > 0)
6783 result += add_cmd_modifier(buf,
6784 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
6785#ifdef FEAT_WINDOWS
6786 /* :tab */
6787 if (cmdmod.tab > 0)
6788 result += add_cmd_modifier(buf, "tab", &multi_mods);
6789 /* :topleft */
6790 if (cmdmod.split & WSP_TOP)
6791 result += add_cmd_modifier(buf, "topleft", &multi_mods);
6792#endif
6793 /* TODO: How to support :unsilent?*/
6794 /* :verbose */
6795 if (p_verbose > 0)
6796 result += add_cmd_modifier(buf, "verbose", &multi_mods);
6797#ifdef FEAT_WINDOWS
6798 /* :vertical */
6799 if (cmdmod.split & WSP_VERT)
6800 result += add_cmd_modifier(buf, "vertical", &multi_mods);
6801#endif
6802 if (quote && buf != NULL)
6803 {
6804 buf += result - 2;
6805 *buf = '"';
6806 }
6807 break;
6808 }
6809
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 case ct_REGISTER:
6811 result = eap->regname ? 1 : 0;
6812 if (quote)
6813 result += 2;
6814 if (buf != NULL)
6815 {
6816 if (quote)
6817 *buf++ = '\'';
6818 if (eap->regname)
6819 *buf++ = eap->regname;
6820 if (quote)
6821 *buf = '\'';
6822 }
6823 break;
6824
6825 case ct_LT:
6826 result = 1;
6827 if (buf != NULL)
6828 *buf = '<';
6829 break;
6830
6831 default:
6832 /* Not recognized: just copy the '<' and return -1. */
6833 result = (size_t)-1;
6834 if (buf != NULL)
6835 *buf = '<';
6836 break;
6837 }
6838
6839 return result;
6840}
6841
6842 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006843do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844{
6845 char_u *buf;
6846 char_u *p;
6847 char_u *q;
6848
6849 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006850 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006851 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 size_t len, totlen;
6853
6854 size_t split_len = 0;
6855 char_u *split_buf = NULL;
6856 ucmd_T *cmd;
6857#ifdef FEAT_EVAL
6858 scid_T save_current_SID = current_SID;
6859#endif
6860
6861 if (eap->cmdidx == CMD_USER)
6862 cmd = USER_CMD(eap->useridx);
6863 else
6864 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6865
6866 /*
6867 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006868 * First round: "buf" is NULL, compute length, allocate "buf".
6869 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 */
6871 buf = NULL;
6872 for (;;)
6873 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006874 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006875 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006877
6878 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006880 start = vim_strchr(p, '<');
6881 if (start != NULL)
6882 end = vim_strchr(start + 1, '>');
6883 if (buf != NULL)
6884 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006885 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6886 ;
6887 if (*ksp == K_SPECIAL
6888 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006889 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6890# ifdef FEAT_GUI
6891 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6892# endif
6893 ))
6894 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006895 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006896 * KS_SPECIAL KE_FILLER, like for mappings, but
6897 * do_cmdline() doesn't handle that, so convert it back.
6898 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6899 len = ksp - p;
6900 if (len > 0)
6901 {
6902 mch_memmove(q, p, len);
6903 q += len;
6904 }
6905 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6906 p = ksp + 3;
6907 continue;
6908 }
6909 }
6910
6911 /* break if there no <item> is found */
6912 if (start == NULL || end == NULL)
6913 break;
6914
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 /* Include the '>' */
6916 ++end;
6917
6918 /* Take everything up to the '<' */
6919 len = start - p;
6920 if (buf == NULL)
6921 totlen += len;
6922 else
6923 {
6924 mch_memmove(q, p, len);
6925 q += len;
6926 }
6927
6928 len = uc_check_code(start, end - start, q, cmd, eap,
6929 &split_buf, &split_len);
6930 if (len == (size_t)-1)
6931 {
6932 /* no match, continue after '<' */
6933 p = start + 1;
6934 len = 1;
6935 }
6936 else
6937 p = end;
6938 if (buf == NULL)
6939 totlen += len;
6940 else
6941 q += len;
6942 }
6943 if (buf != NULL) /* second time here, finished */
6944 {
6945 STRCPY(q, p);
6946 break;
6947 }
6948
6949 totlen += STRLEN(p); /* Add on the trailing characters */
6950 buf = alloc((unsigned)(totlen + 1));
6951 if (buf == NULL)
6952 {
6953 vim_free(split_buf);
6954 return;
6955 }
6956 }
6957
6958#ifdef FEAT_EVAL
6959 current_SID = cmd->uc_scriptID;
6960#endif
6961 (void)do_cmdline(buf, eap->getline, eap->cookie,
6962 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6963#ifdef FEAT_EVAL
6964 current_SID = save_current_SID;
6965#endif
6966 vim_free(buf);
6967 vim_free(split_buf);
6968}
6969
6970# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6971 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006972get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973{
6974 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6975}
6976
6977/*
6978 * Function given to ExpandGeneric() to obtain the list of user command names.
6979 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006981get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982{
6983 if (idx < curbuf->b_ucmds.ga_len)
6984 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6985 idx -= curbuf->b_ucmds.ga_len;
6986 if (idx < ucmds.ga_len)
6987 return USER_CMD(idx)->uc_name;
6988 return NULL;
6989}
6990
6991/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006992 * Function given to ExpandGeneric() to obtain the list of user address type names.
6993 */
6994 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006995get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006996{
6997 return (char_u *)addr_type_complete[idx].name;
6998}
6999
7000/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 * Function given to ExpandGeneric() to obtain the list of user command
7002 * attributes.
7003 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007005get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006{
7007 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007008 {"addr", "bang", "bar", "buffer", "complete",
7009 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007011 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 return NULL;
7013 return (char_u *)user_cmd_flags[idx];
7014}
7015
7016/*
7017 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
7018 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007020get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021{
7022 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
7023
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007024 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 return NULL;
7026 return (char_u *)user_cmd_nargs[idx];
7027}
7028
7029/*
7030 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7031 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007033get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034{
7035 return (char_u *)command_complete[idx].name;
7036}
7037# endif /* FEAT_CMDL_COMPL */
7038
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007039/*
7040 * Parse address type argument
7041 */
7042 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007043parse_addr_type_arg(
7044 char_u *value,
7045 int vallen,
7046 long *argt,
7047 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007048{
7049 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007050
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007051 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7052 {
7053 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7054 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7055 if (a && b)
7056 {
7057 *addr_type_arg = addr_type_complete[i].expand;
7058 break;
7059 }
7060 }
7061
7062 if (addr_type_complete[i].expand == -1)
7063 {
7064 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007065
Bram Moolenaar1c465442017-03-12 20:10:05 +01007066 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007067 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007068 err[i] = NUL;
7069 EMSG2(_("E180: Invalid address type value: %s"), err);
7070 return FAIL;
7071 }
7072
7073 if (*addr_type_arg != ADDR_LINES)
7074 *argt |= NOTADR;
7075
7076 return OK;
7077}
7078
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079#endif /* FEAT_USR_CMDS */
7080
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007081#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7082/*
7083 * Parse a completion argument "value[vallen]".
7084 * The detected completion goes in "*complp", argument type in "*argt".
7085 * When there is an argument, for function and user defined completion, it's
7086 * copied to allocated memory and stored in "*compl_arg".
7087 * Returns FAIL if something is wrong.
7088 */
7089 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007090parse_compl_arg(
7091 char_u *value,
7092 int vallen,
7093 int *complp,
7094 long *argt,
7095 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007096{
7097 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007098# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007099 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007100# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007101 int i;
7102 int valend = vallen;
7103
7104 /* Look for any argument part - which is the part after any ',' */
7105 for (i = 0; i < vallen; ++i)
7106 {
7107 if (value[i] == ',')
7108 {
7109 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007110# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007111 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007112# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007113 valend = i;
7114 break;
7115 }
7116 }
7117
7118 for (i = 0; command_complete[i].expand != 0; ++i)
7119 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007120 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007121 && STRNCMP(value, command_complete[i].name, valend) == 0)
7122 {
7123 *complp = command_complete[i].expand;
7124 if (command_complete[i].expand == EXPAND_BUFFERS)
7125 *argt |= BUFNAME;
7126 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7127 || command_complete[i].expand == EXPAND_FILES)
7128 *argt |= XFILE;
7129 break;
7130 }
7131 }
7132
7133 if (command_complete[i].expand == 0)
7134 {
7135 EMSG2(_("E180: Invalid complete value: %s"), value);
7136 return FAIL;
7137 }
7138
7139# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7140 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7141 && arg != NULL)
7142# else
7143 if (arg != NULL)
7144# endif
7145 {
7146 EMSG(_("E468: Completion argument only allowed for custom completion"));
7147 return FAIL;
7148 }
7149
7150# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7151 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7152 && arg == NULL)
7153 {
7154 EMSG(_("E467: Custom completion requires a function argument"));
7155 return FAIL;
7156 }
7157
7158 if (arg != NULL)
7159 *compl_arg = vim_strnsave(arg, (int)arglen);
7160# endif
7161 return OK;
7162}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007163
7164 int
7165cmdcomplete_str_to_type(char_u *complete_str)
7166{
7167 int i;
7168
7169 for (i = 0; command_complete[i].expand != 0; ++i)
7170 if (STRCMP(complete_str, command_complete[i].name) == 0)
7171 return command_complete[i].expand;
7172
7173 return EXPAND_NOTHING;
7174}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007175#endif
7176
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007178ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179{
Bram Moolenaare6850792010-05-14 15:28:44 +02007180 if (*eap->arg == NUL)
7181 {
7182#ifdef FEAT_EVAL
7183 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7184 char_u *p = NULL;
7185
7186 if (expr != NULL)
7187 {
7188 ++emsg_off;
7189 p = eval_to_string(expr, NULL, FALSE);
7190 --emsg_off;
7191 vim_free(expr);
7192 }
7193 if (p != NULL)
7194 {
7195 MSG(p);
7196 vim_free(p);
7197 }
7198 else
7199 MSG("default");
7200#else
7201 MSG(_("unknown"));
7202#endif
7203 }
7204 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007205 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206}
7207
7208 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007209ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210{
7211 if (*eap->arg == NUL && eap->cmd[2] == '!')
7212 MSG(_("Greetings, Vim user!"));
7213 do_highlight(eap->arg, eap->forceit, FALSE);
7214}
7215
7216
7217/*
7218 * Call this function if we thought we were going to exit, but we won't
7219 * (because of an error). May need to restore the terminal mode.
7220 */
7221 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007222not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223{
7224 exiting = FALSE;
7225 settmode(TMODE_RAW);
7226}
7227
7228/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007229 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 */
7231 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007232ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233{
Bram Moolenaarf240e182014-11-27 18:33:02 +01007234#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007235 win_T *wp;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007236#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007237
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238#ifdef FEAT_CMDWIN
7239 if (cmdwin_type != 0)
7240 {
7241 cmdwin_result = Ctrl_C;
7242 return;
7243 }
7244#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007245 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007246 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007247 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007248 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007249 return;
7250 }
Bram Moolenaarf240e182014-11-27 18:33:02 +01007251#ifdef FEAT_WINDOWS
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007252 if (eap->addr_count > 0)
7253 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007254 int wnr = eap->line2;
7255
7256 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7257 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007258 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007259 }
7260 else
Bram Moolenaarf240e182014-11-27 18:33:02 +01007261#endif
7262#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007263 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007264#endif
7265
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007266#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02007267 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007268 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02007269 * being closed (can only happen in autocommands). */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007270 if (curbuf_locked() || (wp->w_buffer->b_nwindows == 1
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007271 && wp->w_buffer->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007272 return;
7273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274
7275#ifdef FEAT_NETBEANS_INTG
7276 netbeansForcedQuit = eap->forceit;
7277#endif
7278
7279 /*
7280 * If there are more files or windows we won't exit.
7281 */
7282 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7283 exiting = TRUE;
7284 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007285 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7286 | (eap->forceit ? CCGD_FORCEIT : 0)
7287 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007289 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 {
7291 not_exiting();
7292 }
7293 else
7294 {
7295#ifdef FEAT_WINDOWS
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007296 /* quit last window
7297 * Note: only_one_window() returns true, even so a help window is
7298 * still open. In that case only quit, if no address has been
7299 * specified. Example:
7300 * :h|wincmd w|1q - don't quit
7301 * :h|wincmd w|q - quit
7302 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007303 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304#endif
7305 getout(0);
7306#ifdef FEAT_WINDOWS
7307# ifdef FEAT_GUI
7308 need_mouse_correct = TRUE;
7309# endif
7310 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007311 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312#endif
7313 }
7314}
7315
7316/*
7317 * ":cquit".
7318 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007320ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321{
7322 getout(1); /* this does not always pass on the exit code to the Manx
7323 compiler. why? */
7324}
7325
7326/*
7327 * ":qall": try to quit all windows
7328 */
7329 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007330ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331{
7332# ifdef FEAT_CMDWIN
7333 if (cmdwin_type != 0)
7334 {
7335 if (eap->forceit)
7336 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7337 else
7338 cmdwin_result = K_XF2;
7339 return;
7340 }
7341# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007342
7343 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007344 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007345 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007346 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007347 return;
7348 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007349#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007350 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7351 /* Refuse to quit when locked or when the buffer in the last window is
7352 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007353 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007354 return;
7355#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007356
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007358 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 getout(0);
7360 not_exiting();
7361}
7362
Bram Moolenaard9967712006-03-11 21:18:15 +00007363#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364/*
7365 * ":close": close current window, unless it is the last one
7366 */
7367 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007368ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007370 win_T *win;
7371 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372# ifdef FEAT_CMDWIN
7373 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007374 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 else
7376# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007377 if (!text_locked()
7378#ifdef FEAT_AUTOCMD
7379 && !curbuf_locked()
7380#endif
7381 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007382 {
7383 if (eap->addr_count == 0)
7384 ex_win_close(eap->forceit, curwin, NULL);
7385 else {
Bram Moolenaar29323592016-07-24 22:04:11 +02007386 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007387 {
7388 winnr++;
7389 if (winnr == eap->line2)
7390 break;
7391 }
7392 if (win == NULL)
7393 win = lastwin;
7394 ex_win_close(eap->forceit, win, NULL);
7395 }
7396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397}
7398
Bram Moolenaard9967712006-03-11 21:18:15 +00007399# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007400/*
7401 * ":pclose": Close any preview window.
7402 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007404ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007405{
7406 win_T *win;
7407
Bram Moolenaar29323592016-07-24 22:04:11 +02007408 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007409 if (win->w_p_pvw)
7410 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007411 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007412 break;
7413 }
7414}
Bram Moolenaard9967712006-03-11 21:18:15 +00007415# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007416
Bram Moolenaarf740b292006-02-16 22:11:02 +00007417/*
7418 * Close window "win" and take care of handling closing the last window for a
7419 * modified buffer.
7420 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007421 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007422ex_win_close(
7423 int forceit,
7424 win_T *win,
7425 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426{
7427 int need_hide;
7428 buf_T *buf = win->w_buffer;
7429
7430 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007431 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007433# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 if ((p_confirm || cmdmod.confirm) && p_write)
7435 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007436 bufref_T bufref;
7437
7438 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007440 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 return;
7442 need_hide = FALSE;
7443 }
7444 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007445# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 {
7447 EMSG(_(e_nowrtmsg));
7448 return;
7449 }
7450 }
7451
Bram Moolenaard9967712006-03-11 21:18:15 +00007452# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007454# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007455
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007457 if (tp == NULL)
7458 win_close(win, !need_hide && !P_HID(buf));
7459 else
7460 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461}
7462
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007464 * Handle the argument for a tabpage related ex command.
7465 * Returns a tabpage number.
7466 * When an error is encountered then eap->errmsg is set.
7467 */
7468 static int
7469get_tabpage_arg(exarg_T *eap)
7470{
7471 int tab_number;
7472 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7473
7474 if (eap->arg && *eap->arg != NUL)
7475 {
7476 char_u *p = eap->arg;
7477 char_u *p_save;
7478 int relative = 0; /* argument +N/-N means: go to N places to the
7479 * right/left relative to the current position. */
7480
7481 if (*p == '-')
7482 {
7483 relative = -1;
7484 p++;
7485 }
7486 else if (*p == '+')
7487 {
7488 relative = 1;
7489 p++;
7490 }
7491
7492 p_save = p;
7493 tab_number = getdigits(&p);
7494
7495 if (relative == 0)
7496 {
7497 if (STRCMP(p, "$") == 0)
7498 tab_number = LAST_TAB_NR;
7499 else if (p == p_save || *p_save == '-' || *p != NUL
7500 || tab_number > LAST_TAB_NR)
7501 {
7502 /* No numbers as argument. */
7503 eap->errmsg = e_invarg;
7504 goto theend;
7505 }
7506 }
7507 else
7508 {
7509 if (*p_save == NUL)
7510 tab_number = 1;
7511 else if (p == p_save || *p_save == '-' || *p != NUL
7512 || tab_number == 0)
7513 {
7514 /* No numbers as argument. */
7515 eap->errmsg = e_invarg;
7516 goto theend;
7517 }
7518 tab_number = tab_number * relative + tabpage_index(curtab);
7519 if (!unaccept_arg0 && relative == -1)
7520 --tab_number;
7521 }
7522 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7523 eap->errmsg = e_invarg;
7524 }
7525 else if (eap->addr_count > 0)
7526 {
7527 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007528 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007529 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007530 tab_number = 0;
7531 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007532 else
7533 {
7534 tab_number = eap->line2;
7535 if (!unaccept_arg0 && **eap->cmdlinep == '-')
7536 {
7537 --tab_number;
7538 if (tab_number < unaccept_arg0)
7539 eap->errmsg = e_invarg;
7540 }
7541 }
7542 }
7543 else
7544 {
7545 switch (eap->cmdidx)
7546 {
7547 case CMD_tabnext:
7548 tab_number = tabpage_index(curtab) + 1;
7549 if (tab_number > LAST_TAB_NR)
7550 tab_number = 1;
7551 break;
7552 case CMD_tabmove:
7553 tab_number = LAST_TAB_NR;
7554 break;
7555 default:
7556 tab_number = tabpage_index(curtab);
7557 }
7558 }
7559
7560theend:
7561 return tab_number;
7562}
7563
7564/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007565 * ":tabclose": close current tab page, unless it is the last one.
7566 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 */
7568 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007569ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007571 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007572 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007573
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007574# ifdef FEAT_CMDWIN
7575 if (cmdwin_type != 0)
7576 cmdwin_result = K_IGNORE;
7577 else
7578# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007579 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007580 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007581 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007583 tab_number = get_tabpage_arg(eap);
7584 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007585 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007586 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007587 if (tp == NULL)
7588 {
7589 beep_flush();
7590 return;
7591 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007592 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007593 {
7594 tabpage_close_other(tp, eap->forceit);
7595 return;
7596 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007597 else if (!text_locked()
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007598#ifdef FEAT_AUTOCMD
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007599 && !curbuf_locked()
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007600#endif
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007601 )
7602 tabpage_close(eap->forceit);
7603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007605}
7606
7607/*
7608 * ":tabonly": close all tab pages except the current one
7609 */
7610 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007611ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007612{
7613 tabpage_T *tp;
7614 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007615 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007616
7617# ifdef FEAT_CMDWIN
7618 if (cmdwin_type != 0)
7619 cmdwin_result = K_IGNORE;
7620 else
7621# endif
7622 if (first_tabpage->tp_next == NULL)
7623 MSG(_("Already only one tab page"));
7624 else
7625 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007626 tab_number = get_tabpage_arg(eap);
7627 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007628 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007629 goto_tabpage(tab_number);
7630 /* Repeat this up to a 1000 times, because autocommands may
7631 * mess up the lists. */
7632 for (done = 0; done < 1000; ++done)
7633 {
7634 FOR_ALL_TABPAGES(tp)
7635 if (tp->tp_topframe != topframe)
7636 {
7637 tabpage_close_other(tp, eap->forceit);
7638 /* if we failed to close it quit */
7639 if (valid_tabpage(tp))
7640 done = 1000;
7641 /* start over, "tp" is now invalid */
7642 break;
7643 }
7644 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007645 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007646 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007647 }
7648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650
7651/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007652 * Close the current tab page.
7653 */
7654 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007655tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007656{
7657 /* First close all the windows but the current one. If that worked then
7658 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007659 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007660 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007661 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007662 ex_win_close(forceit, curwin, NULL);
7663# ifdef FEAT_GUI
7664 need_mouse_correct = TRUE;
7665# endif
7666}
7667
7668/*
7669 * Close tab page "tp", which is not the current tab page.
7670 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007671 * Also takes care of the tab pages line disappearing when closing the
7672 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007673 */
7674 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007675tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007676{
7677 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007678 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007679 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007680
7681 /* Limit to 1000 windows, autocommands may add a window while we close
7682 * one. OK, so I'm paranoid... */
7683 while (++done < 1000)
7684 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007685 wp = tp->tp_firstwin;
7686 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007687
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007688 /* Autocommands may delete the tab page under our fingers and we may
7689 * fail to close a window with a modified buffer. */
7690 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007691 break;
7692 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007693
Bram Moolenaar29323592016-07-24 22:04:11 +02007694#ifdef FEAT_AUTOCMD
7695 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
7696#endif
7697
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007698 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007699 if (h != tabline_height())
7700 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007701}
7702
7703/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 * ":only".
7705 */
7706 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007707ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007709 win_T *wp;
7710 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711# ifdef FEAT_GUI
7712 need_mouse_correct = TRUE;
7713# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007714 if (eap->addr_count > 0)
7715 {
7716 wnr = eap->line2;
7717 for (wp = firstwin; --wnr > 0; )
7718 {
7719 if (wp->w_next == NULL)
7720 break;
7721 else
7722 wp = wp->w_next;
7723 }
7724 win_goto(wp);
7725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 close_others(TRUE, eap->forceit);
7727}
7728
7729/*
7730 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007731 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007733 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007734ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735{
7736 if (eap->addr_count == 0)
7737 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007738 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739}
7740#endif /* FEAT_WINDOWS */
7741
7742 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007743ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007745 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746#ifdef FEAT_WINDOWS
Bram Moolenaar2256c992016-11-15 21:17:07 +01007747 if (!eap->skip)
7748 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749# ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007750 need_mouse_correct = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751# endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007752 if (eap->addr_count == 0)
7753 win_close(curwin, FALSE); /* don't free buffer */
7754 else
7755 {
7756 int winnr = 0;
7757 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007758
Bram Moolenaar2256c992016-11-15 21:17:07 +01007759 FOR_ALL_WINDOWS(win)
7760 {
7761 winnr++;
7762 if (winnr == eap->line2)
7763 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007764 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007765 if (win == NULL)
7766 win = lastwin;
7767 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771}
7772
7773/*
7774 * ":stop" and ":suspend": Suspend Vim.
7775 */
7776 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007777ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778{
7779 /*
7780 * Disallow suspending for "rvim".
7781 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007782 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 {
7784 if (!eap->forceit)
7785 autowrite_all();
7786 windgoto((int)Rows - 1, 0);
7787 out_char('\n');
7788 out_flush();
7789 stoptermcap();
7790 out_flush(); /* needed for SUN to restore xterm buffer */
7791#ifdef FEAT_TITLE
7792 mch_restore_title(3); /* restore window titles */
7793#endif
7794 ui_suspend(); /* call machine specific function */
7795#ifdef FEAT_TITLE
7796 maketitle();
7797 resettitle(); /* force updating the title */
7798#endif
7799 starttermcap();
7800 scroll_start(); /* scroll screen before redrawing */
7801 redraw_later_clear();
7802 shell_resized(); /* may have resized window */
7803 }
7804}
7805
7806/*
7807 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7808 */
7809 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007810ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811{
7812#ifdef FEAT_CMDWIN
7813 if (cmdwin_type != 0)
7814 {
7815 cmdwin_result = Ctrl_C;
7816 return;
7817 }
7818#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007819 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007820 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007821 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007822 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007823 return;
7824 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007825#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007826 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7827 /* Refuse to quit when locked or when the buffer in the last window is
7828 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007829 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007830 return;
7831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832
7833 /*
7834 * if more files or windows we won't exit
7835 */
7836 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7837 exiting = TRUE;
7838 if ( ((eap->cmdidx == CMD_wq
7839 || curbufIsChanged())
7840 && do_write(eap) == FAIL)
7841 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007842 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 {
7844 not_exiting();
7845 }
7846 else
7847 {
7848#ifdef FEAT_WINDOWS
7849 if (only_one_window()) /* quit last window, exit Vim */
7850#endif
7851 getout(0);
7852#ifdef FEAT_WINDOWS
7853# ifdef FEAT_GUI
7854 need_mouse_correct = TRUE;
7855# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007856 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 win_close(curwin, !P_HID(curwin->w_buffer));
7858#endif
7859 }
7860}
7861
7862/*
7863 * ":print", ":list", ":number".
7864 */
7865 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007866ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007868 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7869 EMSG(_(e_emptybuf));
7870 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007872 for ( ;!got_int; ui_breakcheck())
7873 {
7874 print_line(eap->line1,
7875 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7876 || (eap->flags & EXFLAG_NR)),
7877 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7878 if (++eap->line1 > eap->line2)
7879 break;
7880 out_flush(); /* show one line at a time */
7881 }
7882 setpcmark();
7883 /* put cursor at last line */
7884 curwin->w_cursor.lnum = eap->line2;
7885 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 }
7887
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889}
7890
7891#ifdef FEAT_BYTEOFF
7892 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007893ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894{
7895 goto_byte(eap->line2);
7896}
7897#endif
7898
7899/*
7900 * ":shell".
7901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007903ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904{
7905 do_shell(NULL, 0);
7906}
7907
7908#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7909 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007910 || defined(FEAT_GUI_MSWIN) \
7911 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 || defined(PROTO)
7913
7914/*
7915 * Handle a file drop. The code is here because a drop is *nearly* like an
7916 * :args command, but not quite (we have a list of exact filenames, so we
7917 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7918 * code is very similar to :args and hence needs access to a lot of the static
7919 * functions in this file.
7920 *
7921 * The list should be allocated using alloc(), as should each item in the
7922 * list. This function takes over responsibility for freeing the list.
7923 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007924 * XXX The list is made into the argument list. This is freed using
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007925 * FreeWild(), which does a series of vim_free() calls.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 */
7927 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007928handle_drop(
7929 int filec, /* the number of files dropped */
7930 char_u **filev, /* the list of files dropped */
7931 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932{
7933 exarg_T ea;
7934 int save_msg_scroll = msg_scroll;
7935
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007936 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007937 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007939#ifdef FEAT_AUTOCMD
7940 if (curbuf_locked())
7941 return;
7942#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007943 /* When the screen is being updated we should not change buffers and
7944 * windows structures, it may cause freed memory to be used. */
7945 if (updating_screen)
7946 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947
7948 /* Check whether the current buffer is changed. If so, we will need
7949 * to split the current window or data could be lost.
7950 * We don't need to check if the 'hidden' option is set, as in this
7951 * case the buffer won't be lost.
7952 */
7953 if (!P_HID(curbuf) && !split)
7954 {
7955 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007956 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 --emsg_off;
7958 }
7959 if (split)
7960 {
7961# ifdef FEAT_WINDOWS
7962 if (win_split(0, 0) == FAIL)
7963 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007964 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965
7966 /* When splitting the window, create a new alist. Otherwise the
7967 * existing one is overwritten. */
7968 alist_unlink(curwin->w_alist);
7969 alist_new();
7970# else
7971 return; /* can't split, always fail */
7972# endif
7973 }
7974
7975 /*
7976 * Set up the new argument list.
7977 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007978 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979
7980 /*
7981 * Move to the first file.
7982 */
7983 /* Fake up a minimal "next" command for do_argfile() */
7984 vim_memset(&ea, 0, sizeof(ea));
7985 ea.cmd = (char_u *)"next";
7986 do_argfile(&ea, 0);
7987
7988 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7989 * mode that is not needed here. */
7990 need_start_insertmode = FALSE;
7991
7992 /* Restore msg_scroll, otherwise a following command may cause scrolling
7993 * unexpectedly. The screen will be redrawn by the caller, thus
7994 * msg_scroll being set by displaying a message is irrelevant. */
7995 msg_scroll = save_msg_scroll;
7996}
7997#endif
7998
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999/*
8000 * Clear an argument list: free all file names and reset it to zero entries.
8001 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008002 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008003alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004{
8005 while (--al->al_ga.ga_len >= 0)
8006 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
8007 ga_clear(&al->al_ga);
8008}
8009
8010/*
8011 * Init an argument list.
8012 */
8013 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008014alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015{
8016 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
8017}
8018
8019#if defined(FEAT_WINDOWS) || defined(PROTO)
8020
8021/*
8022 * Remove a reference from an argument list.
8023 * Ignored when the argument list is the global one.
8024 * If the argument list is no longer used by any window, free it.
8025 */
8026 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008027alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028{
8029 if (al != &global_alist && --al->al_refcount <= 0)
8030 {
8031 alist_clear(al);
8032 vim_free(al);
8033 }
8034}
8035
8036# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
8037/*
8038 * Create a new argument list and use it for the current window.
8039 */
8040 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008041alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042{
8043 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
8044 if (curwin->w_alist == NULL)
8045 {
8046 curwin->w_alist = &global_alist;
8047 ++global_alist.al_refcount;
8048 }
8049 else
8050 {
8051 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008052 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 alist_init(curwin->w_alist);
8054 }
8055}
8056# endif
8057#endif
8058
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008059#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060/*
8061 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008062 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8063 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 */
8065 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008066alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067{
8068 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008069 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 char_u **new_arg_files;
8071 int new_arg_file_count;
8072 char_u *save_p_su = p_su;
8073 int i;
8074
8075 /* Don't use 'suffixes' here. This should work like the shell did the
8076 * expansion. Also, the vimrc file isn't read yet, thus the user
8077 * can't set the options. */
8078 p_su = empty_option;
8079 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8080 if (old_arg_files != NULL)
8081 {
8082 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008083 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8084 old_arg_count = GARGCOUNT;
8085 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008087 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 && new_arg_file_count > 0)
8089 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008090 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8091 TRUE, fnum_list, fnum_len);
8092 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 }
8095 p_su = save_p_su;
8096}
8097#endif
8098
8099/*
8100 * Set the argument list for the current window.
8101 * Takes over the allocated files[] and the allocated fnames in it.
8102 */
8103 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008104alist_set(
8105 alist_T *al,
8106 int count,
8107 char_u **files,
8108 int use_curbuf,
8109 int *fnum_list,
8110 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111{
8112 int i;
8113
8114 alist_clear(al);
8115 if (ga_grow(&al->al_ga, count) == OK)
8116 {
8117 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008118 {
8119 if (got_int)
8120 {
8121 /* When adding many buffers this can take a long time. Allow
8122 * interrupting here. */
8123 while (i < count)
8124 vim_free(files[i++]);
8125 break;
8126 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008127
8128 /* May set buffer name of a buffer previously used for the
8129 * argument list, so that it's re-used by alist_add. */
8130 if (fnum_list != NULL && i < fnum_len)
8131 buf_set_name(fnum_list[i], files[i]);
8132
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008134 ui_breakcheck();
8135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 vim_free(files);
8137 }
8138 else
8139 FreeWild(count, files);
8140#ifdef FEAT_WINDOWS
8141 if (al == &global_alist)
8142#endif
8143 arg_had_last = FALSE;
8144}
8145
8146/*
8147 * Add file "fname" to argument list "al".
8148 * "fname" must have been allocated and "al" must have been checked for room.
8149 */
8150 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008151alist_add(
8152 alist_T *al,
8153 char_u *fname,
8154 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155{
8156 if (fname == NULL) /* don't add NULL file names */
8157 return;
8158#ifdef BACKSLASH_IN_FILENAME
8159 slash_adjust(fname);
8160#endif
8161 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8162 if (set_fnum > 0)
8163 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8164 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8165 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166}
8167
8168#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8169/*
8170 * Adjust slashes in file names. Called after 'shellslash' was set.
8171 */
8172 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008173alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174{
8175 int i;
8176# ifdef FEAT_WINDOWS
8177 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008178 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179# endif
8180
8181 for (i = 0; i < GARGCOUNT; ++i)
8182 if (GARGLIST[i].ae_fname != NULL)
8183 slash_adjust(GARGLIST[i].ae_fname);
8184# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00008185 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 if (wp->w_alist != &global_alist)
8187 for (i = 0; i < WARGCOUNT(wp); ++i)
8188 if (WARGLIST(wp)[i].ae_fname != NULL)
8189 slash_adjust(WARGLIST(wp)[i].ae_fname);
8190# endif
8191}
8192#endif
8193
8194/*
8195 * ":preserve".
8196 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008198ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008200 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 ml_preserve(curbuf, TRUE);
8202}
8203
8204/*
8205 * ":recover".
8206 */
8207 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008208ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209{
8210 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8211 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008212 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8213 | CCGD_MULTWIN
8214 | (eap->forceit ? CCGD_FORCEIT : 0)
8215 | CCGD_EXCMD)
8216
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 && (*eap->arg == NUL
8218 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8219 ml_recover();
8220 recoverymode = FALSE;
8221}
8222
8223/*
8224 * Command modifier used in a wrong way.
8225 */
8226 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008227ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228{
8229 eap->errmsg = e_invcmd;
8230}
8231
8232#ifdef FEAT_WINDOWS
8233/*
8234 * :sview [+command] file split window with new file, read-only
8235 * :split [[+command] file] split window with current or new file
8236 * :vsplit [[+command] file] split window vertically with current or new file
8237 * :new [[+command] file] split window with no or new file
8238 * :vnew [[+command] file] split vertically window with no or new file
8239 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008240 *
8241 * :tabedit open new Tab page with empty window
8242 * :tabedit [+command] file open new Tab page and edit "file"
8243 * :tabnew [[+command] file] just like :tabedit
8244 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 */
8246 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008247ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008249 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008250# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008252# endif
8253# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008255# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008257# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008259# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008261# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008263 * quickfix windows. But it's OK when doing ":tab split". */
8264 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 {
8266 if (eap->cmdidx == CMD_split)
8267 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 if (eap->cmdidx == CMD_vsplit)
8269 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008271# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008273# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008274 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 {
8276 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8277 FNAME_MESS, TRUE, curbuf->b_ffname);
8278 if (fname == NULL)
8279 goto theend;
8280 eap->arg = fname;
8281 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008282# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008284# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008286# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 && eap->cmdidx != CMD_new)
8290 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008291# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008292 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008293# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008294 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008295# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008296 au_has_group((char_u *)"FileExplorer"))
8297 {
8298 /* No browsing supported but we do have the file explorer:
8299 * Edit the directory. */
8300 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8301 eap->arg = (char_u *)".";
8302 }
8303 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008304# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008305 {
8306 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008308 if (fname == NULL)
8309 goto theend;
8310 eap->arg = fname;
8311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 }
8313 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008314# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008316 /*
8317 * Either open new tab page or split the window.
8318 */
8319 if (eap->cmdidx == CMD_tabedit
8320 || eap->cmdidx == CMD_tabfind
8321 || eap->cmdidx == CMD_tabnew)
8322 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008323 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8324 : eap->addr_count == 0 ? 0
8325 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008326 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008327 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008328
8329 /* set the alternate buffer for the window we came from */
8330 if (curwin != old_curwin
8331 && win_valid(old_curwin)
8332 && old_curwin->w_buffer != curbuf
8333 && !cmdmod.keepalt)
8334 old_curwin->w_alt_fnum = curbuf->b_fnum;
8335 }
8336 }
8337 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8339 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008340# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 /* Reset 'scrollbind' when editing another file, but keep it when
8342 * doing ":split" without arguments. */
8343 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008344# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008346# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008348 {
8349 RESET_BINDING(curwin);
8350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 else
8352 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008353# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 do_exedit(eap, old_curwin);
8355 }
8356
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008357# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008359# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008361# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362theend:
8363 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008364# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008366
8367/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008368 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008369 */
8370 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008371tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008372{
8373 exarg_T ea;
8374
8375 vim_memset(&ea, 0, sizeof(ea));
8376 ea.cmdidx = CMD_tabnew;
8377 ea.cmd = (char_u *)"tabn";
8378 ea.arg = (char_u *)"";
8379 ex_splitview(&ea);
8380}
8381
8382/*
8383 * :tabnext command
8384 */
8385 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008386ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008387{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008388 int tab_number;
8389
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008390 switch (eap->cmdidx)
8391 {
8392 case CMD_tabfirst:
8393 case CMD_tabrewind:
8394 goto_tabpage(1);
8395 break;
8396 case CMD_tablast:
8397 goto_tabpage(9999);
8398 break;
8399 case CMD_tabprevious:
8400 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008401 if (eap->arg && *eap->arg != NUL)
8402 {
8403 char_u *p = eap->arg;
8404 char_u *p_save = p;
8405
8406 tab_number = getdigits(&p);
8407 if (p == p_save || *p_save == '-' || *p != NUL
8408 || tab_number == 0)
8409 {
8410 /* No numbers as argument. */
8411 eap->errmsg = e_invarg;
8412 return;
8413 }
8414 }
8415 else
8416 {
8417 if (eap->addr_count == 0)
8418 tab_number = 1;
8419 else
8420 {
8421 tab_number = eap->line2;
8422 if (tab_number < 1)
8423 {
8424 eap->errmsg = e_invrange;
8425 return;
8426 }
8427 }
8428 }
8429 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008430 break;
8431 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008432 tab_number = get_tabpage_arg(eap);
8433 if (eap->errmsg == NULL)
8434 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008435 break;
8436 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008437}
8438
8439/*
8440 * :tabmove command
8441 */
8442 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008443ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008444{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008445 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008446
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008447 tab_number = get_tabpage_arg(eap);
8448 if (eap->errmsg == NULL)
8449 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008450}
8451
8452/*
8453 * :tabs command: List tabs and their contents.
8454 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008455 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008456ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008457{
8458 tabpage_T *tp;
8459 win_T *wp;
8460 int tabcount = 1;
8461
8462 msg_start();
8463 msg_scroll = TRUE;
8464 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8465 {
8466 msg_putchar('\n');
8467 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008468 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008469 out_flush(); /* output one line at a time */
8470 ui_breakcheck();
8471
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008472 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008473 wp = firstwin;
8474 else
8475 wp = tp->tp_firstwin;
8476 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8477 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008478 msg_putchar('\n');
8479 msg_putchar(wp == curwin ? '>' : ' ');
8480 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008481 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8482 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008483 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008484 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008485 else
8486 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8487 IObuff, IOSIZE, TRUE);
8488 msg_outtrans(IObuff);
8489 out_flush(); /* output one line at a time */
8490 ui_breakcheck();
8491 }
8492 }
8493}
8494
8495#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496
8497/*
8498 * ":mode": Set screen mode.
8499 * If no argument given, just get the screen size and redraw.
8500 */
8501 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008502ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503{
8504 if (*eap->arg == NUL)
8505 shell_resized();
8506 else
8507 mch_screenmode(eap->arg);
8508}
8509
8510#ifdef FEAT_WINDOWS
8511/*
8512 * ":resize".
8513 * set, increment or decrement current window height
8514 */
8515 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008516ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517{
8518 int n;
8519 win_T *wp = curwin;
8520
8521 if (eap->addr_count > 0)
8522 {
8523 n = eap->line2;
8524 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8525 ;
8526 }
8527
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008528# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008530# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 if (cmdmod.split & WSP_VERT)
8533 {
8534 if (*eap->arg == '-' || *eap->arg == '+')
8535 n += W_WIDTH(curwin);
8536 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8537 n = 9999;
8538 win_setwidth_win((int)n, wp);
8539 }
8540 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 {
8542 if (*eap->arg == '-' || *eap->arg == '+')
8543 n += curwin->w_height;
8544 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8545 n = 9999;
8546 win_setheight_win((int)n, wp);
8547 }
8548}
8549#endif
8550
8551/*
8552 * ":find [+command] <file>" command.
8553 */
8554 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008555ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556{
8557#ifdef FEAT_SEARCHPATH
8558 char_u *fname;
8559 int count;
8560
8561 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8562 TRUE, curbuf->b_ffname);
8563 if (eap->addr_count > 0)
8564 {
8565 /* Repeat finding the file "count" times. This matters when it
8566 * appears several times in the path. */
8567 count = eap->line2;
8568 while (fname != NULL && --count > 0)
8569 {
8570 vim_free(fname);
8571 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8572 FALSE, curbuf->b_ffname);
8573 }
8574 }
8575
8576 if (fname != NULL)
8577 {
8578 eap->arg = fname;
8579#endif
8580 do_exedit(eap, NULL);
8581#ifdef FEAT_SEARCHPATH
8582 vim_free(fname);
8583 }
8584#endif
8585}
8586
8587/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008588 * ":open" simulation: for now just work like ":visual".
8589 */
8590 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008591ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008592{
8593 regmatch_T regmatch;
8594 char_u *p;
8595
8596 curwin->w_cursor.lnum = eap->line2;
8597 beginline(BL_SOL | BL_FIX);
8598 if (*eap->arg == '/')
8599 {
8600 /* ":open /pattern/": put cursor in column found with pattern */
8601 ++eap->arg;
8602 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8603 *p = NUL;
8604 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8605 if (regmatch.regprog != NULL)
8606 {
8607 regmatch.rm_ic = p_ic;
8608 p = ml_get_curline();
8609 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008610 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008611 else
8612 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008613 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008614 }
8615 /* Move to the NUL, ignore any other arguments. */
8616 eap->arg += STRLEN(eap->arg);
8617 }
8618 check_cursor();
8619
8620 eap->cmdidx = CMD_visual;
8621 do_exedit(eap, NULL);
8622}
8623
8624/*
8625 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 */
8627 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008628ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629{
8630 do_exedit(eap, NULL);
8631}
8632
8633/*
8634 * ":edit <file>" command and alikes.
8635 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008637do_exedit(
8638 exarg_T *eap,
8639 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640{
8641 int n;
8642#ifdef FEAT_WINDOWS
8643 int need_hide;
8644#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008645 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646
8647 /*
8648 * ":vi" command ends Ex mode.
8649 */
8650 if (exmode_active && (eap->cmdidx == CMD_visual
8651 || eap->cmdidx == CMD_view))
8652 {
8653 exmode_active = FALSE;
8654 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008655 {
8656 /* Special case: ":global/pat/visual\NLvi-commands" */
8657 if (global_busy)
8658 {
8659 int rd = RedrawingDisabled;
8660 int nwr = no_wait_return;
8661 int ms = msg_scroll;
8662#ifdef FEAT_GUI
8663 int he = hold_gui_events;
8664#endif
8665
8666 if (eap->nextcmd != NULL)
8667 {
8668 stuffReadbuff(eap->nextcmd);
8669 eap->nextcmd = NULL;
8670 }
8671
8672 if (exmode_was != EXMODE_VIM)
8673 settmode(TMODE_RAW);
8674 RedrawingDisabled = 0;
8675 no_wait_return = 0;
8676 need_wait_return = FALSE;
8677 msg_scroll = 0;
8678#ifdef FEAT_GUI
8679 hold_gui_events = 0;
8680#endif
8681 must_redraw = CLEAR;
8682
8683 main_loop(FALSE, TRUE);
8684
8685 RedrawingDisabled = rd;
8686 no_wait_return = nwr;
8687 msg_scroll = ms;
8688#ifdef FEAT_GUI
8689 hold_gui_events = he;
8690#endif
8691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 }
8695
8696 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008697 || eap->cmdidx == CMD_tabnew
8698 || eap->cmdidx == CMD_tabedit
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008699#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 || eap->cmdidx == CMD_vnew
8701#endif
8702 ) && *eap->arg == NUL)
8703 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008704 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 setpcmark();
8706 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008707 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8708 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 }
8710 else if ((eap->cmdidx != CMD_split
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008711#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 && eap->cmdidx != CMD_vsplit
8713#endif
8714 )
8715 || *eap->arg != NUL
8716#ifdef FEAT_BROWSE
8717 || cmdmod.browse
8718#endif
8719 )
8720 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008721#ifdef FEAT_AUTOCMD
8722 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8723 * can bring us here, others are stopped earlier. */
8724 if (*eap->arg != NUL && curbuf_locked())
8725 return;
8726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 n = readonlymode;
8728 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8729 readonlymode = TRUE;
8730 else if (eap->cmdidx == CMD_enew)
8731 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8732 empty buffer */
8733 setpcmark();
8734 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8735 NULL, eap,
8736 /* ":edit" goes to first line if Vi compatible */
8737 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8738 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8739 ? ECMD_ONE : eap->do_ecmd_lnum,
8740 (P_HID(curbuf) ? ECMD_HIDE : 0)
8741 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008742 /* after a split we can use an existing buffer */
8743 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744#ifdef FEAT_LISTCMDS
8745 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8746#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008747 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 {
8749 /* Editing the file failed. If the window was split, close it. */
8750#ifdef FEAT_WINDOWS
8751 if (old_curwin != NULL)
8752 {
8753 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8754 if (!need_hide || P_HID(curbuf))
8755 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008756# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8757 cleanup_T cs;
8758
8759 /* Reset the error/interrupt/exception state here so that
8760 * aborting() returns FALSE when closing a window. */
8761 enter_cleanup(&cs);
8762# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763# ifdef FEAT_GUI
8764 need_mouse_correct = TRUE;
8765# endif
8766 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008767
8768# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8769 /* Restore the error/interrupt/exception state if not
8770 * discarded by a new aborting error, interrupt, or
8771 * uncaught exception. */
8772 leave_cleanup(&cs);
8773# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 }
8775 }
8776#endif
8777 }
8778 else if (readonlymode && curbuf->b_nwindows == 1)
8779 {
8780 /* When editing an already visited buffer, 'readonly' won't be set
8781 * but the previous value is kept. With ":view" and ":sview" we
8782 * want the file to be readonly, except when another window is
8783 * editing the same buffer. */
8784 curbuf->b_p_ro = TRUE;
8785 }
8786 readonlymode = n;
8787 }
8788 else
8789 {
8790 if (eap->do_ecmd_cmd != NULL)
8791 do_cmdline_cmd(eap->do_ecmd_cmd);
8792#ifdef FEAT_TITLE
8793 n = curwin->w_arg_idx_invalid;
8794#endif
8795 check_arg_idx(curwin);
8796#ifdef FEAT_TITLE
8797 if (n != curwin->w_arg_idx_invalid)
8798 maketitle();
8799#endif
8800 }
8801
8802#ifdef FEAT_WINDOWS
8803 /*
8804 * if ":split file" worked, set alternate file name in old window to new
8805 * file
8806 */
8807 if (old_curwin != NULL
8808 && *eap->arg != NUL
8809 && curwin != old_curwin
8810 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008811 && old_curwin->w_buffer != curbuf
8812 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 old_curwin->w_alt_fnum = curbuf->b_fnum;
8814#endif
8815
8816 ex_no_reprint = TRUE;
8817}
8818
8819#ifndef FEAT_GUI
8820/*
8821 * ":gui" and ":gvim" when there is no GUI.
8822 */
8823 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008824ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825{
8826 eap->errmsg = e_nogvim;
8827}
8828#endif
8829
8830#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8831 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008832ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833{
8834 gui_make_tearoff(eap->arg);
8835}
8836#endif
8837
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008838#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008840ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008842 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843}
8844#endif
8845
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008847ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848{
8849 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8850 MSG(_("No swap file"));
8851 else
8852 msg(curbuf->b_ml.ml_mfp->mf_fname);
8853}
8854
8855/*
8856 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8857 * offset.
8858 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8859 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008861ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862{
8863#ifdef FEAT_SCROLLBIND
8864 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008865 win_T *save_curwin = curwin;
8866 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 long topline;
8868 long y;
8869 linenr_T old_linenr = curwin->w_cursor.lnum;
8870
8871 setpcmark();
8872
8873 /*
8874 * determine max topline
8875 */
8876 if (curwin->w_p_scb)
8877 {
8878 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008879 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 {
8881 if (wp->w_p_scb && wp->w_buffer)
8882 {
8883 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8884 if (topline > y)
8885 topline = y;
8886 }
8887 }
8888 if (topline < 1)
8889 topline = 1;
8890 }
8891 else
8892 {
8893 topline = 1;
8894 }
8895
8896
8897 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008898 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008900 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 {
8902 if (curwin->w_p_scb)
8903 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008904 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 y = topline - curwin->w_topline;
8906 if (y > 0)
8907 scrollup(y, TRUE);
8908 else
8909 scrolldown(-y, TRUE);
8910 curwin->w_scbind_pos = topline;
8911 redraw_later(VALID);
8912 cursor_correct();
8913#ifdef FEAT_WINDOWS
8914 curwin->w_redr_status = TRUE;
8915#endif
8916 }
8917 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008918 curwin = save_curwin;
8919 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 if (curwin->w_p_scb)
8921 {
8922 did_syncbind = TRUE;
8923 checkpcmark();
8924 if (old_linenr != curwin->w_cursor.lnum)
8925 {
8926 char_u ctrl_o[2];
8927
8928 ctrl_o[0] = Ctrl_O;
8929 ctrl_o[1] = 0;
8930 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8931 }
8932 }
8933#endif
8934}
8935
8936
8937 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008938ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008940 int i;
8941 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8942 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943
8944 if (eap->usefilter) /* :r!cmd */
8945 do_bang(1, eap, FALSE, FALSE, TRUE);
8946 else
8947 {
8948 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8949 return;
8950
8951#ifdef FEAT_BROWSE
8952 if (cmdmod.browse)
8953 {
8954 char_u *browseFile;
8955
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008956 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 NULL, NULL, NULL, curbuf);
8958 if (browseFile != NULL)
8959 {
8960 i = readfile(browseFile, NULL,
8961 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8962 vim_free(browseFile);
8963 }
8964 else
8965 i = OK;
8966 }
8967 else
8968#endif
8969 if (*eap->arg == NUL)
8970 {
8971 if (check_fname() == FAIL) /* check for no file name */
8972 return;
8973 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8974 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8975 }
8976 else
8977 {
8978 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8979 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8980 i = readfile(eap->arg, NULL,
8981 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8982
8983 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01008984 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 {
8986#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8987 if (!aborting())
8988#endif
8989 EMSG2(_(e_notopen), eap->arg);
8990 }
8991 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008992 {
8993 if (empty && exmode_active)
8994 {
8995 /* Delete the empty line that remains. Historically ex does
8996 * this but vi doesn't. */
8997 if (eap->line2 == 0)
8998 lnum = curbuf->b_ml.ml_line_count;
8999 else
9000 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009001 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009002 {
9003 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009004 if (curwin->w_cursor.lnum > 1
9005 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009006 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00009007 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009008 }
9009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 }
9013}
9014
Bram Moolenaarea408852005-06-25 22:49:46 +00009015static char_u *prev_dir = NULL;
9016
9017#if defined(EXITFREE) || defined(PROTO)
9018 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009019free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00009020{
9021 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00009022 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00009023
9024 vim_free(globaldir);
9025 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00009026}
9027#endif
9028
Bram Moolenaarf4258302013-06-02 18:20:17 +02009029/*
9030 * Deal with the side effects of changing the current directory.
9031 * When "local" is TRUE then this was after an ":lcd" command.
9032 */
9033 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009034post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02009035{
9036 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01009037 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02009038 if (local)
9039 {
9040 /* If still in global directory, need to remember current
9041 * directory as global directory. */
9042 if (globaldir == NULL && prev_dir != NULL)
9043 globaldir = vim_strsave(prev_dir);
9044 /* Remember this local directory for the window. */
9045 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9046 curwin->w_localdir = vim_strsave(NameBuff);
9047 }
9048 else
9049 {
9050 /* We are now in the global directory, no need to remember its
9051 * name. */
9052 vim_free(globaldir);
9053 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02009054 }
9055
9056 shorten_fnames(TRUE);
9057}
9058
Bram Moolenaarea408852005-06-25 22:49:46 +00009059
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060/*
9061 * ":cd", ":lcd", ":chdir" and ":lchdir".
9062 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00009063 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009064ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 char_u *new_dir;
9067 char_u *tofree;
9068
9069 new_dir = eap->arg;
9070#if !defined(UNIX) && !defined(VMS)
9071 /* for non-UNIX ":cd" means: print current directory */
9072 if (*new_dir == NUL)
9073 ex_pwd(NULL);
9074 else
9075#endif
9076 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009077#ifdef FEAT_AUTOCMD
9078 if (allbuf_locked())
9079 return;
9080#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009081 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9082 && !eap->forceit)
9083 {
Bram Moolenaar81870892007-11-11 18:17:28 +00009084 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009085 return;
9086 }
9087
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 /* ":cd -": Change to previous directory */
9089 if (STRCMP(new_dir, "-") == 0)
9090 {
9091 if (prev_dir == NULL)
9092 {
9093 EMSG(_("E186: No previous directory"));
9094 return;
9095 }
9096 new_dir = prev_dir;
9097 }
9098
9099 /* Save current directory for next ":cd -" */
9100 tofree = prev_dir;
9101 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9102 prev_dir = vim_strsave(NameBuff);
9103 else
9104 prev_dir = NULL;
9105
9106#if defined(UNIX) || defined(VMS)
9107 /* for UNIX ":cd" means: go to home directory */
9108 if (*new_dir == NUL)
9109 {
9110 /* use NameBuff for home directory name */
9111# ifdef VMS
9112 char_u *p;
9113
9114 p = mch_getenv((char_u *)"SYS$LOGIN");
9115 if (p == NULL || *p == NUL) /* empty is the same as not set */
9116 NameBuff[0] = NUL;
9117 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009118 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119# else
9120 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9121# endif
9122 new_dir = NameBuff;
9123 }
9124#endif
9125 if (new_dir == NULL || vim_chdir(new_dir))
9126 EMSG(_(e_failed));
9127 else
9128 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02009129 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130
9131 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009132 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 ex_pwd(eap);
9134 }
9135 vim_free(tofree);
9136 }
9137}
9138
9139/*
9140 * ":pwd".
9141 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009143ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144{
9145 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9146 {
9147#ifdef BACKSLASH_IN_FILENAME
9148 slash_adjust(NameBuff);
9149#endif
9150 msg(NameBuff);
9151 }
9152 else
9153 EMSG(_("E187: Unknown"));
9154}
9155
9156/*
9157 * ":=".
9158 */
9159 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009160ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161{
9162 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009163 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164}
9165
9166 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009167ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009169 int n;
9170 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171
9172 if (cursor_valid())
9173 {
9174 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9175 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01009176 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009178
9179 len = eap->line2;
9180 switch (*eap->arg)
9181 {
9182 case 'm': break;
9183 case NUL: len *= 1000L; break;
9184 default: EMSG2(_(e_invarg2), eap->arg); return;
9185 }
9186 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187}
9188
9189/*
9190 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9191 */
9192 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009193do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194{
9195 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009196 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197
9198 cursor_on();
9199 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009200 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009202 wait_now = msec - done > 1000L ? 1000L : msec - done;
9203#ifdef FEAT_TIMERS
9204 {
9205 long due_time = check_due_timer();
9206
9207 if (due_time > 0 && due_time < wait_now)
9208 wait_now = due_time;
9209 }
9210#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009211#ifdef FEAT_JOB_CHANNEL
9212 if (has_any_channel() && wait_now > 100L)
9213 wait_now = 100L;
9214#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009215 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009216#ifdef FEAT_JOB_CHANNEL
9217 if (has_any_channel())
9218 ui_breakcheck_force(TRUE);
9219 else
9220#endif
9221 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009222#ifdef MESSAGE_QUEUE
9223 /* Process the netbeans and clientserver messages that may have been
9224 * received in the call to ui_breakcheck() when the GUI is in use. This
9225 * may occur when running a test case. */
9226 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 }
9229}
9230
9231 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009232do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233{
9234 int mode;
9235 char_u *cmdp;
9236
9237 cmdp = eap->cmd;
9238 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9239
9240 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9241 eap->arg, mode, isabbrev))
9242 {
9243 case 1: EMSG(_(e_invarg));
9244 break;
9245 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9246 break;
9247 }
9248}
9249
9250/*
9251 * ":winsize" command (obsolete).
9252 */
9253 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009254ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255{
9256 int w, h;
9257 char_u *arg = eap->arg;
9258 char_u *p;
9259
9260 w = getdigits(&arg);
9261 arg = skipwhite(arg);
9262 p = arg;
9263 h = getdigits(&arg);
9264 if (*p != NUL && *arg == NUL)
9265 set_shellsize(w, h, TRUE);
9266 else
9267 EMSG(_("E465: :winsize requires two number arguments"));
9268}
9269
9270#ifdef FEAT_WINDOWS
9271 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009272ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273{
9274 int xchar = NUL;
9275 char_u *p;
9276
9277 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9278 {
9279 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9280 if (eap->arg[1] == NUL)
9281 {
9282 EMSG(_(e_invarg));
9283 return;
9284 }
9285 xchar = eap->arg[1];
9286 p = eap->arg + 2;
9287 }
9288 else
9289 p = eap->arg + 1;
9290
9291 eap->nextcmd = check_nextcmd(p);
9292 p = skipwhite(p);
9293 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9294 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009295 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 {
9297 /* Pass flags on for ":vertical wincmd ]". */
9298 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009299 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9301 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009302 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303 }
9304}
9305#endif
9306
Bram Moolenaar843ee412004-06-30 16:16:41 +00009307#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308/*
9309 * ":winpos".
9310 */
9311 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009312ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313{
9314 int x, y;
9315 char_u *arg = eap->arg;
9316 char_u *p;
9317
9318 if (*arg == NUL)
9319 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009320# if defined(FEAT_GUI) || defined(MSWIN)
9321# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009323# else
9324 if (mch_get_winpos(&x, &y) != FAIL)
9325# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 {
9327 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9328 msg(IObuff);
9329 }
9330 else
9331# endif
9332 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9333 }
9334 else
9335 {
9336 x = getdigits(&arg);
9337 arg = skipwhite(arg);
9338 p = arg;
9339 y = getdigits(&arg);
9340 if (*p == NUL || *arg != NUL)
9341 {
9342 EMSG(_("E466: :winpos requires two number arguments"));
9343 return;
9344 }
9345# ifdef FEAT_GUI
9346 if (gui.in_use)
9347 gui_mch_set_winpos(x, y);
9348 else if (gui.starting)
9349 {
9350 /* Remember the coordinates for when the window is opened. */
9351 gui_win_x = x;
9352 gui_win_y = y;
9353 }
9354# ifdef HAVE_TGETENT
9355 else
9356# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009357# else
9358# ifdef MSWIN
9359 mch_set_winpos(x, y);
9360# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361# endif
9362# ifdef HAVE_TGETENT
9363 if (*T_CWP)
9364 term_set_winpos(x, y);
9365# endif
9366 }
9367}
9368#endif
9369
9370/*
9371 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9372 */
9373 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009374ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375{
9376 oparg_T oa;
9377
9378 clear_oparg(&oa);
9379 oa.regname = eap->regname;
9380 oa.start.lnum = eap->line1;
9381 oa.end.lnum = eap->line2;
9382 oa.line_count = eap->line2 - eap->line1 + 1;
9383 oa.motion_type = MLINE;
9384#ifdef FEAT_VIRTUALEDIT
9385 virtual_op = FALSE;
9386#endif
9387 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9388 {
9389 setpcmark();
9390 curwin->w_cursor.lnum = eap->line1;
9391 beginline(BL_SOL | BL_FIX);
9392 }
9393
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009394 if (VIsual_active)
9395 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009396
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 switch (eap->cmdidx)
9398 {
9399 case CMD_delete:
9400 oa.op_type = OP_DELETE;
9401 op_delete(&oa);
9402 break;
9403
9404 case CMD_yank:
9405 oa.op_type = OP_YANK;
9406 (void)op_yank(&oa, FALSE, TRUE);
9407 break;
9408
9409 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009410 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009412 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9413#else
9414 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009416 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 oa.op_type = OP_RSHIFT;
9418 else
9419 oa.op_type = OP_LSHIFT;
9420 op_shift(&oa, FALSE, eap->amount);
9421 break;
9422 }
9423#ifdef FEAT_VIRTUALEDIT
9424 virtual_op = MAYBE;
9425#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009426 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427}
9428
9429/*
9430 * ":put".
9431 */
9432 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009433ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434{
9435 /* ":0put" works like ":1put!". */
9436 if (eap->line2 == 0)
9437 {
9438 eap->line2 = 1;
9439 eap->forceit = TRUE;
9440 }
9441 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009442 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9443 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444}
9445
9446/*
9447 * Handle ":copy" and ":move".
9448 */
9449 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009450ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451{
9452 long n;
9453
Bram Moolenaarded27822017-01-02 14:27:34 +01009454 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 if (eap->arg == NULL) /* error detected */
9456 {
9457 eap->nextcmd = NULL;
9458 return;
9459 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009460 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461
9462 /*
9463 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9464 */
9465 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9466 {
9467 EMSG(_(e_invaddr));
9468 return;
9469 }
9470
9471 if (eap->cmdidx == CMD_move)
9472 {
9473 if (do_move(eap->line1, eap->line2, n) == FAIL)
9474 return;
9475 }
9476 else
9477 ex_copy(eap->line1, eap->line2, n);
9478 u_clearline();
9479 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009480 ex_may_print(eap);
9481}
9482
9483/*
9484 * Print the current line if flags were given to the Ex command.
9485 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009486 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009487ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009488{
9489 if (eap->flags != 0)
9490 {
9491 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9492 (eap->flags & EXFLAG_LIST));
9493 ex_no_reprint = TRUE;
9494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495}
9496
9497/*
9498 * ":smagic" and ":snomagic".
9499 */
9500 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009501ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502{
9503 int magic_save = p_magic;
9504
9505 p_magic = (eap->cmdidx == CMD_smagic);
9506 do_sub(eap);
9507 p_magic = magic_save;
9508}
9509
9510/*
9511 * ":join".
9512 */
9513 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009514ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515{
9516 curwin->w_cursor.lnum = eap->line1;
9517 if (eap->line1 == eap->line2)
9518 {
9519 if (eap->addr_count >= 2) /* :2,2join does nothing */
9520 return;
9521 if (eap->line2 == curbuf->b_ml.ml_line_count)
9522 {
9523 beep_flush();
9524 return;
9525 }
9526 ++eap->line2;
9527 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009528 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009530 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531}
9532
9533/*
9534 * ":[addr]@r" or ":[addr]*r": execute register
9535 */
9536 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009537ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538{
9539 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009540 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541
9542 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009543 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544
9545#ifdef USE_ON_FLY_SCROLL
9546 dont_scroll = TRUE; /* disallow scrolling here */
9547#endif
9548
9549 /* get the register name. No name means to use the previous one */
9550 c = *eap->arg;
9551 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9552 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009553 /* Put the register in the typeahead buffer with the "silent" flag. */
9554 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9555 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009556 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 else
9560 {
9561 int save_efr = exec_from_reg;
9562
9563 exec_from_reg = TRUE;
9564
9565 /*
9566 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009567 * Continue until the stuff buffer is empty and all added characters
9568 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009570 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9572
9573 exec_from_reg = save_efr;
9574 }
9575}
9576
9577/*
9578 * ":!".
9579 */
9580 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009581ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582{
9583 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9584}
9585
9586/*
9587 * ":undo".
9588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009590ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009592 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009593 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009594 else
9595 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596}
9597
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009598#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009599 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009600ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009601{
9602 char_u hash[UNDO_HASH_SIZE];
9603
9604 u_compute_hash(hash);
9605 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9606}
9607
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009608 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009609ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009610{
9611 char_u hash[UNDO_HASH_SIZE];
9612
9613 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009614 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009615}
9616#endif
9617
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618/*
9619 * ":redo".
9620 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009622ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623{
9624 u_redo(1);
9625}
9626
9627/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009628 * ":earlier" and ":later".
9629 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009630 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009631ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009632{
9633 long count = 0;
9634 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009635 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009636 char_u *p = eap->arg;
9637
9638 if (*p == NUL)
9639 count = 1;
9640 else if (isdigit(*p))
9641 {
9642 count = getdigits(&p);
9643 switch (*p)
9644 {
9645 case 's': ++p; sec = TRUE; break;
9646 case 'm': ++p; sec = TRUE; count *= 60; break;
9647 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009648 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9649 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009650 }
9651 }
9652
9653 if (*p != NUL)
9654 EMSG2(_(e_invarg2), eap->arg);
9655 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009656 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9657 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009658}
9659
9660/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661 * ":redir": start/stop redirection.
9662 */
9663 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009664ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665{
9666 char *mode;
9667 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009668 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669
Bram Moolenaarba768492016-07-08 15:32:54 +02009670#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009671 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009672 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009673 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009674 return;
9675 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009676#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009677
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 if (STRICMP(eap->arg, "END") == 0)
9679 close_redir();
9680 else
9681 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009682 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009684 ++arg;
9685 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009687 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 mode = "a";
9689 }
9690 else
9691 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009692 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693
9694 close_redir();
9695
9696 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009697 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 if (fname == NULL)
9699 return;
9700#ifdef FEAT_BROWSE
9701 if (cmdmod.browse)
9702 {
9703 char_u *browseFile;
9704
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009705 browseFile = do_browse(BROWSE_SAVE,
9706 (char_u *)_("Save Redirection"),
9707 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 if (browseFile == NULL)
9709 return; /* operation cancelled */
9710 vim_free(fname);
9711 fname = browseFile;
9712 eap->forceit = TRUE; /* since dialog already asked */
9713 }
9714#endif
9715
9716 redir_fd = open_exfile(fname, eap->forceit, mode);
9717 vim_free(fname);
9718 }
9719#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009720 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 {
9722 /* redirect to a register a-z (resp. A-Z for appending) */
9723 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009724 ++arg;
9725 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009727 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009728 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009730 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009732 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009733 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009734 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009735 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009737 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009738 if (*arg == '>')
9739 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009740 /* Make register empty when not using @A-@Z and the
9741 * command is valid. */
9742 if (*arg == NUL && !isupper(redir_reg))
9743 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009745 }
9746 if (*arg != NUL)
9747 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009748 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009749 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009750 }
9751 }
9752 else if (*arg == '=' && arg[1] == '>')
9753 {
9754 int append;
9755
9756 /* redirect to a variable */
9757 close_redir();
9758 arg += 2;
9759
9760 if (*arg == '>')
9761 {
9762 ++arg;
9763 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 }
9765 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009766 append = FALSE;
9767
9768 if (var_redir_start(skipwhite(arg), append) == OK)
9769 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 }
9771#endif
9772
9773 /* TODO: redirect to a buffer */
9774
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009776 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009778
9779 /* Make sure redirection is not off. Can happen for cmdline completion
9780 * that indirectly invokes a command to catch its output. */
9781 if (redir_fd != NULL
9782#ifdef FEAT_EVAL
9783 || redir_reg || redir_vname
9784#endif
9785 )
9786 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787}
9788
9789/*
9790 * ":redraw": force redraw
9791 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009792 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009793ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794{
9795 int r = RedrawingDisabled;
9796 int p = p_lz;
9797
9798 RedrawingDisabled = 0;
9799 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009800 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009802 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803#ifdef FEAT_TITLE
9804 if (need_maketitle)
9805 maketitle();
9806#endif
9807 RedrawingDisabled = r;
9808 p_lz = p;
9809
9810 /* Reset msg_didout, so that a message that's there is overwritten. */
9811 msg_didout = FALSE;
9812 msg_col = 0;
9813
Bram Moolenaar56667a52013-07-17 11:54:28 +02009814 /* No need to wait after an intentional redraw. */
9815 need_wait_return = FALSE;
9816
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 out_flush();
9818}
9819
9820/*
9821 * ":redrawstatus": force redraw of status line(s)
9822 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009824ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825{
9826#if defined(FEAT_WINDOWS)
9827 int r = RedrawingDisabled;
9828 int p = p_lz;
9829
9830 RedrawingDisabled = 0;
9831 p_lz = FALSE;
9832 if (eap->forceit)
9833 status_redraw_all();
9834 else
9835 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009836 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837 RedrawingDisabled = r;
9838 p_lz = p;
9839 out_flush();
9840#endif
9841}
9842
9843 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009844close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845{
9846 if (redir_fd != NULL)
9847 {
9848 fclose(redir_fd);
9849 redir_fd = NULL;
9850 }
9851#ifdef FEAT_EVAL
9852 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009853 if (redir_vname)
9854 {
9855 var_redir_stop();
9856 redir_vname = 0;
9857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858#endif
9859}
9860
9861#if defined(FEAT_SESSION) && defined(USE_CRNL)
9862# define MKSESSION_NL
9863static int mksession_nl = FALSE; /* use NL only in put_eol() */
9864#endif
9865
9866/*
9867 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9868 */
9869 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009870ex_mkrc(
9871 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872{
9873 FILE *fd;
9874 int failed = FALSE;
9875 char_u *fname;
9876#ifdef FEAT_BROWSE
9877 char_u *browseFile = NULL;
9878#endif
9879#ifdef FEAT_SESSION
9880 int view_session = FALSE;
9881 int using_vdir = FALSE; /* using 'viewdir'? */
9882 char_u *viewFile = NULL;
9883 unsigned *flagp;
9884#endif
9885
9886 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9887 {
9888#ifdef FEAT_SESSION
9889 view_session = TRUE;
9890#else
9891 ex_ni(eap);
9892 return;
9893#endif
9894 }
9895
9896#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009897 /* Use the short file name until ":lcd" is used. We also don't use the
9898 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009899 did_lcd = FALSE;
9900
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9902 if (eap->cmdidx == CMD_mkview
9903 && (*eap->arg == NUL
9904 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9905 {
9906 eap->forceit = TRUE;
9907 fname = get_view_file(*eap->arg);
9908 if (fname == NULL)
9909 return;
9910 viewFile = fname;
9911 using_vdir = TRUE;
9912 }
9913 else
9914#endif
9915 if (*eap->arg != NUL)
9916 fname = eap->arg;
9917 else if (eap->cmdidx == CMD_mkvimrc)
9918 fname = (char_u *)VIMRC_FILE;
9919#ifdef FEAT_SESSION
9920 else if (eap->cmdidx == CMD_mksession)
9921 fname = (char_u *)SESSION_FILE;
9922#endif
9923 else
9924 fname = (char_u *)EXRC_FILE;
9925
9926#ifdef FEAT_BROWSE
9927 if (cmdmod.browse)
9928 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009929 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930# ifdef FEAT_SESSION
9931 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9932 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9933# endif
9934 (char_u *)_("Save Setup"),
9935 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9936 if (browseFile == NULL)
9937 goto theend;
9938 fname = browseFile;
9939 eap->forceit = TRUE; /* since dialog already asked */
9940 }
9941#endif
9942
9943#if defined(FEAT_SESSION) && defined(vim_mkdir)
9944 /* When using 'viewdir' may have to create the directory. */
9945 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009946 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947#endif
9948
9949 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9950 if (fd != NULL)
9951 {
9952#ifdef FEAT_SESSION
9953 if (eap->cmdidx == CMD_mkview)
9954 flagp = &vop_flags;
9955 else
9956 flagp = &ssop_flags;
9957#endif
9958
9959#ifdef MKSESSION_NL
9960 /* "unix" in 'sessionoptions': use NL line separator */
9961 if (view_session && (*flagp & SSOP_UNIX))
9962 mksession_nl = TRUE;
9963#endif
9964
9965 /* Write the version command for :mkvimrc */
9966 if (eap->cmdidx == CMD_mkvimrc)
9967 (void)put_line(fd, "version 6.0");
9968
9969#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009970 if (eap->cmdidx == CMD_mksession)
9971 {
9972 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9973 failed = TRUE;
9974 }
9975
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 if (eap->cmdidx != CMD_mkview)
9977#endif
9978 {
9979 /* Write setting 'compatible' first, because it has side effects.
9980 * For that same reason only do it when needed. */
9981 if (p_cp)
9982 (void)put_line(fd, "if !&cp | set cp | endif");
9983 else
9984 (void)put_line(fd, "if &cp | set nocp | endif");
9985 }
9986
9987#ifdef FEAT_SESSION
9988 if (!view_session
9989 || (eap->cmdidx == CMD_mksession
9990 && (*flagp & SSOP_OPTIONS)))
9991#endif
9992 failed |= (makemap(fd, NULL) == FAIL
9993 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9994
9995#ifdef FEAT_SESSION
9996 if (!failed && view_session)
9997 {
9998 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9999 failed = TRUE;
10000 if (eap->cmdidx == CMD_mksession)
10001 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010002 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003
Bram Moolenaard9462e32011-04-11 21:35:11 +020010004 dirnow = alloc(MAXPATHL);
10005 if (dirnow == NULL)
10006 failed = TRUE;
10007 else
10008 {
10009 /*
10010 * Change to session file's dir.
10011 */
10012 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010014 *dirnow = NUL;
10015 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
10016 {
10017 if (vim_chdirfile(fname) == OK)
10018 shorten_fnames(TRUE);
10019 }
10020 else if (*dirnow != NUL
10021 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
10022 {
10023 if (mch_chdir((char *)globaldir) == 0)
10024 shorten_fnames(TRUE);
10025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026
Bram Moolenaard9462e32011-04-11 21:35:11 +020010027 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028
Bram Moolenaard9462e32011-04-11 21:35:11 +020010029 /* restore original dir */
10030 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +020010032 {
10033 if (mch_chdir((char *)dirnow) != 0)
10034 EMSG(_(e_prev_dir));
10035 shorten_fnames(TRUE);
10036 }
10037 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 }
10039 }
10040 else
10041 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010042 failed |= (put_view(fd, curwin, !using_vdir, flagp,
10043 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044 }
10045 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
10046 == FAIL)
10047 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010048 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
10049 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010050 if (eap->cmdidx == CMD_mksession)
10051 {
10052 if (put_line(fd, "unlet SessionLoad") == FAIL)
10053 failed = TRUE;
10054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 }
10056#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010057 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
10058 failed = TRUE;
10059
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 failed |= fclose(fd);
10061
10062 if (failed)
10063 EMSG(_(e_write));
10064#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
10065 else if (eap->cmdidx == CMD_mksession)
10066 {
10067 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010068 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069
Bram Moolenaard9462e32011-04-11 21:35:11 +020010070 tbuf = alloc(MAXPATHL);
10071 if (tbuf != NULL)
10072 {
10073 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10074 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10075 vim_free(tbuf);
10076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077 }
10078#endif
10079#ifdef MKSESSION_NL
10080 mksession_nl = FALSE;
10081#endif
10082 }
10083
10084#ifdef FEAT_BROWSE
10085theend:
10086 vim_free(browseFile);
10087#endif
10088#ifdef FEAT_SESSION
10089 vim_free(viewFile);
10090#endif
10091}
10092
Bram Moolenaardf177f62005-02-22 08:39:57 +000010093#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10094 || defined(PROTO)
10095 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010096vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010097{
10098 if (vim_mkdir(name, prot) != 0)
10099 {
10100 EMSG2(_("E739: Cannot create directory: %s"), name);
10101 return FAIL;
10102 }
10103 return OK;
10104}
10105#endif
10106
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107/*
10108 * Open a file for writing for an Ex command, with some checks.
10109 * Return file descriptor, or NULL on failure.
10110 */
10111 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010112open_exfile(
10113 char_u *fname,
10114 int forceit,
10115 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116{
10117 FILE *fd;
10118
10119#ifdef UNIX
10120 /* with Unix it is possible to open a directory */
10121 if (mch_isdir(fname))
10122 {
10123 EMSG2(_(e_isadir2), fname);
10124 return NULL;
10125 }
10126#endif
10127 if (!forceit && *mode != 'a' && vim_fexists(fname))
10128 {
10129 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
10130 return NULL;
10131 }
10132
10133 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
10134 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
10135
10136 return fd;
10137}
10138
10139/*
10140 * ":mark" and ":k".
10141 */
10142 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010143ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144{
10145 pos_T pos;
10146
10147 if (*eap->arg == NUL) /* No argument? */
10148 EMSG(_(e_argreq));
10149 else if (eap->arg[1] != NUL) /* more than one character? */
10150 EMSG(_(e_trailing));
10151 else
10152 {
10153 pos = curwin->w_cursor; /* save curwin->w_cursor */
10154 curwin->w_cursor.lnum = eap->line2;
10155 beginline(BL_WHITE | BL_FIX);
10156 if (setmark(*eap->arg) == FAIL) /* set mark */
10157 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10158 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10159 }
10160}
10161
10162/*
10163 * Update w_topline, w_leftcol and the cursor position.
10164 */
10165 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010166update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167{
10168 check_cursor(); /* put cursor on valid line */
10169 update_topline();
10170 if (!curwin->w_p_wrap)
10171 validate_cursor();
10172 update_curswant();
10173}
10174
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175/*
10176 * ":normal[!] {commands}": Execute normal mode commands.
10177 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010178 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010179ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 int save_msg_scroll = msg_scroll;
10182 int save_restart_edit = restart_edit;
10183 int save_msg_didout = msg_didout;
10184 int save_State = State;
10185 tasave_T tabuf;
10186 int save_insertmode = p_im;
10187 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010188 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189#ifdef FEAT_MBYTE
10190 char_u *arg = NULL;
10191 int l;
10192 char_u *p;
10193#endif
10194
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010195 if (ex_normal_lock > 0)
10196 {
10197 EMSG(_(e_secure));
10198 return;
10199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 if (ex_normal_busy >= p_mmd)
10201 {
10202 EMSG(_("E192: Recursive use of :normal too deep"));
10203 return;
10204 }
10205 ++ex_normal_busy;
10206
10207 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10208 restart_edit = 0; /* don't go to Insert mode */
10209 p_im = FALSE; /* don't use 'insertmode' */
10210
10211#ifdef FEAT_MBYTE
10212 /*
10213 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10214 * this for the K_SPECIAL leading byte, otherwise special keys will not
10215 * work.
10216 */
10217 if (has_mbyte)
10218 {
10219 int len = 0;
10220
10221 /* Count the number of characters to be escaped. */
10222 for (p = eap->arg; *p != NUL; ++p)
10223 {
10224# ifdef FEAT_GUI
10225 if (*p == CSI) /* leadbyte CSI */
10226 len += 2;
10227# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010228 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10230# ifdef FEAT_GUI
10231 || *p == CSI
10232# endif
10233 )
10234 len += 2;
10235 }
10236 if (len > 0)
10237 {
10238 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10239 if (arg != NULL)
10240 {
10241 len = 0;
10242 for (p = eap->arg; *p != NUL; ++p)
10243 {
10244 arg[len++] = *p;
10245# ifdef FEAT_GUI
10246 if (*p == CSI)
10247 {
10248 arg[len++] = KS_EXTRA;
10249 arg[len++] = (int)KE_CSI;
10250 }
10251# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010252 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253 {
10254 arg[len++] = *++p;
10255 if (*p == K_SPECIAL)
10256 {
10257 arg[len++] = KS_SPECIAL;
10258 arg[len++] = KE_FILLER;
10259 }
10260# ifdef FEAT_GUI
10261 else if (*p == CSI)
10262 {
10263 arg[len++] = KS_EXTRA;
10264 arg[len++] = (int)KE_CSI;
10265 }
10266# endif
10267 }
10268 arg[len] = NUL;
10269 }
10270 }
10271 }
10272 }
10273#endif
10274
10275 /*
10276 * Save the current typeahead. This is required to allow using ":normal"
10277 * from an event handler and makes sure we don't hang when the argument
10278 * ends with half a command.
10279 */
10280 save_typeahead(&tabuf);
10281 if (tabuf.typebuf_valid)
10282 {
10283 /*
10284 * Repeat the :normal command for each line in the range. When no
10285 * range given, execute it just once, without positioning the cursor
10286 * first.
10287 */
10288 do
10289 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290 if (eap->addr_count != 0)
10291 {
10292 curwin->w_cursor.lnum = eap->line1++;
10293 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010294 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295 }
10296
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010297 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298#ifdef FEAT_MBYTE
10299 arg != NULL ? arg :
10300#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010301 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302 }
10303 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10304 }
10305
10306 /* Might not return to the main loop when in an event handler. */
10307 update_topline_cursor();
10308
10309 /* Restore the previous typeahead. */
10310 restore_typeahead(&tabuf);
10311
10312 --ex_normal_busy;
10313 msg_scroll = save_msg_scroll;
10314 restart_edit = save_restart_edit;
10315 p_im = save_insertmode;
10316 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010317 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
10319
10320 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +010010321 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +010010323#ifdef FEAT_MOUSE
10324 setmouse();
10325#endif
10326#ifdef CURSOR_SHAPE
10327 ui_cursor_shape(); /* may show different cursor shape */
10328#endif
10329
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330#ifdef FEAT_MBYTE
10331 vim_free(arg);
10332#endif
10333}
10334
10335/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010336 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 */
10338 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010339ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010341 if (eap->forceit)
10342 {
10343 coladvance((colnr_T)MAXCOL);
10344 curwin->w_curswant = MAXCOL;
10345 curwin->w_set_curswant = FALSE;
10346 }
10347
Bram Moolenaara40c5002005-01-09 21:16:21 +000010348 /* Ignore the command when already in Insert mode. Inserting an
10349 * expression register that invokes a function can do this. */
10350 if (State & INSERT)
10351 return;
10352
Bram Moolenaar64969662005-12-14 21:59:55 +000010353 if (eap->cmdidx == CMD_startinsert)
10354 restart_edit = 'a';
10355 else if (eap->cmdidx == CMD_startreplace)
10356 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010358 restart_edit = 'V';
10359
10360 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010362 if (eap->cmdidx == CMD_startinsert)
10363 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 curwin->w_curswant = 0; /* avoid MAXCOL */
10365 }
10366}
10367
10368/*
10369 * ":stopinsert"
10370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010372ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373{
10374 restart_edit = 0;
10375 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010376 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010379/*
10380 * Execute normal mode command "cmd".
10381 * "remap" can be REMAP_NONE or REMAP_YES.
10382 */
10383 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010384exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010385{
Bram Moolenaar25281632016-01-21 23:32:32 +010010386 /* Stuff the argument into the typeahead buffer. */
10387 ins_typebuf(cmd, remap, 0, TRUE, silent);
10388 exec_normal(FALSE);
10389}
Bram Moolenaar25281632016-01-21 23:32:32 +010010390
Bram Moolenaar25281632016-01-21 23:32:32 +010010391/*
10392 * Execute normal_cmd() until there is no typeahead left.
10393 */
10394 void
10395exec_normal(int was_typed)
10396{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010397 oparg_T oa;
10398
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010399 clear_oparg(&oa);
10400 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010401 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10402 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010403 {
10404 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010405 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010406 }
10407}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010408
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409#ifdef FEAT_FIND_ID
10410 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010411ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412{
10413 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10414 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10415 (linenr_T)1, (linenr_T)MAXLNUM);
10416}
10417
10418#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
10419/*
10420 * ":psearch"
10421 */
10422 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010423ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424{
10425 g_do_tagpreview = p_pvh;
10426 ex_findpat(eap);
10427 g_do_tagpreview = 0;
10428}
10429#endif
10430
10431 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010432ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433{
10434 int whole = TRUE;
10435 long n;
10436 char_u *p;
10437 int action;
10438
10439 switch (cmdnames[eap->cmdidx].cmd_name[2])
10440 {
10441 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10442 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10443 action = ACTION_GOTO;
10444 else
10445 action = ACTION_SHOW;
10446 break;
10447 case 'i': /* ":ilist" and ":dlist" */
10448 action = ACTION_SHOW_ALL;
10449 break;
10450 case 'u': /* ":ijump" and ":djump" */
10451 action = ACTION_GOTO;
10452 break;
10453 default: /* ":isplit" and ":dsplit" */
10454 action = ACTION_SPLIT;
10455 break;
10456 }
10457
10458 n = 1;
10459 if (vim_isdigit(*eap->arg)) /* get count */
10460 {
10461 n = getdigits(&eap->arg);
10462 eap->arg = skipwhite(eap->arg);
10463 }
10464 if (*eap->arg == '/') /* Match regexp, not just whole words */
10465 {
10466 whole = FALSE;
10467 ++eap->arg;
10468 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10469 if (*p)
10470 {
10471 *p++ = NUL;
10472 p = skipwhite(p);
10473
10474 /* Check for trailing illegal characters */
10475 if (!ends_excmd(*p))
10476 eap->errmsg = e_trailing;
10477 else
10478 eap->nextcmd = check_nextcmd(p);
10479 }
10480 }
10481 if (!eap->skip)
10482 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10483 whole, !eap->forceit,
10484 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10485 n, action, eap->line1, eap->line2);
10486}
10487#endif
10488
10489#ifdef FEAT_WINDOWS
10490
10491# ifdef FEAT_QUICKFIX
10492/*
10493 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10494 */
10495 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010496ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010498 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10500}
10501
10502/*
10503 * ":pedit"
10504 */
10505 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010506ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507{
10508 win_T *curwin_save = curwin;
10509
10510 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010511 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512 keep_help_flag = curwin_save->w_buffer->b_help;
10513 do_exedit(eap, NULL);
10514 keep_help_flag = FALSE;
10515 if (curwin != curwin_save && win_valid(curwin_save))
10516 {
10517 /* Return cursor to where we were */
10518 validate_cursor();
10519 redraw_later(VALID);
10520 win_enter(curwin_save, TRUE);
10521 }
10522 g_do_tagpreview = 0;
10523}
10524# endif
10525
10526/*
10527 * ":stag", ":stselect" and ":stjump".
10528 */
10529 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010530ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531{
10532 postponed_split = -1;
10533 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010534 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10536 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010537 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538}
10539#endif
10540
10541/*
10542 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10543 */
10544 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010545ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546{
10547 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10548}
10549
10550 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010551ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552{
10553 int cmd;
10554
10555 switch (name[1])
10556 {
10557 case 'j': cmd = DT_JUMP; /* ":tjump" */
10558 break;
10559 case 's': cmd = DT_SELECT; /* ":tselect" */
10560 break;
10561 case 'p': cmd = DT_PREV; /* ":tprevious" */
10562 break;
10563 case 'N': cmd = DT_PREV; /* ":tNext" */
10564 break;
10565 case 'n': cmd = DT_NEXT; /* ":tnext" */
10566 break;
10567 case 'o': cmd = DT_POP; /* ":pop" */
10568 break;
10569 case 'f': /* ":tfirst" */
10570 case 'r': cmd = DT_FIRST; /* ":trewind" */
10571 break;
10572 case 'l': cmd = DT_LAST; /* ":tlast" */
10573 break;
10574 default: /* ":tag" */
10575#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010576 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010578 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579 return;
10580 }
10581#endif
10582 cmd = DT_TAG;
10583 break;
10584 }
10585
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010586 if (name[0] == 'l')
10587 {
10588#ifndef FEAT_QUICKFIX
10589 ex_ni(eap);
10590 return;
10591#else
10592 cmd = DT_LTAG;
10593#endif
10594 }
10595
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10597 eap->forceit, TRUE);
10598}
10599
10600/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010601 * Check "str" for starting with a special cmdline variable.
10602 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10603 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10604 */
10605 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010606find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010607{
10608 int len;
10609 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010610 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010611 "%",
10612#define SPEC_PERC 0
10613 "#",
10614#define SPEC_HASH 1
10615 "<cword>", /* cursor word */
10616#define SPEC_CWORD 2
10617 "<cWORD>", /* cursor WORD */
10618#define SPEC_CCWORD 3
10619 "<cfile>", /* cursor path name */
10620#define SPEC_CFILE 4
10621 "<sfile>", /* ":so" file name */
10622#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010623 "<slnum>", /* ":so" file line number */
10624#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010625#ifdef FEAT_AUTOCMD
10626 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010627# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010628 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010629# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010630 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010631# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010632#endif
10633#ifdef FEAT_CLIENTSERVER
10634 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010635# ifdef FEAT_AUTOCMD
10636# define SPEC_CLIENT 10
10637# else
10638# define SPEC_CLIENT 7
10639# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010640#endif
10641 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010642
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010643 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010644 {
10645 len = (int)STRLEN(spec_str[i]);
10646 if (STRNCMP(src, spec_str[i], len) == 0)
10647 {
10648 *usedlen = len;
10649 return i;
10650 }
10651 }
10652 return -1;
10653}
10654
10655/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656 * Evaluate cmdline variables.
10657 *
10658 * change '%' to curbuf->b_ffname
10659 * '#' to curwin->w_altfile
10660 * '<cword>' to word under the cursor
10661 * '<cWORD>' to WORD under the cursor
10662 * '<cfile>' to path name under the cursor
10663 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010664 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665 * '<afile>' to file name for autocommand
10666 * '<abuf>' to buffer number for autocommand
10667 * '<amatch>' to matching name for autocommand
10668 *
10669 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10670 * "" for error without a message) and NULL is returned.
10671 * Returns an allocated string if a valid match was found.
10672 * Returns NULL if no match was found. "usedlen" then still contains the
10673 * number of characters to skip.
10674 */
10675 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010676eval_vars(
10677 char_u *src, /* pointer into commandline */
10678 char_u *srcstart, /* beginning of valid memory for src */
10679 int *usedlen, /* characters after src that are used */
10680 linenr_T *lnump, /* line number for :e command, or NULL */
10681 char_u **errormsg, /* pointer to error message */
10682 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010683 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684{
10685 int i;
10686 char_u *s;
10687 char_u *result;
10688 char_u *resultbuf = NULL;
10689 int resultlen;
10690 buf_T *buf;
10691 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10692 int spec_idx;
10693#ifdef FEAT_MODIFY_FNAME
10694 int skip_mod = FALSE;
10695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697
10698 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010699 if (escaped != NULL)
10700 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701
10702 /*
10703 * Check if there is something to do.
10704 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010705 spec_idx = find_cmdline_var(src, usedlen);
10706 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707 {
10708 *usedlen = 1;
10709 return NULL;
10710 }
10711
10712 /*
10713 * Skip when preceded with a backslash "\%" and "\#".
10714 * Note: In "\\%" the % is also not recognized!
10715 */
10716 if (src > srcstart && src[-1] == '\\')
10717 {
10718 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010719 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720 return NULL;
10721 }
10722
10723 /*
10724 * word or WORD under cursor
10725 */
10726 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10727 {
10728 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10729 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10730 if (resultlen == 0)
10731 {
10732 *errormsg = (char_u *)"";
10733 return NULL;
10734 }
10735 }
10736
10737 /*
10738 * '#': Alternate file name
10739 * '%': Current file name
10740 * File name under the cursor
10741 * File name for autocommand
10742 * and following modifiers
10743 */
10744 else
10745 {
10746 switch (spec_idx)
10747 {
10748 case SPEC_PERC: /* '%': current file */
10749 if (curbuf->b_fname == NULL)
10750 {
10751 result = (char_u *)"";
10752 valid = 0; /* Must have ":p:h" to be valid */
10753 }
10754 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756 break;
10757
10758 case SPEC_HASH: /* '#' or "#99": alternate file */
10759 if (src[1] == '#') /* "##": the argument list */
10760 {
10761 result = arg_all();
10762 resultbuf = result;
10763 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010764 if (escaped != NULL)
10765 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766#ifdef FEAT_MODIFY_FNAME
10767 skip_mod = TRUE;
10768#endif
10769 break;
10770 }
10771 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010772 if (*s == '<') /* "#<99" uses v:oldfiles */
10773 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774 i = (int)getdigits(&s);
10775 *usedlen = (int)(s - src); /* length of what we expand */
10776
Bram Moolenaard812df62008-11-09 12:46:09 +000010777 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010779 if (*usedlen < 2)
10780 {
10781 /* Should we give an error message for #<text? */
10782 *usedlen = 1;
10783 return NULL;
10784 }
10785#ifdef FEAT_EVAL
10786 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10787 (long)i);
10788 if (result == NULL)
10789 {
10790 *errormsg = (char_u *)"";
10791 return NULL;
10792 }
10793#else
10794 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 }
10798 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010799 {
10800 buf = buflist_findnr(i);
10801 if (buf == NULL)
10802 {
10803 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10804 return NULL;
10805 }
10806 if (lnump != NULL)
10807 *lnump = ECMD_LAST;
10808 if (buf->b_fname == NULL)
10809 {
10810 result = (char_u *)"";
10811 valid = 0; /* Must have ":p:h" to be valid */
10812 }
10813 else
10814 result = buf->b_fname;
10815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816 break;
10817
10818#ifdef FEAT_SEARCHPATH
10819 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010820 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010821 if (result == NULL)
10822 {
10823 *errormsg = (char_u *)"";
10824 return NULL;
10825 }
10826 resultbuf = result; /* remember allocated string */
10827 break;
10828#endif
10829
10830#ifdef FEAT_AUTOCMD
10831 case SPEC_AFILE: /* file name for autocommand */
10832 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010833 if (result != NULL && !autocmd_fname_full)
10834 {
10835 /* Still need to turn the fname into a full path. It is
10836 * postponed to avoid a delay when <afile> is not used. */
10837 autocmd_fname_full = TRUE;
10838 result = FullName_save(autocmd_fname, FALSE);
10839 vim_free(autocmd_fname);
10840 autocmd_fname = result;
10841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842 if (result == NULL)
10843 {
10844 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10845 return NULL;
10846 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010847 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848 break;
10849
10850 case SPEC_ABUF: /* buffer number for autocommand */
10851 if (autocmd_bufnr <= 0)
10852 {
10853 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10854 return NULL;
10855 }
10856 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10857 result = strbuf;
10858 break;
10859
10860 case SPEC_AMATCH: /* match name for autocommand */
10861 result = autocmd_match;
10862 if (result == NULL)
10863 {
10864 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10865 return NULL;
10866 }
10867 break;
10868
10869#endif
10870 case SPEC_SFILE: /* file name for ":so" command */
10871 result = sourcing_name;
10872 if (result == NULL)
10873 {
10874 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10875 return NULL;
10876 }
10877 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010878 case SPEC_SLNUM: /* line in file for ":so" command */
10879 if (sourcing_name == NULL || sourcing_lnum == 0)
10880 {
10881 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10882 return NULL;
10883 }
10884 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10885 result = strbuf;
10886 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887#if defined(FEAT_CLIENTSERVER)
10888 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010889 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10890 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891 result = strbuf;
10892 break;
10893#endif
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020010894 default:
10895 result = (char_u *)""; /* avoid gcc warning */
10896 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010897 }
10898
10899 resultlen = (int)STRLEN(result); /* length of new string */
10900 if (src[*usedlen] == '<') /* remove the file name extension */
10901 {
10902 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904 resultlen = (int)(s - result);
10905 }
10906#ifdef FEAT_MODIFY_FNAME
10907 else if (!skip_mod)
10908 {
10909 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10910 &resultlen);
10911 if (result == NULL)
10912 {
10913 *errormsg = (char_u *)"";
10914 return NULL;
10915 }
10916 }
10917#endif
10918 }
10919
10920 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10921 {
10922 if (valid != VALID_HEAD + VALID_PATH)
10923 /* xgettext:no-c-format */
10924 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10925 else
10926 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10927 result = NULL;
10928 }
10929 else
10930 result = vim_strnsave(result, resultlen);
10931 vim_free(resultbuf);
10932 return result;
10933}
10934
10935/*
10936 * Concatenate all files in the argument list, separated by spaces, and return
10937 * it in one allocated string.
10938 * Spaces and backslashes in the file names are escaped with a backslash.
10939 * Returns NULL when out of memory.
10940 */
10941 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010942arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943{
10944 int len;
10945 int idx;
10946 char_u *retval = NULL;
10947 char_u *p;
10948
10949 /*
10950 * Do this loop two times:
10951 * first time: compute the total length
10952 * second time: concatenate the names
10953 */
10954 for (;;)
10955 {
10956 len = 0;
10957 for (idx = 0; idx < ARGCOUNT; ++idx)
10958 {
10959 p = alist_name(&ARGLIST[idx]);
10960 if (p != NULL)
10961 {
10962 if (len > 0)
10963 {
10964 /* insert a space in between names */
10965 if (retval != NULL)
10966 retval[len] = ' ';
10967 ++len;
10968 }
10969 for ( ; *p != NUL; ++p)
10970 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010971 if (*p == ' '
10972#ifndef BACKSLASH_IN_FILENAME
10973 || *p == '\\'
10974#endif
10975 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 {
10977 /* insert a backslash */
10978 if (retval != NULL)
10979 retval[len] = '\\';
10980 ++len;
10981 }
10982 if (retval != NULL)
10983 retval[len] = *p;
10984 ++len;
10985 }
10986 }
10987 }
10988
10989 /* second time: break here */
10990 if (retval != NULL)
10991 {
10992 retval[len] = NUL;
10993 break;
10994 }
10995
10996 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010997 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998 if (retval == NULL)
10999 break;
11000 }
11001
11002 return retval;
11003}
11004
11005#if defined(FEAT_AUTOCMD) || defined(PROTO)
11006/*
11007 * Expand the <sfile> string in "arg".
11008 *
11009 * Returns an allocated string, or NULL for any error.
11010 */
11011 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011012expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013{
11014 char_u *errormsg;
11015 int len;
11016 char_u *result;
11017 char_u *newres;
11018 char_u *repl;
11019 int srclen;
11020 char_u *p;
11021
11022 result = vim_strsave(arg);
11023 if (result == NULL)
11024 return NULL;
11025
11026 for (p = result; *p; )
11027 {
11028 if (STRNCMP(p, "<sfile>", 7) != 0)
11029 ++p;
11030 else
11031 {
11032 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000011033 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034 if (errormsg != NULL)
11035 {
11036 if (*errormsg)
11037 emsg(errormsg);
11038 vim_free(result);
11039 return NULL;
11040 }
11041 if (repl == NULL) /* no match (cannot happen) */
11042 {
11043 p += srclen;
11044 continue;
11045 }
11046 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11047 newres = alloc(len);
11048 if (newres == NULL)
11049 {
11050 vim_free(repl);
11051 vim_free(result);
11052 return NULL;
11053 }
11054 mch_memmove(newres, result, (size_t)(p - result));
11055 STRCPY(newres + (p - result), repl);
11056 len = (int)STRLEN(newres);
11057 STRCAT(newres, p + srclen);
11058 vim_free(repl);
11059 vim_free(result);
11060 result = newres;
11061 p = newres + len; /* continue after the match */
11062 }
11063 }
11064
11065 return result;
11066}
11067#endif
11068
11069#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011070static int ses_winsizes(FILE *fd, int restore_size,
11071 win_T *tab_firstwin);
11072static int ses_win_rec(FILE *fd, frame_T *fr);
11073static frame_T *ses_skipframe(frame_T *fr);
11074static int ses_do_frame(frame_T *fr);
11075static int ses_do_win(win_T *wp);
11076static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11077static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
11078static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079
11080/*
11081 * Write openfile commands for the current buffers to an .exrc file.
11082 * Return FAIL on error, OK otherwise.
11083 */
11084 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011085makeopens(
11086 FILE *fd,
11087 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088{
11089 buf_T *buf;
11090 int only_save_windows = TRUE;
11091 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 int restore_size = TRUE;
11093 win_T *wp;
11094 char_u *sname;
11095 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011096 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011097 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011098 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011099 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011100 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011101 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102
11103 if (ssop_flags & SSOP_BUFFERS)
11104 only_save_windows = FALSE; /* Save ALL buffers */
11105
11106 /*
11107 * Begin by setting the this_session variable, and then other
11108 * sessionable variables.
11109 */
11110#ifdef FEAT_EVAL
11111 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11112 return FAIL;
11113 if (ssop_flags & SSOP_GLOBALS)
11114 if (store_session_globals(fd) == FAIL)
11115 return FAIL;
11116#endif
11117
11118 /*
11119 * Close all windows but one.
11120 */
11121 if (put_line(fd, "silent only") == FAIL)
11122 return FAIL;
11123
11124 /*
11125 * Now a :cd command to the session directory or the current directory
11126 */
11127 if (ssop_flags & SSOP_SESDIR)
11128 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011129 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11130 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131 return FAIL;
11132 }
11133 else if (ssop_flags & SSOP_CURDIR)
11134 {
11135 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11136 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011137 || fputs("cd ", fd) < 0
11138 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11139 || put_eol(fd) == FAIL)
11140 {
11141 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144 vim_free(sname);
11145 }
11146
11147 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011148 * If there is an empty, unnamed buffer we will wipe it out later.
11149 * Remember the buffer number.
11150 */
11151 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11152 return FAIL;
11153 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11154 return FAIL;
11155 if (put_line(fd, "endif") == FAIL)
11156 return FAIL;
11157
11158 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159 * Now save the current files, current buffer first.
11160 */
11161 if (put_line(fd, "set shortmess=aoO") == FAIL)
11162 return FAIL;
11163
11164 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011165 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166 {
11167 if (!(only_save_windows && buf->b_nwindows == 0)
11168 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
11169 && buf->b_fname != NULL
11170 && buf->b_p_bl)
11171 {
11172 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11173 : buf->b_wininfo->wi_fpos.lnum) < 0
11174 || ses_fname(fd, buf, &ssop_flags) == FAIL)
11175 return FAIL;
11176 }
11177 }
11178
11179 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011180 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11182 return FAIL;
11183
11184 if (ssop_flags & SSOP_RESIZE)
11185 {
11186 /* Note: after the restore we still check it worked!*/
11187 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11188 || put_eol(fd) == FAIL)
11189 return FAIL;
11190 }
11191
11192#ifdef FEAT_GUI
11193 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11194 {
11195 int x, y;
11196
11197 if (gui_mch_get_winpos(&x, &y) == OK)
11198 {
11199 /* Note: after the restore we still check it worked!*/
11200 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11201 return FAIL;
11202 }
11203 }
11204#endif
11205
11206 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011207 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11208 * will be displayed when creating the next tab. That resizes the windows
11209 * in the first tab, which may cause problems. Set 'showtabline' to 2
11210 * temporarily to avoid that.
11211 */
11212 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11213 {
11214 if (put_line(fd, "set stal=2") == FAIL)
11215 return FAIL;
11216 restore_stal = TRUE;
11217 }
11218
11219 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011220 * May repeat putting Windows for each tab, when "tabpages" is in
11221 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011222 * Don't use goto_tabpage(), it may change directory and trigger
11223 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011225 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011226 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011227 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011229 int need_tabnew = FALSE;
11230 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011231
Bram Moolenaar18144c82006-04-12 21:52:12 +000011232 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011234 tabpage_T *tp = find_tabpage(tabnr);
11235
11236 if (tp == NULL)
11237 break; /* done all tab pages */
11238 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011239 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011240 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011241 tab_topframe = topframe;
11242 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011243 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011244 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011245 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011246 tab_topframe = tp->tp_topframe;
11247 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011248 if (tabnr > 1)
11249 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251
Bram Moolenaar18144c82006-04-12 21:52:12 +000011252 /*
11253 * Before creating the window layout, try loading one file. If this
11254 * is aborted we don't end up with a number of useless windows.
11255 * This may have side effects! (e.g., compressed or network file).
11256 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011257 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011258 {
11259 if (ses_do_win(wp)
11260 && wp->w_buffer->b_ffname != NULL
11261 && !wp->w_buffer->b_help
11262#ifdef FEAT_QUICKFIX
11263 && !bt_nofile(wp->w_buffer)
11264#endif
11265 )
11266 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011267 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000011268 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
11269 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011270 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011271 if (!wp->w_arg_idx_invalid)
11272 edited_win = wp;
11273 break;
11274 }
11275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011277 /* If no file got edited create an empty tab page. */
11278 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11279 return FAIL;
11280
Bram Moolenaar18144c82006-04-12 21:52:12 +000011281 /*
11282 * Save current window layout.
11283 */
11284 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011286 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011288 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11289 return FAIL;
11290 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11291 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292
Bram Moolenaar18144c82006-04-12 21:52:12 +000011293 /*
11294 * Check if window sizes can be restored (no windows omitted).
11295 * Remember the window number of the current window after restoring.
11296 */
11297 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011298 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011299 {
11300 if (ses_do_win(wp))
11301 ++nr;
11302 else
11303 restore_size = FALSE;
11304 if (curwin == wp)
11305 cnr = nr;
11306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307
Bram Moolenaar18144c82006-04-12 21:52:12 +000011308 /* Go to the first window. */
11309 if (put_line(fd, "wincmd t") == FAIL)
11310 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011311
Bram Moolenaar18144c82006-04-12 21:52:12 +000011312 /*
11313 * If more than one window, see if sizes can be restored.
11314 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11315 * resized when moving between windows.
11316 * Do this before restoring the view, so that the topline and the
11317 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011318 * winminheight and winminwidth need to be set to avoid an error if the
11319 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011320 */
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011321 if (put_line(fd, "set winminheight=1 winheight=1 winminwidth=1 winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011322 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011323 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011324 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325
Bram Moolenaar18144c82006-04-12 21:52:12 +000011326 /*
11327 * Restore the view of the window (options, file, cursor, etc.).
11328 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011329 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011330 {
11331 if (!ses_do_win(wp))
11332 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011333 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11334 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011335 return FAIL;
11336 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11337 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011338 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011339 }
11340
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011341 /* The argument index in the first tab page is zero, need to set it in
11342 * each window. For further tab pages it's the window where we do
11343 * "tabedit". */
11344 cur_arg_idx = next_arg_idx;
11345
Bram Moolenaar18144c82006-04-12 21:52:12 +000011346 /*
11347 * Restore cursor to the current window if it's not the first one.
11348 */
11349 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11350 || put_eol(fd) == FAIL))
11351 return FAIL;
11352
11353 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011354 * Restore window sizes again after jumping around in windows, because
11355 * the current window has a minimum size while others may not.
11356 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011357 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011358 return FAIL;
11359
Bram Moolenaar18144c82006-04-12 21:52:12 +000011360 /* Don't continue in another tab page when doing only the current one
11361 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011362 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011363 break;
11364 }
11365
11366 if (ssop_flags & SSOP_TABPAGES)
11367 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011368 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11369 || put_eol(fd) == FAIL)
11370 return FAIL;
11371 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011372 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11373 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011374
Bram Moolenaar9c102382006-05-03 21:26:49 +000011375 /*
11376 * Wipe out an empty unnamed buffer we started in.
11377 */
11378 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11379 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011380 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011381 return FAIL;
11382 if (put_line(fd, "endif") == FAIL)
11383 return FAIL;
11384 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11385 return FAIL;
11386
11387 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11388 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11389 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11390 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011391 /* Re-apply 'winminheight' and 'winminwidth'. */
11392 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11393 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11394 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395
11396 /*
11397 * Lastly, execute the x.vim file if it exists.
11398 */
11399 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11400 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011401 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402 || put_line(fd, "endif") == FAIL)
11403 return FAIL;
11404
11405 return OK;
11406}
11407
11408 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011409ses_winsizes(
11410 FILE *fd,
11411 int restore_size,
11412 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413{
11414 int n = 0;
11415 win_T *wp;
11416
11417 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11418 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011419 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420 {
11421 if (!ses_do_win(wp))
11422 continue;
11423 ++n;
11424
11425 /* restore height when not full height */
11426 if (wp->w_height + wp->w_status_height < topframe->fr_height
11427 && (fprintf(fd,
11428 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11429 n, (long)wp->w_height, Rows / 2, Rows) < 0
11430 || put_eol(fd) == FAIL))
11431 return FAIL;
11432
11433 /* restore width when not full width */
11434 if (wp->w_width < Columns && (fprintf(fd,
11435 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11436 n, (long)wp->w_width, Columns / 2, Columns) < 0
11437 || put_eol(fd) == FAIL))
11438 return FAIL;
11439 }
11440 }
11441 else
11442 {
11443 /* Just equalise window sizes */
11444 if (put_line(fd, "wincmd =") == FAIL)
11445 return FAIL;
11446 }
11447 return OK;
11448}
11449
11450/*
11451 * Write commands to "fd" to recursively create windows for frame "fr",
11452 * horizontally and vertically split.
11453 * After the commands the last window in the frame is the current window.
11454 * Returns FAIL when writing the commands to "fd" fails.
11455 */
11456 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011457ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011458{
11459 frame_T *frc;
11460 int count = 0;
11461
11462 if (fr->fr_layout != FR_LEAF)
11463 {
11464 /* Find first frame that's not skipped and then create a window for
11465 * each following one (first frame is already there). */
11466 frc = ses_skipframe(fr->fr_child);
11467 if (frc != NULL)
11468 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11469 {
11470 /* Make window as big as possible so that we have lots of room
11471 * to split. */
11472 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11473 || put_line(fd, fr->fr_layout == FR_COL
11474 ? "split" : "vsplit") == FAIL)
11475 return FAIL;
11476 ++count;
11477 }
11478
11479 /* Go back to the first window. */
11480 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11481 ? "%dwincmd k" : "%dwincmd h", count) < 0
11482 || put_eol(fd) == FAIL))
11483 return FAIL;
11484
11485 /* Recursively create frames/windows in each window of this column or
11486 * row. */
11487 frc = ses_skipframe(fr->fr_child);
11488 while (frc != NULL)
11489 {
11490 ses_win_rec(fd, frc);
11491 frc = ses_skipframe(frc->fr_next);
11492 /* Go to next window. */
11493 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11494 return FAIL;
11495 }
11496 }
11497 return OK;
11498}
11499
11500/*
11501 * Skip frames that don't contain windows we want to save in the Session.
11502 * Returns NULL when there none.
11503 */
11504 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011505ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011506{
11507 frame_T *frc;
11508
11509 for (frc = fr; frc != NULL; frc = frc->fr_next)
11510 if (ses_do_frame(frc))
11511 break;
11512 return frc;
11513}
11514
11515/*
11516 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11517 * the Session.
11518 */
11519 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011520ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521{
11522 frame_T *frc;
11523
11524 if (fr->fr_layout == FR_LEAF)
11525 return ses_do_win(fr->fr_win);
11526 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11527 if (ses_do_frame(frc))
11528 return TRUE;
11529 return FALSE;
11530}
11531
11532/*
11533 * Return non-zero if window "wp" is to be stored in the Session.
11534 */
11535 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011536ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537{
11538 if (wp->w_buffer->b_fname == NULL
11539#ifdef FEAT_QUICKFIX
11540 /* When 'buftype' is "nofile" can't restore the window contents. */
11541 || bt_nofile(wp->w_buffer)
11542#endif
11543 )
11544 return (ssop_flags & SSOP_BLANK);
11545 if (wp->w_buffer->b_help)
11546 return (ssop_flags & SSOP_HELP);
11547 return TRUE;
11548}
11549
11550/*
11551 * Write commands to "fd" to restore the view of a window.
11552 * Caller must make sure 'scrolloff' is zero.
11553 */
11554 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011555put_view(
11556 FILE *fd,
11557 win_T *wp,
11558 int add_edit, /* add ":edit" command to view */
11559 unsigned *flagp, /* vop_flags or ssop_flags */
11560 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011561 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011562{
11563 win_T *save_curwin;
11564 int f;
11565 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011566 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567
11568 /* Always restore cursor position for ":mksession". For ":mkview" only
11569 * when 'viewoptions' contains "cursor". */
11570 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11571
11572 /*
11573 * Local argument list.
11574 */
11575 if (wp->w_alist == &global_alist)
11576 {
11577 if (put_line(fd, "argglobal") == FAIL)
11578 return FAIL;
11579 }
11580 else
11581 {
11582 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11583 flagp == &vop_flags
11584 || !(*flagp & SSOP_CURDIR)
11585 || wp->w_localdir != NULL, flagp) == FAIL)
11586 return FAIL;
11587 }
11588
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011589 /* Only when part of a session: restore the argument index. Some
11590 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011591 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011592 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011593 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011594 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011595 || put_eol(fd) == FAIL)
11596 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011597 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011598 }
11599
11600 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011601 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011602 {
11603 /*
11604 * Load the file.
11605 */
11606 if (wp->w_buffer->b_ffname != NULL
11607#ifdef FEAT_QUICKFIX
11608 && !bt_nofile(wp->w_buffer)
11609#endif
11610 )
11611 {
11612 /*
11613 * Editing a file in this buffer: use ":edit file".
11614 * This may have side effects! (e.g., compressed or network file).
11615 */
11616 if (fputs("edit ", fd) < 0
11617 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11618 return FAIL;
11619 }
11620 else
11621 {
11622 /* No file in this buffer, just make it empty. */
11623 if (put_line(fd, "enew") == FAIL)
11624 return FAIL;
11625#ifdef FEAT_QUICKFIX
11626 if (wp->w_buffer->b_ffname != NULL)
11627 {
11628 /* The buffer does have a name, but it's not a file name. */
11629 if (fputs("file ", fd) < 0
11630 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11631 return FAIL;
11632 }
11633#endif
11634 do_cursor = FALSE;
11635 }
11636 }
11637
11638 /*
11639 * Local mappings and abbreviations.
11640 */
11641 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11642 && makemap(fd, wp->w_buffer) == FAIL)
11643 return FAIL;
11644
11645 /*
11646 * Local options. Need to go to the window temporarily.
11647 * Store only local values when using ":mkview" and when ":mksession" is
11648 * used and 'sessionoptions' doesn't include "options".
11649 * Some folding options are always stored when "folds" is included,
11650 * otherwise the folds would not be restored correctly.
11651 */
11652 save_curwin = curwin;
11653 curwin = wp;
11654 curbuf = curwin->w_buffer;
11655 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11656 f = makeset(fd, OPT_LOCAL,
11657 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11658#ifdef FEAT_FOLDING
11659 else if (*flagp & SSOP_FOLDS)
11660 f = makefoldset(fd);
11661#endif
11662 else
11663 f = OK;
11664 curwin = save_curwin;
11665 curbuf = curwin->w_buffer;
11666 if (f == FAIL)
11667 return FAIL;
11668
11669#ifdef FEAT_FOLDING
11670 /*
11671 * Save Folds when 'buftype' is empty and for help files.
11672 */
11673 if ((*flagp & SSOP_FOLDS)
11674 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011675# ifdef FEAT_QUICKFIX
11676 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11677# endif
11678 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011679 {
11680 if (put_folds(fd, wp) == FAIL)
11681 return FAIL;
11682 }
11683#endif
11684
11685 /*
11686 * Set the cursor after creating folds, since that moves the cursor.
11687 */
11688 if (do_cursor)
11689 {
11690
11691 /* Restore the cursor line in the file and relatively in the
11692 * window. Don't use "G", it changes the jumplist. */
11693 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11694 (long)wp->w_cursor.lnum,
11695 (long)(wp->w_cursor.lnum - wp->w_topline),
11696 (long)wp->w_height / 2, (long)wp->w_height) < 0
11697 || put_eol(fd) == FAIL
11698 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11699 || put_line(fd, "exe s:l") == FAIL
11700 || put_line(fd, "normal! zt") == FAIL
11701 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11702 || put_eol(fd) == FAIL)
11703 return FAIL;
11704 /* Restore the cursor column and left offset when not wrapping. */
11705 if (wp->w_cursor.col == 0)
11706 {
11707 if (put_line(fd, "normal! 0") == FAIL)
11708 return FAIL;
11709 }
11710 else
11711 {
11712 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11713 {
11714 if (fprintf(fd,
11715 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011716 (long)wp->w_virtcol + 1,
11717 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718 (long)wp->w_width / 2, (long)wp->w_width) < 0
11719 || put_eol(fd) == FAIL
11720 || put_line(fd, "if s:c > 0") == FAIL
11721 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011722 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11723 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011724 || put_eol(fd) == FAIL
11725 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011726 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727 || put_eol(fd) == FAIL
11728 || put_line(fd, "endif") == FAIL)
11729 return FAIL;
11730 }
11731 else
11732 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011733 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011734 || put_eol(fd) == FAIL)
11735 return FAIL;
11736 }
11737 }
11738 }
11739
11740 /*
11741 * Local directory.
11742 */
11743 if (wp->w_localdir != NULL)
11744 {
11745 if (fputs("lcd ", fd) < 0
11746 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11747 || put_eol(fd) == FAIL)
11748 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011749 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011750 }
11751
11752 return OK;
11753}
11754
11755/*
11756 * Write an argument list to the session file.
11757 * Returns FAIL if writing fails.
11758 */
11759 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011760ses_arglist(
11761 FILE *fd,
11762 char *cmd,
11763 garray_T *gap,
11764 int fullname, /* TRUE: use full path name */
11765 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766{
11767 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011768 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011769 char_u *s;
11770
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011771 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11772 return FAIL;
11773 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774 return FAIL;
11775 for (i = 0; i < gap->ga_len; ++i)
11776 {
11777 /* NULL file names are skipped (only happens when out of memory). */
11778 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11779 if (s != NULL)
11780 {
11781 if (fullname)
11782 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011783 buf = alloc(MAXPATHL);
11784 if (buf != NULL)
11785 {
11786 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11787 s = buf;
11788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011789 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010011790 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011791 || ses_put_fname(fd, s, flagp) == FAIL
11792 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011793 {
11794 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011796 }
11797 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011798 }
11799 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011800 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011801}
11802
11803/*
11804 * Write a buffer name to the session file.
11805 * Also ends the line.
11806 * Returns FAIL if writing fails.
11807 */
11808 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011809ses_fname(FILE *fd, buf_T *buf, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810{
11811 char_u *name;
11812
11813 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011814 * the session file will be sourced.
11815 * Don't do this for ":mkview", we don't know the current directory.
11816 * Don't do this after ":lcd", we don't keep track of what the current
11817 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818 if (buf->b_sfname != NULL
11819 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011820 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011821#ifdef FEAT_AUTOCHDIR
11822 && !p_acd
11823#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011824 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825 name = buf->b_sfname;
11826 else
11827 name = buf->b_ffname;
11828 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11829 return FAIL;
11830 return OK;
11831}
11832
11833/*
11834 * Write a file name to the session file.
11835 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11836 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011837 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011838 */
11839 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011840ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841{
11842 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011843 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011844 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011845
11846 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011847 if (sname == NULL)
11848 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011850 if (*flagp & SSOP_SLASH)
11851 {
11852 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011853 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011854 if (*p == '\\')
11855 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011857
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011858 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011859 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011861 if (p == NULL)
11862 return FAIL;
11863
11864 /* write the result */
11865 if (fputs((char *)p, fd) < 0)
11866 retval = FAIL;
11867
11868 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011869 return retval;
11870}
11871
11872/*
11873 * ":loadview [nr]"
11874 */
11875 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011876ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877{
11878 char_u *fname;
11879
11880 fname = get_view_file(*eap->arg);
11881 if (fname != NULL)
11882 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011883 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011884 vim_free(fname);
11885 }
11886}
11887
11888/*
11889 * Get the name of the view file for the current buffer.
11890 */
11891 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011892get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011893{
11894 int len = 0;
11895 char_u *p, *s;
11896 char_u *retval;
11897 char_u *sname;
11898
11899 if (curbuf->b_ffname == NULL)
11900 {
11901 EMSG(_(e_noname));
11902 return NULL;
11903 }
11904 sname = home_replace_save(NULL, curbuf->b_ffname);
11905 if (sname == NULL)
11906 return NULL;
11907
11908 /*
11909 * We want a file name without separators, because we're not going to make
11910 * a directory.
11911 * "normal" path separator -> "=+"
11912 * "=" -> "=="
11913 * ":" path separator -> "=-"
11914 */
11915 for (p = sname; *p; ++p)
11916 if (*p == '=' || vim_ispathsep(*p))
11917 ++len;
11918 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11919 if (retval != NULL)
11920 {
11921 STRCPY(retval, p_vdir);
11922 add_pathsep(retval);
11923 s = retval + STRLEN(retval);
11924 for (p = sname; *p; ++p)
11925 {
11926 if (*p == '=')
11927 {
11928 *s++ = '=';
11929 *s++ = '=';
11930 }
11931 else if (vim_ispathsep(*p))
11932 {
11933 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011934#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011935 if (*p == ':')
11936 *s++ = '-';
11937 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011939 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011940 }
11941 else
11942 *s++ = *p;
11943 }
11944 *s++ = '=';
11945 *s++ = c;
11946 STRCPY(s, ".vim");
11947 }
11948
11949 vim_free(sname);
11950 return retval;
11951}
11952
11953#endif /* FEAT_SESSION */
11954
11955/*
11956 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11957 * Return FAIL for a write error.
11958 */
11959 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011960put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961{
11962 if (
11963#ifdef USE_CRNL
11964 (
11965# ifdef MKSESSION_NL
11966 !mksession_nl &&
11967# endif
11968 (putc('\r', fd) < 0)) ||
11969#endif
11970 (putc('\n', fd) < 0))
11971 return FAIL;
11972 return OK;
11973}
11974
11975/*
11976 * Write a line to "fd".
11977 * Return FAIL for a write error.
11978 */
11979 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011980put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981{
11982 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11983 return FAIL;
11984 return OK;
11985}
11986
11987#ifdef FEAT_VIMINFO
11988/*
11989 * ":rviminfo" and ":wviminfo".
11990 */
11991 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011992ex_viminfo(
11993 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994{
11995 char_u *save_viminfo;
11996
11997 save_viminfo = p_viminfo;
11998 if (*p_viminfo == NUL)
11999 p_viminfo = (char_u *)"'100";
12000 if (eap->cmdidx == CMD_rviminfo)
12001 {
Bram Moolenaard812df62008-11-09 12:46:09 +000012002 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
12003 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004 EMSG(_("E195: Cannot open viminfo file for reading"));
12005 }
12006 else
12007 write_viminfo(eap->arg, eap->forceit);
12008 p_viminfo = save_viminfo;
12009}
12010#endif
12011
12012#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012013/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020012014 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000012015 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012016 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012017 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012018dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020 if (fname == NULL)
12021 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012022 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012023}
12024#endif
12025
12026/*
12027 * ":behave {mswin,xterm}"
12028 */
12029 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012030ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012031{
12032 if (STRCMP(eap->arg, "mswin") == 0)
12033 {
12034 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12035 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12036 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12037 set_option_value((char_u *)"keymodel", 0L,
12038 (char_u *)"startsel,stopsel", 0);
12039 }
12040 else if (STRCMP(eap->arg, "xterm") == 0)
12041 {
12042 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12043 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12044 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12045 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12046 }
12047 else
12048 EMSG2(_(e_invarg2), eap->arg);
12049}
12050
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012051#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12052/*
12053 * Function given to ExpandGeneric() to obtain the possible arguments of the
12054 * ":behave {mswin,xterm}" command.
12055 */
12056 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012057get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012058{
12059 if (idx == 0)
12060 return (char_u *)"mswin";
12061 if (idx == 1)
12062 return (char_u *)"xterm";
12063 return NULL;
12064}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012065
12066/*
12067 * Function given to ExpandGeneric() to obtain the possible arguments of the
12068 * ":messages {clear}" command.
12069 */
12070 char_u *
12071get_messages_arg(expand_T *xp UNUSED, int idx)
12072{
12073 if (idx == 0)
12074 return (char_u *)"clear";
12075 return NULL;
12076}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012077#endif
12078
Bram Moolenaar071d4272004-06-13 20:20:40 +000012079#ifdef FEAT_AUTOCMD
12080static int filetype_detect = FALSE;
12081static int filetype_plugin = FALSE;
12082static int filetype_indent = FALSE;
12083
12084/*
12085 * ":filetype [plugin] [indent] {on,off,detect}"
12086 * on: Load the filetype.vim file to install autocommands for file types.
12087 * off: Load the ftoff.vim file to remove all autocommands for file types.
12088 * plugin on: load filetype.vim and ftplugin.vim
12089 * plugin off: load ftplugof.vim
12090 * indent on: load filetype.vim and indent.vim
12091 * indent off: load indoff.vim
12092 */
12093 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012094ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012095{
12096 char_u *arg = eap->arg;
12097 int plugin = FALSE;
12098 int indent = FALSE;
12099
12100 if (*eap->arg == NUL)
12101 {
12102 /* Print current status. */
12103 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
12104 filetype_detect ? "ON" : "OFF",
12105 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12106 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12107 return;
12108 }
12109
12110 /* Accept "plugin" and "indent" in any order. */
12111 for (;;)
12112 {
12113 if (STRNCMP(arg, "plugin", 6) == 0)
12114 {
12115 plugin = TRUE;
12116 arg = skipwhite(arg + 6);
12117 continue;
12118 }
12119 if (STRNCMP(arg, "indent", 6) == 0)
12120 {
12121 indent = TRUE;
12122 arg = skipwhite(arg + 6);
12123 continue;
12124 }
12125 break;
12126 }
12127 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12128 {
12129 if (*arg == 'o' || !filetype_detect)
12130 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012131 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012132 filetype_detect = TRUE;
12133 if (plugin)
12134 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012135 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012136 filetype_plugin = TRUE;
12137 }
12138 if (indent)
12139 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012140 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141 filetype_indent = TRUE;
12142 }
12143 }
12144 if (*arg == 'd')
12145 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012146 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012147 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012148 }
12149 }
12150 else if (STRCMP(arg, "off") == 0)
12151 {
12152 if (plugin || indent)
12153 {
12154 if (plugin)
12155 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012156 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157 filetype_plugin = FALSE;
12158 }
12159 if (indent)
12160 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012161 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162 filetype_indent = FALSE;
12163 }
12164 }
12165 else
12166 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012167 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012168 filetype_detect = FALSE;
12169 }
12170 }
12171 else
12172 EMSG2(_(e_invarg2), arg);
12173}
12174
12175/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012176 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177 */
12178 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012179ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012180{
12181 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012182 {
12183 char_u *arg = eap->arg;
12184
12185 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12186 arg += 9;
12187
12188 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12189 if (arg != eap->arg)
12190 did_filetype = FALSE;
12191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012192}
12193#endif
12194
Bram Moolenaar071d4272004-06-13 20:20:40 +000012195 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012196ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012197{
12198#ifdef FEAT_DIGRAPHS
12199 if (*eap->arg != NUL)
12200 putdigraph(eap->arg);
12201 else
12202 listdigraphs();
12203#else
12204 EMSG(_("E196: No digraphs in this version"));
12205#endif
12206}
12207
12208 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012209ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012210{
12211 int flags = 0;
12212
12213 if (eap->cmdidx == CMD_setlocal)
12214 flags = OPT_LOCAL;
12215 else if (eap->cmdidx == CMD_setglobal)
12216 flags = OPT_GLOBAL;
12217#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
12218 if (cmdmod.browse && flags == 0)
12219 ex_options(eap);
12220 else
12221#endif
12222 (void)do_set(eap->arg, flags);
12223}
12224
12225#ifdef FEAT_SEARCH_EXTRA
12226/*
12227 * ":nohlsearch"
12228 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012229 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012230ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012231{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010012232 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012233 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234}
12235
12236/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012237 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012238 * Sets nextcmd to the start of the next command, if any. Also called when
12239 * skipping commands to find the next command.
12240 */
12241 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012242ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012243{
12244 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012245 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012246 char_u *end;
12247 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012248 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012249
12250 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012251 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012252 else
12253 {
12254 EMSG(e_invcmd);
12255 return;
12256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012257
12258 /* First clear any old pattern. */
12259 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012260 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261
12262 if (ends_excmd(*eap->arg))
12263 end = eap->arg;
12264 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012265 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266 end = eap->arg + 4;
12267 else
12268 {
12269 p = skiptowhite(eap->arg);
12270 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012271 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012272 p = skipwhite(p);
12273 if (*p == NUL)
12274 {
12275 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012276 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012277 EMSG2(_(e_invarg2), eap->arg);
12278 return;
12279 }
12280 end = skip_regexp(p + 1, *p, TRUE, NULL);
12281 if (!eap->skip)
12282 {
12283 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12284 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012285 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012286 eap->errmsg = e_trailing;
12287 return;
12288 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012289 if (*end != *p)
12290 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012291 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012292 EMSG2(_(e_invarg2), p);
12293 return;
12294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295
12296 c = *end;
12297 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012298 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012299 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012300 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301 }
12302 }
12303 eap->nextcmd = find_nextcmd(end);
12304}
12305#endif
12306
12307#ifdef FEAT_CRYPT
12308/*
12309 * ":X": Get crypt key
12310 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012311 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012312ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012314 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012315 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012316}
12317#endif
12318
12319#ifdef FEAT_FOLDING
12320 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012321ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322{
12323 if (foldManualAllowed(TRUE))
12324 foldCreate(eap->line1, eap->line2);
12325}
12326
12327 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012328ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329{
12330 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12331 eap->forceit, FALSE);
12332}
12333
12334 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012335ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336{
12337 linenr_T lnum;
12338
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012339#ifdef FEAT_CLIPBOARD
12340 start_global_changes();
12341#endif
12342
Bram Moolenaar071d4272004-06-13 20:20:40 +000012343 /* First set the marks for all lines closed/open. */
12344 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12345 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12346 ml_setmarked(lnum);
12347
12348 /* Execute the command on the marked lines. */
12349 global_exe(eap->arg);
12350 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012351#ifdef FEAT_CLIPBOARD
12352 end_global_changes();
12353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354}
12355#endif