blob: 506a8088d9fd9e2eef2df60c6e9c2a96f5987c39 [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
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200491#ifndef FEAT_TERMINAL
492# define ex_terminal ex_ni
493#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000494
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495/*
496 * Declare cmdnames[].
497 */
498#define DO_DECLARE_EXCMD
499#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200500#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100501
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502static char_u dollar_command[2] = {'$', 0};
503
504
505#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000506/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507typedef struct
508{
509 char_u *line; /* command line */
510 linenr_T lnum; /* sourcing_lnum of the line */
511} wcmd_T;
512
513/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000514 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000516 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000518struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519{
520 garray_T *lines_gap; /* growarray with line info */
521 int current_line; /* last read line from growarray */
522 int repeating; /* TRUE when looping a second time */
523 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100524 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 void *cookie;
526};
527
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100528static char_u *get_loop_line(int c, void *cookie, int indent);
529static int store_loop_line(garray_T *gap, char_u *line);
530static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000531
532/* Struct to save a few things while debugging. Used in do_cmdline() only. */
533struct dbg_stuff
534{
535 int trylevel;
536 int force_abort;
537 except_T *caught_stack;
538 char_u *vv_exception;
539 char_u *vv_throwpoint;
540 int did_emsg;
541 int got_int;
542 int did_throw;
543 int need_rethrow;
544 int check_cstack;
545 except_T *current_exception;
546};
547
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100548static void save_dbg_stuff(struct dbg_stuff *dsp);
549static void restore_dbg_stuff(struct dbg_stuff *dsp);
Bram Moolenaared203462004-06-16 11:19:22 +0000550
551 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100552save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000553{
554 dsp->trylevel = trylevel; trylevel = 0;
555 dsp->force_abort = force_abort; force_abort = FALSE;
556 dsp->caught_stack = caught_stack; caught_stack = NULL;
557 dsp->vv_exception = v_exception(NULL);
558 dsp->vv_throwpoint = v_throwpoint(NULL);
559
560 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
561 dsp->did_emsg = did_emsg; did_emsg = FALSE;
562 dsp->got_int = got_int; got_int = FALSE;
563 dsp->did_throw = did_throw; did_throw = FALSE;
564 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
565 dsp->check_cstack = check_cstack; check_cstack = FALSE;
566 dsp->current_exception = current_exception; current_exception = NULL;
567}
568
569 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100570restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000571{
572 suppress_errthrow = FALSE;
573 trylevel = dsp->trylevel;
574 force_abort = dsp->force_abort;
575 caught_stack = dsp->caught_stack;
576 (void)v_exception(dsp->vv_exception);
577 (void)v_throwpoint(dsp->vv_throwpoint);
578 did_emsg = dsp->did_emsg;
579 got_int = dsp->got_int;
580 did_throw = dsp->did_throw;
581 need_rethrow = dsp->need_rethrow;
582 check_cstack = dsp->check_cstack;
583 current_exception = dsp->current_exception;
584}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585#endif
586
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587/*
588 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
589 * command is given.
590 */
591 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100592do_exmode(
593 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594{
595 int save_msg_scroll;
596 int prev_msg_row;
597 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100598 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000599
600 if (improved)
601 exmode_active = EXMODE_VIM;
602 else
603 exmode_active = EXMODE_NORMAL;
604 State = NORMAL;
605
606 /* When using ":global /pat/ visual" and then "Q" we return to continue
607 * the :global command. */
608 if (global_busy)
609 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
611 save_msg_scroll = msg_scroll;
612 ++RedrawingDisabled; /* don't redisplay the window */
613 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614#ifdef FEAT_GUI
615 /* Ignore scrollbar and mouse events in Ex mode */
616 ++hold_gui_events;
617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618
619 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
620 while (exmode_active)
621 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000622 /* Check for a ":normal" command and no more characters left. */
623 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
624 {
625 exmode_active = FALSE;
626 break;
627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 msg_scroll = TRUE;
629 need_wait_return = FALSE;
630 ex_pressedreturn = FALSE;
631 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100632 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 prev_msg_row = msg_row;
634 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 if (improved)
636 {
637 cmdline_row = msg_row;
638 do_cmdline(NULL, getexline, NULL, 0);
639 }
640 else
641 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
642 lines_left = Rows - 1;
643
Bram Moolenaardf177f62005-02-22 08:39:57 +0000644 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100645 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000647 if (curbuf->b_ml.ml_flags & ML_EMPTY)
648 EMSG(_(e_emptybuf));
649 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000651 if (ex_pressedreturn)
652 {
653 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000654 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000655 msg_row = prev_msg_row;
656 if (prev_msg_row == Rows - 1)
657 msg_row--;
658 }
659 msg_col = 0;
660 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
661 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000664 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
665 {
666 if (curbuf->b_ml.ml_flags & ML_EMPTY)
667 EMSG(_(e_emptybuf));
668 else
669 EMSG(_("E501: At end-of-file"));
670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 }
672
673#ifdef FEAT_GUI
674 --hold_gui_events;
675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 --RedrawingDisabled;
677 --no_wait_return;
678 update_screen(CLEAR);
679 need_wait_return = FALSE;
680 msg_scroll = save_msg_scroll;
681}
682
683/*
684 * Execute a simple command line. Used for translated commands like "*".
685 */
686 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100687do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688{
689 return do_cmdline(cmd, NULL, NULL,
690 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
691}
692
693/*
694 * do_cmdline(): execute one Ex command line
695 *
696 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100697 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 * 2. Split up in parts separated with '|'.
699 *
700 * This function can be called recursively!
701 *
702 * flags:
703 * DOCMD_VERBOSE - The command will be included in the error message.
704 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100705 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 * DOCMD_KEYTYPED - Don't reset KeyTyped.
707 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
708 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
709 *
710 * return FAIL if cmdline could not be executed, OK otherwise
711 */
712 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100713do_cmdline(
714 char_u *cmdline,
715 char_u *(*fgetline)(int, void *, int),
716 void *cookie, /* argument for fgetline() */
717 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718{
719 char_u *next_cmdline; /* next cmd to execute */
720 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100721 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 static int recursive = 0; /* recursive depth */
723 int msg_didout_before_start = 0;
724 int count = 0; /* line number count */
725 int did_inc = FALSE; /* incremented RedrawingDisabled */
726 int retval = OK;
727#ifdef FEAT_EVAL
728 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000729 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 int current_line = 0; /* active line in lines_ga */
731 char_u *fname = NULL; /* function or script name */
732 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
733 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000734 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 int initial_trylevel;
736 struct msglist **saved_msg_list = NULL;
737 struct msglist *private_msg_list;
738
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100739 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100740 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000742 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000744 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100746# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747# define cmd_cookie cookie
748#endif
749 static int call_depth = 0; /* recursiveness */
750
751#ifdef FEAT_EVAL
752 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
753 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000754 * This ensures that the do_errthrow() call in do_one_cmd() does not
755 * combine the messages stored by an earlier invocation of do_one_cmd()
756 * with the command name of the later one. This would happen when
757 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 saved_msg_list = msg_list;
759 msg_list = &private_msg_list;
760 private_msg_list = NULL;
761#endif
762
763 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100764 * here. The value of 200 allows nested function calls, ":source", etc.
765 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100766 if (call_depth >= 200
767#ifdef FEAT_EVAL
768 && call_depth >= p_mfd
769#endif
770 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 {
772 EMSG(_("E169: Command too recursive"));
773#ifdef FEAT_EVAL
774 /* When converting to an exception, we do not include the command name
775 * since this is not an error of the specific command. */
776 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
777 msg_list = saved_msg_list;
778#endif
779 return FAIL;
780 }
781 ++call_depth;
782
783#ifdef FEAT_EVAL
784 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000785 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 cstack.cs_trylevel = 0;
787 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000788 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
790
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100791 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792
793 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100794 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000795 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 ++ex_nesting_level;
797
798 /* Get the function or script name and the address where the next breakpoint
799 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000800 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 {
802 fname = func_name(real_cookie);
803 breakpoint = func_breakpoint(real_cookie);
804 dbg_tick = func_dbg_tick(real_cookie);
805 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100806 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 {
808 fname = sourcing_name;
809 breakpoint = source_breakpoint(real_cookie);
810 dbg_tick = source_dbg_tick(real_cookie);
811 }
812
813 /*
814 * Initialize "force_abort" and "suppress_errthrow" at the top level.
815 */
816 if (!recursive)
817 {
818 force_abort = FALSE;
819 suppress_errthrow = FALSE;
820 }
821
822 /*
823 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000824 * exception handling (used when debugging). Otherwise clear it to avoid
825 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000827 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000828 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000829 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200830 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831
832 initial_trylevel = trylevel;
833
834 /*
835 * "did_throw" will be set to TRUE when an exception is being thrown.
836 */
837 did_throw = FALSE;
838#endif
839 /*
840 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000841 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 * If force_abort is set, we cancel everything.
843 */
844 did_emsg = FALSE;
845
846 /*
847 * KeyTyped is only set when calling vgetc(). Reset it here when not
848 * calling vgetc() (sourced command lines).
849 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100850 if (!(flags & DOCMD_KEYTYPED)
851 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 KeyTyped = FALSE;
853
854 /*
855 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000856 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 * - for multiple commands on one line, separated with '|'
858 * - when repeating until there are no more lines (for ":source")
859 */
860 next_cmdline = cmdline;
861 do
862 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000863#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100864 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000865#endif
866
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000867 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 if (next_cmdline == NULL
869#ifdef FEAT_EVAL
870 && !force_abort
871 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000872 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873#endif
874 )
875 did_emsg = FALSE;
876
877 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000878 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100879 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 * 3. If a line is given: Make a copy, so we can mess with it.
881 */
882
883#ifdef FEAT_EVAL
884 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000885 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 {
887 /* Each '|' separated command is stored separately in lines_ga, to
888 * be able to jump to it. Don't use next_cmdline now. */
889 vim_free(cmdline_copy);
890 cmdline_copy = NULL;
891
892 /* Check if a function has returned or, unless it has an unclosed
893 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000894 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000896# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000897 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000898 func_line_end(real_cookie);
899# endif
900 if (func_has_ended(real_cookie))
901 {
902 retval = FAIL;
903 break;
904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000906#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000907 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100908 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000909 script_line_end();
910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911
912 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100913 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 {
915 retval = FAIL;
916 break;
917 }
918
919 /* If breakpoints have been added/deleted need to check for it. */
920 if (breakpoint != NULL && dbg_tick != NULL
921 && *dbg_tick != debug_tick)
922 {
923 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100924 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 fname, sourcing_lnum);
926 *dbg_tick = debug_tick;
927 }
928
929 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
930 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
931
932 /* Did we encounter a breakpoint? */
933 if (breakpoint != NULL && *breakpoint != 0
934 && *breakpoint <= sourcing_lnum)
935 {
936 dbg_breakpoint(fname, sourcing_lnum);
937 /* Find next breakpoint. */
938 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100939 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 fname, sourcing_lnum);
941 *dbg_tick = debug_tick;
942 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000943# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000944 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000945 {
946 if (getline_is_func)
947 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100948 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000949 script_line_start();
950 }
951# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 }
953
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000954 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000956 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100957 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 * below, so that it stores lines in or reads them from
959 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000960 * while/for loop. */
961 cmd_getline = get_loop_line;
962 cmd_cookie = (void *)&cmd_loop_cookie;
963 cmd_loop_cookie.lines_gap = &lines_ga;
964 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100965 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000966 cmd_loop_cookie.cookie = cookie;
967 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 }
969 else
970 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100971 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 cmd_cookie = cookie;
973 }
974#endif
975
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100976 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 if (next_cmdline == NULL)
978 {
979 /*
980 * Need to set msg_didout for the first line after an ":if",
981 * otherwise the ":if" will be overwritten.
982 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100983 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100985 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986#ifdef FEAT_EVAL
987 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
988#else
989 0
990#endif
991 )) == NULL)
992 {
993 /* Don't call wait_return for aborted command line. The NULL
994 * returned for the end of a sourced file or executed function
995 * doesn't do this. */
996 if (KeyTyped && !(flags & DOCMD_REPEAT))
997 need_wait_return = FALSE;
998 retval = FAIL;
999 break;
1000 }
1001 used_getline = TRUE;
1002
1003 /*
1004 * Keep the first typed line. Clear it when more lines are typed.
1005 */
1006 if (flags & DOCMD_KEEPLINE)
1007 {
1008 vim_free(repeat_cmdline);
1009 if (count == 0)
1010 repeat_cmdline = vim_strsave(next_cmdline);
1011 else
1012 repeat_cmdline = NULL;
1013 }
1014 }
1015
1016 /* 3. Make a copy of the command so we can mess with it. */
1017 else if (cmdline_copy == NULL)
1018 {
1019 next_cmdline = vim_strsave(next_cmdline);
1020 if (next_cmdline == NULL)
1021 {
1022 EMSG(_(e_outofmem));
1023 retval = FAIL;
1024 break;
1025 }
1026 }
1027 cmdline_copy = next_cmdline;
1028
1029#ifdef FEAT_EVAL
1030 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001031 * Save the current line when inside a ":while" or ":for", and when
1032 * the command looks like a ":while" or ":for", because we may need it
1033 * later. When there is a '|' and another command, it is stored
1034 * separately, because we need to be able to jump back to it from an
1035 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001037 if (current_line == lines_ga.ga_len
1038 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001040 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 {
1042 retval = FAIL;
1043 break;
1044 }
1045 }
1046 did_endif = FALSE;
1047#endif
1048
1049 if (count++ == 0)
1050 {
1051 /*
1052 * All output from the commands is put below each other, without
1053 * waiting for a return. Don't do this when executing commands
1054 * from a script or when being called recursive (e.g. for ":e
1055 * +command file").
1056 */
1057 if (!(flags & DOCMD_NOWAIT) && !recursive)
1058 {
1059 msg_didout_before_start = msg_didout;
1060 msg_didany = FALSE; /* no output yet */
1061 msg_start();
1062 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001063 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 ++RedrawingDisabled;
1065 did_inc = TRUE;
1066 }
1067 }
1068
1069 if (p_verbose >= 15 && sourcing_name != NULL)
1070 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001072 verbose_enter_scroll();
1073
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 smsg((char_u *)_("line %ld: %s"),
1075 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001076 if (msg_silent == 0)
1077 msg_puts((char_u *)"\n"); /* don't overwrite this */
1078
1079 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 --no_wait_return;
1081 }
1082
1083 /*
1084 * 2. Execute one '|' separated command.
1085 * do_one_cmd() will return NULL if there is no trailing '|'.
1086 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1087 */
1088 ++recursive;
1089 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1090#ifdef FEAT_EVAL
1091 &cstack,
1092#endif
1093 cmd_getline, cmd_cookie);
1094 --recursive;
1095
1096#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001097 if (cmd_cookie == (void *)&cmd_loop_cookie)
1098 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001100 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101#endif
1102
1103 if (next_cmdline == NULL)
1104 {
1105 vim_free(cmdline_copy);
1106 cmdline_copy = NULL;
1107#ifdef FEAT_CMDHIST
1108 /*
1109 * If the command was typed, remember it for the ':' register.
1110 * Do this AFTER executing the command to make :@: work.
1111 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001112 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 && new_last_cmdline != NULL)
1114 {
1115 vim_free(last_cmdline);
1116 last_cmdline = new_last_cmdline;
1117 new_last_cmdline = NULL;
1118 }
1119#endif
1120 }
1121 else
1122 {
1123 /* need to copy the command after the '|' to cmdline_copy, for the
1124 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001125 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 next_cmdline = cmdline_copy;
1127 }
1128
1129
1130#ifdef FEAT_EVAL
1131 /* reset did_emsg for a function that is not aborted by an error */
1132 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001133 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 && !func_has_abort(real_cookie))
1135 did_emsg = FALSE;
1136
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001137 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 {
1139 ++current_line;
1140
1141 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001142 * An ":endwhile", ":endfor" and ":continue" is handled here.
1143 * If we were executing commands, jump back to the ":while" or
1144 * ":for".
1145 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001147 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001149 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001151 /* Jump back to the matching ":while" or ":for". Be careful
1152 * not to use a cs_line[] from an entry that isn't a ":while"
1153 * or ":for": It would make "current_line" invalid and can
1154 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 if (!did_emsg && !got_int && !did_throw
1156 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001157 && (cstack.cs_flags[cstack.cs_idx]
1158 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 && cstack.cs_line[cstack.cs_idx] >= 0
1160 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1161 {
1162 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001163 /* remember we jumped there */
1164 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 line_breakcheck(); /* check if CTRL-C typed */
1166
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001167 /* Check for the next breakpoint at or after the ":while"
1168 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 if (breakpoint != NULL)
1170 {
1171 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001172 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 fname,
1174 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1175 *dbg_tick = debug_tick;
1176 }
1177 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001178 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001180 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001182 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1183 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 }
1185 }
1186
1187 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001188 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001190 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001192 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1194 }
1195 }
1196
1197 /*
1198 * When not inside any ":while" loop, clear remembered lines.
1199 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 {
1202 if (lines_ga.ga_len > 0)
1203 {
1204 sourcing_lnum =
1205 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1206 free_cmdlines(&lines_ga);
1207 }
1208 current_line = 0;
1209 }
1210
1211 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001212 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1213 * being restored at the ":endtry". Reset them here and set the
1214 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1215 * This includes the case where a missing ":endif", ":endwhile" or
1216 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001218 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001220 cstack.cs_lflags &= ~CSL_HAD_FINA;
1221 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1222 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 did_throw ? (void *)current_exception : NULL);
1224 did_emsg = got_int = did_throw = FALSE;
1225 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1226 }
1227
1228 /* Update global "trylevel" for recursive calls to do_cmdline() from
1229 * within this loop. */
1230 trylevel = initial_trylevel + cstack.cs_trylevel;
1231
1232 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001233 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 * files) is aborted because of an error, an interrupt, or an uncaught
1235 * exception, cancel everything. If it is left normally, reset
1236 * force_abort to get the non-EH compatible abortion behavior for
1237 * the rest of the script.
1238 */
1239 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1240 force_abort = FALSE;
1241
1242 /* Convert an interrupt to an exception if appropriate. */
1243 (void)do_intthrow(&cstack);
1244#endif /* FEAT_EVAL */
1245
1246 }
1247 /*
1248 * Continue executing command lines when:
1249 * - no CTRL-C typed, no aborting error, no exception thrown or try
1250 * conditionals need to be checked for executing finally clauses or
1251 * catching an interrupt exception
1252 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001253 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 * looping for ":source" command or function call.
1255 */
1256 while (!((got_int
1257#ifdef FEAT_EVAL
1258 || (did_emsg && force_abort) || did_throw
1259#endif
1260 )
1261#ifdef FEAT_EVAL
1262 && cstack.cs_trylevel == 0
1263#endif
1264 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001265 && !(did_emsg
1266#ifdef FEAT_EVAL
1267 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001268 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001269 * the :endtry to be missed. */
1270 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1271#endif
1272 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001273 && (getline_equal(fgetline, cookie, getexmodeline)
1274 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 && (next_cmdline != NULL
1276#ifdef FEAT_EVAL
1277 || cstack.cs_idx >= 0
1278#endif
1279 || (flags & DOCMD_REPEAT)));
1280
1281 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001282 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283#ifdef FEAT_EVAL
1284 free_cmdlines(&lines_ga);
1285 ga_clear(&lines_ga);
1286
1287 if (cstack.cs_idx >= 0)
1288 {
1289 /*
1290 * If a sourced file or executed function ran to its end, report the
1291 * unclosed conditional.
1292 */
1293 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001294 && ((getline_equal(fgetline, cookie, getsourceline)
1295 && !source_finished(fgetline, cookie))
1296 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 && !func_has_ended(real_cookie))))
1298 {
1299 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1300 EMSG(_(e_endtry));
1301 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1302 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001303 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1304 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 else
1306 EMSG(_(e_endif));
1307 }
1308
1309 /*
1310 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1311 * ":endtry" in a sourced file or executed function. If the try
1312 * conditional is in its finally clause, ignore anything pending.
1313 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001314 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 */
1316 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001317 {
1318 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1319
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001320 if (idx >= 0)
1321 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001322 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1323 &cstack.cs_looplevel);
1324 }
1325 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 trylevel = initial_trylevel;
1327 }
1328
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001329 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1330 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001332 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 ? (char_u *)"endfunction" : (char_u *)NULL);
1334
1335 if (trylevel == 0)
1336 {
1337 /*
1338 * When an exception is being thrown out of the outermost try
1339 * conditional, discard the uncaught exception, disable the conversion
1340 * of interrupts or errors to exceptions, and ensure that no more
1341 * commands are executed.
1342 */
1343 if (did_throw)
1344 {
1345 void *p = NULL;
1346 char_u *saved_sourcing_name;
1347 int saved_sourcing_lnum;
1348 struct msglist *messages = NULL, *next;
1349
1350 /*
1351 * If the uncaught exception is a user exception, report it as an
1352 * error. If it is an error exception, display the saved error
1353 * message now. For an interrupt exception, do nothing; the
1354 * interrupt message is given elsewhere.
1355 */
1356 switch (current_exception->type)
1357 {
1358 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001359 vim_snprintf((char *)IObuff, IOSIZE,
1360 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 current_exception->value);
1362 p = vim_strsave(IObuff);
1363 break;
1364 case ET_ERROR:
1365 messages = current_exception->messages;
1366 current_exception->messages = NULL;
1367 break;
1368 case ET_INTERRUPT:
1369 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 }
1371
1372 saved_sourcing_name = sourcing_name;
1373 saved_sourcing_lnum = sourcing_lnum;
1374 sourcing_name = current_exception->throw_name;
1375 sourcing_lnum = current_exception->throw_lnum;
1376 current_exception->throw_name = NULL;
1377
1378 discard_current_exception(); /* uses IObuff if 'verbose' */
1379 suppress_errthrow = TRUE;
1380 force_abort = TRUE;
1381
1382 if (messages != NULL)
1383 {
1384 do
1385 {
1386 next = messages->next;
1387 emsg(messages->msg);
1388 vim_free(messages->msg);
1389 vim_free(messages);
1390 messages = next;
1391 }
1392 while (messages != NULL);
1393 }
1394 else if (p != NULL)
1395 {
1396 emsg(p);
1397 vim_free(p);
1398 }
1399 vim_free(sourcing_name);
1400 sourcing_name = saved_sourcing_name;
1401 sourcing_lnum = saved_sourcing_lnum;
1402 }
1403
1404 /*
1405 * On an interrupt or an aborting error not converted to an exception,
1406 * disable the conversion of errors to exceptions. (Interrupts are not
1407 * converted any more, here.) This enables also the interrupt message
1408 * when force_abort is set and did_emsg unset in case of an interrupt
1409 * from a finally clause after an error.
1410 */
1411 else if (got_int || (did_emsg && force_abort))
1412 suppress_errthrow = TRUE;
1413 }
1414
1415 /*
1416 * The current cstack will be freed when do_cmdline() returns. An uncaught
1417 * exception will have to be rethrown in the previous cstack. If a function
1418 * has just returned or a script file was just finished and the previous
1419 * cstack belongs to the same function or, respectively, script file, it
1420 * will have to be checked for finally clauses to be executed due to the
1421 * ":return" or ":finish". This is done in do_one_cmd().
1422 */
1423 if (did_throw)
1424 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001425 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001427 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 && ex_nesting_level > func_level(real_cookie) + 1))
1429 {
1430 if (!did_throw)
1431 check_cstack = TRUE;
1432 }
1433 else
1434 {
1435 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001436 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 --ex_nesting_level;
1438 /*
1439 * Go to debug mode when returning from a function in which we are
1440 * single-stepping.
1441 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001442 if ((getline_equal(fgetline, cookie, getsourceline)
1443 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001445 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 ? (char_u *)_("End of sourced file")
1447 : (char_u *)_("End of function"));
1448 }
1449
1450 /*
1451 * Restore the exception environment (done after returning from the
1452 * debugger).
1453 */
1454 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001455 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456
1457 msg_list = saved_msg_list;
1458#endif /* FEAT_EVAL */
1459
1460 /*
1461 * If there was too much output to fit on the command line, ask the user to
1462 * hit return before redrawing the screen. With the ":global" command we do
1463 * this only once after the command is finished.
1464 */
1465 if (did_inc)
1466 {
1467 --RedrawingDisabled;
1468 --no_wait_return;
1469 msg_scroll = FALSE;
1470
1471 /*
1472 * When just finished an ":if"-":else" which was typed, no need to
1473 * wait for hit-return. Also for an error situation.
1474 */
1475 if (retval == FAIL
1476#ifdef FEAT_EVAL
1477 || (did_endif && KeyTyped && !did_emsg)
1478#endif
1479 )
1480 {
1481 need_wait_return = FALSE;
1482 msg_didany = FALSE; /* don't wait when restarting edit */
1483 }
1484 else if (need_wait_return)
1485 {
1486 /*
1487 * The msg_start() above clears msg_didout. The wait_return we do
1488 * here should not overwrite the command that may be shown before
1489 * doing that.
1490 */
1491 msg_didout |= msg_didout_before_start;
1492 wait_return(FALSE);
1493 }
1494 }
1495
Bram Moolenaar2a942252012-11-28 23:03:07 +01001496#ifdef FEAT_EVAL
1497 did_endif = FALSE; /* in case do_cmdline used recursively */
1498#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 /*
1500 * Reset if_level, in case a sourced script file contains more ":if" than
1501 * ":endif" (could be ":if x | foo | endif").
1502 */
1503 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001504#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001505
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 --call_depth;
1507 return retval;
1508}
1509
1510#ifdef FEAT_EVAL
1511/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001512 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 */
1514 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001515get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001517 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 wcmd_T *wp;
1519 char_u *line;
1520
1521 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1522 {
1523 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001524 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001526 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 if (cp->getline == NULL)
1528 line = getcmdline(c, 0L, indent);
1529 else
1530 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001531 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 ++cp->current_line;
1533
1534 return line;
1535 }
1536
1537 KeyTyped = FALSE;
1538 ++cp->current_line;
1539 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1540 sourcing_lnum = wp->lnum;
1541 return vim_strsave(wp->line);
1542}
1543
1544/*
1545 * Store a line in "gap" so that a ":while" loop can execute it again.
1546 */
1547 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001548store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549{
1550 if (ga_grow(gap, 1) == FAIL)
1551 return FAIL;
1552 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1553 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1554 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 return OK;
1556}
1557
1558/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001559 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 */
1561 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001562free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563{
1564 while (gap->ga_len > 0)
1565 {
1566 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1567 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 }
1569}
1570#endif
1571
1572/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001573 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1574 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001577getline_equal(
1578 char_u *(*fgetline)(int, void *, int),
1579 void *cookie UNUSED, /* argument for fgetline() */
1580 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581{
1582#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001583 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001584 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585
Bram Moolenaar89d40322006-08-29 15:30:07 +00001586 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001587 * function that's originally used to obtain the lines. This may be
1588 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001589 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001590 cp = (struct loop_cookie *)cookie;
1591 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 {
1593 gp = cp->getline;
1594 cp = cp->cookie;
1595 }
1596 return gp == func;
1597#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001598 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599#endif
1600}
1601
1602#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1603/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001604 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 * getline function. Otherwise return "cookie".
1606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001608getline_cookie(
1609 char_u *(*fgetline)(int, void *, int) UNUSED,
1610 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611{
1612# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001613 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001614 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615
Bram Moolenaar89d40322006-08-29 15:30:07 +00001616 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001617 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001619 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001620 cp = (struct loop_cookie *)cookie;
1621 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 {
1623 gp = cp->getline;
1624 cp = cp->cookie;
1625 }
1626 return cp;
1627# else
1628 return cookie;
1629# endif
1630}
1631#endif
1632
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001633
1634/*
1635 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1636 * ":bwipeout", etc.
1637 * Returns the buffer number.
1638 */
1639 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001640compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001641{
1642 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001643 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001644 int count = offset;
1645
1646 buf = firstbuf;
1647 while (buf->b_next != NULL && buf->b_fnum < lnum)
1648 buf = buf->b_next;
1649 while (count != 0)
1650 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001651 count += (offset < 0) ? 1 : -1;
1652 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1653 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001654 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001655 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001656 if (addr_type == ADDR_LOADED_BUFFERS)
1657 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001658 while (buf->b_ml.ml_mfp == NULL)
1659 {
1660 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1661 if (nextbuf == NULL)
1662 break;
1663 buf = nextbuf;
1664 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001665 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001666 /* we might have gone too far, last buffer is not loadedd */
1667 if (addr_type == ADDR_LOADED_BUFFERS)
1668 while (buf->b_ml.ml_mfp == NULL)
1669 {
1670 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1671 if (nextbuf == NULL)
1672 break;
1673 buf = nextbuf;
1674 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001675 return buf->b_fnum;
1676}
1677
Bram Moolenaarf240e182014-11-27 18:33:02 +01001678#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001679static int current_win_nr(win_T *win);
1680static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001681
1682 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001683current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001684{
1685 win_T *wp;
1686 int nr = 0;
1687
Bram Moolenaar29323592016-07-24 22:04:11 +02001688 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001689 {
1690 ++nr;
1691 if (wp == win)
1692 break;
1693 }
1694 return nr;
1695}
1696
1697 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001698current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001699{
1700 tabpage_T *tp;
1701 int nr = 0;
1702
Bram Moolenaar29323592016-07-24 22:04:11 +02001703 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001704 {
1705 ++nr;
1706 if (tp == tab)
1707 break;
1708 }
1709 return nr;
1710}
1711
1712# define CURRENT_WIN_NR current_win_nr(curwin)
1713# define LAST_WIN_NR current_win_nr(NULL)
1714# define CURRENT_TAB_NR current_tab_nr(curtab)
1715# define LAST_TAB_NR current_tab_nr(NULL)
1716#else
1717# define CURRENT_WIN_NR 1
1718# define LAST_WIN_NR 1
1719# define CURRENT_TAB_NR 1
1720# define LAST_TAB_NR 1
1721#endif
1722
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001723
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724/*
1725 * Execute one Ex command.
1726 *
1727 * If 'sourcing' is TRUE, the command will be included in the error message.
1728 *
1729 * 1. skip comment lines and leading space
1730 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001731 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001732 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001733 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001734 * 6. parse arguments
1735 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001737 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 *
1739 * This function may be called recursively!
1740 */
1741#if (_MSC_VER == 1200)
1742/*
Bram Moolenaared203462004-06-16 11:19:22 +00001743 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001745 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746#endif
1747 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001748do_one_cmd(
1749 char_u **cmdlinep,
1750 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001752 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001754 char_u *(*fgetline)(int, void *, int),
1755 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756{
1757 char_u *p;
1758 linenr_T lnum;
1759 long n;
1760 char_u *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001761 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 exarg_T ea; /* Ex command arguments */
1763 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001764 int save_msg_scroll = msg_scroll;
1765 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001767#ifdef HAVE_SANDBOX
1768 int did_sandbox = FALSE;
1769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 cmdmod_T save_cmdmod;
1771 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001772 char_u *cmd;
Bram Moolenaarded27822017-01-02 14:27:34 +01001773 int address_count = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774
1775 vim_memset(&ea, 0, sizeof(ea));
1776 ea.line1 = 1;
1777 ea.line2 = 1;
1778#ifdef FEAT_EVAL
1779 ++ex_nesting_level;
1780#endif
1781
Bram Moolenaar21691f82012-12-05 19:13:18 +01001782 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 if (quitmore
1784#ifdef FEAT_EVAL
1785 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001786 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001787#endif
1788#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001789 /* avoid that an autocommand, e.g. QuitPre, does this */
1790 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791#endif
1792 )
1793 --quitmore;
1794
1795 /*
1796 * Reset browse, confirm, etc.. They are restored when returning, for
1797 * recursive calls.
1798 */
1799 save_cmdmod = cmdmod;
1800 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1801
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001802 /* "#!anything" is handled like a comment. */
1803 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1804 goto doend;
1805
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 /*
1807 * Repeat until no more command modifiers are found.
1808 */
1809 ea.cmd = *cmdlinep;
1810 for (;;)
1811 {
1812/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001813 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 */
1815 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1816 ++ea.cmd;
1817
1818 /* in ex mode, an empty line works like :+ */
1819 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001820 && (getline_equal(fgetline, cookie, getexmodeline)
1821 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001822 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 {
1824 ea.cmd = (char_u *)"+";
1825 ex_pressedreturn = TRUE;
1826 }
1827
1828 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001829 if (*ea.cmd == '"')
1830 goto doend;
1831 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001832 {
1833 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836
1837/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001838 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 */
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001840 p = skip_range(ea.cmd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 switch (*p)
1842 {
1843 /* When adding an entry, also modify cmd_exists(). */
1844 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1845 break;
1846#ifdef FEAT_WINDOWS
1847 cmdmod.split |= WSP_ABOVE;
1848#endif
1849 continue;
1850
1851 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1852 {
1853#ifdef FEAT_WINDOWS
1854 cmdmod.split |= WSP_BELOW;
1855#endif
1856 continue;
1857 }
1858 if (checkforcmd(&ea.cmd, "browse", 3))
1859 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001860#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 cmdmod.browse = TRUE;
1862#endif
1863 continue;
1864 }
1865 if (!checkforcmd(&ea.cmd, "botright", 2))
1866 break;
1867#ifdef FEAT_WINDOWS
1868 cmdmod.split |= WSP_BOT;
1869#endif
1870 continue;
1871
1872 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1873 break;
1874#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1875 cmdmod.confirm = TRUE;
1876#endif
1877 continue;
1878
1879 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1880 {
1881 cmdmod.keepmarks = TRUE;
1882 continue;
1883 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001884 if (checkforcmd(&ea.cmd, "keepalt", 5))
1885 {
1886 cmdmod.keepalt = TRUE;
1887 continue;
1888 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001889 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1890 {
1891 cmdmod.keeppatterns = TRUE;
1892 continue;
1893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1895 break;
1896 cmdmod.keepjumps = TRUE;
1897 continue;
1898
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001899 case 'f': /* only accept ":filter {pat} cmd" */
1900 {
1901 char_u *reg_pat;
1902
1903 if (!checkforcmd(&p, "filter", 4)
1904 || *p == NUL || ends_excmd(*p))
1905 break;
Bram Moolenaard29459b2016-08-26 22:29:11 +02001906 if (*p == '!')
1907 {
1908 cmdmod.filter_force = TRUE;
1909 p = skipwhite(p + 1);
1910 if (*p == NUL || ends_excmd(*p))
1911 break;
1912 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001913 p = skip_vimgrep_pat(p, &reg_pat, NULL);
1914 if (p == NULL || *p == NUL)
1915 break;
1916 cmdmod.filter_regmatch.regprog =
1917 vim_regcomp(reg_pat, RE_MAGIC);
1918 if (cmdmod.filter_regmatch.regprog == NULL)
1919 break;
1920 ea.cmd = p;
1921 continue;
1922 }
1923
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 /* ":hide" and ":hide | cmd" are not modifiers */
1925 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1926 || *p == NUL || ends_excmd(*p))
1927 break;
1928 ea.cmd = p;
1929 cmdmod.hide = TRUE;
1930 continue;
1931
1932 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1933 {
1934 cmdmod.lockmarks = TRUE;
1935 continue;
1936 }
1937
1938 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1939 break;
1940#ifdef FEAT_WINDOWS
1941 cmdmod.split |= WSP_ABOVE;
1942#endif
1943 continue;
1944
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001945 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001946 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001947#ifdef FEAT_AUTOCMD
1948 if (cmdmod.save_ei == NULL)
1949 {
1950 /* Set 'eventignore' to "all". Restore the
1951 * existing option value later. */
1952 cmdmod.save_ei = vim_strsave(p_ei);
1953 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001954 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001955 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001956#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001957 continue;
1958 }
Bram Moolenaar3bcfca32016-07-30 19:39:29 +02001959 if (!checkforcmd(&ea.cmd, "noswapfile", 3))
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001960 break;
1961 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001962 continue;
1963
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1965 break;
1966#ifdef FEAT_WINDOWS
1967 cmdmod.split |= WSP_BELOW;
1968#endif
1969 continue;
1970
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001971 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1972 {
1973#ifdef HAVE_SANDBOX
1974 if (!did_sandbox)
1975 ++sandbox;
1976 did_sandbox = TRUE;
1977#endif
1978 continue;
1979 }
1980 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001982 if (save_msg_silent == -1)
1983 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 ++msg_silent;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001985 if (*ea.cmd == '!' && !VIM_ISWHITE(ea.cmd[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 {
1987 /* ":silent!", but not "silent !cmd" */
1988 ea.cmd = skipwhite(ea.cmd + 1);
1989 ++emsg_silent;
1990 ++did_esilent;
1991 }
1992 continue;
1993
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001994 case 't': if (checkforcmd(&p, "tab", 3))
1995 {
1996#ifdef FEAT_WINDOWS
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001997 long tabnr = get_address(&ea, &ea.cmd, ADDR_TABS,
Bram Moolenaarded27822017-01-02 14:27:34 +01001998 ea.skip, FALSE, 1);
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001999 if (tabnr == MAXLNUM)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002000 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02002001 else
2002 {
2003 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2004 {
2005 errormsg = (char_u *)_(e_invrange);
2006 goto doend;
2007 }
2008 cmdmod.tab = tabnr + 1;
2009 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002010 ea.cmd = p;
2011#endif
2012 continue;
2013 }
2014 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 break;
2016#ifdef FEAT_WINDOWS
2017 cmdmod.split |= WSP_TOP;
2018#endif
2019 continue;
2020
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002021 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
2022 break;
2023 if (save_msg_silent == -1)
2024 save_msg_silent = msg_silent;
2025 msg_silent = 0;
2026 continue;
2027
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
2029 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002030#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 cmdmod.split |= WSP_VERT;
2032#endif
2033 continue;
2034 }
2035 if (!checkforcmd(&p, "verbose", 4))
2036 break;
2037 if (verbose_save < 0)
2038 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00002039 if (vim_isdigit(*ea.cmd))
2040 p_verbose = atoi((char *)ea.cmd);
2041 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 p_verbose = 1;
2043 ea.cmd = p;
2044 continue;
2045 }
2046 break;
2047 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002048 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049
2050#ifdef FEAT_EVAL
2051 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2052 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2053#else
2054 ea.skip = (if_level > 0);
2055#endif
2056
2057#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002058# ifdef FEAT_PROFILE
2059 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002060 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002061 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002062 if (getline_equal(fgetline, cookie, get_func_line))
2063 func_line_exec(getline_cookie(fgetline, cookie));
2064 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002065 script_line_exec();
2066 }
2067#endif
2068
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 /* May go to debug mode. If this happens and the ">quit" debug command is
2070 * used, throw an interrupt exception and skip the next command. */
2071 dbg_check_breakpoint(&ea);
2072 if (!ea.skip && got_int)
2073 {
2074 ea.skip = TRUE;
2075 (void)do_intthrow(cstack);
2076 }
2077#endif
2078
2079/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002080 * 3. Skip over the range to find the command. Let "p" point to after it.
2081 *
2082 * We need the command to know what kind of range it uses.
2083 */
2084 cmd = ea.cmd;
2085 ea.cmd = skip_range(ea.cmd, NULL);
2086 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2087 ea.cmd = skipwhite(ea.cmd + 1);
2088 p = find_command(&ea, NULL);
2089
2090/*
2091 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 *
2093 * where 'addr' is:
2094 *
2095 * % (entire file)
2096 * $ [+-NUM]
2097 * 'x [+-NUM] (where x denotes a currently defined mark)
2098 * . [+-NUM]
2099 * [+-NUM]..
2100 * NUM
2101 *
2102 * The ea.cmd pointer is updated to point to the first character following the
2103 * range spec. If an initial address is found, but no second, the upper bound
2104 * is equal to the lower.
2105 */
2106
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002107 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002108 if (!IS_USER_CMDIDX(ea.cmdidx))
2109 {
2110 if (ea.cmdidx != CMD_SIZE)
2111 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2112 else
2113 ea.addr_type = ADDR_LINES;
2114
2115#ifdef FEAT_WINDOWS
2116 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002117 if (ea.cmdidx == CMD_wincmd && p != NULL)
2118 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002119#endif
2120 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002121
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002123 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 for (;;)
2125 {
2126 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002127 switch (ea.addr_type)
2128 {
2129 case ADDR_LINES:
2130 /* default is current line number */
2131 ea.line2 = curwin->w_cursor.lnum;
2132 break;
2133 case ADDR_WINDOWS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002134 ea.line2 = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002135 break;
2136 case ADDR_ARGUMENTS:
2137 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002138 if (ea.line2 > ARGCOUNT)
2139 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002140 break;
2141 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002142 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002143 ea.line2 = curbuf->b_fnum;
2144 break;
2145 case ADDR_TABS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002146 ea.line2 = CURRENT_TAB_NR;
2147 break;
2148 case ADDR_TABS_RELATIVE:
2149 ea.line2 = 1;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002150 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002151#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002152 case ADDR_QUICKFIX:
2153 ea.line2 = qf_get_cur_valid_idx(&ea);
2154 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002155#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002158 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip,
Bram Moolenaarded27822017-01-02 14:27:34 +01002159 ea.addr_count == 0, address_count++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 if (ea.cmd == NULL) /* error detected */
2161 goto doend;
2162 if (lnum == MAXLNUM)
2163 {
2164 if (*ea.cmd == '%') /* '%' - all lines */
2165 {
2166 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002167 switch (ea.addr_type)
2168 {
2169 case ADDR_LINES:
2170 ea.line1 = 1;
2171 ea.line2 = curbuf->b_ml.ml_line_count;
2172 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002173 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002174 {
2175 buf_T *buf = firstbuf;
2176
2177 while (buf->b_next != NULL
2178 && buf->b_ml.ml_mfp == NULL)
2179 buf = buf->b_next;
2180 ea.line1 = buf->b_fnum;
2181 buf = lastbuf;
2182 while (buf->b_prev != NULL
2183 && buf->b_ml.ml_mfp == NULL)
2184 buf = buf->b_prev;
2185 ea.line2 = buf->b_fnum;
2186 break;
2187 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002188 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002189 ea.line1 = firstbuf->b_fnum;
2190 ea.line2 = lastbuf->b_fnum;
2191 break;
2192 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002193 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002194 if (IS_USER_CMDIDX(ea.cmdidx))
2195 {
2196 ea.line1 = 1;
2197 ea.line2 = ea.addr_type == ADDR_WINDOWS
2198 ? LAST_WIN_NR : LAST_TAB_NR;
2199 }
2200 else
2201 {
2202 /* there is no Vim command which uses '%' and
2203 * ADDR_WINDOWS or ADDR_TABS */
2204 errormsg = (char_u *)_(e_invrange);
2205 goto doend;
2206 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002207 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002208 case ADDR_TABS_RELATIVE:
2209 errormsg = (char_u *)_(e_invrange);
2210 goto doend;
2211 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002212 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002213 if (ARGCOUNT == 0)
2214 ea.line1 = ea.line2 = 0;
2215 else
2216 {
2217 ea.line1 = 1;
2218 ea.line2 = ARGCOUNT;
2219 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002220 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002221#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002222 case ADDR_QUICKFIX:
2223 ea.line1 = 1;
2224 ea.line2 = qf_get_size(&ea);
2225 if (ea.line2 == 0)
2226 ea.line2 = 1;
2227 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002228#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 ++ea.addr_count;
2231 }
2232 /* '*' - visual area */
2233 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2234 {
2235 pos_T *fp;
2236
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002237 if (ea.addr_type != ADDR_LINES)
2238 {
2239 errormsg = (char_u *)_(e_invrange);
2240 goto doend;
2241 }
2242
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 ++ea.cmd;
2244 if (!ea.skip)
2245 {
2246 fp = getmark('<', FALSE);
2247 if (check_mark(fp) == FAIL)
2248 goto doend;
2249 ea.line1 = fp->lnum;
2250 fp = getmark('>', FALSE);
2251 if (check_mark(fp) == FAIL)
2252 goto doend;
2253 ea.line2 = fp->lnum;
2254 ++ea.addr_count;
2255 }
2256 }
2257 }
2258 else
2259 ea.line2 = lnum;
2260 ea.addr_count++;
2261
2262 if (*ea.cmd == ';')
2263 {
2264 if (!ea.skip)
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002265 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 curwin->w_cursor.lnum = ea.line2;
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +01002267 /* don't leave the cursor on an illegal line or column */
2268 check_cursor();
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 }
2271 else if (*ea.cmd != ',')
2272 break;
2273 ++ea.cmd;
2274 }
2275
2276 /* One address given: set start and end lines */
2277 if (ea.addr_count == 1)
2278 {
2279 ea.line1 = ea.line2;
2280 /* ... but only implicit: really no address given */
2281 if (lnum == MAXLNUM)
2282 ea.addr_count = 0;
2283 }
2284
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002286 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 */
2288
2289 /*
2290 * Skip ':' and any white space
2291 */
2292 ea.cmd = skipwhite(ea.cmd);
2293 while (*ea.cmd == ':')
2294 ea.cmd = skipwhite(ea.cmd + 1);
2295
2296 /*
2297 * If we got a line, but no command, then go to the line.
2298 * If we find a '|' or '\n' we set ea.nextcmd.
2299 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002300 if (*ea.cmd == NUL || *ea.cmd == '"'
2301 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 {
2303 /*
2304 * strange vi behaviour:
2305 * ":3" jumps to line 3
2306 * ":3|..." prints line 3
2307 * ":|" prints current line
2308 */
2309 if (ea.skip) /* skip this if inside :if */
2310 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002311 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 {
2313 ea.cmdidx = CMD_print;
2314 ea.argt = RANGE+COUNT+TRLBAR;
2315 if ((errormsg = invalid_range(&ea)) == NULL)
2316 {
2317 correct_range(&ea);
2318 ex_print(&ea);
2319 }
2320 }
2321 else if (ea.addr_count != 0)
2322 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002323 if (ea.line2 > curbuf->b_ml.ml_line_count)
2324 {
2325 /* With '-' in 'cpoptions' a line number past the file is an
2326 * error, otherwise put it at the end of the file. */
2327 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2328 ea.line2 = -1;
2329 else
2330 ea.line2 = curbuf->b_ml.ml_line_count;
2331 }
2332
2333 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002334 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 else
2336 {
2337 if (ea.line2 == 0)
2338 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 else
2340 curwin->w_cursor.lnum = ea.line2;
2341 beginline(BL_SOL | BL_FIX);
2342 }
2343 }
2344 goto doend;
2345 }
2346
Bram Moolenaard5005162014-08-22 23:05:54 +02002347#ifdef FEAT_AUTOCMD
2348 /* If this looks like an undefined user command and there are CmdUndefined
2349 * autocommands defined, trigger the matching autocommands. */
2350 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2351 && ASCII_ISUPPER(*ea.cmd)
2352 && has_cmdundefined())
2353 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002354 int ret;
2355
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002356 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002357 while (ASCII_ISALNUM(*p))
2358 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002359 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002360 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2361 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002362 /* If the autocommands did something and didn't cause an error, try
2363 * finding the command again. */
Bram Moolenaareac784e2016-07-28 22:08:24 +02002364 p = (ret && !aborting()) ? find_command(&ea, NULL) : ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002365 }
2366#endif
2367
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368#ifdef FEAT_USR_CMDS
2369 if (p == NULL)
2370 {
2371 if (!ea.skip)
2372 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2373 goto doend;
2374 }
2375 /* Check for wrong commands. */
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002376 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2377 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 {
2379 errormsg = uc_fun_cmd();
2380 goto doend;
2381 }
2382#endif
2383 if (ea.cmdidx == CMD_SIZE)
2384 {
2385 if (!ea.skip)
2386 {
2387 STRCPY(IObuff, _("E492: Not an editor command"));
2388 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002389 {
2390 /* If the modifier was parsed OK the error must be in the
2391 * following command */
2392 if (after_modifier != NULL)
2393 append_command(after_modifier);
2394 else
2395 append_command(*cmdlinep);
2396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002398 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 }
2400 goto doend;
2401 }
2402
Bram Moolenaar958636c2014-10-21 20:01:58 +02002403 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2404 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002405#ifdef HAVE_EX_SCRIPT_NI
2406 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2407#endif
2408 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409
2410#ifndef FEAT_EVAL
2411 /*
2412 * When the expression evaluation is disabled, recognize the ":if" and
2413 * ":endif" commands and ignore everything in between it.
2414 */
2415 if (ea.cmdidx == CMD_if)
2416 ++if_level;
2417 if (if_level)
2418 {
2419 if (ea.cmdidx == CMD_endif)
2420 --if_level;
2421 goto doend;
2422 }
2423
2424#endif
2425
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002426 /* forced commands */
2427 if (*p == '!' && ea.cmdidx != CMD_substitute
2428 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 {
2430 ++p;
2431 ea.forceit = TRUE;
2432 }
2433 else
2434 ea.forceit = FALSE;
2435
2436/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002437 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002439 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002440 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441
2442 if (!ea.skip)
2443 {
2444#ifdef HAVE_SANDBOX
2445 if (sandbox != 0 && !(ea.argt & SBOXOK))
2446 {
2447 /* Command not allowed in sandbox. */
2448 errormsg = (char_u *)_(e_sandbox);
2449 goto doend;
2450 }
2451#endif
2452 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2453 {
2454 /* Command not allowed in non-'modifiable' buffer */
2455 errormsg = (char_u *)_(e_modifiable);
2456 goto doend;
2457 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002458
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002459 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002460 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002462 /* Command not allowed when editing the command line. */
Bram Moolenaar75c19462017-02-12 18:34:05 +01002463 errormsg = (char_u *)_(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 goto doend;
2465 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002466#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002467 /* Disallow editing another buffer when "curbuf_lock" is set.
2468 * Do allow ":edit" (check for argument later).
2469 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002470 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002471 && ea.cmdidx != CMD_edit
2472 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002473 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002474 && curbuf_locked())
2475 goto doend;
2476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477
2478 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2479 {
2480 /* no range allowed */
2481 errormsg = (char_u *)_(e_norange);
2482 goto doend;
2483 }
2484 }
2485
2486 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2487 {
2488 errormsg = (char_u *)_(e_nobang);
2489 goto doend;
2490 }
2491
2492 /*
2493 * Don't complain about the range if it is not used
2494 * (could happen if line_count is accidentally set to 0).
2495 */
2496 if (!ea.skip && !ni)
2497 {
2498 /*
2499 * If the range is backwards, ask for confirmation and, if given, swap
2500 * ea.line1 & ea.line2 so it's forwards again.
2501 * When global command is busy, don't ask, will fail below.
2502 */
2503 if (!global_busy && ea.line1 > ea.line2)
2504 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002505 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002507 if (sourcing || exmode_active)
2508 {
2509 errormsg = (char_u *)_("E493: Backwards range given");
2510 goto doend;
2511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 if (ask_yesno((char_u *)
2513 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002514 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 }
2516 lnum = ea.line1;
2517 ea.line1 = ea.line2;
2518 ea.line2 = lnum;
2519 }
2520 if ((errormsg = invalid_range(&ea)) != NULL)
2521 goto doend;
2522 }
2523
2524 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2525 ea.line2 = 1;
2526
2527 correct_range(&ea);
2528
2529#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002530 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2531 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 {
2533 /* Put the first line at the start of a closed fold, put the last line
2534 * at the end of a closed fold. */
2535 (void)hasFolding(ea.line1, &ea.line1, NULL);
2536 (void)hasFolding(ea.line2, NULL, &ea.line2);
2537 }
2538#endif
2539
2540#ifdef FEAT_QUICKFIX
2541 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002542 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 * option here, so things like % get expanded.
2544 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002545 p = replace_makeprg(&ea, p, cmdlinep);
2546 if (p == NULL)
2547 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548#endif
2549
2550 /*
2551 * Skip to start of argument.
2552 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2553 */
2554 if (ea.cmdidx == CMD_bang)
2555 ea.arg = p;
2556 else
2557 ea.arg = skipwhite(p);
2558
2559 /*
2560 * Check for "++opt=val" argument.
2561 * Must be first, allow ":w ++enc=utf8 !cmd"
2562 */
2563 if (ea.argt & ARGOPT)
2564 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2565 if (getargopt(&ea) == FAIL && !ni)
2566 {
2567 errormsg = (char_u *)_(e_invarg);
2568 goto doend;
2569 }
2570
2571 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2572 {
2573 if (*ea.arg == '>') /* append */
2574 {
2575 if (*++ea.arg != '>') /* typed wrong */
2576 {
2577 errormsg = (char_u *)_("E494: Use w or w>>");
2578 goto doend;
2579 }
2580 ea.arg = skipwhite(ea.arg + 1);
2581 ea.append = TRUE;
2582 }
2583 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2584 {
2585 ++ea.arg;
2586 ea.usefilter = TRUE;
2587 }
2588 }
2589
2590 if (ea.cmdidx == CMD_read)
2591 {
2592 if (ea.forceit)
2593 {
2594 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2595 ea.forceit = FALSE;
2596 }
2597 else if (*ea.arg == '!') /* :r !filter */
2598 {
2599 ++ea.arg;
2600 ea.usefilter = TRUE;
2601 }
2602 }
2603
2604 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2605 {
2606 ea.amount = 1;
2607 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2608 {
2609 ++ea.arg;
2610 ++ea.amount;
2611 }
2612 ea.arg = skipwhite(ea.arg);
2613 }
2614
2615 /*
2616 * Check for "+command" argument, before checking for next command.
2617 * Don't do this for ":read !cmd" and ":write !cmd".
2618 */
2619 if ((ea.argt & EDITCMD) && !ea.usefilter)
2620 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2621
2622 /*
2623 * Check for '|' to separate commands and '"' to start comments.
2624 * Don't do this for ":read !cmd" and ":write !cmd".
2625 */
2626 if ((ea.argt & TRLBAR) && !ea.usefilter)
2627 separate_nextcmd(&ea);
2628
2629 /*
2630 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002631 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2632 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002634 else if (ea.cmdidx == CMD_bang
2635 || ea.cmdidx == CMD_global
2636 || ea.cmdidx == CMD_vglobal
2637 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 {
2639 for (p = ea.arg; *p; ++p)
2640 {
2641 /* Remove one backslash before a newline, so that it's possible to
2642 * pass a newline to the shell and also a newline that is preceded
2643 * with a backslash. This makes it impossible to end a shell
2644 * command in a backslash, but that doesn't appear useful.
2645 * Halving the number of backslashes is incompatible with previous
2646 * versions. */
2647 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002648 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 else if (*p == '\n')
2650 {
2651 ea.nextcmd = p + 1;
2652 *p = NUL;
2653 break;
2654 }
2655 }
2656 }
2657
2658 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2659 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002660 buf_T *buf;
2661
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002663 switch (ea.addr_type)
2664 {
2665 case ADDR_LINES:
2666 ea.line2 = curbuf->b_ml.ml_line_count;
2667 break;
2668 case ADDR_LOADED_BUFFERS:
2669 buf = firstbuf;
2670 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2671 buf = buf->b_next;
2672 ea.line1 = buf->b_fnum;
2673 buf = lastbuf;
2674 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2675 buf = buf->b_prev;
2676 ea.line2 = buf->b_fnum;
2677 break;
2678 case ADDR_BUFFERS:
2679 ea.line1 = firstbuf->b_fnum;
2680 ea.line2 = lastbuf->b_fnum;
2681 break;
2682 case ADDR_WINDOWS:
2683 ea.line2 = LAST_WIN_NR;
2684 break;
2685 case ADDR_TABS:
2686 ea.line2 = LAST_TAB_NR;
2687 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002688 case ADDR_TABS_RELATIVE:
2689 ea.line2 = 1;
2690 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002691 case ADDR_ARGUMENTS:
2692 if (ARGCOUNT == 0)
2693 ea.line1 = ea.line2 = 0;
2694 else
2695 ea.line2 = ARGCOUNT;
2696 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002697#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002698 case ADDR_QUICKFIX:
2699 ea.line2 = qf_get_size(&ea);
2700 if (ea.line2 == 0)
2701 ea.line2 = 1;
2702 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002703#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 }
2706
2707 /* accept numbered register only when no count allowed (:put) */
2708 if ( (ea.argt & REGSTR)
2709 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002710 /* Do not allow register = for user commands */
2711 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2713 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002714#ifndef FEAT_CLIPBOARD
2715 /* check these explicitly for a more specific error message */
2716 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002718 errormsg = (char_u *)_(e_invalidreg);
2719 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 }
2721#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002722 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2723 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002724 {
2725 ea.regname = *ea.arg++;
2726#ifdef FEAT_EVAL
2727 /* for '=' register: accept the rest of the line as an expression */
2728 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2729 {
2730 set_expr_line(vim_strsave(ea.arg));
2731 ea.arg += STRLEN(ea.arg);
2732 }
2733#endif
2734 ea.arg = skipwhite(ea.arg);
2735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 }
2737
2738 /*
2739 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2740 * count, it's a buffer name.
2741 */
2742 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2743 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002744 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 {
2746 n = getdigits(&ea.arg);
2747 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002748 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 {
2750 errormsg = (char_u *)_(e_zerocount);
2751 goto doend;
2752 }
2753 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2754 {
2755 ea.line2 = n;
2756 if (ea.addr_count == 0)
2757 ea.addr_count = 1;
2758 }
2759 else
2760 {
2761 ea.line1 = ea.line2;
2762 ea.line2 += n - 1;
2763 ++ea.addr_count;
2764 /*
2765 * Be vi compatible: no error message for out of range.
2766 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002767 if (ea.addr_type == ADDR_LINES
2768 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 ea.line2 = curbuf->b_ml.ml_line_count;
2770 }
2771 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002772
2773 /*
2774 * Check for flags: 'l', 'p' and '#'.
2775 */
2776 if (ea.argt & EXFLAGS)
2777 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002779 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002780 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 {
2782 errormsg = (char_u *)_(e_trailing);
2783 goto doend;
2784 }
2785
2786 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2787 {
2788 errormsg = (char_u *)_(e_argreq);
2789 goto doend;
2790 }
2791
2792#ifdef FEAT_EVAL
2793 /*
2794 * Skip the command when it's not going to be executed.
2795 * The commands like :if, :endif, etc. always need to be executed.
2796 * Also make an exception for commands that handle a trailing command
2797 * themselves.
2798 */
2799 if (ea.skip)
2800 {
2801 switch (ea.cmdidx)
2802 {
2803 /* commands that need evaluation */
2804 case CMD_while:
2805 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002806 case CMD_for:
2807 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 case CMD_if:
2809 case CMD_elseif:
2810 case CMD_else:
2811 case CMD_endif:
2812 case CMD_try:
2813 case CMD_catch:
2814 case CMD_finally:
2815 case CMD_endtry:
2816 case CMD_function:
2817 break;
2818
2819 /* Commands that handle '|' themselves. Check: A command should
2820 * either have the TRLBAR flag, appear in this list or appear in
2821 * the list at ":help :bar". */
2822 case CMD_aboveleft:
2823 case CMD_and:
2824 case CMD_belowright:
2825 case CMD_botright:
2826 case CMD_browse:
2827 case CMD_call:
2828 case CMD_confirm:
2829 case CMD_delfunction:
2830 case CMD_djump:
2831 case CMD_dlist:
2832 case CMD_dsearch:
2833 case CMD_dsplit:
2834 case CMD_echo:
2835 case CMD_echoerr:
2836 case CMD_echomsg:
2837 case CMD_echon:
2838 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002839 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 case CMD_help:
2841 case CMD_hide:
2842 case CMD_ijump:
2843 case CMD_ilist:
2844 case CMD_isearch:
2845 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002846 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 case CMD_keepjumps:
2848 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002849 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 case CMD_leftabove:
2851 case CMD_let:
2852 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002853 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002855 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002856 case CMD_noautocmd:
2857 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 case CMD_perl:
2859 case CMD_psearch:
2860 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002861 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002862 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 case CMD_return:
2864 case CMD_rightbelow:
2865 case CMD_ruby:
2866 case CMD_silent:
2867 case CMD_smagic:
2868 case CMD_snomagic:
2869 case CMD_substitute:
2870 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002871 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 case CMD_tcl:
2873 case CMD_throw:
2874 case CMD_tilde:
2875 case CMD_topleft:
2876 case CMD_unlet:
2877 case CMD_verbose:
2878 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002879 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 break;
2881
2882 default: goto doend;
2883 }
2884 }
2885#endif
2886
2887 if (ea.argt & XFILE)
2888 {
2889 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2890 goto doend;
2891 }
2892
2893#ifdef FEAT_LISTCMDS
2894 /*
2895 * Accept buffer name. Cannot be used at the same time with a buffer
2896 * number. Don't do this for a user command.
2897 */
2898 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002899 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 {
2901 /*
2902 * :bdelete, :bwipeout and :bunload take several arguments, separated
2903 * by spaces: find next space (skipping over escaped characters).
2904 * The others take one argument: ignore trailing spaces.
2905 */
2906 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2907 || ea.cmdidx == CMD_bunload)
2908 p = skiptowhite_esc(ea.arg);
2909 else
2910 {
2911 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002912 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 --p;
2914 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002915 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2916 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 if (ea.line2 < 0) /* failed */
2918 goto doend;
2919 ea.addr_count = 1;
2920 ea.arg = skipwhite(p);
2921 }
2922#endif
2923
2924/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002925 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 *
2927 * The "ea" structure holds the arguments that can be used.
2928 */
2929 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002930 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 ea.cookie = cookie;
2932#ifdef FEAT_EVAL
2933 ea.cstack = cstack;
2934#endif
2935
2936#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002937 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 {
2939 /*
2940 * Execute a user-defined command.
2941 */
2942 do_ucmd(&ea);
2943 }
2944 else
2945#endif
2946 {
2947 /*
2948 * Call the function to execute the command.
2949 */
2950 ea.errmsg = NULL;
2951 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2952 if (ea.errmsg != NULL)
2953 errormsg = (char_u *)_(ea.errmsg);
2954 }
2955
2956#ifdef FEAT_EVAL
2957 /*
2958 * If the command just executed called do_cmdline(), any throw or ":return"
2959 * or ":finish" encountered there must also check the cstack of the still
2960 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2961 * exception, or reanimate a returned function or finished script file and
2962 * return or finish it again.
2963 */
2964 if (need_rethrow)
2965 do_throw(cstack);
2966 else if (check_cstack)
2967 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002968 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002970 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 && current_func_returned())
2972 do_return(&ea, TRUE, FALSE, NULL);
2973 }
2974 need_rethrow = check_cstack = FALSE;
2975#endif
2976
2977doend:
2978 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002979 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002981 curwin->w_cursor.col = 0;
2982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983
2984 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2985 {
2986 if (sourcing)
2987 {
2988 if (errormsg != IObuff)
2989 {
2990 STRCPY(IObuff, errormsg);
2991 errormsg = IObuff;
2992 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002993 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 }
2995 emsg(errormsg);
2996 }
2997#ifdef FEAT_EVAL
2998 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002999 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
3000 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001#endif
3002
3003 if (verbose_save >= 0)
3004 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00003005#ifdef FEAT_AUTOCMD
3006 if (cmdmod.save_ei != NULL)
3007 {
3008 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003009 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
3010 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00003011 free_string_option(cmdmod.save_ei);
3012 }
3013#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003014 if (cmdmod.filter_regmatch.regprog != NULL)
3015 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016
3017 cmdmod = save_cmdmod;
3018
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003019 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 {
3021 /* messages could be enabled for a serious error, need to check if the
3022 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00003023 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003024 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 emsg_silent -= did_esilent;
3026 if (emsg_silent < 0)
3027 emsg_silent = 0;
3028 /* Restore msg_scroll, it's set by file I/O commands, even when no
3029 * message is actually displayed. */
3030 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003031
3032 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
3033 * somewhere in the line. Put it back in the first column. */
3034 if (redirecting())
3035 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 }
3037
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003038#ifdef HAVE_SANDBOX
3039 if (did_sandbox)
3040 --sandbox;
3041#endif
3042
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3044 ea.nextcmd = NULL;
3045
3046#ifdef FEAT_EVAL
3047 --ex_nesting_level;
3048#endif
3049
3050 return ea.nextcmd;
3051}
3052#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003053 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054#endif
3055
3056/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003057 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 * If there is a match advance "pp" to the argument and return TRUE.
3059 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003060 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003061checkforcmd(
3062 char_u **pp, /* start of command */
3063 char *cmd, /* name of command */
3064 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065{
3066 int i;
3067
3068 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003069 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 break;
3071 if (i >= len && !isalpha((*pp)[i]))
3072 {
3073 *pp = skipwhite(*pp + i);
3074 return TRUE;
3075 }
3076 return FALSE;
3077}
3078
3079/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003080 * Append "cmd" to the error message in IObuff.
3081 * Takes care of limiting the length and handling 0xa0, which would be
3082 * invisible otherwise.
3083 */
3084 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003085append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003086{
3087 char_u *s = cmd;
3088 char_u *d;
3089
3090 STRCAT(IObuff, ": ");
3091 d = IObuff + STRLEN(IObuff);
3092 while (*s != NUL && d - IObuff < IOSIZE - 7)
3093 {
3094 if (
3095#ifdef FEAT_MBYTE
3096 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3097#endif
3098 *s == 0xa0)
3099 {
3100 s +=
3101#ifdef FEAT_MBYTE
3102 enc_utf8 ? 2 :
3103#endif
3104 1;
3105 STRCPY(d, "<a0>");
3106 d += 4;
3107 }
3108 else
3109 MB_COPY_CHAR(s, d);
3110 }
3111 *d = NUL;
3112}
3113
3114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003116 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003118 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 * Returns NULL for an ambiguous user command.
3120 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003122find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123{
3124 int len;
3125 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003126 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127
3128 /*
3129 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003130 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 * - the 'k' command can directly be followed by any character.
3132 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003133 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003135 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 */
3137 p = eap->cmd;
3138 if (*p == 'k')
3139 {
3140 eap->cmdidx = CMD_k;
3141 ++p;
3142 }
3143 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003144 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3145 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 || p[1] == 'g'
3147 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3148 || p[1] == 'I'
3149 || (p[1] == 'r' && p[2] != 'e')))
3150 {
3151 eap->cmdidx = CMD_substitute;
3152 ++p;
3153 }
3154 else
3155 {
3156 while (ASCII_ISALPHA(*p))
3157 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003158 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003159 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003160 while (ASCII_ISALNUM(*p))
3161 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003162
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 /* check for non-alpha command */
3164 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3165 ++p;
3166 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003167 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3168 {
3169 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3170 * :delete with the 'l' flag. Same for 'p'. */
3171 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003172 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003173 break;
3174 if (i == len - 1)
3175 {
3176 --len;
3177 if (p[-1] == 'l')
3178 eap->flags |= EXFLAG_LIST;
3179 else
3180 eap->flags |= EXFLAG_PRINT;
3181 }
3182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003184 if (ASCII_ISLOWER(eap->cmd[0]))
3185 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003186 int c1 = eap->cmd[0];
3187 int c2 = eap->cmd[1];
3188
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003189 if (command_count != (int)CMD_SIZE)
3190 {
3191 iemsg((char_u *)_("E943: Command table needs to be updated, run 'make cmdidxs'"));
3192 getout(1);
3193 }
3194
3195 /* Use a precomputed index for fast look-up in cmdnames[]
3196 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003197 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3198 if (ASCII_ISLOWER(c2))
3199 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003202 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203
3204 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3205 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3206 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3207 (size_t)len) == 0)
3208 {
3209#ifdef FEAT_EVAL
3210 if (full != NULL
3211 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3212 *full = TRUE;
3213#endif
3214 break;
3215 }
3216
3217#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003218 /* Look for a user defined command as a last resort. Let ":Print" be
3219 * overruled by a user defined command. */
3220 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3221 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003223 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 while (ASCII_ISALNUM(*p))
3225 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003226 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 }
3228#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003229 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 eap->cmdidx = CMD_SIZE;
3231 }
3232
3233 return p;
3234}
3235
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003236#ifdef FEAT_USR_CMDS
3237/*
3238 * Search for a user command that matches "eap->cmd".
3239 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3240 * Return a pointer to just after the command.
3241 * Return NULL if there is no matching command.
3242 */
3243 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003244find_ucmd(
3245 exarg_T *eap,
3246 char_u *p, /* end of the command (possibly including count) */
3247 int *full, /* set to TRUE for a full match */
3248 expand_T *xp, /* used for completion, NULL otherwise */
3249 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003250{
3251 int len = (int)(p - eap->cmd);
3252 int j, k, matchlen = 0;
3253 ucmd_T *uc;
3254 int found = FALSE;
3255 int possible = FALSE;
3256 char_u *cp, *np; /* Point into typed cmd and test name */
3257 garray_T *gap;
3258 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3259 only full match global is accepted. */
3260
3261 /*
3262 * Look for buffer-local user commands first, then global ones.
3263 */
3264 gap = &curbuf->b_ucmds;
3265 for (;;)
3266 {
3267 for (j = 0; j < gap->ga_len; ++j)
3268 {
3269 uc = USER_CMD_GA(gap, j);
3270 cp = eap->cmd;
3271 np = uc->uc_name;
3272 k = 0;
3273 while (k < len && *np != NUL && *cp++ == *np++)
3274 k++;
3275 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3276 {
3277 /* If finding a second match, the command is ambiguous. But
3278 * not if a buffer-local command wasn't a full match and a
3279 * global command is a full match. */
3280 if (k == len && found && *np != NUL)
3281 {
3282 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003283 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003284 amb_local = TRUE;
3285 }
3286
3287 if (!found || (k == len && *np == NUL))
3288 {
3289 /* If we matched up to a digit, then there could
3290 * be another command including the digit that we
3291 * should use instead.
3292 */
3293 if (k == len)
3294 found = TRUE;
3295 else
3296 possible = TRUE;
3297
3298 if (gap == &ucmds)
3299 eap->cmdidx = CMD_USER;
3300 else
3301 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003302 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003303 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003304 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003305
3306# ifdef FEAT_CMDL_COMPL
3307 if (compl != NULL)
3308 *compl = uc->uc_compl;
3309# ifdef FEAT_EVAL
3310 if (xp != NULL)
3311 {
3312 xp->xp_arg = uc->uc_compl_arg;
3313 xp->xp_scriptID = uc->uc_scriptID;
3314 }
3315# endif
3316# endif
3317 /* Do not search for further abbreviations
3318 * if this is an exact match. */
3319 matchlen = k;
3320 if (k == len && *np == NUL)
3321 {
3322 if (full != NULL)
3323 *full = TRUE;
3324 amb_local = FALSE;
3325 break;
3326 }
3327 }
3328 }
3329 }
3330
3331 /* Stop if we found a full match or searched all. */
3332 if (j < gap->ga_len || gap == &ucmds)
3333 break;
3334 gap = &ucmds;
3335 }
3336
3337 /* Only found ambiguous matches. */
3338 if (amb_local)
3339 {
3340 if (xp != NULL)
3341 xp->xp_context = EXPAND_UNSUCCESSFUL;
3342 return NULL;
3343 }
3344
3345 /* The match we found may be followed immediately by a number. Move "p"
3346 * back to point to it. */
3347 if (found || possible)
3348 return p + (matchlen - len);
3349 return p;
3350}
3351#endif
3352
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003354static struct cmdmod
3355{
3356 char *name;
3357 int minlen;
3358 int has_count; /* :123verbose :3tab */
3359} cmdmods[] = {
3360 {"aboveleft", 3, FALSE},
3361 {"belowright", 3, FALSE},
3362 {"botright", 2, FALSE},
3363 {"browse", 3, FALSE},
3364 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003365 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003366 {"hide", 3, FALSE},
3367 {"keepalt", 5, FALSE},
3368 {"keepjumps", 5, FALSE},
3369 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003370 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003371 {"leftabove", 5, FALSE},
3372 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003373 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003374 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003375 {"rightbelow", 6, FALSE},
3376 {"sandbox", 3, FALSE},
3377 {"silent", 3, FALSE},
3378 {"tab", 3, TRUE},
3379 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003380 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003381 {"verbose", 4, TRUE},
3382 {"vertical", 4, FALSE},
3383};
3384
3385/*
3386 * Return length of a command modifier (including optional count).
3387 * Return zero when it's not a modifier.
3388 */
3389 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003390modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003391{
3392 int i, j;
3393 char_u *p = cmd;
3394
3395 if (VIM_ISDIGIT(*cmd))
3396 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003397 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003398 {
3399 for (j = 0; p[j] != NUL; ++j)
3400 if (p[j] != cmdmods[i].name[j])
3401 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003402 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003403 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003404 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003405 }
3406 return 0;
3407}
3408
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409/*
3410 * Return > 0 if an Ex command "name" exists.
3411 * Return 2 if there is an exact match.
3412 * Return 3 if there is an ambiguous match.
3413 */
3414 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003415cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416{
3417 exarg_T ea;
3418 int full = FALSE;
3419 int i;
3420 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003421 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422
3423 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003424 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 {
3426 for (j = 0; name[j] != NUL; ++j)
3427 if (name[j] != cmdmods[i].name[j])
3428 break;
3429 if (name[j] == NUL && j >= cmdmods[i].minlen)
3430 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3431 }
3432
Bram Moolenaara9587612006-05-04 21:47:50 +00003433 /* Check built-in commands and user defined commands.
3434 * For ":2match" and ":3match" we need to skip the number. */
3435 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003437 p = find_command(&ea, &full);
3438 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003440 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3441 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003442 if (*skipwhite(p) != NUL)
3443 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3445}
3446#endif
3447
3448/*
3449 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3450 * we don't need/want deleted. Maybe this could be done better if we didn't
3451 * repeat all this stuff. The only problem is that they may not stay
3452 * perfectly compatible with each other, but then the command line syntax
3453 * probably won't change that much -- webb.
3454 */
3455 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003456set_one_cmd_context(
3457 expand_T *xp,
3458 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459{
3460 char_u *p;
3461 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003462 int len = 0;
3463 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3465 int compl = EXPAND_NOTHING;
3466#endif
3467#ifdef FEAT_CMDL_COMPL
3468 int delim;
3469#endif
3470 int forceit = FALSE;
3471 int usefilter = FALSE; /* filter instead of file name */
3472
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003473 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 xp->xp_pattern = buff;
3475 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003476 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477
3478/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003479 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 */
3481 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3482 ;
3483 xp->xp_pattern = cmd;
3484
3485 if (*cmd == NUL)
3486 return NULL;
3487 if (*cmd == '"') /* ignore comment lines */
3488 {
3489 xp->xp_context = EXPAND_NOTHING;
3490 return NULL;
3491 }
3492
3493/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003494 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 */
3496 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 xp->xp_pattern = cmd;
3498 if (*cmd == NUL)
3499 return NULL;
3500 if (*cmd == '"')
3501 {
3502 xp->xp_context = EXPAND_NOTHING;
3503 return NULL;
3504 }
3505
3506 if (*cmd == '|' || *cmd == '\n')
3507 return cmd + 1; /* There's another command */
3508
3509 /*
3510 * Isolate the command and search for it in the command table.
3511 * Exceptions:
3512 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003513 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3515 */
3516 if (*cmd == 'k' && cmd[1] != 'e')
3517 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003518 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 p = cmd + 1;
3520 }
3521 else
3522 {
3523 p = cmd;
3524 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3525 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003526 /* a user command may contain digits */
3527 if (ASCII_ISUPPER(cmd[0]))
3528 while (ASCII_ISALNUM(*p) || *p == '*')
3529 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003530 /* for python 3.x: ":py3*" commands completion */
3531 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003532 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003533 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003534 while (ASCII_ISALPHA(*p) || *p == '*')
3535 ++p;
3536 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003537 /* check for non-alpha command */
3538 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3539 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003540 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003542 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 {
3544 xp->xp_context = EXPAND_UNSUCCESSFUL;
3545 return NULL;
3546 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003547 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003548 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3549 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3550 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 break;
3552
3553#ifdef FEAT_USR_CMDS
3554 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3556 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557#endif
3558 }
3559
3560 /*
3561 * If the cursor is touching the command, and it ends in an alpha-numeric
3562 * character, complete the command name.
3563 */
3564 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3565 return NULL;
3566
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003567 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 {
3569 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3570 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003571 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 p = cmd + 1;
3573 }
3574#ifdef FEAT_USR_CMDS
3575 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3576 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003577 ea.cmd = cmd;
3578 p = find_ucmd(&ea, p, NULL, xp,
3579# if defined(FEAT_CMDL_COMPL)
3580 &compl
3581# else
3582 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003584 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003585 if (p == NULL)
3586 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 }
3588#endif
3589 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003590 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 {
3592 /* Not still touching the command and it was an illegal one */
3593 xp->xp_context = EXPAND_UNSUCCESSFUL;
3594 return NULL;
3595 }
3596
3597 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3598
3599 if (*p == '!') /* forced commands */
3600 {
3601 forceit = TRUE;
3602 ++p;
3603 }
3604
3605/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003606 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003608 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003609 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610
3611 arg = skipwhite(p);
3612
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003613 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 {
3615 if (*arg == '>') /* append */
3616 {
3617 if (*++arg == '>')
3618 ++arg;
3619 arg = skipwhite(arg);
3620 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003621 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 {
3623 ++arg;
3624 usefilter = TRUE;
3625 }
3626 }
3627
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003628 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 {
3630 usefilter = forceit; /* :r! filter if forced */
3631 if (*arg == '!') /* :r !filter */
3632 {
3633 ++arg;
3634 usefilter = TRUE;
3635 }
3636 }
3637
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003638 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 {
3640 while (*arg == *cmd) /* allow any number of '>' or '<' */
3641 ++arg;
3642 arg = skipwhite(arg);
3643 }
3644
3645 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003646 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 {
3648 /* Check if we're in the +command */
3649 p = arg + 1;
3650 arg = skip_cmd_arg(arg, FALSE);
3651
3652 /* Still touching the command after '+'? */
3653 if (*arg == NUL)
3654 return p;
3655
3656 /* Skip space(s) after +command to get to the real argument */
3657 arg = skipwhite(arg);
3658 }
3659
3660 /*
3661 * Check for '|' to separate commands and '"' to start comments.
3662 * Don't do this for ":read !cmd" and ":write !cmd".
3663 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003664 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 {
3666 p = arg;
3667 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003668 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 p += 2;
3670 while (*p)
3671 {
3672 if (*p == Ctrl_V)
3673 {
3674 if (p[1] != NUL)
3675 ++p;
3676 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003677 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 || *p == '|' || *p == '\n')
3679 {
3680 if (*(p - 1) != '\\')
3681 {
3682 if (*p == '|' || *p == '\n')
3683 return p + 1;
3684 return NULL; /* It's a comment */
3685 }
3686 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003687 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 }
3689 }
3690
3691 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003692 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 vim_strchr((char_u *)"|\"", *arg) == NULL)
3694 return NULL;
3695
3696 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003697 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003699 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003700 while (*p && p < buff + len)
3701 {
3702 if (*p == ' ' || *p == TAB)
3703 {
3704 /* argument starts after a space */
3705 xp->xp_pattern = ++p;
3706 }
3707 else
3708 {
3709 if (*p == '\\' && *(p + 1) != NUL)
3710 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003711 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003712 }
3713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003715 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003717 int c;
3718 int in_quote = FALSE;
3719 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720
3721 /*
3722 * Allow spaces within back-quotes to count as part of the argument
3723 * being expanded.
3724 */
3725 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003726 p = xp->xp_pattern;
3727 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003729#ifdef FEAT_MBYTE
3730 if (has_mbyte)
3731 c = mb_ptr2char(p);
3732 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003734 c = *p;
3735 if (c == '\\' && p[1] != NUL)
3736 ++p;
3737 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 {
3739 if (!in_quote)
3740 {
3741 xp->xp_pattern = p;
3742 bow = p + 1;
3743 }
3744 in_quote = !in_quote;
3745 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003746 /* An argument can contain just about everything, except
3747 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003748 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003749#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003750 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003751#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003752 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003753 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003754 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003755 while (*p != NUL)
3756 {
3757#ifdef FEAT_MBYTE
3758 if (has_mbyte)
3759 c = mb_ptr2char(p);
3760 else
3761#endif
3762 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003763 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003764 break;
3765#ifdef FEAT_MBYTE
3766 if (has_mbyte)
3767 len = (*mb_ptr2len)(p);
3768 else
3769#endif
3770 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003771 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003772 }
3773 if (in_quote)
3774 bow = p;
3775 else
3776 xp->xp_pattern = p;
3777 p -= len;
3778 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003779 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 }
3781
3782 /*
3783 * If we are still inside the quotes, and we passed a space, just
3784 * expand from there.
3785 */
3786 if (bow != NULL && in_quote)
3787 xp->xp_pattern = bow;
3788 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003789
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003790 /* For a shell command more chars need to be escaped. */
3791 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003792 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003793#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003794 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003795#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003796 /* When still after the command name expand executables. */
3797 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003798 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800
3801 /* Check for environment variable */
3802 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003803#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 || *xp->xp_pattern == '%'
3805#endif
3806 )
3807 {
3808 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3809 if (!vim_isIDc(*p))
3810 break;
3811 if (*p == NUL)
3812 {
3813 xp->xp_context = EXPAND_ENV_VARS;
3814 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003815#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3816 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003817 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003818 compl = EXPAND_ENV_VARS;
3819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 }
3821 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003822#if defined(FEAT_CMDL_COMPL)
3823 /* Check for user names */
3824 if (*xp->xp_pattern == '~')
3825 {
3826 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3827 ;
3828 /* Complete ~user only if it partially matches a user name.
3829 * A full match ~user<Tab> will be replaced by user's home
3830 * directory i.e. something like ~user<Tab> -> /home/user/ */
3831 if (*p == NUL && p > xp->xp_pattern + 1
3832 && match_user(xp->xp_pattern + 1) == 1)
3833 {
3834 xp->xp_context = EXPAND_USER;
3835 ++xp->xp_pattern;
3836 }
3837 }
3838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 }
3840
3841/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003842 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003844 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003846 case CMD_find:
3847 case CMD_sfind:
3848 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003849 if (xp->xp_context == EXPAND_FILES)
3850 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003851 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 case CMD_cd:
3853 case CMD_chdir:
3854 case CMD_lcd:
3855 case CMD_lchdir:
3856 if (xp->xp_context == EXPAND_FILES)
3857 xp->xp_context = EXPAND_DIRECTORIES;
3858 break;
3859 case CMD_help:
3860 xp->xp_context = EXPAND_HELP;
3861 xp->xp_pattern = arg;
3862 break;
3863
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003864 /* Command modifiers: return the argument.
3865 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003867 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 case CMD_belowright:
3869 case CMD_botright:
3870 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003871 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003872 case CMD_cdo:
3873 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003875 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 case CMD_folddoclosed:
3877 case CMD_folddoopen:
3878 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003879 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 case CMD_keepjumps:
3881 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003882 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003883 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003885 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003887 case CMD_noautocmd:
3888 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003890 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003892 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003893 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 case CMD_topleft:
3895 case CMD_verbose:
3896 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003897 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 return arg;
3899
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003900 case CMD_filter:
3901 if (*arg != NUL)
3902 arg = skip_vimgrep_pat(arg, NULL, NULL);
3903 if (arg == NULL || *arg == NUL)
3904 {
3905 xp->xp_context = EXPAND_NOTHING;
3906 return NULL;
3907 }
3908 return skipwhite(arg);
3909
Bram Moolenaar4f688582007-07-24 12:34:30 +00003910#ifdef FEAT_CMDL_COMPL
3911# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 case CMD_match:
3913 if (*arg == NUL || !ends_excmd(*arg))
3914 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003915 /* also complete "None" */
3916 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 arg = skipwhite(skiptowhite(arg));
3918 if (*arg != NUL)
3919 {
3920 xp->xp_context = EXPAND_NOTHING;
3921 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3922 }
3923 }
3924 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003925# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927/*
3928 * All completion for the +cmdline_compl feature goes here.
3929 */
3930
3931# ifdef FEAT_USR_CMDS
3932 case CMD_command:
3933 /* Check for attributes */
3934 while (*arg == '-')
3935 {
3936 arg++; /* Skip "-" */
3937 p = skiptowhite(arg);
3938 if (*p == NUL)
3939 {
3940 /* Cursor is still in the attribute */
3941 p = vim_strchr(arg, '=');
3942 if (p == NULL)
3943 {
3944 /* No "=", so complete attribute names */
3945 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3946 xp->xp_pattern = arg;
3947 return NULL;
3948 }
3949
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003950 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 * their arguments as well.
3952 */
3953 if (STRNICMP(arg, "complete", p - arg) == 0)
3954 {
3955 xp->xp_context = EXPAND_USER_COMPLETE;
3956 xp->xp_pattern = p + 1;
3957 return NULL;
3958 }
3959 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3960 {
3961 xp->xp_context = EXPAND_USER_NARGS;
3962 xp->xp_pattern = p + 1;
3963 return NULL;
3964 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003965 else if (STRNICMP(arg, "addr", p - arg) == 0)
3966 {
3967 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3968 xp->xp_pattern = p + 1;
3969 return NULL;
3970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 return NULL;
3972 }
3973 arg = skipwhite(p);
3974 }
3975
3976 /* After the attributes comes the new command name */
3977 p = skiptowhite(arg);
3978 if (*p == NUL)
3979 {
3980 xp->xp_context = EXPAND_USER_COMMANDS;
3981 xp->xp_pattern = arg;
3982 break;
3983 }
3984
3985 /* And finally comes a normal command */
3986 return skipwhite(p);
3987
3988 case CMD_delcommand:
3989 xp->xp_context = EXPAND_USER_COMMANDS;
3990 xp->xp_pattern = arg;
3991 break;
3992# endif
3993
3994 case CMD_global:
3995 case CMD_vglobal:
3996 delim = *arg; /* get the delimiter */
3997 if (delim)
3998 ++arg; /* skip delimiter if there is one */
3999
4000 while (arg[0] != NUL && arg[0] != delim)
4001 {
4002 if (arg[0] == '\\' && arg[1] != NUL)
4003 ++arg;
4004 ++arg;
4005 }
4006 if (arg[0] != NUL)
4007 return arg + 1;
4008 break;
4009 case CMD_and:
4010 case CMD_substitute:
4011 delim = *arg;
4012 if (delim)
4013 {
4014 /* skip "from" part */
4015 ++arg;
4016 arg = skip_regexp(arg, delim, p_magic, NULL);
4017 }
4018 /* skip "to" part */
4019 while (arg[0] != NUL && arg[0] != delim)
4020 {
4021 if (arg[0] == '\\' && arg[1] != NUL)
4022 ++arg;
4023 ++arg;
4024 }
4025 if (arg[0] != NUL) /* skip delimiter */
4026 ++arg;
4027 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
4028 ++arg;
4029 if (arg[0] != NUL)
4030 return arg;
4031 break;
4032 case CMD_isearch:
4033 case CMD_dsearch:
4034 case CMD_ilist:
4035 case CMD_dlist:
4036 case CMD_ijump:
4037 case CMD_psearch:
4038 case CMD_djump:
4039 case CMD_isplit:
4040 case CMD_dsplit:
4041 arg = skipwhite(skipdigits(arg)); /* skip count */
4042 if (*arg == '/') /* Match regexp, not just whole words */
4043 {
4044 for (++arg; *arg && *arg != '/'; arg++)
4045 if (*arg == '\\' && arg[1] != NUL)
4046 arg++;
4047 if (*arg)
4048 {
4049 arg = skipwhite(arg + 1);
4050
4051 /* Check for trailing illegal characters */
4052 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4053 xp->xp_context = EXPAND_NOTHING;
4054 else
4055 return arg;
4056 }
4057 }
4058 break;
4059#ifdef FEAT_AUTOCMD
4060 case CMD_autocmd:
4061 return set_context_in_autocmd(xp, arg, FALSE);
4062
4063 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004064 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 return set_context_in_autocmd(xp, arg, TRUE);
4066#endif
4067 case CMD_set:
4068 set_context_in_set_cmd(xp, arg, 0);
4069 break;
4070 case CMD_setglobal:
4071 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4072 break;
4073 case CMD_setlocal:
4074 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4075 break;
4076 case CMD_tag:
4077 case CMD_stag:
4078 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004079 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 case CMD_tselect:
4081 case CMD_stselect:
4082 case CMD_ptselect:
4083 case CMD_tjump:
4084 case CMD_stjump:
4085 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004086 if (*p_wop != NUL)
4087 xp->xp_context = EXPAND_TAGS_LISTFILES;
4088 else
4089 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 xp->xp_pattern = arg;
4091 break;
4092 case CMD_augroup:
4093 xp->xp_context = EXPAND_AUGROUP;
4094 xp->xp_pattern = arg;
4095 break;
4096#ifdef FEAT_SYN_HL
4097 case CMD_syntax:
4098 set_context_in_syntax_cmd(xp, arg);
4099 break;
4100#endif
4101#ifdef FEAT_EVAL
4102 case CMD_let:
4103 case CMD_if:
4104 case CMD_elseif:
4105 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004106 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 case CMD_echo:
4108 case CMD_echon:
4109 case CMD_execute:
4110 case CMD_echomsg:
4111 case CMD_echoerr:
4112 case CMD_call:
4113 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004114 case CMD_cexpr:
4115 case CMD_caddexpr:
4116 case CMD_cgetexpr:
4117 case CMD_lexpr:
4118 case CMD_laddexpr:
4119 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004120 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 break;
4122
4123 case CMD_unlet:
4124 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4125 arg = xp->xp_pattern + 1;
4126 xp->xp_context = EXPAND_USER_VARS;
4127 xp->xp_pattern = arg;
4128 break;
4129
4130 case CMD_function:
4131 case CMD_delfunction:
4132 xp->xp_context = EXPAND_USER_FUNC;
4133 xp->xp_pattern = arg;
4134 break;
4135
4136 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004137 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 break;
4139#endif
4140 case CMD_highlight:
4141 set_context_in_highlight_cmd(xp, arg);
4142 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004143#ifdef FEAT_CSCOPE
4144 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004145 case CMD_lcscope:
4146 case CMD_scscope:
4147 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004148 break;
4149#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004150#ifdef FEAT_SIGNS
4151 case CMD_sign:
4152 set_context_in_sign_cmd(xp, arg);
4153 break;
4154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155#ifdef FEAT_LISTCMDS
4156 case CMD_bdelete:
4157 case CMD_bwipeout:
4158 case CMD_bunload:
4159 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4160 arg = xp->xp_pattern + 1;
4161 /*FALLTHROUGH*/
4162 case CMD_buffer:
4163 case CMD_sbuffer:
4164 case CMD_checktime:
4165 xp->xp_context = EXPAND_BUFFERS;
4166 xp->xp_pattern = arg;
4167 break;
4168#endif
4169#ifdef FEAT_USR_CMDS
4170 case CMD_USER:
4171 case CMD_USER_BUF:
4172 if (compl != EXPAND_NOTHING)
4173 {
4174 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004175 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 {
4177# ifdef FEAT_MENU
4178 if (compl == EXPAND_MENUS)
4179 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4180# endif
4181 if (compl == EXPAND_COMMANDS)
4182 return arg;
4183 if (compl == EXPAND_MAPPINGS)
4184 return set_context_in_map_cmd(xp, (char_u *)"map",
4185 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004186 /* Find start of last argument. */
4187 p = arg;
4188 while (*p)
4189 {
4190 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004191 /* argument starts after a space */
4192 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004193 else if (*p == '\\' && *(p + 1) != NUL)
4194 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004195 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 xp->xp_pattern = arg;
4198 }
4199 xp->xp_context = compl;
4200 }
4201 break;
4202#endif
4203 case CMD_map: case CMD_noremap:
4204 case CMD_nmap: case CMD_nnoremap:
4205 case CMD_vmap: case CMD_vnoremap:
4206 case CMD_omap: case CMD_onoremap:
4207 case CMD_imap: case CMD_inoremap:
4208 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004209 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004210 case CMD_smap: case CMD_snoremap:
4211 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004213 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 case CMD_unmap:
4215 case CMD_nunmap:
4216 case CMD_vunmap:
4217 case CMD_ounmap:
4218 case CMD_iunmap:
4219 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004220 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004221 case CMD_sunmap:
4222 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004224 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 case CMD_abbreviate: case CMD_noreabbrev:
4226 case CMD_cabbrev: case CMD_cnoreabbrev:
4227 case CMD_iabbrev: case CMD_inoreabbrev:
4228 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004229 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 case CMD_unabbreviate:
4231 case CMD_cunabbrev:
4232 case CMD_iunabbrev:
4233 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004234 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235#ifdef FEAT_MENU
4236 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4237 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4238 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4239 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4240 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4241 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4242 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4243 case CMD_tmenu: case CMD_tunmenu:
4244 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4245 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4246#endif
4247
4248 case CMD_colorscheme:
4249 xp->xp_context = EXPAND_COLORS;
4250 xp->xp_pattern = arg;
4251 break;
4252
4253 case CMD_compiler:
4254 xp->xp_context = EXPAND_COMPILER;
4255 xp->xp_pattern = arg;
4256 break;
4257
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004258 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004259 xp->xp_context = EXPAND_OWNSYNTAX;
4260 xp->xp_pattern = arg;
4261 break;
4262
4263 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004264 xp->xp_context = EXPAND_FILETYPE;
4265 xp->xp_pattern = arg;
4266 break;
4267
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004268 case CMD_packadd:
4269 xp->xp_context = EXPAND_PACKADD;
4270 xp->xp_pattern = arg;
4271 break;
4272
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4274 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4275 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004276 p = skiptowhite(arg);
4277 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 {
4279 xp->xp_context = EXPAND_LANGUAGE;
4280 xp->xp_pattern = arg;
4281 }
4282 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004283 {
4284 if ( STRNCMP(arg, "messages", p - arg) == 0
4285 || STRNCMP(arg, "ctype", p - arg) == 0
4286 || STRNCMP(arg, "time", p - arg) == 0)
4287 {
4288 xp->xp_context = EXPAND_LOCALES;
4289 xp->xp_pattern = skipwhite(p);
4290 }
4291 else
4292 xp->xp_context = EXPAND_NOTHING;
4293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 break;
4295#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004296#if defined(FEAT_PROFILE)
4297 case CMD_profile:
4298 set_context_in_profile_cmd(xp, arg);
4299 break;
4300#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004301 case CMD_behave:
4302 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004303 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004304 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004306 case CMD_messages:
4307 xp->xp_context = EXPAND_MESSAGES;
4308 xp->xp_pattern = arg;
4309 break;
4310
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004311#if defined(FEAT_CMDHIST)
4312 case CMD_history:
4313 xp->xp_context = EXPAND_HISTORY;
4314 xp->xp_pattern = arg;
4315 break;
4316#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004317#if defined(FEAT_PROFILE)
4318 case CMD_syntime:
4319 xp->xp_context = EXPAND_SYNTIME;
4320 xp->xp_pattern = arg;
4321 break;
4322#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004323
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324#endif /* FEAT_CMDL_COMPL */
4325
4326 default:
4327 break;
4328 }
4329 return NULL;
4330}
4331
4332/*
4333 * skip a range specifier of the form: addr [,addr] [;addr] ..
4334 *
4335 * Backslashed delimiters after / or ? will be skipped, and commands will
4336 * not be expanded between /'s and ?'s or after "'".
4337 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004338 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 * Returns the "cmd" pointer advanced to beyond the range.
4340 */
4341 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004342skip_range(
4343 char_u *cmd,
4344 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004346 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004348 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004350 if (*cmd == '\\')
4351 {
4352 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4353 ++cmd;
4354 else
4355 break;
4356 }
4357 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 {
4359 if (*++cmd == NUL && ctx != NULL)
4360 *ctx = EXPAND_NOTHING;
4361 }
4362 else if (*cmd == '/' || *cmd == '?')
4363 {
4364 delim = *cmd++;
4365 while (*cmd != NUL && *cmd != delim)
4366 if (*cmd++ == '\\' && *cmd != NUL)
4367 ++cmd;
4368 if (*cmd == NUL && ctx != NULL)
4369 *ctx = EXPAND_NOTHING;
4370 }
4371 if (*cmd != NUL)
4372 ++cmd;
4373 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004374
4375 /* Skip ":" and white space. */
4376 while (*cmd == ':')
4377 cmd = skipwhite(cmd + 1);
4378
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 return cmd;
4380}
4381
4382/*
4383 * get a single EX address
4384 *
4385 * Set ptr to the next character after the part that was interpreted.
4386 * Set ptr to NULL when an error is encountered.
4387 *
4388 * Return MAXLNUM when no Ex address was found.
4389 */
4390 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004391get_address(
4392 exarg_T *eap UNUSED,
4393 char_u **ptr,
4394 int addr_type, /* flag: one of ADDR_LINES, ... */
4395 int skip, /* only skip the address, don't use it */
Bram Moolenaarded27822017-01-02 14:27:34 +01004396 int to_other_file, /* flag: may jump to other file */
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01004397 int address_count UNUSED) /* 1 for first address, >1 after comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398{
4399 int c;
4400 int i;
4401 long n;
4402 char_u *cmd;
4403 pos_T pos;
4404 pos_T *fp;
4405 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004406 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407
4408 cmd = skipwhite(*ptr);
4409 lnum = MAXLNUM;
4410 do
4411 {
4412 switch (*cmd)
4413 {
4414 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004415 ++cmd;
4416 switch (addr_type)
4417 {
4418 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 lnum = curwin->w_cursor.lnum;
4420 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004421 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004422 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004423 break;
4424 case ADDR_ARGUMENTS:
4425 lnum = curwin->w_arg_idx + 1;
4426 break;
4427 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004428 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004429 lnum = curbuf->b_fnum;
4430 break;
4431 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004432 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004433 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004434 case ADDR_TABS_RELATIVE:
4435 EMSG(_(e_invrange));
4436 cmd = NULL;
4437 goto error;
4438 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004439#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004440 case ADDR_QUICKFIX:
4441 lnum = qf_get_cur_valid_idx(eap);
4442 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004443#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004444 }
4445 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446
4447 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004448 ++cmd;
4449 switch (addr_type)
4450 {
4451 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 lnum = curbuf->b_ml.ml_line_count;
4453 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004454 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004455 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004456 break;
4457 case ADDR_ARGUMENTS:
4458 lnum = ARGCOUNT;
4459 break;
4460 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004461 buf = lastbuf;
4462 while (buf->b_ml.ml_mfp == NULL)
4463 {
4464 if (buf->b_prev == NULL)
4465 break;
4466 buf = buf->b_prev;
4467 }
4468 lnum = buf->b_fnum;
4469 break;
4470 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004471 lnum = lastbuf->b_fnum;
4472 break;
4473 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004474 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004475 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004476 case ADDR_TABS_RELATIVE:
4477 EMSG(_(e_invrange));
4478 cmd = NULL;
4479 goto error;
4480 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004481#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004482 case ADDR_QUICKFIX:
4483 lnum = qf_get_size(eap);
4484 if (lnum == 0)
4485 lnum = 1;
4486 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004487#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004488 }
4489 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490
4491 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004492 if (*++cmd == NUL)
4493 {
4494 cmd = NULL;
4495 goto error;
4496 }
4497 if (addr_type != ADDR_LINES)
4498 {
4499 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004500 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004501 goto error;
4502 }
4503 if (skip)
4504 ++cmd;
4505 else
4506 {
4507 /* Only accept a mark in another file when it is
4508 * used by itself: ":'M". */
4509 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4510 ++cmd;
4511 if (fp == (pos_T *)-1)
4512 /* Jumped to another file. */
4513 lnum = curwin->w_cursor.lnum;
4514 else
4515 {
4516 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 {
4518 cmd = NULL;
4519 goto error;
4520 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004521 lnum = fp->lnum;
4522 }
4523 }
4524 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525
4526 case '/':
4527 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004528 c = *cmd++;
4529 if (addr_type != ADDR_LINES)
4530 {
4531 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004532 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004533 goto error;
4534 }
4535 if (skip) /* skip "/pat/" */
4536 {
4537 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4538 if (*cmd == c)
4539 ++cmd;
4540 }
4541 else
4542 {
4543 pos = curwin->w_cursor; /* save curwin->w_cursor */
4544 /*
4545 * When '/' or '?' follows another address, start
4546 * from there.
4547 */
4548 if (lnum != MAXLNUM)
4549 curwin->w_cursor.lnum = lnum;
4550 /*
4551 * Start a forward search at the end of the line.
4552 * Start a backward search at the start of the line.
4553 * This makes sure we never match in the current
4554 * line, and can match anywhere in the
4555 * next/previous line.
4556 */
4557 if (c == '/')
4558 curwin->w_cursor.col = MAXCOL;
4559 else
4560 curwin->w_cursor.col = 0;
4561 searchcmdlen = 0;
4562 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004563 SEARCH_HIS | SEARCH_MSG, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004564 {
4565 curwin->w_cursor = pos;
4566 cmd = NULL;
4567 goto error;
4568 }
4569 lnum = curwin->w_cursor.lnum;
4570 curwin->w_cursor = pos;
4571 /* adjust command string pointer */
4572 cmd += searchcmdlen;
4573 }
4574 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575
4576 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004577 ++cmd;
4578 if (addr_type != ADDR_LINES)
4579 {
4580 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004581 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004582 goto error;
4583 }
4584 if (*cmd == '&')
4585 i = RE_SUBST;
4586 else if (*cmd == '?' || *cmd == '/')
4587 i = RE_SEARCH;
4588 else
4589 {
4590 EMSG(_(e_backslash));
4591 cmd = NULL;
4592 goto error;
4593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004595 if (!skip)
4596 {
4597 /*
4598 * When search follows another address, start from
4599 * there.
4600 */
4601 if (lnum != MAXLNUM)
4602 pos.lnum = lnum;
4603 else
4604 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004606 /*
4607 * Start the search just like for the above
4608 * do_search().
4609 */
4610 if (*cmd != '?')
4611 pos.col = MAXCOL;
4612 else
4613 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004614#ifdef FEAT_VIRTUALEDIT
4615 pos.coladd = 0;
4616#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004617 if (searchit(curwin, curbuf, &pos,
4618 *cmd == '?' ? BACKWARD : FORWARD,
4619 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004620 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004621 lnum = pos.lnum;
4622 else
4623 {
4624 cmd = NULL;
4625 goto error;
4626 }
4627 }
4628 ++cmd;
4629 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630
4631 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004632 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4633 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 }
4635
4636 for (;;)
4637 {
4638 cmd = skipwhite(cmd);
4639 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4640 break;
4641
4642 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004643 {
4644 switch (addr_type)
4645 {
4646 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004647 /* "+1" is same as ".+1" */
4648 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004649 break;
4650 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004651 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004652 break;
4653 case ADDR_ARGUMENTS:
4654 lnum = curwin->w_arg_idx + 1;
4655 break;
4656 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004657 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004658 lnum = curbuf->b_fnum;
4659 break;
4660 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004661 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004662 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004663 case ADDR_TABS_RELATIVE:
4664 lnum = 1;
4665 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004666#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004667 case ADDR_QUICKFIX:
4668 lnum = qf_get_cur_valid_idx(eap);
4669 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004670#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004671 }
4672 }
4673
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 if (VIM_ISDIGIT(*cmd))
4675 i = '+'; /* "number" is same as "+number" */
4676 else
4677 i = *cmd++;
4678 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4679 n = 1;
4680 else
4681 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004682
4683 if (addr_type == ADDR_TABS_RELATIVE)
4684 {
4685 EMSG(_(e_invrange));
4686 cmd = NULL;
4687 goto error;
4688 }
4689 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004690 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004691 lnum = compute_buffer_local_count(
4692 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004694 {
4695#ifdef FEAT_FOLDING
4696 /* Relative line addressing, need to adjust for folded lines
4697 * now, but only do it after the first address. */
4698 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4699 && address_count >= 2)
4700 (void)hasFolding(lnum, NULL, &lnum);
4701#endif
4702 if (i == '-')
4703 lnum -= n;
4704 else
4705 lnum += n;
4706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 }
4708 } while (*cmd == '/' || *cmd == '?');
4709
4710error:
4711 *ptr = cmd;
4712 return lnum;
4713}
4714
4715/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004716 * Get flags from an Ex command argument.
4717 */
4718 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004719get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004720{
4721 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4722 {
4723 if (*eap->arg == 'l')
4724 eap->flags |= EXFLAG_LIST;
4725 else if (*eap->arg == 'p')
4726 eap->flags |= EXFLAG_PRINT;
4727 else
4728 eap->flags |= EXFLAG_NR;
4729 eap->arg = skipwhite(eap->arg + 1);
4730 }
4731}
4732
4733/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 * Function called for command which is Not Implemented. NI!
4735 */
4736 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004737ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738{
4739 if (!eap->skip)
4740 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4741}
4742
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004743#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744/*
4745 * Function called for script command which is Not Implemented. NI!
4746 * Skips over ":perl <<EOF" constructs.
4747 */
4748 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004749ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750{
4751 if (!eap->skip)
4752 ex_ni(eap);
4753 else
4754 vim_free(script_get(eap, eap->arg));
4755}
4756#endif
4757
4758/*
4759 * Check range in Ex command for validity.
4760 * Return NULL when valid, error message when invalid.
4761 */
4762 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004763invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004765 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 if ( eap->line1 < 0
4767 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004768 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004770
4771 if (eap->argt & RANGE)
4772 {
4773 switch(eap->addr_type)
4774 {
4775 case ADDR_LINES:
4776 if (!(eap->argt & NOTADR)
4777 && eap->line2 > curbuf->b_ml.ml_line_count
4778#ifdef FEAT_DIFF
4779 + (eap->cmdidx == CMD_diffget)
4780#endif
4781 )
4782 return (char_u *)_(e_invrange);
4783 break;
4784 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004785 /* add 1 if ARGCOUNT is 0 */
4786 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004787 return (char_u *)_(e_invrange);
4788 break;
4789 case ADDR_BUFFERS:
4790 if (eap->line1 < firstbuf->b_fnum
4791 || eap->line2 > lastbuf->b_fnum)
4792 return (char_u *)_(e_invrange);
4793 break;
4794 case ADDR_LOADED_BUFFERS:
4795 buf = firstbuf;
4796 while (buf->b_ml.ml_mfp == NULL)
4797 {
4798 if (buf->b_next == NULL)
4799 return (char_u *)_(e_invrange);
4800 buf = buf->b_next;
4801 }
4802 if (eap->line1 < buf->b_fnum)
4803 return (char_u *)_(e_invrange);
4804 buf = lastbuf;
4805 while (buf->b_ml.ml_mfp == NULL)
4806 {
4807 if (buf->b_prev == NULL)
4808 return (char_u *)_(e_invrange);
4809 buf = buf->b_prev;
4810 }
4811 if (eap->line2 > buf->b_fnum)
4812 return (char_u *)_(e_invrange);
4813 break;
4814 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004815 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004816 return (char_u *)_(e_invrange);
4817 break;
4818 case ADDR_TABS:
4819 if (eap->line2 > LAST_TAB_NR)
4820 return (char_u *)_(e_invrange);
4821 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004822 case ADDR_TABS_RELATIVE:
4823 /* Do nothing */
4824 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004825#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004826 case ADDR_QUICKFIX:
4827 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4828 return (char_u *)_(e_invrange);
4829 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004830#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004831 }
4832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 return NULL;
4834}
4835
4836/*
4837 * Correct the range for zero line number, if required.
4838 */
4839 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004840correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841{
4842 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4843 {
4844 if (eap->line1 == 0)
4845 eap->line1 = 1;
4846 if (eap->line2 == 0)
4847 eap->line2 = 1;
4848 }
4849}
4850
Bram Moolenaar748bf032005-02-02 23:04:36 +00004851#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004852static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004853
4854/*
4855 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4856 * pattern. Otherwise return eap->arg.
4857 */
4858 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004859skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004860{
4861 char_u *p = eap->arg;
4862
Bram Moolenaara37420f2006-02-04 22:37:47 +00004863 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4864 || eap->cmdidx == CMD_vimgrepadd
4865 || eap->cmdidx == CMD_lvimgrepadd
4866 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004867 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004868 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004869 if (p == NULL)
4870 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004871 }
4872 return p;
4873}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004874
4875/*
4876 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4877 * in the command line, so that things like % get expanded.
4878 */
4879 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004880replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004881{
4882 char_u *new_cmdline;
4883 char_u *program;
4884 char_u *pos;
4885 char_u *ptr;
4886 int len;
4887 int i;
4888
4889 /*
4890 * Don't do it when ":vimgrep" is used for ":grep".
4891 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004892 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4893 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4894 || eap->cmdidx == CMD_grepadd
4895 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004896 && !grep_internal(eap->cmdidx))
4897 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004898 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4899 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004900 {
4901 if (*curbuf->b_p_gp == NUL)
4902 program = p_gp;
4903 else
4904 program = curbuf->b_p_gp;
4905 }
4906 else
4907 {
4908 if (*curbuf->b_p_mp == NUL)
4909 program = p_mp;
4910 else
4911 program = curbuf->b_p_mp;
4912 }
4913
4914 p = skipwhite(p);
4915
4916 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4917 {
4918 /* replace $* by given arguments */
4919 i = 1;
4920 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4921 ++i;
4922 len = (int)STRLEN(p);
4923 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4924 if (new_cmdline == NULL)
4925 return NULL; /* out of memory */
4926 ptr = new_cmdline;
4927 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4928 {
4929 i = (int)(pos - program);
4930 STRNCPY(ptr, program, i);
4931 STRCPY(ptr += i, p);
4932 ptr += len;
4933 program = pos + 2;
4934 }
4935 STRCPY(ptr, program);
4936 }
4937 else
4938 {
4939 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4940 if (new_cmdline == NULL)
4941 return NULL; /* out of memory */
4942 STRCPY(new_cmdline, program);
4943 STRCAT(new_cmdline, " ");
4944 STRCAT(new_cmdline, p);
4945 }
4946 msg_make(p);
4947
4948 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4949 vim_free(*cmdlinep);
4950 *cmdlinep = new_cmdline;
4951 p = new_cmdline;
4952 }
4953 return p;
4954}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004955#endif
4956
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957/*
4958 * Expand file name in Ex command argument.
4959 * Return FAIL for failure, OK otherwise.
4960 */
4961 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004962expand_filename(
4963 exarg_T *eap,
4964 char_u **cmdlinep,
4965 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966{
4967 int has_wildcards; /* need to expand wildcards */
4968 char_u *repl;
4969 int srclen;
4970 char_u *p;
4971 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004972 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973
Bram Moolenaar748bf032005-02-02 23:04:36 +00004974#ifdef FEAT_QUICKFIX
4975 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4976 p = skip_grep_pat(eap);
4977#else
4978 p = eap->arg;
4979#endif
4980
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 /*
4982 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4983 * the file name contains a wildcard it should not cause expanding.
4984 * (it will be expanded anyway if there is a wildcard before replacing).
4985 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004986 has_wildcards = mch_has_wildcard(p);
4987 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004989#ifdef FEAT_EVAL
4990 /* Skip over `=expr`, wildcards in it are not expanded. */
4991 if (p[0] == '`' && p[1] == '=')
4992 {
4993 p += 2;
4994 (void)skip_expr(&p);
4995 if (*p == '`')
4996 ++p;
4997 continue;
4998 }
4999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 /*
5001 * Quick check if this cannot be the start of a special string.
5002 * Also removes backslash before '%', '#' and '<'.
5003 */
5004 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5005 {
5006 ++p;
5007 continue;
5008 }
5009
5010 /*
5011 * Try to find a match at this position.
5012 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005013 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
5014 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 if (*errormsgp != NULL) /* error detected */
5016 return FAIL;
5017 if (repl == NULL) /* no match found */
5018 {
5019 p += srclen;
5020 continue;
5021 }
5022
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005023 /* Wildcards won't be expanded below, the replacement is taken
5024 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
5025 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5026 {
5027 char_u *l = repl;
5028
5029 repl = expand_env_save(repl);
5030 vim_free(l);
5031 }
5032
Bram Moolenaar63b92542007-03-27 14:57:09 +00005033 /* Need to escape white space et al. with a backslash.
5034 * Don't do this for:
5035 * - replacement that already has been escaped: "##"
5036 * - shell commands (may have to use quotes instead).
5037 * - non-unix systems when there is a single argument (spaces don't
5038 * separate arguments then).
5039 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005041 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 && eap->cmdidx != CMD_bang
5043 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00005044 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00005046 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00005048 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005049 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050#ifndef UNIX
5051 && !(eap->argt & NOSPC)
5052#endif
5053 )
5054 {
5055 char_u *l;
5056#ifdef BACKSLASH_IN_FILENAME
5057 /* Don't escape a backslash here, because rem_backslash() doesn't
5058 * remove it later. */
5059 static char_u *nobslash = (char_u *)" \t\"|";
5060# define ESCAPE_CHARS nobslash
5061#else
5062# define ESCAPE_CHARS escape_chars
5063#endif
5064
5065 for (l = repl; *l; ++l)
5066 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5067 {
5068 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5069 if (l != NULL)
5070 {
5071 vim_free(repl);
5072 repl = l;
5073 }
5074 break;
5075 }
5076 }
5077
5078 /* For a shell command a '!' must be escaped. */
5079 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005080 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 {
5082 char_u *l;
5083
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005084 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 if (l != NULL)
5086 {
5087 vim_free(repl);
5088 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 }
5090 }
5091
5092 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5093 vim_free(repl);
5094 if (p == NULL)
5095 return FAIL;
5096 }
5097
5098 /*
5099 * One file argument: Expand wildcards.
5100 * Don't do this with ":r !command" or ":w !command".
5101 */
5102 if ((eap->argt & NOSPC) && !eap->usefilter)
5103 {
5104 /*
5105 * May do this twice:
5106 * 1. Replace environment variables.
5107 * 2. Replace any other wildcards, remove backslashes.
5108 */
5109 for (n = 1; n <= 2; ++n)
5110 {
5111 if (n == 2)
5112 {
5113#ifdef UNIX
5114 /*
5115 * Only for Unix we check for more than one file name.
5116 * For other systems spaces are considered to be part
5117 * of the file name.
5118 * Only check here if there is no wildcard, otherwise
5119 * ExpandOne() will check for errors. This allows
5120 * ":e `ls ve*.c`" on Unix.
5121 */
5122 if (!has_wildcards)
5123 for (p = eap->arg; *p; ++p)
5124 {
5125 /* skip escaped characters */
5126 if (p[1] && (*p == '\\' || *p == Ctrl_V))
5127 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005128 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 {
5130 *errormsgp = (char_u *)_("E172: Only one file name allowed");
5131 return FAIL;
5132 }
5133 }
5134#endif
5135
5136 /*
5137 * Halve the number of backslashes (this is Vi compatible).
5138 * For Unix and OS/2, when wildcards are expanded, this is
5139 * done by ExpandOne() below.
5140 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005141#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 if (!has_wildcards)
5143#endif
5144 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 }
5146
5147 if (has_wildcards)
5148 {
5149 if (n == 1)
5150 {
5151 /*
5152 * First loop: May expand environment variables. This
5153 * can be done much faster with expand_env() than with
5154 * something else (e.g., calling a shell).
5155 * After expanding environment variables, check again
5156 * if there are still wildcards present.
5157 */
5158 if (vim_strchr(eap->arg, '$') != NULL
5159 || vim_strchr(eap->arg, '~') != NULL)
5160 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005161 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005162 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 has_wildcards = mch_has_wildcard(NameBuff);
5164 p = NameBuff;
5165 }
5166 else
5167 p = NULL;
5168 }
5169 else /* n == 2 */
5170 {
5171 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005172 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173
5174 ExpandInit(&xpc);
5175 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005176 if (p_wic)
5177 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005179 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 if (p == NULL)
5181 return FAIL;
5182 }
5183 if (p != NULL)
5184 {
5185 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5186 p, cmdlinep);
5187 if (n == 2) /* p came from ExpandOne() */
5188 vim_free(p);
5189 }
5190 }
5191 }
5192 }
5193 return OK;
5194}
5195
5196/*
5197 * Replace part of the command line, keeping eap->cmd, eap->arg and
5198 * eap->nextcmd correct.
5199 * "src" points to the part that is to be replaced, of length "srclen".
5200 * "repl" is the replacement string.
5201 * Returns a pointer to the character after the replaced string.
5202 * Returns NULL for failure.
5203 */
5204 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005205repl_cmdline(
5206 exarg_T *eap,
5207 char_u *src,
5208 int srclen,
5209 char_u *repl,
5210 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211{
5212 int len;
5213 int i;
5214 char_u *new_cmdline;
5215
5216 /*
5217 * The new command line is build in new_cmdline[].
5218 * First allocate it.
5219 * Careful: a "+cmd" argument may have been NUL terminated.
5220 */
5221 len = (int)STRLEN(repl);
5222 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005223 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5225 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5226 return NULL; /* out of memory! */
5227
5228 /*
5229 * Copy the stuff before the expanded part.
5230 * Copy the expanded stuff.
5231 * Copy what came after the expanded part.
5232 * Copy the next commands, if there are any.
5233 */
5234 i = (int)(src - *cmdlinep); /* length of part before match */
5235 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005236
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 mch_memmove(new_cmdline + i, repl, (size_t)len);
5238 i += len; /* remember the end of the string */
5239 STRCPY(new_cmdline + i, src + srclen);
5240 src = new_cmdline + i; /* remember where to continue */
5241
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005242 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 {
5244 i = (int)STRLEN(new_cmdline) + 1;
5245 STRCPY(new_cmdline + i, eap->nextcmd);
5246 eap->nextcmd = new_cmdline + i;
5247 }
5248 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5249 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5250 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5251 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5252 vim_free(*cmdlinep);
5253 *cmdlinep = new_cmdline;
5254
5255 return src;
5256}
5257
5258/*
5259 * Check for '|' to separate commands and '"' to start comments.
5260 */
5261 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005262separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263{
5264 char_u *p;
5265
Bram Moolenaar86b68352004-12-27 21:59:20 +00005266#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005267 p = skip_grep_pat(eap);
5268#else
5269 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005270#endif
5271
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005272 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 {
5274 if (*p == Ctrl_V)
5275 {
5276 if (eap->argt & (USECTRLV | XFILE))
5277 ++p; /* skip CTRL-V and next char */
5278 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005279 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005280 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 if (*p == NUL) /* stop at NUL after CTRL-V */
5282 break;
5283 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005284
5285#ifdef FEAT_EVAL
5286 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005287 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005288 {
5289 p += 2;
5290 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005291 }
5292#endif
5293
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 /* Check for '"': start of comment or '|': next command */
5295 /* :@" and :*" do not start a comment!
5296 * :redir @" doesn't either. */
5297 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5298 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5299 || p != eap->arg)
5300 && (eap->cmdidx != CMD_redir
5301 || p != eap->arg + 1 || p[-1] != '@'))
5302 || *p == '|' || *p == '\n')
5303 {
5304 /*
5305 * We remove the '\' before the '|', unless USECTRLV is used
5306 * AND 'b' is present in 'cpoptions'.
5307 */
5308 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5309 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5310 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005311 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 --p;
5313 }
5314 else
5315 {
5316 eap->nextcmd = check_nextcmd(p);
5317 *p = NUL;
5318 break;
5319 }
5320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005322
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5324 del_trailing_spaces(eap->arg);
5325}
5326
5327/*
5328 * get + command from ex argument
5329 */
5330 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005331getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332{
5333 char_u *arg = *argp;
5334 char_u *command = NULL;
5335
5336 if (*arg == '+') /* +[command] */
5337 {
5338 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005339 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 command = dollar_command;
5341 else
5342 {
5343 command = arg;
5344 arg = skip_cmd_arg(command, TRUE);
5345 if (*arg != NUL)
5346 *arg++ = NUL; /* terminate command with NUL */
5347 }
5348
5349 arg = skipwhite(arg); /* skip over spaces */
5350 *argp = arg;
5351 }
5352 return command;
5353}
5354
5355/*
5356 * Find end of "+command" argument. Skip over "\ " and "\\".
5357 */
5358 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005359skip_cmd_arg(
5360 char_u *p,
5361 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362{
5363 while (*p && !vim_isspace(*p))
5364 {
5365 if (*p == '\\' && p[1] != NUL)
5366 {
5367 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005368 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 else
5370 ++p;
5371 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005372 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 }
5374 return p;
5375}
5376
5377/*
5378 * Get "++opt=arg" argument.
5379 * Return FAIL or OK.
5380 */
5381 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005382getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383{
5384 char_u *arg = eap->arg + 2;
5385 int *pp = NULL;
5386#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005387 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 char_u *p;
5389#endif
5390
5391 /* ":edit ++[no]bin[ary] file" */
5392 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5393 {
5394 if (*arg == 'n')
5395 {
5396 arg += 2;
5397 eap->force_bin = FORCE_NOBIN;
5398 }
5399 else
5400 eap->force_bin = FORCE_BIN;
5401 if (!checkforcmd(&arg, "binary", 3))
5402 return FAIL;
5403 eap->arg = skipwhite(arg);
5404 return OK;
5405 }
5406
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005407 /* ":read ++edit file" */
5408 if (STRNCMP(arg, "edit", 4) == 0)
5409 {
5410 eap->read_edit = TRUE;
5411 eap->arg = skipwhite(arg + 4);
5412 return OK;
5413 }
5414
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 if (STRNCMP(arg, "ff", 2) == 0)
5416 {
5417 arg += 2;
5418 pp = &eap->force_ff;
5419 }
5420 else if (STRNCMP(arg, "fileformat", 10) == 0)
5421 {
5422 arg += 10;
5423 pp = &eap->force_ff;
5424 }
5425#ifdef FEAT_MBYTE
5426 else if (STRNCMP(arg, "enc", 3) == 0)
5427 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005428 if (STRNCMP(arg, "encoding", 8) == 0)
5429 arg += 8;
5430 else
5431 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 pp = &eap->force_enc;
5433 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005434 else if (STRNCMP(arg, "bad", 3) == 0)
5435 {
5436 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005437 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439#endif
5440
5441 if (pp == NULL || *arg != '=')
5442 return FAIL;
5443
5444 ++arg;
5445 *pp = (int)(arg - eap->cmd);
5446 arg = skip_cmd_arg(arg, FALSE);
5447 eap->arg = skipwhite(arg);
5448 *arg = NUL;
5449
5450#ifdef FEAT_MBYTE
5451 if (pp == &eap->force_ff)
5452 {
5453#endif
5454 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5455 return FAIL;
5456#ifdef FEAT_MBYTE
5457 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005458 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 {
5460 /* Make 'fileencoding' lower case. */
5461 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5462 *p = TOLOWER_ASC(*p);
5463 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005464 else
5465 {
5466 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5467 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005468 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005469 if (STRICMP(p, "keep") == 0)
5470 eap->bad_char = BAD_KEEP;
5471 else if (STRICMP(p, "drop") == 0)
5472 eap->bad_char = BAD_DROP;
5473 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5474 eap->bad_char = *p;
5475 else
5476 return FAIL;
5477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478#endif
5479
5480 return OK;
5481}
5482
5483/*
5484 * ":abbreviate" and friends.
5485 */
5486 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005487ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488{
5489 do_exmap(eap, TRUE); /* almost the same as mapping */
5490}
5491
5492/*
5493 * ":map" and friends.
5494 */
5495 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005496ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497{
5498 /*
5499 * If we are sourcing .exrc or .vimrc in current directory we
5500 * print the mappings for security reasons.
5501 */
5502 if (secure)
5503 {
5504 secure = 2;
5505 msg_outtrans(eap->cmd);
5506 msg_putchar('\n');
5507 }
5508 do_exmap(eap, FALSE);
5509}
5510
5511/*
5512 * ":unmap" and friends.
5513 */
5514 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005515ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516{
5517 do_exmap(eap, FALSE);
5518}
5519
5520/*
5521 * ":mapclear" and friends.
5522 */
5523 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005524ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525{
5526 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5527}
5528
5529/*
5530 * ":abclear" and friends.
5531 */
5532 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005533ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534{
5535 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5536}
5537
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005538#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005540ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541{
5542 /*
5543 * Disallow auto commands from .exrc and .vimrc in current
5544 * directory for security reasons.
5545 */
5546 if (secure)
5547 {
5548 secure = 2;
5549 eap->errmsg = e_curdir;
5550 }
5551 else if (eap->cmdidx == CMD_autocmd)
5552 do_autocmd(eap->arg, eap->forceit);
5553 else
5554 do_augroup(eap->arg, eap->forceit);
5555}
5556
5557/*
5558 * ":doautocmd": Apply the automatic commands to the current buffer.
5559 */
5560 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005561ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005563 char_u *arg = eap->arg;
5564 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005565 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005566
Bram Moolenaar1610d052016-06-09 22:53:01 +02005567 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5568 /* Only when there is no <nomodeline>. */
5569 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005570 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571}
5572#endif
5573
5574#ifdef FEAT_LISTCMDS
5575/*
5576 * :[N]bunload[!] [N] [bufname] unload buffer
5577 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5578 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5579 */
5580 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005581ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582{
5583 eap->errmsg = do_bufdel(
5584 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5585 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5586 : DOBUF_UNLOAD, eap->arg,
5587 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5588}
5589
5590/*
5591 * :[N]buffer [N] to buffer N
5592 * :[N]sbuffer [N] to buffer N
5593 */
5594 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005595ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596{
5597 if (*eap->arg)
5598 eap->errmsg = e_trailing;
5599 else
5600 {
5601 if (eap->addr_count == 0) /* default is current buffer */
5602 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5603 else
5604 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005605 if (eap->do_ecmd_cmd != NULL)
5606 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 }
5608}
5609
5610/*
5611 * :[N]bmodified [N] to next mod. buffer
5612 * :[N]sbmodified [N] to next mod. buffer
5613 */
5614 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005615ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616{
5617 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005618 if (eap->do_ecmd_cmd != NULL)
5619 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620}
5621
5622/*
5623 * :[N]bnext [N] to next buffer
5624 * :[N]sbnext [N] split and to next buffer
5625 */
5626 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005627ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628{
5629 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005630 if (eap->do_ecmd_cmd != NULL)
5631 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632}
5633
5634/*
5635 * :[N]bNext [N] to previous buffer
5636 * :[N]bprevious [N] to previous buffer
5637 * :[N]sbNext [N] split and to previous buffer
5638 * :[N]sbprevious [N] split and to previous buffer
5639 */
5640 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005641ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642{
5643 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005644 if (eap->do_ecmd_cmd != NULL)
5645 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646}
5647
5648/*
5649 * :brewind to first buffer
5650 * :bfirst to first buffer
5651 * :sbrewind split and to first buffer
5652 * :sbfirst split and to first buffer
5653 */
5654 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005655ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656{
5657 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005658 if (eap->do_ecmd_cmd != NULL)
5659 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660}
5661
5662/*
5663 * :blast to last buffer
5664 * :sblast split and to last buffer
5665 */
5666 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005667ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668{
5669 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005670 if (eap->do_ecmd_cmd != NULL)
5671 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672}
5673#endif
5674
5675 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005676ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677{
5678 return (c == NUL || c == '|' || c == '"' || c == '\n');
5679}
5680
5681#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5682 || defined(PROTO)
5683/*
5684 * Return the next command, after the first '|' or '\n'.
5685 * Return NULL if not found.
5686 */
5687 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005688find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689{
5690 while (*p != '|' && *p != '\n')
5691 {
5692 if (*p == NUL)
5693 return NULL;
5694 ++p;
5695 }
5696 return (p + 1);
5697}
5698#endif
5699
5700/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005701 * Check if *p is a separator between Ex commands, skipping over white space.
5702 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 */
5704 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005705check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005707 char_u *s = skipwhite(p);
5708
5709 if (*s == '|' || *s == '\n')
5710 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 else
5712 return NULL;
5713}
5714
5715/*
5716 * - if there are more files to edit
5717 * - and this is the last window
5718 * - and forceit not used
5719 * - and not repeated twice on a row
5720 * return FAIL and give error message if 'message' TRUE
5721 * return OK otherwise
5722 */
5723 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005724check_more(
5725 int message, /* when FALSE check only, no messages */
5726 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727{
5728 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5729
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005730 if (!forceit && only_one_window()
5731 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 {
5733 if (message)
5734 {
5735#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5736 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5737 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005738 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739
5740 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005741 vim_strncpy(buff,
5742 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005743 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005745 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 _("%d more files to edit. Quit anyway?"), n);
5747 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5748 return OK;
5749 return FAIL;
5750 }
5751#endif
5752 if (n == 1)
5753 EMSG(_("E173: 1 more file to edit"));
5754 else
5755 EMSGN(_("E173: %ld more files to edit"), n);
5756 quitmore = 2; /* next try to quit is allowed */
5757 }
5758 return FAIL;
5759 }
5760 return OK;
5761}
5762
5763#ifdef FEAT_CMDL_COMPL
5764/*
5765 * Function given to ExpandGeneric() to obtain the list of command names.
5766 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005768get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769{
5770 if (idx >= (int)CMD_SIZE)
5771# ifdef FEAT_USR_CMDS
5772 return get_user_command_name(idx);
5773# else
5774 return NULL;
5775# endif
5776 return cmdnames[idx].cmd_name;
5777}
5778#endif
5779
5780#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005781static 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);
5782static void uc_list(char_u *name, size_t name_len);
5783static 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);
5784static char_u *uc_split_args(char_u *arg, size_t *lenp);
5785static 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 +00005786
5787 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005788uc_add_command(
5789 char_u *name,
5790 size_t name_len,
5791 char_u *rep,
5792 long argt,
5793 long def,
5794 int flags,
5795 int compl,
5796 char_u *compl_arg,
5797 int addr_type,
5798 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799{
5800 ucmd_T *cmd = NULL;
5801 char_u *p;
5802 int i;
5803 int cmp = 1;
5804 char_u *rep_buf = NULL;
5805 garray_T *gap;
5806
Bram Moolenaar9c102382006-05-03 21:26:49 +00005807 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 if (rep_buf == NULL)
5809 {
5810 /* Can't replace termcodes - try using the string as is */
5811 rep_buf = vim_strsave(rep);
5812
5813 /* Give up if out of memory */
5814 if (rep_buf == NULL)
5815 return FAIL;
5816 }
5817
5818 /* get address of growarray: global or in curbuf */
5819 if (flags & UC_BUFFER)
5820 {
5821 gap = &curbuf->b_ucmds;
5822 if (gap->ga_itemsize == 0)
5823 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5824 }
5825 else
5826 gap = &ucmds;
5827
5828 /* Search for the command in the already defined commands. */
5829 for (i = 0; i < gap->ga_len; ++i)
5830 {
5831 size_t len;
5832
5833 cmd = USER_CMD_GA(gap, i);
5834 len = STRLEN(cmd->uc_name);
5835 cmp = STRNCMP(name, cmd->uc_name, name_len);
5836 if (cmp == 0)
5837 {
5838 if (name_len < len)
5839 cmp = -1;
5840 else if (name_len > len)
5841 cmp = 1;
5842 }
5843
5844 if (cmp == 0)
5845 {
5846 if (!force)
5847 {
5848 EMSG(_("E174: Command already exists: add ! to replace it"));
5849 goto fail;
5850 }
5851
5852 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005853 cmd->uc_rep = NULL;
5854#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5855 vim_free(cmd->uc_compl_arg);
5856 cmd->uc_compl_arg = NULL;
5857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 break;
5859 }
5860
5861 /* Stop as soon as we pass the name to add */
5862 if (cmp < 0)
5863 break;
5864 }
5865
5866 /* Extend the array unless we're replacing an existing command */
5867 if (cmp != 0)
5868 {
5869 if (ga_grow(gap, 1) != OK)
5870 goto fail;
5871 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5872 goto fail;
5873
5874 cmd = USER_CMD_GA(gap, i);
5875 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5876
5877 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878
5879 cmd->uc_name = p;
5880 }
5881
5882 cmd->uc_rep = rep_buf;
5883 cmd->uc_argt = argt;
5884 cmd->uc_def = def;
5885 cmd->uc_compl = compl;
5886#ifdef FEAT_EVAL
5887 cmd->uc_scriptID = current_SID;
5888# ifdef FEAT_CMDL_COMPL
5889 cmd->uc_compl_arg = compl_arg;
5890# endif
5891#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005892 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893
5894 return OK;
5895
5896fail:
5897 vim_free(rep_buf);
5898#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5899 vim_free(compl_arg);
5900#endif
5901 return FAIL;
5902}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005905#if defined(FEAT_USR_CMDS)
5906static struct
5907{
5908 int expand;
5909 char *name;
5910} addr_type_complete[] =
5911{
5912 {ADDR_ARGUMENTS, "arguments"},
5913 {ADDR_LINES, "lines"},
5914 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5915 {ADDR_TABS, "tabs"},
5916 {ADDR_BUFFERS, "buffers"},
5917 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005918 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005919 {-1, NULL}
5920};
5921#endif
5922
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005923#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924/*
5925 * List of names for completion for ":command" with the EXPAND_ flag.
5926 * Must be alphabetical for completion.
5927 */
5928static struct
5929{
5930 int expand;
5931 char *name;
5932} command_complete[] =
5933{
5934 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005935 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005937 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005939 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005940#if defined(FEAT_CSCOPE)
5941 {EXPAND_CSCOPE, "cscope"},
5942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5944 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005945 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946#endif
5947 {EXPAND_DIRECTORIES, "dir"},
5948 {EXPAND_ENV_VARS, "environment"},
5949 {EXPAND_EVENTS, "event"},
5950 {EXPAND_EXPRESSION, "expression"},
5951 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005952 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005953 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 {EXPAND_FUNCTIONS, "function"},
5955 {EXPAND_HELP, "help"},
5956 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005957#if defined(FEAT_CMDHIST)
5958 {EXPAND_HISTORY, "history"},
5959#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005960#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005961 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005962 {EXPAND_LOCALES, "locale"},
5963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 {EXPAND_MAPPINGS, "mapping"},
5965 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005966 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005967 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005968#if defined(FEAT_PROFILE)
5969 {EXPAND_SYNTIME, "syntime"},
5970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005972 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005973 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005974#if defined(FEAT_SIGNS)
5975 {EXPAND_SIGN, "sign"},
5976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 {EXPAND_TAGS, "tag"},
5978 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005979 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 {EXPAND_USER_VARS, "var"},
5981 {0, NULL}
5982};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005985#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005987uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988{
5989 int i, j;
5990 int found = FALSE;
5991 ucmd_T *cmd;
5992 int len;
5993 long a;
5994 garray_T *gap;
5995
5996 gap = &curbuf->b_ucmds;
5997 for (;;)
5998 {
5999 for (i = 0; i < gap->ga_len; ++i)
6000 {
6001 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006002 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003
Bram Moolenaard29459b2016-08-26 22:29:11 +02006004 /* Skip commands which don't match the requested prefix and
6005 * commands filtered out. */
6006 if (STRNCMP(name, cmd->uc_name, name_len) != 0
6007 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 continue;
6009
6010 /* Put out the title first time */
6011 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006012 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 found = TRUE;
6014 msg_putchar('\n');
6015 if (got_int)
6016 break;
6017
6018 /* Special cases */
6019 msg_putchar(a & BANG ? '!' : ' ');
6020 msg_putchar(a & REGSTR ? '"' : ' ');
6021 msg_putchar(gap != &ucmds ? 'b' : ' ');
6022 msg_putchar(' ');
6023
Bram Moolenaar8820b482017-03-16 17:23:31 +01006024 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 len = (int)STRLEN(cmd->uc_name) + 4;
6026
6027 do {
6028 msg_putchar(' ');
6029 ++len;
6030 } while (len < 16);
6031
6032 len = 0;
6033
6034 /* Arguments */
6035 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
6036 {
6037 case 0: IObuff[len++] = '0'; break;
6038 case (EXTRA): IObuff[len++] = '*'; break;
6039 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
6040 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
6041 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
6042 }
6043
6044 do {
6045 IObuff[len++] = ' ';
6046 } while (len < 5);
6047
6048 /* Range */
6049 if (a & (RANGE|COUNT))
6050 {
6051 if (a & COUNT)
6052 {
6053 /* -count=N */
6054 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6055 len += (int)STRLEN(IObuff + len);
6056 }
6057 else if (a & DFLALL)
6058 IObuff[len++] = '%';
6059 else if (cmd->uc_def >= 0)
6060 {
6061 /* -range=N */
6062 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6063 len += (int)STRLEN(IObuff + len);
6064 }
6065 else
6066 IObuff[len++] = '.';
6067 }
6068
6069 do {
6070 IObuff[len++] = ' ';
6071 } while (len < 11);
6072
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006073 /* Address Type */
6074 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6075 if (addr_type_complete[j].expand != ADDR_LINES
6076 && addr_type_complete[j].expand == cmd->uc_addr_type)
6077 {
6078 STRCPY(IObuff + len, addr_type_complete[j].name);
6079 len += (int)STRLEN(IObuff + len);
6080 break;
6081 }
6082
6083 do {
6084 IObuff[len++] = ' ';
6085 } while (len < 21);
6086
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 /* Completion */
6088 for (j = 0; command_complete[j].expand != 0; ++j)
6089 if (command_complete[j].expand == cmd->uc_compl)
6090 {
6091 STRCPY(IObuff + len, command_complete[j].name);
6092 len += (int)STRLEN(IObuff + len);
6093 break;
6094 }
6095
6096 do {
6097 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006098 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099
6100 IObuff[len] = '\0';
6101 msg_outtrans(IObuff);
6102
6103 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006104#ifdef FEAT_EVAL
6105 if (p_verbose > 0)
6106 last_set_msg(cmd->uc_scriptID);
6107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 out_flush();
6109 ui_breakcheck();
6110 if (got_int)
6111 break;
6112 }
6113 if (gap == &ucmds || i < gap->ga_len)
6114 break;
6115 gap = &ucmds;
6116 }
6117
6118 if (!found)
6119 MSG(_("No user-defined commands found"));
6120}
6121
6122 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006123uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124{
6125 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6126 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6127 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6128 0xb9, 0x7f, 0};
6129 int i;
6130
6131 for (i = 0; fcmd[i]; ++i)
6132 IObuff[i] = fcmd[i] - 0x40;
6133 IObuff[i] = 0;
6134 return IObuff;
6135}
6136
6137 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006138uc_scan_attr(
6139 char_u *attr,
6140 size_t len,
6141 long *argt,
6142 long *def,
6143 int *flags,
6144 int *compl,
6145 char_u **compl_arg,
6146 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147{
6148 char_u *p;
6149
6150 if (len == 0)
6151 {
6152 EMSG(_("E175: No attribute specified"));
6153 return FAIL;
6154 }
6155
6156 /* First, try the simple attributes (no arguments) */
6157 if (STRNICMP(attr, "bang", len) == 0)
6158 *argt |= BANG;
6159 else if (STRNICMP(attr, "buffer", len) == 0)
6160 *flags |= UC_BUFFER;
6161 else if (STRNICMP(attr, "register", len) == 0)
6162 *argt |= REGSTR;
6163 else if (STRNICMP(attr, "bar", len) == 0)
6164 *argt |= TRLBAR;
6165 else
6166 {
6167 int i;
6168 char_u *val = NULL;
6169 size_t vallen = 0;
6170 size_t attrlen = len;
6171
6172 /* Look for the attribute name - which is the part before any '=' */
6173 for (i = 0; i < (int)len; ++i)
6174 {
6175 if (attr[i] == '=')
6176 {
6177 val = &attr[i + 1];
6178 vallen = len - i - 1;
6179 attrlen = i;
6180 break;
6181 }
6182 }
6183
6184 if (STRNICMP(attr, "nargs", attrlen) == 0)
6185 {
6186 if (vallen == 1)
6187 {
6188 if (*val == '0')
6189 /* Do nothing - this is the default */;
6190 else if (*val == '1')
6191 *argt |= (EXTRA | NOSPC | NEEDARG);
6192 else if (*val == '*')
6193 *argt |= EXTRA;
6194 else if (*val == '?')
6195 *argt |= (EXTRA | NOSPC);
6196 else if (*val == '+')
6197 *argt |= (EXTRA | NEEDARG);
6198 else
6199 goto wrong_nargs;
6200 }
6201 else
6202 {
6203wrong_nargs:
6204 EMSG(_("E176: Invalid number of arguments"));
6205 return FAIL;
6206 }
6207 }
6208 else if (STRNICMP(attr, "range", attrlen) == 0)
6209 {
6210 *argt |= RANGE;
6211 if (vallen == 1 && *val == '%')
6212 *argt |= DFLALL;
6213 else if (val != NULL)
6214 {
6215 p = val;
6216 if (*def >= 0)
6217 {
6218two_count:
6219 EMSG(_("E177: Count cannot be specified twice"));
6220 return FAIL;
6221 }
6222
6223 *def = getdigits(&p);
6224 *argt |= (ZEROR | NOTADR);
6225
6226 if (p != val + vallen || vallen == 0)
6227 {
6228invalid_count:
6229 EMSG(_("E178: Invalid default value for count"));
6230 return FAIL;
6231 }
6232 }
6233 }
6234 else if (STRNICMP(attr, "count", attrlen) == 0)
6235 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006236 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237
6238 if (val != NULL)
6239 {
6240 p = val;
6241 if (*def >= 0)
6242 goto two_count;
6243
6244 *def = getdigits(&p);
6245
6246 if (p != val + vallen)
6247 goto invalid_count;
6248 }
6249
6250 if (*def < 0)
6251 *def = 0;
6252 }
6253 else if (STRNICMP(attr, "complete", attrlen) == 0)
6254 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 if (val == NULL)
6256 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006257 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 return FAIL;
6259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006261 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6262 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006265 else if (STRNICMP(attr, "addr", attrlen) == 0)
6266 {
6267 *argt |= RANGE;
6268 if (val == NULL)
6269 {
6270 EMSG(_("E179: argument required for -addr"));
6271 return FAIL;
6272 }
6273 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6274 == FAIL)
6275 return FAIL;
6276 if (addr_type_arg != ADDR_LINES)
6277 *argt |= (ZEROR | NOTADR) ;
6278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 else
6280 {
6281 char_u ch = attr[len];
6282 attr[len] = '\0';
6283 EMSG2(_("E181: Invalid attribute: %s"), attr);
6284 attr[len] = ch;
6285 return FAIL;
6286 }
6287 }
6288
6289 return OK;
6290}
6291
Bram Moolenaara850a712009-01-28 14:42:59 +00006292/*
6293 * ":command ..."
6294 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006296ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297{
6298 char_u *name;
6299 char_u *end;
6300 char_u *p;
6301 long argt = 0;
6302 long def = -1;
6303 int flags = 0;
6304 int compl = EXPAND_NOTHING;
6305 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006306 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006308 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309
6310 p = eap->arg;
6311
6312 /* Check for attributes */
6313 while (*p == '-')
6314 {
6315 ++p;
6316 end = skiptowhite(p);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006317 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 == FAIL)
6319 return;
6320 p = skipwhite(end);
6321 }
6322
6323 /* Get the name (if any) and skip to the following argument */
6324 name = p;
6325 if (ASCII_ISALPHA(*p))
6326 while (ASCII_ISALNUM(*p))
6327 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006328 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 {
6330 EMSG(_("E182: Invalid command name"));
6331 return;
6332 }
6333 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006334 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335
6336 /* If there is nothing after the name, and no attributes were specified,
6337 * we are listing commands
6338 */
6339 p = skipwhite(end);
6340 if (!has_attr && ends_excmd(*p))
6341 {
6342 uc_list(name, end - name);
6343 }
6344 else if (!ASCII_ISUPPER(*name))
6345 {
6346 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6347 return;
6348 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006349 else if ((name_len == 1 && *name == 'X')
6350 || (name_len <= 4
6351 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6352 {
6353 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6354 return;
6355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 else
6357 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006358 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359}
6360
6361/*
6362 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 * Clear all user commands, global and for current buffer.
6364 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006365 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006366ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367{
6368 uc_clear(&ucmds);
6369 uc_clear(&curbuf->b_ucmds);
6370}
6371
6372/*
6373 * Clear all user commands for "gap".
6374 */
6375 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006376uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377{
6378 int i;
6379 ucmd_T *cmd;
6380
6381 for (i = 0; i < gap->ga_len; ++i)
6382 {
6383 cmd = USER_CMD_GA(gap, i);
6384 vim_free(cmd->uc_name);
6385 vim_free(cmd->uc_rep);
6386# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6387 vim_free(cmd->uc_compl_arg);
6388# endif
6389 }
6390 ga_clear(gap);
6391}
6392
6393 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006394ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395{
6396 int i = 0;
6397 ucmd_T *cmd = NULL;
6398 int cmp = -1;
6399 garray_T *gap;
6400
6401 gap = &curbuf->b_ucmds;
6402 for (;;)
6403 {
6404 for (i = 0; i < gap->ga_len; ++i)
6405 {
6406 cmd = USER_CMD_GA(gap, i);
6407 cmp = STRCMP(eap->arg, cmd->uc_name);
6408 if (cmp <= 0)
6409 break;
6410 }
6411 if (gap == &ucmds || cmp == 0)
6412 break;
6413 gap = &ucmds;
6414 }
6415
6416 if (cmp != 0)
6417 {
6418 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6419 return;
6420 }
6421
6422 vim_free(cmd->uc_name);
6423 vim_free(cmd->uc_rep);
6424# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6425 vim_free(cmd->uc_compl_arg);
6426# endif
6427
6428 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429
6430 if (i < gap->ga_len)
6431 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6432}
6433
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006434/*
6435 * split and quote args for <f-args>
6436 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006438uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439{
6440 char_u *buf;
6441 char_u *p;
6442 char_u *q;
6443 int len;
6444
6445 /* Precalculate length */
6446 p = arg;
6447 len = 2; /* Initial and final quotes */
6448
6449 while (*p)
6450 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006451 if (p[0] == '\\' && p[1] == '\\')
6452 {
6453 len += 2;
6454 p += 2;
6455 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006456 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 {
6458 len += 1;
6459 p += 2;
6460 }
6461 else if (*p == '\\' || *p == '"')
6462 {
6463 len += 2;
6464 p += 1;
6465 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006466 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 {
6468 p = skipwhite(p);
6469 if (*p == NUL)
6470 break;
6471 len += 3; /* "," */
6472 }
6473 else
6474 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006475#ifdef FEAT_MBYTE
6476 int charlen = (*mb_ptr2len)(p);
6477 len += charlen;
6478 p += charlen;
6479#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 ++len;
6481 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 }
6484 }
6485
6486 buf = alloc(len + 1);
6487 if (buf == NULL)
6488 {
6489 *lenp = 0;
6490 return buf;
6491 }
6492
6493 p = arg;
6494 q = buf;
6495 *q++ = '"';
6496 while (*p)
6497 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006498 if (p[0] == '\\' && p[1] == '\\')
6499 {
6500 *q++ = '\\';
6501 *q++ = '\\';
6502 p += 2;
6503 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006504 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 {
6506 *q++ = p[1];
6507 p += 2;
6508 }
6509 else if (*p == '\\' || *p == '"')
6510 {
6511 *q++ = '\\';
6512 *q++ = *p++;
6513 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006514 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 {
6516 p = skipwhite(p);
6517 if (*p == NUL)
6518 break;
6519 *q++ = '"';
6520 *q++ = ',';
6521 *q++ = '"';
6522 }
6523 else
6524 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006525 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 }
6527 }
6528 *q++ = '"';
6529 *q = 0;
6530
6531 *lenp = len;
6532 return buf;
6533}
6534
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006535 static size_t
6536add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6537{
6538 size_t result;
6539
6540 result = STRLEN(mod_str);
6541 if (*multi_mods)
6542 result += 1;
6543 if (buf != NULL)
6544 {
6545 if (*multi_mods)
6546 STRCAT(buf, " ");
6547 STRCAT(buf, mod_str);
6548 }
6549
6550 *multi_mods = 1;
6551
6552 return result;
6553}
6554
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555/*
6556 * Check for a <> code in a user command.
6557 * "code" points to the '<'. "len" the length of the <> (inclusive).
6558 * "buf" is where the result is to be added.
6559 * "split_buf" points to a buffer used for splitting, caller should free it.
6560 * "split_len" is the length of what "split_buf" contains.
6561 * Returns the length of the replacement, which has been added to "buf".
6562 * Returns -1 if there was no match, and only the "<" has been copied.
6563 */
6564 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006565uc_check_code(
6566 char_u *code,
6567 size_t len,
6568 char_u *buf,
6569 ucmd_T *cmd, /* the user command we're expanding */
6570 exarg_T *eap, /* ex arguments */
6571 char_u **split_buf,
6572 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573{
6574 size_t result = 0;
6575 char_u *p = code + 1;
6576 size_t l = len - 2;
6577 int quote = 0;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006578 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_MODS,
6579 ct_REGISTER, ct_LT, ct_NONE } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580
6581 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6582 {
6583 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6584 p += 2;
6585 l -= 2;
6586 }
6587
Bram Moolenaar371d5402006-03-20 21:47:49 +00006588 ++l;
6589 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006591 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006593 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006595 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006597 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006599 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006601 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006603 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006605 else if (STRNICMP(p, "mods>", l) == 0)
6606 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607
6608 switch (type)
6609 {
6610 case ct_ARGS:
6611 /* Simple case first */
6612 if (*eap->arg == NUL)
6613 {
6614 if (quote == 1)
6615 {
6616 result = 2;
6617 if (buf != NULL)
6618 STRCPY(buf, "''");
6619 }
6620 else
6621 result = 0;
6622 break;
6623 }
6624
6625 /* When specified there is a single argument don't split it.
6626 * Works for ":Cmd %" when % is "a b c". */
6627 if ((eap->argt & NOSPC) && quote == 2)
6628 quote = 1;
6629
6630 switch (quote)
6631 {
6632 case 0: /* No quoting, no splitting */
6633 result = STRLEN(eap->arg);
6634 if (buf != NULL)
6635 STRCPY(buf, eap->arg);
6636 break;
6637 case 1: /* Quote, but don't split */
6638 result = STRLEN(eap->arg) + 2;
6639 for (p = eap->arg; *p; ++p)
6640 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006641#ifdef FEAT_MBYTE
6642 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6643 /* DBCS can contain \ in a trail byte, skip the
6644 * double-byte character. */
6645 ++p;
6646 else
6647#endif
6648 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 ++result;
6650 }
6651
6652 if (buf != NULL)
6653 {
6654 *buf++ = '"';
6655 for (p = eap->arg; *p; ++p)
6656 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006657#ifdef FEAT_MBYTE
6658 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6659 /* DBCS can contain \ in a trail byte, copy the
6660 * double-byte character to avoid escaping. */
6661 *buf++ = *p++;
6662 else
6663#endif
6664 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 *buf++ = '\\';
6666 *buf++ = *p;
6667 }
6668 *buf = '"';
6669 }
6670
6671 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006672 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 /* This is hard, so only do it once, and cache the result */
6674 if (*split_buf == NULL)
6675 *split_buf = uc_split_args(eap->arg, split_len);
6676
6677 result = *split_len;
6678 if (buf != NULL && result != 0)
6679 STRCPY(buf, *split_buf);
6680
6681 break;
6682 }
6683 break;
6684
6685 case ct_BANG:
6686 result = eap->forceit ? 1 : 0;
6687 if (quote)
6688 result += 2;
6689 if (buf != NULL)
6690 {
6691 if (quote)
6692 *buf++ = '"';
6693 if (eap->forceit)
6694 *buf++ = '!';
6695 if (quote)
6696 *buf = '"';
6697 }
6698 break;
6699
6700 case ct_LINE1:
6701 case ct_LINE2:
6702 case ct_COUNT:
6703 {
6704 char num_buf[20];
6705 long num = (type == ct_LINE1) ? eap->line1 :
6706 (type == ct_LINE2) ? eap->line2 :
6707 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6708 size_t num_len;
6709
6710 sprintf(num_buf, "%ld", num);
6711 num_len = STRLEN(num_buf);
6712 result = num_len;
6713
6714 if (quote)
6715 result += 2;
6716
6717 if (buf != NULL)
6718 {
6719 if (quote)
6720 *buf++ = '"';
6721 STRCPY(buf, num_buf);
6722 buf += num_len;
6723 if (quote)
6724 *buf = '"';
6725 }
6726
6727 break;
6728 }
6729
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006730 case ct_MODS:
6731 {
6732 int multi_mods = 0;
6733 typedef struct {
6734 int *varp;
6735 char *name;
6736 } mod_entry_T;
6737 static mod_entry_T mod_entries[] = {
6738#ifdef FEAT_BROWSE_CMD
6739 {&cmdmod.browse, "browse"},
6740#endif
6741#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6742 {&cmdmod.confirm, "confirm"},
6743#endif
6744 {&cmdmod.hide, "hide"},
6745 {&cmdmod.keepalt, "keepalt"},
6746 {&cmdmod.keepjumps, "keepjumps"},
6747 {&cmdmod.keepmarks, "keepmarks"},
6748 {&cmdmod.keeppatterns, "keeppatterns"},
6749 {&cmdmod.lockmarks, "lockmarks"},
6750 {&cmdmod.noswapfile, "noswapfile"},
6751 {NULL, NULL}
6752 };
6753 int i;
6754
6755 result = quote ? 2 : 0;
6756 if (buf != NULL)
6757 {
6758 if (quote)
6759 *buf++ = '"';
6760 *buf = '\0';
6761 }
6762
6763#ifdef FEAT_WINDOWS
6764 /* :aboveleft and :leftabove */
6765 if (cmdmod.split & WSP_ABOVE)
6766 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6767 /* :belowright and :rightbelow */
6768 if (cmdmod.split & WSP_BELOW)
6769 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6770 /* :botright */
6771 if (cmdmod.split & WSP_BOT)
6772 result += add_cmd_modifier(buf, "botright", &multi_mods);
6773#endif
6774
6775 /* the modifiers that are simple flags */
6776 for (i = 0; mod_entries[i].varp != NULL; ++i)
6777 if (*mod_entries[i].varp)
6778 result += add_cmd_modifier(buf, mod_entries[i].name,
6779 &multi_mods);
6780
6781 /* TODO: How to support :noautocmd? */
6782#ifdef HAVE_SANDBOX
6783 /* TODO: How to support :sandbox?*/
6784#endif
6785 /* :silent */
6786 if (msg_silent > 0)
6787 result += add_cmd_modifier(buf,
6788 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
6789#ifdef FEAT_WINDOWS
6790 /* :tab */
6791 if (cmdmod.tab > 0)
6792 result += add_cmd_modifier(buf, "tab", &multi_mods);
6793 /* :topleft */
6794 if (cmdmod.split & WSP_TOP)
6795 result += add_cmd_modifier(buf, "topleft", &multi_mods);
6796#endif
6797 /* TODO: How to support :unsilent?*/
6798 /* :verbose */
6799 if (p_verbose > 0)
6800 result += add_cmd_modifier(buf, "verbose", &multi_mods);
6801#ifdef FEAT_WINDOWS
6802 /* :vertical */
6803 if (cmdmod.split & WSP_VERT)
6804 result += add_cmd_modifier(buf, "vertical", &multi_mods);
6805#endif
6806 if (quote && buf != NULL)
6807 {
6808 buf += result - 2;
6809 *buf = '"';
6810 }
6811 break;
6812 }
6813
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 case ct_REGISTER:
6815 result = eap->regname ? 1 : 0;
6816 if (quote)
6817 result += 2;
6818 if (buf != NULL)
6819 {
6820 if (quote)
6821 *buf++ = '\'';
6822 if (eap->regname)
6823 *buf++ = eap->regname;
6824 if (quote)
6825 *buf = '\'';
6826 }
6827 break;
6828
6829 case ct_LT:
6830 result = 1;
6831 if (buf != NULL)
6832 *buf = '<';
6833 break;
6834
6835 default:
6836 /* Not recognized: just copy the '<' and return -1. */
6837 result = (size_t)-1;
6838 if (buf != NULL)
6839 *buf = '<';
6840 break;
6841 }
6842
6843 return result;
6844}
6845
6846 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006847do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848{
6849 char_u *buf;
6850 char_u *p;
6851 char_u *q;
6852
6853 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006854 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006855 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 size_t len, totlen;
6857
6858 size_t split_len = 0;
6859 char_u *split_buf = NULL;
6860 ucmd_T *cmd;
6861#ifdef FEAT_EVAL
6862 scid_T save_current_SID = current_SID;
6863#endif
6864
6865 if (eap->cmdidx == CMD_USER)
6866 cmd = USER_CMD(eap->useridx);
6867 else
6868 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6869
6870 /*
6871 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006872 * First round: "buf" is NULL, compute length, allocate "buf".
6873 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 */
6875 buf = NULL;
6876 for (;;)
6877 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006878 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006879 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006881
6882 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006884 start = vim_strchr(p, '<');
6885 if (start != NULL)
6886 end = vim_strchr(start + 1, '>');
6887 if (buf != NULL)
6888 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006889 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6890 ;
6891 if (*ksp == K_SPECIAL
6892 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006893 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6894# ifdef FEAT_GUI
6895 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6896# endif
6897 ))
6898 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006899 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006900 * KS_SPECIAL KE_FILLER, like for mappings, but
6901 * do_cmdline() doesn't handle that, so convert it back.
6902 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6903 len = ksp - p;
6904 if (len > 0)
6905 {
6906 mch_memmove(q, p, len);
6907 q += len;
6908 }
6909 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6910 p = ksp + 3;
6911 continue;
6912 }
6913 }
6914
6915 /* break if there no <item> is found */
6916 if (start == NULL || end == NULL)
6917 break;
6918
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 /* Include the '>' */
6920 ++end;
6921
6922 /* Take everything up to the '<' */
6923 len = start - p;
6924 if (buf == NULL)
6925 totlen += len;
6926 else
6927 {
6928 mch_memmove(q, p, len);
6929 q += len;
6930 }
6931
6932 len = uc_check_code(start, end - start, q, cmd, eap,
6933 &split_buf, &split_len);
6934 if (len == (size_t)-1)
6935 {
6936 /* no match, continue after '<' */
6937 p = start + 1;
6938 len = 1;
6939 }
6940 else
6941 p = end;
6942 if (buf == NULL)
6943 totlen += len;
6944 else
6945 q += len;
6946 }
6947 if (buf != NULL) /* second time here, finished */
6948 {
6949 STRCPY(q, p);
6950 break;
6951 }
6952
6953 totlen += STRLEN(p); /* Add on the trailing characters */
6954 buf = alloc((unsigned)(totlen + 1));
6955 if (buf == NULL)
6956 {
6957 vim_free(split_buf);
6958 return;
6959 }
6960 }
6961
6962#ifdef FEAT_EVAL
6963 current_SID = cmd->uc_scriptID;
6964#endif
6965 (void)do_cmdline(buf, eap->getline, eap->cookie,
6966 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6967#ifdef FEAT_EVAL
6968 current_SID = save_current_SID;
6969#endif
6970 vim_free(buf);
6971 vim_free(split_buf);
6972}
6973
6974# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6975 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006976get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977{
6978 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6979}
6980
6981/*
6982 * Function given to ExpandGeneric() to obtain the list of user command names.
6983 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006985get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986{
6987 if (idx < curbuf->b_ucmds.ga_len)
6988 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6989 idx -= curbuf->b_ucmds.ga_len;
6990 if (idx < ucmds.ga_len)
6991 return USER_CMD(idx)->uc_name;
6992 return NULL;
6993}
6994
6995/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006996 * Function given to ExpandGeneric() to obtain the list of user address type names.
6997 */
6998 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006999get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007000{
7001 return (char_u *)addr_type_complete[idx].name;
7002}
7003
7004/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 * Function given to ExpandGeneric() to obtain the list of user command
7006 * attributes.
7007 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007009get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010{
7011 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007012 {"addr", "bang", "bar", "buffer", "complete",
7013 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007015 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 return NULL;
7017 return (char_u *)user_cmd_flags[idx];
7018}
7019
7020/*
7021 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
7022 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007024get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025{
7026 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
7027
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007028 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 return NULL;
7030 return (char_u *)user_cmd_nargs[idx];
7031}
7032
7033/*
7034 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007037get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038{
7039 return (char_u *)command_complete[idx].name;
7040}
7041# endif /* FEAT_CMDL_COMPL */
7042
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007043/*
7044 * Parse address type argument
7045 */
7046 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007047parse_addr_type_arg(
7048 char_u *value,
7049 int vallen,
7050 long *argt,
7051 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007052{
7053 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007054
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007055 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7056 {
7057 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7058 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7059 if (a && b)
7060 {
7061 *addr_type_arg = addr_type_complete[i].expand;
7062 break;
7063 }
7064 }
7065
7066 if (addr_type_complete[i].expand == -1)
7067 {
7068 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007069
Bram Moolenaar1c465442017-03-12 20:10:05 +01007070 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007071 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007072 err[i] = NUL;
7073 EMSG2(_("E180: Invalid address type value: %s"), err);
7074 return FAIL;
7075 }
7076
7077 if (*addr_type_arg != ADDR_LINES)
7078 *argt |= NOTADR;
7079
7080 return OK;
7081}
7082
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083#endif /* FEAT_USR_CMDS */
7084
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007085#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7086/*
7087 * Parse a completion argument "value[vallen]".
7088 * The detected completion goes in "*complp", argument type in "*argt".
7089 * When there is an argument, for function and user defined completion, it's
7090 * copied to allocated memory and stored in "*compl_arg".
7091 * Returns FAIL if something is wrong.
7092 */
7093 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007094parse_compl_arg(
7095 char_u *value,
7096 int vallen,
7097 int *complp,
7098 long *argt,
7099 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007100{
7101 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007102# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007103 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007104# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007105 int i;
7106 int valend = vallen;
7107
7108 /* Look for any argument part - which is the part after any ',' */
7109 for (i = 0; i < vallen; ++i)
7110 {
7111 if (value[i] == ',')
7112 {
7113 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007114# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007115 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007116# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007117 valend = i;
7118 break;
7119 }
7120 }
7121
7122 for (i = 0; command_complete[i].expand != 0; ++i)
7123 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007124 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007125 && STRNCMP(value, command_complete[i].name, valend) == 0)
7126 {
7127 *complp = command_complete[i].expand;
7128 if (command_complete[i].expand == EXPAND_BUFFERS)
7129 *argt |= BUFNAME;
7130 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7131 || command_complete[i].expand == EXPAND_FILES)
7132 *argt |= XFILE;
7133 break;
7134 }
7135 }
7136
7137 if (command_complete[i].expand == 0)
7138 {
7139 EMSG2(_("E180: Invalid complete value: %s"), value);
7140 return FAIL;
7141 }
7142
7143# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7144 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7145 && arg != NULL)
7146# else
7147 if (arg != NULL)
7148# endif
7149 {
7150 EMSG(_("E468: Completion argument only allowed for custom completion"));
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 {
7158 EMSG(_("E467: Custom completion requires a function argument"));
7159 return FAIL;
7160 }
7161
7162 if (arg != NULL)
7163 *compl_arg = vim_strnsave(arg, (int)arglen);
7164# endif
7165 return OK;
7166}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007167
7168 int
7169cmdcomplete_str_to_type(char_u *complete_str)
7170{
7171 int i;
7172
7173 for (i = 0; command_complete[i].expand != 0; ++i)
7174 if (STRCMP(complete_str, command_complete[i].name) == 0)
7175 return command_complete[i].expand;
7176
7177 return EXPAND_NOTHING;
7178}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007179#endif
7180
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007182ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183{
Bram Moolenaare6850792010-05-14 15:28:44 +02007184 if (*eap->arg == NUL)
7185 {
7186#ifdef FEAT_EVAL
7187 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7188 char_u *p = NULL;
7189
7190 if (expr != NULL)
7191 {
7192 ++emsg_off;
7193 p = eval_to_string(expr, NULL, FALSE);
7194 --emsg_off;
7195 vim_free(expr);
7196 }
7197 if (p != NULL)
7198 {
7199 MSG(p);
7200 vim_free(p);
7201 }
7202 else
7203 MSG("default");
7204#else
7205 MSG(_("unknown"));
7206#endif
7207 }
7208 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007209 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210}
7211
7212 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007213ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214{
7215 if (*eap->arg == NUL && eap->cmd[2] == '!')
7216 MSG(_("Greetings, Vim user!"));
7217 do_highlight(eap->arg, eap->forceit, FALSE);
7218}
7219
7220
7221/*
7222 * Call this function if we thought we were going to exit, but we won't
7223 * (because of an error). May need to restore the terminal mode.
7224 */
7225 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007226not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227{
7228 exiting = FALSE;
7229 settmode(TMODE_RAW);
7230}
7231
7232/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007233 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 */
7235 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007236ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237{
Bram Moolenaarf240e182014-11-27 18:33:02 +01007238#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007239 win_T *wp;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007240#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007241
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242#ifdef FEAT_CMDWIN
7243 if (cmdwin_type != 0)
7244 {
7245 cmdwin_result = Ctrl_C;
7246 return;
7247 }
7248#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007249 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007250 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007251 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007252 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007253 return;
7254 }
Bram Moolenaarf240e182014-11-27 18:33:02 +01007255#ifdef FEAT_WINDOWS
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007256 if (eap->addr_count > 0)
7257 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007258 int wnr = eap->line2;
7259
7260 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7261 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007262 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007263 }
7264 else
Bram Moolenaarf240e182014-11-27 18:33:02 +01007265#endif
7266#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007267 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007268#endif
7269
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007270#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02007271 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007272 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02007273 * being closed (can only happen in autocommands). */
Bram Moolenaar0ea50702017-07-08 14:44:50 +02007274 if (curbuf_locked()
7275# ifdef FEAT_WINDOWS
7276 || !win_valid(wp)
7277# endif
7278 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007279 return;
7280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281
7282#ifdef FEAT_NETBEANS_INTG
7283 netbeansForcedQuit = eap->forceit;
7284#endif
7285
7286 /*
7287 * If there are more files or windows we won't exit.
7288 */
7289 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7290 exiting = TRUE;
7291 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007292 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7293 | (eap->forceit ? CCGD_FORCEIT : 0)
7294 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007296 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 {
7298 not_exiting();
7299 }
7300 else
7301 {
7302#ifdef FEAT_WINDOWS
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007303 /* quit last window
7304 * Note: only_one_window() returns true, even so a help window is
7305 * still open. In that case only quit, if no address has been
7306 * specified. Example:
7307 * :h|wincmd w|1q - don't quit
7308 * :h|wincmd w|q - quit
7309 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007310 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311#endif
7312 getout(0);
7313#ifdef FEAT_WINDOWS
7314# ifdef FEAT_GUI
7315 need_mouse_correct = TRUE;
7316# endif
7317 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007318 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319#endif
7320 }
7321}
7322
7323/*
7324 * ":cquit".
7325 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007327ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328{
7329 getout(1); /* this does not always pass on the exit code to the Manx
7330 compiler. why? */
7331}
7332
7333/*
7334 * ":qall": try to quit all windows
7335 */
7336 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007337ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338{
7339# ifdef FEAT_CMDWIN
7340 if (cmdwin_type != 0)
7341 {
7342 if (eap->forceit)
7343 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7344 else
7345 cmdwin_result = K_XF2;
7346 return;
7347 }
7348# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007349
7350 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007351 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007352 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007353 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007354 return;
7355 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007356#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007357 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7358 /* Refuse to quit when locked or when the buffer in the last window is
7359 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007360 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007361 return;
7362#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007363
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007365 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 getout(0);
7367 not_exiting();
7368}
7369
Bram Moolenaard9967712006-03-11 21:18:15 +00007370#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371/*
7372 * ":close": close current window, unless it is the last one
7373 */
7374 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007375ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007377 win_T *win;
7378 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379# ifdef FEAT_CMDWIN
7380 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007381 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 else
7383# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007384 if (!text_locked()
7385#ifdef FEAT_AUTOCMD
7386 && !curbuf_locked()
7387#endif
7388 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007389 {
7390 if (eap->addr_count == 0)
7391 ex_win_close(eap->forceit, curwin, NULL);
7392 else {
Bram Moolenaar29323592016-07-24 22:04:11 +02007393 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007394 {
7395 winnr++;
7396 if (winnr == eap->line2)
7397 break;
7398 }
7399 if (win == NULL)
7400 win = lastwin;
7401 ex_win_close(eap->forceit, win, NULL);
7402 }
7403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404}
7405
Bram Moolenaard9967712006-03-11 21:18:15 +00007406# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007407/*
7408 * ":pclose": Close any preview window.
7409 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007411ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007412{
7413 win_T *win;
7414
Bram Moolenaar29323592016-07-24 22:04:11 +02007415 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007416 if (win->w_p_pvw)
7417 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007418 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007419 break;
7420 }
7421}
Bram Moolenaard9967712006-03-11 21:18:15 +00007422# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007423
Bram Moolenaarf740b292006-02-16 22:11:02 +00007424/*
7425 * Close window "win" and take care of handling closing the last window for a
7426 * modified buffer.
7427 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007428 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007429ex_win_close(
7430 int forceit,
7431 win_T *win,
7432 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433{
7434 int need_hide;
7435 buf_T *buf = win->w_buffer;
7436
7437 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007438 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007440# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 if ((p_confirm || cmdmod.confirm) && p_write)
7442 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007443 bufref_T bufref;
7444
7445 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007447 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 return;
7449 need_hide = FALSE;
7450 }
7451 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007452# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 {
7454 EMSG(_(e_nowrtmsg));
7455 return;
7456 }
7457 }
7458
Bram Moolenaard9967712006-03-11 21:18:15 +00007459# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007461# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007462
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007464 if (tp == NULL)
7465 win_close(win, !need_hide && !P_HID(buf));
7466 else
7467 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468}
7469
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007471 * Handle the argument for a tabpage related ex command.
7472 * Returns a tabpage number.
7473 * When an error is encountered then eap->errmsg is set.
7474 */
7475 static int
7476get_tabpage_arg(exarg_T *eap)
7477{
7478 int tab_number;
7479 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7480
7481 if (eap->arg && *eap->arg != NUL)
7482 {
7483 char_u *p = eap->arg;
7484 char_u *p_save;
7485 int relative = 0; /* argument +N/-N means: go to N places to the
7486 * right/left relative to the current position. */
7487
7488 if (*p == '-')
7489 {
7490 relative = -1;
7491 p++;
7492 }
7493 else if (*p == '+')
7494 {
7495 relative = 1;
7496 p++;
7497 }
7498
7499 p_save = p;
7500 tab_number = getdigits(&p);
7501
7502 if (relative == 0)
7503 {
7504 if (STRCMP(p, "$") == 0)
7505 tab_number = LAST_TAB_NR;
7506 else if (p == p_save || *p_save == '-' || *p != NUL
7507 || tab_number > LAST_TAB_NR)
7508 {
7509 /* No numbers as argument. */
7510 eap->errmsg = e_invarg;
7511 goto theend;
7512 }
7513 }
7514 else
7515 {
7516 if (*p_save == NUL)
7517 tab_number = 1;
7518 else if (p == p_save || *p_save == '-' || *p != NUL
7519 || tab_number == 0)
7520 {
7521 /* No numbers as argument. */
7522 eap->errmsg = e_invarg;
7523 goto theend;
7524 }
7525 tab_number = tab_number * relative + tabpage_index(curtab);
7526 if (!unaccept_arg0 && relative == -1)
7527 --tab_number;
7528 }
7529 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7530 eap->errmsg = e_invarg;
7531 }
7532 else if (eap->addr_count > 0)
7533 {
7534 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007535 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007536 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007537 tab_number = 0;
7538 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007539 else
7540 {
7541 tab_number = eap->line2;
7542 if (!unaccept_arg0 && **eap->cmdlinep == '-')
7543 {
7544 --tab_number;
7545 if (tab_number < unaccept_arg0)
7546 eap->errmsg = e_invarg;
7547 }
7548 }
7549 }
7550 else
7551 {
7552 switch (eap->cmdidx)
7553 {
7554 case CMD_tabnext:
7555 tab_number = tabpage_index(curtab) + 1;
7556 if (tab_number > LAST_TAB_NR)
7557 tab_number = 1;
7558 break;
7559 case CMD_tabmove:
7560 tab_number = LAST_TAB_NR;
7561 break;
7562 default:
7563 tab_number = tabpage_index(curtab);
7564 }
7565 }
7566
7567theend:
7568 return tab_number;
7569}
7570
7571/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007572 * ":tabclose": close current tab page, unless it is the last one.
7573 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 */
7575 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007576ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007578 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007579 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007580
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007581# ifdef FEAT_CMDWIN
7582 if (cmdwin_type != 0)
7583 cmdwin_result = K_IGNORE;
7584 else
7585# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007586 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007587 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007588 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007590 tab_number = get_tabpage_arg(eap);
7591 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007592 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007593 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007594 if (tp == NULL)
7595 {
7596 beep_flush();
7597 return;
7598 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007599 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007600 {
7601 tabpage_close_other(tp, eap->forceit);
7602 return;
7603 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007604 else if (!text_locked()
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007605#ifdef FEAT_AUTOCMD
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007606 && !curbuf_locked()
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007607#endif
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007608 )
7609 tabpage_close(eap->forceit);
7610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007612}
7613
7614/*
7615 * ":tabonly": close all tab pages except the current one
7616 */
7617 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007618ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007619{
7620 tabpage_T *tp;
7621 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007622 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007623
7624# ifdef FEAT_CMDWIN
7625 if (cmdwin_type != 0)
7626 cmdwin_result = K_IGNORE;
7627 else
7628# endif
7629 if (first_tabpage->tp_next == NULL)
7630 MSG(_("Already only one tab page"));
7631 else
7632 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007633 tab_number = get_tabpage_arg(eap);
7634 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007635 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007636 goto_tabpage(tab_number);
7637 /* Repeat this up to a 1000 times, because autocommands may
7638 * mess up the lists. */
7639 for (done = 0; done < 1000; ++done)
7640 {
7641 FOR_ALL_TABPAGES(tp)
7642 if (tp->tp_topframe != topframe)
7643 {
7644 tabpage_close_other(tp, eap->forceit);
7645 /* if we failed to close it quit */
7646 if (valid_tabpage(tp))
7647 done = 1000;
7648 /* start over, "tp" is now invalid */
7649 break;
7650 }
7651 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007652 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007653 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007654 }
7655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657
7658/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007659 * Close the current tab page.
7660 */
7661 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007662tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007663{
7664 /* First close all the windows but the current one. If that worked then
7665 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007666 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007667 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007668 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007669 ex_win_close(forceit, curwin, NULL);
7670# ifdef FEAT_GUI
7671 need_mouse_correct = TRUE;
7672# endif
7673}
7674
7675/*
7676 * Close tab page "tp", which is not the current tab page.
7677 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007678 * Also takes care of the tab pages line disappearing when closing the
7679 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007680 */
7681 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007682tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007683{
7684 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007685 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007686 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007687
7688 /* Limit to 1000 windows, autocommands may add a window while we close
7689 * one. OK, so I'm paranoid... */
7690 while (++done < 1000)
7691 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007692 wp = tp->tp_firstwin;
7693 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007694
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007695 /* Autocommands may delete the tab page under our fingers and we may
7696 * fail to close a window with a modified buffer. */
7697 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007698 break;
7699 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007700
Bram Moolenaar29323592016-07-24 22:04:11 +02007701#ifdef FEAT_AUTOCMD
7702 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
7703#endif
7704
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007705 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007706 if (h != tabline_height())
7707 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007708}
7709
7710/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 * ":only".
7712 */
7713 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007714ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007716 win_T *wp;
7717 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718# ifdef FEAT_GUI
7719 need_mouse_correct = TRUE;
7720# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007721 if (eap->addr_count > 0)
7722 {
7723 wnr = eap->line2;
7724 for (wp = firstwin; --wnr > 0; )
7725 {
7726 if (wp->w_next == NULL)
7727 break;
7728 else
7729 wp = wp->w_next;
7730 }
7731 win_goto(wp);
7732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 close_others(TRUE, eap->forceit);
7734}
7735
7736/*
7737 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007738 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007740 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007741ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742{
7743 if (eap->addr_count == 0)
7744 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007745 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746}
7747#endif /* FEAT_WINDOWS */
7748
7749 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007750ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007752 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753#ifdef FEAT_WINDOWS
Bram Moolenaar2256c992016-11-15 21:17:07 +01007754 if (!eap->skip)
7755 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756# ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007757 need_mouse_correct = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758# endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007759 if (eap->addr_count == 0)
7760 win_close(curwin, FALSE); /* don't free buffer */
7761 else
7762 {
7763 int winnr = 0;
7764 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007765
Bram Moolenaar2256c992016-11-15 21:17:07 +01007766 FOR_ALL_WINDOWS(win)
7767 {
7768 winnr++;
7769 if (winnr == eap->line2)
7770 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007771 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007772 if (win == NULL)
7773 win = lastwin;
7774 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778}
7779
7780/*
7781 * ":stop" and ":suspend": Suspend Vim.
7782 */
7783 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007784ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785{
7786 /*
7787 * Disallow suspending for "rvim".
7788 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007789 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 {
7791 if (!eap->forceit)
7792 autowrite_all();
7793 windgoto((int)Rows - 1, 0);
7794 out_char('\n');
7795 out_flush();
7796 stoptermcap();
7797 out_flush(); /* needed for SUN to restore xterm buffer */
7798#ifdef FEAT_TITLE
7799 mch_restore_title(3); /* restore window titles */
7800#endif
7801 ui_suspend(); /* call machine specific function */
7802#ifdef FEAT_TITLE
7803 maketitle();
7804 resettitle(); /* force updating the title */
7805#endif
7806 starttermcap();
7807 scroll_start(); /* scroll screen before redrawing */
7808 redraw_later_clear();
7809 shell_resized(); /* may have resized window */
7810 }
7811}
7812
7813/*
7814 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7815 */
7816 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007817ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818{
7819#ifdef FEAT_CMDWIN
7820 if (cmdwin_type != 0)
7821 {
7822 cmdwin_result = Ctrl_C;
7823 return;
7824 }
7825#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007826 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007827 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007828 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007829 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007830 return;
7831 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007832#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007833 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7834 /* Refuse to quit when locked or when the buffer in the last window is
7835 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007836 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007837 return;
7838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839
7840 /*
7841 * if more files or windows we won't exit
7842 */
7843 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7844 exiting = TRUE;
7845 if ( ((eap->cmdidx == CMD_wq
7846 || curbufIsChanged())
7847 && do_write(eap) == FAIL)
7848 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007849 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 {
7851 not_exiting();
7852 }
7853 else
7854 {
7855#ifdef FEAT_WINDOWS
7856 if (only_one_window()) /* quit last window, exit Vim */
7857#endif
7858 getout(0);
7859#ifdef FEAT_WINDOWS
7860# ifdef FEAT_GUI
7861 need_mouse_correct = TRUE;
7862# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007863 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 win_close(curwin, !P_HID(curwin->w_buffer));
7865#endif
7866 }
7867}
7868
7869/*
7870 * ":print", ":list", ":number".
7871 */
7872 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007873ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007875 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7876 EMSG(_(e_emptybuf));
7877 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007879 for ( ;!got_int; ui_breakcheck())
7880 {
7881 print_line(eap->line1,
7882 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7883 || (eap->flags & EXFLAG_NR)),
7884 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7885 if (++eap->line1 > eap->line2)
7886 break;
7887 out_flush(); /* show one line at a time */
7888 }
7889 setpcmark();
7890 /* put cursor at last line */
7891 curwin->w_cursor.lnum = eap->line2;
7892 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 }
7894
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896}
7897
7898#ifdef FEAT_BYTEOFF
7899 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007900ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901{
7902 goto_byte(eap->line2);
7903}
7904#endif
7905
7906/*
7907 * ":shell".
7908 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007910ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911{
7912 do_shell(NULL, 0);
7913}
7914
7915#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7916 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007917 || defined(FEAT_GUI_MSWIN) \
7918 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 || defined(PROTO)
7920
7921/*
7922 * Handle a file drop. The code is here because a drop is *nearly* like an
7923 * :args command, but not quite (we have a list of exact filenames, so we
7924 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7925 * code is very similar to :args and hence needs access to a lot of the static
7926 * functions in this file.
7927 *
7928 * The list should be allocated using alloc(), as should each item in the
7929 * list. This function takes over responsibility for freeing the list.
7930 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007931 * XXX The list is made into the argument list. This is freed using
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007932 * FreeWild(), which does a series of vim_free() calls.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 */
7934 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007935handle_drop(
7936 int filec, /* the number of files dropped */
7937 char_u **filev, /* the list of files dropped */
7938 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939{
7940 exarg_T ea;
7941 int save_msg_scroll = msg_scroll;
7942
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007943 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007944 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007946#ifdef FEAT_AUTOCMD
7947 if (curbuf_locked())
7948 return;
7949#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007950 /* When the screen is being updated we should not change buffers and
7951 * windows structures, it may cause freed memory to be used. */
7952 if (updating_screen)
7953 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954
7955 /* Check whether the current buffer is changed. If so, we will need
7956 * to split the current window or data could be lost.
7957 * We don't need to check if the 'hidden' option is set, as in this
7958 * case the buffer won't be lost.
7959 */
7960 if (!P_HID(curbuf) && !split)
7961 {
7962 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007963 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 --emsg_off;
7965 }
7966 if (split)
7967 {
7968# ifdef FEAT_WINDOWS
7969 if (win_split(0, 0) == FAIL)
7970 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007971 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972
7973 /* When splitting the window, create a new alist. Otherwise the
7974 * existing one is overwritten. */
7975 alist_unlink(curwin->w_alist);
7976 alist_new();
7977# else
7978 return; /* can't split, always fail */
7979# endif
7980 }
7981
7982 /*
7983 * Set up the new argument list.
7984 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007985 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986
7987 /*
7988 * Move to the first file.
7989 */
7990 /* Fake up a minimal "next" command for do_argfile() */
7991 vim_memset(&ea, 0, sizeof(ea));
7992 ea.cmd = (char_u *)"next";
7993 do_argfile(&ea, 0);
7994
7995 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7996 * mode that is not needed here. */
7997 need_start_insertmode = FALSE;
7998
7999 /* Restore msg_scroll, otherwise a following command may cause scrolling
8000 * unexpectedly. The screen will be redrawn by the caller, thus
8001 * msg_scroll being set by displaying a message is irrelevant. */
8002 msg_scroll = save_msg_scroll;
8003}
8004#endif
8005
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006/*
8007 * Clear an argument list: free all file names and reset it to zero entries.
8008 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008009 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008010alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011{
8012 while (--al->al_ga.ga_len >= 0)
8013 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
8014 ga_clear(&al->al_ga);
8015}
8016
8017/*
8018 * Init an argument list.
8019 */
8020 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008021alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022{
8023 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
8024}
8025
8026#if defined(FEAT_WINDOWS) || defined(PROTO)
8027
8028/*
8029 * Remove a reference from an argument list.
8030 * Ignored when the argument list is the global one.
8031 * If the argument list is no longer used by any window, free it.
8032 */
8033 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008034alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035{
8036 if (al != &global_alist && --al->al_refcount <= 0)
8037 {
8038 alist_clear(al);
8039 vim_free(al);
8040 }
8041}
8042
8043# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
8044/*
8045 * Create a new argument list and use it for the current window.
8046 */
8047 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008048alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049{
8050 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
8051 if (curwin->w_alist == NULL)
8052 {
8053 curwin->w_alist = &global_alist;
8054 ++global_alist.al_refcount;
8055 }
8056 else
8057 {
8058 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008059 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 alist_init(curwin->w_alist);
8061 }
8062}
8063# endif
8064#endif
8065
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008066#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067/*
8068 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008069 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8070 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 */
8072 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008073alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074{
8075 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008076 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 char_u **new_arg_files;
8078 int new_arg_file_count;
8079 char_u *save_p_su = p_su;
8080 int i;
8081
8082 /* Don't use 'suffixes' here. This should work like the shell did the
8083 * expansion. Also, the vimrc file isn't read yet, thus the user
8084 * can't set the options. */
8085 p_su = empty_option;
8086 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8087 if (old_arg_files != NULL)
8088 {
8089 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008090 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8091 old_arg_count = GARGCOUNT;
8092 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008094 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 && new_arg_file_count > 0)
8096 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008097 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8098 TRUE, fnum_list, fnum_len);
8099 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 }
8102 p_su = save_p_su;
8103}
8104#endif
8105
8106/*
8107 * Set the argument list for the current window.
8108 * Takes over the allocated files[] and the allocated fnames in it.
8109 */
8110 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008111alist_set(
8112 alist_T *al,
8113 int count,
8114 char_u **files,
8115 int use_curbuf,
8116 int *fnum_list,
8117 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118{
8119 int i;
8120
8121 alist_clear(al);
8122 if (ga_grow(&al->al_ga, count) == OK)
8123 {
8124 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008125 {
8126 if (got_int)
8127 {
8128 /* When adding many buffers this can take a long time. Allow
8129 * interrupting here. */
8130 while (i < count)
8131 vim_free(files[i++]);
8132 break;
8133 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008134
8135 /* May set buffer name of a buffer previously used for the
8136 * argument list, so that it's re-used by alist_add. */
8137 if (fnum_list != NULL && i < fnum_len)
8138 buf_set_name(fnum_list[i], files[i]);
8139
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008141 ui_breakcheck();
8142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 vim_free(files);
8144 }
8145 else
8146 FreeWild(count, files);
8147#ifdef FEAT_WINDOWS
8148 if (al == &global_alist)
8149#endif
8150 arg_had_last = FALSE;
8151}
8152
8153/*
8154 * Add file "fname" to argument list "al".
8155 * "fname" must have been allocated and "al" must have been checked for room.
8156 */
8157 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008158alist_add(
8159 alist_T *al,
8160 char_u *fname,
8161 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162{
8163 if (fname == NULL) /* don't add NULL file names */
8164 return;
8165#ifdef BACKSLASH_IN_FILENAME
8166 slash_adjust(fname);
8167#endif
8168 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8169 if (set_fnum > 0)
8170 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8171 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8172 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173}
8174
8175#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8176/*
8177 * Adjust slashes in file names. Called after 'shellslash' was set.
8178 */
8179 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008180alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181{
8182 int i;
8183# ifdef FEAT_WINDOWS
8184 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008185 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186# endif
8187
8188 for (i = 0; i < GARGCOUNT; ++i)
8189 if (GARGLIST[i].ae_fname != NULL)
8190 slash_adjust(GARGLIST[i].ae_fname);
8191# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00008192 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 if (wp->w_alist != &global_alist)
8194 for (i = 0; i < WARGCOUNT(wp); ++i)
8195 if (WARGLIST(wp)[i].ae_fname != NULL)
8196 slash_adjust(WARGLIST(wp)[i].ae_fname);
8197# endif
8198}
8199#endif
8200
8201/*
8202 * ":preserve".
8203 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008205ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008207 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 ml_preserve(curbuf, TRUE);
8209}
8210
8211/*
8212 * ":recover".
8213 */
8214 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008215ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216{
8217 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8218 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008219 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8220 | CCGD_MULTWIN
8221 | (eap->forceit ? CCGD_FORCEIT : 0)
8222 | CCGD_EXCMD)
8223
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 && (*eap->arg == NUL
8225 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8226 ml_recover();
8227 recoverymode = FALSE;
8228}
8229
8230/*
8231 * Command modifier used in a wrong way.
8232 */
8233 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008234ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235{
8236 eap->errmsg = e_invcmd;
8237}
8238
8239#ifdef FEAT_WINDOWS
8240/*
8241 * :sview [+command] file split window with new file, read-only
8242 * :split [[+command] file] split window with current or new file
8243 * :vsplit [[+command] file] split window vertically with current or new file
8244 * :new [[+command] file] split window with no or new file
8245 * :vnew [[+command] file] split vertically window with no or new file
8246 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008247 *
8248 * :tabedit open new Tab page with empty window
8249 * :tabedit [+command] file open new Tab page and edit "file"
8250 * :tabnew [[+command] file] just like :tabedit
8251 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 */
8253 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008254ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008256 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008257# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008259# endif
8260# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008262# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008264# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008266# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008268# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008270 * quickfix windows. But it's OK when doing ":tab split". */
8271 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 {
8273 if (eap->cmdidx == CMD_split)
8274 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 if (eap->cmdidx == CMD_vsplit)
8276 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008278# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008280# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008281 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 {
8283 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8284 FNAME_MESS, TRUE, curbuf->b_ffname);
8285 if (fname == NULL)
8286 goto theend;
8287 eap->arg = fname;
8288 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008289# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008291# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008293# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 && eap->cmdidx != CMD_new)
8297 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008298# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008299 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008300# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008301 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008302# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008303 au_has_group((char_u *)"FileExplorer"))
8304 {
8305 /* No browsing supported but we do have the file explorer:
8306 * Edit the directory. */
8307 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8308 eap->arg = (char_u *)".";
8309 }
8310 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008311# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008312 {
8313 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008315 if (fname == NULL)
8316 goto theend;
8317 eap->arg = fname;
8318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 }
8320 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008321# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008323 /*
8324 * Either open new tab page or split the window.
8325 */
8326 if (eap->cmdidx == CMD_tabedit
8327 || eap->cmdidx == CMD_tabfind
8328 || eap->cmdidx == CMD_tabnew)
8329 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008330 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8331 : eap->addr_count == 0 ? 0
8332 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008333 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008334 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008335
8336 /* set the alternate buffer for the window we came from */
8337 if (curwin != old_curwin
8338 && win_valid(old_curwin)
8339 && old_curwin->w_buffer != curbuf
8340 && !cmdmod.keepalt)
8341 old_curwin->w_alt_fnum = curbuf->b_fnum;
8342 }
8343 }
8344 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8346 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008347# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 /* Reset 'scrollbind' when editing another file, but keep it when
8349 * doing ":split" without arguments. */
8350 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008351# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008353# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008355 {
8356 RESET_BINDING(curwin);
8357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 else
8359 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008360# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 do_exedit(eap, old_curwin);
8362 }
8363
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008364# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008366# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008368# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369theend:
8370 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008371# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008373
8374/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008375 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008376 */
8377 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008378tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008379{
8380 exarg_T ea;
8381
8382 vim_memset(&ea, 0, sizeof(ea));
8383 ea.cmdidx = CMD_tabnew;
8384 ea.cmd = (char_u *)"tabn";
8385 ea.arg = (char_u *)"";
8386 ex_splitview(&ea);
8387}
8388
8389/*
8390 * :tabnext command
8391 */
8392 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008393ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008394{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008395 int tab_number;
8396
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008397 switch (eap->cmdidx)
8398 {
8399 case CMD_tabfirst:
8400 case CMD_tabrewind:
8401 goto_tabpage(1);
8402 break;
8403 case CMD_tablast:
8404 goto_tabpage(9999);
8405 break;
8406 case CMD_tabprevious:
8407 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008408 if (eap->arg && *eap->arg != NUL)
8409 {
8410 char_u *p = eap->arg;
8411 char_u *p_save = p;
8412
8413 tab_number = getdigits(&p);
8414 if (p == p_save || *p_save == '-' || *p != NUL
8415 || tab_number == 0)
8416 {
8417 /* No numbers as argument. */
8418 eap->errmsg = e_invarg;
8419 return;
8420 }
8421 }
8422 else
8423 {
8424 if (eap->addr_count == 0)
8425 tab_number = 1;
8426 else
8427 {
8428 tab_number = eap->line2;
8429 if (tab_number < 1)
8430 {
8431 eap->errmsg = e_invrange;
8432 return;
8433 }
8434 }
8435 }
8436 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008437 break;
8438 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008439 tab_number = get_tabpage_arg(eap);
8440 if (eap->errmsg == NULL)
8441 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008442 break;
8443 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008444}
8445
8446/*
8447 * :tabmove command
8448 */
8449 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008450ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008451{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008452 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008453
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008454 tab_number = get_tabpage_arg(eap);
8455 if (eap->errmsg == NULL)
8456 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008457}
8458
8459/*
8460 * :tabs command: List tabs and their contents.
8461 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008462 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008463ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008464{
8465 tabpage_T *tp;
8466 win_T *wp;
8467 int tabcount = 1;
8468
8469 msg_start();
8470 msg_scroll = TRUE;
8471 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8472 {
8473 msg_putchar('\n');
8474 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008475 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008476 out_flush(); /* output one line at a time */
8477 ui_breakcheck();
8478
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008479 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008480 wp = firstwin;
8481 else
8482 wp = tp->tp_firstwin;
8483 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8484 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008485 msg_putchar('\n');
8486 msg_putchar(wp == curwin ? '>' : ' ');
8487 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008488 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8489 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008490 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008491 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008492 else
8493 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8494 IObuff, IOSIZE, TRUE);
8495 msg_outtrans(IObuff);
8496 out_flush(); /* output one line at a time */
8497 ui_breakcheck();
8498 }
8499 }
8500}
8501
8502#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503
8504/*
8505 * ":mode": Set screen mode.
8506 * If no argument given, just get the screen size and redraw.
8507 */
8508 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008509ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510{
8511 if (*eap->arg == NUL)
8512 shell_resized();
8513 else
8514 mch_screenmode(eap->arg);
8515}
8516
8517#ifdef FEAT_WINDOWS
8518/*
8519 * ":resize".
8520 * set, increment or decrement current window height
8521 */
8522 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008523ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524{
8525 int n;
8526 win_T *wp = curwin;
8527
8528 if (eap->addr_count > 0)
8529 {
8530 n = eap->line2;
8531 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8532 ;
8533 }
8534
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008535# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008537# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 if (cmdmod.split & WSP_VERT)
8540 {
8541 if (*eap->arg == '-' || *eap->arg == '+')
8542 n += W_WIDTH(curwin);
8543 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8544 n = 9999;
8545 win_setwidth_win((int)n, wp);
8546 }
8547 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 {
8549 if (*eap->arg == '-' || *eap->arg == '+')
8550 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02008551 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 n = 9999;
8553 win_setheight_win((int)n, wp);
8554 }
8555}
8556#endif
8557
8558/*
8559 * ":find [+command] <file>" command.
8560 */
8561 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008562ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563{
8564#ifdef FEAT_SEARCHPATH
8565 char_u *fname;
8566 int count;
8567
8568 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8569 TRUE, curbuf->b_ffname);
8570 if (eap->addr_count > 0)
8571 {
8572 /* Repeat finding the file "count" times. This matters when it
8573 * appears several times in the path. */
8574 count = eap->line2;
8575 while (fname != NULL && --count > 0)
8576 {
8577 vim_free(fname);
8578 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8579 FALSE, curbuf->b_ffname);
8580 }
8581 }
8582
8583 if (fname != NULL)
8584 {
8585 eap->arg = fname;
8586#endif
8587 do_exedit(eap, NULL);
8588#ifdef FEAT_SEARCHPATH
8589 vim_free(fname);
8590 }
8591#endif
8592}
8593
8594/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008595 * ":open" simulation: for now just work like ":visual".
8596 */
8597 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008598ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008599{
8600 regmatch_T regmatch;
8601 char_u *p;
8602
8603 curwin->w_cursor.lnum = eap->line2;
8604 beginline(BL_SOL | BL_FIX);
8605 if (*eap->arg == '/')
8606 {
8607 /* ":open /pattern/": put cursor in column found with pattern */
8608 ++eap->arg;
8609 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8610 *p = NUL;
8611 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8612 if (regmatch.regprog != NULL)
8613 {
8614 regmatch.rm_ic = p_ic;
8615 p = ml_get_curline();
8616 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008617 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008618 else
8619 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008620 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008621 }
8622 /* Move to the NUL, ignore any other arguments. */
8623 eap->arg += STRLEN(eap->arg);
8624 }
8625 check_cursor();
8626
8627 eap->cmdidx = CMD_visual;
8628 do_exedit(eap, NULL);
8629}
8630
8631/*
8632 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 */
8634 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008635ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636{
8637 do_exedit(eap, NULL);
8638}
8639
8640/*
8641 * ":edit <file>" command and alikes.
8642 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008644do_exedit(
8645 exarg_T *eap,
8646 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647{
8648 int n;
8649#ifdef FEAT_WINDOWS
8650 int need_hide;
8651#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008652 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653
8654 /*
8655 * ":vi" command ends Ex mode.
8656 */
8657 if (exmode_active && (eap->cmdidx == CMD_visual
8658 || eap->cmdidx == CMD_view))
8659 {
8660 exmode_active = FALSE;
8661 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008662 {
8663 /* Special case: ":global/pat/visual\NLvi-commands" */
8664 if (global_busy)
8665 {
8666 int rd = RedrawingDisabled;
8667 int nwr = no_wait_return;
8668 int ms = msg_scroll;
8669#ifdef FEAT_GUI
8670 int he = hold_gui_events;
8671#endif
8672
8673 if (eap->nextcmd != NULL)
8674 {
8675 stuffReadbuff(eap->nextcmd);
8676 eap->nextcmd = NULL;
8677 }
8678
8679 if (exmode_was != EXMODE_VIM)
8680 settmode(TMODE_RAW);
8681 RedrawingDisabled = 0;
8682 no_wait_return = 0;
8683 need_wait_return = FALSE;
8684 msg_scroll = 0;
8685#ifdef FEAT_GUI
8686 hold_gui_events = 0;
8687#endif
8688 must_redraw = CLEAR;
8689
8690 main_loop(FALSE, TRUE);
8691
8692 RedrawingDisabled = rd;
8693 no_wait_return = nwr;
8694 msg_scroll = ms;
8695#ifdef FEAT_GUI
8696 hold_gui_events = he;
8697#endif
8698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 }
8702
8703 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008704 || eap->cmdidx == CMD_tabnew
8705 || eap->cmdidx == CMD_tabedit
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008706#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 || eap->cmdidx == CMD_vnew
8708#endif
8709 ) && *eap->arg == NUL)
8710 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008711 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 setpcmark();
8713 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008714 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8715 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 }
8717 else if ((eap->cmdidx != CMD_split
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008718#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 && eap->cmdidx != CMD_vsplit
8720#endif
8721 )
8722 || *eap->arg != NUL
8723#ifdef FEAT_BROWSE
8724 || cmdmod.browse
8725#endif
8726 )
8727 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008728#ifdef FEAT_AUTOCMD
8729 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8730 * can bring us here, others are stopped earlier. */
8731 if (*eap->arg != NUL && curbuf_locked())
8732 return;
8733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 n = readonlymode;
8735 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8736 readonlymode = TRUE;
8737 else if (eap->cmdidx == CMD_enew)
8738 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8739 empty buffer */
8740 setpcmark();
8741 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8742 NULL, eap,
8743 /* ":edit" goes to first line if Vi compatible */
8744 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8745 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8746 ? ECMD_ONE : eap->do_ecmd_lnum,
8747 (P_HID(curbuf) ? ECMD_HIDE : 0)
8748 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008749 /* after a split we can use an existing buffer */
8750 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751#ifdef FEAT_LISTCMDS
8752 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8753#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008754 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 {
8756 /* Editing the file failed. If the window was split, close it. */
8757#ifdef FEAT_WINDOWS
8758 if (old_curwin != NULL)
8759 {
8760 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8761 if (!need_hide || P_HID(curbuf))
8762 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008763# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8764 cleanup_T cs;
8765
8766 /* Reset the error/interrupt/exception state here so that
8767 * aborting() returns FALSE when closing a window. */
8768 enter_cleanup(&cs);
8769# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770# ifdef FEAT_GUI
8771 need_mouse_correct = TRUE;
8772# endif
8773 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008774
8775# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8776 /* Restore the error/interrupt/exception state if not
8777 * discarded by a new aborting error, interrupt, or
8778 * uncaught exception. */
8779 leave_cleanup(&cs);
8780# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 }
8782 }
8783#endif
8784 }
8785 else if (readonlymode && curbuf->b_nwindows == 1)
8786 {
8787 /* When editing an already visited buffer, 'readonly' won't be set
8788 * but the previous value is kept. With ":view" and ":sview" we
8789 * want the file to be readonly, except when another window is
8790 * editing the same buffer. */
8791 curbuf->b_p_ro = TRUE;
8792 }
8793 readonlymode = n;
8794 }
8795 else
8796 {
8797 if (eap->do_ecmd_cmd != NULL)
8798 do_cmdline_cmd(eap->do_ecmd_cmd);
8799#ifdef FEAT_TITLE
8800 n = curwin->w_arg_idx_invalid;
8801#endif
8802 check_arg_idx(curwin);
8803#ifdef FEAT_TITLE
8804 if (n != curwin->w_arg_idx_invalid)
8805 maketitle();
8806#endif
8807 }
8808
8809#ifdef FEAT_WINDOWS
8810 /*
8811 * if ":split file" worked, set alternate file name in old window to new
8812 * file
8813 */
8814 if (old_curwin != NULL
8815 && *eap->arg != NUL
8816 && curwin != old_curwin
8817 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008818 && old_curwin->w_buffer != curbuf
8819 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 old_curwin->w_alt_fnum = curbuf->b_fnum;
8821#endif
8822
8823 ex_no_reprint = TRUE;
8824}
8825
8826#ifndef FEAT_GUI
8827/*
8828 * ":gui" and ":gvim" when there is no GUI.
8829 */
8830 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008831ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832{
8833 eap->errmsg = e_nogvim;
8834}
8835#endif
8836
8837#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8838 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008839ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840{
8841 gui_make_tearoff(eap->arg);
8842}
8843#endif
8844
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008845#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008847ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008849 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850}
8851#endif
8852
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008854ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855{
8856 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8857 MSG(_("No swap file"));
8858 else
8859 msg(curbuf->b_ml.ml_mfp->mf_fname);
8860}
8861
8862/*
8863 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8864 * offset.
8865 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8866 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008868ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869{
8870#ifdef FEAT_SCROLLBIND
8871 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008872 win_T *save_curwin = curwin;
8873 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 long topline;
8875 long y;
8876 linenr_T old_linenr = curwin->w_cursor.lnum;
8877
8878 setpcmark();
8879
8880 /*
8881 * determine max topline
8882 */
8883 if (curwin->w_p_scb)
8884 {
8885 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008886 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 {
8888 if (wp->w_p_scb && wp->w_buffer)
8889 {
8890 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8891 if (topline > y)
8892 topline = y;
8893 }
8894 }
8895 if (topline < 1)
8896 topline = 1;
8897 }
8898 else
8899 {
8900 topline = 1;
8901 }
8902
8903
8904 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008905 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008907 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 {
8909 if (curwin->w_p_scb)
8910 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008911 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 y = topline - curwin->w_topline;
8913 if (y > 0)
8914 scrollup(y, TRUE);
8915 else
8916 scrolldown(-y, TRUE);
8917 curwin->w_scbind_pos = topline;
8918 redraw_later(VALID);
8919 cursor_correct();
8920#ifdef FEAT_WINDOWS
8921 curwin->w_redr_status = TRUE;
8922#endif
8923 }
8924 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008925 curwin = save_curwin;
8926 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 if (curwin->w_p_scb)
8928 {
8929 did_syncbind = TRUE;
8930 checkpcmark();
8931 if (old_linenr != curwin->w_cursor.lnum)
8932 {
8933 char_u ctrl_o[2];
8934
8935 ctrl_o[0] = Ctrl_O;
8936 ctrl_o[1] = 0;
8937 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8938 }
8939 }
8940#endif
8941}
8942
8943
8944 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008945ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008947 int i;
8948 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8949 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950
8951 if (eap->usefilter) /* :r!cmd */
8952 do_bang(1, eap, FALSE, FALSE, TRUE);
8953 else
8954 {
8955 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8956 return;
8957
8958#ifdef FEAT_BROWSE
8959 if (cmdmod.browse)
8960 {
8961 char_u *browseFile;
8962
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008963 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 NULL, NULL, NULL, curbuf);
8965 if (browseFile != NULL)
8966 {
8967 i = readfile(browseFile, NULL,
8968 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8969 vim_free(browseFile);
8970 }
8971 else
8972 i = OK;
8973 }
8974 else
8975#endif
8976 if (*eap->arg == NUL)
8977 {
8978 if (check_fname() == FAIL) /* check for no file name */
8979 return;
8980 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8981 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8982 }
8983 else
8984 {
8985 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8986 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8987 i = readfile(eap->arg, NULL,
8988 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8989
8990 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01008991 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 {
8993#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8994 if (!aborting())
8995#endif
8996 EMSG2(_(e_notopen), eap->arg);
8997 }
8998 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008999 {
9000 if (empty && exmode_active)
9001 {
9002 /* Delete the empty line that remains. Historically ex does
9003 * this but vi doesn't. */
9004 if (eap->line2 == 0)
9005 lnum = curbuf->b_ml.ml_line_count;
9006 else
9007 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009008 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009009 {
9010 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009011 if (curwin->w_cursor.lnum > 1
9012 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009013 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00009014 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009015 }
9016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 }
9020}
9021
Bram Moolenaarea408852005-06-25 22:49:46 +00009022static char_u *prev_dir = NULL;
9023
9024#if defined(EXITFREE) || defined(PROTO)
9025 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009026free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00009027{
9028 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00009029 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00009030
9031 vim_free(globaldir);
9032 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00009033}
9034#endif
9035
Bram Moolenaarf4258302013-06-02 18:20:17 +02009036/*
9037 * Deal with the side effects of changing the current directory.
9038 * When "local" is TRUE then this was after an ":lcd" command.
9039 */
9040 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009041post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02009042{
9043 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01009044 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02009045 if (local)
9046 {
9047 /* If still in global directory, need to remember current
9048 * directory as global directory. */
9049 if (globaldir == NULL && prev_dir != NULL)
9050 globaldir = vim_strsave(prev_dir);
9051 /* Remember this local directory for the window. */
9052 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9053 curwin->w_localdir = vim_strsave(NameBuff);
9054 }
9055 else
9056 {
9057 /* We are now in the global directory, no need to remember its
9058 * name. */
9059 vim_free(globaldir);
9060 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02009061 }
9062
9063 shorten_fnames(TRUE);
9064}
9065
Bram Moolenaarea408852005-06-25 22:49:46 +00009066
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067/*
9068 * ":cd", ":lcd", ":chdir" and ":lchdir".
9069 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00009070 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009071ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 char_u *new_dir;
9074 char_u *tofree;
9075
9076 new_dir = eap->arg;
9077#if !defined(UNIX) && !defined(VMS)
9078 /* for non-UNIX ":cd" means: print current directory */
9079 if (*new_dir == NUL)
9080 ex_pwd(NULL);
9081 else
9082#endif
9083 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009084#ifdef FEAT_AUTOCMD
9085 if (allbuf_locked())
9086 return;
9087#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009088 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9089 && !eap->forceit)
9090 {
Bram Moolenaar81870892007-11-11 18:17:28 +00009091 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009092 return;
9093 }
9094
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 /* ":cd -": Change to previous directory */
9096 if (STRCMP(new_dir, "-") == 0)
9097 {
9098 if (prev_dir == NULL)
9099 {
9100 EMSG(_("E186: No previous directory"));
9101 return;
9102 }
9103 new_dir = prev_dir;
9104 }
9105
9106 /* Save current directory for next ":cd -" */
9107 tofree = prev_dir;
9108 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9109 prev_dir = vim_strsave(NameBuff);
9110 else
9111 prev_dir = NULL;
9112
9113#if defined(UNIX) || defined(VMS)
9114 /* for UNIX ":cd" means: go to home directory */
9115 if (*new_dir == NUL)
9116 {
9117 /* use NameBuff for home directory name */
9118# ifdef VMS
9119 char_u *p;
9120
9121 p = mch_getenv((char_u *)"SYS$LOGIN");
9122 if (p == NULL || *p == NUL) /* empty is the same as not set */
9123 NameBuff[0] = NUL;
9124 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009125 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126# else
9127 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9128# endif
9129 new_dir = NameBuff;
9130 }
9131#endif
9132 if (new_dir == NULL || vim_chdir(new_dir))
9133 EMSG(_(e_failed));
9134 else
9135 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02009136 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137
9138 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009139 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140 ex_pwd(eap);
9141 }
9142 vim_free(tofree);
9143 }
9144}
9145
9146/*
9147 * ":pwd".
9148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009150ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151{
9152 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9153 {
9154#ifdef BACKSLASH_IN_FILENAME
9155 slash_adjust(NameBuff);
9156#endif
9157 msg(NameBuff);
9158 }
9159 else
9160 EMSG(_("E187: Unknown"));
9161}
9162
9163/*
9164 * ":=".
9165 */
9166 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009167ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168{
9169 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009170 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171}
9172
9173 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009174ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009176 int n;
9177 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178
9179 if (cursor_valid())
9180 {
9181 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9182 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01009183 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009185
9186 len = eap->line2;
9187 switch (*eap->arg)
9188 {
9189 case 'm': break;
9190 case NUL: len *= 1000L; break;
9191 default: EMSG2(_(e_invarg2), eap->arg); return;
9192 }
9193 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194}
9195
9196/*
9197 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9198 */
9199 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009200do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201{
9202 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009203 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204
9205 cursor_on();
9206 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009207 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009209 wait_now = msec - done > 1000L ? 1000L : msec - done;
9210#ifdef FEAT_TIMERS
9211 {
9212 long due_time = check_due_timer();
9213
9214 if (due_time > 0 && due_time < wait_now)
9215 wait_now = due_time;
9216 }
9217#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009218#ifdef FEAT_JOB_CHANNEL
9219 if (has_any_channel() && wait_now > 100L)
9220 wait_now = 100L;
9221#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009222 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009223#ifdef FEAT_JOB_CHANNEL
9224 if (has_any_channel())
9225 ui_breakcheck_force(TRUE);
9226 else
9227#endif
9228 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009229#ifdef MESSAGE_QUEUE
9230 /* Process the netbeans and clientserver messages that may have been
9231 * received in the call to ui_breakcheck() when the GUI is in use. This
9232 * may occur when running a test case. */
9233 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 }
9236}
9237
9238 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009239do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240{
9241 int mode;
9242 char_u *cmdp;
9243
9244 cmdp = eap->cmd;
9245 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9246
9247 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9248 eap->arg, mode, isabbrev))
9249 {
9250 case 1: EMSG(_(e_invarg));
9251 break;
9252 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9253 break;
9254 }
9255}
9256
9257/*
9258 * ":winsize" command (obsolete).
9259 */
9260 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009261ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262{
9263 int w, h;
9264 char_u *arg = eap->arg;
9265 char_u *p;
9266
9267 w = getdigits(&arg);
9268 arg = skipwhite(arg);
9269 p = arg;
9270 h = getdigits(&arg);
9271 if (*p != NUL && *arg == NUL)
9272 set_shellsize(w, h, TRUE);
9273 else
9274 EMSG(_("E465: :winsize requires two number arguments"));
9275}
9276
9277#ifdef FEAT_WINDOWS
9278 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009279ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280{
9281 int xchar = NUL;
9282 char_u *p;
9283
9284 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9285 {
9286 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9287 if (eap->arg[1] == NUL)
9288 {
9289 EMSG(_(e_invarg));
9290 return;
9291 }
9292 xchar = eap->arg[1];
9293 p = eap->arg + 2;
9294 }
9295 else
9296 p = eap->arg + 1;
9297
9298 eap->nextcmd = check_nextcmd(p);
9299 p = skipwhite(p);
9300 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9301 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009302 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303 {
9304 /* Pass flags on for ":vertical wincmd ]". */
9305 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009306 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9308 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009309 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310 }
9311}
9312#endif
9313
Bram Moolenaar843ee412004-06-30 16:16:41 +00009314#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315/*
9316 * ":winpos".
9317 */
9318 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009319ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320{
9321 int x, y;
9322 char_u *arg = eap->arg;
9323 char_u *p;
9324
9325 if (*arg == NUL)
9326 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009327# if defined(FEAT_GUI) || defined(MSWIN)
9328# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009330# else
9331 if (mch_get_winpos(&x, &y) != FAIL)
9332# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 {
9334 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9335 msg(IObuff);
9336 }
9337 else
9338# endif
9339 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9340 }
9341 else
9342 {
9343 x = getdigits(&arg);
9344 arg = skipwhite(arg);
9345 p = arg;
9346 y = getdigits(&arg);
9347 if (*p == NUL || *arg != NUL)
9348 {
9349 EMSG(_("E466: :winpos requires two number arguments"));
9350 return;
9351 }
9352# ifdef FEAT_GUI
9353 if (gui.in_use)
9354 gui_mch_set_winpos(x, y);
9355 else if (gui.starting)
9356 {
9357 /* Remember the coordinates for when the window is opened. */
9358 gui_win_x = x;
9359 gui_win_y = y;
9360 }
9361# ifdef HAVE_TGETENT
9362 else
9363# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009364# else
9365# ifdef MSWIN
9366 mch_set_winpos(x, y);
9367# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368# endif
9369# ifdef HAVE_TGETENT
9370 if (*T_CWP)
9371 term_set_winpos(x, y);
9372# endif
9373 }
9374}
9375#endif
9376
9377/*
9378 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9379 */
9380 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009381ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382{
9383 oparg_T oa;
9384
9385 clear_oparg(&oa);
9386 oa.regname = eap->regname;
9387 oa.start.lnum = eap->line1;
9388 oa.end.lnum = eap->line2;
9389 oa.line_count = eap->line2 - eap->line1 + 1;
9390 oa.motion_type = MLINE;
9391#ifdef FEAT_VIRTUALEDIT
9392 virtual_op = FALSE;
9393#endif
9394 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9395 {
9396 setpcmark();
9397 curwin->w_cursor.lnum = eap->line1;
9398 beginline(BL_SOL | BL_FIX);
9399 }
9400
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009401 if (VIsual_active)
9402 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009403
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 switch (eap->cmdidx)
9405 {
9406 case CMD_delete:
9407 oa.op_type = OP_DELETE;
9408 op_delete(&oa);
9409 break;
9410
9411 case CMD_yank:
9412 oa.op_type = OP_YANK;
9413 (void)op_yank(&oa, FALSE, TRUE);
9414 break;
9415
9416 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009417 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009419 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9420#else
9421 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009423 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 oa.op_type = OP_RSHIFT;
9425 else
9426 oa.op_type = OP_LSHIFT;
9427 op_shift(&oa, FALSE, eap->amount);
9428 break;
9429 }
9430#ifdef FEAT_VIRTUALEDIT
9431 virtual_op = MAYBE;
9432#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009433 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434}
9435
9436/*
9437 * ":put".
9438 */
9439 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009440ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441{
9442 /* ":0put" works like ":1put!". */
9443 if (eap->line2 == 0)
9444 {
9445 eap->line2 = 1;
9446 eap->forceit = TRUE;
9447 }
9448 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009449 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9450 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451}
9452
9453/*
9454 * Handle ":copy" and ":move".
9455 */
9456 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009457ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458{
9459 long n;
9460
Bram Moolenaarded27822017-01-02 14:27:34 +01009461 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 if (eap->arg == NULL) /* error detected */
9463 {
9464 eap->nextcmd = NULL;
9465 return;
9466 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009467 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468
9469 /*
9470 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9471 */
9472 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9473 {
9474 EMSG(_(e_invaddr));
9475 return;
9476 }
9477
9478 if (eap->cmdidx == CMD_move)
9479 {
9480 if (do_move(eap->line1, eap->line2, n) == FAIL)
9481 return;
9482 }
9483 else
9484 ex_copy(eap->line1, eap->line2, n);
9485 u_clearline();
9486 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009487 ex_may_print(eap);
9488}
9489
9490/*
9491 * Print the current line if flags were given to the Ex command.
9492 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009493 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009494ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009495{
9496 if (eap->flags != 0)
9497 {
9498 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9499 (eap->flags & EXFLAG_LIST));
9500 ex_no_reprint = TRUE;
9501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502}
9503
9504/*
9505 * ":smagic" and ":snomagic".
9506 */
9507 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009508ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509{
9510 int magic_save = p_magic;
9511
9512 p_magic = (eap->cmdidx == CMD_smagic);
9513 do_sub(eap);
9514 p_magic = magic_save;
9515}
9516
9517/*
9518 * ":join".
9519 */
9520 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009521ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522{
9523 curwin->w_cursor.lnum = eap->line1;
9524 if (eap->line1 == eap->line2)
9525 {
9526 if (eap->addr_count >= 2) /* :2,2join does nothing */
9527 return;
9528 if (eap->line2 == curbuf->b_ml.ml_line_count)
9529 {
9530 beep_flush();
9531 return;
9532 }
9533 ++eap->line2;
9534 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009535 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009537 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538}
9539
9540/*
9541 * ":[addr]@r" or ":[addr]*r": execute register
9542 */
9543 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009544ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545{
9546 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009547 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548
9549 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009550 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551
9552#ifdef USE_ON_FLY_SCROLL
9553 dont_scroll = TRUE; /* disallow scrolling here */
9554#endif
9555
9556 /* get the register name. No name means to use the previous one */
9557 c = *eap->arg;
9558 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9559 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009560 /* Put the register in the typeahead buffer with the "silent" flag. */
9561 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9562 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009563 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566 else
9567 {
9568 int save_efr = exec_from_reg;
9569
9570 exec_from_reg = TRUE;
9571
9572 /*
9573 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009574 * Continue until the stuff buffer is empty and all added characters
9575 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009577 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9579
9580 exec_from_reg = save_efr;
9581 }
9582}
9583
9584/*
9585 * ":!".
9586 */
9587 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009588ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589{
9590 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9591}
9592
9593/*
9594 * ":undo".
9595 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009597ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009599 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009600 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009601 else
9602 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603}
9604
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009605#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009606 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009607ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009608{
9609 char_u hash[UNDO_HASH_SIZE];
9610
9611 u_compute_hash(hash);
9612 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9613}
9614
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009615 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009616ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009617{
9618 char_u hash[UNDO_HASH_SIZE];
9619
9620 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009621 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009622}
9623#endif
9624
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625/*
9626 * ":redo".
9627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009629ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630{
9631 u_redo(1);
9632}
9633
9634/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009635 * ":earlier" and ":later".
9636 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009637 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009638ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009639{
9640 long count = 0;
9641 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009642 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009643 char_u *p = eap->arg;
9644
9645 if (*p == NUL)
9646 count = 1;
9647 else if (isdigit(*p))
9648 {
9649 count = getdigits(&p);
9650 switch (*p)
9651 {
9652 case 's': ++p; sec = TRUE; break;
9653 case 'm': ++p; sec = TRUE; count *= 60; break;
9654 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009655 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9656 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009657 }
9658 }
9659
9660 if (*p != NUL)
9661 EMSG2(_(e_invarg2), eap->arg);
9662 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009663 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9664 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009665}
9666
9667/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 * ":redir": start/stop redirection.
9669 */
9670 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009671ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672{
9673 char *mode;
9674 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009675 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676
Bram Moolenaarba768492016-07-08 15:32:54 +02009677#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009678 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009679 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009680 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009681 return;
9682 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009683#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009684
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 if (STRICMP(eap->arg, "END") == 0)
9686 close_redir();
9687 else
9688 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009689 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009691 ++arg;
9692 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009694 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 mode = "a";
9696 }
9697 else
9698 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009699 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700
9701 close_redir();
9702
9703 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009704 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 if (fname == NULL)
9706 return;
9707#ifdef FEAT_BROWSE
9708 if (cmdmod.browse)
9709 {
9710 char_u *browseFile;
9711
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009712 browseFile = do_browse(BROWSE_SAVE,
9713 (char_u *)_("Save Redirection"),
9714 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 if (browseFile == NULL)
9716 return; /* operation cancelled */
9717 vim_free(fname);
9718 fname = browseFile;
9719 eap->forceit = TRUE; /* since dialog already asked */
9720 }
9721#endif
9722
9723 redir_fd = open_exfile(fname, eap->forceit, mode);
9724 vim_free(fname);
9725 }
9726#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009727 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 {
9729 /* redirect to a register a-z (resp. A-Z for appending) */
9730 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009731 ++arg;
9732 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009734 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009735 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009737 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009739 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009740 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009741 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009742 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009744 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009745 if (*arg == '>')
9746 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009747 /* Make register empty when not using @A-@Z and the
9748 * command is valid. */
9749 if (*arg == NUL && !isupper(redir_reg))
9750 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009752 }
9753 if (*arg != NUL)
9754 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009755 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009756 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009757 }
9758 }
9759 else if (*arg == '=' && arg[1] == '>')
9760 {
9761 int append;
9762
9763 /* redirect to a variable */
9764 close_redir();
9765 arg += 2;
9766
9767 if (*arg == '>')
9768 {
9769 ++arg;
9770 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771 }
9772 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009773 append = FALSE;
9774
9775 if (var_redir_start(skipwhite(arg), append) == OK)
9776 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 }
9778#endif
9779
9780 /* TODO: redirect to a buffer */
9781
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009783 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009785
9786 /* Make sure redirection is not off. Can happen for cmdline completion
9787 * that indirectly invokes a command to catch its output. */
9788 if (redir_fd != NULL
9789#ifdef FEAT_EVAL
9790 || redir_reg || redir_vname
9791#endif
9792 )
9793 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794}
9795
9796/*
9797 * ":redraw": force redraw
9798 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009799 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009800ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801{
9802 int r = RedrawingDisabled;
9803 int p = p_lz;
9804
9805 RedrawingDisabled = 0;
9806 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009807 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009809 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810#ifdef FEAT_TITLE
9811 if (need_maketitle)
9812 maketitle();
9813#endif
9814 RedrawingDisabled = r;
9815 p_lz = p;
9816
9817 /* Reset msg_didout, so that a message that's there is overwritten. */
9818 msg_didout = FALSE;
9819 msg_col = 0;
9820
Bram Moolenaar56667a52013-07-17 11:54:28 +02009821 /* No need to wait after an intentional redraw. */
9822 need_wait_return = FALSE;
9823
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824 out_flush();
9825}
9826
9827/*
9828 * ":redrawstatus": force redraw of status line(s)
9829 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009831ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832{
9833#if defined(FEAT_WINDOWS)
9834 int r = RedrawingDisabled;
9835 int p = p_lz;
9836
9837 RedrawingDisabled = 0;
9838 p_lz = FALSE;
9839 if (eap->forceit)
9840 status_redraw_all();
9841 else
9842 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009843 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 RedrawingDisabled = r;
9845 p_lz = p;
9846 out_flush();
9847#endif
9848}
9849
9850 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009851close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852{
9853 if (redir_fd != NULL)
9854 {
9855 fclose(redir_fd);
9856 redir_fd = NULL;
9857 }
9858#ifdef FEAT_EVAL
9859 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009860 if (redir_vname)
9861 {
9862 var_redir_stop();
9863 redir_vname = 0;
9864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865#endif
9866}
9867
9868#if defined(FEAT_SESSION) && defined(USE_CRNL)
9869# define MKSESSION_NL
9870static int mksession_nl = FALSE; /* use NL only in put_eol() */
9871#endif
9872
9873/*
9874 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9875 */
9876 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009877ex_mkrc(
9878 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879{
9880 FILE *fd;
9881 int failed = FALSE;
9882 char_u *fname;
9883#ifdef FEAT_BROWSE
9884 char_u *browseFile = NULL;
9885#endif
9886#ifdef FEAT_SESSION
9887 int view_session = FALSE;
9888 int using_vdir = FALSE; /* using 'viewdir'? */
9889 char_u *viewFile = NULL;
9890 unsigned *flagp;
9891#endif
9892
9893 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9894 {
9895#ifdef FEAT_SESSION
9896 view_session = TRUE;
9897#else
9898 ex_ni(eap);
9899 return;
9900#endif
9901 }
9902
9903#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009904 /* Use the short file name until ":lcd" is used. We also don't use the
9905 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009906 did_lcd = FALSE;
9907
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9909 if (eap->cmdidx == CMD_mkview
9910 && (*eap->arg == NUL
9911 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9912 {
9913 eap->forceit = TRUE;
9914 fname = get_view_file(*eap->arg);
9915 if (fname == NULL)
9916 return;
9917 viewFile = fname;
9918 using_vdir = TRUE;
9919 }
9920 else
9921#endif
9922 if (*eap->arg != NUL)
9923 fname = eap->arg;
9924 else if (eap->cmdidx == CMD_mkvimrc)
9925 fname = (char_u *)VIMRC_FILE;
9926#ifdef FEAT_SESSION
9927 else if (eap->cmdidx == CMD_mksession)
9928 fname = (char_u *)SESSION_FILE;
9929#endif
9930 else
9931 fname = (char_u *)EXRC_FILE;
9932
9933#ifdef FEAT_BROWSE
9934 if (cmdmod.browse)
9935 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009936 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937# ifdef FEAT_SESSION
9938 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9939 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9940# endif
9941 (char_u *)_("Save Setup"),
9942 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9943 if (browseFile == NULL)
9944 goto theend;
9945 fname = browseFile;
9946 eap->forceit = TRUE; /* since dialog already asked */
9947 }
9948#endif
9949
9950#if defined(FEAT_SESSION) && defined(vim_mkdir)
9951 /* When using 'viewdir' may have to create the directory. */
9952 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009953 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954#endif
9955
9956 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9957 if (fd != NULL)
9958 {
9959#ifdef FEAT_SESSION
9960 if (eap->cmdidx == CMD_mkview)
9961 flagp = &vop_flags;
9962 else
9963 flagp = &ssop_flags;
9964#endif
9965
9966#ifdef MKSESSION_NL
9967 /* "unix" in 'sessionoptions': use NL line separator */
9968 if (view_session && (*flagp & SSOP_UNIX))
9969 mksession_nl = TRUE;
9970#endif
9971
9972 /* Write the version command for :mkvimrc */
9973 if (eap->cmdidx == CMD_mkvimrc)
9974 (void)put_line(fd, "version 6.0");
9975
9976#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009977 if (eap->cmdidx == CMD_mksession)
9978 {
9979 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9980 failed = TRUE;
9981 }
9982
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983 if (eap->cmdidx != CMD_mkview)
9984#endif
9985 {
9986 /* Write setting 'compatible' first, because it has side effects.
9987 * For that same reason only do it when needed. */
9988 if (p_cp)
9989 (void)put_line(fd, "if !&cp | set cp | endif");
9990 else
9991 (void)put_line(fd, "if &cp | set nocp | endif");
9992 }
9993
9994#ifdef FEAT_SESSION
9995 if (!view_session
9996 || (eap->cmdidx == CMD_mksession
9997 && (*flagp & SSOP_OPTIONS)))
9998#endif
9999 failed |= (makemap(fd, NULL) == FAIL
10000 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
10001
10002#ifdef FEAT_SESSION
10003 if (!failed && view_session)
10004 {
10005 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
10006 failed = TRUE;
10007 if (eap->cmdidx == CMD_mksession)
10008 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010009 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010
Bram Moolenaard9462e32011-04-11 21:35:11 +020010011 dirnow = alloc(MAXPATHL);
10012 if (dirnow == NULL)
10013 failed = TRUE;
10014 else
10015 {
10016 /*
10017 * Change to session file's dir.
10018 */
10019 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010021 *dirnow = NUL;
10022 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
10023 {
10024 if (vim_chdirfile(fname) == OK)
10025 shorten_fnames(TRUE);
10026 }
10027 else if (*dirnow != NUL
10028 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
10029 {
10030 if (mch_chdir((char *)globaldir) == 0)
10031 shorten_fnames(TRUE);
10032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033
Bram Moolenaard9462e32011-04-11 21:35:11 +020010034 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035
Bram Moolenaard9462e32011-04-11 21:35:11 +020010036 /* restore original dir */
10037 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +020010039 {
10040 if (mch_chdir((char *)dirnow) != 0)
10041 EMSG(_(e_prev_dir));
10042 shorten_fnames(TRUE);
10043 }
10044 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 }
10046 }
10047 else
10048 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010049 failed |= (put_view(fd, curwin, !using_vdir, flagp,
10050 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051 }
10052 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
10053 == FAIL)
10054 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010055 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
10056 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010057 if (eap->cmdidx == CMD_mksession)
10058 {
10059 if (put_line(fd, "unlet SessionLoad") == FAIL)
10060 failed = TRUE;
10061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 }
10063#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010064 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
10065 failed = TRUE;
10066
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 failed |= fclose(fd);
10068
10069 if (failed)
10070 EMSG(_(e_write));
10071#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
10072 else if (eap->cmdidx == CMD_mksession)
10073 {
10074 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010075 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076
Bram Moolenaard9462e32011-04-11 21:35:11 +020010077 tbuf = alloc(MAXPATHL);
10078 if (tbuf != NULL)
10079 {
10080 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10081 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10082 vim_free(tbuf);
10083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 }
10085#endif
10086#ifdef MKSESSION_NL
10087 mksession_nl = FALSE;
10088#endif
10089 }
10090
10091#ifdef FEAT_BROWSE
10092theend:
10093 vim_free(browseFile);
10094#endif
10095#ifdef FEAT_SESSION
10096 vim_free(viewFile);
10097#endif
10098}
10099
Bram Moolenaardf177f62005-02-22 08:39:57 +000010100#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10101 || defined(PROTO)
10102 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010103vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010104{
10105 if (vim_mkdir(name, prot) != 0)
10106 {
10107 EMSG2(_("E739: Cannot create directory: %s"), name);
10108 return FAIL;
10109 }
10110 return OK;
10111}
10112#endif
10113
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114/*
10115 * Open a file for writing for an Ex command, with some checks.
10116 * Return file descriptor, or NULL on failure.
10117 */
10118 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010119open_exfile(
10120 char_u *fname,
10121 int forceit,
10122 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123{
10124 FILE *fd;
10125
10126#ifdef UNIX
10127 /* with Unix it is possible to open a directory */
10128 if (mch_isdir(fname))
10129 {
10130 EMSG2(_(e_isadir2), fname);
10131 return NULL;
10132 }
10133#endif
10134 if (!forceit && *mode != 'a' && vim_fexists(fname))
10135 {
10136 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
10137 return NULL;
10138 }
10139
10140 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
10141 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
10142
10143 return fd;
10144}
10145
10146/*
10147 * ":mark" and ":k".
10148 */
10149 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010150ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151{
10152 pos_T pos;
10153
10154 if (*eap->arg == NUL) /* No argument? */
10155 EMSG(_(e_argreq));
10156 else if (eap->arg[1] != NUL) /* more than one character? */
10157 EMSG(_(e_trailing));
10158 else
10159 {
10160 pos = curwin->w_cursor; /* save curwin->w_cursor */
10161 curwin->w_cursor.lnum = eap->line2;
10162 beginline(BL_WHITE | BL_FIX);
10163 if (setmark(*eap->arg) == FAIL) /* set mark */
10164 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10165 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10166 }
10167}
10168
10169/*
10170 * Update w_topline, w_leftcol and the cursor position.
10171 */
10172 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010173update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174{
10175 check_cursor(); /* put cursor on valid line */
10176 update_topline();
10177 if (!curwin->w_p_wrap)
10178 validate_cursor();
10179 update_curswant();
10180}
10181
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182/*
10183 * ":normal[!] {commands}": Execute normal mode commands.
10184 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010185 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010186ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 int save_msg_scroll = msg_scroll;
10189 int save_restart_edit = restart_edit;
10190 int save_msg_didout = msg_didout;
10191 int save_State = State;
10192 tasave_T tabuf;
10193 int save_insertmode = p_im;
10194 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010195 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196#ifdef FEAT_MBYTE
10197 char_u *arg = NULL;
10198 int l;
10199 char_u *p;
10200#endif
10201
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010202 if (ex_normal_lock > 0)
10203 {
10204 EMSG(_(e_secure));
10205 return;
10206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207 if (ex_normal_busy >= p_mmd)
10208 {
10209 EMSG(_("E192: Recursive use of :normal too deep"));
10210 return;
10211 }
10212 ++ex_normal_busy;
10213
10214 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10215 restart_edit = 0; /* don't go to Insert mode */
10216 p_im = FALSE; /* don't use 'insertmode' */
10217
10218#ifdef FEAT_MBYTE
10219 /*
10220 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10221 * this for the K_SPECIAL leading byte, otherwise special keys will not
10222 * work.
10223 */
10224 if (has_mbyte)
10225 {
10226 int len = 0;
10227
10228 /* Count the number of characters to be escaped. */
10229 for (p = eap->arg; *p != NUL; ++p)
10230 {
10231# ifdef FEAT_GUI
10232 if (*p == CSI) /* leadbyte CSI */
10233 len += 2;
10234# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010235 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10237# ifdef FEAT_GUI
10238 || *p == CSI
10239# endif
10240 )
10241 len += 2;
10242 }
10243 if (len > 0)
10244 {
10245 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10246 if (arg != NULL)
10247 {
10248 len = 0;
10249 for (p = eap->arg; *p != NUL; ++p)
10250 {
10251 arg[len++] = *p;
10252# ifdef FEAT_GUI
10253 if (*p == CSI)
10254 {
10255 arg[len++] = KS_EXTRA;
10256 arg[len++] = (int)KE_CSI;
10257 }
10258# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010259 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 {
10261 arg[len++] = *++p;
10262 if (*p == K_SPECIAL)
10263 {
10264 arg[len++] = KS_SPECIAL;
10265 arg[len++] = KE_FILLER;
10266 }
10267# ifdef FEAT_GUI
10268 else if (*p == CSI)
10269 {
10270 arg[len++] = KS_EXTRA;
10271 arg[len++] = (int)KE_CSI;
10272 }
10273# endif
10274 }
10275 arg[len] = NUL;
10276 }
10277 }
10278 }
10279 }
10280#endif
10281
10282 /*
10283 * Save the current typeahead. This is required to allow using ":normal"
10284 * from an event handler and makes sure we don't hang when the argument
10285 * ends with half a command.
10286 */
10287 save_typeahead(&tabuf);
10288 if (tabuf.typebuf_valid)
10289 {
10290 /*
10291 * Repeat the :normal command for each line in the range. When no
10292 * range given, execute it just once, without positioning the cursor
10293 * first.
10294 */
10295 do
10296 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 if (eap->addr_count != 0)
10298 {
10299 curwin->w_cursor.lnum = eap->line1++;
10300 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010301 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302 }
10303
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010304 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305#ifdef FEAT_MBYTE
10306 arg != NULL ? arg :
10307#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010308 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309 }
10310 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10311 }
10312
10313 /* Might not return to the main loop when in an event handler. */
10314 update_topline_cursor();
10315
10316 /* Restore the previous typeahead. */
10317 restore_typeahead(&tabuf);
10318
10319 --ex_normal_busy;
10320 msg_scroll = save_msg_scroll;
10321 restart_edit = save_restart_edit;
10322 p_im = save_insertmode;
10323 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010324 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
10326
10327 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +010010328 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +010010330#ifdef FEAT_MOUSE
10331 setmouse();
10332#endif
10333#ifdef CURSOR_SHAPE
10334 ui_cursor_shape(); /* may show different cursor shape */
10335#endif
10336
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337#ifdef FEAT_MBYTE
10338 vim_free(arg);
10339#endif
10340}
10341
10342/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010343 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 */
10345 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010346ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010348 if (eap->forceit)
10349 {
10350 coladvance((colnr_T)MAXCOL);
10351 curwin->w_curswant = MAXCOL;
10352 curwin->w_set_curswant = FALSE;
10353 }
10354
Bram Moolenaara40c5002005-01-09 21:16:21 +000010355 /* Ignore the command when already in Insert mode. Inserting an
10356 * expression register that invokes a function can do this. */
10357 if (State & INSERT)
10358 return;
10359
Bram Moolenaar64969662005-12-14 21:59:55 +000010360 if (eap->cmdidx == CMD_startinsert)
10361 restart_edit = 'a';
10362 else if (eap->cmdidx == CMD_startreplace)
10363 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010365 restart_edit = 'V';
10366
10367 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010369 if (eap->cmdidx == CMD_startinsert)
10370 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371 curwin->w_curswant = 0; /* avoid MAXCOL */
10372 }
10373}
10374
10375/*
10376 * ":stopinsert"
10377 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010379ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380{
10381 restart_edit = 0;
10382 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010383 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010386/*
10387 * Execute normal mode command "cmd".
10388 * "remap" can be REMAP_NONE or REMAP_YES.
10389 */
10390 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010391exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010392{
Bram Moolenaar25281632016-01-21 23:32:32 +010010393 /* Stuff the argument into the typeahead buffer. */
10394 ins_typebuf(cmd, remap, 0, TRUE, silent);
10395 exec_normal(FALSE);
10396}
Bram Moolenaar25281632016-01-21 23:32:32 +010010397
Bram Moolenaar25281632016-01-21 23:32:32 +010010398/*
10399 * Execute normal_cmd() until there is no typeahead left.
10400 */
10401 void
10402exec_normal(int was_typed)
10403{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010404 oparg_T oa;
10405
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010406 clear_oparg(&oa);
10407 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010408 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10409 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010410 {
10411 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010412 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010413 }
10414}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010415
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416#ifdef FEAT_FIND_ID
10417 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010418ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419{
10420 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10421 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10422 (linenr_T)1, (linenr_T)MAXLNUM);
10423}
10424
10425#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
10426/*
10427 * ":psearch"
10428 */
10429 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010430ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431{
10432 g_do_tagpreview = p_pvh;
10433 ex_findpat(eap);
10434 g_do_tagpreview = 0;
10435}
10436#endif
10437
10438 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010439ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440{
10441 int whole = TRUE;
10442 long n;
10443 char_u *p;
10444 int action;
10445
10446 switch (cmdnames[eap->cmdidx].cmd_name[2])
10447 {
10448 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10449 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10450 action = ACTION_GOTO;
10451 else
10452 action = ACTION_SHOW;
10453 break;
10454 case 'i': /* ":ilist" and ":dlist" */
10455 action = ACTION_SHOW_ALL;
10456 break;
10457 case 'u': /* ":ijump" and ":djump" */
10458 action = ACTION_GOTO;
10459 break;
10460 default: /* ":isplit" and ":dsplit" */
10461 action = ACTION_SPLIT;
10462 break;
10463 }
10464
10465 n = 1;
10466 if (vim_isdigit(*eap->arg)) /* get count */
10467 {
10468 n = getdigits(&eap->arg);
10469 eap->arg = skipwhite(eap->arg);
10470 }
10471 if (*eap->arg == '/') /* Match regexp, not just whole words */
10472 {
10473 whole = FALSE;
10474 ++eap->arg;
10475 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10476 if (*p)
10477 {
10478 *p++ = NUL;
10479 p = skipwhite(p);
10480
10481 /* Check for trailing illegal characters */
10482 if (!ends_excmd(*p))
10483 eap->errmsg = e_trailing;
10484 else
10485 eap->nextcmd = check_nextcmd(p);
10486 }
10487 }
10488 if (!eap->skip)
10489 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10490 whole, !eap->forceit,
10491 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10492 n, action, eap->line1, eap->line2);
10493}
10494#endif
10495
10496#ifdef FEAT_WINDOWS
10497
10498# ifdef FEAT_QUICKFIX
10499/*
10500 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10501 */
10502 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010503ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010505 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10507}
10508
10509/*
10510 * ":pedit"
10511 */
10512 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010513ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514{
10515 win_T *curwin_save = curwin;
10516
10517 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010518 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519 keep_help_flag = curwin_save->w_buffer->b_help;
10520 do_exedit(eap, NULL);
10521 keep_help_flag = FALSE;
10522 if (curwin != curwin_save && win_valid(curwin_save))
10523 {
10524 /* Return cursor to where we were */
10525 validate_cursor();
10526 redraw_later(VALID);
10527 win_enter(curwin_save, TRUE);
10528 }
10529 g_do_tagpreview = 0;
10530}
10531# endif
10532
10533/*
10534 * ":stag", ":stselect" and ":stjump".
10535 */
10536 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010537ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538{
10539 postponed_split = -1;
10540 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010541 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10543 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010544 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545}
10546#endif
10547
10548/*
10549 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10550 */
10551 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010552ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553{
10554 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10555}
10556
10557 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010558ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559{
10560 int cmd;
10561
10562 switch (name[1])
10563 {
10564 case 'j': cmd = DT_JUMP; /* ":tjump" */
10565 break;
10566 case 's': cmd = DT_SELECT; /* ":tselect" */
10567 break;
10568 case 'p': cmd = DT_PREV; /* ":tprevious" */
10569 break;
10570 case 'N': cmd = DT_PREV; /* ":tNext" */
10571 break;
10572 case 'n': cmd = DT_NEXT; /* ":tnext" */
10573 break;
10574 case 'o': cmd = DT_POP; /* ":pop" */
10575 break;
10576 case 'f': /* ":tfirst" */
10577 case 'r': cmd = DT_FIRST; /* ":trewind" */
10578 break;
10579 case 'l': cmd = DT_LAST; /* ":tlast" */
10580 break;
10581 default: /* ":tag" */
10582#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010583 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010584 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010585 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586 return;
10587 }
10588#endif
10589 cmd = DT_TAG;
10590 break;
10591 }
10592
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010593 if (name[0] == 'l')
10594 {
10595#ifndef FEAT_QUICKFIX
10596 ex_ni(eap);
10597 return;
10598#else
10599 cmd = DT_LTAG;
10600#endif
10601 }
10602
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10604 eap->forceit, TRUE);
10605}
10606
10607/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010608 * Check "str" for starting with a special cmdline variable.
10609 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10610 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10611 */
10612 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010613find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010614{
10615 int len;
10616 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010617 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010618 "%",
10619#define SPEC_PERC 0
10620 "#",
10621#define SPEC_HASH 1
10622 "<cword>", /* cursor word */
10623#define SPEC_CWORD 2
10624 "<cWORD>", /* cursor WORD */
10625#define SPEC_CCWORD 3
10626 "<cfile>", /* cursor path name */
10627#define SPEC_CFILE 4
10628 "<sfile>", /* ":so" file name */
10629#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010630 "<slnum>", /* ":so" file line number */
10631#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010632#ifdef FEAT_AUTOCMD
10633 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010634# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010635 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010636# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010637 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010638# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010639#endif
10640#ifdef FEAT_CLIENTSERVER
10641 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010642# ifdef FEAT_AUTOCMD
10643# define SPEC_CLIENT 10
10644# else
10645# define SPEC_CLIENT 7
10646# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010647#endif
10648 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010649
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010650 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010651 {
10652 len = (int)STRLEN(spec_str[i]);
10653 if (STRNCMP(src, spec_str[i], len) == 0)
10654 {
10655 *usedlen = len;
10656 return i;
10657 }
10658 }
10659 return -1;
10660}
10661
10662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663 * Evaluate cmdline variables.
10664 *
10665 * change '%' to curbuf->b_ffname
10666 * '#' to curwin->w_altfile
10667 * '<cword>' to word under the cursor
10668 * '<cWORD>' to WORD under the cursor
10669 * '<cfile>' to path name under the cursor
10670 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010671 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672 * '<afile>' to file name for autocommand
10673 * '<abuf>' to buffer number for autocommand
10674 * '<amatch>' to matching name for autocommand
10675 *
10676 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10677 * "" for error without a message) and NULL is returned.
10678 * Returns an allocated string if a valid match was found.
10679 * Returns NULL if no match was found. "usedlen" then still contains the
10680 * number of characters to skip.
10681 */
10682 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010683eval_vars(
10684 char_u *src, /* pointer into commandline */
10685 char_u *srcstart, /* beginning of valid memory for src */
10686 int *usedlen, /* characters after src that are used */
10687 linenr_T *lnump, /* line number for :e command, or NULL */
10688 char_u **errormsg, /* pointer to error message */
10689 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010690 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691{
10692 int i;
10693 char_u *s;
10694 char_u *result;
10695 char_u *resultbuf = NULL;
10696 int resultlen;
10697 buf_T *buf;
10698 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10699 int spec_idx;
10700#ifdef FEAT_MODIFY_FNAME
10701 int skip_mod = FALSE;
10702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704
10705 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010706 if (escaped != NULL)
10707 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708
10709 /*
10710 * Check if there is something to do.
10711 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010712 spec_idx = find_cmdline_var(src, usedlen);
10713 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714 {
10715 *usedlen = 1;
10716 return NULL;
10717 }
10718
10719 /*
10720 * Skip when preceded with a backslash "\%" and "\#".
10721 * Note: In "\\%" the % is also not recognized!
10722 */
10723 if (src > srcstart && src[-1] == '\\')
10724 {
10725 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010726 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727 return NULL;
10728 }
10729
10730 /*
10731 * word or WORD under cursor
10732 */
10733 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10734 {
10735 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10736 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10737 if (resultlen == 0)
10738 {
10739 *errormsg = (char_u *)"";
10740 return NULL;
10741 }
10742 }
10743
10744 /*
10745 * '#': Alternate file name
10746 * '%': Current file name
10747 * File name under the cursor
10748 * File name for autocommand
10749 * and following modifiers
10750 */
10751 else
10752 {
10753 switch (spec_idx)
10754 {
10755 case SPEC_PERC: /* '%': current file */
10756 if (curbuf->b_fname == NULL)
10757 {
10758 result = (char_u *)"";
10759 valid = 0; /* Must have ":p:h" to be valid */
10760 }
10761 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763 break;
10764
10765 case SPEC_HASH: /* '#' or "#99": alternate file */
10766 if (src[1] == '#') /* "##": the argument list */
10767 {
10768 result = arg_all();
10769 resultbuf = result;
10770 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010771 if (escaped != NULL)
10772 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773#ifdef FEAT_MODIFY_FNAME
10774 skip_mod = TRUE;
10775#endif
10776 break;
10777 }
10778 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010779 if (*s == '<') /* "#<99" uses v:oldfiles */
10780 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781 i = (int)getdigits(&s);
10782 *usedlen = (int)(s - src); /* length of what we expand */
10783
Bram Moolenaard812df62008-11-09 12:46:09 +000010784 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010786 if (*usedlen < 2)
10787 {
10788 /* Should we give an error message for #<text? */
10789 *usedlen = 1;
10790 return NULL;
10791 }
10792#ifdef FEAT_EVAL
10793 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10794 (long)i);
10795 if (result == NULL)
10796 {
10797 *errormsg = (char_u *)"";
10798 return NULL;
10799 }
10800#else
10801 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804 }
10805 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010806 {
10807 buf = buflist_findnr(i);
10808 if (buf == NULL)
10809 {
10810 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10811 return NULL;
10812 }
10813 if (lnump != NULL)
10814 *lnump = ECMD_LAST;
10815 if (buf->b_fname == NULL)
10816 {
10817 result = (char_u *)"";
10818 valid = 0; /* Must have ":p:h" to be valid */
10819 }
10820 else
10821 result = buf->b_fname;
10822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010823 break;
10824
10825#ifdef FEAT_SEARCHPATH
10826 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010827 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828 if (result == NULL)
10829 {
10830 *errormsg = (char_u *)"";
10831 return NULL;
10832 }
10833 resultbuf = result; /* remember allocated string */
10834 break;
10835#endif
10836
10837#ifdef FEAT_AUTOCMD
10838 case SPEC_AFILE: /* file name for autocommand */
10839 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010840 if (result != NULL && !autocmd_fname_full)
10841 {
10842 /* Still need to turn the fname into a full path. It is
10843 * postponed to avoid a delay when <afile> is not used. */
10844 autocmd_fname_full = TRUE;
10845 result = FullName_save(autocmd_fname, FALSE);
10846 vim_free(autocmd_fname);
10847 autocmd_fname = result;
10848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849 if (result == NULL)
10850 {
10851 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10852 return NULL;
10853 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010854 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 break;
10856
10857 case SPEC_ABUF: /* buffer number for autocommand */
10858 if (autocmd_bufnr <= 0)
10859 {
10860 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10861 return NULL;
10862 }
10863 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10864 result = strbuf;
10865 break;
10866
10867 case SPEC_AMATCH: /* match name for autocommand */
10868 result = autocmd_match;
10869 if (result == NULL)
10870 {
10871 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10872 return NULL;
10873 }
10874 break;
10875
10876#endif
10877 case SPEC_SFILE: /* file name for ":so" command */
10878 result = sourcing_name;
10879 if (result == NULL)
10880 {
10881 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10882 return NULL;
10883 }
10884 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010885 case SPEC_SLNUM: /* line in file for ":so" command */
10886 if (sourcing_name == NULL || sourcing_lnum == 0)
10887 {
10888 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10889 return NULL;
10890 }
10891 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10892 result = strbuf;
10893 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894#if defined(FEAT_CLIENTSERVER)
10895 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010896 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10897 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898 result = strbuf;
10899 break;
10900#endif
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020010901 default:
10902 result = (char_u *)""; /* avoid gcc warning */
10903 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904 }
10905
10906 resultlen = (int)STRLEN(result); /* length of new string */
10907 if (src[*usedlen] == '<') /* remove the file name extension */
10908 {
10909 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911 resultlen = (int)(s - result);
10912 }
10913#ifdef FEAT_MODIFY_FNAME
10914 else if (!skip_mod)
10915 {
10916 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10917 &resultlen);
10918 if (result == NULL)
10919 {
10920 *errormsg = (char_u *)"";
10921 return NULL;
10922 }
10923 }
10924#endif
10925 }
10926
10927 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10928 {
10929 if (valid != VALID_HEAD + VALID_PATH)
10930 /* xgettext:no-c-format */
10931 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10932 else
10933 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10934 result = NULL;
10935 }
10936 else
10937 result = vim_strnsave(result, resultlen);
10938 vim_free(resultbuf);
10939 return result;
10940}
10941
10942/*
10943 * Concatenate all files in the argument list, separated by spaces, and return
10944 * it in one allocated string.
10945 * Spaces and backslashes in the file names are escaped with a backslash.
10946 * Returns NULL when out of memory.
10947 */
10948 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010949arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950{
10951 int len;
10952 int idx;
10953 char_u *retval = NULL;
10954 char_u *p;
10955
10956 /*
10957 * Do this loop two times:
10958 * first time: compute the total length
10959 * second time: concatenate the names
10960 */
10961 for (;;)
10962 {
10963 len = 0;
10964 for (idx = 0; idx < ARGCOUNT; ++idx)
10965 {
10966 p = alist_name(&ARGLIST[idx]);
10967 if (p != NULL)
10968 {
10969 if (len > 0)
10970 {
10971 /* insert a space in between names */
10972 if (retval != NULL)
10973 retval[len] = ' ';
10974 ++len;
10975 }
10976 for ( ; *p != NUL; ++p)
10977 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010978 if (*p == ' '
10979#ifndef BACKSLASH_IN_FILENAME
10980 || *p == '\\'
10981#endif
10982 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983 {
10984 /* insert a backslash */
10985 if (retval != NULL)
10986 retval[len] = '\\';
10987 ++len;
10988 }
10989 if (retval != NULL)
10990 retval[len] = *p;
10991 ++len;
10992 }
10993 }
10994 }
10995
10996 /* second time: break here */
10997 if (retval != NULL)
10998 {
10999 retval[len] = NUL;
11000 break;
11001 }
11002
11003 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000011004 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005 if (retval == NULL)
11006 break;
11007 }
11008
11009 return retval;
11010}
11011
11012#if defined(FEAT_AUTOCMD) || defined(PROTO)
11013/*
11014 * Expand the <sfile> string in "arg".
11015 *
11016 * Returns an allocated string, or NULL for any error.
11017 */
11018 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011019expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020{
11021 char_u *errormsg;
11022 int len;
11023 char_u *result;
11024 char_u *newres;
11025 char_u *repl;
11026 int srclen;
11027 char_u *p;
11028
11029 result = vim_strsave(arg);
11030 if (result == NULL)
11031 return NULL;
11032
11033 for (p = result; *p; )
11034 {
11035 if (STRNCMP(p, "<sfile>", 7) != 0)
11036 ++p;
11037 else
11038 {
11039 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000011040 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 if (errormsg != NULL)
11042 {
11043 if (*errormsg)
11044 emsg(errormsg);
11045 vim_free(result);
11046 return NULL;
11047 }
11048 if (repl == NULL) /* no match (cannot happen) */
11049 {
11050 p += srclen;
11051 continue;
11052 }
11053 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11054 newres = alloc(len);
11055 if (newres == NULL)
11056 {
11057 vim_free(repl);
11058 vim_free(result);
11059 return NULL;
11060 }
11061 mch_memmove(newres, result, (size_t)(p - result));
11062 STRCPY(newres + (p - result), repl);
11063 len = (int)STRLEN(newres);
11064 STRCAT(newres, p + srclen);
11065 vim_free(repl);
11066 vim_free(result);
11067 result = newres;
11068 p = newres + len; /* continue after the match */
11069 }
11070 }
11071
11072 return result;
11073}
11074#endif
11075
11076#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011077static int ses_winsizes(FILE *fd, int restore_size,
11078 win_T *tab_firstwin);
11079static int ses_win_rec(FILE *fd, frame_T *fr);
11080static frame_T *ses_skipframe(frame_T *fr);
11081static int ses_do_frame(frame_T *fr);
11082static int ses_do_win(win_T *wp);
11083static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11084static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
11085static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086
11087/*
11088 * Write openfile commands for the current buffers to an .exrc file.
11089 * Return FAIL on error, OK otherwise.
11090 */
11091 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011092makeopens(
11093 FILE *fd,
11094 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095{
11096 buf_T *buf;
11097 int only_save_windows = TRUE;
11098 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099 int restore_size = TRUE;
11100 win_T *wp;
11101 char_u *sname;
11102 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011103 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011104 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011105 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011106 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011107 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011108 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109
11110 if (ssop_flags & SSOP_BUFFERS)
11111 only_save_windows = FALSE; /* Save ALL buffers */
11112
11113 /*
11114 * Begin by setting the this_session variable, and then other
11115 * sessionable variables.
11116 */
11117#ifdef FEAT_EVAL
11118 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11119 return FAIL;
11120 if (ssop_flags & SSOP_GLOBALS)
11121 if (store_session_globals(fd) == FAIL)
11122 return FAIL;
11123#endif
11124
11125 /*
11126 * Close all windows but one.
11127 */
11128 if (put_line(fd, "silent only") == FAIL)
11129 return FAIL;
11130
11131 /*
11132 * Now a :cd command to the session directory or the current directory
11133 */
11134 if (ssop_flags & SSOP_SESDIR)
11135 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011136 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11137 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138 return FAIL;
11139 }
11140 else if (ssop_flags & SSOP_CURDIR)
11141 {
11142 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11143 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011144 || fputs("cd ", fd) < 0
11145 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11146 || put_eol(fd) == FAIL)
11147 {
11148 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151 vim_free(sname);
11152 }
11153
11154 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011155 * If there is an empty, unnamed buffer we will wipe it out later.
11156 * Remember the buffer number.
11157 */
11158 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11159 return FAIL;
11160 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11161 return FAIL;
11162 if (put_line(fd, "endif") == FAIL)
11163 return FAIL;
11164
11165 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166 * Now save the current files, current buffer first.
11167 */
11168 if (put_line(fd, "set shortmess=aoO") == FAIL)
11169 return FAIL;
11170
11171 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011172 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173 {
11174 if (!(only_save_windows && buf->b_nwindows == 0)
11175 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
11176 && buf->b_fname != NULL
11177 && buf->b_p_bl)
11178 {
11179 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11180 : buf->b_wininfo->wi_fpos.lnum) < 0
11181 || ses_fname(fd, buf, &ssop_flags) == FAIL)
11182 return FAIL;
11183 }
11184 }
11185
11186 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011187 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11189 return FAIL;
11190
11191 if (ssop_flags & SSOP_RESIZE)
11192 {
11193 /* Note: after the restore we still check it worked!*/
11194 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11195 || put_eol(fd) == FAIL)
11196 return FAIL;
11197 }
11198
11199#ifdef FEAT_GUI
11200 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11201 {
11202 int x, y;
11203
11204 if (gui_mch_get_winpos(&x, &y) == OK)
11205 {
11206 /* Note: after the restore we still check it worked!*/
11207 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11208 return FAIL;
11209 }
11210 }
11211#endif
11212
11213 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011214 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11215 * will be displayed when creating the next tab. That resizes the windows
11216 * in the first tab, which may cause problems. Set 'showtabline' to 2
11217 * temporarily to avoid that.
11218 */
11219 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11220 {
11221 if (put_line(fd, "set stal=2") == FAIL)
11222 return FAIL;
11223 restore_stal = TRUE;
11224 }
11225
11226 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011227 * May repeat putting Windows for each tab, when "tabpages" is in
11228 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011229 * Don't use goto_tabpage(), it may change directory and trigger
11230 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011231 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011232 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011233 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011234 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011235 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011236 int need_tabnew = FALSE;
11237 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011238
Bram Moolenaar18144c82006-04-12 21:52:12 +000011239 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011241 tabpage_T *tp = find_tabpage(tabnr);
11242
11243 if (tp == NULL)
11244 break; /* done all tab pages */
11245 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011246 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011247 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011248 tab_topframe = topframe;
11249 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011250 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011251 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011252 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011253 tab_topframe = tp->tp_topframe;
11254 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011255 if (tabnr > 1)
11256 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258
Bram Moolenaar18144c82006-04-12 21:52:12 +000011259 /*
11260 * Before creating the window layout, try loading one file. If this
11261 * is aborted we don't end up with a number of useless windows.
11262 * This may have side effects! (e.g., compressed or network file).
11263 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011264 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011265 {
11266 if (ses_do_win(wp)
11267 && wp->w_buffer->b_ffname != NULL
11268 && !wp->w_buffer->b_help
11269#ifdef FEAT_QUICKFIX
11270 && !bt_nofile(wp->w_buffer)
11271#endif
11272 )
11273 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011274 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000011275 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
11276 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011277 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011278 if (!wp->w_arg_idx_invalid)
11279 edited_win = wp;
11280 break;
11281 }
11282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011284 /* If no file got edited create an empty tab page. */
11285 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11286 return FAIL;
11287
Bram Moolenaar18144c82006-04-12 21:52:12 +000011288 /*
11289 * Save current window layout.
11290 */
11291 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011293 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011295 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11296 return FAIL;
11297 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11298 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299
Bram Moolenaar18144c82006-04-12 21:52:12 +000011300 /*
11301 * Check if window sizes can be restored (no windows omitted).
11302 * Remember the window number of the current window after restoring.
11303 */
11304 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011305 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011306 {
11307 if (ses_do_win(wp))
11308 ++nr;
11309 else
11310 restore_size = FALSE;
11311 if (curwin == wp)
11312 cnr = nr;
11313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011314
Bram Moolenaar18144c82006-04-12 21:52:12 +000011315 /* Go to the first window. */
11316 if (put_line(fd, "wincmd t") == FAIL)
11317 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011318
Bram Moolenaar18144c82006-04-12 21:52:12 +000011319 /*
11320 * If more than one window, see if sizes can be restored.
11321 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11322 * resized when moving between windows.
11323 * Do this before restoring the view, so that the topline and the
11324 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011325 * winminheight and winminwidth need to be set to avoid an error if the
11326 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011327 */
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011328 if (put_line(fd, "set winminheight=1 winheight=1 winminwidth=1 winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011329 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011330 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011331 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332
Bram Moolenaar18144c82006-04-12 21:52:12 +000011333 /*
11334 * Restore the view of the window (options, file, cursor, etc.).
11335 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011336 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011337 {
11338 if (!ses_do_win(wp))
11339 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011340 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11341 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011342 return FAIL;
11343 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11344 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011345 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011346 }
11347
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011348 /* The argument index in the first tab page is zero, need to set it in
11349 * each window. For further tab pages it's the window where we do
11350 * "tabedit". */
11351 cur_arg_idx = next_arg_idx;
11352
Bram Moolenaar18144c82006-04-12 21:52:12 +000011353 /*
11354 * Restore cursor to the current window if it's not the first one.
11355 */
11356 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11357 || put_eol(fd) == FAIL))
11358 return FAIL;
11359
11360 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011361 * Restore window sizes again after jumping around in windows, because
11362 * the current window has a minimum size while others may not.
11363 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011364 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011365 return FAIL;
11366
Bram Moolenaar18144c82006-04-12 21:52:12 +000011367 /* Don't continue in another tab page when doing only the current one
11368 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011369 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011370 break;
11371 }
11372
11373 if (ssop_flags & SSOP_TABPAGES)
11374 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011375 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11376 || put_eol(fd) == FAIL)
11377 return FAIL;
11378 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011379 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11380 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011381
Bram Moolenaar9c102382006-05-03 21:26:49 +000011382 /*
11383 * Wipe out an empty unnamed buffer we started in.
11384 */
11385 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11386 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011387 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011388 return FAIL;
11389 if (put_line(fd, "endif") == FAIL)
11390 return FAIL;
11391 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11392 return FAIL;
11393
11394 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11395 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11396 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11397 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011398 /* Re-apply 'winminheight' and 'winminwidth'. */
11399 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11400 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11401 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402
11403 /*
11404 * Lastly, execute the x.vim file if it exists.
11405 */
11406 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11407 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011408 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011409 || put_line(fd, "endif") == FAIL)
11410 return FAIL;
11411
11412 return OK;
11413}
11414
11415 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011416ses_winsizes(
11417 FILE *fd,
11418 int restore_size,
11419 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420{
11421 int n = 0;
11422 win_T *wp;
11423
11424 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11425 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011426 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011427 {
11428 if (!ses_do_win(wp))
11429 continue;
11430 ++n;
11431
11432 /* restore height when not full height */
11433 if (wp->w_height + wp->w_status_height < topframe->fr_height
11434 && (fprintf(fd,
11435 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11436 n, (long)wp->w_height, Rows / 2, Rows) < 0
11437 || put_eol(fd) == FAIL))
11438 return FAIL;
11439
11440 /* restore width when not full width */
11441 if (wp->w_width < Columns && (fprintf(fd,
11442 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11443 n, (long)wp->w_width, Columns / 2, Columns) < 0
11444 || put_eol(fd) == FAIL))
11445 return FAIL;
11446 }
11447 }
11448 else
11449 {
11450 /* Just equalise window sizes */
11451 if (put_line(fd, "wincmd =") == FAIL)
11452 return FAIL;
11453 }
11454 return OK;
11455}
11456
11457/*
11458 * Write commands to "fd" to recursively create windows for frame "fr",
11459 * horizontally and vertically split.
11460 * After the commands the last window in the frame is the current window.
11461 * Returns FAIL when writing the commands to "fd" fails.
11462 */
11463 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011464ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465{
11466 frame_T *frc;
11467 int count = 0;
11468
11469 if (fr->fr_layout != FR_LEAF)
11470 {
11471 /* Find first frame that's not skipped and then create a window for
11472 * each following one (first frame is already there). */
11473 frc = ses_skipframe(fr->fr_child);
11474 if (frc != NULL)
11475 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11476 {
11477 /* Make window as big as possible so that we have lots of room
11478 * to split. */
11479 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11480 || put_line(fd, fr->fr_layout == FR_COL
11481 ? "split" : "vsplit") == FAIL)
11482 return FAIL;
11483 ++count;
11484 }
11485
11486 /* Go back to the first window. */
11487 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11488 ? "%dwincmd k" : "%dwincmd h", count) < 0
11489 || put_eol(fd) == FAIL))
11490 return FAIL;
11491
11492 /* Recursively create frames/windows in each window of this column or
11493 * row. */
11494 frc = ses_skipframe(fr->fr_child);
11495 while (frc != NULL)
11496 {
11497 ses_win_rec(fd, frc);
11498 frc = ses_skipframe(frc->fr_next);
11499 /* Go to next window. */
11500 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11501 return FAIL;
11502 }
11503 }
11504 return OK;
11505}
11506
11507/*
11508 * Skip frames that don't contain windows we want to save in the Session.
11509 * Returns NULL when there none.
11510 */
11511 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011512ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011513{
11514 frame_T *frc;
11515
11516 for (frc = fr; frc != NULL; frc = frc->fr_next)
11517 if (ses_do_frame(frc))
11518 break;
11519 return frc;
11520}
11521
11522/*
11523 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11524 * the Session.
11525 */
11526 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011527ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011528{
11529 frame_T *frc;
11530
11531 if (fr->fr_layout == FR_LEAF)
11532 return ses_do_win(fr->fr_win);
11533 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11534 if (ses_do_frame(frc))
11535 return TRUE;
11536 return FALSE;
11537}
11538
11539/*
11540 * Return non-zero if window "wp" is to be stored in the Session.
11541 */
11542 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011543ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011544{
11545 if (wp->w_buffer->b_fname == NULL
11546#ifdef FEAT_QUICKFIX
11547 /* When 'buftype' is "nofile" can't restore the window contents. */
11548 || bt_nofile(wp->w_buffer)
11549#endif
11550 )
11551 return (ssop_flags & SSOP_BLANK);
11552 if (wp->w_buffer->b_help)
11553 return (ssop_flags & SSOP_HELP);
11554 return TRUE;
11555}
11556
11557/*
11558 * Write commands to "fd" to restore the view of a window.
11559 * Caller must make sure 'scrolloff' is zero.
11560 */
11561 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011562put_view(
11563 FILE *fd,
11564 win_T *wp,
11565 int add_edit, /* add ":edit" command to view */
11566 unsigned *flagp, /* vop_flags or ssop_flags */
11567 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011568 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011569{
11570 win_T *save_curwin;
11571 int f;
11572 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011573 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011574
11575 /* Always restore cursor position for ":mksession". For ":mkview" only
11576 * when 'viewoptions' contains "cursor". */
11577 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11578
11579 /*
11580 * Local argument list.
11581 */
11582 if (wp->w_alist == &global_alist)
11583 {
11584 if (put_line(fd, "argglobal") == FAIL)
11585 return FAIL;
11586 }
11587 else
11588 {
11589 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11590 flagp == &vop_flags
11591 || !(*flagp & SSOP_CURDIR)
11592 || wp->w_localdir != NULL, flagp) == FAIL)
11593 return FAIL;
11594 }
11595
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011596 /* Only when part of a session: restore the argument index. Some
11597 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011598 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011599 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011601 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011602 || put_eol(fd) == FAIL)
11603 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011604 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011605 }
11606
11607 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011608 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011609 {
11610 /*
11611 * Load the file.
11612 */
11613 if (wp->w_buffer->b_ffname != NULL
11614#ifdef FEAT_QUICKFIX
11615 && !bt_nofile(wp->w_buffer)
11616#endif
11617 )
11618 {
11619 /*
11620 * Editing a file in this buffer: use ":edit file".
11621 * This may have side effects! (e.g., compressed or network file).
11622 */
11623 if (fputs("edit ", fd) < 0
11624 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11625 return FAIL;
11626 }
11627 else
11628 {
11629 /* No file in this buffer, just make it empty. */
11630 if (put_line(fd, "enew") == FAIL)
11631 return FAIL;
11632#ifdef FEAT_QUICKFIX
11633 if (wp->w_buffer->b_ffname != NULL)
11634 {
11635 /* The buffer does have a name, but it's not a file name. */
11636 if (fputs("file ", fd) < 0
11637 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11638 return FAIL;
11639 }
11640#endif
11641 do_cursor = FALSE;
11642 }
11643 }
11644
11645 /*
11646 * Local mappings and abbreviations.
11647 */
11648 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11649 && makemap(fd, wp->w_buffer) == FAIL)
11650 return FAIL;
11651
11652 /*
11653 * Local options. Need to go to the window temporarily.
11654 * Store only local values when using ":mkview" and when ":mksession" is
11655 * used and 'sessionoptions' doesn't include "options".
11656 * Some folding options are always stored when "folds" is included,
11657 * otherwise the folds would not be restored correctly.
11658 */
11659 save_curwin = curwin;
11660 curwin = wp;
11661 curbuf = curwin->w_buffer;
11662 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11663 f = makeset(fd, OPT_LOCAL,
11664 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11665#ifdef FEAT_FOLDING
11666 else if (*flagp & SSOP_FOLDS)
11667 f = makefoldset(fd);
11668#endif
11669 else
11670 f = OK;
11671 curwin = save_curwin;
11672 curbuf = curwin->w_buffer;
11673 if (f == FAIL)
11674 return FAIL;
11675
11676#ifdef FEAT_FOLDING
11677 /*
11678 * Save Folds when 'buftype' is empty and for help files.
11679 */
11680 if ((*flagp & SSOP_FOLDS)
11681 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar1f2903c2017-07-23 19:51:01 +020011682 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011683 {
11684 if (put_folds(fd, wp) == FAIL)
11685 return FAIL;
11686 }
11687#endif
11688
11689 /*
11690 * Set the cursor after creating folds, since that moves the cursor.
11691 */
11692 if (do_cursor)
11693 {
11694
11695 /* Restore the cursor line in the file and relatively in the
11696 * window. Don't use "G", it changes the jumplist. */
11697 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11698 (long)wp->w_cursor.lnum,
11699 (long)(wp->w_cursor.lnum - wp->w_topline),
11700 (long)wp->w_height / 2, (long)wp->w_height) < 0
11701 || put_eol(fd) == FAIL
11702 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11703 || put_line(fd, "exe s:l") == FAIL
11704 || put_line(fd, "normal! zt") == FAIL
11705 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11706 || put_eol(fd) == FAIL)
11707 return FAIL;
11708 /* Restore the cursor column and left offset when not wrapping. */
11709 if (wp->w_cursor.col == 0)
11710 {
11711 if (put_line(fd, "normal! 0") == FAIL)
11712 return FAIL;
11713 }
11714 else
11715 {
11716 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11717 {
11718 if (fprintf(fd,
11719 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011720 (long)wp->w_virtcol + 1,
11721 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011722 (long)wp->w_width / 2, (long)wp->w_width) < 0
11723 || put_eol(fd) == FAIL
11724 || put_line(fd, "if s:c > 0") == FAIL
11725 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011726 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11727 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011728 || put_eol(fd) == FAIL
11729 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011730 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731 || put_eol(fd) == FAIL
11732 || put_line(fd, "endif") == FAIL)
11733 return FAIL;
11734 }
11735 else
11736 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011737 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738 || put_eol(fd) == FAIL)
11739 return FAIL;
11740 }
11741 }
11742 }
11743
11744 /*
11745 * Local directory.
11746 */
11747 if (wp->w_localdir != NULL)
11748 {
11749 if (fputs("lcd ", fd) < 0
11750 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11751 || put_eol(fd) == FAIL)
11752 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011753 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754 }
11755
11756 return OK;
11757}
11758
11759/*
11760 * Write an argument list to the session file.
11761 * Returns FAIL if writing fails.
11762 */
11763 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011764ses_arglist(
11765 FILE *fd,
11766 char *cmd,
11767 garray_T *gap,
11768 int fullname, /* TRUE: use full path name */
11769 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011770{
11771 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011772 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011773 char_u *s;
11774
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011775 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11776 return FAIL;
11777 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011778 return FAIL;
11779 for (i = 0; i < gap->ga_len; ++i)
11780 {
11781 /* NULL file names are skipped (only happens when out of memory). */
11782 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11783 if (s != NULL)
11784 {
11785 if (fullname)
11786 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011787 buf = alloc(MAXPATHL);
11788 if (buf != NULL)
11789 {
11790 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11791 s = buf;
11792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011793 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010011794 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011795 || ses_put_fname(fd, s, flagp) == FAIL
11796 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011797 {
11798 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011799 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011800 }
11801 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011802 }
11803 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011804 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011805}
11806
11807/*
11808 * Write a buffer name to the session file.
11809 * Also ends the line.
11810 * Returns FAIL if writing fails.
11811 */
11812 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011813ses_fname(FILE *fd, buf_T *buf, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011814{
11815 char_u *name;
11816
11817 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011818 * the session file will be sourced.
11819 * Don't do this for ":mkview", we don't know the current directory.
11820 * Don't do this after ":lcd", we don't keep track of what the current
11821 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011822 if (buf->b_sfname != NULL
11823 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011824 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011825#ifdef FEAT_AUTOCHDIR
11826 && !p_acd
11827#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011828 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011829 name = buf->b_sfname;
11830 else
11831 name = buf->b_ffname;
11832 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11833 return FAIL;
11834 return OK;
11835}
11836
11837/*
11838 * Write a file name to the session file.
11839 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11840 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011841 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842 */
11843 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011844ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011845{
11846 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011847 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011848 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849
11850 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011851 if (sname == NULL)
11852 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011853
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011854 if (*flagp & SSOP_SLASH)
11855 {
11856 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011857 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011858 if (*p == '\\')
11859 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011861
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011862 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011863 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011865 if (p == NULL)
11866 return FAIL;
11867
11868 /* write the result */
11869 if (fputs((char *)p, fd) < 0)
11870 retval = FAIL;
11871
11872 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873 return retval;
11874}
11875
11876/*
11877 * ":loadview [nr]"
11878 */
11879 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011880ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011881{
11882 char_u *fname;
11883
11884 fname = get_view_file(*eap->arg);
11885 if (fname != NULL)
11886 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011887 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011888 vim_free(fname);
11889 }
11890}
11891
11892/*
11893 * Get the name of the view file for the current buffer.
11894 */
11895 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011896get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011897{
11898 int len = 0;
11899 char_u *p, *s;
11900 char_u *retval;
11901 char_u *sname;
11902
11903 if (curbuf->b_ffname == NULL)
11904 {
11905 EMSG(_(e_noname));
11906 return NULL;
11907 }
11908 sname = home_replace_save(NULL, curbuf->b_ffname);
11909 if (sname == NULL)
11910 return NULL;
11911
11912 /*
11913 * We want a file name without separators, because we're not going to make
11914 * a directory.
11915 * "normal" path separator -> "=+"
11916 * "=" -> "=="
11917 * ":" path separator -> "=-"
11918 */
11919 for (p = sname; *p; ++p)
11920 if (*p == '=' || vim_ispathsep(*p))
11921 ++len;
11922 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11923 if (retval != NULL)
11924 {
11925 STRCPY(retval, p_vdir);
11926 add_pathsep(retval);
11927 s = retval + STRLEN(retval);
11928 for (p = sname; *p; ++p)
11929 {
11930 if (*p == '=')
11931 {
11932 *s++ = '=';
11933 *s++ = '=';
11934 }
11935 else if (vim_ispathsep(*p))
11936 {
11937 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011938#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011939 if (*p == ':')
11940 *s++ = '-';
11941 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011942#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011943 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011944 }
11945 else
11946 *s++ = *p;
11947 }
11948 *s++ = '=';
11949 *s++ = c;
11950 STRCPY(s, ".vim");
11951 }
11952
11953 vim_free(sname);
11954 return retval;
11955}
11956
11957#endif /* FEAT_SESSION */
11958
11959/*
11960 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11961 * Return FAIL for a write error.
11962 */
11963 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011964put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011965{
11966 if (
11967#ifdef USE_CRNL
11968 (
11969# ifdef MKSESSION_NL
11970 !mksession_nl &&
11971# endif
11972 (putc('\r', fd) < 0)) ||
11973#endif
11974 (putc('\n', fd) < 0))
11975 return FAIL;
11976 return OK;
11977}
11978
11979/*
11980 * Write a line to "fd".
11981 * Return FAIL for a write error.
11982 */
11983 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011984put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985{
11986 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11987 return FAIL;
11988 return OK;
11989}
11990
11991#ifdef FEAT_VIMINFO
11992/*
11993 * ":rviminfo" and ":wviminfo".
11994 */
11995 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011996ex_viminfo(
11997 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011998{
11999 char_u *save_viminfo;
12000
12001 save_viminfo = p_viminfo;
12002 if (*p_viminfo == NUL)
12003 p_viminfo = (char_u *)"'100";
12004 if (eap->cmdidx == CMD_rviminfo)
12005 {
Bram Moolenaard812df62008-11-09 12:46:09 +000012006 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
12007 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008 EMSG(_("E195: Cannot open viminfo file for reading"));
12009 }
12010 else
12011 write_viminfo(eap->arg, eap->forceit);
12012 p_viminfo = save_viminfo;
12013}
12014#endif
12015
12016#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012017/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020012018 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000012019 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012020 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012021 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012022dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012023{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024 if (fname == NULL)
12025 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012026 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012027}
12028#endif
12029
12030/*
12031 * ":behave {mswin,xterm}"
12032 */
12033 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012034ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012035{
12036 if (STRCMP(eap->arg, "mswin") == 0)
12037 {
12038 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12039 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12040 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12041 set_option_value((char_u *)"keymodel", 0L,
12042 (char_u *)"startsel,stopsel", 0);
12043 }
12044 else if (STRCMP(eap->arg, "xterm") == 0)
12045 {
12046 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12047 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12048 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12049 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12050 }
12051 else
12052 EMSG2(_(e_invarg2), eap->arg);
12053}
12054
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012055#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12056/*
12057 * Function given to ExpandGeneric() to obtain the possible arguments of the
12058 * ":behave {mswin,xterm}" command.
12059 */
12060 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012061get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012062{
12063 if (idx == 0)
12064 return (char_u *)"mswin";
12065 if (idx == 1)
12066 return (char_u *)"xterm";
12067 return NULL;
12068}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012069
12070/*
12071 * Function given to ExpandGeneric() to obtain the possible arguments of the
12072 * ":messages {clear}" command.
12073 */
12074 char_u *
12075get_messages_arg(expand_T *xp UNUSED, int idx)
12076{
12077 if (idx == 0)
12078 return (char_u *)"clear";
12079 return NULL;
12080}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012081#endif
12082
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083#ifdef FEAT_AUTOCMD
12084static int filetype_detect = FALSE;
12085static int filetype_plugin = FALSE;
12086static int filetype_indent = FALSE;
12087
12088/*
12089 * ":filetype [plugin] [indent] {on,off,detect}"
12090 * on: Load the filetype.vim file to install autocommands for file types.
12091 * off: Load the ftoff.vim file to remove all autocommands for file types.
12092 * plugin on: load filetype.vim and ftplugin.vim
12093 * plugin off: load ftplugof.vim
12094 * indent on: load filetype.vim and indent.vim
12095 * indent off: load indoff.vim
12096 */
12097 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012098ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012099{
12100 char_u *arg = eap->arg;
12101 int plugin = FALSE;
12102 int indent = FALSE;
12103
12104 if (*eap->arg == NUL)
12105 {
12106 /* Print current status. */
12107 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
12108 filetype_detect ? "ON" : "OFF",
12109 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12110 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12111 return;
12112 }
12113
12114 /* Accept "plugin" and "indent" in any order. */
12115 for (;;)
12116 {
12117 if (STRNCMP(arg, "plugin", 6) == 0)
12118 {
12119 plugin = TRUE;
12120 arg = skipwhite(arg + 6);
12121 continue;
12122 }
12123 if (STRNCMP(arg, "indent", 6) == 0)
12124 {
12125 indent = TRUE;
12126 arg = skipwhite(arg + 6);
12127 continue;
12128 }
12129 break;
12130 }
12131 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12132 {
12133 if (*arg == 'o' || !filetype_detect)
12134 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012135 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012136 filetype_detect = TRUE;
12137 if (plugin)
12138 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012139 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012140 filetype_plugin = TRUE;
12141 }
12142 if (indent)
12143 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012144 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012145 filetype_indent = TRUE;
12146 }
12147 }
12148 if (*arg == 'd')
12149 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012150 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012151 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012152 }
12153 }
12154 else if (STRCMP(arg, "off") == 0)
12155 {
12156 if (plugin || indent)
12157 {
12158 if (plugin)
12159 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012160 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161 filetype_plugin = FALSE;
12162 }
12163 if (indent)
12164 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012165 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012166 filetype_indent = FALSE;
12167 }
12168 }
12169 else
12170 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012171 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012172 filetype_detect = FALSE;
12173 }
12174 }
12175 else
12176 EMSG2(_(e_invarg2), arg);
12177}
12178
12179/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012180 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181 */
12182 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012183ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184{
12185 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012186 {
12187 char_u *arg = eap->arg;
12188
12189 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12190 arg += 9;
12191
12192 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12193 if (arg != eap->arg)
12194 did_filetype = FALSE;
12195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012196}
12197#endif
12198
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012200ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012201{
12202#ifdef FEAT_DIGRAPHS
12203 if (*eap->arg != NUL)
12204 putdigraph(eap->arg);
12205 else
12206 listdigraphs();
12207#else
12208 EMSG(_("E196: No digraphs in this version"));
12209#endif
12210}
12211
12212 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012213ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012214{
12215 int flags = 0;
12216
12217 if (eap->cmdidx == CMD_setlocal)
12218 flags = OPT_LOCAL;
12219 else if (eap->cmdidx == CMD_setglobal)
12220 flags = OPT_GLOBAL;
12221#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
12222 if (cmdmod.browse && flags == 0)
12223 ex_options(eap);
12224 else
12225#endif
12226 (void)do_set(eap->arg, flags);
12227}
12228
12229#ifdef FEAT_SEARCH_EXTRA
12230/*
12231 * ":nohlsearch"
12232 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012233 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012234ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010012236 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012237 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012238}
12239
12240/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012241 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012242 * Sets nextcmd to the start of the next command, if any. Also called when
12243 * skipping commands to find the next command.
12244 */
12245 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012246ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012247{
12248 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012249 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012250 char_u *end;
12251 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012252 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012253
12254 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012255 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012256 else
12257 {
12258 EMSG(e_invcmd);
12259 return;
12260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261
12262 /* First clear any old pattern. */
12263 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012264 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012265
12266 if (ends_excmd(*eap->arg))
12267 end = eap->arg;
12268 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012269 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012270 end = eap->arg + 4;
12271 else
12272 {
12273 p = skiptowhite(eap->arg);
12274 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012275 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012276 p = skipwhite(p);
12277 if (*p == NUL)
12278 {
12279 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012280 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012281 EMSG2(_(e_invarg2), eap->arg);
12282 return;
12283 }
12284 end = skip_regexp(p + 1, *p, TRUE, NULL);
12285 if (!eap->skip)
12286 {
12287 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12288 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012289 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290 eap->errmsg = e_trailing;
12291 return;
12292 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012293 if (*end != *p)
12294 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012295 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012296 EMSG2(_(e_invarg2), p);
12297 return;
12298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012299
12300 c = *end;
12301 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012302 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012303 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012304 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012305 }
12306 }
12307 eap->nextcmd = find_nextcmd(end);
12308}
12309#endif
12310
12311#ifdef FEAT_CRYPT
12312/*
12313 * ":X": Get crypt key
12314 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012315 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012316ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012317{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012318 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012319 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320}
12321#endif
12322
12323#ifdef FEAT_FOLDING
12324 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012325ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012326{
12327 if (foldManualAllowed(TRUE))
12328 foldCreate(eap->line1, eap->line2);
12329}
12330
12331 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012332ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333{
12334 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12335 eap->forceit, FALSE);
12336}
12337
12338 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012339ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012340{
12341 linenr_T lnum;
12342
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012343#ifdef FEAT_CLIPBOARD
12344 start_global_changes();
12345#endif
12346
Bram Moolenaar071d4272004-06-13 20:20:40 +000012347 /* First set the marks for all lines closed/open. */
12348 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12349 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12350 ml_setmarked(lnum);
12351
12352 /* Execute the command on the marked lines. */
12353 global_exe(eap->arg);
12354 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012355#ifdef FEAT_CLIPBOARD
12356 end_global_changes();
12357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012358}
12359#endif