blob: 5ad094ca81f488d3fcd2c3c54f31cd49288c41d4 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
22#ifdef FEAT_USR_CMDS
23typedef struct ucmd
24{
25 char_u *uc_name; /* The command name */
26 long_u uc_argt; /* The argument type */
27 char_u *uc_rep; /* The command's replacement string */
28 long uc_def; /* The default value for a range/count */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int uc_compl; /* completion type */
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +010030 int uc_addr_type; /* The command's address type */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010031# ifdef FEAT_EVAL
32 scid_T uc_scriptID; /* SID where the command was defined */
33# ifdef FEAT_CMDL_COMPL
Bram Moolenaar071d4272004-06-13 20:20:40 +000034 char_u *uc_compl_arg; /* completion argument if any */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010035# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000036# endif
37} ucmd_T;
38
39#define UC_BUFFER 1 /* -buffer: local to current buffer */
40
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000041static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
43#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
44#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
45
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010046static void do_ucmd(exarg_T *eap);
47static void ex_command(exarg_T *eap);
48static void ex_delcommand(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# ifdef FEAT_CMDL_COMPL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010050static char_u *get_user_command_name(int idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051# endif
52
Bram Moolenaar958636c2014-10-21 20:01:58 +020053/* Wether a command index indicates a user command. */
54# define IS_USER_CMDIDX(idx) ((int)(idx) < 0)
55
Bram Moolenaar071d4272004-06-13 20:20:40 +000056#else
57# define ex_command ex_ni
58# define ex_comclear ex_ni
59# define ex_delcommand ex_ni
Bram Moolenaar958636c2014-10-21 20:01:58 +020060/* Wether a command index indicates a user command. */
61# define IS_USER_CMDIDX(idx) (FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000062#endif
63
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010064static int compute_buffer_local_count(int addr_type, int lnum, int local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000065#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010066static char_u *do_one_cmd(char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000067#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010068static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000069static int if_level = 0; /* depth in :if */
70#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010071static void append_command(char_u *cmd);
72static char_u *find_command(exarg_T *eap, int *full);
Bram Moolenaar071d4272004-06-13 20:20:40 +000073
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010074static void ex_abbreviate(exarg_T *eap);
75static void ex_map(exarg_T *eap);
76static void ex_unmap(exarg_T *eap);
77static void ex_mapclear(exarg_T *eap);
78static void ex_abclear(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#ifndef FEAT_MENU
80# define ex_emenu ex_ni
81# define ex_menu ex_ni
82# define ex_menutranslate ex_ni
83#endif
84#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static void ex_autocmd(exarg_T *eap);
86static void ex_doautocmd(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#else
88# define ex_autocmd ex_ni
89# define ex_doautocmd ex_ni
90# define ex_doautoall ex_ni
91#endif
92#ifdef FEAT_LISTCMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010093static void ex_bunload(exarg_T *eap);
94static void ex_buffer(exarg_T *eap);
95static void ex_bmodified(exarg_T *eap);
96static void ex_bnext(exarg_T *eap);
97static void ex_bprevious(exarg_T *eap);
98static void ex_brewind(exarg_T *eap);
99static void ex_blast(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#else
101# define ex_bunload ex_ni
102# define ex_buffer ex_ni
103# define ex_bmodified ex_ni
104# define ex_bnext ex_ni
105# define ex_bprevious ex_ni
106# define ex_brewind ex_ni
107# define ex_blast ex_ni
108# define buflist_list ex_ni
109# define ex_checktime ex_ni
110#endif
111#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
112# define ex_buffer_all ex_ni
113#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100114static char_u *getargcmd(char_u **);
115static char_u *skip_cmd_arg(char_u *p, int rembs);
116static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#ifndef FEAT_QUICKFIX
118# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000119# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120# define ex_cc ex_ni
121# define ex_cnext ex_ni
122# define ex_cfile ex_ni
123# define qf_list ex_ni
124# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200125# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000127# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#endif
129#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
130# define ex_cclose ex_ni
131# define ex_copen ex_ni
132# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +0200133# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000135#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
136# define ex_cexpr ex_ni
137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100139static int check_more(int, int);
Bram Moolenaarded27822017-01-02 14:27:34 +0100140static linenr_T get_address(exarg_T *, char_u **, int addr_type, int skip, int to_other_file, int address_count);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100141static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200142#if !defined(FEAT_PERL) \
143 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
144 || !defined(FEAT_TCL) \
145 || !defined(FEAT_RUBY) \
146 || !defined(FEAT_LUA) \
147 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000148# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100149static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100151static char_u *invalid_range(exarg_T *eap);
152static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000153#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100154static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000155#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100156static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
157static void ex_highlight(exarg_T *eap);
158static void ex_colorscheme(exarg_T *eap);
159static void ex_quit(exarg_T *eap);
160static void ex_cquit(exarg_T *eap);
161static void ex_quit_all(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100163static void ex_close(exarg_T *eap);
164static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
165static void ex_only(exarg_T *eap);
166static void ex_resize(exarg_T *eap);
167static void ex_stag(exarg_T *eap);
168static void ex_tabclose(exarg_T *eap);
169static void ex_tabonly(exarg_T *eap);
170static void ex_tabnext(exarg_T *eap);
171static void ex_tabmove(exarg_T *eap);
172static void ex_tabs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173#else
174# define ex_close ex_ni
175# define ex_only ex_ni
176# define ex_all ex_ni
177# define ex_resize ex_ni
178# define ex_splitview ex_ni
179# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000180# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000181# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000182# define ex_tabs ex_ni
183# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000184# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185#endif
186#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100187static void ex_pclose(exarg_T *eap);
188static void ex_ptag(exarg_T *eap);
189static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190#else
191# define ex_pclose ex_ni
192# define ex_ptag ex_ni
193# define ex_pedit ex_ni
194#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100195static void ex_hide(exarg_T *eap);
196static void ex_stop(exarg_T *eap);
197static void ex_exit(exarg_T *eap);
198static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100200static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#else
202# define ex_goto ex_ni
203#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100204static void ex_shell(exarg_T *eap);
205static void ex_preserve(exarg_T *eap);
206static void ex_recover(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207#ifndef FEAT_LISTCMDS
208# define ex_argedit ex_ni
209# define ex_argadd ex_ni
210# define ex_argdelete ex_ni
211# define ex_listdo ex_ni
212#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100213static void ex_mode(exarg_T *eap);
214static void ex_wrongmodifier(exarg_T *eap);
215static void ex_find(exarg_T *eap);
216static void ex_open(exarg_T *eap);
217static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
219# define ex_drop ex_ni
220#endif
221#ifndef FEAT_GUI
222# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#endif
225#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100226static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227#else
228# define ex_tearoff ex_ni
229#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000230#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100231static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232#else
233# define ex_popup ex_ni
234#endif
235#ifndef FEAT_GUI_MSWIN
236# define ex_simalt ex_ni
237#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000238#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239# define gui_mch_find_dialog ex_ni
240# define gui_mch_replace_dialog ex_ni
241#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000242#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243# define ex_helpfind ex_ni
244#endif
245#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100246# define ex_cscope ex_ni
247# define ex_scscope ex_ni
248# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249#endif
250#ifndef FEAT_SYN_HL
251# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200252# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000253#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200254#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200255# define ex_syntime ex_ni
256#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000257#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000258# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000259# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000260# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000261# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000262# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000263#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200264#ifndef FEAT_PERSISTENT_UNDO
265# define ex_rundo ex_ni
266# define ex_wundo ex_ni
267#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200268#ifndef FEAT_LUA
269# define ex_lua ex_script_ni
270# define ex_luado ex_ni
271# define ex_luafile ex_ni
272#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000273#ifndef FEAT_MZSCHEME
274# define ex_mzscheme ex_script_ni
275# define ex_mzfile ex_ni
276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#ifndef FEAT_PERL
278# define ex_perl ex_script_ni
279# define ex_perldo ex_ni
280#endif
281#ifndef FEAT_PYTHON
282# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200283# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284# define ex_pyfile ex_ni
285#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200286#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200287# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200288# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200289# define ex_py3file ex_ni
290#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100291#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
292# define ex_pyx ex_script_ni
293# define ex_pyxdo ex_ni
294# define ex_pyxfile ex_ni
295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296#ifndef FEAT_TCL
297# define ex_tcl ex_script_ni
298# define ex_tcldo ex_ni
299# define ex_tclfile ex_ni
300#endif
301#ifndef FEAT_RUBY
302# define ex_ruby ex_script_ni
303# define ex_rubydo ex_ni
304# define ex_rubyfile ex_ni
305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306#ifndef FEAT_KEYMAP
307# define ex_loadkeymap ex_ni
308#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100309static void ex_swapname(exarg_T *eap);
310static void ex_syncbind(exarg_T *eap);
311static void ex_read(exarg_T *eap);
312static void ex_pwd(exarg_T *eap);
313static void ex_equal(exarg_T *eap);
314static void ex_sleep(exarg_T *eap);
315static void do_exmap(exarg_T *eap, int isabbrev);
316static void ex_winsize(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_wincmd(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319#else
320# define ex_wincmd ex_ni
321#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000322#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100323static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324#else
325# define ex_winpos ex_ni
326#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100327static void ex_operators(exarg_T *eap);
328static void ex_put(exarg_T *eap);
329static void ex_copymove(exarg_T *eap);
330static void ex_submagic(exarg_T *eap);
331static void ex_join(exarg_T *eap);
332static void ex_at(exarg_T *eap);
333static void ex_bang(exarg_T *eap);
334static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200335#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100336static void ex_wundo(exarg_T *eap);
337static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200338#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100339static void ex_redo(exarg_T *eap);
340static void ex_later(exarg_T *eap);
341static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100342static void ex_redrawstatus(exarg_T *eap);
343static void close_redir(void);
344static void ex_mkrc(exarg_T *eap);
345static void ex_mark(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346#ifdef FEAT_USR_CMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100347static char_u *uc_fun_cmd(void);
348static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100350static void ex_startinsert(exarg_T *eap);
351static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100353static void ex_checkpath(exarg_T *eap);
354static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355#else
356# define ex_findpat ex_ni
357# define ex_checkpath ex_ni
358#endif
359#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100360static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361#else
362# define ex_psearch ex_ni
363#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100364static void ex_tag(exarg_T *eap);
365static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366#ifndef FEAT_EVAL
367# define ex_scriptnames ex_ni
368# define ex_finish ex_ni
369# define ex_echo ex_ni
370# define ex_echohl ex_ni
371# define ex_execute ex_ni
372# define ex_call ex_ni
373# define ex_if ex_ni
374# define ex_endif ex_ni
375# define ex_else ex_ni
376# define ex_while ex_ni
377# define ex_continue ex_ni
378# define ex_break ex_ni
379# define ex_endwhile ex_ni
380# define ex_throw ex_ni
381# define ex_try ex_ni
382# define ex_catch ex_ni
383# define ex_finally ex_ni
384# define ex_endtry ex_ni
385# define ex_endfunction ex_ni
386# define ex_let ex_ni
387# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000388# define ex_lockvar ex_ni
389# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390# define ex_function ex_ni
391# define ex_delfunction ex_ni
392# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000393# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100395static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100397static int makeopens(FILE *fd, char_u *dirnow);
398static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
399static void ex_loadview(exarg_T *eap);
400static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000401static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402#else
403# define ex_loadview ex_ni
404#endif
405#ifndef FEAT_EVAL
406# define ex_compiler ex_ni
407#endif
408#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100409static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410#else
411# define ex_viminfo ex_ni
412#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100413static void ex_behave(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100415static void ex_filetype(exarg_T *eap);
416static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417#else
418# define ex_filetype ex_ni
419# define ex_setfiletype ex_ni
420#endif
421#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000422# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423# define ex_diffpatch ex_ni
424# define ex_diffgetput ex_ni
425# define ex_diffsplit ex_ni
426# define ex_diffthis ex_ni
427# define ex_diffupdate ex_ni
428#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100429static void ex_digraphs(exarg_T *eap);
430static void ex_set(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
432# define ex_options ex_ni
433#endif
434#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100435static void ex_nohlsearch(exarg_T *eap);
436static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437#else
438# define ex_nohlsearch ex_ni
439# define ex_match ex_ni
440#endif
441#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100442static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443#else
444# define ex_X ex_ni
445#endif
446#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100447static void ex_fold(exarg_T *eap);
448static void ex_foldopen(exarg_T *eap);
449static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450#else
451# define ex_fold ex_ni
452# define ex_foldopen ex_ni
453# define ex_folddo ex_ni
454#endif
455#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
456 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
457# define ex_language ex_ni
458#endif
459#ifndef FEAT_SIGNS
460# define ex_sign ex_ni
461#endif
462#ifndef FEAT_SUN_WORKSHOP
463# define ex_wsverb ex_ni
464#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000465#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200466# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000467# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200468# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470
471#ifndef FEAT_EVAL
472# define ex_debug ex_ni
473# define ex_breakadd ex_ni
474# define ex_debuggreedy ex_ni
475# define ex_breakdel ex_ni
476# define ex_breaklist ex_ni
477#endif
478
479#ifndef FEAT_CMDHIST
480# define ex_history ex_ni
481#endif
482#ifndef FEAT_JUMPLIST
483# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200484# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485# define ex_changes ex_ni
486#endif
487
Bram Moolenaar05159a02005-02-26 23:04:13 +0000488#ifndef FEAT_PROFILE
489# define ex_profile ex_ni
490#endif
491
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492/*
493 * Declare cmdnames[].
494 */
495#define DO_DECLARE_EXCMD
496#include "ex_cmds.h"
497
498/*
499 * Table used to quickly search for a command, based on its first character.
500 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000501static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502{
503 CMD_append,
504 CMD_buffer,
505 CMD_change,
506 CMD_delete,
507 CMD_edit,
508 CMD_file,
509 CMD_global,
510 CMD_help,
511 CMD_insert,
512 CMD_join,
513 CMD_k,
514 CMD_list,
515 CMD_move,
516 CMD_next,
517 CMD_open,
518 CMD_print,
519 CMD_quit,
520 CMD_read,
521 CMD_substitute,
522 CMD_t,
523 CMD_undo,
524 CMD_vglobal,
525 CMD_write,
526 CMD_xit,
527 CMD_yank,
528 CMD_z,
529 CMD_bang
530};
531
532static char_u dollar_command[2] = {'$', 0};
533
534
535#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000536/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537typedef struct
538{
539 char_u *line; /* command line */
540 linenr_T lnum; /* sourcing_lnum of the line */
541} wcmd_T;
542
543/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000544 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000546 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000548struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549{
550 garray_T *lines_gap; /* growarray with line info */
551 int current_line; /* last read line from growarray */
552 int repeating; /* TRUE when looping a second time */
553 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100554 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 void *cookie;
556};
557
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100558static char_u *get_loop_line(int c, void *cookie, int indent);
559static int store_loop_line(garray_T *gap, char_u *line);
560static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000561
562/* Struct to save a few things while debugging. Used in do_cmdline() only. */
563struct dbg_stuff
564{
565 int trylevel;
566 int force_abort;
567 except_T *caught_stack;
568 char_u *vv_exception;
569 char_u *vv_throwpoint;
570 int did_emsg;
571 int got_int;
572 int did_throw;
573 int need_rethrow;
574 int check_cstack;
575 except_T *current_exception;
576};
577
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100578static void save_dbg_stuff(struct dbg_stuff *dsp);
579static void restore_dbg_stuff(struct dbg_stuff *dsp);
Bram Moolenaared203462004-06-16 11:19:22 +0000580
581 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100582save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000583{
584 dsp->trylevel = trylevel; trylevel = 0;
585 dsp->force_abort = force_abort; force_abort = FALSE;
586 dsp->caught_stack = caught_stack; caught_stack = NULL;
587 dsp->vv_exception = v_exception(NULL);
588 dsp->vv_throwpoint = v_throwpoint(NULL);
589
590 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
591 dsp->did_emsg = did_emsg; did_emsg = FALSE;
592 dsp->got_int = got_int; got_int = FALSE;
593 dsp->did_throw = did_throw; did_throw = FALSE;
594 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
595 dsp->check_cstack = check_cstack; check_cstack = FALSE;
596 dsp->current_exception = current_exception; current_exception = NULL;
597}
598
599 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100600restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000601{
602 suppress_errthrow = FALSE;
603 trylevel = dsp->trylevel;
604 force_abort = dsp->force_abort;
605 caught_stack = dsp->caught_stack;
606 (void)v_exception(dsp->vv_exception);
607 (void)v_throwpoint(dsp->vv_throwpoint);
608 did_emsg = dsp->did_emsg;
609 got_int = dsp->got_int;
610 did_throw = dsp->did_throw;
611 need_rethrow = dsp->need_rethrow;
612 check_cstack = dsp->check_cstack;
613 current_exception = dsp->current_exception;
614}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615#endif
616
617
618/*
619 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
620 * command is given.
621 */
622 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100623do_exmode(
624 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625{
626 int save_msg_scroll;
627 int prev_msg_row;
628 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100629 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000630
631 if (improved)
632 exmode_active = EXMODE_VIM;
633 else
634 exmode_active = EXMODE_NORMAL;
635 State = NORMAL;
636
637 /* When using ":global /pat/ visual" and then "Q" we return to continue
638 * the :global command. */
639 if (global_busy)
640 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641
642 save_msg_scroll = msg_scroll;
643 ++RedrawingDisabled; /* don't redisplay the window */
644 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645#ifdef FEAT_GUI
646 /* Ignore scrollbar and mouse events in Ex mode */
647 ++hold_gui_events;
648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
650 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
651 while (exmode_active)
652 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000653 /* Check for a ":normal" command and no more characters left. */
654 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
655 {
656 exmode_active = FALSE;
657 break;
658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 msg_scroll = TRUE;
660 need_wait_return = FALSE;
661 ex_pressedreturn = FALSE;
662 ex_no_reprint = FALSE;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100663 changedtick = *curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 prev_msg_row = msg_row;
665 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 if (improved)
667 {
668 cmdline_row = msg_row;
669 do_cmdline(NULL, getexline, NULL, 0);
670 }
671 else
672 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
673 lines_left = Rows - 1;
674
Bram Moolenaardf177f62005-02-22 08:39:57 +0000675 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar79518e22017-02-17 16:31:35 +0100676 || changedtick != *curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000678 if (curbuf->b_ml.ml_flags & ML_EMPTY)
679 EMSG(_(e_emptybuf));
680 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000682 if (ex_pressedreturn)
683 {
684 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000685 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000686 msg_row = prev_msg_row;
687 if (prev_msg_row == Rows - 1)
688 msg_row--;
689 }
690 msg_col = 0;
691 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
692 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000695 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
696 {
697 if (curbuf->b_ml.ml_flags & ML_EMPTY)
698 EMSG(_(e_emptybuf));
699 else
700 EMSG(_("E501: At end-of-file"));
701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 }
703
704#ifdef FEAT_GUI
705 --hold_gui_events;
706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 --RedrawingDisabled;
708 --no_wait_return;
709 update_screen(CLEAR);
710 need_wait_return = FALSE;
711 msg_scroll = save_msg_scroll;
712}
713
714/*
715 * Execute a simple command line. Used for translated commands like "*".
716 */
717 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100718do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719{
720 return do_cmdline(cmd, NULL, NULL,
721 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
722}
723
724/*
725 * do_cmdline(): execute one Ex command line
726 *
727 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100728 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 * 2. Split up in parts separated with '|'.
730 *
731 * This function can be called recursively!
732 *
733 * flags:
734 * DOCMD_VERBOSE - The command will be included in the error message.
735 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100736 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 * DOCMD_KEYTYPED - Don't reset KeyTyped.
738 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
739 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
740 *
741 * return FAIL if cmdline could not be executed, OK otherwise
742 */
743 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100744do_cmdline(
745 char_u *cmdline,
746 char_u *(*fgetline)(int, void *, int),
747 void *cookie, /* argument for fgetline() */
748 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749{
750 char_u *next_cmdline; /* next cmd to execute */
751 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100752 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 static int recursive = 0; /* recursive depth */
754 int msg_didout_before_start = 0;
755 int count = 0; /* line number count */
756 int did_inc = FALSE; /* incremented RedrawingDisabled */
757 int retval = OK;
758#ifdef FEAT_EVAL
759 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000760 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 int current_line = 0; /* active line in lines_ga */
762 char_u *fname = NULL; /* function or script name */
763 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
764 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000765 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 int initial_trylevel;
767 struct msglist **saved_msg_list = NULL;
768 struct msglist *private_msg_list;
769
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100770 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100771 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000773 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000775 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100777# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778# define cmd_cookie cookie
779#endif
780 static int call_depth = 0; /* recursiveness */
781
782#ifdef FEAT_EVAL
783 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
784 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000785 * This ensures that the do_errthrow() call in do_one_cmd() does not
786 * combine the messages stored by an earlier invocation of do_one_cmd()
787 * with the command name of the later one. This would happen when
788 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 saved_msg_list = msg_list;
790 msg_list = &private_msg_list;
791 private_msg_list = NULL;
792#endif
793
794 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100795 * here. The value of 200 allows nested function calls, ":source", etc.
796 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100797 if (call_depth >= 200
798#ifdef FEAT_EVAL
799 && call_depth >= p_mfd
800#endif
801 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 {
803 EMSG(_("E169: Command too recursive"));
804#ifdef FEAT_EVAL
805 /* When converting to an exception, we do not include the command name
806 * since this is not an error of the specific command. */
807 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
808 msg_list = saved_msg_list;
809#endif
810 return FAIL;
811 }
812 ++call_depth;
813
814#ifdef FEAT_EVAL
815 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000816 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 cstack.cs_trylevel = 0;
818 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000819 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
821
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100822 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823
824 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100825 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000826 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 ++ex_nesting_level;
828
829 /* Get the function or script name and the address where the next breakpoint
830 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000831 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 {
833 fname = func_name(real_cookie);
834 breakpoint = func_breakpoint(real_cookie);
835 dbg_tick = func_dbg_tick(real_cookie);
836 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100837 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 {
839 fname = sourcing_name;
840 breakpoint = source_breakpoint(real_cookie);
841 dbg_tick = source_dbg_tick(real_cookie);
842 }
843
844 /*
845 * Initialize "force_abort" and "suppress_errthrow" at the top level.
846 */
847 if (!recursive)
848 {
849 force_abort = FALSE;
850 suppress_errthrow = FALSE;
851 }
852
853 /*
854 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000855 * exception handling (used when debugging). Otherwise clear it to avoid
856 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000858 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000859 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000860 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200861 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862
863 initial_trylevel = trylevel;
864
865 /*
866 * "did_throw" will be set to TRUE when an exception is being thrown.
867 */
868 did_throw = FALSE;
869#endif
870 /*
871 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000872 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 * If force_abort is set, we cancel everything.
874 */
875 did_emsg = FALSE;
876
877 /*
878 * KeyTyped is only set when calling vgetc(). Reset it here when not
879 * calling vgetc() (sourced command lines).
880 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100881 if (!(flags & DOCMD_KEYTYPED)
882 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 KeyTyped = FALSE;
884
885 /*
886 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000887 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 * - for multiple commands on one line, separated with '|'
889 * - when repeating until there are no more lines (for ":source")
890 */
891 next_cmdline = cmdline;
892 do
893 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000894#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100895 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000896#endif
897
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000898 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 if (next_cmdline == NULL
900#ifdef FEAT_EVAL
901 && !force_abort
902 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000903 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904#endif
905 )
906 did_emsg = FALSE;
907
908 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000909 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100910 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 * 3. If a line is given: Make a copy, so we can mess with it.
912 */
913
914#ifdef FEAT_EVAL
915 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000916 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 {
918 /* Each '|' separated command is stored separately in lines_ga, to
919 * be able to jump to it. Don't use next_cmdline now. */
920 vim_free(cmdline_copy);
921 cmdline_copy = NULL;
922
923 /* Check if a function has returned or, unless it has an unclosed
924 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000925 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000927# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000928 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000929 func_line_end(real_cookie);
930# endif
931 if (func_has_ended(real_cookie))
932 {
933 retval = FAIL;
934 break;
935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000937#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000938 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100939 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000940 script_line_end();
941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942
943 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100944 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 {
946 retval = FAIL;
947 break;
948 }
949
950 /* If breakpoints have been added/deleted need to check for it. */
951 if (breakpoint != NULL && dbg_tick != NULL
952 && *dbg_tick != debug_tick)
953 {
954 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100955 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 fname, sourcing_lnum);
957 *dbg_tick = debug_tick;
958 }
959
960 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
961 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
962
963 /* Did we encounter a breakpoint? */
964 if (breakpoint != NULL && *breakpoint != 0
965 && *breakpoint <= sourcing_lnum)
966 {
967 dbg_breakpoint(fname, sourcing_lnum);
968 /* Find next breakpoint. */
969 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100970 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 fname, sourcing_lnum);
972 *dbg_tick = debug_tick;
973 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000974# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000975 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000976 {
977 if (getline_is_func)
978 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100979 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000980 script_line_start();
981 }
982# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 }
984
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000985 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000987 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100988 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 * below, so that it stores lines in or reads them from
990 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000991 * while/for loop. */
992 cmd_getline = get_loop_line;
993 cmd_cookie = (void *)&cmd_loop_cookie;
994 cmd_loop_cookie.lines_gap = &lines_ga;
995 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100996 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000997 cmd_loop_cookie.cookie = cookie;
998 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 }
1000 else
1001 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001002 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 cmd_cookie = cookie;
1004 }
1005#endif
1006
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001007 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 if (next_cmdline == NULL)
1009 {
1010 /*
1011 * Need to set msg_didout for the first line after an ":if",
1012 * otherwise the ":if" will be overwritten.
1013 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001014 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001016 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017#ifdef FEAT_EVAL
1018 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1019#else
1020 0
1021#endif
1022 )) == NULL)
1023 {
1024 /* Don't call wait_return for aborted command line. The NULL
1025 * returned for the end of a sourced file or executed function
1026 * doesn't do this. */
1027 if (KeyTyped && !(flags & DOCMD_REPEAT))
1028 need_wait_return = FALSE;
1029 retval = FAIL;
1030 break;
1031 }
1032 used_getline = TRUE;
1033
1034 /*
1035 * Keep the first typed line. Clear it when more lines are typed.
1036 */
1037 if (flags & DOCMD_KEEPLINE)
1038 {
1039 vim_free(repeat_cmdline);
1040 if (count == 0)
1041 repeat_cmdline = vim_strsave(next_cmdline);
1042 else
1043 repeat_cmdline = NULL;
1044 }
1045 }
1046
1047 /* 3. Make a copy of the command so we can mess with it. */
1048 else if (cmdline_copy == NULL)
1049 {
1050 next_cmdline = vim_strsave(next_cmdline);
1051 if (next_cmdline == NULL)
1052 {
1053 EMSG(_(e_outofmem));
1054 retval = FAIL;
1055 break;
1056 }
1057 }
1058 cmdline_copy = next_cmdline;
1059
1060#ifdef FEAT_EVAL
1061 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001062 * Save the current line when inside a ":while" or ":for", and when
1063 * the command looks like a ":while" or ":for", because we may need it
1064 * later. When there is a '|' and another command, it is stored
1065 * separately, because we need to be able to jump back to it from an
1066 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001068 if (current_line == lines_ga.ga_len
1069 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001071 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 {
1073 retval = FAIL;
1074 break;
1075 }
1076 }
1077 did_endif = FALSE;
1078#endif
1079
1080 if (count++ == 0)
1081 {
1082 /*
1083 * All output from the commands is put below each other, without
1084 * waiting for a return. Don't do this when executing commands
1085 * from a script or when being called recursive (e.g. for ":e
1086 * +command file").
1087 */
1088 if (!(flags & DOCMD_NOWAIT) && !recursive)
1089 {
1090 msg_didout_before_start = msg_didout;
1091 msg_didany = FALSE; /* no output yet */
1092 msg_start();
1093 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001094 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 ++RedrawingDisabled;
1096 did_inc = TRUE;
1097 }
1098 }
1099
1100 if (p_verbose >= 15 && sourcing_name != NULL)
1101 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001103 verbose_enter_scroll();
1104
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 smsg((char_u *)_("line %ld: %s"),
1106 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001107 if (msg_silent == 0)
1108 msg_puts((char_u *)"\n"); /* don't overwrite this */
1109
1110 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 --no_wait_return;
1112 }
1113
1114 /*
1115 * 2. Execute one '|' separated command.
1116 * do_one_cmd() will return NULL if there is no trailing '|'.
1117 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1118 */
1119 ++recursive;
1120 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1121#ifdef FEAT_EVAL
1122 &cstack,
1123#endif
1124 cmd_getline, cmd_cookie);
1125 --recursive;
1126
1127#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001128 if (cmd_cookie == (void *)&cmd_loop_cookie)
1129 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001131 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132#endif
1133
1134 if (next_cmdline == NULL)
1135 {
1136 vim_free(cmdline_copy);
1137 cmdline_copy = NULL;
1138#ifdef FEAT_CMDHIST
1139 /*
1140 * If the command was typed, remember it for the ':' register.
1141 * Do this AFTER executing the command to make :@: work.
1142 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001143 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 && new_last_cmdline != NULL)
1145 {
1146 vim_free(last_cmdline);
1147 last_cmdline = new_last_cmdline;
1148 new_last_cmdline = NULL;
1149 }
1150#endif
1151 }
1152 else
1153 {
1154 /* need to copy the command after the '|' to cmdline_copy, for the
1155 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001156 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 next_cmdline = cmdline_copy;
1158 }
1159
1160
1161#ifdef FEAT_EVAL
1162 /* reset did_emsg for a function that is not aborted by an error */
1163 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001164 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 && !func_has_abort(real_cookie))
1166 did_emsg = FALSE;
1167
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001168 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 {
1170 ++current_line;
1171
1172 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001173 * An ":endwhile", ":endfor" and ":continue" is handled here.
1174 * If we were executing commands, jump back to the ":while" or
1175 * ":for".
1176 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001178 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001180 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001182 /* Jump back to the matching ":while" or ":for". Be careful
1183 * not to use a cs_line[] from an entry that isn't a ":while"
1184 * or ":for": It would make "current_line" invalid and can
1185 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 if (!did_emsg && !got_int && !did_throw
1187 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001188 && (cstack.cs_flags[cstack.cs_idx]
1189 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 && cstack.cs_line[cstack.cs_idx] >= 0
1191 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1192 {
1193 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001194 /* remember we jumped there */
1195 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 line_breakcheck(); /* check if CTRL-C typed */
1197
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001198 /* Check for the next breakpoint at or after the ":while"
1199 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 if (breakpoint != NULL)
1201 {
1202 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001203 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 fname,
1205 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1206 *dbg_tick = debug_tick;
1207 }
1208 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001209 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001211 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001213 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1214 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 }
1216 }
1217
1218 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001219 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001221 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001223 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1225 }
1226 }
1227
1228 /*
1229 * When not inside any ":while" loop, clear remembered lines.
1230 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001231 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 {
1233 if (lines_ga.ga_len > 0)
1234 {
1235 sourcing_lnum =
1236 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1237 free_cmdlines(&lines_ga);
1238 }
1239 current_line = 0;
1240 }
1241
1242 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001243 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1244 * being restored at the ":endtry". Reset them here and set the
1245 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1246 * This includes the case where a missing ":endif", ":endwhile" or
1247 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001249 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001251 cstack.cs_lflags &= ~CSL_HAD_FINA;
1252 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1253 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 did_throw ? (void *)current_exception : NULL);
1255 did_emsg = got_int = did_throw = FALSE;
1256 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1257 }
1258
1259 /* Update global "trylevel" for recursive calls to do_cmdline() from
1260 * within this loop. */
1261 trylevel = initial_trylevel + cstack.cs_trylevel;
1262
1263 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001264 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 * files) is aborted because of an error, an interrupt, or an uncaught
1266 * exception, cancel everything. If it is left normally, reset
1267 * force_abort to get the non-EH compatible abortion behavior for
1268 * the rest of the script.
1269 */
1270 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1271 force_abort = FALSE;
1272
1273 /* Convert an interrupt to an exception if appropriate. */
1274 (void)do_intthrow(&cstack);
1275#endif /* FEAT_EVAL */
1276
1277 }
1278 /*
1279 * Continue executing command lines when:
1280 * - no CTRL-C typed, no aborting error, no exception thrown or try
1281 * conditionals need to be checked for executing finally clauses or
1282 * catching an interrupt exception
1283 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001284 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 * looping for ":source" command or function call.
1286 */
1287 while (!((got_int
1288#ifdef FEAT_EVAL
1289 || (did_emsg && force_abort) || did_throw
1290#endif
1291 )
1292#ifdef FEAT_EVAL
1293 && cstack.cs_trylevel == 0
1294#endif
1295 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001296 && !(did_emsg
1297#ifdef FEAT_EVAL
1298 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001299 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001300 * the :endtry to be missed. */
1301 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1302#endif
1303 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001304 && (getline_equal(fgetline, cookie, getexmodeline)
1305 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 && (next_cmdline != NULL
1307#ifdef FEAT_EVAL
1308 || cstack.cs_idx >= 0
1309#endif
1310 || (flags & DOCMD_REPEAT)));
1311
1312 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001313 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314#ifdef FEAT_EVAL
1315 free_cmdlines(&lines_ga);
1316 ga_clear(&lines_ga);
1317
1318 if (cstack.cs_idx >= 0)
1319 {
1320 /*
1321 * If a sourced file or executed function ran to its end, report the
1322 * unclosed conditional.
1323 */
1324 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001325 && ((getline_equal(fgetline, cookie, getsourceline)
1326 && !source_finished(fgetline, cookie))
1327 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 && !func_has_ended(real_cookie))))
1329 {
1330 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1331 EMSG(_(e_endtry));
1332 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1333 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001334 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1335 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 else
1337 EMSG(_(e_endif));
1338 }
1339
1340 /*
1341 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1342 * ":endtry" in a sourced file or executed function. If the try
1343 * conditional is in its finally clause, ignore anything pending.
1344 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001345 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 */
1347 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001348 {
1349 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1350
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001351 if (idx >= 0)
1352 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001353 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1354 &cstack.cs_looplevel);
1355 }
1356 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 trylevel = initial_trylevel;
1358 }
1359
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001360 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1361 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001363 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 ? (char_u *)"endfunction" : (char_u *)NULL);
1365
1366 if (trylevel == 0)
1367 {
1368 /*
1369 * When an exception is being thrown out of the outermost try
1370 * conditional, discard the uncaught exception, disable the conversion
1371 * of interrupts or errors to exceptions, and ensure that no more
1372 * commands are executed.
1373 */
1374 if (did_throw)
1375 {
1376 void *p = NULL;
1377 char_u *saved_sourcing_name;
1378 int saved_sourcing_lnum;
1379 struct msglist *messages = NULL, *next;
1380
1381 /*
1382 * If the uncaught exception is a user exception, report it as an
1383 * error. If it is an error exception, display the saved error
1384 * message now. For an interrupt exception, do nothing; the
1385 * interrupt message is given elsewhere.
1386 */
1387 switch (current_exception->type)
1388 {
1389 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001390 vim_snprintf((char *)IObuff, IOSIZE,
1391 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 current_exception->value);
1393 p = vim_strsave(IObuff);
1394 break;
1395 case ET_ERROR:
1396 messages = current_exception->messages;
1397 current_exception->messages = NULL;
1398 break;
1399 case ET_INTERRUPT:
1400 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 }
1402
1403 saved_sourcing_name = sourcing_name;
1404 saved_sourcing_lnum = sourcing_lnum;
1405 sourcing_name = current_exception->throw_name;
1406 sourcing_lnum = current_exception->throw_lnum;
1407 current_exception->throw_name = NULL;
1408
1409 discard_current_exception(); /* uses IObuff if 'verbose' */
1410 suppress_errthrow = TRUE;
1411 force_abort = TRUE;
1412
1413 if (messages != NULL)
1414 {
1415 do
1416 {
1417 next = messages->next;
1418 emsg(messages->msg);
1419 vim_free(messages->msg);
1420 vim_free(messages);
1421 messages = next;
1422 }
1423 while (messages != NULL);
1424 }
1425 else if (p != NULL)
1426 {
1427 emsg(p);
1428 vim_free(p);
1429 }
1430 vim_free(sourcing_name);
1431 sourcing_name = saved_sourcing_name;
1432 sourcing_lnum = saved_sourcing_lnum;
1433 }
1434
1435 /*
1436 * On an interrupt or an aborting error not converted to an exception,
1437 * disable the conversion of errors to exceptions. (Interrupts are not
1438 * converted any more, here.) This enables also the interrupt message
1439 * when force_abort is set and did_emsg unset in case of an interrupt
1440 * from a finally clause after an error.
1441 */
1442 else if (got_int || (did_emsg && force_abort))
1443 suppress_errthrow = TRUE;
1444 }
1445
1446 /*
1447 * The current cstack will be freed when do_cmdline() returns. An uncaught
1448 * exception will have to be rethrown in the previous cstack. If a function
1449 * has just returned or a script file was just finished and the previous
1450 * cstack belongs to the same function or, respectively, script file, it
1451 * will have to be checked for finally clauses to be executed due to the
1452 * ":return" or ":finish". This is done in do_one_cmd().
1453 */
1454 if (did_throw)
1455 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001456 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001458 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 && ex_nesting_level > func_level(real_cookie) + 1))
1460 {
1461 if (!did_throw)
1462 check_cstack = TRUE;
1463 }
1464 else
1465 {
1466 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001467 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 --ex_nesting_level;
1469 /*
1470 * Go to debug mode when returning from a function in which we are
1471 * single-stepping.
1472 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001473 if ((getline_equal(fgetline, cookie, getsourceline)
1474 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001476 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 ? (char_u *)_("End of sourced file")
1478 : (char_u *)_("End of function"));
1479 }
1480
1481 /*
1482 * Restore the exception environment (done after returning from the
1483 * debugger).
1484 */
1485 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001486 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487
1488 msg_list = saved_msg_list;
1489#endif /* FEAT_EVAL */
1490
1491 /*
1492 * If there was too much output to fit on the command line, ask the user to
1493 * hit return before redrawing the screen. With the ":global" command we do
1494 * this only once after the command is finished.
1495 */
1496 if (did_inc)
1497 {
1498 --RedrawingDisabled;
1499 --no_wait_return;
1500 msg_scroll = FALSE;
1501
1502 /*
1503 * When just finished an ":if"-":else" which was typed, no need to
1504 * wait for hit-return. Also for an error situation.
1505 */
1506 if (retval == FAIL
1507#ifdef FEAT_EVAL
1508 || (did_endif && KeyTyped && !did_emsg)
1509#endif
1510 )
1511 {
1512 need_wait_return = FALSE;
1513 msg_didany = FALSE; /* don't wait when restarting edit */
1514 }
1515 else if (need_wait_return)
1516 {
1517 /*
1518 * The msg_start() above clears msg_didout. The wait_return we do
1519 * here should not overwrite the command that may be shown before
1520 * doing that.
1521 */
1522 msg_didout |= msg_didout_before_start;
1523 wait_return(FALSE);
1524 }
1525 }
1526
Bram Moolenaar2a942252012-11-28 23:03:07 +01001527#ifdef FEAT_EVAL
1528 did_endif = FALSE; /* in case do_cmdline used recursively */
1529#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 /*
1531 * Reset if_level, in case a sourced script file contains more ":if" than
1532 * ":endif" (could be ":if x | foo | endif").
1533 */
1534 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001535#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001536
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 --call_depth;
1538 return retval;
1539}
1540
1541#ifdef FEAT_EVAL
1542/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001543 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 */
1545 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001546get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001548 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 wcmd_T *wp;
1550 char_u *line;
1551
1552 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1553 {
1554 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001555 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001557 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 if (cp->getline == NULL)
1559 line = getcmdline(c, 0L, indent);
1560 else
1561 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001562 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 ++cp->current_line;
1564
1565 return line;
1566 }
1567
1568 KeyTyped = FALSE;
1569 ++cp->current_line;
1570 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1571 sourcing_lnum = wp->lnum;
1572 return vim_strsave(wp->line);
1573}
1574
1575/*
1576 * Store a line in "gap" so that a ":while" loop can execute it again.
1577 */
1578 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001579store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580{
1581 if (ga_grow(gap, 1) == FAIL)
1582 return FAIL;
1583 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1584 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1585 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 return OK;
1587}
1588
1589/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001590 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 */
1592 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001593free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594{
1595 while (gap->ga_len > 0)
1596 {
1597 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1598 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 }
1600}
1601#endif
1602
1603/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001604 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1605 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001608getline_equal(
1609 char_u *(*fgetline)(int, void *, int),
1610 void *cookie UNUSED, /* argument for fgetline() */
1611 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612{
1613#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001614 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001615 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616
Bram Moolenaar89d40322006-08-29 15:30:07 +00001617 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001618 * function that's originally used to obtain the lines. This may be
1619 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001620 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001621 cp = (struct loop_cookie *)cookie;
1622 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 {
1624 gp = cp->getline;
1625 cp = cp->cookie;
1626 }
1627 return gp == func;
1628#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001629 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630#endif
1631}
1632
1633#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1634/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001635 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 * getline function. Otherwise return "cookie".
1637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001639getline_cookie(
1640 char_u *(*fgetline)(int, void *, int) UNUSED,
1641 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642{
1643# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001644 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001645 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646
Bram Moolenaar89d40322006-08-29 15:30:07 +00001647 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001648 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001650 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001651 cp = (struct loop_cookie *)cookie;
1652 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 {
1654 gp = cp->getline;
1655 cp = cp->cookie;
1656 }
1657 return cp;
1658# else
1659 return cookie;
1660# endif
1661}
1662#endif
1663
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001664
1665/*
1666 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1667 * ":bwipeout", etc.
1668 * Returns the buffer number.
1669 */
1670 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001671compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001672{
1673 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001674 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001675 int count = offset;
1676
1677 buf = firstbuf;
1678 while (buf->b_next != NULL && buf->b_fnum < lnum)
1679 buf = buf->b_next;
1680 while (count != 0)
1681 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001682 count += (offset < 0) ? 1 : -1;
1683 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1684 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001685 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001686 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001687 if (addr_type == ADDR_LOADED_BUFFERS)
1688 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001689 while (buf->b_ml.ml_mfp == NULL)
1690 {
1691 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1692 if (nextbuf == NULL)
1693 break;
1694 buf = nextbuf;
1695 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001696 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001697 /* we might have gone too far, last buffer is not loadedd */
1698 if (addr_type == ADDR_LOADED_BUFFERS)
1699 while (buf->b_ml.ml_mfp == NULL)
1700 {
1701 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1702 if (nextbuf == NULL)
1703 break;
1704 buf = nextbuf;
1705 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001706 return buf->b_fnum;
1707}
1708
Bram Moolenaarf240e182014-11-27 18:33:02 +01001709#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001710static int current_win_nr(win_T *win);
1711static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001712
1713 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001714current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001715{
1716 win_T *wp;
1717 int nr = 0;
1718
Bram Moolenaar29323592016-07-24 22:04:11 +02001719 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001720 {
1721 ++nr;
1722 if (wp == win)
1723 break;
1724 }
1725 return nr;
1726}
1727
1728 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001729current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001730{
1731 tabpage_T *tp;
1732 int nr = 0;
1733
Bram Moolenaar29323592016-07-24 22:04:11 +02001734 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001735 {
1736 ++nr;
1737 if (tp == tab)
1738 break;
1739 }
1740 return nr;
1741}
1742
1743# define CURRENT_WIN_NR current_win_nr(curwin)
1744# define LAST_WIN_NR current_win_nr(NULL)
1745# define CURRENT_TAB_NR current_tab_nr(curtab)
1746# define LAST_TAB_NR current_tab_nr(NULL)
1747#else
1748# define CURRENT_WIN_NR 1
1749# define LAST_WIN_NR 1
1750# define CURRENT_TAB_NR 1
1751# define LAST_TAB_NR 1
1752#endif
1753
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001754
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755/*
1756 * Execute one Ex command.
1757 *
1758 * If 'sourcing' is TRUE, the command will be included in the error message.
1759 *
1760 * 1. skip comment lines and leading space
1761 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001762 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001763 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001764 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001765 * 6. parse arguments
1766 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001768 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 *
1770 * This function may be called recursively!
1771 */
1772#if (_MSC_VER == 1200)
1773/*
Bram Moolenaared203462004-06-16 11:19:22 +00001774 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001776 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777#endif
1778 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001779do_one_cmd(
1780 char_u **cmdlinep,
1781 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001783 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001785 char_u *(*fgetline)(int, void *, int),
1786 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787{
1788 char_u *p;
1789 linenr_T lnum;
1790 long n;
1791 char_u *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001792 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 exarg_T ea; /* Ex command arguments */
1794 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001795 int save_msg_scroll = msg_scroll;
1796 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001798#ifdef HAVE_SANDBOX
1799 int did_sandbox = FALSE;
1800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 cmdmod_T save_cmdmod;
1802 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001803 char_u *cmd;
Bram Moolenaarded27822017-01-02 14:27:34 +01001804 int address_count = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805
1806 vim_memset(&ea, 0, sizeof(ea));
1807 ea.line1 = 1;
1808 ea.line2 = 1;
1809#ifdef FEAT_EVAL
1810 ++ex_nesting_level;
1811#endif
1812
Bram Moolenaar21691f82012-12-05 19:13:18 +01001813 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 if (quitmore
1815#ifdef FEAT_EVAL
1816 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001817 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001818#endif
1819#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001820 /* avoid that an autocommand, e.g. QuitPre, does this */
1821 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822#endif
1823 )
1824 --quitmore;
1825
1826 /*
1827 * Reset browse, confirm, etc.. They are restored when returning, for
1828 * recursive calls.
1829 */
1830 save_cmdmod = cmdmod;
1831 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1832
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001833 /* "#!anything" is handled like a comment. */
1834 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1835 goto doend;
1836
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 /*
1838 * Repeat until no more command modifiers are found.
1839 */
1840 ea.cmd = *cmdlinep;
1841 for (;;)
1842 {
1843/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001844 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 */
1846 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1847 ++ea.cmd;
1848
1849 /* in ex mode, an empty line works like :+ */
1850 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001851 && (getline_equal(fgetline, cookie, getexmodeline)
1852 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001853 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 {
1855 ea.cmd = (char_u *)"+";
1856 ex_pressedreturn = TRUE;
1857 }
1858
1859 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001860 if (*ea.cmd == '"')
1861 goto doend;
1862 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001863 {
1864 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867
1868/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001869 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 */
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001871 p = skip_range(ea.cmd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 switch (*p)
1873 {
1874 /* When adding an entry, also modify cmd_exists(). */
1875 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1876 break;
1877#ifdef FEAT_WINDOWS
1878 cmdmod.split |= WSP_ABOVE;
1879#endif
1880 continue;
1881
1882 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1883 {
1884#ifdef FEAT_WINDOWS
1885 cmdmod.split |= WSP_BELOW;
1886#endif
1887 continue;
1888 }
1889 if (checkforcmd(&ea.cmd, "browse", 3))
1890 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001891#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 cmdmod.browse = TRUE;
1893#endif
1894 continue;
1895 }
1896 if (!checkforcmd(&ea.cmd, "botright", 2))
1897 break;
1898#ifdef FEAT_WINDOWS
1899 cmdmod.split |= WSP_BOT;
1900#endif
1901 continue;
1902
1903 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1904 break;
1905#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1906 cmdmod.confirm = TRUE;
1907#endif
1908 continue;
1909
1910 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1911 {
1912 cmdmod.keepmarks = TRUE;
1913 continue;
1914 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001915 if (checkforcmd(&ea.cmd, "keepalt", 5))
1916 {
1917 cmdmod.keepalt = TRUE;
1918 continue;
1919 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001920 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1921 {
1922 cmdmod.keeppatterns = TRUE;
1923 continue;
1924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1926 break;
1927 cmdmod.keepjumps = TRUE;
1928 continue;
1929
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001930 case 'f': /* only accept ":filter {pat} cmd" */
1931 {
1932 char_u *reg_pat;
1933
1934 if (!checkforcmd(&p, "filter", 4)
1935 || *p == NUL || ends_excmd(*p))
1936 break;
Bram Moolenaard29459b2016-08-26 22:29:11 +02001937 if (*p == '!')
1938 {
1939 cmdmod.filter_force = TRUE;
1940 p = skipwhite(p + 1);
1941 if (*p == NUL || ends_excmd(*p))
1942 break;
1943 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001944 p = skip_vimgrep_pat(p, &reg_pat, NULL);
1945 if (p == NULL || *p == NUL)
1946 break;
1947 cmdmod.filter_regmatch.regprog =
1948 vim_regcomp(reg_pat, RE_MAGIC);
1949 if (cmdmod.filter_regmatch.regprog == NULL)
1950 break;
1951 ea.cmd = p;
1952 continue;
1953 }
1954
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 /* ":hide" and ":hide | cmd" are not modifiers */
1956 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1957 || *p == NUL || ends_excmd(*p))
1958 break;
1959 ea.cmd = p;
1960 cmdmod.hide = TRUE;
1961 continue;
1962
1963 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1964 {
1965 cmdmod.lockmarks = TRUE;
1966 continue;
1967 }
1968
1969 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1970 break;
1971#ifdef FEAT_WINDOWS
1972 cmdmod.split |= WSP_ABOVE;
1973#endif
1974 continue;
1975
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001976 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001977 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001978#ifdef FEAT_AUTOCMD
1979 if (cmdmod.save_ei == NULL)
1980 {
1981 /* Set 'eventignore' to "all". Restore the
1982 * existing option value later. */
1983 cmdmod.save_ei = vim_strsave(p_ei);
1984 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001985 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001986 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001987#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001988 continue;
1989 }
Bram Moolenaar3bcfca32016-07-30 19:39:29 +02001990 if (!checkforcmd(&ea.cmd, "noswapfile", 3))
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001991 break;
1992 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001993 continue;
1994
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1996 break;
1997#ifdef FEAT_WINDOWS
1998 cmdmod.split |= WSP_BELOW;
1999#endif
2000 continue;
2001
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002002 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
2003 {
2004#ifdef HAVE_SANDBOX
2005 if (!did_sandbox)
2006 ++sandbox;
2007 did_sandbox = TRUE;
2008#endif
2009 continue;
2010 }
2011 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002013 if (save_msg_silent == -1)
2014 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
2017 {
2018 /* ":silent!", but not "silent !cmd" */
2019 ea.cmd = skipwhite(ea.cmd + 1);
2020 ++emsg_silent;
2021 ++did_esilent;
2022 }
2023 continue;
2024
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002025 case 't': if (checkforcmd(&p, "tab", 3))
2026 {
2027#ifdef FEAT_WINDOWS
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02002028 long tabnr = get_address(&ea, &ea.cmd, ADDR_TABS,
Bram Moolenaarded27822017-01-02 14:27:34 +01002029 ea.skip, FALSE, 1);
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02002030 if (tabnr == MAXLNUM)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002031 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02002032 else
2033 {
2034 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2035 {
2036 errormsg = (char_u *)_(e_invrange);
2037 goto doend;
2038 }
2039 cmdmod.tab = tabnr + 1;
2040 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002041 ea.cmd = p;
2042#endif
2043 continue;
2044 }
2045 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 break;
2047#ifdef FEAT_WINDOWS
2048 cmdmod.split |= WSP_TOP;
2049#endif
2050 continue;
2051
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002052 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
2053 break;
2054 if (save_msg_silent == -1)
2055 save_msg_silent = msg_silent;
2056 msg_silent = 0;
2057 continue;
2058
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
2060 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002061#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 cmdmod.split |= WSP_VERT;
2063#endif
2064 continue;
2065 }
2066 if (!checkforcmd(&p, "verbose", 4))
2067 break;
2068 if (verbose_save < 0)
2069 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00002070 if (vim_isdigit(*ea.cmd))
2071 p_verbose = atoi((char *)ea.cmd);
2072 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 p_verbose = 1;
2074 ea.cmd = p;
2075 continue;
2076 }
2077 break;
2078 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002079 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080
2081#ifdef FEAT_EVAL
2082 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2083 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2084#else
2085 ea.skip = (if_level > 0);
2086#endif
2087
2088#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002089# ifdef FEAT_PROFILE
2090 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002091 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002092 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002093 if (getline_equal(fgetline, cookie, get_func_line))
2094 func_line_exec(getline_cookie(fgetline, cookie));
2095 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002096 script_line_exec();
2097 }
2098#endif
2099
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 /* May go to debug mode. If this happens and the ">quit" debug command is
2101 * used, throw an interrupt exception and skip the next command. */
2102 dbg_check_breakpoint(&ea);
2103 if (!ea.skip && got_int)
2104 {
2105 ea.skip = TRUE;
2106 (void)do_intthrow(cstack);
2107 }
2108#endif
2109
2110/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002111 * 3. Skip over the range to find the command. Let "p" point to after it.
2112 *
2113 * We need the command to know what kind of range it uses.
2114 */
2115 cmd = ea.cmd;
2116 ea.cmd = skip_range(ea.cmd, NULL);
2117 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2118 ea.cmd = skipwhite(ea.cmd + 1);
2119 p = find_command(&ea, NULL);
2120
2121/*
2122 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 *
2124 * where 'addr' is:
2125 *
2126 * % (entire file)
2127 * $ [+-NUM]
2128 * 'x [+-NUM] (where x denotes a currently defined mark)
2129 * . [+-NUM]
2130 * [+-NUM]..
2131 * NUM
2132 *
2133 * The ea.cmd pointer is updated to point to the first character following the
2134 * range spec. If an initial address is found, but no second, the upper bound
2135 * is equal to the lower.
2136 */
2137
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002138 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002139 if (!IS_USER_CMDIDX(ea.cmdidx))
2140 {
2141 if (ea.cmdidx != CMD_SIZE)
2142 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2143 else
2144 ea.addr_type = ADDR_LINES;
2145
2146#ifdef FEAT_WINDOWS
2147 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002148 if (ea.cmdidx == CMD_wincmd && p != NULL)
2149 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002150#endif
2151 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002152
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002154 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 for (;;)
2156 {
2157 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002158 switch (ea.addr_type)
2159 {
2160 case ADDR_LINES:
2161 /* default is current line number */
2162 ea.line2 = curwin->w_cursor.lnum;
2163 break;
2164 case ADDR_WINDOWS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002165 ea.line2 = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002166 break;
2167 case ADDR_ARGUMENTS:
2168 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002169 if (ea.line2 > ARGCOUNT)
2170 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002171 break;
2172 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002173 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002174 ea.line2 = curbuf->b_fnum;
2175 break;
2176 case ADDR_TABS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002177 ea.line2 = CURRENT_TAB_NR;
2178 break;
2179 case ADDR_TABS_RELATIVE:
2180 ea.line2 = 1;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002181 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002182#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002183 case ADDR_QUICKFIX:
2184 ea.line2 = qf_get_cur_valid_idx(&ea);
2185 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002186#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002189 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip,
Bram Moolenaarded27822017-01-02 14:27:34 +01002190 ea.addr_count == 0, address_count++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 if (ea.cmd == NULL) /* error detected */
2192 goto doend;
2193 if (lnum == MAXLNUM)
2194 {
2195 if (*ea.cmd == '%') /* '%' - all lines */
2196 {
2197 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002198 switch (ea.addr_type)
2199 {
2200 case ADDR_LINES:
2201 ea.line1 = 1;
2202 ea.line2 = curbuf->b_ml.ml_line_count;
2203 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002204 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002205 {
2206 buf_T *buf = firstbuf;
2207
2208 while (buf->b_next != NULL
2209 && buf->b_ml.ml_mfp == NULL)
2210 buf = buf->b_next;
2211 ea.line1 = buf->b_fnum;
2212 buf = lastbuf;
2213 while (buf->b_prev != NULL
2214 && buf->b_ml.ml_mfp == NULL)
2215 buf = buf->b_prev;
2216 ea.line2 = buf->b_fnum;
2217 break;
2218 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002219 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002220 ea.line1 = firstbuf->b_fnum;
2221 ea.line2 = lastbuf->b_fnum;
2222 break;
2223 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002224 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002225 if (IS_USER_CMDIDX(ea.cmdidx))
2226 {
2227 ea.line1 = 1;
2228 ea.line2 = ea.addr_type == ADDR_WINDOWS
2229 ? LAST_WIN_NR : LAST_TAB_NR;
2230 }
2231 else
2232 {
2233 /* there is no Vim command which uses '%' and
2234 * ADDR_WINDOWS or ADDR_TABS */
2235 errormsg = (char_u *)_(e_invrange);
2236 goto doend;
2237 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002238 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002239 case ADDR_TABS_RELATIVE:
2240 errormsg = (char_u *)_(e_invrange);
2241 goto doend;
2242 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002243 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002244 if (ARGCOUNT == 0)
2245 ea.line1 = ea.line2 = 0;
2246 else
2247 {
2248 ea.line1 = 1;
2249 ea.line2 = ARGCOUNT;
2250 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002251 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002252#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002253 case ADDR_QUICKFIX:
2254 ea.line1 = 1;
2255 ea.line2 = qf_get_size(&ea);
2256 if (ea.line2 == 0)
2257 ea.line2 = 1;
2258 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002259#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 ++ea.addr_count;
2262 }
2263 /* '*' - visual area */
2264 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2265 {
2266 pos_T *fp;
2267
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002268 if (ea.addr_type != ADDR_LINES)
2269 {
2270 errormsg = (char_u *)_(e_invrange);
2271 goto doend;
2272 }
2273
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 ++ea.cmd;
2275 if (!ea.skip)
2276 {
2277 fp = getmark('<', FALSE);
2278 if (check_mark(fp) == FAIL)
2279 goto doend;
2280 ea.line1 = fp->lnum;
2281 fp = getmark('>', FALSE);
2282 if (check_mark(fp) == FAIL)
2283 goto doend;
2284 ea.line2 = fp->lnum;
2285 ++ea.addr_count;
2286 }
2287 }
2288 }
2289 else
2290 ea.line2 = lnum;
2291 ea.addr_count++;
2292
2293 if (*ea.cmd == ';')
2294 {
2295 if (!ea.skip)
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002296 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 curwin->w_cursor.lnum = ea.line2;
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +01002298 /* don't leave the cursor on an illegal line or column */
2299 check_cursor();
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 }
2302 else if (*ea.cmd != ',')
2303 break;
2304 ++ea.cmd;
2305 }
2306
2307 /* One address given: set start and end lines */
2308 if (ea.addr_count == 1)
2309 {
2310 ea.line1 = ea.line2;
2311 /* ... but only implicit: really no address given */
2312 if (lnum == MAXLNUM)
2313 ea.addr_count = 0;
2314 }
2315
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002317 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 */
2319
2320 /*
2321 * Skip ':' and any white space
2322 */
2323 ea.cmd = skipwhite(ea.cmd);
2324 while (*ea.cmd == ':')
2325 ea.cmd = skipwhite(ea.cmd + 1);
2326
2327 /*
2328 * If we got a line, but no command, then go to the line.
2329 * If we find a '|' or '\n' we set ea.nextcmd.
2330 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002331 if (*ea.cmd == NUL || *ea.cmd == '"'
2332 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 {
2334 /*
2335 * strange vi behaviour:
2336 * ":3" jumps to line 3
2337 * ":3|..." prints line 3
2338 * ":|" prints current line
2339 */
2340 if (ea.skip) /* skip this if inside :if */
2341 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002342 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 {
2344 ea.cmdidx = CMD_print;
2345 ea.argt = RANGE+COUNT+TRLBAR;
2346 if ((errormsg = invalid_range(&ea)) == NULL)
2347 {
2348 correct_range(&ea);
2349 ex_print(&ea);
2350 }
2351 }
2352 else if (ea.addr_count != 0)
2353 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002354 if (ea.line2 > curbuf->b_ml.ml_line_count)
2355 {
2356 /* With '-' in 'cpoptions' a line number past the file is an
2357 * error, otherwise put it at the end of the file. */
2358 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2359 ea.line2 = -1;
2360 else
2361 ea.line2 = curbuf->b_ml.ml_line_count;
2362 }
2363
2364 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002365 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 else
2367 {
2368 if (ea.line2 == 0)
2369 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 else
2371 curwin->w_cursor.lnum = ea.line2;
2372 beginline(BL_SOL | BL_FIX);
2373 }
2374 }
2375 goto doend;
2376 }
2377
Bram Moolenaard5005162014-08-22 23:05:54 +02002378#ifdef FEAT_AUTOCMD
2379 /* If this looks like an undefined user command and there are CmdUndefined
2380 * autocommands defined, trigger the matching autocommands. */
2381 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2382 && ASCII_ISUPPER(*ea.cmd)
2383 && has_cmdundefined())
2384 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002385 int ret;
2386
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002387 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002388 while (ASCII_ISALNUM(*p))
2389 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002390 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002391 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2392 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002393 /* If the autocommands did something and didn't cause an error, try
2394 * finding the command again. */
Bram Moolenaareac784e2016-07-28 22:08:24 +02002395 p = (ret && !aborting()) ? find_command(&ea, NULL) : ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002396 }
2397#endif
2398
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399#ifdef FEAT_USR_CMDS
2400 if (p == NULL)
2401 {
2402 if (!ea.skip)
2403 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2404 goto doend;
2405 }
2406 /* Check for wrong commands. */
2407 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2408 {
2409 errormsg = uc_fun_cmd();
2410 goto doend;
2411 }
2412#endif
2413 if (ea.cmdidx == CMD_SIZE)
2414 {
2415 if (!ea.skip)
2416 {
2417 STRCPY(IObuff, _("E492: Not an editor command"));
2418 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002419 {
2420 /* If the modifier was parsed OK the error must be in the
2421 * following command */
2422 if (after_modifier != NULL)
2423 append_command(after_modifier);
2424 else
2425 append_command(*cmdlinep);
2426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002428 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 }
2430 goto doend;
2431 }
2432
Bram Moolenaar958636c2014-10-21 20:01:58 +02002433 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2434 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002435#ifdef HAVE_EX_SCRIPT_NI
2436 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2437#endif
2438 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439
2440#ifndef FEAT_EVAL
2441 /*
2442 * When the expression evaluation is disabled, recognize the ":if" and
2443 * ":endif" commands and ignore everything in between it.
2444 */
2445 if (ea.cmdidx == CMD_if)
2446 ++if_level;
2447 if (if_level)
2448 {
2449 if (ea.cmdidx == CMD_endif)
2450 --if_level;
2451 goto doend;
2452 }
2453
2454#endif
2455
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002456 /* forced commands */
2457 if (*p == '!' && ea.cmdidx != CMD_substitute
2458 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 {
2460 ++p;
2461 ea.forceit = TRUE;
2462 }
2463 else
2464 ea.forceit = FALSE;
2465
2466/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002467 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002469 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002470 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471
2472 if (!ea.skip)
2473 {
2474#ifdef HAVE_SANDBOX
2475 if (sandbox != 0 && !(ea.argt & SBOXOK))
2476 {
2477 /* Command not allowed in sandbox. */
2478 errormsg = (char_u *)_(e_sandbox);
2479 goto doend;
2480 }
2481#endif
2482 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2483 {
2484 /* Command not allowed in non-'modifiable' buffer */
2485 errormsg = (char_u *)_(e_modifiable);
2486 goto doend;
2487 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002488
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002489 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002490 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002492 /* Command not allowed when editing the command line. */
Bram Moolenaar75c19462017-02-12 18:34:05 +01002493 errormsg = (char_u *)_(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 goto doend;
2495 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002496#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002497 /* Disallow editing another buffer when "curbuf_lock" is set.
2498 * Do allow ":edit" (check for argument later).
2499 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002500 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002501 && ea.cmdidx != CMD_edit
2502 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002503 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002504 && curbuf_locked())
2505 goto doend;
2506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507
2508 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2509 {
2510 /* no range allowed */
2511 errormsg = (char_u *)_(e_norange);
2512 goto doend;
2513 }
2514 }
2515
2516 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2517 {
2518 errormsg = (char_u *)_(e_nobang);
2519 goto doend;
2520 }
2521
2522 /*
2523 * Don't complain about the range if it is not used
2524 * (could happen if line_count is accidentally set to 0).
2525 */
2526 if (!ea.skip && !ni)
2527 {
2528 /*
2529 * If the range is backwards, ask for confirmation and, if given, swap
2530 * ea.line1 & ea.line2 so it's forwards again.
2531 * When global command is busy, don't ask, will fail below.
2532 */
2533 if (!global_busy && ea.line1 > ea.line2)
2534 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002535 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002537 if (sourcing || exmode_active)
2538 {
2539 errormsg = (char_u *)_("E493: Backwards range given");
2540 goto doend;
2541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 if (ask_yesno((char_u *)
2543 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002544 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 }
2546 lnum = ea.line1;
2547 ea.line1 = ea.line2;
2548 ea.line2 = lnum;
2549 }
2550 if ((errormsg = invalid_range(&ea)) != NULL)
2551 goto doend;
2552 }
2553
2554 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2555 ea.line2 = 1;
2556
2557 correct_range(&ea);
2558
2559#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002560 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2561 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 {
2563 /* Put the first line at the start of a closed fold, put the last line
2564 * at the end of a closed fold. */
2565 (void)hasFolding(ea.line1, &ea.line1, NULL);
2566 (void)hasFolding(ea.line2, NULL, &ea.line2);
2567 }
2568#endif
2569
2570#ifdef FEAT_QUICKFIX
2571 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002572 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 * option here, so things like % get expanded.
2574 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002575 p = replace_makeprg(&ea, p, cmdlinep);
2576 if (p == NULL)
2577 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578#endif
2579
2580 /*
2581 * Skip to start of argument.
2582 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2583 */
2584 if (ea.cmdidx == CMD_bang)
2585 ea.arg = p;
2586 else
2587 ea.arg = skipwhite(p);
2588
2589 /*
2590 * Check for "++opt=val" argument.
2591 * Must be first, allow ":w ++enc=utf8 !cmd"
2592 */
2593 if (ea.argt & ARGOPT)
2594 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2595 if (getargopt(&ea) == FAIL && !ni)
2596 {
2597 errormsg = (char_u *)_(e_invarg);
2598 goto doend;
2599 }
2600
2601 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2602 {
2603 if (*ea.arg == '>') /* append */
2604 {
2605 if (*++ea.arg != '>') /* typed wrong */
2606 {
2607 errormsg = (char_u *)_("E494: Use w or w>>");
2608 goto doend;
2609 }
2610 ea.arg = skipwhite(ea.arg + 1);
2611 ea.append = TRUE;
2612 }
2613 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2614 {
2615 ++ea.arg;
2616 ea.usefilter = TRUE;
2617 }
2618 }
2619
2620 if (ea.cmdidx == CMD_read)
2621 {
2622 if (ea.forceit)
2623 {
2624 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2625 ea.forceit = FALSE;
2626 }
2627 else if (*ea.arg == '!') /* :r !filter */
2628 {
2629 ++ea.arg;
2630 ea.usefilter = TRUE;
2631 }
2632 }
2633
2634 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2635 {
2636 ea.amount = 1;
2637 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2638 {
2639 ++ea.arg;
2640 ++ea.amount;
2641 }
2642 ea.arg = skipwhite(ea.arg);
2643 }
2644
2645 /*
2646 * Check for "+command" argument, before checking for next command.
2647 * Don't do this for ":read !cmd" and ":write !cmd".
2648 */
2649 if ((ea.argt & EDITCMD) && !ea.usefilter)
2650 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2651
2652 /*
2653 * Check for '|' to separate commands and '"' to start comments.
2654 * Don't do this for ":read !cmd" and ":write !cmd".
2655 */
2656 if ((ea.argt & TRLBAR) && !ea.usefilter)
2657 separate_nextcmd(&ea);
2658
2659 /*
2660 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002661 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2662 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002664 else if (ea.cmdidx == CMD_bang
2665 || ea.cmdidx == CMD_global
2666 || ea.cmdidx == CMD_vglobal
2667 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 {
2669 for (p = ea.arg; *p; ++p)
2670 {
2671 /* Remove one backslash before a newline, so that it's possible to
2672 * pass a newline to the shell and also a newline that is preceded
2673 * with a backslash. This makes it impossible to end a shell
2674 * command in a backslash, but that doesn't appear useful.
2675 * Halving the number of backslashes is incompatible with previous
2676 * versions. */
2677 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002678 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 else if (*p == '\n')
2680 {
2681 ea.nextcmd = p + 1;
2682 *p = NUL;
2683 break;
2684 }
2685 }
2686 }
2687
2688 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2689 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002690 buf_T *buf;
2691
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002693 switch (ea.addr_type)
2694 {
2695 case ADDR_LINES:
2696 ea.line2 = curbuf->b_ml.ml_line_count;
2697 break;
2698 case ADDR_LOADED_BUFFERS:
2699 buf = firstbuf;
2700 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2701 buf = buf->b_next;
2702 ea.line1 = buf->b_fnum;
2703 buf = lastbuf;
2704 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2705 buf = buf->b_prev;
2706 ea.line2 = buf->b_fnum;
2707 break;
2708 case ADDR_BUFFERS:
2709 ea.line1 = firstbuf->b_fnum;
2710 ea.line2 = lastbuf->b_fnum;
2711 break;
2712 case ADDR_WINDOWS:
2713 ea.line2 = LAST_WIN_NR;
2714 break;
2715 case ADDR_TABS:
2716 ea.line2 = LAST_TAB_NR;
2717 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002718 case ADDR_TABS_RELATIVE:
2719 ea.line2 = 1;
2720 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002721 case ADDR_ARGUMENTS:
2722 if (ARGCOUNT == 0)
2723 ea.line1 = ea.line2 = 0;
2724 else
2725 ea.line2 = ARGCOUNT;
2726 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002727#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002728 case ADDR_QUICKFIX:
2729 ea.line2 = qf_get_size(&ea);
2730 if (ea.line2 == 0)
2731 ea.line2 = 1;
2732 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002733#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 }
2736
2737 /* accept numbered register only when no count allowed (:put) */
2738 if ( (ea.argt & REGSTR)
2739 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002740 /* Do not allow register = for user commands */
2741 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2743 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002744#ifndef FEAT_CLIPBOARD
2745 /* check these explicitly for a more specific error message */
2746 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002748 errormsg = (char_u *)_(e_invalidreg);
2749 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 }
2751#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002752 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2753 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002754 {
2755 ea.regname = *ea.arg++;
2756#ifdef FEAT_EVAL
2757 /* for '=' register: accept the rest of the line as an expression */
2758 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2759 {
2760 set_expr_line(vim_strsave(ea.arg));
2761 ea.arg += STRLEN(ea.arg);
2762 }
2763#endif
2764 ea.arg = skipwhite(ea.arg);
2765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 }
2767
2768 /*
2769 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2770 * count, it's a buffer name.
2771 */
2772 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2773 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2774 || vim_iswhite(*p)))
2775 {
2776 n = getdigits(&ea.arg);
2777 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002778 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 {
2780 errormsg = (char_u *)_(e_zerocount);
2781 goto doend;
2782 }
2783 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2784 {
2785 ea.line2 = n;
2786 if (ea.addr_count == 0)
2787 ea.addr_count = 1;
2788 }
2789 else
2790 {
2791 ea.line1 = ea.line2;
2792 ea.line2 += n - 1;
2793 ++ea.addr_count;
2794 /*
2795 * Be vi compatible: no error message for out of range.
2796 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002797 if (ea.addr_type == ADDR_LINES
2798 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 ea.line2 = curbuf->b_ml.ml_line_count;
2800 }
2801 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002802
2803 /*
2804 * Check for flags: 'l', 'p' and '#'.
2805 */
2806 if (ea.argt & EXFLAGS)
2807 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002809 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002810 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 {
2812 errormsg = (char_u *)_(e_trailing);
2813 goto doend;
2814 }
2815
2816 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2817 {
2818 errormsg = (char_u *)_(e_argreq);
2819 goto doend;
2820 }
2821
2822#ifdef FEAT_EVAL
2823 /*
2824 * Skip the command when it's not going to be executed.
2825 * The commands like :if, :endif, etc. always need to be executed.
2826 * Also make an exception for commands that handle a trailing command
2827 * themselves.
2828 */
2829 if (ea.skip)
2830 {
2831 switch (ea.cmdidx)
2832 {
2833 /* commands that need evaluation */
2834 case CMD_while:
2835 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002836 case CMD_for:
2837 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 case CMD_if:
2839 case CMD_elseif:
2840 case CMD_else:
2841 case CMD_endif:
2842 case CMD_try:
2843 case CMD_catch:
2844 case CMD_finally:
2845 case CMD_endtry:
2846 case CMD_function:
2847 break;
2848
2849 /* Commands that handle '|' themselves. Check: A command should
2850 * either have the TRLBAR flag, appear in this list or appear in
2851 * the list at ":help :bar". */
2852 case CMD_aboveleft:
2853 case CMD_and:
2854 case CMD_belowright:
2855 case CMD_botright:
2856 case CMD_browse:
2857 case CMD_call:
2858 case CMD_confirm:
2859 case CMD_delfunction:
2860 case CMD_djump:
2861 case CMD_dlist:
2862 case CMD_dsearch:
2863 case CMD_dsplit:
2864 case CMD_echo:
2865 case CMD_echoerr:
2866 case CMD_echomsg:
2867 case CMD_echon:
2868 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002869 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 case CMD_help:
2871 case CMD_hide:
2872 case CMD_ijump:
2873 case CMD_ilist:
2874 case CMD_isearch:
2875 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002876 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 case CMD_keepjumps:
2878 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002879 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 case CMD_leftabove:
2881 case CMD_let:
2882 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002883 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002885 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002886 case CMD_noautocmd:
2887 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 case CMD_perl:
2889 case CMD_psearch:
2890 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002891 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002892 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 case CMD_return:
2894 case CMD_rightbelow:
2895 case CMD_ruby:
2896 case CMD_silent:
2897 case CMD_smagic:
2898 case CMD_snomagic:
2899 case CMD_substitute:
2900 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002901 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 case CMD_tcl:
2903 case CMD_throw:
2904 case CMD_tilde:
2905 case CMD_topleft:
2906 case CMD_unlet:
2907 case CMD_verbose:
2908 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002909 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 break;
2911
2912 default: goto doend;
2913 }
2914 }
2915#endif
2916
2917 if (ea.argt & XFILE)
2918 {
2919 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2920 goto doend;
2921 }
2922
2923#ifdef FEAT_LISTCMDS
2924 /*
2925 * Accept buffer name. Cannot be used at the same time with a buffer
2926 * number. Don't do this for a user command.
2927 */
2928 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002929 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 {
2931 /*
2932 * :bdelete, :bwipeout and :bunload take several arguments, separated
2933 * by spaces: find next space (skipping over escaped characters).
2934 * The others take one argument: ignore trailing spaces.
2935 */
2936 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2937 || ea.cmdidx == CMD_bunload)
2938 p = skiptowhite_esc(ea.arg);
2939 else
2940 {
2941 p = ea.arg + STRLEN(ea.arg);
2942 while (p > ea.arg && vim_iswhite(p[-1]))
2943 --p;
2944 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002945 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2946 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 if (ea.line2 < 0) /* failed */
2948 goto doend;
2949 ea.addr_count = 1;
2950 ea.arg = skipwhite(p);
2951 }
2952#endif
2953
2954/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002955 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 *
2957 * The "ea" structure holds the arguments that can be used.
2958 */
2959 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002960 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 ea.cookie = cookie;
2962#ifdef FEAT_EVAL
2963 ea.cstack = cstack;
2964#endif
2965
2966#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002967 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 {
2969 /*
2970 * Execute a user-defined command.
2971 */
2972 do_ucmd(&ea);
2973 }
2974 else
2975#endif
2976 {
2977 /*
2978 * Call the function to execute the command.
2979 */
2980 ea.errmsg = NULL;
2981 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2982 if (ea.errmsg != NULL)
2983 errormsg = (char_u *)_(ea.errmsg);
2984 }
2985
2986#ifdef FEAT_EVAL
2987 /*
2988 * If the command just executed called do_cmdline(), any throw or ":return"
2989 * or ":finish" encountered there must also check the cstack of the still
2990 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2991 * exception, or reanimate a returned function or finished script file and
2992 * return or finish it again.
2993 */
2994 if (need_rethrow)
2995 do_throw(cstack);
2996 else if (check_cstack)
2997 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002998 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003000 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 && current_func_returned())
3002 do_return(&ea, TRUE, FALSE, NULL);
3003 }
3004 need_rethrow = check_cstack = FALSE;
3005#endif
3006
3007doend:
3008 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
3009 curwin->w_cursor.lnum = 1;
3010
3011 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
3012 {
3013 if (sourcing)
3014 {
3015 if (errormsg != IObuff)
3016 {
3017 STRCPY(IObuff, errormsg);
3018 errormsg = IObuff;
3019 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003020 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 }
3022 emsg(errormsg);
3023 }
3024#ifdef FEAT_EVAL
3025 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02003026 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
3027 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028#endif
3029
3030 if (verbose_save >= 0)
3031 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00003032#ifdef FEAT_AUTOCMD
3033 if (cmdmod.save_ei != NULL)
3034 {
3035 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003036 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
3037 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00003038 free_string_option(cmdmod.save_ei);
3039 }
3040#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003041 if (cmdmod.filter_regmatch.regprog != NULL)
3042 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043
3044 cmdmod = save_cmdmod;
3045
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003046 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 {
3048 /* messages could be enabled for a serious error, need to check if the
3049 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00003050 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003051 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 emsg_silent -= did_esilent;
3053 if (emsg_silent < 0)
3054 emsg_silent = 0;
3055 /* Restore msg_scroll, it's set by file I/O commands, even when no
3056 * message is actually displayed. */
3057 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003058
3059 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
3060 * somewhere in the line. Put it back in the first column. */
3061 if (redirecting())
3062 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 }
3064
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003065#ifdef HAVE_SANDBOX
3066 if (did_sandbox)
3067 --sandbox;
3068#endif
3069
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3071 ea.nextcmd = NULL;
3072
3073#ifdef FEAT_EVAL
3074 --ex_nesting_level;
3075#endif
3076
3077 return ea.nextcmd;
3078}
3079#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003080 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081#endif
3082
3083/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003084 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 * If there is a match advance "pp" to the argument and return TRUE.
3086 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003087 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003088checkforcmd(
3089 char_u **pp, /* start of command */
3090 char *cmd, /* name of command */
3091 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092{
3093 int i;
3094
3095 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003096 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 break;
3098 if (i >= len && !isalpha((*pp)[i]))
3099 {
3100 *pp = skipwhite(*pp + i);
3101 return TRUE;
3102 }
3103 return FALSE;
3104}
3105
3106/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003107 * Append "cmd" to the error message in IObuff.
3108 * Takes care of limiting the length and handling 0xa0, which would be
3109 * invisible otherwise.
3110 */
3111 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003112append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003113{
3114 char_u *s = cmd;
3115 char_u *d;
3116
3117 STRCAT(IObuff, ": ");
3118 d = IObuff + STRLEN(IObuff);
3119 while (*s != NUL && d - IObuff < IOSIZE - 7)
3120 {
3121 if (
3122#ifdef FEAT_MBYTE
3123 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3124#endif
3125 *s == 0xa0)
3126 {
3127 s +=
3128#ifdef FEAT_MBYTE
3129 enc_utf8 ? 2 :
3130#endif
3131 1;
3132 STRCPY(d, "<a0>");
3133 d += 4;
3134 }
3135 else
3136 MB_COPY_CHAR(s, d);
3137 }
3138 *d = NUL;
3139}
3140
3141/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003143 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003145 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 * Returns NULL for an ambiguous user command.
3147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003149find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150{
3151 int len;
3152 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003153 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154
3155 /*
3156 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003157 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 * - the 'k' command can directly be followed by any character.
3159 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003160 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003162 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 */
3164 p = eap->cmd;
3165 if (*p == 'k')
3166 {
3167 eap->cmdidx = CMD_k;
3168 ++p;
3169 }
3170 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003171 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3172 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 || p[1] == 'g'
3174 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3175 || p[1] == 'I'
3176 || (p[1] == 'r' && p[2] != 'e')))
3177 {
3178 eap->cmdidx = CMD_substitute;
3179 ++p;
3180 }
3181 else
3182 {
3183 while (ASCII_ISALPHA(*p))
3184 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003185 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003186 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003187 while (ASCII_ISALNUM(*p))
3188 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003189
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 /* check for non-alpha command */
3191 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3192 ++p;
3193 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003194 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3195 {
3196 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3197 * :delete with the 'l' flag. Same for 'p'. */
3198 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003199 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003200 break;
3201 if (i == len - 1)
3202 {
3203 --len;
3204 if (p[-1] == 'l')
3205 eap->flags |= EXFLAG_LIST;
3206 else
3207 eap->flags |= EXFLAG_PRINT;
3208 }
3209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210
3211 if (ASCII_ISLOWER(*eap->cmd))
3212 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
3213 else
3214 eap->cmdidx = cmdidxs[26];
3215
3216 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3217 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3218 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3219 (size_t)len) == 0)
3220 {
3221#ifdef FEAT_EVAL
3222 if (full != NULL
3223 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3224 *full = TRUE;
3225#endif
3226 break;
3227 }
3228
3229#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003230 /* Look for a user defined command as a last resort. Let ":Print" be
3231 * overruled by a user defined command. */
3232 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3233 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003235 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 while (ASCII_ISALNUM(*p))
3237 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003238 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 }
3240#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003241 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 eap->cmdidx = CMD_SIZE;
3243 }
3244
3245 return p;
3246}
3247
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003248#ifdef FEAT_USR_CMDS
3249/*
3250 * Search for a user command that matches "eap->cmd".
3251 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3252 * Return a pointer to just after the command.
3253 * Return NULL if there is no matching command.
3254 */
3255 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003256find_ucmd(
3257 exarg_T *eap,
3258 char_u *p, /* end of the command (possibly including count) */
3259 int *full, /* set to TRUE for a full match */
3260 expand_T *xp, /* used for completion, NULL otherwise */
3261 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003262{
3263 int len = (int)(p - eap->cmd);
3264 int j, k, matchlen = 0;
3265 ucmd_T *uc;
3266 int found = FALSE;
3267 int possible = FALSE;
3268 char_u *cp, *np; /* Point into typed cmd and test name */
3269 garray_T *gap;
3270 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3271 only full match global is accepted. */
3272
3273 /*
3274 * Look for buffer-local user commands first, then global ones.
3275 */
3276 gap = &curbuf->b_ucmds;
3277 for (;;)
3278 {
3279 for (j = 0; j < gap->ga_len; ++j)
3280 {
3281 uc = USER_CMD_GA(gap, j);
3282 cp = eap->cmd;
3283 np = uc->uc_name;
3284 k = 0;
3285 while (k < len && *np != NUL && *cp++ == *np++)
3286 k++;
3287 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3288 {
3289 /* If finding a second match, the command is ambiguous. But
3290 * not if a buffer-local command wasn't a full match and a
3291 * global command is a full match. */
3292 if (k == len && found && *np != NUL)
3293 {
3294 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003295 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003296 amb_local = TRUE;
3297 }
3298
3299 if (!found || (k == len && *np == NUL))
3300 {
3301 /* If we matched up to a digit, then there could
3302 * be another command including the digit that we
3303 * should use instead.
3304 */
3305 if (k == len)
3306 found = TRUE;
3307 else
3308 possible = TRUE;
3309
3310 if (gap == &ucmds)
3311 eap->cmdidx = CMD_USER;
3312 else
3313 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003314 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003315 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003316 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003317
3318# ifdef FEAT_CMDL_COMPL
3319 if (compl != NULL)
3320 *compl = uc->uc_compl;
3321# ifdef FEAT_EVAL
3322 if (xp != NULL)
3323 {
3324 xp->xp_arg = uc->uc_compl_arg;
3325 xp->xp_scriptID = uc->uc_scriptID;
3326 }
3327# endif
3328# endif
3329 /* Do not search for further abbreviations
3330 * if this is an exact match. */
3331 matchlen = k;
3332 if (k == len && *np == NUL)
3333 {
3334 if (full != NULL)
3335 *full = TRUE;
3336 amb_local = FALSE;
3337 break;
3338 }
3339 }
3340 }
3341 }
3342
3343 /* Stop if we found a full match or searched all. */
3344 if (j < gap->ga_len || gap == &ucmds)
3345 break;
3346 gap = &ucmds;
3347 }
3348
3349 /* Only found ambiguous matches. */
3350 if (amb_local)
3351 {
3352 if (xp != NULL)
3353 xp->xp_context = EXPAND_UNSUCCESSFUL;
3354 return NULL;
3355 }
3356
3357 /* The match we found may be followed immediately by a number. Move "p"
3358 * back to point to it. */
3359 if (found || possible)
3360 return p + (matchlen - len);
3361 return p;
3362}
3363#endif
3364
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003366static struct cmdmod
3367{
3368 char *name;
3369 int minlen;
3370 int has_count; /* :123verbose :3tab */
3371} cmdmods[] = {
3372 {"aboveleft", 3, FALSE},
3373 {"belowright", 3, FALSE},
3374 {"botright", 2, FALSE},
3375 {"browse", 3, FALSE},
3376 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003377 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003378 {"hide", 3, FALSE},
3379 {"keepalt", 5, FALSE},
3380 {"keepjumps", 5, FALSE},
3381 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003382 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003383 {"leftabove", 5, FALSE},
3384 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003385 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003386 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003387 {"rightbelow", 6, FALSE},
3388 {"sandbox", 3, FALSE},
3389 {"silent", 3, FALSE},
3390 {"tab", 3, TRUE},
3391 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003392 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003393 {"verbose", 4, TRUE},
3394 {"vertical", 4, FALSE},
3395};
3396
3397/*
3398 * Return length of a command modifier (including optional count).
3399 * Return zero when it's not a modifier.
3400 */
3401 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003402modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003403{
3404 int i, j;
3405 char_u *p = cmd;
3406
3407 if (VIM_ISDIGIT(*cmd))
3408 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003409 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003410 {
3411 for (j = 0; p[j] != NUL; ++j)
3412 if (p[j] != cmdmods[i].name[j])
3413 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003414 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003415 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003416 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003417 }
3418 return 0;
3419}
3420
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421/*
3422 * Return > 0 if an Ex command "name" exists.
3423 * Return 2 if there is an exact match.
3424 * Return 3 if there is an ambiguous match.
3425 */
3426 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003427cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428{
3429 exarg_T ea;
3430 int full = FALSE;
3431 int i;
3432 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003433 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434
3435 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003436 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 {
3438 for (j = 0; name[j] != NUL; ++j)
3439 if (name[j] != cmdmods[i].name[j])
3440 break;
3441 if (name[j] == NUL && j >= cmdmods[i].minlen)
3442 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3443 }
3444
Bram Moolenaara9587612006-05-04 21:47:50 +00003445 /* Check built-in commands and user defined commands.
3446 * For ":2match" and ":3match" we need to skip the number. */
3447 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003449 p = find_command(&ea, &full);
3450 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003452 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3453 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003454 if (*skipwhite(p) != NUL)
3455 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3457}
3458#endif
3459
3460/*
3461 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3462 * we don't need/want deleted. Maybe this could be done better if we didn't
3463 * repeat all this stuff. The only problem is that they may not stay
3464 * perfectly compatible with each other, but then the command line syntax
3465 * probably won't change that much -- webb.
3466 */
3467 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003468set_one_cmd_context(
3469 expand_T *xp,
3470 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471{
3472 char_u *p;
3473 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003474 int len = 0;
3475 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3477 int compl = EXPAND_NOTHING;
3478#endif
3479#ifdef FEAT_CMDL_COMPL
3480 int delim;
3481#endif
3482 int forceit = FALSE;
3483 int usefilter = FALSE; /* filter instead of file name */
3484
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003485 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 xp->xp_pattern = buff;
3487 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003488 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
3490/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003491 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 */
3493 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3494 ;
3495 xp->xp_pattern = cmd;
3496
3497 if (*cmd == NUL)
3498 return NULL;
3499 if (*cmd == '"') /* ignore comment lines */
3500 {
3501 xp->xp_context = EXPAND_NOTHING;
3502 return NULL;
3503 }
3504
3505/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003506 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 */
3508 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 xp->xp_pattern = cmd;
3510 if (*cmd == NUL)
3511 return NULL;
3512 if (*cmd == '"')
3513 {
3514 xp->xp_context = EXPAND_NOTHING;
3515 return NULL;
3516 }
3517
3518 if (*cmd == '|' || *cmd == '\n')
3519 return cmd + 1; /* There's another command */
3520
3521 /*
3522 * Isolate the command and search for it in the command table.
3523 * Exceptions:
3524 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003525 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3527 */
3528 if (*cmd == 'k' && cmd[1] != 'e')
3529 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003530 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 p = cmd + 1;
3532 }
3533 else
3534 {
3535 p = cmd;
3536 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3537 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003538 /* a user command may contain digits */
3539 if (ASCII_ISUPPER(cmd[0]))
3540 while (ASCII_ISALNUM(*p) || *p == '*')
3541 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003542 /* for python 3.x: ":py3*" commands completion */
3543 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003544 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003545 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003546 while (ASCII_ISALPHA(*p) || *p == '*')
3547 ++p;
3548 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003549 /* check for non-alpha command */
3550 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3551 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003552 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003554 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 {
3556 xp->xp_context = EXPAND_UNSUCCESSFUL;
3557 return NULL;
3558 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003559 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003560 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3561 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3562 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 break;
3564
3565#ifdef FEAT_USR_CMDS
3566 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3568 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569#endif
3570 }
3571
3572 /*
3573 * If the cursor is touching the command, and it ends in an alpha-numeric
3574 * character, complete the command name.
3575 */
3576 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3577 return NULL;
3578
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003579 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 {
3581 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3582 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003583 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 p = cmd + 1;
3585 }
3586#ifdef FEAT_USR_CMDS
3587 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3588 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003589 ea.cmd = cmd;
3590 p = find_ucmd(&ea, p, NULL, xp,
3591# if defined(FEAT_CMDL_COMPL)
3592 &compl
3593# else
3594 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003596 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003597 if (p == NULL)
3598 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 }
3600#endif
3601 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003602 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 {
3604 /* Not still touching the command and it was an illegal one */
3605 xp->xp_context = EXPAND_UNSUCCESSFUL;
3606 return NULL;
3607 }
3608
3609 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3610
3611 if (*p == '!') /* forced commands */
3612 {
3613 forceit = TRUE;
3614 ++p;
3615 }
3616
3617/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003618 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003620 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003621 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622
3623 arg = skipwhite(p);
3624
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003625 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 {
3627 if (*arg == '>') /* append */
3628 {
3629 if (*++arg == '>')
3630 ++arg;
3631 arg = skipwhite(arg);
3632 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003633 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 {
3635 ++arg;
3636 usefilter = TRUE;
3637 }
3638 }
3639
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003640 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 {
3642 usefilter = forceit; /* :r! filter if forced */
3643 if (*arg == '!') /* :r !filter */
3644 {
3645 ++arg;
3646 usefilter = TRUE;
3647 }
3648 }
3649
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003650 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 {
3652 while (*arg == *cmd) /* allow any number of '>' or '<' */
3653 ++arg;
3654 arg = skipwhite(arg);
3655 }
3656
3657 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003658 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 {
3660 /* Check if we're in the +command */
3661 p = arg + 1;
3662 arg = skip_cmd_arg(arg, FALSE);
3663
3664 /* Still touching the command after '+'? */
3665 if (*arg == NUL)
3666 return p;
3667
3668 /* Skip space(s) after +command to get to the real argument */
3669 arg = skipwhite(arg);
3670 }
3671
3672 /*
3673 * Check for '|' to separate commands and '"' to start comments.
3674 * Don't do this for ":read !cmd" and ":write !cmd".
3675 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003676 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 {
3678 p = arg;
3679 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003680 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 p += 2;
3682 while (*p)
3683 {
3684 if (*p == Ctrl_V)
3685 {
3686 if (p[1] != NUL)
3687 ++p;
3688 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003689 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 || *p == '|' || *p == '\n')
3691 {
3692 if (*(p - 1) != '\\')
3693 {
3694 if (*p == '|' || *p == '\n')
3695 return p + 1;
3696 return NULL; /* It's a comment */
3697 }
3698 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003699 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 }
3701 }
3702
3703 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003704 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 vim_strchr((char_u *)"|\"", *arg) == NULL)
3706 return NULL;
3707
3708 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003709 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003711 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003712 while (*p && p < buff + len)
3713 {
3714 if (*p == ' ' || *p == TAB)
3715 {
3716 /* argument starts after a space */
3717 xp->xp_pattern = ++p;
3718 }
3719 else
3720 {
3721 if (*p == '\\' && *(p + 1) != NUL)
3722 ++p; /* skip over escaped character */
3723 mb_ptr_adv(p);
3724 }
3725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003727 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003729 int c;
3730 int in_quote = FALSE;
3731 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732
3733 /*
3734 * Allow spaces within back-quotes to count as part of the argument
3735 * being expanded.
3736 */
3737 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003738 p = xp->xp_pattern;
3739 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003741#ifdef FEAT_MBYTE
3742 if (has_mbyte)
3743 c = mb_ptr2char(p);
3744 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003746 c = *p;
3747 if (c == '\\' && p[1] != NUL)
3748 ++p;
3749 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 {
3751 if (!in_quote)
3752 {
3753 xp->xp_pattern = p;
3754 bow = p + 1;
3755 }
3756 in_quote = !in_quote;
3757 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003758 /* An argument can contain just about everything, except
3759 * characters that end the command and white space. */
3760 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003761#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003762 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003763#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003764 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003765 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003766 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003767 while (*p != NUL)
3768 {
3769#ifdef FEAT_MBYTE
3770 if (has_mbyte)
3771 c = mb_ptr2char(p);
3772 else
3773#endif
3774 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003775 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003776 break;
3777#ifdef FEAT_MBYTE
3778 if (has_mbyte)
3779 len = (*mb_ptr2len)(p);
3780 else
3781#endif
3782 len = 1;
3783 mb_ptr_adv(p);
3784 }
3785 if (in_quote)
3786 bow = p;
3787 else
3788 xp->xp_pattern = p;
3789 p -= len;
3790 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003791 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 }
3793
3794 /*
3795 * If we are still inside the quotes, and we passed a space, just
3796 * expand from there.
3797 */
3798 if (bow != NULL && in_quote)
3799 xp->xp_pattern = bow;
3800 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003801
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003802 /* For a shell command more chars need to be escaped. */
3803 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003804 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003805#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003806 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003807#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003808 /* When still after the command name expand executables. */
3809 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003810 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812
3813 /* Check for environment variable */
3814 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003815#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 || *xp->xp_pattern == '%'
3817#endif
3818 )
3819 {
3820 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3821 if (!vim_isIDc(*p))
3822 break;
3823 if (*p == NUL)
3824 {
3825 xp->xp_context = EXPAND_ENV_VARS;
3826 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003827#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3828 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003829 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003830 compl = EXPAND_ENV_VARS;
3831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 }
3833 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003834#if defined(FEAT_CMDL_COMPL)
3835 /* Check for user names */
3836 if (*xp->xp_pattern == '~')
3837 {
3838 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3839 ;
3840 /* Complete ~user only if it partially matches a user name.
3841 * A full match ~user<Tab> will be replaced by user's home
3842 * directory i.e. something like ~user<Tab> -> /home/user/ */
3843 if (*p == NUL && p > xp->xp_pattern + 1
3844 && match_user(xp->xp_pattern + 1) == 1)
3845 {
3846 xp->xp_context = EXPAND_USER;
3847 ++xp->xp_pattern;
3848 }
3849 }
3850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 }
3852
3853/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003854 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003856 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003858 case CMD_find:
3859 case CMD_sfind:
3860 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003861 if (xp->xp_context == EXPAND_FILES)
3862 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003863 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 case CMD_cd:
3865 case CMD_chdir:
3866 case CMD_lcd:
3867 case CMD_lchdir:
3868 if (xp->xp_context == EXPAND_FILES)
3869 xp->xp_context = EXPAND_DIRECTORIES;
3870 break;
3871 case CMD_help:
3872 xp->xp_context = EXPAND_HELP;
3873 xp->xp_pattern = arg;
3874 break;
3875
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003876 /* Command modifiers: return the argument.
3877 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003879 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 case CMD_belowright:
3881 case CMD_botright:
3882 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003883 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003884 case CMD_cdo:
3885 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003887 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 case CMD_folddoclosed:
3889 case CMD_folddoopen:
3890 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003891 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 case CMD_keepjumps:
3893 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003894 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003895 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003897 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003899 case CMD_noautocmd:
3900 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003902 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003904 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003905 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 case CMD_topleft:
3907 case CMD_verbose:
3908 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003909 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 return arg;
3911
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003912 case CMD_filter:
3913 if (*arg != NUL)
3914 arg = skip_vimgrep_pat(arg, NULL, NULL);
3915 if (arg == NULL || *arg == NUL)
3916 {
3917 xp->xp_context = EXPAND_NOTHING;
3918 return NULL;
3919 }
3920 return skipwhite(arg);
3921
Bram Moolenaar4f688582007-07-24 12:34:30 +00003922#ifdef FEAT_CMDL_COMPL
3923# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 case CMD_match:
3925 if (*arg == NUL || !ends_excmd(*arg))
3926 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003927 /* also complete "None" */
3928 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 arg = skipwhite(skiptowhite(arg));
3930 if (*arg != NUL)
3931 {
3932 xp->xp_context = EXPAND_NOTHING;
3933 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3934 }
3935 }
3936 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003937# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939/*
3940 * All completion for the +cmdline_compl feature goes here.
3941 */
3942
3943# ifdef FEAT_USR_CMDS
3944 case CMD_command:
3945 /* Check for attributes */
3946 while (*arg == '-')
3947 {
3948 arg++; /* Skip "-" */
3949 p = skiptowhite(arg);
3950 if (*p == NUL)
3951 {
3952 /* Cursor is still in the attribute */
3953 p = vim_strchr(arg, '=');
3954 if (p == NULL)
3955 {
3956 /* No "=", so complete attribute names */
3957 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3958 xp->xp_pattern = arg;
3959 return NULL;
3960 }
3961
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003962 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 * their arguments as well.
3964 */
3965 if (STRNICMP(arg, "complete", p - arg) == 0)
3966 {
3967 xp->xp_context = EXPAND_USER_COMPLETE;
3968 xp->xp_pattern = p + 1;
3969 return NULL;
3970 }
3971 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3972 {
3973 xp->xp_context = EXPAND_USER_NARGS;
3974 xp->xp_pattern = p + 1;
3975 return NULL;
3976 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003977 else if (STRNICMP(arg, "addr", p - arg) == 0)
3978 {
3979 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3980 xp->xp_pattern = p + 1;
3981 return NULL;
3982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 return NULL;
3984 }
3985 arg = skipwhite(p);
3986 }
3987
3988 /* After the attributes comes the new command name */
3989 p = skiptowhite(arg);
3990 if (*p == NUL)
3991 {
3992 xp->xp_context = EXPAND_USER_COMMANDS;
3993 xp->xp_pattern = arg;
3994 break;
3995 }
3996
3997 /* And finally comes a normal command */
3998 return skipwhite(p);
3999
4000 case CMD_delcommand:
4001 xp->xp_context = EXPAND_USER_COMMANDS;
4002 xp->xp_pattern = arg;
4003 break;
4004# endif
4005
4006 case CMD_global:
4007 case CMD_vglobal:
4008 delim = *arg; /* get the delimiter */
4009 if (delim)
4010 ++arg; /* skip delimiter if there is one */
4011
4012 while (arg[0] != NUL && arg[0] != delim)
4013 {
4014 if (arg[0] == '\\' && arg[1] != NUL)
4015 ++arg;
4016 ++arg;
4017 }
4018 if (arg[0] != NUL)
4019 return arg + 1;
4020 break;
4021 case CMD_and:
4022 case CMD_substitute:
4023 delim = *arg;
4024 if (delim)
4025 {
4026 /* skip "from" part */
4027 ++arg;
4028 arg = skip_regexp(arg, delim, p_magic, NULL);
4029 }
4030 /* skip "to" part */
4031 while (arg[0] != NUL && arg[0] != delim)
4032 {
4033 if (arg[0] == '\\' && arg[1] != NUL)
4034 ++arg;
4035 ++arg;
4036 }
4037 if (arg[0] != NUL) /* skip delimiter */
4038 ++arg;
4039 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
4040 ++arg;
4041 if (arg[0] != NUL)
4042 return arg;
4043 break;
4044 case CMD_isearch:
4045 case CMD_dsearch:
4046 case CMD_ilist:
4047 case CMD_dlist:
4048 case CMD_ijump:
4049 case CMD_psearch:
4050 case CMD_djump:
4051 case CMD_isplit:
4052 case CMD_dsplit:
4053 arg = skipwhite(skipdigits(arg)); /* skip count */
4054 if (*arg == '/') /* Match regexp, not just whole words */
4055 {
4056 for (++arg; *arg && *arg != '/'; arg++)
4057 if (*arg == '\\' && arg[1] != NUL)
4058 arg++;
4059 if (*arg)
4060 {
4061 arg = skipwhite(arg + 1);
4062
4063 /* Check for trailing illegal characters */
4064 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4065 xp->xp_context = EXPAND_NOTHING;
4066 else
4067 return arg;
4068 }
4069 }
4070 break;
4071#ifdef FEAT_AUTOCMD
4072 case CMD_autocmd:
4073 return set_context_in_autocmd(xp, arg, FALSE);
4074
4075 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004076 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 return set_context_in_autocmd(xp, arg, TRUE);
4078#endif
4079 case CMD_set:
4080 set_context_in_set_cmd(xp, arg, 0);
4081 break;
4082 case CMD_setglobal:
4083 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4084 break;
4085 case CMD_setlocal:
4086 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4087 break;
4088 case CMD_tag:
4089 case CMD_stag:
4090 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004091 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 case CMD_tselect:
4093 case CMD_stselect:
4094 case CMD_ptselect:
4095 case CMD_tjump:
4096 case CMD_stjump:
4097 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004098 if (*p_wop != NUL)
4099 xp->xp_context = EXPAND_TAGS_LISTFILES;
4100 else
4101 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 xp->xp_pattern = arg;
4103 break;
4104 case CMD_augroup:
4105 xp->xp_context = EXPAND_AUGROUP;
4106 xp->xp_pattern = arg;
4107 break;
4108#ifdef FEAT_SYN_HL
4109 case CMD_syntax:
4110 set_context_in_syntax_cmd(xp, arg);
4111 break;
4112#endif
4113#ifdef FEAT_EVAL
4114 case CMD_let:
4115 case CMD_if:
4116 case CMD_elseif:
4117 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004118 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 case CMD_echo:
4120 case CMD_echon:
4121 case CMD_execute:
4122 case CMD_echomsg:
4123 case CMD_echoerr:
4124 case CMD_call:
4125 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004126 case CMD_cexpr:
4127 case CMD_caddexpr:
4128 case CMD_cgetexpr:
4129 case CMD_lexpr:
4130 case CMD_laddexpr:
4131 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004132 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 break;
4134
4135 case CMD_unlet:
4136 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4137 arg = xp->xp_pattern + 1;
4138 xp->xp_context = EXPAND_USER_VARS;
4139 xp->xp_pattern = arg;
4140 break;
4141
4142 case CMD_function:
4143 case CMD_delfunction:
4144 xp->xp_context = EXPAND_USER_FUNC;
4145 xp->xp_pattern = arg;
4146 break;
4147
4148 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004149 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 break;
4151#endif
4152 case CMD_highlight:
4153 set_context_in_highlight_cmd(xp, arg);
4154 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004155#ifdef FEAT_CSCOPE
4156 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004157 case CMD_lcscope:
4158 case CMD_scscope:
4159 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004160 break;
4161#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004162#ifdef FEAT_SIGNS
4163 case CMD_sign:
4164 set_context_in_sign_cmd(xp, arg);
4165 break;
4166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167#ifdef FEAT_LISTCMDS
4168 case CMD_bdelete:
4169 case CMD_bwipeout:
4170 case CMD_bunload:
4171 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4172 arg = xp->xp_pattern + 1;
4173 /*FALLTHROUGH*/
4174 case CMD_buffer:
4175 case CMD_sbuffer:
4176 case CMD_checktime:
4177 xp->xp_context = EXPAND_BUFFERS;
4178 xp->xp_pattern = arg;
4179 break;
4180#endif
4181#ifdef FEAT_USR_CMDS
4182 case CMD_USER:
4183 case CMD_USER_BUF:
4184 if (compl != EXPAND_NOTHING)
4185 {
4186 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004187 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 {
4189# ifdef FEAT_MENU
4190 if (compl == EXPAND_MENUS)
4191 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4192# endif
4193 if (compl == EXPAND_COMMANDS)
4194 return arg;
4195 if (compl == EXPAND_MAPPINGS)
4196 return set_context_in_map_cmd(xp, (char_u *)"map",
4197 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004198 /* Find start of last argument. */
4199 p = arg;
4200 while (*p)
4201 {
4202 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004203 /* argument starts after a space */
4204 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004205 else if (*p == '\\' && *(p + 1) != NUL)
4206 ++p; /* skip over escaped character */
4207 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 xp->xp_pattern = arg;
4210 }
4211 xp->xp_context = compl;
4212 }
4213 break;
4214#endif
4215 case CMD_map: case CMD_noremap:
4216 case CMD_nmap: case CMD_nnoremap:
4217 case CMD_vmap: case CMD_vnoremap:
4218 case CMD_omap: case CMD_onoremap:
4219 case CMD_imap: case CMD_inoremap:
4220 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004221 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004222 case CMD_smap: case CMD_snoremap:
4223 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004225 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 case CMD_unmap:
4227 case CMD_nunmap:
4228 case CMD_vunmap:
4229 case CMD_ounmap:
4230 case CMD_iunmap:
4231 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004232 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004233 case CMD_sunmap:
4234 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004236 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 case CMD_abbreviate: case CMD_noreabbrev:
4238 case CMD_cabbrev: case CMD_cnoreabbrev:
4239 case CMD_iabbrev: case CMD_inoreabbrev:
4240 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004241 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 case CMD_unabbreviate:
4243 case CMD_cunabbrev:
4244 case CMD_iunabbrev:
4245 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004246 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247#ifdef FEAT_MENU
4248 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4249 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4250 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4251 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4252 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4253 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4254 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4255 case CMD_tmenu: case CMD_tunmenu:
4256 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4257 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4258#endif
4259
4260 case CMD_colorscheme:
4261 xp->xp_context = EXPAND_COLORS;
4262 xp->xp_pattern = arg;
4263 break;
4264
4265 case CMD_compiler:
4266 xp->xp_context = EXPAND_COMPILER;
4267 xp->xp_pattern = arg;
4268 break;
4269
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004270 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004271 xp->xp_context = EXPAND_OWNSYNTAX;
4272 xp->xp_pattern = arg;
4273 break;
4274
4275 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004276 xp->xp_context = EXPAND_FILETYPE;
4277 xp->xp_pattern = arg;
4278 break;
4279
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004280 case CMD_packadd:
4281 xp->xp_context = EXPAND_PACKADD;
4282 xp->xp_pattern = arg;
4283 break;
4284
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4286 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4287 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004288 p = skiptowhite(arg);
4289 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 {
4291 xp->xp_context = EXPAND_LANGUAGE;
4292 xp->xp_pattern = arg;
4293 }
4294 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004295 {
4296 if ( STRNCMP(arg, "messages", p - arg) == 0
4297 || STRNCMP(arg, "ctype", p - arg) == 0
4298 || STRNCMP(arg, "time", p - arg) == 0)
4299 {
4300 xp->xp_context = EXPAND_LOCALES;
4301 xp->xp_pattern = skipwhite(p);
4302 }
4303 else
4304 xp->xp_context = EXPAND_NOTHING;
4305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 break;
4307#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004308#if defined(FEAT_PROFILE)
4309 case CMD_profile:
4310 set_context_in_profile_cmd(xp, arg);
4311 break;
4312#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004313 case CMD_behave:
4314 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004315 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004316 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004318 case CMD_messages:
4319 xp->xp_context = EXPAND_MESSAGES;
4320 xp->xp_pattern = arg;
4321 break;
4322
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004323#if defined(FEAT_CMDHIST)
4324 case CMD_history:
4325 xp->xp_context = EXPAND_HISTORY;
4326 xp->xp_pattern = arg;
4327 break;
4328#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004329#if defined(FEAT_PROFILE)
4330 case CMD_syntime:
4331 xp->xp_context = EXPAND_SYNTIME;
4332 xp->xp_pattern = arg;
4333 break;
4334#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004335
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336#endif /* FEAT_CMDL_COMPL */
4337
4338 default:
4339 break;
4340 }
4341 return NULL;
4342}
4343
4344/*
4345 * skip a range specifier of the form: addr [,addr] [;addr] ..
4346 *
4347 * Backslashed delimiters after / or ? will be skipped, and commands will
4348 * not be expanded between /'s and ?'s or after "'".
4349 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004350 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 * Returns the "cmd" pointer advanced to beyond the range.
4352 */
4353 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004354skip_range(
4355 char_u *cmd,
4356 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004358 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004360 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004362 if (*cmd == '\\')
4363 {
4364 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4365 ++cmd;
4366 else
4367 break;
4368 }
4369 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 {
4371 if (*++cmd == NUL && ctx != NULL)
4372 *ctx = EXPAND_NOTHING;
4373 }
4374 else if (*cmd == '/' || *cmd == '?')
4375 {
4376 delim = *cmd++;
4377 while (*cmd != NUL && *cmd != delim)
4378 if (*cmd++ == '\\' && *cmd != NUL)
4379 ++cmd;
4380 if (*cmd == NUL && ctx != NULL)
4381 *ctx = EXPAND_NOTHING;
4382 }
4383 if (*cmd != NUL)
4384 ++cmd;
4385 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004386
4387 /* Skip ":" and white space. */
4388 while (*cmd == ':')
4389 cmd = skipwhite(cmd + 1);
4390
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 return cmd;
4392}
4393
4394/*
4395 * get a single EX address
4396 *
4397 * Set ptr to the next character after the part that was interpreted.
4398 * Set ptr to NULL when an error is encountered.
4399 *
4400 * Return MAXLNUM when no Ex address was found.
4401 */
4402 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004403get_address(
4404 exarg_T *eap UNUSED,
4405 char_u **ptr,
4406 int addr_type, /* flag: one of ADDR_LINES, ... */
4407 int skip, /* only skip the address, don't use it */
Bram Moolenaarded27822017-01-02 14:27:34 +01004408 int to_other_file, /* flag: may jump to other file */
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01004409 int address_count UNUSED) /* 1 for first address, >1 after comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410{
4411 int c;
4412 int i;
4413 long n;
4414 char_u *cmd;
4415 pos_T pos;
4416 pos_T *fp;
4417 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004418 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419
4420 cmd = skipwhite(*ptr);
4421 lnum = MAXLNUM;
4422 do
4423 {
4424 switch (*cmd)
4425 {
4426 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004427 ++cmd;
4428 switch (addr_type)
4429 {
4430 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 lnum = curwin->w_cursor.lnum;
4432 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004433 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004434 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004435 break;
4436 case ADDR_ARGUMENTS:
4437 lnum = curwin->w_arg_idx + 1;
4438 break;
4439 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004440 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004441 lnum = curbuf->b_fnum;
4442 break;
4443 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004444 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004445 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004446 case ADDR_TABS_RELATIVE:
4447 EMSG(_(e_invrange));
4448 cmd = NULL;
4449 goto error;
4450 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004451#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004452 case ADDR_QUICKFIX:
4453 lnum = qf_get_cur_valid_idx(eap);
4454 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004455#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004456 }
4457 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458
4459 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004460 ++cmd;
4461 switch (addr_type)
4462 {
4463 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 lnum = curbuf->b_ml.ml_line_count;
4465 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004466 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004467 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004468 break;
4469 case ADDR_ARGUMENTS:
4470 lnum = ARGCOUNT;
4471 break;
4472 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004473 buf = lastbuf;
4474 while (buf->b_ml.ml_mfp == NULL)
4475 {
4476 if (buf->b_prev == NULL)
4477 break;
4478 buf = buf->b_prev;
4479 }
4480 lnum = buf->b_fnum;
4481 break;
4482 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004483 lnum = lastbuf->b_fnum;
4484 break;
4485 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004486 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004487 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004488 case ADDR_TABS_RELATIVE:
4489 EMSG(_(e_invrange));
4490 cmd = NULL;
4491 goto error;
4492 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004493#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004494 case ADDR_QUICKFIX:
4495 lnum = qf_get_size(eap);
4496 if (lnum == 0)
4497 lnum = 1;
4498 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004499#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004500 }
4501 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502
4503 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004504 if (*++cmd == NUL)
4505 {
4506 cmd = NULL;
4507 goto error;
4508 }
4509 if (addr_type != ADDR_LINES)
4510 {
4511 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004512 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004513 goto error;
4514 }
4515 if (skip)
4516 ++cmd;
4517 else
4518 {
4519 /* Only accept a mark in another file when it is
4520 * used by itself: ":'M". */
4521 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4522 ++cmd;
4523 if (fp == (pos_T *)-1)
4524 /* Jumped to another file. */
4525 lnum = curwin->w_cursor.lnum;
4526 else
4527 {
4528 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 {
4530 cmd = NULL;
4531 goto error;
4532 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004533 lnum = fp->lnum;
4534 }
4535 }
4536 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537
4538 case '/':
4539 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004540 c = *cmd++;
4541 if (addr_type != ADDR_LINES)
4542 {
4543 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004544 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004545 goto error;
4546 }
4547 if (skip) /* skip "/pat/" */
4548 {
4549 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4550 if (*cmd == c)
4551 ++cmd;
4552 }
4553 else
4554 {
4555 pos = curwin->w_cursor; /* save curwin->w_cursor */
4556 /*
4557 * When '/' or '?' follows another address, start
4558 * from there.
4559 */
4560 if (lnum != MAXLNUM)
4561 curwin->w_cursor.lnum = lnum;
4562 /*
4563 * Start a forward search at the end of the line.
4564 * Start a backward search at the start of the line.
4565 * This makes sure we never match in the current
4566 * line, and can match anywhere in the
4567 * next/previous line.
4568 */
4569 if (c == '/')
4570 curwin->w_cursor.col = MAXCOL;
4571 else
4572 curwin->w_cursor.col = 0;
4573 searchcmdlen = 0;
4574 if (!do_search(NULL, c, cmd, 1L,
4575 SEARCH_HIS | SEARCH_MSG, NULL))
4576 {
4577 curwin->w_cursor = pos;
4578 cmd = NULL;
4579 goto error;
4580 }
4581 lnum = curwin->w_cursor.lnum;
4582 curwin->w_cursor = pos;
4583 /* adjust command string pointer */
4584 cmd += searchcmdlen;
4585 }
4586 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587
4588 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004589 ++cmd;
4590 if (addr_type != ADDR_LINES)
4591 {
4592 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004593 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004594 goto error;
4595 }
4596 if (*cmd == '&')
4597 i = RE_SUBST;
4598 else if (*cmd == '?' || *cmd == '/')
4599 i = RE_SEARCH;
4600 else
4601 {
4602 EMSG(_(e_backslash));
4603 cmd = NULL;
4604 goto error;
4605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004607 if (!skip)
4608 {
4609 /*
4610 * When search follows another address, start from
4611 * there.
4612 */
4613 if (lnum != MAXLNUM)
4614 pos.lnum = lnum;
4615 else
4616 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004618 /*
4619 * Start the search just like for the above
4620 * do_search().
4621 */
4622 if (*cmd != '?')
4623 pos.col = MAXCOL;
4624 else
4625 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004626#ifdef FEAT_VIRTUALEDIT
4627 pos.coladd = 0;
4628#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004629 if (searchit(curwin, curbuf, &pos,
4630 *cmd == '?' ? BACKWARD : FORWARD,
4631 (char_u *)"", 1L, SEARCH_MSG,
4632 i, (linenr_T)0, NULL) != FAIL)
4633 lnum = pos.lnum;
4634 else
4635 {
4636 cmd = NULL;
4637 goto error;
4638 }
4639 }
4640 ++cmd;
4641 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642
4643 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004644 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4645 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 }
4647
4648 for (;;)
4649 {
4650 cmd = skipwhite(cmd);
4651 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4652 break;
4653
4654 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004655 {
4656 switch (addr_type)
4657 {
4658 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004659 /* "+1" is same as ".+1" */
4660 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004661 break;
4662 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004663 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004664 break;
4665 case ADDR_ARGUMENTS:
4666 lnum = curwin->w_arg_idx + 1;
4667 break;
4668 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004669 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004670 lnum = curbuf->b_fnum;
4671 break;
4672 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004673 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004674 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004675 case ADDR_TABS_RELATIVE:
4676 lnum = 1;
4677 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004678#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004679 case ADDR_QUICKFIX:
4680 lnum = qf_get_cur_valid_idx(eap);
4681 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004682#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004683 }
4684 }
4685
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 if (VIM_ISDIGIT(*cmd))
4687 i = '+'; /* "number" is same as "+number" */
4688 else
4689 i = *cmd++;
4690 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4691 n = 1;
4692 else
4693 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004694
4695 if (addr_type == ADDR_TABS_RELATIVE)
4696 {
4697 EMSG(_(e_invrange));
4698 cmd = NULL;
4699 goto error;
4700 }
4701 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004702 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004703 lnum = compute_buffer_local_count(
4704 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004706 {
4707#ifdef FEAT_FOLDING
4708 /* Relative line addressing, need to adjust for folded lines
4709 * now, but only do it after the first address. */
4710 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4711 && address_count >= 2)
4712 (void)hasFolding(lnum, NULL, &lnum);
4713#endif
4714 if (i == '-')
4715 lnum -= n;
4716 else
4717 lnum += n;
4718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 }
4720 } while (*cmd == '/' || *cmd == '?');
4721
4722error:
4723 *ptr = cmd;
4724 return lnum;
4725}
4726
4727/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004728 * Get flags from an Ex command argument.
4729 */
4730 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004731get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004732{
4733 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4734 {
4735 if (*eap->arg == 'l')
4736 eap->flags |= EXFLAG_LIST;
4737 else if (*eap->arg == 'p')
4738 eap->flags |= EXFLAG_PRINT;
4739 else
4740 eap->flags |= EXFLAG_NR;
4741 eap->arg = skipwhite(eap->arg + 1);
4742 }
4743}
4744
4745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 * Function called for command which is Not Implemented. NI!
4747 */
4748 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004749ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750{
4751 if (!eap->skip)
4752 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4753}
4754
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004755#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756/*
4757 * Function called for script command which is Not Implemented. NI!
4758 * Skips over ":perl <<EOF" constructs.
4759 */
4760 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004761ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762{
4763 if (!eap->skip)
4764 ex_ni(eap);
4765 else
4766 vim_free(script_get(eap, eap->arg));
4767}
4768#endif
4769
4770/*
4771 * Check range in Ex command for validity.
4772 * Return NULL when valid, error message when invalid.
4773 */
4774 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004775invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004777 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 if ( eap->line1 < 0
4779 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004780 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004782
4783 if (eap->argt & RANGE)
4784 {
4785 switch(eap->addr_type)
4786 {
4787 case ADDR_LINES:
4788 if (!(eap->argt & NOTADR)
4789 && eap->line2 > curbuf->b_ml.ml_line_count
4790#ifdef FEAT_DIFF
4791 + (eap->cmdidx == CMD_diffget)
4792#endif
4793 )
4794 return (char_u *)_(e_invrange);
4795 break;
4796 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004797 /* add 1 if ARGCOUNT is 0 */
4798 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004799 return (char_u *)_(e_invrange);
4800 break;
4801 case ADDR_BUFFERS:
4802 if (eap->line1 < firstbuf->b_fnum
4803 || eap->line2 > lastbuf->b_fnum)
4804 return (char_u *)_(e_invrange);
4805 break;
4806 case ADDR_LOADED_BUFFERS:
4807 buf = firstbuf;
4808 while (buf->b_ml.ml_mfp == NULL)
4809 {
4810 if (buf->b_next == NULL)
4811 return (char_u *)_(e_invrange);
4812 buf = buf->b_next;
4813 }
4814 if (eap->line1 < buf->b_fnum)
4815 return (char_u *)_(e_invrange);
4816 buf = lastbuf;
4817 while (buf->b_ml.ml_mfp == NULL)
4818 {
4819 if (buf->b_prev == NULL)
4820 return (char_u *)_(e_invrange);
4821 buf = buf->b_prev;
4822 }
4823 if (eap->line2 > buf->b_fnum)
4824 return (char_u *)_(e_invrange);
4825 break;
4826 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004827 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004828 return (char_u *)_(e_invrange);
4829 break;
4830 case ADDR_TABS:
4831 if (eap->line2 > LAST_TAB_NR)
4832 return (char_u *)_(e_invrange);
4833 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004834 case ADDR_TABS_RELATIVE:
4835 /* Do nothing */
4836 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004837#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004838 case ADDR_QUICKFIX:
4839 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4840 return (char_u *)_(e_invrange);
4841 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004842#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004843 }
4844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 return NULL;
4846}
4847
4848/*
4849 * Correct the range for zero line number, if required.
4850 */
4851 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004852correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853{
4854 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4855 {
4856 if (eap->line1 == 0)
4857 eap->line1 = 1;
4858 if (eap->line2 == 0)
4859 eap->line2 = 1;
4860 }
4861}
4862
Bram Moolenaar748bf032005-02-02 23:04:36 +00004863#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004864static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004865
4866/*
4867 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4868 * pattern. Otherwise return eap->arg.
4869 */
4870 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004871skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004872{
4873 char_u *p = eap->arg;
4874
Bram Moolenaara37420f2006-02-04 22:37:47 +00004875 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4876 || eap->cmdidx == CMD_vimgrepadd
4877 || eap->cmdidx == CMD_lvimgrepadd
4878 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004879 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004880 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004881 if (p == NULL)
4882 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004883 }
4884 return p;
4885}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004886
4887/*
4888 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4889 * in the command line, so that things like % get expanded.
4890 */
4891 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004892replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004893{
4894 char_u *new_cmdline;
4895 char_u *program;
4896 char_u *pos;
4897 char_u *ptr;
4898 int len;
4899 int i;
4900
4901 /*
4902 * Don't do it when ":vimgrep" is used for ":grep".
4903 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004904 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4905 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4906 || eap->cmdidx == CMD_grepadd
4907 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004908 && !grep_internal(eap->cmdidx))
4909 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004910 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4911 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004912 {
4913 if (*curbuf->b_p_gp == NUL)
4914 program = p_gp;
4915 else
4916 program = curbuf->b_p_gp;
4917 }
4918 else
4919 {
4920 if (*curbuf->b_p_mp == NUL)
4921 program = p_mp;
4922 else
4923 program = curbuf->b_p_mp;
4924 }
4925
4926 p = skipwhite(p);
4927
4928 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4929 {
4930 /* replace $* by given arguments */
4931 i = 1;
4932 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4933 ++i;
4934 len = (int)STRLEN(p);
4935 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4936 if (new_cmdline == NULL)
4937 return NULL; /* out of memory */
4938 ptr = new_cmdline;
4939 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4940 {
4941 i = (int)(pos - program);
4942 STRNCPY(ptr, program, i);
4943 STRCPY(ptr += i, p);
4944 ptr += len;
4945 program = pos + 2;
4946 }
4947 STRCPY(ptr, program);
4948 }
4949 else
4950 {
4951 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4952 if (new_cmdline == NULL)
4953 return NULL; /* out of memory */
4954 STRCPY(new_cmdline, program);
4955 STRCAT(new_cmdline, " ");
4956 STRCAT(new_cmdline, p);
4957 }
4958 msg_make(p);
4959
4960 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4961 vim_free(*cmdlinep);
4962 *cmdlinep = new_cmdline;
4963 p = new_cmdline;
4964 }
4965 return p;
4966}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004967#endif
4968
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969/*
4970 * Expand file name in Ex command argument.
4971 * Return FAIL for failure, OK otherwise.
4972 */
4973 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004974expand_filename(
4975 exarg_T *eap,
4976 char_u **cmdlinep,
4977 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978{
4979 int has_wildcards; /* need to expand wildcards */
4980 char_u *repl;
4981 int srclen;
4982 char_u *p;
4983 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004984 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985
Bram Moolenaar748bf032005-02-02 23:04:36 +00004986#ifdef FEAT_QUICKFIX
4987 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4988 p = skip_grep_pat(eap);
4989#else
4990 p = eap->arg;
4991#endif
4992
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 /*
4994 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4995 * the file name contains a wildcard it should not cause expanding.
4996 * (it will be expanded anyway if there is a wildcard before replacing).
4997 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004998 has_wildcards = mch_has_wildcard(p);
4999 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005001#ifdef FEAT_EVAL
5002 /* Skip over `=expr`, wildcards in it are not expanded. */
5003 if (p[0] == '`' && p[1] == '=')
5004 {
5005 p += 2;
5006 (void)skip_expr(&p);
5007 if (*p == '`')
5008 ++p;
5009 continue;
5010 }
5011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 /*
5013 * Quick check if this cannot be the start of a special string.
5014 * Also removes backslash before '%', '#' and '<'.
5015 */
5016 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5017 {
5018 ++p;
5019 continue;
5020 }
5021
5022 /*
5023 * Try to find a match at this position.
5024 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005025 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
5026 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 if (*errormsgp != NULL) /* error detected */
5028 return FAIL;
5029 if (repl == NULL) /* no match found */
5030 {
5031 p += srclen;
5032 continue;
5033 }
5034
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005035 /* Wildcards won't be expanded below, the replacement is taken
5036 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
5037 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5038 {
5039 char_u *l = repl;
5040
5041 repl = expand_env_save(repl);
5042 vim_free(l);
5043 }
5044
Bram Moolenaar63b92542007-03-27 14:57:09 +00005045 /* Need to escape white space et al. with a backslash.
5046 * Don't do this for:
5047 * - replacement that already has been escaped: "##"
5048 * - shell commands (may have to use quotes instead).
5049 * - non-unix systems when there is a single argument (spaces don't
5050 * separate arguments then).
5051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005053 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 && eap->cmdidx != CMD_bang
5055 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00005056 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00005058 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00005060 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061#ifndef UNIX
5062 && !(eap->argt & NOSPC)
5063#endif
5064 )
5065 {
5066 char_u *l;
5067#ifdef BACKSLASH_IN_FILENAME
5068 /* Don't escape a backslash here, because rem_backslash() doesn't
5069 * remove it later. */
5070 static char_u *nobslash = (char_u *)" \t\"|";
5071# define ESCAPE_CHARS nobslash
5072#else
5073# define ESCAPE_CHARS escape_chars
5074#endif
5075
5076 for (l = repl; *l; ++l)
5077 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5078 {
5079 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5080 if (l != NULL)
5081 {
5082 vim_free(repl);
5083 repl = l;
5084 }
5085 break;
5086 }
5087 }
5088
5089 /* For a shell command a '!' must be escaped. */
5090 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005091 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 {
5093 char_u *l;
5094
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005095 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 if (l != NULL)
5097 {
5098 vim_free(repl);
5099 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 }
5101 }
5102
5103 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5104 vim_free(repl);
5105 if (p == NULL)
5106 return FAIL;
5107 }
5108
5109 /*
5110 * One file argument: Expand wildcards.
5111 * Don't do this with ":r !command" or ":w !command".
5112 */
5113 if ((eap->argt & NOSPC) && !eap->usefilter)
5114 {
5115 /*
5116 * May do this twice:
5117 * 1. Replace environment variables.
5118 * 2. Replace any other wildcards, remove backslashes.
5119 */
5120 for (n = 1; n <= 2; ++n)
5121 {
5122 if (n == 2)
5123 {
5124#ifdef UNIX
5125 /*
5126 * Only for Unix we check for more than one file name.
5127 * For other systems spaces are considered to be part
5128 * of the file name.
5129 * Only check here if there is no wildcard, otherwise
5130 * ExpandOne() will check for errors. This allows
5131 * ":e `ls ve*.c`" on Unix.
5132 */
5133 if (!has_wildcards)
5134 for (p = eap->arg; *p; ++p)
5135 {
5136 /* skip escaped characters */
5137 if (p[1] && (*p == '\\' || *p == Ctrl_V))
5138 ++p;
5139 else if (vim_iswhite(*p))
5140 {
5141 *errormsgp = (char_u *)_("E172: Only one file name allowed");
5142 return FAIL;
5143 }
5144 }
5145#endif
5146
5147 /*
5148 * Halve the number of backslashes (this is Vi compatible).
5149 * For Unix and OS/2, when wildcards are expanded, this is
5150 * done by ExpandOne() below.
5151 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005152#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 if (!has_wildcards)
5154#endif
5155 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 }
5157
5158 if (has_wildcards)
5159 {
5160 if (n == 1)
5161 {
5162 /*
5163 * First loop: May expand environment variables. This
5164 * can be done much faster with expand_env() than with
5165 * something else (e.g., calling a shell).
5166 * After expanding environment variables, check again
5167 * if there are still wildcards present.
5168 */
5169 if (vim_strchr(eap->arg, '$') != NULL
5170 || vim_strchr(eap->arg, '~') != NULL)
5171 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005172 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005173 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 has_wildcards = mch_has_wildcard(NameBuff);
5175 p = NameBuff;
5176 }
5177 else
5178 p = NULL;
5179 }
5180 else /* n == 2 */
5181 {
5182 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005183 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184
5185 ExpandInit(&xpc);
5186 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005187 if (p_wic)
5188 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005190 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 if (p == NULL)
5192 return FAIL;
5193 }
5194 if (p != NULL)
5195 {
5196 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5197 p, cmdlinep);
5198 if (n == 2) /* p came from ExpandOne() */
5199 vim_free(p);
5200 }
5201 }
5202 }
5203 }
5204 return OK;
5205}
5206
5207/*
5208 * Replace part of the command line, keeping eap->cmd, eap->arg and
5209 * eap->nextcmd correct.
5210 * "src" points to the part that is to be replaced, of length "srclen".
5211 * "repl" is the replacement string.
5212 * Returns a pointer to the character after the replaced string.
5213 * Returns NULL for failure.
5214 */
5215 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005216repl_cmdline(
5217 exarg_T *eap,
5218 char_u *src,
5219 int srclen,
5220 char_u *repl,
5221 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222{
5223 int len;
5224 int i;
5225 char_u *new_cmdline;
5226
5227 /*
5228 * The new command line is build in new_cmdline[].
5229 * First allocate it.
5230 * Careful: a "+cmd" argument may have been NUL terminated.
5231 */
5232 len = (int)STRLEN(repl);
5233 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005234 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5236 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5237 return NULL; /* out of memory! */
5238
5239 /*
5240 * Copy the stuff before the expanded part.
5241 * Copy the expanded stuff.
5242 * Copy what came after the expanded part.
5243 * Copy the next commands, if there are any.
5244 */
5245 i = (int)(src - *cmdlinep); /* length of part before match */
5246 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005247
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 mch_memmove(new_cmdline + i, repl, (size_t)len);
5249 i += len; /* remember the end of the string */
5250 STRCPY(new_cmdline + i, src + srclen);
5251 src = new_cmdline + i; /* remember where to continue */
5252
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005253 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 {
5255 i = (int)STRLEN(new_cmdline) + 1;
5256 STRCPY(new_cmdline + i, eap->nextcmd);
5257 eap->nextcmd = new_cmdline + i;
5258 }
5259 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5260 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5261 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5262 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5263 vim_free(*cmdlinep);
5264 *cmdlinep = new_cmdline;
5265
5266 return src;
5267}
5268
5269/*
5270 * Check for '|' to separate commands and '"' to start comments.
5271 */
5272 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005273separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274{
5275 char_u *p;
5276
Bram Moolenaar86b68352004-12-27 21:59:20 +00005277#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005278 p = skip_grep_pat(eap);
5279#else
5280 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005281#endif
5282
5283 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 {
5285 if (*p == Ctrl_V)
5286 {
5287 if (eap->argt & (USECTRLV | XFILE))
5288 ++p; /* skip CTRL-V and next char */
5289 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005290 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005291 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 if (*p == NUL) /* stop at NUL after CTRL-V */
5293 break;
5294 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005295
5296#ifdef FEAT_EVAL
5297 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005298 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005299 {
5300 p += 2;
5301 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005302 }
5303#endif
5304
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 /* Check for '"': start of comment or '|': next command */
5306 /* :@" and :*" do not start a comment!
5307 * :redir @" doesn't either. */
5308 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5309 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5310 || p != eap->arg)
5311 && (eap->cmdidx != CMD_redir
5312 || p != eap->arg + 1 || p[-1] != '@'))
5313 || *p == '|' || *p == '\n')
5314 {
5315 /*
5316 * We remove the '\' before the '|', unless USECTRLV is used
5317 * AND 'b' is present in 'cpoptions'.
5318 */
5319 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5320 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5321 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005322 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 --p;
5324 }
5325 else
5326 {
5327 eap->nextcmd = check_nextcmd(p);
5328 *p = NUL;
5329 break;
5330 }
5331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005333
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5335 del_trailing_spaces(eap->arg);
5336}
5337
5338/*
5339 * get + command from ex argument
5340 */
5341 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005342getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343{
5344 char_u *arg = *argp;
5345 char_u *command = NULL;
5346
5347 if (*arg == '+') /* +[command] */
5348 {
5349 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005350 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 command = dollar_command;
5352 else
5353 {
5354 command = arg;
5355 arg = skip_cmd_arg(command, TRUE);
5356 if (*arg != NUL)
5357 *arg++ = NUL; /* terminate command with NUL */
5358 }
5359
5360 arg = skipwhite(arg); /* skip over spaces */
5361 *argp = arg;
5362 }
5363 return command;
5364}
5365
5366/*
5367 * Find end of "+command" argument. Skip over "\ " and "\\".
5368 */
5369 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005370skip_cmd_arg(
5371 char_u *p,
5372 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373{
5374 while (*p && !vim_isspace(*p))
5375 {
5376 if (*p == '\\' && p[1] != NUL)
5377 {
5378 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005379 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 else
5381 ++p;
5382 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005383 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 }
5385 return p;
5386}
5387
5388/*
5389 * Get "++opt=arg" argument.
5390 * Return FAIL or OK.
5391 */
5392 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005393getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394{
5395 char_u *arg = eap->arg + 2;
5396 int *pp = NULL;
5397#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005398 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 char_u *p;
5400#endif
5401
5402 /* ":edit ++[no]bin[ary] file" */
5403 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5404 {
5405 if (*arg == 'n')
5406 {
5407 arg += 2;
5408 eap->force_bin = FORCE_NOBIN;
5409 }
5410 else
5411 eap->force_bin = FORCE_BIN;
5412 if (!checkforcmd(&arg, "binary", 3))
5413 return FAIL;
5414 eap->arg = skipwhite(arg);
5415 return OK;
5416 }
5417
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005418 /* ":read ++edit file" */
5419 if (STRNCMP(arg, "edit", 4) == 0)
5420 {
5421 eap->read_edit = TRUE;
5422 eap->arg = skipwhite(arg + 4);
5423 return OK;
5424 }
5425
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 if (STRNCMP(arg, "ff", 2) == 0)
5427 {
5428 arg += 2;
5429 pp = &eap->force_ff;
5430 }
5431 else if (STRNCMP(arg, "fileformat", 10) == 0)
5432 {
5433 arg += 10;
5434 pp = &eap->force_ff;
5435 }
5436#ifdef FEAT_MBYTE
5437 else if (STRNCMP(arg, "enc", 3) == 0)
5438 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005439 if (STRNCMP(arg, "encoding", 8) == 0)
5440 arg += 8;
5441 else
5442 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 pp = &eap->force_enc;
5444 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005445 else if (STRNCMP(arg, "bad", 3) == 0)
5446 {
5447 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005448 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450#endif
5451
5452 if (pp == NULL || *arg != '=')
5453 return FAIL;
5454
5455 ++arg;
5456 *pp = (int)(arg - eap->cmd);
5457 arg = skip_cmd_arg(arg, FALSE);
5458 eap->arg = skipwhite(arg);
5459 *arg = NUL;
5460
5461#ifdef FEAT_MBYTE
5462 if (pp == &eap->force_ff)
5463 {
5464#endif
5465 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5466 return FAIL;
5467#ifdef FEAT_MBYTE
5468 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005469 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 {
5471 /* Make 'fileencoding' lower case. */
5472 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5473 *p = TOLOWER_ASC(*p);
5474 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005475 else
5476 {
5477 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5478 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005479 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005480 if (STRICMP(p, "keep") == 0)
5481 eap->bad_char = BAD_KEEP;
5482 else if (STRICMP(p, "drop") == 0)
5483 eap->bad_char = BAD_DROP;
5484 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5485 eap->bad_char = *p;
5486 else
5487 return FAIL;
5488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489#endif
5490
5491 return OK;
5492}
5493
5494/*
5495 * ":abbreviate" and friends.
5496 */
5497 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005498ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499{
5500 do_exmap(eap, TRUE); /* almost the same as mapping */
5501}
5502
5503/*
5504 * ":map" and friends.
5505 */
5506 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005507ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508{
5509 /*
5510 * If we are sourcing .exrc or .vimrc in current directory we
5511 * print the mappings for security reasons.
5512 */
5513 if (secure)
5514 {
5515 secure = 2;
5516 msg_outtrans(eap->cmd);
5517 msg_putchar('\n');
5518 }
5519 do_exmap(eap, FALSE);
5520}
5521
5522/*
5523 * ":unmap" and friends.
5524 */
5525 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005526ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527{
5528 do_exmap(eap, FALSE);
5529}
5530
5531/*
5532 * ":mapclear" and friends.
5533 */
5534 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005535ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536{
5537 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5538}
5539
5540/*
5541 * ":abclear" and friends.
5542 */
5543 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005544ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545{
5546 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5547}
5548
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005549#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005551ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552{
5553 /*
5554 * Disallow auto commands from .exrc and .vimrc in current
5555 * directory for security reasons.
5556 */
5557 if (secure)
5558 {
5559 secure = 2;
5560 eap->errmsg = e_curdir;
5561 }
5562 else if (eap->cmdidx == CMD_autocmd)
5563 do_autocmd(eap->arg, eap->forceit);
5564 else
5565 do_augroup(eap->arg, eap->forceit);
5566}
5567
5568/*
5569 * ":doautocmd": Apply the automatic commands to the current buffer.
5570 */
5571 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005572ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005574 char_u *arg = eap->arg;
5575 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005576 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005577
Bram Moolenaar1610d052016-06-09 22:53:01 +02005578 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5579 /* Only when there is no <nomodeline>. */
5580 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005581 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582}
5583#endif
5584
5585#ifdef FEAT_LISTCMDS
5586/*
5587 * :[N]bunload[!] [N] [bufname] unload buffer
5588 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5589 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5590 */
5591 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005592ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593{
5594 eap->errmsg = do_bufdel(
5595 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5596 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5597 : DOBUF_UNLOAD, eap->arg,
5598 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5599}
5600
5601/*
5602 * :[N]buffer [N] to buffer N
5603 * :[N]sbuffer [N] to buffer N
5604 */
5605 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005606ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607{
5608 if (*eap->arg)
5609 eap->errmsg = e_trailing;
5610 else
5611 {
5612 if (eap->addr_count == 0) /* default is current buffer */
5613 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5614 else
5615 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005616 if (eap->do_ecmd_cmd != NULL)
5617 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 }
5619}
5620
5621/*
5622 * :[N]bmodified [N] to next mod. buffer
5623 * :[N]sbmodified [N] to next mod. buffer
5624 */
5625 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005626ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627{
5628 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005629 if (eap->do_ecmd_cmd != NULL)
5630 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631}
5632
5633/*
5634 * :[N]bnext [N] to next buffer
5635 * :[N]sbnext [N] split and to next buffer
5636 */
5637 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005638ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639{
5640 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005641 if (eap->do_ecmd_cmd != NULL)
5642 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643}
5644
5645/*
5646 * :[N]bNext [N] to previous buffer
5647 * :[N]bprevious [N] to previous buffer
5648 * :[N]sbNext [N] split and to previous buffer
5649 * :[N]sbprevious [N] split and to previous buffer
5650 */
5651 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005652ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653{
5654 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005655 if (eap->do_ecmd_cmd != NULL)
5656 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657}
5658
5659/*
5660 * :brewind to first buffer
5661 * :bfirst to first buffer
5662 * :sbrewind split and to first buffer
5663 * :sbfirst split and to first buffer
5664 */
5665 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005666ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667{
5668 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005669 if (eap->do_ecmd_cmd != NULL)
5670 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671}
5672
5673/*
5674 * :blast to last buffer
5675 * :sblast split and to last buffer
5676 */
5677 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005678ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679{
5680 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005681 if (eap->do_ecmd_cmd != NULL)
5682 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683}
5684#endif
5685
5686 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005687ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688{
5689 return (c == NUL || c == '|' || c == '"' || c == '\n');
5690}
5691
5692#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5693 || defined(PROTO)
5694/*
5695 * Return the next command, after the first '|' or '\n'.
5696 * Return NULL if not found.
5697 */
5698 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005699find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700{
5701 while (*p != '|' && *p != '\n')
5702 {
5703 if (*p == NUL)
5704 return NULL;
5705 ++p;
5706 }
5707 return (p + 1);
5708}
5709#endif
5710
5711/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005712 * Check if *p is a separator between Ex commands, skipping over white space.
5713 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 */
5715 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005716check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005718 char_u *s = skipwhite(p);
5719
5720 if (*s == '|' || *s == '\n')
5721 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 else
5723 return NULL;
5724}
5725
5726/*
5727 * - if there are more files to edit
5728 * - and this is the last window
5729 * - and forceit not used
5730 * - and not repeated twice on a row
5731 * return FAIL and give error message if 'message' TRUE
5732 * return OK otherwise
5733 */
5734 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005735check_more(
5736 int message, /* when FALSE check only, no messages */
5737 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738{
5739 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5740
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005741 if (!forceit && only_one_window()
5742 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 {
5744 if (message)
5745 {
5746#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5747 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5748 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005749 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750
5751 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005752 vim_strncpy(buff,
5753 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005754 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005756 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 _("%d more files to edit. Quit anyway?"), n);
5758 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5759 return OK;
5760 return FAIL;
5761 }
5762#endif
5763 if (n == 1)
5764 EMSG(_("E173: 1 more file to edit"));
5765 else
5766 EMSGN(_("E173: %ld more files to edit"), n);
5767 quitmore = 2; /* next try to quit is allowed */
5768 }
5769 return FAIL;
5770 }
5771 return OK;
5772}
5773
5774#ifdef FEAT_CMDL_COMPL
5775/*
5776 * Function given to ExpandGeneric() to obtain the list of command names.
5777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005779get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780{
5781 if (idx >= (int)CMD_SIZE)
5782# ifdef FEAT_USR_CMDS
5783 return get_user_command_name(idx);
5784# else
5785 return NULL;
5786# endif
5787 return cmdnames[idx].cmd_name;
5788}
5789#endif
5790
5791#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005792static 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);
5793static void uc_list(char_u *name, size_t name_len);
5794static 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);
5795static char_u *uc_split_args(char_u *arg, size_t *lenp);
5796static 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 +00005797
5798 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005799uc_add_command(
5800 char_u *name,
5801 size_t name_len,
5802 char_u *rep,
5803 long argt,
5804 long def,
5805 int flags,
5806 int compl,
5807 char_u *compl_arg,
5808 int addr_type,
5809 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810{
5811 ucmd_T *cmd = NULL;
5812 char_u *p;
5813 int i;
5814 int cmp = 1;
5815 char_u *rep_buf = NULL;
5816 garray_T *gap;
5817
Bram Moolenaar9c102382006-05-03 21:26:49 +00005818 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 if (rep_buf == NULL)
5820 {
5821 /* Can't replace termcodes - try using the string as is */
5822 rep_buf = vim_strsave(rep);
5823
5824 /* Give up if out of memory */
5825 if (rep_buf == NULL)
5826 return FAIL;
5827 }
5828
5829 /* get address of growarray: global or in curbuf */
5830 if (flags & UC_BUFFER)
5831 {
5832 gap = &curbuf->b_ucmds;
5833 if (gap->ga_itemsize == 0)
5834 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5835 }
5836 else
5837 gap = &ucmds;
5838
5839 /* Search for the command in the already defined commands. */
5840 for (i = 0; i < gap->ga_len; ++i)
5841 {
5842 size_t len;
5843
5844 cmd = USER_CMD_GA(gap, i);
5845 len = STRLEN(cmd->uc_name);
5846 cmp = STRNCMP(name, cmd->uc_name, name_len);
5847 if (cmp == 0)
5848 {
5849 if (name_len < len)
5850 cmp = -1;
5851 else if (name_len > len)
5852 cmp = 1;
5853 }
5854
5855 if (cmp == 0)
5856 {
5857 if (!force)
5858 {
5859 EMSG(_("E174: Command already exists: add ! to replace it"));
5860 goto fail;
5861 }
5862
5863 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005864 cmd->uc_rep = NULL;
5865#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5866 vim_free(cmd->uc_compl_arg);
5867 cmd->uc_compl_arg = NULL;
5868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 break;
5870 }
5871
5872 /* Stop as soon as we pass the name to add */
5873 if (cmp < 0)
5874 break;
5875 }
5876
5877 /* Extend the array unless we're replacing an existing command */
5878 if (cmp != 0)
5879 {
5880 if (ga_grow(gap, 1) != OK)
5881 goto fail;
5882 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5883 goto fail;
5884
5885 cmd = USER_CMD_GA(gap, i);
5886 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5887
5888 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889
5890 cmd->uc_name = p;
5891 }
5892
5893 cmd->uc_rep = rep_buf;
5894 cmd->uc_argt = argt;
5895 cmd->uc_def = def;
5896 cmd->uc_compl = compl;
5897#ifdef FEAT_EVAL
5898 cmd->uc_scriptID = current_SID;
5899# ifdef FEAT_CMDL_COMPL
5900 cmd->uc_compl_arg = compl_arg;
5901# endif
5902#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005903 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904
5905 return OK;
5906
5907fail:
5908 vim_free(rep_buf);
5909#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5910 vim_free(compl_arg);
5911#endif
5912 return FAIL;
5913}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005914#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005916#if defined(FEAT_USR_CMDS)
5917static struct
5918{
5919 int expand;
5920 char *name;
5921} addr_type_complete[] =
5922{
5923 {ADDR_ARGUMENTS, "arguments"},
5924 {ADDR_LINES, "lines"},
5925 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5926 {ADDR_TABS, "tabs"},
5927 {ADDR_BUFFERS, "buffers"},
5928 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005929 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005930 {-1, NULL}
5931};
5932#endif
5933
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005934#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935/*
5936 * List of names for completion for ":command" with the EXPAND_ flag.
5937 * Must be alphabetical for completion.
5938 */
5939static struct
5940{
5941 int expand;
5942 char *name;
5943} command_complete[] =
5944{
5945 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005946 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005948 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005950 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005951#if defined(FEAT_CSCOPE)
5952 {EXPAND_CSCOPE, "cscope"},
5953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5955 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005956 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957#endif
5958 {EXPAND_DIRECTORIES, "dir"},
5959 {EXPAND_ENV_VARS, "environment"},
5960 {EXPAND_EVENTS, "event"},
5961 {EXPAND_EXPRESSION, "expression"},
5962 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005963 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005964 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 {EXPAND_FUNCTIONS, "function"},
5966 {EXPAND_HELP, "help"},
5967 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005968#if defined(FEAT_CMDHIST)
5969 {EXPAND_HISTORY, "history"},
5970#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005971#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005972 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005973 {EXPAND_LOCALES, "locale"},
5974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 {EXPAND_MAPPINGS, "mapping"},
5976 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005977 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005978 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005979#if defined(FEAT_PROFILE)
5980 {EXPAND_SYNTIME, "syntime"},
5981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005983 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005984 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005985#if defined(FEAT_SIGNS)
5986 {EXPAND_SIGN, "sign"},
5987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 {EXPAND_TAGS, "tag"},
5989 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005990 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 {EXPAND_USER_VARS, "var"},
5992 {0, NULL}
5993};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005996#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005998uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999{
6000 int i, j;
6001 int found = FALSE;
6002 ucmd_T *cmd;
6003 int len;
6004 long a;
6005 garray_T *gap;
6006
6007 gap = &curbuf->b_ucmds;
6008 for (;;)
6009 {
6010 for (i = 0; i < gap->ga_len; ++i)
6011 {
6012 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006013 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014
Bram Moolenaard29459b2016-08-26 22:29:11 +02006015 /* Skip commands which don't match the requested prefix and
6016 * commands filtered out. */
6017 if (STRNCMP(name, cmd->uc_name, name_len) != 0
6018 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 continue;
6020
6021 /* Put out the title first time */
6022 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006023 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 found = TRUE;
6025 msg_putchar('\n');
6026 if (got_int)
6027 break;
6028
6029 /* Special cases */
6030 msg_putchar(a & BANG ? '!' : ' ');
6031 msg_putchar(a & REGSTR ? '"' : ' ');
6032 msg_putchar(gap != &ucmds ? 'b' : ' ');
6033 msg_putchar(' ');
6034
6035 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
6036 len = (int)STRLEN(cmd->uc_name) + 4;
6037
6038 do {
6039 msg_putchar(' ');
6040 ++len;
6041 } while (len < 16);
6042
6043 len = 0;
6044
6045 /* Arguments */
6046 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
6047 {
6048 case 0: IObuff[len++] = '0'; break;
6049 case (EXTRA): IObuff[len++] = '*'; break;
6050 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
6051 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
6052 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
6053 }
6054
6055 do {
6056 IObuff[len++] = ' ';
6057 } while (len < 5);
6058
6059 /* Range */
6060 if (a & (RANGE|COUNT))
6061 {
6062 if (a & COUNT)
6063 {
6064 /* -count=N */
6065 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6066 len += (int)STRLEN(IObuff + len);
6067 }
6068 else if (a & DFLALL)
6069 IObuff[len++] = '%';
6070 else if (cmd->uc_def >= 0)
6071 {
6072 /* -range=N */
6073 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6074 len += (int)STRLEN(IObuff + len);
6075 }
6076 else
6077 IObuff[len++] = '.';
6078 }
6079
6080 do {
6081 IObuff[len++] = ' ';
6082 } while (len < 11);
6083
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006084 /* Address Type */
6085 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6086 if (addr_type_complete[j].expand != ADDR_LINES
6087 && addr_type_complete[j].expand == cmd->uc_addr_type)
6088 {
6089 STRCPY(IObuff + len, addr_type_complete[j].name);
6090 len += (int)STRLEN(IObuff + len);
6091 break;
6092 }
6093
6094 do {
6095 IObuff[len++] = ' ';
6096 } while (len < 21);
6097
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 /* Completion */
6099 for (j = 0; command_complete[j].expand != 0; ++j)
6100 if (command_complete[j].expand == cmd->uc_compl)
6101 {
6102 STRCPY(IObuff + len, command_complete[j].name);
6103 len += (int)STRLEN(IObuff + len);
6104 break;
6105 }
6106
6107 do {
6108 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006109 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110
6111 IObuff[len] = '\0';
6112 msg_outtrans(IObuff);
6113
6114 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006115#ifdef FEAT_EVAL
6116 if (p_verbose > 0)
6117 last_set_msg(cmd->uc_scriptID);
6118#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 out_flush();
6120 ui_breakcheck();
6121 if (got_int)
6122 break;
6123 }
6124 if (gap == &ucmds || i < gap->ga_len)
6125 break;
6126 gap = &ucmds;
6127 }
6128
6129 if (!found)
6130 MSG(_("No user-defined commands found"));
6131}
6132
6133 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006134uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135{
6136 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6137 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6138 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6139 0xb9, 0x7f, 0};
6140 int i;
6141
6142 for (i = 0; fcmd[i]; ++i)
6143 IObuff[i] = fcmd[i] - 0x40;
6144 IObuff[i] = 0;
6145 return IObuff;
6146}
6147
6148 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006149uc_scan_attr(
6150 char_u *attr,
6151 size_t len,
6152 long *argt,
6153 long *def,
6154 int *flags,
6155 int *compl,
6156 char_u **compl_arg,
6157 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158{
6159 char_u *p;
6160
6161 if (len == 0)
6162 {
6163 EMSG(_("E175: No attribute specified"));
6164 return FAIL;
6165 }
6166
6167 /* First, try the simple attributes (no arguments) */
6168 if (STRNICMP(attr, "bang", len) == 0)
6169 *argt |= BANG;
6170 else if (STRNICMP(attr, "buffer", len) == 0)
6171 *flags |= UC_BUFFER;
6172 else if (STRNICMP(attr, "register", len) == 0)
6173 *argt |= REGSTR;
6174 else if (STRNICMP(attr, "bar", len) == 0)
6175 *argt |= TRLBAR;
6176 else
6177 {
6178 int i;
6179 char_u *val = NULL;
6180 size_t vallen = 0;
6181 size_t attrlen = len;
6182
6183 /* Look for the attribute name - which is the part before any '=' */
6184 for (i = 0; i < (int)len; ++i)
6185 {
6186 if (attr[i] == '=')
6187 {
6188 val = &attr[i + 1];
6189 vallen = len - i - 1;
6190 attrlen = i;
6191 break;
6192 }
6193 }
6194
6195 if (STRNICMP(attr, "nargs", attrlen) == 0)
6196 {
6197 if (vallen == 1)
6198 {
6199 if (*val == '0')
6200 /* Do nothing - this is the default */;
6201 else if (*val == '1')
6202 *argt |= (EXTRA | NOSPC | NEEDARG);
6203 else if (*val == '*')
6204 *argt |= EXTRA;
6205 else if (*val == '?')
6206 *argt |= (EXTRA | NOSPC);
6207 else if (*val == '+')
6208 *argt |= (EXTRA | NEEDARG);
6209 else
6210 goto wrong_nargs;
6211 }
6212 else
6213 {
6214wrong_nargs:
6215 EMSG(_("E176: Invalid number of arguments"));
6216 return FAIL;
6217 }
6218 }
6219 else if (STRNICMP(attr, "range", attrlen) == 0)
6220 {
6221 *argt |= RANGE;
6222 if (vallen == 1 && *val == '%')
6223 *argt |= DFLALL;
6224 else if (val != NULL)
6225 {
6226 p = val;
6227 if (*def >= 0)
6228 {
6229two_count:
6230 EMSG(_("E177: Count cannot be specified twice"));
6231 return FAIL;
6232 }
6233
6234 *def = getdigits(&p);
6235 *argt |= (ZEROR | NOTADR);
6236
6237 if (p != val + vallen || vallen == 0)
6238 {
6239invalid_count:
6240 EMSG(_("E178: Invalid default value for count"));
6241 return FAIL;
6242 }
6243 }
6244 }
6245 else if (STRNICMP(attr, "count", attrlen) == 0)
6246 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006247 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248
6249 if (val != NULL)
6250 {
6251 p = val;
6252 if (*def >= 0)
6253 goto two_count;
6254
6255 *def = getdigits(&p);
6256
6257 if (p != val + vallen)
6258 goto invalid_count;
6259 }
6260
6261 if (*def < 0)
6262 *def = 0;
6263 }
6264 else if (STRNICMP(attr, "complete", attrlen) == 0)
6265 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 if (val == NULL)
6267 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006268 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 return FAIL;
6270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006272 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6273 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006276 else if (STRNICMP(attr, "addr", attrlen) == 0)
6277 {
6278 *argt |= RANGE;
6279 if (val == NULL)
6280 {
6281 EMSG(_("E179: argument required for -addr"));
6282 return FAIL;
6283 }
6284 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6285 == FAIL)
6286 return FAIL;
6287 if (addr_type_arg != ADDR_LINES)
6288 *argt |= (ZEROR | NOTADR) ;
6289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 else
6291 {
6292 char_u ch = attr[len];
6293 attr[len] = '\0';
6294 EMSG2(_("E181: Invalid attribute: %s"), attr);
6295 attr[len] = ch;
6296 return FAIL;
6297 }
6298 }
6299
6300 return OK;
6301}
6302
Bram Moolenaara850a712009-01-28 14:42:59 +00006303/*
6304 * ":command ..."
6305 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006307ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308{
6309 char_u *name;
6310 char_u *end;
6311 char_u *p;
6312 long argt = 0;
6313 long def = -1;
6314 int flags = 0;
6315 int compl = EXPAND_NOTHING;
6316 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006317 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006319 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320
6321 p = eap->arg;
6322
6323 /* Check for attributes */
6324 while (*p == '-')
6325 {
6326 ++p;
6327 end = skiptowhite(p);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006328 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 == FAIL)
6330 return;
6331 p = skipwhite(end);
6332 }
6333
6334 /* Get the name (if any) and skip to the following argument */
6335 name = p;
6336 if (ASCII_ISALPHA(*p))
6337 while (ASCII_ISALNUM(*p))
6338 ++p;
6339 if (!ends_excmd(*p) && !vim_iswhite(*p))
6340 {
6341 EMSG(_("E182: Invalid command name"));
6342 return;
6343 }
6344 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006345 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346
6347 /* If there is nothing after the name, and no attributes were specified,
6348 * we are listing commands
6349 */
6350 p = skipwhite(end);
6351 if (!has_attr && ends_excmd(*p))
6352 {
6353 uc_list(name, end - name);
6354 }
6355 else if (!ASCII_ISUPPER(*name))
6356 {
6357 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6358 return;
6359 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006360 else if ((name_len == 1 && *name == 'X')
6361 || (name_len <= 4
6362 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6363 {
6364 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6365 return;
6366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 else
6368 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006369 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370}
6371
6372/*
6373 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 * Clear all user commands, global and for current buffer.
6375 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006376 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006377ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378{
6379 uc_clear(&ucmds);
6380 uc_clear(&curbuf->b_ucmds);
6381}
6382
6383/*
6384 * Clear all user commands for "gap".
6385 */
6386 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006387uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388{
6389 int i;
6390 ucmd_T *cmd;
6391
6392 for (i = 0; i < gap->ga_len; ++i)
6393 {
6394 cmd = USER_CMD_GA(gap, i);
6395 vim_free(cmd->uc_name);
6396 vim_free(cmd->uc_rep);
6397# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6398 vim_free(cmd->uc_compl_arg);
6399# endif
6400 }
6401 ga_clear(gap);
6402}
6403
6404 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006405ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406{
6407 int i = 0;
6408 ucmd_T *cmd = NULL;
6409 int cmp = -1;
6410 garray_T *gap;
6411
6412 gap = &curbuf->b_ucmds;
6413 for (;;)
6414 {
6415 for (i = 0; i < gap->ga_len; ++i)
6416 {
6417 cmd = USER_CMD_GA(gap, i);
6418 cmp = STRCMP(eap->arg, cmd->uc_name);
6419 if (cmp <= 0)
6420 break;
6421 }
6422 if (gap == &ucmds || cmp == 0)
6423 break;
6424 gap = &ucmds;
6425 }
6426
6427 if (cmp != 0)
6428 {
6429 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6430 return;
6431 }
6432
6433 vim_free(cmd->uc_name);
6434 vim_free(cmd->uc_rep);
6435# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6436 vim_free(cmd->uc_compl_arg);
6437# endif
6438
6439 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440
6441 if (i < gap->ga_len)
6442 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6443}
6444
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006445/*
6446 * split and quote args for <f-args>
6447 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006449uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450{
6451 char_u *buf;
6452 char_u *p;
6453 char_u *q;
6454 int len;
6455
6456 /* Precalculate length */
6457 p = arg;
6458 len = 2; /* Initial and final quotes */
6459
6460 while (*p)
6461 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006462 if (p[0] == '\\' && p[1] == '\\')
6463 {
6464 len += 2;
6465 p += 2;
6466 }
6467 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 {
6469 len += 1;
6470 p += 2;
6471 }
6472 else if (*p == '\\' || *p == '"')
6473 {
6474 len += 2;
6475 p += 1;
6476 }
6477 else if (vim_iswhite(*p))
6478 {
6479 p = skipwhite(p);
6480 if (*p == NUL)
6481 break;
6482 len += 3; /* "," */
6483 }
6484 else
6485 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006486#ifdef FEAT_MBYTE
6487 int charlen = (*mb_ptr2len)(p);
6488 len += charlen;
6489 p += charlen;
6490#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 ++len;
6492 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 }
6495 }
6496
6497 buf = alloc(len + 1);
6498 if (buf == NULL)
6499 {
6500 *lenp = 0;
6501 return buf;
6502 }
6503
6504 p = arg;
6505 q = buf;
6506 *q++ = '"';
6507 while (*p)
6508 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006509 if (p[0] == '\\' && p[1] == '\\')
6510 {
6511 *q++ = '\\';
6512 *q++ = '\\';
6513 p += 2;
6514 }
6515 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 {
6517 *q++ = p[1];
6518 p += 2;
6519 }
6520 else if (*p == '\\' || *p == '"')
6521 {
6522 *q++ = '\\';
6523 *q++ = *p++;
6524 }
6525 else if (vim_iswhite(*p))
6526 {
6527 p = skipwhite(p);
6528 if (*p == NUL)
6529 break;
6530 *q++ = '"';
6531 *q++ = ',';
6532 *q++ = '"';
6533 }
6534 else
6535 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006536 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 }
6538 }
6539 *q++ = '"';
6540 *q = 0;
6541
6542 *lenp = len;
6543 return buf;
6544}
6545
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006546 static size_t
6547add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6548{
6549 size_t result;
6550
6551 result = STRLEN(mod_str);
6552 if (*multi_mods)
6553 result += 1;
6554 if (buf != NULL)
6555 {
6556 if (*multi_mods)
6557 STRCAT(buf, " ");
6558 STRCAT(buf, mod_str);
6559 }
6560
6561 *multi_mods = 1;
6562
6563 return result;
6564}
6565
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566/*
6567 * Check for a <> code in a user command.
6568 * "code" points to the '<'. "len" the length of the <> (inclusive).
6569 * "buf" is where the result is to be added.
6570 * "split_buf" points to a buffer used for splitting, caller should free it.
6571 * "split_len" is the length of what "split_buf" contains.
6572 * Returns the length of the replacement, which has been added to "buf".
6573 * Returns -1 if there was no match, and only the "<" has been copied.
6574 */
6575 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006576uc_check_code(
6577 char_u *code,
6578 size_t len,
6579 char_u *buf,
6580 ucmd_T *cmd, /* the user command we're expanding */
6581 exarg_T *eap, /* ex arguments */
6582 char_u **split_buf,
6583 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584{
6585 size_t result = 0;
6586 char_u *p = code + 1;
6587 size_t l = len - 2;
6588 int quote = 0;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006589 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_MODS,
6590 ct_REGISTER, ct_LT, ct_NONE } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591
6592 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6593 {
6594 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6595 p += 2;
6596 l -= 2;
6597 }
6598
Bram Moolenaar371d5402006-03-20 21:47:49 +00006599 ++l;
6600 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006602 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006604 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006606 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006608 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006610 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006612 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006614 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006616 else if (STRNICMP(p, "mods>", l) == 0)
6617 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618
6619 switch (type)
6620 {
6621 case ct_ARGS:
6622 /* Simple case first */
6623 if (*eap->arg == NUL)
6624 {
6625 if (quote == 1)
6626 {
6627 result = 2;
6628 if (buf != NULL)
6629 STRCPY(buf, "''");
6630 }
6631 else
6632 result = 0;
6633 break;
6634 }
6635
6636 /* When specified there is a single argument don't split it.
6637 * Works for ":Cmd %" when % is "a b c". */
6638 if ((eap->argt & NOSPC) && quote == 2)
6639 quote = 1;
6640
6641 switch (quote)
6642 {
6643 case 0: /* No quoting, no splitting */
6644 result = STRLEN(eap->arg);
6645 if (buf != NULL)
6646 STRCPY(buf, eap->arg);
6647 break;
6648 case 1: /* Quote, but don't split */
6649 result = STRLEN(eap->arg) + 2;
6650 for (p = eap->arg; *p; ++p)
6651 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006652#ifdef FEAT_MBYTE
6653 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6654 /* DBCS can contain \ in a trail byte, skip the
6655 * double-byte character. */
6656 ++p;
6657 else
6658#endif
6659 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 ++result;
6661 }
6662
6663 if (buf != NULL)
6664 {
6665 *buf++ = '"';
6666 for (p = eap->arg; *p; ++p)
6667 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006668#ifdef FEAT_MBYTE
6669 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6670 /* DBCS can contain \ in a trail byte, copy the
6671 * double-byte character to avoid escaping. */
6672 *buf++ = *p++;
6673 else
6674#endif
6675 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 *buf++ = '\\';
6677 *buf++ = *p;
6678 }
6679 *buf = '"';
6680 }
6681
6682 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006683 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 /* This is hard, so only do it once, and cache the result */
6685 if (*split_buf == NULL)
6686 *split_buf = uc_split_args(eap->arg, split_len);
6687
6688 result = *split_len;
6689 if (buf != NULL && result != 0)
6690 STRCPY(buf, *split_buf);
6691
6692 break;
6693 }
6694 break;
6695
6696 case ct_BANG:
6697 result = eap->forceit ? 1 : 0;
6698 if (quote)
6699 result += 2;
6700 if (buf != NULL)
6701 {
6702 if (quote)
6703 *buf++ = '"';
6704 if (eap->forceit)
6705 *buf++ = '!';
6706 if (quote)
6707 *buf = '"';
6708 }
6709 break;
6710
6711 case ct_LINE1:
6712 case ct_LINE2:
6713 case ct_COUNT:
6714 {
6715 char num_buf[20];
6716 long num = (type == ct_LINE1) ? eap->line1 :
6717 (type == ct_LINE2) ? eap->line2 :
6718 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6719 size_t num_len;
6720
6721 sprintf(num_buf, "%ld", num);
6722 num_len = STRLEN(num_buf);
6723 result = num_len;
6724
6725 if (quote)
6726 result += 2;
6727
6728 if (buf != NULL)
6729 {
6730 if (quote)
6731 *buf++ = '"';
6732 STRCPY(buf, num_buf);
6733 buf += num_len;
6734 if (quote)
6735 *buf = '"';
6736 }
6737
6738 break;
6739 }
6740
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006741 case ct_MODS:
6742 {
6743 int multi_mods = 0;
6744 typedef struct {
6745 int *varp;
6746 char *name;
6747 } mod_entry_T;
6748 static mod_entry_T mod_entries[] = {
6749#ifdef FEAT_BROWSE_CMD
6750 {&cmdmod.browse, "browse"},
6751#endif
6752#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6753 {&cmdmod.confirm, "confirm"},
6754#endif
6755 {&cmdmod.hide, "hide"},
6756 {&cmdmod.keepalt, "keepalt"},
6757 {&cmdmod.keepjumps, "keepjumps"},
6758 {&cmdmod.keepmarks, "keepmarks"},
6759 {&cmdmod.keeppatterns, "keeppatterns"},
6760 {&cmdmod.lockmarks, "lockmarks"},
6761 {&cmdmod.noswapfile, "noswapfile"},
6762 {NULL, NULL}
6763 };
6764 int i;
6765
6766 result = quote ? 2 : 0;
6767 if (buf != NULL)
6768 {
6769 if (quote)
6770 *buf++ = '"';
6771 *buf = '\0';
6772 }
6773
6774#ifdef FEAT_WINDOWS
6775 /* :aboveleft and :leftabove */
6776 if (cmdmod.split & WSP_ABOVE)
6777 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6778 /* :belowright and :rightbelow */
6779 if (cmdmod.split & WSP_BELOW)
6780 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6781 /* :botright */
6782 if (cmdmod.split & WSP_BOT)
6783 result += add_cmd_modifier(buf, "botright", &multi_mods);
6784#endif
6785
6786 /* the modifiers that are simple flags */
6787 for (i = 0; mod_entries[i].varp != NULL; ++i)
6788 if (*mod_entries[i].varp)
6789 result += add_cmd_modifier(buf, mod_entries[i].name,
6790 &multi_mods);
6791
6792 /* TODO: How to support :noautocmd? */
6793#ifdef HAVE_SANDBOX
6794 /* TODO: How to support :sandbox?*/
6795#endif
6796 /* :silent */
6797 if (msg_silent > 0)
6798 result += add_cmd_modifier(buf,
6799 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
6800#ifdef FEAT_WINDOWS
6801 /* :tab */
6802 if (cmdmod.tab > 0)
6803 result += add_cmd_modifier(buf, "tab", &multi_mods);
6804 /* :topleft */
6805 if (cmdmod.split & WSP_TOP)
6806 result += add_cmd_modifier(buf, "topleft", &multi_mods);
6807#endif
6808 /* TODO: How to support :unsilent?*/
6809 /* :verbose */
6810 if (p_verbose > 0)
6811 result += add_cmd_modifier(buf, "verbose", &multi_mods);
6812#ifdef FEAT_WINDOWS
6813 /* :vertical */
6814 if (cmdmod.split & WSP_VERT)
6815 result += add_cmd_modifier(buf, "vertical", &multi_mods);
6816#endif
6817 if (quote && buf != NULL)
6818 {
6819 buf += result - 2;
6820 *buf = '"';
6821 }
6822 break;
6823 }
6824
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 case ct_REGISTER:
6826 result = eap->regname ? 1 : 0;
6827 if (quote)
6828 result += 2;
6829 if (buf != NULL)
6830 {
6831 if (quote)
6832 *buf++ = '\'';
6833 if (eap->regname)
6834 *buf++ = eap->regname;
6835 if (quote)
6836 *buf = '\'';
6837 }
6838 break;
6839
6840 case ct_LT:
6841 result = 1;
6842 if (buf != NULL)
6843 *buf = '<';
6844 break;
6845
6846 default:
6847 /* Not recognized: just copy the '<' and return -1. */
6848 result = (size_t)-1;
6849 if (buf != NULL)
6850 *buf = '<';
6851 break;
6852 }
6853
6854 return result;
6855}
6856
6857 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006858do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859{
6860 char_u *buf;
6861 char_u *p;
6862 char_u *q;
6863
6864 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006865 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006866 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 size_t len, totlen;
6868
6869 size_t split_len = 0;
6870 char_u *split_buf = NULL;
6871 ucmd_T *cmd;
6872#ifdef FEAT_EVAL
6873 scid_T save_current_SID = current_SID;
6874#endif
6875
6876 if (eap->cmdidx == CMD_USER)
6877 cmd = USER_CMD(eap->useridx);
6878 else
6879 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6880
6881 /*
6882 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006883 * First round: "buf" is NULL, compute length, allocate "buf".
6884 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 */
6886 buf = NULL;
6887 for (;;)
6888 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006889 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006890 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006892
6893 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006895 start = vim_strchr(p, '<');
6896 if (start != NULL)
6897 end = vim_strchr(start + 1, '>');
6898 if (buf != NULL)
6899 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006900 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6901 ;
6902 if (*ksp == K_SPECIAL
6903 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006904 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6905# ifdef FEAT_GUI
6906 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6907# endif
6908 ))
6909 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006910 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006911 * KS_SPECIAL KE_FILLER, like for mappings, but
6912 * do_cmdline() doesn't handle that, so convert it back.
6913 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6914 len = ksp - p;
6915 if (len > 0)
6916 {
6917 mch_memmove(q, p, len);
6918 q += len;
6919 }
6920 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6921 p = ksp + 3;
6922 continue;
6923 }
6924 }
6925
6926 /* break if there no <item> is found */
6927 if (start == NULL || end == NULL)
6928 break;
6929
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 /* Include the '>' */
6931 ++end;
6932
6933 /* Take everything up to the '<' */
6934 len = start - p;
6935 if (buf == NULL)
6936 totlen += len;
6937 else
6938 {
6939 mch_memmove(q, p, len);
6940 q += len;
6941 }
6942
6943 len = uc_check_code(start, end - start, q, cmd, eap,
6944 &split_buf, &split_len);
6945 if (len == (size_t)-1)
6946 {
6947 /* no match, continue after '<' */
6948 p = start + 1;
6949 len = 1;
6950 }
6951 else
6952 p = end;
6953 if (buf == NULL)
6954 totlen += len;
6955 else
6956 q += len;
6957 }
6958 if (buf != NULL) /* second time here, finished */
6959 {
6960 STRCPY(q, p);
6961 break;
6962 }
6963
6964 totlen += STRLEN(p); /* Add on the trailing characters */
6965 buf = alloc((unsigned)(totlen + 1));
6966 if (buf == NULL)
6967 {
6968 vim_free(split_buf);
6969 return;
6970 }
6971 }
6972
6973#ifdef FEAT_EVAL
6974 current_SID = cmd->uc_scriptID;
6975#endif
6976 (void)do_cmdline(buf, eap->getline, eap->cookie,
6977 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6978#ifdef FEAT_EVAL
6979 current_SID = save_current_SID;
6980#endif
6981 vim_free(buf);
6982 vim_free(split_buf);
6983}
6984
6985# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6986 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006987get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988{
6989 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6990}
6991
6992/*
6993 * Function given to ExpandGeneric() to obtain the list of user command names.
6994 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006996get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997{
6998 if (idx < curbuf->b_ucmds.ga_len)
6999 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
7000 idx -= curbuf->b_ucmds.ga_len;
7001 if (idx < ucmds.ga_len)
7002 return USER_CMD(idx)->uc_name;
7003 return NULL;
7004}
7005
7006/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007007 * Function given to ExpandGeneric() to obtain the list of user address type names.
7008 */
7009 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007010get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007011{
7012 return (char_u *)addr_type_complete[idx].name;
7013}
7014
7015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 * Function given to ExpandGeneric() to obtain the list of user command
7017 * attributes.
7018 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007020get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021{
7022 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007023 {"addr", "bang", "bar", "buffer", "complete",
7024 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007026 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 return NULL;
7028 return (char_u *)user_cmd_flags[idx];
7029}
7030
7031/*
7032 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
7033 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007035get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036{
7037 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
7038
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007039 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 return NULL;
7041 return (char_u *)user_cmd_nargs[idx];
7042}
7043
7044/*
7045 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007048get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049{
7050 return (char_u *)command_complete[idx].name;
7051}
7052# endif /* FEAT_CMDL_COMPL */
7053
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007054/*
7055 * Parse address type argument
7056 */
7057 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007058parse_addr_type_arg(
7059 char_u *value,
7060 int vallen,
7061 long *argt,
7062 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007063{
7064 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007065
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007066 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7067 {
7068 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7069 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7070 if (a && b)
7071 {
7072 *addr_type_arg = addr_type_complete[i].expand;
7073 break;
7074 }
7075 }
7076
7077 if (addr_type_complete[i].expand == -1)
7078 {
7079 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007080
7081 for (i = 0; err[i] != NUL && !vim_iswhite(err[i]); i++)
7082 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007083 err[i] = NUL;
7084 EMSG2(_("E180: Invalid address type value: %s"), err);
7085 return FAIL;
7086 }
7087
7088 if (*addr_type_arg != ADDR_LINES)
7089 *argt |= NOTADR;
7090
7091 return OK;
7092}
7093
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094#endif /* FEAT_USR_CMDS */
7095
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007096#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7097/*
7098 * Parse a completion argument "value[vallen]".
7099 * The detected completion goes in "*complp", argument type in "*argt".
7100 * When there is an argument, for function and user defined completion, it's
7101 * copied to allocated memory and stored in "*compl_arg".
7102 * Returns FAIL if something is wrong.
7103 */
7104 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007105parse_compl_arg(
7106 char_u *value,
7107 int vallen,
7108 int *complp,
7109 long *argt,
7110 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007111{
7112 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007113# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007114 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007115# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007116 int i;
7117 int valend = vallen;
7118
7119 /* Look for any argument part - which is the part after any ',' */
7120 for (i = 0; i < vallen; ++i)
7121 {
7122 if (value[i] == ',')
7123 {
7124 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007125# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007126 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007127# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007128 valend = i;
7129 break;
7130 }
7131 }
7132
7133 for (i = 0; command_complete[i].expand != 0; ++i)
7134 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007135 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007136 && STRNCMP(value, command_complete[i].name, valend) == 0)
7137 {
7138 *complp = command_complete[i].expand;
7139 if (command_complete[i].expand == EXPAND_BUFFERS)
7140 *argt |= BUFNAME;
7141 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7142 || command_complete[i].expand == EXPAND_FILES)
7143 *argt |= XFILE;
7144 break;
7145 }
7146 }
7147
7148 if (command_complete[i].expand == 0)
7149 {
7150 EMSG2(_("E180: Invalid complete value: %s"), value);
7151 return FAIL;
7152 }
7153
7154# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7155 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7156 && arg != NULL)
7157# else
7158 if (arg != NULL)
7159# endif
7160 {
7161 EMSG(_("E468: Completion argument only allowed for custom completion"));
7162 return FAIL;
7163 }
7164
7165# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7166 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7167 && arg == NULL)
7168 {
7169 EMSG(_("E467: Custom completion requires a function argument"));
7170 return FAIL;
7171 }
7172
7173 if (arg != NULL)
7174 *compl_arg = vim_strnsave(arg, (int)arglen);
7175# endif
7176 return OK;
7177}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007178
7179 int
7180cmdcomplete_str_to_type(char_u *complete_str)
7181{
7182 int i;
7183
7184 for (i = 0; command_complete[i].expand != 0; ++i)
7185 if (STRCMP(complete_str, command_complete[i].name) == 0)
7186 return command_complete[i].expand;
7187
7188 return EXPAND_NOTHING;
7189}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007190#endif
7191
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007193ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194{
Bram Moolenaare6850792010-05-14 15:28:44 +02007195 if (*eap->arg == NUL)
7196 {
7197#ifdef FEAT_EVAL
7198 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7199 char_u *p = NULL;
7200
7201 if (expr != NULL)
7202 {
7203 ++emsg_off;
7204 p = eval_to_string(expr, NULL, FALSE);
7205 --emsg_off;
7206 vim_free(expr);
7207 }
7208 if (p != NULL)
7209 {
7210 MSG(p);
7211 vim_free(p);
7212 }
7213 else
7214 MSG("default");
7215#else
7216 MSG(_("unknown"));
7217#endif
7218 }
7219 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007220 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221}
7222
7223 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007224ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225{
7226 if (*eap->arg == NUL && eap->cmd[2] == '!')
7227 MSG(_("Greetings, Vim user!"));
7228 do_highlight(eap->arg, eap->forceit, FALSE);
7229}
7230
7231
7232/*
7233 * Call this function if we thought we were going to exit, but we won't
7234 * (because of an error). May need to restore the terminal mode.
7235 */
7236 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007237not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238{
7239 exiting = FALSE;
7240 settmode(TMODE_RAW);
7241}
7242
7243/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007244 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 */
7246 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007247ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248{
Bram Moolenaarf240e182014-11-27 18:33:02 +01007249#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007250 win_T *wp;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007251#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007252
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253#ifdef FEAT_CMDWIN
7254 if (cmdwin_type != 0)
7255 {
7256 cmdwin_result = Ctrl_C;
7257 return;
7258 }
7259#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007260 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007261 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007262 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007263 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007264 return;
7265 }
Bram Moolenaarf240e182014-11-27 18:33:02 +01007266#ifdef FEAT_WINDOWS
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007267 if (eap->addr_count > 0)
7268 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007269 int wnr = eap->line2;
7270
7271 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7272 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007273 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007274 }
7275 else
Bram Moolenaarf240e182014-11-27 18:33:02 +01007276#endif
7277#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007278 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007279#endif
7280
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007281#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02007282 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007283 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02007284 * being closed (can only happen in autocommands). */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007285 if (curbuf_locked() || (wp->w_buffer->b_nwindows == 1
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007286 && wp->w_buffer->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007287 return;
7288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289
7290#ifdef FEAT_NETBEANS_INTG
7291 netbeansForcedQuit = eap->forceit;
7292#endif
7293
7294 /*
7295 * If there are more files or windows we won't exit.
7296 */
7297 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7298 exiting = TRUE;
7299 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007300 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7301 | (eap->forceit ? CCGD_FORCEIT : 0)
7302 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007304 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 {
7306 not_exiting();
7307 }
7308 else
7309 {
7310#ifdef FEAT_WINDOWS
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007311 /* quit last window
7312 * Note: only_one_window() returns true, even so a help window is
7313 * still open. In that case only quit, if no address has been
7314 * specified. Example:
7315 * :h|wincmd w|1q - don't quit
7316 * :h|wincmd w|q - quit
7317 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007318 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319#endif
7320 getout(0);
7321#ifdef FEAT_WINDOWS
7322# ifdef FEAT_GUI
7323 need_mouse_correct = TRUE;
7324# endif
7325 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007326 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327#endif
7328 }
7329}
7330
7331/*
7332 * ":cquit".
7333 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007335ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336{
7337 getout(1); /* this does not always pass on the exit code to the Manx
7338 compiler. why? */
7339}
7340
7341/*
7342 * ":qall": try to quit all windows
7343 */
7344 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007345ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346{
7347# ifdef FEAT_CMDWIN
7348 if (cmdwin_type != 0)
7349 {
7350 if (eap->forceit)
7351 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7352 else
7353 cmdwin_result = K_XF2;
7354 return;
7355 }
7356# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007357
7358 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007359 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007360 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007361 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007362 return;
7363 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007364#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007365 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7366 /* Refuse to quit when locked or when the buffer in the last window is
7367 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007368 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007369 return;
7370#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007371
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007373 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 getout(0);
7375 not_exiting();
7376}
7377
Bram Moolenaard9967712006-03-11 21:18:15 +00007378#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379/*
7380 * ":close": close current window, unless it is the last one
7381 */
7382 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007383ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007385 win_T *win;
7386 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387# ifdef FEAT_CMDWIN
7388 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007389 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 else
7391# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007392 if (!text_locked()
7393#ifdef FEAT_AUTOCMD
7394 && !curbuf_locked()
7395#endif
7396 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007397 {
7398 if (eap->addr_count == 0)
7399 ex_win_close(eap->forceit, curwin, NULL);
7400 else {
Bram Moolenaar29323592016-07-24 22:04:11 +02007401 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007402 {
7403 winnr++;
7404 if (winnr == eap->line2)
7405 break;
7406 }
7407 if (win == NULL)
7408 win = lastwin;
7409 ex_win_close(eap->forceit, win, NULL);
7410 }
7411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412}
7413
Bram Moolenaard9967712006-03-11 21:18:15 +00007414# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007415/*
7416 * ":pclose": Close any preview window.
7417 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007419ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007420{
7421 win_T *win;
7422
Bram Moolenaar29323592016-07-24 22:04:11 +02007423 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007424 if (win->w_p_pvw)
7425 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007426 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007427 break;
7428 }
7429}
Bram Moolenaard9967712006-03-11 21:18:15 +00007430# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007431
Bram Moolenaarf740b292006-02-16 22:11:02 +00007432/*
7433 * Close window "win" and take care of handling closing the last window for a
7434 * modified buffer.
7435 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007436 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007437ex_win_close(
7438 int forceit,
7439 win_T *win,
7440 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441{
7442 int need_hide;
7443 buf_T *buf = win->w_buffer;
7444
7445 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007446 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007448# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 if ((p_confirm || cmdmod.confirm) && p_write)
7450 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007451 bufref_T bufref;
7452
7453 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007455 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 return;
7457 need_hide = FALSE;
7458 }
7459 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007460# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 {
7462 EMSG(_(e_nowrtmsg));
7463 return;
7464 }
7465 }
7466
Bram Moolenaard9967712006-03-11 21:18:15 +00007467# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007469# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007470
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007472 if (tp == NULL)
7473 win_close(win, !need_hide && !P_HID(buf));
7474 else
7475 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476}
7477
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007479 * Handle the argument for a tabpage related ex command.
7480 * Returns a tabpage number.
7481 * When an error is encountered then eap->errmsg is set.
7482 */
7483 static int
7484get_tabpage_arg(exarg_T *eap)
7485{
7486 int tab_number;
7487 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7488
7489 if (eap->arg && *eap->arg != NUL)
7490 {
7491 char_u *p = eap->arg;
7492 char_u *p_save;
7493 int relative = 0; /* argument +N/-N means: go to N places to the
7494 * right/left relative to the current position. */
7495
7496 if (*p == '-')
7497 {
7498 relative = -1;
7499 p++;
7500 }
7501 else if (*p == '+')
7502 {
7503 relative = 1;
7504 p++;
7505 }
7506
7507 p_save = p;
7508 tab_number = getdigits(&p);
7509
7510 if (relative == 0)
7511 {
7512 if (STRCMP(p, "$") == 0)
7513 tab_number = LAST_TAB_NR;
7514 else if (p == p_save || *p_save == '-' || *p != NUL
7515 || tab_number > LAST_TAB_NR)
7516 {
7517 /* No numbers as argument. */
7518 eap->errmsg = e_invarg;
7519 goto theend;
7520 }
7521 }
7522 else
7523 {
7524 if (*p_save == NUL)
7525 tab_number = 1;
7526 else if (p == p_save || *p_save == '-' || *p != NUL
7527 || tab_number == 0)
7528 {
7529 /* No numbers as argument. */
7530 eap->errmsg = e_invarg;
7531 goto theend;
7532 }
7533 tab_number = tab_number * relative + tabpage_index(curtab);
7534 if (!unaccept_arg0 && relative == -1)
7535 --tab_number;
7536 }
7537 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7538 eap->errmsg = e_invarg;
7539 }
7540 else if (eap->addr_count > 0)
7541 {
7542 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007543 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007544 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007545 tab_number = 0;
7546 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007547 else
7548 {
7549 tab_number = eap->line2;
7550 if (!unaccept_arg0 && **eap->cmdlinep == '-')
7551 {
7552 --tab_number;
7553 if (tab_number < unaccept_arg0)
7554 eap->errmsg = e_invarg;
7555 }
7556 }
7557 }
7558 else
7559 {
7560 switch (eap->cmdidx)
7561 {
7562 case CMD_tabnext:
7563 tab_number = tabpage_index(curtab) + 1;
7564 if (tab_number > LAST_TAB_NR)
7565 tab_number = 1;
7566 break;
7567 case CMD_tabmove:
7568 tab_number = LAST_TAB_NR;
7569 break;
7570 default:
7571 tab_number = tabpage_index(curtab);
7572 }
7573 }
7574
7575theend:
7576 return tab_number;
7577}
7578
7579/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007580 * ":tabclose": close current tab page, unless it is the last one.
7581 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 */
7583 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007584ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007586 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007587 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007588
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007589# ifdef FEAT_CMDWIN
7590 if (cmdwin_type != 0)
7591 cmdwin_result = K_IGNORE;
7592 else
7593# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007594 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007595 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007596 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007598 tab_number = get_tabpage_arg(eap);
7599 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007600 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007601 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007602 if (tp == NULL)
7603 {
7604 beep_flush();
7605 return;
7606 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007607 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007608 {
7609 tabpage_close_other(tp, eap->forceit);
7610 return;
7611 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007612 else if (!text_locked()
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007613#ifdef FEAT_AUTOCMD
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007614 && !curbuf_locked()
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007615#endif
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007616 )
7617 tabpage_close(eap->forceit);
7618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007620}
7621
7622/*
7623 * ":tabonly": close all tab pages except the current one
7624 */
7625 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007626ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007627{
7628 tabpage_T *tp;
7629 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007630 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007631
7632# ifdef FEAT_CMDWIN
7633 if (cmdwin_type != 0)
7634 cmdwin_result = K_IGNORE;
7635 else
7636# endif
7637 if (first_tabpage->tp_next == NULL)
7638 MSG(_("Already only one tab page"));
7639 else
7640 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007641 tab_number = get_tabpage_arg(eap);
7642 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007643 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007644 goto_tabpage(tab_number);
7645 /* Repeat this up to a 1000 times, because autocommands may
7646 * mess up the lists. */
7647 for (done = 0; done < 1000; ++done)
7648 {
7649 FOR_ALL_TABPAGES(tp)
7650 if (tp->tp_topframe != topframe)
7651 {
7652 tabpage_close_other(tp, eap->forceit);
7653 /* if we failed to close it quit */
7654 if (valid_tabpage(tp))
7655 done = 1000;
7656 /* start over, "tp" is now invalid */
7657 break;
7658 }
7659 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007660 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007661 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007662 }
7663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665
7666/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007667 * Close the current tab page.
7668 */
7669 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007670tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007671{
7672 /* First close all the windows but the current one. If that worked then
7673 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007674 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007675 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007676 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007677 ex_win_close(forceit, curwin, NULL);
7678# ifdef FEAT_GUI
7679 need_mouse_correct = TRUE;
7680# endif
7681}
7682
7683/*
7684 * Close tab page "tp", which is not the current tab page.
7685 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007686 * Also takes care of the tab pages line disappearing when closing the
7687 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007688 */
7689 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007690tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007691{
7692 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007693 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007694 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007695
7696 /* Limit to 1000 windows, autocommands may add a window while we close
7697 * one. OK, so I'm paranoid... */
7698 while (++done < 1000)
7699 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007700 wp = tp->tp_firstwin;
7701 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007702
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007703 /* Autocommands may delete the tab page under our fingers and we may
7704 * fail to close a window with a modified buffer. */
7705 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007706 break;
7707 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007708
Bram Moolenaar29323592016-07-24 22:04:11 +02007709#ifdef FEAT_AUTOCMD
7710 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
7711#endif
7712
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007713 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007714 if (h != tabline_height())
7715 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007716}
7717
7718/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 * ":only".
7720 */
7721 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007722ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007724 win_T *wp;
7725 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726# ifdef FEAT_GUI
7727 need_mouse_correct = TRUE;
7728# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007729 if (eap->addr_count > 0)
7730 {
7731 wnr = eap->line2;
7732 for (wp = firstwin; --wnr > 0; )
7733 {
7734 if (wp->w_next == NULL)
7735 break;
7736 else
7737 wp = wp->w_next;
7738 }
7739 win_goto(wp);
7740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 close_others(TRUE, eap->forceit);
7742}
7743
7744/*
7745 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007746 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007748 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007749ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750{
7751 if (eap->addr_count == 0)
7752 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007753 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754}
7755#endif /* FEAT_WINDOWS */
7756
7757 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007758ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007760 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761#ifdef FEAT_WINDOWS
Bram Moolenaar2256c992016-11-15 21:17:07 +01007762 if (!eap->skip)
7763 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764# ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007765 need_mouse_correct = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766# endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007767 if (eap->addr_count == 0)
7768 win_close(curwin, FALSE); /* don't free buffer */
7769 else
7770 {
7771 int winnr = 0;
7772 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007773
Bram Moolenaar2256c992016-11-15 21:17:07 +01007774 FOR_ALL_WINDOWS(win)
7775 {
7776 winnr++;
7777 if (winnr == eap->line2)
7778 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007779 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007780 if (win == NULL)
7781 win = lastwin;
7782 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786}
7787
7788/*
7789 * ":stop" and ":suspend": Suspend Vim.
7790 */
7791 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007792ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793{
7794 /*
7795 * Disallow suspending for "rvim".
7796 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007797 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 {
7799 if (!eap->forceit)
7800 autowrite_all();
7801 windgoto((int)Rows - 1, 0);
7802 out_char('\n');
7803 out_flush();
7804 stoptermcap();
7805 out_flush(); /* needed for SUN to restore xterm buffer */
7806#ifdef FEAT_TITLE
7807 mch_restore_title(3); /* restore window titles */
7808#endif
7809 ui_suspend(); /* call machine specific function */
7810#ifdef FEAT_TITLE
7811 maketitle();
7812 resettitle(); /* force updating the title */
7813#endif
7814 starttermcap();
7815 scroll_start(); /* scroll screen before redrawing */
7816 redraw_later_clear();
7817 shell_resized(); /* may have resized window */
7818 }
7819}
7820
7821/*
7822 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7823 */
7824 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007825ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826{
7827#ifdef FEAT_CMDWIN
7828 if (cmdwin_type != 0)
7829 {
7830 cmdwin_result = Ctrl_C;
7831 return;
7832 }
7833#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007834 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007835 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007836 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007837 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007838 return;
7839 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007840#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007841 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7842 /* Refuse to quit when locked or when the buffer in the last window is
7843 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007844 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007845 return;
7846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847
7848 /*
7849 * if more files or windows we won't exit
7850 */
7851 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7852 exiting = TRUE;
7853 if ( ((eap->cmdidx == CMD_wq
7854 || curbufIsChanged())
7855 && do_write(eap) == FAIL)
7856 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007857 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 {
7859 not_exiting();
7860 }
7861 else
7862 {
7863#ifdef FEAT_WINDOWS
7864 if (only_one_window()) /* quit last window, exit Vim */
7865#endif
7866 getout(0);
7867#ifdef FEAT_WINDOWS
7868# ifdef FEAT_GUI
7869 need_mouse_correct = TRUE;
7870# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007871 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 win_close(curwin, !P_HID(curwin->w_buffer));
7873#endif
7874 }
7875}
7876
7877/*
7878 * ":print", ":list", ":number".
7879 */
7880 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007881ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007883 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7884 EMSG(_(e_emptybuf));
7885 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007887 for ( ;!got_int; ui_breakcheck())
7888 {
7889 print_line(eap->line1,
7890 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7891 || (eap->flags & EXFLAG_NR)),
7892 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7893 if (++eap->line1 > eap->line2)
7894 break;
7895 out_flush(); /* show one line at a time */
7896 }
7897 setpcmark();
7898 /* put cursor at last line */
7899 curwin->w_cursor.lnum = eap->line2;
7900 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 }
7902
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904}
7905
7906#ifdef FEAT_BYTEOFF
7907 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007908ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909{
7910 goto_byte(eap->line2);
7911}
7912#endif
7913
7914/*
7915 * ":shell".
7916 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007918ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919{
7920 do_shell(NULL, 0);
7921}
7922
7923#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7924 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007925 || defined(FEAT_GUI_MSWIN) \
7926 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 || defined(PROTO)
7928
7929/*
7930 * Handle a file drop. The code is here because a drop is *nearly* like an
7931 * :args command, but not quite (we have a list of exact filenames, so we
7932 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7933 * code is very similar to :args and hence needs access to a lot of the static
7934 * functions in this file.
7935 *
7936 * The list should be allocated using alloc(), as should each item in the
7937 * list. This function takes over responsibility for freeing the list.
7938 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007939 * XXX The list is made into the argument list. This is freed using
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007940 * FreeWild(), which does a series of vim_free() calls.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 */
7942 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007943handle_drop(
7944 int filec, /* the number of files dropped */
7945 char_u **filev, /* the list of files dropped */
7946 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947{
7948 exarg_T ea;
7949 int save_msg_scroll = msg_scroll;
7950
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007951 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007952 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007954#ifdef FEAT_AUTOCMD
7955 if (curbuf_locked())
7956 return;
7957#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007958 /* When the screen is being updated we should not change buffers and
7959 * windows structures, it may cause freed memory to be used. */
7960 if (updating_screen)
7961 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962
7963 /* Check whether the current buffer is changed. If so, we will need
7964 * to split the current window or data could be lost.
7965 * We don't need to check if the 'hidden' option is set, as in this
7966 * case the buffer won't be lost.
7967 */
7968 if (!P_HID(curbuf) && !split)
7969 {
7970 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007971 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 --emsg_off;
7973 }
7974 if (split)
7975 {
7976# ifdef FEAT_WINDOWS
7977 if (win_split(0, 0) == FAIL)
7978 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007979 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980
7981 /* When splitting the window, create a new alist. Otherwise the
7982 * existing one is overwritten. */
7983 alist_unlink(curwin->w_alist);
7984 alist_new();
7985# else
7986 return; /* can't split, always fail */
7987# endif
7988 }
7989
7990 /*
7991 * Set up the new argument list.
7992 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007993 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994
7995 /*
7996 * Move to the first file.
7997 */
7998 /* Fake up a minimal "next" command for do_argfile() */
7999 vim_memset(&ea, 0, sizeof(ea));
8000 ea.cmd = (char_u *)"next";
8001 do_argfile(&ea, 0);
8002
8003 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
8004 * mode that is not needed here. */
8005 need_start_insertmode = FALSE;
8006
8007 /* Restore msg_scroll, otherwise a following command may cause scrolling
8008 * unexpectedly. The screen will be redrawn by the caller, thus
8009 * msg_scroll being set by displaying a message is irrelevant. */
8010 msg_scroll = save_msg_scroll;
8011}
8012#endif
8013
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014/*
8015 * Clear an argument list: free all file names and reset it to zero entries.
8016 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008017 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008018alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019{
8020 while (--al->al_ga.ga_len >= 0)
8021 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
8022 ga_clear(&al->al_ga);
8023}
8024
8025/*
8026 * Init an argument list.
8027 */
8028 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008029alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030{
8031 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
8032}
8033
8034#if defined(FEAT_WINDOWS) || defined(PROTO)
8035
8036/*
8037 * Remove a reference from an argument list.
8038 * Ignored when the argument list is the global one.
8039 * If the argument list is no longer used by any window, free it.
8040 */
8041 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008042alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043{
8044 if (al != &global_alist && --al->al_refcount <= 0)
8045 {
8046 alist_clear(al);
8047 vim_free(al);
8048 }
8049}
8050
8051# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
8052/*
8053 * Create a new argument list and use it for the current window.
8054 */
8055 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008056alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057{
8058 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
8059 if (curwin->w_alist == NULL)
8060 {
8061 curwin->w_alist = &global_alist;
8062 ++global_alist.al_refcount;
8063 }
8064 else
8065 {
8066 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008067 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 alist_init(curwin->w_alist);
8069 }
8070}
8071# endif
8072#endif
8073
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008074#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075/*
8076 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008077 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8078 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 */
8080 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008081alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082{
8083 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008084 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 char_u **new_arg_files;
8086 int new_arg_file_count;
8087 char_u *save_p_su = p_su;
8088 int i;
8089
8090 /* Don't use 'suffixes' here. This should work like the shell did the
8091 * expansion. Also, the vimrc file isn't read yet, thus the user
8092 * can't set the options. */
8093 p_su = empty_option;
8094 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8095 if (old_arg_files != NULL)
8096 {
8097 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008098 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8099 old_arg_count = GARGCOUNT;
8100 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008102 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 && new_arg_file_count > 0)
8104 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008105 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8106 TRUE, fnum_list, fnum_len);
8107 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 }
8110 p_su = save_p_su;
8111}
8112#endif
8113
8114/*
8115 * Set the argument list for the current window.
8116 * Takes over the allocated files[] and the allocated fnames in it.
8117 */
8118 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008119alist_set(
8120 alist_T *al,
8121 int count,
8122 char_u **files,
8123 int use_curbuf,
8124 int *fnum_list,
8125 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126{
8127 int i;
8128
8129 alist_clear(al);
8130 if (ga_grow(&al->al_ga, count) == OK)
8131 {
8132 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008133 {
8134 if (got_int)
8135 {
8136 /* When adding many buffers this can take a long time. Allow
8137 * interrupting here. */
8138 while (i < count)
8139 vim_free(files[i++]);
8140 break;
8141 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008142
8143 /* May set buffer name of a buffer previously used for the
8144 * argument list, so that it's re-used by alist_add. */
8145 if (fnum_list != NULL && i < fnum_len)
8146 buf_set_name(fnum_list[i], files[i]);
8147
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008149 ui_breakcheck();
8150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 vim_free(files);
8152 }
8153 else
8154 FreeWild(count, files);
8155#ifdef FEAT_WINDOWS
8156 if (al == &global_alist)
8157#endif
8158 arg_had_last = FALSE;
8159}
8160
8161/*
8162 * Add file "fname" to argument list "al".
8163 * "fname" must have been allocated and "al" must have been checked for room.
8164 */
8165 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008166alist_add(
8167 alist_T *al,
8168 char_u *fname,
8169 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170{
8171 if (fname == NULL) /* don't add NULL file names */
8172 return;
8173#ifdef BACKSLASH_IN_FILENAME
8174 slash_adjust(fname);
8175#endif
8176 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8177 if (set_fnum > 0)
8178 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8179 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8180 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181}
8182
8183#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8184/*
8185 * Adjust slashes in file names. Called after 'shellslash' was set.
8186 */
8187 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008188alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189{
8190 int i;
8191# ifdef FEAT_WINDOWS
8192 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008193 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194# endif
8195
8196 for (i = 0; i < GARGCOUNT; ++i)
8197 if (GARGLIST[i].ae_fname != NULL)
8198 slash_adjust(GARGLIST[i].ae_fname);
8199# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00008200 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 if (wp->w_alist != &global_alist)
8202 for (i = 0; i < WARGCOUNT(wp); ++i)
8203 if (WARGLIST(wp)[i].ae_fname != NULL)
8204 slash_adjust(WARGLIST(wp)[i].ae_fname);
8205# endif
8206}
8207#endif
8208
8209/*
8210 * ":preserve".
8211 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008213ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008215 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 ml_preserve(curbuf, TRUE);
8217}
8218
8219/*
8220 * ":recover".
8221 */
8222 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008223ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224{
8225 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8226 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008227 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8228 | CCGD_MULTWIN
8229 | (eap->forceit ? CCGD_FORCEIT : 0)
8230 | CCGD_EXCMD)
8231
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 && (*eap->arg == NUL
8233 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8234 ml_recover();
8235 recoverymode = FALSE;
8236}
8237
8238/*
8239 * Command modifier used in a wrong way.
8240 */
8241 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008242ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243{
8244 eap->errmsg = e_invcmd;
8245}
8246
8247#ifdef FEAT_WINDOWS
8248/*
8249 * :sview [+command] file split window with new file, read-only
8250 * :split [[+command] file] split window with current or new file
8251 * :vsplit [[+command] file] split window vertically with current or new file
8252 * :new [[+command] file] split window with no or new file
8253 * :vnew [[+command] file] split vertically window with no or new file
8254 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008255 *
8256 * :tabedit open new Tab page with empty window
8257 * :tabedit [+command] file open new Tab page and edit "file"
8258 * :tabnew [[+command] file] just like :tabedit
8259 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 */
8261 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008262ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008264 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008265# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008267# endif
8268# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008270# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008272# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008274# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008276# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008278 * quickfix windows. But it's OK when doing ":tab split". */
8279 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 {
8281 if (eap->cmdidx == CMD_split)
8282 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 if (eap->cmdidx == CMD_vsplit)
8284 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008286# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008288# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008289 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 {
8291 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8292 FNAME_MESS, TRUE, curbuf->b_ffname);
8293 if (fname == NULL)
8294 goto theend;
8295 eap->arg = fname;
8296 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008297# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008299# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008301# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 && eap->cmdidx != CMD_new)
8305 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008306# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008307 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008308# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008309 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008310# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008311 au_has_group((char_u *)"FileExplorer"))
8312 {
8313 /* No browsing supported but we do have the file explorer:
8314 * Edit the directory. */
8315 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8316 eap->arg = (char_u *)".";
8317 }
8318 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008319# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008320 {
8321 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008323 if (fname == NULL)
8324 goto theend;
8325 eap->arg = fname;
8326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 }
8328 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008329# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008331 /*
8332 * Either open new tab page or split the window.
8333 */
8334 if (eap->cmdidx == CMD_tabedit
8335 || eap->cmdidx == CMD_tabfind
8336 || eap->cmdidx == CMD_tabnew)
8337 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008338 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8339 : eap->addr_count == 0 ? 0
8340 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008341 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008342 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008343
8344 /* set the alternate buffer for the window we came from */
8345 if (curwin != old_curwin
8346 && win_valid(old_curwin)
8347 && old_curwin->w_buffer != curbuf
8348 && !cmdmod.keepalt)
8349 old_curwin->w_alt_fnum = curbuf->b_fnum;
8350 }
8351 }
8352 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8354 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008355# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 /* Reset 'scrollbind' when editing another file, but keep it when
8357 * doing ":split" without arguments. */
8358 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008359# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008361# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008363 {
8364 RESET_BINDING(curwin);
8365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 else
8367 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008368# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 do_exedit(eap, old_curwin);
8370 }
8371
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008372# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008374# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008376# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377theend:
8378 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008379# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008381
8382/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008383 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008384 */
8385 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008386tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008387{
8388 exarg_T ea;
8389
8390 vim_memset(&ea, 0, sizeof(ea));
8391 ea.cmdidx = CMD_tabnew;
8392 ea.cmd = (char_u *)"tabn";
8393 ea.arg = (char_u *)"";
8394 ex_splitview(&ea);
8395}
8396
8397/*
8398 * :tabnext command
8399 */
8400 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008401ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008402{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008403 int tab_number;
8404
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008405 switch (eap->cmdidx)
8406 {
8407 case CMD_tabfirst:
8408 case CMD_tabrewind:
8409 goto_tabpage(1);
8410 break;
8411 case CMD_tablast:
8412 goto_tabpage(9999);
8413 break;
8414 case CMD_tabprevious:
8415 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008416 if (eap->arg && *eap->arg != NUL)
8417 {
8418 char_u *p = eap->arg;
8419 char_u *p_save = p;
8420
8421 tab_number = getdigits(&p);
8422 if (p == p_save || *p_save == '-' || *p != NUL
8423 || tab_number == 0)
8424 {
8425 /* No numbers as argument. */
8426 eap->errmsg = e_invarg;
8427 return;
8428 }
8429 }
8430 else
8431 {
8432 if (eap->addr_count == 0)
8433 tab_number = 1;
8434 else
8435 {
8436 tab_number = eap->line2;
8437 if (tab_number < 1)
8438 {
8439 eap->errmsg = e_invrange;
8440 return;
8441 }
8442 }
8443 }
8444 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008445 break;
8446 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008447 tab_number = get_tabpage_arg(eap);
8448 if (eap->errmsg == NULL)
8449 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008450 break;
8451 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008452}
8453
8454/*
8455 * :tabmove command
8456 */
8457 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008458ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008459{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008460 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008461
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008462 tab_number = get_tabpage_arg(eap);
8463 if (eap->errmsg == NULL)
8464 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008465}
8466
8467/*
8468 * :tabs command: List tabs and their contents.
8469 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008470 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008471ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008472{
8473 tabpage_T *tp;
8474 win_T *wp;
8475 int tabcount = 1;
8476
8477 msg_start();
8478 msg_scroll = TRUE;
8479 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8480 {
8481 msg_putchar('\n');
8482 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
8483 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
8484 out_flush(); /* output one line at a time */
8485 ui_breakcheck();
8486
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008487 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008488 wp = firstwin;
8489 else
8490 wp = tp->tp_firstwin;
8491 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8492 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008493 msg_putchar('\n');
8494 msg_putchar(wp == curwin ? '>' : ' ');
8495 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008496 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8497 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008498 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008499 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008500 else
8501 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8502 IObuff, IOSIZE, TRUE);
8503 msg_outtrans(IObuff);
8504 out_flush(); /* output one line at a time */
8505 ui_breakcheck();
8506 }
8507 }
8508}
8509
8510#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511
8512/*
8513 * ":mode": Set screen mode.
8514 * If no argument given, just get the screen size and redraw.
8515 */
8516 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008517ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518{
8519 if (*eap->arg == NUL)
8520 shell_resized();
8521 else
8522 mch_screenmode(eap->arg);
8523}
8524
8525#ifdef FEAT_WINDOWS
8526/*
8527 * ":resize".
8528 * set, increment or decrement current window height
8529 */
8530 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008531ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532{
8533 int n;
8534 win_T *wp = curwin;
8535
8536 if (eap->addr_count > 0)
8537 {
8538 n = eap->line2;
8539 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8540 ;
8541 }
8542
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008543# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008545# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 if (cmdmod.split & WSP_VERT)
8548 {
8549 if (*eap->arg == '-' || *eap->arg == '+')
8550 n += W_WIDTH(curwin);
8551 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8552 n = 9999;
8553 win_setwidth_win((int)n, wp);
8554 }
8555 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 {
8557 if (*eap->arg == '-' || *eap->arg == '+')
8558 n += curwin->w_height;
8559 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8560 n = 9999;
8561 win_setheight_win((int)n, wp);
8562 }
8563}
8564#endif
8565
8566/*
8567 * ":find [+command] <file>" command.
8568 */
8569 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008570ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571{
8572#ifdef FEAT_SEARCHPATH
8573 char_u *fname;
8574 int count;
8575
8576 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8577 TRUE, curbuf->b_ffname);
8578 if (eap->addr_count > 0)
8579 {
8580 /* Repeat finding the file "count" times. This matters when it
8581 * appears several times in the path. */
8582 count = eap->line2;
8583 while (fname != NULL && --count > 0)
8584 {
8585 vim_free(fname);
8586 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8587 FALSE, curbuf->b_ffname);
8588 }
8589 }
8590
8591 if (fname != NULL)
8592 {
8593 eap->arg = fname;
8594#endif
8595 do_exedit(eap, NULL);
8596#ifdef FEAT_SEARCHPATH
8597 vim_free(fname);
8598 }
8599#endif
8600}
8601
8602/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008603 * ":open" simulation: for now just work like ":visual".
8604 */
8605 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008606ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008607{
8608 regmatch_T regmatch;
8609 char_u *p;
8610
8611 curwin->w_cursor.lnum = eap->line2;
8612 beginline(BL_SOL | BL_FIX);
8613 if (*eap->arg == '/')
8614 {
8615 /* ":open /pattern/": put cursor in column found with pattern */
8616 ++eap->arg;
8617 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8618 *p = NUL;
8619 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8620 if (regmatch.regprog != NULL)
8621 {
8622 regmatch.rm_ic = p_ic;
8623 p = ml_get_curline();
8624 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008625 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008626 else
8627 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008628 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008629 }
8630 /* Move to the NUL, ignore any other arguments. */
8631 eap->arg += STRLEN(eap->arg);
8632 }
8633 check_cursor();
8634
8635 eap->cmdidx = CMD_visual;
8636 do_exedit(eap, NULL);
8637}
8638
8639/*
8640 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 */
8642 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008643ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644{
8645 do_exedit(eap, NULL);
8646}
8647
8648/*
8649 * ":edit <file>" command and alikes.
8650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008652do_exedit(
8653 exarg_T *eap,
8654 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655{
8656 int n;
8657#ifdef FEAT_WINDOWS
8658 int need_hide;
8659#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008660 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661
8662 /*
8663 * ":vi" command ends Ex mode.
8664 */
8665 if (exmode_active && (eap->cmdidx == CMD_visual
8666 || eap->cmdidx == CMD_view))
8667 {
8668 exmode_active = FALSE;
8669 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008670 {
8671 /* Special case: ":global/pat/visual\NLvi-commands" */
8672 if (global_busy)
8673 {
8674 int rd = RedrawingDisabled;
8675 int nwr = no_wait_return;
8676 int ms = msg_scroll;
8677#ifdef FEAT_GUI
8678 int he = hold_gui_events;
8679#endif
8680
8681 if (eap->nextcmd != NULL)
8682 {
8683 stuffReadbuff(eap->nextcmd);
8684 eap->nextcmd = NULL;
8685 }
8686
8687 if (exmode_was != EXMODE_VIM)
8688 settmode(TMODE_RAW);
8689 RedrawingDisabled = 0;
8690 no_wait_return = 0;
8691 need_wait_return = FALSE;
8692 msg_scroll = 0;
8693#ifdef FEAT_GUI
8694 hold_gui_events = 0;
8695#endif
8696 must_redraw = CLEAR;
8697
8698 main_loop(FALSE, TRUE);
8699
8700 RedrawingDisabled = rd;
8701 no_wait_return = nwr;
8702 msg_scroll = ms;
8703#ifdef FEAT_GUI
8704 hold_gui_events = he;
8705#endif
8706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 }
8710
8711 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008712 || eap->cmdidx == CMD_tabnew
8713 || eap->cmdidx == CMD_tabedit
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008714#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 || eap->cmdidx == CMD_vnew
8716#endif
8717 ) && *eap->arg == NUL)
8718 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008719 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 setpcmark();
8721 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008722 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8723 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 }
8725 else if ((eap->cmdidx != CMD_split
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008726#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 && eap->cmdidx != CMD_vsplit
8728#endif
8729 )
8730 || *eap->arg != NUL
8731#ifdef FEAT_BROWSE
8732 || cmdmod.browse
8733#endif
8734 )
8735 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008736#ifdef FEAT_AUTOCMD
8737 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8738 * can bring us here, others are stopped earlier. */
8739 if (*eap->arg != NUL && curbuf_locked())
8740 return;
8741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 n = readonlymode;
8743 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8744 readonlymode = TRUE;
8745 else if (eap->cmdidx == CMD_enew)
8746 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8747 empty buffer */
8748 setpcmark();
8749 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8750 NULL, eap,
8751 /* ":edit" goes to first line if Vi compatible */
8752 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8753 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8754 ? ECMD_ONE : eap->do_ecmd_lnum,
8755 (P_HID(curbuf) ? ECMD_HIDE : 0)
8756 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008757 /* after a split we can use an existing buffer */
8758 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759#ifdef FEAT_LISTCMDS
8760 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8761#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008762 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 {
8764 /* Editing the file failed. If the window was split, close it. */
8765#ifdef FEAT_WINDOWS
8766 if (old_curwin != NULL)
8767 {
8768 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8769 if (!need_hide || P_HID(curbuf))
8770 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008771# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8772 cleanup_T cs;
8773
8774 /* Reset the error/interrupt/exception state here so that
8775 * aborting() returns FALSE when closing a window. */
8776 enter_cleanup(&cs);
8777# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778# ifdef FEAT_GUI
8779 need_mouse_correct = TRUE;
8780# endif
8781 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008782
8783# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8784 /* Restore the error/interrupt/exception state if not
8785 * discarded by a new aborting error, interrupt, or
8786 * uncaught exception. */
8787 leave_cleanup(&cs);
8788# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 }
8790 }
8791#endif
8792 }
8793 else if (readonlymode && curbuf->b_nwindows == 1)
8794 {
8795 /* When editing an already visited buffer, 'readonly' won't be set
8796 * but the previous value is kept. With ":view" and ":sview" we
8797 * want the file to be readonly, except when another window is
8798 * editing the same buffer. */
8799 curbuf->b_p_ro = TRUE;
8800 }
8801 readonlymode = n;
8802 }
8803 else
8804 {
8805 if (eap->do_ecmd_cmd != NULL)
8806 do_cmdline_cmd(eap->do_ecmd_cmd);
8807#ifdef FEAT_TITLE
8808 n = curwin->w_arg_idx_invalid;
8809#endif
8810 check_arg_idx(curwin);
8811#ifdef FEAT_TITLE
8812 if (n != curwin->w_arg_idx_invalid)
8813 maketitle();
8814#endif
8815 }
8816
8817#ifdef FEAT_WINDOWS
8818 /*
8819 * if ":split file" worked, set alternate file name in old window to new
8820 * file
8821 */
8822 if (old_curwin != NULL
8823 && *eap->arg != NUL
8824 && curwin != old_curwin
8825 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008826 && old_curwin->w_buffer != curbuf
8827 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 old_curwin->w_alt_fnum = curbuf->b_fnum;
8829#endif
8830
8831 ex_no_reprint = TRUE;
8832}
8833
8834#ifndef FEAT_GUI
8835/*
8836 * ":gui" and ":gvim" when there is no GUI.
8837 */
8838 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008839ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840{
8841 eap->errmsg = e_nogvim;
8842}
8843#endif
8844
8845#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8846 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008847ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848{
8849 gui_make_tearoff(eap->arg);
8850}
8851#endif
8852
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008853#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008855ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008857 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858}
8859#endif
8860
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008862ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863{
8864 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8865 MSG(_("No swap file"));
8866 else
8867 msg(curbuf->b_ml.ml_mfp->mf_fname);
8868}
8869
8870/*
8871 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8872 * offset.
8873 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8874 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008876ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877{
8878#ifdef FEAT_SCROLLBIND
8879 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008880 win_T *save_curwin = curwin;
8881 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 long topline;
8883 long y;
8884 linenr_T old_linenr = curwin->w_cursor.lnum;
8885
8886 setpcmark();
8887
8888 /*
8889 * determine max topline
8890 */
8891 if (curwin->w_p_scb)
8892 {
8893 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008894 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 {
8896 if (wp->w_p_scb && wp->w_buffer)
8897 {
8898 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8899 if (topline > y)
8900 topline = y;
8901 }
8902 }
8903 if (topline < 1)
8904 topline = 1;
8905 }
8906 else
8907 {
8908 topline = 1;
8909 }
8910
8911
8912 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008913 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008915 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 {
8917 if (curwin->w_p_scb)
8918 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008919 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 y = topline - curwin->w_topline;
8921 if (y > 0)
8922 scrollup(y, TRUE);
8923 else
8924 scrolldown(-y, TRUE);
8925 curwin->w_scbind_pos = topline;
8926 redraw_later(VALID);
8927 cursor_correct();
8928#ifdef FEAT_WINDOWS
8929 curwin->w_redr_status = TRUE;
8930#endif
8931 }
8932 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008933 curwin = save_curwin;
8934 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 if (curwin->w_p_scb)
8936 {
8937 did_syncbind = TRUE;
8938 checkpcmark();
8939 if (old_linenr != curwin->w_cursor.lnum)
8940 {
8941 char_u ctrl_o[2];
8942
8943 ctrl_o[0] = Ctrl_O;
8944 ctrl_o[1] = 0;
8945 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8946 }
8947 }
8948#endif
8949}
8950
8951
8952 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008953ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008955 int i;
8956 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8957 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958
8959 if (eap->usefilter) /* :r!cmd */
8960 do_bang(1, eap, FALSE, FALSE, TRUE);
8961 else
8962 {
8963 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8964 return;
8965
8966#ifdef FEAT_BROWSE
8967 if (cmdmod.browse)
8968 {
8969 char_u *browseFile;
8970
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008971 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 NULL, NULL, NULL, curbuf);
8973 if (browseFile != NULL)
8974 {
8975 i = readfile(browseFile, NULL,
8976 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8977 vim_free(browseFile);
8978 }
8979 else
8980 i = OK;
8981 }
8982 else
8983#endif
8984 if (*eap->arg == NUL)
8985 {
8986 if (check_fname() == FAIL) /* check for no file name */
8987 return;
8988 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8989 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8990 }
8991 else
8992 {
8993 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8994 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8995 i = readfile(eap->arg, NULL,
8996 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8997
8998 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01008999 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000 {
9001#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
9002 if (!aborting())
9003#endif
9004 EMSG2(_(e_notopen), eap->arg);
9005 }
9006 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009007 {
9008 if (empty && exmode_active)
9009 {
9010 /* Delete the empty line that remains. Historically ex does
9011 * this but vi doesn't. */
9012 if (eap->line2 == 0)
9013 lnum = curbuf->b_ml.ml_line_count;
9014 else
9015 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009016 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009017 {
9018 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009019 if (curwin->w_cursor.lnum > 1
9020 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009021 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00009022 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009023 }
9024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 }
9028}
9029
Bram Moolenaarea408852005-06-25 22:49:46 +00009030static char_u *prev_dir = NULL;
9031
9032#if defined(EXITFREE) || defined(PROTO)
9033 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009034free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00009035{
9036 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00009037 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00009038
9039 vim_free(globaldir);
9040 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00009041}
9042#endif
9043
Bram Moolenaarf4258302013-06-02 18:20:17 +02009044/*
9045 * Deal with the side effects of changing the current directory.
9046 * When "local" is TRUE then this was after an ":lcd" command.
9047 */
9048 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009049post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02009050{
9051 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01009052 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02009053 if (local)
9054 {
9055 /* If still in global directory, need to remember current
9056 * directory as global directory. */
9057 if (globaldir == NULL && prev_dir != NULL)
9058 globaldir = vim_strsave(prev_dir);
9059 /* Remember this local directory for the window. */
9060 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9061 curwin->w_localdir = vim_strsave(NameBuff);
9062 }
9063 else
9064 {
9065 /* We are now in the global directory, no need to remember its
9066 * name. */
9067 vim_free(globaldir);
9068 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02009069 }
9070
9071 shorten_fnames(TRUE);
9072}
9073
Bram Moolenaarea408852005-06-25 22:49:46 +00009074
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075/*
9076 * ":cd", ":lcd", ":chdir" and ":lchdir".
9077 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00009078 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009079ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 char_u *new_dir;
9082 char_u *tofree;
9083
9084 new_dir = eap->arg;
9085#if !defined(UNIX) && !defined(VMS)
9086 /* for non-UNIX ":cd" means: print current directory */
9087 if (*new_dir == NUL)
9088 ex_pwd(NULL);
9089 else
9090#endif
9091 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009092#ifdef FEAT_AUTOCMD
9093 if (allbuf_locked())
9094 return;
9095#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009096 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9097 && !eap->forceit)
9098 {
Bram Moolenaar81870892007-11-11 18:17:28 +00009099 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009100 return;
9101 }
9102
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 /* ":cd -": Change to previous directory */
9104 if (STRCMP(new_dir, "-") == 0)
9105 {
9106 if (prev_dir == NULL)
9107 {
9108 EMSG(_("E186: No previous directory"));
9109 return;
9110 }
9111 new_dir = prev_dir;
9112 }
9113
9114 /* Save current directory for next ":cd -" */
9115 tofree = prev_dir;
9116 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9117 prev_dir = vim_strsave(NameBuff);
9118 else
9119 prev_dir = NULL;
9120
9121#if defined(UNIX) || defined(VMS)
9122 /* for UNIX ":cd" means: go to home directory */
9123 if (*new_dir == NUL)
9124 {
9125 /* use NameBuff for home directory name */
9126# ifdef VMS
9127 char_u *p;
9128
9129 p = mch_getenv((char_u *)"SYS$LOGIN");
9130 if (p == NULL || *p == NUL) /* empty is the same as not set */
9131 NameBuff[0] = NUL;
9132 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009133 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134# else
9135 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9136# endif
9137 new_dir = NameBuff;
9138 }
9139#endif
9140 if (new_dir == NULL || vim_chdir(new_dir))
9141 EMSG(_(e_failed));
9142 else
9143 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02009144 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145
9146 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009147 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 ex_pwd(eap);
9149 }
9150 vim_free(tofree);
9151 }
9152}
9153
9154/*
9155 * ":pwd".
9156 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009158ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159{
9160 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9161 {
9162#ifdef BACKSLASH_IN_FILENAME
9163 slash_adjust(NameBuff);
9164#endif
9165 msg(NameBuff);
9166 }
9167 else
9168 EMSG(_("E187: Unknown"));
9169}
9170
9171/*
9172 * ":=".
9173 */
9174 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009175ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176{
9177 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009178 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179}
9180
9181 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009182ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009184 int n;
9185 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186
9187 if (cursor_valid())
9188 {
9189 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9190 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01009191 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009193
9194 len = eap->line2;
9195 switch (*eap->arg)
9196 {
9197 case 'm': break;
9198 case NUL: len *= 1000L; break;
9199 default: EMSG2(_(e_invarg2), eap->arg); return;
9200 }
9201 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202}
9203
9204/*
9205 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9206 */
9207 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009208do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209{
9210 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009211 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212
9213 cursor_on();
9214 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009215 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009217 wait_now = msec - done > 1000L ? 1000L : msec - done;
9218#ifdef FEAT_TIMERS
9219 {
9220 long due_time = check_due_timer();
9221
9222 if (due_time > 0 && due_time < wait_now)
9223 wait_now = due_time;
9224 }
9225#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009226#ifdef FEAT_JOB_CHANNEL
9227 if (has_any_channel() && wait_now > 100L)
9228 wait_now = 100L;
9229#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009230 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009231#ifdef FEAT_JOB_CHANNEL
9232 if (has_any_channel())
9233 ui_breakcheck_force(TRUE);
9234 else
9235#endif
9236 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009237#ifdef MESSAGE_QUEUE
9238 /* Process the netbeans and clientserver messages that may have been
9239 * received in the call to ui_breakcheck() when the GUI is in use. This
9240 * may occur when running a test case. */
9241 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 }
9244}
9245
9246 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009247do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248{
9249 int mode;
9250 char_u *cmdp;
9251
9252 cmdp = eap->cmd;
9253 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9254
9255 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9256 eap->arg, mode, isabbrev))
9257 {
9258 case 1: EMSG(_(e_invarg));
9259 break;
9260 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9261 break;
9262 }
9263}
9264
9265/*
9266 * ":winsize" command (obsolete).
9267 */
9268 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009269ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270{
9271 int w, h;
9272 char_u *arg = eap->arg;
9273 char_u *p;
9274
9275 w = getdigits(&arg);
9276 arg = skipwhite(arg);
9277 p = arg;
9278 h = getdigits(&arg);
9279 if (*p != NUL && *arg == NUL)
9280 set_shellsize(w, h, TRUE);
9281 else
9282 EMSG(_("E465: :winsize requires two number arguments"));
9283}
9284
9285#ifdef FEAT_WINDOWS
9286 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009287ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288{
9289 int xchar = NUL;
9290 char_u *p;
9291
9292 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9293 {
9294 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9295 if (eap->arg[1] == NUL)
9296 {
9297 EMSG(_(e_invarg));
9298 return;
9299 }
9300 xchar = eap->arg[1];
9301 p = eap->arg + 2;
9302 }
9303 else
9304 p = eap->arg + 1;
9305
9306 eap->nextcmd = check_nextcmd(p);
9307 p = skipwhite(p);
9308 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9309 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009310 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311 {
9312 /* Pass flags on for ":vertical wincmd ]". */
9313 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009314 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9316 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009317 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318 }
9319}
9320#endif
9321
Bram Moolenaar843ee412004-06-30 16:16:41 +00009322#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323/*
9324 * ":winpos".
9325 */
9326 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009327ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328{
9329 int x, y;
9330 char_u *arg = eap->arg;
9331 char_u *p;
9332
9333 if (*arg == NUL)
9334 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009335# if defined(FEAT_GUI) || defined(MSWIN)
9336# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009338# else
9339 if (mch_get_winpos(&x, &y) != FAIL)
9340# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 {
9342 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9343 msg(IObuff);
9344 }
9345 else
9346# endif
9347 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9348 }
9349 else
9350 {
9351 x = getdigits(&arg);
9352 arg = skipwhite(arg);
9353 p = arg;
9354 y = getdigits(&arg);
9355 if (*p == NUL || *arg != NUL)
9356 {
9357 EMSG(_("E466: :winpos requires two number arguments"));
9358 return;
9359 }
9360# ifdef FEAT_GUI
9361 if (gui.in_use)
9362 gui_mch_set_winpos(x, y);
9363 else if (gui.starting)
9364 {
9365 /* Remember the coordinates for when the window is opened. */
9366 gui_win_x = x;
9367 gui_win_y = y;
9368 }
9369# ifdef HAVE_TGETENT
9370 else
9371# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009372# else
9373# ifdef MSWIN
9374 mch_set_winpos(x, y);
9375# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376# endif
9377# ifdef HAVE_TGETENT
9378 if (*T_CWP)
9379 term_set_winpos(x, y);
9380# endif
9381 }
9382}
9383#endif
9384
9385/*
9386 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9387 */
9388 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009389ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390{
9391 oparg_T oa;
9392
9393 clear_oparg(&oa);
9394 oa.regname = eap->regname;
9395 oa.start.lnum = eap->line1;
9396 oa.end.lnum = eap->line2;
9397 oa.line_count = eap->line2 - eap->line1 + 1;
9398 oa.motion_type = MLINE;
9399#ifdef FEAT_VIRTUALEDIT
9400 virtual_op = FALSE;
9401#endif
9402 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9403 {
9404 setpcmark();
9405 curwin->w_cursor.lnum = eap->line1;
9406 beginline(BL_SOL | BL_FIX);
9407 }
9408
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009409 if (VIsual_active)
9410 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009411
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 switch (eap->cmdidx)
9413 {
9414 case CMD_delete:
9415 oa.op_type = OP_DELETE;
9416 op_delete(&oa);
9417 break;
9418
9419 case CMD_yank:
9420 oa.op_type = OP_YANK;
9421 (void)op_yank(&oa, FALSE, TRUE);
9422 break;
9423
9424 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009425 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009427 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9428#else
9429 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009431 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 oa.op_type = OP_RSHIFT;
9433 else
9434 oa.op_type = OP_LSHIFT;
9435 op_shift(&oa, FALSE, eap->amount);
9436 break;
9437 }
9438#ifdef FEAT_VIRTUALEDIT
9439 virtual_op = MAYBE;
9440#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009441 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442}
9443
9444/*
9445 * ":put".
9446 */
9447 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009448ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449{
9450 /* ":0put" works like ":1put!". */
9451 if (eap->line2 == 0)
9452 {
9453 eap->line2 = 1;
9454 eap->forceit = TRUE;
9455 }
9456 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009457 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9458 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459}
9460
9461/*
9462 * Handle ":copy" and ":move".
9463 */
9464 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009465ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466{
9467 long n;
9468
Bram Moolenaarded27822017-01-02 14:27:34 +01009469 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 if (eap->arg == NULL) /* error detected */
9471 {
9472 eap->nextcmd = NULL;
9473 return;
9474 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009475 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476
9477 /*
9478 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9479 */
9480 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9481 {
9482 EMSG(_(e_invaddr));
9483 return;
9484 }
9485
9486 if (eap->cmdidx == CMD_move)
9487 {
9488 if (do_move(eap->line1, eap->line2, n) == FAIL)
9489 return;
9490 }
9491 else
9492 ex_copy(eap->line1, eap->line2, n);
9493 u_clearline();
9494 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009495 ex_may_print(eap);
9496}
9497
9498/*
9499 * Print the current line if flags were given to the Ex command.
9500 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009501 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009502ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009503{
9504 if (eap->flags != 0)
9505 {
9506 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9507 (eap->flags & EXFLAG_LIST));
9508 ex_no_reprint = TRUE;
9509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510}
9511
9512/*
9513 * ":smagic" and ":snomagic".
9514 */
9515 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009516ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517{
9518 int magic_save = p_magic;
9519
9520 p_magic = (eap->cmdidx == CMD_smagic);
9521 do_sub(eap);
9522 p_magic = magic_save;
9523}
9524
9525/*
9526 * ":join".
9527 */
9528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009529ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530{
9531 curwin->w_cursor.lnum = eap->line1;
9532 if (eap->line1 == eap->line2)
9533 {
9534 if (eap->addr_count >= 2) /* :2,2join does nothing */
9535 return;
9536 if (eap->line2 == curbuf->b_ml.ml_line_count)
9537 {
9538 beep_flush();
9539 return;
9540 }
9541 ++eap->line2;
9542 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009543 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009545 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546}
9547
9548/*
9549 * ":[addr]@r" or ":[addr]*r": execute register
9550 */
9551 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009552ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553{
9554 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009555 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556
9557 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009558 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559
9560#ifdef USE_ON_FLY_SCROLL
9561 dont_scroll = TRUE; /* disallow scrolling here */
9562#endif
9563
9564 /* get the register name. No name means to use the previous one */
9565 c = *eap->arg;
9566 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9567 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009568 /* Put the register in the typeahead buffer with the "silent" flag. */
9569 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9570 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009571 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 else
9575 {
9576 int save_efr = exec_from_reg;
9577
9578 exec_from_reg = TRUE;
9579
9580 /*
9581 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009582 * Continue until the stuff buffer is empty and all added characters
9583 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009585 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9587
9588 exec_from_reg = save_efr;
9589 }
9590}
9591
9592/*
9593 * ":!".
9594 */
9595 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009596ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597{
9598 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9599}
9600
9601/*
9602 * ":undo".
9603 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009605ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009607 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009608 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009609 else
9610 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611}
9612
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009613#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009614 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009615ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009616{
9617 char_u hash[UNDO_HASH_SIZE];
9618
9619 u_compute_hash(hash);
9620 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9621}
9622
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009623 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009624ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009625{
9626 char_u hash[UNDO_HASH_SIZE];
9627
9628 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009629 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009630}
9631#endif
9632
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633/*
9634 * ":redo".
9635 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009637ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638{
9639 u_redo(1);
9640}
9641
9642/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009643 * ":earlier" and ":later".
9644 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009645 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009646ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009647{
9648 long count = 0;
9649 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009650 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009651 char_u *p = eap->arg;
9652
9653 if (*p == NUL)
9654 count = 1;
9655 else if (isdigit(*p))
9656 {
9657 count = getdigits(&p);
9658 switch (*p)
9659 {
9660 case 's': ++p; sec = TRUE; break;
9661 case 'm': ++p; sec = TRUE; count *= 60; break;
9662 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009663 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9664 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009665 }
9666 }
9667
9668 if (*p != NUL)
9669 EMSG2(_(e_invarg2), eap->arg);
9670 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009671 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9672 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009673}
9674
9675/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 * ":redir": start/stop redirection.
9677 */
9678 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009679ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680{
9681 char *mode;
9682 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009683 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684
Bram Moolenaarba768492016-07-08 15:32:54 +02009685#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009686 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009687 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009688 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009689 return;
9690 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009691#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009692
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 if (STRICMP(eap->arg, "END") == 0)
9694 close_redir();
9695 else
9696 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009697 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009699 ++arg;
9700 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009702 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 mode = "a";
9704 }
9705 else
9706 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009707 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708
9709 close_redir();
9710
9711 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009712 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 if (fname == NULL)
9714 return;
9715#ifdef FEAT_BROWSE
9716 if (cmdmod.browse)
9717 {
9718 char_u *browseFile;
9719
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009720 browseFile = do_browse(BROWSE_SAVE,
9721 (char_u *)_("Save Redirection"),
9722 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723 if (browseFile == NULL)
9724 return; /* operation cancelled */
9725 vim_free(fname);
9726 fname = browseFile;
9727 eap->forceit = TRUE; /* since dialog already asked */
9728 }
9729#endif
9730
9731 redir_fd = open_exfile(fname, eap->forceit, mode);
9732 vim_free(fname);
9733 }
9734#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009735 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 {
9737 /* redirect to a register a-z (resp. A-Z for appending) */
9738 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009739 ++arg;
9740 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009742 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009743 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009745 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009747 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009748 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009749 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009750 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009752 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009753 if (*arg == '>')
9754 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009755 /* Make register empty when not using @A-@Z and the
9756 * command is valid. */
9757 if (*arg == NUL && !isupper(redir_reg))
9758 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009760 }
9761 if (*arg != NUL)
9762 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009763 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009764 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009765 }
9766 }
9767 else if (*arg == '=' && arg[1] == '>')
9768 {
9769 int append;
9770
9771 /* redirect to a variable */
9772 close_redir();
9773 arg += 2;
9774
9775 if (*arg == '>')
9776 {
9777 ++arg;
9778 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 }
9780 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009781 append = FALSE;
9782
9783 if (var_redir_start(skipwhite(arg), append) == OK)
9784 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 }
9786#endif
9787
9788 /* TODO: redirect to a buffer */
9789
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009791 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009793
9794 /* Make sure redirection is not off. Can happen for cmdline completion
9795 * that indirectly invokes a command to catch its output. */
9796 if (redir_fd != NULL
9797#ifdef FEAT_EVAL
9798 || redir_reg || redir_vname
9799#endif
9800 )
9801 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802}
9803
9804/*
9805 * ":redraw": force redraw
9806 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009807 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009808ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809{
9810 int r = RedrawingDisabled;
9811 int p = p_lz;
9812
9813 RedrawingDisabled = 0;
9814 p_lz = FALSE;
9815 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009816 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817#ifdef FEAT_TITLE
9818 if (need_maketitle)
9819 maketitle();
9820#endif
9821 RedrawingDisabled = r;
9822 p_lz = p;
9823
9824 /* Reset msg_didout, so that a message that's there is overwritten. */
9825 msg_didout = FALSE;
9826 msg_col = 0;
9827
Bram Moolenaar56667a52013-07-17 11:54:28 +02009828 /* No need to wait after an intentional redraw. */
9829 need_wait_return = FALSE;
9830
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831 out_flush();
9832}
9833
9834/*
9835 * ":redrawstatus": force redraw of status line(s)
9836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009838ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839{
9840#if defined(FEAT_WINDOWS)
9841 int r = RedrawingDisabled;
9842 int p = p_lz;
9843
9844 RedrawingDisabled = 0;
9845 p_lz = FALSE;
9846 if (eap->forceit)
9847 status_redraw_all();
9848 else
9849 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009850 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851 RedrawingDisabled = r;
9852 p_lz = p;
9853 out_flush();
9854#endif
9855}
9856
9857 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009858close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859{
9860 if (redir_fd != NULL)
9861 {
9862 fclose(redir_fd);
9863 redir_fd = NULL;
9864 }
9865#ifdef FEAT_EVAL
9866 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009867 if (redir_vname)
9868 {
9869 var_redir_stop();
9870 redir_vname = 0;
9871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872#endif
9873}
9874
9875#if defined(FEAT_SESSION) && defined(USE_CRNL)
9876# define MKSESSION_NL
9877static int mksession_nl = FALSE; /* use NL only in put_eol() */
9878#endif
9879
9880/*
9881 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9882 */
9883 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009884ex_mkrc(
9885 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886{
9887 FILE *fd;
9888 int failed = FALSE;
9889 char_u *fname;
9890#ifdef FEAT_BROWSE
9891 char_u *browseFile = NULL;
9892#endif
9893#ifdef FEAT_SESSION
9894 int view_session = FALSE;
9895 int using_vdir = FALSE; /* using 'viewdir'? */
9896 char_u *viewFile = NULL;
9897 unsigned *flagp;
9898#endif
9899
9900 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9901 {
9902#ifdef FEAT_SESSION
9903 view_session = TRUE;
9904#else
9905 ex_ni(eap);
9906 return;
9907#endif
9908 }
9909
9910#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009911 /* Use the short file name until ":lcd" is used. We also don't use the
9912 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009913 did_lcd = FALSE;
9914
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9916 if (eap->cmdidx == CMD_mkview
9917 && (*eap->arg == NUL
9918 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9919 {
9920 eap->forceit = TRUE;
9921 fname = get_view_file(*eap->arg);
9922 if (fname == NULL)
9923 return;
9924 viewFile = fname;
9925 using_vdir = TRUE;
9926 }
9927 else
9928#endif
9929 if (*eap->arg != NUL)
9930 fname = eap->arg;
9931 else if (eap->cmdidx == CMD_mkvimrc)
9932 fname = (char_u *)VIMRC_FILE;
9933#ifdef FEAT_SESSION
9934 else if (eap->cmdidx == CMD_mksession)
9935 fname = (char_u *)SESSION_FILE;
9936#endif
9937 else
9938 fname = (char_u *)EXRC_FILE;
9939
9940#ifdef FEAT_BROWSE
9941 if (cmdmod.browse)
9942 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009943 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944# ifdef FEAT_SESSION
9945 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9946 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9947# endif
9948 (char_u *)_("Save Setup"),
9949 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9950 if (browseFile == NULL)
9951 goto theend;
9952 fname = browseFile;
9953 eap->forceit = TRUE; /* since dialog already asked */
9954 }
9955#endif
9956
9957#if defined(FEAT_SESSION) && defined(vim_mkdir)
9958 /* When using 'viewdir' may have to create the directory. */
9959 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009960 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961#endif
9962
9963 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9964 if (fd != NULL)
9965 {
9966#ifdef FEAT_SESSION
9967 if (eap->cmdidx == CMD_mkview)
9968 flagp = &vop_flags;
9969 else
9970 flagp = &ssop_flags;
9971#endif
9972
9973#ifdef MKSESSION_NL
9974 /* "unix" in 'sessionoptions': use NL line separator */
9975 if (view_session && (*flagp & SSOP_UNIX))
9976 mksession_nl = TRUE;
9977#endif
9978
9979 /* Write the version command for :mkvimrc */
9980 if (eap->cmdidx == CMD_mkvimrc)
9981 (void)put_line(fd, "version 6.0");
9982
9983#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009984 if (eap->cmdidx == CMD_mksession)
9985 {
9986 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9987 failed = TRUE;
9988 }
9989
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 if (eap->cmdidx != CMD_mkview)
9991#endif
9992 {
9993 /* Write setting 'compatible' first, because it has side effects.
9994 * For that same reason only do it when needed. */
9995 if (p_cp)
9996 (void)put_line(fd, "if !&cp | set cp | endif");
9997 else
9998 (void)put_line(fd, "if &cp | set nocp | endif");
9999 }
10000
10001#ifdef FEAT_SESSION
10002 if (!view_session
10003 || (eap->cmdidx == CMD_mksession
10004 && (*flagp & SSOP_OPTIONS)))
10005#endif
10006 failed |= (makemap(fd, NULL) == FAIL
10007 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
10008
10009#ifdef FEAT_SESSION
10010 if (!failed && view_session)
10011 {
10012 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
10013 failed = TRUE;
10014 if (eap->cmdidx == CMD_mksession)
10015 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010016 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017
Bram Moolenaard9462e32011-04-11 21:35:11 +020010018 dirnow = alloc(MAXPATHL);
10019 if (dirnow == NULL)
10020 failed = TRUE;
10021 else
10022 {
10023 /*
10024 * Change to session file's dir.
10025 */
10026 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010028 *dirnow = NUL;
10029 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
10030 {
10031 if (vim_chdirfile(fname) == OK)
10032 shorten_fnames(TRUE);
10033 }
10034 else if (*dirnow != NUL
10035 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
10036 {
10037 if (mch_chdir((char *)globaldir) == 0)
10038 shorten_fnames(TRUE);
10039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040
Bram Moolenaard9462e32011-04-11 21:35:11 +020010041 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042
Bram Moolenaard9462e32011-04-11 21:35:11 +020010043 /* restore original dir */
10044 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +020010046 {
10047 if (mch_chdir((char *)dirnow) != 0)
10048 EMSG(_(e_prev_dir));
10049 shorten_fnames(TRUE);
10050 }
10051 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 }
10053 }
10054 else
10055 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010056 failed |= (put_view(fd, curwin, !using_vdir, flagp,
10057 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058 }
10059 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
10060 == FAIL)
10061 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010062 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
10063 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010064 if (eap->cmdidx == CMD_mksession)
10065 {
10066 if (put_line(fd, "unlet SessionLoad") == FAIL)
10067 failed = TRUE;
10068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 }
10070#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010071 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
10072 failed = TRUE;
10073
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074 failed |= fclose(fd);
10075
10076 if (failed)
10077 EMSG(_(e_write));
10078#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
10079 else if (eap->cmdidx == CMD_mksession)
10080 {
10081 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010082 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083
Bram Moolenaard9462e32011-04-11 21:35:11 +020010084 tbuf = alloc(MAXPATHL);
10085 if (tbuf != NULL)
10086 {
10087 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10088 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10089 vim_free(tbuf);
10090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091 }
10092#endif
10093#ifdef MKSESSION_NL
10094 mksession_nl = FALSE;
10095#endif
10096 }
10097
10098#ifdef FEAT_BROWSE
10099theend:
10100 vim_free(browseFile);
10101#endif
10102#ifdef FEAT_SESSION
10103 vim_free(viewFile);
10104#endif
10105}
10106
Bram Moolenaardf177f62005-02-22 08:39:57 +000010107#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10108 || defined(PROTO)
10109 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010110vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010111{
10112 if (vim_mkdir(name, prot) != 0)
10113 {
10114 EMSG2(_("E739: Cannot create directory: %s"), name);
10115 return FAIL;
10116 }
10117 return OK;
10118}
10119#endif
10120
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121/*
10122 * Open a file for writing for an Ex command, with some checks.
10123 * Return file descriptor, or NULL on failure.
10124 */
10125 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010126open_exfile(
10127 char_u *fname,
10128 int forceit,
10129 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130{
10131 FILE *fd;
10132
10133#ifdef UNIX
10134 /* with Unix it is possible to open a directory */
10135 if (mch_isdir(fname))
10136 {
10137 EMSG2(_(e_isadir2), fname);
10138 return NULL;
10139 }
10140#endif
10141 if (!forceit && *mode != 'a' && vim_fexists(fname))
10142 {
10143 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
10144 return NULL;
10145 }
10146
10147 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
10148 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
10149
10150 return fd;
10151}
10152
10153/*
10154 * ":mark" and ":k".
10155 */
10156 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010157ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158{
10159 pos_T pos;
10160
10161 if (*eap->arg == NUL) /* No argument? */
10162 EMSG(_(e_argreq));
10163 else if (eap->arg[1] != NUL) /* more than one character? */
10164 EMSG(_(e_trailing));
10165 else
10166 {
10167 pos = curwin->w_cursor; /* save curwin->w_cursor */
10168 curwin->w_cursor.lnum = eap->line2;
10169 beginline(BL_WHITE | BL_FIX);
10170 if (setmark(*eap->arg) == FAIL) /* set mark */
10171 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10172 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10173 }
10174}
10175
10176/*
10177 * Update w_topline, w_leftcol and the cursor position.
10178 */
10179 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010180update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181{
10182 check_cursor(); /* put cursor on valid line */
10183 update_topline();
10184 if (!curwin->w_p_wrap)
10185 validate_cursor();
10186 update_curswant();
10187}
10188
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189/*
10190 * ":normal[!] {commands}": Execute normal mode commands.
10191 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010192 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010193ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195 int save_msg_scroll = msg_scroll;
10196 int save_restart_edit = restart_edit;
10197 int save_msg_didout = msg_didout;
10198 int save_State = State;
10199 tasave_T tabuf;
10200 int save_insertmode = p_im;
10201 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010202 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203#ifdef FEAT_MBYTE
10204 char_u *arg = NULL;
10205 int l;
10206 char_u *p;
10207#endif
10208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010209 if (ex_normal_lock > 0)
10210 {
10211 EMSG(_(e_secure));
10212 return;
10213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 if (ex_normal_busy >= p_mmd)
10215 {
10216 EMSG(_("E192: Recursive use of :normal too deep"));
10217 return;
10218 }
10219 ++ex_normal_busy;
10220
10221 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10222 restart_edit = 0; /* don't go to Insert mode */
10223 p_im = FALSE; /* don't use 'insertmode' */
10224
10225#ifdef FEAT_MBYTE
10226 /*
10227 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10228 * this for the K_SPECIAL leading byte, otherwise special keys will not
10229 * work.
10230 */
10231 if (has_mbyte)
10232 {
10233 int len = 0;
10234
10235 /* Count the number of characters to be escaped. */
10236 for (p = eap->arg; *p != NUL; ++p)
10237 {
10238# ifdef FEAT_GUI
10239 if (*p == CSI) /* leadbyte CSI */
10240 len += 2;
10241# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010242 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010243 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10244# ifdef FEAT_GUI
10245 || *p == CSI
10246# endif
10247 )
10248 len += 2;
10249 }
10250 if (len > 0)
10251 {
10252 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10253 if (arg != NULL)
10254 {
10255 len = 0;
10256 for (p = eap->arg; *p != NUL; ++p)
10257 {
10258 arg[len++] = *p;
10259# ifdef FEAT_GUI
10260 if (*p == CSI)
10261 {
10262 arg[len++] = KS_EXTRA;
10263 arg[len++] = (int)KE_CSI;
10264 }
10265# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010266 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 {
10268 arg[len++] = *++p;
10269 if (*p == K_SPECIAL)
10270 {
10271 arg[len++] = KS_SPECIAL;
10272 arg[len++] = KE_FILLER;
10273 }
10274# ifdef FEAT_GUI
10275 else if (*p == CSI)
10276 {
10277 arg[len++] = KS_EXTRA;
10278 arg[len++] = (int)KE_CSI;
10279 }
10280# endif
10281 }
10282 arg[len] = NUL;
10283 }
10284 }
10285 }
10286 }
10287#endif
10288
10289 /*
10290 * Save the current typeahead. This is required to allow using ":normal"
10291 * from an event handler and makes sure we don't hang when the argument
10292 * ends with half a command.
10293 */
10294 save_typeahead(&tabuf);
10295 if (tabuf.typebuf_valid)
10296 {
10297 /*
10298 * Repeat the :normal command for each line in the range. When no
10299 * range given, execute it just once, without positioning the cursor
10300 * first.
10301 */
10302 do
10303 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304 if (eap->addr_count != 0)
10305 {
10306 curwin->w_cursor.lnum = eap->line1++;
10307 curwin->w_cursor.col = 0;
10308 }
10309
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010310 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311#ifdef FEAT_MBYTE
10312 arg != NULL ? arg :
10313#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010314 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315 }
10316 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10317 }
10318
10319 /* Might not return to the main loop when in an event handler. */
10320 update_topline_cursor();
10321
10322 /* Restore the previous typeahead. */
10323 restore_typeahead(&tabuf);
10324
10325 --ex_normal_busy;
10326 msg_scroll = save_msg_scroll;
10327 restart_edit = save_restart_edit;
10328 p_im = save_insertmode;
10329 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010330 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
10332
10333 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +010010334 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +010010336#ifdef FEAT_MOUSE
10337 setmouse();
10338#endif
10339#ifdef CURSOR_SHAPE
10340 ui_cursor_shape(); /* may show different cursor shape */
10341#endif
10342
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343#ifdef FEAT_MBYTE
10344 vim_free(arg);
10345#endif
10346}
10347
10348/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010349 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 */
10351 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010352ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010354 if (eap->forceit)
10355 {
10356 coladvance((colnr_T)MAXCOL);
10357 curwin->w_curswant = MAXCOL;
10358 curwin->w_set_curswant = FALSE;
10359 }
10360
Bram Moolenaara40c5002005-01-09 21:16:21 +000010361 /* Ignore the command when already in Insert mode. Inserting an
10362 * expression register that invokes a function can do this. */
10363 if (State & INSERT)
10364 return;
10365
Bram Moolenaar64969662005-12-14 21:59:55 +000010366 if (eap->cmdidx == CMD_startinsert)
10367 restart_edit = 'a';
10368 else if (eap->cmdidx == CMD_startreplace)
10369 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010371 restart_edit = 'V';
10372
10373 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010375 if (eap->cmdidx == CMD_startinsert)
10376 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377 curwin->w_curswant = 0; /* avoid MAXCOL */
10378 }
10379}
10380
10381/*
10382 * ":stopinsert"
10383 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010385ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386{
10387 restart_edit = 0;
10388 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010389 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010392/*
10393 * Execute normal mode command "cmd".
10394 * "remap" can be REMAP_NONE or REMAP_YES.
10395 */
10396 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010397exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010398{
Bram Moolenaar25281632016-01-21 23:32:32 +010010399 /* Stuff the argument into the typeahead buffer. */
10400 ins_typebuf(cmd, remap, 0, TRUE, silent);
10401 exec_normal(FALSE);
10402}
Bram Moolenaar25281632016-01-21 23:32:32 +010010403
Bram Moolenaar25281632016-01-21 23:32:32 +010010404/*
10405 * Execute normal_cmd() until there is no typeahead left.
10406 */
10407 void
10408exec_normal(int was_typed)
10409{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010410 oparg_T oa;
10411
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010412 clear_oparg(&oa);
10413 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010414 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10415 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010416 {
10417 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010418 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010419 }
10420}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010421
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422#ifdef FEAT_FIND_ID
10423 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010424ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425{
10426 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10427 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10428 (linenr_T)1, (linenr_T)MAXLNUM);
10429}
10430
10431#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
10432/*
10433 * ":psearch"
10434 */
10435 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010436ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437{
10438 g_do_tagpreview = p_pvh;
10439 ex_findpat(eap);
10440 g_do_tagpreview = 0;
10441}
10442#endif
10443
10444 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010445ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446{
10447 int whole = TRUE;
10448 long n;
10449 char_u *p;
10450 int action;
10451
10452 switch (cmdnames[eap->cmdidx].cmd_name[2])
10453 {
10454 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10455 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10456 action = ACTION_GOTO;
10457 else
10458 action = ACTION_SHOW;
10459 break;
10460 case 'i': /* ":ilist" and ":dlist" */
10461 action = ACTION_SHOW_ALL;
10462 break;
10463 case 'u': /* ":ijump" and ":djump" */
10464 action = ACTION_GOTO;
10465 break;
10466 default: /* ":isplit" and ":dsplit" */
10467 action = ACTION_SPLIT;
10468 break;
10469 }
10470
10471 n = 1;
10472 if (vim_isdigit(*eap->arg)) /* get count */
10473 {
10474 n = getdigits(&eap->arg);
10475 eap->arg = skipwhite(eap->arg);
10476 }
10477 if (*eap->arg == '/') /* Match regexp, not just whole words */
10478 {
10479 whole = FALSE;
10480 ++eap->arg;
10481 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10482 if (*p)
10483 {
10484 *p++ = NUL;
10485 p = skipwhite(p);
10486
10487 /* Check for trailing illegal characters */
10488 if (!ends_excmd(*p))
10489 eap->errmsg = e_trailing;
10490 else
10491 eap->nextcmd = check_nextcmd(p);
10492 }
10493 }
10494 if (!eap->skip)
10495 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10496 whole, !eap->forceit,
10497 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10498 n, action, eap->line1, eap->line2);
10499}
10500#endif
10501
10502#ifdef FEAT_WINDOWS
10503
10504# ifdef FEAT_QUICKFIX
10505/*
10506 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10507 */
10508 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010509ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010511 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10513}
10514
10515/*
10516 * ":pedit"
10517 */
10518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010519ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520{
10521 win_T *curwin_save = curwin;
10522
10523 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010524 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525 keep_help_flag = curwin_save->w_buffer->b_help;
10526 do_exedit(eap, NULL);
10527 keep_help_flag = FALSE;
10528 if (curwin != curwin_save && win_valid(curwin_save))
10529 {
10530 /* Return cursor to where we were */
10531 validate_cursor();
10532 redraw_later(VALID);
10533 win_enter(curwin_save, TRUE);
10534 }
10535 g_do_tagpreview = 0;
10536}
10537# endif
10538
10539/*
10540 * ":stag", ":stselect" and ":stjump".
10541 */
10542 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010543ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544{
10545 postponed_split = -1;
10546 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010547 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10549 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010550 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551}
10552#endif
10553
10554/*
10555 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10556 */
10557 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010558ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559{
10560 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10561}
10562
10563 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010564ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565{
10566 int cmd;
10567
10568 switch (name[1])
10569 {
10570 case 'j': cmd = DT_JUMP; /* ":tjump" */
10571 break;
10572 case 's': cmd = DT_SELECT; /* ":tselect" */
10573 break;
10574 case 'p': cmd = DT_PREV; /* ":tprevious" */
10575 break;
10576 case 'N': cmd = DT_PREV; /* ":tNext" */
10577 break;
10578 case 'n': cmd = DT_NEXT; /* ":tnext" */
10579 break;
10580 case 'o': cmd = DT_POP; /* ":pop" */
10581 break;
10582 case 'f': /* ":tfirst" */
10583 case 'r': cmd = DT_FIRST; /* ":trewind" */
10584 break;
10585 case 'l': cmd = DT_LAST; /* ":tlast" */
10586 break;
10587 default: /* ":tag" */
10588#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010589 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010591 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592 return;
10593 }
10594#endif
10595 cmd = DT_TAG;
10596 break;
10597 }
10598
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010599 if (name[0] == 'l')
10600 {
10601#ifndef FEAT_QUICKFIX
10602 ex_ni(eap);
10603 return;
10604#else
10605 cmd = DT_LTAG;
10606#endif
10607 }
10608
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10610 eap->forceit, TRUE);
10611}
10612
10613/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010614 * Check "str" for starting with a special cmdline variable.
10615 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10616 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10617 */
10618 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010619find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010620{
10621 int len;
10622 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010623 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010624 "%",
10625#define SPEC_PERC 0
10626 "#",
10627#define SPEC_HASH 1
10628 "<cword>", /* cursor word */
10629#define SPEC_CWORD 2
10630 "<cWORD>", /* cursor WORD */
10631#define SPEC_CCWORD 3
10632 "<cfile>", /* cursor path name */
10633#define SPEC_CFILE 4
10634 "<sfile>", /* ":so" file name */
10635#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010636 "<slnum>", /* ":so" file line number */
10637#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010638#ifdef FEAT_AUTOCMD
10639 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010640# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010641 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010642# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010643 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010644# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010645#endif
10646#ifdef FEAT_CLIENTSERVER
10647 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010648# ifdef FEAT_AUTOCMD
10649# define SPEC_CLIENT 10
10650# else
10651# define SPEC_CLIENT 7
10652# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010653#endif
10654 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010655
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010656 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010657 {
10658 len = (int)STRLEN(spec_str[i]);
10659 if (STRNCMP(src, spec_str[i], len) == 0)
10660 {
10661 *usedlen = len;
10662 return i;
10663 }
10664 }
10665 return -1;
10666}
10667
10668/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669 * Evaluate cmdline variables.
10670 *
10671 * change '%' to curbuf->b_ffname
10672 * '#' to curwin->w_altfile
10673 * '<cword>' to word under the cursor
10674 * '<cWORD>' to WORD under the cursor
10675 * '<cfile>' to path name under the cursor
10676 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010677 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678 * '<afile>' to file name for autocommand
10679 * '<abuf>' to buffer number for autocommand
10680 * '<amatch>' to matching name for autocommand
10681 *
10682 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10683 * "" for error without a message) and NULL is returned.
10684 * Returns an allocated string if a valid match was found.
10685 * Returns NULL if no match was found. "usedlen" then still contains the
10686 * number of characters to skip.
10687 */
10688 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010689eval_vars(
10690 char_u *src, /* pointer into commandline */
10691 char_u *srcstart, /* beginning of valid memory for src */
10692 int *usedlen, /* characters after src that are used */
10693 linenr_T *lnump, /* line number for :e command, or NULL */
10694 char_u **errormsg, /* pointer to error message */
10695 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010696 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697{
10698 int i;
10699 char_u *s;
10700 char_u *result;
10701 char_u *resultbuf = NULL;
10702 int resultlen;
10703 buf_T *buf;
10704 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10705 int spec_idx;
10706#ifdef FEAT_MODIFY_FNAME
10707 int skip_mod = FALSE;
10708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710
10711 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010712 if (escaped != NULL)
10713 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714
10715 /*
10716 * Check if there is something to do.
10717 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010718 spec_idx = find_cmdline_var(src, usedlen);
10719 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720 {
10721 *usedlen = 1;
10722 return NULL;
10723 }
10724
10725 /*
10726 * Skip when preceded with a backslash "\%" and "\#".
10727 * Note: In "\\%" the % is also not recognized!
10728 */
10729 if (src > srcstart && src[-1] == '\\')
10730 {
10731 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010732 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733 return NULL;
10734 }
10735
10736 /*
10737 * word or WORD under cursor
10738 */
10739 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10740 {
10741 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10742 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10743 if (resultlen == 0)
10744 {
10745 *errormsg = (char_u *)"";
10746 return NULL;
10747 }
10748 }
10749
10750 /*
10751 * '#': Alternate file name
10752 * '%': Current file name
10753 * File name under the cursor
10754 * File name for autocommand
10755 * and following modifiers
10756 */
10757 else
10758 {
10759 switch (spec_idx)
10760 {
10761 case SPEC_PERC: /* '%': current file */
10762 if (curbuf->b_fname == NULL)
10763 {
10764 result = (char_u *)"";
10765 valid = 0; /* Must have ":p:h" to be valid */
10766 }
10767 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769 break;
10770
10771 case SPEC_HASH: /* '#' or "#99": alternate file */
10772 if (src[1] == '#') /* "##": the argument list */
10773 {
10774 result = arg_all();
10775 resultbuf = result;
10776 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010777 if (escaped != NULL)
10778 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779#ifdef FEAT_MODIFY_FNAME
10780 skip_mod = TRUE;
10781#endif
10782 break;
10783 }
10784 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010785 if (*s == '<') /* "#<99" uses v:oldfiles */
10786 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787 i = (int)getdigits(&s);
10788 *usedlen = (int)(s - src); /* length of what we expand */
10789
Bram Moolenaard812df62008-11-09 12:46:09 +000010790 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010792 if (*usedlen < 2)
10793 {
10794 /* Should we give an error message for #<text? */
10795 *usedlen = 1;
10796 return NULL;
10797 }
10798#ifdef FEAT_EVAL
10799 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10800 (long)i);
10801 if (result == NULL)
10802 {
10803 *errormsg = (char_u *)"";
10804 return NULL;
10805 }
10806#else
10807 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810 }
10811 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010812 {
10813 buf = buflist_findnr(i);
10814 if (buf == NULL)
10815 {
10816 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10817 return NULL;
10818 }
10819 if (lnump != NULL)
10820 *lnump = ECMD_LAST;
10821 if (buf->b_fname == NULL)
10822 {
10823 result = (char_u *)"";
10824 valid = 0; /* Must have ":p:h" to be valid */
10825 }
10826 else
10827 result = buf->b_fname;
10828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829 break;
10830
10831#ifdef FEAT_SEARCHPATH
10832 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010833 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 if (result == NULL)
10835 {
10836 *errormsg = (char_u *)"";
10837 return NULL;
10838 }
10839 resultbuf = result; /* remember allocated string */
10840 break;
10841#endif
10842
10843#ifdef FEAT_AUTOCMD
10844 case SPEC_AFILE: /* file name for autocommand */
10845 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010846 if (result != NULL && !autocmd_fname_full)
10847 {
10848 /* Still need to turn the fname into a full path. It is
10849 * postponed to avoid a delay when <afile> is not used. */
10850 autocmd_fname_full = TRUE;
10851 result = FullName_save(autocmd_fname, FALSE);
10852 vim_free(autocmd_fname);
10853 autocmd_fname = result;
10854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 if (result == NULL)
10856 {
10857 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10858 return NULL;
10859 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010860 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861 break;
10862
10863 case SPEC_ABUF: /* buffer number for autocommand */
10864 if (autocmd_bufnr <= 0)
10865 {
10866 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10867 return NULL;
10868 }
10869 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10870 result = strbuf;
10871 break;
10872
10873 case SPEC_AMATCH: /* match name for autocommand */
10874 result = autocmd_match;
10875 if (result == NULL)
10876 {
10877 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10878 return NULL;
10879 }
10880 break;
10881
10882#endif
10883 case SPEC_SFILE: /* file name for ":so" command */
10884 result = sourcing_name;
10885 if (result == NULL)
10886 {
10887 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10888 return NULL;
10889 }
10890 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010891 case SPEC_SLNUM: /* line in file for ":so" command */
10892 if (sourcing_name == NULL || sourcing_lnum == 0)
10893 {
10894 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10895 return NULL;
10896 }
10897 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10898 result = strbuf;
10899 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900#if defined(FEAT_CLIENTSERVER)
10901 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010902 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10903 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904 result = strbuf;
10905 break;
10906#endif
10907 }
10908
10909 resultlen = (int)STRLEN(result); /* length of new string */
10910 if (src[*usedlen] == '<') /* remove the file name extension */
10911 {
10912 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 resultlen = (int)(s - result);
10915 }
10916#ifdef FEAT_MODIFY_FNAME
10917 else if (!skip_mod)
10918 {
10919 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10920 &resultlen);
10921 if (result == NULL)
10922 {
10923 *errormsg = (char_u *)"";
10924 return NULL;
10925 }
10926 }
10927#endif
10928 }
10929
10930 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10931 {
10932 if (valid != VALID_HEAD + VALID_PATH)
10933 /* xgettext:no-c-format */
10934 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10935 else
10936 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10937 result = NULL;
10938 }
10939 else
10940 result = vim_strnsave(result, resultlen);
10941 vim_free(resultbuf);
10942 return result;
10943}
10944
10945/*
10946 * Concatenate all files in the argument list, separated by spaces, and return
10947 * it in one allocated string.
10948 * Spaces and backslashes in the file names are escaped with a backslash.
10949 * Returns NULL when out of memory.
10950 */
10951 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010952arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953{
10954 int len;
10955 int idx;
10956 char_u *retval = NULL;
10957 char_u *p;
10958
10959 /*
10960 * Do this loop two times:
10961 * first time: compute the total length
10962 * second time: concatenate the names
10963 */
10964 for (;;)
10965 {
10966 len = 0;
10967 for (idx = 0; idx < ARGCOUNT; ++idx)
10968 {
10969 p = alist_name(&ARGLIST[idx]);
10970 if (p != NULL)
10971 {
10972 if (len > 0)
10973 {
10974 /* insert a space in between names */
10975 if (retval != NULL)
10976 retval[len] = ' ';
10977 ++len;
10978 }
10979 for ( ; *p != NUL; ++p)
10980 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010981 if (*p == ' '
10982#ifndef BACKSLASH_IN_FILENAME
10983 || *p == '\\'
10984#endif
10985 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986 {
10987 /* insert a backslash */
10988 if (retval != NULL)
10989 retval[len] = '\\';
10990 ++len;
10991 }
10992 if (retval != NULL)
10993 retval[len] = *p;
10994 ++len;
10995 }
10996 }
10997 }
10998
10999 /* second time: break here */
11000 if (retval != NULL)
11001 {
11002 retval[len] = NUL;
11003 break;
11004 }
11005
11006 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000011007 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008 if (retval == NULL)
11009 break;
11010 }
11011
11012 return retval;
11013}
11014
11015#if defined(FEAT_AUTOCMD) || defined(PROTO)
11016/*
11017 * Expand the <sfile> string in "arg".
11018 *
11019 * Returns an allocated string, or NULL for any error.
11020 */
11021 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011022expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023{
11024 char_u *errormsg;
11025 int len;
11026 char_u *result;
11027 char_u *newres;
11028 char_u *repl;
11029 int srclen;
11030 char_u *p;
11031
11032 result = vim_strsave(arg);
11033 if (result == NULL)
11034 return NULL;
11035
11036 for (p = result; *p; )
11037 {
11038 if (STRNCMP(p, "<sfile>", 7) != 0)
11039 ++p;
11040 else
11041 {
11042 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000011043 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011044 if (errormsg != NULL)
11045 {
11046 if (*errormsg)
11047 emsg(errormsg);
11048 vim_free(result);
11049 return NULL;
11050 }
11051 if (repl == NULL) /* no match (cannot happen) */
11052 {
11053 p += srclen;
11054 continue;
11055 }
11056 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11057 newres = alloc(len);
11058 if (newres == NULL)
11059 {
11060 vim_free(repl);
11061 vim_free(result);
11062 return NULL;
11063 }
11064 mch_memmove(newres, result, (size_t)(p - result));
11065 STRCPY(newres + (p - result), repl);
11066 len = (int)STRLEN(newres);
11067 STRCAT(newres, p + srclen);
11068 vim_free(repl);
11069 vim_free(result);
11070 result = newres;
11071 p = newres + len; /* continue after the match */
11072 }
11073 }
11074
11075 return result;
11076}
11077#endif
11078
11079#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011080static int ses_winsizes(FILE *fd, int restore_size,
11081 win_T *tab_firstwin);
11082static int ses_win_rec(FILE *fd, frame_T *fr);
11083static frame_T *ses_skipframe(frame_T *fr);
11084static int ses_do_frame(frame_T *fr);
11085static int ses_do_win(win_T *wp);
11086static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11087static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
11088static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011089
11090/*
11091 * Write openfile commands for the current buffers to an .exrc file.
11092 * Return FAIL on error, OK otherwise.
11093 */
11094 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011095makeopens(
11096 FILE *fd,
11097 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098{
11099 buf_T *buf;
11100 int only_save_windows = TRUE;
11101 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102 int restore_size = TRUE;
11103 win_T *wp;
11104 char_u *sname;
11105 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011106 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011107 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011108 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011109 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011110 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011111 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112
11113 if (ssop_flags & SSOP_BUFFERS)
11114 only_save_windows = FALSE; /* Save ALL buffers */
11115
11116 /*
11117 * Begin by setting the this_session variable, and then other
11118 * sessionable variables.
11119 */
11120#ifdef FEAT_EVAL
11121 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11122 return FAIL;
11123 if (ssop_flags & SSOP_GLOBALS)
11124 if (store_session_globals(fd) == FAIL)
11125 return FAIL;
11126#endif
11127
11128 /*
11129 * Close all windows but one.
11130 */
11131 if (put_line(fd, "silent only") == FAIL)
11132 return FAIL;
11133
11134 /*
11135 * Now a :cd command to the session directory or the current directory
11136 */
11137 if (ssop_flags & SSOP_SESDIR)
11138 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011139 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11140 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141 return FAIL;
11142 }
11143 else if (ssop_flags & SSOP_CURDIR)
11144 {
11145 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11146 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011147 || fputs("cd ", fd) < 0
11148 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11149 || put_eol(fd) == FAIL)
11150 {
11151 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011152 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011154 vim_free(sname);
11155 }
11156
11157 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011158 * If there is an empty, unnamed buffer we will wipe it out later.
11159 * Remember the buffer number.
11160 */
11161 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11162 return FAIL;
11163 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11164 return FAIL;
11165 if (put_line(fd, "endif") == FAIL)
11166 return FAIL;
11167
11168 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169 * Now save the current files, current buffer first.
11170 */
11171 if (put_line(fd, "set shortmess=aoO") == FAIL)
11172 return FAIL;
11173
11174 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011175 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176 {
11177 if (!(only_save_windows && buf->b_nwindows == 0)
11178 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
11179 && buf->b_fname != NULL
11180 && buf->b_p_bl)
11181 {
11182 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11183 : buf->b_wininfo->wi_fpos.lnum) < 0
11184 || ses_fname(fd, buf, &ssop_flags) == FAIL)
11185 return FAIL;
11186 }
11187 }
11188
11189 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011190 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011191 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11192 return FAIL;
11193
11194 if (ssop_flags & SSOP_RESIZE)
11195 {
11196 /* Note: after the restore we still check it worked!*/
11197 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11198 || put_eol(fd) == FAIL)
11199 return FAIL;
11200 }
11201
11202#ifdef FEAT_GUI
11203 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11204 {
11205 int x, y;
11206
11207 if (gui_mch_get_winpos(&x, &y) == OK)
11208 {
11209 /* Note: after the restore we still check it worked!*/
11210 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11211 return FAIL;
11212 }
11213 }
11214#endif
11215
11216 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011217 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11218 * will be displayed when creating the next tab. That resizes the windows
11219 * in the first tab, which may cause problems. Set 'showtabline' to 2
11220 * temporarily to avoid that.
11221 */
11222 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11223 {
11224 if (put_line(fd, "set stal=2") == FAIL)
11225 return FAIL;
11226 restore_stal = TRUE;
11227 }
11228
11229 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011230 * May repeat putting Windows for each tab, when "tabpages" is in
11231 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011232 * Don't use goto_tabpage(), it may change directory and trigger
11233 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011235 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011236 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011237 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011239 int need_tabnew = FALSE;
11240 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011241
Bram Moolenaar18144c82006-04-12 21:52:12 +000011242 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011243 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011244 tabpage_T *tp = find_tabpage(tabnr);
11245
11246 if (tp == NULL)
11247 break; /* done all tab pages */
11248 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011249 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011250 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011251 tab_topframe = topframe;
11252 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011253 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011254 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011255 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011256 tab_topframe = tp->tp_topframe;
11257 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011258 if (tabnr > 1)
11259 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261
Bram Moolenaar18144c82006-04-12 21:52:12 +000011262 /*
11263 * Before creating the window layout, try loading one file. If this
11264 * is aborted we don't end up with a number of useless windows.
11265 * This may have side effects! (e.g., compressed or network file).
11266 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011267 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011268 {
11269 if (ses_do_win(wp)
11270 && wp->w_buffer->b_ffname != NULL
11271 && !wp->w_buffer->b_help
11272#ifdef FEAT_QUICKFIX
11273 && !bt_nofile(wp->w_buffer)
11274#endif
11275 )
11276 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011277 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000011278 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
11279 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011280 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011281 if (!wp->w_arg_idx_invalid)
11282 edited_win = wp;
11283 break;
11284 }
11285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011287 /* If no file got edited create an empty tab page. */
11288 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11289 return FAIL;
11290
Bram Moolenaar18144c82006-04-12 21:52:12 +000011291 /*
11292 * Save current window layout.
11293 */
11294 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011296 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011298 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11299 return FAIL;
11300 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11301 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302
Bram Moolenaar18144c82006-04-12 21:52:12 +000011303 /*
11304 * Check if window sizes can be restored (no windows omitted).
11305 * Remember the window number of the current window after restoring.
11306 */
11307 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011308 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011309 {
11310 if (ses_do_win(wp))
11311 ++nr;
11312 else
11313 restore_size = FALSE;
11314 if (curwin == wp)
11315 cnr = nr;
11316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317
Bram Moolenaar18144c82006-04-12 21:52:12 +000011318 /* Go to the first window. */
11319 if (put_line(fd, "wincmd t") == FAIL)
11320 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011321
Bram Moolenaar18144c82006-04-12 21:52:12 +000011322 /*
11323 * If more than one window, see if sizes can be restored.
11324 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11325 * resized when moving between windows.
11326 * Do this before restoring the view, so that the topline and the
11327 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011328 * winminheight and winminwidth need to be set to avoid an error if the
11329 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011330 */
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011331 if (put_line(fd, "set winminheight=1 winheight=1 winminwidth=1 winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011332 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011333 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011334 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335
Bram Moolenaar18144c82006-04-12 21:52:12 +000011336 /*
11337 * Restore the view of the window (options, file, cursor, etc.).
11338 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011339 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011340 {
11341 if (!ses_do_win(wp))
11342 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011343 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11344 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011345 return FAIL;
11346 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11347 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011348 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011349 }
11350
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011351 /* The argument index in the first tab page is zero, need to set it in
11352 * each window. For further tab pages it's the window where we do
11353 * "tabedit". */
11354 cur_arg_idx = next_arg_idx;
11355
Bram Moolenaar18144c82006-04-12 21:52:12 +000011356 /*
11357 * Restore cursor to the current window if it's not the first one.
11358 */
11359 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11360 || put_eol(fd) == FAIL))
11361 return FAIL;
11362
11363 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011364 * Restore window sizes again after jumping around in windows, because
11365 * the current window has a minimum size while others may not.
11366 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011367 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011368 return FAIL;
11369
Bram Moolenaar18144c82006-04-12 21:52:12 +000011370 /* Don't continue in another tab page when doing only the current one
11371 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011372 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011373 break;
11374 }
11375
11376 if (ssop_flags & SSOP_TABPAGES)
11377 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011378 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11379 || put_eol(fd) == FAIL)
11380 return FAIL;
11381 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011382 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11383 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011384
Bram Moolenaar9c102382006-05-03 21:26:49 +000011385 /*
11386 * Wipe out an empty unnamed buffer we started in.
11387 */
11388 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11389 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011390 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011391 return FAIL;
11392 if (put_line(fd, "endif") == FAIL)
11393 return FAIL;
11394 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11395 return FAIL;
11396
11397 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11398 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11399 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11400 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011401 /* Re-apply 'winminheight' and 'winminwidth'. */
11402 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11403 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11404 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011405
11406 /*
11407 * Lastly, execute the x.vim file if it exists.
11408 */
11409 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11410 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011411 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412 || put_line(fd, "endif") == FAIL)
11413 return FAIL;
11414
11415 return OK;
11416}
11417
11418 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011419ses_winsizes(
11420 FILE *fd,
11421 int restore_size,
11422 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423{
11424 int n = 0;
11425 win_T *wp;
11426
11427 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11428 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011429 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430 {
11431 if (!ses_do_win(wp))
11432 continue;
11433 ++n;
11434
11435 /* restore height when not full height */
11436 if (wp->w_height + wp->w_status_height < topframe->fr_height
11437 && (fprintf(fd,
11438 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11439 n, (long)wp->w_height, Rows / 2, Rows) < 0
11440 || put_eol(fd) == FAIL))
11441 return FAIL;
11442
11443 /* restore width when not full width */
11444 if (wp->w_width < Columns && (fprintf(fd,
11445 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11446 n, (long)wp->w_width, Columns / 2, Columns) < 0
11447 || put_eol(fd) == FAIL))
11448 return FAIL;
11449 }
11450 }
11451 else
11452 {
11453 /* Just equalise window sizes */
11454 if (put_line(fd, "wincmd =") == FAIL)
11455 return FAIL;
11456 }
11457 return OK;
11458}
11459
11460/*
11461 * Write commands to "fd" to recursively create windows for frame "fr",
11462 * horizontally and vertically split.
11463 * After the commands the last window in the frame is the current window.
11464 * Returns FAIL when writing the commands to "fd" fails.
11465 */
11466 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011467ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468{
11469 frame_T *frc;
11470 int count = 0;
11471
11472 if (fr->fr_layout != FR_LEAF)
11473 {
11474 /* Find first frame that's not skipped and then create a window for
11475 * each following one (first frame is already there). */
11476 frc = ses_skipframe(fr->fr_child);
11477 if (frc != NULL)
11478 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11479 {
11480 /* Make window as big as possible so that we have lots of room
11481 * to split. */
11482 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11483 || put_line(fd, fr->fr_layout == FR_COL
11484 ? "split" : "vsplit") == FAIL)
11485 return FAIL;
11486 ++count;
11487 }
11488
11489 /* Go back to the first window. */
11490 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11491 ? "%dwincmd k" : "%dwincmd h", count) < 0
11492 || put_eol(fd) == FAIL))
11493 return FAIL;
11494
11495 /* Recursively create frames/windows in each window of this column or
11496 * row. */
11497 frc = ses_skipframe(fr->fr_child);
11498 while (frc != NULL)
11499 {
11500 ses_win_rec(fd, frc);
11501 frc = ses_skipframe(frc->fr_next);
11502 /* Go to next window. */
11503 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11504 return FAIL;
11505 }
11506 }
11507 return OK;
11508}
11509
11510/*
11511 * Skip frames that don't contain windows we want to save in the Session.
11512 * Returns NULL when there none.
11513 */
11514 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011515ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011516{
11517 frame_T *frc;
11518
11519 for (frc = fr; frc != NULL; frc = frc->fr_next)
11520 if (ses_do_frame(frc))
11521 break;
11522 return frc;
11523}
11524
11525/*
11526 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11527 * the Session.
11528 */
11529 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011530ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011531{
11532 frame_T *frc;
11533
11534 if (fr->fr_layout == FR_LEAF)
11535 return ses_do_win(fr->fr_win);
11536 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11537 if (ses_do_frame(frc))
11538 return TRUE;
11539 return FALSE;
11540}
11541
11542/*
11543 * Return non-zero if window "wp" is to be stored in the Session.
11544 */
11545 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011546ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011547{
11548 if (wp->w_buffer->b_fname == NULL
11549#ifdef FEAT_QUICKFIX
11550 /* When 'buftype' is "nofile" can't restore the window contents. */
11551 || bt_nofile(wp->w_buffer)
11552#endif
11553 )
11554 return (ssop_flags & SSOP_BLANK);
11555 if (wp->w_buffer->b_help)
11556 return (ssop_flags & SSOP_HELP);
11557 return TRUE;
11558}
11559
11560/*
11561 * Write commands to "fd" to restore the view of a window.
11562 * Caller must make sure 'scrolloff' is zero.
11563 */
11564 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011565put_view(
11566 FILE *fd,
11567 win_T *wp,
11568 int add_edit, /* add ":edit" command to view */
11569 unsigned *flagp, /* vop_flags or ssop_flags */
11570 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011571 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011572{
11573 win_T *save_curwin;
11574 int f;
11575 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011576 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011577
11578 /* Always restore cursor position for ":mksession". For ":mkview" only
11579 * when 'viewoptions' contains "cursor". */
11580 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11581
11582 /*
11583 * Local argument list.
11584 */
11585 if (wp->w_alist == &global_alist)
11586 {
11587 if (put_line(fd, "argglobal") == FAIL)
11588 return FAIL;
11589 }
11590 else
11591 {
11592 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11593 flagp == &vop_flags
11594 || !(*flagp & SSOP_CURDIR)
11595 || wp->w_localdir != NULL, flagp) == FAIL)
11596 return FAIL;
11597 }
11598
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011599 /* Only when part of a session: restore the argument index. Some
11600 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011601 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011602 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011604 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011605 || put_eol(fd) == FAIL)
11606 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011607 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608 }
11609
11610 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011611 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612 {
11613 /*
11614 * Load the file.
11615 */
11616 if (wp->w_buffer->b_ffname != NULL
11617#ifdef FEAT_QUICKFIX
11618 && !bt_nofile(wp->w_buffer)
11619#endif
11620 )
11621 {
11622 /*
11623 * Editing a file in this buffer: use ":edit file".
11624 * This may have side effects! (e.g., compressed or network file).
11625 */
11626 if (fputs("edit ", fd) < 0
11627 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11628 return FAIL;
11629 }
11630 else
11631 {
11632 /* No file in this buffer, just make it empty. */
11633 if (put_line(fd, "enew") == FAIL)
11634 return FAIL;
11635#ifdef FEAT_QUICKFIX
11636 if (wp->w_buffer->b_ffname != NULL)
11637 {
11638 /* The buffer does have a name, but it's not a file name. */
11639 if (fputs("file ", fd) < 0
11640 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11641 return FAIL;
11642 }
11643#endif
11644 do_cursor = FALSE;
11645 }
11646 }
11647
11648 /*
11649 * Local mappings and abbreviations.
11650 */
11651 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11652 && makemap(fd, wp->w_buffer) == FAIL)
11653 return FAIL;
11654
11655 /*
11656 * Local options. Need to go to the window temporarily.
11657 * Store only local values when using ":mkview" and when ":mksession" is
11658 * used and 'sessionoptions' doesn't include "options".
11659 * Some folding options are always stored when "folds" is included,
11660 * otherwise the folds would not be restored correctly.
11661 */
11662 save_curwin = curwin;
11663 curwin = wp;
11664 curbuf = curwin->w_buffer;
11665 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11666 f = makeset(fd, OPT_LOCAL,
11667 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11668#ifdef FEAT_FOLDING
11669 else if (*flagp & SSOP_FOLDS)
11670 f = makefoldset(fd);
11671#endif
11672 else
11673 f = OK;
11674 curwin = save_curwin;
11675 curbuf = curwin->w_buffer;
11676 if (f == FAIL)
11677 return FAIL;
11678
11679#ifdef FEAT_FOLDING
11680 /*
11681 * Save Folds when 'buftype' is empty and for help files.
11682 */
11683 if ((*flagp & SSOP_FOLDS)
11684 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011685# ifdef FEAT_QUICKFIX
11686 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11687# endif
11688 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011689 {
11690 if (put_folds(fd, wp) == FAIL)
11691 return FAIL;
11692 }
11693#endif
11694
11695 /*
11696 * Set the cursor after creating folds, since that moves the cursor.
11697 */
11698 if (do_cursor)
11699 {
11700
11701 /* Restore the cursor line in the file and relatively in the
11702 * window. Don't use "G", it changes the jumplist. */
11703 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11704 (long)wp->w_cursor.lnum,
11705 (long)(wp->w_cursor.lnum - wp->w_topline),
11706 (long)wp->w_height / 2, (long)wp->w_height) < 0
11707 || put_eol(fd) == FAIL
11708 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11709 || put_line(fd, "exe s:l") == FAIL
11710 || put_line(fd, "normal! zt") == FAIL
11711 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11712 || put_eol(fd) == FAIL)
11713 return FAIL;
11714 /* Restore the cursor column and left offset when not wrapping. */
11715 if (wp->w_cursor.col == 0)
11716 {
11717 if (put_line(fd, "normal! 0") == FAIL)
11718 return FAIL;
11719 }
11720 else
11721 {
11722 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11723 {
11724 if (fprintf(fd,
11725 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011726 (long)wp->w_virtcol + 1,
11727 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011728 (long)wp->w_width / 2, (long)wp->w_width) < 0
11729 || put_eol(fd) == FAIL
11730 || put_line(fd, "if s:c > 0") == FAIL
11731 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011732 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11733 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011734 || put_eol(fd) == FAIL
11735 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011736 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011737 || put_eol(fd) == FAIL
11738 || put_line(fd, "endif") == FAIL)
11739 return FAIL;
11740 }
11741 else
11742 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011743 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011744 || put_eol(fd) == FAIL)
11745 return FAIL;
11746 }
11747 }
11748 }
11749
11750 /*
11751 * Local directory.
11752 */
11753 if (wp->w_localdir != NULL)
11754 {
11755 if (fputs("lcd ", fd) < 0
11756 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11757 || put_eol(fd) == FAIL)
11758 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011759 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011760 }
11761
11762 return OK;
11763}
11764
11765/*
11766 * Write an argument list to the session file.
11767 * Returns FAIL if writing fails.
11768 */
11769 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011770ses_arglist(
11771 FILE *fd,
11772 char *cmd,
11773 garray_T *gap,
11774 int fullname, /* TRUE: use full path name */
11775 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011776{
11777 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011778 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011779 char_u *s;
11780
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011781 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11782 return FAIL;
11783 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011784 return FAIL;
11785 for (i = 0; i < gap->ga_len; ++i)
11786 {
11787 /* NULL file names are skipped (only happens when out of memory). */
11788 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11789 if (s != NULL)
11790 {
11791 if (fullname)
11792 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011793 buf = alloc(MAXPATHL);
11794 if (buf != NULL)
11795 {
11796 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11797 s = buf;
11798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011799 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010011800 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011801 || ses_put_fname(fd, s, flagp) == FAIL
11802 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011803 {
11804 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011805 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011806 }
11807 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011808 }
11809 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011810 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811}
11812
11813/*
11814 * Write a buffer name to the session file.
11815 * Also ends the line.
11816 * Returns FAIL if writing fails.
11817 */
11818 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011819ses_fname(FILE *fd, buf_T *buf, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011820{
11821 char_u *name;
11822
11823 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011824 * the session file will be sourced.
11825 * Don't do this for ":mkview", we don't know the current directory.
11826 * Don't do this after ":lcd", we don't keep track of what the current
11827 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011828 if (buf->b_sfname != NULL
11829 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011830 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011831#ifdef FEAT_AUTOCHDIR
11832 && !p_acd
11833#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011834 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011835 name = buf->b_sfname;
11836 else
11837 name = buf->b_ffname;
11838 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11839 return FAIL;
11840 return OK;
11841}
11842
11843/*
11844 * Write a file name to the session file.
11845 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11846 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011847 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011848 */
11849 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011850ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011851{
11852 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011853 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855
11856 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011857 if (sname == NULL)
11858 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011860 if (*flagp & SSOP_SLASH)
11861 {
11862 /* change all backslashes to forward slashes */
11863 for (p = sname; *p != NUL; mb_ptr_adv(p))
11864 if (*p == '\\')
11865 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011866 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011867
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011868 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011869 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011870 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011871 if (p == NULL)
11872 return FAIL;
11873
11874 /* write the result */
11875 if (fputs((char *)p, fd) < 0)
11876 retval = FAIL;
11877
11878 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011879 return retval;
11880}
11881
11882/*
11883 * ":loadview [nr]"
11884 */
11885 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011886ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011887{
11888 char_u *fname;
11889
11890 fname = get_view_file(*eap->arg);
11891 if (fname != NULL)
11892 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011893 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011894 vim_free(fname);
11895 }
11896}
11897
11898/*
11899 * Get the name of the view file for the current buffer.
11900 */
11901 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011902get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011903{
11904 int len = 0;
11905 char_u *p, *s;
11906 char_u *retval;
11907 char_u *sname;
11908
11909 if (curbuf->b_ffname == NULL)
11910 {
11911 EMSG(_(e_noname));
11912 return NULL;
11913 }
11914 sname = home_replace_save(NULL, curbuf->b_ffname);
11915 if (sname == NULL)
11916 return NULL;
11917
11918 /*
11919 * We want a file name without separators, because we're not going to make
11920 * a directory.
11921 * "normal" path separator -> "=+"
11922 * "=" -> "=="
11923 * ":" path separator -> "=-"
11924 */
11925 for (p = sname; *p; ++p)
11926 if (*p == '=' || vim_ispathsep(*p))
11927 ++len;
11928 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11929 if (retval != NULL)
11930 {
11931 STRCPY(retval, p_vdir);
11932 add_pathsep(retval);
11933 s = retval + STRLEN(retval);
11934 for (p = sname; *p; ++p)
11935 {
11936 if (*p == '=')
11937 {
11938 *s++ = '=';
11939 *s++ = '=';
11940 }
11941 else if (vim_ispathsep(*p))
11942 {
11943 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011944#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011945 if (*p == ':')
11946 *s++ = '-';
11947 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011948#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011949 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950 }
11951 else
11952 *s++ = *p;
11953 }
11954 *s++ = '=';
11955 *s++ = c;
11956 STRCPY(s, ".vim");
11957 }
11958
11959 vim_free(sname);
11960 return retval;
11961}
11962
11963#endif /* FEAT_SESSION */
11964
11965/*
11966 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11967 * Return FAIL for a write error.
11968 */
11969 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011970put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971{
11972 if (
11973#ifdef USE_CRNL
11974 (
11975# ifdef MKSESSION_NL
11976 !mksession_nl &&
11977# endif
11978 (putc('\r', fd) < 0)) ||
11979#endif
11980 (putc('\n', fd) < 0))
11981 return FAIL;
11982 return OK;
11983}
11984
11985/*
11986 * Write a line to "fd".
11987 * Return FAIL for a write error.
11988 */
11989 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011990put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011991{
11992 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11993 return FAIL;
11994 return OK;
11995}
11996
11997#ifdef FEAT_VIMINFO
11998/*
11999 * ":rviminfo" and ":wviminfo".
12000 */
12001 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012002ex_viminfo(
12003 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004{
12005 char_u *save_viminfo;
12006
12007 save_viminfo = p_viminfo;
12008 if (*p_viminfo == NUL)
12009 p_viminfo = (char_u *)"'100";
12010 if (eap->cmdidx == CMD_rviminfo)
12011 {
Bram Moolenaard812df62008-11-09 12:46:09 +000012012 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
12013 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012014 EMSG(_("E195: Cannot open viminfo file for reading"));
12015 }
12016 else
12017 write_viminfo(eap->arg, eap->forceit);
12018 p_viminfo = save_viminfo;
12019}
12020#endif
12021
12022#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012023/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020012024 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000012025 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012026 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012027 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012028dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012030 if (fname == NULL)
12031 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012032 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033}
12034#endif
12035
12036/*
12037 * ":behave {mswin,xterm}"
12038 */
12039 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012040ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041{
12042 if (STRCMP(eap->arg, "mswin") == 0)
12043 {
12044 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12045 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12046 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12047 set_option_value((char_u *)"keymodel", 0L,
12048 (char_u *)"startsel,stopsel", 0);
12049 }
12050 else if (STRCMP(eap->arg, "xterm") == 0)
12051 {
12052 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12053 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12054 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12055 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12056 }
12057 else
12058 EMSG2(_(e_invarg2), eap->arg);
12059}
12060
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012061#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12062/*
12063 * Function given to ExpandGeneric() to obtain the possible arguments of the
12064 * ":behave {mswin,xterm}" command.
12065 */
12066 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012067get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012068{
12069 if (idx == 0)
12070 return (char_u *)"mswin";
12071 if (idx == 1)
12072 return (char_u *)"xterm";
12073 return NULL;
12074}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012075
12076/*
12077 * Function given to ExpandGeneric() to obtain the possible arguments of the
12078 * ":messages {clear}" command.
12079 */
12080 char_u *
12081get_messages_arg(expand_T *xp UNUSED, int idx)
12082{
12083 if (idx == 0)
12084 return (char_u *)"clear";
12085 return NULL;
12086}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012087#endif
12088
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089#ifdef FEAT_AUTOCMD
12090static int filetype_detect = FALSE;
12091static int filetype_plugin = FALSE;
12092static int filetype_indent = FALSE;
12093
12094/*
12095 * ":filetype [plugin] [indent] {on,off,detect}"
12096 * on: Load the filetype.vim file to install autocommands for file types.
12097 * off: Load the ftoff.vim file to remove all autocommands for file types.
12098 * plugin on: load filetype.vim and ftplugin.vim
12099 * plugin off: load ftplugof.vim
12100 * indent on: load filetype.vim and indent.vim
12101 * indent off: load indoff.vim
12102 */
12103 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012104ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012105{
12106 char_u *arg = eap->arg;
12107 int plugin = FALSE;
12108 int indent = FALSE;
12109
12110 if (*eap->arg == NUL)
12111 {
12112 /* Print current status. */
12113 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
12114 filetype_detect ? "ON" : "OFF",
12115 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12116 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12117 return;
12118 }
12119
12120 /* Accept "plugin" and "indent" in any order. */
12121 for (;;)
12122 {
12123 if (STRNCMP(arg, "plugin", 6) == 0)
12124 {
12125 plugin = TRUE;
12126 arg = skipwhite(arg + 6);
12127 continue;
12128 }
12129 if (STRNCMP(arg, "indent", 6) == 0)
12130 {
12131 indent = TRUE;
12132 arg = skipwhite(arg + 6);
12133 continue;
12134 }
12135 break;
12136 }
12137 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12138 {
12139 if (*arg == 'o' || !filetype_detect)
12140 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012141 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012142 filetype_detect = TRUE;
12143 if (plugin)
12144 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012145 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012146 filetype_plugin = TRUE;
12147 }
12148 if (indent)
12149 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012150 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151 filetype_indent = TRUE;
12152 }
12153 }
12154 if (*arg == 'd')
12155 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012156 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012157 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158 }
12159 }
12160 else if (STRCMP(arg, "off") == 0)
12161 {
12162 if (plugin || indent)
12163 {
12164 if (plugin)
12165 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012166 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012167 filetype_plugin = FALSE;
12168 }
12169 if (indent)
12170 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012171 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012172 filetype_indent = FALSE;
12173 }
12174 }
12175 else
12176 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012177 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012178 filetype_detect = FALSE;
12179 }
12180 }
12181 else
12182 EMSG2(_(e_invarg2), arg);
12183}
12184
12185/*
12186 * ":setfiletype {name}"
12187 */
12188 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012189ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012190{
12191 if (!did_filetype)
12192 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
12193}
12194#endif
12195
Bram Moolenaar071d4272004-06-13 20:20:40 +000012196 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012197ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198{
12199#ifdef FEAT_DIGRAPHS
12200 if (*eap->arg != NUL)
12201 putdigraph(eap->arg);
12202 else
12203 listdigraphs();
12204#else
12205 EMSG(_("E196: No digraphs in this version"));
12206#endif
12207}
12208
12209 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012210ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012211{
12212 int flags = 0;
12213
12214 if (eap->cmdidx == CMD_setlocal)
12215 flags = OPT_LOCAL;
12216 else if (eap->cmdidx == CMD_setglobal)
12217 flags = OPT_GLOBAL;
12218#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
12219 if (cmdmod.browse && flags == 0)
12220 ex_options(eap);
12221 else
12222#endif
12223 (void)do_set(eap->arg, flags);
12224}
12225
12226#ifdef FEAT_SEARCH_EXTRA
12227/*
12228 * ":nohlsearch"
12229 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012230 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012231ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010012233 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012234 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235}
12236
12237/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012238 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012239 * Sets nextcmd to the start of the next command, if any. Also called when
12240 * skipping commands to find the next command.
12241 */
12242 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012243ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012244{
12245 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012246 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012247 char_u *end;
12248 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012249 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012250
12251 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012252 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012253 else
12254 {
12255 EMSG(e_invcmd);
12256 return;
12257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012258
12259 /* First clear any old pattern. */
12260 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012261 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262
12263 if (ends_excmd(*eap->arg))
12264 end = eap->arg;
12265 else if ((STRNICMP(eap->arg, "none", 4) == 0
12266 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
12267 end = eap->arg + 4;
12268 else
12269 {
12270 p = skiptowhite(eap->arg);
12271 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012272 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012273 p = skipwhite(p);
12274 if (*p == NUL)
12275 {
12276 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012277 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012278 EMSG2(_(e_invarg2), eap->arg);
12279 return;
12280 }
12281 end = skip_regexp(p + 1, *p, TRUE, NULL);
12282 if (!eap->skip)
12283 {
12284 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12285 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012286 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287 eap->errmsg = e_trailing;
12288 return;
12289 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012290 if (*end != *p)
12291 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012292 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012293 EMSG2(_(e_invarg2), p);
12294 return;
12295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012296
12297 c = *end;
12298 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012299 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012300 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012301 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012302 }
12303 }
12304 eap->nextcmd = find_nextcmd(end);
12305}
12306#endif
12307
12308#ifdef FEAT_CRYPT
12309/*
12310 * ":X": Get crypt key
12311 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012313ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012314{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012315 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012316 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012317}
12318#endif
12319
12320#ifdef FEAT_FOLDING
12321 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012322ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323{
12324 if (foldManualAllowed(TRUE))
12325 foldCreate(eap->line1, eap->line2);
12326}
12327
12328 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012329ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330{
12331 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12332 eap->forceit, FALSE);
12333}
12334
12335 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012336ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337{
12338 linenr_T lnum;
12339
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012340#ifdef FEAT_CLIPBOARD
12341 start_global_changes();
12342#endif
12343
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344 /* First set the marks for all lines closed/open. */
12345 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12346 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12347 ml_setmarked(lnum);
12348
12349 /* Execute the command on the marked lines. */
12350 global_exe(eap->arg);
12351 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012352#ifdef FEAT_CLIPBOARD
12353 end_global_changes();
12354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012355}
12356#endif