blob: 646f7ec0802043298dd56f23335bcfc356d2636f [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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
125# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000126# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127#endif
128#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
129# define ex_cclose ex_ni
130# define ex_copen ex_ni
131# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +0200132# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000134#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
135# define ex_cexpr ex_ni
136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100138static int check_more(int, int);
139static linenr_T get_address(exarg_T *, char_u **, int addr_type, int skip, int to_other_file);
140static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200141#if !defined(FEAT_PERL) \
142 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
143 || !defined(FEAT_TCL) \
144 || !defined(FEAT_RUBY) \
145 || !defined(FEAT_LUA) \
146 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000147# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100148static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100150static char_u *invalid_range(exarg_T *eap);
151static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000152#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100153static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000154#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100155static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
156static void ex_highlight(exarg_T *eap);
157static void ex_colorscheme(exarg_T *eap);
158static void ex_quit(exarg_T *eap);
159static void ex_cquit(exarg_T *eap);
160static void ex_quit_all(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100162static void ex_close(exarg_T *eap);
163static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
164static void ex_only(exarg_T *eap);
165static void ex_resize(exarg_T *eap);
166static void ex_stag(exarg_T *eap);
167static void ex_tabclose(exarg_T *eap);
168static void ex_tabonly(exarg_T *eap);
169static void ex_tabnext(exarg_T *eap);
170static void ex_tabmove(exarg_T *eap);
171static void ex_tabs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#else
173# define ex_close ex_ni
174# define ex_only ex_ni
175# define ex_all ex_ni
176# define ex_resize ex_ni
177# define ex_splitview ex_ni
178# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000179# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000180# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000181# define ex_tabs ex_ni
182# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000183# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184#endif
185#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100186static void ex_pclose(exarg_T *eap);
187static void ex_ptag(exarg_T *eap);
188static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189#else
190# define ex_pclose ex_ni
191# define ex_ptag ex_ni
192# define ex_pedit ex_ni
193#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100194static void ex_hide(exarg_T *eap);
195static void ex_stop(exarg_T *eap);
196static void ex_exit(exarg_T *eap);
197static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100199static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200#else
201# define ex_goto ex_ni
202#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100203static void ex_shell(exarg_T *eap);
204static void ex_preserve(exarg_T *eap);
205static void ex_recover(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206#ifndef FEAT_LISTCMDS
207# define ex_argedit ex_ni
208# define ex_argadd ex_ni
209# define ex_argdelete ex_ni
210# define ex_listdo ex_ni
211#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100212static void ex_mode(exarg_T *eap);
213static void ex_wrongmodifier(exarg_T *eap);
214static void ex_find(exarg_T *eap);
215static void ex_open(exarg_T *eap);
216static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
218# define ex_drop ex_ni
219#endif
220#ifndef FEAT_GUI
221# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100222static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223#endif
224#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100225static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#else
227# define ex_tearoff ex_ni
228#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000229#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100230static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231#else
232# define ex_popup ex_ni
233#endif
234#ifndef FEAT_GUI_MSWIN
235# define ex_simalt ex_ni
236#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000237#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238# define gui_mch_find_dialog ex_ni
239# define gui_mch_replace_dialog ex_ni
240#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000241#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242# define ex_helpfind ex_ni
243#endif
244#ifndef FEAT_CSCOPE
245# define do_cscope ex_ni
246# define do_scscope ex_ni
247# define do_cstag ex_ni
248#endif
249#ifndef FEAT_SYN_HL
250# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200251# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000252#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200253#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200254# define ex_syntime ex_ni
255#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000256#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000257# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000258# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000259# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000260# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000261# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000262#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200263#ifndef FEAT_PERSISTENT_UNDO
264# define ex_rundo ex_ni
265# define ex_wundo ex_ni
266#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200267#ifndef FEAT_LUA
268# define ex_lua ex_script_ni
269# define ex_luado ex_ni
270# define ex_luafile ex_ni
271#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000272#ifndef FEAT_MZSCHEME
273# define ex_mzscheme ex_script_ni
274# define ex_mzfile ex_ni
275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276#ifndef FEAT_PERL
277# define ex_perl ex_script_ni
278# define ex_perldo ex_ni
279#endif
280#ifndef FEAT_PYTHON
281# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200282# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283# define ex_pyfile ex_ni
284#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200285#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200286# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200287# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200288# define ex_py3file ex_ni
289#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290#ifndef FEAT_TCL
291# define ex_tcl ex_script_ni
292# define ex_tcldo ex_ni
293# define ex_tclfile ex_ni
294#endif
295#ifndef FEAT_RUBY
296# define ex_ruby ex_script_ni
297# define ex_rubydo ex_ni
298# define ex_rubyfile ex_ni
299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300#ifndef FEAT_KEYMAP
301# define ex_loadkeymap ex_ni
302#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100303static void ex_swapname(exarg_T *eap);
304static void ex_syncbind(exarg_T *eap);
305static void ex_read(exarg_T *eap);
306static void ex_pwd(exarg_T *eap);
307static void ex_equal(exarg_T *eap);
308static void ex_sleep(exarg_T *eap);
309static void do_exmap(exarg_T *eap, int isabbrev);
310static void ex_winsize(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100312static void ex_wincmd(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313#else
314# define ex_wincmd ex_ni
315#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000316#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100317static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318#else
319# define ex_winpos ex_ni
320#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100321static void ex_operators(exarg_T *eap);
322static void ex_put(exarg_T *eap);
323static void ex_copymove(exarg_T *eap);
324static void ex_submagic(exarg_T *eap);
325static void ex_join(exarg_T *eap);
326static void ex_at(exarg_T *eap);
327static void ex_bang(exarg_T *eap);
328static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200329#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100330static void ex_wundo(exarg_T *eap);
331static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200332#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100333static void ex_redo(exarg_T *eap);
334static void ex_later(exarg_T *eap);
335static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100336static void ex_redrawstatus(exarg_T *eap);
337static void close_redir(void);
338static void ex_mkrc(exarg_T *eap);
339static void ex_mark(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340#ifdef FEAT_USR_CMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100341static char_u *uc_fun_cmd(void);
342static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100344static void ex_startinsert(exarg_T *eap);
345static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100347static void ex_checkpath(exarg_T *eap);
348static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349#else
350# define ex_findpat ex_ni
351# define ex_checkpath ex_ni
352#endif
353#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100354static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355#else
356# define ex_psearch ex_ni
357#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100358static void ex_tag(exarg_T *eap);
359static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360#ifndef FEAT_EVAL
361# define ex_scriptnames ex_ni
362# define ex_finish ex_ni
363# define ex_echo ex_ni
364# define ex_echohl ex_ni
365# define ex_execute ex_ni
366# define ex_call ex_ni
367# define ex_if ex_ni
368# define ex_endif ex_ni
369# define ex_else ex_ni
370# define ex_while ex_ni
371# define ex_continue ex_ni
372# define ex_break ex_ni
373# define ex_endwhile ex_ni
374# define ex_throw ex_ni
375# define ex_try ex_ni
376# define ex_catch ex_ni
377# define ex_finally ex_ni
378# define ex_endtry ex_ni
379# define ex_endfunction ex_ni
380# define ex_let ex_ni
381# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000382# define ex_lockvar ex_ni
383# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384# define ex_function ex_ni
385# define ex_delfunction ex_ni
386# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000387# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100389static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100391static int makeopens(FILE *fd, char_u *dirnow);
392static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
393static void ex_loadview(exarg_T *eap);
394static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000395static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396#else
397# define ex_loadview ex_ni
398#endif
399#ifndef FEAT_EVAL
400# define ex_compiler ex_ni
401#endif
402#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100403static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404#else
405# define ex_viminfo ex_ni
406#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100407static void ex_behave(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100409static void ex_filetype(exarg_T *eap);
410static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411#else
412# define ex_filetype ex_ni
413# define ex_setfiletype ex_ni
414#endif
415#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000416# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417# define ex_diffpatch ex_ni
418# define ex_diffgetput ex_ni
419# define ex_diffsplit ex_ni
420# define ex_diffthis ex_ni
421# define ex_diffupdate ex_ni
422#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100423static void ex_digraphs(exarg_T *eap);
424static void ex_set(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
426# define ex_options ex_ni
427#endif
428#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100429static void ex_nohlsearch(exarg_T *eap);
430static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431#else
432# define ex_nohlsearch ex_ni
433# define ex_match ex_ni
434#endif
435#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100436static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437#else
438# define ex_X ex_ni
439#endif
440#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100441static void ex_fold(exarg_T *eap);
442static void ex_foldopen(exarg_T *eap);
443static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444#else
445# define ex_fold ex_ni
446# define ex_foldopen ex_ni
447# define ex_folddo ex_ni
448#endif
449#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
450 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
451# define ex_language ex_ni
452#endif
453#ifndef FEAT_SIGNS
454# define ex_sign ex_ni
455#endif
456#ifndef FEAT_SUN_WORKSHOP
457# define ex_wsverb ex_ni
458#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000459#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200460# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000461# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200462# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464
465#ifndef FEAT_EVAL
466# define ex_debug ex_ni
467# define ex_breakadd ex_ni
468# define ex_debuggreedy ex_ni
469# define ex_breakdel ex_ni
470# define ex_breaklist ex_ni
471#endif
472
473#ifndef FEAT_CMDHIST
474# define ex_history ex_ni
475#endif
476#ifndef FEAT_JUMPLIST
477# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200478# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479# define ex_changes ex_ni
480#endif
481
Bram Moolenaar05159a02005-02-26 23:04:13 +0000482#ifndef FEAT_PROFILE
483# define ex_profile ex_ni
484#endif
485
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486/*
487 * Declare cmdnames[].
488 */
489#define DO_DECLARE_EXCMD
490#include "ex_cmds.h"
491
492/*
493 * Table used to quickly search for a command, based on its first character.
494 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000495static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496{
497 CMD_append,
498 CMD_buffer,
499 CMD_change,
500 CMD_delete,
501 CMD_edit,
502 CMD_file,
503 CMD_global,
504 CMD_help,
505 CMD_insert,
506 CMD_join,
507 CMD_k,
508 CMD_list,
509 CMD_move,
510 CMD_next,
511 CMD_open,
512 CMD_print,
513 CMD_quit,
514 CMD_read,
515 CMD_substitute,
516 CMD_t,
517 CMD_undo,
518 CMD_vglobal,
519 CMD_write,
520 CMD_xit,
521 CMD_yank,
522 CMD_z,
523 CMD_bang
524};
525
526static char_u dollar_command[2] = {'$', 0};
527
528
529#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000530/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531typedef struct
532{
533 char_u *line; /* command line */
534 linenr_T lnum; /* sourcing_lnum of the line */
535} wcmd_T;
536
537/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000538 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000540 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000542struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543{
544 garray_T *lines_gap; /* growarray with line info */
545 int current_line; /* last read line from growarray */
546 int repeating; /* TRUE when looping a second time */
547 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100548 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 void *cookie;
550};
551
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100552static char_u *get_loop_line(int c, void *cookie, int indent);
553static int store_loop_line(garray_T *gap, char_u *line);
554static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000555
556/* Struct to save a few things while debugging. Used in do_cmdline() only. */
557struct dbg_stuff
558{
559 int trylevel;
560 int force_abort;
561 except_T *caught_stack;
562 char_u *vv_exception;
563 char_u *vv_throwpoint;
564 int did_emsg;
565 int got_int;
566 int did_throw;
567 int need_rethrow;
568 int check_cstack;
569 except_T *current_exception;
570};
571
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100572static void save_dbg_stuff(struct dbg_stuff *dsp);
573static void restore_dbg_stuff(struct dbg_stuff *dsp);
Bram Moolenaared203462004-06-16 11:19:22 +0000574
575 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100576save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000577{
578 dsp->trylevel = trylevel; trylevel = 0;
579 dsp->force_abort = force_abort; force_abort = FALSE;
580 dsp->caught_stack = caught_stack; caught_stack = NULL;
581 dsp->vv_exception = v_exception(NULL);
582 dsp->vv_throwpoint = v_throwpoint(NULL);
583
584 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
585 dsp->did_emsg = did_emsg; did_emsg = FALSE;
586 dsp->got_int = got_int; got_int = FALSE;
587 dsp->did_throw = did_throw; did_throw = FALSE;
588 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
589 dsp->check_cstack = check_cstack; check_cstack = FALSE;
590 dsp->current_exception = current_exception; current_exception = NULL;
591}
592
593 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100594restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000595{
596 suppress_errthrow = FALSE;
597 trylevel = dsp->trylevel;
598 force_abort = dsp->force_abort;
599 caught_stack = dsp->caught_stack;
600 (void)v_exception(dsp->vv_exception);
601 (void)v_throwpoint(dsp->vv_throwpoint);
602 did_emsg = dsp->did_emsg;
603 got_int = dsp->got_int;
604 did_throw = dsp->did_throw;
605 need_rethrow = dsp->need_rethrow;
606 check_cstack = dsp->check_cstack;
607 current_exception = dsp->current_exception;
608}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609#endif
610
611
612/*
613 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
614 * command is given.
615 */
616 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100617do_exmode(
618 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619{
620 int save_msg_scroll;
621 int prev_msg_row;
622 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000623 int changedtick;
624
625 if (improved)
626 exmode_active = EXMODE_VIM;
627 else
628 exmode_active = EXMODE_NORMAL;
629 State = NORMAL;
630
631 /* When using ":global /pat/ visual" and then "Q" we return to continue
632 * the :global command. */
633 if (global_busy)
634 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635
636 save_msg_scroll = msg_scroll;
637 ++RedrawingDisabled; /* don't redisplay the window */
638 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639#ifdef FEAT_GUI
640 /* Ignore scrollbar and mouse events in Ex mode */
641 ++hold_gui_events;
642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643
644 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
645 while (exmode_active)
646 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000647 /* Check for a ":normal" command and no more characters left. */
648 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
649 {
650 exmode_active = FALSE;
651 break;
652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 msg_scroll = TRUE;
654 need_wait_return = FALSE;
655 ex_pressedreturn = FALSE;
656 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000657 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 prev_msg_row = msg_row;
659 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 if (improved)
661 {
662 cmdline_row = msg_row;
663 do_cmdline(NULL, getexline, NULL, 0);
664 }
665 else
666 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
667 lines_left = Rows - 1;
668
Bram Moolenaardf177f62005-02-22 08:39:57 +0000669 if ((prev_line != curwin->w_cursor.lnum
670 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000672 if (curbuf->b_ml.ml_flags & ML_EMPTY)
673 EMSG(_(e_emptybuf));
674 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000676 if (ex_pressedreturn)
677 {
678 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000679 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000680 msg_row = prev_msg_row;
681 if (prev_msg_row == Rows - 1)
682 msg_row--;
683 }
684 msg_col = 0;
685 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
686 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000689 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
690 {
691 if (curbuf->b_ml.ml_flags & ML_EMPTY)
692 EMSG(_(e_emptybuf));
693 else
694 EMSG(_("E501: At end-of-file"));
695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 }
697
698#ifdef FEAT_GUI
699 --hold_gui_events;
700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 --RedrawingDisabled;
702 --no_wait_return;
703 update_screen(CLEAR);
704 need_wait_return = FALSE;
705 msg_scroll = save_msg_scroll;
706}
707
708/*
709 * Execute a simple command line. Used for translated commands like "*".
710 */
711 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100712do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713{
714 return do_cmdline(cmd, NULL, NULL,
715 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
716}
717
718/*
719 * do_cmdline(): execute one Ex command line
720 *
721 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100722 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 * 2. Split up in parts separated with '|'.
724 *
725 * This function can be called recursively!
726 *
727 * flags:
728 * DOCMD_VERBOSE - The command will be included in the error message.
729 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100730 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 * DOCMD_KEYTYPED - Don't reset KeyTyped.
732 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
733 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
734 *
735 * return FAIL if cmdline could not be executed, OK otherwise
736 */
737 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100738do_cmdline(
739 char_u *cmdline,
740 char_u *(*fgetline)(int, void *, int),
741 void *cookie, /* argument for fgetline() */
742 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743{
744 char_u *next_cmdline; /* next cmd to execute */
745 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100746 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 static int recursive = 0; /* recursive depth */
748 int msg_didout_before_start = 0;
749 int count = 0; /* line number count */
750 int did_inc = FALSE; /* incremented RedrawingDisabled */
751 int retval = OK;
752#ifdef FEAT_EVAL
753 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000754 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 int current_line = 0; /* active line in lines_ga */
756 char_u *fname = NULL; /* function or script name */
757 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
758 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000759 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 int initial_trylevel;
761 struct msglist **saved_msg_list = NULL;
762 struct msglist *private_msg_list;
763
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100764 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100765 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000767 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000769 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100771# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772# define cmd_cookie cookie
773#endif
774 static int call_depth = 0; /* recursiveness */
775
776#ifdef FEAT_EVAL
777 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
778 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000779 * This ensures that the do_errthrow() call in do_one_cmd() does not
780 * combine the messages stored by an earlier invocation of do_one_cmd()
781 * with the command name of the later one. This would happen when
782 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 saved_msg_list = msg_list;
784 msg_list = &private_msg_list;
785 private_msg_list = NULL;
786#endif
787
788 /* It's possible to create an endless loop with ":execute", catch that
789 * here. The value of 200 allows nested function calls, ":source", etc. */
790 if (call_depth == 200)
791 {
792 EMSG(_("E169: Command too recursive"));
793#ifdef FEAT_EVAL
794 /* When converting to an exception, we do not include the command name
795 * since this is not an error of the specific command. */
796 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
797 msg_list = saved_msg_list;
798#endif
799 return FAIL;
800 }
801 ++call_depth;
802
803#ifdef FEAT_EVAL
804 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000805 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 cstack.cs_trylevel = 0;
807 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000808 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
810
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100811 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812
813 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100814 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000815 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 ++ex_nesting_level;
817
818 /* Get the function or script name and the address where the next breakpoint
819 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000820 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 {
822 fname = func_name(real_cookie);
823 breakpoint = func_breakpoint(real_cookie);
824 dbg_tick = func_dbg_tick(real_cookie);
825 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100826 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 {
828 fname = sourcing_name;
829 breakpoint = source_breakpoint(real_cookie);
830 dbg_tick = source_dbg_tick(real_cookie);
831 }
832
833 /*
834 * Initialize "force_abort" and "suppress_errthrow" at the top level.
835 */
836 if (!recursive)
837 {
838 force_abort = FALSE;
839 suppress_errthrow = FALSE;
840 }
841
842 /*
843 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000844 * exception handling (used when debugging). Otherwise clear it to avoid
845 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000847 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000848 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000849 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200850 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851
852 initial_trylevel = trylevel;
853
854 /*
855 * "did_throw" will be set to TRUE when an exception is being thrown.
856 */
857 did_throw = FALSE;
858#endif
859 /*
860 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000861 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 * If force_abort is set, we cancel everything.
863 */
864 did_emsg = FALSE;
865
866 /*
867 * KeyTyped is only set when calling vgetc(). Reset it here when not
868 * calling vgetc() (sourced command lines).
869 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100870 if (!(flags & DOCMD_KEYTYPED)
871 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 KeyTyped = FALSE;
873
874 /*
875 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000876 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 * - for multiple commands on one line, separated with '|'
878 * - when repeating until there are no more lines (for ":source")
879 */
880 next_cmdline = cmdline;
881 do
882 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000883#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100884 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000885#endif
886
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000887 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 if (next_cmdline == NULL
889#ifdef FEAT_EVAL
890 && !force_abort
891 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000892 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893#endif
894 )
895 did_emsg = FALSE;
896
897 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000898 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100899 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 * 3. If a line is given: Make a copy, so we can mess with it.
901 */
902
903#ifdef FEAT_EVAL
904 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000905 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 {
907 /* Each '|' separated command is stored separately in lines_ga, to
908 * be able to jump to it. Don't use next_cmdline now. */
909 vim_free(cmdline_copy);
910 cmdline_copy = NULL;
911
912 /* Check if a function has returned or, unless it has an unclosed
913 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000914 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000916# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000917 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000918 func_line_end(real_cookie);
919# endif
920 if (func_has_ended(real_cookie))
921 {
922 retval = FAIL;
923 break;
924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000926#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000927 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100928 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000929 script_line_end();
930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931
932 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100933 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 {
935 retval = FAIL;
936 break;
937 }
938
939 /* If breakpoints have been added/deleted need to check for it. */
940 if (breakpoint != NULL && dbg_tick != NULL
941 && *dbg_tick != debug_tick)
942 {
943 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100944 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 fname, sourcing_lnum);
946 *dbg_tick = debug_tick;
947 }
948
949 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
950 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
951
952 /* Did we encounter a breakpoint? */
953 if (breakpoint != NULL && *breakpoint != 0
954 && *breakpoint <= sourcing_lnum)
955 {
956 dbg_breakpoint(fname, sourcing_lnum);
957 /* Find next breakpoint. */
958 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100959 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 fname, sourcing_lnum);
961 *dbg_tick = debug_tick;
962 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000963# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000964 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000965 {
966 if (getline_is_func)
967 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100968 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000969 script_line_start();
970 }
971# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 }
973
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000974 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000976 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100977 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 * below, so that it stores lines in or reads them from
979 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000980 * while/for loop. */
981 cmd_getline = get_loop_line;
982 cmd_cookie = (void *)&cmd_loop_cookie;
983 cmd_loop_cookie.lines_gap = &lines_ga;
984 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100985 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000986 cmd_loop_cookie.cookie = cookie;
987 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 }
989 else
990 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100991 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 cmd_cookie = cookie;
993 }
994#endif
995
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100996 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 if (next_cmdline == NULL)
998 {
999 /*
1000 * Need to set msg_didout for the first line after an ":if",
1001 * otherwise the ":if" will be overwritten.
1002 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001003 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001005 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006#ifdef FEAT_EVAL
1007 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1008#else
1009 0
1010#endif
1011 )) == NULL)
1012 {
1013 /* Don't call wait_return for aborted command line. The NULL
1014 * returned for the end of a sourced file or executed function
1015 * doesn't do this. */
1016 if (KeyTyped && !(flags & DOCMD_REPEAT))
1017 need_wait_return = FALSE;
1018 retval = FAIL;
1019 break;
1020 }
1021 used_getline = TRUE;
1022
1023 /*
1024 * Keep the first typed line. Clear it when more lines are typed.
1025 */
1026 if (flags & DOCMD_KEEPLINE)
1027 {
1028 vim_free(repeat_cmdline);
1029 if (count == 0)
1030 repeat_cmdline = vim_strsave(next_cmdline);
1031 else
1032 repeat_cmdline = NULL;
1033 }
1034 }
1035
1036 /* 3. Make a copy of the command so we can mess with it. */
1037 else if (cmdline_copy == NULL)
1038 {
1039 next_cmdline = vim_strsave(next_cmdline);
1040 if (next_cmdline == NULL)
1041 {
1042 EMSG(_(e_outofmem));
1043 retval = FAIL;
1044 break;
1045 }
1046 }
1047 cmdline_copy = next_cmdline;
1048
1049#ifdef FEAT_EVAL
1050 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001051 * Save the current line when inside a ":while" or ":for", and when
1052 * the command looks like a ":while" or ":for", because we may need it
1053 * later. When there is a '|' and another command, it is stored
1054 * separately, because we need to be able to jump back to it from an
1055 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001057 if (current_line == lines_ga.ga_len
1058 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001060 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 {
1062 retval = FAIL;
1063 break;
1064 }
1065 }
1066 did_endif = FALSE;
1067#endif
1068
1069 if (count++ == 0)
1070 {
1071 /*
1072 * All output from the commands is put below each other, without
1073 * waiting for a return. Don't do this when executing commands
1074 * from a script or when being called recursive (e.g. for ":e
1075 * +command file").
1076 */
1077 if (!(flags & DOCMD_NOWAIT) && !recursive)
1078 {
1079 msg_didout_before_start = msg_didout;
1080 msg_didany = FALSE; /* no output yet */
1081 msg_start();
1082 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001083 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 ++RedrawingDisabled;
1085 did_inc = TRUE;
1086 }
1087 }
1088
1089 if (p_verbose >= 15 && sourcing_name != NULL)
1090 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001092 verbose_enter_scroll();
1093
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 smsg((char_u *)_("line %ld: %s"),
1095 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001096 if (msg_silent == 0)
1097 msg_puts((char_u *)"\n"); /* don't overwrite this */
1098
1099 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 --no_wait_return;
1101 }
1102
1103 /*
1104 * 2. Execute one '|' separated command.
1105 * do_one_cmd() will return NULL if there is no trailing '|'.
1106 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1107 */
1108 ++recursive;
1109 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1110#ifdef FEAT_EVAL
1111 &cstack,
1112#endif
1113 cmd_getline, cmd_cookie);
1114 --recursive;
1115
1116#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001117 if (cmd_cookie == (void *)&cmd_loop_cookie)
1118 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001120 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121#endif
1122
1123 if (next_cmdline == NULL)
1124 {
1125 vim_free(cmdline_copy);
1126 cmdline_copy = NULL;
1127#ifdef FEAT_CMDHIST
1128 /*
1129 * If the command was typed, remember it for the ':' register.
1130 * Do this AFTER executing the command to make :@: work.
1131 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001132 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 && new_last_cmdline != NULL)
1134 {
1135 vim_free(last_cmdline);
1136 last_cmdline = new_last_cmdline;
1137 new_last_cmdline = NULL;
1138 }
1139#endif
1140 }
1141 else
1142 {
1143 /* need to copy the command after the '|' to cmdline_copy, for the
1144 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001145 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 next_cmdline = cmdline_copy;
1147 }
1148
1149
1150#ifdef FEAT_EVAL
1151 /* reset did_emsg for a function that is not aborted by an error */
1152 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001153 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 && !func_has_abort(real_cookie))
1155 did_emsg = FALSE;
1156
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001157 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 {
1159 ++current_line;
1160
1161 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001162 * An ":endwhile", ":endfor" and ":continue" is handled here.
1163 * If we were executing commands, jump back to the ":while" or
1164 * ":for".
1165 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001167 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001169 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001171 /* Jump back to the matching ":while" or ":for". Be careful
1172 * not to use a cs_line[] from an entry that isn't a ":while"
1173 * or ":for": It would make "current_line" invalid and can
1174 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 if (!did_emsg && !got_int && !did_throw
1176 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001177 && (cstack.cs_flags[cstack.cs_idx]
1178 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 && cstack.cs_line[cstack.cs_idx] >= 0
1180 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1181 {
1182 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001183 /* remember we jumped there */
1184 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 line_breakcheck(); /* check if CTRL-C typed */
1186
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001187 /* Check for the next breakpoint at or after the ":while"
1188 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 if (breakpoint != NULL)
1190 {
1191 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001192 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 fname,
1194 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1195 *dbg_tick = debug_tick;
1196 }
1197 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001198 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001202 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1203 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 }
1205 }
1206
1207 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001208 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001210 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001212 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1214 }
1215 }
1216
1217 /*
1218 * When not inside any ":while" loop, clear remembered lines.
1219 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001220 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 {
1222 if (lines_ga.ga_len > 0)
1223 {
1224 sourcing_lnum =
1225 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1226 free_cmdlines(&lines_ga);
1227 }
1228 current_line = 0;
1229 }
1230
1231 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001232 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1233 * being restored at the ":endtry". Reset them here and set the
1234 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1235 * This includes the case where a missing ":endif", ":endwhile" or
1236 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001238 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001240 cstack.cs_lflags &= ~CSL_HAD_FINA;
1241 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1242 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 did_throw ? (void *)current_exception : NULL);
1244 did_emsg = got_int = did_throw = FALSE;
1245 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1246 }
1247
1248 /* Update global "trylevel" for recursive calls to do_cmdline() from
1249 * within this loop. */
1250 trylevel = initial_trylevel + cstack.cs_trylevel;
1251
1252 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001253 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 * files) is aborted because of an error, an interrupt, or an uncaught
1255 * exception, cancel everything. If it is left normally, reset
1256 * force_abort to get the non-EH compatible abortion behavior for
1257 * the rest of the script.
1258 */
1259 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1260 force_abort = FALSE;
1261
1262 /* Convert an interrupt to an exception if appropriate. */
1263 (void)do_intthrow(&cstack);
1264#endif /* FEAT_EVAL */
1265
1266 }
1267 /*
1268 * Continue executing command lines when:
1269 * - no CTRL-C typed, no aborting error, no exception thrown or try
1270 * conditionals need to be checked for executing finally clauses or
1271 * catching an interrupt exception
1272 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001273 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 * looping for ":source" command or function call.
1275 */
1276 while (!((got_int
1277#ifdef FEAT_EVAL
1278 || (did_emsg && force_abort) || did_throw
1279#endif
1280 )
1281#ifdef FEAT_EVAL
1282 && cstack.cs_trylevel == 0
1283#endif
1284 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001285 && !(did_emsg
1286#ifdef FEAT_EVAL
1287 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001288 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001289 * the :endtry to be missed. */
1290 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1291#endif
1292 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001293 && (getline_equal(fgetline, cookie, getexmodeline)
1294 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 && (next_cmdline != NULL
1296#ifdef FEAT_EVAL
1297 || cstack.cs_idx >= 0
1298#endif
1299 || (flags & DOCMD_REPEAT)));
1300
1301 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001302 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303#ifdef FEAT_EVAL
1304 free_cmdlines(&lines_ga);
1305 ga_clear(&lines_ga);
1306
1307 if (cstack.cs_idx >= 0)
1308 {
1309 /*
1310 * If a sourced file or executed function ran to its end, report the
1311 * unclosed conditional.
1312 */
1313 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001314 && ((getline_equal(fgetline, cookie, getsourceline)
1315 && !source_finished(fgetline, cookie))
1316 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 && !func_has_ended(real_cookie))))
1318 {
1319 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1320 EMSG(_(e_endtry));
1321 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1322 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001323 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1324 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 else
1326 EMSG(_(e_endif));
1327 }
1328
1329 /*
1330 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1331 * ":endtry" in a sourced file or executed function. If the try
1332 * conditional is in its finally clause, ignore anything pending.
1333 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001334 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 */
1336 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001337 {
1338 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1339
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001340 if (idx >= 0)
1341 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001342 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1343 &cstack.cs_looplevel);
1344 }
1345 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 trylevel = initial_trylevel;
1347 }
1348
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001349 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1350 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001352 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 ? (char_u *)"endfunction" : (char_u *)NULL);
1354
1355 if (trylevel == 0)
1356 {
1357 /*
1358 * When an exception is being thrown out of the outermost try
1359 * conditional, discard the uncaught exception, disable the conversion
1360 * of interrupts or errors to exceptions, and ensure that no more
1361 * commands are executed.
1362 */
1363 if (did_throw)
1364 {
1365 void *p = NULL;
1366 char_u *saved_sourcing_name;
1367 int saved_sourcing_lnum;
1368 struct msglist *messages = NULL, *next;
1369
1370 /*
1371 * If the uncaught exception is a user exception, report it as an
1372 * error. If it is an error exception, display the saved error
1373 * message now. For an interrupt exception, do nothing; the
1374 * interrupt message is given elsewhere.
1375 */
1376 switch (current_exception->type)
1377 {
1378 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001379 vim_snprintf((char *)IObuff, IOSIZE,
1380 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 current_exception->value);
1382 p = vim_strsave(IObuff);
1383 break;
1384 case ET_ERROR:
1385 messages = current_exception->messages;
1386 current_exception->messages = NULL;
1387 break;
1388 case ET_INTERRUPT:
1389 break;
1390 default:
1391 p = vim_strsave((char_u *)_(e_internal));
1392 }
1393
1394 saved_sourcing_name = sourcing_name;
1395 saved_sourcing_lnum = sourcing_lnum;
1396 sourcing_name = current_exception->throw_name;
1397 sourcing_lnum = current_exception->throw_lnum;
1398 current_exception->throw_name = NULL;
1399
1400 discard_current_exception(); /* uses IObuff if 'verbose' */
1401 suppress_errthrow = TRUE;
1402 force_abort = TRUE;
1403
1404 if (messages != NULL)
1405 {
1406 do
1407 {
1408 next = messages->next;
1409 emsg(messages->msg);
1410 vim_free(messages->msg);
1411 vim_free(messages);
1412 messages = next;
1413 }
1414 while (messages != NULL);
1415 }
1416 else if (p != NULL)
1417 {
1418 emsg(p);
1419 vim_free(p);
1420 }
1421 vim_free(sourcing_name);
1422 sourcing_name = saved_sourcing_name;
1423 sourcing_lnum = saved_sourcing_lnum;
1424 }
1425
1426 /*
1427 * On an interrupt or an aborting error not converted to an exception,
1428 * disable the conversion of errors to exceptions. (Interrupts are not
1429 * converted any more, here.) This enables also the interrupt message
1430 * when force_abort is set and did_emsg unset in case of an interrupt
1431 * from a finally clause after an error.
1432 */
1433 else if (got_int || (did_emsg && force_abort))
1434 suppress_errthrow = TRUE;
1435 }
1436
1437 /*
1438 * The current cstack will be freed when do_cmdline() returns. An uncaught
1439 * exception will have to be rethrown in the previous cstack. If a function
1440 * has just returned or a script file was just finished and the previous
1441 * cstack belongs to the same function or, respectively, script file, it
1442 * will have to be checked for finally clauses to be executed due to the
1443 * ":return" or ":finish". This is done in do_one_cmd().
1444 */
1445 if (did_throw)
1446 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001447 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001449 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 && ex_nesting_level > func_level(real_cookie) + 1))
1451 {
1452 if (!did_throw)
1453 check_cstack = TRUE;
1454 }
1455 else
1456 {
1457 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001458 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 --ex_nesting_level;
1460 /*
1461 * Go to debug mode when returning from a function in which we are
1462 * single-stepping.
1463 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001464 if ((getline_equal(fgetline, cookie, getsourceline)
1465 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001467 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 ? (char_u *)_("End of sourced file")
1469 : (char_u *)_("End of function"));
1470 }
1471
1472 /*
1473 * Restore the exception environment (done after returning from the
1474 * debugger).
1475 */
1476 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001477 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478
1479 msg_list = saved_msg_list;
1480#endif /* FEAT_EVAL */
1481
1482 /*
1483 * If there was too much output to fit on the command line, ask the user to
1484 * hit return before redrawing the screen. With the ":global" command we do
1485 * this only once after the command is finished.
1486 */
1487 if (did_inc)
1488 {
1489 --RedrawingDisabled;
1490 --no_wait_return;
1491 msg_scroll = FALSE;
1492
1493 /*
1494 * When just finished an ":if"-":else" which was typed, no need to
1495 * wait for hit-return. Also for an error situation.
1496 */
1497 if (retval == FAIL
1498#ifdef FEAT_EVAL
1499 || (did_endif && KeyTyped && !did_emsg)
1500#endif
1501 )
1502 {
1503 need_wait_return = FALSE;
1504 msg_didany = FALSE; /* don't wait when restarting edit */
1505 }
1506 else if (need_wait_return)
1507 {
1508 /*
1509 * The msg_start() above clears msg_didout. The wait_return we do
1510 * here should not overwrite the command that may be shown before
1511 * doing that.
1512 */
1513 msg_didout |= msg_didout_before_start;
1514 wait_return(FALSE);
1515 }
1516 }
1517
Bram Moolenaar2a942252012-11-28 23:03:07 +01001518#ifdef FEAT_EVAL
1519 did_endif = FALSE; /* in case do_cmdline used recursively */
1520#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 /*
1522 * Reset if_level, in case a sourced script file contains more ":if" than
1523 * ":endif" (could be ":if x | foo | endif").
1524 */
1525 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001526#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001527
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 --call_depth;
1529 return retval;
1530}
1531
1532#ifdef FEAT_EVAL
1533/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001534 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 */
1536 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001537get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001539 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 wcmd_T *wp;
1541 char_u *line;
1542
1543 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1544 {
1545 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001546 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001548 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 if (cp->getline == NULL)
1550 line = getcmdline(c, 0L, indent);
1551 else
1552 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001553 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 ++cp->current_line;
1555
1556 return line;
1557 }
1558
1559 KeyTyped = FALSE;
1560 ++cp->current_line;
1561 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1562 sourcing_lnum = wp->lnum;
1563 return vim_strsave(wp->line);
1564}
1565
1566/*
1567 * Store a line in "gap" so that a ":while" loop can execute it again.
1568 */
1569 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001570store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571{
1572 if (ga_grow(gap, 1) == FAIL)
1573 return FAIL;
1574 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1575 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1576 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 return OK;
1578}
1579
1580/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001581 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 */
1583 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001584free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585{
1586 while (gap->ga_len > 0)
1587 {
1588 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1589 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 }
1591}
1592#endif
1593
1594/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001595 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1596 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001599getline_equal(
1600 char_u *(*fgetline)(int, void *, int),
1601 void *cookie UNUSED, /* argument for fgetline() */
1602 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603{
1604#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001605 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001606 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607
Bram Moolenaar89d40322006-08-29 15:30:07 +00001608 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001609 * function that's originally used to obtain the lines. This may be
1610 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001611 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001612 cp = (struct loop_cookie *)cookie;
1613 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 {
1615 gp = cp->getline;
1616 cp = cp->cookie;
1617 }
1618 return gp == func;
1619#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001620 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621#endif
1622}
1623
1624#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1625/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001626 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 * getline function. Otherwise return "cookie".
1628 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001630getline_cookie(
1631 char_u *(*fgetline)(int, void *, int) UNUSED,
1632 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633{
1634# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001635 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001636 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
Bram Moolenaar89d40322006-08-29 15:30:07 +00001638 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001639 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001641 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001642 cp = (struct loop_cookie *)cookie;
1643 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 {
1645 gp = cp->getline;
1646 cp = cp->cookie;
1647 }
1648 return cp;
1649# else
1650 return cookie;
1651# endif
1652}
1653#endif
1654
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001655
1656/*
1657 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1658 * ":bwipeout", etc.
1659 * Returns the buffer number.
1660 */
1661 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001662compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001663{
1664 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001665 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001666 int count = offset;
1667
1668 buf = firstbuf;
1669 while (buf->b_next != NULL && buf->b_fnum < lnum)
1670 buf = buf->b_next;
1671 while (count != 0)
1672 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001673 count += (offset < 0) ? 1 : -1;
1674 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1675 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001676 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001677 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001678 if (addr_type == ADDR_LOADED_BUFFERS)
1679 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001680 while (buf->b_ml.ml_mfp == NULL)
1681 {
1682 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1683 if (nextbuf == NULL)
1684 break;
1685 buf = nextbuf;
1686 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001687 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001688 /* we might have gone too far, last buffer is not loadedd */
1689 if (addr_type == ADDR_LOADED_BUFFERS)
1690 while (buf->b_ml.ml_mfp == NULL)
1691 {
1692 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1693 if (nextbuf == NULL)
1694 break;
1695 buf = nextbuf;
1696 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001697 return buf->b_fnum;
1698}
1699
Bram Moolenaarf240e182014-11-27 18:33:02 +01001700#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001701static int current_win_nr(win_T *win);
1702static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001703
1704 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001705current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001706{
1707 win_T *wp;
1708 int nr = 0;
1709
1710 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1711 {
1712 ++nr;
1713 if (wp == win)
1714 break;
1715 }
1716 return nr;
1717}
1718
1719 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001720current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001721{
1722 tabpage_T *tp;
1723 int nr = 0;
1724
1725 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
1726 {
1727 ++nr;
1728 if (tp == tab)
1729 break;
1730 }
1731 return nr;
1732}
1733
1734# define CURRENT_WIN_NR current_win_nr(curwin)
1735# define LAST_WIN_NR current_win_nr(NULL)
1736# define CURRENT_TAB_NR current_tab_nr(curtab)
1737# define LAST_TAB_NR current_tab_nr(NULL)
1738#else
1739# define CURRENT_WIN_NR 1
1740# define LAST_WIN_NR 1
1741# define CURRENT_TAB_NR 1
1742# define LAST_TAB_NR 1
1743#endif
1744
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001745
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746/*
1747 * Execute one Ex command.
1748 *
1749 * If 'sourcing' is TRUE, the command will be included in the error message.
1750 *
1751 * 1. skip comment lines and leading space
1752 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001753 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001754 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001755 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001756 * 6. parse arguments
1757 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001759 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 *
1761 * This function may be called recursively!
1762 */
1763#if (_MSC_VER == 1200)
1764/*
Bram Moolenaared203462004-06-16 11:19:22 +00001765 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001767 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768#endif
1769 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001770do_one_cmd(
1771 char_u **cmdlinep,
1772 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001774 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001776 char_u *(*fgetline)(int, void *, int),
1777 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778{
1779 char_u *p;
1780 linenr_T lnum;
1781 long n;
1782 char_u *errormsg = NULL; /* error message */
1783 exarg_T ea; /* Ex command arguments */
1784 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001785 int save_msg_scroll = msg_scroll;
1786 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001788#ifdef HAVE_SANDBOX
1789 int did_sandbox = FALSE;
1790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 cmdmod_T save_cmdmod;
1792 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001793 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794
1795 vim_memset(&ea, 0, sizeof(ea));
1796 ea.line1 = 1;
1797 ea.line2 = 1;
1798#ifdef FEAT_EVAL
1799 ++ex_nesting_level;
1800#endif
1801
Bram Moolenaar21691f82012-12-05 19:13:18 +01001802 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 if (quitmore
1804#ifdef FEAT_EVAL
1805 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001806 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001807#endif
1808#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001809 /* avoid that an autocommand, e.g. QuitPre, does this */
1810 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811#endif
1812 )
1813 --quitmore;
1814
1815 /*
1816 * Reset browse, confirm, etc.. They are restored when returning, for
1817 * recursive calls.
1818 */
1819 save_cmdmod = cmdmod;
1820 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1821
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001822 /* "#!anything" is handled like a comment. */
1823 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1824 goto doend;
1825
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 /*
1827 * Repeat until no more command modifiers are found.
1828 */
1829 ea.cmd = *cmdlinep;
1830 for (;;)
1831 {
1832/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001833 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 */
1835 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1836 ++ea.cmd;
1837
1838 /* in ex mode, an empty line works like :+ */
1839 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001840 && (getline_equal(fgetline, cookie, getexmodeline)
1841 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001842 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 {
1844 ea.cmd = (char_u *)"+";
1845 ex_pressedreturn = TRUE;
1846 }
1847
1848 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001849 if (*ea.cmd == '"')
1850 goto doend;
1851 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001852 {
1853 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856
1857/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001858 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 */
1860 p = ea.cmd;
1861 if (VIM_ISDIGIT(*ea.cmd))
1862 p = skipwhite(skipdigits(ea.cmd));
1863 switch (*p)
1864 {
1865 /* When adding an entry, also modify cmd_exists(). */
1866 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1867 break;
1868#ifdef FEAT_WINDOWS
1869 cmdmod.split |= WSP_ABOVE;
1870#endif
1871 continue;
1872
1873 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1874 {
1875#ifdef FEAT_WINDOWS
1876 cmdmod.split |= WSP_BELOW;
1877#endif
1878 continue;
1879 }
1880 if (checkforcmd(&ea.cmd, "browse", 3))
1881 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001882#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 cmdmod.browse = TRUE;
1884#endif
1885 continue;
1886 }
1887 if (!checkforcmd(&ea.cmd, "botright", 2))
1888 break;
1889#ifdef FEAT_WINDOWS
1890 cmdmod.split |= WSP_BOT;
1891#endif
1892 continue;
1893
1894 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1895 break;
1896#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1897 cmdmod.confirm = TRUE;
1898#endif
1899 continue;
1900
1901 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1902 {
1903 cmdmod.keepmarks = TRUE;
1904 continue;
1905 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001906 if (checkforcmd(&ea.cmd, "keepalt", 5))
1907 {
1908 cmdmod.keepalt = TRUE;
1909 continue;
1910 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001911 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1912 {
1913 cmdmod.keeppatterns = TRUE;
1914 continue;
1915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1917 break;
1918 cmdmod.keepjumps = TRUE;
1919 continue;
1920
1921 /* ":hide" and ":hide | cmd" are not modifiers */
1922 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1923 || *p == NUL || ends_excmd(*p))
1924 break;
1925 ea.cmd = p;
1926 cmdmod.hide = TRUE;
1927 continue;
1928
1929 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1930 {
1931 cmdmod.lockmarks = TRUE;
1932 continue;
1933 }
1934
1935 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1936 break;
1937#ifdef FEAT_WINDOWS
1938 cmdmod.split |= WSP_ABOVE;
1939#endif
1940 continue;
1941
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001942 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001943 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001944#ifdef FEAT_AUTOCMD
1945 if (cmdmod.save_ei == NULL)
1946 {
1947 /* Set 'eventignore' to "all". Restore the
1948 * existing option value later. */
1949 cmdmod.save_ei = vim_strsave(p_ei);
1950 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001951 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001952 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001953#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001954 continue;
1955 }
1956 if (!checkforcmd(&ea.cmd, "noswapfile", 6))
1957 break;
1958 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001959 continue;
1960
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1962 break;
1963#ifdef FEAT_WINDOWS
1964 cmdmod.split |= WSP_BELOW;
1965#endif
1966 continue;
1967
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001968 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1969 {
1970#ifdef HAVE_SANDBOX
1971 if (!did_sandbox)
1972 ++sandbox;
1973 did_sandbox = TRUE;
1974#endif
1975 continue;
1976 }
1977 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001979 if (save_msg_silent == -1)
1980 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1983 {
1984 /* ":silent!", but not "silent !cmd" */
1985 ea.cmd = skipwhite(ea.cmd + 1);
1986 ++emsg_silent;
1987 ++did_esilent;
1988 }
1989 continue;
1990
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001991 case 't': if (checkforcmd(&p, "tab", 3))
1992 {
1993#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001994 if (vim_isdigit(*ea.cmd))
1995 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1996 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001997 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001998 ea.cmd = p;
1999#endif
2000 continue;
2001 }
2002 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 break;
2004#ifdef FEAT_WINDOWS
2005 cmdmod.split |= WSP_TOP;
2006#endif
2007 continue;
2008
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002009 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
2010 break;
2011 if (save_msg_silent == -1)
2012 save_msg_silent = msg_silent;
2013 msg_silent = 0;
2014 continue;
2015
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
2017 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002018#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 cmdmod.split |= WSP_VERT;
2020#endif
2021 continue;
2022 }
2023 if (!checkforcmd(&p, "verbose", 4))
2024 break;
2025 if (verbose_save < 0)
2026 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00002027 if (vim_isdigit(*ea.cmd))
2028 p_verbose = atoi((char *)ea.cmd);
2029 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 p_verbose = 1;
2031 ea.cmd = p;
2032 continue;
2033 }
2034 break;
2035 }
2036
2037#ifdef FEAT_EVAL
2038 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2039 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2040#else
2041 ea.skip = (if_level > 0);
2042#endif
2043
2044#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002045# ifdef FEAT_PROFILE
2046 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002047 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002048 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002049 if (getline_equal(fgetline, cookie, get_func_line))
2050 func_line_exec(getline_cookie(fgetline, cookie));
2051 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002052 script_line_exec();
2053 }
2054#endif
2055
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 /* May go to debug mode. If this happens and the ">quit" debug command is
2057 * used, throw an interrupt exception and skip the next command. */
2058 dbg_check_breakpoint(&ea);
2059 if (!ea.skip && got_int)
2060 {
2061 ea.skip = TRUE;
2062 (void)do_intthrow(cstack);
2063 }
2064#endif
2065
2066/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002067 * 3. Skip over the range to find the command. Let "p" point to after it.
2068 *
2069 * We need the command to know what kind of range it uses.
2070 */
2071 cmd = ea.cmd;
2072 ea.cmd = skip_range(ea.cmd, NULL);
2073 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2074 ea.cmd = skipwhite(ea.cmd + 1);
2075 p = find_command(&ea, NULL);
2076
2077/*
2078 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 *
2080 * where 'addr' is:
2081 *
2082 * % (entire file)
2083 * $ [+-NUM]
2084 * 'x [+-NUM] (where x denotes a currently defined mark)
2085 * . [+-NUM]
2086 * [+-NUM]..
2087 * NUM
2088 *
2089 * The ea.cmd pointer is updated to point to the first character following the
2090 * range spec. If an initial address is found, but no second, the upper bound
2091 * is equal to the lower.
2092 */
2093
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002094 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002095 if (!IS_USER_CMDIDX(ea.cmdidx))
2096 {
2097 if (ea.cmdidx != CMD_SIZE)
2098 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2099 else
2100 ea.addr_type = ADDR_LINES;
2101
2102#ifdef FEAT_WINDOWS
2103 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002104 if (ea.cmdidx == CMD_wincmd && p != NULL)
2105 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002106#endif
2107 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002108
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002110 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 for (;;)
2112 {
2113 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002114 switch (ea.addr_type)
2115 {
2116 case ADDR_LINES:
2117 /* default is current line number */
2118 ea.line2 = curwin->w_cursor.lnum;
2119 break;
2120 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002121 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002122 ea.line2 = lnum;
2123 break;
2124 case ADDR_ARGUMENTS:
2125 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002126 if (ea.line2 > ARGCOUNT)
2127 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002128 break;
2129 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002130 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002131 ea.line2 = curbuf->b_fnum;
2132 break;
2133 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002134 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002135 ea.line2 = lnum;
2136 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002137#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002138 case ADDR_QUICKFIX:
2139 ea.line2 = qf_get_cur_valid_idx(&ea);
2140 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002141#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002144 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip,
2145 ea.addr_count == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 if (ea.cmd == NULL) /* error detected */
2147 goto doend;
2148 if (lnum == MAXLNUM)
2149 {
2150 if (*ea.cmd == '%') /* '%' - all lines */
2151 {
2152 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002153 switch (ea.addr_type)
2154 {
2155 case ADDR_LINES:
2156 ea.line1 = 1;
2157 ea.line2 = curbuf->b_ml.ml_line_count;
2158 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002159 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002160 {
2161 buf_T *buf = firstbuf;
2162
2163 while (buf->b_next != NULL
2164 && buf->b_ml.ml_mfp == NULL)
2165 buf = buf->b_next;
2166 ea.line1 = buf->b_fnum;
2167 buf = lastbuf;
2168 while (buf->b_prev != NULL
2169 && buf->b_ml.ml_mfp == NULL)
2170 buf = buf->b_prev;
2171 ea.line2 = buf->b_fnum;
2172 break;
2173 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002174 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002175 ea.line1 = firstbuf->b_fnum;
2176 ea.line2 = lastbuf->b_fnum;
2177 break;
2178 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002179 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002180 if (IS_USER_CMDIDX(ea.cmdidx))
2181 {
2182 ea.line1 = 1;
2183 ea.line2 = ea.addr_type == ADDR_WINDOWS
2184 ? LAST_WIN_NR : LAST_TAB_NR;
2185 }
2186 else
2187 {
2188 /* there is no Vim command which uses '%' and
2189 * ADDR_WINDOWS or ADDR_TABS */
2190 errormsg = (char_u *)_(e_invrange);
2191 goto doend;
2192 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002193 break;
2194 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002195 if (ARGCOUNT == 0)
2196 ea.line1 = ea.line2 = 0;
2197 else
2198 {
2199 ea.line1 = 1;
2200 ea.line2 = ARGCOUNT;
2201 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002202 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002203#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002204 case ADDR_QUICKFIX:
2205 ea.line1 = 1;
2206 ea.line2 = qf_get_size(&ea);
2207 if (ea.line2 == 0)
2208 ea.line2 = 1;
2209 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002210#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 ++ea.addr_count;
2213 }
2214 /* '*' - visual area */
2215 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2216 {
2217 pos_T *fp;
2218
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002219 if (ea.addr_type != ADDR_LINES)
2220 {
2221 errormsg = (char_u *)_(e_invrange);
2222 goto doend;
2223 }
2224
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 ++ea.cmd;
2226 if (!ea.skip)
2227 {
2228 fp = getmark('<', FALSE);
2229 if (check_mark(fp) == FAIL)
2230 goto doend;
2231 ea.line1 = fp->lnum;
2232 fp = getmark('>', FALSE);
2233 if (check_mark(fp) == FAIL)
2234 goto doend;
2235 ea.line2 = fp->lnum;
2236 ++ea.addr_count;
2237 }
2238 }
2239 }
2240 else
2241 ea.line2 = lnum;
2242 ea.addr_count++;
2243
2244 if (*ea.cmd == ';')
2245 {
2246 if (!ea.skip)
2247 curwin->w_cursor.lnum = ea.line2;
2248 }
2249 else if (*ea.cmd != ',')
2250 break;
2251 ++ea.cmd;
2252 }
2253
2254 /* One address given: set start and end lines */
2255 if (ea.addr_count == 1)
2256 {
2257 ea.line1 = ea.line2;
2258 /* ... but only implicit: really no address given */
2259 if (lnum == MAXLNUM)
2260 ea.addr_count = 0;
2261 }
2262
2263 /* Don't leave the cursor on an illegal line (caused by ';') */
2264 check_cursor_lnum();
2265
2266/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002267 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 */
2269
2270 /*
2271 * Skip ':' and any white space
2272 */
2273 ea.cmd = skipwhite(ea.cmd);
2274 while (*ea.cmd == ':')
2275 ea.cmd = skipwhite(ea.cmd + 1);
2276
2277 /*
2278 * If we got a line, but no command, then go to the line.
2279 * If we find a '|' or '\n' we set ea.nextcmd.
2280 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002281 if (*ea.cmd == NUL || *ea.cmd == '"'
2282 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 {
2284 /*
2285 * strange vi behaviour:
2286 * ":3" jumps to line 3
2287 * ":3|..." prints line 3
2288 * ":|" prints current line
2289 */
2290 if (ea.skip) /* skip this if inside :if */
2291 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002292 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 {
2294 ea.cmdidx = CMD_print;
2295 ea.argt = RANGE+COUNT+TRLBAR;
2296 if ((errormsg = invalid_range(&ea)) == NULL)
2297 {
2298 correct_range(&ea);
2299 ex_print(&ea);
2300 }
2301 }
2302 else if (ea.addr_count != 0)
2303 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002304 if (ea.line2 > curbuf->b_ml.ml_line_count)
2305 {
2306 /* With '-' in 'cpoptions' a line number past the file is an
2307 * error, otherwise put it at the end of the file. */
2308 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2309 ea.line2 = -1;
2310 else
2311 ea.line2 = curbuf->b_ml.ml_line_count;
2312 }
2313
2314 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002315 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 else
2317 {
2318 if (ea.line2 == 0)
2319 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 else
2321 curwin->w_cursor.lnum = ea.line2;
2322 beginline(BL_SOL | BL_FIX);
2323 }
2324 }
2325 goto doend;
2326 }
2327
Bram Moolenaard5005162014-08-22 23:05:54 +02002328#ifdef FEAT_AUTOCMD
2329 /* If this looks like an undefined user command and there are CmdUndefined
2330 * autocommands defined, trigger the matching autocommands. */
2331 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2332 && ASCII_ISUPPER(*ea.cmd)
2333 && has_cmdundefined())
2334 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002335 int ret;
2336
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002337 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002338 while (ASCII_ISALNUM(*p))
2339 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002340 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002341 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2342 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002343 /* If the autocommands did something and didn't cause an error, try
2344 * finding the command again. */
2345 p = (ret && !aborting()) ? find_command(&ea, NULL) : NULL;
Bram Moolenaard5005162014-08-22 23:05:54 +02002346 }
2347#endif
2348
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349#ifdef FEAT_USR_CMDS
2350 if (p == NULL)
2351 {
2352 if (!ea.skip)
2353 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2354 goto doend;
2355 }
2356 /* Check for wrong commands. */
2357 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2358 {
2359 errormsg = uc_fun_cmd();
2360 goto doend;
2361 }
2362#endif
2363 if (ea.cmdidx == CMD_SIZE)
2364 {
2365 if (!ea.skip)
2366 {
2367 STRCPY(IObuff, _("E492: Not an editor command"));
2368 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002369 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002371 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 }
2373 goto doend;
2374 }
2375
Bram Moolenaar958636c2014-10-21 20:01:58 +02002376 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2377 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002378#ifdef HAVE_EX_SCRIPT_NI
2379 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2380#endif
2381 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382
2383#ifndef FEAT_EVAL
2384 /*
2385 * When the expression evaluation is disabled, recognize the ":if" and
2386 * ":endif" commands and ignore everything in between it.
2387 */
2388 if (ea.cmdidx == CMD_if)
2389 ++if_level;
2390 if (if_level)
2391 {
2392 if (ea.cmdidx == CMD_endif)
2393 --if_level;
2394 goto doend;
2395 }
2396
2397#endif
2398
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002399 /* forced commands */
2400 if (*p == '!' && ea.cmdidx != CMD_substitute
2401 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 {
2403 ++p;
2404 ea.forceit = TRUE;
2405 }
2406 else
2407 ea.forceit = FALSE;
2408
2409/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002410 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002412 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002413 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414
2415 if (!ea.skip)
2416 {
2417#ifdef HAVE_SANDBOX
2418 if (sandbox != 0 && !(ea.argt & SBOXOK))
2419 {
2420 /* Command not allowed in sandbox. */
2421 errormsg = (char_u *)_(e_sandbox);
2422 goto doend;
2423 }
2424#endif
2425 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2426 {
2427 /* Command not allowed in non-'modifiable' buffer */
2428 errormsg = (char_u *)_(e_modifiable);
2429 goto doend;
2430 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002431
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002432 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002433 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002435 /* Command not allowed when editing the command line. */
2436#ifdef FEAT_CMDWIN
2437 if (cmdwin_type != 0)
2438 errormsg = (char_u *)_(e_cmdwin);
2439 else
2440#endif
2441 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 goto doend;
2443 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002444#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002445 /* Disallow editing another buffer when "curbuf_lock" is set.
2446 * Do allow ":edit" (check for argument later).
2447 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002448 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002449 && ea.cmdidx != CMD_edit
2450 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002451 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002452 && curbuf_locked())
2453 goto doend;
2454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455
2456 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2457 {
2458 /* no range allowed */
2459 errormsg = (char_u *)_(e_norange);
2460 goto doend;
2461 }
2462 }
2463
2464 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2465 {
2466 errormsg = (char_u *)_(e_nobang);
2467 goto doend;
2468 }
2469
2470 /*
2471 * Don't complain about the range if it is not used
2472 * (could happen if line_count is accidentally set to 0).
2473 */
2474 if (!ea.skip && !ni)
2475 {
2476 /*
2477 * If the range is backwards, ask for confirmation and, if given, swap
2478 * ea.line1 & ea.line2 so it's forwards again.
2479 * When global command is busy, don't ask, will fail below.
2480 */
2481 if (!global_busy && ea.line1 > ea.line2)
2482 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002483 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002485 if (sourcing || exmode_active)
2486 {
2487 errormsg = (char_u *)_("E493: Backwards range given");
2488 goto doend;
2489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 if (ask_yesno((char_u *)
2491 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002492 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 }
2494 lnum = ea.line1;
2495 ea.line1 = ea.line2;
2496 ea.line2 = lnum;
2497 }
2498 if ((errormsg = invalid_range(&ea)) != NULL)
2499 goto doend;
2500 }
2501
2502 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2503 ea.line2 = 1;
2504
2505 correct_range(&ea);
2506
2507#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002508 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2509 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 {
2511 /* Put the first line at the start of a closed fold, put the last line
2512 * at the end of a closed fold. */
2513 (void)hasFolding(ea.line1, &ea.line1, NULL);
2514 (void)hasFolding(ea.line2, NULL, &ea.line2);
2515 }
2516#endif
2517
2518#ifdef FEAT_QUICKFIX
2519 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002520 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 * option here, so things like % get expanded.
2522 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002523 p = replace_makeprg(&ea, p, cmdlinep);
2524 if (p == NULL)
2525 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526#endif
2527
2528 /*
2529 * Skip to start of argument.
2530 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2531 */
2532 if (ea.cmdidx == CMD_bang)
2533 ea.arg = p;
2534 else
2535 ea.arg = skipwhite(p);
2536
2537 /*
2538 * Check for "++opt=val" argument.
2539 * Must be first, allow ":w ++enc=utf8 !cmd"
2540 */
2541 if (ea.argt & ARGOPT)
2542 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2543 if (getargopt(&ea) == FAIL && !ni)
2544 {
2545 errormsg = (char_u *)_(e_invarg);
2546 goto doend;
2547 }
2548
2549 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2550 {
2551 if (*ea.arg == '>') /* append */
2552 {
2553 if (*++ea.arg != '>') /* typed wrong */
2554 {
2555 errormsg = (char_u *)_("E494: Use w or w>>");
2556 goto doend;
2557 }
2558 ea.arg = skipwhite(ea.arg + 1);
2559 ea.append = TRUE;
2560 }
2561 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2562 {
2563 ++ea.arg;
2564 ea.usefilter = TRUE;
2565 }
2566 }
2567
2568 if (ea.cmdidx == CMD_read)
2569 {
2570 if (ea.forceit)
2571 {
2572 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2573 ea.forceit = FALSE;
2574 }
2575 else if (*ea.arg == '!') /* :r !filter */
2576 {
2577 ++ea.arg;
2578 ea.usefilter = TRUE;
2579 }
2580 }
2581
2582 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2583 {
2584 ea.amount = 1;
2585 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2586 {
2587 ++ea.arg;
2588 ++ea.amount;
2589 }
2590 ea.arg = skipwhite(ea.arg);
2591 }
2592
2593 /*
2594 * Check for "+command" argument, before checking for next command.
2595 * Don't do this for ":read !cmd" and ":write !cmd".
2596 */
2597 if ((ea.argt & EDITCMD) && !ea.usefilter)
2598 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2599
2600 /*
2601 * Check for '|' to separate commands and '"' to start comments.
2602 * Don't do this for ":read !cmd" and ":write !cmd".
2603 */
2604 if ((ea.argt & TRLBAR) && !ea.usefilter)
2605 separate_nextcmd(&ea);
2606
2607 /*
2608 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002609 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2610 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002612 else if (ea.cmdidx == CMD_bang
2613 || ea.cmdidx == CMD_global
2614 || ea.cmdidx == CMD_vglobal
2615 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 {
2617 for (p = ea.arg; *p; ++p)
2618 {
2619 /* Remove one backslash before a newline, so that it's possible to
2620 * pass a newline to the shell and also a newline that is preceded
2621 * with a backslash. This makes it impossible to end a shell
2622 * command in a backslash, but that doesn't appear useful.
2623 * Halving the number of backslashes is incompatible with previous
2624 * versions. */
2625 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002626 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 else if (*p == '\n')
2628 {
2629 ea.nextcmd = p + 1;
2630 *p = NUL;
2631 break;
2632 }
2633 }
2634 }
2635
2636 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2637 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002638 buf_T *buf;
2639
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002641 switch (ea.addr_type)
2642 {
2643 case ADDR_LINES:
2644 ea.line2 = curbuf->b_ml.ml_line_count;
2645 break;
2646 case ADDR_LOADED_BUFFERS:
2647 buf = firstbuf;
2648 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2649 buf = buf->b_next;
2650 ea.line1 = buf->b_fnum;
2651 buf = lastbuf;
2652 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2653 buf = buf->b_prev;
2654 ea.line2 = buf->b_fnum;
2655 break;
2656 case ADDR_BUFFERS:
2657 ea.line1 = firstbuf->b_fnum;
2658 ea.line2 = lastbuf->b_fnum;
2659 break;
2660 case ADDR_WINDOWS:
2661 ea.line2 = LAST_WIN_NR;
2662 break;
2663 case ADDR_TABS:
2664 ea.line2 = LAST_TAB_NR;
2665 break;
2666 case ADDR_ARGUMENTS:
2667 if (ARGCOUNT == 0)
2668 ea.line1 = ea.line2 = 0;
2669 else
2670 ea.line2 = ARGCOUNT;
2671 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002672#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002673 case ADDR_QUICKFIX:
2674 ea.line2 = qf_get_size(&ea);
2675 if (ea.line2 == 0)
2676 ea.line2 = 1;
2677 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002678#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 }
2681
2682 /* accept numbered register only when no count allowed (:put) */
2683 if ( (ea.argt & REGSTR)
2684 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002685 /* Do not allow register = for user commands */
2686 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2688 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002689#ifndef FEAT_CLIPBOARD
2690 /* check these explicitly for a more specific error message */
2691 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002693 errormsg = (char_u *)_(e_invalidreg);
2694 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 }
2696#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002697 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2698 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002699 {
2700 ea.regname = *ea.arg++;
2701#ifdef FEAT_EVAL
2702 /* for '=' register: accept the rest of the line as an expression */
2703 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2704 {
2705 set_expr_line(vim_strsave(ea.arg));
2706 ea.arg += STRLEN(ea.arg);
2707 }
2708#endif
2709 ea.arg = skipwhite(ea.arg);
2710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 }
2712
2713 /*
2714 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2715 * count, it's a buffer name.
2716 */
2717 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2718 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2719 || vim_iswhite(*p)))
2720 {
2721 n = getdigits(&ea.arg);
2722 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002723 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 {
2725 errormsg = (char_u *)_(e_zerocount);
2726 goto doend;
2727 }
2728 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2729 {
2730 ea.line2 = n;
2731 if (ea.addr_count == 0)
2732 ea.addr_count = 1;
2733 }
2734 else
2735 {
2736 ea.line1 = ea.line2;
2737 ea.line2 += n - 1;
2738 ++ea.addr_count;
2739 /*
2740 * Be vi compatible: no error message for out of range.
2741 */
2742 if (ea.line2 > curbuf->b_ml.ml_line_count)
2743 ea.line2 = curbuf->b_ml.ml_line_count;
2744 }
2745 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002746
2747 /*
2748 * Check for flags: 'l', 'p' and '#'.
2749 */
2750 if (ea.argt & EXFLAGS)
2751 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002753 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002754 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 {
2756 errormsg = (char_u *)_(e_trailing);
2757 goto doend;
2758 }
2759
2760 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2761 {
2762 errormsg = (char_u *)_(e_argreq);
2763 goto doend;
2764 }
2765
2766#ifdef FEAT_EVAL
2767 /*
2768 * Skip the command when it's not going to be executed.
2769 * The commands like :if, :endif, etc. always need to be executed.
2770 * Also make an exception for commands that handle a trailing command
2771 * themselves.
2772 */
2773 if (ea.skip)
2774 {
2775 switch (ea.cmdidx)
2776 {
2777 /* commands that need evaluation */
2778 case CMD_while:
2779 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002780 case CMD_for:
2781 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 case CMD_if:
2783 case CMD_elseif:
2784 case CMD_else:
2785 case CMD_endif:
2786 case CMD_try:
2787 case CMD_catch:
2788 case CMD_finally:
2789 case CMD_endtry:
2790 case CMD_function:
2791 break;
2792
2793 /* Commands that handle '|' themselves. Check: A command should
2794 * either have the TRLBAR flag, appear in this list or appear in
2795 * the list at ":help :bar". */
2796 case CMD_aboveleft:
2797 case CMD_and:
2798 case CMD_belowright:
2799 case CMD_botright:
2800 case CMD_browse:
2801 case CMD_call:
2802 case CMD_confirm:
2803 case CMD_delfunction:
2804 case CMD_djump:
2805 case CMD_dlist:
2806 case CMD_dsearch:
2807 case CMD_dsplit:
2808 case CMD_echo:
2809 case CMD_echoerr:
2810 case CMD_echomsg:
2811 case CMD_echon:
2812 case CMD_execute:
2813 case CMD_help:
2814 case CMD_hide:
2815 case CMD_ijump:
2816 case CMD_ilist:
2817 case CMD_isearch:
2818 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002819 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 case CMD_keepjumps:
2821 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002822 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 case CMD_leftabove:
2824 case CMD_let:
2825 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002826 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002828 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002829 case CMD_noautocmd:
2830 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 case CMD_perl:
2832 case CMD_psearch:
2833 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002834 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002835 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 case CMD_return:
2837 case CMD_rightbelow:
2838 case CMD_ruby:
2839 case CMD_silent:
2840 case CMD_smagic:
2841 case CMD_snomagic:
2842 case CMD_substitute:
2843 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002844 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 case CMD_tcl:
2846 case CMD_throw:
2847 case CMD_tilde:
2848 case CMD_topleft:
2849 case CMD_unlet:
2850 case CMD_verbose:
2851 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002852 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 break;
2854
2855 default: goto doend;
2856 }
2857 }
2858#endif
2859
2860 if (ea.argt & XFILE)
2861 {
2862 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2863 goto doend;
2864 }
2865
2866#ifdef FEAT_LISTCMDS
2867 /*
2868 * Accept buffer name. Cannot be used at the same time with a buffer
2869 * number. Don't do this for a user command.
2870 */
2871 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002872 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 {
2874 /*
2875 * :bdelete, :bwipeout and :bunload take several arguments, separated
2876 * by spaces: find next space (skipping over escaped characters).
2877 * The others take one argument: ignore trailing spaces.
2878 */
2879 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2880 || ea.cmdidx == CMD_bunload)
2881 p = skiptowhite_esc(ea.arg);
2882 else
2883 {
2884 p = ea.arg + STRLEN(ea.arg);
2885 while (p > ea.arg && vim_iswhite(p[-1]))
2886 --p;
2887 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002888 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2889 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 if (ea.line2 < 0) /* failed */
2891 goto doend;
2892 ea.addr_count = 1;
2893 ea.arg = skipwhite(p);
2894 }
2895#endif
2896
2897/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002898 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 *
2900 * The "ea" structure holds the arguments that can be used.
2901 */
2902 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002903 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 ea.cookie = cookie;
2905#ifdef FEAT_EVAL
2906 ea.cstack = cstack;
2907#endif
2908
2909#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002910 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 {
2912 /*
2913 * Execute a user-defined command.
2914 */
2915 do_ucmd(&ea);
2916 }
2917 else
2918#endif
2919 {
2920 /*
2921 * Call the function to execute the command.
2922 */
2923 ea.errmsg = NULL;
2924 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2925 if (ea.errmsg != NULL)
2926 errormsg = (char_u *)_(ea.errmsg);
2927 }
2928
2929#ifdef FEAT_EVAL
2930 /*
2931 * If the command just executed called do_cmdline(), any throw or ":return"
2932 * or ":finish" encountered there must also check the cstack of the still
2933 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2934 * exception, or reanimate a returned function or finished script file and
2935 * return or finish it again.
2936 */
2937 if (need_rethrow)
2938 do_throw(cstack);
2939 else if (check_cstack)
2940 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002941 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002943 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 && current_func_returned())
2945 do_return(&ea, TRUE, FALSE, NULL);
2946 }
2947 need_rethrow = check_cstack = FALSE;
2948#endif
2949
2950doend:
2951 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2952 curwin->w_cursor.lnum = 1;
2953
2954 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2955 {
2956 if (sourcing)
2957 {
2958 if (errormsg != IObuff)
2959 {
2960 STRCPY(IObuff, errormsg);
2961 errormsg = IObuff;
2962 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002963 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 }
2965 emsg(errormsg);
2966 }
2967#ifdef FEAT_EVAL
2968 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002969 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2970 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971#endif
2972
2973 if (verbose_save >= 0)
2974 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002975#ifdef FEAT_AUTOCMD
2976 if (cmdmod.save_ei != NULL)
2977 {
2978 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002979 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2980 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002981 free_string_option(cmdmod.save_ei);
2982 }
2983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984
2985 cmdmod = save_cmdmod;
2986
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002987 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 {
2989 /* messages could be enabled for a serious error, need to check if the
2990 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002991 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002992 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 emsg_silent -= did_esilent;
2994 if (emsg_silent < 0)
2995 emsg_silent = 0;
2996 /* Restore msg_scroll, it's set by file I/O commands, even when no
2997 * message is actually displayed. */
2998 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002999
3000 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
3001 * somewhere in the line. Put it back in the first column. */
3002 if (redirecting())
3003 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 }
3005
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003006#ifdef HAVE_SANDBOX
3007 if (did_sandbox)
3008 --sandbox;
3009#endif
3010
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3012 ea.nextcmd = NULL;
3013
3014#ifdef FEAT_EVAL
3015 --ex_nesting_level;
3016#endif
3017
3018 return ea.nextcmd;
3019}
3020#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003021 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022#endif
3023
3024/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003025 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 * If there is a match advance "pp" to the argument and return TRUE.
3027 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003028 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003029checkforcmd(
3030 char_u **pp, /* start of command */
3031 char *cmd, /* name of command */
3032 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033{
3034 int i;
3035
3036 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003037 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 break;
3039 if (i >= len && !isalpha((*pp)[i]))
3040 {
3041 *pp = skipwhite(*pp + i);
3042 return TRUE;
3043 }
3044 return FALSE;
3045}
3046
3047/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003048 * Append "cmd" to the error message in IObuff.
3049 * Takes care of limiting the length and handling 0xa0, which would be
3050 * invisible otherwise.
3051 */
3052 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003053append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003054{
3055 char_u *s = cmd;
3056 char_u *d;
3057
3058 STRCAT(IObuff, ": ");
3059 d = IObuff + STRLEN(IObuff);
3060 while (*s != NUL && d - IObuff < IOSIZE - 7)
3061 {
3062 if (
3063#ifdef FEAT_MBYTE
3064 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3065#endif
3066 *s == 0xa0)
3067 {
3068 s +=
3069#ifdef FEAT_MBYTE
3070 enc_utf8 ? 2 :
3071#endif
3072 1;
3073 STRCPY(d, "<a0>");
3074 d += 4;
3075 }
3076 else
3077 MB_COPY_CHAR(s, d);
3078 }
3079 *d = NUL;
3080}
3081
3082/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003084 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003086 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 * Returns NULL for an ambiguous user command.
3088 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003090find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091{
3092 int len;
3093 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003094 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095
3096 /*
3097 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003098 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 * - the 'k' command can directly be followed by any character.
3100 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003101 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003103 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 */
3105 p = eap->cmd;
3106 if (*p == 'k')
3107 {
3108 eap->cmdidx = CMD_k;
3109 ++p;
3110 }
3111 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003112 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3113 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 || p[1] == 'g'
3115 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3116 || p[1] == 'I'
3117 || (p[1] == 'r' && p[2] != 'e')))
3118 {
3119 eap->cmdidx = CMD_substitute;
3120 ++p;
3121 }
3122 else
3123 {
3124 while (ASCII_ISALPHA(*p))
3125 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003126 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003127 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003128 while (ASCII_ISALNUM(*p))
3129 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003130
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 /* check for non-alpha command */
3132 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3133 ++p;
3134 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003135 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3136 {
3137 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3138 * :delete with the 'l' flag. Same for 'p'. */
3139 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003140 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003141 break;
3142 if (i == len - 1)
3143 {
3144 --len;
3145 if (p[-1] == 'l')
3146 eap->flags |= EXFLAG_LIST;
3147 else
3148 eap->flags |= EXFLAG_PRINT;
3149 }
3150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151
3152 if (ASCII_ISLOWER(*eap->cmd))
3153 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
3154 else
3155 eap->cmdidx = cmdidxs[26];
3156
3157 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3158 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3159 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3160 (size_t)len) == 0)
3161 {
3162#ifdef FEAT_EVAL
3163 if (full != NULL
3164 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3165 *full = TRUE;
3166#endif
3167 break;
3168 }
3169
3170#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003171 /* Look for a user defined command as a last resort. Let ":Print" be
3172 * overruled by a user defined command. */
3173 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3174 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003176 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 while (ASCII_ISALNUM(*p))
3178 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003179 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 }
3181#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003182 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 eap->cmdidx = CMD_SIZE;
3184 }
3185
3186 return p;
3187}
3188
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003189#ifdef FEAT_USR_CMDS
3190/*
3191 * Search for a user command that matches "eap->cmd".
3192 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3193 * Return a pointer to just after the command.
3194 * Return NULL if there is no matching command.
3195 */
3196 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003197find_ucmd(
3198 exarg_T *eap,
3199 char_u *p, /* end of the command (possibly including count) */
3200 int *full, /* set to TRUE for a full match */
3201 expand_T *xp, /* used for completion, NULL otherwise */
3202 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003203{
3204 int len = (int)(p - eap->cmd);
3205 int j, k, matchlen = 0;
3206 ucmd_T *uc;
3207 int found = FALSE;
3208 int possible = FALSE;
3209 char_u *cp, *np; /* Point into typed cmd and test name */
3210 garray_T *gap;
3211 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3212 only full match global is accepted. */
3213
3214 /*
3215 * Look for buffer-local user commands first, then global ones.
3216 */
3217 gap = &curbuf->b_ucmds;
3218 for (;;)
3219 {
3220 for (j = 0; j < gap->ga_len; ++j)
3221 {
3222 uc = USER_CMD_GA(gap, j);
3223 cp = eap->cmd;
3224 np = uc->uc_name;
3225 k = 0;
3226 while (k < len && *np != NUL && *cp++ == *np++)
3227 k++;
3228 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3229 {
3230 /* If finding a second match, the command is ambiguous. But
3231 * not if a buffer-local command wasn't a full match and a
3232 * global command is a full match. */
3233 if (k == len && found && *np != NUL)
3234 {
3235 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003236 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003237 amb_local = TRUE;
3238 }
3239
3240 if (!found || (k == len && *np == NUL))
3241 {
3242 /* If we matched up to a digit, then there could
3243 * be another command including the digit that we
3244 * should use instead.
3245 */
3246 if (k == len)
3247 found = TRUE;
3248 else
3249 possible = TRUE;
3250
3251 if (gap == &ucmds)
3252 eap->cmdidx = CMD_USER;
3253 else
3254 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003255 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003256 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003257 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003258
3259# ifdef FEAT_CMDL_COMPL
3260 if (compl != NULL)
3261 *compl = uc->uc_compl;
3262# ifdef FEAT_EVAL
3263 if (xp != NULL)
3264 {
3265 xp->xp_arg = uc->uc_compl_arg;
3266 xp->xp_scriptID = uc->uc_scriptID;
3267 }
3268# endif
3269# endif
3270 /* Do not search for further abbreviations
3271 * if this is an exact match. */
3272 matchlen = k;
3273 if (k == len && *np == NUL)
3274 {
3275 if (full != NULL)
3276 *full = TRUE;
3277 amb_local = FALSE;
3278 break;
3279 }
3280 }
3281 }
3282 }
3283
3284 /* Stop if we found a full match or searched all. */
3285 if (j < gap->ga_len || gap == &ucmds)
3286 break;
3287 gap = &ucmds;
3288 }
3289
3290 /* Only found ambiguous matches. */
3291 if (amb_local)
3292 {
3293 if (xp != NULL)
3294 xp->xp_context = EXPAND_UNSUCCESSFUL;
3295 return NULL;
3296 }
3297
3298 /* The match we found may be followed immediately by a number. Move "p"
3299 * back to point to it. */
3300 if (found || possible)
3301 return p + (matchlen - len);
3302 return p;
3303}
3304#endif
3305
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003307static struct cmdmod
3308{
3309 char *name;
3310 int minlen;
3311 int has_count; /* :123verbose :3tab */
3312} cmdmods[] = {
3313 {"aboveleft", 3, FALSE},
3314 {"belowright", 3, FALSE},
3315 {"botright", 2, FALSE},
3316 {"browse", 3, FALSE},
3317 {"confirm", 4, FALSE},
3318 {"hide", 3, FALSE},
3319 {"keepalt", 5, FALSE},
3320 {"keepjumps", 5, FALSE},
3321 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003322 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003323 {"leftabove", 5, FALSE},
3324 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003325 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003326 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003327 {"rightbelow", 6, FALSE},
3328 {"sandbox", 3, FALSE},
3329 {"silent", 3, FALSE},
3330 {"tab", 3, TRUE},
3331 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003332 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003333 {"verbose", 4, TRUE},
3334 {"vertical", 4, FALSE},
3335};
3336
3337/*
3338 * Return length of a command modifier (including optional count).
3339 * Return zero when it's not a modifier.
3340 */
3341 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003342modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003343{
3344 int i, j;
3345 char_u *p = cmd;
3346
3347 if (VIM_ISDIGIT(*cmd))
3348 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003349 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003350 {
3351 for (j = 0; p[j] != NUL; ++j)
3352 if (p[j] != cmdmods[i].name[j])
3353 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003354 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003355 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003356 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003357 }
3358 return 0;
3359}
3360
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361/*
3362 * Return > 0 if an Ex command "name" exists.
3363 * Return 2 if there is an exact match.
3364 * Return 3 if there is an ambiguous match.
3365 */
3366 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003367cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368{
3369 exarg_T ea;
3370 int full = FALSE;
3371 int i;
3372 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003373 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374
3375 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003376 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 {
3378 for (j = 0; name[j] != NUL; ++j)
3379 if (name[j] != cmdmods[i].name[j])
3380 break;
3381 if (name[j] == NUL && j >= cmdmods[i].minlen)
3382 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3383 }
3384
Bram Moolenaara9587612006-05-04 21:47:50 +00003385 /* Check built-in commands and user defined commands.
3386 * For ":2match" and ":3match" we need to skip the number. */
3387 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003389 p = find_command(&ea, &full);
3390 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003392 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3393 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003394 if (*skipwhite(p) != NUL)
3395 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3397}
3398#endif
3399
3400/*
3401 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3402 * we don't need/want deleted. Maybe this could be done better if we didn't
3403 * repeat all this stuff. The only problem is that they may not stay
3404 * perfectly compatible with each other, but then the command line syntax
3405 * probably won't change that much -- webb.
3406 */
3407 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003408set_one_cmd_context(
3409 expand_T *xp,
3410 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411{
3412 char_u *p;
3413 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003414 int len = 0;
3415 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3417 int compl = EXPAND_NOTHING;
3418#endif
3419#ifdef FEAT_CMDL_COMPL
3420 int delim;
3421#endif
3422 int forceit = FALSE;
3423 int usefilter = FALSE; /* filter instead of file name */
3424
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003425 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 xp->xp_pattern = buff;
3427 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003428 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429
3430/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003431 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 */
3433 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3434 ;
3435 xp->xp_pattern = cmd;
3436
3437 if (*cmd == NUL)
3438 return NULL;
3439 if (*cmd == '"') /* ignore comment lines */
3440 {
3441 xp->xp_context = EXPAND_NOTHING;
3442 return NULL;
3443 }
3444
3445/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003446 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 */
3448 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 xp->xp_pattern = cmd;
3450 if (*cmd == NUL)
3451 return NULL;
3452 if (*cmd == '"')
3453 {
3454 xp->xp_context = EXPAND_NOTHING;
3455 return NULL;
3456 }
3457
3458 if (*cmd == '|' || *cmd == '\n')
3459 return cmd + 1; /* There's another command */
3460
3461 /*
3462 * Isolate the command and search for it in the command table.
3463 * Exceptions:
3464 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003465 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3467 */
3468 if (*cmd == 'k' && cmd[1] != 'e')
3469 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003470 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 p = cmd + 1;
3472 }
3473 else
3474 {
3475 p = cmd;
3476 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3477 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003478 /* a user command may contain digits */
3479 if (ASCII_ISUPPER(cmd[0]))
3480 while (ASCII_ISALNUM(*p) || *p == '*')
3481 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003482 /* for python 3.x: ":py3*" commands completion */
3483 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003484 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003485 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003486 while (ASCII_ISALPHA(*p) || *p == '*')
3487 ++p;
3488 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003489 /* check for non-alpha command */
3490 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3491 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003492 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003494 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 {
3496 xp->xp_context = EXPAND_UNSUCCESSFUL;
3497 return NULL;
3498 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003499 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003500 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3501 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3502 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 break;
3504
3505#ifdef FEAT_USR_CMDS
3506 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3508 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509#endif
3510 }
3511
3512 /*
3513 * If the cursor is touching the command, and it ends in an alpha-numeric
3514 * character, complete the command name.
3515 */
3516 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3517 return NULL;
3518
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003519 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 {
3521 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3522 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003523 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 p = cmd + 1;
3525 }
3526#ifdef FEAT_USR_CMDS
3527 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3528 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003529 ea.cmd = cmd;
3530 p = find_ucmd(&ea, p, NULL, xp,
3531# if defined(FEAT_CMDL_COMPL)
3532 &compl
3533# else
3534 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003536 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003537 if (p == NULL)
3538 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 }
3540#endif
3541 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003542 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 {
3544 /* Not still touching the command and it was an illegal one */
3545 xp->xp_context = EXPAND_UNSUCCESSFUL;
3546 return NULL;
3547 }
3548
3549 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3550
3551 if (*p == '!') /* forced commands */
3552 {
3553 forceit = TRUE;
3554 ++p;
3555 }
3556
3557/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003558 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003560 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003561 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562
3563 arg = skipwhite(p);
3564
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003565 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 {
3567 if (*arg == '>') /* append */
3568 {
3569 if (*++arg == '>')
3570 ++arg;
3571 arg = skipwhite(arg);
3572 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003573 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 {
3575 ++arg;
3576 usefilter = TRUE;
3577 }
3578 }
3579
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003580 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 {
3582 usefilter = forceit; /* :r! filter if forced */
3583 if (*arg == '!') /* :r !filter */
3584 {
3585 ++arg;
3586 usefilter = TRUE;
3587 }
3588 }
3589
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003590 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 {
3592 while (*arg == *cmd) /* allow any number of '>' or '<' */
3593 ++arg;
3594 arg = skipwhite(arg);
3595 }
3596
3597 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003598 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 {
3600 /* Check if we're in the +command */
3601 p = arg + 1;
3602 arg = skip_cmd_arg(arg, FALSE);
3603
3604 /* Still touching the command after '+'? */
3605 if (*arg == NUL)
3606 return p;
3607
3608 /* Skip space(s) after +command to get to the real argument */
3609 arg = skipwhite(arg);
3610 }
3611
3612 /*
3613 * Check for '|' to separate commands and '"' to start comments.
3614 * Don't do this for ":read !cmd" and ":write !cmd".
3615 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003616 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 {
3618 p = arg;
3619 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003620 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 p += 2;
3622 while (*p)
3623 {
3624 if (*p == Ctrl_V)
3625 {
3626 if (p[1] != NUL)
3627 ++p;
3628 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003629 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 || *p == '|' || *p == '\n')
3631 {
3632 if (*(p - 1) != '\\')
3633 {
3634 if (*p == '|' || *p == '\n')
3635 return p + 1;
3636 return NULL; /* It's a comment */
3637 }
3638 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003639 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 }
3641 }
3642
3643 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003644 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 vim_strchr((char_u *)"|\"", *arg) == NULL)
3646 return NULL;
3647
3648 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003649 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003651 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003652 while (*p && p < buff + len)
3653 {
3654 if (*p == ' ' || *p == TAB)
3655 {
3656 /* argument starts after a space */
3657 xp->xp_pattern = ++p;
3658 }
3659 else
3660 {
3661 if (*p == '\\' && *(p + 1) != NUL)
3662 ++p; /* skip over escaped character */
3663 mb_ptr_adv(p);
3664 }
3665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003667 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003669 int c;
3670 int in_quote = FALSE;
3671 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672
3673 /*
3674 * Allow spaces within back-quotes to count as part of the argument
3675 * being expanded.
3676 */
3677 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003678 p = xp->xp_pattern;
3679 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003681#ifdef FEAT_MBYTE
3682 if (has_mbyte)
3683 c = mb_ptr2char(p);
3684 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003686 c = *p;
3687 if (c == '\\' && p[1] != NUL)
3688 ++p;
3689 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 {
3691 if (!in_quote)
3692 {
3693 xp->xp_pattern = p;
3694 bow = p + 1;
3695 }
3696 in_quote = !in_quote;
3697 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003698 /* An argument can contain just about everything, except
3699 * characters that end the command and white space. */
3700 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003701#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003702 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003703#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003704 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003705 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003706 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003707 while (*p != NUL)
3708 {
3709#ifdef FEAT_MBYTE
3710 if (has_mbyte)
3711 c = mb_ptr2char(p);
3712 else
3713#endif
3714 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003715 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003716 break;
3717#ifdef FEAT_MBYTE
3718 if (has_mbyte)
3719 len = (*mb_ptr2len)(p);
3720 else
3721#endif
3722 len = 1;
3723 mb_ptr_adv(p);
3724 }
3725 if (in_quote)
3726 bow = p;
3727 else
3728 xp->xp_pattern = p;
3729 p -= len;
3730 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003731 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 }
3733
3734 /*
3735 * If we are still inside the quotes, and we passed a space, just
3736 * expand from there.
3737 */
3738 if (bow != NULL && in_quote)
3739 xp->xp_pattern = bow;
3740 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003741
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003742 /* For a shell command more chars need to be escaped. */
3743 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003744 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003745#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003746 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003747#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003748 /* When still after the command name expand executables. */
3749 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003750 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752
3753 /* Check for environment variable */
3754 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003755#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 || *xp->xp_pattern == '%'
3757#endif
3758 )
3759 {
3760 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3761 if (!vim_isIDc(*p))
3762 break;
3763 if (*p == NUL)
3764 {
3765 xp->xp_context = EXPAND_ENV_VARS;
3766 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003767#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3768 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003769 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003770 compl = EXPAND_ENV_VARS;
3771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 }
3773 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003774#if defined(FEAT_CMDL_COMPL)
3775 /* Check for user names */
3776 if (*xp->xp_pattern == '~')
3777 {
3778 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3779 ;
3780 /* Complete ~user only if it partially matches a user name.
3781 * A full match ~user<Tab> will be replaced by user's home
3782 * directory i.e. something like ~user<Tab> -> /home/user/ */
3783 if (*p == NUL && p > xp->xp_pattern + 1
3784 && match_user(xp->xp_pattern + 1) == 1)
3785 {
3786 xp->xp_context = EXPAND_USER;
3787 ++xp->xp_pattern;
3788 }
3789 }
3790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 }
3792
3793/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003794 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003796 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003798 case CMD_find:
3799 case CMD_sfind:
3800 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003801 if (xp->xp_context == EXPAND_FILES)
3802 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003803 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 case CMD_cd:
3805 case CMD_chdir:
3806 case CMD_lcd:
3807 case CMD_lchdir:
3808 if (xp->xp_context == EXPAND_FILES)
3809 xp->xp_context = EXPAND_DIRECTORIES;
3810 break;
3811 case CMD_help:
3812 xp->xp_context = EXPAND_HELP;
3813 xp->xp_pattern = arg;
3814 break;
3815
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003816 /* Command modifiers: return the argument.
3817 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003819 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 case CMD_belowright:
3821 case CMD_botright:
3822 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003823 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003824 case CMD_cdo:
3825 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003827 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 case CMD_folddoclosed:
3829 case CMD_folddoopen:
3830 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003831 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 case CMD_keepjumps:
3833 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003834 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003835 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003837 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003839 case CMD_noautocmd:
3840 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003842 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003844 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003845 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 case CMD_topleft:
3847 case CMD_verbose:
3848 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003849 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 return arg;
3851
Bram Moolenaar4f688582007-07-24 12:34:30 +00003852#ifdef FEAT_CMDL_COMPL
3853# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 case CMD_match:
3855 if (*arg == NUL || !ends_excmd(*arg))
3856 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003857 /* also complete "None" */
3858 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 arg = skipwhite(skiptowhite(arg));
3860 if (*arg != NUL)
3861 {
3862 xp->xp_context = EXPAND_NOTHING;
3863 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3864 }
3865 }
3866 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003867# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869/*
3870 * All completion for the +cmdline_compl feature goes here.
3871 */
3872
3873# ifdef FEAT_USR_CMDS
3874 case CMD_command:
3875 /* Check for attributes */
3876 while (*arg == '-')
3877 {
3878 arg++; /* Skip "-" */
3879 p = skiptowhite(arg);
3880 if (*p == NUL)
3881 {
3882 /* Cursor is still in the attribute */
3883 p = vim_strchr(arg, '=');
3884 if (p == NULL)
3885 {
3886 /* No "=", so complete attribute names */
3887 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3888 xp->xp_pattern = arg;
3889 return NULL;
3890 }
3891
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003892 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 * their arguments as well.
3894 */
3895 if (STRNICMP(arg, "complete", p - arg) == 0)
3896 {
3897 xp->xp_context = EXPAND_USER_COMPLETE;
3898 xp->xp_pattern = p + 1;
3899 return NULL;
3900 }
3901 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3902 {
3903 xp->xp_context = EXPAND_USER_NARGS;
3904 xp->xp_pattern = p + 1;
3905 return NULL;
3906 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003907 else if (STRNICMP(arg, "addr", p - arg) == 0)
3908 {
3909 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3910 xp->xp_pattern = p + 1;
3911 return NULL;
3912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 return NULL;
3914 }
3915 arg = skipwhite(p);
3916 }
3917
3918 /* After the attributes comes the new command name */
3919 p = skiptowhite(arg);
3920 if (*p == NUL)
3921 {
3922 xp->xp_context = EXPAND_USER_COMMANDS;
3923 xp->xp_pattern = arg;
3924 break;
3925 }
3926
3927 /* And finally comes a normal command */
3928 return skipwhite(p);
3929
3930 case CMD_delcommand:
3931 xp->xp_context = EXPAND_USER_COMMANDS;
3932 xp->xp_pattern = arg;
3933 break;
3934# endif
3935
3936 case CMD_global:
3937 case CMD_vglobal:
3938 delim = *arg; /* get the delimiter */
3939 if (delim)
3940 ++arg; /* skip delimiter if there is one */
3941
3942 while (arg[0] != NUL && arg[0] != delim)
3943 {
3944 if (arg[0] == '\\' && arg[1] != NUL)
3945 ++arg;
3946 ++arg;
3947 }
3948 if (arg[0] != NUL)
3949 return arg + 1;
3950 break;
3951 case CMD_and:
3952 case CMD_substitute:
3953 delim = *arg;
3954 if (delim)
3955 {
3956 /* skip "from" part */
3957 ++arg;
3958 arg = skip_regexp(arg, delim, p_magic, NULL);
3959 }
3960 /* skip "to" part */
3961 while (arg[0] != NUL && arg[0] != delim)
3962 {
3963 if (arg[0] == '\\' && arg[1] != NUL)
3964 ++arg;
3965 ++arg;
3966 }
3967 if (arg[0] != NUL) /* skip delimiter */
3968 ++arg;
3969 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3970 ++arg;
3971 if (arg[0] != NUL)
3972 return arg;
3973 break;
3974 case CMD_isearch:
3975 case CMD_dsearch:
3976 case CMD_ilist:
3977 case CMD_dlist:
3978 case CMD_ijump:
3979 case CMD_psearch:
3980 case CMD_djump:
3981 case CMD_isplit:
3982 case CMD_dsplit:
3983 arg = skipwhite(skipdigits(arg)); /* skip count */
3984 if (*arg == '/') /* Match regexp, not just whole words */
3985 {
3986 for (++arg; *arg && *arg != '/'; arg++)
3987 if (*arg == '\\' && arg[1] != NUL)
3988 arg++;
3989 if (*arg)
3990 {
3991 arg = skipwhite(arg + 1);
3992
3993 /* Check for trailing illegal characters */
3994 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3995 xp->xp_context = EXPAND_NOTHING;
3996 else
3997 return arg;
3998 }
3999 }
4000 break;
4001#ifdef FEAT_AUTOCMD
4002 case CMD_autocmd:
4003 return set_context_in_autocmd(xp, arg, FALSE);
4004
4005 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004006 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 return set_context_in_autocmd(xp, arg, TRUE);
4008#endif
4009 case CMD_set:
4010 set_context_in_set_cmd(xp, arg, 0);
4011 break;
4012 case CMD_setglobal:
4013 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4014 break;
4015 case CMD_setlocal:
4016 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4017 break;
4018 case CMD_tag:
4019 case CMD_stag:
4020 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004021 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 case CMD_tselect:
4023 case CMD_stselect:
4024 case CMD_ptselect:
4025 case CMD_tjump:
4026 case CMD_stjump:
4027 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004028 if (*p_wop != NUL)
4029 xp->xp_context = EXPAND_TAGS_LISTFILES;
4030 else
4031 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 xp->xp_pattern = arg;
4033 break;
4034 case CMD_augroup:
4035 xp->xp_context = EXPAND_AUGROUP;
4036 xp->xp_pattern = arg;
4037 break;
4038#ifdef FEAT_SYN_HL
4039 case CMD_syntax:
4040 set_context_in_syntax_cmd(xp, arg);
4041 break;
4042#endif
4043#ifdef FEAT_EVAL
4044 case CMD_let:
4045 case CMD_if:
4046 case CMD_elseif:
4047 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004048 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 case CMD_echo:
4050 case CMD_echon:
4051 case CMD_execute:
4052 case CMD_echomsg:
4053 case CMD_echoerr:
4054 case CMD_call:
4055 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004056 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 break;
4058
4059 case CMD_unlet:
4060 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4061 arg = xp->xp_pattern + 1;
4062 xp->xp_context = EXPAND_USER_VARS;
4063 xp->xp_pattern = arg;
4064 break;
4065
4066 case CMD_function:
4067 case CMD_delfunction:
4068 xp->xp_context = EXPAND_USER_FUNC;
4069 xp->xp_pattern = arg;
4070 break;
4071
4072 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004073 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 break;
4075#endif
4076 case CMD_highlight:
4077 set_context_in_highlight_cmd(xp, arg);
4078 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004079#ifdef FEAT_CSCOPE
4080 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004081 case CMD_lcscope:
4082 case CMD_scscope:
4083 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004084 break;
4085#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004086#ifdef FEAT_SIGNS
4087 case CMD_sign:
4088 set_context_in_sign_cmd(xp, arg);
4089 break;
4090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091#ifdef FEAT_LISTCMDS
4092 case CMD_bdelete:
4093 case CMD_bwipeout:
4094 case CMD_bunload:
4095 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4096 arg = xp->xp_pattern + 1;
4097 /*FALLTHROUGH*/
4098 case CMD_buffer:
4099 case CMD_sbuffer:
4100 case CMD_checktime:
4101 xp->xp_context = EXPAND_BUFFERS;
4102 xp->xp_pattern = arg;
4103 break;
4104#endif
4105#ifdef FEAT_USR_CMDS
4106 case CMD_USER:
4107 case CMD_USER_BUF:
4108 if (compl != EXPAND_NOTHING)
4109 {
4110 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004111 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 {
4113# ifdef FEAT_MENU
4114 if (compl == EXPAND_MENUS)
4115 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4116# endif
4117 if (compl == EXPAND_COMMANDS)
4118 return arg;
4119 if (compl == EXPAND_MAPPINGS)
4120 return set_context_in_map_cmd(xp, (char_u *)"map",
4121 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004122 /* Find start of last argument. */
4123 p = arg;
4124 while (*p)
4125 {
4126 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004127 /* argument starts after a space */
4128 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004129 else if (*p == '\\' && *(p + 1) != NUL)
4130 ++p; /* skip over escaped character */
4131 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 xp->xp_pattern = arg;
4134 }
4135 xp->xp_context = compl;
4136 }
4137 break;
4138#endif
4139 case CMD_map: case CMD_noremap:
4140 case CMD_nmap: case CMD_nnoremap:
4141 case CMD_vmap: case CMD_vnoremap:
4142 case CMD_omap: case CMD_onoremap:
4143 case CMD_imap: case CMD_inoremap:
4144 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004145 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004146 case CMD_smap: case CMD_snoremap:
4147 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004149 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 case CMD_unmap:
4151 case CMD_nunmap:
4152 case CMD_vunmap:
4153 case CMD_ounmap:
4154 case CMD_iunmap:
4155 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004156 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004157 case CMD_sunmap:
4158 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004160 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 case CMD_abbreviate: case CMD_noreabbrev:
4162 case CMD_cabbrev: case CMD_cnoreabbrev:
4163 case CMD_iabbrev: case CMD_inoreabbrev:
4164 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004165 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 case CMD_unabbreviate:
4167 case CMD_cunabbrev:
4168 case CMD_iunabbrev:
4169 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004170 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171#ifdef FEAT_MENU
4172 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4173 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4174 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4175 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4176 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4177 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4178 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4179 case CMD_tmenu: case CMD_tunmenu:
4180 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4181 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4182#endif
4183
4184 case CMD_colorscheme:
4185 xp->xp_context = EXPAND_COLORS;
4186 xp->xp_pattern = arg;
4187 break;
4188
4189 case CMD_compiler:
4190 xp->xp_context = EXPAND_COMPILER;
4191 xp->xp_pattern = arg;
4192 break;
4193
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004194 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004195 xp->xp_context = EXPAND_OWNSYNTAX;
4196 xp->xp_pattern = arg;
4197 break;
4198
4199 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004200 xp->xp_context = EXPAND_FILETYPE;
4201 xp->xp_pattern = arg;
4202 break;
4203
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004204 case CMD_packadd:
4205 xp->xp_context = EXPAND_PACKADD;
4206 xp->xp_pattern = arg;
4207 break;
4208
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4210 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4211 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004212 p = skiptowhite(arg);
4213 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 {
4215 xp->xp_context = EXPAND_LANGUAGE;
4216 xp->xp_pattern = arg;
4217 }
4218 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004219 {
4220 if ( STRNCMP(arg, "messages", p - arg) == 0
4221 || STRNCMP(arg, "ctype", p - arg) == 0
4222 || STRNCMP(arg, "time", p - arg) == 0)
4223 {
4224 xp->xp_context = EXPAND_LOCALES;
4225 xp->xp_pattern = skipwhite(p);
4226 }
4227 else
4228 xp->xp_context = EXPAND_NOTHING;
4229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 break;
4231#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004232#if defined(FEAT_PROFILE)
4233 case CMD_profile:
4234 set_context_in_profile_cmd(xp, arg);
4235 break;
4236#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004237 case CMD_behave:
4238 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004239 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004240 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004242#if defined(FEAT_CMDHIST)
4243 case CMD_history:
4244 xp->xp_context = EXPAND_HISTORY;
4245 xp->xp_pattern = arg;
4246 break;
4247#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004248#if defined(FEAT_PROFILE)
4249 case CMD_syntime:
4250 xp->xp_context = EXPAND_SYNTIME;
4251 xp->xp_pattern = arg;
4252 break;
4253#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004254
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255#endif /* FEAT_CMDL_COMPL */
4256
4257 default:
4258 break;
4259 }
4260 return NULL;
4261}
4262
4263/*
4264 * skip a range specifier of the form: addr [,addr] [;addr] ..
4265 *
4266 * Backslashed delimiters after / or ? will be skipped, and commands will
4267 * not be expanded between /'s and ?'s or after "'".
4268 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004269 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 * Returns the "cmd" pointer advanced to beyond the range.
4271 */
4272 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004273skip_range(
4274 char_u *cmd,
4275 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004277 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278
Bram Moolenaardf177f62005-02-22 08:39:57 +00004279 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 {
4281 if (*cmd == '\'')
4282 {
4283 if (*++cmd == NUL && ctx != NULL)
4284 *ctx = EXPAND_NOTHING;
4285 }
4286 else if (*cmd == '/' || *cmd == '?')
4287 {
4288 delim = *cmd++;
4289 while (*cmd != NUL && *cmd != delim)
4290 if (*cmd++ == '\\' && *cmd != NUL)
4291 ++cmd;
4292 if (*cmd == NUL && ctx != NULL)
4293 *ctx = EXPAND_NOTHING;
4294 }
4295 if (*cmd != NUL)
4296 ++cmd;
4297 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004298
4299 /* Skip ":" and white space. */
4300 while (*cmd == ':')
4301 cmd = skipwhite(cmd + 1);
4302
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 return cmd;
4304}
4305
4306/*
4307 * get a single EX address
4308 *
4309 * Set ptr to the next character after the part that was interpreted.
4310 * Set ptr to NULL when an error is encountered.
4311 *
4312 * Return MAXLNUM when no Ex address was found.
4313 */
4314 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004315get_address(
4316 exarg_T *eap UNUSED,
4317 char_u **ptr,
4318 int addr_type, /* flag: one of ADDR_LINES, ... */
4319 int skip, /* only skip the address, don't use it */
4320 int to_other_file) /* flag: may jump to other file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321{
4322 int c;
4323 int i;
4324 long n;
4325 char_u *cmd;
4326 pos_T pos;
4327 pos_T *fp;
4328 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004329 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330
4331 cmd = skipwhite(*ptr);
4332 lnum = MAXLNUM;
4333 do
4334 {
4335 switch (*cmd)
4336 {
4337 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004338 ++cmd;
4339 switch (addr_type)
4340 {
4341 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 lnum = curwin->w_cursor.lnum;
4343 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004344 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004345 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004346 break;
4347 case ADDR_ARGUMENTS:
4348 lnum = curwin->w_arg_idx + 1;
4349 break;
4350 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004351 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004352 lnum = curbuf->b_fnum;
4353 break;
4354 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004355 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004356 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004357#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004358 case ADDR_QUICKFIX:
4359 lnum = qf_get_cur_valid_idx(eap);
4360 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004361#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004362 }
4363 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364
4365 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004366 ++cmd;
4367 switch (addr_type)
4368 {
4369 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 lnum = curbuf->b_ml.ml_line_count;
4371 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004372 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004373 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004374 break;
4375 case ADDR_ARGUMENTS:
4376 lnum = ARGCOUNT;
4377 break;
4378 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004379 buf = lastbuf;
4380 while (buf->b_ml.ml_mfp == NULL)
4381 {
4382 if (buf->b_prev == NULL)
4383 break;
4384 buf = buf->b_prev;
4385 }
4386 lnum = buf->b_fnum;
4387 break;
4388 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004389 lnum = lastbuf->b_fnum;
4390 break;
4391 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004392 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004393 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004394#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004395 case ADDR_QUICKFIX:
4396 lnum = qf_get_size(eap);
4397 if (lnum == 0)
4398 lnum = 1;
4399 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004400#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004401 }
4402 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403
4404 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004405 if (*++cmd == NUL)
4406 {
4407 cmd = NULL;
4408 goto error;
4409 }
4410 if (addr_type != ADDR_LINES)
4411 {
4412 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004413 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004414 goto error;
4415 }
4416 if (skip)
4417 ++cmd;
4418 else
4419 {
4420 /* Only accept a mark in another file when it is
4421 * used by itself: ":'M". */
4422 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4423 ++cmd;
4424 if (fp == (pos_T *)-1)
4425 /* Jumped to another file. */
4426 lnum = curwin->w_cursor.lnum;
4427 else
4428 {
4429 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 {
4431 cmd = NULL;
4432 goto error;
4433 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004434 lnum = fp->lnum;
4435 }
4436 }
4437 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438
4439 case '/':
4440 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004441 c = *cmd++;
4442 if (addr_type != ADDR_LINES)
4443 {
4444 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004445 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004446 goto error;
4447 }
4448 if (skip) /* skip "/pat/" */
4449 {
4450 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4451 if (*cmd == c)
4452 ++cmd;
4453 }
4454 else
4455 {
4456 pos = curwin->w_cursor; /* save curwin->w_cursor */
4457 /*
4458 * When '/' or '?' follows another address, start
4459 * from there.
4460 */
4461 if (lnum != MAXLNUM)
4462 curwin->w_cursor.lnum = lnum;
4463 /*
4464 * Start a forward search at the end of the line.
4465 * Start a backward search at the start of the line.
4466 * This makes sure we never match in the current
4467 * line, and can match anywhere in the
4468 * next/previous line.
4469 */
4470 if (c == '/')
4471 curwin->w_cursor.col = MAXCOL;
4472 else
4473 curwin->w_cursor.col = 0;
4474 searchcmdlen = 0;
4475 if (!do_search(NULL, c, cmd, 1L,
4476 SEARCH_HIS | SEARCH_MSG, NULL))
4477 {
4478 curwin->w_cursor = pos;
4479 cmd = NULL;
4480 goto error;
4481 }
4482 lnum = curwin->w_cursor.lnum;
4483 curwin->w_cursor = pos;
4484 /* adjust command string pointer */
4485 cmd += searchcmdlen;
4486 }
4487 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488
4489 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004490 ++cmd;
4491 if (addr_type != ADDR_LINES)
4492 {
4493 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004494 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004495 goto error;
4496 }
4497 if (*cmd == '&')
4498 i = RE_SUBST;
4499 else if (*cmd == '?' || *cmd == '/')
4500 i = RE_SEARCH;
4501 else
4502 {
4503 EMSG(_(e_backslash));
4504 cmd = NULL;
4505 goto error;
4506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004508 if (!skip)
4509 {
4510 /*
4511 * When search follows another address, start from
4512 * there.
4513 */
4514 if (lnum != MAXLNUM)
4515 pos.lnum = lnum;
4516 else
4517 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004519 /*
4520 * Start the search just like for the above
4521 * do_search().
4522 */
4523 if (*cmd != '?')
4524 pos.col = MAXCOL;
4525 else
4526 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004527#ifdef FEAT_VIRTUALEDIT
4528 pos.coladd = 0;
4529#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004530 if (searchit(curwin, curbuf, &pos,
4531 *cmd == '?' ? BACKWARD : FORWARD,
4532 (char_u *)"", 1L, SEARCH_MSG,
4533 i, (linenr_T)0, NULL) != FAIL)
4534 lnum = pos.lnum;
4535 else
4536 {
4537 cmd = NULL;
4538 goto error;
4539 }
4540 }
4541 ++cmd;
4542 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543
4544 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004545 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4546 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 }
4548
4549 for (;;)
4550 {
4551 cmd = skipwhite(cmd);
4552 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4553 break;
4554
4555 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004556 {
4557 switch (addr_type)
4558 {
4559 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004560 /* "+1" is same as ".+1" */
4561 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004562 break;
4563 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004564 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004565 break;
4566 case ADDR_ARGUMENTS:
4567 lnum = curwin->w_arg_idx + 1;
4568 break;
4569 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004570 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004571 lnum = curbuf->b_fnum;
4572 break;
4573 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004574 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004575 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004576#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004577 case ADDR_QUICKFIX:
4578 lnum = qf_get_cur_valid_idx(eap);
4579 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004580#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004581 }
4582 }
4583
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 if (VIM_ISDIGIT(*cmd))
4585 i = '+'; /* "number" is same as "+number" */
4586 else
4587 i = *cmd++;
4588 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4589 n = 1;
4590 else
4591 n = getdigits(&cmd);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004592 if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004593 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004594 lnum = compute_buffer_local_count(
4595 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004596 else if (i == '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 lnum -= n;
4598 else
4599 lnum += n;
4600 }
4601 } while (*cmd == '/' || *cmd == '?');
4602
4603error:
4604 *ptr = cmd;
4605 return lnum;
4606}
4607
4608/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004609 * Get flags from an Ex command argument.
4610 */
4611 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004612get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004613{
4614 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4615 {
4616 if (*eap->arg == 'l')
4617 eap->flags |= EXFLAG_LIST;
4618 else if (*eap->arg == 'p')
4619 eap->flags |= EXFLAG_PRINT;
4620 else
4621 eap->flags |= EXFLAG_NR;
4622 eap->arg = skipwhite(eap->arg + 1);
4623 }
4624}
4625
4626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 * Function called for command which is Not Implemented. NI!
4628 */
4629 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004630ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631{
4632 if (!eap->skip)
4633 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4634}
4635
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004636#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637/*
4638 * Function called for script command which is Not Implemented. NI!
4639 * Skips over ":perl <<EOF" constructs.
4640 */
4641 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004642ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643{
4644 if (!eap->skip)
4645 ex_ni(eap);
4646 else
4647 vim_free(script_get(eap, eap->arg));
4648}
4649#endif
4650
4651/*
4652 * Check range in Ex command for validity.
4653 * Return NULL when valid, error message when invalid.
4654 */
4655 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004656invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004658 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 if ( eap->line1 < 0
4660 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004661 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004663
4664 if (eap->argt & RANGE)
4665 {
4666 switch(eap->addr_type)
4667 {
4668 case ADDR_LINES:
4669 if (!(eap->argt & NOTADR)
4670 && eap->line2 > curbuf->b_ml.ml_line_count
4671#ifdef FEAT_DIFF
4672 + (eap->cmdidx == CMD_diffget)
4673#endif
4674 )
4675 return (char_u *)_(e_invrange);
4676 break;
4677 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004678 /* add 1 if ARGCOUNT is 0 */
4679 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004680 return (char_u *)_(e_invrange);
4681 break;
4682 case ADDR_BUFFERS:
4683 if (eap->line1 < firstbuf->b_fnum
4684 || eap->line2 > lastbuf->b_fnum)
4685 return (char_u *)_(e_invrange);
4686 break;
4687 case ADDR_LOADED_BUFFERS:
4688 buf = firstbuf;
4689 while (buf->b_ml.ml_mfp == NULL)
4690 {
4691 if (buf->b_next == NULL)
4692 return (char_u *)_(e_invrange);
4693 buf = buf->b_next;
4694 }
4695 if (eap->line1 < buf->b_fnum)
4696 return (char_u *)_(e_invrange);
4697 buf = lastbuf;
4698 while (buf->b_ml.ml_mfp == NULL)
4699 {
4700 if (buf->b_prev == NULL)
4701 return (char_u *)_(e_invrange);
4702 buf = buf->b_prev;
4703 }
4704 if (eap->line2 > buf->b_fnum)
4705 return (char_u *)_(e_invrange);
4706 break;
4707 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004708 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004709 return (char_u *)_(e_invrange);
4710 break;
4711 case ADDR_TABS:
4712 if (eap->line2 > LAST_TAB_NR)
4713 return (char_u *)_(e_invrange);
4714 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004715#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004716 case ADDR_QUICKFIX:
4717 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4718 return (char_u *)_(e_invrange);
4719 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004720#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004721 }
4722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 return NULL;
4724}
4725
4726/*
4727 * Correct the range for zero line number, if required.
4728 */
4729 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004730correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731{
4732 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4733 {
4734 if (eap->line1 == 0)
4735 eap->line1 = 1;
4736 if (eap->line2 == 0)
4737 eap->line2 = 1;
4738 }
4739}
4740
Bram Moolenaar748bf032005-02-02 23:04:36 +00004741#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004742static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004743
4744/*
4745 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4746 * pattern. Otherwise return eap->arg.
4747 */
4748 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004749skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004750{
4751 char_u *p = eap->arg;
4752
Bram Moolenaara37420f2006-02-04 22:37:47 +00004753 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4754 || eap->cmdidx == CMD_vimgrepadd
4755 || eap->cmdidx == CMD_lvimgrepadd
4756 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004757 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004758 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004759 if (p == NULL)
4760 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004761 }
4762 return p;
4763}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004764
4765/*
4766 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4767 * in the command line, so that things like % get expanded.
4768 */
4769 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004770replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004771{
4772 char_u *new_cmdline;
4773 char_u *program;
4774 char_u *pos;
4775 char_u *ptr;
4776 int len;
4777 int i;
4778
4779 /*
4780 * Don't do it when ":vimgrep" is used for ":grep".
4781 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004782 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4783 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4784 || eap->cmdidx == CMD_grepadd
4785 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004786 && !grep_internal(eap->cmdidx))
4787 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004788 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4789 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004790 {
4791 if (*curbuf->b_p_gp == NUL)
4792 program = p_gp;
4793 else
4794 program = curbuf->b_p_gp;
4795 }
4796 else
4797 {
4798 if (*curbuf->b_p_mp == NUL)
4799 program = p_mp;
4800 else
4801 program = curbuf->b_p_mp;
4802 }
4803
4804 p = skipwhite(p);
4805
4806 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4807 {
4808 /* replace $* by given arguments */
4809 i = 1;
4810 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4811 ++i;
4812 len = (int)STRLEN(p);
4813 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4814 if (new_cmdline == NULL)
4815 return NULL; /* out of memory */
4816 ptr = new_cmdline;
4817 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4818 {
4819 i = (int)(pos - program);
4820 STRNCPY(ptr, program, i);
4821 STRCPY(ptr += i, p);
4822 ptr += len;
4823 program = pos + 2;
4824 }
4825 STRCPY(ptr, program);
4826 }
4827 else
4828 {
4829 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4830 if (new_cmdline == NULL)
4831 return NULL; /* out of memory */
4832 STRCPY(new_cmdline, program);
4833 STRCAT(new_cmdline, " ");
4834 STRCAT(new_cmdline, p);
4835 }
4836 msg_make(p);
4837
4838 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4839 vim_free(*cmdlinep);
4840 *cmdlinep = new_cmdline;
4841 p = new_cmdline;
4842 }
4843 return p;
4844}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004845#endif
4846
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847/*
4848 * Expand file name in Ex command argument.
4849 * Return FAIL for failure, OK otherwise.
4850 */
4851 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004852expand_filename(
4853 exarg_T *eap,
4854 char_u **cmdlinep,
4855 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856{
4857 int has_wildcards; /* need to expand wildcards */
4858 char_u *repl;
4859 int srclen;
4860 char_u *p;
4861 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004862 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863
Bram Moolenaar748bf032005-02-02 23:04:36 +00004864#ifdef FEAT_QUICKFIX
4865 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4866 p = skip_grep_pat(eap);
4867#else
4868 p = eap->arg;
4869#endif
4870
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 /*
4872 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4873 * the file name contains a wildcard it should not cause expanding.
4874 * (it will be expanded anyway if there is a wildcard before replacing).
4875 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004876 has_wildcards = mch_has_wildcard(p);
4877 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004879#ifdef FEAT_EVAL
4880 /* Skip over `=expr`, wildcards in it are not expanded. */
4881 if (p[0] == '`' && p[1] == '=')
4882 {
4883 p += 2;
4884 (void)skip_expr(&p);
4885 if (*p == '`')
4886 ++p;
4887 continue;
4888 }
4889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 /*
4891 * Quick check if this cannot be the start of a special string.
4892 * Also removes backslash before '%', '#' and '<'.
4893 */
4894 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4895 {
4896 ++p;
4897 continue;
4898 }
4899
4900 /*
4901 * Try to find a match at this position.
4902 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004903 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4904 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 if (*errormsgp != NULL) /* error detected */
4906 return FAIL;
4907 if (repl == NULL) /* no match found */
4908 {
4909 p += srclen;
4910 continue;
4911 }
4912
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004913 /* Wildcards won't be expanded below, the replacement is taken
4914 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4915 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4916 {
4917 char_u *l = repl;
4918
4919 repl = expand_env_save(repl);
4920 vim_free(l);
4921 }
4922
Bram Moolenaar63b92542007-03-27 14:57:09 +00004923 /* Need to escape white space et al. with a backslash.
4924 * Don't do this for:
4925 * - replacement that already has been escaped: "##"
4926 * - shell commands (may have to use quotes instead).
4927 * - non-unix systems when there is a single argument (spaces don't
4928 * separate arguments then).
4929 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004931 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 && eap->cmdidx != CMD_bang
4933 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004934 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004936 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004938 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939#ifndef UNIX
4940 && !(eap->argt & NOSPC)
4941#endif
4942 )
4943 {
4944 char_u *l;
4945#ifdef BACKSLASH_IN_FILENAME
4946 /* Don't escape a backslash here, because rem_backslash() doesn't
4947 * remove it later. */
4948 static char_u *nobslash = (char_u *)" \t\"|";
4949# define ESCAPE_CHARS nobslash
4950#else
4951# define ESCAPE_CHARS escape_chars
4952#endif
4953
4954 for (l = repl; *l; ++l)
4955 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4956 {
4957 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4958 if (l != NULL)
4959 {
4960 vim_free(repl);
4961 repl = l;
4962 }
4963 break;
4964 }
4965 }
4966
4967 /* For a shell command a '!' must be escaped. */
4968 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004969 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 {
4971 char_u *l;
4972
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004973 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 if (l != NULL)
4975 {
4976 vim_free(repl);
4977 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 }
4979 }
4980
4981 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4982 vim_free(repl);
4983 if (p == NULL)
4984 return FAIL;
4985 }
4986
4987 /*
4988 * One file argument: Expand wildcards.
4989 * Don't do this with ":r !command" or ":w !command".
4990 */
4991 if ((eap->argt & NOSPC) && !eap->usefilter)
4992 {
4993 /*
4994 * May do this twice:
4995 * 1. Replace environment variables.
4996 * 2. Replace any other wildcards, remove backslashes.
4997 */
4998 for (n = 1; n <= 2; ++n)
4999 {
5000 if (n == 2)
5001 {
5002#ifdef UNIX
5003 /*
5004 * Only for Unix we check for more than one file name.
5005 * For other systems spaces are considered to be part
5006 * of the file name.
5007 * Only check here if there is no wildcard, otherwise
5008 * ExpandOne() will check for errors. This allows
5009 * ":e `ls ve*.c`" on Unix.
5010 */
5011 if (!has_wildcards)
5012 for (p = eap->arg; *p; ++p)
5013 {
5014 /* skip escaped characters */
5015 if (p[1] && (*p == '\\' || *p == Ctrl_V))
5016 ++p;
5017 else if (vim_iswhite(*p))
5018 {
5019 *errormsgp = (char_u *)_("E172: Only one file name allowed");
5020 return FAIL;
5021 }
5022 }
5023#endif
5024
5025 /*
5026 * Halve the number of backslashes (this is Vi compatible).
5027 * For Unix and OS/2, when wildcards are expanded, this is
5028 * done by ExpandOne() below.
5029 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005030#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 if (!has_wildcards)
5032#endif
5033 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 }
5035
5036 if (has_wildcards)
5037 {
5038 if (n == 1)
5039 {
5040 /*
5041 * First loop: May expand environment variables. This
5042 * can be done much faster with expand_env() than with
5043 * something else (e.g., calling a shell).
5044 * After expanding environment variables, check again
5045 * if there are still wildcards present.
5046 */
5047 if (vim_strchr(eap->arg, '$') != NULL
5048 || vim_strchr(eap->arg, '~') != NULL)
5049 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005050 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005051 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 has_wildcards = mch_has_wildcard(NameBuff);
5053 p = NameBuff;
5054 }
5055 else
5056 p = NULL;
5057 }
5058 else /* n == 2 */
5059 {
5060 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005061 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062
5063 ExpandInit(&xpc);
5064 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005065 if (p_wic)
5066 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005068 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 if (p == NULL)
5070 return FAIL;
5071 }
5072 if (p != NULL)
5073 {
5074 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5075 p, cmdlinep);
5076 if (n == 2) /* p came from ExpandOne() */
5077 vim_free(p);
5078 }
5079 }
5080 }
5081 }
5082 return OK;
5083}
5084
5085/*
5086 * Replace part of the command line, keeping eap->cmd, eap->arg and
5087 * eap->nextcmd correct.
5088 * "src" points to the part that is to be replaced, of length "srclen".
5089 * "repl" is the replacement string.
5090 * Returns a pointer to the character after the replaced string.
5091 * Returns NULL for failure.
5092 */
5093 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005094repl_cmdline(
5095 exarg_T *eap,
5096 char_u *src,
5097 int srclen,
5098 char_u *repl,
5099 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100{
5101 int len;
5102 int i;
5103 char_u *new_cmdline;
5104
5105 /*
5106 * The new command line is build in new_cmdline[].
5107 * First allocate it.
5108 * Careful: a "+cmd" argument may have been NUL terminated.
5109 */
5110 len = (int)STRLEN(repl);
5111 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005112 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5114 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5115 return NULL; /* out of memory! */
5116
5117 /*
5118 * Copy the stuff before the expanded part.
5119 * Copy the expanded stuff.
5120 * Copy what came after the expanded part.
5121 * Copy the next commands, if there are any.
5122 */
5123 i = (int)(src - *cmdlinep); /* length of part before match */
5124 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005125
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 mch_memmove(new_cmdline + i, repl, (size_t)len);
5127 i += len; /* remember the end of the string */
5128 STRCPY(new_cmdline + i, src + srclen);
5129 src = new_cmdline + i; /* remember where to continue */
5130
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005131 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 {
5133 i = (int)STRLEN(new_cmdline) + 1;
5134 STRCPY(new_cmdline + i, eap->nextcmd);
5135 eap->nextcmd = new_cmdline + i;
5136 }
5137 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5138 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5139 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5140 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5141 vim_free(*cmdlinep);
5142 *cmdlinep = new_cmdline;
5143
5144 return src;
5145}
5146
5147/*
5148 * Check for '|' to separate commands and '"' to start comments.
5149 */
5150 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005151separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152{
5153 char_u *p;
5154
Bram Moolenaar86b68352004-12-27 21:59:20 +00005155#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005156 p = skip_grep_pat(eap);
5157#else
5158 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005159#endif
5160
5161 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 {
5163 if (*p == Ctrl_V)
5164 {
5165 if (eap->argt & (USECTRLV | XFILE))
5166 ++p; /* skip CTRL-V and next char */
5167 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005168 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005169 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 if (*p == NUL) /* stop at NUL after CTRL-V */
5171 break;
5172 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005173
5174#ifdef FEAT_EVAL
5175 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005176 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005177 {
5178 p += 2;
5179 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005180 }
5181#endif
5182
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 /* Check for '"': start of comment or '|': next command */
5184 /* :@" and :*" do not start a comment!
5185 * :redir @" doesn't either. */
5186 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5187 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5188 || p != eap->arg)
5189 && (eap->cmdidx != CMD_redir
5190 || p != eap->arg + 1 || p[-1] != '@'))
5191 || *p == '|' || *p == '\n')
5192 {
5193 /*
5194 * We remove the '\' before the '|', unless USECTRLV is used
5195 * AND 'b' is present in 'cpoptions'.
5196 */
5197 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5198 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5199 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005200 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 --p;
5202 }
5203 else
5204 {
5205 eap->nextcmd = check_nextcmd(p);
5206 *p = NUL;
5207 break;
5208 }
5209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005211
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5213 del_trailing_spaces(eap->arg);
5214}
5215
5216/*
5217 * get + command from ex argument
5218 */
5219 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005220getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221{
5222 char_u *arg = *argp;
5223 char_u *command = NULL;
5224
5225 if (*arg == '+') /* +[command] */
5226 {
5227 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005228 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 command = dollar_command;
5230 else
5231 {
5232 command = arg;
5233 arg = skip_cmd_arg(command, TRUE);
5234 if (*arg != NUL)
5235 *arg++ = NUL; /* terminate command with NUL */
5236 }
5237
5238 arg = skipwhite(arg); /* skip over spaces */
5239 *argp = arg;
5240 }
5241 return command;
5242}
5243
5244/*
5245 * Find end of "+command" argument. Skip over "\ " and "\\".
5246 */
5247 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005248skip_cmd_arg(
5249 char_u *p,
5250 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251{
5252 while (*p && !vim_isspace(*p))
5253 {
5254 if (*p == '\\' && p[1] != NUL)
5255 {
5256 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005257 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 else
5259 ++p;
5260 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005261 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 }
5263 return p;
5264}
5265
5266/*
5267 * Get "++opt=arg" argument.
5268 * Return FAIL or OK.
5269 */
5270 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005271getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272{
5273 char_u *arg = eap->arg + 2;
5274 int *pp = NULL;
5275#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005276 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 char_u *p;
5278#endif
5279
5280 /* ":edit ++[no]bin[ary] file" */
5281 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5282 {
5283 if (*arg == 'n')
5284 {
5285 arg += 2;
5286 eap->force_bin = FORCE_NOBIN;
5287 }
5288 else
5289 eap->force_bin = FORCE_BIN;
5290 if (!checkforcmd(&arg, "binary", 3))
5291 return FAIL;
5292 eap->arg = skipwhite(arg);
5293 return OK;
5294 }
5295
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005296 /* ":read ++edit file" */
5297 if (STRNCMP(arg, "edit", 4) == 0)
5298 {
5299 eap->read_edit = TRUE;
5300 eap->arg = skipwhite(arg + 4);
5301 return OK;
5302 }
5303
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 if (STRNCMP(arg, "ff", 2) == 0)
5305 {
5306 arg += 2;
5307 pp = &eap->force_ff;
5308 }
5309 else if (STRNCMP(arg, "fileformat", 10) == 0)
5310 {
5311 arg += 10;
5312 pp = &eap->force_ff;
5313 }
5314#ifdef FEAT_MBYTE
5315 else if (STRNCMP(arg, "enc", 3) == 0)
5316 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005317 if (STRNCMP(arg, "encoding", 8) == 0)
5318 arg += 8;
5319 else
5320 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 pp = &eap->force_enc;
5322 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005323 else if (STRNCMP(arg, "bad", 3) == 0)
5324 {
5325 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005326 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328#endif
5329
5330 if (pp == NULL || *arg != '=')
5331 return FAIL;
5332
5333 ++arg;
5334 *pp = (int)(arg - eap->cmd);
5335 arg = skip_cmd_arg(arg, FALSE);
5336 eap->arg = skipwhite(arg);
5337 *arg = NUL;
5338
5339#ifdef FEAT_MBYTE
5340 if (pp == &eap->force_ff)
5341 {
5342#endif
5343 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5344 return FAIL;
5345#ifdef FEAT_MBYTE
5346 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005347 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 {
5349 /* Make 'fileencoding' lower case. */
5350 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5351 *p = TOLOWER_ASC(*p);
5352 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005353 else
5354 {
5355 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5356 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005357 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005358 if (STRICMP(p, "keep") == 0)
5359 eap->bad_char = BAD_KEEP;
5360 else if (STRICMP(p, "drop") == 0)
5361 eap->bad_char = BAD_DROP;
5362 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5363 eap->bad_char = *p;
5364 else
5365 return FAIL;
5366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367#endif
5368
5369 return OK;
5370}
5371
5372/*
5373 * ":abbreviate" and friends.
5374 */
5375 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005376ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377{
5378 do_exmap(eap, TRUE); /* almost the same as mapping */
5379}
5380
5381/*
5382 * ":map" and friends.
5383 */
5384 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005385ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386{
5387 /*
5388 * If we are sourcing .exrc or .vimrc in current directory we
5389 * print the mappings for security reasons.
5390 */
5391 if (secure)
5392 {
5393 secure = 2;
5394 msg_outtrans(eap->cmd);
5395 msg_putchar('\n');
5396 }
5397 do_exmap(eap, FALSE);
5398}
5399
5400/*
5401 * ":unmap" and friends.
5402 */
5403 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005404ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405{
5406 do_exmap(eap, FALSE);
5407}
5408
5409/*
5410 * ":mapclear" and friends.
5411 */
5412 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005413ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414{
5415 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5416}
5417
5418/*
5419 * ":abclear" and friends.
5420 */
5421 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005422ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423{
5424 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5425}
5426
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005427#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005429ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430{
5431 /*
5432 * Disallow auto commands from .exrc and .vimrc in current
5433 * directory for security reasons.
5434 */
5435 if (secure)
5436 {
5437 secure = 2;
5438 eap->errmsg = e_curdir;
5439 }
5440 else if (eap->cmdidx == CMD_autocmd)
5441 do_autocmd(eap->arg, eap->forceit);
5442 else
5443 do_augroup(eap->arg, eap->forceit);
5444}
5445
5446/*
5447 * ":doautocmd": Apply the automatic commands to the current buffer.
5448 */
5449 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005450ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005452 char_u *arg = eap->arg;
5453 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005454 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005455
Bram Moolenaar1610d052016-06-09 22:53:01 +02005456 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5457 /* Only when there is no <nomodeline>. */
5458 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005459 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460}
5461#endif
5462
5463#ifdef FEAT_LISTCMDS
5464/*
5465 * :[N]bunload[!] [N] [bufname] unload buffer
5466 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5467 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5468 */
5469 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005470ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471{
5472 eap->errmsg = do_bufdel(
5473 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5474 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5475 : DOBUF_UNLOAD, eap->arg,
5476 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5477}
5478
5479/*
5480 * :[N]buffer [N] to buffer N
5481 * :[N]sbuffer [N] to buffer N
5482 */
5483 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005484ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485{
5486 if (*eap->arg)
5487 eap->errmsg = e_trailing;
5488 else
5489 {
5490 if (eap->addr_count == 0) /* default is current buffer */
5491 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5492 else
5493 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005494 if (eap->do_ecmd_cmd != NULL)
5495 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 }
5497}
5498
5499/*
5500 * :[N]bmodified [N] to next mod. buffer
5501 * :[N]sbmodified [N] to next mod. buffer
5502 */
5503 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005504ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505{
5506 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005507 if (eap->do_ecmd_cmd != NULL)
5508 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509}
5510
5511/*
5512 * :[N]bnext [N] to next buffer
5513 * :[N]sbnext [N] split and to next buffer
5514 */
5515 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005516ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517{
5518 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005519 if (eap->do_ecmd_cmd != NULL)
5520 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521}
5522
5523/*
5524 * :[N]bNext [N] to previous buffer
5525 * :[N]bprevious [N] to previous buffer
5526 * :[N]sbNext [N] split and to previous buffer
5527 * :[N]sbprevious [N] split and to previous buffer
5528 */
5529 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005530ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531{
5532 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005533 if (eap->do_ecmd_cmd != NULL)
5534 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535}
5536
5537/*
5538 * :brewind to first buffer
5539 * :bfirst to first buffer
5540 * :sbrewind split and to first buffer
5541 * :sbfirst split and to first buffer
5542 */
5543 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005544ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545{
5546 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005547 if (eap->do_ecmd_cmd != NULL)
5548 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549}
5550
5551/*
5552 * :blast to last buffer
5553 * :sblast split and to last buffer
5554 */
5555 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005556ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557{
5558 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005559 if (eap->do_ecmd_cmd != NULL)
5560 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561}
5562#endif
5563
5564 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005565ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566{
5567 return (c == NUL || c == '|' || c == '"' || c == '\n');
5568}
5569
5570#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5571 || defined(PROTO)
5572/*
5573 * Return the next command, after the first '|' or '\n'.
5574 * Return NULL if not found.
5575 */
5576 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005577find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578{
5579 while (*p != '|' && *p != '\n')
5580 {
5581 if (*p == NUL)
5582 return NULL;
5583 ++p;
5584 }
5585 return (p + 1);
5586}
5587#endif
5588
5589/*
5590 * Check if *p is a separator between Ex commands.
5591 * Return NULL if it isn't, (p + 1) if it is.
5592 */
5593 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005594check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595{
5596 p = skipwhite(p);
5597 if (*p == '|' || *p == '\n')
5598 return (p + 1);
5599 else
5600 return NULL;
5601}
5602
5603/*
5604 * - if there are more files to edit
5605 * - and this is the last window
5606 * - and forceit not used
5607 * - and not repeated twice on a row
5608 * return FAIL and give error message if 'message' TRUE
5609 * return OK otherwise
5610 */
5611 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005612check_more(
5613 int message, /* when FALSE check only, no messages */
5614 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615{
5616 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5617
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005618 if (!forceit && only_one_window()
5619 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 {
5621 if (message)
5622 {
5623#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5624 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5625 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005626 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627
5628 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005629 vim_strncpy(buff,
5630 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005631 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005633 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 _("%d more files to edit. Quit anyway?"), n);
5635 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5636 return OK;
5637 return FAIL;
5638 }
5639#endif
5640 if (n == 1)
5641 EMSG(_("E173: 1 more file to edit"));
5642 else
5643 EMSGN(_("E173: %ld more files to edit"), n);
5644 quitmore = 2; /* next try to quit is allowed */
5645 }
5646 return FAIL;
5647 }
5648 return OK;
5649}
5650
5651#ifdef FEAT_CMDL_COMPL
5652/*
5653 * Function given to ExpandGeneric() to obtain the list of command names.
5654 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005656get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657{
5658 if (idx >= (int)CMD_SIZE)
5659# ifdef FEAT_USR_CMDS
5660 return get_user_command_name(idx);
5661# else
5662 return NULL;
5663# endif
5664 return cmdnames[idx].cmd_name;
5665}
5666#endif
5667
5668#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005669static 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);
5670static void uc_list(char_u *name, size_t name_len);
5671static 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);
5672static char_u *uc_split_args(char_u *arg, size_t *lenp);
5673static 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 +00005674
5675 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005676uc_add_command(
5677 char_u *name,
5678 size_t name_len,
5679 char_u *rep,
5680 long argt,
5681 long def,
5682 int flags,
5683 int compl,
5684 char_u *compl_arg,
5685 int addr_type,
5686 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687{
5688 ucmd_T *cmd = NULL;
5689 char_u *p;
5690 int i;
5691 int cmp = 1;
5692 char_u *rep_buf = NULL;
5693 garray_T *gap;
5694
Bram Moolenaar9c102382006-05-03 21:26:49 +00005695 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 if (rep_buf == NULL)
5697 {
5698 /* Can't replace termcodes - try using the string as is */
5699 rep_buf = vim_strsave(rep);
5700
5701 /* Give up if out of memory */
5702 if (rep_buf == NULL)
5703 return FAIL;
5704 }
5705
5706 /* get address of growarray: global or in curbuf */
5707 if (flags & UC_BUFFER)
5708 {
5709 gap = &curbuf->b_ucmds;
5710 if (gap->ga_itemsize == 0)
5711 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5712 }
5713 else
5714 gap = &ucmds;
5715
5716 /* Search for the command in the already defined commands. */
5717 for (i = 0; i < gap->ga_len; ++i)
5718 {
5719 size_t len;
5720
5721 cmd = USER_CMD_GA(gap, i);
5722 len = STRLEN(cmd->uc_name);
5723 cmp = STRNCMP(name, cmd->uc_name, name_len);
5724 if (cmp == 0)
5725 {
5726 if (name_len < len)
5727 cmp = -1;
5728 else if (name_len > len)
5729 cmp = 1;
5730 }
5731
5732 if (cmp == 0)
5733 {
5734 if (!force)
5735 {
5736 EMSG(_("E174: Command already exists: add ! to replace it"));
5737 goto fail;
5738 }
5739
5740 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005741 cmd->uc_rep = NULL;
5742#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5743 vim_free(cmd->uc_compl_arg);
5744 cmd->uc_compl_arg = NULL;
5745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 break;
5747 }
5748
5749 /* Stop as soon as we pass the name to add */
5750 if (cmp < 0)
5751 break;
5752 }
5753
5754 /* Extend the array unless we're replacing an existing command */
5755 if (cmp != 0)
5756 {
5757 if (ga_grow(gap, 1) != OK)
5758 goto fail;
5759 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5760 goto fail;
5761
5762 cmd = USER_CMD_GA(gap, i);
5763 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5764
5765 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766
5767 cmd->uc_name = p;
5768 }
5769
5770 cmd->uc_rep = rep_buf;
5771 cmd->uc_argt = argt;
5772 cmd->uc_def = def;
5773 cmd->uc_compl = compl;
5774#ifdef FEAT_EVAL
5775 cmd->uc_scriptID = current_SID;
5776# ifdef FEAT_CMDL_COMPL
5777 cmd->uc_compl_arg = compl_arg;
5778# endif
5779#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005780 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781
5782 return OK;
5783
5784fail:
5785 vim_free(rep_buf);
5786#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5787 vim_free(compl_arg);
5788#endif
5789 return FAIL;
5790}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005793#if defined(FEAT_USR_CMDS)
5794static struct
5795{
5796 int expand;
5797 char *name;
5798} addr_type_complete[] =
5799{
5800 {ADDR_ARGUMENTS, "arguments"},
5801 {ADDR_LINES, "lines"},
5802 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5803 {ADDR_TABS, "tabs"},
5804 {ADDR_BUFFERS, "buffers"},
5805 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005806 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005807 {-1, NULL}
5808};
5809#endif
5810
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005811#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812/*
5813 * List of names for completion for ":command" with the EXPAND_ flag.
5814 * Must be alphabetical for completion.
5815 */
5816static struct
5817{
5818 int expand;
5819 char *name;
5820} command_complete[] =
5821{
5822 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005823 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005825 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005827 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005828#if defined(FEAT_CSCOPE)
5829 {EXPAND_CSCOPE, "cscope"},
5830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5832 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005833 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834#endif
5835 {EXPAND_DIRECTORIES, "dir"},
5836 {EXPAND_ENV_VARS, "environment"},
5837 {EXPAND_EVENTS, "event"},
5838 {EXPAND_EXPRESSION, "expression"},
5839 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005840 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005841 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 {EXPAND_FUNCTIONS, "function"},
5843 {EXPAND_HELP, "help"},
5844 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005845#if defined(FEAT_CMDHIST)
5846 {EXPAND_HISTORY, "history"},
5847#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005848#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005849 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005850 {EXPAND_LOCALES, "locale"},
5851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 {EXPAND_MAPPINGS, "mapping"},
5853 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005854 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005855#if defined(FEAT_PROFILE)
5856 {EXPAND_SYNTIME, "syntime"},
5857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005859 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005860 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005861#if defined(FEAT_SIGNS)
5862 {EXPAND_SIGN, "sign"},
5863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 {EXPAND_TAGS, "tag"},
5865 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005866 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 {EXPAND_USER_VARS, "var"},
5868 {0, NULL}
5869};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005872#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005874uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875{
5876 int i, j;
5877 int found = FALSE;
5878 ucmd_T *cmd;
5879 int len;
5880 long a;
5881 garray_T *gap;
5882
5883 gap = &curbuf->b_ucmds;
5884 for (;;)
5885 {
5886 for (i = 0; i < gap->ga_len; ++i)
5887 {
5888 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005889 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890
5891 /* Skip commands which don't match the requested prefix */
5892 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5893 continue;
5894
5895 /* Put out the title first time */
5896 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005897 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 found = TRUE;
5899 msg_putchar('\n');
5900 if (got_int)
5901 break;
5902
5903 /* Special cases */
5904 msg_putchar(a & BANG ? '!' : ' ');
5905 msg_putchar(a & REGSTR ? '"' : ' ');
5906 msg_putchar(gap != &ucmds ? 'b' : ' ');
5907 msg_putchar(' ');
5908
5909 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5910 len = (int)STRLEN(cmd->uc_name) + 4;
5911
5912 do {
5913 msg_putchar(' ');
5914 ++len;
5915 } while (len < 16);
5916
5917 len = 0;
5918
5919 /* Arguments */
5920 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5921 {
5922 case 0: IObuff[len++] = '0'; break;
5923 case (EXTRA): IObuff[len++] = '*'; break;
5924 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5925 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5926 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5927 }
5928
5929 do {
5930 IObuff[len++] = ' ';
5931 } while (len < 5);
5932
5933 /* Range */
5934 if (a & (RANGE|COUNT))
5935 {
5936 if (a & COUNT)
5937 {
5938 /* -count=N */
5939 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5940 len += (int)STRLEN(IObuff + len);
5941 }
5942 else if (a & DFLALL)
5943 IObuff[len++] = '%';
5944 else if (cmd->uc_def >= 0)
5945 {
5946 /* -range=N */
5947 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5948 len += (int)STRLEN(IObuff + len);
5949 }
5950 else
5951 IObuff[len++] = '.';
5952 }
5953
5954 do {
5955 IObuff[len++] = ' ';
5956 } while (len < 11);
5957
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005958 /* Address Type */
5959 for (j = 0; addr_type_complete[j].expand != -1; ++j)
5960 if (addr_type_complete[j].expand != ADDR_LINES
5961 && addr_type_complete[j].expand == cmd->uc_addr_type)
5962 {
5963 STRCPY(IObuff + len, addr_type_complete[j].name);
5964 len += (int)STRLEN(IObuff + len);
5965 break;
5966 }
5967
5968 do {
5969 IObuff[len++] = ' ';
5970 } while (len < 21);
5971
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 /* Completion */
5973 for (j = 0; command_complete[j].expand != 0; ++j)
5974 if (command_complete[j].expand == cmd->uc_compl)
5975 {
5976 STRCPY(IObuff + len, command_complete[j].name);
5977 len += (int)STRLEN(IObuff + len);
5978 break;
5979 }
5980
5981 do {
5982 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005983 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984
5985 IObuff[len] = '\0';
5986 msg_outtrans(IObuff);
5987
5988 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005989#ifdef FEAT_EVAL
5990 if (p_verbose > 0)
5991 last_set_msg(cmd->uc_scriptID);
5992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 out_flush();
5994 ui_breakcheck();
5995 if (got_int)
5996 break;
5997 }
5998 if (gap == &ucmds || i < gap->ga_len)
5999 break;
6000 gap = &ucmds;
6001 }
6002
6003 if (!found)
6004 MSG(_("No user-defined commands found"));
6005}
6006
6007 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006008uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009{
6010 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6011 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6012 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6013 0xb9, 0x7f, 0};
6014 int i;
6015
6016 for (i = 0; fcmd[i]; ++i)
6017 IObuff[i] = fcmd[i] - 0x40;
6018 IObuff[i] = 0;
6019 return IObuff;
6020}
6021
6022 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006023uc_scan_attr(
6024 char_u *attr,
6025 size_t len,
6026 long *argt,
6027 long *def,
6028 int *flags,
6029 int *compl,
6030 char_u **compl_arg,
6031 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032{
6033 char_u *p;
6034
6035 if (len == 0)
6036 {
6037 EMSG(_("E175: No attribute specified"));
6038 return FAIL;
6039 }
6040
6041 /* First, try the simple attributes (no arguments) */
6042 if (STRNICMP(attr, "bang", len) == 0)
6043 *argt |= BANG;
6044 else if (STRNICMP(attr, "buffer", len) == 0)
6045 *flags |= UC_BUFFER;
6046 else if (STRNICMP(attr, "register", len) == 0)
6047 *argt |= REGSTR;
6048 else if (STRNICMP(attr, "bar", len) == 0)
6049 *argt |= TRLBAR;
6050 else
6051 {
6052 int i;
6053 char_u *val = NULL;
6054 size_t vallen = 0;
6055 size_t attrlen = len;
6056
6057 /* Look for the attribute name - which is the part before any '=' */
6058 for (i = 0; i < (int)len; ++i)
6059 {
6060 if (attr[i] == '=')
6061 {
6062 val = &attr[i + 1];
6063 vallen = len - i - 1;
6064 attrlen = i;
6065 break;
6066 }
6067 }
6068
6069 if (STRNICMP(attr, "nargs", attrlen) == 0)
6070 {
6071 if (vallen == 1)
6072 {
6073 if (*val == '0')
6074 /* Do nothing - this is the default */;
6075 else if (*val == '1')
6076 *argt |= (EXTRA | NOSPC | NEEDARG);
6077 else if (*val == '*')
6078 *argt |= EXTRA;
6079 else if (*val == '?')
6080 *argt |= (EXTRA | NOSPC);
6081 else if (*val == '+')
6082 *argt |= (EXTRA | NEEDARG);
6083 else
6084 goto wrong_nargs;
6085 }
6086 else
6087 {
6088wrong_nargs:
6089 EMSG(_("E176: Invalid number of arguments"));
6090 return FAIL;
6091 }
6092 }
6093 else if (STRNICMP(attr, "range", attrlen) == 0)
6094 {
6095 *argt |= RANGE;
6096 if (vallen == 1 && *val == '%')
6097 *argt |= DFLALL;
6098 else if (val != NULL)
6099 {
6100 p = val;
6101 if (*def >= 0)
6102 {
6103two_count:
6104 EMSG(_("E177: Count cannot be specified twice"));
6105 return FAIL;
6106 }
6107
6108 *def = getdigits(&p);
6109 *argt |= (ZEROR | NOTADR);
6110
6111 if (p != val + vallen || vallen == 0)
6112 {
6113invalid_count:
6114 EMSG(_("E178: Invalid default value for count"));
6115 return FAIL;
6116 }
6117 }
6118 }
6119 else if (STRNICMP(attr, "count", attrlen) == 0)
6120 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006121 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122
6123 if (val != NULL)
6124 {
6125 p = val;
6126 if (*def >= 0)
6127 goto two_count;
6128
6129 *def = getdigits(&p);
6130
6131 if (p != val + vallen)
6132 goto invalid_count;
6133 }
6134
6135 if (*def < 0)
6136 *def = 0;
6137 }
6138 else if (STRNICMP(attr, "complete", attrlen) == 0)
6139 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 if (val == NULL)
6141 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006142 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 return FAIL;
6144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006146 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6147 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006150 else if (STRNICMP(attr, "addr", attrlen) == 0)
6151 {
6152 *argt |= RANGE;
6153 if (val == NULL)
6154 {
6155 EMSG(_("E179: argument required for -addr"));
6156 return FAIL;
6157 }
6158 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6159 == FAIL)
6160 return FAIL;
6161 if (addr_type_arg != ADDR_LINES)
6162 *argt |= (ZEROR | NOTADR) ;
6163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 else
6165 {
6166 char_u ch = attr[len];
6167 attr[len] = '\0';
6168 EMSG2(_("E181: Invalid attribute: %s"), attr);
6169 attr[len] = ch;
6170 return FAIL;
6171 }
6172 }
6173
6174 return OK;
6175}
6176
Bram Moolenaara850a712009-01-28 14:42:59 +00006177/*
6178 * ":command ..."
6179 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006181ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182{
6183 char_u *name;
6184 char_u *end;
6185 char_u *p;
6186 long argt = 0;
6187 long def = -1;
6188 int flags = 0;
6189 int compl = EXPAND_NOTHING;
6190 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006191 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006193 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194
6195 p = eap->arg;
6196
6197 /* Check for attributes */
6198 while (*p == '-')
6199 {
6200 ++p;
6201 end = skiptowhite(p);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006202 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 == FAIL)
6204 return;
6205 p = skipwhite(end);
6206 }
6207
6208 /* Get the name (if any) and skip to the following argument */
6209 name = p;
6210 if (ASCII_ISALPHA(*p))
6211 while (ASCII_ISALNUM(*p))
6212 ++p;
6213 if (!ends_excmd(*p) && !vim_iswhite(*p))
6214 {
6215 EMSG(_("E182: Invalid command name"));
6216 return;
6217 }
6218 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006219 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220
6221 /* If there is nothing after the name, and no attributes were specified,
6222 * we are listing commands
6223 */
6224 p = skipwhite(end);
6225 if (!has_attr && ends_excmd(*p))
6226 {
6227 uc_list(name, end - name);
6228 }
6229 else if (!ASCII_ISUPPER(*name))
6230 {
6231 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6232 return;
6233 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006234 else if ((name_len == 1 && *name == 'X')
6235 || (name_len <= 4
6236 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6237 {
6238 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6239 return;
6240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 else
6242 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006243 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244}
6245
6246/*
6247 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 * Clear all user commands, global and for current buffer.
6249 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006250 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006251ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252{
6253 uc_clear(&ucmds);
6254 uc_clear(&curbuf->b_ucmds);
6255}
6256
6257/*
6258 * Clear all user commands for "gap".
6259 */
6260 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006261uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262{
6263 int i;
6264 ucmd_T *cmd;
6265
6266 for (i = 0; i < gap->ga_len; ++i)
6267 {
6268 cmd = USER_CMD_GA(gap, i);
6269 vim_free(cmd->uc_name);
6270 vim_free(cmd->uc_rep);
6271# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6272 vim_free(cmd->uc_compl_arg);
6273# endif
6274 }
6275 ga_clear(gap);
6276}
6277
6278 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006279ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280{
6281 int i = 0;
6282 ucmd_T *cmd = NULL;
6283 int cmp = -1;
6284 garray_T *gap;
6285
6286 gap = &curbuf->b_ucmds;
6287 for (;;)
6288 {
6289 for (i = 0; i < gap->ga_len; ++i)
6290 {
6291 cmd = USER_CMD_GA(gap, i);
6292 cmp = STRCMP(eap->arg, cmd->uc_name);
6293 if (cmp <= 0)
6294 break;
6295 }
6296 if (gap == &ucmds || cmp == 0)
6297 break;
6298 gap = &ucmds;
6299 }
6300
6301 if (cmp != 0)
6302 {
6303 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6304 return;
6305 }
6306
6307 vim_free(cmd->uc_name);
6308 vim_free(cmd->uc_rep);
6309# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6310 vim_free(cmd->uc_compl_arg);
6311# endif
6312
6313 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314
6315 if (i < gap->ga_len)
6316 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6317}
6318
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006319/*
6320 * split and quote args for <f-args>
6321 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006323uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324{
6325 char_u *buf;
6326 char_u *p;
6327 char_u *q;
6328 int len;
6329
6330 /* Precalculate length */
6331 p = arg;
6332 len = 2; /* Initial and final quotes */
6333
6334 while (*p)
6335 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006336 if (p[0] == '\\' && p[1] == '\\')
6337 {
6338 len += 2;
6339 p += 2;
6340 }
6341 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 {
6343 len += 1;
6344 p += 2;
6345 }
6346 else if (*p == '\\' || *p == '"')
6347 {
6348 len += 2;
6349 p += 1;
6350 }
6351 else if (vim_iswhite(*p))
6352 {
6353 p = skipwhite(p);
6354 if (*p == NUL)
6355 break;
6356 len += 3; /* "," */
6357 }
6358 else
6359 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006360#ifdef FEAT_MBYTE
6361 int charlen = (*mb_ptr2len)(p);
6362 len += charlen;
6363 p += charlen;
6364#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 ++len;
6366 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 }
6369 }
6370
6371 buf = alloc(len + 1);
6372 if (buf == NULL)
6373 {
6374 *lenp = 0;
6375 return buf;
6376 }
6377
6378 p = arg;
6379 q = buf;
6380 *q++ = '"';
6381 while (*p)
6382 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006383 if (p[0] == '\\' && p[1] == '\\')
6384 {
6385 *q++ = '\\';
6386 *q++ = '\\';
6387 p += 2;
6388 }
6389 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 {
6391 *q++ = p[1];
6392 p += 2;
6393 }
6394 else if (*p == '\\' || *p == '"')
6395 {
6396 *q++ = '\\';
6397 *q++ = *p++;
6398 }
6399 else if (vim_iswhite(*p))
6400 {
6401 p = skipwhite(p);
6402 if (*p == NUL)
6403 break;
6404 *q++ = '"';
6405 *q++ = ',';
6406 *q++ = '"';
6407 }
6408 else
6409 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006410 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 }
6412 }
6413 *q++ = '"';
6414 *q = 0;
6415
6416 *lenp = len;
6417 return buf;
6418}
6419
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006420 static size_t
6421add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6422{
6423 size_t result;
6424
6425 result = STRLEN(mod_str);
6426 if (*multi_mods)
6427 result += 1;
6428 if (buf != NULL)
6429 {
6430 if (*multi_mods)
6431 STRCAT(buf, " ");
6432 STRCAT(buf, mod_str);
6433 }
6434
6435 *multi_mods = 1;
6436
6437 return result;
6438}
6439
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440/*
6441 * Check for a <> code in a user command.
6442 * "code" points to the '<'. "len" the length of the <> (inclusive).
6443 * "buf" is where the result is to be added.
6444 * "split_buf" points to a buffer used for splitting, caller should free it.
6445 * "split_len" is the length of what "split_buf" contains.
6446 * Returns the length of the replacement, which has been added to "buf".
6447 * Returns -1 if there was no match, and only the "<" has been copied.
6448 */
6449 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006450uc_check_code(
6451 char_u *code,
6452 size_t len,
6453 char_u *buf,
6454 ucmd_T *cmd, /* the user command we're expanding */
6455 exarg_T *eap, /* ex arguments */
6456 char_u **split_buf,
6457 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458{
6459 size_t result = 0;
6460 char_u *p = code + 1;
6461 size_t l = len - 2;
6462 int quote = 0;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006463 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_MODS,
6464 ct_REGISTER, ct_LT, ct_NONE } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465
6466 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6467 {
6468 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6469 p += 2;
6470 l -= 2;
6471 }
6472
Bram Moolenaar371d5402006-03-20 21:47:49 +00006473 ++l;
6474 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006476 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006478 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006480 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006482 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006484 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006486 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006488 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006490 else if (STRNICMP(p, "mods>", l) == 0)
6491 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492
6493 switch (type)
6494 {
6495 case ct_ARGS:
6496 /* Simple case first */
6497 if (*eap->arg == NUL)
6498 {
6499 if (quote == 1)
6500 {
6501 result = 2;
6502 if (buf != NULL)
6503 STRCPY(buf, "''");
6504 }
6505 else
6506 result = 0;
6507 break;
6508 }
6509
6510 /* When specified there is a single argument don't split it.
6511 * Works for ":Cmd %" when % is "a b c". */
6512 if ((eap->argt & NOSPC) && quote == 2)
6513 quote = 1;
6514
6515 switch (quote)
6516 {
6517 case 0: /* No quoting, no splitting */
6518 result = STRLEN(eap->arg);
6519 if (buf != NULL)
6520 STRCPY(buf, eap->arg);
6521 break;
6522 case 1: /* Quote, but don't split */
6523 result = STRLEN(eap->arg) + 2;
6524 for (p = eap->arg; *p; ++p)
6525 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006526#ifdef FEAT_MBYTE
6527 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6528 /* DBCS can contain \ in a trail byte, skip the
6529 * double-byte character. */
6530 ++p;
6531 else
6532#endif
6533 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 ++result;
6535 }
6536
6537 if (buf != NULL)
6538 {
6539 *buf++ = '"';
6540 for (p = eap->arg; *p; ++p)
6541 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006542#ifdef FEAT_MBYTE
6543 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6544 /* DBCS can contain \ in a trail byte, copy the
6545 * double-byte character to avoid escaping. */
6546 *buf++ = *p++;
6547 else
6548#endif
6549 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 *buf++ = '\\';
6551 *buf++ = *p;
6552 }
6553 *buf = '"';
6554 }
6555
6556 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006557 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 /* This is hard, so only do it once, and cache the result */
6559 if (*split_buf == NULL)
6560 *split_buf = uc_split_args(eap->arg, split_len);
6561
6562 result = *split_len;
6563 if (buf != NULL && result != 0)
6564 STRCPY(buf, *split_buf);
6565
6566 break;
6567 }
6568 break;
6569
6570 case ct_BANG:
6571 result = eap->forceit ? 1 : 0;
6572 if (quote)
6573 result += 2;
6574 if (buf != NULL)
6575 {
6576 if (quote)
6577 *buf++ = '"';
6578 if (eap->forceit)
6579 *buf++ = '!';
6580 if (quote)
6581 *buf = '"';
6582 }
6583 break;
6584
6585 case ct_LINE1:
6586 case ct_LINE2:
6587 case ct_COUNT:
6588 {
6589 char num_buf[20];
6590 long num = (type == ct_LINE1) ? eap->line1 :
6591 (type == ct_LINE2) ? eap->line2 :
6592 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6593 size_t num_len;
6594
6595 sprintf(num_buf, "%ld", num);
6596 num_len = STRLEN(num_buf);
6597 result = num_len;
6598
6599 if (quote)
6600 result += 2;
6601
6602 if (buf != NULL)
6603 {
6604 if (quote)
6605 *buf++ = '"';
6606 STRCPY(buf, num_buf);
6607 buf += num_len;
6608 if (quote)
6609 *buf = '"';
6610 }
6611
6612 break;
6613 }
6614
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006615 case ct_MODS:
6616 {
6617 int multi_mods = 0;
6618 typedef struct {
6619 int *varp;
6620 char *name;
6621 } mod_entry_T;
6622 static mod_entry_T mod_entries[] = {
6623#ifdef FEAT_BROWSE_CMD
6624 {&cmdmod.browse, "browse"},
6625#endif
6626#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6627 {&cmdmod.confirm, "confirm"},
6628#endif
6629 {&cmdmod.hide, "hide"},
6630 {&cmdmod.keepalt, "keepalt"},
6631 {&cmdmod.keepjumps, "keepjumps"},
6632 {&cmdmod.keepmarks, "keepmarks"},
6633 {&cmdmod.keeppatterns, "keeppatterns"},
6634 {&cmdmod.lockmarks, "lockmarks"},
6635 {&cmdmod.noswapfile, "noswapfile"},
6636 {NULL, NULL}
6637 };
6638 int i;
6639
6640 result = quote ? 2 : 0;
6641 if (buf != NULL)
6642 {
6643 if (quote)
6644 *buf++ = '"';
6645 *buf = '\0';
6646 }
6647
6648#ifdef FEAT_WINDOWS
6649 /* :aboveleft and :leftabove */
6650 if (cmdmod.split & WSP_ABOVE)
6651 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6652 /* :belowright and :rightbelow */
6653 if (cmdmod.split & WSP_BELOW)
6654 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6655 /* :botright */
6656 if (cmdmod.split & WSP_BOT)
6657 result += add_cmd_modifier(buf, "botright", &multi_mods);
6658#endif
6659
6660 /* the modifiers that are simple flags */
6661 for (i = 0; mod_entries[i].varp != NULL; ++i)
6662 if (*mod_entries[i].varp)
6663 result += add_cmd_modifier(buf, mod_entries[i].name,
6664 &multi_mods);
6665
6666 /* TODO: How to support :noautocmd? */
6667#ifdef HAVE_SANDBOX
6668 /* TODO: How to support :sandbox?*/
6669#endif
6670 /* :silent */
6671 if (msg_silent > 0)
6672 result += add_cmd_modifier(buf,
6673 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
6674#ifdef FEAT_WINDOWS
6675 /* :tab */
6676 if (cmdmod.tab > 0)
6677 result += add_cmd_modifier(buf, "tab", &multi_mods);
6678 /* :topleft */
6679 if (cmdmod.split & WSP_TOP)
6680 result += add_cmd_modifier(buf, "topleft", &multi_mods);
6681#endif
6682 /* TODO: How to support :unsilent?*/
6683 /* :verbose */
6684 if (p_verbose > 0)
6685 result += add_cmd_modifier(buf, "verbose", &multi_mods);
6686#ifdef FEAT_WINDOWS
6687 /* :vertical */
6688 if (cmdmod.split & WSP_VERT)
6689 result += add_cmd_modifier(buf, "vertical", &multi_mods);
6690#endif
6691 if (quote && buf != NULL)
6692 {
6693 buf += result - 2;
6694 *buf = '"';
6695 }
6696 break;
6697 }
6698
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 case ct_REGISTER:
6700 result = eap->regname ? 1 : 0;
6701 if (quote)
6702 result += 2;
6703 if (buf != NULL)
6704 {
6705 if (quote)
6706 *buf++ = '\'';
6707 if (eap->regname)
6708 *buf++ = eap->regname;
6709 if (quote)
6710 *buf = '\'';
6711 }
6712 break;
6713
6714 case ct_LT:
6715 result = 1;
6716 if (buf != NULL)
6717 *buf = '<';
6718 break;
6719
6720 default:
6721 /* Not recognized: just copy the '<' and return -1. */
6722 result = (size_t)-1;
6723 if (buf != NULL)
6724 *buf = '<';
6725 break;
6726 }
6727
6728 return result;
6729}
6730
6731 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006732do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733{
6734 char_u *buf;
6735 char_u *p;
6736 char_u *q;
6737
6738 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006739 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006740 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 size_t len, totlen;
6742
6743 size_t split_len = 0;
6744 char_u *split_buf = NULL;
6745 ucmd_T *cmd;
6746#ifdef FEAT_EVAL
6747 scid_T save_current_SID = current_SID;
6748#endif
6749
6750 if (eap->cmdidx == CMD_USER)
6751 cmd = USER_CMD(eap->useridx);
6752 else
6753 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6754
6755 /*
6756 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006757 * First round: "buf" is NULL, compute length, allocate "buf".
6758 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 */
6760 buf = NULL;
6761 for (;;)
6762 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006763 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006764 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006766
6767 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006769 start = vim_strchr(p, '<');
6770 if (start != NULL)
6771 end = vim_strchr(start + 1, '>');
6772 if (buf != NULL)
6773 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006774 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6775 ;
6776 if (*ksp == K_SPECIAL
6777 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006778 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6779# ifdef FEAT_GUI
6780 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6781# endif
6782 ))
6783 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006784 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006785 * KS_SPECIAL KE_FILLER, like for mappings, but
6786 * do_cmdline() doesn't handle that, so convert it back.
6787 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6788 len = ksp - p;
6789 if (len > 0)
6790 {
6791 mch_memmove(q, p, len);
6792 q += len;
6793 }
6794 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6795 p = ksp + 3;
6796 continue;
6797 }
6798 }
6799
6800 /* break if there no <item> is found */
6801 if (start == NULL || end == NULL)
6802 break;
6803
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 /* Include the '>' */
6805 ++end;
6806
6807 /* Take everything up to the '<' */
6808 len = start - p;
6809 if (buf == NULL)
6810 totlen += len;
6811 else
6812 {
6813 mch_memmove(q, p, len);
6814 q += len;
6815 }
6816
6817 len = uc_check_code(start, end - start, q, cmd, eap,
6818 &split_buf, &split_len);
6819 if (len == (size_t)-1)
6820 {
6821 /* no match, continue after '<' */
6822 p = start + 1;
6823 len = 1;
6824 }
6825 else
6826 p = end;
6827 if (buf == NULL)
6828 totlen += len;
6829 else
6830 q += len;
6831 }
6832 if (buf != NULL) /* second time here, finished */
6833 {
6834 STRCPY(q, p);
6835 break;
6836 }
6837
6838 totlen += STRLEN(p); /* Add on the trailing characters */
6839 buf = alloc((unsigned)(totlen + 1));
6840 if (buf == NULL)
6841 {
6842 vim_free(split_buf);
6843 return;
6844 }
6845 }
6846
6847#ifdef FEAT_EVAL
6848 current_SID = cmd->uc_scriptID;
6849#endif
6850 (void)do_cmdline(buf, eap->getline, eap->cookie,
6851 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6852#ifdef FEAT_EVAL
6853 current_SID = save_current_SID;
6854#endif
6855 vim_free(buf);
6856 vim_free(split_buf);
6857}
6858
6859# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6860 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006861get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862{
6863 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6864}
6865
6866/*
6867 * Function given to ExpandGeneric() to obtain the list of user command names.
6868 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006870get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871{
6872 if (idx < curbuf->b_ucmds.ga_len)
6873 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6874 idx -= curbuf->b_ucmds.ga_len;
6875 if (idx < ucmds.ga_len)
6876 return USER_CMD(idx)->uc_name;
6877 return NULL;
6878}
6879
6880/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006881 * Function given to ExpandGeneric() to obtain the list of user address type names.
6882 */
6883 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006884get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006885{
6886 return (char_u *)addr_type_complete[idx].name;
6887}
6888
6889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 * Function given to ExpandGeneric() to obtain the list of user command
6891 * attributes.
6892 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006894get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895{
6896 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006897 {"addr", "bang", "bar", "buffer", "complete",
6898 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006900 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 return NULL;
6902 return (char_u *)user_cmd_flags[idx];
6903}
6904
6905/*
6906 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6907 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006909get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910{
6911 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6912
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006913 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 return NULL;
6915 return (char_u *)user_cmd_nargs[idx];
6916}
6917
6918/*
6919 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006922get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923{
6924 return (char_u *)command_complete[idx].name;
6925}
6926# endif /* FEAT_CMDL_COMPL */
6927
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006928/*
6929 * Parse address type argument
6930 */
6931 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006932parse_addr_type_arg(
6933 char_u *value,
6934 int vallen,
6935 long *argt,
6936 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006937{
6938 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01006939
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006940 for (i = 0; addr_type_complete[i].expand != -1; ++i)
6941 {
6942 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
6943 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
6944 if (a && b)
6945 {
6946 *addr_type_arg = addr_type_complete[i].expand;
6947 break;
6948 }
6949 }
6950
6951 if (addr_type_complete[i].expand == -1)
6952 {
6953 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01006954
6955 for (i = 0; err[i] != NUL && !vim_iswhite(err[i]); i++)
6956 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006957 err[i] = NUL;
6958 EMSG2(_("E180: Invalid address type value: %s"), err);
6959 return FAIL;
6960 }
6961
6962 if (*addr_type_arg != ADDR_LINES)
6963 *argt |= NOTADR;
6964
6965 return OK;
6966}
6967
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968#endif /* FEAT_USR_CMDS */
6969
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006970#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6971/*
6972 * Parse a completion argument "value[vallen]".
6973 * The detected completion goes in "*complp", argument type in "*argt".
6974 * When there is an argument, for function and user defined completion, it's
6975 * copied to allocated memory and stored in "*compl_arg".
6976 * Returns FAIL if something is wrong.
6977 */
6978 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006979parse_compl_arg(
6980 char_u *value,
6981 int vallen,
6982 int *complp,
6983 long *argt,
6984 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006985{
6986 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006987# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006988 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006989# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006990 int i;
6991 int valend = vallen;
6992
6993 /* Look for any argument part - which is the part after any ',' */
6994 for (i = 0; i < vallen; ++i)
6995 {
6996 if (value[i] == ',')
6997 {
6998 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006999# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007000 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007001# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007002 valend = i;
7003 break;
7004 }
7005 }
7006
7007 for (i = 0; command_complete[i].expand != 0; ++i)
7008 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007009 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007010 && STRNCMP(value, command_complete[i].name, valend) == 0)
7011 {
7012 *complp = command_complete[i].expand;
7013 if (command_complete[i].expand == EXPAND_BUFFERS)
7014 *argt |= BUFNAME;
7015 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7016 || command_complete[i].expand == EXPAND_FILES)
7017 *argt |= XFILE;
7018 break;
7019 }
7020 }
7021
7022 if (command_complete[i].expand == 0)
7023 {
7024 EMSG2(_("E180: Invalid complete value: %s"), value);
7025 return FAIL;
7026 }
7027
7028# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7029 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7030 && arg != NULL)
7031# else
7032 if (arg != NULL)
7033# endif
7034 {
7035 EMSG(_("E468: Completion argument only allowed for custom completion"));
7036 return FAIL;
7037 }
7038
7039# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7040 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7041 && arg == NULL)
7042 {
7043 EMSG(_("E467: Custom completion requires a function argument"));
7044 return FAIL;
7045 }
7046
7047 if (arg != NULL)
7048 *compl_arg = vim_strnsave(arg, (int)arglen);
7049# endif
7050 return OK;
7051}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007052
7053 int
7054cmdcomplete_str_to_type(char_u *complete_str)
7055{
7056 int i;
7057
7058 for (i = 0; command_complete[i].expand != 0; ++i)
7059 if (STRCMP(complete_str, command_complete[i].name) == 0)
7060 return command_complete[i].expand;
7061
7062 return EXPAND_NOTHING;
7063}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007064#endif
7065
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007067ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068{
Bram Moolenaare6850792010-05-14 15:28:44 +02007069 if (*eap->arg == NUL)
7070 {
7071#ifdef FEAT_EVAL
7072 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7073 char_u *p = NULL;
7074
7075 if (expr != NULL)
7076 {
7077 ++emsg_off;
7078 p = eval_to_string(expr, NULL, FALSE);
7079 --emsg_off;
7080 vim_free(expr);
7081 }
7082 if (p != NULL)
7083 {
7084 MSG(p);
7085 vim_free(p);
7086 }
7087 else
7088 MSG("default");
7089#else
7090 MSG(_("unknown"));
7091#endif
7092 }
7093 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007094 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095}
7096
7097 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007098ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099{
7100 if (*eap->arg == NUL && eap->cmd[2] == '!')
7101 MSG(_("Greetings, Vim user!"));
7102 do_highlight(eap->arg, eap->forceit, FALSE);
7103}
7104
7105
7106/*
7107 * Call this function if we thought we were going to exit, but we won't
7108 * (because of an error). May need to restore the terminal mode.
7109 */
7110 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007111not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112{
7113 exiting = FALSE;
7114 settmode(TMODE_RAW);
7115}
7116
7117/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007118 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 */
7120 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007121ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122{
Bram Moolenaarf240e182014-11-27 18:33:02 +01007123#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007124 win_T *wp;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007125#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007126
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127#ifdef FEAT_CMDWIN
7128 if (cmdwin_type != 0)
7129 {
7130 cmdwin_result = Ctrl_C;
7131 return;
7132 }
7133#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007134 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007135 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007136 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007137 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007138 return;
7139 }
Bram Moolenaarf240e182014-11-27 18:33:02 +01007140#ifdef FEAT_WINDOWS
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007141 if (eap->addr_count > 0)
7142 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007143 int wnr = eap->line2;
7144
7145 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7146 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007147 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007148 }
7149 else
Bram Moolenaarf240e182014-11-27 18:33:02 +01007150#endif
7151#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007152 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007153#endif
7154
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007155#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02007156 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007157 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02007158 * being closed (can only happen in autocommands). */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007159 if (curbuf_locked() || (wp->w_buffer->b_nwindows == 1
7160 && wp->w_buffer->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007161 return;
7162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163
7164#ifdef FEAT_NETBEANS_INTG
7165 netbeansForcedQuit = eap->forceit;
7166#endif
7167
7168 /*
7169 * If there are more files or windows we won't exit.
7170 */
7171 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7172 exiting = TRUE;
7173 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007174 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7175 | (eap->forceit ? CCGD_FORCEIT : 0)
7176 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007178 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 {
7180 not_exiting();
7181 }
7182 else
7183 {
7184#ifdef FEAT_WINDOWS
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007185 /* quit last window
7186 * Note: only_one_window() returns true, even so a help window is
7187 * still open. In that case only quit, if no address has been
7188 * specified. Example:
7189 * :h|wincmd w|1q - don't quit
7190 * :h|wincmd w|q - quit
7191 */
7192 if (only_one_window() && (firstwin == lastwin || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193#endif
7194 getout(0);
7195#ifdef FEAT_WINDOWS
7196# ifdef FEAT_GUI
7197 need_mouse_correct = TRUE;
7198# endif
7199 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007200 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201#endif
7202 }
7203}
7204
7205/*
7206 * ":cquit".
7207 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007209ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210{
7211 getout(1); /* this does not always pass on the exit code to the Manx
7212 compiler. why? */
7213}
7214
7215/*
7216 * ":qall": try to quit all windows
7217 */
7218 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007219ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220{
7221# ifdef FEAT_CMDWIN
7222 if (cmdwin_type != 0)
7223 {
7224 if (eap->forceit)
7225 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7226 else
7227 cmdwin_result = K_XF2;
7228 return;
7229 }
7230# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007231
7232 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007233 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007234 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007235 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007236 return;
7237 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007238#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007239 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7240 /* Refuse to quit when locked or when the buffer in the last window is
7241 * being closed (can only happen in autocommands). */
7242 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007243 return;
7244#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007245
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007247 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 getout(0);
7249 not_exiting();
7250}
7251
Bram Moolenaard9967712006-03-11 21:18:15 +00007252#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253/*
7254 * ":close": close current window, unless it is the last one
7255 */
7256 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007257ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007259 win_T *win;
7260 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261# ifdef FEAT_CMDWIN
7262 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007263 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 else
7265# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007266 if (!text_locked()
7267#ifdef FEAT_AUTOCMD
7268 && !curbuf_locked()
7269#endif
7270 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007271 {
7272 if (eap->addr_count == 0)
7273 ex_win_close(eap->forceit, curwin, NULL);
7274 else {
7275 for (win = firstwin; win != NULL; win = win->w_next)
7276 {
7277 winnr++;
7278 if (winnr == eap->line2)
7279 break;
7280 }
7281 if (win == NULL)
7282 win = lastwin;
7283 ex_win_close(eap->forceit, win, NULL);
7284 }
7285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286}
7287
Bram Moolenaard9967712006-03-11 21:18:15 +00007288# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007289/*
7290 * ":pclose": Close any preview window.
7291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007293ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007294{
7295 win_T *win;
7296
7297 for (win = firstwin; win != NULL; win = win->w_next)
7298 if (win->w_p_pvw)
7299 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007300 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007301 break;
7302 }
7303}
Bram Moolenaard9967712006-03-11 21:18:15 +00007304# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007305
Bram Moolenaarf740b292006-02-16 22:11:02 +00007306/*
7307 * Close window "win" and take care of handling closing the last window for a
7308 * modified buffer.
7309 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007310 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007311ex_win_close(
7312 int forceit,
7313 win_T *win,
7314 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315{
7316 int need_hide;
7317 buf_T *buf = win->w_buffer;
7318
7319 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007320 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007322# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 if ((p_confirm || cmdmod.confirm) && p_write)
7324 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007325 bufref_T bufref;
7326
7327 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007329 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 return;
7331 need_hide = FALSE;
7332 }
7333 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007334# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 {
7336 EMSG(_(e_nowrtmsg));
7337 return;
7338 }
7339 }
7340
Bram Moolenaard9967712006-03-11 21:18:15 +00007341# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007343# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007344
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007346 if (tp == NULL)
7347 win_close(win, !need_hide && !P_HID(buf));
7348 else
7349 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350}
7351
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007353 * ":tabclose": close current tab page, unless it is the last one.
7354 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 */
7356 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007357ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007359 tabpage_T *tp;
7360
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007361# ifdef FEAT_CMDWIN
7362 if (cmdwin_type != 0)
7363 cmdwin_result = K_IGNORE;
7364 else
7365# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007366 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007367 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007368 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007370 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007371 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007372 tp = find_tabpage((int)eap->line2);
7373 if (tp == NULL)
7374 {
7375 beep_flush();
7376 return;
7377 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007378 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007379 {
7380 tabpage_close_other(tp, eap->forceit);
7381 return;
7382 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007383 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007384 if (!text_locked()
7385#ifdef FEAT_AUTOCMD
7386 && !curbuf_locked()
7387#endif
7388 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00007389 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007391}
7392
7393/*
7394 * ":tabonly": close all tab pages except the current one
7395 */
7396 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007397ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007398{
7399 tabpage_T *tp;
7400 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007401
7402# ifdef FEAT_CMDWIN
7403 if (cmdwin_type != 0)
7404 cmdwin_result = K_IGNORE;
7405 else
7406# endif
7407 if (first_tabpage->tp_next == NULL)
7408 MSG(_("Already only one tab page"));
7409 else
7410 {
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007411 if (eap->addr_count > 0)
7412 goto_tabpage(eap->line2);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007413 /* Repeat this up to a 1000 times, because autocommands may mess
7414 * up the lists. */
7415 for (done = 0; done < 1000; ++done)
7416 {
7417 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7418 if (tp->tp_topframe != topframe)
7419 {
7420 tabpage_close_other(tp, eap->forceit);
7421 /* if we failed to close it quit */
7422 if (valid_tabpage(tp))
7423 done = 1000;
7424 /* start over, "tp" is now invalid */
7425 break;
7426 }
7427 if (first_tabpage->tp_next == NULL)
7428 break;
7429 }
7430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432
7433/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007434 * Close the current tab page.
7435 */
7436 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007437tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007438{
7439 /* First close all the windows but the current one. If that worked then
7440 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007441 if (lastwin != firstwin)
7442 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007443 if (lastwin == firstwin)
7444 ex_win_close(forceit, curwin, NULL);
7445# ifdef FEAT_GUI
7446 need_mouse_correct = TRUE;
7447# endif
7448}
7449
7450/*
7451 * Close tab page "tp", which is not the current tab page.
7452 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007453 * Also takes care of the tab pages line disappearing when closing the
7454 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007455 */
7456 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007457tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007458{
7459 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007460 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007461 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007462
7463 /* Limit to 1000 windows, autocommands may add a window while we close
7464 * one. OK, so I'm paranoid... */
7465 while (++done < 1000)
7466 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007467 wp = tp->tp_firstwin;
7468 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007469
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007470 /* Autocommands may delete the tab page under our fingers and we may
7471 * fail to close a window with a modified buffer. */
7472 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007473 break;
7474 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007475
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007476 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007477 if (h != tabline_height())
7478 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007479}
7480
7481/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 * ":only".
7483 */
7484 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007485ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007487 win_T *wp;
7488 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489# ifdef FEAT_GUI
7490 need_mouse_correct = TRUE;
7491# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007492 if (eap->addr_count > 0)
7493 {
7494 wnr = eap->line2;
7495 for (wp = firstwin; --wnr > 0; )
7496 {
7497 if (wp->w_next == NULL)
7498 break;
7499 else
7500 wp = wp->w_next;
7501 }
7502 win_goto(wp);
7503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 close_others(TRUE, eap->forceit);
7505}
7506
7507/*
7508 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007509 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007511 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007512ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513{
7514 if (eap->addr_count == 0)
7515 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007516 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517}
7518#endif /* FEAT_WINDOWS */
7519
7520 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007521ex_hide(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522{
7523 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
7524 eap->errmsg = e_invarg;
7525 else
7526 {
7527 /* ":hide" or ":hide | cmd": hide current window */
7528 eap->nextcmd = check_nextcmd(eap->arg);
7529#ifdef FEAT_WINDOWS
7530 if (!eap->skip)
7531 {
7532# ifdef FEAT_GUI
7533 need_mouse_correct = TRUE;
7534# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007535 if (eap->addr_count == 0)
7536 win_close(curwin, FALSE); /* don't free buffer */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007537 else
7538 {
7539 int winnr = 0;
7540 win_T *win;
7541
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007542 for (win = firstwin; win != NULL; win = win->w_next)
7543 {
7544 winnr++;
7545 if (winnr == eap->line2)
7546 break;
7547 }
7548 if (win == NULL)
7549 win = lastwin;
7550 win_close(win, FALSE);
7551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 }
7553#endif
7554 }
7555}
7556
7557/*
7558 * ":stop" and ":suspend": Suspend Vim.
7559 */
7560 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007561ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562{
7563 /*
7564 * Disallow suspending for "rvim".
7565 */
7566 if (!check_restricted()
7567#ifdef WIN3264
7568 /*
7569 * Check if external commands are allowed now.
7570 */
7571 && can_end_termcap_mode(TRUE)
7572#endif
7573 )
7574 {
7575 if (!eap->forceit)
7576 autowrite_all();
7577 windgoto((int)Rows - 1, 0);
7578 out_char('\n');
7579 out_flush();
7580 stoptermcap();
7581 out_flush(); /* needed for SUN to restore xterm buffer */
7582#ifdef FEAT_TITLE
7583 mch_restore_title(3); /* restore window titles */
7584#endif
7585 ui_suspend(); /* call machine specific function */
7586#ifdef FEAT_TITLE
7587 maketitle();
7588 resettitle(); /* force updating the title */
7589#endif
7590 starttermcap();
7591 scroll_start(); /* scroll screen before redrawing */
7592 redraw_later_clear();
7593 shell_resized(); /* may have resized window */
7594 }
7595}
7596
7597/*
7598 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7599 */
7600 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007601ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602{
7603#ifdef FEAT_CMDWIN
7604 if (cmdwin_type != 0)
7605 {
7606 cmdwin_result = Ctrl_C;
7607 return;
7608 }
7609#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007610 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007611 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007612 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007613 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007614 return;
7615 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007616#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007617 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7618 /* Refuse to quit when locked or when the buffer in the last window is
7619 * being closed (can only happen in autocommands). */
7620 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007621 return;
7622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623
7624 /*
7625 * if more files or windows we won't exit
7626 */
7627 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7628 exiting = TRUE;
7629 if ( ((eap->cmdidx == CMD_wq
7630 || curbufIsChanged())
7631 && do_write(eap) == FAIL)
7632 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007633 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 {
7635 not_exiting();
7636 }
7637 else
7638 {
7639#ifdef FEAT_WINDOWS
7640 if (only_one_window()) /* quit last window, exit Vim */
7641#endif
7642 getout(0);
7643#ifdef FEAT_WINDOWS
7644# ifdef FEAT_GUI
7645 need_mouse_correct = TRUE;
7646# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007647 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 win_close(curwin, !P_HID(curwin->w_buffer));
7649#endif
7650 }
7651}
7652
7653/*
7654 * ":print", ":list", ":number".
7655 */
7656 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007657ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007659 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7660 EMSG(_(e_emptybuf));
7661 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007663 for ( ;!got_int; ui_breakcheck())
7664 {
7665 print_line(eap->line1,
7666 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7667 || (eap->flags & EXFLAG_NR)),
7668 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7669 if (++eap->line1 > eap->line2)
7670 break;
7671 out_flush(); /* show one line at a time */
7672 }
7673 setpcmark();
7674 /* put cursor at last line */
7675 curwin->w_cursor.lnum = eap->line2;
7676 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 }
7678
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680}
7681
7682#ifdef FEAT_BYTEOFF
7683 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007684ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685{
7686 goto_byte(eap->line2);
7687}
7688#endif
7689
7690/*
7691 * ":shell".
7692 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007694ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695{
7696 do_shell(NULL, 0);
7697}
7698
7699#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7700 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007701 || defined(FEAT_GUI_MSWIN) \
7702 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 || defined(PROTO)
7704
7705/*
7706 * Handle a file drop. The code is here because a drop is *nearly* like an
7707 * :args command, but not quite (we have a list of exact filenames, so we
7708 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7709 * code is very similar to :args and hence needs access to a lot of the static
7710 * functions in this file.
7711 *
7712 * The list should be allocated using alloc(), as should each item in the
7713 * list. This function takes over responsibility for freeing the list.
7714 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007715 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7717 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7718 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7719 * file functionality is (currently) not in EMX this is not presently a
7720 * problem.
7721 */
7722 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007723handle_drop(
7724 int filec, /* the number of files dropped */
7725 char_u **filev, /* the list of files dropped */
7726 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727{
7728 exarg_T ea;
7729 int save_msg_scroll = msg_scroll;
7730
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007731 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007732 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007734#ifdef FEAT_AUTOCMD
7735 if (curbuf_locked())
7736 return;
7737#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007738 /* When the screen is being updated we should not change buffers and
7739 * windows structures, it may cause freed memory to be used. */
7740 if (updating_screen)
7741 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742
7743 /* Check whether the current buffer is changed. If so, we will need
7744 * to split the current window or data could be lost.
7745 * We don't need to check if the 'hidden' option is set, as in this
7746 * case the buffer won't be lost.
7747 */
7748 if (!P_HID(curbuf) && !split)
7749 {
7750 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007751 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 --emsg_off;
7753 }
7754 if (split)
7755 {
7756# ifdef FEAT_WINDOWS
7757 if (win_split(0, 0) == FAIL)
7758 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007759 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760
7761 /* When splitting the window, create a new alist. Otherwise the
7762 * existing one is overwritten. */
7763 alist_unlink(curwin->w_alist);
7764 alist_new();
7765# else
7766 return; /* can't split, always fail */
7767# endif
7768 }
7769
7770 /*
7771 * Set up the new argument list.
7772 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007773 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774
7775 /*
7776 * Move to the first file.
7777 */
7778 /* Fake up a minimal "next" command for do_argfile() */
7779 vim_memset(&ea, 0, sizeof(ea));
7780 ea.cmd = (char_u *)"next";
7781 do_argfile(&ea, 0);
7782
7783 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7784 * mode that is not needed here. */
7785 need_start_insertmode = FALSE;
7786
7787 /* Restore msg_scroll, otherwise a following command may cause scrolling
7788 * unexpectedly. The screen will be redrawn by the caller, thus
7789 * msg_scroll being set by displaying a message is irrelevant. */
7790 msg_scroll = save_msg_scroll;
7791}
7792#endif
7793
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794/*
7795 * Clear an argument list: free all file names and reset it to zero entries.
7796 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007797 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007798alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799{
7800 while (--al->al_ga.ga_len >= 0)
7801 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7802 ga_clear(&al->al_ga);
7803}
7804
7805/*
7806 * Init an argument list.
7807 */
7808 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007809alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810{
7811 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7812}
7813
7814#if defined(FEAT_WINDOWS) || defined(PROTO)
7815
7816/*
7817 * Remove a reference from an argument list.
7818 * Ignored when the argument list is the global one.
7819 * If the argument list is no longer used by any window, free it.
7820 */
7821 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007822alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823{
7824 if (al != &global_alist && --al->al_refcount <= 0)
7825 {
7826 alist_clear(al);
7827 vim_free(al);
7828 }
7829}
7830
7831# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7832/*
7833 * Create a new argument list and use it for the current window.
7834 */
7835 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007836alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837{
7838 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7839 if (curwin->w_alist == NULL)
7840 {
7841 curwin->w_alist = &global_alist;
7842 ++global_alist.al_refcount;
7843 }
7844 else
7845 {
7846 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007847 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 alist_init(curwin->w_alist);
7849 }
7850}
7851# endif
7852#endif
7853
Bram Moolenaar53076832015-12-31 19:53:21 +01007854#if (!defined(UNIX) && !defined(__EMX__)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855/*
7856 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007857 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7858 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 */
7860 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007861alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862{
7863 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007864 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 char_u **new_arg_files;
7866 int new_arg_file_count;
7867 char_u *save_p_su = p_su;
7868 int i;
7869
7870 /* Don't use 'suffixes' here. This should work like the shell did the
7871 * expansion. Also, the vimrc file isn't read yet, thus the user
7872 * can't set the options. */
7873 p_su = empty_option;
7874 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7875 if (old_arg_files != NULL)
7876 {
7877 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007878 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7879 old_arg_count = GARGCOUNT;
7880 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007882 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 && new_arg_file_count > 0)
7884 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007885 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7886 TRUE, fnum_list, fnum_len);
7887 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 }
7890 p_su = save_p_su;
7891}
7892#endif
7893
7894/*
7895 * Set the argument list for the current window.
7896 * Takes over the allocated files[] and the allocated fnames in it.
7897 */
7898 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007899alist_set(
7900 alist_T *al,
7901 int count,
7902 char_u **files,
7903 int use_curbuf,
7904 int *fnum_list,
7905 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906{
7907 int i;
7908
7909 alist_clear(al);
7910 if (ga_grow(&al->al_ga, count) == OK)
7911 {
7912 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007913 {
7914 if (got_int)
7915 {
7916 /* When adding many buffers this can take a long time. Allow
7917 * interrupting here. */
7918 while (i < count)
7919 vim_free(files[i++]);
7920 break;
7921 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007922
7923 /* May set buffer name of a buffer previously used for the
7924 * argument list, so that it's re-used by alist_add. */
7925 if (fnum_list != NULL && i < fnum_len)
7926 buf_set_name(fnum_list[i], files[i]);
7927
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007929 ui_breakcheck();
7930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 vim_free(files);
7932 }
7933 else
7934 FreeWild(count, files);
7935#ifdef FEAT_WINDOWS
7936 if (al == &global_alist)
7937#endif
7938 arg_had_last = FALSE;
7939}
7940
7941/*
7942 * Add file "fname" to argument list "al".
7943 * "fname" must have been allocated and "al" must have been checked for room.
7944 */
7945 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007946alist_add(
7947 alist_T *al,
7948 char_u *fname,
7949 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950{
7951 if (fname == NULL) /* don't add NULL file names */
7952 return;
7953#ifdef BACKSLASH_IN_FILENAME
7954 slash_adjust(fname);
7955#endif
7956 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7957 if (set_fnum > 0)
7958 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7959 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7960 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961}
7962
7963#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7964/*
7965 * Adjust slashes in file names. Called after 'shellslash' was set.
7966 */
7967 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007968alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969{
7970 int i;
7971# ifdef FEAT_WINDOWS
7972 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007973 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974# endif
7975
7976 for (i = 0; i < GARGCOUNT; ++i)
7977 if (GARGLIST[i].ae_fname != NULL)
7978 slash_adjust(GARGLIST[i].ae_fname);
7979# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007980 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 if (wp->w_alist != &global_alist)
7982 for (i = 0; i < WARGCOUNT(wp); ++i)
7983 if (WARGLIST(wp)[i].ae_fname != NULL)
7984 slash_adjust(WARGLIST(wp)[i].ae_fname);
7985# endif
7986}
7987#endif
7988
7989/*
7990 * ":preserve".
7991 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007993ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007995 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 ml_preserve(curbuf, TRUE);
7997}
7998
7999/*
8000 * ":recover".
8001 */
8002 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008003ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004{
8005 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8006 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008007 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8008 | CCGD_MULTWIN
8009 | (eap->forceit ? CCGD_FORCEIT : 0)
8010 | CCGD_EXCMD)
8011
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 && (*eap->arg == NUL
8013 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8014 ml_recover();
8015 recoverymode = FALSE;
8016}
8017
8018/*
8019 * Command modifier used in a wrong way.
8020 */
8021 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008022ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023{
8024 eap->errmsg = e_invcmd;
8025}
8026
8027#ifdef FEAT_WINDOWS
8028/*
8029 * :sview [+command] file split window with new file, read-only
8030 * :split [[+command] file] split window with current or new file
8031 * :vsplit [[+command] file] split window vertically with current or new file
8032 * :new [[+command] file] split window with no or new file
8033 * :vnew [[+command] file] split vertically window with no or new file
8034 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008035 *
8036 * :tabedit open new Tab page with empty window
8037 * :tabedit [+command] file open new Tab page and edit "file"
8038 * :tabnew [[+command] file] just like :tabedit
8039 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 */
8041 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008042ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008044 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008045# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008047# endif
8048# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008050# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008052# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008054# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008056# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008058 * quickfix windows. But it's OK when doing ":tab split". */
8059 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 {
8061 if (eap->cmdidx == CMD_split)
8062 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 if (eap->cmdidx == CMD_vsplit)
8064 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008066# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008068# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008069 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 {
8071 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8072 FNAME_MESS, TRUE, curbuf->b_ffname);
8073 if (fname == NULL)
8074 goto theend;
8075 eap->arg = fname;
8076 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008077# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008079# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008081# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 && eap->cmdidx != CMD_new)
8085 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008086# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008087 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008088# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008089 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008090# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008091 au_has_group((char_u *)"FileExplorer"))
8092 {
8093 /* No browsing supported but we do have the file explorer:
8094 * Edit the directory. */
8095 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8096 eap->arg = (char_u *)".";
8097 }
8098 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008099# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008100 {
8101 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008103 if (fname == NULL)
8104 goto theend;
8105 eap->arg = fname;
8106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 }
8108 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008109# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008111 /*
8112 * Either open new tab page or split the window.
8113 */
8114 if (eap->cmdidx == CMD_tabedit
8115 || eap->cmdidx == CMD_tabfind
8116 || eap->cmdidx == CMD_tabnew)
8117 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008118 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8119 : eap->addr_count == 0 ? 0
8120 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008121 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008122 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008123
8124 /* set the alternate buffer for the window we came from */
8125 if (curwin != old_curwin
8126 && win_valid(old_curwin)
8127 && old_curwin->w_buffer != curbuf
8128 && !cmdmod.keepalt)
8129 old_curwin->w_alt_fnum = curbuf->b_fnum;
8130 }
8131 }
8132 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8134 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008135# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 /* Reset 'scrollbind' when editing another file, but keep it when
8137 * doing ":split" without arguments. */
8138 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008139# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008141# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008143 {
8144 RESET_BINDING(curwin);
8145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 else
8147 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008148# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 do_exedit(eap, old_curwin);
8150 }
8151
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008152# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008154# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008156# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157theend:
8158 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008159# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008161
8162/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008163 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008164 */
8165 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008166tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008167{
8168 exarg_T ea;
8169
8170 vim_memset(&ea, 0, sizeof(ea));
8171 ea.cmdidx = CMD_tabnew;
8172 ea.cmd = (char_u *)"tabn";
8173 ea.arg = (char_u *)"";
8174 ex_splitview(&ea);
8175}
8176
8177/*
8178 * :tabnext command
8179 */
8180 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008181ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008182{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008183 switch (eap->cmdidx)
8184 {
8185 case CMD_tabfirst:
8186 case CMD_tabrewind:
8187 goto_tabpage(1);
8188 break;
8189 case CMD_tablast:
8190 goto_tabpage(9999);
8191 break;
8192 case CMD_tabprevious:
8193 case CMD_tabNext:
8194 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
8195 break;
8196 default: /* CMD_tabnext */
8197 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
8198 break;
8199 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008200}
8201
8202/*
8203 * :tabmove command
8204 */
8205 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008206ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008207{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008208 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008209
8210 if (eap->arg && *eap->arg != NUL)
8211 {
8212 char_u *p = eap->arg;
8213 int relative = 0; /* argument +N/-N means: move N places to the
8214 * right/left relative to the current position. */
8215
8216 if (*eap->arg == '-')
8217 {
8218 relative = -1;
8219 p = eap->arg + 1;
8220 }
8221 else if (*eap->arg == '+')
8222 {
8223 relative = 1;
8224 p = eap->arg + 1;
8225 }
8226 else
8227 p = eap->arg;
8228
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008229 if (relative == 0)
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008230 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008231 if (STRCMP(p, "$") == 0)
8232 tab_number = LAST_TAB_NR;
8233 else if (p == skipdigits(p))
8234 {
8235 /* No numbers as argument. */
8236 eap->errmsg = e_invarg;
8237 return;
8238 }
8239 else
8240 tab_number = getdigits(&p);
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008241 }
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008242 else
8243 {
8244 if (*p != NUL)
8245 tab_number = getdigits(&p);
8246 else
8247 tab_number = 1;
8248 tab_number = tab_number * relative + tabpage_index(curtab);
8249 if (relative == -1)
8250 --tab_number;
8251 }
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008252 }
8253 else if (eap->addr_count != 0)
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008254 {
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008255 tab_number = eap->line2;
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008256 if (**eap->cmdlinep == '-')
8257 --tab_number;
8258 }
8259 else
8260 tab_number = LAST_TAB_NR;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008261
8262 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008263}
8264
8265/*
8266 * :tabs command: List tabs and their contents.
8267 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008268 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008269ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008270{
8271 tabpage_T *tp;
8272 win_T *wp;
8273 int tabcount = 1;
8274
8275 msg_start();
8276 msg_scroll = TRUE;
8277 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8278 {
8279 msg_putchar('\n');
8280 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
8281 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
8282 out_flush(); /* output one line at a time */
8283 ui_breakcheck();
8284
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008285 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008286 wp = firstwin;
8287 else
8288 wp = tp->tp_firstwin;
8289 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8290 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008291 msg_putchar('\n');
8292 msg_putchar(wp == curwin ? '>' : ' ');
8293 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008294 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8295 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008296 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008297 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008298 else
8299 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8300 IObuff, IOSIZE, TRUE);
8301 msg_outtrans(IObuff);
8302 out_flush(); /* output one line at a time */
8303 ui_breakcheck();
8304 }
8305 }
8306}
8307
8308#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309
8310/*
8311 * ":mode": Set screen mode.
8312 * If no argument given, just get the screen size and redraw.
8313 */
8314 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008315ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316{
8317 if (*eap->arg == NUL)
8318 shell_resized();
8319 else
8320 mch_screenmode(eap->arg);
8321}
8322
8323#ifdef FEAT_WINDOWS
8324/*
8325 * ":resize".
8326 * set, increment or decrement current window height
8327 */
8328 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008329ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330{
8331 int n;
8332 win_T *wp = curwin;
8333
8334 if (eap->addr_count > 0)
8335 {
8336 n = eap->line2;
8337 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8338 ;
8339 }
8340
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008341# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008343# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 if (cmdmod.split & WSP_VERT)
8346 {
8347 if (*eap->arg == '-' || *eap->arg == '+')
8348 n += W_WIDTH(curwin);
8349 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8350 n = 9999;
8351 win_setwidth_win((int)n, wp);
8352 }
8353 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 {
8355 if (*eap->arg == '-' || *eap->arg == '+')
8356 n += curwin->w_height;
8357 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8358 n = 9999;
8359 win_setheight_win((int)n, wp);
8360 }
8361}
8362#endif
8363
8364/*
8365 * ":find [+command] <file>" command.
8366 */
8367 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008368ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369{
8370#ifdef FEAT_SEARCHPATH
8371 char_u *fname;
8372 int count;
8373
8374 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8375 TRUE, curbuf->b_ffname);
8376 if (eap->addr_count > 0)
8377 {
8378 /* Repeat finding the file "count" times. This matters when it
8379 * appears several times in the path. */
8380 count = eap->line2;
8381 while (fname != NULL && --count > 0)
8382 {
8383 vim_free(fname);
8384 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8385 FALSE, curbuf->b_ffname);
8386 }
8387 }
8388
8389 if (fname != NULL)
8390 {
8391 eap->arg = fname;
8392#endif
8393 do_exedit(eap, NULL);
8394#ifdef FEAT_SEARCHPATH
8395 vim_free(fname);
8396 }
8397#endif
8398}
8399
8400/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008401 * ":open" simulation: for now just work like ":visual".
8402 */
8403 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008404ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008405{
8406 regmatch_T regmatch;
8407 char_u *p;
8408
8409 curwin->w_cursor.lnum = eap->line2;
8410 beginline(BL_SOL | BL_FIX);
8411 if (*eap->arg == '/')
8412 {
8413 /* ":open /pattern/": put cursor in column found with pattern */
8414 ++eap->arg;
8415 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8416 *p = NUL;
8417 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8418 if (regmatch.regprog != NULL)
8419 {
8420 regmatch.rm_ic = p_ic;
8421 p = ml_get_curline();
8422 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008423 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008424 else
8425 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008426 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008427 }
8428 /* Move to the NUL, ignore any other arguments. */
8429 eap->arg += STRLEN(eap->arg);
8430 }
8431 check_cursor();
8432
8433 eap->cmdidx = CMD_visual;
8434 do_exedit(eap, NULL);
8435}
8436
8437/*
8438 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 */
8440 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008441ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442{
8443 do_exedit(eap, NULL);
8444}
8445
8446/*
8447 * ":edit <file>" command and alikes.
8448 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008450do_exedit(
8451 exarg_T *eap,
8452 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453{
8454 int n;
8455#ifdef FEAT_WINDOWS
8456 int need_hide;
8457#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008458 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459
8460 /*
8461 * ":vi" command ends Ex mode.
8462 */
8463 if (exmode_active && (eap->cmdidx == CMD_visual
8464 || eap->cmdidx == CMD_view))
8465 {
8466 exmode_active = FALSE;
8467 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008468 {
8469 /* Special case: ":global/pat/visual\NLvi-commands" */
8470 if (global_busy)
8471 {
8472 int rd = RedrawingDisabled;
8473 int nwr = no_wait_return;
8474 int ms = msg_scroll;
8475#ifdef FEAT_GUI
8476 int he = hold_gui_events;
8477#endif
8478
8479 if (eap->nextcmd != NULL)
8480 {
8481 stuffReadbuff(eap->nextcmd);
8482 eap->nextcmd = NULL;
8483 }
8484
8485 if (exmode_was != EXMODE_VIM)
8486 settmode(TMODE_RAW);
8487 RedrawingDisabled = 0;
8488 no_wait_return = 0;
8489 need_wait_return = FALSE;
8490 msg_scroll = 0;
8491#ifdef FEAT_GUI
8492 hold_gui_events = 0;
8493#endif
8494 must_redraw = CLEAR;
8495
8496 main_loop(FALSE, TRUE);
8497
8498 RedrawingDisabled = rd;
8499 no_wait_return = nwr;
8500 msg_scroll = ms;
8501#ifdef FEAT_GUI
8502 hold_gui_events = he;
8503#endif
8504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 }
8508
8509 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008510 || eap->cmdidx == CMD_tabnew
8511 || eap->cmdidx == CMD_tabedit
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008512#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 || eap->cmdidx == CMD_vnew
8514#endif
8515 ) && *eap->arg == NUL)
8516 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008517 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 setpcmark();
8519 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008520 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8521 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 }
8523 else if ((eap->cmdidx != CMD_split
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008524#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 && eap->cmdidx != CMD_vsplit
8526#endif
8527 )
8528 || *eap->arg != NUL
8529#ifdef FEAT_BROWSE
8530 || cmdmod.browse
8531#endif
8532 )
8533 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008534#ifdef FEAT_AUTOCMD
8535 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8536 * can bring us here, others are stopped earlier. */
8537 if (*eap->arg != NUL && curbuf_locked())
8538 return;
8539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 n = readonlymode;
8541 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8542 readonlymode = TRUE;
8543 else if (eap->cmdidx == CMD_enew)
8544 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8545 empty buffer */
8546 setpcmark();
8547 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8548 NULL, eap,
8549 /* ":edit" goes to first line if Vi compatible */
8550 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8551 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8552 ? ECMD_ONE : eap->do_ecmd_lnum,
8553 (P_HID(curbuf) ? ECMD_HIDE : 0)
8554 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008555 /* after a split we can use an existing buffer */
8556 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557#ifdef FEAT_LISTCMDS
8558 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8559#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008560 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 {
8562 /* Editing the file failed. If the window was split, close it. */
8563#ifdef FEAT_WINDOWS
8564 if (old_curwin != NULL)
8565 {
8566 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8567 if (!need_hide || P_HID(curbuf))
8568 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008569# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8570 cleanup_T cs;
8571
8572 /* Reset the error/interrupt/exception state here so that
8573 * aborting() returns FALSE when closing a window. */
8574 enter_cleanup(&cs);
8575# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576# ifdef FEAT_GUI
8577 need_mouse_correct = TRUE;
8578# endif
8579 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008580
8581# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8582 /* Restore the error/interrupt/exception state if not
8583 * discarded by a new aborting error, interrupt, or
8584 * uncaught exception. */
8585 leave_cleanup(&cs);
8586# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 }
8588 }
8589#endif
8590 }
8591 else if (readonlymode && curbuf->b_nwindows == 1)
8592 {
8593 /* When editing an already visited buffer, 'readonly' won't be set
8594 * but the previous value is kept. With ":view" and ":sview" we
8595 * want the file to be readonly, except when another window is
8596 * editing the same buffer. */
8597 curbuf->b_p_ro = TRUE;
8598 }
8599 readonlymode = n;
8600 }
8601 else
8602 {
8603 if (eap->do_ecmd_cmd != NULL)
8604 do_cmdline_cmd(eap->do_ecmd_cmd);
8605#ifdef FEAT_TITLE
8606 n = curwin->w_arg_idx_invalid;
8607#endif
8608 check_arg_idx(curwin);
8609#ifdef FEAT_TITLE
8610 if (n != curwin->w_arg_idx_invalid)
8611 maketitle();
8612#endif
8613 }
8614
8615#ifdef FEAT_WINDOWS
8616 /*
8617 * if ":split file" worked, set alternate file name in old window to new
8618 * file
8619 */
8620 if (old_curwin != NULL
8621 && *eap->arg != NUL
8622 && curwin != old_curwin
8623 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008624 && old_curwin->w_buffer != curbuf
8625 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 old_curwin->w_alt_fnum = curbuf->b_fnum;
8627#endif
8628
8629 ex_no_reprint = TRUE;
8630}
8631
8632#ifndef FEAT_GUI
8633/*
8634 * ":gui" and ":gvim" when there is no GUI.
8635 */
8636 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008637ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638{
8639 eap->errmsg = e_nogvim;
8640}
8641#endif
8642
8643#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8644 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008645ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646{
8647 gui_make_tearoff(eap->arg);
8648}
8649#endif
8650
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008651#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008653ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008655 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656}
8657#endif
8658
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008660ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661{
8662 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8663 MSG(_("No swap file"));
8664 else
8665 msg(curbuf->b_ml.ml_mfp->mf_fname);
8666}
8667
8668/*
8669 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8670 * offset.
8671 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8672 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008674ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675{
8676#ifdef FEAT_SCROLLBIND
8677 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008678 win_T *save_curwin = curwin;
8679 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 long topline;
8681 long y;
8682 linenr_T old_linenr = curwin->w_cursor.lnum;
8683
8684 setpcmark();
8685
8686 /*
8687 * determine max topline
8688 */
8689 if (curwin->w_p_scb)
8690 {
8691 topline = curwin->w_topline;
8692 for (wp = firstwin; wp; wp = wp->w_next)
8693 {
8694 if (wp->w_p_scb && wp->w_buffer)
8695 {
8696 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8697 if (topline > y)
8698 topline = y;
8699 }
8700 }
8701 if (topline < 1)
8702 topline = 1;
8703 }
8704 else
8705 {
8706 topline = 1;
8707 }
8708
8709
8710 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008711 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8714 {
8715 if (curwin->w_p_scb)
8716 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008717 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 y = topline - curwin->w_topline;
8719 if (y > 0)
8720 scrollup(y, TRUE);
8721 else
8722 scrolldown(-y, TRUE);
8723 curwin->w_scbind_pos = topline;
8724 redraw_later(VALID);
8725 cursor_correct();
8726#ifdef FEAT_WINDOWS
8727 curwin->w_redr_status = TRUE;
8728#endif
8729 }
8730 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008731 curwin = save_curwin;
8732 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 if (curwin->w_p_scb)
8734 {
8735 did_syncbind = TRUE;
8736 checkpcmark();
8737 if (old_linenr != curwin->w_cursor.lnum)
8738 {
8739 char_u ctrl_o[2];
8740
8741 ctrl_o[0] = Ctrl_O;
8742 ctrl_o[1] = 0;
8743 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8744 }
8745 }
8746#endif
8747}
8748
8749
8750 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008751ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008753 int i;
8754 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8755 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756
8757 if (eap->usefilter) /* :r!cmd */
8758 do_bang(1, eap, FALSE, FALSE, TRUE);
8759 else
8760 {
8761 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8762 return;
8763
8764#ifdef FEAT_BROWSE
8765 if (cmdmod.browse)
8766 {
8767 char_u *browseFile;
8768
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008769 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 NULL, NULL, NULL, curbuf);
8771 if (browseFile != NULL)
8772 {
8773 i = readfile(browseFile, NULL,
8774 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8775 vim_free(browseFile);
8776 }
8777 else
8778 i = OK;
8779 }
8780 else
8781#endif
8782 if (*eap->arg == NUL)
8783 {
8784 if (check_fname() == FAIL) /* check for no file name */
8785 return;
8786 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8787 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8788 }
8789 else
8790 {
8791 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8792 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8793 i = readfile(eap->arg, NULL,
8794 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8795
8796 }
8797 if (i == FAIL)
8798 {
8799#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8800 if (!aborting())
8801#endif
8802 EMSG2(_(e_notopen), eap->arg);
8803 }
8804 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008805 {
8806 if (empty && exmode_active)
8807 {
8808 /* Delete the empty line that remains. Historically ex does
8809 * this but vi doesn't. */
8810 if (eap->line2 == 0)
8811 lnum = curbuf->b_ml.ml_line_count;
8812 else
8813 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008814 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008815 {
8816 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008817 if (curwin->w_cursor.lnum > 1
8818 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008819 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008820 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008821 }
8822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 }
8826}
8827
Bram Moolenaarea408852005-06-25 22:49:46 +00008828static char_u *prev_dir = NULL;
8829
8830#if defined(EXITFREE) || defined(PROTO)
8831 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008832free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00008833{
8834 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008835 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008836
8837 vim_free(globaldir);
8838 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008839}
8840#endif
8841
Bram Moolenaarf4258302013-06-02 18:20:17 +02008842/*
8843 * Deal with the side effects of changing the current directory.
8844 * When "local" is TRUE then this was after an ":lcd" command.
8845 */
8846 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008847post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008848{
8849 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008850 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008851 if (local)
8852 {
8853 /* If still in global directory, need to remember current
8854 * directory as global directory. */
8855 if (globaldir == NULL && prev_dir != NULL)
8856 globaldir = vim_strsave(prev_dir);
8857 /* Remember this local directory for the window. */
8858 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8859 curwin->w_localdir = vim_strsave(NameBuff);
8860 }
8861 else
8862 {
8863 /* We are now in the global directory, no need to remember its
8864 * name. */
8865 vim_free(globaldir);
8866 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008867 }
8868
8869 shorten_fnames(TRUE);
8870}
8871
Bram Moolenaarea408852005-06-25 22:49:46 +00008872
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873/*
8874 * ":cd", ":lcd", ":chdir" and ":lchdir".
8875 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008876 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008877ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 char_u *new_dir;
8880 char_u *tofree;
8881
8882 new_dir = eap->arg;
8883#if !defined(UNIX) && !defined(VMS)
8884 /* for non-UNIX ":cd" means: print current directory */
8885 if (*new_dir == NUL)
8886 ex_pwd(NULL);
8887 else
8888#endif
8889 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008890#ifdef FEAT_AUTOCMD
8891 if (allbuf_locked())
8892 return;
8893#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008894 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8895 && !eap->forceit)
8896 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008897 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008898 return;
8899 }
8900
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 /* ":cd -": Change to previous directory */
8902 if (STRCMP(new_dir, "-") == 0)
8903 {
8904 if (prev_dir == NULL)
8905 {
8906 EMSG(_("E186: No previous directory"));
8907 return;
8908 }
8909 new_dir = prev_dir;
8910 }
8911
8912 /* Save current directory for next ":cd -" */
8913 tofree = prev_dir;
8914 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8915 prev_dir = vim_strsave(NameBuff);
8916 else
8917 prev_dir = NULL;
8918
8919#if defined(UNIX) || defined(VMS)
8920 /* for UNIX ":cd" means: go to home directory */
8921 if (*new_dir == NUL)
8922 {
8923 /* use NameBuff for home directory name */
8924# ifdef VMS
8925 char_u *p;
8926
8927 p = mch_getenv((char_u *)"SYS$LOGIN");
8928 if (p == NULL || *p == NUL) /* empty is the same as not set */
8929 NameBuff[0] = NUL;
8930 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008931 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932# else
8933 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8934# endif
8935 new_dir = NameBuff;
8936 }
8937#endif
8938 if (new_dir == NULL || vim_chdir(new_dir))
8939 EMSG(_(e_failed));
8940 else
8941 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008942 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943
8944 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008945 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 ex_pwd(eap);
8947 }
8948 vim_free(tofree);
8949 }
8950}
8951
8952/*
8953 * ":pwd".
8954 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008956ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957{
8958 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8959 {
8960#ifdef BACKSLASH_IN_FILENAME
8961 slash_adjust(NameBuff);
8962#endif
8963 msg(NameBuff);
8964 }
8965 else
8966 EMSG(_("E187: Unknown"));
8967}
8968
8969/*
8970 * ":=".
8971 */
8972 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008973ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974{
8975 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008976 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977}
8978
8979 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008980ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008982 int n;
8983 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984
8985 if (cursor_valid())
8986 {
8987 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8988 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01008989 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008991
8992 len = eap->line2;
8993 switch (*eap->arg)
8994 {
8995 case 'm': break;
8996 case NUL: len *= 1000L; break;
8997 default: EMSG2(_(e_invarg2), eap->arg); return;
8998 }
8999 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000}
9001
9002/*
9003 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9004 */
9005 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009006do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007{
9008 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009009 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010
9011 cursor_on();
9012 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009013 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009015 wait_now = msec - done > 1000L ? 1000L : msec - done;
9016#ifdef FEAT_TIMERS
9017 {
9018 long due_time = check_due_timer();
9019
9020 if (due_time > 0 && due_time < wait_now)
9021 wait_now = due_time;
9022 }
9023#endif
9024 ui_delay(wait_now, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009026#ifdef MESSAGE_QUEUE
9027 /* Process the netbeans and clientserver messages that may have been
9028 * received in the call to ui_breakcheck() when the GUI is in use. This
9029 * may occur when running a test case. */
9030 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 }
9033}
9034
9035 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009036do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037{
9038 int mode;
9039 char_u *cmdp;
9040
9041 cmdp = eap->cmd;
9042 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9043
9044 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9045 eap->arg, mode, isabbrev))
9046 {
9047 case 1: EMSG(_(e_invarg));
9048 break;
9049 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9050 break;
9051 }
9052}
9053
9054/*
9055 * ":winsize" command (obsolete).
9056 */
9057 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009058ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059{
9060 int w, h;
9061 char_u *arg = eap->arg;
9062 char_u *p;
9063
9064 w = getdigits(&arg);
9065 arg = skipwhite(arg);
9066 p = arg;
9067 h = getdigits(&arg);
9068 if (*p != NUL && *arg == NUL)
9069 set_shellsize(w, h, TRUE);
9070 else
9071 EMSG(_("E465: :winsize requires two number arguments"));
9072}
9073
9074#ifdef FEAT_WINDOWS
9075 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009076ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077{
9078 int xchar = NUL;
9079 char_u *p;
9080
9081 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9082 {
9083 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9084 if (eap->arg[1] == NUL)
9085 {
9086 EMSG(_(e_invarg));
9087 return;
9088 }
9089 xchar = eap->arg[1];
9090 p = eap->arg + 2;
9091 }
9092 else
9093 p = eap->arg + 1;
9094
9095 eap->nextcmd = check_nextcmd(p);
9096 p = skipwhite(p);
9097 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9098 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009099 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100 {
9101 /* Pass flags on for ":vertical wincmd ]". */
9102 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009103 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9105 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009106 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 }
9108}
9109#endif
9110
Bram Moolenaar843ee412004-06-30 16:16:41 +00009111#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112/*
9113 * ":winpos".
9114 */
9115 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009116ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117{
9118 int x, y;
9119 char_u *arg = eap->arg;
9120 char_u *p;
9121
9122 if (*arg == NUL)
9123 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009124# if defined(FEAT_GUI) || defined(MSWIN)
9125# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009127# else
9128 if (mch_get_winpos(&x, &y) != FAIL)
9129# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 {
9131 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9132 msg(IObuff);
9133 }
9134 else
9135# endif
9136 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9137 }
9138 else
9139 {
9140 x = getdigits(&arg);
9141 arg = skipwhite(arg);
9142 p = arg;
9143 y = getdigits(&arg);
9144 if (*p == NUL || *arg != NUL)
9145 {
9146 EMSG(_("E466: :winpos requires two number arguments"));
9147 return;
9148 }
9149# ifdef FEAT_GUI
9150 if (gui.in_use)
9151 gui_mch_set_winpos(x, y);
9152 else if (gui.starting)
9153 {
9154 /* Remember the coordinates for when the window is opened. */
9155 gui_win_x = x;
9156 gui_win_y = y;
9157 }
9158# ifdef HAVE_TGETENT
9159 else
9160# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009161# else
9162# ifdef MSWIN
9163 mch_set_winpos(x, y);
9164# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165# endif
9166# ifdef HAVE_TGETENT
9167 if (*T_CWP)
9168 term_set_winpos(x, y);
9169# endif
9170 }
9171}
9172#endif
9173
9174/*
9175 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9176 */
9177 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009178ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179{
9180 oparg_T oa;
9181
9182 clear_oparg(&oa);
9183 oa.regname = eap->regname;
9184 oa.start.lnum = eap->line1;
9185 oa.end.lnum = eap->line2;
9186 oa.line_count = eap->line2 - eap->line1 + 1;
9187 oa.motion_type = MLINE;
9188#ifdef FEAT_VIRTUALEDIT
9189 virtual_op = FALSE;
9190#endif
9191 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9192 {
9193 setpcmark();
9194 curwin->w_cursor.lnum = eap->line1;
9195 beginline(BL_SOL | BL_FIX);
9196 }
9197
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009198 if (VIsual_active)
9199 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009200
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 switch (eap->cmdidx)
9202 {
9203 case CMD_delete:
9204 oa.op_type = OP_DELETE;
9205 op_delete(&oa);
9206 break;
9207
9208 case CMD_yank:
9209 oa.op_type = OP_YANK;
9210 (void)op_yank(&oa, FALSE, TRUE);
9211 break;
9212
9213 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009214 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009216 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9217#else
9218 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009220 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 oa.op_type = OP_RSHIFT;
9222 else
9223 oa.op_type = OP_LSHIFT;
9224 op_shift(&oa, FALSE, eap->amount);
9225 break;
9226 }
9227#ifdef FEAT_VIRTUALEDIT
9228 virtual_op = MAYBE;
9229#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009230 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231}
9232
9233/*
9234 * ":put".
9235 */
9236 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009237ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238{
9239 /* ":0put" works like ":1put!". */
9240 if (eap->line2 == 0)
9241 {
9242 eap->line2 = 1;
9243 eap->forceit = TRUE;
9244 }
9245 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009246 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9247 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248}
9249
9250/*
9251 * Handle ":copy" and ":move".
9252 */
9253 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009254ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255{
9256 long n;
9257
Bram Moolenaaraa23b372015-09-08 18:46:31 +02009258 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259 if (eap->arg == NULL) /* error detected */
9260 {
9261 eap->nextcmd = NULL;
9262 return;
9263 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009264 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265
9266 /*
9267 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9268 */
9269 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9270 {
9271 EMSG(_(e_invaddr));
9272 return;
9273 }
9274
9275 if (eap->cmdidx == CMD_move)
9276 {
9277 if (do_move(eap->line1, eap->line2, n) == FAIL)
9278 return;
9279 }
9280 else
9281 ex_copy(eap->line1, eap->line2, n);
9282 u_clearline();
9283 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009284 ex_may_print(eap);
9285}
9286
9287/*
9288 * Print the current line if flags were given to the Ex command.
9289 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009290 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009291ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009292{
9293 if (eap->flags != 0)
9294 {
9295 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9296 (eap->flags & EXFLAG_LIST));
9297 ex_no_reprint = TRUE;
9298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299}
9300
9301/*
9302 * ":smagic" and ":snomagic".
9303 */
9304 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009305ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306{
9307 int magic_save = p_magic;
9308
9309 p_magic = (eap->cmdidx == CMD_smagic);
9310 do_sub(eap);
9311 p_magic = magic_save;
9312}
9313
9314/*
9315 * ":join".
9316 */
9317 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009318ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319{
9320 curwin->w_cursor.lnum = eap->line1;
9321 if (eap->line1 == eap->line2)
9322 {
9323 if (eap->addr_count >= 2) /* :2,2join does nothing */
9324 return;
9325 if (eap->line2 == curbuf->b_ml.ml_line_count)
9326 {
9327 beep_flush();
9328 return;
9329 }
9330 ++eap->line2;
9331 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009332 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009334 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335}
9336
9337/*
9338 * ":[addr]@r" or ":[addr]*r": execute register
9339 */
9340 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009341ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342{
9343 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009344 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345
9346 curwin->w_cursor.lnum = eap->line2;
9347
9348#ifdef USE_ON_FLY_SCROLL
9349 dont_scroll = TRUE; /* disallow scrolling here */
9350#endif
9351
9352 /* get the register name. No name means to use the previous one */
9353 c = *eap->arg;
9354 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9355 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009356 /* Put the register in the typeahead buffer with the "silent" flag. */
9357 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9358 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009359 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 else
9363 {
9364 int save_efr = exec_from_reg;
9365
9366 exec_from_reg = TRUE;
9367
9368 /*
9369 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009370 * Continue until the stuff buffer is empty and all added characters
9371 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009373 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9375
9376 exec_from_reg = save_efr;
9377 }
9378}
9379
9380/*
9381 * ":!".
9382 */
9383 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009384ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385{
9386 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9387}
9388
9389/*
9390 * ":undo".
9391 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009393ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009395 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009396 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009397 else
9398 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399}
9400
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009401#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009402 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009403ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009404{
9405 char_u hash[UNDO_HASH_SIZE];
9406
9407 u_compute_hash(hash);
9408 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9409}
9410
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009411 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009412ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009413{
9414 char_u hash[UNDO_HASH_SIZE];
9415
9416 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009417 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009418}
9419#endif
9420
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421/*
9422 * ":redo".
9423 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009425ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426{
9427 u_redo(1);
9428}
9429
9430/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009431 * ":earlier" and ":later".
9432 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009433 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009434ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009435{
9436 long count = 0;
9437 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009438 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009439 char_u *p = eap->arg;
9440
9441 if (*p == NUL)
9442 count = 1;
9443 else if (isdigit(*p))
9444 {
9445 count = getdigits(&p);
9446 switch (*p)
9447 {
9448 case 's': ++p; sec = TRUE; break;
9449 case 'm': ++p; sec = TRUE; count *= 60; break;
9450 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009451 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9452 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009453 }
9454 }
9455
9456 if (*p != NUL)
9457 EMSG2(_(e_invarg2), eap->arg);
9458 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009459 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9460 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009461}
9462
9463/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 * ":redir": start/stop redirection.
9465 */
9466 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009467ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468{
9469 char *mode;
9470 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009471 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472
Bram Moolenaarba768492016-07-08 15:32:54 +02009473#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009474 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009475 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009476 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009477 return;
9478 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009479#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009480
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 if (STRICMP(eap->arg, "END") == 0)
9482 close_redir();
9483 else
9484 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009485 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009487 ++arg;
9488 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009490 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 mode = "a";
9492 }
9493 else
9494 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009495 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496
9497 close_redir();
9498
9499 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009500 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 if (fname == NULL)
9502 return;
9503#ifdef FEAT_BROWSE
9504 if (cmdmod.browse)
9505 {
9506 char_u *browseFile;
9507
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009508 browseFile = do_browse(BROWSE_SAVE,
9509 (char_u *)_("Save Redirection"),
9510 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 if (browseFile == NULL)
9512 return; /* operation cancelled */
9513 vim_free(fname);
9514 fname = browseFile;
9515 eap->forceit = TRUE; /* since dialog already asked */
9516 }
9517#endif
9518
9519 redir_fd = open_exfile(fname, eap->forceit, mode);
9520 vim_free(fname);
9521 }
9522#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009523 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 {
9525 /* redirect to a register a-z (resp. A-Z for appending) */
9526 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009527 ++arg;
9528 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009530 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009531 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009533 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009535 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009536 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009537 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009538 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009540 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009541 if (*arg == '>')
9542 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009543 /* Make register empty when not using @A-@Z and the
9544 * command is valid. */
9545 if (*arg == NUL && !isupper(redir_reg))
9546 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009548 }
9549 if (*arg != NUL)
9550 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009551 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009552 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009553 }
9554 }
9555 else if (*arg == '=' && arg[1] == '>')
9556 {
9557 int append;
9558
9559 /* redirect to a variable */
9560 close_redir();
9561 arg += 2;
9562
9563 if (*arg == '>')
9564 {
9565 ++arg;
9566 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567 }
9568 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009569 append = FALSE;
9570
9571 if (var_redir_start(skipwhite(arg), append) == OK)
9572 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 }
9574#endif
9575
9576 /* TODO: redirect to a buffer */
9577
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009579 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009581
9582 /* Make sure redirection is not off. Can happen for cmdline completion
9583 * that indirectly invokes a command to catch its output. */
9584 if (redir_fd != NULL
9585#ifdef FEAT_EVAL
9586 || redir_reg || redir_vname
9587#endif
9588 )
9589 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590}
9591
9592/*
9593 * ":redraw": force redraw
9594 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009595 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009596ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597{
9598 int r = RedrawingDisabled;
9599 int p = p_lz;
9600
9601 RedrawingDisabled = 0;
9602 p_lz = FALSE;
9603 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009604 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605#ifdef FEAT_TITLE
9606 if (need_maketitle)
9607 maketitle();
9608#endif
9609 RedrawingDisabled = r;
9610 p_lz = p;
9611
9612 /* Reset msg_didout, so that a message that's there is overwritten. */
9613 msg_didout = FALSE;
9614 msg_col = 0;
9615
Bram Moolenaar56667a52013-07-17 11:54:28 +02009616 /* No need to wait after an intentional redraw. */
9617 need_wait_return = FALSE;
9618
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 out_flush();
9620}
9621
9622/*
9623 * ":redrawstatus": force redraw of status line(s)
9624 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009626ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627{
9628#if defined(FEAT_WINDOWS)
9629 int r = RedrawingDisabled;
9630 int p = p_lz;
9631
9632 RedrawingDisabled = 0;
9633 p_lz = FALSE;
9634 if (eap->forceit)
9635 status_redraw_all();
9636 else
9637 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009638 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 RedrawingDisabled = r;
9640 p_lz = p;
9641 out_flush();
9642#endif
9643}
9644
9645 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009646close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647{
9648 if (redir_fd != NULL)
9649 {
9650 fclose(redir_fd);
9651 redir_fd = NULL;
9652 }
9653#ifdef FEAT_EVAL
9654 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009655 if (redir_vname)
9656 {
9657 var_redir_stop();
9658 redir_vname = 0;
9659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660#endif
9661}
9662
9663#if defined(FEAT_SESSION) && defined(USE_CRNL)
9664# define MKSESSION_NL
9665static int mksession_nl = FALSE; /* use NL only in put_eol() */
9666#endif
9667
9668/*
9669 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9670 */
9671 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009672ex_mkrc(
9673 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674{
9675 FILE *fd;
9676 int failed = FALSE;
9677 char_u *fname;
9678#ifdef FEAT_BROWSE
9679 char_u *browseFile = NULL;
9680#endif
9681#ifdef FEAT_SESSION
9682 int view_session = FALSE;
9683 int using_vdir = FALSE; /* using 'viewdir'? */
9684 char_u *viewFile = NULL;
9685 unsigned *flagp;
9686#endif
9687
9688 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9689 {
9690#ifdef FEAT_SESSION
9691 view_session = TRUE;
9692#else
9693 ex_ni(eap);
9694 return;
9695#endif
9696 }
9697
9698#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009699 /* Use the short file name until ":lcd" is used. We also don't use the
9700 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009701 did_lcd = FALSE;
9702
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9704 if (eap->cmdidx == CMD_mkview
9705 && (*eap->arg == NUL
9706 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9707 {
9708 eap->forceit = TRUE;
9709 fname = get_view_file(*eap->arg);
9710 if (fname == NULL)
9711 return;
9712 viewFile = fname;
9713 using_vdir = TRUE;
9714 }
9715 else
9716#endif
9717 if (*eap->arg != NUL)
9718 fname = eap->arg;
9719 else if (eap->cmdidx == CMD_mkvimrc)
9720 fname = (char_u *)VIMRC_FILE;
9721#ifdef FEAT_SESSION
9722 else if (eap->cmdidx == CMD_mksession)
9723 fname = (char_u *)SESSION_FILE;
9724#endif
9725 else
9726 fname = (char_u *)EXRC_FILE;
9727
9728#ifdef FEAT_BROWSE
9729 if (cmdmod.browse)
9730 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009731 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732# ifdef FEAT_SESSION
9733 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9734 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9735# endif
9736 (char_u *)_("Save Setup"),
9737 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9738 if (browseFile == NULL)
9739 goto theend;
9740 fname = browseFile;
9741 eap->forceit = TRUE; /* since dialog already asked */
9742 }
9743#endif
9744
9745#if defined(FEAT_SESSION) && defined(vim_mkdir)
9746 /* When using 'viewdir' may have to create the directory. */
9747 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009748 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749#endif
9750
9751 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9752 if (fd != NULL)
9753 {
9754#ifdef FEAT_SESSION
9755 if (eap->cmdidx == CMD_mkview)
9756 flagp = &vop_flags;
9757 else
9758 flagp = &ssop_flags;
9759#endif
9760
9761#ifdef MKSESSION_NL
9762 /* "unix" in 'sessionoptions': use NL line separator */
9763 if (view_session && (*flagp & SSOP_UNIX))
9764 mksession_nl = TRUE;
9765#endif
9766
9767 /* Write the version command for :mkvimrc */
9768 if (eap->cmdidx == CMD_mkvimrc)
9769 (void)put_line(fd, "version 6.0");
9770
9771#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009772 if (eap->cmdidx == CMD_mksession)
9773 {
9774 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9775 failed = TRUE;
9776 }
9777
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 if (eap->cmdidx != CMD_mkview)
9779#endif
9780 {
9781 /* Write setting 'compatible' first, because it has side effects.
9782 * For that same reason only do it when needed. */
9783 if (p_cp)
9784 (void)put_line(fd, "if !&cp | set cp | endif");
9785 else
9786 (void)put_line(fd, "if &cp | set nocp | endif");
9787 }
9788
9789#ifdef FEAT_SESSION
9790 if (!view_session
9791 || (eap->cmdidx == CMD_mksession
9792 && (*flagp & SSOP_OPTIONS)))
9793#endif
9794 failed |= (makemap(fd, NULL) == FAIL
9795 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9796
9797#ifdef FEAT_SESSION
9798 if (!failed && view_session)
9799 {
9800 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9801 failed = TRUE;
9802 if (eap->cmdidx == CMD_mksession)
9803 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009804 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805
Bram Moolenaard9462e32011-04-11 21:35:11 +02009806 dirnow = alloc(MAXPATHL);
9807 if (dirnow == NULL)
9808 failed = TRUE;
9809 else
9810 {
9811 /*
9812 * Change to session file's dir.
9813 */
9814 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009816 *dirnow = NUL;
9817 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9818 {
9819 if (vim_chdirfile(fname) == OK)
9820 shorten_fnames(TRUE);
9821 }
9822 else if (*dirnow != NUL
9823 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9824 {
9825 if (mch_chdir((char *)globaldir) == 0)
9826 shorten_fnames(TRUE);
9827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828
Bram Moolenaard9462e32011-04-11 21:35:11 +02009829 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830
Bram Moolenaard9462e32011-04-11 21:35:11 +02009831 /* restore original dir */
9832 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009834 {
9835 if (mch_chdir((char *)dirnow) != 0)
9836 EMSG(_(e_prev_dir));
9837 shorten_fnames(TRUE);
9838 }
9839 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840 }
9841 }
9842 else
9843 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009844 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9845 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 }
9847 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9848 == FAIL)
9849 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009850 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9851 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009852 if (eap->cmdidx == CMD_mksession)
9853 {
9854 if (put_line(fd, "unlet SessionLoad") == FAIL)
9855 failed = TRUE;
9856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 }
9858#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009859 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9860 failed = TRUE;
9861
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 failed |= fclose(fd);
9863
9864 if (failed)
9865 EMSG(_(e_write));
9866#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9867 else if (eap->cmdidx == CMD_mksession)
9868 {
9869 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009870 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871
Bram Moolenaard9462e32011-04-11 21:35:11 +02009872 tbuf = alloc(MAXPATHL);
9873 if (tbuf != NULL)
9874 {
9875 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9876 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9877 vim_free(tbuf);
9878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 }
9880#endif
9881#ifdef MKSESSION_NL
9882 mksession_nl = FALSE;
9883#endif
9884 }
9885
9886#ifdef FEAT_BROWSE
9887theend:
9888 vim_free(browseFile);
9889#endif
9890#ifdef FEAT_SESSION
9891 vim_free(viewFile);
9892#endif
9893}
9894
Bram Moolenaardf177f62005-02-22 08:39:57 +00009895#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9896 || defined(PROTO)
9897 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009898vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009899{
9900 if (vim_mkdir(name, prot) != 0)
9901 {
9902 EMSG2(_("E739: Cannot create directory: %s"), name);
9903 return FAIL;
9904 }
9905 return OK;
9906}
9907#endif
9908
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909/*
9910 * Open a file for writing for an Ex command, with some checks.
9911 * Return file descriptor, or NULL on failure.
9912 */
9913 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009914open_exfile(
9915 char_u *fname,
9916 int forceit,
9917 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918{
9919 FILE *fd;
9920
9921#ifdef UNIX
9922 /* with Unix it is possible to open a directory */
9923 if (mch_isdir(fname))
9924 {
9925 EMSG2(_(e_isadir2), fname);
9926 return NULL;
9927 }
9928#endif
9929 if (!forceit && *mode != 'a' && vim_fexists(fname))
9930 {
9931 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9932 return NULL;
9933 }
9934
9935 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9936 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9937
9938 return fd;
9939}
9940
9941/*
9942 * ":mark" and ":k".
9943 */
9944 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009945ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946{
9947 pos_T pos;
9948
9949 if (*eap->arg == NUL) /* No argument? */
9950 EMSG(_(e_argreq));
9951 else if (eap->arg[1] != NUL) /* more than one character? */
9952 EMSG(_(e_trailing));
9953 else
9954 {
9955 pos = curwin->w_cursor; /* save curwin->w_cursor */
9956 curwin->w_cursor.lnum = eap->line2;
9957 beginline(BL_WHITE | BL_FIX);
9958 if (setmark(*eap->arg) == FAIL) /* set mark */
9959 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9960 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9961 }
9962}
9963
9964/*
9965 * Update w_topline, w_leftcol and the cursor position.
9966 */
9967 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009968update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969{
9970 check_cursor(); /* put cursor on valid line */
9971 update_topline();
9972 if (!curwin->w_p_wrap)
9973 validate_cursor();
9974 update_curswant();
9975}
9976
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977/*
9978 * ":normal[!] {commands}": Execute normal mode commands.
9979 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01009980 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009981ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983 int save_msg_scroll = msg_scroll;
9984 int save_restart_edit = restart_edit;
9985 int save_msg_didout = msg_didout;
9986 int save_State = State;
9987 tasave_T tabuf;
9988 int save_insertmode = p_im;
9989 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009990 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991#ifdef FEAT_MBYTE
9992 char_u *arg = NULL;
9993 int l;
9994 char_u *p;
9995#endif
9996
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009997 if (ex_normal_lock > 0)
9998 {
9999 EMSG(_(e_secure));
10000 return;
10001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 if (ex_normal_busy >= p_mmd)
10003 {
10004 EMSG(_("E192: Recursive use of :normal too deep"));
10005 return;
10006 }
10007 ++ex_normal_busy;
10008
10009 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10010 restart_edit = 0; /* don't go to Insert mode */
10011 p_im = FALSE; /* don't use 'insertmode' */
10012
10013#ifdef FEAT_MBYTE
10014 /*
10015 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10016 * this for the K_SPECIAL leading byte, otherwise special keys will not
10017 * work.
10018 */
10019 if (has_mbyte)
10020 {
10021 int len = 0;
10022
10023 /* Count the number of characters to be escaped. */
10024 for (p = eap->arg; *p != NUL; ++p)
10025 {
10026# ifdef FEAT_GUI
10027 if (*p == CSI) /* leadbyte CSI */
10028 len += 2;
10029# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010030 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10032# ifdef FEAT_GUI
10033 || *p == CSI
10034# endif
10035 )
10036 len += 2;
10037 }
10038 if (len > 0)
10039 {
10040 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10041 if (arg != NULL)
10042 {
10043 len = 0;
10044 for (p = eap->arg; *p != NUL; ++p)
10045 {
10046 arg[len++] = *p;
10047# ifdef FEAT_GUI
10048 if (*p == CSI)
10049 {
10050 arg[len++] = KS_EXTRA;
10051 arg[len++] = (int)KE_CSI;
10052 }
10053# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010054 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 {
10056 arg[len++] = *++p;
10057 if (*p == K_SPECIAL)
10058 {
10059 arg[len++] = KS_SPECIAL;
10060 arg[len++] = KE_FILLER;
10061 }
10062# ifdef FEAT_GUI
10063 else if (*p == CSI)
10064 {
10065 arg[len++] = KS_EXTRA;
10066 arg[len++] = (int)KE_CSI;
10067 }
10068# endif
10069 }
10070 arg[len] = NUL;
10071 }
10072 }
10073 }
10074 }
10075#endif
10076
10077 /*
10078 * Save the current typeahead. This is required to allow using ":normal"
10079 * from an event handler and makes sure we don't hang when the argument
10080 * ends with half a command.
10081 */
10082 save_typeahead(&tabuf);
10083 if (tabuf.typebuf_valid)
10084 {
10085 /*
10086 * Repeat the :normal command for each line in the range. When no
10087 * range given, execute it just once, without positioning the cursor
10088 * first.
10089 */
10090 do
10091 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 if (eap->addr_count != 0)
10093 {
10094 curwin->w_cursor.lnum = eap->line1++;
10095 curwin->w_cursor.col = 0;
10096 }
10097
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010098 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099#ifdef FEAT_MBYTE
10100 arg != NULL ? arg :
10101#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010102 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 }
10104 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10105 }
10106
10107 /* Might not return to the main loop when in an event handler. */
10108 update_topline_cursor();
10109
10110 /* Restore the previous typeahead. */
10111 restore_typeahead(&tabuf);
10112
10113 --ex_normal_busy;
10114 msg_scroll = save_msg_scroll;
10115 restart_edit = save_restart_edit;
10116 p_im = save_insertmode;
10117 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010118 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
10120
10121 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +010010122 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +010010124#ifdef FEAT_MOUSE
10125 setmouse();
10126#endif
10127#ifdef CURSOR_SHAPE
10128 ui_cursor_shape(); /* may show different cursor shape */
10129#endif
10130
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131#ifdef FEAT_MBYTE
10132 vim_free(arg);
10133#endif
10134}
10135
10136/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010137 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 */
10139 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010140ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010142 if (eap->forceit)
10143 {
10144 coladvance((colnr_T)MAXCOL);
10145 curwin->w_curswant = MAXCOL;
10146 curwin->w_set_curswant = FALSE;
10147 }
10148
Bram Moolenaara40c5002005-01-09 21:16:21 +000010149 /* Ignore the command when already in Insert mode. Inserting an
10150 * expression register that invokes a function can do this. */
10151 if (State & INSERT)
10152 return;
10153
Bram Moolenaar64969662005-12-14 21:59:55 +000010154 if (eap->cmdidx == CMD_startinsert)
10155 restart_edit = 'a';
10156 else if (eap->cmdidx == CMD_startreplace)
10157 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010159 restart_edit = 'V';
10160
10161 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010163 if (eap->cmdidx == CMD_startinsert)
10164 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165 curwin->w_curswant = 0; /* avoid MAXCOL */
10166 }
10167}
10168
10169/*
10170 * ":stopinsert"
10171 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010173ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174{
10175 restart_edit = 0;
10176 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010177 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010180/*
10181 * Execute normal mode command "cmd".
10182 * "remap" can be REMAP_NONE or REMAP_YES.
10183 */
10184 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010185exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010186{
Bram Moolenaar25281632016-01-21 23:32:32 +010010187 /* Stuff the argument into the typeahead buffer. */
10188 ins_typebuf(cmd, remap, 0, TRUE, silent);
10189 exec_normal(FALSE);
10190}
Bram Moolenaar25281632016-01-21 23:32:32 +010010191
Bram Moolenaar25281632016-01-21 23:32:32 +010010192/*
10193 * Execute normal_cmd() until there is no typeahead left.
10194 */
10195 void
10196exec_normal(int was_typed)
10197{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010198 oparg_T oa;
10199
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010200 clear_oparg(&oa);
10201 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010202 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10203 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010204 {
10205 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010206 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010207 }
10208}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010209
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210#ifdef FEAT_FIND_ID
10211 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010212ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213{
10214 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10215 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10216 (linenr_T)1, (linenr_T)MAXLNUM);
10217}
10218
10219#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
10220/*
10221 * ":psearch"
10222 */
10223 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010224ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225{
10226 g_do_tagpreview = p_pvh;
10227 ex_findpat(eap);
10228 g_do_tagpreview = 0;
10229}
10230#endif
10231
10232 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010233ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234{
10235 int whole = TRUE;
10236 long n;
10237 char_u *p;
10238 int action;
10239
10240 switch (cmdnames[eap->cmdidx].cmd_name[2])
10241 {
10242 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10243 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10244 action = ACTION_GOTO;
10245 else
10246 action = ACTION_SHOW;
10247 break;
10248 case 'i': /* ":ilist" and ":dlist" */
10249 action = ACTION_SHOW_ALL;
10250 break;
10251 case 'u': /* ":ijump" and ":djump" */
10252 action = ACTION_GOTO;
10253 break;
10254 default: /* ":isplit" and ":dsplit" */
10255 action = ACTION_SPLIT;
10256 break;
10257 }
10258
10259 n = 1;
10260 if (vim_isdigit(*eap->arg)) /* get count */
10261 {
10262 n = getdigits(&eap->arg);
10263 eap->arg = skipwhite(eap->arg);
10264 }
10265 if (*eap->arg == '/') /* Match regexp, not just whole words */
10266 {
10267 whole = FALSE;
10268 ++eap->arg;
10269 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10270 if (*p)
10271 {
10272 *p++ = NUL;
10273 p = skipwhite(p);
10274
10275 /* Check for trailing illegal characters */
10276 if (!ends_excmd(*p))
10277 eap->errmsg = e_trailing;
10278 else
10279 eap->nextcmd = check_nextcmd(p);
10280 }
10281 }
10282 if (!eap->skip)
10283 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10284 whole, !eap->forceit,
10285 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10286 n, action, eap->line1, eap->line2);
10287}
10288#endif
10289
10290#ifdef FEAT_WINDOWS
10291
10292# ifdef FEAT_QUICKFIX
10293/*
10294 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10295 */
10296 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010297ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010299 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10301}
10302
10303/*
10304 * ":pedit"
10305 */
10306 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010307ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308{
10309 win_T *curwin_save = curwin;
10310
10311 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010312 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 keep_help_flag = curwin_save->w_buffer->b_help;
10314 do_exedit(eap, NULL);
10315 keep_help_flag = FALSE;
10316 if (curwin != curwin_save && win_valid(curwin_save))
10317 {
10318 /* Return cursor to where we were */
10319 validate_cursor();
10320 redraw_later(VALID);
10321 win_enter(curwin_save, TRUE);
10322 }
10323 g_do_tagpreview = 0;
10324}
10325# endif
10326
10327/*
10328 * ":stag", ":stselect" and ":stjump".
10329 */
10330 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010331ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332{
10333 postponed_split = -1;
10334 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010335 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10337 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010338 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339}
10340#endif
10341
10342/*
10343 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10344 */
10345 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010346ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347{
10348 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10349}
10350
10351 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010352ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353{
10354 int cmd;
10355
10356 switch (name[1])
10357 {
10358 case 'j': cmd = DT_JUMP; /* ":tjump" */
10359 break;
10360 case 's': cmd = DT_SELECT; /* ":tselect" */
10361 break;
10362 case 'p': cmd = DT_PREV; /* ":tprevious" */
10363 break;
10364 case 'N': cmd = DT_PREV; /* ":tNext" */
10365 break;
10366 case 'n': cmd = DT_NEXT; /* ":tnext" */
10367 break;
10368 case 'o': cmd = DT_POP; /* ":pop" */
10369 break;
10370 case 'f': /* ":tfirst" */
10371 case 'r': cmd = DT_FIRST; /* ":trewind" */
10372 break;
10373 case 'l': cmd = DT_LAST; /* ":tlast" */
10374 break;
10375 default: /* ":tag" */
10376#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010377 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378 {
10379 do_cstag(eap);
10380 return;
10381 }
10382#endif
10383 cmd = DT_TAG;
10384 break;
10385 }
10386
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010387 if (name[0] == 'l')
10388 {
10389#ifndef FEAT_QUICKFIX
10390 ex_ni(eap);
10391 return;
10392#else
10393 cmd = DT_LTAG;
10394#endif
10395 }
10396
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10398 eap->forceit, TRUE);
10399}
10400
10401/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010402 * Check "str" for starting with a special cmdline variable.
10403 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10404 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10405 */
10406 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010407find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010408{
10409 int len;
10410 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010411 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010412 "%",
10413#define SPEC_PERC 0
10414 "#",
10415#define SPEC_HASH 1
10416 "<cword>", /* cursor word */
10417#define SPEC_CWORD 2
10418 "<cWORD>", /* cursor WORD */
10419#define SPEC_CCWORD 3
10420 "<cfile>", /* cursor path name */
10421#define SPEC_CFILE 4
10422 "<sfile>", /* ":so" file name */
10423#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010424 "<slnum>", /* ":so" file line number */
10425#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010426#ifdef FEAT_AUTOCMD
10427 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010428# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010429 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010430# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010431 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010432# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010433#endif
10434#ifdef FEAT_CLIENTSERVER
10435 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010436# ifdef FEAT_AUTOCMD
10437# define SPEC_CLIENT 10
10438# else
10439# define SPEC_CLIENT 7
10440# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010441#endif
10442 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010443
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010444 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010445 {
10446 len = (int)STRLEN(spec_str[i]);
10447 if (STRNCMP(src, spec_str[i], len) == 0)
10448 {
10449 *usedlen = len;
10450 return i;
10451 }
10452 }
10453 return -1;
10454}
10455
10456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457 * Evaluate cmdline variables.
10458 *
10459 * change '%' to curbuf->b_ffname
10460 * '#' to curwin->w_altfile
10461 * '<cword>' to word under the cursor
10462 * '<cWORD>' to WORD under the cursor
10463 * '<cfile>' to path name under the cursor
10464 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010465 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466 * '<afile>' to file name for autocommand
10467 * '<abuf>' to buffer number for autocommand
10468 * '<amatch>' to matching name for autocommand
10469 *
10470 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10471 * "" for error without a message) and NULL is returned.
10472 * Returns an allocated string if a valid match was found.
10473 * Returns NULL if no match was found. "usedlen" then still contains the
10474 * number of characters to skip.
10475 */
10476 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010477eval_vars(
10478 char_u *src, /* pointer into commandline */
10479 char_u *srcstart, /* beginning of valid memory for src */
10480 int *usedlen, /* characters after src that are used */
10481 linenr_T *lnump, /* line number for :e command, or NULL */
10482 char_u **errormsg, /* pointer to error message */
10483 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010484 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485{
10486 int i;
10487 char_u *s;
10488 char_u *result;
10489 char_u *resultbuf = NULL;
10490 int resultlen;
10491 buf_T *buf;
10492 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10493 int spec_idx;
10494#ifdef FEAT_MODIFY_FNAME
10495 int skip_mod = FALSE;
10496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498
10499 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010500 if (escaped != NULL)
10501 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502
10503 /*
10504 * Check if there is something to do.
10505 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010506 spec_idx = find_cmdline_var(src, usedlen);
10507 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508 {
10509 *usedlen = 1;
10510 return NULL;
10511 }
10512
10513 /*
10514 * Skip when preceded with a backslash "\%" and "\#".
10515 * Note: In "\\%" the % is also not recognized!
10516 */
10517 if (src > srcstart && src[-1] == '\\')
10518 {
10519 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010520 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 return NULL;
10522 }
10523
10524 /*
10525 * word or WORD under cursor
10526 */
10527 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10528 {
10529 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10530 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10531 if (resultlen == 0)
10532 {
10533 *errormsg = (char_u *)"";
10534 return NULL;
10535 }
10536 }
10537
10538 /*
10539 * '#': Alternate file name
10540 * '%': Current file name
10541 * File name under the cursor
10542 * File name for autocommand
10543 * and following modifiers
10544 */
10545 else
10546 {
10547 switch (spec_idx)
10548 {
10549 case SPEC_PERC: /* '%': current file */
10550 if (curbuf->b_fname == NULL)
10551 {
10552 result = (char_u *)"";
10553 valid = 0; /* Must have ":p:h" to be valid */
10554 }
10555 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 break;
10558
10559 case SPEC_HASH: /* '#' or "#99": alternate file */
10560 if (src[1] == '#') /* "##": the argument list */
10561 {
10562 result = arg_all();
10563 resultbuf = result;
10564 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010565 if (escaped != NULL)
10566 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567#ifdef FEAT_MODIFY_FNAME
10568 skip_mod = TRUE;
10569#endif
10570 break;
10571 }
10572 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010573 if (*s == '<') /* "#<99" uses v:oldfiles */
10574 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575 i = (int)getdigits(&s);
10576 *usedlen = (int)(s - src); /* length of what we expand */
10577
Bram Moolenaard812df62008-11-09 12:46:09 +000010578 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010580 if (*usedlen < 2)
10581 {
10582 /* Should we give an error message for #<text? */
10583 *usedlen = 1;
10584 return NULL;
10585 }
10586#ifdef FEAT_EVAL
10587 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10588 (long)i);
10589 if (result == NULL)
10590 {
10591 *errormsg = (char_u *)"";
10592 return NULL;
10593 }
10594#else
10595 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598 }
10599 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010600 {
10601 buf = buflist_findnr(i);
10602 if (buf == NULL)
10603 {
10604 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10605 return NULL;
10606 }
10607 if (lnump != NULL)
10608 *lnump = ECMD_LAST;
10609 if (buf->b_fname == NULL)
10610 {
10611 result = (char_u *)"";
10612 valid = 0; /* Must have ":p:h" to be valid */
10613 }
10614 else
10615 result = buf->b_fname;
10616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617 break;
10618
10619#ifdef FEAT_SEARCHPATH
10620 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010621 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622 if (result == NULL)
10623 {
10624 *errormsg = (char_u *)"";
10625 return NULL;
10626 }
10627 resultbuf = result; /* remember allocated string */
10628 break;
10629#endif
10630
10631#ifdef FEAT_AUTOCMD
10632 case SPEC_AFILE: /* file name for autocommand */
10633 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010634 if (result != NULL && !autocmd_fname_full)
10635 {
10636 /* Still need to turn the fname into a full path. It is
10637 * postponed to avoid a delay when <afile> is not used. */
10638 autocmd_fname_full = TRUE;
10639 result = FullName_save(autocmd_fname, FALSE);
10640 vim_free(autocmd_fname);
10641 autocmd_fname = result;
10642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643 if (result == NULL)
10644 {
10645 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10646 return NULL;
10647 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010648 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649 break;
10650
10651 case SPEC_ABUF: /* buffer number for autocommand */
10652 if (autocmd_bufnr <= 0)
10653 {
10654 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10655 return NULL;
10656 }
10657 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10658 result = strbuf;
10659 break;
10660
10661 case SPEC_AMATCH: /* match name for autocommand */
10662 result = autocmd_match;
10663 if (result == NULL)
10664 {
10665 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10666 return NULL;
10667 }
10668 break;
10669
10670#endif
10671 case SPEC_SFILE: /* file name for ":so" command */
10672 result = sourcing_name;
10673 if (result == NULL)
10674 {
10675 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10676 return NULL;
10677 }
10678 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010679 case SPEC_SLNUM: /* line in file for ":so" command */
10680 if (sourcing_name == NULL || sourcing_lnum == 0)
10681 {
10682 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10683 return NULL;
10684 }
10685 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10686 result = strbuf;
10687 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688#if defined(FEAT_CLIENTSERVER)
10689 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010690 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10691 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692 result = strbuf;
10693 break;
10694#endif
10695 }
10696
10697 resultlen = (int)STRLEN(result); /* length of new string */
10698 if (src[*usedlen] == '<') /* remove the file name extension */
10699 {
10700 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702 resultlen = (int)(s - result);
10703 }
10704#ifdef FEAT_MODIFY_FNAME
10705 else if (!skip_mod)
10706 {
10707 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10708 &resultlen);
10709 if (result == NULL)
10710 {
10711 *errormsg = (char_u *)"";
10712 return NULL;
10713 }
10714 }
10715#endif
10716 }
10717
10718 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10719 {
10720 if (valid != VALID_HEAD + VALID_PATH)
10721 /* xgettext:no-c-format */
10722 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10723 else
10724 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10725 result = NULL;
10726 }
10727 else
10728 result = vim_strnsave(result, resultlen);
10729 vim_free(resultbuf);
10730 return result;
10731}
10732
10733/*
10734 * Concatenate all files in the argument list, separated by spaces, and return
10735 * it in one allocated string.
10736 * Spaces and backslashes in the file names are escaped with a backslash.
10737 * Returns NULL when out of memory.
10738 */
10739 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010740arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741{
10742 int len;
10743 int idx;
10744 char_u *retval = NULL;
10745 char_u *p;
10746
10747 /*
10748 * Do this loop two times:
10749 * first time: compute the total length
10750 * second time: concatenate the names
10751 */
10752 for (;;)
10753 {
10754 len = 0;
10755 for (idx = 0; idx < ARGCOUNT; ++idx)
10756 {
10757 p = alist_name(&ARGLIST[idx]);
10758 if (p != NULL)
10759 {
10760 if (len > 0)
10761 {
10762 /* insert a space in between names */
10763 if (retval != NULL)
10764 retval[len] = ' ';
10765 ++len;
10766 }
10767 for ( ; *p != NUL; ++p)
10768 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010769 if (*p == ' '
10770#ifndef BACKSLASH_IN_FILENAME
10771 || *p == '\\'
10772#endif
10773 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774 {
10775 /* insert a backslash */
10776 if (retval != NULL)
10777 retval[len] = '\\';
10778 ++len;
10779 }
10780 if (retval != NULL)
10781 retval[len] = *p;
10782 ++len;
10783 }
10784 }
10785 }
10786
10787 /* second time: break here */
10788 if (retval != NULL)
10789 {
10790 retval[len] = NUL;
10791 break;
10792 }
10793
10794 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010795 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796 if (retval == NULL)
10797 break;
10798 }
10799
10800 return retval;
10801}
10802
10803#if defined(FEAT_AUTOCMD) || defined(PROTO)
10804/*
10805 * Expand the <sfile> string in "arg".
10806 *
10807 * Returns an allocated string, or NULL for any error.
10808 */
10809 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010810expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811{
10812 char_u *errormsg;
10813 int len;
10814 char_u *result;
10815 char_u *newres;
10816 char_u *repl;
10817 int srclen;
10818 char_u *p;
10819
10820 result = vim_strsave(arg);
10821 if (result == NULL)
10822 return NULL;
10823
10824 for (p = result; *p; )
10825 {
10826 if (STRNCMP(p, "<sfile>", 7) != 0)
10827 ++p;
10828 else
10829 {
10830 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010831 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832 if (errormsg != NULL)
10833 {
10834 if (*errormsg)
10835 emsg(errormsg);
10836 vim_free(result);
10837 return NULL;
10838 }
10839 if (repl == NULL) /* no match (cannot happen) */
10840 {
10841 p += srclen;
10842 continue;
10843 }
10844 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10845 newres = alloc(len);
10846 if (newres == NULL)
10847 {
10848 vim_free(repl);
10849 vim_free(result);
10850 return NULL;
10851 }
10852 mch_memmove(newres, result, (size_t)(p - result));
10853 STRCPY(newres + (p - result), repl);
10854 len = (int)STRLEN(newres);
10855 STRCAT(newres, p + srclen);
10856 vim_free(repl);
10857 vim_free(result);
10858 result = newres;
10859 p = newres + len; /* continue after the match */
10860 }
10861 }
10862
10863 return result;
10864}
10865#endif
10866
10867#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010010868static int ses_winsizes(FILE *fd, int restore_size,
10869 win_T *tab_firstwin);
10870static int ses_win_rec(FILE *fd, frame_T *fr);
10871static frame_T *ses_skipframe(frame_T *fr);
10872static int ses_do_frame(frame_T *fr);
10873static int ses_do_win(win_T *wp);
10874static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
10875static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
10876static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877
10878/*
10879 * Write openfile commands for the current buffers to an .exrc file.
10880 * Return FAIL on error, OK otherwise.
10881 */
10882 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010883makeopens(
10884 FILE *fd,
10885 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886{
10887 buf_T *buf;
10888 int only_save_windows = TRUE;
10889 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890 int restore_size = TRUE;
10891 win_T *wp;
10892 char_u *sname;
10893 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010894 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010895 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010896 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010897 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010898 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010899 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900
10901 if (ssop_flags & SSOP_BUFFERS)
10902 only_save_windows = FALSE; /* Save ALL buffers */
10903
10904 /*
10905 * Begin by setting the this_session variable, and then other
10906 * sessionable variables.
10907 */
10908#ifdef FEAT_EVAL
10909 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10910 return FAIL;
10911 if (ssop_flags & SSOP_GLOBALS)
10912 if (store_session_globals(fd) == FAIL)
10913 return FAIL;
10914#endif
10915
10916 /*
10917 * Close all windows but one.
10918 */
10919 if (put_line(fd, "silent only") == FAIL)
10920 return FAIL;
10921
10922 /*
10923 * Now a :cd command to the session directory or the current directory
10924 */
10925 if (ssop_flags & SSOP_SESDIR)
10926 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010927 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10928 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929 return FAIL;
10930 }
10931 else if (ssop_flags & SSOP_CURDIR)
10932 {
10933 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10934 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010935 || fputs("cd ", fd) < 0
10936 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10937 || put_eol(fd) == FAIL)
10938 {
10939 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942 vim_free(sname);
10943 }
10944
10945 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010946 * If there is an empty, unnamed buffer we will wipe it out later.
10947 * Remember the buffer number.
10948 */
10949 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10950 return FAIL;
10951 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10952 return FAIL;
10953 if (put_line(fd, "endif") == FAIL)
10954 return FAIL;
10955
10956 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957 * Now save the current files, current buffer first.
10958 */
10959 if (put_line(fd, "set shortmess=aoO") == FAIL)
10960 return FAIL;
10961
10962 /* Now put the other buffers into the buffer list */
10963 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10964 {
10965 if (!(only_save_windows && buf->b_nwindows == 0)
10966 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10967 && buf->b_fname != NULL
10968 && buf->b_p_bl)
10969 {
10970 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10971 : buf->b_wininfo->wi_fpos.lnum) < 0
10972 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10973 return FAIL;
10974 }
10975 }
10976
10977 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010978 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10980 return FAIL;
10981
10982 if (ssop_flags & SSOP_RESIZE)
10983 {
10984 /* Note: after the restore we still check it worked!*/
10985 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10986 || put_eol(fd) == FAIL)
10987 return FAIL;
10988 }
10989
10990#ifdef FEAT_GUI
10991 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10992 {
10993 int x, y;
10994
10995 if (gui_mch_get_winpos(&x, &y) == OK)
10996 {
10997 /* Note: after the restore we still check it worked!*/
10998 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10999 return FAIL;
11000 }
11001 }
11002#endif
11003
11004 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011005 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11006 * will be displayed when creating the next tab. That resizes the windows
11007 * in the first tab, which may cause problems. Set 'showtabline' to 2
11008 * temporarily to avoid that.
11009 */
11010 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11011 {
11012 if (put_line(fd, "set stal=2") == FAIL)
11013 return FAIL;
11014 restore_stal = TRUE;
11015 }
11016
11017 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011018 * May repeat putting Windows for each tab, when "tabpages" is in
11019 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011020 * Don't use goto_tabpage(), it may change directory and trigger
11021 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011023 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011024 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011025 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011027 int need_tabnew = FALSE;
11028 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011029
Bram Moolenaar18144c82006-04-12 21:52:12 +000011030 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011032 tabpage_T *tp = find_tabpage(tabnr);
11033
11034 if (tp == NULL)
11035 break; /* done all tab pages */
11036 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011037 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011038 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011039 tab_topframe = topframe;
11040 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011041 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011042 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011043 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011044 tab_topframe = tp->tp_topframe;
11045 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011046 if (tabnr > 1)
11047 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049
Bram Moolenaar18144c82006-04-12 21:52:12 +000011050 /*
11051 * Before creating the window layout, try loading one file. If this
11052 * is aborted we don't end up with a number of useless windows.
11053 * This may have side effects! (e.g., compressed or network file).
11054 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011055 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011056 {
11057 if (ses_do_win(wp)
11058 && wp->w_buffer->b_ffname != NULL
11059 && !wp->w_buffer->b_help
11060#ifdef FEAT_QUICKFIX
11061 && !bt_nofile(wp->w_buffer)
11062#endif
11063 )
11064 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011065 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000011066 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
11067 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011068 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011069 if (!wp->w_arg_idx_invalid)
11070 edited_win = wp;
11071 break;
11072 }
11073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011075 /* If no file got edited create an empty tab page. */
11076 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11077 return FAIL;
11078
Bram Moolenaar18144c82006-04-12 21:52:12 +000011079 /*
11080 * Save current window layout.
11081 */
11082 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011084 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011085 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011086 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11087 return FAIL;
11088 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11089 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090
Bram Moolenaar18144c82006-04-12 21:52:12 +000011091 /*
11092 * Check if window sizes can be restored (no windows omitted).
11093 * Remember the window number of the current window after restoring.
11094 */
11095 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011096 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011097 {
11098 if (ses_do_win(wp))
11099 ++nr;
11100 else
11101 restore_size = FALSE;
11102 if (curwin == wp)
11103 cnr = nr;
11104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105
Bram Moolenaar18144c82006-04-12 21:52:12 +000011106 /* Go to the first window. */
11107 if (put_line(fd, "wincmd t") == FAIL)
11108 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011109
Bram Moolenaar18144c82006-04-12 21:52:12 +000011110 /*
11111 * If more than one window, see if sizes can be restored.
11112 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11113 * resized when moving between windows.
11114 * Do this before restoring the view, so that the topline and the
11115 * cursor can be set. This is done again below.
11116 */
11117 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
11118 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011119 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011120 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121
Bram Moolenaar18144c82006-04-12 21:52:12 +000011122 /*
11123 * Restore the view of the window (options, file, cursor, etc.).
11124 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011125 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011126 {
11127 if (!ses_do_win(wp))
11128 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011129 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11130 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011131 return FAIL;
11132 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11133 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011134 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011135 }
11136
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011137 /* The argument index in the first tab page is zero, need to set it in
11138 * each window. For further tab pages it's the window where we do
11139 * "tabedit". */
11140 cur_arg_idx = next_arg_idx;
11141
Bram Moolenaar18144c82006-04-12 21:52:12 +000011142 /*
11143 * Restore cursor to the current window if it's not the first one.
11144 */
11145 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11146 || put_eol(fd) == FAIL))
11147 return FAIL;
11148
11149 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011150 * Restore window sizes again after jumping around in windows, because
11151 * the current window has a minimum size while others may not.
11152 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011153 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011154 return FAIL;
11155
Bram Moolenaar18144c82006-04-12 21:52:12 +000011156 /* Don't continue in another tab page when doing only the current one
11157 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011158 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011159 break;
11160 }
11161
11162 if (ssop_flags & SSOP_TABPAGES)
11163 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011164 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11165 || put_eol(fd) == FAIL)
11166 return FAIL;
11167 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011168 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11169 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011170
Bram Moolenaar9c102382006-05-03 21:26:49 +000011171 /*
11172 * Wipe out an empty unnamed buffer we started in.
11173 */
11174 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11175 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011176 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011177 return FAIL;
11178 if (put_line(fd, "endif") == FAIL)
11179 return FAIL;
11180 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11181 return FAIL;
11182
11183 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11184 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11185 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11186 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187
11188 /*
11189 * Lastly, execute the x.vim file if it exists.
11190 */
11191 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11192 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011193 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011194 || put_line(fd, "endif") == FAIL)
11195 return FAIL;
11196
11197 return OK;
11198}
11199
11200 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011201ses_winsizes(
11202 FILE *fd,
11203 int restore_size,
11204 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205{
11206 int n = 0;
11207 win_T *wp;
11208
11209 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11210 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011211 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212 {
11213 if (!ses_do_win(wp))
11214 continue;
11215 ++n;
11216
11217 /* restore height when not full height */
11218 if (wp->w_height + wp->w_status_height < topframe->fr_height
11219 && (fprintf(fd,
11220 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11221 n, (long)wp->w_height, Rows / 2, Rows) < 0
11222 || put_eol(fd) == FAIL))
11223 return FAIL;
11224
11225 /* restore width when not full width */
11226 if (wp->w_width < Columns && (fprintf(fd,
11227 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11228 n, (long)wp->w_width, Columns / 2, Columns) < 0
11229 || put_eol(fd) == FAIL))
11230 return FAIL;
11231 }
11232 }
11233 else
11234 {
11235 /* Just equalise window sizes */
11236 if (put_line(fd, "wincmd =") == FAIL)
11237 return FAIL;
11238 }
11239 return OK;
11240}
11241
11242/*
11243 * Write commands to "fd" to recursively create windows for frame "fr",
11244 * horizontally and vertically split.
11245 * After the commands the last window in the frame is the current window.
11246 * Returns FAIL when writing the commands to "fd" fails.
11247 */
11248 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011249ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250{
11251 frame_T *frc;
11252 int count = 0;
11253
11254 if (fr->fr_layout != FR_LEAF)
11255 {
11256 /* Find first frame that's not skipped and then create a window for
11257 * each following one (first frame is already there). */
11258 frc = ses_skipframe(fr->fr_child);
11259 if (frc != NULL)
11260 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11261 {
11262 /* Make window as big as possible so that we have lots of room
11263 * to split. */
11264 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11265 || put_line(fd, fr->fr_layout == FR_COL
11266 ? "split" : "vsplit") == FAIL)
11267 return FAIL;
11268 ++count;
11269 }
11270
11271 /* Go back to the first window. */
11272 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11273 ? "%dwincmd k" : "%dwincmd h", count) < 0
11274 || put_eol(fd) == FAIL))
11275 return FAIL;
11276
11277 /* Recursively create frames/windows in each window of this column or
11278 * row. */
11279 frc = ses_skipframe(fr->fr_child);
11280 while (frc != NULL)
11281 {
11282 ses_win_rec(fd, frc);
11283 frc = ses_skipframe(frc->fr_next);
11284 /* Go to next window. */
11285 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11286 return FAIL;
11287 }
11288 }
11289 return OK;
11290}
11291
11292/*
11293 * Skip frames that don't contain windows we want to save in the Session.
11294 * Returns NULL when there none.
11295 */
11296 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011297ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298{
11299 frame_T *frc;
11300
11301 for (frc = fr; frc != NULL; frc = frc->fr_next)
11302 if (ses_do_frame(frc))
11303 break;
11304 return frc;
11305}
11306
11307/*
11308 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11309 * the Session.
11310 */
11311 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011312ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313{
11314 frame_T *frc;
11315
11316 if (fr->fr_layout == FR_LEAF)
11317 return ses_do_win(fr->fr_win);
11318 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11319 if (ses_do_frame(frc))
11320 return TRUE;
11321 return FALSE;
11322}
11323
11324/*
11325 * Return non-zero if window "wp" is to be stored in the Session.
11326 */
11327 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011328ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329{
11330 if (wp->w_buffer->b_fname == NULL
11331#ifdef FEAT_QUICKFIX
11332 /* When 'buftype' is "nofile" can't restore the window contents. */
11333 || bt_nofile(wp->w_buffer)
11334#endif
11335 )
11336 return (ssop_flags & SSOP_BLANK);
11337 if (wp->w_buffer->b_help)
11338 return (ssop_flags & SSOP_HELP);
11339 return TRUE;
11340}
11341
11342/*
11343 * Write commands to "fd" to restore the view of a window.
11344 * Caller must make sure 'scrolloff' is zero.
11345 */
11346 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011347put_view(
11348 FILE *fd,
11349 win_T *wp,
11350 int add_edit, /* add ":edit" command to view */
11351 unsigned *flagp, /* vop_flags or ssop_flags */
11352 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011353 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354{
11355 win_T *save_curwin;
11356 int f;
11357 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011358 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359
11360 /* Always restore cursor position for ":mksession". For ":mkview" only
11361 * when 'viewoptions' contains "cursor". */
11362 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11363
11364 /*
11365 * Local argument list.
11366 */
11367 if (wp->w_alist == &global_alist)
11368 {
11369 if (put_line(fd, "argglobal") == FAIL)
11370 return FAIL;
11371 }
11372 else
11373 {
11374 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11375 flagp == &vop_flags
11376 || !(*flagp & SSOP_CURDIR)
11377 || wp->w_localdir != NULL, flagp) == FAIL)
11378 return FAIL;
11379 }
11380
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011381 /* Only when part of a session: restore the argument index. Some
11382 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011383 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011384 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011386 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011387 || put_eol(fd) == FAIL)
11388 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011389 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390 }
11391
11392 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011393 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394 {
11395 /*
11396 * Load the file.
11397 */
11398 if (wp->w_buffer->b_ffname != NULL
11399#ifdef FEAT_QUICKFIX
11400 && !bt_nofile(wp->w_buffer)
11401#endif
11402 )
11403 {
11404 /*
11405 * Editing a file in this buffer: use ":edit file".
11406 * This may have side effects! (e.g., compressed or network file).
11407 */
11408 if (fputs("edit ", fd) < 0
11409 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11410 return FAIL;
11411 }
11412 else
11413 {
11414 /* No file in this buffer, just make it empty. */
11415 if (put_line(fd, "enew") == FAIL)
11416 return FAIL;
11417#ifdef FEAT_QUICKFIX
11418 if (wp->w_buffer->b_ffname != NULL)
11419 {
11420 /* The buffer does have a name, but it's not a file name. */
11421 if (fputs("file ", fd) < 0
11422 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11423 return FAIL;
11424 }
11425#endif
11426 do_cursor = FALSE;
11427 }
11428 }
11429
11430 /*
11431 * Local mappings and abbreviations.
11432 */
11433 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11434 && makemap(fd, wp->w_buffer) == FAIL)
11435 return FAIL;
11436
11437 /*
11438 * Local options. Need to go to the window temporarily.
11439 * Store only local values when using ":mkview" and when ":mksession" is
11440 * used and 'sessionoptions' doesn't include "options".
11441 * Some folding options are always stored when "folds" is included,
11442 * otherwise the folds would not be restored correctly.
11443 */
11444 save_curwin = curwin;
11445 curwin = wp;
11446 curbuf = curwin->w_buffer;
11447 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11448 f = makeset(fd, OPT_LOCAL,
11449 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11450#ifdef FEAT_FOLDING
11451 else if (*flagp & SSOP_FOLDS)
11452 f = makefoldset(fd);
11453#endif
11454 else
11455 f = OK;
11456 curwin = save_curwin;
11457 curbuf = curwin->w_buffer;
11458 if (f == FAIL)
11459 return FAIL;
11460
11461#ifdef FEAT_FOLDING
11462 /*
11463 * Save Folds when 'buftype' is empty and for help files.
11464 */
11465 if ((*flagp & SSOP_FOLDS)
11466 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011467# ifdef FEAT_QUICKFIX
11468 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11469# endif
11470 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011471 {
11472 if (put_folds(fd, wp) == FAIL)
11473 return FAIL;
11474 }
11475#endif
11476
11477 /*
11478 * Set the cursor after creating folds, since that moves the cursor.
11479 */
11480 if (do_cursor)
11481 {
11482
11483 /* Restore the cursor line in the file and relatively in the
11484 * window. Don't use "G", it changes the jumplist. */
11485 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11486 (long)wp->w_cursor.lnum,
11487 (long)(wp->w_cursor.lnum - wp->w_topline),
11488 (long)wp->w_height / 2, (long)wp->w_height) < 0
11489 || put_eol(fd) == FAIL
11490 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11491 || put_line(fd, "exe s:l") == FAIL
11492 || put_line(fd, "normal! zt") == FAIL
11493 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11494 || put_eol(fd) == FAIL)
11495 return FAIL;
11496 /* Restore the cursor column and left offset when not wrapping. */
11497 if (wp->w_cursor.col == 0)
11498 {
11499 if (put_line(fd, "normal! 0") == FAIL)
11500 return FAIL;
11501 }
11502 else
11503 {
11504 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11505 {
11506 if (fprintf(fd,
11507 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011508 (long)wp->w_virtcol + 1,
11509 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011510 (long)wp->w_width / 2, (long)wp->w_width) < 0
11511 || put_eol(fd) == FAIL
11512 || put_line(fd, "if s:c > 0") == FAIL
11513 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011514 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11515 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011516 || put_eol(fd) == FAIL
11517 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011518 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011519 || put_eol(fd) == FAIL
11520 || put_line(fd, "endif") == FAIL)
11521 return FAIL;
11522 }
11523 else
11524 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011525 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011526 || put_eol(fd) == FAIL)
11527 return FAIL;
11528 }
11529 }
11530 }
11531
11532 /*
11533 * Local directory.
11534 */
11535 if (wp->w_localdir != NULL)
11536 {
11537 if (fputs("lcd ", fd) < 0
11538 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11539 || put_eol(fd) == FAIL)
11540 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011541 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542 }
11543
11544 return OK;
11545}
11546
11547/*
11548 * Write an argument list to the session file.
11549 * Returns FAIL if writing fails.
11550 */
11551 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011552ses_arglist(
11553 FILE *fd,
11554 char *cmd,
11555 garray_T *gap,
11556 int fullname, /* TRUE: use full path name */
11557 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011558{
11559 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011560 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011561 char_u *s;
11562
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011563 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11564 return FAIL;
11565 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566 return FAIL;
11567 for (i = 0; i < gap->ga_len; ++i)
11568 {
11569 /* NULL file names are skipped (only happens when out of memory). */
11570 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11571 if (s != NULL)
11572 {
11573 if (fullname)
11574 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011575 buf = alloc(MAXPATHL);
11576 if (buf != NULL)
11577 {
11578 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11579 s = buf;
11580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011581 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011582 if (fputs("argadd ", fd) < 0
11583 || ses_put_fname(fd, s, flagp) == FAIL
11584 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011585 {
11586 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011587 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011588 }
11589 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590 }
11591 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011592 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011593}
11594
11595/*
11596 * Write a buffer name to the session file.
11597 * Also ends the line.
11598 * Returns FAIL if writing fails.
11599 */
11600 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011601ses_fname(FILE *fd, buf_T *buf, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011602{
11603 char_u *name;
11604
11605 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011606 * the session file will be sourced.
11607 * Don't do this for ":mkview", we don't know the current directory.
11608 * Don't do this after ":lcd", we don't keep track of what the current
11609 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011610 if (buf->b_sfname != NULL
11611 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011612 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011613#ifdef FEAT_AUTOCHDIR
11614 && !p_acd
11615#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011616 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011617 name = buf->b_sfname;
11618 else
11619 name = buf->b_ffname;
11620 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11621 return FAIL;
11622 return OK;
11623}
11624
11625/*
11626 * Write a file name to the session file.
11627 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11628 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011629 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011630 */
11631 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011632ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011633{
11634 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011635 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011636 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011637
11638 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011639 if (sname == NULL)
11640 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011641
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011642 if (*flagp & SSOP_SLASH)
11643 {
11644 /* change all backslashes to forward slashes */
11645 for (p = sname; *p != NUL; mb_ptr_adv(p))
11646 if (*p == '\\')
11647 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011648 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011649
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011650 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011651 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011652 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011653 if (p == NULL)
11654 return FAIL;
11655
11656 /* write the result */
11657 if (fputs((char *)p, fd) < 0)
11658 retval = FAIL;
11659
11660 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011661 return retval;
11662}
11663
11664/*
11665 * ":loadview [nr]"
11666 */
11667 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011668ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011669{
11670 char_u *fname;
11671
11672 fname = get_view_file(*eap->arg);
11673 if (fname != NULL)
11674 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011675 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011676 vim_free(fname);
11677 }
11678}
11679
11680/*
11681 * Get the name of the view file for the current buffer.
11682 */
11683 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011684get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011685{
11686 int len = 0;
11687 char_u *p, *s;
11688 char_u *retval;
11689 char_u *sname;
11690
11691 if (curbuf->b_ffname == NULL)
11692 {
11693 EMSG(_(e_noname));
11694 return NULL;
11695 }
11696 sname = home_replace_save(NULL, curbuf->b_ffname);
11697 if (sname == NULL)
11698 return NULL;
11699
11700 /*
11701 * We want a file name without separators, because we're not going to make
11702 * a directory.
11703 * "normal" path separator -> "=+"
11704 * "=" -> "=="
11705 * ":" path separator -> "=-"
11706 */
11707 for (p = sname; *p; ++p)
11708 if (*p == '=' || vim_ispathsep(*p))
11709 ++len;
11710 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11711 if (retval != NULL)
11712 {
11713 STRCPY(retval, p_vdir);
11714 add_pathsep(retval);
11715 s = retval + STRLEN(retval);
11716 for (p = sname; *p; ++p)
11717 {
11718 if (*p == '=')
11719 {
11720 *s++ = '=';
11721 *s++ = '=';
11722 }
11723 else if (vim_ispathsep(*p))
11724 {
11725 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011726#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727 if (*p == ':')
11728 *s++ = '-';
11729 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011730#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011731 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011732 }
11733 else
11734 *s++ = *p;
11735 }
11736 *s++ = '=';
11737 *s++ = c;
11738 STRCPY(s, ".vim");
11739 }
11740
11741 vim_free(sname);
11742 return retval;
11743}
11744
11745#endif /* FEAT_SESSION */
11746
11747/*
11748 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11749 * Return FAIL for a write error.
11750 */
11751 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011752put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011753{
11754 if (
11755#ifdef USE_CRNL
11756 (
11757# ifdef MKSESSION_NL
11758 !mksession_nl &&
11759# endif
11760 (putc('\r', fd) < 0)) ||
11761#endif
11762 (putc('\n', fd) < 0))
11763 return FAIL;
11764 return OK;
11765}
11766
11767/*
11768 * Write a line to "fd".
11769 * Return FAIL for a write error.
11770 */
11771 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011772put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011773{
11774 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11775 return FAIL;
11776 return OK;
11777}
11778
11779#ifdef FEAT_VIMINFO
11780/*
11781 * ":rviminfo" and ":wviminfo".
11782 */
11783 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011784ex_viminfo(
11785 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011786{
11787 char_u *save_viminfo;
11788
11789 save_viminfo = p_viminfo;
11790 if (*p_viminfo == NUL)
11791 p_viminfo = (char_u *)"'100";
11792 if (eap->cmdidx == CMD_rviminfo)
11793 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011794 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11795 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011796 EMSG(_("E195: Cannot open viminfo file for reading"));
11797 }
11798 else
11799 write_viminfo(eap->arg, eap->forceit);
11800 p_viminfo = save_viminfo;
11801}
11802#endif
11803
11804#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011805/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011806 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011807 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011808 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011810dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011812 if (fname == NULL)
11813 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011814 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011815}
11816#endif
11817
11818/*
11819 * ":behave {mswin,xterm}"
11820 */
11821 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011822ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011823{
11824 if (STRCMP(eap->arg, "mswin") == 0)
11825 {
11826 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11827 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11828 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11829 set_option_value((char_u *)"keymodel", 0L,
11830 (char_u *)"startsel,stopsel", 0);
11831 }
11832 else if (STRCMP(eap->arg, "xterm") == 0)
11833 {
11834 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11835 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11836 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11837 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11838 }
11839 else
11840 EMSG2(_(e_invarg2), eap->arg);
11841}
11842
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011843#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11844/*
11845 * Function given to ExpandGeneric() to obtain the possible arguments of the
11846 * ":behave {mswin,xterm}" command.
11847 */
11848 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011849get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011850{
11851 if (idx == 0)
11852 return (char_u *)"mswin";
11853 if (idx == 1)
11854 return (char_u *)"xterm";
11855 return NULL;
11856}
11857#endif
11858
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859#ifdef FEAT_AUTOCMD
11860static int filetype_detect = FALSE;
11861static int filetype_plugin = FALSE;
11862static int filetype_indent = FALSE;
11863
11864/*
11865 * ":filetype [plugin] [indent] {on,off,detect}"
11866 * on: Load the filetype.vim file to install autocommands for file types.
11867 * off: Load the ftoff.vim file to remove all autocommands for file types.
11868 * plugin on: load filetype.vim and ftplugin.vim
11869 * plugin off: load ftplugof.vim
11870 * indent on: load filetype.vim and indent.vim
11871 * indent off: load indoff.vim
11872 */
11873 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011874ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011875{
11876 char_u *arg = eap->arg;
11877 int plugin = FALSE;
11878 int indent = FALSE;
11879
11880 if (*eap->arg == NUL)
11881 {
11882 /* Print current status. */
11883 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11884 filetype_detect ? "ON" : "OFF",
11885 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11886 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11887 return;
11888 }
11889
11890 /* Accept "plugin" and "indent" in any order. */
11891 for (;;)
11892 {
11893 if (STRNCMP(arg, "plugin", 6) == 0)
11894 {
11895 plugin = TRUE;
11896 arg = skipwhite(arg + 6);
11897 continue;
11898 }
11899 if (STRNCMP(arg, "indent", 6) == 0)
11900 {
11901 indent = TRUE;
11902 arg = skipwhite(arg + 6);
11903 continue;
11904 }
11905 break;
11906 }
11907 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11908 {
11909 if (*arg == 'o' || !filetype_detect)
11910 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011911 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011912 filetype_detect = TRUE;
11913 if (plugin)
11914 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011915 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011916 filetype_plugin = TRUE;
11917 }
11918 if (indent)
11919 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011920 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921 filetype_indent = TRUE;
11922 }
11923 }
11924 if (*arg == 'd')
11925 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020011926 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011927 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928 }
11929 }
11930 else if (STRCMP(arg, "off") == 0)
11931 {
11932 if (plugin || indent)
11933 {
11934 if (plugin)
11935 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011936 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011937 filetype_plugin = FALSE;
11938 }
11939 if (indent)
11940 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011941 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011942 filetype_indent = FALSE;
11943 }
11944 }
11945 else
11946 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011947 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011948 filetype_detect = FALSE;
11949 }
11950 }
11951 else
11952 EMSG2(_(e_invarg2), arg);
11953}
11954
11955/*
11956 * ":setfiletype {name}"
11957 */
11958 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011959ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011960{
11961 if (!did_filetype)
11962 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11963}
11964#endif
11965
Bram Moolenaar071d4272004-06-13 20:20:40 +000011966 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011967ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011968{
11969#ifdef FEAT_DIGRAPHS
11970 if (*eap->arg != NUL)
11971 putdigraph(eap->arg);
11972 else
11973 listdigraphs();
11974#else
11975 EMSG(_("E196: No digraphs in this version"));
11976#endif
11977}
11978
11979 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011980ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981{
11982 int flags = 0;
11983
11984 if (eap->cmdidx == CMD_setlocal)
11985 flags = OPT_LOCAL;
11986 else if (eap->cmdidx == CMD_setglobal)
11987 flags = OPT_GLOBAL;
11988#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11989 if (cmdmod.browse && flags == 0)
11990 ex_options(eap);
11991 else
11992#endif
11993 (void)do_set(eap->arg, flags);
11994}
11995
11996#ifdef FEAT_SEARCH_EXTRA
11997/*
11998 * ":nohlsearch"
11999 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012001ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012002{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010012003 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012004 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005}
12006
12007/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012008 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012009 * Sets nextcmd to the start of the next command, if any. Also called when
12010 * skipping commands to find the next command.
12011 */
12012 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012013ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012014{
12015 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012016 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012017 char_u *end;
12018 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012019 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012020
12021 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012022 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012023 else
12024 {
12025 EMSG(e_invcmd);
12026 return;
12027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012028
12029 /* First clear any old pattern. */
12030 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012031 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032
12033 if (ends_excmd(*eap->arg))
12034 end = eap->arg;
12035 else if ((STRNICMP(eap->arg, "none", 4) == 0
12036 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
12037 end = eap->arg + 4;
12038 else
12039 {
12040 p = skiptowhite(eap->arg);
12041 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012042 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012043 p = skipwhite(p);
12044 if (*p == NUL)
12045 {
12046 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012047 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012048 EMSG2(_(e_invarg2), eap->arg);
12049 return;
12050 }
12051 end = skip_regexp(p + 1, *p, TRUE, NULL);
12052 if (!eap->skip)
12053 {
12054 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12055 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012056 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057 eap->errmsg = e_trailing;
12058 return;
12059 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012060 if (*end != *p)
12061 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012062 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012063 EMSG2(_(e_invarg2), p);
12064 return;
12065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012066
12067 c = *end;
12068 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012069 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012070 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012071 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012072 }
12073 }
12074 eap->nextcmd = find_nextcmd(end);
12075}
12076#endif
12077
12078#ifdef FEAT_CRYPT
12079/*
12080 * ":X": Get crypt key
12081 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012083ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012085 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012086 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012087}
12088#endif
12089
12090#ifdef FEAT_FOLDING
12091 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012092ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012093{
12094 if (foldManualAllowed(TRUE))
12095 foldCreate(eap->line1, eap->line2);
12096}
12097
12098 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012099ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100{
12101 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12102 eap->forceit, FALSE);
12103}
12104
12105 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012106ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012107{
12108 linenr_T lnum;
12109
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012110#ifdef FEAT_CLIPBOARD
12111 start_global_changes();
12112#endif
12113
Bram Moolenaar071d4272004-06-13 20:20:40 +000012114 /* First set the marks for all lines closed/open. */
12115 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12116 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12117 ml_setmarked(lnum);
12118
12119 /* Execute the command on the marked lines. */
12120 global_exe(eap->arg);
12121 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012122#ifdef FEAT_CLIPBOARD
12123 end_global_changes();
12124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012125}
12126#endif