blob: 7dfd990a11fadd139e018ec6218471310d340ca9 [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
132#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000133#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
134# define ex_cexpr ex_ni
135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100137static int check_more(int, int);
138static linenr_T get_address(exarg_T *, char_u **, int addr_type, int skip, int to_other_file);
139static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200140#if !defined(FEAT_PERL) \
141 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
142 || !defined(FEAT_TCL) \
143 || !defined(FEAT_RUBY) \
144 || !defined(FEAT_LUA) \
145 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000146# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100147static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100149static char_u *invalid_range(exarg_T *eap);
150static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000151#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100152static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000153#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100154static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
155static void ex_highlight(exarg_T *eap);
156static void ex_colorscheme(exarg_T *eap);
157static void ex_quit(exarg_T *eap);
158static void ex_cquit(exarg_T *eap);
159static void ex_quit_all(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100161static void ex_close(exarg_T *eap);
162static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
163static void ex_only(exarg_T *eap);
164static void ex_resize(exarg_T *eap);
165static void ex_stag(exarg_T *eap);
166static void ex_tabclose(exarg_T *eap);
167static void ex_tabonly(exarg_T *eap);
168static void ex_tabnext(exarg_T *eap);
169static void ex_tabmove(exarg_T *eap);
170static void ex_tabs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171#else
172# define ex_close ex_ni
173# define ex_only ex_ni
174# define ex_all ex_ni
175# define ex_resize ex_ni
176# define ex_splitview ex_ni
177# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000178# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000179# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000180# define ex_tabs ex_ni
181# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000182# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183#endif
184#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100185static void ex_pclose(exarg_T *eap);
186static void ex_ptag(exarg_T *eap);
187static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188#else
189# define ex_pclose ex_ni
190# define ex_ptag ex_ni
191# define ex_pedit ex_ni
192#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100193static void ex_hide(exarg_T *eap);
194static void ex_stop(exarg_T *eap);
195static void ex_exit(exarg_T *eap);
196static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100198static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199#else
200# define ex_goto ex_ni
201#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100202static void ex_shell(exarg_T *eap);
203static void ex_preserve(exarg_T *eap);
204static void ex_recover(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205#ifndef FEAT_LISTCMDS
206# define ex_argedit ex_ni
207# define ex_argadd ex_ni
208# define ex_argdelete ex_ni
209# define ex_listdo ex_ni
210#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100211static void ex_mode(exarg_T *eap);
212static void ex_wrongmodifier(exarg_T *eap);
213static void ex_find(exarg_T *eap);
214static void ex_open(exarg_T *eap);
215static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
217# define ex_drop ex_ni
218#endif
219#ifndef FEAT_GUI
220# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100221static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222#endif
223#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100224static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225#else
226# define ex_tearoff ex_ni
227#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000228#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100229static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230#else
231# define ex_popup ex_ni
232#endif
233#ifndef FEAT_GUI_MSWIN
234# define ex_simalt ex_ni
235#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000236#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237# define gui_mch_find_dialog ex_ni
238# define gui_mch_replace_dialog ex_ni
239#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000240#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241# define ex_helpfind ex_ni
242#endif
243#ifndef FEAT_CSCOPE
244# define do_cscope ex_ni
245# define do_scscope ex_ni
246# define do_cstag ex_ni
247#endif
248#ifndef FEAT_SYN_HL
249# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200250# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000251#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200252#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200253# define ex_syntime ex_ni
254#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000255#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000256# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000257# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000258# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000259# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000260# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000261#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200262#ifndef FEAT_PERSISTENT_UNDO
263# define ex_rundo ex_ni
264# define ex_wundo ex_ni
265#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200266#ifndef FEAT_LUA
267# define ex_lua ex_script_ni
268# define ex_luado ex_ni
269# define ex_luafile ex_ni
270#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000271#ifndef FEAT_MZSCHEME
272# define ex_mzscheme ex_script_ni
273# define ex_mzfile ex_ni
274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275#ifndef FEAT_PERL
276# define ex_perl ex_script_ni
277# define ex_perldo ex_ni
278#endif
279#ifndef FEAT_PYTHON
280# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200281# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282# define ex_pyfile ex_ni
283#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200284#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200285# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200286# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200287# define ex_py3file ex_ni
288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289#ifndef FEAT_TCL
290# define ex_tcl ex_script_ni
291# define ex_tcldo ex_ni
292# define ex_tclfile ex_ni
293#endif
294#ifndef FEAT_RUBY
295# define ex_ruby ex_script_ni
296# define ex_rubydo ex_ni
297# define ex_rubyfile ex_ni
298#endif
299#ifndef FEAT_SNIFF
300# define ex_sniff ex_ni
301#endif
302#ifndef FEAT_KEYMAP
303# define ex_loadkeymap ex_ni
304#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100305static void ex_swapname(exarg_T *eap);
306static void ex_syncbind(exarg_T *eap);
307static void ex_read(exarg_T *eap);
308static void ex_pwd(exarg_T *eap);
309static void ex_equal(exarg_T *eap);
310static void ex_sleep(exarg_T *eap);
311static void do_exmap(exarg_T *eap, int isabbrev);
312static void ex_winsize(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100314static void ex_wincmd(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315#else
316# define ex_wincmd ex_ni
317#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000318#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100319static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320#else
321# define ex_winpos ex_ni
322#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100323static void ex_operators(exarg_T *eap);
324static void ex_put(exarg_T *eap);
325static void ex_copymove(exarg_T *eap);
326static void ex_submagic(exarg_T *eap);
327static void ex_join(exarg_T *eap);
328static void ex_at(exarg_T *eap);
329static void ex_bang(exarg_T *eap);
330static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200331#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100332static void ex_wundo(exarg_T *eap);
333static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200334#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100335static void ex_redo(exarg_T *eap);
336static void ex_later(exarg_T *eap);
337static void ex_redir(exarg_T *eap);
338static void ex_redraw(exarg_T *eap);
339static void ex_redrawstatus(exarg_T *eap);
340static void close_redir(void);
341static void ex_mkrc(exarg_T *eap);
342static void ex_mark(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343#ifdef FEAT_USR_CMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100344static char_u *uc_fun_cmd(void);
345static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346#endif
347#ifdef FEAT_EX_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100348static void ex_normal(exarg_T *eap);
349static void ex_startinsert(exarg_T *eap);
350static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351#else
352# define ex_normal ex_ni
353# define ex_align ex_ni
354# define ex_retab ex_ni
355# define ex_startinsert ex_ni
356# define ex_stopinsert ex_ni
357# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000358# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359#endif
360#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100361static void ex_checkpath(exarg_T *eap);
362static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363#else
364# define ex_findpat ex_ni
365# define ex_checkpath ex_ni
366#endif
367#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100368static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369#else
370# define ex_psearch ex_ni
371#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100372static void ex_tag(exarg_T *eap);
373static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374#ifndef FEAT_EVAL
375# define ex_scriptnames ex_ni
376# define ex_finish ex_ni
377# define ex_echo ex_ni
378# define ex_echohl ex_ni
379# define ex_execute ex_ni
380# define ex_call ex_ni
381# define ex_if ex_ni
382# define ex_endif ex_ni
383# define ex_else ex_ni
384# define ex_while ex_ni
385# define ex_continue ex_ni
386# define ex_break ex_ni
387# define ex_endwhile ex_ni
388# define ex_throw ex_ni
389# define ex_try ex_ni
390# define ex_catch ex_ni
391# define ex_finally ex_ni
392# define ex_endtry ex_ni
393# define ex_endfunction ex_ni
394# define ex_let ex_ni
395# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000396# define ex_lockvar ex_ni
397# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398# define ex_function ex_ni
399# define ex_delfunction ex_ni
400# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000401# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100403static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100405static int makeopens(FILE *fd, char_u *dirnow);
406static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
407static void ex_loadview(exarg_T *eap);
408static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000409static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410#else
411# define ex_loadview ex_ni
412#endif
413#ifndef FEAT_EVAL
414# define ex_compiler ex_ni
415#endif
416#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100417static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418#else
419# define ex_viminfo ex_ni
420#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100421static void ex_behave(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100423static void ex_filetype(exarg_T *eap);
424static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425#else
426# define ex_filetype ex_ni
427# define ex_setfiletype ex_ni
428#endif
429#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000430# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431# define ex_diffpatch ex_ni
432# define ex_diffgetput ex_ni
433# define ex_diffsplit ex_ni
434# define ex_diffthis ex_ni
435# define ex_diffupdate ex_ni
436#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100437static void ex_digraphs(exarg_T *eap);
438static void ex_set(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
440# define ex_options ex_ni
441#endif
442#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100443static void ex_nohlsearch(exarg_T *eap);
444static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445#else
446# define ex_nohlsearch ex_ni
447# define ex_match ex_ni
448#endif
449#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100450static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451#else
452# define ex_X ex_ni
453#endif
454#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100455static void ex_fold(exarg_T *eap);
456static void ex_foldopen(exarg_T *eap);
457static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458#else
459# define ex_fold ex_ni
460# define ex_foldopen ex_ni
461# define ex_folddo ex_ni
462#endif
463#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
464 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
465# define ex_language ex_ni
466#endif
467#ifndef FEAT_SIGNS
468# define ex_sign ex_ni
469#endif
470#ifndef FEAT_SUN_WORKSHOP
471# define ex_wsverb ex_ni
472#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000473#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200474# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000475# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200476# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478
479#ifndef FEAT_EVAL
480# define ex_debug ex_ni
481# define ex_breakadd ex_ni
482# define ex_debuggreedy ex_ni
483# define ex_breakdel ex_ni
484# define ex_breaklist ex_ni
485#endif
486
487#ifndef FEAT_CMDHIST
488# define ex_history ex_ni
489#endif
490#ifndef FEAT_JUMPLIST
491# define ex_jumps ex_ni
492# define ex_changes ex_ni
493#endif
494
Bram Moolenaar05159a02005-02-26 23:04:13 +0000495#ifndef FEAT_PROFILE
496# define ex_profile ex_ni
497#endif
498
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499/*
500 * Declare cmdnames[].
501 */
502#define DO_DECLARE_EXCMD
503#include "ex_cmds.h"
504
505/*
506 * Table used to quickly search for a command, based on its first character.
507 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000508static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509{
510 CMD_append,
511 CMD_buffer,
512 CMD_change,
513 CMD_delete,
514 CMD_edit,
515 CMD_file,
516 CMD_global,
517 CMD_help,
518 CMD_insert,
519 CMD_join,
520 CMD_k,
521 CMD_list,
522 CMD_move,
523 CMD_next,
524 CMD_open,
525 CMD_print,
526 CMD_quit,
527 CMD_read,
528 CMD_substitute,
529 CMD_t,
530 CMD_undo,
531 CMD_vglobal,
532 CMD_write,
533 CMD_xit,
534 CMD_yank,
535 CMD_z,
536 CMD_bang
537};
538
539static char_u dollar_command[2] = {'$', 0};
540
541
542#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000543/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544typedef struct
545{
546 char_u *line; /* command line */
547 linenr_T lnum; /* sourcing_lnum of the line */
548} wcmd_T;
549
550/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000551 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000553 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000555struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556{
557 garray_T *lines_gap; /* growarray with line info */
558 int current_line; /* last read line from growarray */
559 int repeating; /* TRUE when looping a second time */
560 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100561 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 void *cookie;
563};
564
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100565static char_u *get_loop_line(int c, void *cookie, int indent);
566static int store_loop_line(garray_T *gap, char_u *line);
567static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000568
569/* Struct to save a few things while debugging. Used in do_cmdline() only. */
570struct dbg_stuff
571{
572 int trylevel;
573 int force_abort;
574 except_T *caught_stack;
575 char_u *vv_exception;
576 char_u *vv_throwpoint;
577 int did_emsg;
578 int got_int;
579 int did_throw;
580 int need_rethrow;
581 int check_cstack;
582 except_T *current_exception;
583};
584
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100585static void save_dbg_stuff(struct dbg_stuff *dsp);
586static void restore_dbg_stuff(struct dbg_stuff *dsp);
Bram Moolenaared203462004-06-16 11:19:22 +0000587
588 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100589save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000590{
591 dsp->trylevel = trylevel; trylevel = 0;
592 dsp->force_abort = force_abort; force_abort = FALSE;
593 dsp->caught_stack = caught_stack; caught_stack = NULL;
594 dsp->vv_exception = v_exception(NULL);
595 dsp->vv_throwpoint = v_throwpoint(NULL);
596
597 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
598 dsp->did_emsg = did_emsg; did_emsg = FALSE;
599 dsp->got_int = got_int; got_int = FALSE;
600 dsp->did_throw = did_throw; did_throw = FALSE;
601 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
602 dsp->check_cstack = check_cstack; check_cstack = FALSE;
603 dsp->current_exception = current_exception; current_exception = NULL;
604}
605
606 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100607restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000608{
609 suppress_errthrow = FALSE;
610 trylevel = dsp->trylevel;
611 force_abort = dsp->force_abort;
612 caught_stack = dsp->caught_stack;
613 (void)v_exception(dsp->vv_exception);
614 (void)v_throwpoint(dsp->vv_throwpoint);
615 did_emsg = dsp->did_emsg;
616 got_int = dsp->got_int;
617 did_throw = dsp->did_throw;
618 need_rethrow = dsp->need_rethrow;
619 check_cstack = dsp->check_cstack;
620 current_exception = dsp->current_exception;
621}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622#endif
623
624
625/*
626 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
627 * command is given.
628 */
629 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100630do_exmode(
631 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632{
633 int save_msg_scroll;
634 int prev_msg_row;
635 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000636 int changedtick;
637
638 if (improved)
639 exmode_active = EXMODE_VIM;
640 else
641 exmode_active = EXMODE_NORMAL;
642 State = NORMAL;
643
644 /* When using ":global /pat/ visual" and then "Q" we return to continue
645 * the :global command. */
646 if (global_busy)
647 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648
649 save_msg_scroll = msg_scroll;
650 ++RedrawingDisabled; /* don't redisplay the window */
651 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652#ifdef FEAT_GUI
653 /* Ignore scrollbar and mouse events in Ex mode */
654 ++hold_gui_events;
655#endif
656#ifdef FEAT_SNIFF
657 want_sniff_request = 0; /* No K_SNIFF wanted */
658#endif
659
660 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
661 while (exmode_active)
662 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000663#ifdef FEAT_EX_EXTRA
664 /* Check for a ":normal" command and no more characters left. */
665 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
666 {
667 exmode_active = FALSE;
668 break;
669 }
670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 msg_scroll = TRUE;
672 need_wait_return = FALSE;
673 ex_pressedreturn = FALSE;
674 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000675 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 prev_msg_row = msg_row;
677 prev_line = curwin->w_cursor.lnum;
678#ifdef FEAT_SNIFF
679 ProcessSniffRequests();
680#endif
681 if (improved)
682 {
683 cmdline_row = msg_row;
684 do_cmdline(NULL, getexline, NULL, 0);
685 }
686 else
687 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
688 lines_left = Rows - 1;
689
Bram Moolenaardf177f62005-02-22 08:39:57 +0000690 if ((prev_line != curwin->w_cursor.lnum
691 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000693 if (curbuf->b_ml.ml_flags & ML_EMPTY)
694 EMSG(_(e_emptybuf));
695 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000697 if (ex_pressedreturn)
698 {
699 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000700 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000701 msg_row = prev_msg_row;
702 if (prev_msg_row == Rows - 1)
703 msg_row--;
704 }
705 msg_col = 0;
706 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
707 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000710 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
711 {
712 if (curbuf->b_ml.ml_flags & ML_EMPTY)
713 EMSG(_(e_emptybuf));
714 else
715 EMSG(_("E501: At end-of-file"));
716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 }
718
719#ifdef FEAT_GUI
720 --hold_gui_events;
721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 --RedrawingDisabled;
723 --no_wait_return;
724 update_screen(CLEAR);
725 need_wait_return = FALSE;
726 msg_scroll = save_msg_scroll;
727}
728
729/*
730 * Execute a simple command line. Used for translated commands like "*".
731 */
732 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100733do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734{
735 return do_cmdline(cmd, NULL, NULL,
736 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
737}
738
739/*
740 * do_cmdline(): execute one Ex command line
741 *
742 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100743 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 * 2. Split up in parts separated with '|'.
745 *
746 * This function can be called recursively!
747 *
748 * flags:
749 * DOCMD_VERBOSE - The command will be included in the error message.
750 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100751 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 * DOCMD_KEYTYPED - Don't reset KeyTyped.
753 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
754 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
755 *
756 * return FAIL if cmdline could not be executed, OK otherwise
757 */
758 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100759do_cmdline(
760 char_u *cmdline,
761 char_u *(*fgetline)(int, void *, int),
762 void *cookie, /* argument for fgetline() */
763 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764{
765 char_u *next_cmdline; /* next cmd to execute */
766 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100767 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 static int recursive = 0; /* recursive depth */
769 int msg_didout_before_start = 0;
770 int count = 0; /* line number count */
771 int did_inc = FALSE; /* incremented RedrawingDisabled */
772 int retval = OK;
773#ifdef FEAT_EVAL
774 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000775 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 int current_line = 0; /* active line in lines_ga */
777 char_u *fname = NULL; /* function or script name */
778 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
779 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000780 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 int initial_trylevel;
782 struct msglist **saved_msg_list = NULL;
783 struct msglist *private_msg_list;
784
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100785 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100786 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000788 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000790 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100792# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793# define cmd_cookie cookie
794#endif
795 static int call_depth = 0; /* recursiveness */
796
797#ifdef FEAT_EVAL
798 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
799 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000800 * This ensures that the do_errthrow() call in do_one_cmd() does not
801 * combine the messages stored by an earlier invocation of do_one_cmd()
802 * with the command name of the later one. This would happen when
803 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 saved_msg_list = msg_list;
805 msg_list = &private_msg_list;
806 private_msg_list = NULL;
807#endif
808
809 /* It's possible to create an endless loop with ":execute", catch that
810 * here. The value of 200 allows nested function calls, ":source", etc. */
811 if (call_depth == 200)
812 {
813 EMSG(_("E169: Command too recursive"));
814#ifdef FEAT_EVAL
815 /* When converting to an exception, we do not include the command name
816 * since this is not an error of the specific command. */
817 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
818 msg_list = saved_msg_list;
819#endif
820 return FAIL;
821 }
822 ++call_depth;
823
824#ifdef FEAT_EVAL
825 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000826 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 cstack.cs_trylevel = 0;
828 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000829 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
831
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100832 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833
834 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100835 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000836 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 ++ex_nesting_level;
838
839 /* Get the function or script name and the address where the next breakpoint
840 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000841 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 {
843 fname = func_name(real_cookie);
844 breakpoint = func_breakpoint(real_cookie);
845 dbg_tick = func_dbg_tick(real_cookie);
846 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100847 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 {
849 fname = sourcing_name;
850 breakpoint = source_breakpoint(real_cookie);
851 dbg_tick = source_dbg_tick(real_cookie);
852 }
853
854 /*
855 * Initialize "force_abort" and "suppress_errthrow" at the top level.
856 */
857 if (!recursive)
858 {
859 force_abort = FALSE;
860 suppress_errthrow = FALSE;
861 }
862
863 /*
864 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000865 * exception handling (used when debugging). Otherwise clear it to avoid
866 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000868 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000869 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000870 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200871 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872
873 initial_trylevel = trylevel;
874
875 /*
876 * "did_throw" will be set to TRUE when an exception is being thrown.
877 */
878 did_throw = FALSE;
879#endif
880 /*
881 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000882 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 * If force_abort is set, we cancel everything.
884 */
885 did_emsg = FALSE;
886
887 /*
888 * KeyTyped is only set when calling vgetc(). Reset it here when not
889 * calling vgetc() (sourced command lines).
890 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100891 if (!(flags & DOCMD_KEYTYPED)
892 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 KeyTyped = FALSE;
894
895 /*
896 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000897 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 * - for multiple commands on one line, separated with '|'
899 * - when repeating until there are no more lines (for ":source")
900 */
901 next_cmdline = cmdline;
902 do
903 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000904#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100905 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000906#endif
907
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000908 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 if (next_cmdline == NULL
910#ifdef FEAT_EVAL
911 && !force_abort
912 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000913 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914#endif
915 )
916 did_emsg = FALSE;
917
918 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000919 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100920 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 * 3. If a line is given: Make a copy, so we can mess with it.
922 */
923
924#ifdef FEAT_EVAL
925 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000926 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 {
928 /* Each '|' separated command is stored separately in lines_ga, to
929 * be able to jump to it. Don't use next_cmdline now. */
930 vim_free(cmdline_copy);
931 cmdline_copy = NULL;
932
933 /* Check if a function has returned or, unless it has an unclosed
934 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000935 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000937# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000938 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000939 func_line_end(real_cookie);
940# endif
941 if (func_has_ended(real_cookie))
942 {
943 retval = FAIL;
944 break;
945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000947#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000948 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100949 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000950 script_line_end();
951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952
953 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100954 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 {
956 retval = FAIL;
957 break;
958 }
959
960 /* If breakpoints have been added/deleted need to check for it. */
961 if (breakpoint != NULL && dbg_tick != NULL
962 && *dbg_tick != debug_tick)
963 {
964 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100965 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 fname, sourcing_lnum);
967 *dbg_tick = debug_tick;
968 }
969
970 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
971 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
972
973 /* Did we encounter a breakpoint? */
974 if (breakpoint != NULL && *breakpoint != 0
975 && *breakpoint <= sourcing_lnum)
976 {
977 dbg_breakpoint(fname, sourcing_lnum);
978 /* Find next breakpoint. */
979 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100980 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 fname, sourcing_lnum);
982 *dbg_tick = debug_tick;
983 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000984# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000985 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000986 {
987 if (getline_is_func)
988 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100989 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000990 script_line_start();
991 }
992# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 }
994
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000995 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000997 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100998 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 * below, so that it stores lines in or reads them from
1000 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001001 * while/for loop. */
1002 cmd_getline = get_loop_line;
1003 cmd_cookie = (void *)&cmd_loop_cookie;
1004 cmd_loop_cookie.lines_gap = &lines_ga;
1005 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001006 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001007 cmd_loop_cookie.cookie = cookie;
1008 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 }
1010 else
1011 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001012 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 cmd_cookie = cookie;
1014 }
1015#endif
1016
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001017 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 if (next_cmdline == NULL)
1019 {
1020 /*
1021 * Need to set msg_didout for the first line after an ":if",
1022 * otherwise the ":if" will be overwritten.
1023 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001024 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001026 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027#ifdef FEAT_EVAL
1028 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1029#else
1030 0
1031#endif
1032 )) == NULL)
1033 {
1034 /* Don't call wait_return for aborted command line. The NULL
1035 * returned for the end of a sourced file or executed function
1036 * doesn't do this. */
1037 if (KeyTyped && !(flags & DOCMD_REPEAT))
1038 need_wait_return = FALSE;
1039 retval = FAIL;
1040 break;
1041 }
1042 used_getline = TRUE;
1043
1044 /*
1045 * Keep the first typed line. Clear it when more lines are typed.
1046 */
1047 if (flags & DOCMD_KEEPLINE)
1048 {
1049 vim_free(repeat_cmdline);
1050 if (count == 0)
1051 repeat_cmdline = vim_strsave(next_cmdline);
1052 else
1053 repeat_cmdline = NULL;
1054 }
1055 }
1056
1057 /* 3. Make a copy of the command so we can mess with it. */
1058 else if (cmdline_copy == NULL)
1059 {
1060 next_cmdline = vim_strsave(next_cmdline);
1061 if (next_cmdline == NULL)
1062 {
1063 EMSG(_(e_outofmem));
1064 retval = FAIL;
1065 break;
1066 }
1067 }
1068 cmdline_copy = next_cmdline;
1069
1070#ifdef FEAT_EVAL
1071 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001072 * Save the current line when inside a ":while" or ":for", and when
1073 * the command looks like a ":while" or ":for", because we may need it
1074 * later. When there is a '|' and another command, it is stored
1075 * separately, because we need to be able to jump back to it from an
1076 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001078 if (current_line == lines_ga.ga_len
1079 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001081 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 {
1083 retval = FAIL;
1084 break;
1085 }
1086 }
1087 did_endif = FALSE;
1088#endif
1089
1090 if (count++ == 0)
1091 {
1092 /*
1093 * All output from the commands is put below each other, without
1094 * waiting for a return. Don't do this when executing commands
1095 * from a script or when being called recursive (e.g. for ":e
1096 * +command file").
1097 */
1098 if (!(flags & DOCMD_NOWAIT) && !recursive)
1099 {
1100 msg_didout_before_start = msg_didout;
1101 msg_didany = FALSE; /* no output yet */
1102 msg_start();
1103 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001104 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 ++RedrawingDisabled;
1106 did_inc = TRUE;
1107 }
1108 }
1109
1110 if (p_verbose >= 15 && sourcing_name != NULL)
1111 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001113 verbose_enter_scroll();
1114
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 smsg((char_u *)_("line %ld: %s"),
1116 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001117 if (msg_silent == 0)
1118 msg_puts((char_u *)"\n"); /* don't overwrite this */
1119
1120 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 --no_wait_return;
1122 }
1123
1124 /*
1125 * 2. Execute one '|' separated command.
1126 * do_one_cmd() will return NULL if there is no trailing '|'.
1127 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1128 */
1129 ++recursive;
1130 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1131#ifdef FEAT_EVAL
1132 &cstack,
1133#endif
1134 cmd_getline, cmd_cookie);
1135 --recursive;
1136
1137#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001138 if (cmd_cookie == (void *)&cmd_loop_cookie)
1139 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001141 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142#endif
1143
1144 if (next_cmdline == NULL)
1145 {
1146 vim_free(cmdline_copy);
1147 cmdline_copy = NULL;
1148#ifdef FEAT_CMDHIST
1149 /*
1150 * If the command was typed, remember it for the ':' register.
1151 * Do this AFTER executing the command to make :@: work.
1152 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001153 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 && new_last_cmdline != NULL)
1155 {
1156 vim_free(last_cmdline);
1157 last_cmdline = new_last_cmdline;
1158 new_last_cmdline = NULL;
1159 }
1160#endif
1161 }
1162 else
1163 {
1164 /* need to copy the command after the '|' to cmdline_copy, for the
1165 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001166 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 next_cmdline = cmdline_copy;
1168 }
1169
1170
1171#ifdef FEAT_EVAL
1172 /* reset did_emsg for a function that is not aborted by an error */
1173 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001174 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 && !func_has_abort(real_cookie))
1176 did_emsg = FALSE;
1177
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001178 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 {
1180 ++current_line;
1181
1182 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001183 * An ":endwhile", ":endfor" and ":continue" is handled here.
1184 * If we were executing commands, jump back to the ":while" or
1185 * ":for".
1186 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001188 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001190 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001192 /* Jump back to the matching ":while" or ":for". Be careful
1193 * not to use a cs_line[] from an entry that isn't a ":while"
1194 * or ":for": It would make "current_line" invalid and can
1195 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 if (!did_emsg && !got_int && !did_throw
1197 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001198 && (cstack.cs_flags[cstack.cs_idx]
1199 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 && cstack.cs_line[cstack.cs_idx] >= 0
1201 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1202 {
1203 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001204 /* remember we jumped there */
1205 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 line_breakcheck(); /* check if CTRL-C typed */
1207
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001208 /* Check for the next breakpoint at or after the ":while"
1209 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 if (breakpoint != NULL)
1211 {
1212 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001213 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 fname,
1215 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1216 *dbg_tick = debug_tick;
1217 }
1218 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001219 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001221 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001223 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1224 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 }
1226 }
1227
1228 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001229 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001231 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001233 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1235 }
1236 }
1237
1238 /*
1239 * When not inside any ":while" loop, clear remembered lines.
1240 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001241 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 {
1243 if (lines_ga.ga_len > 0)
1244 {
1245 sourcing_lnum =
1246 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1247 free_cmdlines(&lines_ga);
1248 }
1249 current_line = 0;
1250 }
1251
1252 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001253 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1254 * being restored at the ":endtry". Reset them here and set the
1255 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1256 * This includes the case where a missing ":endif", ":endwhile" or
1257 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001259 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001261 cstack.cs_lflags &= ~CSL_HAD_FINA;
1262 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1263 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 did_throw ? (void *)current_exception : NULL);
1265 did_emsg = got_int = did_throw = FALSE;
1266 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1267 }
1268
1269 /* Update global "trylevel" for recursive calls to do_cmdline() from
1270 * within this loop. */
1271 trylevel = initial_trylevel + cstack.cs_trylevel;
1272
1273 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001274 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 * files) is aborted because of an error, an interrupt, or an uncaught
1276 * exception, cancel everything. If it is left normally, reset
1277 * force_abort to get the non-EH compatible abortion behavior for
1278 * the rest of the script.
1279 */
1280 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1281 force_abort = FALSE;
1282
1283 /* Convert an interrupt to an exception if appropriate. */
1284 (void)do_intthrow(&cstack);
1285#endif /* FEAT_EVAL */
1286
1287 }
1288 /*
1289 * Continue executing command lines when:
1290 * - no CTRL-C typed, no aborting error, no exception thrown or try
1291 * conditionals need to be checked for executing finally clauses or
1292 * catching an interrupt exception
1293 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001294 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 * looping for ":source" command or function call.
1296 */
1297 while (!((got_int
1298#ifdef FEAT_EVAL
1299 || (did_emsg && force_abort) || did_throw
1300#endif
1301 )
1302#ifdef FEAT_EVAL
1303 && cstack.cs_trylevel == 0
1304#endif
1305 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001306 && !(did_emsg
1307#ifdef FEAT_EVAL
1308 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001309 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001310 * the :endtry to be missed. */
1311 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1312#endif
1313 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001314 && (getline_equal(fgetline, cookie, getexmodeline)
1315 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 && (next_cmdline != NULL
1317#ifdef FEAT_EVAL
1318 || cstack.cs_idx >= 0
1319#endif
1320 || (flags & DOCMD_REPEAT)));
1321
1322 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001323 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324#ifdef FEAT_EVAL
1325 free_cmdlines(&lines_ga);
1326 ga_clear(&lines_ga);
1327
1328 if (cstack.cs_idx >= 0)
1329 {
1330 /*
1331 * If a sourced file or executed function ran to its end, report the
1332 * unclosed conditional.
1333 */
1334 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001335 && ((getline_equal(fgetline, cookie, getsourceline)
1336 && !source_finished(fgetline, cookie))
1337 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 && !func_has_ended(real_cookie))))
1339 {
1340 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1341 EMSG(_(e_endtry));
1342 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1343 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001344 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1345 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 else
1347 EMSG(_(e_endif));
1348 }
1349
1350 /*
1351 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1352 * ":endtry" in a sourced file or executed function. If the try
1353 * conditional is in its finally clause, ignore anything pending.
1354 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001355 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 */
1357 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001358 {
1359 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1360
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001361 if (idx >= 0)
1362 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001363 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1364 &cstack.cs_looplevel);
1365 }
1366 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 trylevel = initial_trylevel;
1368 }
1369
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001370 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1371 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001373 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 ? (char_u *)"endfunction" : (char_u *)NULL);
1375
1376 if (trylevel == 0)
1377 {
1378 /*
1379 * When an exception is being thrown out of the outermost try
1380 * conditional, discard the uncaught exception, disable the conversion
1381 * of interrupts or errors to exceptions, and ensure that no more
1382 * commands are executed.
1383 */
1384 if (did_throw)
1385 {
1386 void *p = NULL;
1387 char_u *saved_sourcing_name;
1388 int saved_sourcing_lnum;
1389 struct msglist *messages = NULL, *next;
1390
1391 /*
1392 * If the uncaught exception is a user exception, report it as an
1393 * error. If it is an error exception, display the saved error
1394 * message now. For an interrupt exception, do nothing; the
1395 * interrupt message is given elsewhere.
1396 */
1397 switch (current_exception->type)
1398 {
1399 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001400 vim_snprintf((char *)IObuff, IOSIZE,
1401 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 current_exception->value);
1403 p = vim_strsave(IObuff);
1404 break;
1405 case ET_ERROR:
1406 messages = current_exception->messages;
1407 current_exception->messages = NULL;
1408 break;
1409 case ET_INTERRUPT:
1410 break;
1411 default:
1412 p = vim_strsave((char_u *)_(e_internal));
1413 }
1414
1415 saved_sourcing_name = sourcing_name;
1416 saved_sourcing_lnum = sourcing_lnum;
1417 sourcing_name = current_exception->throw_name;
1418 sourcing_lnum = current_exception->throw_lnum;
1419 current_exception->throw_name = NULL;
1420
1421 discard_current_exception(); /* uses IObuff if 'verbose' */
1422 suppress_errthrow = TRUE;
1423 force_abort = TRUE;
1424
1425 if (messages != NULL)
1426 {
1427 do
1428 {
1429 next = messages->next;
1430 emsg(messages->msg);
1431 vim_free(messages->msg);
1432 vim_free(messages);
1433 messages = next;
1434 }
1435 while (messages != NULL);
1436 }
1437 else if (p != NULL)
1438 {
1439 emsg(p);
1440 vim_free(p);
1441 }
1442 vim_free(sourcing_name);
1443 sourcing_name = saved_sourcing_name;
1444 sourcing_lnum = saved_sourcing_lnum;
1445 }
1446
1447 /*
1448 * On an interrupt or an aborting error not converted to an exception,
1449 * disable the conversion of errors to exceptions. (Interrupts are not
1450 * converted any more, here.) This enables also the interrupt message
1451 * when force_abort is set and did_emsg unset in case of an interrupt
1452 * from a finally clause after an error.
1453 */
1454 else if (got_int || (did_emsg && force_abort))
1455 suppress_errthrow = TRUE;
1456 }
1457
1458 /*
1459 * The current cstack will be freed when do_cmdline() returns. An uncaught
1460 * exception will have to be rethrown in the previous cstack. If a function
1461 * has just returned or a script file was just finished and the previous
1462 * cstack belongs to the same function or, respectively, script file, it
1463 * will have to be checked for finally clauses to be executed due to the
1464 * ":return" or ":finish". This is done in do_one_cmd().
1465 */
1466 if (did_throw)
1467 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001468 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001470 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 && ex_nesting_level > func_level(real_cookie) + 1))
1472 {
1473 if (!did_throw)
1474 check_cstack = TRUE;
1475 }
1476 else
1477 {
1478 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001479 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 --ex_nesting_level;
1481 /*
1482 * Go to debug mode when returning from a function in which we are
1483 * single-stepping.
1484 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001485 if ((getline_equal(fgetline, cookie, getsourceline)
1486 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001488 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 ? (char_u *)_("End of sourced file")
1490 : (char_u *)_("End of function"));
1491 }
1492
1493 /*
1494 * Restore the exception environment (done after returning from the
1495 * debugger).
1496 */
1497 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001498 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499
1500 msg_list = saved_msg_list;
1501#endif /* FEAT_EVAL */
1502
1503 /*
1504 * If there was too much output to fit on the command line, ask the user to
1505 * hit return before redrawing the screen. With the ":global" command we do
1506 * this only once after the command is finished.
1507 */
1508 if (did_inc)
1509 {
1510 --RedrawingDisabled;
1511 --no_wait_return;
1512 msg_scroll = FALSE;
1513
1514 /*
1515 * When just finished an ":if"-":else" which was typed, no need to
1516 * wait for hit-return. Also for an error situation.
1517 */
1518 if (retval == FAIL
1519#ifdef FEAT_EVAL
1520 || (did_endif && KeyTyped && !did_emsg)
1521#endif
1522 )
1523 {
1524 need_wait_return = FALSE;
1525 msg_didany = FALSE; /* don't wait when restarting edit */
1526 }
1527 else if (need_wait_return)
1528 {
1529 /*
1530 * The msg_start() above clears msg_didout. The wait_return we do
1531 * here should not overwrite the command that may be shown before
1532 * doing that.
1533 */
1534 msg_didout |= msg_didout_before_start;
1535 wait_return(FALSE);
1536 }
1537 }
1538
Bram Moolenaar2a942252012-11-28 23:03:07 +01001539#ifdef FEAT_EVAL
1540 did_endif = FALSE; /* in case do_cmdline used recursively */
1541#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 /*
1543 * Reset if_level, in case a sourced script file contains more ":if" than
1544 * ":endif" (could be ":if x | foo | endif").
1545 */
1546 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001547#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001548
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 --call_depth;
1550 return retval;
1551}
1552
1553#ifdef FEAT_EVAL
1554/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001555 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 */
1557 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001558get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001560 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 wcmd_T *wp;
1562 char_u *line;
1563
1564 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1565 {
1566 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001567 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001569 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 if (cp->getline == NULL)
1571 line = getcmdline(c, 0L, indent);
1572 else
1573 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001574 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 ++cp->current_line;
1576
1577 return line;
1578 }
1579
1580 KeyTyped = FALSE;
1581 ++cp->current_line;
1582 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1583 sourcing_lnum = wp->lnum;
1584 return vim_strsave(wp->line);
1585}
1586
1587/*
1588 * Store a line in "gap" so that a ":while" loop can execute it again.
1589 */
1590 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001591store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592{
1593 if (ga_grow(gap, 1) == FAIL)
1594 return FAIL;
1595 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1596 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1597 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 return OK;
1599}
1600
1601/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001602 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 */
1604 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001605free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606{
1607 while (gap->ga_len > 0)
1608 {
1609 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1610 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 }
1612}
1613#endif
1614
1615/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001616 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1617 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001620getline_equal(
1621 char_u *(*fgetline)(int, void *, int),
1622 void *cookie UNUSED, /* argument for fgetline() */
1623 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624{
1625#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001626 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001627 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628
Bram Moolenaar89d40322006-08-29 15:30:07 +00001629 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001630 * function that's originally used to obtain the lines. This may be
1631 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001632 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001633 cp = (struct loop_cookie *)cookie;
1634 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 {
1636 gp = cp->getline;
1637 cp = cp->cookie;
1638 }
1639 return gp == func;
1640#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001641 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642#endif
1643}
1644
1645#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1646/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001647 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 * getline function. Otherwise return "cookie".
1649 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001651getline_cookie(
1652 char_u *(*fgetline)(int, void *, int) UNUSED,
1653 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654{
1655# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001656 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001657 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658
Bram Moolenaar89d40322006-08-29 15:30:07 +00001659 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001660 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001662 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001663 cp = (struct loop_cookie *)cookie;
1664 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 {
1666 gp = cp->getline;
1667 cp = cp->cookie;
1668 }
1669 return cp;
1670# else
1671 return cookie;
1672# endif
1673}
1674#endif
1675
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001676
1677/*
1678 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1679 * ":bwipeout", etc.
1680 * Returns the buffer number.
1681 */
1682 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001683compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001684{
1685 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001686 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001687 int count = offset;
1688
1689 buf = firstbuf;
1690 while (buf->b_next != NULL && buf->b_fnum < lnum)
1691 buf = buf->b_next;
1692 while (count != 0)
1693 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001694 count += (offset < 0) ? 1 : -1;
1695 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1696 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001697 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001698 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001699 if (addr_type == ADDR_LOADED_BUFFERS)
1700 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001701 while (buf->b_ml.ml_mfp == NULL)
1702 {
1703 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1704 if (nextbuf == NULL)
1705 break;
1706 buf = nextbuf;
1707 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001708 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001709 /* we might have gone too far, last buffer is not loadedd */
1710 if (addr_type == ADDR_LOADED_BUFFERS)
1711 while (buf->b_ml.ml_mfp == NULL)
1712 {
1713 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1714 if (nextbuf == NULL)
1715 break;
1716 buf = nextbuf;
1717 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001718 return buf->b_fnum;
1719}
1720
Bram Moolenaarf240e182014-11-27 18:33:02 +01001721#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001722static int current_win_nr(win_T *win);
1723static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001724
1725 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001726current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001727{
1728 win_T *wp;
1729 int nr = 0;
1730
1731 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1732 {
1733 ++nr;
1734 if (wp == win)
1735 break;
1736 }
1737 return nr;
1738}
1739
1740 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001741current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001742{
1743 tabpage_T *tp;
1744 int nr = 0;
1745
1746 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
1747 {
1748 ++nr;
1749 if (tp == tab)
1750 break;
1751 }
1752 return nr;
1753}
1754
1755# define CURRENT_WIN_NR current_win_nr(curwin)
1756# define LAST_WIN_NR current_win_nr(NULL)
1757# define CURRENT_TAB_NR current_tab_nr(curtab)
1758# define LAST_TAB_NR current_tab_nr(NULL)
1759#else
1760# define CURRENT_WIN_NR 1
1761# define LAST_WIN_NR 1
1762# define CURRENT_TAB_NR 1
1763# define LAST_TAB_NR 1
1764#endif
1765
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001766
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767/*
1768 * Execute one Ex command.
1769 *
1770 * If 'sourcing' is TRUE, the command will be included in the error message.
1771 *
1772 * 1. skip comment lines and leading space
1773 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001774 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001775 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001776 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001777 * 6. parse arguments
1778 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001780 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 *
1782 * This function may be called recursively!
1783 */
1784#if (_MSC_VER == 1200)
1785/*
Bram Moolenaared203462004-06-16 11:19:22 +00001786 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001788 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789#endif
1790 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001791do_one_cmd(
1792 char_u **cmdlinep,
1793 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001795 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001797 char_u *(*fgetline)(int, void *, int),
1798 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799{
1800 char_u *p;
1801 linenr_T lnum;
1802 long n;
1803 char_u *errormsg = NULL; /* error message */
1804 exarg_T ea; /* Ex command arguments */
1805 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001806 int save_msg_scroll = msg_scroll;
1807 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001809#ifdef HAVE_SANDBOX
1810 int did_sandbox = FALSE;
1811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 cmdmod_T save_cmdmod;
1813 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001814 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815
1816 vim_memset(&ea, 0, sizeof(ea));
1817 ea.line1 = 1;
1818 ea.line2 = 1;
1819#ifdef FEAT_EVAL
1820 ++ex_nesting_level;
1821#endif
1822
Bram Moolenaar21691f82012-12-05 19:13:18 +01001823 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 if (quitmore
1825#ifdef FEAT_EVAL
1826 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001827 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001828#endif
1829#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001830 /* avoid that an autocommand, e.g. QuitPre, does this */
1831 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832#endif
1833 )
1834 --quitmore;
1835
1836 /*
1837 * Reset browse, confirm, etc.. They are restored when returning, for
1838 * recursive calls.
1839 */
1840 save_cmdmod = cmdmod;
1841 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1842
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001843 /* "#!anything" is handled like a comment. */
1844 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1845 goto doend;
1846
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 /*
1848 * Repeat until no more command modifiers are found.
1849 */
1850 ea.cmd = *cmdlinep;
1851 for (;;)
1852 {
1853/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001854 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 */
1856 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1857 ++ea.cmd;
1858
1859 /* in ex mode, an empty line works like :+ */
1860 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001861 && (getline_equal(fgetline, cookie, getexmodeline)
1862 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001863 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 {
1865 ea.cmd = (char_u *)"+";
1866 ex_pressedreturn = TRUE;
1867 }
1868
1869 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001870 if (*ea.cmd == '"')
1871 goto doend;
1872 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001873 {
1874 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877
1878/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001879 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 */
1881 p = ea.cmd;
1882 if (VIM_ISDIGIT(*ea.cmd))
1883 p = skipwhite(skipdigits(ea.cmd));
1884 switch (*p)
1885 {
1886 /* When adding an entry, also modify cmd_exists(). */
1887 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1888 break;
1889#ifdef FEAT_WINDOWS
1890 cmdmod.split |= WSP_ABOVE;
1891#endif
1892 continue;
1893
1894 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1895 {
1896#ifdef FEAT_WINDOWS
1897 cmdmod.split |= WSP_BELOW;
1898#endif
1899 continue;
1900 }
1901 if (checkforcmd(&ea.cmd, "browse", 3))
1902 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001903#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 cmdmod.browse = TRUE;
1905#endif
1906 continue;
1907 }
1908 if (!checkforcmd(&ea.cmd, "botright", 2))
1909 break;
1910#ifdef FEAT_WINDOWS
1911 cmdmod.split |= WSP_BOT;
1912#endif
1913 continue;
1914
1915 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1916 break;
1917#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1918 cmdmod.confirm = TRUE;
1919#endif
1920 continue;
1921
1922 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1923 {
1924 cmdmod.keepmarks = TRUE;
1925 continue;
1926 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001927 if (checkforcmd(&ea.cmd, "keepalt", 5))
1928 {
1929 cmdmod.keepalt = TRUE;
1930 continue;
1931 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001932 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1933 {
1934 cmdmod.keeppatterns = TRUE;
1935 continue;
1936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1938 break;
1939 cmdmod.keepjumps = TRUE;
1940 continue;
1941
1942 /* ":hide" and ":hide | cmd" are not modifiers */
1943 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1944 || *p == NUL || ends_excmd(*p))
1945 break;
1946 ea.cmd = p;
1947 cmdmod.hide = TRUE;
1948 continue;
1949
1950 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1951 {
1952 cmdmod.lockmarks = TRUE;
1953 continue;
1954 }
1955
1956 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1957 break;
1958#ifdef FEAT_WINDOWS
1959 cmdmod.split |= WSP_ABOVE;
1960#endif
1961 continue;
1962
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001963 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001964 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001965#ifdef FEAT_AUTOCMD
1966 if (cmdmod.save_ei == NULL)
1967 {
1968 /* Set 'eventignore' to "all". Restore the
1969 * existing option value later. */
1970 cmdmod.save_ei = vim_strsave(p_ei);
1971 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001972 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001973 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001974#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001975 continue;
1976 }
1977 if (!checkforcmd(&ea.cmd, "noswapfile", 6))
1978 break;
1979 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001980 continue;
1981
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1983 break;
1984#ifdef FEAT_WINDOWS
1985 cmdmod.split |= WSP_BELOW;
1986#endif
1987 continue;
1988
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001989 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1990 {
1991#ifdef HAVE_SANDBOX
1992 if (!did_sandbox)
1993 ++sandbox;
1994 did_sandbox = TRUE;
1995#endif
1996 continue;
1997 }
1998 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002000 if (save_msg_silent == -1)
2001 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
2004 {
2005 /* ":silent!", but not "silent !cmd" */
2006 ea.cmd = skipwhite(ea.cmd + 1);
2007 ++emsg_silent;
2008 ++did_esilent;
2009 }
2010 continue;
2011
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002012 case 't': if (checkforcmd(&p, "tab", 3))
2013 {
2014#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002015 if (vim_isdigit(*ea.cmd))
2016 cmdmod.tab = atoi((char *)ea.cmd) + 1;
2017 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002018 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002019 ea.cmd = p;
2020#endif
2021 continue;
2022 }
2023 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 break;
2025#ifdef FEAT_WINDOWS
2026 cmdmod.split |= WSP_TOP;
2027#endif
2028 continue;
2029
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002030 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
2031 break;
2032 if (save_msg_silent == -1)
2033 save_msg_silent = msg_silent;
2034 msg_silent = 0;
2035 continue;
2036
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
2038 {
2039#ifdef FEAT_VERTSPLIT
2040 cmdmod.split |= WSP_VERT;
2041#endif
2042 continue;
2043 }
2044 if (!checkforcmd(&p, "verbose", 4))
2045 break;
2046 if (verbose_save < 0)
2047 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00002048 if (vim_isdigit(*ea.cmd))
2049 p_verbose = atoi((char *)ea.cmd);
2050 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 p_verbose = 1;
2052 ea.cmd = p;
2053 continue;
2054 }
2055 break;
2056 }
2057
2058#ifdef FEAT_EVAL
2059 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2060 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2061#else
2062 ea.skip = (if_level > 0);
2063#endif
2064
2065#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002066# ifdef FEAT_PROFILE
2067 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002068 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002069 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002070 if (getline_equal(fgetline, cookie, get_func_line))
2071 func_line_exec(getline_cookie(fgetline, cookie));
2072 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002073 script_line_exec();
2074 }
2075#endif
2076
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 /* May go to debug mode. If this happens and the ">quit" debug command is
2078 * used, throw an interrupt exception and skip the next command. */
2079 dbg_check_breakpoint(&ea);
2080 if (!ea.skip && got_int)
2081 {
2082 ea.skip = TRUE;
2083 (void)do_intthrow(cstack);
2084 }
2085#endif
2086
2087/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002088 * 3. Skip over the range to find the command. Let "p" point to after it.
2089 *
2090 * We need the command to know what kind of range it uses.
2091 */
2092 cmd = ea.cmd;
2093 ea.cmd = skip_range(ea.cmd, NULL);
2094 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2095 ea.cmd = skipwhite(ea.cmd + 1);
2096 p = find_command(&ea, NULL);
2097
2098/*
2099 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 *
2101 * where 'addr' is:
2102 *
2103 * % (entire file)
2104 * $ [+-NUM]
2105 * 'x [+-NUM] (where x denotes a currently defined mark)
2106 * . [+-NUM]
2107 * [+-NUM]..
2108 * NUM
2109 *
2110 * The ea.cmd pointer is updated to point to the first character following the
2111 * range spec. If an initial address is found, but no second, the upper bound
2112 * is equal to the lower.
2113 */
2114
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002115 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002116 if (!IS_USER_CMDIDX(ea.cmdidx))
2117 {
2118 if (ea.cmdidx != CMD_SIZE)
2119 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2120 else
2121 ea.addr_type = ADDR_LINES;
2122
2123#ifdef FEAT_WINDOWS
2124 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002125 if (ea.cmdidx == CMD_wincmd && p != NULL)
2126 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002127#endif
2128 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002129
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002131 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 for (;;)
2133 {
2134 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002135 switch (ea.addr_type)
2136 {
2137 case ADDR_LINES:
2138 /* default is current line number */
2139 ea.line2 = curwin->w_cursor.lnum;
2140 break;
2141 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002142 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002143 ea.line2 = lnum;
2144 break;
2145 case ADDR_ARGUMENTS:
2146 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002147 if (ea.line2 > ARGCOUNT)
2148 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002149 break;
2150 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002151 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002152 ea.line2 = curbuf->b_fnum;
2153 break;
2154 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002155 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002156 ea.line2 = lnum;
2157 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002158#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002159 case ADDR_QUICKFIX:
2160 ea.line2 = qf_get_cur_valid_idx(&ea);
2161 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002162#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002165 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip, ea.addr_count == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 if (ea.cmd == NULL) /* error detected */
2167 goto doend;
2168 if (lnum == MAXLNUM)
2169 {
2170 if (*ea.cmd == '%') /* '%' - all lines */
2171 {
2172 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002173 switch (ea.addr_type)
2174 {
2175 case ADDR_LINES:
2176 ea.line1 = 1;
2177 ea.line2 = curbuf->b_ml.ml_line_count;
2178 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002179 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002180 {
2181 buf_T *buf = firstbuf;
2182
2183 while (buf->b_next != NULL
2184 && buf->b_ml.ml_mfp == NULL)
2185 buf = buf->b_next;
2186 ea.line1 = buf->b_fnum;
2187 buf = lastbuf;
2188 while (buf->b_prev != NULL
2189 && buf->b_ml.ml_mfp == NULL)
2190 buf = buf->b_prev;
2191 ea.line2 = buf->b_fnum;
2192 break;
2193 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002194 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002195 ea.line1 = firstbuf->b_fnum;
2196 ea.line2 = lastbuf->b_fnum;
2197 break;
2198 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002199 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002200 if (IS_USER_CMDIDX(ea.cmdidx))
2201 {
2202 ea.line1 = 1;
2203 ea.line2 = ea.addr_type == ADDR_WINDOWS
2204 ? LAST_WIN_NR : LAST_TAB_NR;
2205 }
2206 else
2207 {
2208 /* there is no Vim command which uses '%' and
2209 * ADDR_WINDOWS or ADDR_TABS */
2210 errormsg = (char_u *)_(e_invrange);
2211 goto doend;
2212 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002213 break;
2214 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002215 if (ARGCOUNT == 0)
2216 ea.line1 = ea.line2 = 0;
2217 else
2218 {
2219 ea.line1 = 1;
2220 ea.line2 = ARGCOUNT;
2221 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002222 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002223#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002224 case ADDR_QUICKFIX:
2225 ea.line1 = 1;
2226 ea.line2 = qf_get_size(&ea);
2227 if (ea.line2 == 0)
2228 ea.line2 = 1;
2229 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002230#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 ++ea.addr_count;
2233 }
2234 /* '*' - visual area */
2235 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2236 {
2237 pos_T *fp;
2238
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002239 if (ea.addr_type != ADDR_LINES)
2240 {
2241 errormsg = (char_u *)_(e_invrange);
2242 goto doend;
2243 }
2244
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 ++ea.cmd;
2246 if (!ea.skip)
2247 {
2248 fp = getmark('<', FALSE);
2249 if (check_mark(fp) == FAIL)
2250 goto doend;
2251 ea.line1 = fp->lnum;
2252 fp = getmark('>', FALSE);
2253 if (check_mark(fp) == FAIL)
2254 goto doend;
2255 ea.line2 = fp->lnum;
2256 ++ea.addr_count;
2257 }
2258 }
2259 }
2260 else
2261 ea.line2 = lnum;
2262 ea.addr_count++;
2263
2264 if (*ea.cmd == ';')
2265 {
2266 if (!ea.skip)
2267 curwin->w_cursor.lnum = ea.line2;
2268 }
2269 else if (*ea.cmd != ',')
2270 break;
2271 ++ea.cmd;
2272 }
2273
2274 /* One address given: set start and end lines */
2275 if (ea.addr_count == 1)
2276 {
2277 ea.line1 = ea.line2;
2278 /* ... but only implicit: really no address given */
2279 if (lnum == MAXLNUM)
2280 ea.addr_count = 0;
2281 }
2282
2283 /* Don't leave the cursor on an illegal line (caused by ';') */
2284 check_cursor_lnum();
2285
2286/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002287 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 */
2289
2290 /*
2291 * Skip ':' and any white space
2292 */
2293 ea.cmd = skipwhite(ea.cmd);
2294 while (*ea.cmd == ':')
2295 ea.cmd = skipwhite(ea.cmd + 1);
2296
2297 /*
2298 * If we got a line, but no command, then go to the line.
2299 * If we find a '|' or '\n' we set ea.nextcmd.
2300 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002301 if (*ea.cmd == NUL || *ea.cmd == '"'
2302 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
2304 /*
2305 * strange vi behaviour:
2306 * ":3" jumps to line 3
2307 * ":3|..." prints line 3
2308 * ":|" prints current line
2309 */
2310 if (ea.skip) /* skip this if inside :if */
2311 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002312 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 {
2314 ea.cmdidx = CMD_print;
2315 ea.argt = RANGE+COUNT+TRLBAR;
2316 if ((errormsg = invalid_range(&ea)) == NULL)
2317 {
2318 correct_range(&ea);
2319 ex_print(&ea);
2320 }
2321 }
2322 else if (ea.addr_count != 0)
2323 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002324 if (ea.line2 > curbuf->b_ml.ml_line_count)
2325 {
2326 /* With '-' in 'cpoptions' a line number past the file is an
2327 * error, otherwise put it at the end of the file. */
2328 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2329 ea.line2 = -1;
2330 else
2331 ea.line2 = curbuf->b_ml.ml_line_count;
2332 }
2333
2334 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002335 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 else
2337 {
2338 if (ea.line2 == 0)
2339 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 else
2341 curwin->w_cursor.lnum = ea.line2;
2342 beginline(BL_SOL | BL_FIX);
2343 }
2344 }
2345 goto doend;
2346 }
2347
Bram Moolenaard5005162014-08-22 23:05:54 +02002348#ifdef FEAT_AUTOCMD
2349 /* If this looks like an undefined user command and there are CmdUndefined
2350 * autocommands defined, trigger the matching autocommands. */
2351 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2352 && ASCII_ISUPPER(*ea.cmd)
2353 && has_cmdundefined())
2354 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002355 int ret;
2356
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002357 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002358 while (ASCII_ISALNUM(*p))
2359 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002360 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002361 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2362 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002363 /* If the autocommands did something and didn't cause an error, try
2364 * finding the command again. */
2365 p = (ret && !aborting()) ? find_command(&ea, NULL) : NULL;
Bram Moolenaard5005162014-08-22 23:05:54 +02002366 }
2367#endif
2368
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369#ifdef FEAT_USR_CMDS
2370 if (p == NULL)
2371 {
2372 if (!ea.skip)
2373 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2374 goto doend;
2375 }
2376 /* Check for wrong commands. */
2377 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2378 {
2379 errormsg = uc_fun_cmd();
2380 goto doend;
2381 }
2382#endif
2383 if (ea.cmdidx == CMD_SIZE)
2384 {
2385 if (!ea.skip)
2386 {
2387 STRCPY(IObuff, _("E492: Not an editor command"));
2388 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002389 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002391 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 }
2393 goto doend;
2394 }
2395
Bram Moolenaar958636c2014-10-21 20:01:58 +02002396 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2397 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002398#ifdef HAVE_EX_SCRIPT_NI
2399 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2400#endif
2401 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402
2403#ifndef FEAT_EVAL
2404 /*
2405 * When the expression evaluation is disabled, recognize the ":if" and
2406 * ":endif" commands and ignore everything in between it.
2407 */
2408 if (ea.cmdidx == CMD_if)
2409 ++if_level;
2410 if (if_level)
2411 {
2412 if (ea.cmdidx == CMD_endif)
2413 --if_level;
2414 goto doend;
2415 }
2416
2417#endif
2418
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002419 /* forced commands */
2420 if (*p == '!' && ea.cmdidx != CMD_substitute
2421 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 {
2423 ++p;
2424 ea.forceit = TRUE;
2425 }
2426 else
2427 ea.forceit = FALSE;
2428
2429/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002430 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002432 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002433 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434
2435 if (!ea.skip)
2436 {
2437#ifdef HAVE_SANDBOX
2438 if (sandbox != 0 && !(ea.argt & SBOXOK))
2439 {
2440 /* Command not allowed in sandbox. */
2441 errormsg = (char_u *)_(e_sandbox);
2442 goto doend;
2443 }
2444#endif
2445 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2446 {
2447 /* Command not allowed in non-'modifiable' buffer */
2448 errormsg = (char_u *)_(e_modifiable);
2449 goto doend;
2450 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002451
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002452 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002453 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002455 /* Command not allowed when editing the command line. */
2456#ifdef FEAT_CMDWIN
2457 if (cmdwin_type != 0)
2458 errormsg = (char_u *)_(e_cmdwin);
2459 else
2460#endif
2461 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 goto doend;
2463 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002464#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002465 /* Disallow editing another buffer when "curbuf_lock" is set.
2466 * Do allow ":edit" (check for argument later).
2467 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002468 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002469 && ea.cmdidx != CMD_edit
2470 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002471 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002472 && curbuf_locked())
2473 goto doend;
2474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475
2476 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2477 {
2478 /* no range allowed */
2479 errormsg = (char_u *)_(e_norange);
2480 goto doend;
2481 }
2482 }
2483
2484 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2485 {
2486 errormsg = (char_u *)_(e_nobang);
2487 goto doend;
2488 }
2489
2490 /*
2491 * Don't complain about the range if it is not used
2492 * (could happen if line_count is accidentally set to 0).
2493 */
2494 if (!ea.skip && !ni)
2495 {
2496 /*
2497 * If the range is backwards, ask for confirmation and, if given, swap
2498 * ea.line1 & ea.line2 so it's forwards again.
2499 * When global command is busy, don't ask, will fail below.
2500 */
2501 if (!global_busy && ea.line1 > ea.line2)
2502 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002503 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002505 if (sourcing || exmode_active)
2506 {
2507 errormsg = (char_u *)_("E493: Backwards range given");
2508 goto doend;
2509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 if (ask_yesno((char_u *)
2511 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002512 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 }
2514 lnum = ea.line1;
2515 ea.line1 = ea.line2;
2516 ea.line2 = lnum;
2517 }
2518 if ((errormsg = invalid_range(&ea)) != NULL)
2519 goto doend;
2520 }
2521
2522 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2523 ea.line2 = 1;
2524
2525 correct_range(&ea);
2526
2527#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002528 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2529 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 {
2531 /* Put the first line at the start of a closed fold, put the last line
2532 * at the end of a closed fold. */
2533 (void)hasFolding(ea.line1, &ea.line1, NULL);
2534 (void)hasFolding(ea.line2, NULL, &ea.line2);
2535 }
2536#endif
2537
2538#ifdef FEAT_QUICKFIX
2539 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002540 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 * option here, so things like % get expanded.
2542 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002543 p = replace_makeprg(&ea, p, cmdlinep);
2544 if (p == NULL)
2545 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546#endif
2547
2548 /*
2549 * Skip to start of argument.
2550 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2551 */
2552 if (ea.cmdidx == CMD_bang)
2553 ea.arg = p;
2554 else
2555 ea.arg = skipwhite(p);
2556
2557 /*
2558 * Check for "++opt=val" argument.
2559 * Must be first, allow ":w ++enc=utf8 !cmd"
2560 */
2561 if (ea.argt & ARGOPT)
2562 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2563 if (getargopt(&ea) == FAIL && !ni)
2564 {
2565 errormsg = (char_u *)_(e_invarg);
2566 goto doend;
2567 }
2568
2569 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2570 {
2571 if (*ea.arg == '>') /* append */
2572 {
2573 if (*++ea.arg != '>') /* typed wrong */
2574 {
2575 errormsg = (char_u *)_("E494: Use w or w>>");
2576 goto doend;
2577 }
2578 ea.arg = skipwhite(ea.arg + 1);
2579 ea.append = TRUE;
2580 }
2581 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2582 {
2583 ++ea.arg;
2584 ea.usefilter = TRUE;
2585 }
2586 }
2587
2588 if (ea.cmdidx == CMD_read)
2589 {
2590 if (ea.forceit)
2591 {
2592 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2593 ea.forceit = FALSE;
2594 }
2595 else if (*ea.arg == '!') /* :r !filter */
2596 {
2597 ++ea.arg;
2598 ea.usefilter = TRUE;
2599 }
2600 }
2601
2602 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2603 {
2604 ea.amount = 1;
2605 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2606 {
2607 ++ea.arg;
2608 ++ea.amount;
2609 }
2610 ea.arg = skipwhite(ea.arg);
2611 }
2612
2613 /*
2614 * Check for "+command" argument, before checking for next command.
2615 * Don't do this for ":read !cmd" and ":write !cmd".
2616 */
2617 if ((ea.argt & EDITCMD) && !ea.usefilter)
2618 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2619
2620 /*
2621 * Check for '|' to separate commands and '"' to start comments.
2622 * Don't do this for ":read !cmd" and ":write !cmd".
2623 */
2624 if ((ea.argt & TRLBAR) && !ea.usefilter)
2625 separate_nextcmd(&ea);
2626
2627 /*
2628 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002629 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2630 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002632 else if (ea.cmdidx == CMD_bang
2633 || ea.cmdidx == CMD_global
2634 || ea.cmdidx == CMD_vglobal
2635 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 {
2637 for (p = ea.arg; *p; ++p)
2638 {
2639 /* Remove one backslash before a newline, so that it's possible to
2640 * pass a newline to the shell and also a newline that is preceded
2641 * with a backslash. This makes it impossible to end a shell
2642 * command in a backslash, but that doesn't appear useful.
2643 * Halving the number of backslashes is incompatible with previous
2644 * versions. */
2645 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002646 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 else if (*p == '\n')
2648 {
2649 ea.nextcmd = p + 1;
2650 *p = NUL;
2651 break;
2652 }
2653 }
2654 }
2655
2656 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2657 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002658 buf_T *buf;
2659
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002661 switch (ea.addr_type)
2662 {
2663 case ADDR_LINES:
2664 ea.line2 = curbuf->b_ml.ml_line_count;
2665 break;
2666 case ADDR_LOADED_BUFFERS:
2667 buf = firstbuf;
2668 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2669 buf = buf->b_next;
2670 ea.line1 = buf->b_fnum;
2671 buf = lastbuf;
2672 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2673 buf = buf->b_prev;
2674 ea.line2 = buf->b_fnum;
2675 break;
2676 case ADDR_BUFFERS:
2677 ea.line1 = firstbuf->b_fnum;
2678 ea.line2 = lastbuf->b_fnum;
2679 break;
2680 case ADDR_WINDOWS:
2681 ea.line2 = LAST_WIN_NR;
2682 break;
2683 case ADDR_TABS:
2684 ea.line2 = LAST_TAB_NR;
2685 break;
2686 case ADDR_ARGUMENTS:
2687 if (ARGCOUNT == 0)
2688 ea.line1 = ea.line2 = 0;
2689 else
2690 ea.line2 = ARGCOUNT;
2691 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002692#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002693 case ADDR_QUICKFIX:
2694 ea.line2 = qf_get_size(&ea);
2695 if (ea.line2 == 0)
2696 ea.line2 = 1;
2697 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002698#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 }
2701
2702 /* accept numbered register only when no count allowed (:put) */
2703 if ( (ea.argt & REGSTR)
2704 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002705 /* Do not allow register = for user commands */
2706 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2708 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002709#ifndef FEAT_CLIPBOARD
2710 /* check these explicitly for a more specific error message */
2711 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002713 errormsg = (char_u *)_(e_invalidreg);
2714 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 }
2716#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002717 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2718 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002719 {
2720 ea.regname = *ea.arg++;
2721#ifdef FEAT_EVAL
2722 /* for '=' register: accept the rest of the line as an expression */
2723 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2724 {
2725 set_expr_line(vim_strsave(ea.arg));
2726 ea.arg += STRLEN(ea.arg);
2727 }
2728#endif
2729 ea.arg = skipwhite(ea.arg);
2730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 }
2732
2733 /*
2734 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2735 * count, it's a buffer name.
2736 */
2737 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2738 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2739 || vim_iswhite(*p)))
2740 {
2741 n = getdigits(&ea.arg);
2742 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002743 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 {
2745 errormsg = (char_u *)_(e_zerocount);
2746 goto doend;
2747 }
2748 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2749 {
2750 ea.line2 = n;
2751 if (ea.addr_count == 0)
2752 ea.addr_count = 1;
2753 }
2754 else
2755 {
2756 ea.line1 = ea.line2;
2757 ea.line2 += n - 1;
2758 ++ea.addr_count;
2759 /*
2760 * Be vi compatible: no error message for out of range.
2761 */
2762 if (ea.line2 > curbuf->b_ml.ml_line_count)
2763 ea.line2 = curbuf->b_ml.ml_line_count;
2764 }
2765 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002766
2767 /*
2768 * Check for flags: 'l', 'p' and '#'.
2769 */
2770 if (ea.argt & EXFLAGS)
2771 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002773 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002774 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 {
2776 errormsg = (char_u *)_(e_trailing);
2777 goto doend;
2778 }
2779
2780 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2781 {
2782 errormsg = (char_u *)_(e_argreq);
2783 goto doend;
2784 }
2785
2786#ifdef FEAT_EVAL
2787 /*
2788 * Skip the command when it's not going to be executed.
2789 * The commands like :if, :endif, etc. always need to be executed.
2790 * Also make an exception for commands that handle a trailing command
2791 * themselves.
2792 */
2793 if (ea.skip)
2794 {
2795 switch (ea.cmdidx)
2796 {
2797 /* commands that need evaluation */
2798 case CMD_while:
2799 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002800 case CMD_for:
2801 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 case CMD_if:
2803 case CMD_elseif:
2804 case CMD_else:
2805 case CMD_endif:
2806 case CMD_try:
2807 case CMD_catch:
2808 case CMD_finally:
2809 case CMD_endtry:
2810 case CMD_function:
2811 break;
2812
2813 /* Commands that handle '|' themselves. Check: A command should
2814 * either have the TRLBAR flag, appear in this list or appear in
2815 * the list at ":help :bar". */
2816 case CMD_aboveleft:
2817 case CMD_and:
2818 case CMD_belowright:
2819 case CMD_botright:
2820 case CMD_browse:
2821 case CMD_call:
2822 case CMD_confirm:
2823 case CMD_delfunction:
2824 case CMD_djump:
2825 case CMD_dlist:
2826 case CMD_dsearch:
2827 case CMD_dsplit:
2828 case CMD_echo:
2829 case CMD_echoerr:
2830 case CMD_echomsg:
2831 case CMD_echon:
2832 case CMD_execute:
2833 case CMD_help:
2834 case CMD_hide:
2835 case CMD_ijump:
2836 case CMD_ilist:
2837 case CMD_isearch:
2838 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002839 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 case CMD_keepjumps:
2841 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002842 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 case CMD_leftabove:
2844 case CMD_let:
2845 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002846 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002848 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002849 case CMD_noautocmd:
2850 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 case CMD_perl:
2852 case CMD_psearch:
2853 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002854 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002855 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 case CMD_return:
2857 case CMD_rightbelow:
2858 case CMD_ruby:
2859 case CMD_silent:
2860 case CMD_smagic:
2861 case CMD_snomagic:
2862 case CMD_substitute:
2863 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002864 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 case CMD_tcl:
2866 case CMD_throw:
2867 case CMD_tilde:
2868 case CMD_topleft:
2869 case CMD_unlet:
2870 case CMD_verbose:
2871 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002872 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 break;
2874
2875 default: goto doend;
2876 }
2877 }
2878#endif
2879
2880 if (ea.argt & XFILE)
2881 {
2882 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2883 goto doend;
2884 }
2885
2886#ifdef FEAT_LISTCMDS
2887 /*
2888 * Accept buffer name. Cannot be used at the same time with a buffer
2889 * number. Don't do this for a user command.
2890 */
2891 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002892 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 {
2894 /*
2895 * :bdelete, :bwipeout and :bunload take several arguments, separated
2896 * by spaces: find next space (skipping over escaped characters).
2897 * The others take one argument: ignore trailing spaces.
2898 */
2899 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2900 || ea.cmdidx == CMD_bunload)
2901 p = skiptowhite_esc(ea.arg);
2902 else
2903 {
2904 p = ea.arg + STRLEN(ea.arg);
2905 while (p > ea.arg && vim_iswhite(p[-1]))
2906 --p;
2907 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002908 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2909 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 if (ea.line2 < 0) /* failed */
2911 goto doend;
2912 ea.addr_count = 1;
2913 ea.arg = skipwhite(p);
2914 }
2915#endif
2916
2917/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002918 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 *
2920 * The "ea" structure holds the arguments that can be used.
2921 */
2922 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002923 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 ea.cookie = cookie;
2925#ifdef FEAT_EVAL
2926 ea.cstack = cstack;
2927#endif
2928
2929#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002930 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 {
2932 /*
2933 * Execute a user-defined command.
2934 */
2935 do_ucmd(&ea);
2936 }
2937 else
2938#endif
2939 {
2940 /*
2941 * Call the function to execute the command.
2942 */
2943 ea.errmsg = NULL;
2944 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2945 if (ea.errmsg != NULL)
2946 errormsg = (char_u *)_(ea.errmsg);
2947 }
2948
2949#ifdef FEAT_EVAL
2950 /*
2951 * If the command just executed called do_cmdline(), any throw or ":return"
2952 * or ":finish" encountered there must also check the cstack of the still
2953 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2954 * exception, or reanimate a returned function or finished script file and
2955 * return or finish it again.
2956 */
2957 if (need_rethrow)
2958 do_throw(cstack);
2959 else if (check_cstack)
2960 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002961 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002963 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 && current_func_returned())
2965 do_return(&ea, TRUE, FALSE, NULL);
2966 }
2967 need_rethrow = check_cstack = FALSE;
2968#endif
2969
2970doend:
2971 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2972 curwin->w_cursor.lnum = 1;
2973
2974 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2975 {
2976 if (sourcing)
2977 {
2978 if (errormsg != IObuff)
2979 {
2980 STRCPY(IObuff, errormsg);
2981 errormsg = IObuff;
2982 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002983 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 }
2985 emsg(errormsg);
2986 }
2987#ifdef FEAT_EVAL
2988 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002989 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2990 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991#endif
2992
2993 if (verbose_save >= 0)
2994 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002995#ifdef FEAT_AUTOCMD
2996 if (cmdmod.save_ei != NULL)
2997 {
2998 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002999 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
3000 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00003001 free_string_option(cmdmod.save_ei);
3002 }
3003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004
3005 cmdmod = save_cmdmod;
3006
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003007 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 {
3009 /* messages could be enabled for a serious error, need to check if the
3010 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00003011 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003012 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 emsg_silent -= did_esilent;
3014 if (emsg_silent < 0)
3015 emsg_silent = 0;
3016 /* Restore msg_scroll, it's set by file I/O commands, even when no
3017 * message is actually displayed. */
3018 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003019
3020 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
3021 * somewhere in the line. Put it back in the first column. */
3022 if (redirecting())
3023 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 }
3025
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003026#ifdef HAVE_SANDBOX
3027 if (did_sandbox)
3028 --sandbox;
3029#endif
3030
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3032 ea.nextcmd = NULL;
3033
3034#ifdef FEAT_EVAL
3035 --ex_nesting_level;
3036#endif
3037
3038 return ea.nextcmd;
3039}
3040#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003041 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042#endif
3043
3044/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003045 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 * If there is a match advance "pp" to the argument and return TRUE.
3047 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003048 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003049checkforcmd(
3050 char_u **pp, /* start of command */
3051 char *cmd, /* name of command */
3052 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053{
3054 int i;
3055
3056 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003057 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 break;
3059 if (i >= len && !isalpha((*pp)[i]))
3060 {
3061 *pp = skipwhite(*pp + i);
3062 return TRUE;
3063 }
3064 return FALSE;
3065}
3066
3067/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003068 * Append "cmd" to the error message in IObuff.
3069 * Takes care of limiting the length and handling 0xa0, which would be
3070 * invisible otherwise.
3071 */
3072 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003073append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003074{
3075 char_u *s = cmd;
3076 char_u *d;
3077
3078 STRCAT(IObuff, ": ");
3079 d = IObuff + STRLEN(IObuff);
3080 while (*s != NUL && d - IObuff < IOSIZE - 7)
3081 {
3082 if (
3083#ifdef FEAT_MBYTE
3084 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3085#endif
3086 *s == 0xa0)
3087 {
3088 s +=
3089#ifdef FEAT_MBYTE
3090 enc_utf8 ? 2 :
3091#endif
3092 1;
3093 STRCPY(d, "<a0>");
3094 d += 4;
3095 }
3096 else
3097 MB_COPY_CHAR(s, d);
3098 }
3099 *d = NUL;
3100}
3101
3102/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003104 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003106 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 * Returns NULL for an ambiguous user command.
3108 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003110find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111{
3112 int len;
3113 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003114 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115
3116 /*
3117 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003118 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 * - the 'k' command can directly be followed by any character.
3120 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003121 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003123 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 */
3125 p = eap->cmd;
3126 if (*p == 'k')
3127 {
3128 eap->cmdidx = CMD_k;
3129 ++p;
3130 }
3131 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003132 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3133 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 || p[1] == 'g'
3135 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3136 || p[1] == 'I'
3137 || (p[1] == 'r' && p[2] != 'e')))
3138 {
3139 eap->cmdidx = CMD_substitute;
3140 ++p;
3141 }
3142 else
3143 {
3144 while (ASCII_ISALPHA(*p))
3145 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003146 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003147 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003148 while (ASCII_ISALNUM(*p))
3149 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003150
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 /* check for non-alpha command */
3152 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3153 ++p;
3154 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003155 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3156 {
3157 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3158 * :delete with the 'l' flag. Same for 'p'. */
3159 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003160 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003161 break;
3162 if (i == len - 1)
3163 {
3164 --len;
3165 if (p[-1] == 'l')
3166 eap->flags |= EXFLAG_LIST;
3167 else
3168 eap->flags |= EXFLAG_PRINT;
3169 }
3170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171
3172 if (ASCII_ISLOWER(*eap->cmd))
3173 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
3174 else
3175 eap->cmdidx = cmdidxs[26];
3176
3177 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3178 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3179 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3180 (size_t)len) == 0)
3181 {
3182#ifdef FEAT_EVAL
3183 if (full != NULL
3184 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3185 *full = TRUE;
3186#endif
3187 break;
3188 }
3189
3190#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003191 /* Look for a user defined command as a last resort. Let ":Print" be
3192 * overruled by a user defined command. */
3193 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3194 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003196 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 while (ASCII_ISALNUM(*p))
3198 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003199 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 }
3201#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003202 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 eap->cmdidx = CMD_SIZE;
3204 }
3205
3206 return p;
3207}
3208
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003209#ifdef FEAT_USR_CMDS
3210/*
3211 * Search for a user command that matches "eap->cmd".
3212 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3213 * Return a pointer to just after the command.
3214 * Return NULL if there is no matching command.
3215 */
3216 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003217find_ucmd(
3218 exarg_T *eap,
3219 char_u *p, /* end of the command (possibly including count) */
3220 int *full, /* set to TRUE for a full match */
3221 expand_T *xp, /* used for completion, NULL otherwise */
3222 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003223{
3224 int len = (int)(p - eap->cmd);
3225 int j, k, matchlen = 0;
3226 ucmd_T *uc;
3227 int found = FALSE;
3228 int possible = FALSE;
3229 char_u *cp, *np; /* Point into typed cmd and test name */
3230 garray_T *gap;
3231 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3232 only full match global is accepted. */
3233
3234 /*
3235 * Look for buffer-local user commands first, then global ones.
3236 */
3237 gap = &curbuf->b_ucmds;
3238 for (;;)
3239 {
3240 for (j = 0; j < gap->ga_len; ++j)
3241 {
3242 uc = USER_CMD_GA(gap, j);
3243 cp = eap->cmd;
3244 np = uc->uc_name;
3245 k = 0;
3246 while (k < len && *np != NUL && *cp++ == *np++)
3247 k++;
3248 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3249 {
3250 /* If finding a second match, the command is ambiguous. But
3251 * not if a buffer-local command wasn't a full match and a
3252 * global command is a full match. */
3253 if (k == len && found && *np != NUL)
3254 {
3255 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003256 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003257 amb_local = TRUE;
3258 }
3259
3260 if (!found || (k == len && *np == NUL))
3261 {
3262 /* If we matched up to a digit, then there could
3263 * be another command including the digit that we
3264 * should use instead.
3265 */
3266 if (k == len)
3267 found = TRUE;
3268 else
3269 possible = TRUE;
3270
3271 if (gap == &ucmds)
3272 eap->cmdidx = CMD_USER;
3273 else
3274 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003275 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003276 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003277 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003278
3279# ifdef FEAT_CMDL_COMPL
3280 if (compl != NULL)
3281 *compl = uc->uc_compl;
3282# ifdef FEAT_EVAL
3283 if (xp != NULL)
3284 {
3285 xp->xp_arg = uc->uc_compl_arg;
3286 xp->xp_scriptID = uc->uc_scriptID;
3287 }
3288# endif
3289# endif
3290 /* Do not search for further abbreviations
3291 * if this is an exact match. */
3292 matchlen = k;
3293 if (k == len && *np == NUL)
3294 {
3295 if (full != NULL)
3296 *full = TRUE;
3297 amb_local = FALSE;
3298 break;
3299 }
3300 }
3301 }
3302 }
3303
3304 /* Stop if we found a full match or searched all. */
3305 if (j < gap->ga_len || gap == &ucmds)
3306 break;
3307 gap = &ucmds;
3308 }
3309
3310 /* Only found ambiguous matches. */
3311 if (amb_local)
3312 {
3313 if (xp != NULL)
3314 xp->xp_context = EXPAND_UNSUCCESSFUL;
3315 return NULL;
3316 }
3317
3318 /* The match we found may be followed immediately by a number. Move "p"
3319 * back to point to it. */
3320 if (found || possible)
3321 return p + (matchlen - len);
3322 return p;
3323}
3324#endif
3325
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003327static struct cmdmod
3328{
3329 char *name;
3330 int minlen;
3331 int has_count; /* :123verbose :3tab */
3332} cmdmods[] = {
3333 {"aboveleft", 3, FALSE},
3334 {"belowright", 3, FALSE},
3335 {"botright", 2, FALSE},
3336 {"browse", 3, FALSE},
3337 {"confirm", 4, FALSE},
3338 {"hide", 3, FALSE},
3339 {"keepalt", 5, FALSE},
3340 {"keepjumps", 5, FALSE},
3341 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003342 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003343 {"leftabove", 5, FALSE},
3344 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003345 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003346 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003347 {"rightbelow", 6, FALSE},
3348 {"sandbox", 3, FALSE},
3349 {"silent", 3, FALSE},
3350 {"tab", 3, TRUE},
3351 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003352 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003353 {"verbose", 4, TRUE},
3354 {"vertical", 4, FALSE},
3355};
3356
3357/*
3358 * Return length of a command modifier (including optional count).
3359 * Return zero when it's not a modifier.
3360 */
3361 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003362modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003363{
3364 int i, j;
3365 char_u *p = cmd;
3366
3367 if (VIM_ISDIGIT(*cmd))
3368 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003369 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003370 {
3371 for (j = 0; p[j] != NUL; ++j)
3372 if (p[j] != cmdmods[i].name[j])
3373 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003374 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003375 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003376 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003377 }
3378 return 0;
3379}
3380
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381/*
3382 * Return > 0 if an Ex command "name" exists.
3383 * Return 2 if there is an exact match.
3384 * Return 3 if there is an ambiguous match.
3385 */
3386 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003387cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388{
3389 exarg_T ea;
3390 int full = FALSE;
3391 int i;
3392 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003393 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394
3395 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003396 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 {
3398 for (j = 0; name[j] != NUL; ++j)
3399 if (name[j] != cmdmods[i].name[j])
3400 break;
3401 if (name[j] == NUL && j >= cmdmods[i].minlen)
3402 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3403 }
3404
Bram Moolenaara9587612006-05-04 21:47:50 +00003405 /* Check built-in commands and user defined commands.
3406 * For ":2match" and ":3match" we need to skip the number. */
3407 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003409 p = find_command(&ea, &full);
3410 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003412 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3413 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003414 if (*skipwhite(p) != NUL)
3415 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3417}
3418#endif
3419
3420/*
3421 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3422 * we don't need/want deleted. Maybe this could be done better if we didn't
3423 * repeat all this stuff. The only problem is that they may not stay
3424 * perfectly compatible with each other, but then the command line syntax
3425 * probably won't change that much -- webb.
3426 */
3427 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003428set_one_cmd_context(
3429 expand_T *xp,
3430 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431{
3432 char_u *p;
3433 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003434 int len = 0;
3435 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3437 int compl = EXPAND_NOTHING;
3438#endif
3439#ifdef FEAT_CMDL_COMPL
3440 int delim;
3441#endif
3442 int forceit = FALSE;
3443 int usefilter = FALSE; /* filter instead of file name */
3444
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003445 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 xp->xp_pattern = buff;
3447 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003448 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449
3450/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003451 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 */
3453 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3454 ;
3455 xp->xp_pattern = cmd;
3456
3457 if (*cmd == NUL)
3458 return NULL;
3459 if (*cmd == '"') /* ignore comment lines */
3460 {
3461 xp->xp_context = EXPAND_NOTHING;
3462 return NULL;
3463 }
3464
3465/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003466 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 */
3468 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 xp->xp_pattern = cmd;
3470 if (*cmd == NUL)
3471 return NULL;
3472 if (*cmd == '"')
3473 {
3474 xp->xp_context = EXPAND_NOTHING;
3475 return NULL;
3476 }
3477
3478 if (*cmd == '|' || *cmd == '\n')
3479 return cmd + 1; /* There's another command */
3480
3481 /*
3482 * Isolate the command and search for it in the command table.
3483 * Exceptions:
3484 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003485 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3487 */
3488 if (*cmd == 'k' && cmd[1] != 'e')
3489 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003490 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 p = cmd + 1;
3492 }
3493 else
3494 {
3495 p = cmd;
3496 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3497 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003498 /* a user command may contain digits */
3499 if (ASCII_ISUPPER(cmd[0]))
3500 while (ASCII_ISALNUM(*p) || *p == '*')
3501 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003502 /* for python 3.x: ":py3*" commands completion */
3503 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003504 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003505 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003506 while (ASCII_ISALPHA(*p) || *p == '*')
3507 ++p;
3508 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003509 /* check for non-alpha command */
3510 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3511 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003512 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003514 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 {
3516 xp->xp_context = EXPAND_UNSUCCESSFUL;
3517 return NULL;
3518 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003519 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003520 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3521 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3522 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 break;
3524
3525#ifdef FEAT_USR_CMDS
3526 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3528 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529#endif
3530 }
3531
3532 /*
3533 * If the cursor is touching the command, and it ends in an alpha-numeric
3534 * character, complete the command name.
3535 */
3536 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3537 return NULL;
3538
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003539 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 {
3541 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3542 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003543 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 p = cmd + 1;
3545 }
3546#ifdef FEAT_USR_CMDS
3547 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3548 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003549 ea.cmd = cmd;
3550 p = find_ucmd(&ea, p, NULL, xp,
3551# if defined(FEAT_CMDL_COMPL)
3552 &compl
3553# else
3554 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003556 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003557 if (p == NULL)
3558 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 }
3560#endif
3561 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003562 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 {
3564 /* Not still touching the command and it was an illegal one */
3565 xp->xp_context = EXPAND_UNSUCCESSFUL;
3566 return NULL;
3567 }
3568
3569 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3570
3571 if (*p == '!') /* forced commands */
3572 {
3573 forceit = TRUE;
3574 ++p;
3575 }
3576
3577/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003578 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003580 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003581 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582
3583 arg = skipwhite(p);
3584
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003585 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 {
3587 if (*arg == '>') /* append */
3588 {
3589 if (*++arg == '>')
3590 ++arg;
3591 arg = skipwhite(arg);
3592 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003593 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 {
3595 ++arg;
3596 usefilter = TRUE;
3597 }
3598 }
3599
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003600 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 {
3602 usefilter = forceit; /* :r! filter if forced */
3603 if (*arg == '!') /* :r !filter */
3604 {
3605 ++arg;
3606 usefilter = TRUE;
3607 }
3608 }
3609
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003610 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 {
3612 while (*arg == *cmd) /* allow any number of '>' or '<' */
3613 ++arg;
3614 arg = skipwhite(arg);
3615 }
3616
3617 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003618 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 {
3620 /* Check if we're in the +command */
3621 p = arg + 1;
3622 arg = skip_cmd_arg(arg, FALSE);
3623
3624 /* Still touching the command after '+'? */
3625 if (*arg == NUL)
3626 return p;
3627
3628 /* Skip space(s) after +command to get to the real argument */
3629 arg = skipwhite(arg);
3630 }
3631
3632 /*
3633 * Check for '|' to separate commands and '"' to start comments.
3634 * Don't do this for ":read !cmd" and ":write !cmd".
3635 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003636 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 {
3638 p = arg;
3639 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003640 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 p += 2;
3642 while (*p)
3643 {
3644 if (*p == Ctrl_V)
3645 {
3646 if (p[1] != NUL)
3647 ++p;
3648 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003649 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 || *p == '|' || *p == '\n')
3651 {
3652 if (*(p - 1) != '\\')
3653 {
3654 if (*p == '|' || *p == '\n')
3655 return p + 1;
3656 return NULL; /* It's a comment */
3657 }
3658 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003659 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 }
3661 }
3662
3663 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003664 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 vim_strchr((char_u *)"|\"", *arg) == NULL)
3666 return NULL;
3667
3668 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003669 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003671 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003672 while (*p && p < buff + len)
3673 {
3674 if (*p == ' ' || *p == TAB)
3675 {
3676 /* argument starts after a space */
3677 xp->xp_pattern = ++p;
3678 }
3679 else
3680 {
3681 if (*p == '\\' && *(p + 1) != NUL)
3682 ++p; /* skip over escaped character */
3683 mb_ptr_adv(p);
3684 }
3685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003687 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003689 int c;
3690 int in_quote = FALSE;
3691 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692
3693 /*
3694 * Allow spaces within back-quotes to count as part of the argument
3695 * being expanded.
3696 */
3697 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003698 p = xp->xp_pattern;
3699 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003701#ifdef FEAT_MBYTE
3702 if (has_mbyte)
3703 c = mb_ptr2char(p);
3704 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003706 c = *p;
3707 if (c == '\\' && p[1] != NUL)
3708 ++p;
3709 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 {
3711 if (!in_quote)
3712 {
3713 xp->xp_pattern = p;
3714 bow = p + 1;
3715 }
3716 in_quote = !in_quote;
3717 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003718 /* An argument can contain just about everything, except
3719 * characters that end the command and white space. */
3720 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003721#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003722 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003723#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003724 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003725 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003726 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003727 while (*p != NUL)
3728 {
3729#ifdef FEAT_MBYTE
3730 if (has_mbyte)
3731 c = mb_ptr2char(p);
3732 else
3733#endif
3734 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003735 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003736 break;
3737#ifdef FEAT_MBYTE
3738 if (has_mbyte)
3739 len = (*mb_ptr2len)(p);
3740 else
3741#endif
3742 len = 1;
3743 mb_ptr_adv(p);
3744 }
3745 if (in_quote)
3746 bow = p;
3747 else
3748 xp->xp_pattern = p;
3749 p -= len;
3750 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003751 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 }
3753
3754 /*
3755 * If we are still inside the quotes, and we passed a space, just
3756 * expand from there.
3757 */
3758 if (bow != NULL && in_quote)
3759 xp->xp_pattern = bow;
3760 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003761
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003762 /* For a shell command more chars need to be escaped. */
3763 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003764 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003765#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003766 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003767#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003768 /* When still after the command name expand executables. */
3769 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003770 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772
3773 /* Check for environment variable */
3774 if (*xp->xp_pattern == '$'
Bram Moolenaare7fedb62015-12-31 19:07:19 +01003775#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 || *xp->xp_pattern == '%'
3777#endif
3778 )
3779 {
3780 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3781 if (!vim_isIDc(*p))
3782 break;
3783 if (*p == NUL)
3784 {
3785 xp->xp_context = EXPAND_ENV_VARS;
3786 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003787#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3788 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003789 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003790 compl = EXPAND_ENV_VARS;
3791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 }
3793 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003794#if defined(FEAT_CMDL_COMPL)
3795 /* Check for user names */
3796 if (*xp->xp_pattern == '~')
3797 {
3798 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3799 ;
3800 /* Complete ~user only if it partially matches a user name.
3801 * A full match ~user<Tab> will be replaced by user's home
3802 * directory i.e. something like ~user<Tab> -> /home/user/ */
3803 if (*p == NUL && p > xp->xp_pattern + 1
3804 && match_user(xp->xp_pattern + 1) == 1)
3805 {
3806 xp->xp_context = EXPAND_USER;
3807 ++xp->xp_pattern;
3808 }
3809 }
3810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 }
3812
3813/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003814 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003816 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003818 case CMD_find:
3819 case CMD_sfind:
3820 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003821 if (xp->xp_context == EXPAND_FILES)
3822 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003823 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 case CMD_cd:
3825 case CMD_chdir:
3826 case CMD_lcd:
3827 case CMD_lchdir:
3828 if (xp->xp_context == EXPAND_FILES)
3829 xp->xp_context = EXPAND_DIRECTORIES;
3830 break;
3831 case CMD_help:
3832 xp->xp_context = EXPAND_HELP;
3833 xp->xp_pattern = arg;
3834 break;
3835
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003836 /* Command modifiers: return the argument.
3837 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003839 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 case CMD_belowright:
3841 case CMD_botright:
3842 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003843 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003844 case CMD_cdo:
3845 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003847 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 case CMD_folddoclosed:
3849 case CMD_folddoopen:
3850 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003851 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 case CMD_keepjumps:
3853 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003854 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003855 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003857 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003859 case CMD_noautocmd:
3860 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003862 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003864 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003865 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 case CMD_topleft:
3867 case CMD_verbose:
3868 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003869 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 return arg;
3871
Bram Moolenaar4f688582007-07-24 12:34:30 +00003872#ifdef FEAT_CMDL_COMPL
3873# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 case CMD_match:
3875 if (*arg == NUL || !ends_excmd(*arg))
3876 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003877 /* also complete "None" */
3878 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 arg = skipwhite(skiptowhite(arg));
3880 if (*arg != NUL)
3881 {
3882 xp->xp_context = EXPAND_NOTHING;
3883 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3884 }
3885 }
3886 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003887# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889/*
3890 * All completion for the +cmdline_compl feature goes here.
3891 */
3892
3893# ifdef FEAT_USR_CMDS
3894 case CMD_command:
3895 /* Check for attributes */
3896 while (*arg == '-')
3897 {
3898 arg++; /* Skip "-" */
3899 p = skiptowhite(arg);
3900 if (*p == NUL)
3901 {
3902 /* Cursor is still in the attribute */
3903 p = vim_strchr(arg, '=');
3904 if (p == NULL)
3905 {
3906 /* No "=", so complete attribute names */
3907 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3908 xp->xp_pattern = arg;
3909 return NULL;
3910 }
3911
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003912 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 * their arguments as well.
3914 */
3915 if (STRNICMP(arg, "complete", p - arg) == 0)
3916 {
3917 xp->xp_context = EXPAND_USER_COMPLETE;
3918 xp->xp_pattern = p + 1;
3919 return NULL;
3920 }
3921 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3922 {
3923 xp->xp_context = EXPAND_USER_NARGS;
3924 xp->xp_pattern = p + 1;
3925 return NULL;
3926 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003927 else if (STRNICMP(arg, "addr", p - arg) == 0)
3928 {
3929 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3930 xp->xp_pattern = p + 1;
3931 return NULL;
3932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 return NULL;
3934 }
3935 arg = skipwhite(p);
3936 }
3937
3938 /* After the attributes comes the new command name */
3939 p = skiptowhite(arg);
3940 if (*p == NUL)
3941 {
3942 xp->xp_context = EXPAND_USER_COMMANDS;
3943 xp->xp_pattern = arg;
3944 break;
3945 }
3946
3947 /* And finally comes a normal command */
3948 return skipwhite(p);
3949
3950 case CMD_delcommand:
3951 xp->xp_context = EXPAND_USER_COMMANDS;
3952 xp->xp_pattern = arg;
3953 break;
3954# endif
3955
3956 case CMD_global:
3957 case CMD_vglobal:
3958 delim = *arg; /* get the delimiter */
3959 if (delim)
3960 ++arg; /* skip delimiter if there is one */
3961
3962 while (arg[0] != NUL && arg[0] != delim)
3963 {
3964 if (arg[0] == '\\' && arg[1] != NUL)
3965 ++arg;
3966 ++arg;
3967 }
3968 if (arg[0] != NUL)
3969 return arg + 1;
3970 break;
3971 case CMD_and:
3972 case CMD_substitute:
3973 delim = *arg;
3974 if (delim)
3975 {
3976 /* skip "from" part */
3977 ++arg;
3978 arg = skip_regexp(arg, delim, p_magic, NULL);
3979 }
3980 /* skip "to" part */
3981 while (arg[0] != NUL && arg[0] != delim)
3982 {
3983 if (arg[0] == '\\' && arg[1] != NUL)
3984 ++arg;
3985 ++arg;
3986 }
3987 if (arg[0] != NUL) /* skip delimiter */
3988 ++arg;
3989 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3990 ++arg;
3991 if (arg[0] != NUL)
3992 return arg;
3993 break;
3994 case CMD_isearch:
3995 case CMD_dsearch:
3996 case CMD_ilist:
3997 case CMD_dlist:
3998 case CMD_ijump:
3999 case CMD_psearch:
4000 case CMD_djump:
4001 case CMD_isplit:
4002 case CMD_dsplit:
4003 arg = skipwhite(skipdigits(arg)); /* skip count */
4004 if (*arg == '/') /* Match regexp, not just whole words */
4005 {
4006 for (++arg; *arg && *arg != '/'; arg++)
4007 if (*arg == '\\' && arg[1] != NUL)
4008 arg++;
4009 if (*arg)
4010 {
4011 arg = skipwhite(arg + 1);
4012
4013 /* Check for trailing illegal characters */
4014 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4015 xp->xp_context = EXPAND_NOTHING;
4016 else
4017 return arg;
4018 }
4019 }
4020 break;
4021#ifdef FEAT_AUTOCMD
4022 case CMD_autocmd:
4023 return set_context_in_autocmd(xp, arg, FALSE);
4024
4025 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004026 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 return set_context_in_autocmd(xp, arg, TRUE);
4028#endif
4029 case CMD_set:
4030 set_context_in_set_cmd(xp, arg, 0);
4031 break;
4032 case CMD_setglobal:
4033 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4034 break;
4035 case CMD_setlocal:
4036 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4037 break;
4038 case CMD_tag:
4039 case CMD_stag:
4040 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004041 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 case CMD_tselect:
4043 case CMD_stselect:
4044 case CMD_ptselect:
4045 case CMD_tjump:
4046 case CMD_stjump:
4047 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004048 if (*p_wop != NUL)
4049 xp->xp_context = EXPAND_TAGS_LISTFILES;
4050 else
4051 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 xp->xp_pattern = arg;
4053 break;
4054 case CMD_augroup:
4055 xp->xp_context = EXPAND_AUGROUP;
4056 xp->xp_pattern = arg;
4057 break;
4058#ifdef FEAT_SYN_HL
4059 case CMD_syntax:
4060 set_context_in_syntax_cmd(xp, arg);
4061 break;
4062#endif
4063#ifdef FEAT_EVAL
4064 case CMD_let:
4065 case CMD_if:
4066 case CMD_elseif:
4067 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004068 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 case CMD_echo:
4070 case CMD_echon:
4071 case CMD_execute:
4072 case CMD_echomsg:
4073 case CMD_echoerr:
4074 case CMD_call:
4075 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004076 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 break;
4078
4079 case CMD_unlet:
4080 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4081 arg = xp->xp_pattern + 1;
4082 xp->xp_context = EXPAND_USER_VARS;
4083 xp->xp_pattern = arg;
4084 break;
4085
4086 case CMD_function:
4087 case CMD_delfunction:
4088 xp->xp_context = EXPAND_USER_FUNC;
4089 xp->xp_pattern = arg;
4090 break;
4091
4092 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004093 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 break;
4095#endif
4096 case CMD_highlight:
4097 set_context_in_highlight_cmd(xp, arg);
4098 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004099#ifdef FEAT_CSCOPE
4100 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004101 case CMD_lcscope:
4102 case CMD_scscope:
4103 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004104 break;
4105#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004106#ifdef FEAT_SIGNS
4107 case CMD_sign:
4108 set_context_in_sign_cmd(xp, arg);
4109 break;
4110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111#ifdef FEAT_LISTCMDS
4112 case CMD_bdelete:
4113 case CMD_bwipeout:
4114 case CMD_bunload:
4115 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4116 arg = xp->xp_pattern + 1;
4117 /*FALLTHROUGH*/
4118 case CMD_buffer:
4119 case CMD_sbuffer:
4120 case CMD_checktime:
4121 xp->xp_context = EXPAND_BUFFERS;
4122 xp->xp_pattern = arg;
4123 break;
4124#endif
4125#ifdef FEAT_USR_CMDS
4126 case CMD_USER:
4127 case CMD_USER_BUF:
4128 if (compl != EXPAND_NOTHING)
4129 {
4130 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004131 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 {
4133# ifdef FEAT_MENU
4134 if (compl == EXPAND_MENUS)
4135 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4136# endif
4137 if (compl == EXPAND_COMMANDS)
4138 return arg;
4139 if (compl == EXPAND_MAPPINGS)
4140 return set_context_in_map_cmd(xp, (char_u *)"map",
4141 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004142 /* Find start of last argument. */
4143 p = arg;
4144 while (*p)
4145 {
4146 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004147 /* argument starts after a space */
4148 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004149 else if (*p == '\\' && *(p + 1) != NUL)
4150 ++p; /* skip over escaped character */
4151 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 xp->xp_pattern = arg;
4154 }
4155 xp->xp_context = compl;
4156 }
4157 break;
4158#endif
4159 case CMD_map: case CMD_noremap:
4160 case CMD_nmap: case CMD_nnoremap:
4161 case CMD_vmap: case CMD_vnoremap:
4162 case CMD_omap: case CMD_onoremap:
4163 case CMD_imap: case CMD_inoremap:
4164 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004165 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004166 case CMD_smap: case CMD_snoremap:
4167 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004169 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 case CMD_unmap:
4171 case CMD_nunmap:
4172 case CMD_vunmap:
4173 case CMD_ounmap:
4174 case CMD_iunmap:
4175 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004176 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004177 case CMD_sunmap:
4178 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004180 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 case CMD_abbreviate: case CMD_noreabbrev:
4182 case CMD_cabbrev: case CMD_cnoreabbrev:
4183 case CMD_iabbrev: case CMD_inoreabbrev:
4184 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004185 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 case CMD_unabbreviate:
4187 case CMD_cunabbrev:
4188 case CMD_iunabbrev:
4189 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004190 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191#ifdef FEAT_MENU
4192 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4193 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4194 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4195 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4196 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4197 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4198 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4199 case CMD_tmenu: case CMD_tunmenu:
4200 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4201 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4202#endif
4203
4204 case CMD_colorscheme:
4205 xp->xp_context = EXPAND_COLORS;
4206 xp->xp_pattern = arg;
4207 break;
4208
4209 case CMD_compiler:
4210 xp->xp_context = EXPAND_COMPILER;
4211 xp->xp_pattern = arg;
4212 break;
4213
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004214 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004215 xp->xp_context = EXPAND_OWNSYNTAX;
4216 xp->xp_pattern = arg;
4217 break;
4218
4219 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004220 xp->xp_context = EXPAND_FILETYPE;
4221 xp->xp_pattern = arg;
4222 break;
4223
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4225 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4226 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004227 p = skiptowhite(arg);
4228 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 {
4230 xp->xp_context = EXPAND_LANGUAGE;
4231 xp->xp_pattern = arg;
4232 }
4233 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004234 {
4235 if ( STRNCMP(arg, "messages", p - arg) == 0
4236 || STRNCMP(arg, "ctype", p - arg) == 0
4237 || STRNCMP(arg, "time", p - arg) == 0)
4238 {
4239 xp->xp_context = EXPAND_LOCALES;
4240 xp->xp_pattern = skipwhite(p);
4241 }
4242 else
4243 xp->xp_context = EXPAND_NOTHING;
4244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 break;
4246#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004247#if defined(FEAT_PROFILE)
4248 case CMD_profile:
4249 set_context_in_profile_cmd(xp, arg);
4250 break;
4251#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004252 case CMD_behave:
4253 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004254 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004255 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004257#if defined(FEAT_CMDHIST)
4258 case CMD_history:
4259 xp->xp_context = EXPAND_HISTORY;
4260 xp->xp_pattern = arg;
4261 break;
4262#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004263#if defined(FEAT_PROFILE)
4264 case CMD_syntime:
4265 xp->xp_context = EXPAND_SYNTIME;
4266 xp->xp_pattern = arg;
4267 break;
4268#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004269
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270#endif /* FEAT_CMDL_COMPL */
4271
4272 default:
4273 break;
4274 }
4275 return NULL;
4276}
4277
4278/*
4279 * skip a range specifier of the form: addr [,addr] [;addr] ..
4280 *
4281 * Backslashed delimiters after / or ? will be skipped, and commands will
4282 * not be expanded between /'s and ?'s or after "'".
4283 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004284 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 * Returns the "cmd" pointer advanced to beyond the range.
4286 */
4287 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004288skip_range(
4289 char_u *cmd,
4290 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004292 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293
Bram Moolenaardf177f62005-02-22 08:39:57 +00004294 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 {
4296 if (*cmd == '\'')
4297 {
4298 if (*++cmd == NUL && ctx != NULL)
4299 *ctx = EXPAND_NOTHING;
4300 }
4301 else if (*cmd == '/' || *cmd == '?')
4302 {
4303 delim = *cmd++;
4304 while (*cmd != NUL && *cmd != delim)
4305 if (*cmd++ == '\\' && *cmd != NUL)
4306 ++cmd;
4307 if (*cmd == NUL && ctx != NULL)
4308 *ctx = EXPAND_NOTHING;
4309 }
4310 if (*cmd != NUL)
4311 ++cmd;
4312 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004313
4314 /* Skip ":" and white space. */
4315 while (*cmd == ':')
4316 cmd = skipwhite(cmd + 1);
4317
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 return cmd;
4319}
4320
4321/*
4322 * get a single EX address
4323 *
4324 * Set ptr to the next character after the part that was interpreted.
4325 * Set ptr to NULL when an error is encountered.
4326 *
4327 * Return MAXLNUM when no Ex address was found.
4328 */
4329 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004330get_address(
4331 exarg_T *eap UNUSED,
4332 char_u **ptr,
4333 int addr_type, /* flag: one of ADDR_LINES, ... */
4334 int skip, /* only skip the address, don't use it */
4335 int to_other_file) /* flag: may jump to other file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336{
4337 int c;
4338 int i;
4339 long n;
4340 char_u *cmd;
4341 pos_T pos;
4342 pos_T *fp;
4343 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004344 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345
4346 cmd = skipwhite(*ptr);
4347 lnum = MAXLNUM;
4348 do
4349 {
4350 switch (*cmd)
4351 {
4352 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004353 ++cmd;
4354 switch (addr_type)
4355 {
4356 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 lnum = curwin->w_cursor.lnum;
4358 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004359 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004360 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004361 break;
4362 case ADDR_ARGUMENTS:
4363 lnum = curwin->w_arg_idx + 1;
4364 break;
4365 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004366 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004367 lnum = curbuf->b_fnum;
4368 break;
4369 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004370 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004371 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004372#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004373 case ADDR_QUICKFIX:
4374 lnum = qf_get_cur_valid_idx(eap);
4375 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004376#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004377 }
4378 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379
4380 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004381 ++cmd;
4382 switch (addr_type)
4383 {
4384 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 lnum = curbuf->b_ml.ml_line_count;
4386 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004387 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004388 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004389 break;
4390 case ADDR_ARGUMENTS:
4391 lnum = ARGCOUNT;
4392 break;
4393 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004394 buf = lastbuf;
4395 while (buf->b_ml.ml_mfp == NULL)
4396 {
4397 if (buf->b_prev == NULL)
4398 break;
4399 buf = buf->b_prev;
4400 }
4401 lnum = buf->b_fnum;
4402 break;
4403 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004404 lnum = lastbuf->b_fnum;
4405 break;
4406 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004407 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004408 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004409#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004410 case ADDR_QUICKFIX:
4411 lnum = qf_get_size(eap);
4412 if (lnum == 0)
4413 lnum = 1;
4414 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004415#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004416 }
4417 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418
4419 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004420 if (*++cmd == NUL)
4421 {
4422 cmd = NULL;
4423 goto error;
4424 }
4425 if (addr_type != ADDR_LINES)
4426 {
4427 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004428 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004429 goto error;
4430 }
4431 if (skip)
4432 ++cmd;
4433 else
4434 {
4435 /* Only accept a mark in another file when it is
4436 * used by itself: ":'M". */
4437 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4438 ++cmd;
4439 if (fp == (pos_T *)-1)
4440 /* Jumped to another file. */
4441 lnum = curwin->w_cursor.lnum;
4442 else
4443 {
4444 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 {
4446 cmd = NULL;
4447 goto error;
4448 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004449 lnum = fp->lnum;
4450 }
4451 }
4452 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
4454 case '/':
4455 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004456 c = *cmd++;
4457 if (addr_type != ADDR_LINES)
4458 {
4459 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004460 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004461 goto error;
4462 }
4463 if (skip) /* skip "/pat/" */
4464 {
4465 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4466 if (*cmd == c)
4467 ++cmd;
4468 }
4469 else
4470 {
4471 pos = curwin->w_cursor; /* save curwin->w_cursor */
4472 /*
4473 * When '/' or '?' follows another address, start
4474 * from there.
4475 */
4476 if (lnum != MAXLNUM)
4477 curwin->w_cursor.lnum = lnum;
4478 /*
4479 * Start a forward search at the end of the line.
4480 * Start a backward search at the start of the line.
4481 * This makes sure we never match in the current
4482 * line, and can match anywhere in the
4483 * next/previous line.
4484 */
4485 if (c == '/')
4486 curwin->w_cursor.col = MAXCOL;
4487 else
4488 curwin->w_cursor.col = 0;
4489 searchcmdlen = 0;
4490 if (!do_search(NULL, c, cmd, 1L,
4491 SEARCH_HIS | SEARCH_MSG, NULL))
4492 {
4493 curwin->w_cursor = pos;
4494 cmd = NULL;
4495 goto error;
4496 }
4497 lnum = curwin->w_cursor.lnum;
4498 curwin->w_cursor = pos;
4499 /* adjust command string pointer */
4500 cmd += searchcmdlen;
4501 }
4502 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503
4504 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004505 ++cmd;
4506 if (addr_type != ADDR_LINES)
4507 {
4508 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004509 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004510 goto error;
4511 }
4512 if (*cmd == '&')
4513 i = RE_SUBST;
4514 else if (*cmd == '?' || *cmd == '/')
4515 i = RE_SEARCH;
4516 else
4517 {
4518 EMSG(_(e_backslash));
4519 cmd = NULL;
4520 goto error;
4521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004523 if (!skip)
4524 {
4525 /*
4526 * When search follows another address, start from
4527 * there.
4528 */
4529 if (lnum != MAXLNUM)
4530 pos.lnum = lnum;
4531 else
4532 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004534 /*
4535 * Start the search just like for the above
4536 * do_search().
4537 */
4538 if (*cmd != '?')
4539 pos.col = MAXCOL;
4540 else
4541 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004542#ifdef FEAT_VIRTUALEDIT
4543 pos.coladd = 0;
4544#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004545 if (searchit(curwin, curbuf, &pos,
4546 *cmd == '?' ? BACKWARD : FORWARD,
4547 (char_u *)"", 1L, SEARCH_MSG,
4548 i, (linenr_T)0, NULL) != FAIL)
4549 lnum = pos.lnum;
4550 else
4551 {
4552 cmd = NULL;
4553 goto error;
4554 }
4555 }
4556 ++cmd;
4557 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558
4559 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004560 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4561 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 }
4563
4564 for (;;)
4565 {
4566 cmd = skipwhite(cmd);
4567 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4568 break;
4569
4570 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004571 {
4572 switch (addr_type)
4573 {
4574 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004575 /* "+1" is same as ".+1" */
4576 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004577 break;
4578 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004579 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004580 break;
4581 case ADDR_ARGUMENTS:
4582 lnum = curwin->w_arg_idx + 1;
4583 break;
4584 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004585 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004586 lnum = curbuf->b_fnum;
4587 break;
4588 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004589 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004590 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004591#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004592 case ADDR_QUICKFIX:
4593 lnum = qf_get_cur_valid_idx(eap);
4594 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004595#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004596 }
4597 }
4598
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 if (VIM_ISDIGIT(*cmd))
4600 i = '+'; /* "number" is same as "+number" */
4601 else
4602 i = *cmd++;
4603 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4604 n = 1;
4605 else
4606 n = getdigits(&cmd);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004607 if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004608 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004609 lnum = compute_buffer_local_count(
4610 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004611 else if (i == '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 lnum -= n;
4613 else
4614 lnum += n;
4615 }
4616 } while (*cmd == '/' || *cmd == '?');
4617
4618error:
4619 *ptr = cmd;
4620 return lnum;
4621}
4622
4623/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004624 * Get flags from an Ex command argument.
4625 */
4626 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004627get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004628{
4629 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4630 {
4631 if (*eap->arg == 'l')
4632 eap->flags |= EXFLAG_LIST;
4633 else if (*eap->arg == 'p')
4634 eap->flags |= EXFLAG_PRINT;
4635 else
4636 eap->flags |= EXFLAG_NR;
4637 eap->arg = skipwhite(eap->arg + 1);
4638 }
4639}
4640
4641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 * Function called for command which is Not Implemented. NI!
4643 */
4644 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004645ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646{
4647 if (!eap->skip)
4648 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4649}
4650
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004651#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652/*
4653 * Function called for script command which is Not Implemented. NI!
4654 * Skips over ":perl <<EOF" constructs.
4655 */
4656 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004657ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658{
4659 if (!eap->skip)
4660 ex_ni(eap);
4661 else
4662 vim_free(script_get(eap, eap->arg));
4663}
4664#endif
4665
4666/*
4667 * Check range in Ex command for validity.
4668 * Return NULL when valid, error message when invalid.
4669 */
4670 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004671invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004673 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 if ( eap->line1 < 0
4675 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004676 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004678
4679 if (eap->argt & RANGE)
4680 {
4681 switch(eap->addr_type)
4682 {
4683 case ADDR_LINES:
4684 if (!(eap->argt & NOTADR)
4685 && eap->line2 > curbuf->b_ml.ml_line_count
4686#ifdef FEAT_DIFF
4687 + (eap->cmdidx == CMD_diffget)
4688#endif
4689 )
4690 return (char_u *)_(e_invrange);
4691 break;
4692 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004693 /* add 1 if ARGCOUNT is 0 */
4694 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004695 return (char_u *)_(e_invrange);
4696 break;
4697 case ADDR_BUFFERS:
4698 if (eap->line1 < firstbuf->b_fnum
4699 || eap->line2 > lastbuf->b_fnum)
4700 return (char_u *)_(e_invrange);
4701 break;
4702 case ADDR_LOADED_BUFFERS:
4703 buf = firstbuf;
4704 while (buf->b_ml.ml_mfp == NULL)
4705 {
4706 if (buf->b_next == NULL)
4707 return (char_u *)_(e_invrange);
4708 buf = buf->b_next;
4709 }
4710 if (eap->line1 < buf->b_fnum)
4711 return (char_u *)_(e_invrange);
4712 buf = lastbuf;
4713 while (buf->b_ml.ml_mfp == NULL)
4714 {
4715 if (buf->b_prev == NULL)
4716 return (char_u *)_(e_invrange);
4717 buf = buf->b_prev;
4718 }
4719 if (eap->line2 > buf->b_fnum)
4720 return (char_u *)_(e_invrange);
4721 break;
4722 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004723 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004724 return (char_u *)_(e_invrange);
4725 break;
4726 case ADDR_TABS:
4727 if (eap->line2 > LAST_TAB_NR)
4728 return (char_u *)_(e_invrange);
4729 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004730#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004731 case ADDR_QUICKFIX:
4732 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4733 return (char_u *)_(e_invrange);
4734 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004735#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004736 }
4737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 return NULL;
4739}
4740
4741/*
4742 * Correct the range for zero line number, if required.
4743 */
4744 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004745correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746{
4747 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4748 {
4749 if (eap->line1 == 0)
4750 eap->line1 = 1;
4751 if (eap->line2 == 0)
4752 eap->line2 = 1;
4753 }
4754}
4755
Bram Moolenaar748bf032005-02-02 23:04:36 +00004756#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004757static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004758
4759/*
4760 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4761 * pattern. Otherwise return eap->arg.
4762 */
4763 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004764skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004765{
4766 char_u *p = eap->arg;
4767
Bram Moolenaara37420f2006-02-04 22:37:47 +00004768 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4769 || eap->cmdidx == CMD_vimgrepadd
4770 || eap->cmdidx == CMD_lvimgrepadd
4771 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004772 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004773 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004774 if (p == NULL)
4775 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004776 }
4777 return p;
4778}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004779
4780/*
4781 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4782 * in the command line, so that things like % get expanded.
4783 */
4784 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004785replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004786{
4787 char_u *new_cmdline;
4788 char_u *program;
4789 char_u *pos;
4790 char_u *ptr;
4791 int len;
4792 int i;
4793
4794 /*
4795 * Don't do it when ":vimgrep" is used for ":grep".
4796 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004797 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4798 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4799 || eap->cmdidx == CMD_grepadd
4800 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004801 && !grep_internal(eap->cmdidx))
4802 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004803 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4804 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004805 {
4806 if (*curbuf->b_p_gp == NUL)
4807 program = p_gp;
4808 else
4809 program = curbuf->b_p_gp;
4810 }
4811 else
4812 {
4813 if (*curbuf->b_p_mp == NUL)
4814 program = p_mp;
4815 else
4816 program = curbuf->b_p_mp;
4817 }
4818
4819 p = skipwhite(p);
4820
4821 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4822 {
4823 /* replace $* by given arguments */
4824 i = 1;
4825 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4826 ++i;
4827 len = (int)STRLEN(p);
4828 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4829 if (new_cmdline == NULL)
4830 return NULL; /* out of memory */
4831 ptr = new_cmdline;
4832 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4833 {
4834 i = (int)(pos - program);
4835 STRNCPY(ptr, program, i);
4836 STRCPY(ptr += i, p);
4837 ptr += len;
4838 program = pos + 2;
4839 }
4840 STRCPY(ptr, program);
4841 }
4842 else
4843 {
4844 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4845 if (new_cmdline == NULL)
4846 return NULL; /* out of memory */
4847 STRCPY(new_cmdline, program);
4848 STRCAT(new_cmdline, " ");
4849 STRCAT(new_cmdline, p);
4850 }
4851 msg_make(p);
4852
4853 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4854 vim_free(*cmdlinep);
4855 *cmdlinep = new_cmdline;
4856 p = new_cmdline;
4857 }
4858 return p;
4859}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004860#endif
4861
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862/*
4863 * Expand file name in Ex command argument.
4864 * Return FAIL for failure, OK otherwise.
4865 */
4866 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004867expand_filename(
4868 exarg_T *eap,
4869 char_u **cmdlinep,
4870 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871{
4872 int has_wildcards; /* need to expand wildcards */
4873 char_u *repl;
4874 int srclen;
4875 char_u *p;
4876 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004877 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878
Bram Moolenaar748bf032005-02-02 23:04:36 +00004879#ifdef FEAT_QUICKFIX
4880 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4881 p = skip_grep_pat(eap);
4882#else
4883 p = eap->arg;
4884#endif
4885
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 /*
4887 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4888 * the file name contains a wildcard it should not cause expanding.
4889 * (it will be expanded anyway if there is a wildcard before replacing).
4890 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004891 has_wildcards = mch_has_wildcard(p);
4892 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004894#ifdef FEAT_EVAL
4895 /* Skip over `=expr`, wildcards in it are not expanded. */
4896 if (p[0] == '`' && p[1] == '=')
4897 {
4898 p += 2;
4899 (void)skip_expr(&p);
4900 if (*p == '`')
4901 ++p;
4902 continue;
4903 }
4904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 /*
4906 * Quick check if this cannot be the start of a special string.
4907 * Also removes backslash before '%', '#' and '<'.
4908 */
4909 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4910 {
4911 ++p;
4912 continue;
4913 }
4914
4915 /*
4916 * Try to find a match at this position.
4917 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004918 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4919 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 if (*errormsgp != NULL) /* error detected */
4921 return FAIL;
4922 if (repl == NULL) /* no match found */
4923 {
4924 p += srclen;
4925 continue;
4926 }
4927
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004928 /* Wildcards won't be expanded below, the replacement is taken
4929 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4930 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4931 {
4932 char_u *l = repl;
4933
4934 repl = expand_env_save(repl);
4935 vim_free(l);
4936 }
4937
Bram Moolenaar63b92542007-03-27 14:57:09 +00004938 /* Need to escape white space et al. with a backslash.
4939 * Don't do this for:
4940 * - replacement that already has been escaped: "##"
4941 * - shell commands (may have to use quotes instead).
4942 * - non-unix systems when there is a single argument (spaces don't
4943 * separate arguments then).
4944 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004946 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 && eap->cmdidx != CMD_bang
4948 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004949 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004951 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004953 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954#ifndef UNIX
4955 && !(eap->argt & NOSPC)
4956#endif
4957 )
4958 {
4959 char_u *l;
4960#ifdef BACKSLASH_IN_FILENAME
4961 /* Don't escape a backslash here, because rem_backslash() doesn't
4962 * remove it later. */
4963 static char_u *nobslash = (char_u *)" \t\"|";
4964# define ESCAPE_CHARS nobslash
4965#else
4966# define ESCAPE_CHARS escape_chars
4967#endif
4968
4969 for (l = repl; *l; ++l)
4970 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4971 {
4972 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4973 if (l != NULL)
4974 {
4975 vim_free(repl);
4976 repl = l;
4977 }
4978 break;
4979 }
4980 }
4981
4982 /* For a shell command a '!' must be escaped. */
4983 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004984 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
4986 char_u *l;
4987
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004988 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 if (l != NULL)
4990 {
4991 vim_free(repl);
4992 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 }
4994 }
4995
4996 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4997 vim_free(repl);
4998 if (p == NULL)
4999 return FAIL;
5000 }
5001
5002 /*
5003 * One file argument: Expand wildcards.
5004 * Don't do this with ":r !command" or ":w !command".
5005 */
5006 if ((eap->argt & NOSPC) && !eap->usefilter)
5007 {
5008 /*
5009 * May do this twice:
5010 * 1. Replace environment variables.
5011 * 2. Replace any other wildcards, remove backslashes.
5012 */
5013 for (n = 1; n <= 2; ++n)
5014 {
5015 if (n == 2)
5016 {
5017#ifdef UNIX
5018 /*
5019 * Only for Unix we check for more than one file name.
5020 * For other systems spaces are considered to be part
5021 * of the file name.
5022 * Only check here if there is no wildcard, otherwise
5023 * ExpandOne() will check for errors. This allows
5024 * ":e `ls ve*.c`" on Unix.
5025 */
5026 if (!has_wildcards)
5027 for (p = eap->arg; *p; ++p)
5028 {
5029 /* skip escaped characters */
5030 if (p[1] && (*p == '\\' || *p == Ctrl_V))
5031 ++p;
5032 else if (vim_iswhite(*p))
5033 {
5034 *errormsgp = (char_u *)_("E172: Only one file name allowed");
5035 return FAIL;
5036 }
5037 }
5038#endif
5039
5040 /*
5041 * Halve the number of backslashes (this is Vi compatible).
5042 * For Unix and OS/2, when wildcards are expanded, this is
5043 * done by ExpandOne() below.
5044 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005045#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 if (!has_wildcards)
5047#endif
5048 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 }
5050
5051 if (has_wildcards)
5052 {
5053 if (n == 1)
5054 {
5055 /*
5056 * First loop: May expand environment variables. This
5057 * can be done much faster with expand_env() than with
5058 * something else (e.g., calling a shell).
5059 * After expanding environment variables, check again
5060 * if there are still wildcards present.
5061 */
5062 if (vim_strchr(eap->arg, '$') != NULL
5063 || vim_strchr(eap->arg, '~') != NULL)
5064 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005065 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005066 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 has_wildcards = mch_has_wildcard(NameBuff);
5068 p = NameBuff;
5069 }
5070 else
5071 p = NULL;
5072 }
5073 else /* n == 2 */
5074 {
5075 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005076 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077
5078 ExpandInit(&xpc);
5079 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005080 if (p_wic)
5081 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005083 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 if (p == NULL)
5085 return FAIL;
5086 }
5087 if (p != NULL)
5088 {
5089 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5090 p, cmdlinep);
5091 if (n == 2) /* p came from ExpandOne() */
5092 vim_free(p);
5093 }
5094 }
5095 }
5096 }
5097 return OK;
5098}
5099
5100/*
5101 * Replace part of the command line, keeping eap->cmd, eap->arg and
5102 * eap->nextcmd correct.
5103 * "src" points to the part that is to be replaced, of length "srclen".
5104 * "repl" is the replacement string.
5105 * Returns a pointer to the character after the replaced string.
5106 * Returns NULL for failure.
5107 */
5108 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005109repl_cmdline(
5110 exarg_T *eap,
5111 char_u *src,
5112 int srclen,
5113 char_u *repl,
5114 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115{
5116 int len;
5117 int i;
5118 char_u *new_cmdline;
5119
5120 /*
5121 * The new command line is build in new_cmdline[].
5122 * First allocate it.
5123 * Careful: a "+cmd" argument may have been NUL terminated.
5124 */
5125 len = (int)STRLEN(repl);
5126 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005127 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5129 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5130 return NULL; /* out of memory! */
5131
5132 /*
5133 * Copy the stuff before the expanded part.
5134 * Copy the expanded stuff.
5135 * Copy what came after the expanded part.
5136 * Copy the next commands, if there are any.
5137 */
5138 i = (int)(src - *cmdlinep); /* length of part before match */
5139 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005140
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 mch_memmove(new_cmdline + i, repl, (size_t)len);
5142 i += len; /* remember the end of the string */
5143 STRCPY(new_cmdline + i, src + srclen);
5144 src = new_cmdline + i; /* remember where to continue */
5145
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005146 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 {
5148 i = (int)STRLEN(new_cmdline) + 1;
5149 STRCPY(new_cmdline + i, eap->nextcmd);
5150 eap->nextcmd = new_cmdline + i;
5151 }
5152 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5153 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5154 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5155 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5156 vim_free(*cmdlinep);
5157 *cmdlinep = new_cmdline;
5158
5159 return src;
5160}
5161
5162/*
5163 * Check for '|' to separate commands and '"' to start comments.
5164 */
5165 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005166separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167{
5168 char_u *p;
5169
Bram Moolenaar86b68352004-12-27 21:59:20 +00005170#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005171 p = skip_grep_pat(eap);
5172#else
5173 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005174#endif
5175
5176 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 {
5178 if (*p == Ctrl_V)
5179 {
5180 if (eap->argt & (USECTRLV | XFILE))
5181 ++p; /* skip CTRL-V and next char */
5182 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005183 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005184 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 if (*p == NUL) /* stop at NUL after CTRL-V */
5186 break;
5187 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005188
5189#ifdef FEAT_EVAL
5190 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005191 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005192 {
5193 p += 2;
5194 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005195 }
5196#endif
5197
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 /* Check for '"': start of comment or '|': next command */
5199 /* :@" and :*" do not start a comment!
5200 * :redir @" doesn't either. */
5201 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5202 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5203 || p != eap->arg)
5204 && (eap->cmdidx != CMD_redir
5205 || p != eap->arg + 1 || p[-1] != '@'))
5206 || *p == '|' || *p == '\n')
5207 {
5208 /*
5209 * We remove the '\' before the '|', unless USECTRLV is used
5210 * AND 'b' is present in 'cpoptions'.
5211 */
5212 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5213 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5214 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005215 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 --p;
5217 }
5218 else
5219 {
5220 eap->nextcmd = check_nextcmd(p);
5221 *p = NUL;
5222 break;
5223 }
5224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005226
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5228 del_trailing_spaces(eap->arg);
5229}
5230
5231/*
5232 * get + command from ex argument
5233 */
5234 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005235getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236{
5237 char_u *arg = *argp;
5238 char_u *command = NULL;
5239
5240 if (*arg == '+') /* +[command] */
5241 {
5242 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005243 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 command = dollar_command;
5245 else
5246 {
5247 command = arg;
5248 arg = skip_cmd_arg(command, TRUE);
5249 if (*arg != NUL)
5250 *arg++ = NUL; /* terminate command with NUL */
5251 }
5252
5253 arg = skipwhite(arg); /* skip over spaces */
5254 *argp = arg;
5255 }
5256 return command;
5257}
5258
5259/*
5260 * Find end of "+command" argument. Skip over "\ " and "\\".
5261 */
5262 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005263skip_cmd_arg(
5264 char_u *p,
5265 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266{
5267 while (*p && !vim_isspace(*p))
5268 {
5269 if (*p == '\\' && p[1] != NUL)
5270 {
5271 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005272 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 else
5274 ++p;
5275 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005276 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 }
5278 return p;
5279}
5280
5281/*
5282 * Get "++opt=arg" argument.
5283 * Return FAIL or OK.
5284 */
5285 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005286getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287{
5288 char_u *arg = eap->arg + 2;
5289 int *pp = NULL;
5290#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005291 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 char_u *p;
5293#endif
5294
5295 /* ":edit ++[no]bin[ary] file" */
5296 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5297 {
5298 if (*arg == 'n')
5299 {
5300 arg += 2;
5301 eap->force_bin = FORCE_NOBIN;
5302 }
5303 else
5304 eap->force_bin = FORCE_BIN;
5305 if (!checkforcmd(&arg, "binary", 3))
5306 return FAIL;
5307 eap->arg = skipwhite(arg);
5308 return OK;
5309 }
5310
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005311 /* ":read ++edit file" */
5312 if (STRNCMP(arg, "edit", 4) == 0)
5313 {
5314 eap->read_edit = TRUE;
5315 eap->arg = skipwhite(arg + 4);
5316 return OK;
5317 }
5318
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 if (STRNCMP(arg, "ff", 2) == 0)
5320 {
5321 arg += 2;
5322 pp = &eap->force_ff;
5323 }
5324 else if (STRNCMP(arg, "fileformat", 10) == 0)
5325 {
5326 arg += 10;
5327 pp = &eap->force_ff;
5328 }
5329#ifdef FEAT_MBYTE
5330 else if (STRNCMP(arg, "enc", 3) == 0)
5331 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005332 if (STRNCMP(arg, "encoding", 8) == 0)
5333 arg += 8;
5334 else
5335 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 pp = &eap->force_enc;
5337 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005338 else if (STRNCMP(arg, "bad", 3) == 0)
5339 {
5340 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005341 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343#endif
5344
5345 if (pp == NULL || *arg != '=')
5346 return FAIL;
5347
5348 ++arg;
5349 *pp = (int)(arg - eap->cmd);
5350 arg = skip_cmd_arg(arg, FALSE);
5351 eap->arg = skipwhite(arg);
5352 *arg = NUL;
5353
5354#ifdef FEAT_MBYTE
5355 if (pp == &eap->force_ff)
5356 {
5357#endif
5358 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5359 return FAIL;
5360#ifdef FEAT_MBYTE
5361 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005362 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 {
5364 /* Make 'fileencoding' lower case. */
5365 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5366 *p = TOLOWER_ASC(*p);
5367 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005368 else
5369 {
5370 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5371 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005372 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005373 if (STRICMP(p, "keep") == 0)
5374 eap->bad_char = BAD_KEEP;
5375 else if (STRICMP(p, "drop") == 0)
5376 eap->bad_char = BAD_DROP;
5377 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5378 eap->bad_char = *p;
5379 else
5380 return FAIL;
5381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382#endif
5383
5384 return OK;
5385}
5386
5387/*
5388 * ":abbreviate" and friends.
5389 */
5390 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005391ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392{
5393 do_exmap(eap, TRUE); /* almost the same as mapping */
5394}
5395
5396/*
5397 * ":map" and friends.
5398 */
5399 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005400ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401{
5402 /*
5403 * If we are sourcing .exrc or .vimrc in current directory we
5404 * print the mappings for security reasons.
5405 */
5406 if (secure)
5407 {
5408 secure = 2;
5409 msg_outtrans(eap->cmd);
5410 msg_putchar('\n');
5411 }
5412 do_exmap(eap, FALSE);
5413}
5414
5415/*
5416 * ":unmap" and friends.
5417 */
5418 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005419ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420{
5421 do_exmap(eap, FALSE);
5422}
5423
5424/*
5425 * ":mapclear" and friends.
5426 */
5427 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005428ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429{
5430 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5431}
5432
5433/*
5434 * ":abclear" and friends.
5435 */
5436 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005437ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438{
5439 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5440}
5441
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005442#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005444ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445{
5446 /*
5447 * Disallow auto commands from .exrc and .vimrc in current
5448 * directory for security reasons.
5449 */
5450 if (secure)
5451 {
5452 secure = 2;
5453 eap->errmsg = e_curdir;
5454 }
5455 else if (eap->cmdidx == CMD_autocmd)
5456 do_autocmd(eap->arg, eap->forceit);
5457 else
5458 do_augroup(eap->arg, eap->forceit);
5459}
5460
5461/*
5462 * ":doautocmd": Apply the automatic commands to the current buffer.
5463 */
5464 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005465ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005467 char_u *arg = eap->arg;
5468 int call_do_modelines = check_nomodeline(&arg);
5469
5470 (void)do_doautocmd(arg, TRUE);
5471 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5472 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473}
5474#endif
5475
5476#ifdef FEAT_LISTCMDS
5477/*
5478 * :[N]bunload[!] [N] [bufname] unload buffer
5479 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5480 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5481 */
5482 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005483ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484{
5485 eap->errmsg = do_bufdel(
5486 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5487 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5488 : DOBUF_UNLOAD, eap->arg,
5489 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5490}
5491
5492/*
5493 * :[N]buffer [N] to buffer N
5494 * :[N]sbuffer [N] to buffer N
5495 */
5496 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005497ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498{
5499 if (*eap->arg)
5500 eap->errmsg = e_trailing;
5501 else
5502 {
5503 if (eap->addr_count == 0) /* default is current buffer */
5504 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5505 else
5506 goto_buffer(eap, DOBUF_FIRST, 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/*
5513 * :[N]bmodified [N] to next mod. buffer
5514 * :[N]sbmodified [N] to next mod. buffer
5515 */
5516 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005517ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518{
5519 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005520 if (eap->do_ecmd_cmd != NULL)
5521 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522}
5523
5524/*
5525 * :[N]bnext [N] to next buffer
5526 * :[N]sbnext [N] split and to next buffer
5527 */
5528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005529ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530{
5531 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005532 if (eap->do_ecmd_cmd != NULL)
5533 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534}
5535
5536/*
5537 * :[N]bNext [N] to previous buffer
5538 * :[N]bprevious [N] to previous buffer
5539 * :[N]sbNext [N] split and to previous buffer
5540 * :[N]sbprevious [N] split and to previous buffer
5541 */
5542 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005543ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544{
5545 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005546 if (eap->do_ecmd_cmd != NULL)
5547 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548}
5549
5550/*
5551 * :brewind to first buffer
5552 * :bfirst to first buffer
5553 * :sbrewind split and to first buffer
5554 * :sbfirst split and to first buffer
5555 */
5556 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005557ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558{
5559 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005560 if (eap->do_ecmd_cmd != NULL)
5561 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562}
5563
5564/*
5565 * :blast to last buffer
5566 * :sblast split and to last buffer
5567 */
5568 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005569ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570{
5571 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005572 if (eap->do_ecmd_cmd != NULL)
5573 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574}
5575#endif
5576
5577 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005578ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579{
5580 return (c == NUL || c == '|' || c == '"' || c == '\n');
5581}
5582
5583#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5584 || defined(PROTO)
5585/*
5586 * Return the next command, after the first '|' or '\n'.
5587 * Return NULL if not found.
5588 */
5589 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005590find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591{
5592 while (*p != '|' && *p != '\n')
5593 {
5594 if (*p == NUL)
5595 return NULL;
5596 ++p;
5597 }
5598 return (p + 1);
5599}
5600#endif
5601
5602/*
5603 * Check if *p is a separator between Ex commands.
5604 * Return NULL if it isn't, (p + 1) if it is.
5605 */
5606 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005607check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608{
5609 p = skipwhite(p);
5610 if (*p == '|' || *p == '\n')
5611 return (p + 1);
5612 else
5613 return NULL;
5614}
5615
5616/*
5617 * - if there are more files to edit
5618 * - and this is the last window
5619 * - and forceit not used
5620 * - and not repeated twice on a row
5621 * return FAIL and give error message if 'message' TRUE
5622 * return OK otherwise
5623 */
5624 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005625check_more(
5626 int message, /* when FALSE check only, no messages */
5627 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628{
5629 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5630
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005631 if (!forceit && only_one_window()
5632 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 {
5634 if (message)
5635 {
5636#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5637 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5638 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005639 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640
5641 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005642 vim_strncpy(buff,
5643 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005644 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005646 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 _("%d more files to edit. Quit anyway?"), n);
5648 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5649 return OK;
5650 return FAIL;
5651 }
5652#endif
5653 if (n == 1)
5654 EMSG(_("E173: 1 more file to edit"));
5655 else
5656 EMSGN(_("E173: %ld more files to edit"), n);
5657 quitmore = 2; /* next try to quit is allowed */
5658 }
5659 return FAIL;
5660 }
5661 return OK;
5662}
5663
5664#ifdef FEAT_CMDL_COMPL
5665/*
5666 * Function given to ExpandGeneric() to obtain the list of command names.
5667 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005669get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670{
5671 if (idx >= (int)CMD_SIZE)
5672# ifdef FEAT_USR_CMDS
5673 return get_user_command_name(idx);
5674# else
5675 return NULL;
5676# endif
5677 return cmdnames[idx].cmd_name;
5678}
5679#endif
5680
5681#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005682static 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);
5683static void uc_list(char_u *name, size_t name_len);
5684static 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);
5685static char_u *uc_split_args(char_u *arg, size_t *lenp);
5686static 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 +00005687
5688 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005689uc_add_command(
5690 char_u *name,
5691 size_t name_len,
5692 char_u *rep,
5693 long argt,
5694 long def,
5695 int flags,
5696 int compl,
5697 char_u *compl_arg,
5698 int addr_type,
5699 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700{
5701 ucmd_T *cmd = NULL;
5702 char_u *p;
5703 int i;
5704 int cmp = 1;
5705 char_u *rep_buf = NULL;
5706 garray_T *gap;
5707
Bram Moolenaar9c102382006-05-03 21:26:49 +00005708 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 if (rep_buf == NULL)
5710 {
5711 /* Can't replace termcodes - try using the string as is */
5712 rep_buf = vim_strsave(rep);
5713
5714 /* Give up if out of memory */
5715 if (rep_buf == NULL)
5716 return FAIL;
5717 }
5718
5719 /* get address of growarray: global or in curbuf */
5720 if (flags & UC_BUFFER)
5721 {
5722 gap = &curbuf->b_ucmds;
5723 if (gap->ga_itemsize == 0)
5724 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5725 }
5726 else
5727 gap = &ucmds;
5728
5729 /* Search for the command in the already defined commands. */
5730 for (i = 0; i < gap->ga_len; ++i)
5731 {
5732 size_t len;
5733
5734 cmd = USER_CMD_GA(gap, i);
5735 len = STRLEN(cmd->uc_name);
5736 cmp = STRNCMP(name, cmd->uc_name, name_len);
5737 if (cmp == 0)
5738 {
5739 if (name_len < len)
5740 cmp = -1;
5741 else if (name_len > len)
5742 cmp = 1;
5743 }
5744
5745 if (cmp == 0)
5746 {
5747 if (!force)
5748 {
5749 EMSG(_("E174: Command already exists: add ! to replace it"));
5750 goto fail;
5751 }
5752
5753 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005754 cmd->uc_rep = NULL;
5755#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5756 vim_free(cmd->uc_compl_arg);
5757 cmd->uc_compl_arg = NULL;
5758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 break;
5760 }
5761
5762 /* Stop as soon as we pass the name to add */
5763 if (cmp < 0)
5764 break;
5765 }
5766
5767 /* Extend the array unless we're replacing an existing command */
5768 if (cmp != 0)
5769 {
5770 if (ga_grow(gap, 1) != OK)
5771 goto fail;
5772 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5773 goto fail;
5774
5775 cmd = USER_CMD_GA(gap, i);
5776 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5777
5778 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779
5780 cmd->uc_name = p;
5781 }
5782
5783 cmd->uc_rep = rep_buf;
5784 cmd->uc_argt = argt;
5785 cmd->uc_def = def;
5786 cmd->uc_compl = compl;
5787#ifdef FEAT_EVAL
5788 cmd->uc_scriptID = current_SID;
5789# ifdef FEAT_CMDL_COMPL
5790 cmd->uc_compl_arg = compl_arg;
5791# endif
5792#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005793 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794
5795 return OK;
5796
5797fail:
5798 vim_free(rep_buf);
5799#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5800 vim_free(compl_arg);
5801#endif
5802 return FAIL;
5803}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005806#if defined(FEAT_USR_CMDS)
5807static struct
5808{
5809 int expand;
5810 char *name;
5811} addr_type_complete[] =
5812{
5813 {ADDR_ARGUMENTS, "arguments"},
5814 {ADDR_LINES, "lines"},
5815 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5816 {ADDR_TABS, "tabs"},
5817 {ADDR_BUFFERS, "buffers"},
5818 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005819 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005820 {-1, NULL}
5821};
5822#endif
5823
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005824#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825/*
5826 * List of names for completion for ":command" with the EXPAND_ flag.
5827 * Must be alphabetical for completion.
5828 */
5829static struct
5830{
5831 int expand;
5832 char *name;
5833} command_complete[] =
5834{
5835 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005836 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005838 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005840 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005841#if defined(FEAT_CSCOPE)
5842 {EXPAND_CSCOPE, "cscope"},
5843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5845 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005846 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847#endif
5848 {EXPAND_DIRECTORIES, "dir"},
5849 {EXPAND_ENV_VARS, "environment"},
5850 {EXPAND_EVENTS, "event"},
5851 {EXPAND_EXPRESSION, "expression"},
5852 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005853 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005854 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 {EXPAND_FUNCTIONS, "function"},
5856 {EXPAND_HELP, "help"},
5857 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005858#if defined(FEAT_CMDHIST)
5859 {EXPAND_HISTORY, "history"},
5860#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005861#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005862 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005863 {EXPAND_LOCALES, "locale"},
5864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 {EXPAND_MAPPINGS, "mapping"},
5866 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005867 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005868#if defined(FEAT_PROFILE)
5869 {EXPAND_SYNTIME, "syntime"},
5870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005872 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005873#if defined(FEAT_SIGNS)
5874 {EXPAND_SIGN, "sign"},
5875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 {EXPAND_TAGS, "tag"},
5877 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005878 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 {EXPAND_USER_VARS, "var"},
5880 {0, NULL}
5881};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005884#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005886uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887{
5888 int i, j;
5889 int found = FALSE;
5890 ucmd_T *cmd;
5891 int len;
5892 long a;
5893 garray_T *gap;
5894
5895 gap = &curbuf->b_ucmds;
5896 for (;;)
5897 {
5898 for (i = 0; i < gap->ga_len; ++i)
5899 {
5900 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005901 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902
5903 /* Skip commands which don't match the requested prefix */
5904 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5905 continue;
5906
5907 /* Put out the title first time */
5908 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005909 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 found = TRUE;
5911 msg_putchar('\n');
5912 if (got_int)
5913 break;
5914
5915 /* Special cases */
5916 msg_putchar(a & BANG ? '!' : ' ');
5917 msg_putchar(a & REGSTR ? '"' : ' ');
5918 msg_putchar(gap != &ucmds ? 'b' : ' ');
5919 msg_putchar(' ');
5920
5921 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5922 len = (int)STRLEN(cmd->uc_name) + 4;
5923
5924 do {
5925 msg_putchar(' ');
5926 ++len;
5927 } while (len < 16);
5928
5929 len = 0;
5930
5931 /* Arguments */
5932 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5933 {
5934 case 0: IObuff[len++] = '0'; break;
5935 case (EXTRA): IObuff[len++] = '*'; break;
5936 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5937 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5938 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5939 }
5940
5941 do {
5942 IObuff[len++] = ' ';
5943 } while (len < 5);
5944
5945 /* Range */
5946 if (a & (RANGE|COUNT))
5947 {
5948 if (a & COUNT)
5949 {
5950 /* -count=N */
5951 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5952 len += (int)STRLEN(IObuff + len);
5953 }
5954 else if (a & DFLALL)
5955 IObuff[len++] = '%';
5956 else if (cmd->uc_def >= 0)
5957 {
5958 /* -range=N */
5959 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5960 len += (int)STRLEN(IObuff + len);
5961 }
5962 else
5963 IObuff[len++] = '.';
5964 }
5965
5966 do {
5967 IObuff[len++] = ' ';
5968 } while (len < 11);
5969
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005970 /* Address Type */
5971 for (j = 0; addr_type_complete[j].expand != -1; ++j)
5972 if (addr_type_complete[j].expand != ADDR_LINES
5973 && addr_type_complete[j].expand == cmd->uc_addr_type)
5974 {
5975 STRCPY(IObuff + len, addr_type_complete[j].name);
5976 len += (int)STRLEN(IObuff + len);
5977 break;
5978 }
5979
5980 do {
5981 IObuff[len++] = ' ';
5982 } while (len < 21);
5983
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 /* Completion */
5985 for (j = 0; command_complete[j].expand != 0; ++j)
5986 if (command_complete[j].expand == cmd->uc_compl)
5987 {
5988 STRCPY(IObuff + len, command_complete[j].name);
5989 len += (int)STRLEN(IObuff + len);
5990 break;
5991 }
5992
5993 do {
5994 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005995 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996
5997 IObuff[len] = '\0';
5998 msg_outtrans(IObuff);
5999
6000 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006001#ifdef FEAT_EVAL
6002 if (p_verbose > 0)
6003 last_set_msg(cmd->uc_scriptID);
6004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 out_flush();
6006 ui_breakcheck();
6007 if (got_int)
6008 break;
6009 }
6010 if (gap == &ucmds || i < gap->ga_len)
6011 break;
6012 gap = &ucmds;
6013 }
6014
6015 if (!found)
6016 MSG(_("No user-defined commands found"));
6017}
6018
6019 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006020uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021{
6022 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6023 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6024 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6025 0xb9, 0x7f, 0};
6026 int i;
6027
6028 for (i = 0; fcmd[i]; ++i)
6029 IObuff[i] = fcmd[i] - 0x40;
6030 IObuff[i] = 0;
6031 return IObuff;
6032}
6033
6034 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006035uc_scan_attr(
6036 char_u *attr,
6037 size_t len,
6038 long *argt,
6039 long *def,
6040 int *flags,
6041 int *compl,
6042 char_u **compl_arg,
6043 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044{
6045 char_u *p;
6046
6047 if (len == 0)
6048 {
6049 EMSG(_("E175: No attribute specified"));
6050 return FAIL;
6051 }
6052
6053 /* First, try the simple attributes (no arguments) */
6054 if (STRNICMP(attr, "bang", len) == 0)
6055 *argt |= BANG;
6056 else if (STRNICMP(attr, "buffer", len) == 0)
6057 *flags |= UC_BUFFER;
6058 else if (STRNICMP(attr, "register", len) == 0)
6059 *argt |= REGSTR;
6060 else if (STRNICMP(attr, "bar", len) == 0)
6061 *argt |= TRLBAR;
6062 else
6063 {
6064 int i;
6065 char_u *val = NULL;
6066 size_t vallen = 0;
6067 size_t attrlen = len;
6068
6069 /* Look for the attribute name - which is the part before any '=' */
6070 for (i = 0; i < (int)len; ++i)
6071 {
6072 if (attr[i] == '=')
6073 {
6074 val = &attr[i + 1];
6075 vallen = len - i - 1;
6076 attrlen = i;
6077 break;
6078 }
6079 }
6080
6081 if (STRNICMP(attr, "nargs", attrlen) == 0)
6082 {
6083 if (vallen == 1)
6084 {
6085 if (*val == '0')
6086 /* Do nothing - this is the default */;
6087 else if (*val == '1')
6088 *argt |= (EXTRA | NOSPC | NEEDARG);
6089 else if (*val == '*')
6090 *argt |= EXTRA;
6091 else if (*val == '?')
6092 *argt |= (EXTRA | NOSPC);
6093 else if (*val == '+')
6094 *argt |= (EXTRA | NEEDARG);
6095 else
6096 goto wrong_nargs;
6097 }
6098 else
6099 {
6100wrong_nargs:
6101 EMSG(_("E176: Invalid number of arguments"));
6102 return FAIL;
6103 }
6104 }
6105 else if (STRNICMP(attr, "range", attrlen) == 0)
6106 {
6107 *argt |= RANGE;
6108 if (vallen == 1 && *val == '%')
6109 *argt |= DFLALL;
6110 else if (val != NULL)
6111 {
6112 p = val;
6113 if (*def >= 0)
6114 {
6115two_count:
6116 EMSG(_("E177: Count cannot be specified twice"));
6117 return FAIL;
6118 }
6119
6120 *def = getdigits(&p);
6121 *argt |= (ZEROR | NOTADR);
6122
6123 if (p != val + vallen || vallen == 0)
6124 {
6125invalid_count:
6126 EMSG(_("E178: Invalid default value for count"));
6127 return FAIL;
6128 }
6129 }
6130 }
6131 else if (STRNICMP(attr, "count", attrlen) == 0)
6132 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006133 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134
6135 if (val != NULL)
6136 {
6137 p = val;
6138 if (*def >= 0)
6139 goto two_count;
6140
6141 *def = getdigits(&p);
6142
6143 if (p != val + vallen)
6144 goto invalid_count;
6145 }
6146
6147 if (*def < 0)
6148 *def = 0;
6149 }
6150 else if (STRNICMP(attr, "complete", attrlen) == 0)
6151 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 if (val == NULL)
6153 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006154 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 return FAIL;
6156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006158 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6159 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006162 else if (STRNICMP(attr, "addr", attrlen) == 0)
6163 {
6164 *argt |= RANGE;
6165 if (val == NULL)
6166 {
6167 EMSG(_("E179: argument required for -addr"));
6168 return FAIL;
6169 }
6170 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6171 == FAIL)
6172 return FAIL;
6173 if (addr_type_arg != ADDR_LINES)
6174 *argt |= (ZEROR | NOTADR) ;
6175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 else
6177 {
6178 char_u ch = attr[len];
6179 attr[len] = '\0';
6180 EMSG2(_("E181: Invalid attribute: %s"), attr);
6181 attr[len] = ch;
6182 return FAIL;
6183 }
6184 }
6185
6186 return OK;
6187}
6188
Bram Moolenaara850a712009-01-28 14:42:59 +00006189/*
6190 * ":command ..."
6191 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006193ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194{
6195 char_u *name;
6196 char_u *end;
6197 char_u *p;
6198 long argt = 0;
6199 long def = -1;
6200 int flags = 0;
6201 int compl = EXPAND_NOTHING;
6202 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006203 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006205 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206
6207 p = eap->arg;
6208
6209 /* Check for attributes */
6210 while (*p == '-')
6211 {
6212 ++p;
6213 end = skiptowhite(p);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006214 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 == FAIL)
6216 return;
6217 p = skipwhite(end);
6218 }
6219
6220 /* Get the name (if any) and skip to the following argument */
6221 name = p;
6222 if (ASCII_ISALPHA(*p))
6223 while (ASCII_ISALNUM(*p))
6224 ++p;
6225 if (!ends_excmd(*p) && !vim_iswhite(*p))
6226 {
6227 EMSG(_("E182: Invalid command name"));
6228 return;
6229 }
6230 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006231 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232
6233 /* If there is nothing after the name, and no attributes were specified,
6234 * we are listing commands
6235 */
6236 p = skipwhite(end);
6237 if (!has_attr && ends_excmd(*p))
6238 {
6239 uc_list(name, end - name);
6240 }
6241 else if (!ASCII_ISUPPER(*name))
6242 {
6243 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6244 return;
6245 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006246 else if ((name_len == 1 && *name == 'X')
6247 || (name_len <= 4
6248 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6249 {
6250 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6251 return;
6252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 else
6254 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006255 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256}
6257
6258/*
6259 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 * Clear all user commands, global and for current buffer.
6261 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006262 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006263ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264{
6265 uc_clear(&ucmds);
6266 uc_clear(&curbuf->b_ucmds);
6267}
6268
6269/*
6270 * Clear all user commands for "gap".
6271 */
6272 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006273uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274{
6275 int i;
6276 ucmd_T *cmd;
6277
6278 for (i = 0; i < gap->ga_len; ++i)
6279 {
6280 cmd = USER_CMD_GA(gap, i);
6281 vim_free(cmd->uc_name);
6282 vim_free(cmd->uc_rep);
6283# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6284 vim_free(cmd->uc_compl_arg);
6285# endif
6286 }
6287 ga_clear(gap);
6288}
6289
6290 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006291ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292{
6293 int i = 0;
6294 ucmd_T *cmd = NULL;
6295 int cmp = -1;
6296 garray_T *gap;
6297
6298 gap = &curbuf->b_ucmds;
6299 for (;;)
6300 {
6301 for (i = 0; i < gap->ga_len; ++i)
6302 {
6303 cmd = USER_CMD_GA(gap, i);
6304 cmp = STRCMP(eap->arg, cmd->uc_name);
6305 if (cmp <= 0)
6306 break;
6307 }
6308 if (gap == &ucmds || cmp == 0)
6309 break;
6310 gap = &ucmds;
6311 }
6312
6313 if (cmp != 0)
6314 {
6315 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6316 return;
6317 }
6318
6319 vim_free(cmd->uc_name);
6320 vim_free(cmd->uc_rep);
6321# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6322 vim_free(cmd->uc_compl_arg);
6323# endif
6324
6325 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326
6327 if (i < gap->ga_len)
6328 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6329}
6330
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006331/*
6332 * split and quote args for <f-args>
6333 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006335uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336{
6337 char_u *buf;
6338 char_u *p;
6339 char_u *q;
6340 int len;
6341
6342 /* Precalculate length */
6343 p = arg;
6344 len = 2; /* Initial and final quotes */
6345
6346 while (*p)
6347 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006348 if (p[0] == '\\' && p[1] == '\\')
6349 {
6350 len += 2;
6351 p += 2;
6352 }
6353 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 {
6355 len += 1;
6356 p += 2;
6357 }
6358 else if (*p == '\\' || *p == '"')
6359 {
6360 len += 2;
6361 p += 1;
6362 }
6363 else if (vim_iswhite(*p))
6364 {
6365 p = skipwhite(p);
6366 if (*p == NUL)
6367 break;
6368 len += 3; /* "," */
6369 }
6370 else
6371 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006372#ifdef FEAT_MBYTE
6373 int charlen = (*mb_ptr2len)(p);
6374 len += charlen;
6375 p += charlen;
6376#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 ++len;
6378 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 }
6381 }
6382
6383 buf = alloc(len + 1);
6384 if (buf == NULL)
6385 {
6386 *lenp = 0;
6387 return buf;
6388 }
6389
6390 p = arg;
6391 q = buf;
6392 *q++ = '"';
6393 while (*p)
6394 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006395 if (p[0] == '\\' && p[1] == '\\')
6396 {
6397 *q++ = '\\';
6398 *q++ = '\\';
6399 p += 2;
6400 }
6401 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 {
6403 *q++ = p[1];
6404 p += 2;
6405 }
6406 else if (*p == '\\' || *p == '"')
6407 {
6408 *q++ = '\\';
6409 *q++ = *p++;
6410 }
6411 else if (vim_iswhite(*p))
6412 {
6413 p = skipwhite(p);
6414 if (*p == NUL)
6415 break;
6416 *q++ = '"';
6417 *q++ = ',';
6418 *q++ = '"';
6419 }
6420 else
6421 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006422 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 }
6424 }
6425 *q++ = '"';
6426 *q = 0;
6427
6428 *lenp = len;
6429 return buf;
6430}
6431
6432/*
6433 * Check for a <> code in a user command.
6434 * "code" points to the '<'. "len" the length of the <> (inclusive).
6435 * "buf" is where the result is to be added.
6436 * "split_buf" points to a buffer used for splitting, caller should free it.
6437 * "split_len" is the length of what "split_buf" contains.
6438 * Returns the length of the replacement, which has been added to "buf".
6439 * Returns -1 if there was no match, and only the "<" has been copied.
6440 */
6441 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006442uc_check_code(
6443 char_u *code,
6444 size_t len,
6445 char_u *buf,
6446 ucmd_T *cmd, /* the user command we're expanding */
6447 exarg_T *eap, /* ex arguments */
6448 char_u **split_buf,
6449 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450{
6451 size_t result = 0;
6452 char_u *p = code + 1;
6453 size_t l = len - 2;
6454 int quote = 0;
6455 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
6456 ct_LT, ct_NONE } type = ct_NONE;
6457
6458 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6459 {
6460 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6461 p += 2;
6462 l -= 2;
6463 }
6464
Bram Moolenaar371d5402006-03-20 21:47:49 +00006465 ++l;
6466 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006468 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006470 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006472 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006474 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006476 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006478 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006480 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 type = ct_REGISTER;
6482
6483 switch (type)
6484 {
6485 case ct_ARGS:
6486 /* Simple case first */
6487 if (*eap->arg == NUL)
6488 {
6489 if (quote == 1)
6490 {
6491 result = 2;
6492 if (buf != NULL)
6493 STRCPY(buf, "''");
6494 }
6495 else
6496 result = 0;
6497 break;
6498 }
6499
6500 /* When specified there is a single argument don't split it.
6501 * Works for ":Cmd %" when % is "a b c". */
6502 if ((eap->argt & NOSPC) && quote == 2)
6503 quote = 1;
6504
6505 switch (quote)
6506 {
6507 case 0: /* No quoting, no splitting */
6508 result = STRLEN(eap->arg);
6509 if (buf != NULL)
6510 STRCPY(buf, eap->arg);
6511 break;
6512 case 1: /* Quote, but don't split */
6513 result = STRLEN(eap->arg) + 2;
6514 for (p = eap->arg; *p; ++p)
6515 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006516#ifdef FEAT_MBYTE
6517 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6518 /* DBCS can contain \ in a trail byte, skip the
6519 * double-byte character. */
6520 ++p;
6521 else
6522#endif
6523 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 ++result;
6525 }
6526
6527 if (buf != NULL)
6528 {
6529 *buf++ = '"';
6530 for (p = eap->arg; *p; ++p)
6531 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006532#ifdef FEAT_MBYTE
6533 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6534 /* DBCS can contain \ in a trail byte, copy the
6535 * double-byte character to avoid escaping. */
6536 *buf++ = *p++;
6537 else
6538#endif
6539 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 *buf++ = '\\';
6541 *buf++ = *p;
6542 }
6543 *buf = '"';
6544 }
6545
6546 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006547 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 /* This is hard, so only do it once, and cache the result */
6549 if (*split_buf == NULL)
6550 *split_buf = uc_split_args(eap->arg, split_len);
6551
6552 result = *split_len;
6553 if (buf != NULL && result != 0)
6554 STRCPY(buf, *split_buf);
6555
6556 break;
6557 }
6558 break;
6559
6560 case ct_BANG:
6561 result = eap->forceit ? 1 : 0;
6562 if (quote)
6563 result += 2;
6564 if (buf != NULL)
6565 {
6566 if (quote)
6567 *buf++ = '"';
6568 if (eap->forceit)
6569 *buf++ = '!';
6570 if (quote)
6571 *buf = '"';
6572 }
6573 break;
6574
6575 case ct_LINE1:
6576 case ct_LINE2:
6577 case ct_COUNT:
6578 {
6579 char num_buf[20];
6580 long num = (type == ct_LINE1) ? eap->line1 :
6581 (type == ct_LINE2) ? eap->line2 :
6582 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6583 size_t num_len;
6584
6585 sprintf(num_buf, "%ld", num);
6586 num_len = STRLEN(num_buf);
6587 result = num_len;
6588
6589 if (quote)
6590 result += 2;
6591
6592 if (buf != NULL)
6593 {
6594 if (quote)
6595 *buf++ = '"';
6596 STRCPY(buf, num_buf);
6597 buf += num_len;
6598 if (quote)
6599 *buf = '"';
6600 }
6601
6602 break;
6603 }
6604
6605 case ct_REGISTER:
6606 result = eap->regname ? 1 : 0;
6607 if (quote)
6608 result += 2;
6609 if (buf != NULL)
6610 {
6611 if (quote)
6612 *buf++ = '\'';
6613 if (eap->regname)
6614 *buf++ = eap->regname;
6615 if (quote)
6616 *buf = '\'';
6617 }
6618 break;
6619
6620 case ct_LT:
6621 result = 1;
6622 if (buf != NULL)
6623 *buf = '<';
6624 break;
6625
6626 default:
6627 /* Not recognized: just copy the '<' and return -1. */
6628 result = (size_t)-1;
6629 if (buf != NULL)
6630 *buf = '<';
6631 break;
6632 }
6633
6634 return result;
6635}
6636
6637 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006638do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639{
6640 char_u *buf;
6641 char_u *p;
6642 char_u *q;
6643
6644 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006645 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006646 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 size_t len, totlen;
6648
6649 size_t split_len = 0;
6650 char_u *split_buf = NULL;
6651 ucmd_T *cmd;
6652#ifdef FEAT_EVAL
6653 scid_T save_current_SID = current_SID;
6654#endif
6655
6656 if (eap->cmdidx == CMD_USER)
6657 cmd = USER_CMD(eap->useridx);
6658 else
6659 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6660
6661 /*
6662 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006663 * First round: "buf" is NULL, compute length, allocate "buf".
6664 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 */
6666 buf = NULL;
6667 for (;;)
6668 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006669 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006670 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006672
6673 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006675 start = vim_strchr(p, '<');
6676 if (start != NULL)
6677 end = vim_strchr(start + 1, '>');
6678 if (buf != NULL)
6679 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006680 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6681 ;
6682 if (*ksp == K_SPECIAL
6683 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006684 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6685# ifdef FEAT_GUI
6686 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6687# endif
6688 ))
6689 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006690 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006691 * KS_SPECIAL KE_FILLER, like for mappings, but
6692 * do_cmdline() doesn't handle that, so convert it back.
6693 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6694 len = ksp - p;
6695 if (len > 0)
6696 {
6697 mch_memmove(q, p, len);
6698 q += len;
6699 }
6700 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6701 p = ksp + 3;
6702 continue;
6703 }
6704 }
6705
6706 /* break if there no <item> is found */
6707 if (start == NULL || end == NULL)
6708 break;
6709
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 /* Include the '>' */
6711 ++end;
6712
6713 /* Take everything up to the '<' */
6714 len = start - p;
6715 if (buf == NULL)
6716 totlen += len;
6717 else
6718 {
6719 mch_memmove(q, p, len);
6720 q += len;
6721 }
6722
6723 len = uc_check_code(start, end - start, q, cmd, eap,
6724 &split_buf, &split_len);
6725 if (len == (size_t)-1)
6726 {
6727 /* no match, continue after '<' */
6728 p = start + 1;
6729 len = 1;
6730 }
6731 else
6732 p = end;
6733 if (buf == NULL)
6734 totlen += len;
6735 else
6736 q += len;
6737 }
6738 if (buf != NULL) /* second time here, finished */
6739 {
6740 STRCPY(q, p);
6741 break;
6742 }
6743
6744 totlen += STRLEN(p); /* Add on the trailing characters */
6745 buf = alloc((unsigned)(totlen + 1));
6746 if (buf == NULL)
6747 {
6748 vim_free(split_buf);
6749 return;
6750 }
6751 }
6752
6753#ifdef FEAT_EVAL
6754 current_SID = cmd->uc_scriptID;
6755#endif
6756 (void)do_cmdline(buf, eap->getline, eap->cookie,
6757 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6758#ifdef FEAT_EVAL
6759 current_SID = save_current_SID;
6760#endif
6761 vim_free(buf);
6762 vim_free(split_buf);
6763}
6764
6765# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6766 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006767get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768{
6769 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6770}
6771
6772/*
6773 * Function given to ExpandGeneric() to obtain the list of user command names.
6774 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006776get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777{
6778 if (idx < curbuf->b_ucmds.ga_len)
6779 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6780 idx -= curbuf->b_ucmds.ga_len;
6781 if (idx < ucmds.ga_len)
6782 return USER_CMD(idx)->uc_name;
6783 return NULL;
6784}
6785
6786/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006787 * Function given to ExpandGeneric() to obtain the list of user address type names.
6788 */
6789 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006790get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006791{
6792 return (char_u *)addr_type_complete[idx].name;
6793}
6794
6795/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 * Function given to ExpandGeneric() to obtain the list of user command
6797 * attributes.
6798 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006800get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801{
6802 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006803 {"addr", "bang", "bar", "buffer", "complete",
6804 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006806 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 return NULL;
6808 return (char_u *)user_cmd_flags[idx];
6809}
6810
6811/*
6812 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006815get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816{
6817 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6818
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006819 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 return NULL;
6821 return (char_u *)user_cmd_nargs[idx];
6822}
6823
6824/*
6825 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6826 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006828get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829{
6830 return (char_u *)command_complete[idx].name;
6831}
6832# endif /* FEAT_CMDL_COMPL */
6833
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006834/*
6835 * Parse address type argument
6836 */
6837 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006838parse_addr_type_arg(
6839 char_u *value,
6840 int vallen,
6841 long *argt,
6842 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006843{
6844 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01006845
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006846 for (i = 0; addr_type_complete[i].expand != -1; ++i)
6847 {
6848 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
6849 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
6850 if (a && b)
6851 {
6852 *addr_type_arg = addr_type_complete[i].expand;
6853 break;
6854 }
6855 }
6856
6857 if (addr_type_complete[i].expand == -1)
6858 {
6859 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01006860
6861 for (i = 0; err[i] != NUL && !vim_iswhite(err[i]); i++)
6862 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006863 err[i] = NUL;
6864 EMSG2(_("E180: Invalid address type value: %s"), err);
6865 return FAIL;
6866 }
6867
6868 if (*addr_type_arg != ADDR_LINES)
6869 *argt |= NOTADR;
6870
6871 return OK;
6872}
6873
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874#endif /* FEAT_USR_CMDS */
6875
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006876#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6877/*
6878 * Parse a completion argument "value[vallen]".
6879 * The detected completion goes in "*complp", argument type in "*argt".
6880 * When there is an argument, for function and user defined completion, it's
6881 * copied to allocated memory and stored in "*compl_arg".
6882 * Returns FAIL if something is wrong.
6883 */
6884 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006885parse_compl_arg(
6886 char_u *value,
6887 int vallen,
6888 int *complp,
6889 long *argt,
6890 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006891{
6892 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006893# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006894 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006895# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006896 int i;
6897 int valend = vallen;
6898
6899 /* Look for any argument part - which is the part after any ',' */
6900 for (i = 0; i < vallen; ++i)
6901 {
6902 if (value[i] == ',')
6903 {
6904 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006905# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006906 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006907# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006908 valend = i;
6909 break;
6910 }
6911 }
6912
6913 for (i = 0; command_complete[i].expand != 0; ++i)
6914 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006915 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006916 && STRNCMP(value, command_complete[i].name, valend) == 0)
6917 {
6918 *complp = command_complete[i].expand;
6919 if (command_complete[i].expand == EXPAND_BUFFERS)
6920 *argt |= BUFNAME;
6921 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6922 || command_complete[i].expand == EXPAND_FILES)
6923 *argt |= XFILE;
6924 break;
6925 }
6926 }
6927
6928 if (command_complete[i].expand == 0)
6929 {
6930 EMSG2(_("E180: Invalid complete value: %s"), value);
6931 return FAIL;
6932 }
6933
6934# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6935 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6936 && arg != NULL)
6937# else
6938 if (arg != NULL)
6939# endif
6940 {
6941 EMSG(_("E468: Completion argument only allowed for custom completion"));
6942 return FAIL;
6943 }
6944
6945# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6946 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6947 && arg == NULL)
6948 {
6949 EMSG(_("E467: Custom completion requires a function argument"));
6950 return FAIL;
6951 }
6952
6953 if (arg != NULL)
6954 *compl_arg = vim_strnsave(arg, (int)arglen);
6955# endif
6956 return OK;
6957}
6958#endif
6959
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006961ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962{
Bram Moolenaare6850792010-05-14 15:28:44 +02006963 if (*eap->arg == NUL)
6964 {
6965#ifdef FEAT_EVAL
6966 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6967 char_u *p = NULL;
6968
6969 if (expr != NULL)
6970 {
6971 ++emsg_off;
6972 p = eval_to_string(expr, NULL, FALSE);
6973 --emsg_off;
6974 vim_free(expr);
6975 }
6976 if (p != NULL)
6977 {
6978 MSG(p);
6979 vim_free(p);
6980 }
6981 else
6982 MSG("default");
6983#else
6984 MSG(_("unknown"));
6985#endif
6986 }
6987 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006988 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989}
6990
6991 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006992ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993{
6994 if (*eap->arg == NUL && eap->cmd[2] == '!')
6995 MSG(_("Greetings, Vim user!"));
6996 do_highlight(eap->arg, eap->forceit, FALSE);
6997}
6998
6999
7000/*
7001 * Call this function if we thought we were going to exit, but we won't
7002 * (because of an error). May need to restore the terminal mode.
7003 */
7004 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007005not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006{
7007 exiting = FALSE;
7008 settmode(TMODE_RAW);
7009}
7010
7011/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007012 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 */
7014 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007015ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016{
Bram Moolenaarf240e182014-11-27 18:33:02 +01007017#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007018 win_T *wp;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007019#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007020
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021#ifdef FEAT_CMDWIN
7022 if (cmdwin_type != 0)
7023 {
7024 cmdwin_result = Ctrl_C;
7025 return;
7026 }
7027#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007028 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007029 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007030 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007031 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007032 return;
7033 }
Bram Moolenaarf240e182014-11-27 18:33:02 +01007034#ifdef FEAT_WINDOWS
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007035 if (eap->addr_count > 0)
7036 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007037 int wnr = eap->line2;
7038
7039 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7040 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007041 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007042 }
7043 else
Bram Moolenaarf240e182014-11-27 18:33:02 +01007044#endif
7045#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007046 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007047#endif
7048
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007049#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02007050 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007051 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02007052 * being closed (can only happen in autocommands). */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007053 if (curbuf_locked() || (wp->w_buffer->b_nwindows == 1
7054 && wp->w_buffer->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007055 return;
7056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057
7058#ifdef FEAT_NETBEANS_INTG
7059 netbeansForcedQuit = eap->forceit;
7060#endif
7061
7062 /*
7063 * If there are more files or windows we won't exit.
7064 */
7065 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7066 exiting = TRUE;
7067 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007068 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7069 | (eap->forceit ? CCGD_FORCEIT : 0)
7070 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007072 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 {
7074 not_exiting();
7075 }
7076 else
7077 {
7078#ifdef FEAT_WINDOWS
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007079 /* quit last window
7080 * Note: only_one_window() returns true, even so a help window is
7081 * still open. In that case only quit, if no address has been
7082 * specified. Example:
7083 * :h|wincmd w|1q - don't quit
7084 * :h|wincmd w|q - quit
7085 */
7086 if (only_one_window() && (firstwin == lastwin || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087#endif
7088 getout(0);
7089#ifdef FEAT_WINDOWS
7090# ifdef FEAT_GUI
7091 need_mouse_correct = TRUE;
7092# endif
7093 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007094 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095#endif
7096 }
7097}
7098
7099/*
7100 * ":cquit".
7101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007103ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104{
7105 getout(1); /* this does not always pass on the exit code to the Manx
7106 compiler. why? */
7107}
7108
7109/*
7110 * ":qall": try to quit all windows
7111 */
7112 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007113ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114{
7115# ifdef FEAT_CMDWIN
7116 if (cmdwin_type != 0)
7117 {
7118 if (eap->forceit)
7119 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7120 else
7121 cmdwin_result = K_XF2;
7122 return;
7123 }
7124# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007125
7126 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007127 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007128 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007129 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007130 return;
7131 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007132#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007133 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7134 /* Refuse to quit when locked or when the buffer in the last window is
7135 * being closed (can only happen in autocommands). */
7136 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007137 return;
7138#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007139
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007141 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 getout(0);
7143 not_exiting();
7144}
7145
Bram Moolenaard9967712006-03-11 21:18:15 +00007146#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147/*
7148 * ":close": close current window, unless it is the last one
7149 */
7150 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007151ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007153 win_T *win;
7154 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155# ifdef FEAT_CMDWIN
7156 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007157 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 else
7159# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007160 if (!text_locked()
7161#ifdef FEAT_AUTOCMD
7162 && !curbuf_locked()
7163#endif
7164 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007165 {
7166 if (eap->addr_count == 0)
7167 ex_win_close(eap->forceit, curwin, NULL);
7168 else {
7169 for (win = firstwin; win != NULL; win = win->w_next)
7170 {
7171 winnr++;
7172 if (winnr == eap->line2)
7173 break;
7174 }
7175 if (win == NULL)
7176 win = lastwin;
7177 ex_win_close(eap->forceit, win, NULL);
7178 }
7179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180}
7181
Bram Moolenaard9967712006-03-11 21:18:15 +00007182# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007183/*
7184 * ":pclose": Close any preview window.
7185 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007187ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007188{
7189 win_T *win;
7190
7191 for (win = firstwin; win != NULL; win = win->w_next)
7192 if (win->w_p_pvw)
7193 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007194 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007195 break;
7196 }
7197}
Bram Moolenaard9967712006-03-11 21:18:15 +00007198# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007199
Bram Moolenaarf740b292006-02-16 22:11:02 +00007200/*
7201 * Close window "win" and take care of handling closing the last window for a
7202 * modified buffer.
7203 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007204 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007205ex_win_close(
7206 int forceit,
7207 win_T *win,
7208 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209{
7210 int need_hide;
7211 buf_T *buf = win->w_buffer;
7212
7213 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007214 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007216# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 if ((p_confirm || cmdmod.confirm) && p_write)
7218 {
7219 dialog_changed(buf, FALSE);
7220 if (buf_valid(buf) && bufIsChanged(buf))
7221 return;
7222 need_hide = FALSE;
7223 }
7224 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007225# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 {
7227 EMSG(_(e_nowrtmsg));
7228 return;
7229 }
7230 }
7231
Bram Moolenaard9967712006-03-11 21:18:15 +00007232# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007234# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007235
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007237 if (tp == NULL)
7238 win_close(win, !need_hide && !P_HID(buf));
7239 else
7240 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241}
7242
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007244 * ":tabclose": close current tab page, unless it is the last one.
7245 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 */
7247 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007248ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007250 tabpage_T *tp;
7251
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007252# ifdef FEAT_CMDWIN
7253 if (cmdwin_type != 0)
7254 cmdwin_result = K_IGNORE;
7255 else
7256# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007257 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007258 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007259 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007261 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007262 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007263 tp = find_tabpage((int)eap->line2);
7264 if (tp == NULL)
7265 {
7266 beep_flush();
7267 return;
7268 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007269 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007270 {
7271 tabpage_close_other(tp, eap->forceit);
7272 return;
7273 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007274 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007275 if (!text_locked()
7276#ifdef FEAT_AUTOCMD
7277 && !curbuf_locked()
7278#endif
7279 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00007280 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007282}
7283
7284/*
7285 * ":tabonly": close all tab pages except the current one
7286 */
7287 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007288ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007289{
7290 tabpage_T *tp;
7291 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007292
7293# ifdef FEAT_CMDWIN
7294 if (cmdwin_type != 0)
7295 cmdwin_result = K_IGNORE;
7296 else
7297# endif
7298 if (first_tabpage->tp_next == NULL)
7299 MSG(_("Already only one tab page"));
7300 else
7301 {
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007302 if (eap->addr_count > 0)
7303 goto_tabpage(eap->line2);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007304 /* Repeat this up to a 1000 times, because autocommands may mess
7305 * up the lists. */
7306 for (done = 0; done < 1000; ++done)
7307 {
7308 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7309 if (tp->tp_topframe != topframe)
7310 {
7311 tabpage_close_other(tp, eap->forceit);
7312 /* if we failed to close it quit */
7313 if (valid_tabpage(tp))
7314 done = 1000;
7315 /* start over, "tp" is now invalid */
7316 break;
7317 }
7318 if (first_tabpage->tp_next == NULL)
7319 break;
7320 }
7321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323
7324/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007325 * Close the current tab page.
7326 */
7327 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007328tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007329{
7330 /* First close all the windows but the current one. If that worked then
7331 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007332 if (lastwin != firstwin)
7333 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007334 if (lastwin == firstwin)
7335 ex_win_close(forceit, curwin, NULL);
7336# ifdef FEAT_GUI
7337 need_mouse_correct = TRUE;
7338# endif
7339}
7340
7341/*
7342 * Close tab page "tp", which is not the current tab page.
7343 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007344 * Also takes care of the tab pages line disappearing when closing the
7345 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007346 */
7347 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007348tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007349{
7350 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007351 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007352 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007353
7354 /* Limit to 1000 windows, autocommands may add a window while we close
7355 * one. OK, so I'm paranoid... */
7356 while (++done < 1000)
7357 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007358 wp = tp->tp_firstwin;
7359 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007360
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007361 /* Autocommands may delete the tab page under our fingers and we may
7362 * fail to close a window with a modified buffer. */
7363 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007364 break;
7365 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007366
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007367 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007368 if (h != tabline_height())
7369 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007370}
7371
7372/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 * ":only".
7374 */
7375 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007376ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007378 win_T *wp;
7379 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380# ifdef FEAT_GUI
7381 need_mouse_correct = TRUE;
7382# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007383 if (eap->addr_count > 0)
7384 {
7385 wnr = eap->line2;
7386 for (wp = firstwin; --wnr > 0; )
7387 {
7388 if (wp->w_next == NULL)
7389 break;
7390 else
7391 wp = wp->w_next;
7392 }
7393 win_goto(wp);
7394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 close_others(TRUE, eap->forceit);
7396}
7397
7398/*
7399 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007400 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007402 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007403ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404{
7405 if (eap->addr_count == 0)
7406 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007407 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408}
7409#endif /* FEAT_WINDOWS */
7410
7411 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007412ex_hide(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413{
7414 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
7415 eap->errmsg = e_invarg;
7416 else
7417 {
7418 /* ":hide" or ":hide | cmd": hide current window */
7419 eap->nextcmd = check_nextcmd(eap->arg);
7420#ifdef FEAT_WINDOWS
7421 if (!eap->skip)
7422 {
7423# ifdef FEAT_GUI
7424 need_mouse_correct = TRUE;
7425# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007426 if (eap->addr_count == 0)
7427 win_close(curwin, FALSE); /* don't free buffer */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007428 else
7429 {
7430 int winnr = 0;
7431 win_T *win;
7432
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007433 for (win = firstwin; win != NULL; win = win->w_next)
7434 {
7435 winnr++;
7436 if (winnr == eap->line2)
7437 break;
7438 }
7439 if (win == NULL)
7440 win = lastwin;
7441 win_close(win, FALSE);
7442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 }
7444#endif
7445 }
7446}
7447
7448/*
7449 * ":stop" and ":suspend": Suspend Vim.
7450 */
7451 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007452ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453{
7454 /*
7455 * Disallow suspending for "rvim".
7456 */
7457 if (!check_restricted()
7458#ifdef WIN3264
7459 /*
7460 * Check if external commands are allowed now.
7461 */
7462 && can_end_termcap_mode(TRUE)
7463#endif
7464 )
7465 {
7466 if (!eap->forceit)
7467 autowrite_all();
7468 windgoto((int)Rows - 1, 0);
7469 out_char('\n');
7470 out_flush();
7471 stoptermcap();
7472 out_flush(); /* needed for SUN to restore xterm buffer */
7473#ifdef FEAT_TITLE
7474 mch_restore_title(3); /* restore window titles */
7475#endif
7476 ui_suspend(); /* call machine specific function */
7477#ifdef FEAT_TITLE
7478 maketitle();
7479 resettitle(); /* force updating the title */
7480#endif
7481 starttermcap();
7482 scroll_start(); /* scroll screen before redrawing */
7483 redraw_later_clear();
7484 shell_resized(); /* may have resized window */
7485 }
7486}
7487
7488/*
7489 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7490 */
7491 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007492ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493{
7494#ifdef FEAT_CMDWIN
7495 if (cmdwin_type != 0)
7496 {
7497 cmdwin_result = Ctrl_C;
7498 return;
7499 }
7500#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007501 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007502 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007503 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007504 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007505 return;
7506 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007507#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007508 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7509 /* Refuse to quit when locked or when the buffer in the last window is
7510 * being closed (can only happen in autocommands). */
7511 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007512 return;
7513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514
7515 /*
7516 * if more files or windows we won't exit
7517 */
7518 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7519 exiting = TRUE;
7520 if ( ((eap->cmdidx == CMD_wq
7521 || curbufIsChanged())
7522 && do_write(eap) == FAIL)
7523 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007524 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 {
7526 not_exiting();
7527 }
7528 else
7529 {
7530#ifdef FEAT_WINDOWS
7531 if (only_one_window()) /* quit last window, exit Vim */
7532#endif
7533 getout(0);
7534#ifdef FEAT_WINDOWS
7535# ifdef FEAT_GUI
7536 need_mouse_correct = TRUE;
7537# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007538 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 win_close(curwin, !P_HID(curwin->w_buffer));
7540#endif
7541 }
7542}
7543
7544/*
7545 * ":print", ":list", ":number".
7546 */
7547 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007548ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007550 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7551 EMSG(_(e_emptybuf));
7552 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007554 for ( ;!got_int; ui_breakcheck())
7555 {
7556 print_line(eap->line1,
7557 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7558 || (eap->flags & EXFLAG_NR)),
7559 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7560 if (++eap->line1 > eap->line2)
7561 break;
7562 out_flush(); /* show one line at a time */
7563 }
7564 setpcmark();
7565 /* put cursor at last line */
7566 curwin->w_cursor.lnum = eap->line2;
7567 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 }
7569
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571}
7572
7573#ifdef FEAT_BYTEOFF
7574 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007575ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576{
7577 goto_byte(eap->line2);
7578}
7579#endif
7580
7581/*
7582 * ":shell".
7583 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007585ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586{
7587 do_shell(NULL, 0);
7588}
7589
7590#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7591 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007592 || defined(FEAT_GUI_MSWIN) \
7593 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 || defined(PROTO)
7595
7596/*
7597 * Handle a file drop. The code is here because a drop is *nearly* like an
7598 * :args command, but not quite (we have a list of exact filenames, so we
7599 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7600 * code is very similar to :args and hence needs access to a lot of the static
7601 * functions in this file.
7602 *
7603 * The list should be allocated using alloc(), as should each item in the
7604 * list. This function takes over responsibility for freeing the list.
7605 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007606 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7608 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7609 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7610 * file functionality is (currently) not in EMX this is not presently a
7611 * problem.
7612 */
7613 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007614handle_drop(
7615 int filec, /* the number of files dropped */
7616 char_u **filev, /* the list of files dropped */
7617 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618{
7619 exarg_T ea;
7620 int save_msg_scroll = msg_scroll;
7621
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007622 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007623 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007625#ifdef FEAT_AUTOCMD
7626 if (curbuf_locked())
7627 return;
7628#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007629 /* When the screen is being updated we should not change buffers and
7630 * windows structures, it may cause freed memory to be used. */
7631 if (updating_screen)
7632 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633
7634 /* Check whether the current buffer is changed. If so, we will need
7635 * to split the current window or data could be lost.
7636 * We don't need to check if the 'hidden' option is set, as in this
7637 * case the buffer won't be lost.
7638 */
7639 if (!P_HID(curbuf) && !split)
7640 {
7641 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007642 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 --emsg_off;
7644 }
7645 if (split)
7646 {
7647# ifdef FEAT_WINDOWS
7648 if (win_split(0, 0) == FAIL)
7649 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007650 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651
7652 /* When splitting the window, create a new alist. Otherwise the
7653 * existing one is overwritten. */
7654 alist_unlink(curwin->w_alist);
7655 alist_new();
7656# else
7657 return; /* can't split, always fail */
7658# endif
7659 }
7660
7661 /*
7662 * Set up the new argument list.
7663 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007664 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665
7666 /*
7667 * Move to the first file.
7668 */
7669 /* Fake up a minimal "next" command for do_argfile() */
7670 vim_memset(&ea, 0, sizeof(ea));
7671 ea.cmd = (char_u *)"next";
7672 do_argfile(&ea, 0);
7673
7674 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7675 * mode that is not needed here. */
7676 need_start_insertmode = FALSE;
7677
7678 /* Restore msg_scroll, otherwise a following command may cause scrolling
7679 * unexpectedly. The screen will be redrawn by the caller, thus
7680 * msg_scroll being set by displaying a message is irrelevant. */
7681 msg_scroll = save_msg_scroll;
7682}
7683#endif
7684
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685/*
7686 * Clear an argument list: free all file names and reset it to zero entries.
7687 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007688 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007689alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690{
7691 while (--al->al_ga.ga_len >= 0)
7692 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7693 ga_clear(&al->al_ga);
7694}
7695
7696/*
7697 * Init an argument list.
7698 */
7699 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007700alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701{
7702 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7703}
7704
7705#if defined(FEAT_WINDOWS) || defined(PROTO)
7706
7707/*
7708 * Remove a reference from an argument list.
7709 * Ignored when the argument list is the global one.
7710 * If the argument list is no longer used by any window, free it.
7711 */
7712 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007713alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714{
7715 if (al != &global_alist && --al->al_refcount <= 0)
7716 {
7717 alist_clear(al);
7718 vim_free(al);
7719 }
7720}
7721
7722# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7723/*
7724 * Create a new argument list and use it for the current window.
7725 */
7726 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007727alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728{
7729 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7730 if (curwin->w_alist == NULL)
7731 {
7732 curwin->w_alist = &global_alist;
7733 ++global_alist.al_refcount;
7734 }
7735 else
7736 {
7737 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007738 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 alist_init(curwin->w_alist);
7740 }
7741}
7742# endif
7743#endif
7744
Bram Moolenaar53076832015-12-31 19:53:21 +01007745#if (!defined(UNIX) && !defined(__EMX__)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746/*
7747 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007748 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7749 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 */
7751 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007752alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753{
7754 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007755 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 char_u **new_arg_files;
7757 int new_arg_file_count;
7758 char_u *save_p_su = p_su;
7759 int i;
7760
7761 /* Don't use 'suffixes' here. This should work like the shell did the
7762 * expansion. Also, the vimrc file isn't read yet, thus the user
7763 * can't set the options. */
7764 p_su = empty_option;
7765 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7766 if (old_arg_files != NULL)
7767 {
7768 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007769 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7770 old_arg_count = GARGCOUNT;
7771 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007773 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 && new_arg_file_count > 0)
7775 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007776 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7777 TRUE, fnum_list, fnum_len);
7778 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 }
7781 p_su = save_p_su;
7782}
7783#endif
7784
7785/*
7786 * Set the argument list for the current window.
7787 * Takes over the allocated files[] and the allocated fnames in it.
7788 */
7789 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007790alist_set(
7791 alist_T *al,
7792 int count,
7793 char_u **files,
7794 int use_curbuf,
7795 int *fnum_list,
7796 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797{
7798 int i;
7799
7800 alist_clear(al);
7801 if (ga_grow(&al->al_ga, count) == OK)
7802 {
7803 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007804 {
7805 if (got_int)
7806 {
7807 /* When adding many buffers this can take a long time. Allow
7808 * interrupting here. */
7809 while (i < count)
7810 vim_free(files[i++]);
7811 break;
7812 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007813
7814 /* May set buffer name of a buffer previously used for the
7815 * argument list, so that it's re-used by alist_add. */
7816 if (fnum_list != NULL && i < fnum_len)
7817 buf_set_name(fnum_list[i], files[i]);
7818
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007820 ui_breakcheck();
7821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 vim_free(files);
7823 }
7824 else
7825 FreeWild(count, files);
7826#ifdef FEAT_WINDOWS
7827 if (al == &global_alist)
7828#endif
7829 arg_had_last = FALSE;
7830}
7831
7832/*
7833 * Add file "fname" to argument list "al".
7834 * "fname" must have been allocated and "al" must have been checked for room.
7835 */
7836 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007837alist_add(
7838 alist_T *al,
7839 char_u *fname,
7840 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841{
7842 if (fname == NULL) /* don't add NULL file names */
7843 return;
7844#ifdef BACKSLASH_IN_FILENAME
7845 slash_adjust(fname);
7846#endif
7847 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7848 if (set_fnum > 0)
7849 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7850 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7851 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852}
7853
7854#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7855/*
7856 * Adjust slashes in file names. Called after 'shellslash' was set.
7857 */
7858 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007859alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860{
7861 int i;
7862# ifdef FEAT_WINDOWS
7863 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007864 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865# endif
7866
7867 for (i = 0; i < GARGCOUNT; ++i)
7868 if (GARGLIST[i].ae_fname != NULL)
7869 slash_adjust(GARGLIST[i].ae_fname);
7870# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007871 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 if (wp->w_alist != &global_alist)
7873 for (i = 0; i < WARGCOUNT(wp); ++i)
7874 if (WARGLIST(wp)[i].ae_fname != NULL)
7875 slash_adjust(WARGLIST(wp)[i].ae_fname);
7876# endif
7877}
7878#endif
7879
7880/*
7881 * ":preserve".
7882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007884ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007886 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 ml_preserve(curbuf, TRUE);
7888}
7889
7890/*
7891 * ":recover".
7892 */
7893 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007894ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895{
7896 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7897 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007898 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7899 | CCGD_MULTWIN
7900 | (eap->forceit ? CCGD_FORCEIT : 0)
7901 | CCGD_EXCMD)
7902
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 && (*eap->arg == NUL
7904 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7905 ml_recover();
7906 recoverymode = FALSE;
7907}
7908
7909/*
7910 * Command modifier used in a wrong way.
7911 */
7912 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007913ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914{
7915 eap->errmsg = e_invcmd;
7916}
7917
7918#ifdef FEAT_WINDOWS
7919/*
7920 * :sview [+command] file split window with new file, read-only
7921 * :split [[+command] file] split window with current or new file
7922 * :vsplit [[+command] file] split window vertically with current or new file
7923 * :new [[+command] file] split window with no or new file
7924 * :vnew [[+command] file] split vertically window with no or new file
7925 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007926 *
7927 * :tabedit open new Tab page with empty window
7928 * :tabedit [+command] file open new Tab page and edit "file"
7929 * :tabnew [[+command] file] just like :tabedit
7930 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 */
7932 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007933ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007935 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007936# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007938# endif
7939# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007941# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007943# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7945 {
7946 ex_ni(eap);
7947 return;
7948 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007949# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007951# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007953# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007955# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007957 * quickfix windows. But it's OK when doing ":tab split". */
7958 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 {
7960 if (eap->cmdidx == CMD_split)
7961 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007962# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 if (eap->cmdidx == CMD_vsplit)
7964 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007965# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007967# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007969# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007970 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 {
7972 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7973 FNAME_MESS, TRUE, curbuf->b_ffname);
7974 if (fname == NULL)
7975 goto theend;
7976 eap->arg = fname;
7977 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007978# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007980# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007982# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007984# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007986# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 && eap->cmdidx != CMD_new)
7988 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007989# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007990 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007991# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007992 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007993# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007994 au_has_group((char_u *)"FileExplorer"))
7995 {
7996 /* No browsing supported but we do have the file explorer:
7997 * Edit the directory. */
7998 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7999 eap->arg = (char_u *)".";
8000 }
8001 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008002# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008003 {
8004 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008006 if (fname == NULL)
8007 goto theend;
8008 eap->arg = fname;
8009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 }
8011 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008012# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008014 /*
8015 * Either open new tab page or split the window.
8016 */
8017 if (eap->cmdidx == CMD_tabedit
8018 || eap->cmdidx == CMD_tabfind
8019 || eap->cmdidx == CMD_tabnew)
8020 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008021 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8022 : eap->addr_count == 0 ? 0
8023 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008024 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008025 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008026
8027 /* set the alternate buffer for the window we came from */
8028 if (curwin != old_curwin
8029 && win_valid(old_curwin)
8030 && old_curwin->w_buffer != curbuf
8031 && !cmdmod.keepalt)
8032 old_curwin->w_alt_fnum = curbuf->b_fnum;
8033 }
8034 }
8035 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8037 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008038# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 /* Reset 'scrollbind' when editing another file, but keep it when
8040 * doing ":split" without arguments. */
8041 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008042# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008044# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008046 {
8047 RESET_BINDING(curwin);
8048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 else
8050 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008051# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 do_exedit(eap, old_curwin);
8053 }
8054
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008055# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008057# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008059# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060theend:
8061 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008062# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008064
8065/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008066 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008067 */
8068 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008069tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008070{
8071 exarg_T ea;
8072
8073 vim_memset(&ea, 0, sizeof(ea));
8074 ea.cmdidx = CMD_tabnew;
8075 ea.cmd = (char_u *)"tabn";
8076 ea.arg = (char_u *)"";
8077 ex_splitview(&ea);
8078}
8079
8080/*
8081 * :tabnext command
8082 */
8083 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008084ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008085{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008086 switch (eap->cmdidx)
8087 {
8088 case CMD_tabfirst:
8089 case CMD_tabrewind:
8090 goto_tabpage(1);
8091 break;
8092 case CMD_tablast:
8093 goto_tabpage(9999);
8094 break;
8095 case CMD_tabprevious:
8096 case CMD_tabNext:
8097 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
8098 break;
8099 default: /* CMD_tabnext */
8100 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
8101 break;
8102 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008103}
8104
8105/*
8106 * :tabmove command
8107 */
8108 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008109ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008110{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008111 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008112
8113 if (eap->arg && *eap->arg != NUL)
8114 {
8115 char_u *p = eap->arg;
8116 int relative = 0; /* argument +N/-N means: move N places to the
8117 * right/left relative to the current position. */
8118
8119 if (*eap->arg == '-')
8120 {
8121 relative = -1;
8122 p = eap->arg + 1;
8123 }
8124 else if (*eap->arg == '+')
8125 {
8126 relative = 1;
8127 p = eap->arg + 1;
8128 }
8129 else
8130 p = eap->arg;
8131
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008132 if (relative == 0)
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008133 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008134 if (STRCMP(p, "$") == 0)
8135 tab_number = LAST_TAB_NR;
8136 else if (p == skipdigits(p))
8137 {
8138 /* No numbers as argument. */
8139 eap->errmsg = e_invarg;
8140 return;
8141 }
8142 else
8143 tab_number = getdigits(&p);
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008144 }
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008145 else
8146 {
8147 if (*p != NUL)
8148 tab_number = getdigits(&p);
8149 else
8150 tab_number = 1;
8151 tab_number = tab_number * relative + tabpage_index(curtab);
8152 if (relative == -1)
8153 --tab_number;
8154 }
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008155 }
8156 else if (eap->addr_count != 0)
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008157 {
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008158 tab_number = eap->line2;
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008159 if (**eap->cmdlinep == '-')
8160 --tab_number;
8161 }
8162 else
8163 tab_number = LAST_TAB_NR;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008164
8165 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008166}
8167
8168/*
8169 * :tabs command: List tabs and their contents.
8170 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008171 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008172ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008173{
8174 tabpage_T *tp;
8175 win_T *wp;
8176 int tabcount = 1;
8177
8178 msg_start();
8179 msg_scroll = TRUE;
8180 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8181 {
8182 msg_putchar('\n');
8183 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
8184 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
8185 out_flush(); /* output one line at a time */
8186 ui_breakcheck();
8187
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008188 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008189 wp = firstwin;
8190 else
8191 wp = tp->tp_firstwin;
8192 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8193 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008194 msg_putchar('\n');
8195 msg_putchar(wp == curwin ? '>' : ' ');
8196 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008197 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8198 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008199 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008200 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008201 else
8202 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8203 IObuff, IOSIZE, TRUE);
8204 msg_outtrans(IObuff);
8205 out_flush(); /* output one line at a time */
8206 ui_breakcheck();
8207 }
8208 }
8209}
8210
8211#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212
8213/*
8214 * ":mode": Set screen mode.
8215 * If no argument given, just get the screen size and redraw.
8216 */
8217 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008218ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219{
8220 if (*eap->arg == NUL)
8221 shell_resized();
8222 else
8223 mch_screenmode(eap->arg);
8224}
8225
8226#ifdef FEAT_WINDOWS
8227/*
8228 * ":resize".
8229 * set, increment or decrement current window height
8230 */
8231 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008232ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233{
8234 int n;
8235 win_T *wp = curwin;
8236
8237 if (eap->addr_count > 0)
8238 {
8239 n = eap->line2;
8240 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8241 ;
8242 }
8243
8244#ifdef FEAT_GUI
8245 need_mouse_correct = TRUE;
8246#endif
8247 n = atol((char *)eap->arg);
8248#ifdef FEAT_VERTSPLIT
8249 if (cmdmod.split & WSP_VERT)
8250 {
8251 if (*eap->arg == '-' || *eap->arg == '+')
8252 n += W_WIDTH(curwin);
8253 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8254 n = 9999;
8255 win_setwidth_win((int)n, wp);
8256 }
8257 else
8258#endif
8259 {
8260 if (*eap->arg == '-' || *eap->arg == '+')
8261 n += curwin->w_height;
8262 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8263 n = 9999;
8264 win_setheight_win((int)n, wp);
8265 }
8266}
8267#endif
8268
8269/*
8270 * ":find [+command] <file>" command.
8271 */
8272 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008273ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274{
8275#ifdef FEAT_SEARCHPATH
8276 char_u *fname;
8277 int count;
8278
8279 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8280 TRUE, curbuf->b_ffname);
8281 if (eap->addr_count > 0)
8282 {
8283 /* Repeat finding the file "count" times. This matters when it
8284 * appears several times in the path. */
8285 count = eap->line2;
8286 while (fname != NULL && --count > 0)
8287 {
8288 vim_free(fname);
8289 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8290 FALSE, curbuf->b_ffname);
8291 }
8292 }
8293
8294 if (fname != NULL)
8295 {
8296 eap->arg = fname;
8297#endif
8298 do_exedit(eap, NULL);
8299#ifdef FEAT_SEARCHPATH
8300 vim_free(fname);
8301 }
8302#endif
8303}
8304
8305/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008306 * ":open" simulation: for now just work like ":visual".
8307 */
8308 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008309ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008310{
8311 regmatch_T regmatch;
8312 char_u *p;
8313
8314 curwin->w_cursor.lnum = eap->line2;
8315 beginline(BL_SOL | BL_FIX);
8316 if (*eap->arg == '/')
8317 {
8318 /* ":open /pattern/": put cursor in column found with pattern */
8319 ++eap->arg;
8320 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8321 *p = NUL;
8322 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8323 if (regmatch.regprog != NULL)
8324 {
8325 regmatch.rm_ic = p_ic;
8326 p = ml_get_curline();
8327 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008328 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008329 else
8330 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008331 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008332 }
8333 /* Move to the NUL, ignore any other arguments. */
8334 eap->arg += STRLEN(eap->arg);
8335 }
8336 check_cursor();
8337
8338 eap->cmdidx = CMD_visual;
8339 do_exedit(eap, NULL);
8340}
8341
8342/*
8343 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 */
8345 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008346ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347{
8348 do_exedit(eap, NULL);
8349}
8350
8351/*
8352 * ":edit <file>" command and alikes.
8353 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008355do_exedit(
8356 exarg_T *eap,
8357 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358{
8359 int n;
8360#ifdef FEAT_WINDOWS
8361 int need_hide;
8362#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008363 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364
8365 /*
8366 * ":vi" command ends Ex mode.
8367 */
8368 if (exmode_active && (eap->cmdidx == CMD_visual
8369 || eap->cmdidx == CMD_view))
8370 {
8371 exmode_active = FALSE;
8372 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008373 {
8374 /* Special case: ":global/pat/visual\NLvi-commands" */
8375 if (global_busy)
8376 {
8377 int rd = RedrawingDisabled;
8378 int nwr = no_wait_return;
8379 int ms = msg_scroll;
8380#ifdef FEAT_GUI
8381 int he = hold_gui_events;
8382#endif
8383
8384 if (eap->nextcmd != NULL)
8385 {
8386 stuffReadbuff(eap->nextcmd);
8387 eap->nextcmd = NULL;
8388 }
8389
8390 if (exmode_was != EXMODE_VIM)
8391 settmode(TMODE_RAW);
8392 RedrawingDisabled = 0;
8393 no_wait_return = 0;
8394 need_wait_return = FALSE;
8395 msg_scroll = 0;
8396#ifdef FEAT_GUI
8397 hold_gui_events = 0;
8398#endif
8399 must_redraw = CLEAR;
8400
8401 main_loop(FALSE, TRUE);
8402
8403 RedrawingDisabled = rd;
8404 no_wait_return = nwr;
8405 msg_scroll = ms;
8406#ifdef FEAT_GUI
8407 hold_gui_events = he;
8408#endif
8409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 }
8413
8414 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008415 || eap->cmdidx == CMD_tabnew
8416 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417#ifdef FEAT_VERTSPLIT
8418 || eap->cmdidx == CMD_vnew
8419#endif
8420 ) && *eap->arg == NUL)
8421 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008422 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 setpcmark();
8424 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008425 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8426 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 }
8428 else if ((eap->cmdidx != CMD_split
8429#ifdef FEAT_VERTSPLIT
8430 && eap->cmdidx != CMD_vsplit
8431#endif
8432 )
8433 || *eap->arg != NUL
8434#ifdef FEAT_BROWSE
8435 || cmdmod.browse
8436#endif
8437 )
8438 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008439#ifdef FEAT_AUTOCMD
8440 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8441 * can bring us here, others are stopped earlier. */
8442 if (*eap->arg != NUL && curbuf_locked())
8443 return;
8444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 n = readonlymode;
8446 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8447 readonlymode = TRUE;
8448 else if (eap->cmdidx == CMD_enew)
8449 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8450 empty buffer */
8451 setpcmark();
8452 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8453 NULL, eap,
8454 /* ":edit" goes to first line if Vi compatible */
8455 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8456 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8457 ? ECMD_ONE : eap->do_ecmd_lnum,
8458 (P_HID(curbuf) ? ECMD_HIDE : 0)
8459 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008460 /* after a split we can use an existing buffer */
8461 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462#ifdef FEAT_LISTCMDS
8463 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8464#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008465 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 {
8467 /* Editing the file failed. If the window was split, close it. */
8468#ifdef FEAT_WINDOWS
8469 if (old_curwin != NULL)
8470 {
8471 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8472 if (!need_hide || P_HID(curbuf))
8473 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008474# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8475 cleanup_T cs;
8476
8477 /* Reset the error/interrupt/exception state here so that
8478 * aborting() returns FALSE when closing a window. */
8479 enter_cleanup(&cs);
8480# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481# ifdef FEAT_GUI
8482 need_mouse_correct = TRUE;
8483# endif
8484 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008485
8486# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8487 /* Restore the error/interrupt/exception state if not
8488 * discarded by a new aborting error, interrupt, or
8489 * uncaught exception. */
8490 leave_cleanup(&cs);
8491# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 }
8493 }
8494#endif
8495 }
8496 else if (readonlymode && curbuf->b_nwindows == 1)
8497 {
8498 /* When editing an already visited buffer, 'readonly' won't be set
8499 * but the previous value is kept. With ":view" and ":sview" we
8500 * want the file to be readonly, except when another window is
8501 * editing the same buffer. */
8502 curbuf->b_p_ro = TRUE;
8503 }
8504 readonlymode = n;
8505 }
8506 else
8507 {
8508 if (eap->do_ecmd_cmd != NULL)
8509 do_cmdline_cmd(eap->do_ecmd_cmd);
8510#ifdef FEAT_TITLE
8511 n = curwin->w_arg_idx_invalid;
8512#endif
8513 check_arg_idx(curwin);
8514#ifdef FEAT_TITLE
8515 if (n != curwin->w_arg_idx_invalid)
8516 maketitle();
8517#endif
8518 }
8519
8520#ifdef FEAT_WINDOWS
8521 /*
8522 * if ":split file" worked, set alternate file name in old window to new
8523 * file
8524 */
8525 if (old_curwin != NULL
8526 && *eap->arg != NUL
8527 && curwin != old_curwin
8528 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008529 && old_curwin->w_buffer != curbuf
8530 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 old_curwin->w_alt_fnum = curbuf->b_fnum;
8532#endif
8533
8534 ex_no_reprint = TRUE;
8535}
8536
8537#ifndef FEAT_GUI
8538/*
8539 * ":gui" and ":gvim" when there is no GUI.
8540 */
8541 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008542ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543{
8544 eap->errmsg = e_nogvim;
8545}
8546#endif
8547
8548#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8549 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008550ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551{
8552 gui_make_tearoff(eap->arg);
8553}
8554#endif
8555
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008556#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008558ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008560 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561}
8562#endif
8563
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008565ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566{
8567 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8568 MSG(_("No swap file"));
8569 else
8570 msg(curbuf->b_ml.ml_mfp->mf_fname);
8571}
8572
8573/*
8574 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8575 * offset.
8576 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8577 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008579ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580{
8581#ifdef FEAT_SCROLLBIND
8582 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008583 win_T *save_curwin = curwin;
8584 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 long topline;
8586 long y;
8587 linenr_T old_linenr = curwin->w_cursor.lnum;
8588
8589 setpcmark();
8590
8591 /*
8592 * determine max topline
8593 */
8594 if (curwin->w_p_scb)
8595 {
8596 topline = curwin->w_topline;
8597 for (wp = firstwin; wp; wp = wp->w_next)
8598 {
8599 if (wp->w_p_scb && wp->w_buffer)
8600 {
8601 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8602 if (topline > y)
8603 topline = y;
8604 }
8605 }
8606 if (topline < 1)
8607 topline = 1;
8608 }
8609 else
8610 {
8611 topline = 1;
8612 }
8613
8614
8615 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008616 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8619 {
8620 if (curwin->w_p_scb)
8621 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008622 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 y = topline - curwin->w_topline;
8624 if (y > 0)
8625 scrollup(y, TRUE);
8626 else
8627 scrolldown(-y, TRUE);
8628 curwin->w_scbind_pos = topline;
8629 redraw_later(VALID);
8630 cursor_correct();
8631#ifdef FEAT_WINDOWS
8632 curwin->w_redr_status = TRUE;
8633#endif
8634 }
8635 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008636 curwin = save_curwin;
8637 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 if (curwin->w_p_scb)
8639 {
8640 did_syncbind = TRUE;
8641 checkpcmark();
8642 if (old_linenr != curwin->w_cursor.lnum)
8643 {
8644 char_u ctrl_o[2];
8645
8646 ctrl_o[0] = Ctrl_O;
8647 ctrl_o[1] = 0;
8648 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8649 }
8650 }
8651#endif
8652}
8653
8654
8655 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008656ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008658 int i;
8659 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8660 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661
8662 if (eap->usefilter) /* :r!cmd */
8663 do_bang(1, eap, FALSE, FALSE, TRUE);
8664 else
8665 {
8666 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8667 return;
8668
8669#ifdef FEAT_BROWSE
8670 if (cmdmod.browse)
8671 {
8672 char_u *browseFile;
8673
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008674 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 NULL, NULL, NULL, curbuf);
8676 if (browseFile != NULL)
8677 {
8678 i = readfile(browseFile, NULL,
8679 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8680 vim_free(browseFile);
8681 }
8682 else
8683 i = OK;
8684 }
8685 else
8686#endif
8687 if (*eap->arg == NUL)
8688 {
8689 if (check_fname() == FAIL) /* check for no file name */
8690 return;
8691 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8692 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8693 }
8694 else
8695 {
8696 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8697 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8698 i = readfile(eap->arg, NULL,
8699 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8700
8701 }
8702 if (i == FAIL)
8703 {
8704#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8705 if (!aborting())
8706#endif
8707 EMSG2(_(e_notopen), eap->arg);
8708 }
8709 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008710 {
8711 if (empty && exmode_active)
8712 {
8713 /* Delete the empty line that remains. Historically ex does
8714 * this but vi doesn't. */
8715 if (eap->line2 == 0)
8716 lnum = curbuf->b_ml.ml_line_count;
8717 else
8718 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008719 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008720 {
8721 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008722 if (curwin->w_cursor.lnum > 1
8723 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008724 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008725 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008726 }
8727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 }
8731}
8732
Bram Moolenaarea408852005-06-25 22:49:46 +00008733static char_u *prev_dir = NULL;
8734
8735#if defined(EXITFREE) || defined(PROTO)
8736 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008737free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00008738{
8739 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008740 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008741
8742 vim_free(globaldir);
8743 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008744}
8745#endif
8746
Bram Moolenaarf4258302013-06-02 18:20:17 +02008747/*
8748 * Deal with the side effects of changing the current directory.
8749 * When "local" is TRUE then this was after an ":lcd" command.
8750 */
8751 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008752post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008753{
8754 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008755 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008756 if (local)
8757 {
8758 /* If still in global directory, need to remember current
8759 * directory as global directory. */
8760 if (globaldir == NULL && prev_dir != NULL)
8761 globaldir = vim_strsave(prev_dir);
8762 /* Remember this local directory for the window. */
8763 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8764 curwin->w_localdir = vim_strsave(NameBuff);
8765 }
8766 else
8767 {
8768 /* We are now in the global directory, no need to remember its
8769 * name. */
8770 vim_free(globaldir);
8771 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008772 }
8773
8774 shorten_fnames(TRUE);
8775}
8776
Bram Moolenaarea408852005-06-25 22:49:46 +00008777
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778/*
8779 * ":cd", ":lcd", ":chdir" and ":lchdir".
8780 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008781 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008782ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 char_u *new_dir;
8785 char_u *tofree;
8786
8787 new_dir = eap->arg;
8788#if !defined(UNIX) && !defined(VMS)
8789 /* for non-UNIX ":cd" means: print current directory */
8790 if (*new_dir == NUL)
8791 ex_pwd(NULL);
8792 else
8793#endif
8794 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008795#ifdef FEAT_AUTOCMD
8796 if (allbuf_locked())
8797 return;
8798#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008799 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8800 && !eap->forceit)
8801 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008802 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008803 return;
8804 }
8805
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 /* ":cd -": Change to previous directory */
8807 if (STRCMP(new_dir, "-") == 0)
8808 {
8809 if (prev_dir == NULL)
8810 {
8811 EMSG(_("E186: No previous directory"));
8812 return;
8813 }
8814 new_dir = prev_dir;
8815 }
8816
8817 /* Save current directory for next ":cd -" */
8818 tofree = prev_dir;
8819 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8820 prev_dir = vim_strsave(NameBuff);
8821 else
8822 prev_dir = NULL;
8823
8824#if defined(UNIX) || defined(VMS)
8825 /* for UNIX ":cd" means: go to home directory */
8826 if (*new_dir == NUL)
8827 {
8828 /* use NameBuff for home directory name */
8829# ifdef VMS
8830 char_u *p;
8831
8832 p = mch_getenv((char_u *)"SYS$LOGIN");
8833 if (p == NULL || *p == NUL) /* empty is the same as not set */
8834 NameBuff[0] = NUL;
8835 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008836 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837# else
8838 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8839# endif
8840 new_dir = NameBuff;
8841 }
8842#endif
8843 if (new_dir == NULL || vim_chdir(new_dir))
8844 EMSG(_(e_failed));
8845 else
8846 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008847 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848
8849 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008850 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 ex_pwd(eap);
8852 }
8853 vim_free(tofree);
8854 }
8855}
8856
8857/*
8858 * ":pwd".
8859 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008861ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862{
8863 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8864 {
8865#ifdef BACKSLASH_IN_FILENAME
8866 slash_adjust(NameBuff);
8867#endif
8868 msg(NameBuff);
8869 }
8870 else
8871 EMSG(_("E187: Unknown"));
8872}
8873
8874/*
8875 * ":=".
8876 */
8877 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008878ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879{
8880 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008881 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882}
8883
8884 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008885ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008887 int n;
8888 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889
8890 if (cursor_valid())
8891 {
8892 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8893 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01008894 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008896
8897 len = eap->line2;
8898 switch (*eap->arg)
8899 {
8900 case 'm': break;
8901 case NUL: len *= 1000L; break;
8902 default: EMSG2(_(e_invarg2), eap->arg); return;
8903 }
8904 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905}
8906
8907/*
8908 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8909 */
8910 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008911do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912{
8913 long done;
8914
8915 cursor_on();
8916 out_flush();
8917 for (done = 0; !got_int && done < msec; done += 1000L)
8918 {
8919 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8920 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008921#ifdef MESSAGE_QUEUE
8922 /* Process the netbeans and clientserver messages that may have been
8923 * received in the call to ui_breakcheck() when the GUI is in use. This
8924 * may occur when running a test case. */
8925 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 }
8928}
8929
8930 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008931do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932{
8933 int mode;
8934 char_u *cmdp;
8935
8936 cmdp = eap->cmd;
8937 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8938
8939 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8940 eap->arg, mode, isabbrev))
8941 {
8942 case 1: EMSG(_(e_invarg));
8943 break;
8944 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8945 break;
8946 }
8947}
8948
8949/*
8950 * ":winsize" command (obsolete).
8951 */
8952 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008953ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954{
8955 int w, h;
8956 char_u *arg = eap->arg;
8957 char_u *p;
8958
8959 w = getdigits(&arg);
8960 arg = skipwhite(arg);
8961 p = arg;
8962 h = getdigits(&arg);
8963 if (*p != NUL && *arg == NUL)
8964 set_shellsize(w, h, TRUE);
8965 else
8966 EMSG(_("E465: :winsize requires two number arguments"));
8967}
8968
8969#ifdef FEAT_WINDOWS
8970 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008971ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972{
8973 int xchar = NUL;
8974 char_u *p;
8975
8976 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8977 {
8978 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8979 if (eap->arg[1] == NUL)
8980 {
8981 EMSG(_(e_invarg));
8982 return;
8983 }
8984 xchar = eap->arg[1];
8985 p = eap->arg + 2;
8986 }
8987 else
8988 p = eap->arg + 1;
8989
8990 eap->nextcmd = check_nextcmd(p);
8991 p = skipwhite(p);
8992 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8993 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008994 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 {
8996 /* Pass flags on for ":vertical wincmd ]". */
8997 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008998 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9000 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009001 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 }
9003}
9004#endif
9005
Bram Moolenaar843ee412004-06-30 16:16:41 +00009006#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007/*
9008 * ":winpos".
9009 */
9010 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009011ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012{
9013 int x, y;
9014 char_u *arg = eap->arg;
9015 char_u *p;
9016
9017 if (*arg == NUL)
9018 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009019# if defined(FEAT_GUI) || defined(MSWIN)
9020# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009022# else
9023 if (mch_get_winpos(&x, &y) != FAIL)
9024# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 {
9026 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9027 msg(IObuff);
9028 }
9029 else
9030# endif
9031 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9032 }
9033 else
9034 {
9035 x = getdigits(&arg);
9036 arg = skipwhite(arg);
9037 p = arg;
9038 y = getdigits(&arg);
9039 if (*p == NUL || *arg != NUL)
9040 {
9041 EMSG(_("E466: :winpos requires two number arguments"));
9042 return;
9043 }
9044# ifdef FEAT_GUI
9045 if (gui.in_use)
9046 gui_mch_set_winpos(x, y);
9047 else if (gui.starting)
9048 {
9049 /* Remember the coordinates for when the window is opened. */
9050 gui_win_x = x;
9051 gui_win_y = y;
9052 }
9053# ifdef HAVE_TGETENT
9054 else
9055# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009056# else
9057# ifdef MSWIN
9058 mch_set_winpos(x, y);
9059# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060# endif
9061# ifdef HAVE_TGETENT
9062 if (*T_CWP)
9063 term_set_winpos(x, y);
9064# endif
9065 }
9066}
9067#endif
9068
9069/*
9070 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9071 */
9072 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009073ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074{
9075 oparg_T oa;
9076
9077 clear_oparg(&oa);
9078 oa.regname = eap->regname;
9079 oa.start.lnum = eap->line1;
9080 oa.end.lnum = eap->line2;
9081 oa.line_count = eap->line2 - eap->line1 + 1;
9082 oa.motion_type = MLINE;
9083#ifdef FEAT_VIRTUALEDIT
9084 virtual_op = FALSE;
9085#endif
9086 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9087 {
9088 setpcmark();
9089 curwin->w_cursor.lnum = eap->line1;
9090 beginline(BL_SOL | BL_FIX);
9091 }
9092
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009093 if (VIsual_active)
9094 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009095
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 switch (eap->cmdidx)
9097 {
9098 case CMD_delete:
9099 oa.op_type = OP_DELETE;
9100 op_delete(&oa);
9101 break;
9102
9103 case CMD_yank:
9104 oa.op_type = OP_YANK;
9105 (void)op_yank(&oa, FALSE, TRUE);
9106 break;
9107
9108 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009109 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009111 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9112#else
9113 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009115 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116 oa.op_type = OP_RSHIFT;
9117 else
9118 oa.op_type = OP_LSHIFT;
9119 op_shift(&oa, FALSE, eap->amount);
9120 break;
9121 }
9122#ifdef FEAT_VIRTUALEDIT
9123 virtual_op = MAYBE;
9124#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009125 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126}
9127
9128/*
9129 * ":put".
9130 */
9131 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009132ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133{
9134 /* ":0put" works like ":1put!". */
9135 if (eap->line2 == 0)
9136 {
9137 eap->line2 = 1;
9138 eap->forceit = TRUE;
9139 }
9140 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009141 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9142 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143}
9144
9145/*
9146 * Handle ":copy" and ":move".
9147 */
9148 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009149ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150{
9151 long n;
9152
Bram Moolenaaraa23b372015-09-08 18:46:31 +02009153 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 if (eap->arg == NULL) /* error detected */
9155 {
9156 eap->nextcmd = NULL;
9157 return;
9158 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009159 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160
9161 /*
9162 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9163 */
9164 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9165 {
9166 EMSG(_(e_invaddr));
9167 return;
9168 }
9169
9170 if (eap->cmdidx == CMD_move)
9171 {
9172 if (do_move(eap->line1, eap->line2, n) == FAIL)
9173 return;
9174 }
9175 else
9176 ex_copy(eap->line1, eap->line2, n);
9177 u_clearline();
9178 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009179 ex_may_print(eap);
9180}
9181
9182/*
9183 * Print the current line if flags were given to the Ex command.
9184 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009185 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009186ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009187{
9188 if (eap->flags != 0)
9189 {
9190 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9191 (eap->flags & EXFLAG_LIST));
9192 ex_no_reprint = TRUE;
9193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194}
9195
9196/*
9197 * ":smagic" and ":snomagic".
9198 */
9199 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009200ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201{
9202 int magic_save = p_magic;
9203
9204 p_magic = (eap->cmdidx == CMD_smagic);
9205 do_sub(eap);
9206 p_magic = magic_save;
9207}
9208
9209/*
9210 * ":join".
9211 */
9212 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009213ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214{
9215 curwin->w_cursor.lnum = eap->line1;
9216 if (eap->line1 == eap->line2)
9217 {
9218 if (eap->addr_count >= 2) /* :2,2join does nothing */
9219 return;
9220 if (eap->line2 == curbuf->b_ml.ml_line_count)
9221 {
9222 beep_flush();
9223 return;
9224 }
9225 ++eap->line2;
9226 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009227 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009229 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230}
9231
9232/*
9233 * ":[addr]@r" or ":[addr]*r": execute register
9234 */
9235 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009236ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237{
9238 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009239 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240
9241 curwin->w_cursor.lnum = eap->line2;
9242
9243#ifdef USE_ON_FLY_SCROLL
9244 dont_scroll = TRUE; /* disallow scrolling here */
9245#endif
9246
9247 /* get the register name. No name means to use the previous one */
9248 c = *eap->arg;
9249 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9250 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009251 /* Put the register in the typeahead buffer with the "silent" flag. */
9252 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9253 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009254 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 else
9258 {
9259 int save_efr = exec_from_reg;
9260
9261 exec_from_reg = TRUE;
9262
9263 /*
9264 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009265 * Continue until the stuff buffer is empty and all added characters
9266 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009268 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9270
9271 exec_from_reg = save_efr;
9272 }
9273}
9274
9275/*
9276 * ":!".
9277 */
9278 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009279ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280{
9281 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9282}
9283
9284/*
9285 * ":undo".
9286 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009288ex_undo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009290 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009291 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009292 else
9293 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294}
9295
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009296#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009297 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009298ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009299{
9300 char_u hash[UNDO_HASH_SIZE];
9301
9302 u_compute_hash(hash);
9303 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9304}
9305
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009306 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009307ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009308{
9309 char_u hash[UNDO_HASH_SIZE];
9310
9311 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009312 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009313}
9314#endif
9315
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316/*
9317 * ":redo".
9318 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009320ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321{
9322 u_redo(1);
9323}
9324
9325/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009326 * ":earlier" and ":later".
9327 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009328 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009329ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009330{
9331 long count = 0;
9332 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009333 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009334 char_u *p = eap->arg;
9335
9336 if (*p == NUL)
9337 count = 1;
9338 else if (isdigit(*p))
9339 {
9340 count = getdigits(&p);
9341 switch (*p)
9342 {
9343 case 's': ++p; sec = TRUE; break;
9344 case 'm': ++p; sec = TRUE; count *= 60; break;
9345 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009346 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9347 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009348 }
9349 }
9350
9351 if (*p != NUL)
9352 EMSG2(_(e_invarg2), eap->arg);
9353 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009354 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9355 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009356}
9357
9358/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 * ":redir": start/stop redirection.
9360 */
9361 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009362ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363{
9364 char *mode;
9365 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009366 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367
9368 if (STRICMP(eap->arg, "END") == 0)
9369 close_redir();
9370 else
9371 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009372 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009374 ++arg;
9375 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009377 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 mode = "a";
9379 }
9380 else
9381 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009382 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383
9384 close_redir();
9385
9386 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009387 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 if (fname == NULL)
9389 return;
9390#ifdef FEAT_BROWSE
9391 if (cmdmod.browse)
9392 {
9393 char_u *browseFile;
9394
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009395 browseFile = do_browse(BROWSE_SAVE,
9396 (char_u *)_("Save Redirection"),
9397 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 if (browseFile == NULL)
9399 return; /* operation cancelled */
9400 vim_free(fname);
9401 fname = browseFile;
9402 eap->forceit = TRUE; /* since dialog already asked */
9403 }
9404#endif
9405
9406 redir_fd = open_exfile(fname, eap->forceit, mode);
9407 vim_free(fname);
9408 }
9409#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009410 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 {
9412 /* redirect to a register a-z (resp. A-Z for appending) */
9413 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009414 ++arg;
9415 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009417 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009418 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009420 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009422 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009423 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009424 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009425 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009427 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009428 if (*arg == '>')
9429 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009430 /* Make register empty when not using @A-@Z and the
9431 * command is valid. */
9432 if (*arg == NUL && !isupper(redir_reg))
9433 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009435 }
9436 if (*arg != NUL)
9437 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009438 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009439 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009440 }
9441 }
9442 else if (*arg == '=' && arg[1] == '>')
9443 {
9444 int append;
9445
9446 /* redirect to a variable */
9447 close_redir();
9448 arg += 2;
9449
9450 if (*arg == '>')
9451 {
9452 ++arg;
9453 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 }
9455 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009456 append = FALSE;
9457
9458 if (var_redir_start(skipwhite(arg), append) == OK)
9459 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 }
9461#endif
9462
9463 /* TODO: redirect to a buffer */
9464
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009466 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009468
9469 /* Make sure redirection is not off. Can happen for cmdline completion
9470 * that indirectly invokes a command to catch its output. */
9471 if (redir_fd != NULL
9472#ifdef FEAT_EVAL
9473 || redir_reg || redir_vname
9474#endif
9475 )
9476 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477}
9478
9479/*
9480 * ":redraw": force redraw
9481 */
9482 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009483ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484{
9485 int r = RedrawingDisabled;
9486 int p = p_lz;
9487
9488 RedrawingDisabled = 0;
9489 p_lz = FALSE;
9490 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009491 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492#ifdef FEAT_TITLE
9493 if (need_maketitle)
9494 maketitle();
9495#endif
9496 RedrawingDisabled = r;
9497 p_lz = p;
9498
9499 /* Reset msg_didout, so that a message that's there is overwritten. */
9500 msg_didout = FALSE;
9501 msg_col = 0;
9502
Bram Moolenaar56667a52013-07-17 11:54:28 +02009503 /* No need to wait after an intentional redraw. */
9504 need_wait_return = FALSE;
9505
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 out_flush();
9507}
9508
9509/*
9510 * ":redrawstatus": force redraw of status line(s)
9511 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009513ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514{
9515#if defined(FEAT_WINDOWS)
9516 int r = RedrawingDisabled;
9517 int p = p_lz;
9518
9519 RedrawingDisabled = 0;
9520 p_lz = FALSE;
9521 if (eap->forceit)
9522 status_redraw_all();
9523 else
9524 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009525 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 RedrawingDisabled = r;
9527 p_lz = p;
9528 out_flush();
9529#endif
9530}
9531
9532 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009533close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534{
9535 if (redir_fd != NULL)
9536 {
9537 fclose(redir_fd);
9538 redir_fd = NULL;
9539 }
9540#ifdef FEAT_EVAL
9541 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009542 if (redir_vname)
9543 {
9544 var_redir_stop();
9545 redir_vname = 0;
9546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547#endif
9548}
9549
9550#if defined(FEAT_SESSION) && defined(USE_CRNL)
9551# define MKSESSION_NL
9552static int mksession_nl = FALSE; /* use NL only in put_eol() */
9553#endif
9554
9555/*
9556 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9557 */
9558 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009559ex_mkrc(
9560 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561{
9562 FILE *fd;
9563 int failed = FALSE;
9564 char_u *fname;
9565#ifdef FEAT_BROWSE
9566 char_u *browseFile = NULL;
9567#endif
9568#ifdef FEAT_SESSION
9569 int view_session = FALSE;
9570 int using_vdir = FALSE; /* using 'viewdir'? */
9571 char_u *viewFile = NULL;
9572 unsigned *flagp;
9573#endif
9574
9575 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9576 {
9577#ifdef FEAT_SESSION
9578 view_session = TRUE;
9579#else
9580 ex_ni(eap);
9581 return;
9582#endif
9583 }
9584
9585#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009586 /* Use the short file name until ":lcd" is used. We also don't use the
9587 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009588 did_lcd = FALSE;
9589
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9591 if (eap->cmdidx == CMD_mkview
9592 && (*eap->arg == NUL
9593 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9594 {
9595 eap->forceit = TRUE;
9596 fname = get_view_file(*eap->arg);
9597 if (fname == NULL)
9598 return;
9599 viewFile = fname;
9600 using_vdir = TRUE;
9601 }
9602 else
9603#endif
9604 if (*eap->arg != NUL)
9605 fname = eap->arg;
9606 else if (eap->cmdidx == CMD_mkvimrc)
9607 fname = (char_u *)VIMRC_FILE;
9608#ifdef FEAT_SESSION
9609 else if (eap->cmdidx == CMD_mksession)
9610 fname = (char_u *)SESSION_FILE;
9611#endif
9612 else
9613 fname = (char_u *)EXRC_FILE;
9614
9615#ifdef FEAT_BROWSE
9616 if (cmdmod.browse)
9617 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009618 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619# ifdef FEAT_SESSION
9620 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9621 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9622# endif
9623 (char_u *)_("Save Setup"),
9624 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9625 if (browseFile == NULL)
9626 goto theend;
9627 fname = browseFile;
9628 eap->forceit = TRUE; /* since dialog already asked */
9629 }
9630#endif
9631
9632#if defined(FEAT_SESSION) && defined(vim_mkdir)
9633 /* When using 'viewdir' may have to create the directory. */
9634 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009635 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636#endif
9637
9638 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9639 if (fd != NULL)
9640 {
9641#ifdef FEAT_SESSION
9642 if (eap->cmdidx == CMD_mkview)
9643 flagp = &vop_flags;
9644 else
9645 flagp = &ssop_flags;
9646#endif
9647
9648#ifdef MKSESSION_NL
9649 /* "unix" in 'sessionoptions': use NL line separator */
9650 if (view_session && (*flagp & SSOP_UNIX))
9651 mksession_nl = TRUE;
9652#endif
9653
9654 /* Write the version command for :mkvimrc */
9655 if (eap->cmdidx == CMD_mkvimrc)
9656 (void)put_line(fd, "version 6.0");
9657
9658#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009659 if (eap->cmdidx == CMD_mksession)
9660 {
9661 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9662 failed = TRUE;
9663 }
9664
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 if (eap->cmdidx != CMD_mkview)
9666#endif
9667 {
9668 /* Write setting 'compatible' first, because it has side effects.
9669 * For that same reason only do it when needed. */
9670 if (p_cp)
9671 (void)put_line(fd, "if !&cp | set cp | endif");
9672 else
9673 (void)put_line(fd, "if &cp | set nocp | endif");
9674 }
9675
9676#ifdef FEAT_SESSION
9677 if (!view_session
9678 || (eap->cmdidx == CMD_mksession
9679 && (*flagp & SSOP_OPTIONS)))
9680#endif
9681 failed |= (makemap(fd, NULL) == FAIL
9682 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9683
9684#ifdef FEAT_SESSION
9685 if (!failed && view_session)
9686 {
9687 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9688 failed = TRUE;
9689 if (eap->cmdidx == CMD_mksession)
9690 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009691 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692
Bram Moolenaard9462e32011-04-11 21:35:11 +02009693 dirnow = alloc(MAXPATHL);
9694 if (dirnow == NULL)
9695 failed = TRUE;
9696 else
9697 {
9698 /*
9699 * Change to session file's dir.
9700 */
9701 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009703 *dirnow = NUL;
9704 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9705 {
9706 if (vim_chdirfile(fname) == OK)
9707 shorten_fnames(TRUE);
9708 }
9709 else if (*dirnow != NUL
9710 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9711 {
9712 if (mch_chdir((char *)globaldir) == 0)
9713 shorten_fnames(TRUE);
9714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715
Bram Moolenaard9462e32011-04-11 21:35:11 +02009716 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717
Bram Moolenaard9462e32011-04-11 21:35:11 +02009718 /* restore original dir */
9719 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009721 {
9722 if (mch_chdir((char *)dirnow) != 0)
9723 EMSG(_(e_prev_dir));
9724 shorten_fnames(TRUE);
9725 }
9726 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 }
9728 }
9729 else
9730 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009731 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9732 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 }
9734 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9735 == FAIL)
9736 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009737 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9738 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009739 if (eap->cmdidx == CMD_mksession)
9740 {
9741 if (put_line(fd, "unlet SessionLoad") == FAIL)
9742 failed = TRUE;
9743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 }
9745#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009746 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9747 failed = TRUE;
9748
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 failed |= fclose(fd);
9750
9751 if (failed)
9752 EMSG(_(e_write));
9753#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9754 else if (eap->cmdidx == CMD_mksession)
9755 {
9756 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009757 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758
Bram Moolenaard9462e32011-04-11 21:35:11 +02009759 tbuf = alloc(MAXPATHL);
9760 if (tbuf != NULL)
9761 {
9762 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9763 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9764 vim_free(tbuf);
9765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 }
9767#endif
9768#ifdef MKSESSION_NL
9769 mksession_nl = FALSE;
9770#endif
9771 }
9772
9773#ifdef FEAT_BROWSE
9774theend:
9775 vim_free(browseFile);
9776#endif
9777#ifdef FEAT_SESSION
9778 vim_free(viewFile);
9779#endif
9780}
9781
Bram Moolenaardf177f62005-02-22 08:39:57 +00009782#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9783 || defined(PROTO)
9784 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009785vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009786{
9787 if (vim_mkdir(name, prot) != 0)
9788 {
9789 EMSG2(_("E739: Cannot create directory: %s"), name);
9790 return FAIL;
9791 }
9792 return OK;
9793}
9794#endif
9795
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796/*
9797 * Open a file for writing for an Ex command, with some checks.
9798 * Return file descriptor, or NULL on failure.
9799 */
9800 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009801open_exfile(
9802 char_u *fname,
9803 int forceit,
9804 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805{
9806 FILE *fd;
9807
9808#ifdef UNIX
9809 /* with Unix it is possible to open a directory */
9810 if (mch_isdir(fname))
9811 {
9812 EMSG2(_(e_isadir2), fname);
9813 return NULL;
9814 }
9815#endif
9816 if (!forceit && *mode != 'a' && vim_fexists(fname))
9817 {
9818 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9819 return NULL;
9820 }
9821
9822 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9823 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9824
9825 return fd;
9826}
9827
9828/*
9829 * ":mark" and ":k".
9830 */
9831 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009832ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833{
9834 pos_T pos;
9835
9836 if (*eap->arg == NUL) /* No argument? */
9837 EMSG(_(e_argreq));
9838 else if (eap->arg[1] != NUL) /* more than one character? */
9839 EMSG(_(e_trailing));
9840 else
9841 {
9842 pos = curwin->w_cursor; /* save curwin->w_cursor */
9843 curwin->w_cursor.lnum = eap->line2;
9844 beginline(BL_WHITE | BL_FIX);
9845 if (setmark(*eap->arg) == FAIL) /* set mark */
9846 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9847 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9848 }
9849}
9850
9851/*
9852 * Update w_topline, w_leftcol and the cursor position.
9853 */
9854 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009855update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856{
9857 check_cursor(); /* put cursor on valid line */
9858 update_topline();
9859 if (!curwin->w_p_wrap)
9860 validate_cursor();
9861 update_curswant();
9862}
9863
9864#ifdef FEAT_EX_EXTRA
9865/*
9866 * ":normal[!] {commands}": Execute normal mode commands.
9867 */
9868 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009869ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 int save_msg_scroll = msg_scroll;
9872 int save_restart_edit = restart_edit;
9873 int save_msg_didout = msg_didout;
9874 int save_State = State;
9875 tasave_T tabuf;
9876 int save_insertmode = p_im;
9877 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009878 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879#ifdef FEAT_MBYTE
9880 char_u *arg = NULL;
9881 int l;
9882 char_u *p;
9883#endif
9884
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009885 if (ex_normal_lock > 0)
9886 {
9887 EMSG(_(e_secure));
9888 return;
9889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 if (ex_normal_busy >= p_mmd)
9891 {
9892 EMSG(_("E192: Recursive use of :normal too deep"));
9893 return;
9894 }
9895 ++ex_normal_busy;
9896
9897 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9898 restart_edit = 0; /* don't go to Insert mode */
9899 p_im = FALSE; /* don't use 'insertmode' */
9900
9901#ifdef FEAT_MBYTE
9902 /*
9903 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9904 * this for the K_SPECIAL leading byte, otherwise special keys will not
9905 * work.
9906 */
9907 if (has_mbyte)
9908 {
9909 int len = 0;
9910
9911 /* Count the number of characters to be escaped. */
9912 for (p = eap->arg; *p != NUL; ++p)
9913 {
9914# ifdef FEAT_GUI
9915 if (*p == CSI) /* leadbyte CSI */
9916 len += 2;
9917# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009918 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9920# ifdef FEAT_GUI
9921 || *p == CSI
9922# endif
9923 )
9924 len += 2;
9925 }
9926 if (len > 0)
9927 {
9928 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9929 if (arg != NULL)
9930 {
9931 len = 0;
9932 for (p = eap->arg; *p != NUL; ++p)
9933 {
9934 arg[len++] = *p;
9935# ifdef FEAT_GUI
9936 if (*p == CSI)
9937 {
9938 arg[len++] = KS_EXTRA;
9939 arg[len++] = (int)KE_CSI;
9940 }
9941# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009942 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 {
9944 arg[len++] = *++p;
9945 if (*p == K_SPECIAL)
9946 {
9947 arg[len++] = KS_SPECIAL;
9948 arg[len++] = KE_FILLER;
9949 }
9950# ifdef FEAT_GUI
9951 else if (*p == CSI)
9952 {
9953 arg[len++] = KS_EXTRA;
9954 arg[len++] = (int)KE_CSI;
9955 }
9956# endif
9957 }
9958 arg[len] = NUL;
9959 }
9960 }
9961 }
9962 }
9963#endif
9964
9965 /*
9966 * Save the current typeahead. This is required to allow using ":normal"
9967 * from an event handler and makes sure we don't hang when the argument
9968 * ends with half a command.
9969 */
9970 save_typeahead(&tabuf);
9971 if (tabuf.typebuf_valid)
9972 {
9973 /*
9974 * Repeat the :normal command for each line in the range. When no
9975 * range given, execute it just once, without positioning the cursor
9976 * first.
9977 */
9978 do
9979 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 if (eap->addr_count != 0)
9981 {
9982 curwin->w_cursor.lnum = eap->line1++;
9983 curwin->w_cursor.col = 0;
9984 }
9985
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009986 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987#ifdef FEAT_MBYTE
9988 arg != NULL ? arg :
9989#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009990 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991 }
9992 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9993 }
9994
9995 /* Might not return to the main loop when in an event handler. */
9996 update_topline_cursor();
9997
9998 /* Restore the previous typeahead. */
9999 restore_typeahead(&tabuf);
10000
10001 --ex_normal_busy;
10002 msg_scroll = save_msg_scroll;
10003 restart_edit = save_restart_edit;
10004 p_im = save_insertmode;
10005 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010006 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
10008
10009 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +010010010 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +010010012#ifdef FEAT_MOUSE
10013 setmouse();
10014#endif
10015#ifdef CURSOR_SHAPE
10016 ui_cursor_shape(); /* may show different cursor shape */
10017#endif
10018
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019#ifdef FEAT_MBYTE
10020 vim_free(arg);
10021#endif
10022}
10023
10024/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010025 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 */
10027 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010028ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010030 if (eap->forceit)
10031 {
10032 coladvance((colnr_T)MAXCOL);
10033 curwin->w_curswant = MAXCOL;
10034 curwin->w_set_curswant = FALSE;
10035 }
10036
Bram Moolenaara40c5002005-01-09 21:16:21 +000010037 /* Ignore the command when already in Insert mode. Inserting an
10038 * expression register that invokes a function can do this. */
10039 if (State & INSERT)
10040 return;
10041
Bram Moolenaar64969662005-12-14 21:59:55 +000010042 if (eap->cmdidx == CMD_startinsert)
10043 restart_edit = 'a';
10044 else if (eap->cmdidx == CMD_startreplace)
10045 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010047 restart_edit = 'V';
10048
10049 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010051 if (eap->cmdidx == CMD_startinsert)
10052 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 curwin->w_curswant = 0; /* avoid MAXCOL */
10054 }
10055}
10056
10057/*
10058 * ":stopinsert"
10059 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010061ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062{
10063 restart_edit = 0;
10064 stop_insert_mode = TRUE;
10065}
10066#endif
10067
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010068#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
10069/*
10070 * Execute normal mode command "cmd".
10071 * "remap" can be REMAP_NONE or REMAP_YES.
10072 */
10073 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010074exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010075{
Bram Moolenaar25281632016-01-21 23:32:32 +010010076 /* Stuff the argument into the typeahead buffer. */
10077 ins_typebuf(cmd, remap, 0, TRUE, silent);
10078 exec_normal(FALSE);
10079}
10080#endif
10081
10082#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(FEAT_EVAL) \
10083 || defined(PROTO)
10084/*
10085 * Execute normal_cmd() until there is no typeahead left.
10086 */
10087 void
10088exec_normal(int was_typed)
10089{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010090 oparg_T oa;
10091
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010092 clear_oparg(&oa);
10093 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010094 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10095 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010096 {
10097 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010098 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010099 }
10100}
10101#endif
10102
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103#ifdef FEAT_FIND_ID
10104 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010105ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106{
10107 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10108 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10109 (linenr_T)1, (linenr_T)MAXLNUM);
10110}
10111
10112#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
10113/*
10114 * ":psearch"
10115 */
10116 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010117ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118{
10119 g_do_tagpreview = p_pvh;
10120 ex_findpat(eap);
10121 g_do_tagpreview = 0;
10122}
10123#endif
10124
10125 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010126ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127{
10128 int whole = TRUE;
10129 long n;
10130 char_u *p;
10131 int action;
10132
10133 switch (cmdnames[eap->cmdidx].cmd_name[2])
10134 {
10135 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10136 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10137 action = ACTION_GOTO;
10138 else
10139 action = ACTION_SHOW;
10140 break;
10141 case 'i': /* ":ilist" and ":dlist" */
10142 action = ACTION_SHOW_ALL;
10143 break;
10144 case 'u': /* ":ijump" and ":djump" */
10145 action = ACTION_GOTO;
10146 break;
10147 default: /* ":isplit" and ":dsplit" */
10148 action = ACTION_SPLIT;
10149 break;
10150 }
10151
10152 n = 1;
10153 if (vim_isdigit(*eap->arg)) /* get count */
10154 {
10155 n = getdigits(&eap->arg);
10156 eap->arg = skipwhite(eap->arg);
10157 }
10158 if (*eap->arg == '/') /* Match regexp, not just whole words */
10159 {
10160 whole = FALSE;
10161 ++eap->arg;
10162 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10163 if (*p)
10164 {
10165 *p++ = NUL;
10166 p = skipwhite(p);
10167
10168 /* Check for trailing illegal characters */
10169 if (!ends_excmd(*p))
10170 eap->errmsg = e_trailing;
10171 else
10172 eap->nextcmd = check_nextcmd(p);
10173 }
10174 }
10175 if (!eap->skip)
10176 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10177 whole, !eap->forceit,
10178 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10179 n, action, eap->line1, eap->line2);
10180}
10181#endif
10182
10183#ifdef FEAT_WINDOWS
10184
10185# ifdef FEAT_QUICKFIX
10186/*
10187 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10188 */
10189 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010190ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010192 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10194}
10195
10196/*
10197 * ":pedit"
10198 */
10199 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010200ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201{
10202 win_T *curwin_save = curwin;
10203
10204 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010205 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 keep_help_flag = curwin_save->w_buffer->b_help;
10207 do_exedit(eap, NULL);
10208 keep_help_flag = FALSE;
10209 if (curwin != curwin_save && win_valid(curwin_save))
10210 {
10211 /* Return cursor to where we were */
10212 validate_cursor();
10213 redraw_later(VALID);
10214 win_enter(curwin_save, TRUE);
10215 }
10216 g_do_tagpreview = 0;
10217}
10218# endif
10219
10220/*
10221 * ":stag", ":stselect" and ":stjump".
10222 */
10223 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010224ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225{
10226 postponed_split = -1;
10227 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010228 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10230 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010231 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232}
10233#endif
10234
10235/*
10236 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10237 */
10238 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010239ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240{
10241 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10242}
10243
10244 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010245ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246{
10247 int cmd;
10248
10249 switch (name[1])
10250 {
10251 case 'j': cmd = DT_JUMP; /* ":tjump" */
10252 break;
10253 case 's': cmd = DT_SELECT; /* ":tselect" */
10254 break;
10255 case 'p': cmd = DT_PREV; /* ":tprevious" */
10256 break;
10257 case 'N': cmd = DT_PREV; /* ":tNext" */
10258 break;
10259 case 'n': cmd = DT_NEXT; /* ":tnext" */
10260 break;
10261 case 'o': cmd = DT_POP; /* ":pop" */
10262 break;
10263 case 'f': /* ":tfirst" */
10264 case 'r': cmd = DT_FIRST; /* ":trewind" */
10265 break;
10266 case 'l': cmd = DT_LAST; /* ":tlast" */
10267 break;
10268 default: /* ":tag" */
10269#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010270 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271 {
10272 do_cstag(eap);
10273 return;
10274 }
10275#endif
10276 cmd = DT_TAG;
10277 break;
10278 }
10279
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010280 if (name[0] == 'l')
10281 {
10282#ifndef FEAT_QUICKFIX
10283 ex_ni(eap);
10284 return;
10285#else
10286 cmd = DT_LTAG;
10287#endif
10288 }
10289
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10291 eap->forceit, TRUE);
10292}
10293
10294/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010295 * Check "str" for starting with a special cmdline variable.
10296 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10297 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10298 */
10299 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010300find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010301{
10302 int len;
10303 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010304 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010305 "%",
10306#define SPEC_PERC 0
10307 "#",
10308#define SPEC_HASH 1
10309 "<cword>", /* cursor word */
10310#define SPEC_CWORD 2
10311 "<cWORD>", /* cursor WORD */
10312#define SPEC_CCWORD 3
10313 "<cfile>", /* cursor path name */
10314#define SPEC_CFILE 4
10315 "<sfile>", /* ":so" file name */
10316#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010317 "<slnum>", /* ":so" file line number */
10318#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010319#ifdef FEAT_AUTOCMD
10320 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010321# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010322 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010323# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010324 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010325# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010326#endif
10327#ifdef FEAT_CLIENTSERVER
10328 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010329# ifdef FEAT_AUTOCMD
10330# define SPEC_CLIENT 10
10331# else
10332# define SPEC_CLIENT 7
10333# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010334#endif
10335 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010336
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010337 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010338 {
10339 len = (int)STRLEN(spec_str[i]);
10340 if (STRNCMP(src, spec_str[i], len) == 0)
10341 {
10342 *usedlen = len;
10343 return i;
10344 }
10345 }
10346 return -1;
10347}
10348
10349/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 * Evaluate cmdline variables.
10351 *
10352 * change '%' to curbuf->b_ffname
10353 * '#' to curwin->w_altfile
10354 * '<cword>' to word under the cursor
10355 * '<cWORD>' to WORD under the cursor
10356 * '<cfile>' to path name under the cursor
10357 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010358 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 * '<afile>' to file name for autocommand
10360 * '<abuf>' to buffer number for autocommand
10361 * '<amatch>' to matching name for autocommand
10362 *
10363 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10364 * "" for error without a message) and NULL is returned.
10365 * Returns an allocated string if a valid match was found.
10366 * Returns NULL if no match was found. "usedlen" then still contains the
10367 * number of characters to skip.
10368 */
10369 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010370eval_vars(
10371 char_u *src, /* pointer into commandline */
10372 char_u *srcstart, /* beginning of valid memory for src */
10373 int *usedlen, /* characters after src that are used */
10374 linenr_T *lnump, /* line number for :e command, or NULL */
10375 char_u **errormsg, /* pointer to error message */
10376 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010377 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378{
10379 int i;
10380 char_u *s;
10381 char_u *result;
10382 char_u *resultbuf = NULL;
10383 int resultlen;
10384 buf_T *buf;
10385 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10386 int spec_idx;
10387#ifdef FEAT_MODIFY_FNAME
10388 int skip_mod = FALSE;
10389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391
10392 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010393 if (escaped != NULL)
10394 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395
10396 /*
10397 * Check if there is something to do.
10398 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010399 spec_idx = find_cmdline_var(src, usedlen);
10400 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401 {
10402 *usedlen = 1;
10403 return NULL;
10404 }
10405
10406 /*
10407 * Skip when preceded with a backslash "\%" and "\#".
10408 * Note: In "\\%" the % is also not recognized!
10409 */
10410 if (src > srcstart && src[-1] == '\\')
10411 {
10412 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010413 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414 return NULL;
10415 }
10416
10417 /*
10418 * word or WORD under cursor
10419 */
10420 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10421 {
10422 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10423 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10424 if (resultlen == 0)
10425 {
10426 *errormsg = (char_u *)"";
10427 return NULL;
10428 }
10429 }
10430
10431 /*
10432 * '#': Alternate file name
10433 * '%': Current file name
10434 * File name under the cursor
10435 * File name for autocommand
10436 * and following modifiers
10437 */
10438 else
10439 {
10440 switch (spec_idx)
10441 {
10442 case SPEC_PERC: /* '%': current file */
10443 if (curbuf->b_fname == NULL)
10444 {
10445 result = (char_u *)"";
10446 valid = 0; /* Must have ":p:h" to be valid */
10447 }
10448 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450 break;
10451
10452 case SPEC_HASH: /* '#' or "#99": alternate file */
10453 if (src[1] == '#') /* "##": the argument list */
10454 {
10455 result = arg_all();
10456 resultbuf = result;
10457 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010458 if (escaped != NULL)
10459 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460#ifdef FEAT_MODIFY_FNAME
10461 skip_mod = TRUE;
10462#endif
10463 break;
10464 }
10465 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010466 if (*s == '<') /* "#<99" uses v:oldfiles */
10467 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 i = (int)getdigits(&s);
10469 *usedlen = (int)(s - src); /* length of what we expand */
10470
Bram Moolenaard812df62008-11-09 12:46:09 +000010471 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010473 if (*usedlen < 2)
10474 {
10475 /* Should we give an error message for #<text? */
10476 *usedlen = 1;
10477 return NULL;
10478 }
10479#ifdef FEAT_EVAL
10480 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10481 (long)i);
10482 if (result == NULL)
10483 {
10484 *errormsg = (char_u *)"";
10485 return NULL;
10486 }
10487#else
10488 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491 }
10492 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010493 {
10494 buf = buflist_findnr(i);
10495 if (buf == NULL)
10496 {
10497 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10498 return NULL;
10499 }
10500 if (lnump != NULL)
10501 *lnump = ECMD_LAST;
10502 if (buf->b_fname == NULL)
10503 {
10504 result = (char_u *)"";
10505 valid = 0; /* Must have ":p:h" to be valid */
10506 }
10507 else
10508 result = buf->b_fname;
10509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510 break;
10511
10512#ifdef FEAT_SEARCHPATH
10513 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010514 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 if (result == NULL)
10516 {
10517 *errormsg = (char_u *)"";
10518 return NULL;
10519 }
10520 resultbuf = result; /* remember allocated string */
10521 break;
10522#endif
10523
10524#ifdef FEAT_AUTOCMD
10525 case SPEC_AFILE: /* file name for autocommand */
10526 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010527 if (result != NULL && !autocmd_fname_full)
10528 {
10529 /* Still need to turn the fname into a full path. It is
10530 * postponed to avoid a delay when <afile> is not used. */
10531 autocmd_fname_full = TRUE;
10532 result = FullName_save(autocmd_fname, FALSE);
10533 vim_free(autocmd_fname);
10534 autocmd_fname = result;
10535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536 if (result == NULL)
10537 {
10538 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10539 return NULL;
10540 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010541 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542 break;
10543
10544 case SPEC_ABUF: /* buffer number for autocommand */
10545 if (autocmd_bufnr <= 0)
10546 {
10547 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10548 return NULL;
10549 }
10550 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10551 result = strbuf;
10552 break;
10553
10554 case SPEC_AMATCH: /* match name for autocommand */
10555 result = autocmd_match;
10556 if (result == NULL)
10557 {
10558 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10559 return NULL;
10560 }
10561 break;
10562
10563#endif
10564 case SPEC_SFILE: /* file name for ":so" command */
10565 result = sourcing_name;
10566 if (result == NULL)
10567 {
10568 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10569 return NULL;
10570 }
10571 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010572 case SPEC_SLNUM: /* line in file for ":so" command */
10573 if (sourcing_name == NULL || sourcing_lnum == 0)
10574 {
10575 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10576 return NULL;
10577 }
10578 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10579 result = strbuf;
10580 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581#if defined(FEAT_CLIENTSERVER)
10582 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010583 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10584 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 result = strbuf;
10586 break;
10587#endif
10588 }
10589
10590 resultlen = (int)STRLEN(result); /* length of new string */
10591 if (src[*usedlen] == '<') /* remove the file name extension */
10592 {
10593 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595 resultlen = (int)(s - result);
10596 }
10597#ifdef FEAT_MODIFY_FNAME
10598 else if (!skip_mod)
10599 {
10600 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10601 &resultlen);
10602 if (result == NULL)
10603 {
10604 *errormsg = (char_u *)"";
10605 return NULL;
10606 }
10607 }
10608#endif
10609 }
10610
10611 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10612 {
10613 if (valid != VALID_HEAD + VALID_PATH)
10614 /* xgettext:no-c-format */
10615 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10616 else
10617 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10618 result = NULL;
10619 }
10620 else
10621 result = vim_strnsave(result, resultlen);
10622 vim_free(resultbuf);
10623 return result;
10624}
10625
10626/*
10627 * Concatenate all files in the argument list, separated by spaces, and return
10628 * it in one allocated string.
10629 * Spaces and backslashes in the file names are escaped with a backslash.
10630 * Returns NULL when out of memory.
10631 */
10632 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010633arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634{
10635 int len;
10636 int idx;
10637 char_u *retval = NULL;
10638 char_u *p;
10639
10640 /*
10641 * Do this loop two times:
10642 * first time: compute the total length
10643 * second time: concatenate the names
10644 */
10645 for (;;)
10646 {
10647 len = 0;
10648 for (idx = 0; idx < ARGCOUNT; ++idx)
10649 {
10650 p = alist_name(&ARGLIST[idx]);
10651 if (p != NULL)
10652 {
10653 if (len > 0)
10654 {
10655 /* insert a space in between names */
10656 if (retval != NULL)
10657 retval[len] = ' ';
10658 ++len;
10659 }
10660 for ( ; *p != NUL; ++p)
10661 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010662 if (*p == ' '
10663#ifndef BACKSLASH_IN_FILENAME
10664 || *p == '\\'
10665#endif
10666 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667 {
10668 /* insert a backslash */
10669 if (retval != NULL)
10670 retval[len] = '\\';
10671 ++len;
10672 }
10673 if (retval != NULL)
10674 retval[len] = *p;
10675 ++len;
10676 }
10677 }
10678 }
10679
10680 /* second time: break here */
10681 if (retval != NULL)
10682 {
10683 retval[len] = NUL;
10684 break;
10685 }
10686
10687 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010688 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689 if (retval == NULL)
10690 break;
10691 }
10692
10693 return retval;
10694}
10695
10696#if defined(FEAT_AUTOCMD) || defined(PROTO)
10697/*
10698 * Expand the <sfile> string in "arg".
10699 *
10700 * Returns an allocated string, or NULL for any error.
10701 */
10702 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010703expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704{
10705 char_u *errormsg;
10706 int len;
10707 char_u *result;
10708 char_u *newres;
10709 char_u *repl;
10710 int srclen;
10711 char_u *p;
10712
10713 result = vim_strsave(arg);
10714 if (result == NULL)
10715 return NULL;
10716
10717 for (p = result; *p; )
10718 {
10719 if (STRNCMP(p, "<sfile>", 7) != 0)
10720 ++p;
10721 else
10722 {
10723 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010724 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725 if (errormsg != NULL)
10726 {
10727 if (*errormsg)
10728 emsg(errormsg);
10729 vim_free(result);
10730 return NULL;
10731 }
10732 if (repl == NULL) /* no match (cannot happen) */
10733 {
10734 p += srclen;
10735 continue;
10736 }
10737 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10738 newres = alloc(len);
10739 if (newres == NULL)
10740 {
10741 vim_free(repl);
10742 vim_free(result);
10743 return NULL;
10744 }
10745 mch_memmove(newres, result, (size_t)(p - result));
10746 STRCPY(newres + (p - result), repl);
10747 len = (int)STRLEN(newres);
10748 STRCAT(newres, p + srclen);
10749 vim_free(repl);
10750 vim_free(result);
10751 result = newres;
10752 p = newres + len; /* continue after the match */
10753 }
10754 }
10755
10756 return result;
10757}
10758#endif
10759
10760#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010010761static int ses_winsizes(FILE *fd, int restore_size,
10762 win_T *tab_firstwin);
10763static int ses_win_rec(FILE *fd, frame_T *fr);
10764static frame_T *ses_skipframe(frame_T *fr);
10765static int ses_do_frame(frame_T *fr);
10766static int ses_do_win(win_T *wp);
10767static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
10768static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
10769static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770
10771/*
10772 * Write openfile commands for the current buffers to an .exrc file.
10773 * Return FAIL on error, OK otherwise.
10774 */
10775 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010776makeopens(
10777 FILE *fd,
10778 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779{
10780 buf_T *buf;
10781 int only_save_windows = TRUE;
10782 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783 int restore_size = TRUE;
10784 win_T *wp;
10785 char_u *sname;
10786 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010787 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010788 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010789 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010790 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010791 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010792 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793
10794 if (ssop_flags & SSOP_BUFFERS)
10795 only_save_windows = FALSE; /* Save ALL buffers */
10796
10797 /*
10798 * Begin by setting the this_session variable, and then other
10799 * sessionable variables.
10800 */
10801#ifdef FEAT_EVAL
10802 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10803 return FAIL;
10804 if (ssop_flags & SSOP_GLOBALS)
10805 if (store_session_globals(fd) == FAIL)
10806 return FAIL;
10807#endif
10808
10809 /*
10810 * Close all windows but one.
10811 */
10812 if (put_line(fd, "silent only") == FAIL)
10813 return FAIL;
10814
10815 /*
10816 * Now a :cd command to the session directory or the current directory
10817 */
10818 if (ssop_flags & SSOP_SESDIR)
10819 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010820 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10821 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822 return FAIL;
10823 }
10824 else if (ssop_flags & SSOP_CURDIR)
10825 {
10826 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10827 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010828 || fputs("cd ", fd) < 0
10829 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10830 || put_eol(fd) == FAIL)
10831 {
10832 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835 vim_free(sname);
10836 }
10837
10838 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010839 * If there is an empty, unnamed buffer we will wipe it out later.
10840 * Remember the buffer number.
10841 */
10842 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10843 return FAIL;
10844 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10845 return FAIL;
10846 if (put_line(fd, "endif") == FAIL)
10847 return FAIL;
10848
10849 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850 * Now save the current files, current buffer first.
10851 */
10852 if (put_line(fd, "set shortmess=aoO") == FAIL)
10853 return FAIL;
10854
10855 /* Now put the other buffers into the buffer list */
10856 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10857 {
10858 if (!(only_save_windows && buf->b_nwindows == 0)
10859 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10860 && buf->b_fname != NULL
10861 && buf->b_p_bl)
10862 {
10863 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10864 : buf->b_wininfo->wi_fpos.lnum) < 0
10865 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10866 return FAIL;
10867 }
10868 }
10869
10870 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010871 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10873 return FAIL;
10874
10875 if (ssop_flags & SSOP_RESIZE)
10876 {
10877 /* Note: after the restore we still check it worked!*/
10878 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10879 || put_eol(fd) == FAIL)
10880 return FAIL;
10881 }
10882
10883#ifdef FEAT_GUI
10884 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10885 {
10886 int x, y;
10887
10888 if (gui_mch_get_winpos(&x, &y) == OK)
10889 {
10890 /* Note: after the restore we still check it worked!*/
10891 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10892 return FAIL;
10893 }
10894 }
10895#endif
10896
10897 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010898 * When there are two or more tabpages and 'showtabline' is 1 the tabline
10899 * will be displayed when creating the next tab. That resizes the windows
10900 * in the first tab, which may cause problems. Set 'showtabline' to 2
10901 * temporarily to avoid that.
10902 */
10903 if (p_stal == 1 && first_tabpage->tp_next != NULL)
10904 {
10905 if (put_line(fd, "set stal=2") == FAIL)
10906 return FAIL;
10907 restore_stal = TRUE;
10908 }
10909
10910 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010911 * May repeat putting Windows for each tab, when "tabpages" is in
10912 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010913 * Don't use goto_tabpage(), it may change directory and trigger
10914 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010916 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010917 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010918 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020010920 int need_tabnew = FALSE;
10921 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010922
Bram Moolenaar18144c82006-04-12 21:52:12 +000010923 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010925 tabpage_T *tp = find_tabpage(tabnr);
10926
10927 if (tp == NULL)
10928 break; /* done all tab pages */
10929 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010930 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010931 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010932 tab_topframe = topframe;
10933 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010934 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010935 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010936 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010937 tab_topframe = tp->tp_topframe;
10938 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010939 if (tabnr > 1)
10940 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942
Bram Moolenaar18144c82006-04-12 21:52:12 +000010943 /*
10944 * Before creating the window layout, try loading one file. If this
10945 * is aborted we don't end up with a number of useless windows.
10946 * This may have side effects! (e.g., compressed or network file).
10947 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010948 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010949 {
10950 if (ses_do_win(wp)
10951 && wp->w_buffer->b_ffname != NULL
10952 && !wp->w_buffer->b_help
10953#ifdef FEAT_QUICKFIX
10954 && !bt_nofile(wp->w_buffer)
10955#endif
10956 )
10957 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010958 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010959 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10960 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010961 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010962 if (!wp->w_arg_idx_invalid)
10963 edited_win = wp;
10964 break;
10965 }
10966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010968 /* If no file got edited create an empty tab page. */
10969 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10970 return FAIL;
10971
Bram Moolenaar18144c82006-04-12 21:52:12 +000010972 /*
10973 * Save current window layout.
10974 */
10975 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010977 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010979 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10980 return FAIL;
10981 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10982 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983
Bram Moolenaar18144c82006-04-12 21:52:12 +000010984 /*
10985 * Check if window sizes can be restored (no windows omitted).
10986 * Remember the window number of the current window after restoring.
10987 */
10988 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010989 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010990 {
10991 if (ses_do_win(wp))
10992 ++nr;
10993 else
10994 restore_size = FALSE;
10995 if (curwin == wp)
10996 cnr = nr;
10997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998
Bram Moolenaar18144c82006-04-12 21:52:12 +000010999 /* Go to the first window. */
11000 if (put_line(fd, "wincmd t") == FAIL)
11001 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011002
Bram Moolenaar18144c82006-04-12 21:52:12 +000011003 /*
11004 * If more than one window, see if sizes can be restored.
11005 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11006 * resized when moving between windows.
11007 * Do this before restoring the view, so that the topline and the
11008 * cursor can be set. This is done again below.
11009 */
11010 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
11011 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011012 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011013 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014
Bram Moolenaar18144c82006-04-12 21:52:12 +000011015 /*
11016 * Restore the view of the window (options, file, cursor, etc.).
11017 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011018 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011019 {
11020 if (!ses_do_win(wp))
11021 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011022 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11023 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011024 return FAIL;
11025 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11026 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011027 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011028 }
11029
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011030 /* The argument index in the first tab page is zero, need to set it in
11031 * each window. For further tab pages it's the window where we do
11032 * "tabedit". */
11033 cur_arg_idx = next_arg_idx;
11034
Bram Moolenaar18144c82006-04-12 21:52:12 +000011035 /*
11036 * Restore cursor to the current window if it's not the first one.
11037 */
11038 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11039 || put_eol(fd) == FAIL))
11040 return FAIL;
11041
11042 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011043 * Restore window sizes again after jumping around in windows, because
11044 * the current window has a minimum size while others may not.
11045 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011046 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011047 return FAIL;
11048
Bram Moolenaar18144c82006-04-12 21:52:12 +000011049 /* Don't continue in another tab page when doing only the current one
11050 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011051 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011052 break;
11053 }
11054
11055 if (ssop_flags & SSOP_TABPAGES)
11056 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011057 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11058 || put_eol(fd) == FAIL)
11059 return FAIL;
11060 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011061 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11062 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011063
Bram Moolenaar9c102382006-05-03 21:26:49 +000011064 /*
11065 * Wipe out an empty unnamed buffer we started in.
11066 */
11067 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11068 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011069 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011070 return FAIL;
11071 if (put_line(fd, "endif") == FAIL)
11072 return FAIL;
11073 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11074 return FAIL;
11075
11076 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11077 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11078 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11079 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080
11081 /*
11082 * Lastly, execute the x.vim file if it exists.
11083 */
11084 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11085 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011086 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011087 || put_line(fd, "endif") == FAIL)
11088 return FAIL;
11089
11090 return OK;
11091}
11092
11093 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011094ses_winsizes(
11095 FILE *fd,
11096 int restore_size,
11097 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098{
11099 int n = 0;
11100 win_T *wp;
11101
11102 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11103 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011104 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105 {
11106 if (!ses_do_win(wp))
11107 continue;
11108 ++n;
11109
11110 /* restore height when not full height */
11111 if (wp->w_height + wp->w_status_height < topframe->fr_height
11112 && (fprintf(fd,
11113 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11114 n, (long)wp->w_height, Rows / 2, Rows) < 0
11115 || put_eol(fd) == FAIL))
11116 return FAIL;
11117
11118 /* restore width when not full width */
11119 if (wp->w_width < Columns && (fprintf(fd,
11120 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11121 n, (long)wp->w_width, Columns / 2, Columns) < 0
11122 || put_eol(fd) == FAIL))
11123 return FAIL;
11124 }
11125 }
11126 else
11127 {
11128 /* Just equalise window sizes */
11129 if (put_line(fd, "wincmd =") == FAIL)
11130 return FAIL;
11131 }
11132 return OK;
11133}
11134
11135/*
11136 * Write commands to "fd" to recursively create windows for frame "fr",
11137 * horizontally and vertically split.
11138 * After the commands the last window in the frame is the current window.
11139 * Returns FAIL when writing the commands to "fd" fails.
11140 */
11141 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011142ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143{
11144 frame_T *frc;
11145 int count = 0;
11146
11147 if (fr->fr_layout != FR_LEAF)
11148 {
11149 /* Find first frame that's not skipped and then create a window for
11150 * each following one (first frame is already there). */
11151 frc = ses_skipframe(fr->fr_child);
11152 if (frc != NULL)
11153 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11154 {
11155 /* Make window as big as possible so that we have lots of room
11156 * to split. */
11157 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11158 || put_line(fd, fr->fr_layout == FR_COL
11159 ? "split" : "vsplit") == FAIL)
11160 return FAIL;
11161 ++count;
11162 }
11163
11164 /* Go back to the first window. */
11165 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11166 ? "%dwincmd k" : "%dwincmd h", count) < 0
11167 || put_eol(fd) == FAIL))
11168 return FAIL;
11169
11170 /* Recursively create frames/windows in each window of this column or
11171 * row. */
11172 frc = ses_skipframe(fr->fr_child);
11173 while (frc != NULL)
11174 {
11175 ses_win_rec(fd, frc);
11176 frc = ses_skipframe(frc->fr_next);
11177 /* Go to next window. */
11178 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11179 return FAIL;
11180 }
11181 }
11182 return OK;
11183}
11184
11185/*
11186 * Skip frames that don't contain windows we want to save in the Session.
11187 * Returns NULL when there none.
11188 */
11189 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011190ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011191{
11192 frame_T *frc;
11193
11194 for (frc = fr; frc != NULL; frc = frc->fr_next)
11195 if (ses_do_frame(frc))
11196 break;
11197 return frc;
11198}
11199
11200/*
11201 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11202 * the Session.
11203 */
11204 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011205ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011206{
11207 frame_T *frc;
11208
11209 if (fr->fr_layout == FR_LEAF)
11210 return ses_do_win(fr->fr_win);
11211 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11212 if (ses_do_frame(frc))
11213 return TRUE;
11214 return FALSE;
11215}
11216
11217/*
11218 * Return non-zero if window "wp" is to be stored in the Session.
11219 */
11220 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011221ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222{
11223 if (wp->w_buffer->b_fname == NULL
11224#ifdef FEAT_QUICKFIX
11225 /* When 'buftype' is "nofile" can't restore the window contents. */
11226 || bt_nofile(wp->w_buffer)
11227#endif
11228 )
11229 return (ssop_flags & SSOP_BLANK);
11230 if (wp->w_buffer->b_help)
11231 return (ssop_flags & SSOP_HELP);
11232 return TRUE;
11233}
11234
11235/*
11236 * Write commands to "fd" to restore the view of a window.
11237 * Caller must make sure 'scrolloff' is zero.
11238 */
11239 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011240put_view(
11241 FILE *fd,
11242 win_T *wp,
11243 int add_edit, /* add ":edit" command to view */
11244 unsigned *flagp, /* vop_flags or ssop_flags */
11245 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011246 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247{
11248 win_T *save_curwin;
11249 int f;
11250 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011251 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252
11253 /* Always restore cursor position for ":mksession". For ":mkview" only
11254 * when 'viewoptions' contains "cursor". */
11255 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11256
11257 /*
11258 * Local argument list.
11259 */
11260 if (wp->w_alist == &global_alist)
11261 {
11262 if (put_line(fd, "argglobal") == FAIL)
11263 return FAIL;
11264 }
11265 else
11266 {
11267 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11268 flagp == &vop_flags
11269 || !(*flagp & SSOP_CURDIR)
11270 || wp->w_localdir != NULL, flagp) == FAIL)
11271 return FAIL;
11272 }
11273
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011274 /* Only when part of a session: restore the argument index. Some
11275 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011276 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011277 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011279 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280 || put_eol(fd) == FAIL)
11281 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011282 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283 }
11284
11285 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011286 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287 {
11288 /*
11289 * Load the file.
11290 */
11291 if (wp->w_buffer->b_ffname != NULL
11292#ifdef FEAT_QUICKFIX
11293 && !bt_nofile(wp->w_buffer)
11294#endif
11295 )
11296 {
11297 /*
11298 * Editing a file in this buffer: use ":edit file".
11299 * This may have side effects! (e.g., compressed or network file).
11300 */
11301 if (fputs("edit ", fd) < 0
11302 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11303 return FAIL;
11304 }
11305 else
11306 {
11307 /* No file in this buffer, just make it empty. */
11308 if (put_line(fd, "enew") == FAIL)
11309 return FAIL;
11310#ifdef FEAT_QUICKFIX
11311 if (wp->w_buffer->b_ffname != NULL)
11312 {
11313 /* The buffer does have a name, but it's not a file name. */
11314 if (fputs("file ", fd) < 0
11315 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11316 return FAIL;
11317 }
11318#endif
11319 do_cursor = FALSE;
11320 }
11321 }
11322
11323 /*
11324 * Local mappings and abbreviations.
11325 */
11326 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11327 && makemap(fd, wp->w_buffer) == FAIL)
11328 return FAIL;
11329
11330 /*
11331 * Local options. Need to go to the window temporarily.
11332 * Store only local values when using ":mkview" and when ":mksession" is
11333 * used and 'sessionoptions' doesn't include "options".
11334 * Some folding options are always stored when "folds" is included,
11335 * otherwise the folds would not be restored correctly.
11336 */
11337 save_curwin = curwin;
11338 curwin = wp;
11339 curbuf = curwin->w_buffer;
11340 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11341 f = makeset(fd, OPT_LOCAL,
11342 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11343#ifdef FEAT_FOLDING
11344 else if (*flagp & SSOP_FOLDS)
11345 f = makefoldset(fd);
11346#endif
11347 else
11348 f = OK;
11349 curwin = save_curwin;
11350 curbuf = curwin->w_buffer;
11351 if (f == FAIL)
11352 return FAIL;
11353
11354#ifdef FEAT_FOLDING
11355 /*
11356 * Save Folds when 'buftype' is empty and for help files.
11357 */
11358 if ((*flagp & SSOP_FOLDS)
11359 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011360# ifdef FEAT_QUICKFIX
11361 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11362# endif
11363 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364 {
11365 if (put_folds(fd, wp) == FAIL)
11366 return FAIL;
11367 }
11368#endif
11369
11370 /*
11371 * Set the cursor after creating folds, since that moves the cursor.
11372 */
11373 if (do_cursor)
11374 {
11375
11376 /* Restore the cursor line in the file and relatively in the
11377 * window. Don't use "G", it changes the jumplist. */
11378 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11379 (long)wp->w_cursor.lnum,
11380 (long)(wp->w_cursor.lnum - wp->w_topline),
11381 (long)wp->w_height / 2, (long)wp->w_height) < 0
11382 || put_eol(fd) == FAIL
11383 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11384 || put_line(fd, "exe s:l") == FAIL
11385 || put_line(fd, "normal! zt") == FAIL
11386 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11387 || put_eol(fd) == FAIL)
11388 return FAIL;
11389 /* Restore the cursor column and left offset when not wrapping. */
11390 if (wp->w_cursor.col == 0)
11391 {
11392 if (put_line(fd, "normal! 0") == FAIL)
11393 return FAIL;
11394 }
11395 else
11396 {
11397 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11398 {
11399 if (fprintf(fd,
11400 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011401 (long)wp->w_virtcol + 1,
11402 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011403 (long)wp->w_width / 2, (long)wp->w_width) < 0
11404 || put_eol(fd) == FAIL
11405 || put_line(fd, "if s:c > 0") == FAIL
11406 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011407 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11408 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011409 || put_eol(fd) == FAIL
11410 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011411 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412 || put_eol(fd) == FAIL
11413 || put_line(fd, "endif") == FAIL)
11414 return FAIL;
11415 }
11416 else
11417 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011418 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011419 || put_eol(fd) == FAIL)
11420 return FAIL;
11421 }
11422 }
11423 }
11424
11425 /*
11426 * Local directory.
11427 */
11428 if (wp->w_localdir != NULL)
11429 {
11430 if (fputs("lcd ", fd) < 0
11431 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11432 || put_eol(fd) == FAIL)
11433 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011434 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435 }
11436
11437 return OK;
11438}
11439
11440/*
11441 * Write an argument list to the session file.
11442 * Returns FAIL if writing fails.
11443 */
11444 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011445ses_arglist(
11446 FILE *fd,
11447 char *cmd,
11448 garray_T *gap,
11449 int fullname, /* TRUE: use full path name */
11450 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011451{
11452 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011453 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454 char_u *s;
11455
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011456 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11457 return FAIL;
11458 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011459 return FAIL;
11460 for (i = 0; i < gap->ga_len; ++i)
11461 {
11462 /* NULL file names are skipped (only happens when out of memory). */
11463 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11464 if (s != NULL)
11465 {
11466 if (fullname)
11467 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011468 buf = alloc(MAXPATHL);
11469 if (buf != NULL)
11470 {
11471 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11472 s = buf;
11473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011475 if (fputs("argadd ", fd) < 0
11476 || ses_put_fname(fd, s, flagp) == FAIL
11477 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011478 {
11479 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011481 }
11482 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011483 }
11484 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011485 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486}
11487
11488/*
11489 * Write a buffer name to the session file.
11490 * Also ends the line.
11491 * Returns FAIL if writing fails.
11492 */
11493 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011494ses_fname(FILE *fd, buf_T *buf, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495{
11496 char_u *name;
11497
11498 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011499 * the session file will be sourced.
11500 * Don't do this for ":mkview", we don't know the current directory.
11501 * Don't do this after ":lcd", we don't keep track of what the current
11502 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503 if (buf->b_sfname != NULL
11504 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011505 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011506#ifdef FEAT_AUTOCHDIR
11507 && !p_acd
11508#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011509 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011510 name = buf->b_sfname;
11511 else
11512 name = buf->b_ffname;
11513 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11514 return FAIL;
11515 return OK;
11516}
11517
11518/*
11519 * Write a file name to the session file.
11520 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11521 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011522 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011523 */
11524 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011525ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011526{
11527 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011528 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011529 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011530
11531 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011532 if (sname == NULL)
11533 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011534
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011535 if (*flagp & SSOP_SLASH)
11536 {
11537 /* change all backslashes to forward slashes */
11538 for (p = sname; *p != NUL; mb_ptr_adv(p))
11539 if (*p == '\\')
11540 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011541 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011542
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011543 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011544 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011546 if (p == NULL)
11547 return FAIL;
11548
11549 /* write the result */
11550 if (fputs((char *)p, fd) < 0)
11551 retval = FAIL;
11552
11553 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554 return retval;
11555}
11556
11557/*
11558 * ":loadview [nr]"
11559 */
11560 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011561ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011562{
11563 char_u *fname;
11564
11565 fname = get_view_file(*eap->arg);
11566 if (fname != NULL)
11567 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011568 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011569 vim_free(fname);
11570 }
11571}
11572
11573/*
11574 * Get the name of the view file for the current buffer.
11575 */
11576 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011577get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011578{
11579 int len = 0;
11580 char_u *p, *s;
11581 char_u *retval;
11582 char_u *sname;
11583
11584 if (curbuf->b_ffname == NULL)
11585 {
11586 EMSG(_(e_noname));
11587 return NULL;
11588 }
11589 sname = home_replace_save(NULL, curbuf->b_ffname);
11590 if (sname == NULL)
11591 return NULL;
11592
11593 /*
11594 * We want a file name without separators, because we're not going to make
11595 * a directory.
11596 * "normal" path separator -> "=+"
11597 * "=" -> "=="
11598 * ":" path separator -> "=-"
11599 */
11600 for (p = sname; *p; ++p)
11601 if (*p == '=' || vim_ispathsep(*p))
11602 ++len;
11603 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11604 if (retval != NULL)
11605 {
11606 STRCPY(retval, p_vdir);
11607 add_pathsep(retval);
11608 s = retval + STRLEN(retval);
11609 for (p = sname; *p; ++p)
11610 {
11611 if (*p == '=')
11612 {
11613 *s++ = '=';
11614 *s++ = '=';
11615 }
11616 else if (vim_ispathsep(*p))
11617 {
11618 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011619#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011620 if (*p == ':')
11621 *s++ = '-';
11622 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011623#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011624 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625 }
11626 else
11627 *s++ = *p;
11628 }
11629 *s++ = '=';
11630 *s++ = c;
11631 STRCPY(s, ".vim");
11632 }
11633
11634 vim_free(sname);
11635 return retval;
11636}
11637
11638#endif /* FEAT_SESSION */
11639
11640/*
11641 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11642 * Return FAIL for a write error.
11643 */
11644 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011645put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011646{
11647 if (
11648#ifdef USE_CRNL
11649 (
11650# ifdef MKSESSION_NL
11651 !mksession_nl &&
11652# endif
11653 (putc('\r', fd) < 0)) ||
11654#endif
11655 (putc('\n', fd) < 0))
11656 return FAIL;
11657 return OK;
11658}
11659
11660/*
11661 * Write a line to "fd".
11662 * Return FAIL for a write error.
11663 */
11664 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011665put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011666{
11667 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11668 return FAIL;
11669 return OK;
11670}
11671
11672#ifdef FEAT_VIMINFO
11673/*
11674 * ":rviminfo" and ":wviminfo".
11675 */
11676 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011677ex_viminfo(
11678 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011679{
11680 char_u *save_viminfo;
11681
11682 save_viminfo = p_viminfo;
11683 if (*p_viminfo == NUL)
11684 p_viminfo = (char_u *)"'100";
11685 if (eap->cmdidx == CMD_rviminfo)
11686 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011687 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11688 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011689 EMSG(_("E195: Cannot open viminfo file for reading"));
11690 }
11691 else
11692 write_viminfo(eap->arg, eap->forceit);
11693 p_viminfo = save_viminfo;
11694}
11695#endif
11696
11697#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011698/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011699 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011700 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011701 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011703dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011704{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011705 if (fname == NULL)
11706 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011707 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011708}
11709#endif
11710
11711/*
11712 * ":behave {mswin,xterm}"
11713 */
11714 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011715ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011716{
11717 if (STRCMP(eap->arg, "mswin") == 0)
11718 {
11719 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11720 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11721 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11722 set_option_value((char_u *)"keymodel", 0L,
11723 (char_u *)"startsel,stopsel", 0);
11724 }
11725 else if (STRCMP(eap->arg, "xterm") == 0)
11726 {
11727 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11728 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11729 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11730 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11731 }
11732 else
11733 EMSG2(_(e_invarg2), eap->arg);
11734}
11735
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011736#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11737/*
11738 * Function given to ExpandGeneric() to obtain the possible arguments of the
11739 * ":behave {mswin,xterm}" command.
11740 */
11741 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011742get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011743{
11744 if (idx == 0)
11745 return (char_u *)"mswin";
11746 if (idx == 1)
11747 return (char_u *)"xterm";
11748 return NULL;
11749}
11750#endif
11751
Bram Moolenaar071d4272004-06-13 20:20:40 +000011752#ifdef FEAT_AUTOCMD
11753static int filetype_detect = FALSE;
11754static int filetype_plugin = FALSE;
11755static int filetype_indent = FALSE;
11756
11757/*
11758 * ":filetype [plugin] [indent] {on,off,detect}"
11759 * on: Load the filetype.vim file to install autocommands for file types.
11760 * off: Load the ftoff.vim file to remove all autocommands for file types.
11761 * plugin on: load filetype.vim and ftplugin.vim
11762 * plugin off: load ftplugof.vim
11763 * indent on: load filetype.vim and indent.vim
11764 * indent off: load indoff.vim
11765 */
11766 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011767ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011768{
11769 char_u *arg = eap->arg;
11770 int plugin = FALSE;
11771 int indent = FALSE;
11772
11773 if (*eap->arg == NUL)
11774 {
11775 /* Print current status. */
11776 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11777 filetype_detect ? "ON" : "OFF",
11778 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11779 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11780 return;
11781 }
11782
11783 /* Accept "plugin" and "indent" in any order. */
11784 for (;;)
11785 {
11786 if (STRNCMP(arg, "plugin", 6) == 0)
11787 {
11788 plugin = TRUE;
11789 arg = skipwhite(arg + 6);
11790 continue;
11791 }
11792 if (STRNCMP(arg, "indent", 6) == 0)
11793 {
11794 indent = TRUE;
11795 arg = skipwhite(arg + 6);
11796 continue;
11797 }
11798 break;
11799 }
11800 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11801 {
11802 if (*arg == 'o' || !filetype_detect)
11803 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011804 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011805 filetype_detect = TRUE;
11806 if (plugin)
11807 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011808 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809 filetype_plugin = TRUE;
11810 }
11811 if (indent)
11812 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011813 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011814 filetype_indent = TRUE;
11815 }
11816 }
11817 if (*arg == 'd')
11818 {
11819 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011820 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821 }
11822 }
11823 else if (STRCMP(arg, "off") == 0)
11824 {
11825 if (plugin || indent)
11826 {
11827 if (plugin)
11828 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011829 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011830 filetype_plugin = FALSE;
11831 }
11832 if (indent)
11833 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011834 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011835 filetype_indent = FALSE;
11836 }
11837 }
11838 else
11839 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011840 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841 filetype_detect = FALSE;
11842 }
11843 }
11844 else
11845 EMSG2(_(e_invarg2), arg);
11846}
11847
11848/*
11849 * ":setfiletype {name}"
11850 */
11851 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011852ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011853{
11854 if (!did_filetype)
11855 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11856}
11857#endif
11858
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011860ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011861{
11862#ifdef FEAT_DIGRAPHS
11863 if (*eap->arg != NUL)
11864 putdigraph(eap->arg);
11865 else
11866 listdigraphs();
11867#else
11868 EMSG(_("E196: No digraphs in this version"));
11869#endif
11870}
11871
11872 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011873ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011874{
11875 int flags = 0;
11876
11877 if (eap->cmdidx == CMD_setlocal)
11878 flags = OPT_LOCAL;
11879 else if (eap->cmdidx == CMD_setglobal)
11880 flags = OPT_GLOBAL;
11881#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11882 if (cmdmod.browse && flags == 0)
11883 ex_options(eap);
11884 else
11885#endif
11886 (void)do_set(eap->arg, flags);
11887}
11888
11889#ifdef FEAT_SEARCH_EXTRA
11890/*
11891 * ":nohlsearch"
11892 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011893 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011894ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011895{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010011896 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011897 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011898}
11899
11900/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011901 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011902 * Sets nextcmd to the start of the next command, if any. Also called when
11903 * skipping commands to find the next command.
11904 */
11905 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011906ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011907{
11908 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011909 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011910 char_u *end;
11911 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011912 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011913
11914 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011915 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011916 else
11917 {
11918 EMSG(e_invcmd);
11919 return;
11920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921
11922 /* First clear any old pattern. */
11923 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011924 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011925
11926 if (ends_excmd(*eap->arg))
11927 end = eap->arg;
11928 else if ((STRNICMP(eap->arg, "none", 4) == 0
11929 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11930 end = eap->arg + 4;
11931 else
11932 {
11933 p = skiptowhite(eap->arg);
11934 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011935 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011936 p = skipwhite(p);
11937 if (*p == NUL)
11938 {
11939 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011940 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011941 EMSG2(_(e_invarg2), eap->arg);
11942 return;
11943 }
11944 end = skip_regexp(p + 1, *p, TRUE, NULL);
11945 if (!eap->skip)
11946 {
11947 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11948 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011949 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950 eap->errmsg = e_trailing;
11951 return;
11952 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011953 if (*end != *p)
11954 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011955 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011956 EMSG2(_(e_invarg2), p);
11957 return;
11958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011959
11960 c = *end;
11961 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020011962 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011963 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011964 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011965 }
11966 }
11967 eap->nextcmd = find_nextcmd(end);
11968}
11969#endif
11970
11971#ifdef FEAT_CRYPT
11972/*
11973 * ":X": Get crypt key
11974 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011976ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010011978 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020011979 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011980}
11981#endif
11982
11983#ifdef FEAT_FOLDING
11984 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011985ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011986{
11987 if (foldManualAllowed(TRUE))
11988 foldCreate(eap->line1, eap->line2);
11989}
11990
11991 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011992ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011993{
11994 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11995 eap->forceit, FALSE);
11996}
11997
11998 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011999ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000{
12001 linenr_T lnum;
12002
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012003#ifdef FEAT_CLIPBOARD
12004 start_global_changes();
12005#endif
12006
Bram Moolenaar071d4272004-06-13 20:20:40 +000012007 /* First set the marks for all lines closed/open. */
12008 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12009 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12010 ml_setmarked(lnum);
12011
12012 /* Execute the command on the marked lines. */
12013 global_exe(eap->arg);
12014 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012015#ifdef FEAT_CLIPBOARD
12016 end_global_changes();
12017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012018}
12019#endif