blob: 028af35c66d8d43dc96a685471d697cc2e30b88c [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
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100347static void ex_startinsert(exarg_T *eap);
348static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100350static void ex_checkpath(exarg_T *eap);
351static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352#else
353# define ex_findpat ex_ni
354# define ex_checkpath ex_ni
355#endif
356#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100357static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#else
359# define ex_psearch ex_ni
360#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100361static void ex_tag(exarg_T *eap);
362static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363#ifndef FEAT_EVAL
364# define ex_scriptnames ex_ni
365# define ex_finish ex_ni
366# define ex_echo ex_ni
367# define ex_echohl ex_ni
368# define ex_execute ex_ni
369# define ex_call ex_ni
370# define ex_if ex_ni
371# define ex_endif ex_ni
372# define ex_else ex_ni
373# define ex_while ex_ni
374# define ex_continue ex_ni
375# define ex_break ex_ni
376# define ex_endwhile ex_ni
377# define ex_throw ex_ni
378# define ex_try ex_ni
379# define ex_catch ex_ni
380# define ex_finally ex_ni
381# define ex_endtry ex_ni
382# define ex_endfunction ex_ni
383# define ex_let ex_ni
384# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000385# define ex_lockvar ex_ni
386# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387# define ex_function ex_ni
388# define ex_delfunction ex_ni
389# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000390# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100392static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100394static int makeopens(FILE *fd, char_u *dirnow);
395static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
396static void ex_loadview(exarg_T *eap);
397static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000398static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399#else
400# define ex_loadview ex_ni
401#endif
402#ifndef FEAT_EVAL
403# define ex_compiler ex_ni
404#endif
405#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100406static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407#else
408# define ex_viminfo ex_ni
409#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100410static void ex_behave(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100412static void ex_filetype(exarg_T *eap);
413static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414#else
415# define ex_filetype ex_ni
416# define ex_setfiletype ex_ni
417#endif
418#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000419# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420# define ex_diffpatch ex_ni
421# define ex_diffgetput ex_ni
422# define ex_diffsplit ex_ni
423# define ex_diffthis ex_ni
424# define ex_diffupdate ex_ni
425#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100426static void ex_digraphs(exarg_T *eap);
427static void ex_set(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
429# define ex_options ex_ni
430#endif
431#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100432static void ex_nohlsearch(exarg_T *eap);
433static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434#else
435# define ex_nohlsearch ex_ni
436# define ex_match ex_ni
437#endif
438#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100439static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440#else
441# define ex_X ex_ni
442#endif
443#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100444static void ex_fold(exarg_T *eap);
445static void ex_foldopen(exarg_T *eap);
446static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447#else
448# define ex_fold ex_ni
449# define ex_foldopen ex_ni
450# define ex_folddo ex_ni
451#endif
452#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
453 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
454# define ex_language ex_ni
455#endif
456#ifndef FEAT_SIGNS
457# define ex_sign ex_ni
458#endif
459#ifndef FEAT_SUN_WORKSHOP
460# define ex_wsverb ex_ni
461#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000462#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200463# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000464# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200465# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467
468#ifndef FEAT_EVAL
469# define ex_debug ex_ni
470# define ex_breakadd ex_ni
471# define ex_debuggreedy ex_ni
472# define ex_breakdel ex_ni
473# define ex_breaklist ex_ni
474#endif
475
476#ifndef FEAT_CMDHIST
477# define ex_history ex_ni
478#endif
479#ifndef FEAT_JUMPLIST
480# define ex_jumps ex_ni
481# define ex_changes ex_ni
482#endif
483
Bram Moolenaar05159a02005-02-26 23:04:13 +0000484#ifndef FEAT_PROFILE
485# define ex_profile ex_ni
486#endif
487
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488/*
489 * Declare cmdnames[].
490 */
491#define DO_DECLARE_EXCMD
492#include "ex_cmds.h"
493
494/*
495 * Table used to quickly search for a command, based on its first character.
496 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000497static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498{
499 CMD_append,
500 CMD_buffer,
501 CMD_change,
502 CMD_delete,
503 CMD_edit,
504 CMD_file,
505 CMD_global,
506 CMD_help,
507 CMD_insert,
508 CMD_join,
509 CMD_k,
510 CMD_list,
511 CMD_move,
512 CMD_next,
513 CMD_open,
514 CMD_print,
515 CMD_quit,
516 CMD_read,
517 CMD_substitute,
518 CMD_t,
519 CMD_undo,
520 CMD_vglobal,
521 CMD_write,
522 CMD_xit,
523 CMD_yank,
524 CMD_z,
525 CMD_bang
526};
527
528static char_u dollar_command[2] = {'$', 0};
529
530
531#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000532/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533typedef struct
534{
535 char_u *line; /* command line */
536 linenr_T lnum; /* sourcing_lnum of the line */
537} wcmd_T;
538
539/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000540 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000542 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000544struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545{
546 garray_T *lines_gap; /* growarray with line info */
547 int current_line; /* last read line from growarray */
548 int repeating; /* TRUE when looping a second time */
549 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100550 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 void *cookie;
552};
553
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100554static char_u *get_loop_line(int c, void *cookie, int indent);
555static int store_loop_line(garray_T *gap, char_u *line);
556static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000557
558/* Struct to save a few things while debugging. Used in do_cmdline() only. */
559struct dbg_stuff
560{
561 int trylevel;
562 int force_abort;
563 except_T *caught_stack;
564 char_u *vv_exception;
565 char_u *vv_throwpoint;
566 int did_emsg;
567 int got_int;
568 int did_throw;
569 int need_rethrow;
570 int check_cstack;
571 except_T *current_exception;
572};
573
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100574static void save_dbg_stuff(struct dbg_stuff *dsp);
575static void restore_dbg_stuff(struct dbg_stuff *dsp);
Bram Moolenaared203462004-06-16 11:19:22 +0000576
577 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100578save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000579{
580 dsp->trylevel = trylevel; trylevel = 0;
581 dsp->force_abort = force_abort; force_abort = FALSE;
582 dsp->caught_stack = caught_stack; caught_stack = NULL;
583 dsp->vv_exception = v_exception(NULL);
584 dsp->vv_throwpoint = v_throwpoint(NULL);
585
586 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
587 dsp->did_emsg = did_emsg; did_emsg = FALSE;
588 dsp->got_int = got_int; got_int = FALSE;
589 dsp->did_throw = did_throw; did_throw = FALSE;
590 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
591 dsp->check_cstack = check_cstack; check_cstack = FALSE;
592 dsp->current_exception = current_exception; current_exception = NULL;
593}
594
595 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100596restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000597{
598 suppress_errthrow = FALSE;
599 trylevel = dsp->trylevel;
600 force_abort = dsp->force_abort;
601 caught_stack = dsp->caught_stack;
602 (void)v_exception(dsp->vv_exception);
603 (void)v_throwpoint(dsp->vv_throwpoint);
604 did_emsg = dsp->did_emsg;
605 got_int = dsp->got_int;
606 did_throw = dsp->did_throw;
607 need_rethrow = dsp->need_rethrow;
608 check_cstack = dsp->check_cstack;
609 current_exception = dsp->current_exception;
610}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611#endif
612
613
614/*
615 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
616 * command is given.
617 */
618 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100619do_exmode(
620 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621{
622 int save_msg_scroll;
623 int prev_msg_row;
624 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000625 int changedtick;
626
627 if (improved)
628 exmode_active = EXMODE_VIM;
629 else
630 exmode_active = EXMODE_NORMAL;
631 State = NORMAL;
632
633 /* When using ":global /pat/ visual" and then "Q" we return to continue
634 * the :global command. */
635 if (global_busy)
636 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637
638 save_msg_scroll = msg_scroll;
639 ++RedrawingDisabled; /* don't redisplay the window */
640 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641#ifdef FEAT_GUI
642 /* Ignore scrollbar and mouse events in Ex mode */
643 ++hold_gui_events;
644#endif
645#ifdef FEAT_SNIFF
646 want_sniff_request = 0; /* No K_SNIFF wanted */
647#endif
648
649 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
650 while (exmode_active)
651 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000652 /* Check for a ":normal" command and no more characters left. */
653 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
654 {
655 exmode_active = FALSE;
656 break;
657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 msg_scroll = TRUE;
659 need_wait_return = FALSE;
660 ex_pressedreturn = FALSE;
661 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000662 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 prev_msg_row = msg_row;
664 prev_line = curwin->w_cursor.lnum;
665#ifdef FEAT_SNIFF
666 ProcessSniffRequests();
667#endif
668 if (improved)
669 {
670 cmdline_row = msg_row;
671 do_cmdline(NULL, getexline, NULL, 0);
672 }
673 else
674 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
675 lines_left = Rows - 1;
676
Bram Moolenaardf177f62005-02-22 08:39:57 +0000677 if ((prev_line != curwin->w_cursor.lnum
678 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000680 if (curbuf->b_ml.ml_flags & ML_EMPTY)
681 EMSG(_(e_emptybuf));
682 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000684 if (ex_pressedreturn)
685 {
686 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000687 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000688 msg_row = prev_msg_row;
689 if (prev_msg_row == Rows - 1)
690 msg_row--;
691 }
692 msg_col = 0;
693 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
694 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000697 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
698 {
699 if (curbuf->b_ml.ml_flags & ML_EMPTY)
700 EMSG(_(e_emptybuf));
701 else
702 EMSG(_("E501: At end-of-file"));
703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 }
705
706#ifdef FEAT_GUI
707 --hold_gui_events;
708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 --RedrawingDisabled;
710 --no_wait_return;
711 update_screen(CLEAR);
712 need_wait_return = FALSE;
713 msg_scroll = save_msg_scroll;
714}
715
716/*
717 * Execute a simple command line. Used for translated commands like "*".
718 */
719 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100720do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721{
722 return do_cmdline(cmd, NULL, NULL,
723 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
724}
725
726/*
727 * do_cmdline(): execute one Ex command line
728 *
729 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100730 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 * 2. Split up in parts separated with '|'.
732 *
733 * This function can be called recursively!
734 *
735 * flags:
736 * DOCMD_VERBOSE - The command will be included in the error message.
737 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100738 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 * DOCMD_KEYTYPED - Don't reset KeyTyped.
740 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
741 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
742 *
743 * return FAIL if cmdline could not be executed, OK otherwise
744 */
745 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100746do_cmdline(
747 char_u *cmdline,
748 char_u *(*fgetline)(int, void *, int),
749 void *cookie, /* argument for fgetline() */
750 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751{
752 char_u *next_cmdline; /* next cmd to execute */
753 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100754 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 static int recursive = 0; /* recursive depth */
756 int msg_didout_before_start = 0;
757 int count = 0; /* line number count */
758 int did_inc = FALSE; /* incremented RedrawingDisabled */
759 int retval = OK;
760#ifdef FEAT_EVAL
761 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000762 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 int current_line = 0; /* active line in lines_ga */
764 char_u *fname = NULL; /* function or script name */
765 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
766 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000767 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 int initial_trylevel;
769 struct msglist **saved_msg_list = NULL;
770 struct msglist *private_msg_list;
771
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100772 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100773 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000775 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000777 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100779# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780# define cmd_cookie cookie
781#endif
782 static int call_depth = 0; /* recursiveness */
783
784#ifdef FEAT_EVAL
785 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
786 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000787 * This ensures that the do_errthrow() call in do_one_cmd() does not
788 * combine the messages stored by an earlier invocation of do_one_cmd()
789 * with the command name of the later one. This would happen when
790 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 saved_msg_list = msg_list;
792 msg_list = &private_msg_list;
793 private_msg_list = NULL;
794#endif
795
796 /* It's possible to create an endless loop with ":execute", catch that
797 * here. The value of 200 allows nested function calls, ":source", etc. */
798 if (call_depth == 200)
799 {
800 EMSG(_("E169: Command too recursive"));
801#ifdef FEAT_EVAL
802 /* When converting to an exception, we do not include the command name
803 * since this is not an error of the specific command. */
804 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
805 msg_list = saved_msg_list;
806#endif
807 return FAIL;
808 }
809 ++call_depth;
810
811#ifdef FEAT_EVAL
812 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000813 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 cstack.cs_trylevel = 0;
815 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000816 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
818
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100819 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820
821 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100822 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000823 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 ++ex_nesting_level;
825
826 /* Get the function or script name and the address where the next breakpoint
827 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000828 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 {
830 fname = func_name(real_cookie);
831 breakpoint = func_breakpoint(real_cookie);
832 dbg_tick = func_dbg_tick(real_cookie);
833 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100834 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 {
836 fname = sourcing_name;
837 breakpoint = source_breakpoint(real_cookie);
838 dbg_tick = source_dbg_tick(real_cookie);
839 }
840
841 /*
842 * Initialize "force_abort" and "suppress_errthrow" at the top level.
843 */
844 if (!recursive)
845 {
846 force_abort = FALSE;
847 suppress_errthrow = FALSE;
848 }
849
850 /*
851 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000852 * exception handling (used when debugging). Otherwise clear it to avoid
853 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000855 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000856 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000857 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200858 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859
860 initial_trylevel = trylevel;
861
862 /*
863 * "did_throw" will be set to TRUE when an exception is being thrown.
864 */
865 did_throw = FALSE;
866#endif
867 /*
868 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000869 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 * If force_abort is set, we cancel everything.
871 */
872 did_emsg = FALSE;
873
874 /*
875 * KeyTyped is only set when calling vgetc(). Reset it here when not
876 * calling vgetc() (sourced command lines).
877 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100878 if (!(flags & DOCMD_KEYTYPED)
879 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 KeyTyped = FALSE;
881
882 /*
883 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000884 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 * - for multiple commands on one line, separated with '|'
886 * - when repeating until there are no more lines (for ":source")
887 */
888 next_cmdline = cmdline;
889 do
890 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000891#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100892 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000893#endif
894
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000895 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 if (next_cmdline == NULL
897#ifdef FEAT_EVAL
898 && !force_abort
899 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000900 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901#endif
902 )
903 did_emsg = FALSE;
904
905 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000906 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100907 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 * 3. If a line is given: Make a copy, so we can mess with it.
909 */
910
911#ifdef FEAT_EVAL
912 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000913 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 {
915 /* Each '|' separated command is stored separately in lines_ga, to
916 * be able to jump to it. Don't use next_cmdline now. */
917 vim_free(cmdline_copy);
918 cmdline_copy = NULL;
919
920 /* Check if a function has returned or, unless it has an unclosed
921 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000922 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000924# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000925 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000926 func_line_end(real_cookie);
927# endif
928 if (func_has_ended(real_cookie))
929 {
930 retval = FAIL;
931 break;
932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000934#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000935 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100936 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000937 script_line_end();
938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939
940 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100941 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 {
943 retval = FAIL;
944 break;
945 }
946
947 /* If breakpoints have been added/deleted need to check for it. */
948 if (breakpoint != NULL && dbg_tick != NULL
949 && *dbg_tick != debug_tick)
950 {
951 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100952 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 fname, sourcing_lnum);
954 *dbg_tick = debug_tick;
955 }
956
957 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
958 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
959
960 /* Did we encounter a breakpoint? */
961 if (breakpoint != NULL && *breakpoint != 0
962 && *breakpoint <= sourcing_lnum)
963 {
964 dbg_breakpoint(fname, sourcing_lnum);
965 /* Find next breakpoint. */
966 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100967 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 fname, sourcing_lnum);
969 *dbg_tick = debug_tick;
970 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000971# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000972 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000973 {
974 if (getline_is_func)
975 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100976 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000977 script_line_start();
978 }
979# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 }
981
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000982 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000984 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100985 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 * below, so that it stores lines in or reads them from
987 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000988 * while/for loop. */
989 cmd_getline = get_loop_line;
990 cmd_cookie = (void *)&cmd_loop_cookie;
991 cmd_loop_cookie.lines_gap = &lines_ga;
992 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100993 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000994 cmd_loop_cookie.cookie = cookie;
995 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 }
997 else
998 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100999 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 cmd_cookie = cookie;
1001 }
1002#endif
1003
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001004 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 if (next_cmdline == NULL)
1006 {
1007 /*
1008 * Need to set msg_didout for the first line after an ":if",
1009 * otherwise the ":if" will be overwritten.
1010 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001011 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001013 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014#ifdef FEAT_EVAL
1015 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1016#else
1017 0
1018#endif
1019 )) == NULL)
1020 {
1021 /* Don't call wait_return for aborted command line. The NULL
1022 * returned for the end of a sourced file or executed function
1023 * doesn't do this. */
1024 if (KeyTyped && !(flags & DOCMD_REPEAT))
1025 need_wait_return = FALSE;
1026 retval = FAIL;
1027 break;
1028 }
1029 used_getline = TRUE;
1030
1031 /*
1032 * Keep the first typed line. Clear it when more lines are typed.
1033 */
1034 if (flags & DOCMD_KEEPLINE)
1035 {
1036 vim_free(repeat_cmdline);
1037 if (count == 0)
1038 repeat_cmdline = vim_strsave(next_cmdline);
1039 else
1040 repeat_cmdline = NULL;
1041 }
1042 }
1043
1044 /* 3. Make a copy of the command so we can mess with it. */
1045 else if (cmdline_copy == NULL)
1046 {
1047 next_cmdline = vim_strsave(next_cmdline);
1048 if (next_cmdline == NULL)
1049 {
1050 EMSG(_(e_outofmem));
1051 retval = FAIL;
1052 break;
1053 }
1054 }
1055 cmdline_copy = next_cmdline;
1056
1057#ifdef FEAT_EVAL
1058 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001059 * Save the current line when inside a ":while" or ":for", and when
1060 * the command looks like a ":while" or ":for", because we may need it
1061 * later. When there is a '|' and another command, it is stored
1062 * separately, because we need to be able to jump back to it from an
1063 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001065 if (current_line == lines_ga.ga_len
1066 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001068 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 {
1070 retval = FAIL;
1071 break;
1072 }
1073 }
1074 did_endif = FALSE;
1075#endif
1076
1077 if (count++ == 0)
1078 {
1079 /*
1080 * All output from the commands is put below each other, without
1081 * waiting for a return. Don't do this when executing commands
1082 * from a script or when being called recursive (e.g. for ":e
1083 * +command file").
1084 */
1085 if (!(flags & DOCMD_NOWAIT) && !recursive)
1086 {
1087 msg_didout_before_start = msg_didout;
1088 msg_didany = FALSE; /* no output yet */
1089 msg_start();
1090 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001091 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 ++RedrawingDisabled;
1093 did_inc = TRUE;
1094 }
1095 }
1096
1097 if (p_verbose >= 15 && sourcing_name != NULL)
1098 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001100 verbose_enter_scroll();
1101
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 smsg((char_u *)_("line %ld: %s"),
1103 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001104 if (msg_silent == 0)
1105 msg_puts((char_u *)"\n"); /* don't overwrite this */
1106
1107 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 --no_wait_return;
1109 }
1110
1111 /*
1112 * 2. Execute one '|' separated command.
1113 * do_one_cmd() will return NULL if there is no trailing '|'.
1114 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1115 */
1116 ++recursive;
1117 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1118#ifdef FEAT_EVAL
1119 &cstack,
1120#endif
1121 cmd_getline, cmd_cookie);
1122 --recursive;
1123
1124#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001125 if (cmd_cookie == (void *)&cmd_loop_cookie)
1126 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001128 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129#endif
1130
1131 if (next_cmdline == NULL)
1132 {
1133 vim_free(cmdline_copy);
1134 cmdline_copy = NULL;
1135#ifdef FEAT_CMDHIST
1136 /*
1137 * If the command was typed, remember it for the ':' register.
1138 * Do this AFTER executing the command to make :@: work.
1139 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001140 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 && new_last_cmdline != NULL)
1142 {
1143 vim_free(last_cmdline);
1144 last_cmdline = new_last_cmdline;
1145 new_last_cmdline = NULL;
1146 }
1147#endif
1148 }
1149 else
1150 {
1151 /* need to copy the command after the '|' to cmdline_copy, for the
1152 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001153 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 next_cmdline = cmdline_copy;
1155 }
1156
1157
1158#ifdef FEAT_EVAL
1159 /* reset did_emsg for a function that is not aborted by an error */
1160 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001161 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 && !func_has_abort(real_cookie))
1163 did_emsg = FALSE;
1164
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001165 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 {
1167 ++current_line;
1168
1169 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001170 * An ":endwhile", ":endfor" and ":continue" is handled here.
1171 * If we were executing commands, jump back to the ":while" or
1172 * ":for".
1173 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001175 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001177 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001179 /* Jump back to the matching ":while" or ":for". Be careful
1180 * not to use a cs_line[] from an entry that isn't a ":while"
1181 * or ":for": It would make "current_line" invalid and can
1182 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 if (!did_emsg && !got_int && !did_throw
1184 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001185 && (cstack.cs_flags[cstack.cs_idx]
1186 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 && cstack.cs_line[cstack.cs_idx] >= 0
1188 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1189 {
1190 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001191 /* remember we jumped there */
1192 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 line_breakcheck(); /* check if CTRL-C typed */
1194
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001195 /* Check for the next breakpoint at or after the ":while"
1196 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 if (breakpoint != NULL)
1198 {
1199 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001200 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 fname,
1202 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1203 *dbg_tick = debug_tick;
1204 }
1205 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001206 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001208 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001210 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1211 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 }
1213 }
1214
1215 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001216 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001218 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001220 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1222 }
1223 }
1224
1225 /*
1226 * When not inside any ":while" loop, clear remembered lines.
1227 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001228 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 {
1230 if (lines_ga.ga_len > 0)
1231 {
1232 sourcing_lnum =
1233 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1234 free_cmdlines(&lines_ga);
1235 }
1236 current_line = 0;
1237 }
1238
1239 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001240 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1241 * being restored at the ":endtry". Reset them here and set the
1242 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1243 * This includes the case where a missing ":endif", ":endwhile" or
1244 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001246 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001248 cstack.cs_lflags &= ~CSL_HAD_FINA;
1249 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1250 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 did_throw ? (void *)current_exception : NULL);
1252 did_emsg = got_int = did_throw = FALSE;
1253 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1254 }
1255
1256 /* Update global "trylevel" for recursive calls to do_cmdline() from
1257 * within this loop. */
1258 trylevel = initial_trylevel + cstack.cs_trylevel;
1259
1260 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001261 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 * files) is aborted because of an error, an interrupt, or an uncaught
1263 * exception, cancel everything. If it is left normally, reset
1264 * force_abort to get the non-EH compatible abortion behavior for
1265 * the rest of the script.
1266 */
1267 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1268 force_abort = FALSE;
1269
1270 /* Convert an interrupt to an exception if appropriate. */
1271 (void)do_intthrow(&cstack);
1272#endif /* FEAT_EVAL */
1273
1274 }
1275 /*
1276 * Continue executing command lines when:
1277 * - no CTRL-C typed, no aborting error, no exception thrown or try
1278 * conditionals need to be checked for executing finally clauses or
1279 * catching an interrupt exception
1280 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001281 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 * looping for ":source" command or function call.
1283 */
1284 while (!((got_int
1285#ifdef FEAT_EVAL
1286 || (did_emsg && force_abort) || did_throw
1287#endif
1288 )
1289#ifdef FEAT_EVAL
1290 && cstack.cs_trylevel == 0
1291#endif
1292 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001293 && !(did_emsg
1294#ifdef FEAT_EVAL
1295 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001296 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001297 * the :endtry to be missed. */
1298 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1299#endif
1300 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001301 && (getline_equal(fgetline, cookie, getexmodeline)
1302 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 && (next_cmdline != NULL
1304#ifdef FEAT_EVAL
1305 || cstack.cs_idx >= 0
1306#endif
1307 || (flags & DOCMD_REPEAT)));
1308
1309 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001310 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311#ifdef FEAT_EVAL
1312 free_cmdlines(&lines_ga);
1313 ga_clear(&lines_ga);
1314
1315 if (cstack.cs_idx >= 0)
1316 {
1317 /*
1318 * If a sourced file or executed function ran to its end, report the
1319 * unclosed conditional.
1320 */
1321 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001322 && ((getline_equal(fgetline, cookie, getsourceline)
1323 && !source_finished(fgetline, cookie))
1324 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 && !func_has_ended(real_cookie))))
1326 {
1327 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1328 EMSG(_(e_endtry));
1329 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1330 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001331 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1332 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 else
1334 EMSG(_(e_endif));
1335 }
1336
1337 /*
1338 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1339 * ":endtry" in a sourced file or executed function. If the try
1340 * conditional is in its finally clause, ignore anything pending.
1341 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001342 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 */
1344 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001345 {
1346 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1347
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001348 if (idx >= 0)
1349 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001350 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1351 &cstack.cs_looplevel);
1352 }
1353 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 trylevel = initial_trylevel;
1355 }
1356
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001357 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1358 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001360 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 ? (char_u *)"endfunction" : (char_u *)NULL);
1362
1363 if (trylevel == 0)
1364 {
1365 /*
1366 * When an exception is being thrown out of the outermost try
1367 * conditional, discard the uncaught exception, disable the conversion
1368 * of interrupts or errors to exceptions, and ensure that no more
1369 * commands are executed.
1370 */
1371 if (did_throw)
1372 {
1373 void *p = NULL;
1374 char_u *saved_sourcing_name;
1375 int saved_sourcing_lnum;
1376 struct msglist *messages = NULL, *next;
1377
1378 /*
1379 * If the uncaught exception is a user exception, report it as an
1380 * error. If it is an error exception, display the saved error
1381 * message now. For an interrupt exception, do nothing; the
1382 * interrupt message is given elsewhere.
1383 */
1384 switch (current_exception->type)
1385 {
1386 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001387 vim_snprintf((char *)IObuff, IOSIZE,
1388 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 current_exception->value);
1390 p = vim_strsave(IObuff);
1391 break;
1392 case ET_ERROR:
1393 messages = current_exception->messages;
1394 current_exception->messages = NULL;
1395 break;
1396 case ET_INTERRUPT:
1397 break;
1398 default:
1399 p = vim_strsave((char_u *)_(e_internal));
1400 }
1401
1402 saved_sourcing_name = sourcing_name;
1403 saved_sourcing_lnum = sourcing_lnum;
1404 sourcing_name = current_exception->throw_name;
1405 sourcing_lnum = current_exception->throw_lnum;
1406 current_exception->throw_name = NULL;
1407
1408 discard_current_exception(); /* uses IObuff if 'verbose' */
1409 suppress_errthrow = TRUE;
1410 force_abort = TRUE;
1411
1412 if (messages != NULL)
1413 {
1414 do
1415 {
1416 next = messages->next;
1417 emsg(messages->msg);
1418 vim_free(messages->msg);
1419 vim_free(messages);
1420 messages = next;
1421 }
1422 while (messages != NULL);
1423 }
1424 else if (p != NULL)
1425 {
1426 emsg(p);
1427 vim_free(p);
1428 }
1429 vim_free(sourcing_name);
1430 sourcing_name = saved_sourcing_name;
1431 sourcing_lnum = saved_sourcing_lnum;
1432 }
1433
1434 /*
1435 * On an interrupt or an aborting error not converted to an exception,
1436 * disable the conversion of errors to exceptions. (Interrupts are not
1437 * converted any more, here.) This enables also the interrupt message
1438 * when force_abort is set and did_emsg unset in case of an interrupt
1439 * from a finally clause after an error.
1440 */
1441 else if (got_int || (did_emsg && force_abort))
1442 suppress_errthrow = TRUE;
1443 }
1444
1445 /*
1446 * The current cstack will be freed when do_cmdline() returns. An uncaught
1447 * exception will have to be rethrown in the previous cstack. If a function
1448 * has just returned or a script file was just finished and the previous
1449 * cstack belongs to the same function or, respectively, script file, it
1450 * will have to be checked for finally clauses to be executed due to the
1451 * ":return" or ":finish". This is done in do_one_cmd().
1452 */
1453 if (did_throw)
1454 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001455 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001457 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 && ex_nesting_level > func_level(real_cookie) + 1))
1459 {
1460 if (!did_throw)
1461 check_cstack = TRUE;
1462 }
1463 else
1464 {
1465 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001466 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 --ex_nesting_level;
1468 /*
1469 * Go to debug mode when returning from a function in which we are
1470 * single-stepping.
1471 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001472 if ((getline_equal(fgetline, cookie, getsourceline)
1473 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001475 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 ? (char_u *)_("End of sourced file")
1477 : (char_u *)_("End of function"));
1478 }
1479
1480 /*
1481 * Restore the exception environment (done after returning from the
1482 * debugger).
1483 */
1484 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001485 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486
1487 msg_list = saved_msg_list;
1488#endif /* FEAT_EVAL */
1489
1490 /*
1491 * If there was too much output to fit on the command line, ask the user to
1492 * hit return before redrawing the screen. With the ":global" command we do
1493 * this only once after the command is finished.
1494 */
1495 if (did_inc)
1496 {
1497 --RedrawingDisabled;
1498 --no_wait_return;
1499 msg_scroll = FALSE;
1500
1501 /*
1502 * When just finished an ":if"-":else" which was typed, no need to
1503 * wait for hit-return. Also for an error situation.
1504 */
1505 if (retval == FAIL
1506#ifdef FEAT_EVAL
1507 || (did_endif && KeyTyped && !did_emsg)
1508#endif
1509 )
1510 {
1511 need_wait_return = FALSE;
1512 msg_didany = FALSE; /* don't wait when restarting edit */
1513 }
1514 else if (need_wait_return)
1515 {
1516 /*
1517 * The msg_start() above clears msg_didout. The wait_return we do
1518 * here should not overwrite the command that may be shown before
1519 * doing that.
1520 */
1521 msg_didout |= msg_didout_before_start;
1522 wait_return(FALSE);
1523 }
1524 }
1525
Bram Moolenaar2a942252012-11-28 23:03:07 +01001526#ifdef FEAT_EVAL
1527 did_endif = FALSE; /* in case do_cmdline used recursively */
1528#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 /*
1530 * Reset if_level, in case a sourced script file contains more ":if" than
1531 * ":endif" (could be ":if x | foo | endif").
1532 */
1533 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001534#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001535
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 --call_depth;
1537 return retval;
1538}
1539
1540#ifdef FEAT_EVAL
1541/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001542 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 */
1544 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001545get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001547 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 wcmd_T *wp;
1549 char_u *line;
1550
1551 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1552 {
1553 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001554 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001556 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 if (cp->getline == NULL)
1558 line = getcmdline(c, 0L, indent);
1559 else
1560 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001561 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 ++cp->current_line;
1563
1564 return line;
1565 }
1566
1567 KeyTyped = FALSE;
1568 ++cp->current_line;
1569 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1570 sourcing_lnum = wp->lnum;
1571 return vim_strsave(wp->line);
1572}
1573
1574/*
1575 * Store a line in "gap" so that a ":while" loop can execute it again.
1576 */
1577 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001578store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579{
1580 if (ga_grow(gap, 1) == FAIL)
1581 return FAIL;
1582 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1583 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1584 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 return OK;
1586}
1587
1588/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001589 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 */
1591 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001592free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593{
1594 while (gap->ga_len > 0)
1595 {
1596 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1597 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 }
1599}
1600#endif
1601
1602/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001603 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1604 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001607getline_equal(
1608 char_u *(*fgetline)(int, void *, int),
1609 void *cookie UNUSED, /* argument for fgetline() */
1610 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611{
1612#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001613 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001614 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615
Bram Moolenaar89d40322006-08-29 15:30:07 +00001616 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001617 * function that's originally used to obtain the lines. This may be
1618 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001619 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001620 cp = (struct loop_cookie *)cookie;
1621 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 {
1623 gp = cp->getline;
1624 cp = cp->cookie;
1625 }
1626 return gp == func;
1627#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001628 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629#endif
1630}
1631
1632#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1633/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001634 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 * getline function. Otherwise return "cookie".
1636 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001638getline_cookie(
1639 char_u *(*fgetline)(int, void *, int) UNUSED,
1640 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641{
1642# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001643 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001644 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645
Bram Moolenaar89d40322006-08-29 15:30:07 +00001646 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001647 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001649 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001650 cp = (struct loop_cookie *)cookie;
1651 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 {
1653 gp = cp->getline;
1654 cp = cp->cookie;
1655 }
1656 return cp;
1657# else
1658 return cookie;
1659# endif
1660}
1661#endif
1662
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001663
1664/*
1665 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1666 * ":bwipeout", etc.
1667 * Returns the buffer number.
1668 */
1669 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001670compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001671{
1672 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001673 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001674 int count = offset;
1675
1676 buf = firstbuf;
1677 while (buf->b_next != NULL && buf->b_fnum < lnum)
1678 buf = buf->b_next;
1679 while (count != 0)
1680 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001681 count += (offset < 0) ? 1 : -1;
1682 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1683 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001684 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001685 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001686 if (addr_type == ADDR_LOADED_BUFFERS)
1687 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001688 while (buf->b_ml.ml_mfp == NULL)
1689 {
1690 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1691 if (nextbuf == NULL)
1692 break;
1693 buf = nextbuf;
1694 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001695 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001696 /* we might have gone too far, last buffer is not loadedd */
1697 if (addr_type == ADDR_LOADED_BUFFERS)
1698 while (buf->b_ml.ml_mfp == NULL)
1699 {
1700 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1701 if (nextbuf == NULL)
1702 break;
1703 buf = nextbuf;
1704 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001705 return buf->b_fnum;
1706}
1707
Bram Moolenaarf240e182014-11-27 18:33:02 +01001708#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001709static int current_win_nr(win_T *win);
1710static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001711
1712 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001713current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001714{
1715 win_T *wp;
1716 int nr = 0;
1717
1718 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1719 {
1720 ++nr;
1721 if (wp == win)
1722 break;
1723 }
1724 return nr;
1725}
1726
1727 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001728current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001729{
1730 tabpage_T *tp;
1731 int nr = 0;
1732
1733 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
1734 {
1735 ++nr;
1736 if (tp == tab)
1737 break;
1738 }
1739 return nr;
1740}
1741
1742# define CURRENT_WIN_NR current_win_nr(curwin)
1743# define LAST_WIN_NR current_win_nr(NULL)
1744# define CURRENT_TAB_NR current_tab_nr(curtab)
1745# define LAST_TAB_NR current_tab_nr(NULL)
1746#else
1747# define CURRENT_WIN_NR 1
1748# define LAST_WIN_NR 1
1749# define CURRENT_TAB_NR 1
1750# define LAST_TAB_NR 1
1751#endif
1752
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001753
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754/*
1755 * Execute one Ex command.
1756 *
1757 * If 'sourcing' is TRUE, the command will be included in the error message.
1758 *
1759 * 1. skip comment lines and leading space
1760 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001761 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001762 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001763 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001764 * 6. parse arguments
1765 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001767 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 *
1769 * This function may be called recursively!
1770 */
1771#if (_MSC_VER == 1200)
1772/*
Bram Moolenaared203462004-06-16 11:19:22 +00001773 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001775 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776#endif
1777 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001778do_one_cmd(
1779 char_u **cmdlinep,
1780 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001782 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001784 char_u *(*fgetline)(int, void *, int),
1785 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786{
1787 char_u *p;
1788 linenr_T lnum;
1789 long n;
1790 char_u *errormsg = NULL; /* error message */
1791 exarg_T ea; /* Ex command arguments */
1792 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001793 int save_msg_scroll = msg_scroll;
1794 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001796#ifdef HAVE_SANDBOX
1797 int did_sandbox = FALSE;
1798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 cmdmod_T save_cmdmod;
1800 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001801 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802
1803 vim_memset(&ea, 0, sizeof(ea));
1804 ea.line1 = 1;
1805 ea.line2 = 1;
1806#ifdef FEAT_EVAL
1807 ++ex_nesting_level;
1808#endif
1809
Bram Moolenaar21691f82012-12-05 19:13:18 +01001810 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 if (quitmore
1812#ifdef FEAT_EVAL
1813 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001814 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001815#endif
1816#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001817 /* avoid that an autocommand, e.g. QuitPre, does this */
1818 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819#endif
1820 )
1821 --quitmore;
1822
1823 /*
1824 * Reset browse, confirm, etc.. They are restored when returning, for
1825 * recursive calls.
1826 */
1827 save_cmdmod = cmdmod;
1828 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1829
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001830 /* "#!anything" is handled like a comment. */
1831 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1832 goto doend;
1833
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 /*
1835 * Repeat until no more command modifiers are found.
1836 */
1837 ea.cmd = *cmdlinep;
1838 for (;;)
1839 {
1840/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001841 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 */
1843 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1844 ++ea.cmd;
1845
1846 /* in ex mode, an empty line works like :+ */
1847 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001848 && (getline_equal(fgetline, cookie, getexmodeline)
1849 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001850 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 {
1852 ea.cmd = (char_u *)"+";
1853 ex_pressedreturn = TRUE;
1854 }
1855
1856 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001857 if (*ea.cmd == '"')
1858 goto doend;
1859 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001860 {
1861 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864
1865/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001866 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 */
1868 p = ea.cmd;
1869 if (VIM_ISDIGIT(*ea.cmd))
1870 p = skipwhite(skipdigits(ea.cmd));
1871 switch (*p)
1872 {
1873 /* When adding an entry, also modify cmd_exists(). */
1874 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1875 break;
1876#ifdef FEAT_WINDOWS
1877 cmdmod.split |= WSP_ABOVE;
1878#endif
1879 continue;
1880
1881 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1882 {
1883#ifdef FEAT_WINDOWS
1884 cmdmod.split |= WSP_BELOW;
1885#endif
1886 continue;
1887 }
1888 if (checkforcmd(&ea.cmd, "browse", 3))
1889 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001890#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 cmdmod.browse = TRUE;
1892#endif
1893 continue;
1894 }
1895 if (!checkforcmd(&ea.cmd, "botright", 2))
1896 break;
1897#ifdef FEAT_WINDOWS
1898 cmdmod.split |= WSP_BOT;
1899#endif
1900 continue;
1901
1902 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1903 break;
1904#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1905 cmdmod.confirm = TRUE;
1906#endif
1907 continue;
1908
1909 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1910 {
1911 cmdmod.keepmarks = TRUE;
1912 continue;
1913 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001914 if (checkforcmd(&ea.cmd, "keepalt", 5))
1915 {
1916 cmdmod.keepalt = TRUE;
1917 continue;
1918 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001919 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1920 {
1921 cmdmod.keeppatterns = TRUE;
1922 continue;
1923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1925 break;
1926 cmdmod.keepjumps = TRUE;
1927 continue;
1928
1929 /* ":hide" and ":hide | cmd" are not modifiers */
1930 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1931 || *p == NUL || ends_excmd(*p))
1932 break;
1933 ea.cmd = p;
1934 cmdmod.hide = TRUE;
1935 continue;
1936
1937 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1938 {
1939 cmdmod.lockmarks = TRUE;
1940 continue;
1941 }
1942
1943 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1944 break;
1945#ifdef FEAT_WINDOWS
1946 cmdmod.split |= WSP_ABOVE;
1947#endif
1948 continue;
1949
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001950 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001951 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001952#ifdef FEAT_AUTOCMD
1953 if (cmdmod.save_ei == NULL)
1954 {
1955 /* Set 'eventignore' to "all". Restore the
1956 * existing option value later. */
1957 cmdmod.save_ei = vim_strsave(p_ei);
1958 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001959 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001960 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001961#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001962 continue;
1963 }
1964 if (!checkforcmd(&ea.cmd, "noswapfile", 6))
1965 break;
1966 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001967 continue;
1968
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1970 break;
1971#ifdef FEAT_WINDOWS
1972 cmdmod.split |= WSP_BELOW;
1973#endif
1974 continue;
1975
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001976 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1977 {
1978#ifdef HAVE_SANDBOX
1979 if (!did_sandbox)
1980 ++sandbox;
1981 did_sandbox = TRUE;
1982#endif
1983 continue;
1984 }
1985 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001987 if (save_msg_silent == -1)
1988 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1991 {
1992 /* ":silent!", but not "silent !cmd" */
1993 ea.cmd = skipwhite(ea.cmd + 1);
1994 ++emsg_silent;
1995 ++did_esilent;
1996 }
1997 continue;
1998
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001999 case 't': if (checkforcmd(&p, "tab", 3))
2000 {
2001#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002002 if (vim_isdigit(*ea.cmd))
2003 cmdmod.tab = atoi((char *)ea.cmd) + 1;
2004 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002005 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002006 ea.cmd = p;
2007#endif
2008 continue;
2009 }
2010 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 break;
2012#ifdef FEAT_WINDOWS
2013 cmdmod.split |= WSP_TOP;
2014#endif
2015 continue;
2016
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002017 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
2018 break;
2019 if (save_msg_silent == -1)
2020 save_msg_silent = msg_silent;
2021 msg_silent = 0;
2022 continue;
2023
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
2025 {
2026#ifdef FEAT_VERTSPLIT
2027 cmdmod.split |= WSP_VERT;
2028#endif
2029 continue;
2030 }
2031 if (!checkforcmd(&p, "verbose", 4))
2032 break;
2033 if (verbose_save < 0)
2034 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00002035 if (vim_isdigit(*ea.cmd))
2036 p_verbose = atoi((char *)ea.cmd);
2037 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 p_verbose = 1;
2039 ea.cmd = p;
2040 continue;
2041 }
2042 break;
2043 }
2044
2045#ifdef FEAT_EVAL
2046 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2047 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2048#else
2049 ea.skip = (if_level > 0);
2050#endif
2051
2052#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002053# ifdef FEAT_PROFILE
2054 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002055 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002056 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002057 if (getline_equal(fgetline, cookie, get_func_line))
2058 func_line_exec(getline_cookie(fgetline, cookie));
2059 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002060 script_line_exec();
2061 }
2062#endif
2063
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 /* May go to debug mode. If this happens and the ">quit" debug command is
2065 * used, throw an interrupt exception and skip the next command. */
2066 dbg_check_breakpoint(&ea);
2067 if (!ea.skip && got_int)
2068 {
2069 ea.skip = TRUE;
2070 (void)do_intthrow(cstack);
2071 }
2072#endif
2073
2074/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002075 * 3. Skip over the range to find the command. Let "p" point to after it.
2076 *
2077 * We need the command to know what kind of range it uses.
2078 */
2079 cmd = ea.cmd;
2080 ea.cmd = skip_range(ea.cmd, NULL);
2081 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2082 ea.cmd = skipwhite(ea.cmd + 1);
2083 p = find_command(&ea, NULL);
2084
2085/*
2086 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 *
2088 * where 'addr' is:
2089 *
2090 * % (entire file)
2091 * $ [+-NUM]
2092 * 'x [+-NUM] (where x denotes a currently defined mark)
2093 * . [+-NUM]
2094 * [+-NUM]..
2095 * NUM
2096 *
2097 * The ea.cmd pointer is updated to point to the first character following the
2098 * range spec. If an initial address is found, but no second, the upper bound
2099 * is equal to the lower.
2100 */
2101
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002102 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002103 if (!IS_USER_CMDIDX(ea.cmdidx))
2104 {
2105 if (ea.cmdidx != CMD_SIZE)
2106 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2107 else
2108 ea.addr_type = ADDR_LINES;
2109
2110#ifdef FEAT_WINDOWS
2111 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002112 if (ea.cmdidx == CMD_wincmd && p != NULL)
2113 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002114#endif
2115 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002116
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002118 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 for (;;)
2120 {
2121 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002122 switch (ea.addr_type)
2123 {
2124 case ADDR_LINES:
2125 /* default is current line number */
2126 ea.line2 = curwin->w_cursor.lnum;
2127 break;
2128 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002129 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002130 ea.line2 = lnum;
2131 break;
2132 case ADDR_ARGUMENTS:
2133 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002134 if (ea.line2 > ARGCOUNT)
2135 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002136 break;
2137 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002138 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002139 ea.line2 = curbuf->b_fnum;
2140 break;
2141 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002142 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002143 ea.line2 = lnum;
2144 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002145#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002146 case ADDR_QUICKFIX:
2147 ea.line2 = qf_get_cur_valid_idx(&ea);
2148 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002149#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002152 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip, ea.addr_count == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 if (ea.cmd == NULL) /* error detected */
2154 goto doend;
2155 if (lnum == MAXLNUM)
2156 {
2157 if (*ea.cmd == '%') /* '%' - all lines */
2158 {
2159 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002160 switch (ea.addr_type)
2161 {
2162 case ADDR_LINES:
2163 ea.line1 = 1;
2164 ea.line2 = curbuf->b_ml.ml_line_count;
2165 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002166 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002167 {
2168 buf_T *buf = firstbuf;
2169
2170 while (buf->b_next != NULL
2171 && buf->b_ml.ml_mfp == NULL)
2172 buf = buf->b_next;
2173 ea.line1 = buf->b_fnum;
2174 buf = lastbuf;
2175 while (buf->b_prev != NULL
2176 && buf->b_ml.ml_mfp == NULL)
2177 buf = buf->b_prev;
2178 ea.line2 = buf->b_fnum;
2179 break;
2180 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002181 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002182 ea.line1 = firstbuf->b_fnum;
2183 ea.line2 = lastbuf->b_fnum;
2184 break;
2185 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002186 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002187 if (IS_USER_CMDIDX(ea.cmdidx))
2188 {
2189 ea.line1 = 1;
2190 ea.line2 = ea.addr_type == ADDR_WINDOWS
2191 ? LAST_WIN_NR : LAST_TAB_NR;
2192 }
2193 else
2194 {
2195 /* there is no Vim command which uses '%' and
2196 * ADDR_WINDOWS or ADDR_TABS */
2197 errormsg = (char_u *)_(e_invrange);
2198 goto doend;
2199 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002200 break;
2201 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002202 if (ARGCOUNT == 0)
2203 ea.line1 = ea.line2 = 0;
2204 else
2205 {
2206 ea.line1 = 1;
2207 ea.line2 = ARGCOUNT;
2208 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002209 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002210#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002211 case ADDR_QUICKFIX:
2212 ea.line1 = 1;
2213 ea.line2 = qf_get_size(&ea);
2214 if (ea.line2 == 0)
2215 ea.line2 = 1;
2216 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002217#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 ++ea.addr_count;
2220 }
2221 /* '*' - visual area */
2222 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2223 {
2224 pos_T *fp;
2225
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002226 if (ea.addr_type != ADDR_LINES)
2227 {
2228 errormsg = (char_u *)_(e_invrange);
2229 goto doend;
2230 }
2231
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 ++ea.cmd;
2233 if (!ea.skip)
2234 {
2235 fp = getmark('<', FALSE);
2236 if (check_mark(fp) == FAIL)
2237 goto doend;
2238 ea.line1 = fp->lnum;
2239 fp = getmark('>', FALSE);
2240 if (check_mark(fp) == FAIL)
2241 goto doend;
2242 ea.line2 = fp->lnum;
2243 ++ea.addr_count;
2244 }
2245 }
2246 }
2247 else
2248 ea.line2 = lnum;
2249 ea.addr_count++;
2250
2251 if (*ea.cmd == ';')
2252 {
2253 if (!ea.skip)
2254 curwin->w_cursor.lnum = ea.line2;
2255 }
2256 else if (*ea.cmd != ',')
2257 break;
2258 ++ea.cmd;
2259 }
2260
2261 /* One address given: set start and end lines */
2262 if (ea.addr_count == 1)
2263 {
2264 ea.line1 = ea.line2;
2265 /* ... but only implicit: really no address given */
2266 if (lnum == MAXLNUM)
2267 ea.addr_count = 0;
2268 }
2269
2270 /* Don't leave the cursor on an illegal line (caused by ';') */
2271 check_cursor_lnum();
2272
2273/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002274 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 */
2276
2277 /*
2278 * Skip ':' and any white space
2279 */
2280 ea.cmd = skipwhite(ea.cmd);
2281 while (*ea.cmd == ':')
2282 ea.cmd = skipwhite(ea.cmd + 1);
2283
2284 /*
2285 * If we got a line, but no command, then go to the line.
2286 * If we find a '|' or '\n' we set ea.nextcmd.
2287 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002288 if (*ea.cmd == NUL || *ea.cmd == '"'
2289 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 {
2291 /*
2292 * strange vi behaviour:
2293 * ":3" jumps to line 3
2294 * ":3|..." prints line 3
2295 * ":|" prints current line
2296 */
2297 if (ea.skip) /* skip this if inside :if */
2298 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002299 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 {
2301 ea.cmdidx = CMD_print;
2302 ea.argt = RANGE+COUNT+TRLBAR;
2303 if ((errormsg = invalid_range(&ea)) == NULL)
2304 {
2305 correct_range(&ea);
2306 ex_print(&ea);
2307 }
2308 }
2309 else if (ea.addr_count != 0)
2310 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002311 if (ea.line2 > curbuf->b_ml.ml_line_count)
2312 {
2313 /* With '-' in 'cpoptions' a line number past the file is an
2314 * error, otherwise put it at the end of the file. */
2315 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2316 ea.line2 = -1;
2317 else
2318 ea.line2 = curbuf->b_ml.ml_line_count;
2319 }
2320
2321 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002322 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 else
2324 {
2325 if (ea.line2 == 0)
2326 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 else
2328 curwin->w_cursor.lnum = ea.line2;
2329 beginline(BL_SOL | BL_FIX);
2330 }
2331 }
2332 goto doend;
2333 }
2334
Bram Moolenaard5005162014-08-22 23:05:54 +02002335#ifdef FEAT_AUTOCMD
2336 /* If this looks like an undefined user command and there are CmdUndefined
2337 * autocommands defined, trigger the matching autocommands. */
2338 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2339 && ASCII_ISUPPER(*ea.cmd)
2340 && has_cmdundefined())
2341 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002342 int ret;
2343
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002344 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002345 while (ASCII_ISALNUM(*p))
2346 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002347 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002348 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2349 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002350 /* If the autocommands did something and didn't cause an error, try
2351 * finding the command again. */
2352 p = (ret && !aborting()) ? find_command(&ea, NULL) : NULL;
Bram Moolenaard5005162014-08-22 23:05:54 +02002353 }
2354#endif
2355
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356#ifdef FEAT_USR_CMDS
2357 if (p == NULL)
2358 {
2359 if (!ea.skip)
2360 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2361 goto doend;
2362 }
2363 /* Check for wrong commands. */
2364 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2365 {
2366 errormsg = uc_fun_cmd();
2367 goto doend;
2368 }
2369#endif
2370 if (ea.cmdidx == CMD_SIZE)
2371 {
2372 if (!ea.skip)
2373 {
2374 STRCPY(IObuff, _("E492: Not an editor command"));
2375 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002376 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002378 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 }
2380 goto doend;
2381 }
2382
Bram Moolenaar958636c2014-10-21 20:01:58 +02002383 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2384 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002385#ifdef HAVE_EX_SCRIPT_NI
2386 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2387#endif
2388 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389
2390#ifndef FEAT_EVAL
2391 /*
2392 * When the expression evaluation is disabled, recognize the ":if" and
2393 * ":endif" commands and ignore everything in between it.
2394 */
2395 if (ea.cmdidx == CMD_if)
2396 ++if_level;
2397 if (if_level)
2398 {
2399 if (ea.cmdidx == CMD_endif)
2400 --if_level;
2401 goto doend;
2402 }
2403
2404#endif
2405
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002406 /* forced commands */
2407 if (*p == '!' && ea.cmdidx != CMD_substitute
2408 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 {
2410 ++p;
2411 ea.forceit = TRUE;
2412 }
2413 else
2414 ea.forceit = FALSE;
2415
2416/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002417 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002419 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002420 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421
2422 if (!ea.skip)
2423 {
2424#ifdef HAVE_SANDBOX
2425 if (sandbox != 0 && !(ea.argt & SBOXOK))
2426 {
2427 /* Command not allowed in sandbox. */
2428 errormsg = (char_u *)_(e_sandbox);
2429 goto doend;
2430 }
2431#endif
2432 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2433 {
2434 /* Command not allowed in non-'modifiable' buffer */
2435 errormsg = (char_u *)_(e_modifiable);
2436 goto doend;
2437 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002438
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002439 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002440 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002442 /* Command not allowed when editing the command line. */
2443#ifdef FEAT_CMDWIN
2444 if (cmdwin_type != 0)
2445 errormsg = (char_u *)_(e_cmdwin);
2446 else
2447#endif
2448 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 goto doend;
2450 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002451#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002452 /* Disallow editing another buffer when "curbuf_lock" is set.
2453 * Do allow ":edit" (check for argument later).
2454 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002455 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002456 && ea.cmdidx != CMD_edit
2457 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002458 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002459 && curbuf_locked())
2460 goto doend;
2461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462
2463 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2464 {
2465 /* no range allowed */
2466 errormsg = (char_u *)_(e_norange);
2467 goto doend;
2468 }
2469 }
2470
2471 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2472 {
2473 errormsg = (char_u *)_(e_nobang);
2474 goto doend;
2475 }
2476
2477 /*
2478 * Don't complain about the range if it is not used
2479 * (could happen if line_count is accidentally set to 0).
2480 */
2481 if (!ea.skip && !ni)
2482 {
2483 /*
2484 * If the range is backwards, ask for confirmation and, if given, swap
2485 * ea.line1 & ea.line2 so it's forwards again.
2486 * When global command is busy, don't ask, will fail below.
2487 */
2488 if (!global_busy && ea.line1 > ea.line2)
2489 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002490 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002492 if (sourcing || exmode_active)
2493 {
2494 errormsg = (char_u *)_("E493: Backwards range given");
2495 goto doend;
2496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 if (ask_yesno((char_u *)
2498 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002499 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 }
2501 lnum = ea.line1;
2502 ea.line1 = ea.line2;
2503 ea.line2 = lnum;
2504 }
2505 if ((errormsg = invalid_range(&ea)) != NULL)
2506 goto doend;
2507 }
2508
2509 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2510 ea.line2 = 1;
2511
2512 correct_range(&ea);
2513
2514#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002515 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2516 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 {
2518 /* Put the first line at the start of a closed fold, put the last line
2519 * at the end of a closed fold. */
2520 (void)hasFolding(ea.line1, &ea.line1, NULL);
2521 (void)hasFolding(ea.line2, NULL, &ea.line2);
2522 }
2523#endif
2524
2525#ifdef FEAT_QUICKFIX
2526 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002527 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 * option here, so things like % get expanded.
2529 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002530 p = replace_makeprg(&ea, p, cmdlinep);
2531 if (p == NULL)
2532 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533#endif
2534
2535 /*
2536 * Skip to start of argument.
2537 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2538 */
2539 if (ea.cmdidx == CMD_bang)
2540 ea.arg = p;
2541 else
2542 ea.arg = skipwhite(p);
2543
2544 /*
2545 * Check for "++opt=val" argument.
2546 * Must be first, allow ":w ++enc=utf8 !cmd"
2547 */
2548 if (ea.argt & ARGOPT)
2549 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2550 if (getargopt(&ea) == FAIL && !ni)
2551 {
2552 errormsg = (char_u *)_(e_invarg);
2553 goto doend;
2554 }
2555
2556 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2557 {
2558 if (*ea.arg == '>') /* append */
2559 {
2560 if (*++ea.arg != '>') /* typed wrong */
2561 {
2562 errormsg = (char_u *)_("E494: Use w or w>>");
2563 goto doend;
2564 }
2565 ea.arg = skipwhite(ea.arg + 1);
2566 ea.append = TRUE;
2567 }
2568 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2569 {
2570 ++ea.arg;
2571 ea.usefilter = TRUE;
2572 }
2573 }
2574
2575 if (ea.cmdidx == CMD_read)
2576 {
2577 if (ea.forceit)
2578 {
2579 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2580 ea.forceit = FALSE;
2581 }
2582 else if (*ea.arg == '!') /* :r !filter */
2583 {
2584 ++ea.arg;
2585 ea.usefilter = TRUE;
2586 }
2587 }
2588
2589 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2590 {
2591 ea.amount = 1;
2592 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2593 {
2594 ++ea.arg;
2595 ++ea.amount;
2596 }
2597 ea.arg = skipwhite(ea.arg);
2598 }
2599
2600 /*
2601 * Check for "+command" argument, before checking for next command.
2602 * Don't do this for ":read !cmd" and ":write !cmd".
2603 */
2604 if ((ea.argt & EDITCMD) && !ea.usefilter)
2605 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2606
2607 /*
2608 * Check for '|' to separate commands and '"' to start comments.
2609 * Don't do this for ":read !cmd" and ":write !cmd".
2610 */
2611 if ((ea.argt & TRLBAR) && !ea.usefilter)
2612 separate_nextcmd(&ea);
2613
2614 /*
2615 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002616 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2617 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002619 else if (ea.cmdidx == CMD_bang
2620 || ea.cmdidx == CMD_global
2621 || ea.cmdidx == CMD_vglobal
2622 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 {
2624 for (p = ea.arg; *p; ++p)
2625 {
2626 /* Remove one backslash before a newline, so that it's possible to
2627 * pass a newline to the shell and also a newline that is preceded
2628 * with a backslash. This makes it impossible to end a shell
2629 * command in a backslash, but that doesn't appear useful.
2630 * Halving the number of backslashes is incompatible with previous
2631 * versions. */
2632 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002633 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 else if (*p == '\n')
2635 {
2636 ea.nextcmd = p + 1;
2637 *p = NUL;
2638 break;
2639 }
2640 }
2641 }
2642
2643 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2644 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002645 buf_T *buf;
2646
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002648 switch (ea.addr_type)
2649 {
2650 case ADDR_LINES:
2651 ea.line2 = curbuf->b_ml.ml_line_count;
2652 break;
2653 case ADDR_LOADED_BUFFERS:
2654 buf = firstbuf;
2655 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2656 buf = buf->b_next;
2657 ea.line1 = buf->b_fnum;
2658 buf = lastbuf;
2659 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2660 buf = buf->b_prev;
2661 ea.line2 = buf->b_fnum;
2662 break;
2663 case ADDR_BUFFERS:
2664 ea.line1 = firstbuf->b_fnum;
2665 ea.line2 = lastbuf->b_fnum;
2666 break;
2667 case ADDR_WINDOWS:
2668 ea.line2 = LAST_WIN_NR;
2669 break;
2670 case ADDR_TABS:
2671 ea.line2 = LAST_TAB_NR;
2672 break;
2673 case ADDR_ARGUMENTS:
2674 if (ARGCOUNT == 0)
2675 ea.line1 = ea.line2 = 0;
2676 else
2677 ea.line2 = ARGCOUNT;
2678 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002679#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002680 case ADDR_QUICKFIX:
2681 ea.line2 = qf_get_size(&ea);
2682 if (ea.line2 == 0)
2683 ea.line2 = 1;
2684 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002685#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 }
2688
2689 /* accept numbered register only when no count allowed (:put) */
2690 if ( (ea.argt & REGSTR)
2691 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002692 /* Do not allow register = for user commands */
2693 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2695 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002696#ifndef FEAT_CLIPBOARD
2697 /* check these explicitly for a more specific error message */
2698 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002700 errormsg = (char_u *)_(e_invalidreg);
2701 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 }
2703#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002704 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2705 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002706 {
2707 ea.regname = *ea.arg++;
2708#ifdef FEAT_EVAL
2709 /* for '=' register: accept the rest of the line as an expression */
2710 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2711 {
2712 set_expr_line(vim_strsave(ea.arg));
2713 ea.arg += STRLEN(ea.arg);
2714 }
2715#endif
2716 ea.arg = skipwhite(ea.arg);
2717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 }
2719
2720 /*
2721 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2722 * count, it's a buffer name.
2723 */
2724 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2725 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2726 || vim_iswhite(*p)))
2727 {
2728 n = getdigits(&ea.arg);
2729 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002730 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 {
2732 errormsg = (char_u *)_(e_zerocount);
2733 goto doend;
2734 }
2735 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2736 {
2737 ea.line2 = n;
2738 if (ea.addr_count == 0)
2739 ea.addr_count = 1;
2740 }
2741 else
2742 {
2743 ea.line1 = ea.line2;
2744 ea.line2 += n - 1;
2745 ++ea.addr_count;
2746 /*
2747 * Be vi compatible: no error message for out of range.
2748 */
2749 if (ea.line2 > curbuf->b_ml.ml_line_count)
2750 ea.line2 = curbuf->b_ml.ml_line_count;
2751 }
2752 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002753
2754 /*
2755 * Check for flags: 'l', 'p' and '#'.
2756 */
2757 if (ea.argt & EXFLAGS)
2758 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002760 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002761 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 {
2763 errormsg = (char_u *)_(e_trailing);
2764 goto doend;
2765 }
2766
2767 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2768 {
2769 errormsg = (char_u *)_(e_argreq);
2770 goto doend;
2771 }
2772
2773#ifdef FEAT_EVAL
2774 /*
2775 * Skip the command when it's not going to be executed.
2776 * The commands like :if, :endif, etc. always need to be executed.
2777 * Also make an exception for commands that handle a trailing command
2778 * themselves.
2779 */
2780 if (ea.skip)
2781 {
2782 switch (ea.cmdidx)
2783 {
2784 /* commands that need evaluation */
2785 case CMD_while:
2786 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002787 case CMD_for:
2788 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 case CMD_if:
2790 case CMD_elseif:
2791 case CMD_else:
2792 case CMD_endif:
2793 case CMD_try:
2794 case CMD_catch:
2795 case CMD_finally:
2796 case CMD_endtry:
2797 case CMD_function:
2798 break;
2799
2800 /* Commands that handle '|' themselves. Check: A command should
2801 * either have the TRLBAR flag, appear in this list or appear in
2802 * the list at ":help :bar". */
2803 case CMD_aboveleft:
2804 case CMD_and:
2805 case CMD_belowright:
2806 case CMD_botright:
2807 case CMD_browse:
2808 case CMD_call:
2809 case CMD_confirm:
2810 case CMD_delfunction:
2811 case CMD_djump:
2812 case CMD_dlist:
2813 case CMD_dsearch:
2814 case CMD_dsplit:
2815 case CMD_echo:
2816 case CMD_echoerr:
2817 case CMD_echomsg:
2818 case CMD_echon:
2819 case CMD_execute:
2820 case CMD_help:
2821 case CMD_hide:
2822 case CMD_ijump:
2823 case CMD_ilist:
2824 case CMD_isearch:
2825 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002826 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 case CMD_keepjumps:
2828 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002829 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 case CMD_leftabove:
2831 case CMD_let:
2832 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002833 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002835 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002836 case CMD_noautocmd:
2837 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 case CMD_perl:
2839 case CMD_psearch:
2840 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002841 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002842 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 case CMD_return:
2844 case CMD_rightbelow:
2845 case CMD_ruby:
2846 case CMD_silent:
2847 case CMD_smagic:
2848 case CMD_snomagic:
2849 case CMD_substitute:
2850 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002851 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 case CMD_tcl:
2853 case CMD_throw:
2854 case CMD_tilde:
2855 case CMD_topleft:
2856 case CMD_unlet:
2857 case CMD_verbose:
2858 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002859 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 break;
2861
2862 default: goto doend;
2863 }
2864 }
2865#endif
2866
2867 if (ea.argt & XFILE)
2868 {
2869 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2870 goto doend;
2871 }
2872
2873#ifdef FEAT_LISTCMDS
2874 /*
2875 * Accept buffer name. Cannot be used at the same time with a buffer
2876 * number. Don't do this for a user command.
2877 */
2878 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002879 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 {
2881 /*
2882 * :bdelete, :bwipeout and :bunload take several arguments, separated
2883 * by spaces: find next space (skipping over escaped characters).
2884 * The others take one argument: ignore trailing spaces.
2885 */
2886 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2887 || ea.cmdidx == CMD_bunload)
2888 p = skiptowhite_esc(ea.arg);
2889 else
2890 {
2891 p = ea.arg + STRLEN(ea.arg);
2892 while (p > ea.arg && vim_iswhite(p[-1]))
2893 --p;
2894 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002895 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2896 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 if (ea.line2 < 0) /* failed */
2898 goto doend;
2899 ea.addr_count = 1;
2900 ea.arg = skipwhite(p);
2901 }
2902#endif
2903
2904/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002905 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 *
2907 * The "ea" structure holds the arguments that can be used.
2908 */
2909 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002910 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 ea.cookie = cookie;
2912#ifdef FEAT_EVAL
2913 ea.cstack = cstack;
2914#endif
2915
2916#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002917 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 {
2919 /*
2920 * Execute a user-defined command.
2921 */
2922 do_ucmd(&ea);
2923 }
2924 else
2925#endif
2926 {
2927 /*
2928 * Call the function to execute the command.
2929 */
2930 ea.errmsg = NULL;
2931 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2932 if (ea.errmsg != NULL)
2933 errormsg = (char_u *)_(ea.errmsg);
2934 }
2935
2936#ifdef FEAT_EVAL
2937 /*
2938 * If the command just executed called do_cmdline(), any throw or ":return"
2939 * or ":finish" encountered there must also check the cstack of the still
2940 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2941 * exception, or reanimate a returned function or finished script file and
2942 * return or finish it again.
2943 */
2944 if (need_rethrow)
2945 do_throw(cstack);
2946 else if (check_cstack)
2947 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002948 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002950 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 && current_func_returned())
2952 do_return(&ea, TRUE, FALSE, NULL);
2953 }
2954 need_rethrow = check_cstack = FALSE;
2955#endif
2956
2957doend:
2958 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2959 curwin->w_cursor.lnum = 1;
2960
2961 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2962 {
2963 if (sourcing)
2964 {
2965 if (errormsg != IObuff)
2966 {
2967 STRCPY(IObuff, errormsg);
2968 errormsg = IObuff;
2969 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002970 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 }
2972 emsg(errormsg);
2973 }
2974#ifdef FEAT_EVAL
2975 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002976 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2977 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978#endif
2979
2980 if (verbose_save >= 0)
2981 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002982#ifdef FEAT_AUTOCMD
2983 if (cmdmod.save_ei != NULL)
2984 {
2985 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002986 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2987 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002988 free_string_option(cmdmod.save_ei);
2989 }
2990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991
2992 cmdmod = save_cmdmod;
2993
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002994 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 {
2996 /* messages could be enabled for a serious error, need to check if the
2997 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002998 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002999 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 emsg_silent -= did_esilent;
3001 if (emsg_silent < 0)
3002 emsg_silent = 0;
3003 /* Restore msg_scroll, it's set by file I/O commands, even when no
3004 * message is actually displayed. */
3005 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003006
3007 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
3008 * somewhere in the line. Put it back in the first column. */
3009 if (redirecting())
3010 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 }
3012
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003013#ifdef HAVE_SANDBOX
3014 if (did_sandbox)
3015 --sandbox;
3016#endif
3017
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3019 ea.nextcmd = NULL;
3020
3021#ifdef FEAT_EVAL
3022 --ex_nesting_level;
3023#endif
3024
3025 return ea.nextcmd;
3026}
3027#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003028 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029#endif
3030
3031/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003032 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 * If there is a match advance "pp" to the argument and return TRUE.
3034 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003035 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003036checkforcmd(
3037 char_u **pp, /* start of command */
3038 char *cmd, /* name of command */
3039 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040{
3041 int i;
3042
3043 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003044 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 break;
3046 if (i >= len && !isalpha((*pp)[i]))
3047 {
3048 *pp = skipwhite(*pp + i);
3049 return TRUE;
3050 }
3051 return FALSE;
3052}
3053
3054/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003055 * Append "cmd" to the error message in IObuff.
3056 * Takes care of limiting the length and handling 0xa0, which would be
3057 * invisible otherwise.
3058 */
3059 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003060append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003061{
3062 char_u *s = cmd;
3063 char_u *d;
3064
3065 STRCAT(IObuff, ": ");
3066 d = IObuff + STRLEN(IObuff);
3067 while (*s != NUL && d - IObuff < IOSIZE - 7)
3068 {
3069 if (
3070#ifdef FEAT_MBYTE
3071 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3072#endif
3073 *s == 0xa0)
3074 {
3075 s +=
3076#ifdef FEAT_MBYTE
3077 enc_utf8 ? 2 :
3078#endif
3079 1;
3080 STRCPY(d, "<a0>");
3081 d += 4;
3082 }
3083 else
3084 MB_COPY_CHAR(s, d);
3085 }
3086 *d = NUL;
3087}
3088
3089/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003091 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003093 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 * Returns NULL for an ambiguous user command.
3095 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003097find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098{
3099 int len;
3100 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003101 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102
3103 /*
3104 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003105 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 * - the 'k' command can directly be followed by any character.
3107 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003108 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003110 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 */
3112 p = eap->cmd;
3113 if (*p == 'k')
3114 {
3115 eap->cmdidx = CMD_k;
3116 ++p;
3117 }
3118 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003119 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3120 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 || p[1] == 'g'
3122 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3123 || p[1] == 'I'
3124 || (p[1] == 'r' && p[2] != 'e')))
3125 {
3126 eap->cmdidx = CMD_substitute;
3127 ++p;
3128 }
3129 else
3130 {
3131 while (ASCII_ISALPHA(*p))
3132 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003133 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003134 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003135 while (ASCII_ISALNUM(*p))
3136 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003137
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 /* check for non-alpha command */
3139 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3140 ++p;
3141 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003142 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3143 {
3144 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3145 * :delete with the 'l' flag. Same for 'p'. */
3146 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003147 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003148 break;
3149 if (i == len - 1)
3150 {
3151 --len;
3152 if (p[-1] == 'l')
3153 eap->flags |= EXFLAG_LIST;
3154 else
3155 eap->flags |= EXFLAG_PRINT;
3156 }
3157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158
3159 if (ASCII_ISLOWER(*eap->cmd))
3160 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
3161 else
3162 eap->cmdidx = cmdidxs[26];
3163
3164 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3165 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3166 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3167 (size_t)len) == 0)
3168 {
3169#ifdef FEAT_EVAL
3170 if (full != NULL
3171 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3172 *full = TRUE;
3173#endif
3174 break;
3175 }
3176
3177#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003178 /* Look for a user defined command as a last resort. Let ":Print" be
3179 * overruled by a user defined command. */
3180 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3181 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003183 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 while (ASCII_ISALNUM(*p))
3185 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003186 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 }
3188#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003189 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 eap->cmdidx = CMD_SIZE;
3191 }
3192
3193 return p;
3194}
3195
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003196#ifdef FEAT_USR_CMDS
3197/*
3198 * Search for a user command that matches "eap->cmd".
3199 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3200 * Return a pointer to just after the command.
3201 * Return NULL if there is no matching command.
3202 */
3203 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003204find_ucmd(
3205 exarg_T *eap,
3206 char_u *p, /* end of the command (possibly including count) */
3207 int *full, /* set to TRUE for a full match */
3208 expand_T *xp, /* used for completion, NULL otherwise */
3209 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003210{
3211 int len = (int)(p - eap->cmd);
3212 int j, k, matchlen = 0;
3213 ucmd_T *uc;
3214 int found = FALSE;
3215 int possible = FALSE;
3216 char_u *cp, *np; /* Point into typed cmd and test name */
3217 garray_T *gap;
3218 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3219 only full match global is accepted. */
3220
3221 /*
3222 * Look for buffer-local user commands first, then global ones.
3223 */
3224 gap = &curbuf->b_ucmds;
3225 for (;;)
3226 {
3227 for (j = 0; j < gap->ga_len; ++j)
3228 {
3229 uc = USER_CMD_GA(gap, j);
3230 cp = eap->cmd;
3231 np = uc->uc_name;
3232 k = 0;
3233 while (k < len && *np != NUL && *cp++ == *np++)
3234 k++;
3235 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3236 {
3237 /* If finding a second match, the command is ambiguous. But
3238 * not if a buffer-local command wasn't a full match and a
3239 * global command is a full match. */
3240 if (k == len && found && *np != NUL)
3241 {
3242 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003243 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003244 amb_local = TRUE;
3245 }
3246
3247 if (!found || (k == len && *np == NUL))
3248 {
3249 /* If we matched up to a digit, then there could
3250 * be another command including the digit that we
3251 * should use instead.
3252 */
3253 if (k == len)
3254 found = TRUE;
3255 else
3256 possible = TRUE;
3257
3258 if (gap == &ucmds)
3259 eap->cmdidx = CMD_USER;
3260 else
3261 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003262 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003263 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003264 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003265
3266# ifdef FEAT_CMDL_COMPL
3267 if (compl != NULL)
3268 *compl = uc->uc_compl;
3269# ifdef FEAT_EVAL
3270 if (xp != NULL)
3271 {
3272 xp->xp_arg = uc->uc_compl_arg;
3273 xp->xp_scriptID = uc->uc_scriptID;
3274 }
3275# endif
3276# endif
3277 /* Do not search for further abbreviations
3278 * if this is an exact match. */
3279 matchlen = k;
3280 if (k == len && *np == NUL)
3281 {
3282 if (full != NULL)
3283 *full = TRUE;
3284 amb_local = FALSE;
3285 break;
3286 }
3287 }
3288 }
3289 }
3290
3291 /* Stop if we found a full match or searched all. */
3292 if (j < gap->ga_len || gap == &ucmds)
3293 break;
3294 gap = &ucmds;
3295 }
3296
3297 /* Only found ambiguous matches. */
3298 if (amb_local)
3299 {
3300 if (xp != NULL)
3301 xp->xp_context = EXPAND_UNSUCCESSFUL;
3302 return NULL;
3303 }
3304
3305 /* The match we found may be followed immediately by a number. Move "p"
3306 * back to point to it. */
3307 if (found || possible)
3308 return p + (matchlen - len);
3309 return p;
3310}
3311#endif
3312
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003314static struct cmdmod
3315{
3316 char *name;
3317 int minlen;
3318 int has_count; /* :123verbose :3tab */
3319} cmdmods[] = {
3320 {"aboveleft", 3, FALSE},
3321 {"belowright", 3, FALSE},
3322 {"botright", 2, FALSE},
3323 {"browse", 3, FALSE},
3324 {"confirm", 4, FALSE},
3325 {"hide", 3, FALSE},
3326 {"keepalt", 5, FALSE},
3327 {"keepjumps", 5, FALSE},
3328 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003329 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003330 {"leftabove", 5, FALSE},
3331 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003332 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003333 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003334 {"rightbelow", 6, FALSE},
3335 {"sandbox", 3, FALSE},
3336 {"silent", 3, FALSE},
3337 {"tab", 3, TRUE},
3338 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003339 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003340 {"verbose", 4, TRUE},
3341 {"vertical", 4, FALSE},
3342};
3343
3344/*
3345 * Return length of a command modifier (including optional count).
3346 * Return zero when it's not a modifier.
3347 */
3348 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003349modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003350{
3351 int i, j;
3352 char_u *p = cmd;
3353
3354 if (VIM_ISDIGIT(*cmd))
3355 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003356 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003357 {
3358 for (j = 0; p[j] != NUL; ++j)
3359 if (p[j] != cmdmods[i].name[j])
3360 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003361 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003362 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003363 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003364 }
3365 return 0;
3366}
3367
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368/*
3369 * Return > 0 if an Ex command "name" exists.
3370 * Return 2 if there is an exact match.
3371 * Return 3 if there is an ambiguous match.
3372 */
3373 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003374cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375{
3376 exarg_T ea;
3377 int full = FALSE;
3378 int i;
3379 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003380 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381
3382 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003383 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 {
3385 for (j = 0; name[j] != NUL; ++j)
3386 if (name[j] != cmdmods[i].name[j])
3387 break;
3388 if (name[j] == NUL && j >= cmdmods[i].minlen)
3389 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3390 }
3391
Bram Moolenaara9587612006-05-04 21:47:50 +00003392 /* Check built-in commands and user defined commands.
3393 * For ":2match" and ":3match" we need to skip the number. */
3394 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003396 p = find_command(&ea, &full);
3397 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003399 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3400 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003401 if (*skipwhite(p) != NUL)
3402 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3404}
3405#endif
3406
3407/*
3408 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3409 * we don't need/want deleted. Maybe this could be done better if we didn't
3410 * repeat all this stuff. The only problem is that they may not stay
3411 * perfectly compatible with each other, but then the command line syntax
3412 * probably won't change that much -- webb.
3413 */
3414 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003415set_one_cmd_context(
3416 expand_T *xp,
3417 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418{
3419 char_u *p;
3420 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003421 int len = 0;
3422 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3424 int compl = EXPAND_NOTHING;
3425#endif
3426#ifdef FEAT_CMDL_COMPL
3427 int delim;
3428#endif
3429 int forceit = FALSE;
3430 int usefilter = FALSE; /* filter instead of file name */
3431
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003432 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 xp->xp_pattern = buff;
3434 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003435 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
3437/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003438 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 */
3440 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3441 ;
3442 xp->xp_pattern = cmd;
3443
3444 if (*cmd == NUL)
3445 return NULL;
3446 if (*cmd == '"') /* ignore comment lines */
3447 {
3448 xp->xp_context = EXPAND_NOTHING;
3449 return NULL;
3450 }
3451
3452/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003453 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 */
3455 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 xp->xp_pattern = cmd;
3457 if (*cmd == NUL)
3458 return NULL;
3459 if (*cmd == '"')
3460 {
3461 xp->xp_context = EXPAND_NOTHING;
3462 return NULL;
3463 }
3464
3465 if (*cmd == '|' || *cmd == '\n')
3466 return cmd + 1; /* There's another command */
3467
3468 /*
3469 * Isolate the command and search for it in the command table.
3470 * Exceptions:
3471 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003472 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3474 */
3475 if (*cmd == 'k' && cmd[1] != 'e')
3476 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003477 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 p = cmd + 1;
3479 }
3480 else
3481 {
3482 p = cmd;
3483 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3484 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003485 /* a user command may contain digits */
3486 if (ASCII_ISUPPER(cmd[0]))
3487 while (ASCII_ISALNUM(*p) || *p == '*')
3488 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003489 /* for python 3.x: ":py3*" commands completion */
3490 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003491 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003492 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003493 while (ASCII_ISALPHA(*p) || *p == '*')
3494 ++p;
3495 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003496 /* check for non-alpha command */
3497 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3498 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003499 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003501 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 {
3503 xp->xp_context = EXPAND_UNSUCCESSFUL;
3504 return NULL;
3505 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003506 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003507 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3508 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3509 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 break;
3511
3512#ifdef FEAT_USR_CMDS
3513 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3515 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516#endif
3517 }
3518
3519 /*
3520 * If the cursor is touching the command, and it ends in an alpha-numeric
3521 * character, complete the command name.
3522 */
3523 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3524 return NULL;
3525
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003526 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 {
3528 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3529 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003530 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 p = cmd + 1;
3532 }
3533#ifdef FEAT_USR_CMDS
3534 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3535 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003536 ea.cmd = cmd;
3537 p = find_ucmd(&ea, p, NULL, xp,
3538# if defined(FEAT_CMDL_COMPL)
3539 &compl
3540# else
3541 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003543 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003544 if (p == NULL)
3545 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 }
3547#endif
3548 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003549 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 {
3551 /* Not still touching the command and it was an illegal one */
3552 xp->xp_context = EXPAND_UNSUCCESSFUL;
3553 return NULL;
3554 }
3555
3556 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3557
3558 if (*p == '!') /* forced commands */
3559 {
3560 forceit = TRUE;
3561 ++p;
3562 }
3563
3564/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003565 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003567 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003568 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569
3570 arg = skipwhite(p);
3571
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003572 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 {
3574 if (*arg == '>') /* append */
3575 {
3576 if (*++arg == '>')
3577 ++arg;
3578 arg = skipwhite(arg);
3579 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003580 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 {
3582 ++arg;
3583 usefilter = TRUE;
3584 }
3585 }
3586
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003587 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 {
3589 usefilter = forceit; /* :r! filter if forced */
3590 if (*arg == '!') /* :r !filter */
3591 {
3592 ++arg;
3593 usefilter = TRUE;
3594 }
3595 }
3596
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003597 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 {
3599 while (*arg == *cmd) /* allow any number of '>' or '<' */
3600 ++arg;
3601 arg = skipwhite(arg);
3602 }
3603
3604 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003605 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 {
3607 /* Check if we're in the +command */
3608 p = arg + 1;
3609 arg = skip_cmd_arg(arg, FALSE);
3610
3611 /* Still touching the command after '+'? */
3612 if (*arg == NUL)
3613 return p;
3614
3615 /* Skip space(s) after +command to get to the real argument */
3616 arg = skipwhite(arg);
3617 }
3618
3619 /*
3620 * Check for '|' to separate commands and '"' to start comments.
3621 * Don't do this for ":read !cmd" and ":write !cmd".
3622 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003623 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 {
3625 p = arg;
3626 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003627 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 p += 2;
3629 while (*p)
3630 {
3631 if (*p == Ctrl_V)
3632 {
3633 if (p[1] != NUL)
3634 ++p;
3635 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003636 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 || *p == '|' || *p == '\n')
3638 {
3639 if (*(p - 1) != '\\')
3640 {
3641 if (*p == '|' || *p == '\n')
3642 return p + 1;
3643 return NULL; /* It's a comment */
3644 }
3645 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003646 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 }
3648 }
3649
3650 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003651 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 vim_strchr((char_u *)"|\"", *arg) == NULL)
3653 return NULL;
3654
3655 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003656 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003658 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003659 while (*p && p < buff + len)
3660 {
3661 if (*p == ' ' || *p == TAB)
3662 {
3663 /* argument starts after a space */
3664 xp->xp_pattern = ++p;
3665 }
3666 else
3667 {
3668 if (*p == '\\' && *(p + 1) != NUL)
3669 ++p; /* skip over escaped character */
3670 mb_ptr_adv(p);
3671 }
3672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003674 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003676 int c;
3677 int in_quote = FALSE;
3678 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679
3680 /*
3681 * Allow spaces within back-quotes to count as part of the argument
3682 * being expanded.
3683 */
3684 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003685 p = xp->xp_pattern;
3686 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003688#ifdef FEAT_MBYTE
3689 if (has_mbyte)
3690 c = mb_ptr2char(p);
3691 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003693 c = *p;
3694 if (c == '\\' && p[1] != NUL)
3695 ++p;
3696 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 {
3698 if (!in_quote)
3699 {
3700 xp->xp_pattern = p;
3701 bow = p + 1;
3702 }
3703 in_quote = !in_quote;
3704 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003705 /* An argument can contain just about everything, except
3706 * characters that end the command and white space. */
3707 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003708#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003709 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003710#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003711 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003712 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003713 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003714 while (*p != NUL)
3715 {
3716#ifdef FEAT_MBYTE
3717 if (has_mbyte)
3718 c = mb_ptr2char(p);
3719 else
3720#endif
3721 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003722 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003723 break;
3724#ifdef FEAT_MBYTE
3725 if (has_mbyte)
3726 len = (*mb_ptr2len)(p);
3727 else
3728#endif
3729 len = 1;
3730 mb_ptr_adv(p);
3731 }
3732 if (in_quote)
3733 bow = p;
3734 else
3735 xp->xp_pattern = p;
3736 p -= len;
3737 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003738 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 }
3740
3741 /*
3742 * If we are still inside the quotes, and we passed a space, just
3743 * expand from there.
3744 */
3745 if (bow != NULL && in_quote)
3746 xp->xp_pattern = bow;
3747 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003748
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003749 /* For a shell command more chars need to be escaped. */
3750 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003751 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003752#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003753 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003754#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003755 /* When still after the command name expand executables. */
3756 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003757 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759
3760 /* Check for environment variable */
3761 if (*xp->xp_pattern == '$'
Bram Moolenaare7fedb62015-12-31 19:07:19 +01003762#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 || *xp->xp_pattern == '%'
3764#endif
3765 )
3766 {
3767 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3768 if (!vim_isIDc(*p))
3769 break;
3770 if (*p == NUL)
3771 {
3772 xp->xp_context = EXPAND_ENV_VARS;
3773 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003774#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3775 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003776 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003777 compl = EXPAND_ENV_VARS;
3778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 }
3780 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003781#if defined(FEAT_CMDL_COMPL)
3782 /* Check for user names */
3783 if (*xp->xp_pattern == '~')
3784 {
3785 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3786 ;
3787 /* Complete ~user only if it partially matches a user name.
3788 * A full match ~user<Tab> will be replaced by user's home
3789 * directory i.e. something like ~user<Tab> -> /home/user/ */
3790 if (*p == NUL && p > xp->xp_pattern + 1
3791 && match_user(xp->xp_pattern + 1) == 1)
3792 {
3793 xp->xp_context = EXPAND_USER;
3794 ++xp->xp_pattern;
3795 }
3796 }
3797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 }
3799
3800/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003801 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003803 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003805 case CMD_find:
3806 case CMD_sfind:
3807 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003808 if (xp->xp_context == EXPAND_FILES)
3809 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003810 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 case CMD_cd:
3812 case CMD_chdir:
3813 case CMD_lcd:
3814 case CMD_lchdir:
3815 if (xp->xp_context == EXPAND_FILES)
3816 xp->xp_context = EXPAND_DIRECTORIES;
3817 break;
3818 case CMD_help:
3819 xp->xp_context = EXPAND_HELP;
3820 xp->xp_pattern = arg;
3821 break;
3822
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003823 /* Command modifiers: return the argument.
3824 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003826 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 case CMD_belowright:
3828 case CMD_botright:
3829 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003830 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003831 case CMD_cdo:
3832 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003834 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 case CMD_folddoclosed:
3836 case CMD_folddoopen:
3837 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003838 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 case CMD_keepjumps:
3840 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003841 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003842 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003844 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003846 case CMD_noautocmd:
3847 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003849 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003851 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003852 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 case CMD_topleft:
3854 case CMD_verbose:
3855 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003856 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 return arg;
3858
Bram Moolenaar4f688582007-07-24 12:34:30 +00003859#ifdef FEAT_CMDL_COMPL
3860# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 case CMD_match:
3862 if (*arg == NUL || !ends_excmd(*arg))
3863 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003864 /* also complete "None" */
3865 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 arg = skipwhite(skiptowhite(arg));
3867 if (*arg != NUL)
3868 {
3869 xp->xp_context = EXPAND_NOTHING;
3870 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3871 }
3872 }
3873 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003874# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876/*
3877 * All completion for the +cmdline_compl feature goes here.
3878 */
3879
3880# ifdef FEAT_USR_CMDS
3881 case CMD_command:
3882 /* Check for attributes */
3883 while (*arg == '-')
3884 {
3885 arg++; /* Skip "-" */
3886 p = skiptowhite(arg);
3887 if (*p == NUL)
3888 {
3889 /* Cursor is still in the attribute */
3890 p = vim_strchr(arg, '=');
3891 if (p == NULL)
3892 {
3893 /* No "=", so complete attribute names */
3894 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3895 xp->xp_pattern = arg;
3896 return NULL;
3897 }
3898
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003899 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 * their arguments as well.
3901 */
3902 if (STRNICMP(arg, "complete", p - arg) == 0)
3903 {
3904 xp->xp_context = EXPAND_USER_COMPLETE;
3905 xp->xp_pattern = p + 1;
3906 return NULL;
3907 }
3908 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3909 {
3910 xp->xp_context = EXPAND_USER_NARGS;
3911 xp->xp_pattern = p + 1;
3912 return NULL;
3913 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003914 else if (STRNICMP(arg, "addr", p - arg) == 0)
3915 {
3916 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3917 xp->xp_pattern = p + 1;
3918 return NULL;
3919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 return NULL;
3921 }
3922 arg = skipwhite(p);
3923 }
3924
3925 /* After the attributes comes the new command name */
3926 p = skiptowhite(arg);
3927 if (*p == NUL)
3928 {
3929 xp->xp_context = EXPAND_USER_COMMANDS;
3930 xp->xp_pattern = arg;
3931 break;
3932 }
3933
3934 /* And finally comes a normal command */
3935 return skipwhite(p);
3936
3937 case CMD_delcommand:
3938 xp->xp_context = EXPAND_USER_COMMANDS;
3939 xp->xp_pattern = arg;
3940 break;
3941# endif
3942
3943 case CMD_global:
3944 case CMD_vglobal:
3945 delim = *arg; /* get the delimiter */
3946 if (delim)
3947 ++arg; /* skip delimiter if there is one */
3948
3949 while (arg[0] != NUL && arg[0] != delim)
3950 {
3951 if (arg[0] == '\\' && arg[1] != NUL)
3952 ++arg;
3953 ++arg;
3954 }
3955 if (arg[0] != NUL)
3956 return arg + 1;
3957 break;
3958 case CMD_and:
3959 case CMD_substitute:
3960 delim = *arg;
3961 if (delim)
3962 {
3963 /* skip "from" part */
3964 ++arg;
3965 arg = skip_regexp(arg, delim, p_magic, NULL);
3966 }
3967 /* skip "to" part */
3968 while (arg[0] != NUL && arg[0] != delim)
3969 {
3970 if (arg[0] == '\\' && arg[1] != NUL)
3971 ++arg;
3972 ++arg;
3973 }
3974 if (arg[0] != NUL) /* skip delimiter */
3975 ++arg;
3976 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3977 ++arg;
3978 if (arg[0] != NUL)
3979 return arg;
3980 break;
3981 case CMD_isearch:
3982 case CMD_dsearch:
3983 case CMD_ilist:
3984 case CMD_dlist:
3985 case CMD_ijump:
3986 case CMD_psearch:
3987 case CMD_djump:
3988 case CMD_isplit:
3989 case CMD_dsplit:
3990 arg = skipwhite(skipdigits(arg)); /* skip count */
3991 if (*arg == '/') /* Match regexp, not just whole words */
3992 {
3993 for (++arg; *arg && *arg != '/'; arg++)
3994 if (*arg == '\\' && arg[1] != NUL)
3995 arg++;
3996 if (*arg)
3997 {
3998 arg = skipwhite(arg + 1);
3999
4000 /* Check for trailing illegal characters */
4001 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4002 xp->xp_context = EXPAND_NOTHING;
4003 else
4004 return arg;
4005 }
4006 }
4007 break;
4008#ifdef FEAT_AUTOCMD
4009 case CMD_autocmd:
4010 return set_context_in_autocmd(xp, arg, FALSE);
4011
4012 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004013 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 return set_context_in_autocmd(xp, arg, TRUE);
4015#endif
4016 case CMD_set:
4017 set_context_in_set_cmd(xp, arg, 0);
4018 break;
4019 case CMD_setglobal:
4020 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4021 break;
4022 case CMD_setlocal:
4023 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4024 break;
4025 case CMD_tag:
4026 case CMD_stag:
4027 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004028 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 case CMD_tselect:
4030 case CMD_stselect:
4031 case CMD_ptselect:
4032 case CMD_tjump:
4033 case CMD_stjump:
4034 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004035 if (*p_wop != NUL)
4036 xp->xp_context = EXPAND_TAGS_LISTFILES;
4037 else
4038 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 xp->xp_pattern = arg;
4040 break;
4041 case CMD_augroup:
4042 xp->xp_context = EXPAND_AUGROUP;
4043 xp->xp_pattern = arg;
4044 break;
4045#ifdef FEAT_SYN_HL
4046 case CMD_syntax:
4047 set_context_in_syntax_cmd(xp, arg);
4048 break;
4049#endif
4050#ifdef FEAT_EVAL
4051 case CMD_let:
4052 case CMD_if:
4053 case CMD_elseif:
4054 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004055 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 case CMD_echo:
4057 case CMD_echon:
4058 case CMD_execute:
4059 case CMD_echomsg:
4060 case CMD_echoerr:
4061 case CMD_call:
4062 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004063 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 break;
4065
4066 case CMD_unlet:
4067 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4068 arg = xp->xp_pattern + 1;
4069 xp->xp_context = EXPAND_USER_VARS;
4070 xp->xp_pattern = arg;
4071 break;
4072
4073 case CMD_function:
4074 case CMD_delfunction:
4075 xp->xp_context = EXPAND_USER_FUNC;
4076 xp->xp_pattern = arg;
4077 break;
4078
4079 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004080 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 break;
4082#endif
4083 case CMD_highlight:
4084 set_context_in_highlight_cmd(xp, arg);
4085 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004086#ifdef FEAT_CSCOPE
4087 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004088 case CMD_lcscope:
4089 case CMD_scscope:
4090 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004091 break;
4092#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004093#ifdef FEAT_SIGNS
4094 case CMD_sign:
4095 set_context_in_sign_cmd(xp, arg);
4096 break;
4097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098#ifdef FEAT_LISTCMDS
4099 case CMD_bdelete:
4100 case CMD_bwipeout:
4101 case CMD_bunload:
4102 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4103 arg = xp->xp_pattern + 1;
4104 /*FALLTHROUGH*/
4105 case CMD_buffer:
4106 case CMD_sbuffer:
4107 case CMD_checktime:
4108 xp->xp_context = EXPAND_BUFFERS;
4109 xp->xp_pattern = arg;
4110 break;
4111#endif
4112#ifdef FEAT_USR_CMDS
4113 case CMD_USER:
4114 case CMD_USER_BUF:
4115 if (compl != EXPAND_NOTHING)
4116 {
4117 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004118 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 {
4120# ifdef FEAT_MENU
4121 if (compl == EXPAND_MENUS)
4122 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4123# endif
4124 if (compl == EXPAND_COMMANDS)
4125 return arg;
4126 if (compl == EXPAND_MAPPINGS)
4127 return set_context_in_map_cmd(xp, (char_u *)"map",
4128 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004129 /* Find start of last argument. */
4130 p = arg;
4131 while (*p)
4132 {
4133 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004134 /* argument starts after a space */
4135 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004136 else if (*p == '\\' && *(p + 1) != NUL)
4137 ++p; /* skip over escaped character */
4138 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 xp->xp_pattern = arg;
4141 }
4142 xp->xp_context = compl;
4143 }
4144 break;
4145#endif
4146 case CMD_map: case CMD_noremap:
4147 case CMD_nmap: case CMD_nnoremap:
4148 case CMD_vmap: case CMD_vnoremap:
4149 case CMD_omap: case CMD_onoremap:
4150 case CMD_imap: case CMD_inoremap:
4151 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004152 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004153 case CMD_smap: case CMD_snoremap:
4154 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004156 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 case CMD_unmap:
4158 case CMD_nunmap:
4159 case CMD_vunmap:
4160 case CMD_ounmap:
4161 case CMD_iunmap:
4162 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004163 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004164 case CMD_sunmap:
4165 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004167 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 case CMD_abbreviate: case CMD_noreabbrev:
4169 case CMD_cabbrev: case CMD_cnoreabbrev:
4170 case CMD_iabbrev: case CMD_inoreabbrev:
4171 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004172 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 case CMD_unabbreviate:
4174 case CMD_cunabbrev:
4175 case CMD_iunabbrev:
4176 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004177 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178#ifdef FEAT_MENU
4179 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4180 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4181 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4182 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4183 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4184 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4185 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4186 case CMD_tmenu: case CMD_tunmenu:
4187 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4188 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4189#endif
4190
4191 case CMD_colorscheme:
4192 xp->xp_context = EXPAND_COLORS;
4193 xp->xp_pattern = arg;
4194 break;
4195
4196 case CMD_compiler:
4197 xp->xp_context = EXPAND_COMPILER;
4198 xp->xp_pattern = arg;
4199 break;
4200
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004201 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004202 xp->xp_context = EXPAND_OWNSYNTAX;
4203 xp->xp_pattern = arg;
4204 break;
4205
4206 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004207 xp->xp_context = EXPAND_FILETYPE;
4208 xp->xp_pattern = arg;
4209 break;
4210
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4212 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4213 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004214 p = skiptowhite(arg);
4215 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 {
4217 xp->xp_context = EXPAND_LANGUAGE;
4218 xp->xp_pattern = arg;
4219 }
4220 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004221 {
4222 if ( STRNCMP(arg, "messages", p - arg) == 0
4223 || STRNCMP(arg, "ctype", p - arg) == 0
4224 || STRNCMP(arg, "time", p - arg) == 0)
4225 {
4226 xp->xp_context = EXPAND_LOCALES;
4227 xp->xp_pattern = skipwhite(p);
4228 }
4229 else
4230 xp->xp_context = EXPAND_NOTHING;
4231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 break;
4233#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004234#if defined(FEAT_PROFILE)
4235 case CMD_profile:
4236 set_context_in_profile_cmd(xp, arg);
4237 break;
4238#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004239 case CMD_behave:
4240 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004241 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004242 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004244#if defined(FEAT_CMDHIST)
4245 case CMD_history:
4246 xp->xp_context = EXPAND_HISTORY;
4247 xp->xp_pattern = arg;
4248 break;
4249#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004250#if defined(FEAT_PROFILE)
4251 case CMD_syntime:
4252 xp->xp_context = EXPAND_SYNTIME;
4253 xp->xp_pattern = arg;
4254 break;
4255#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004256
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257#endif /* FEAT_CMDL_COMPL */
4258
4259 default:
4260 break;
4261 }
4262 return NULL;
4263}
4264
4265/*
4266 * skip a range specifier of the form: addr [,addr] [;addr] ..
4267 *
4268 * Backslashed delimiters after / or ? will be skipped, and commands will
4269 * not be expanded between /'s and ?'s or after "'".
4270 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004271 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 * Returns the "cmd" pointer advanced to beyond the range.
4273 */
4274 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004275skip_range(
4276 char_u *cmd,
4277 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004279 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280
Bram Moolenaardf177f62005-02-22 08:39:57 +00004281 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 {
4283 if (*cmd == '\'')
4284 {
4285 if (*++cmd == NUL && ctx != NULL)
4286 *ctx = EXPAND_NOTHING;
4287 }
4288 else if (*cmd == '/' || *cmd == '?')
4289 {
4290 delim = *cmd++;
4291 while (*cmd != NUL && *cmd != delim)
4292 if (*cmd++ == '\\' && *cmd != NUL)
4293 ++cmd;
4294 if (*cmd == NUL && ctx != NULL)
4295 *ctx = EXPAND_NOTHING;
4296 }
4297 if (*cmd != NUL)
4298 ++cmd;
4299 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004300
4301 /* Skip ":" and white space. */
4302 while (*cmd == ':')
4303 cmd = skipwhite(cmd + 1);
4304
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 return cmd;
4306}
4307
4308/*
4309 * get a single EX address
4310 *
4311 * Set ptr to the next character after the part that was interpreted.
4312 * Set ptr to NULL when an error is encountered.
4313 *
4314 * Return MAXLNUM when no Ex address was found.
4315 */
4316 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004317get_address(
4318 exarg_T *eap UNUSED,
4319 char_u **ptr,
4320 int addr_type, /* flag: one of ADDR_LINES, ... */
4321 int skip, /* only skip the address, don't use it */
4322 int to_other_file) /* flag: may jump to other file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323{
4324 int c;
4325 int i;
4326 long n;
4327 char_u *cmd;
4328 pos_T pos;
4329 pos_T *fp;
4330 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004331 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332
4333 cmd = skipwhite(*ptr);
4334 lnum = MAXLNUM;
4335 do
4336 {
4337 switch (*cmd)
4338 {
4339 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004340 ++cmd;
4341 switch (addr_type)
4342 {
4343 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 lnum = curwin->w_cursor.lnum;
4345 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004346 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004347 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004348 break;
4349 case ADDR_ARGUMENTS:
4350 lnum = curwin->w_arg_idx + 1;
4351 break;
4352 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004353 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004354 lnum = curbuf->b_fnum;
4355 break;
4356 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004357 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004358 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004359#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004360 case ADDR_QUICKFIX:
4361 lnum = qf_get_cur_valid_idx(eap);
4362 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004363#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004364 }
4365 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366
4367 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004368 ++cmd;
4369 switch (addr_type)
4370 {
4371 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 lnum = curbuf->b_ml.ml_line_count;
4373 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004374 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004375 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004376 break;
4377 case ADDR_ARGUMENTS:
4378 lnum = ARGCOUNT;
4379 break;
4380 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004381 buf = lastbuf;
4382 while (buf->b_ml.ml_mfp == NULL)
4383 {
4384 if (buf->b_prev == NULL)
4385 break;
4386 buf = buf->b_prev;
4387 }
4388 lnum = buf->b_fnum;
4389 break;
4390 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004391 lnum = lastbuf->b_fnum;
4392 break;
4393 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004394 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004395 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004396#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004397 case ADDR_QUICKFIX:
4398 lnum = qf_get_size(eap);
4399 if (lnum == 0)
4400 lnum = 1;
4401 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004402#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004403 }
4404 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405
4406 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004407 if (*++cmd == NUL)
4408 {
4409 cmd = NULL;
4410 goto error;
4411 }
4412 if (addr_type != ADDR_LINES)
4413 {
4414 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004415 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004416 goto error;
4417 }
4418 if (skip)
4419 ++cmd;
4420 else
4421 {
4422 /* Only accept a mark in another file when it is
4423 * used by itself: ":'M". */
4424 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4425 ++cmd;
4426 if (fp == (pos_T *)-1)
4427 /* Jumped to another file. */
4428 lnum = curwin->w_cursor.lnum;
4429 else
4430 {
4431 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 {
4433 cmd = NULL;
4434 goto error;
4435 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004436 lnum = fp->lnum;
4437 }
4438 }
4439 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440
4441 case '/':
4442 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004443 c = *cmd++;
4444 if (addr_type != ADDR_LINES)
4445 {
4446 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004447 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004448 goto error;
4449 }
4450 if (skip) /* skip "/pat/" */
4451 {
4452 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4453 if (*cmd == c)
4454 ++cmd;
4455 }
4456 else
4457 {
4458 pos = curwin->w_cursor; /* save curwin->w_cursor */
4459 /*
4460 * When '/' or '?' follows another address, start
4461 * from there.
4462 */
4463 if (lnum != MAXLNUM)
4464 curwin->w_cursor.lnum = lnum;
4465 /*
4466 * Start a forward search at the end of the line.
4467 * Start a backward search at the start of the line.
4468 * This makes sure we never match in the current
4469 * line, and can match anywhere in the
4470 * next/previous line.
4471 */
4472 if (c == '/')
4473 curwin->w_cursor.col = MAXCOL;
4474 else
4475 curwin->w_cursor.col = 0;
4476 searchcmdlen = 0;
4477 if (!do_search(NULL, c, cmd, 1L,
4478 SEARCH_HIS | SEARCH_MSG, NULL))
4479 {
4480 curwin->w_cursor = pos;
4481 cmd = NULL;
4482 goto error;
4483 }
4484 lnum = curwin->w_cursor.lnum;
4485 curwin->w_cursor = pos;
4486 /* adjust command string pointer */
4487 cmd += searchcmdlen;
4488 }
4489 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490
4491 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004492 ++cmd;
4493 if (addr_type != ADDR_LINES)
4494 {
4495 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004496 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004497 goto error;
4498 }
4499 if (*cmd == '&')
4500 i = RE_SUBST;
4501 else if (*cmd == '?' || *cmd == '/')
4502 i = RE_SEARCH;
4503 else
4504 {
4505 EMSG(_(e_backslash));
4506 cmd = NULL;
4507 goto error;
4508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004510 if (!skip)
4511 {
4512 /*
4513 * When search follows another address, start from
4514 * there.
4515 */
4516 if (lnum != MAXLNUM)
4517 pos.lnum = lnum;
4518 else
4519 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004521 /*
4522 * Start the search just like for the above
4523 * do_search().
4524 */
4525 if (*cmd != '?')
4526 pos.col = MAXCOL;
4527 else
4528 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004529#ifdef FEAT_VIRTUALEDIT
4530 pos.coladd = 0;
4531#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004532 if (searchit(curwin, curbuf, &pos,
4533 *cmd == '?' ? BACKWARD : FORWARD,
4534 (char_u *)"", 1L, SEARCH_MSG,
4535 i, (linenr_T)0, NULL) != FAIL)
4536 lnum = pos.lnum;
4537 else
4538 {
4539 cmd = NULL;
4540 goto error;
4541 }
4542 }
4543 ++cmd;
4544 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545
4546 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004547 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4548 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 }
4550
4551 for (;;)
4552 {
4553 cmd = skipwhite(cmd);
4554 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4555 break;
4556
4557 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004558 {
4559 switch (addr_type)
4560 {
4561 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004562 /* "+1" is same as ".+1" */
4563 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004564 break;
4565 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004566 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004567 break;
4568 case ADDR_ARGUMENTS:
4569 lnum = curwin->w_arg_idx + 1;
4570 break;
4571 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004572 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004573 lnum = curbuf->b_fnum;
4574 break;
4575 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004576 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004577 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004578#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004579 case ADDR_QUICKFIX:
4580 lnum = qf_get_cur_valid_idx(eap);
4581 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004582#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004583 }
4584 }
4585
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 if (VIM_ISDIGIT(*cmd))
4587 i = '+'; /* "number" is same as "+number" */
4588 else
4589 i = *cmd++;
4590 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4591 n = 1;
4592 else
4593 n = getdigits(&cmd);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004594 if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004595 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004596 lnum = compute_buffer_local_count(
4597 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004598 else if (i == '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 lnum -= n;
4600 else
4601 lnum += n;
4602 }
4603 } while (*cmd == '/' || *cmd == '?');
4604
4605error:
4606 *ptr = cmd;
4607 return lnum;
4608}
4609
4610/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004611 * Get flags from an Ex command argument.
4612 */
4613 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004614get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004615{
4616 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4617 {
4618 if (*eap->arg == 'l')
4619 eap->flags |= EXFLAG_LIST;
4620 else if (*eap->arg == 'p')
4621 eap->flags |= EXFLAG_PRINT;
4622 else
4623 eap->flags |= EXFLAG_NR;
4624 eap->arg = skipwhite(eap->arg + 1);
4625 }
4626}
4627
4628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 * Function called for command which is Not Implemented. NI!
4630 */
4631 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004632ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633{
4634 if (!eap->skip)
4635 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4636}
4637
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004638#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639/*
4640 * Function called for script command which is Not Implemented. NI!
4641 * Skips over ":perl <<EOF" constructs.
4642 */
4643 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004644ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645{
4646 if (!eap->skip)
4647 ex_ni(eap);
4648 else
4649 vim_free(script_get(eap, eap->arg));
4650}
4651#endif
4652
4653/*
4654 * Check range in Ex command for validity.
4655 * Return NULL when valid, error message when invalid.
4656 */
4657 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004658invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004660 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 if ( eap->line1 < 0
4662 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004663 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004665
4666 if (eap->argt & RANGE)
4667 {
4668 switch(eap->addr_type)
4669 {
4670 case ADDR_LINES:
4671 if (!(eap->argt & NOTADR)
4672 && eap->line2 > curbuf->b_ml.ml_line_count
4673#ifdef FEAT_DIFF
4674 + (eap->cmdidx == CMD_diffget)
4675#endif
4676 )
4677 return (char_u *)_(e_invrange);
4678 break;
4679 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004680 /* add 1 if ARGCOUNT is 0 */
4681 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004682 return (char_u *)_(e_invrange);
4683 break;
4684 case ADDR_BUFFERS:
4685 if (eap->line1 < firstbuf->b_fnum
4686 || eap->line2 > lastbuf->b_fnum)
4687 return (char_u *)_(e_invrange);
4688 break;
4689 case ADDR_LOADED_BUFFERS:
4690 buf = firstbuf;
4691 while (buf->b_ml.ml_mfp == NULL)
4692 {
4693 if (buf->b_next == NULL)
4694 return (char_u *)_(e_invrange);
4695 buf = buf->b_next;
4696 }
4697 if (eap->line1 < buf->b_fnum)
4698 return (char_u *)_(e_invrange);
4699 buf = lastbuf;
4700 while (buf->b_ml.ml_mfp == NULL)
4701 {
4702 if (buf->b_prev == NULL)
4703 return (char_u *)_(e_invrange);
4704 buf = buf->b_prev;
4705 }
4706 if (eap->line2 > buf->b_fnum)
4707 return (char_u *)_(e_invrange);
4708 break;
4709 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004710 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004711 return (char_u *)_(e_invrange);
4712 break;
4713 case ADDR_TABS:
4714 if (eap->line2 > LAST_TAB_NR)
4715 return (char_u *)_(e_invrange);
4716 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004717#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004718 case ADDR_QUICKFIX:
4719 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4720 return (char_u *)_(e_invrange);
4721 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004722#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004723 }
4724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 return NULL;
4726}
4727
4728/*
4729 * Correct the range for zero line number, if required.
4730 */
4731 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004732correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733{
4734 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4735 {
4736 if (eap->line1 == 0)
4737 eap->line1 = 1;
4738 if (eap->line2 == 0)
4739 eap->line2 = 1;
4740 }
4741}
4742
Bram Moolenaar748bf032005-02-02 23:04:36 +00004743#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004744static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004745
4746/*
4747 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4748 * pattern. Otherwise return eap->arg.
4749 */
4750 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004751skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004752{
4753 char_u *p = eap->arg;
4754
Bram Moolenaara37420f2006-02-04 22:37:47 +00004755 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4756 || eap->cmdidx == CMD_vimgrepadd
4757 || eap->cmdidx == CMD_lvimgrepadd
4758 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004759 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004760 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004761 if (p == NULL)
4762 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004763 }
4764 return p;
4765}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004766
4767/*
4768 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4769 * in the command line, so that things like % get expanded.
4770 */
4771 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004772replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004773{
4774 char_u *new_cmdline;
4775 char_u *program;
4776 char_u *pos;
4777 char_u *ptr;
4778 int len;
4779 int i;
4780
4781 /*
4782 * Don't do it when ":vimgrep" is used for ":grep".
4783 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004784 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4785 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4786 || eap->cmdidx == CMD_grepadd
4787 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004788 && !grep_internal(eap->cmdidx))
4789 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004790 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4791 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004792 {
4793 if (*curbuf->b_p_gp == NUL)
4794 program = p_gp;
4795 else
4796 program = curbuf->b_p_gp;
4797 }
4798 else
4799 {
4800 if (*curbuf->b_p_mp == NUL)
4801 program = p_mp;
4802 else
4803 program = curbuf->b_p_mp;
4804 }
4805
4806 p = skipwhite(p);
4807
4808 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4809 {
4810 /* replace $* by given arguments */
4811 i = 1;
4812 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4813 ++i;
4814 len = (int)STRLEN(p);
4815 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4816 if (new_cmdline == NULL)
4817 return NULL; /* out of memory */
4818 ptr = new_cmdline;
4819 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4820 {
4821 i = (int)(pos - program);
4822 STRNCPY(ptr, program, i);
4823 STRCPY(ptr += i, p);
4824 ptr += len;
4825 program = pos + 2;
4826 }
4827 STRCPY(ptr, program);
4828 }
4829 else
4830 {
4831 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4832 if (new_cmdline == NULL)
4833 return NULL; /* out of memory */
4834 STRCPY(new_cmdline, program);
4835 STRCAT(new_cmdline, " ");
4836 STRCAT(new_cmdline, p);
4837 }
4838 msg_make(p);
4839
4840 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4841 vim_free(*cmdlinep);
4842 *cmdlinep = new_cmdline;
4843 p = new_cmdline;
4844 }
4845 return p;
4846}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004847#endif
4848
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849/*
4850 * Expand file name in Ex command argument.
4851 * Return FAIL for failure, OK otherwise.
4852 */
4853 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004854expand_filename(
4855 exarg_T *eap,
4856 char_u **cmdlinep,
4857 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858{
4859 int has_wildcards; /* need to expand wildcards */
4860 char_u *repl;
4861 int srclen;
4862 char_u *p;
4863 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004864 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865
Bram Moolenaar748bf032005-02-02 23:04:36 +00004866#ifdef FEAT_QUICKFIX
4867 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4868 p = skip_grep_pat(eap);
4869#else
4870 p = eap->arg;
4871#endif
4872
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 /*
4874 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4875 * the file name contains a wildcard it should not cause expanding.
4876 * (it will be expanded anyway if there is a wildcard before replacing).
4877 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004878 has_wildcards = mch_has_wildcard(p);
4879 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004881#ifdef FEAT_EVAL
4882 /* Skip over `=expr`, wildcards in it are not expanded. */
4883 if (p[0] == '`' && p[1] == '=')
4884 {
4885 p += 2;
4886 (void)skip_expr(&p);
4887 if (*p == '`')
4888 ++p;
4889 continue;
4890 }
4891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 /*
4893 * Quick check if this cannot be the start of a special string.
4894 * Also removes backslash before '%', '#' and '<'.
4895 */
4896 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4897 {
4898 ++p;
4899 continue;
4900 }
4901
4902 /*
4903 * Try to find a match at this position.
4904 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004905 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4906 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 if (*errormsgp != NULL) /* error detected */
4908 return FAIL;
4909 if (repl == NULL) /* no match found */
4910 {
4911 p += srclen;
4912 continue;
4913 }
4914
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004915 /* Wildcards won't be expanded below, the replacement is taken
4916 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4917 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4918 {
4919 char_u *l = repl;
4920
4921 repl = expand_env_save(repl);
4922 vim_free(l);
4923 }
4924
Bram Moolenaar63b92542007-03-27 14:57:09 +00004925 /* Need to escape white space et al. with a backslash.
4926 * Don't do this for:
4927 * - replacement that already has been escaped: "##"
4928 * - shell commands (may have to use quotes instead).
4929 * - non-unix systems when there is a single argument (spaces don't
4930 * separate arguments then).
4931 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004933 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 && eap->cmdidx != CMD_bang
4935 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004936 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004938 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004940 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941#ifndef UNIX
4942 && !(eap->argt & NOSPC)
4943#endif
4944 )
4945 {
4946 char_u *l;
4947#ifdef BACKSLASH_IN_FILENAME
4948 /* Don't escape a backslash here, because rem_backslash() doesn't
4949 * remove it later. */
4950 static char_u *nobslash = (char_u *)" \t\"|";
4951# define ESCAPE_CHARS nobslash
4952#else
4953# define ESCAPE_CHARS escape_chars
4954#endif
4955
4956 for (l = repl; *l; ++l)
4957 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4958 {
4959 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4960 if (l != NULL)
4961 {
4962 vim_free(repl);
4963 repl = l;
4964 }
4965 break;
4966 }
4967 }
4968
4969 /* For a shell command a '!' must be escaped. */
4970 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004971 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 {
4973 char_u *l;
4974
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004975 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 if (l != NULL)
4977 {
4978 vim_free(repl);
4979 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 }
4981 }
4982
4983 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4984 vim_free(repl);
4985 if (p == NULL)
4986 return FAIL;
4987 }
4988
4989 /*
4990 * One file argument: Expand wildcards.
4991 * Don't do this with ":r !command" or ":w !command".
4992 */
4993 if ((eap->argt & NOSPC) && !eap->usefilter)
4994 {
4995 /*
4996 * May do this twice:
4997 * 1. Replace environment variables.
4998 * 2. Replace any other wildcards, remove backslashes.
4999 */
5000 for (n = 1; n <= 2; ++n)
5001 {
5002 if (n == 2)
5003 {
5004#ifdef UNIX
5005 /*
5006 * Only for Unix we check for more than one file name.
5007 * For other systems spaces are considered to be part
5008 * of the file name.
5009 * Only check here if there is no wildcard, otherwise
5010 * ExpandOne() will check for errors. This allows
5011 * ":e `ls ve*.c`" on Unix.
5012 */
5013 if (!has_wildcards)
5014 for (p = eap->arg; *p; ++p)
5015 {
5016 /* skip escaped characters */
5017 if (p[1] && (*p == '\\' || *p == Ctrl_V))
5018 ++p;
5019 else if (vim_iswhite(*p))
5020 {
5021 *errormsgp = (char_u *)_("E172: Only one file name allowed");
5022 return FAIL;
5023 }
5024 }
5025#endif
5026
5027 /*
5028 * Halve the number of backslashes (this is Vi compatible).
5029 * For Unix and OS/2, when wildcards are expanded, this is
5030 * done by ExpandOne() below.
5031 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005032#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 if (!has_wildcards)
5034#endif
5035 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 }
5037
5038 if (has_wildcards)
5039 {
5040 if (n == 1)
5041 {
5042 /*
5043 * First loop: May expand environment variables. This
5044 * can be done much faster with expand_env() than with
5045 * something else (e.g., calling a shell).
5046 * After expanding environment variables, check again
5047 * if there are still wildcards present.
5048 */
5049 if (vim_strchr(eap->arg, '$') != NULL
5050 || vim_strchr(eap->arg, '~') != NULL)
5051 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005052 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005053 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 has_wildcards = mch_has_wildcard(NameBuff);
5055 p = NameBuff;
5056 }
5057 else
5058 p = NULL;
5059 }
5060 else /* n == 2 */
5061 {
5062 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005063 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064
5065 ExpandInit(&xpc);
5066 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005067 if (p_wic)
5068 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005070 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 if (p == NULL)
5072 return FAIL;
5073 }
5074 if (p != NULL)
5075 {
5076 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5077 p, cmdlinep);
5078 if (n == 2) /* p came from ExpandOne() */
5079 vim_free(p);
5080 }
5081 }
5082 }
5083 }
5084 return OK;
5085}
5086
5087/*
5088 * Replace part of the command line, keeping eap->cmd, eap->arg and
5089 * eap->nextcmd correct.
5090 * "src" points to the part that is to be replaced, of length "srclen".
5091 * "repl" is the replacement string.
5092 * Returns a pointer to the character after the replaced string.
5093 * Returns NULL for failure.
5094 */
5095 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005096repl_cmdline(
5097 exarg_T *eap,
5098 char_u *src,
5099 int srclen,
5100 char_u *repl,
5101 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102{
5103 int len;
5104 int i;
5105 char_u *new_cmdline;
5106
5107 /*
5108 * The new command line is build in new_cmdline[].
5109 * First allocate it.
5110 * Careful: a "+cmd" argument may have been NUL terminated.
5111 */
5112 len = (int)STRLEN(repl);
5113 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005114 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5116 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5117 return NULL; /* out of memory! */
5118
5119 /*
5120 * Copy the stuff before the expanded part.
5121 * Copy the expanded stuff.
5122 * Copy what came after the expanded part.
5123 * Copy the next commands, if there are any.
5124 */
5125 i = (int)(src - *cmdlinep); /* length of part before match */
5126 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005127
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 mch_memmove(new_cmdline + i, repl, (size_t)len);
5129 i += len; /* remember the end of the string */
5130 STRCPY(new_cmdline + i, src + srclen);
5131 src = new_cmdline + i; /* remember where to continue */
5132
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005133 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 {
5135 i = (int)STRLEN(new_cmdline) + 1;
5136 STRCPY(new_cmdline + i, eap->nextcmd);
5137 eap->nextcmd = new_cmdline + i;
5138 }
5139 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5140 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5141 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5142 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5143 vim_free(*cmdlinep);
5144 *cmdlinep = new_cmdline;
5145
5146 return src;
5147}
5148
5149/*
5150 * Check for '|' to separate commands and '"' to start comments.
5151 */
5152 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005153separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154{
5155 char_u *p;
5156
Bram Moolenaar86b68352004-12-27 21:59:20 +00005157#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005158 p = skip_grep_pat(eap);
5159#else
5160 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005161#endif
5162
5163 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 {
5165 if (*p == Ctrl_V)
5166 {
5167 if (eap->argt & (USECTRLV | XFILE))
5168 ++p; /* skip CTRL-V and next char */
5169 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005170 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005171 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 if (*p == NUL) /* stop at NUL after CTRL-V */
5173 break;
5174 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005175
5176#ifdef FEAT_EVAL
5177 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005178 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005179 {
5180 p += 2;
5181 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005182 }
5183#endif
5184
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 /* Check for '"': start of comment or '|': next command */
5186 /* :@" and :*" do not start a comment!
5187 * :redir @" doesn't either. */
5188 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5189 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5190 || p != eap->arg)
5191 && (eap->cmdidx != CMD_redir
5192 || p != eap->arg + 1 || p[-1] != '@'))
5193 || *p == '|' || *p == '\n')
5194 {
5195 /*
5196 * We remove the '\' before the '|', unless USECTRLV is used
5197 * AND 'b' is present in 'cpoptions'.
5198 */
5199 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5200 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5201 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005202 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 --p;
5204 }
5205 else
5206 {
5207 eap->nextcmd = check_nextcmd(p);
5208 *p = NUL;
5209 break;
5210 }
5211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005213
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5215 del_trailing_spaces(eap->arg);
5216}
5217
5218/*
5219 * get + command from ex argument
5220 */
5221 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005222getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223{
5224 char_u *arg = *argp;
5225 char_u *command = NULL;
5226
5227 if (*arg == '+') /* +[command] */
5228 {
5229 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005230 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 command = dollar_command;
5232 else
5233 {
5234 command = arg;
5235 arg = skip_cmd_arg(command, TRUE);
5236 if (*arg != NUL)
5237 *arg++ = NUL; /* terminate command with NUL */
5238 }
5239
5240 arg = skipwhite(arg); /* skip over spaces */
5241 *argp = arg;
5242 }
5243 return command;
5244}
5245
5246/*
5247 * Find end of "+command" argument. Skip over "\ " and "\\".
5248 */
5249 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005250skip_cmd_arg(
5251 char_u *p,
5252 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253{
5254 while (*p && !vim_isspace(*p))
5255 {
5256 if (*p == '\\' && p[1] != NUL)
5257 {
5258 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005259 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 else
5261 ++p;
5262 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005263 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 }
5265 return p;
5266}
5267
5268/*
5269 * Get "++opt=arg" argument.
5270 * Return FAIL or OK.
5271 */
5272 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005273getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274{
5275 char_u *arg = eap->arg + 2;
5276 int *pp = NULL;
5277#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005278 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 char_u *p;
5280#endif
5281
5282 /* ":edit ++[no]bin[ary] file" */
5283 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5284 {
5285 if (*arg == 'n')
5286 {
5287 arg += 2;
5288 eap->force_bin = FORCE_NOBIN;
5289 }
5290 else
5291 eap->force_bin = FORCE_BIN;
5292 if (!checkforcmd(&arg, "binary", 3))
5293 return FAIL;
5294 eap->arg = skipwhite(arg);
5295 return OK;
5296 }
5297
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005298 /* ":read ++edit file" */
5299 if (STRNCMP(arg, "edit", 4) == 0)
5300 {
5301 eap->read_edit = TRUE;
5302 eap->arg = skipwhite(arg + 4);
5303 return OK;
5304 }
5305
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 if (STRNCMP(arg, "ff", 2) == 0)
5307 {
5308 arg += 2;
5309 pp = &eap->force_ff;
5310 }
5311 else if (STRNCMP(arg, "fileformat", 10) == 0)
5312 {
5313 arg += 10;
5314 pp = &eap->force_ff;
5315 }
5316#ifdef FEAT_MBYTE
5317 else if (STRNCMP(arg, "enc", 3) == 0)
5318 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005319 if (STRNCMP(arg, "encoding", 8) == 0)
5320 arg += 8;
5321 else
5322 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 pp = &eap->force_enc;
5324 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005325 else if (STRNCMP(arg, "bad", 3) == 0)
5326 {
5327 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005328 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330#endif
5331
5332 if (pp == NULL || *arg != '=')
5333 return FAIL;
5334
5335 ++arg;
5336 *pp = (int)(arg - eap->cmd);
5337 arg = skip_cmd_arg(arg, FALSE);
5338 eap->arg = skipwhite(arg);
5339 *arg = NUL;
5340
5341#ifdef FEAT_MBYTE
5342 if (pp == &eap->force_ff)
5343 {
5344#endif
5345 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5346 return FAIL;
5347#ifdef FEAT_MBYTE
5348 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005349 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 {
5351 /* Make 'fileencoding' lower case. */
5352 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5353 *p = TOLOWER_ASC(*p);
5354 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005355 else
5356 {
5357 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5358 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005359 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005360 if (STRICMP(p, "keep") == 0)
5361 eap->bad_char = BAD_KEEP;
5362 else if (STRICMP(p, "drop") == 0)
5363 eap->bad_char = BAD_DROP;
5364 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5365 eap->bad_char = *p;
5366 else
5367 return FAIL;
5368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369#endif
5370
5371 return OK;
5372}
5373
5374/*
5375 * ":abbreviate" and friends.
5376 */
5377 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005378ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379{
5380 do_exmap(eap, TRUE); /* almost the same as mapping */
5381}
5382
5383/*
5384 * ":map" and friends.
5385 */
5386 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005387ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388{
5389 /*
5390 * If we are sourcing .exrc or .vimrc in current directory we
5391 * print the mappings for security reasons.
5392 */
5393 if (secure)
5394 {
5395 secure = 2;
5396 msg_outtrans(eap->cmd);
5397 msg_putchar('\n');
5398 }
5399 do_exmap(eap, FALSE);
5400}
5401
5402/*
5403 * ":unmap" and friends.
5404 */
5405 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005406ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407{
5408 do_exmap(eap, FALSE);
5409}
5410
5411/*
5412 * ":mapclear" and friends.
5413 */
5414 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005415ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416{
5417 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5418}
5419
5420/*
5421 * ":abclear" and friends.
5422 */
5423 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005424ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425{
5426 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5427}
5428
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005429#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005431ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432{
5433 /*
5434 * Disallow auto commands from .exrc and .vimrc in current
5435 * directory for security reasons.
5436 */
5437 if (secure)
5438 {
5439 secure = 2;
5440 eap->errmsg = e_curdir;
5441 }
5442 else if (eap->cmdidx == CMD_autocmd)
5443 do_autocmd(eap->arg, eap->forceit);
5444 else
5445 do_augroup(eap->arg, eap->forceit);
5446}
5447
5448/*
5449 * ":doautocmd": Apply the automatic commands to the current buffer.
5450 */
5451 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005452ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005454 char_u *arg = eap->arg;
5455 int call_do_modelines = check_nomodeline(&arg);
5456
5457 (void)do_doautocmd(arg, TRUE);
5458 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5459 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460}
5461#endif
5462
5463#ifdef FEAT_LISTCMDS
5464/*
5465 * :[N]bunload[!] [N] [bufname] unload buffer
5466 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5467 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5468 */
5469 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005470ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471{
5472 eap->errmsg = do_bufdel(
5473 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5474 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5475 : DOBUF_UNLOAD, eap->arg,
5476 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5477}
5478
5479/*
5480 * :[N]buffer [N] to buffer N
5481 * :[N]sbuffer [N] to buffer N
5482 */
5483 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005484ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485{
5486 if (*eap->arg)
5487 eap->errmsg = e_trailing;
5488 else
5489 {
5490 if (eap->addr_count == 0) /* default is current buffer */
5491 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5492 else
5493 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005494 if (eap->do_ecmd_cmd != NULL)
5495 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 }
5497}
5498
5499/*
5500 * :[N]bmodified [N] to next mod. buffer
5501 * :[N]sbmodified [N] to next mod. buffer
5502 */
5503 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005504ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505{
5506 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005507 if (eap->do_ecmd_cmd != NULL)
5508 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509}
5510
5511/*
5512 * :[N]bnext [N] to next buffer
5513 * :[N]sbnext [N] split and to next buffer
5514 */
5515 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005516ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517{
5518 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005519 if (eap->do_ecmd_cmd != NULL)
5520 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521}
5522
5523/*
5524 * :[N]bNext [N] to previous buffer
5525 * :[N]bprevious [N] to previous buffer
5526 * :[N]sbNext [N] split and to previous buffer
5527 * :[N]sbprevious [N] split and to previous buffer
5528 */
5529 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005530ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531{
5532 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005533 if (eap->do_ecmd_cmd != NULL)
5534 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535}
5536
5537/*
5538 * :brewind to first buffer
5539 * :bfirst to first buffer
5540 * :sbrewind split and to first buffer
5541 * :sbfirst split and to first buffer
5542 */
5543 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005544ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545{
5546 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005547 if (eap->do_ecmd_cmd != NULL)
5548 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549}
5550
5551/*
5552 * :blast to last buffer
5553 * :sblast split and to last buffer
5554 */
5555 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005556ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557{
5558 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005559 if (eap->do_ecmd_cmd != NULL)
5560 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561}
5562#endif
5563
5564 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005565ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566{
5567 return (c == NUL || c == '|' || c == '"' || c == '\n');
5568}
5569
5570#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5571 || defined(PROTO)
5572/*
5573 * Return the next command, after the first '|' or '\n'.
5574 * Return NULL if not found.
5575 */
5576 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005577find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578{
5579 while (*p != '|' && *p != '\n')
5580 {
5581 if (*p == NUL)
5582 return NULL;
5583 ++p;
5584 }
5585 return (p + 1);
5586}
5587#endif
5588
5589/*
5590 * Check if *p is a separator between Ex commands.
5591 * Return NULL if it isn't, (p + 1) if it is.
5592 */
5593 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005594check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595{
5596 p = skipwhite(p);
5597 if (*p == '|' || *p == '\n')
5598 return (p + 1);
5599 else
5600 return NULL;
5601}
5602
5603/*
5604 * - if there are more files to edit
5605 * - and this is the last window
5606 * - and forceit not used
5607 * - and not repeated twice on a row
5608 * return FAIL and give error message if 'message' TRUE
5609 * return OK otherwise
5610 */
5611 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005612check_more(
5613 int message, /* when FALSE check only, no messages */
5614 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615{
5616 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5617
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005618 if (!forceit && only_one_window()
5619 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 {
5621 if (message)
5622 {
5623#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5624 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5625 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005626 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627
5628 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005629 vim_strncpy(buff,
5630 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005631 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005633 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 _("%d more files to edit. Quit anyway?"), n);
5635 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5636 return OK;
5637 return FAIL;
5638 }
5639#endif
5640 if (n == 1)
5641 EMSG(_("E173: 1 more file to edit"));
5642 else
5643 EMSGN(_("E173: %ld more files to edit"), n);
5644 quitmore = 2; /* next try to quit is allowed */
5645 }
5646 return FAIL;
5647 }
5648 return OK;
5649}
5650
5651#ifdef FEAT_CMDL_COMPL
5652/*
5653 * Function given to ExpandGeneric() to obtain the list of command names.
5654 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005656get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657{
5658 if (idx >= (int)CMD_SIZE)
5659# ifdef FEAT_USR_CMDS
5660 return get_user_command_name(idx);
5661# else
5662 return NULL;
5663# endif
5664 return cmdnames[idx].cmd_name;
5665}
5666#endif
5667
5668#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005669static int uc_add_command(char_u *name, size_t name_len, char_u *rep, long argt, long def, int flags, int compl, char_u *compl_arg, int addr_type, int force);
5670static void uc_list(char_u *name, size_t name_len);
5671static int uc_scan_attr(char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg, int* attr_type_arg);
5672static char_u *uc_split_args(char_u *arg, size_t *lenp);
5673static size_t uc_check_code(char_u *code, size_t len, char_u *buf, ucmd_T *cmd, exarg_T *eap, char_u **split_buf, size_t *split_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674
5675 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005676uc_add_command(
5677 char_u *name,
5678 size_t name_len,
5679 char_u *rep,
5680 long argt,
5681 long def,
5682 int flags,
5683 int compl,
5684 char_u *compl_arg,
5685 int addr_type,
5686 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687{
5688 ucmd_T *cmd = NULL;
5689 char_u *p;
5690 int i;
5691 int cmp = 1;
5692 char_u *rep_buf = NULL;
5693 garray_T *gap;
5694
Bram Moolenaar9c102382006-05-03 21:26:49 +00005695 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 if (rep_buf == NULL)
5697 {
5698 /* Can't replace termcodes - try using the string as is */
5699 rep_buf = vim_strsave(rep);
5700
5701 /* Give up if out of memory */
5702 if (rep_buf == NULL)
5703 return FAIL;
5704 }
5705
5706 /* get address of growarray: global or in curbuf */
5707 if (flags & UC_BUFFER)
5708 {
5709 gap = &curbuf->b_ucmds;
5710 if (gap->ga_itemsize == 0)
5711 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5712 }
5713 else
5714 gap = &ucmds;
5715
5716 /* Search for the command in the already defined commands. */
5717 for (i = 0; i < gap->ga_len; ++i)
5718 {
5719 size_t len;
5720
5721 cmd = USER_CMD_GA(gap, i);
5722 len = STRLEN(cmd->uc_name);
5723 cmp = STRNCMP(name, cmd->uc_name, name_len);
5724 if (cmp == 0)
5725 {
5726 if (name_len < len)
5727 cmp = -1;
5728 else if (name_len > len)
5729 cmp = 1;
5730 }
5731
5732 if (cmp == 0)
5733 {
5734 if (!force)
5735 {
5736 EMSG(_("E174: Command already exists: add ! to replace it"));
5737 goto fail;
5738 }
5739
5740 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005741 cmd->uc_rep = NULL;
5742#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5743 vim_free(cmd->uc_compl_arg);
5744 cmd->uc_compl_arg = NULL;
5745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 break;
5747 }
5748
5749 /* Stop as soon as we pass the name to add */
5750 if (cmp < 0)
5751 break;
5752 }
5753
5754 /* Extend the array unless we're replacing an existing command */
5755 if (cmp != 0)
5756 {
5757 if (ga_grow(gap, 1) != OK)
5758 goto fail;
5759 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5760 goto fail;
5761
5762 cmd = USER_CMD_GA(gap, i);
5763 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5764
5765 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766
5767 cmd->uc_name = p;
5768 }
5769
5770 cmd->uc_rep = rep_buf;
5771 cmd->uc_argt = argt;
5772 cmd->uc_def = def;
5773 cmd->uc_compl = compl;
5774#ifdef FEAT_EVAL
5775 cmd->uc_scriptID = current_SID;
5776# ifdef FEAT_CMDL_COMPL
5777 cmd->uc_compl_arg = compl_arg;
5778# endif
5779#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005780 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781
5782 return OK;
5783
5784fail:
5785 vim_free(rep_buf);
5786#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5787 vim_free(compl_arg);
5788#endif
5789 return FAIL;
5790}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005793#if defined(FEAT_USR_CMDS)
5794static struct
5795{
5796 int expand;
5797 char *name;
5798} addr_type_complete[] =
5799{
5800 {ADDR_ARGUMENTS, "arguments"},
5801 {ADDR_LINES, "lines"},
5802 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5803 {ADDR_TABS, "tabs"},
5804 {ADDR_BUFFERS, "buffers"},
5805 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005806 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005807 {-1, NULL}
5808};
5809#endif
5810
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005811#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812/*
5813 * List of names for completion for ":command" with the EXPAND_ flag.
5814 * Must be alphabetical for completion.
5815 */
5816static struct
5817{
5818 int expand;
5819 char *name;
5820} command_complete[] =
5821{
5822 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005823 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005825 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005827 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005828#if defined(FEAT_CSCOPE)
5829 {EXPAND_CSCOPE, "cscope"},
5830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5832 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005833 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834#endif
5835 {EXPAND_DIRECTORIES, "dir"},
5836 {EXPAND_ENV_VARS, "environment"},
5837 {EXPAND_EVENTS, "event"},
5838 {EXPAND_EXPRESSION, "expression"},
5839 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005840 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005841 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 {EXPAND_FUNCTIONS, "function"},
5843 {EXPAND_HELP, "help"},
5844 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005845#if defined(FEAT_CMDHIST)
5846 {EXPAND_HISTORY, "history"},
5847#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005848#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005849 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005850 {EXPAND_LOCALES, "locale"},
5851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 {EXPAND_MAPPINGS, "mapping"},
5853 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005854 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005855#if defined(FEAT_PROFILE)
5856 {EXPAND_SYNTIME, "syntime"},
5857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005859 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005860#if defined(FEAT_SIGNS)
5861 {EXPAND_SIGN, "sign"},
5862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 {EXPAND_TAGS, "tag"},
5864 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005865 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 {EXPAND_USER_VARS, "var"},
5867 {0, NULL}
5868};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005871#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005873uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874{
5875 int i, j;
5876 int found = FALSE;
5877 ucmd_T *cmd;
5878 int len;
5879 long a;
5880 garray_T *gap;
5881
5882 gap = &curbuf->b_ucmds;
5883 for (;;)
5884 {
5885 for (i = 0; i < gap->ga_len; ++i)
5886 {
5887 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005888 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889
5890 /* Skip commands which don't match the requested prefix */
5891 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5892 continue;
5893
5894 /* Put out the title first time */
5895 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005896 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 found = TRUE;
5898 msg_putchar('\n');
5899 if (got_int)
5900 break;
5901
5902 /* Special cases */
5903 msg_putchar(a & BANG ? '!' : ' ');
5904 msg_putchar(a & REGSTR ? '"' : ' ');
5905 msg_putchar(gap != &ucmds ? 'b' : ' ');
5906 msg_putchar(' ');
5907
5908 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5909 len = (int)STRLEN(cmd->uc_name) + 4;
5910
5911 do {
5912 msg_putchar(' ');
5913 ++len;
5914 } while (len < 16);
5915
5916 len = 0;
5917
5918 /* Arguments */
5919 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5920 {
5921 case 0: IObuff[len++] = '0'; break;
5922 case (EXTRA): IObuff[len++] = '*'; break;
5923 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5924 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5925 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5926 }
5927
5928 do {
5929 IObuff[len++] = ' ';
5930 } while (len < 5);
5931
5932 /* Range */
5933 if (a & (RANGE|COUNT))
5934 {
5935 if (a & COUNT)
5936 {
5937 /* -count=N */
5938 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5939 len += (int)STRLEN(IObuff + len);
5940 }
5941 else if (a & DFLALL)
5942 IObuff[len++] = '%';
5943 else if (cmd->uc_def >= 0)
5944 {
5945 /* -range=N */
5946 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5947 len += (int)STRLEN(IObuff + len);
5948 }
5949 else
5950 IObuff[len++] = '.';
5951 }
5952
5953 do {
5954 IObuff[len++] = ' ';
5955 } while (len < 11);
5956
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005957 /* Address Type */
5958 for (j = 0; addr_type_complete[j].expand != -1; ++j)
5959 if (addr_type_complete[j].expand != ADDR_LINES
5960 && addr_type_complete[j].expand == cmd->uc_addr_type)
5961 {
5962 STRCPY(IObuff + len, addr_type_complete[j].name);
5963 len += (int)STRLEN(IObuff + len);
5964 break;
5965 }
5966
5967 do {
5968 IObuff[len++] = ' ';
5969 } while (len < 21);
5970
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 /* Completion */
5972 for (j = 0; command_complete[j].expand != 0; ++j)
5973 if (command_complete[j].expand == cmd->uc_compl)
5974 {
5975 STRCPY(IObuff + len, command_complete[j].name);
5976 len += (int)STRLEN(IObuff + len);
5977 break;
5978 }
5979
5980 do {
5981 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005982 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983
5984 IObuff[len] = '\0';
5985 msg_outtrans(IObuff);
5986
5987 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005988#ifdef FEAT_EVAL
5989 if (p_verbose > 0)
5990 last_set_msg(cmd->uc_scriptID);
5991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 out_flush();
5993 ui_breakcheck();
5994 if (got_int)
5995 break;
5996 }
5997 if (gap == &ucmds || i < gap->ga_len)
5998 break;
5999 gap = &ucmds;
6000 }
6001
6002 if (!found)
6003 MSG(_("No user-defined commands found"));
6004}
6005
6006 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006007uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008{
6009 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6010 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6011 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6012 0xb9, 0x7f, 0};
6013 int i;
6014
6015 for (i = 0; fcmd[i]; ++i)
6016 IObuff[i] = fcmd[i] - 0x40;
6017 IObuff[i] = 0;
6018 return IObuff;
6019}
6020
6021 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006022uc_scan_attr(
6023 char_u *attr,
6024 size_t len,
6025 long *argt,
6026 long *def,
6027 int *flags,
6028 int *compl,
6029 char_u **compl_arg,
6030 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031{
6032 char_u *p;
6033
6034 if (len == 0)
6035 {
6036 EMSG(_("E175: No attribute specified"));
6037 return FAIL;
6038 }
6039
6040 /* First, try the simple attributes (no arguments) */
6041 if (STRNICMP(attr, "bang", len) == 0)
6042 *argt |= BANG;
6043 else if (STRNICMP(attr, "buffer", len) == 0)
6044 *flags |= UC_BUFFER;
6045 else if (STRNICMP(attr, "register", len) == 0)
6046 *argt |= REGSTR;
6047 else if (STRNICMP(attr, "bar", len) == 0)
6048 *argt |= TRLBAR;
6049 else
6050 {
6051 int i;
6052 char_u *val = NULL;
6053 size_t vallen = 0;
6054 size_t attrlen = len;
6055
6056 /* Look for the attribute name - which is the part before any '=' */
6057 for (i = 0; i < (int)len; ++i)
6058 {
6059 if (attr[i] == '=')
6060 {
6061 val = &attr[i + 1];
6062 vallen = len - i - 1;
6063 attrlen = i;
6064 break;
6065 }
6066 }
6067
6068 if (STRNICMP(attr, "nargs", attrlen) == 0)
6069 {
6070 if (vallen == 1)
6071 {
6072 if (*val == '0')
6073 /* Do nothing - this is the default */;
6074 else if (*val == '1')
6075 *argt |= (EXTRA | NOSPC | NEEDARG);
6076 else if (*val == '*')
6077 *argt |= EXTRA;
6078 else if (*val == '?')
6079 *argt |= (EXTRA | NOSPC);
6080 else if (*val == '+')
6081 *argt |= (EXTRA | NEEDARG);
6082 else
6083 goto wrong_nargs;
6084 }
6085 else
6086 {
6087wrong_nargs:
6088 EMSG(_("E176: Invalid number of arguments"));
6089 return FAIL;
6090 }
6091 }
6092 else if (STRNICMP(attr, "range", attrlen) == 0)
6093 {
6094 *argt |= RANGE;
6095 if (vallen == 1 && *val == '%')
6096 *argt |= DFLALL;
6097 else if (val != NULL)
6098 {
6099 p = val;
6100 if (*def >= 0)
6101 {
6102two_count:
6103 EMSG(_("E177: Count cannot be specified twice"));
6104 return FAIL;
6105 }
6106
6107 *def = getdigits(&p);
6108 *argt |= (ZEROR | NOTADR);
6109
6110 if (p != val + vallen || vallen == 0)
6111 {
6112invalid_count:
6113 EMSG(_("E178: Invalid default value for count"));
6114 return FAIL;
6115 }
6116 }
6117 }
6118 else if (STRNICMP(attr, "count", attrlen) == 0)
6119 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006120 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121
6122 if (val != NULL)
6123 {
6124 p = val;
6125 if (*def >= 0)
6126 goto two_count;
6127
6128 *def = getdigits(&p);
6129
6130 if (p != val + vallen)
6131 goto invalid_count;
6132 }
6133
6134 if (*def < 0)
6135 *def = 0;
6136 }
6137 else if (STRNICMP(attr, "complete", attrlen) == 0)
6138 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 if (val == NULL)
6140 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006141 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 return FAIL;
6143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006145 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6146 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006149 else if (STRNICMP(attr, "addr", attrlen) == 0)
6150 {
6151 *argt |= RANGE;
6152 if (val == NULL)
6153 {
6154 EMSG(_("E179: argument required for -addr"));
6155 return FAIL;
6156 }
6157 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6158 == FAIL)
6159 return FAIL;
6160 if (addr_type_arg != ADDR_LINES)
6161 *argt |= (ZEROR | NOTADR) ;
6162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 else
6164 {
6165 char_u ch = attr[len];
6166 attr[len] = '\0';
6167 EMSG2(_("E181: Invalid attribute: %s"), attr);
6168 attr[len] = ch;
6169 return FAIL;
6170 }
6171 }
6172
6173 return OK;
6174}
6175
Bram Moolenaara850a712009-01-28 14:42:59 +00006176/*
6177 * ":command ..."
6178 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006180ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181{
6182 char_u *name;
6183 char_u *end;
6184 char_u *p;
6185 long argt = 0;
6186 long def = -1;
6187 int flags = 0;
6188 int compl = EXPAND_NOTHING;
6189 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006190 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006192 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193
6194 p = eap->arg;
6195
6196 /* Check for attributes */
6197 while (*p == '-')
6198 {
6199 ++p;
6200 end = skiptowhite(p);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006201 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 == FAIL)
6203 return;
6204 p = skipwhite(end);
6205 }
6206
6207 /* Get the name (if any) and skip to the following argument */
6208 name = p;
6209 if (ASCII_ISALPHA(*p))
6210 while (ASCII_ISALNUM(*p))
6211 ++p;
6212 if (!ends_excmd(*p) && !vim_iswhite(*p))
6213 {
6214 EMSG(_("E182: Invalid command name"));
6215 return;
6216 }
6217 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006218 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219
6220 /* If there is nothing after the name, and no attributes were specified,
6221 * we are listing commands
6222 */
6223 p = skipwhite(end);
6224 if (!has_attr && ends_excmd(*p))
6225 {
6226 uc_list(name, end - name);
6227 }
6228 else if (!ASCII_ISUPPER(*name))
6229 {
6230 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6231 return;
6232 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006233 else if ((name_len == 1 && *name == 'X')
6234 || (name_len <= 4
6235 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6236 {
6237 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6238 return;
6239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 else
6241 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006242 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243}
6244
6245/*
6246 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 * Clear all user commands, global and for current buffer.
6248 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006249 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006250ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251{
6252 uc_clear(&ucmds);
6253 uc_clear(&curbuf->b_ucmds);
6254}
6255
6256/*
6257 * Clear all user commands for "gap".
6258 */
6259 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006260uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261{
6262 int i;
6263 ucmd_T *cmd;
6264
6265 for (i = 0; i < gap->ga_len; ++i)
6266 {
6267 cmd = USER_CMD_GA(gap, i);
6268 vim_free(cmd->uc_name);
6269 vim_free(cmd->uc_rep);
6270# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6271 vim_free(cmd->uc_compl_arg);
6272# endif
6273 }
6274 ga_clear(gap);
6275}
6276
6277 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006278ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279{
6280 int i = 0;
6281 ucmd_T *cmd = NULL;
6282 int cmp = -1;
6283 garray_T *gap;
6284
6285 gap = &curbuf->b_ucmds;
6286 for (;;)
6287 {
6288 for (i = 0; i < gap->ga_len; ++i)
6289 {
6290 cmd = USER_CMD_GA(gap, i);
6291 cmp = STRCMP(eap->arg, cmd->uc_name);
6292 if (cmp <= 0)
6293 break;
6294 }
6295 if (gap == &ucmds || cmp == 0)
6296 break;
6297 gap = &ucmds;
6298 }
6299
6300 if (cmp != 0)
6301 {
6302 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6303 return;
6304 }
6305
6306 vim_free(cmd->uc_name);
6307 vim_free(cmd->uc_rep);
6308# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6309 vim_free(cmd->uc_compl_arg);
6310# endif
6311
6312 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313
6314 if (i < gap->ga_len)
6315 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6316}
6317
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006318/*
6319 * split and quote args for <f-args>
6320 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006322uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323{
6324 char_u *buf;
6325 char_u *p;
6326 char_u *q;
6327 int len;
6328
6329 /* Precalculate length */
6330 p = arg;
6331 len = 2; /* Initial and final quotes */
6332
6333 while (*p)
6334 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006335 if (p[0] == '\\' && p[1] == '\\')
6336 {
6337 len += 2;
6338 p += 2;
6339 }
6340 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 {
6342 len += 1;
6343 p += 2;
6344 }
6345 else if (*p == '\\' || *p == '"')
6346 {
6347 len += 2;
6348 p += 1;
6349 }
6350 else if (vim_iswhite(*p))
6351 {
6352 p = skipwhite(p);
6353 if (*p == NUL)
6354 break;
6355 len += 3; /* "," */
6356 }
6357 else
6358 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006359#ifdef FEAT_MBYTE
6360 int charlen = (*mb_ptr2len)(p);
6361 len += charlen;
6362 p += charlen;
6363#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 ++len;
6365 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 }
6368 }
6369
6370 buf = alloc(len + 1);
6371 if (buf == NULL)
6372 {
6373 *lenp = 0;
6374 return buf;
6375 }
6376
6377 p = arg;
6378 q = buf;
6379 *q++ = '"';
6380 while (*p)
6381 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006382 if (p[0] == '\\' && p[1] == '\\')
6383 {
6384 *q++ = '\\';
6385 *q++ = '\\';
6386 p += 2;
6387 }
6388 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 {
6390 *q++ = p[1];
6391 p += 2;
6392 }
6393 else if (*p == '\\' || *p == '"')
6394 {
6395 *q++ = '\\';
6396 *q++ = *p++;
6397 }
6398 else if (vim_iswhite(*p))
6399 {
6400 p = skipwhite(p);
6401 if (*p == NUL)
6402 break;
6403 *q++ = '"';
6404 *q++ = ',';
6405 *q++ = '"';
6406 }
6407 else
6408 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006409 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 }
6411 }
6412 *q++ = '"';
6413 *q = 0;
6414
6415 *lenp = len;
6416 return buf;
6417}
6418
6419/*
6420 * Check for a <> code in a user command.
6421 * "code" points to the '<'. "len" the length of the <> (inclusive).
6422 * "buf" is where the result is to be added.
6423 * "split_buf" points to a buffer used for splitting, caller should free it.
6424 * "split_len" is the length of what "split_buf" contains.
6425 * Returns the length of the replacement, which has been added to "buf".
6426 * Returns -1 if there was no match, and only the "<" has been copied.
6427 */
6428 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006429uc_check_code(
6430 char_u *code,
6431 size_t len,
6432 char_u *buf,
6433 ucmd_T *cmd, /* the user command we're expanding */
6434 exarg_T *eap, /* ex arguments */
6435 char_u **split_buf,
6436 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437{
6438 size_t result = 0;
6439 char_u *p = code + 1;
6440 size_t l = len - 2;
6441 int quote = 0;
6442 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
6443 ct_LT, ct_NONE } type = ct_NONE;
6444
6445 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6446 {
6447 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6448 p += 2;
6449 l -= 2;
6450 }
6451
Bram Moolenaar371d5402006-03-20 21:47:49 +00006452 ++l;
6453 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006455 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006457 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006459 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006461 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006463 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006465 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006467 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 type = ct_REGISTER;
6469
6470 switch (type)
6471 {
6472 case ct_ARGS:
6473 /* Simple case first */
6474 if (*eap->arg == NUL)
6475 {
6476 if (quote == 1)
6477 {
6478 result = 2;
6479 if (buf != NULL)
6480 STRCPY(buf, "''");
6481 }
6482 else
6483 result = 0;
6484 break;
6485 }
6486
6487 /* When specified there is a single argument don't split it.
6488 * Works for ":Cmd %" when % is "a b c". */
6489 if ((eap->argt & NOSPC) && quote == 2)
6490 quote = 1;
6491
6492 switch (quote)
6493 {
6494 case 0: /* No quoting, no splitting */
6495 result = STRLEN(eap->arg);
6496 if (buf != NULL)
6497 STRCPY(buf, eap->arg);
6498 break;
6499 case 1: /* Quote, but don't split */
6500 result = STRLEN(eap->arg) + 2;
6501 for (p = eap->arg; *p; ++p)
6502 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006503#ifdef FEAT_MBYTE
6504 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6505 /* DBCS can contain \ in a trail byte, skip the
6506 * double-byte character. */
6507 ++p;
6508 else
6509#endif
6510 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 ++result;
6512 }
6513
6514 if (buf != NULL)
6515 {
6516 *buf++ = '"';
6517 for (p = eap->arg; *p; ++p)
6518 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006519#ifdef FEAT_MBYTE
6520 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6521 /* DBCS can contain \ in a trail byte, copy the
6522 * double-byte character to avoid escaping. */
6523 *buf++ = *p++;
6524 else
6525#endif
6526 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 *buf++ = '\\';
6528 *buf++ = *p;
6529 }
6530 *buf = '"';
6531 }
6532
6533 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006534 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 /* This is hard, so only do it once, and cache the result */
6536 if (*split_buf == NULL)
6537 *split_buf = uc_split_args(eap->arg, split_len);
6538
6539 result = *split_len;
6540 if (buf != NULL && result != 0)
6541 STRCPY(buf, *split_buf);
6542
6543 break;
6544 }
6545 break;
6546
6547 case ct_BANG:
6548 result = eap->forceit ? 1 : 0;
6549 if (quote)
6550 result += 2;
6551 if (buf != NULL)
6552 {
6553 if (quote)
6554 *buf++ = '"';
6555 if (eap->forceit)
6556 *buf++ = '!';
6557 if (quote)
6558 *buf = '"';
6559 }
6560 break;
6561
6562 case ct_LINE1:
6563 case ct_LINE2:
6564 case ct_COUNT:
6565 {
6566 char num_buf[20];
6567 long num = (type == ct_LINE1) ? eap->line1 :
6568 (type == ct_LINE2) ? eap->line2 :
6569 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6570 size_t num_len;
6571
6572 sprintf(num_buf, "%ld", num);
6573 num_len = STRLEN(num_buf);
6574 result = num_len;
6575
6576 if (quote)
6577 result += 2;
6578
6579 if (buf != NULL)
6580 {
6581 if (quote)
6582 *buf++ = '"';
6583 STRCPY(buf, num_buf);
6584 buf += num_len;
6585 if (quote)
6586 *buf = '"';
6587 }
6588
6589 break;
6590 }
6591
6592 case ct_REGISTER:
6593 result = eap->regname ? 1 : 0;
6594 if (quote)
6595 result += 2;
6596 if (buf != NULL)
6597 {
6598 if (quote)
6599 *buf++ = '\'';
6600 if (eap->regname)
6601 *buf++ = eap->regname;
6602 if (quote)
6603 *buf = '\'';
6604 }
6605 break;
6606
6607 case ct_LT:
6608 result = 1;
6609 if (buf != NULL)
6610 *buf = '<';
6611 break;
6612
6613 default:
6614 /* Not recognized: just copy the '<' and return -1. */
6615 result = (size_t)-1;
6616 if (buf != NULL)
6617 *buf = '<';
6618 break;
6619 }
6620
6621 return result;
6622}
6623
6624 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006625do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626{
6627 char_u *buf;
6628 char_u *p;
6629 char_u *q;
6630
6631 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006632 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006633 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 size_t len, totlen;
6635
6636 size_t split_len = 0;
6637 char_u *split_buf = NULL;
6638 ucmd_T *cmd;
6639#ifdef FEAT_EVAL
6640 scid_T save_current_SID = current_SID;
6641#endif
6642
6643 if (eap->cmdidx == CMD_USER)
6644 cmd = USER_CMD(eap->useridx);
6645 else
6646 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6647
6648 /*
6649 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006650 * First round: "buf" is NULL, compute length, allocate "buf".
6651 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 */
6653 buf = NULL;
6654 for (;;)
6655 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006656 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006657 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006659
6660 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006662 start = vim_strchr(p, '<');
6663 if (start != NULL)
6664 end = vim_strchr(start + 1, '>');
6665 if (buf != NULL)
6666 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006667 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6668 ;
6669 if (*ksp == K_SPECIAL
6670 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006671 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6672# ifdef FEAT_GUI
6673 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6674# endif
6675 ))
6676 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006677 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006678 * KS_SPECIAL KE_FILLER, like for mappings, but
6679 * do_cmdline() doesn't handle that, so convert it back.
6680 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6681 len = ksp - p;
6682 if (len > 0)
6683 {
6684 mch_memmove(q, p, len);
6685 q += len;
6686 }
6687 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6688 p = ksp + 3;
6689 continue;
6690 }
6691 }
6692
6693 /* break if there no <item> is found */
6694 if (start == NULL || end == NULL)
6695 break;
6696
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 /* Include the '>' */
6698 ++end;
6699
6700 /* Take everything up to the '<' */
6701 len = start - p;
6702 if (buf == NULL)
6703 totlen += len;
6704 else
6705 {
6706 mch_memmove(q, p, len);
6707 q += len;
6708 }
6709
6710 len = uc_check_code(start, end - start, q, cmd, eap,
6711 &split_buf, &split_len);
6712 if (len == (size_t)-1)
6713 {
6714 /* no match, continue after '<' */
6715 p = start + 1;
6716 len = 1;
6717 }
6718 else
6719 p = end;
6720 if (buf == NULL)
6721 totlen += len;
6722 else
6723 q += len;
6724 }
6725 if (buf != NULL) /* second time here, finished */
6726 {
6727 STRCPY(q, p);
6728 break;
6729 }
6730
6731 totlen += STRLEN(p); /* Add on the trailing characters */
6732 buf = alloc((unsigned)(totlen + 1));
6733 if (buf == NULL)
6734 {
6735 vim_free(split_buf);
6736 return;
6737 }
6738 }
6739
6740#ifdef FEAT_EVAL
6741 current_SID = cmd->uc_scriptID;
6742#endif
6743 (void)do_cmdline(buf, eap->getline, eap->cookie,
6744 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6745#ifdef FEAT_EVAL
6746 current_SID = save_current_SID;
6747#endif
6748 vim_free(buf);
6749 vim_free(split_buf);
6750}
6751
6752# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6753 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006754get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755{
6756 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6757}
6758
6759/*
6760 * Function given to ExpandGeneric() to obtain the list of user command names.
6761 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006763get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764{
6765 if (idx < curbuf->b_ucmds.ga_len)
6766 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6767 idx -= curbuf->b_ucmds.ga_len;
6768 if (idx < ucmds.ga_len)
6769 return USER_CMD(idx)->uc_name;
6770 return NULL;
6771}
6772
6773/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006774 * Function given to ExpandGeneric() to obtain the list of user address type names.
6775 */
6776 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006777get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006778{
6779 return (char_u *)addr_type_complete[idx].name;
6780}
6781
6782/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 * Function given to ExpandGeneric() to obtain the list of user command
6784 * attributes.
6785 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006787get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788{
6789 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006790 {"addr", "bang", "bar", "buffer", "complete",
6791 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006793 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 return NULL;
6795 return (char_u *)user_cmd_flags[idx];
6796}
6797
6798/*
6799 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006802get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803{
6804 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6805
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006806 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 return NULL;
6808 return (char_u *)user_cmd_nargs[idx];
6809}
6810
6811/*
6812 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006815get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816{
6817 return (char_u *)command_complete[idx].name;
6818}
6819# endif /* FEAT_CMDL_COMPL */
6820
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006821/*
6822 * Parse address type argument
6823 */
6824 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006825parse_addr_type_arg(
6826 char_u *value,
6827 int vallen,
6828 long *argt,
6829 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006830{
6831 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01006832
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006833 for (i = 0; addr_type_complete[i].expand != -1; ++i)
6834 {
6835 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
6836 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
6837 if (a && b)
6838 {
6839 *addr_type_arg = addr_type_complete[i].expand;
6840 break;
6841 }
6842 }
6843
6844 if (addr_type_complete[i].expand == -1)
6845 {
6846 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01006847
6848 for (i = 0; err[i] != NUL && !vim_iswhite(err[i]); i++)
6849 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006850 err[i] = NUL;
6851 EMSG2(_("E180: Invalid address type value: %s"), err);
6852 return FAIL;
6853 }
6854
6855 if (*addr_type_arg != ADDR_LINES)
6856 *argt |= NOTADR;
6857
6858 return OK;
6859}
6860
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861#endif /* FEAT_USR_CMDS */
6862
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006863#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6864/*
6865 * Parse a completion argument "value[vallen]".
6866 * The detected completion goes in "*complp", argument type in "*argt".
6867 * When there is an argument, for function and user defined completion, it's
6868 * copied to allocated memory and stored in "*compl_arg".
6869 * Returns FAIL if something is wrong.
6870 */
6871 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006872parse_compl_arg(
6873 char_u *value,
6874 int vallen,
6875 int *complp,
6876 long *argt,
6877 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006878{
6879 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006880# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006881 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006882# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006883 int i;
6884 int valend = vallen;
6885
6886 /* Look for any argument part - which is the part after any ',' */
6887 for (i = 0; i < vallen; ++i)
6888 {
6889 if (value[i] == ',')
6890 {
6891 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006892# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006893 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006894# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006895 valend = i;
6896 break;
6897 }
6898 }
6899
6900 for (i = 0; command_complete[i].expand != 0; ++i)
6901 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006902 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006903 && STRNCMP(value, command_complete[i].name, valend) == 0)
6904 {
6905 *complp = command_complete[i].expand;
6906 if (command_complete[i].expand == EXPAND_BUFFERS)
6907 *argt |= BUFNAME;
6908 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6909 || command_complete[i].expand == EXPAND_FILES)
6910 *argt |= XFILE;
6911 break;
6912 }
6913 }
6914
6915 if (command_complete[i].expand == 0)
6916 {
6917 EMSG2(_("E180: Invalid complete value: %s"), value);
6918 return FAIL;
6919 }
6920
6921# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6922 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6923 && arg != NULL)
6924# else
6925 if (arg != NULL)
6926# endif
6927 {
6928 EMSG(_("E468: Completion argument only allowed for custom completion"));
6929 return FAIL;
6930 }
6931
6932# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6933 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6934 && arg == NULL)
6935 {
6936 EMSG(_("E467: Custom completion requires a function argument"));
6937 return FAIL;
6938 }
6939
6940 if (arg != NULL)
6941 *compl_arg = vim_strnsave(arg, (int)arglen);
6942# endif
6943 return OK;
6944}
6945#endif
6946
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006948ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949{
Bram Moolenaare6850792010-05-14 15:28:44 +02006950 if (*eap->arg == NUL)
6951 {
6952#ifdef FEAT_EVAL
6953 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6954 char_u *p = NULL;
6955
6956 if (expr != NULL)
6957 {
6958 ++emsg_off;
6959 p = eval_to_string(expr, NULL, FALSE);
6960 --emsg_off;
6961 vim_free(expr);
6962 }
6963 if (p != NULL)
6964 {
6965 MSG(p);
6966 vim_free(p);
6967 }
6968 else
6969 MSG("default");
6970#else
6971 MSG(_("unknown"));
6972#endif
6973 }
6974 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006975 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976}
6977
6978 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006979ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980{
6981 if (*eap->arg == NUL && eap->cmd[2] == '!')
6982 MSG(_("Greetings, Vim user!"));
6983 do_highlight(eap->arg, eap->forceit, FALSE);
6984}
6985
6986
6987/*
6988 * Call this function if we thought we were going to exit, but we won't
6989 * (because of an error). May need to restore the terminal mode.
6990 */
6991 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006992not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993{
6994 exiting = FALSE;
6995 settmode(TMODE_RAW);
6996}
6997
6998/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01006999 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 */
7001 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007002ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003{
Bram Moolenaarf240e182014-11-27 18:33:02 +01007004#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007005 win_T *wp;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007006#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007007
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008#ifdef FEAT_CMDWIN
7009 if (cmdwin_type != 0)
7010 {
7011 cmdwin_result = Ctrl_C;
7012 return;
7013 }
7014#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007015 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007016 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007017 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007018 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007019 return;
7020 }
Bram Moolenaarf240e182014-11-27 18:33:02 +01007021#ifdef FEAT_WINDOWS
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007022 if (eap->addr_count > 0)
7023 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007024 int wnr = eap->line2;
7025
7026 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7027 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007028 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007029 }
7030 else
Bram Moolenaarf240e182014-11-27 18:33:02 +01007031#endif
7032#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007033 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007034#endif
7035
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007036#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02007037 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007038 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02007039 * being closed (can only happen in autocommands). */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007040 if (curbuf_locked() || (wp->w_buffer->b_nwindows == 1
7041 && wp->w_buffer->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007042 return;
7043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044
7045#ifdef FEAT_NETBEANS_INTG
7046 netbeansForcedQuit = eap->forceit;
7047#endif
7048
7049 /*
7050 * If there are more files or windows we won't exit.
7051 */
7052 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7053 exiting = TRUE;
7054 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007055 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7056 | (eap->forceit ? CCGD_FORCEIT : 0)
7057 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007059 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 {
7061 not_exiting();
7062 }
7063 else
7064 {
7065#ifdef FEAT_WINDOWS
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007066 /* quit last window
7067 * Note: only_one_window() returns true, even so a help window is
7068 * still open. In that case only quit, if no address has been
7069 * specified. Example:
7070 * :h|wincmd w|1q - don't quit
7071 * :h|wincmd w|q - quit
7072 */
7073 if (only_one_window() && (firstwin == lastwin || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074#endif
7075 getout(0);
7076#ifdef FEAT_WINDOWS
7077# ifdef FEAT_GUI
7078 need_mouse_correct = TRUE;
7079# endif
7080 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007081 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082#endif
7083 }
7084}
7085
7086/*
7087 * ":cquit".
7088 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007090ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091{
7092 getout(1); /* this does not always pass on the exit code to the Manx
7093 compiler. why? */
7094}
7095
7096/*
7097 * ":qall": try to quit all windows
7098 */
7099 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007100ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101{
7102# ifdef FEAT_CMDWIN
7103 if (cmdwin_type != 0)
7104 {
7105 if (eap->forceit)
7106 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7107 else
7108 cmdwin_result = K_XF2;
7109 return;
7110 }
7111# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007112
7113 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007114 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007115 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007116 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007117 return;
7118 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007119#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007120 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7121 /* Refuse to quit when locked or when the buffer in the last window is
7122 * being closed (can only happen in autocommands). */
7123 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007124 return;
7125#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007126
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007128 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 getout(0);
7130 not_exiting();
7131}
7132
Bram Moolenaard9967712006-03-11 21:18:15 +00007133#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134/*
7135 * ":close": close current window, unless it is the last one
7136 */
7137 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007138ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007140 win_T *win;
7141 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142# ifdef FEAT_CMDWIN
7143 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007144 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 else
7146# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007147 if (!text_locked()
7148#ifdef FEAT_AUTOCMD
7149 && !curbuf_locked()
7150#endif
7151 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007152 {
7153 if (eap->addr_count == 0)
7154 ex_win_close(eap->forceit, curwin, NULL);
7155 else {
7156 for (win = firstwin; win != NULL; win = win->w_next)
7157 {
7158 winnr++;
7159 if (winnr == eap->line2)
7160 break;
7161 }
7162 if (win == NULL)
7163 win = lastwin;
7164 ex_win_close(eap->forceit, win, NULL);
7165 }
7166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167}
7168
Bram Moolenaard9967712006-03-11 21:18:15 +00007169# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007170/*
7171 * ":pclose": Close any preview window.
7172 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007174ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007175{
7176 win_T *win;
7177
7178 for (win = firstwin; win != NULL; win = win->w_next)
7179 if (win->w_p_pvw)
7180 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007181 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007182 break;
7183 }
7184}
Bram Moolenaard9967712006-03-11 21:18:15 +00007185# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007186
Bram Moolenaarf740b292006-02-16 22:11:02 +00007187/*
7188 * Close window "win" and take care of handling closing the last window for a
7189 * modified buffer.
7190 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007191 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007192ex_win_close(
7193 int forceit,
7194 win_T *win,
7195 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196{
7197 int need_hide;
7198 buf_T *buf = win->w_buffer;
7199
7200 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007201 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007203# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 if ((p_confirm || cmdmod.confirm) && p_write)
7205 {
7206 dialog_changed(buf, FALSE);
7207 if (buf_valid(buf) && bufIsChanged(buf))
7208 return;
7209 need_hide = FALSE;
7210 }
7211 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007212# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 {
7214 EMSG(_(e_nowrtmsg));
7215 return;
7216 }
7217 }
7218
Bram Moolenaard9967712006-03-11 21:18:15 +00007219# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007221# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007222
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007224 if (tp == NULL)
7225 win_close(win, !need_hide && !P_HID(buf));
7226 else
7227 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228}
7229
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007231 * ":tabclose": close current tab page, unless it is the last one.
7232 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 */
7234 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007235ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007237 tabpage_T *tp;
7238
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007239# ifdef FEAT_CMDWIN
7240 if (cmdwin_type != 0)
7241 cmdwin_result = K_IGNORE;
7242 else
7243# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007244 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007245 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007246 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007248 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007249 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007250 tp = find_tabpage((int)eap->line2);
7251 if (tp == NULL)
7252 {
7253 beep_flush();
7254 return;
7255 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007256 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007257 {
7258 tabpage_close_other(tp, eap->forceit);
7259 return;
7260 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007261 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007262 if (!text_locked()
7263#ifdef FEAT_AUTOCMD
7264 && !curbuf_locked()
7265#endif
7266 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00007267 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007269}
7270
7271/*
7272 * ":tabonly": close all tab pages except the current one
7273 */
7274 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007275ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007276{
7277 tabpage_T *tp;
7278 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007279
7280# ifdef FEAT_CMDWIN
7281 if (cmdwin_type != 0)
7282 cmdwin_result = K_IGNORE;
7283 else
7284# endif
7285 if (first_tabpage->tp_next == NULL)
7286 MSG(_("Already only one tab page"));
7287 else
7288 {
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007289 if (eap->addr_count > 0)
7290 goto_tabpage(eap->line2);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007291 /* Repeat this up to a 1000 times, because autocommands may mess
7292 * up the lists. */
7293 for (done = 0; done < 1000; ++done)
7294 {
7295 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7296 if (tp->tp_topframe != topframe)
7297 {
7298 tabpage_close_other(tp, eap->forceit);
7299 /* if we failed to close it quit */
7300 if (valid_tabpage(tp))
7301 done = 1000;
7302 /* start over, "tp" is now invalid */
7303 break;
7304 }
7305 if (first_tabpage->tp_next == NULL)
7306 break;
7307 }
7308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310
7311/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007312 * Close the current tab page.
7313 */
7314 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007315tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007316{
7317 /* First close all the windows but the current one. If that worked then
7318 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007319 if (lastwin != firstwin)
7320 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007321 if (lastwin == firstwin)
7322 ex_win_close(forceit, curwin, NULL);
7323# ifdef FEAT_GUI
7324 need_mouse_correct = TRUE;
7325# endif
7326}
7327
7328/*
7329 * Close tab page "tp", which is not the current tab page.
7330 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007331 * Also takes care of the tab pages line disappearing when closing the
7332 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007333 */
7334 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007335tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007336{
7337 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007338 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007339 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007340
7341 /* Limit to 1000 windows, autocommands may add a window while we close
7342 * one. OK, so I'm paranoid... */
7343 while (++done < 1000)
7344 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007345 wp = tp->tp_firstwin;
7346 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007347
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007348 /* Autocommands may delete the tab page under our fingers and we may
7349 * fail to close a window with a modified buffer. */
7350 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007351 break;
7352 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007353
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007354 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007355 if (h != tabline_height())
7356 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007357}
7358
7359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 * ":only".
7361 */
7362 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007363ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007365 win_T *wp;
7366 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367# ifdef FEAT_GUI
7368 need_mouse_correct = TRUE;
7369# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007370 if (eap->addr_count > 0)
7371 {
7372 wnr = eap->line2;
7373 for (wp = firstwin; --wnr > 0; )
7374 {
7375 if (wp->w_next == NULL)
7376 break;
7377 else
7378 wp = wp->w_next;
7379 }
7380 win_goto(wp);
7381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 close_others(TRUE, eap->forceit);
7383}
7384
7385/*
7386 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007387 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007389 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007390ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391{
7392 if (eap->addr_count == 0)
7393 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007394 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395}
7396#endif /* FEAT_WINDOWS */
7397
7398 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007399ex_hide(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400{
7401 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
7402 eap->errmsg = e_invarg;
7403 else
7404 {
7405 /* ":hide" or ":hide | cmd": hide current window */
7406 eap->nextcmd = check_nextcmd(eap->arg);
7407#ifdef FEAT_WINDOWS
7408 if (!eap->skip)
7409 {
7410# ifdef FEAT_GUI
7411 need_mouse_correct = TRUE;
7412# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007413 if (eap->addr_count == 0)
7414 win_close(curwin, FALSE); /* don't free buffer */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007415 else
7416 {
7417 int winnr = 0;
7418 win_T *win;
7419
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007420 for (win = firstwin; win != NULL; win = win->w_next)
7421 {
7422 winnr++;
7423 if (winnr == eap->line2)
7424 break;
7425 }
7426 if (win == NULL)
7427 win = lastwin;
7428 win_close(win, FALSE);
7429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 }
7431#endif
7432 }
7433}
7434
7435/*
7436 * ":stop" and ":suspend": Suspend Vim.
7437 */
7438 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007439ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440{
7441 /*
7442 * Disallow suspending for "rvim".
7443 */
7444 if (!check_restricted()
7445#ifdef WIN3264
7446 /*
7447 * Check if external commands are allowed now.
7448 */
7449 && can_end_termcap_mode(TRUE)
7450#endif
7451 )
7452 {
7453 if (!eap->forceit)
7454 autowrite_all();
7455 windgoto((int)Rows - 1, 0);
7456 out_char('\n');
7457 out_flush();
7458 stoptermcap();
7459 out_flush(); /* needed for SUN to restore xterm buffer */
7460#ifdef FEAT_TITLE
7461 mch_restore_title(3); /* restore window titles */
7462#endif
7463 ui_suspend(); /* call machine specific function */
7464#ifdef FEAT_TITLE
7465 maketitle();
7466 resettitle(); /* force updating the title */
7467#endif
7468 starttermcap();
7469 scroll_start(); /* scroll screen before redrawing */
7470 redraw_later_clear();
7471 shell_resized(); /* may have resized window */
7472 }
7473}
7474
7475/*
7476 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7477 */
7478 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007479ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480{
7481#ifdef FEAT_CMDWIN
7482 if (cmdwin_type != 0)
7483 {
7484 cmdwin_result = Ctrl_C;
7485 return;
7486 }
7487#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007488 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007489 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007490 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007491 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007492 return;
7493 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007494#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007495 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7496 /* Refuse to quit when locked or when the buffer in the last window is
7497 * being closed (can only happen in autocommands). */
7498 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007499 return;
7500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501
7502 /*
7503 * if more files or windows we won't exit
7504 */
7505 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7506 exiting = TRUE;
7507 if ( ((eap->cmdidx == CMD_wq
7508 || curbufIsChanged())
7509 && do_write(eap) == FAIL)
7510 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007511 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 {
7513 not_exiting();
7514 }
7515 else
7516 {
7517#ifdef FEAT_WINDOWS
7518 if (only_one_window()) /* quit last window, exit Vim */
7519#endif
7520 getout(0);
7521#ifdef FEAT_WINDOWS
7522# ifdef FEAT_GUI
7523 need_mouse_correct = TRUE;
7524# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007525 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 win_close(curwin, !P_HID(curwin->w_buffer));
7527#endif
7528 }
7529}
7530
7531/*
7532 * ":print", ":list", ":number".
7533 */
7534 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007535ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007537 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7538 EMSG(_(e_emptybuf));
7539 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007541 for ( ;!got_int; ui_breakcheck())
7542 {
7543 print_line(eap->line1,
7544 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7545 || (eap->flags & EXFLAG_NR)),
7546 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7547 if (++eap->line1 > eap->line2)
7548 break;
7549 out_flush(); /* show one line at a time */
7550 }
7551 setpcmark();
7552 /* put cursor at last line */
7553 curwin->w_cursor.lnum = eap->line2;
7554 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 }
7556
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558}
7559
7560#ifdef FEAT_BYTEOFF
7561 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007562ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563{
7564 goto_byte(eap->line2);
7565}
7566#endif
7567
7568/*
7569 * ":shell".
7570 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007572ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573{
7574 do_shell(NULL, 0);
7575}
7576
7577#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7578 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007579 || defined(FEAT_GUI_MSWIN) \
7580 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 || defined(PROTO)
7582
7583/*
7584 * Handle a file drop. The code is here because a drop is *nearly* like an
7585 * :args command, but not quite (we have a list of exact filenames, so we
7586 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7587 * code is very similar to :args and hence needs access to a lot of the static
7588 * functions in this file.
7589 *
7590 * The list should be allocated using alloc(), as should each item in the
7591 * list. This function takes over responsibility for freeing the list.
7592 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007593 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7595 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7596 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7597 * file functionality is (currently) not in EMX this is not presently a
7598 * problem.
7599 */
7600 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007601handle_drop(
7602 int filec, /* the number of files dropped */
7603 char_u **filev, /* the list of files dropped */
7604 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605{
7606 exarg_T ea;
7607 int save_msg_scroll = msg_scroll;
7608
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007609 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007610 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007612#ifdef FEAT_AUTOCMD
7613 if (curbuf_locked())
7614 return;
7615#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007616 /* When the screen is being updated we should not change buffers and
7617 * windows structures, it may cause freed memory to be used. */
7618 if (updating_screen)
7619 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620
7621 /* Check whether the current buffer is changed. If so, we will need
7622 * to split the current window or data could be lost.
7623 * We don't need to check if the 'hidden' option is set, as in this
7624 * case the buffer won't be lost.
7625 */
7626 if (!P_HID(curbuf) && !split)
7627 {
7628 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007629 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 --emsg_off;
7631 }
7632 if (split)
7633 {
7634# ifdef FEAT_WINDOWS
7635 if (win_split(0, 0) == FAIL)
7636 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007637 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638
7639 /* When splitting the window, create a new alist. Otherwise the
7640 * existing one is overwritten. */
7641 alist_unlink(curwin->w_alist);
7642 alist_new();
7643# else
7644 return; /* can't split, always fail */
7645# endif
7646 }
7647
7648 /*
7649 * Set up the new argument list.
7650 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007651 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652
7653 /*
7654 * Move to the first file.
7655 */
7656 /* Fake up a minimal "next" command for do_argfile() */
7657 vim_memset(&ea, 0, sizeof(ea));
7658 ea.cmd = (char_u *)"next";
7659 do_argfile(&ea, 0);
7660
7661 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7662 * mode that is not needed here. */
7663 need_start_insertmode = FALSE;
7664
7665 /* Restore msg_scroll, otherwise a following command may cause scrolling
7666 * unexpectedly. The screen will be redrawn by the caller, thus
7667 * msg_scroll being set by displaying a message is irrelevant. */
7668 msg_scroll = save_msg_scroll;
7669}
7670#endif
7671
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672/*
7673 * Clear an argument list: free all file names and reset it to zero entries.
7674 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007675 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007676alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677{
7678 while (--al->al_ga.ga_len >= 0)
7679 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7680 ga_clear(&al->al_ga);
7681}
7682
7683/*
7684 * Init an argument list.
7685 */
7686 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007687alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688{
7689 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7690}
7691
7692#if defined(FEAT_WINDOWS) || defined(PROTO)
7693
7694/*
7695 * Remove a reference from an argument list.
7696 * Ignored when the argument list is the global one.
7697 * If the argument list is no longer used by any window, free it.
7698 */
7699 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007700alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701{
7702 if (al != &global_alist && --al->al_refcount <= 0)
7703 {
7704 alist_clear(al);
7705 vim_free(al);
7706 }
7707}
7708
7709# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7710/*
7711 * Create a new argument list and use it for the current window.
7712 */
7713 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007714alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715{
7716 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7717 if (curwin->w_alist == NULL)
7718 {
7719 curwin->w_alist = &global_alist;
7720 ++global_alist.al_refcount;
7721 }
7722 else
7723 {
7724 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007725 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 alist_init(curwin->w_alist);
7727 }
7728}
7729# endif
7730#endif
7731
Bram Moolenaar53076832015-12-31 19:53:21 +01007732#if (!defined(UNIX) && !defined(__EMX__)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733/*
7734 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007735 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7736 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 */
7738 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007739alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740{
7741 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007742 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 char_u **new_arg_files;
7744 int new_arg_file_count;
7745 char_u *save_p_su = p_su;
7746 int i;
7747
7748 /* Don't use 'suffixes' here. This should work like the shell did the
7749 * expansion. Also, the vimrc file isn't read yet, thus the user
7750 * can't set the options. */
7751 p_su = empty_option;
7752 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7753 if (old_arg_files != NULL)
7754 {
7755 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007756 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7757 old_arg_count = GARGCOUNT;
7758 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007760 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 && new_arg_file_count > 0)
7762 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007763 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7764 TRUE, fnum_list, fnum_len);
7765 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 }
7768 p_su = save_p_su;
7769}
7770#endif
7771
7772/*
7773 * Set the argument list for the current window.
7774 * Takes over the allocated files[] and the allocated fnames in it.
7775 */
7776 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007777alist_set(
7778 alist_T *al,
7779 int count,
7780 char_u **files,
7781 int use_curbuf,
7782 int *fnum_list,
7783 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784{
7785 int i;
7786
7787 alist_clear(al);
7788 if (ga_grow(&al->al_ga, count) == OK)
7789 {
7790 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007791 {
7792 if (got_int)
7793 {
7794 /* When adding many buffers this can take a long time. Allow
7795 * interrupting here. */
7796 while (i < count)
7797 vim_free(files[i++]);
7798 break;
7799 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007800
7801 /* May set buffer name of a buffer previously used for the
7802 * argument list, so that it's re-used by alist_add. */
7803 if (fnum_list != NULL && i < fnum_len)
7804 buf_set_name(fnum_list[i], files[i]);
7805
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007807 ui_breakcheck();
7808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 vim_free(files);
7810 }
7811 else
7812 FreeWild(count, files);
7813#ifdef FEAT_WINDOWS
7814 if (al == &global_alist)
7815#endif
7816 arg_had_last = FALSE;
7817}
7818
7819/*
7820 * Add file "fname" to argument list "al".
7821 * "fname" must have been allocated and "al" must have been checked for room.
7822 */
7823 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007824alist_add(
7825 alist_T *al,
7826 char_u *fname,
7827 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828{
7829 if (fname == NULL) /* don't add NULL file names */
7830 return;
7831#ifdef BACKSLASH_IN_FILENAME
7832 slash_adjust(fname);
7833#endif
7834 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7835 if (set_fnum > 0)
7836 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7837 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7838 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839}
7840
7841#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7842/*
7843 * Adjust slashes in file names. Called after 'shellslash' was set.
7844 */
7845 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007846alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847{
7848 int i;
7849# ifdef FEAT_WINDOWS
7850 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007851 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852# endif
7853
7854 for (i = 0; i < GARGCOUNT; ++i)
7855 if (GARGLIST[i].ae_fname != NULL)
7856 slash_adjust(GARGLIST[i].ae_fname);
7857# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007858 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 if (wp->w_alist != &global_alist)
7860 for (i = 0; i < WARGCOUNT(wp); ++i)
7861 if (WARGLIST(wp)[i].ae_fname != NULL)
7862 slash_adjust(WARGLIST(wp)[i].ae_fname);
7863# endif
7864}
7865#endif
7866
7867/*
7868 * ":preserve".
7869 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007871ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007873 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 ml_preserve(curbuf, TRUE);
7875}
7876
7877/*
7878 * ":recover".
7879 */
7880 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007881ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882{
7883 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7884 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007885 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7886 | CCGD_MULTWIN
7887 | (eap->forceit ? CCGD_FORCEIT : 0)
7888 | CCGD_EXCMD)
7889
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 && (*eap->arg == NUL
7891 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7892 ml_recover();
7893 recoverymode = FALSE;
7894}
7895
7896/*
7897 * Command modifier used in a wrong way.
7898 */
7899 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007900ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901{
7902 eap->errmsg = e_invcmd;
7903}
7904
7905#ifdef FEAT_WINDOWS
7906/*
7907 * :sview [+command] file split window with new file, read-only
7908 * :split [[+command] file] split window with current or new file
7909 * :vsplit [[+command] file] split window vertically with current or new file
7910 * :new [[+command] file] split window with no or new file
7911 * :vnew [[+command] file] split vertically window with no or new file
7912 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007913 *
7914 * :tabedit open new Tab page with empty window
7915 * :tabedit [+command] file open new Tab page and edit "file"
7916 * :tabnew [[+command] file] just like :tabedit
7917 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 */
7919 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007920ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007922 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007923# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007925# endif
7926# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007928# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007930# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7932 {
7933 ex_ni(eap);
7934 return;
7935 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007936# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007938# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007940# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007942# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007944 * quickfix windows. But it's OK when doing ":tab split". */
7945 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 {
7947 if (eap->cmdidx == CMD_split)
7948 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007949# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 if (eap->cmdidx == CMD_vsplit)
7951 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007952# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007954# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007956# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007957 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 {
7959 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7960 FNAME_MESS, TRUE, curbuf->b_ffname);
7961 if (fname == NULL)
7962 goto theend;
7963 eap->arg = fname;
7964 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007965# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007967# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007969# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007971# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007973# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 && eap->cmdidx != CMD_new)
7975 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007976# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007977 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007978# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007979 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007980# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007981 au_has_group((char_u *)"FileExplorer"))
7982 {
7983 /* No browsing supported but we do have the file explorer:
7984 * Edit the directory. */
7985 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7986 eap->arg = (char_u *)".";
7987 }
7988 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007989# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007990 {
7991 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007993 if (fname == NULL)
7994 goto theend;
7995 eap->arg = fname;
7996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 }
7998 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007999# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008001 /*
8002 * Either open new tab page or split the window.
8003 */
8004 if (eap->cmdidx == CMD_tabedit
8005 || eap->cmdidx == CMD_tabfind
8006 || eap->cmdidx == CMD_tabnew)
8007 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008008 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8009 : eap->addr_count == 0 ? 0
8010 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008011 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008012 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008013
8014 /* set the alternate buffer for the window we came from */
8015 if (curwin != old_curwin
8016 && win_valid(old_curwin)
8017 && old_curwin->w_buffer != curbuf
8018 && !cmdmod.keepalt)
8019 old_curwin->w_alt_fnum = curbuf->b_fnum;
8020 }
8021 }
8022 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8024 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008025# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 /* Reset 'scrollbind' when editing another file, but keep it when
8027 * doing ":split" without arguments. */
8028 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008029# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008031# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008033 {
8034 RESET_BINDING(curwin);
8035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 else
8037 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008038# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 do_exedit(eap, old_curwin);
8040 }
8041
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008042# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008044# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008046# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047theend:
8048 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008049# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008051
8052/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008053 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008054 */
8055 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008056tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008057{
8058 exarg_T ea;
8059
8060 vim_memset(&ea, 0, sizeof(ea));
8061 ea.cmdidx = CMD_tabnew;
8062 ea.cmd = (char_u *)"tabn";
8063 ea.arg = (char_u *)"";
8064 ex_splitview(&ea);
8065}
8066
8067/*
8068 * :tabnext command
8069 */
8070 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008071ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008072{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008073 switch (eap->cmdidx)
8074 {
8075 case CMD_tabfirst:
8076 case CMD_tabrewind:
8077 goto_tabpage(1);
8078 break;
8079 case CMD_tablast:
8080 goto_tabpage(9999);
8081 break;
8082 case CMD_tabprevious:
8083 case CMD_tabNext:
8084 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
8085 break;
8086 default: /* CMD_tabnext */
8087 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
8088 break;
8089 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008090}
8091
8092/*
8093 * :tabmove command
8094 */
8095 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008096ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008097{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008098 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008099
8100 if (eap->arg && *eap->arg != NUL)
8101 {
8102 char_u *p = eap->arg;
8103 int relative = 0; /* argument +N/-N means: move N places to the
8104 * right/left relative to the current position. */
8105
8106 if (*eap->arg == '-')
8107 {
8108 relative = -1;
8109 p = eap->arg + 1;
8110 }
8111 else if (*eap->arg == '+')
8112 {
8113 relative = 1;
8114 p = eap->arg + 1;
8115 }
8116 else
8117 p = eap->arg;
8118
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008119 if (relative == 0)
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008120 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008121 if (STRCMP(p, "$") == 0)
8122 tab_number = LAST_TAB_NR;
8123 else if (p == skipdigits(p))
8124 {
8125 /* No numbers as argument. */
8126 eap->errmsg = e_invarg;
8127 return;
8128 }
8129 else
8130 tab_number = getdigits(&p);
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008131 }
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008132 else
8133 {
8134 if (*p != NUL)
8135 tab_number = getdigits(&p);
8136 else
8137 tab_number = 1;
8138 tab_number = tab_number * relative + tabpage_index(curtab);
8139 if (relative == -1)
8140 --tab_number;
8141 }
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008142 }
8143 else if (eap->addr_count != 0)
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008144 {
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008145 tab_number = eap->line2;
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008146 if (**eap->cmdlinep == '-')
8147 --tab_number;
8148 }
8149 else
8150 tab_number = LAST_TAB_NR;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008151
8152 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008153}
8154
8155/*
8156 * :tabs command: List tabs and their contents.
8157 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008158 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008159ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008160{
8161 tabpage_T *tp;
8162 win_T *wp;
8163 int tabcount = 1;
8164
8165 msg_start();
8166 msg_scroll = TRUE;
8167 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8168 {
8169 msg_putchar('\n');
8170 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
8171 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
8172 out_flush(); /* output one line at a time */
8173 ui_breakcheck();
8174
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008175 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008176 wp = firstwin;
8177 else
8178 wp = tp->tp_firstwin;
8179 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8180 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008181 msg_putchar('\n');
8182 msg_putchar(wp == curwin ? '>' : ' ');
8183 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008184 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8185 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008186 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008187 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008188 else
8189 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8190 IObuff, IOSIZE, TRUE);
8191 msg_outtrans(IObuff);
8192 out_flush(); /* output one line at a time */
8193 ui_breakcheck();
8194 }
8195 }
8196}
8197
8198#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199
8200/*
8201 * ":mode": Set screen mode.
8202 * If no argument given, just get the screen size and redraw.
8203 */
8204 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008205ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206{
8207 if (*eap->arg == NUL)
8208 shell_resized();
8209 else
8210 mch_screenmode(eap->arg);
8211}
8212
8213#ifdef FEAT_WINDOWS
8214/*
8215 * ":resize".
8216 * set, increment or decrement current window height
8217 */
8218 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008219ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220{
8221 int n;
8222 win_T *wp = curwin;
8223
8224 if (eap->addr_count > 0)
8225 {
8226 n = eap->line2;
8227 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8228 ;
8229 }
8230
8231#ifdef FEAT_GUI
8232 need_mouse_correct = TRUE;
8233#endif
8234 n = atol((char *)eap->arg);
8235#ifdef FEAT_VERTSPLIT
8236 if (cmdmod.split & WSP_VERT)
8237 {
8238 if (*eap->arg == '-' || *eap->arg == '+')
8239 n += W_WIDTH(curwin);
8240 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8241 n = 9999;
8242 win_setwidth_win((int)n, wp);
8243 }
8244 else
8245#endif
8246 {
8247 if (*eap->arg == '-' || *eap->arg == '+')
8248 n += curwin->w_height;
8249 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8250 n = 9999;
8251 win_setheight_win((int)n, wp);
8252 }
8253}
8254#endif
8255
8256/*
8257 * ":find [+command] <file>" command.
8258 */
8259 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008260ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261{
8262#ifdef FEAT_SEARCHPATH
8263 char_u *fname;
8264 int count;
8265
8266 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8267 TRUE, curbuf->b_ffname);
8268 if (eap->addr_count > 0)
8269 {
8270 /* Repeat finding the file "count" times. This matters when it
8271 * appears several times in the path. */
8272 count = eap->line2;
8273 while (fname != NULL && --count > 0)
8274 {
8275 vim_free(fname);
8276 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8277 FALSE, curbuf->b_ffname);
8278 }
8279 }
8280
8281 if (fname != NULL)
8282 {
8283 eap->arg = fname;
8284#endif
8285 do_exedit(eap, NULL);
8286#ifdef FEAT_SEARCHPATH
8287 vim_free(fname);
8288 }
8289#endif
8290}
8291
8292/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008293 * ":open" simulation: for now just work like ":visual".
8294 */
8295 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008296ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008297{
8298 regmatch_T regmatch;
8299 char_u *p;
8300
8301 curwin->w_cursor.lnum = eap->line2;
8302 beginline(BL_SOL | BL_FIX);
8303 if (*eap->arg == '/')
8304 {
8305 /* ":open /pattern/": put cursor in column found with pattern */
8306 ++eap->arg;
8307 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8308 *p = NUL;
8309 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8310 if (regmatch.regprog != NULL)
8311 {
8312 regmatch.rm_ic = p_ic;
8313 p = ml_get_curline();
8314 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008315 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008316 else
8317 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008318 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008319 }
8320 /* Move to the NUL, ignore any other arguments. */
8321 eap->arg += STRLEN(eap->arg);
8322 }
8323 check_cursor();
8324
8325 eap->cmdidx = CMD_visual;
8326 do_exedit(eap, NULL);
8327}
8328
8329/*
8330 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 */
8332 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008333ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334{
8335 do_exedit(eap, NULL);
8336}
8337
8338/*
8339 * ":edit <file>" command and alikes.
8340 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008342do_exedit(
8343 exarg_T *eap,
8344 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345{
8346 int n;
8347#ifdef FEAT_WINDOWS
8348 int need_hide;
8349#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008350 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351
8352 /*
8353 * ":vi" command ends Ex mode.
8354 */
8355 if (exmode_active && (eap->cmdidx == CMD_visual
8356 || eap->cmdidx == CMD_view))
8357 {
8358 exmode_active = FALSE;
8359 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008360 {
8361 /* Special case: ":global/pat/visual\NLvi-commands" */
8362 if (global_busy)
8363 {
8364 int rd = RedrawingDisabled;
8365 int nwr = no_wait_return;
8366 int ms = msg_scroll;
8367#ifdef FEAT_GUI
8368 int he = hold_gui_events;
8369#endif
8370
8371 if (eap->nextcmd != NULL)
8372 {
8373 stuffReadbuff(eap->nextcmd);
8374 eap->nextcmd = NULL;
8375 }
8376
8377 if (exmode_was != EXMODE_VIM)
8378 settmode(TMODE_RAW);
8379 RedrawingDisabled = 0;
8380 no_wait_return = 0;
8381 need_wait_return = FALSE;
8382 msg_scroll = 0;
8383#ifdef FEAT_GUI
8384 hold_gui_events = 0;
8385#endif
8386 must_redraw = CLEAR;
8387
8388 main_loop(FALSE, TRUE);
8389
8390 RedrawingDisabled = rd;
8391 no_wait_return = nwr;
8392 msg_scroll = ms;
8393#ifdef FEAT_GUI
8394 hold_gui_events = he;
8395#endif
8396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 }
8400
8401 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008402 || eap->cmdidx == CMD_tabnew
8403 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404#ifdef FEAT_VERTSPLIT
8405 || eap->cmdidx == CMD_vnew
8406#endif
8407 ) && *eap->arg == NUL)
8408 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008409 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 setpcmark();
8411 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008412 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8413 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 }
8415 else if ((eap->cmdidx != CMD_split
8416#ifdef FEAT_VERTSPLIT
8417 && eap->cmdidx != CMD_vsplit
8418#endif
8419 )
8420 || *eap->arg != NUL
8421#ifdef FEAT_BROWSE
8422 || cmdmod.browse
8423#endif
8424 )
8425 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008426#ifdef FEAT_AUTOCMD
8427 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8428 * can bring us here, others are stopped earlier. */
8429 if (*eap->arg != NUL && curbuf_locked())
8430 return;
8431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 n = readonlymode;
8433 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8434 readonlymode = TRUE;
8435 else if (eap->cmdidx == CMD_enew)
8436 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8437 empty buffer */
8438 setpcmark();
8439 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8440 NULL, eap,
8441 /* ":edit" goes to first line if Vi compatible */
8442 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8443 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8444 ? ECMD_ONE : eap->do_ecmd_lnum,
8445 (P_HID(curbuf) ? ECMD_HIDE : 0)
8446 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008447 /* after a split we can use an existing buffer */
8448 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449#ifdef FEAT_LISTCMDS
8450 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8451#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008452 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 {
8454 /* Editing the file failed. If the window was split, close it. */
8455#ifdef FEAT_WINDOWS
8456 if (old_curwin != NULL)
8457 {
8458 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8459 if (!need_hide || P_HID(curbuf))
8460 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008461# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8462 cleanup_T cs;
8463
8464 /* Reset the error/interrupt/exception state here so that
8465 * aborting() returns FALSE when closing a window. */
8466 enter_cleanup(&cs);
8467# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468# ifdef FEAT_GUI
8469 need_mouse_correct = TRUE;
8470# endif
8471 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008472
8473# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8474 /* Restore the error/interrupt/exception state if not
8475 * discarded by a new aborting error, interrupt, or
8476 * uncaught exception. */
8477 leave_cleanup(&cs);
8478# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 }
8480 }
8481#endif
8482 }
8483 else if (readonlymode && curbuf->b_nwindows == 1)
8484 {
8485 /* When editing an already visited buffer, 'readonly' won't be set
8486 * but the previous value is kept. With ":view" and ":sview" we
8487 * want the file to be readonly, except when another window is
8488 * editing the same buffer. */
8489 curbuf->b_p_ro = TRUE;
8490 }
8491 readonlymode = n;
8492 }
8493 else
8494 {
8495 if (eap->do_ecmd_cmd != NULL)
8496 do_cmdline_cmd(eap->do_ecmd_cmd);
8497#ifdef FEAT_TITLE
8498 n = curwin->w_arg_idx_invalid;
8499#endif
8500 check_arg_idx(curwin);
8501#ifdef FEAT_TITLE
8502 if (n != curwin->w_arg_idx_invalid)
8503 maketitle();
8504#endif
8505 }
8506
8507#ifdef FEAT_WINDOWS
8508 /*
8509 * if ":split file" worked, set alternate file name in old window to new
8510 * file
8511 */
8512 if (old_curwin != NULL
8513 && *eap->arg != NUL
8514 && curwin != old_curwin
8515 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008516 && old_curwin->w_buffer != curbuf
8517 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 old_curwin->w_alt_fnum = curbuf->b_fnum;
8519#endif
8520
8521 ex_no_reprint = TRUE;
8522}
8523
8524#ifndef FEAT_GUI
8525/*
8526 * ":gui" and ":gvim" when there is no GUI.
8527 */
8528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008529ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530{
8531 eap->errmsg = e_nogvim;
8532}
8533#endif
8534
8535#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8536 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008537ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538{
8539 gui_make_tearoff(eap->arg);
8540}
8541#endif
8542
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008543#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008545ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008547 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548}
8549#endif
8550
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008552ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553{
8554 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8555 MSG(_("No swap file"));
8556 else
8557 msg(curbuf->b_ml.ml_mfp->mf_fname);
8558}
8559
8560/*
8561 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8562 * offset.
8563 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8564 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008566ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567{
8568#ifdef FEAT_SCROLLBIND
8569 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008570 win_T *save_curwin = curwin;
8571 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 long topline;
8573 long y;
8574 linenr_T old_linenr = curwin->w_cursor.lnum;
8575
8576 setpcmark();
8577
8578 /*
8579 * determine max topline
8580 */
8581 if (curwin->w_p_scb)
8582 {
8583 topline = curwin->w_topline;
8584 for (wp = firstwin; wp; wp = wp->w_next)
8585 {
8586 if (wp->w_p_scb && wp->w_buffer)
8587 {
8588 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8589 if (topline > y)
8590 topline = y;
8591 }
8592 }
8593 if (topline < 1)
8594 topline = 1;
8595 }
8596 else
8597 {
8598 topline = 1;
8599 }
8600
8601
8602 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008603 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8606 {
8607 if (curwin->w_p_scb)
8608 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008609 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 y = topline - curwin->w_topline;
8611 if (y > 0)
8612 scrollup(y, TRUE);
8613 else
8614 scrolldown(-y, TRUE);
8615 curwin->w_scbind_pos = topline;
8616 redraw_later(VALID);
8617 cursor_correct();
8618#ifdef FEAT_WINDOWS
8619 curwin->w_redr_status = TRUE;
8620#endif
8621 }
8622 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008623 curwin = save_curwin;
8624 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 if (curwin->w_p_scb)
8626 {
8627 did_syncbind = TRUE;
8628 checkpcmark();
8629 if (old_linenr != curwin->w_cursor.lnum)
8630 {
8631 char_u ctrl_o[2];
8632
8633 ctrl_o[0] = Ctrl_O;
8634 ctrl_o[1] = 0;
8635 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8636 }
8637 }
8638#endif
8639}
8640
8641
8642 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008643ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008645 int i;
8646 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8647 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648
8649 if (eap->usefilter) /* :r!cmd */
8650 do_bang(1, eap, FALSE, FALSE, TRUE);
8651 else
8652 {
8653 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8654 return;
8655
8656#ifdef FEAT_BROWSE
8657 if (cmdmod.browse)
8658 {
8659 char_u *browseFile;
8660
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008661 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 NULL, NULL, NULL, curbuf);
8663 if (browseFile != NULL)
8664 {
8665 i = readfile(browseFile, NULL,
8666 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8667 vim_free(browseFile);
8668 }
8669 else
8670 i = OK;
8671 }
8672 else
8673#endif
8674 if (*eap->arg == NUL)
8675 {
8676 if (check_fname() == FAIL) /* check for no file name */
8677 return;
8678 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8679 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8680 }
8681 else
8682 {
8683 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8684 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8685 i = readfile(eap->arg, NULL,
8686 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8687
8688 }
8689 if (i == FAIL)
8690 {
8691#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8692 if (!aborting())
8693#endif
8694 EMSG2(_(e_notopen), eap->arg);
8695 }
8696 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008697 {
8698 if (empty && exmode_active)
8699 {
8700 /* Delete the empty line that remains. Historically ex does
8701 * this but vi doesn't. */
8702 if (eap->line2 == 0)
8703 lnum = curbuf->b_ml.ml_line_count;
8704 else
8705 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008706 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008707 {
8708 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008709 if (curwin->w_cursor.lnum > 1
8710 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008711 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008712 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008713 }
8714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 }
8718}
8719
Bram Moolenaarea408852005-06-25 22:49:46 +00008720static char_u *prev_dir = NULL;
8721
8722#if defined(EXITFREE) || defined(PROTO)
8723 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008724free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00008725{
8726 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008727 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008728
8729 vim_free(globaldir);
8730 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008731}
8732#endif
8733
Bram Moolenaarf4258302013-06-02 18:20:17 +02008734/*
8735 * Deal with the side effects of changing the current directory.
8736 * When "local" is TRUE then this was after an ":lcd" command.
8737 */
8738 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008739post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008740{
8741 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008742 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008743 if (local)
8744 {
8745 /* If still in global directory, need to remember current
8746 * directory as global directory. */
8747 if (globaldir == NULL && prev_dir != NULL)
8748 globaldir = vim_strsave(prev_dir);
8749 /* Remember this local directory for the window. */
8750 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8751 curwin->w_localdir = vim_strsave(NameBuff);
8752 }
8753 else
8754 {
8755 /* We are now in the global directory, no need to remember its
8756 * name. */
8757 vim_free(globaldir);
8758 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008759 }
8760
8761 shorten_fnames(TRUE);
8762}
8763
Bram Moolenaarea408852005-06-25 22:49:46 +00008764
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765/*
8766 * ":cd", ":lcd", ":chdir" and ":lchdir".
8767 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008768 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008769ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 char_u *new_dir;
8772 char_u *tofree;
8773
8774 new_dir = eap->arg;
8775#if !defined(UNIX) && !defined(VMS)
8776 /* for non-UNIX ":cd" means: print current directory */
8777 if (*new_dir == NUL)
8778 ex_pwd(NULL);
8779 else
8780#endif
8781 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008782#ifdef FEAT_AUTOCMD
8783 if (allbuf_locked())
8784 return;
8785#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008786 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8787 && !eap->forceit)
8788 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008789 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008790 return;
8791 }
8792
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 /* ":cd -": Change to previous directory */
8794 if (STRCMP(new_dir, "-") == 0)
8795 {
8796 if (prev_dir == NULL)
8797 {
8798 EMSG(_("E186: No previous directory"));
8799 return;
8800 }
8801 new_dir = prev_dir;
8802 }
8803
8804 /* Save current directory for next ":cd -" */
8805 tofree = prev_dir;
8806 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8807 prev_dir = vim_strsave(NameBuff);
8808 else
8809 prev_dir = NULL;
8810
8811#if defined(UNIX) || defined(VMS)
8812 /* for UNIX ":cd" means: go to home directory */
8813 if (*new_dir == NUL)
8814 {
8815 /* use NameBuff for home directory name */
8816# ifdef VMS
8817 char_u *p;
8818
8819 p = mch_getenv((char_u *)"SYS$LOGIN");
8820 if (p == NULL || *p == NUL) /* empty is the same as not set */
8821 NameBuff[0] = NUL;
8822 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008823 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824# else
8825 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8826# endif
8827 new_dir = NameBuff;
8828 }
8829#endif
8830 if (new_dir == NULL || vim_chdir(new_dir))
8831 EMSG(_(e_failed));
8832 else
8833 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008834 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835
8836 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008837 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 ex_pwd(eap);
8839 }
8840 vim_free(tofree);
8841 }
8842}
8843
8844/*
8845 * ":pwd".
8846 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008848ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849{
8850 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8851 {
8852#ifdef BACKSLASH_IN_FILENAME
8853 slash_adjust(NameBuff);
8854#endif
8855 msg(NameBuff);
8856 }
8857 else
8858 EMSG(_("E187: Unknown"));
8859}
8860
8861/*
8862 * ":=".
8863 */
8864 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008865ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866{
8867 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008868 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869}
8870
8871 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008872ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008874 int n;
8875 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876
8877 if (cursor_valid())
8878 {
8879 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8880 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01008881 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008883
8884 len = eap->line2;
8885 switch (*eap->arg)
8886 {
8887 case 'm': break;
8888 case NUL: len *= 1000L; break;
8889 default: EMSG2(_(e_invarg2), eap->arg); return;
8890 }
8891 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892}
8893
8894/*
8895 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8896 */
8897 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008898do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899{
8900 long done;
8901
8902 cursor_on();
8903 out_flush();
8904 for (done = 0; !got_int && done < msec; done += 1000L)
8905 {
8906 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8907 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008908#ifdef MESSAGE_QUEUE
8909 /* Process the netbeans and clientserver messages that may have been
8910 * received in the call to ui_breakcheck() when the GUI is in use. This
8911 * may occur when running a test case. */
8912 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 }
8915}
8916
8917 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008918do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919{
8920 int mode;
8921 char_u *cmdp;
8922
8923 cmdp = eap->cmd;
8924 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8925
8926 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8927 eap->arg, mode, isabbrev))
8928 {
8929 case 1: EMSG(_(e_invarg));
8930 break;
8931 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8932 break;
8933 }
8934}
8935
8936/*
8937 * ":winsize" command (obsolete).
8938 */
8939 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008940ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941{
8942 int w, h;
8943 char_u *arg = eap->arg;
8944 char_u *p;
8945
8946 w = getdigits(&arg);
8947 arg = skipwhite(arg);
8948 p = arg;
8949 h = getdigits(&arg);
8950 if (*p != NUL && *arg == NUL)
8951 set_shellsize(w, h, TRUE);
8952 else
8953 EMSG(_("E465: :winsize requires two number arguments"));
8954}
8955
8956#ifdef FEAT_WINDOWS
8957 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008958ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959{
8960 int xchar = NUL;
8961 char_u *p;
8962
8963 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8964 {
8965 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8966 if (eap->arg[1] == NUL)
8967 {
8968 EMSG(_(e_invarg));
8969 return;
8970 }
8971 xchar = eap->arg[1];
8972 p = eap->arg + 2;
8973 }
8974 else
8975 p = eap->arg + 1;
8976
8977 eap->nextcmd = check_nextcmd(p);
8978 p = skipwhite(p);
8979 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8980 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008981 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 {
8983 /* Pass flags on for ":vertical wincmd ]". */
8984 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008985 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8987 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008988 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 }
8990}
8991#endif
8992
Bram Moolenaar843ee412004-06-30 16:16:41 +00008993#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994/*
8995 * ":winpos".
8996 */
8997 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008998ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999{
9000 int x, y;
9001 char_u *arg = eap->arg;
9002 char_u *p;
9003
9004 if (*arg == NUL)
9005 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009006# if defined(FEAT_GUI) || defined(MSWIN)
9007# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009009# else
9010 if (mch_get_winpos(&x, &y) != FAIL)
9011# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 {
9013 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9014 msg(IObuff);
9015 }
9016 else
9017# endif
9018 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9019 }
9020 else
9021 {
9022 x = getdigits(&arg);
9023 arg = skipwhite(arg);
9024 p = arg;
9025 y = getdigits(&arg);
9026 if (*p == NUL || *arg != NUL)
9027 {
9028 EMSG(_("E466: :winpos requires two number arguments"));
9029 return;
9030 }
9031# ifdef FEAT_GUI
9032 if (gui.in_use)
9033 gui_mch_set_winpos(x, y);
9034 else if (gui.starting)
9035 {
9036 /* Remember the coordinates for when the window is opened. */
9037 gui_win_x = x;
9038 gui_win_y = y;
9039 }
9040# ifdef HAVE_TGETENT
9041 else
9042# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009043# else
9044# ifdef MSWIN
9045 mch_set_winpos(x, y);
9046# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047# endif
9048# ifdef HAVE_TGETENT
9049 if (*T_CWP)
9050 term_set_winpos(x, y);
9051# endif
9052 }
9053}
9054#endif
9055
9056/*
9057 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9058 */
9059 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009060ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061{
9062 oparg_T oa;
9063
9064 clear_oparg(&oa);
9065 oa.regname = eap->regname;
9066 oa.start.lnum = eap->line1;
9067 oa.end.lnum = eap->line2;
9068 oa.line_count = eap->line2 - eap->line1 + 1;
9069 oa.motion_type = MLINE;
9070#ifdef FEAT_VIRTUALEDIT
9071 virtual_op = FALSE;
9072#endif
9073 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9074 {
9075 setpcmark();
9076 curwin->w_cursor.lnum = eap->line1;
9077 beginline(BL_SOL | BL_FIX);
9078 }
9079
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009080 if (VIsual_active)
9081 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009082
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 switch (eap->cmdidx)
9084 {
9085 case CMD_delete:
9086 oa.op_type = OP_DELETE;
9087 op_delete(&oa);
9088 break;
9089
9090 case CMD_yank:
9091 oa.op_type = OP_YANK;
9092 (void)op_yank(&oa, FALSE, TRUE);
9093 break;
9094
9095 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009096 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009098 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9099#else
9100 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009102 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 oa.op_type = OP_RSHIFT;
9104 else
9105 oa.op_type = OP_LSHIFT;
9106 op_shift(&oa, FALSE, eap->amount);
9107 break;
9108 }
9109#ifdef FEAT_VIRTUALEDIT
9110 virtual_op = MAYBE;
9111#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009112 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113}
9114
9115/*
9116 * ":put".
9117 */
9118 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009119ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120{
9121 /* ":0put" works like ":1put!". */
9122 if (eap->line2 == 0)
9123 {
9124 eap->line2 = 1;
9125 eap->forceit = TRUE;
9126 }
9127 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009128 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9129 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130}
9131
9132/*
9133 * Handle ":copy" and ":move".
9134 */
9135 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009136ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137{
9138 long n;
9139
Bram Moolenaaraa23b372015-09-08 18:46:31 +02009140 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 if (eap->arg == NULL) /* error detected */
9142 {
9143 eap->nextcmd = NULL;
9144 return;
9145 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009146 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147
9148 /*
9149 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9150 */
9151 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9152 {
9153 EMSG(_(e_invaddr));
9154 return;
9155 }
9156
9157 if (eap->cmdidx == CMD_move)
9158 {
9159 if (do_move(eap->line1, eap->line2, n) == FAIL)
9160 return;
9161 }
9162 else
9163 ex_copy(eap->line1, eap->line2, n);
9164 u_clearline();
9165 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009166 ex_may_print(eap);
9167}
9168
9169/*
9170 * Print the current line if flags were given to the Ex command.
9171 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009172 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009173ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009174{
9175 if (eap->flags != 0)
9176 {
9177 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9178 (eap->flags & EXFLAG_LIST));
9179 ex_no_reprint = TRUE;
9180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181}
9182
9183/*
9184 * ":smagic" and ":snomagic".
9185 */
9186 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009187ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188{
9189 int magic_save = p_magic;
9190
9191 p_magic = (eap->cmdidx == CMD_smagic);
9192 do_sub(eap);
9193 p_magic = magic_save;
9194}
9195
9196/*
9197 * ":join".
9198 */
9199 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009200ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201{
9202 curwin->w_cursor.lnum = eap->line1;
9203 if (eap->line1 == eap->line2)
9204 {
9205 if (eap->addr_count >= 2) /* :2,2join does nothing */
9206 return;
9207 if (eap->line2 == curbuf->b_ml.ml_line_count)
9208 {
9209 beep_flush();
9210 return;
9211 }
9212 ++eap->line2;
9213 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009214 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009216 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217}
9218
9219/*
9220 * ":[addr]@r" or ":[addr]*r": execute register
9221 */
9222 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009223ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224{
9225 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009226 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227
9228 curwin->w_cursor.lnum = eap->line2;
9229
9230#ifdef USE_ON_FLY_SCROLL
9231 dont_scroll = TRUE; /* disallow scrolling here */
9232#endif
9233
9234 /* get the register name. No name means to use the previous one */
9235 c = *eap->arg;
9236 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9237 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009238 /* Put the register in the typeahead buffer with the "silent" flag. */
9239 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9240 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009241 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244 else
9245 {
9246 int save_efr = exec_from_reg;
9247
9248 exec_from_reg = TRUE;
9249
9250 /*
9251 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009252 * Continue until the stuff buffer is empty and all added characters
9253 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009255 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9257
9258 exec_from_reg = save_efr;
9259 }
9260}
9261
9262/*
9263 * ":!".
9264 */
9265 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009266ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267{
9268 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9269}
9270
9271/*
9272 * ":undo".
9273 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009275ex_undo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009277 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009278 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009279 else
9280 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281}
9282
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009283#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009284 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009285ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009286{
9287 char_u hash[UNDO_HASH_SIZE];
9288
9289 u_compute_hash(hash);
9290 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9291}
9292
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009293 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009294ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009295{
9296 char_u hash[UNDO_HASH_SIZE];
9297
9298 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009299 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009300}
9301#endif
9302
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303/*
9304 * ":redo".
9305 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009307ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308{
9309 u_redo(1);
9310}
9311
9312/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009313 * ":earlier" and ":later".
9314 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009315 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009316ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009317{
9318 long count = 0;
9319 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009320 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009321 char_u *p = eap->arg;
9322
9323 if (*p == NUL)
9324 count = 1;
9325 else if (isdigit(*p))
9326 {
9327 count = getdigits(&p);
9328 switch (*p)
9329 {
9330 case 's': ++p; sec = TRUE; break;
9331 case 'm': ++p; sec = TRUE; count *= 60; break;
9332 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009333 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9334 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009335 }
9336 }
9337
9338 if (*p != NUL)
9339 EMSG2(_(e_invarg2), eap->arg);
9340 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009341 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9342 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009343}
9344
9345/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 * ":redir": start/stop redirection.
9347 */
9348 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009349ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350{
9351 char *mode;
9352 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009353 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354
9355 if (STRICMP(eap->arg, "END") == 0)
9356 close_redir();
9357 else
9358 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009359 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009361 ++arg;
9362 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009364 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 mode = "a";
9366 }
9367 else
9368 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009369 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370
9371 close_redir();
9372
9373 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009374 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 if (fname == NULL)
9376 return;
9377#ifdef FEAT_BROWSE
9378 if (cmdmod.browse)
9379 {
9380 char_u *browseFile;
9381
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009382 browseFile = do_browse(BROWSE_SAVE,
9383 (char_u *)_("Save Redirection"),
9384 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 if (browseFile == NULL)
9386 return; /* operation cancelled */
9387 vim_free(fname);
9388 fname = browseFile;
9389 eap->forceit = TRUE; /* since dialog already asked */
9390 }
9391#endif
9392
9393 redir_fd = open_exfile(fname, eap->forceit, mode);
9394 vim_free(fname);
9395 }
9396#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009397 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 {
9399 /* redirect to a register a-z (resp. A-Z for appending) */
9400 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009401 ++arg;
9402 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009404 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009405 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009407 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009409 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009410 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009411 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009412 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009414 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009415 if (*arg == '>')
9416 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009417 /* Make register empty when not using @A-@Z and the
9418 * command is valid. */
9419 if (*arg == NUL && !isupper(redir_reg))
9420 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009422 }
9423 if (*arg != NUL)
9424 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009425 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009426 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009427 }
9428 }
9429 else if (*arg == '=' && arg[1] == '>')
9430 {
9431 int append;
9432
9433 /* redirect to a variable */
9434 close_redir();
9435 arg += 2;
9436
9437 if (*arg == '>')
9438 {
9439 ++arg;
9440 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 }
9442 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009443 append = FALSE;
9444
9445 if (var_redir_start(skipwhite(arg), append) == OK)
9446 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 }
9448#endif
9449
9450 /* TODO: redirect to a buffer */
9451
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009453 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009455
9456 /* Make sure redirection is not off. Can happen for cmdline completion
9457 * that indirectly invokes a command to catch its output. */
9458 if (redir_fd != NULL
9459#ifdef FEAT_EVAL
9460 || redir_reg || redir_vname
9461#endif
9462 )
9463 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464}
9465
9466/*
9467 * ":redraw": force redraw
9468 */
9469 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009470ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471{
9472 int r = RedrawingDisabled;
9473 int p = p_lz;
9474
9475 RedrawingDisabled = 0;
9476 p_lz = FALSE;
9477 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009478 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479#ifdef FEAT_TITLE
9480 if (need_maketitle)
9481 maketitle();
9482#endif
9483 RedrawingDisabled = r;
9484 p_lz = p;
9485
9486 /* Reset msg_didout, so that a message that's there is overwritten. */
9487 msg_didout = FALSE;
9488 msg_col = 0;
9489
Bram Moolenaar56667a52013-07-17 11:54:28 +02009490 /* No need to wait after an intentional redraw. */
9491 need_wait_return = FALSE;
9492
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 out_flush();
9494}
9495
9496/*
9497 * ":redrawstatus": force redraw of status line(s)
9498 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009500ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501{
9502#if defined(FEAT_WINDOWS)
9503 int r = RedrawingDisabled;
9504 int p = p_lz;
9505
9506 RedrawingDisabled = 0;
9507 p_lz = FALSE;
9508 if (eap->forceit)
9509 status_redraw_all();
9510 else
9511 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009512 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 RedrawingDisabled = r;
9514 p_lz = p;
9515 out_flush();
9516#endif
9517}
9518
9519 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009520close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521{
9522 if (redir_fd != NULL)
9523 {
9524 fclose(redir_fd);
9525 redir_fd = NULL;
9526 }
9527#ifdef FEAT_EVAL
9528 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009529 if (redir_vname)
9530 {
9531 var_redir_stop();
9532 redir_vname = 0;
9533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534#endif
9535}
9536
9537#if defined(FEAT_SESSION) && defined(USE_CRNL)
9538# define MKSESSION_NL
9539static int mksession_nl = FALSE; /* use NL only in put_eol() */
9540#endif
9541
9542/*
9543 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9544 */
9545 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009546ex_mkrc(
9547 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548{
9549 FILE *fd;
9550 int failed = FALSE;
9551 char_u *fname;
9552#ifdef FEAT_BROWSE
9553 char_u *browseFile = NULL;
9554#endif
9555#ifdef FEAT_SESSION
9556 int view_session = FALSE;
9557 int using_vdir = FALSE; /* using 'viewdir'? */
9558 char_u *viewFile = NULL;
9559 unsigned *flagp;
9560#endif
9561
9562 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9563 {
9564#ifdef FEAT_SESSION
9565 view_session = TRUE;
9566#else
9567 ex_ni(eap);
9568 return;
9569#endif
9570 }
9571
9572#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009573 /* Use the short file name until ":lcd" is used. We also don't use the
9574 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009575 did_lcd = FALSE;
9576
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9578 if (eap->cmdidx == CMD_mkview
9579 && (*eap->arg == NUL
9580 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9581 {
9582 eap->forceit = TRUE;
9583 fname = get_view_file(*eap->arg);
9584 if (fname == NULL)
9585 return;
9586 viewFile = fname;
9587 using_vdir = TRUE;
9588 }
9589 else
9590#endif
9591 if (*eap->arg != NUL)
9592 fname = eap->arg;
9593 else if (eap->cmdidx == CMD_mkvimrc)
9594 fname = (char_u *)VIMRC_FILE;
9595#ifdef FEAT_SESSION
9596 else if (eap->cmdidx == CMD_mksession)
9597 fname = (char_u *)SESSION_FILE;
9598#endif
9599 else
9600 fname = (char_u *)EXRC_FILE;
9601
9602#ifdef FEAT_BROWSE
9603 if (cmdmod.browse)
9604 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009605 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606# ifdef FEAT_SESSION
9607 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9608 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9609# endif
9610 (char_u *)_("Save Setup"),
9611 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9612 if (browseFile == NULL)
9613 goto theend;
9614 fname = browseFile;
9615 eap->forceit = TRUE; /* since dialog already asked */
9616 }
9617#endif
9618
9619#if defined(FEAT_SESSION) && defined(vim_mkdir)
9620 /* When using 'viewdir' may have to create the directory. */
9621 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009622 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623#endif
9624
9625 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9626 if (fd != NULL)
9627 {
9628#ifdef FEAT_SESSION
9629 if (eap->cmdidx == CMD_mkview)
9630 flagp = &vop_flags;
9631 else
9632 flagp = &ssop_flags;
9633#endif
9634
9635#ifdef MKSESSION_NL
9636 /* "unix" in 'sessionoptions': use NL line separator */
9637 if (view_session && (*flagp & SSOP_UNIX))
9638 mksession_nl = TRUE;
9639#endif
9640
9641 /* Write the version command for :mkvimrc */
9642 if (eap->cmdidx == CMD_mkvimrc)
9643 (void)put_line(fd, "version 6.0");
9644
9645#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009646 if (eap->cmdidx == CMD_mksession)
9647 {
9648 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9649 failed = TRUE;
9650 }
9651
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652 if (eap->cmdidx != CMD_mkview)
9653#endif
9654 {
9655 /* Write setting 'compatible' first, because it has side effects.
9656 * For that same reason only do it when needed. */
9657 if (p_cp)
9658 (void)put_line(fd, "if !&cp | set cp | endif");
9659 else
9660 (void)put_line(fd, "if &cp | set nocp | endif");
9661 }
9662
9663#ifdef FEAT_SESSION
9664 if (!view_session
9665 || (eap->cmdidx == CMD_mksession
9666 && (*flagp & SSOP_OPTIONS)))
9667#endif
9668 failed |= (makemap(fd, NULL) == FAIL
9669 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9670
9671#ifdef FEAT_SESSION
9672 if (!failed && view_session)
9673 {
9674 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9675 failed = TRUE;
9676 if (eap->cmdidx == CMD_mksession)
9677 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009678 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679
Bram Moolenaard9462e32011-04-11 21:35:11 +02009680 dirnow = alloc(MAXPATHL);
9681 if (dirnow == NULL)
9682 failed = TRUE;
9683 else
9684 {
9685 /*
9686 * Change to session file's dir.
9687 */
9688 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009690 *dirnow = NUL;
9691 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9692 {
9693 if (vim_chdirfile(fname) == OK)
9694 shorten_fnames(TRUE);
9695 }
9696 else if (*dirnow != NUL
9697 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9698 {
9699 if (mch_chdir((char *)globaldir) == 0)
9700 shorten_fnames(TRUE);
9701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702
Bram Moolenaard9462e32011-04-11 21:35:11 +02009703 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704
Bram Moolenaard9462e32011-04-11 21:35:11 +02009705 /* restore original dir */
9706 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009708 {
9709 if (mch_chdir((char *)dirnow) != 0)
9710 EMSG(_(e_prev_dir));
9711 shorten_fnames(TRUE);
9712 }
9713 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 }
9715 }
9716 else
9717 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009718 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9719 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 }
9721 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9722 == FAIL)
9723 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009724 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9725 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009726 if (eap->cmdidx == CMD_mksession)
9727 {
9728 if (put_line(fd, "unlet SessionLoad") == FAIL)
9729 failed = TRUE;
9730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 }
9732#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009733 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9734 failed = TRUE;
9735
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 failed |= fclose(fd);
9737
9738 if (failed)
9739 EMSG(_(e_write));
9740#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9741 else if (eap->cmdidx == CMD_mksession)
9742 {
9743 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009744 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745
Bram Moolenaard9462e32011-04-11 21:35:11 +02009746 tbuf = alloc(MAXPATHL);
9747 if (tbuf != NULL)
9748 {
9749 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9750 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9751 vim_free(tbuf);
9752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 }
9754#endif
9755#ifdef MKSESSION_NL
9756 mksession_nl = FALSE;
9757#endif
9758 }
9759
9760#ifdef FEAT_BROWSE
9761theend:
9762 vim_free(browseFile);
9763#endif
9764#ifdef FEAT_SESSION
9765 vim_free(viewFile);
9766#endif
9767}
9768
Bram Moolenaardf177f62005-02-22 08:39:57 +00009769#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9770 || defined(PROTO)
9771 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009772vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009773{
9774 if (vim_mkdir(name, prot) != 0)
9775 {
9776 EMSG2(_("E739: Cannot create directory: %s"), name);
9777 return FAIL;
9778 }
9779 return OK;
9780}
9781#endif
9782
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783/*
9784 * Open a file for writing for an Ex command, with some checks.
9785 * Return file descriptor, or NULL on failure.
9786 */
9787 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009788open_exfile(
9789 char_u *fname,
9790 int forceit,
9791 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792{
9793 FILE *fd;
9794
9795#ifdef UNIX
9796 /* with Unix it is possible to open a directory */
9797 if (mch_isdir(fname))
9798 {
9799 EMSG2(_(e_isadir2), fname);
9800 return NULL;
9801 }
9802#endif
9803 if (!forceit && *mode != 'a' && vim_fexists(fname))
9804 {
9805 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9806 return NULL;
9807 }
9808
9809 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9810 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9811
9812 return fd;
9813}
9814
9815/*
9816 * ":mark" and ":k".
9817 */
9818 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009819ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820{
9821 pos_T pos;
9822
9823 if (*eap->arg == NUL) /* No argument? */
9824 EMSG(_(e_argreq));
9825 else if (eap->arg[1] != NUL) /* more than one character? */
9826 EMSG(_(e_trailing));
9827 else
9828 {
9829 pos = curwin->w_cursor; /* save curwin->w_cursor */
9830 curwin->w_cursor.lnum = eap->line2;
9831 beginline(BL_WHITE | BL_FIX);
9832 if (setmark(*eap->arg) == FAIL) /* set mark */
9833 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9834 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9835 }
9836}
9837
9838/*
9839 * Update w_topline, w_leftcol and the cursor position.
9840 */
9841 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009842update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843{
9844 check_cursor(); /* put cursor on valid line */
9845 update_topline();
9846 if (!curwin->w_p_wrap)
9847 validate_cursor();
9848 update_curswant();
9849}
9850
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851/*
9852 * ":normal[!] {commands}": Execute normal mode commands.
9853 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01009854 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009855ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 int save_msg_scroll = msg_scroll;
9858 int save_restart_edit = restart_edit;
9859 int save_msg_didout = msg_didout;
9860 int save_State = State;
9861 tasave_T tabuf;
9862 int save_insertmode = p_im;
9863 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009864 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865#ifdef FEAT_MBYTE
9866 char_u *arg = NULL;
9867 int l;
9868 char_u *p;
9869#endif
9870
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009871 if (ex_normal_lock > 0)
9872 {
9873 EMSG(_(e_secure));
9874 return;
9875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876 if (ex_normal_busy >= p_mmd)
9877 {
9878 EMSG(_("E192: Recursive use of :normal too deep"));
9879 return;
9880 }
9881 ++ex_normal_busy;
9882
9883 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9884 restart_edit = 0; /* don't go to Insert mode */
9885 p_im = FALSE; /* don't use 'insertmode' */
9886
9887#ifdef FEAT_MBYTE
9888 /*
9889 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9890 * this for the K_SPECIAL leading byte, otherwise special keys will not
9891 * work.
9892 */
9893 if (has_mbyte)
9894 {
9895 int len = 0;
9896
9897 /* Count the number of characters to be escaped. */
9898 for (p = eap->arg; *p != NUL; ++p)
9899 {
9900# ifdef FEAT_GUI
9901 if (*p == CSI) /* leadbyte CSI */
9902 len += 2;
9903# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009904 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9906# ifdef FEAT_GUI
9907 || *p == CSI
9908# endif
9909 )
9910 len += 2;
9911 }
9912 if (len > 0)
9913 {
9914 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9915 if (arg != NULL)
9916 {
9917 len = 0;
9918 for (p = eap->arg; *p != NUL; ++p)
9919 {
9920 arg[len++] = *p;
9921# ifdef FEAT_GUI
9922 if (*p == CSI)
9923 {
9924 arg[len++] = KS_EXTRA;
9925 arg[len++] = (int)KE_CSI;
9926 }
9927# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009928 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929 {
9930 arg[len++] = *++p;
9931 if (*p == K_SPECIAL)
9932 {
9933 arg[len++] = KS_SPECIAL;
9934 arg[len++] = KE_FILLER;
9935 }
9936# ifdef FEAT_GUI
9937 else if (*p == CSI)
9938 {
9939 arg[len++] = KS_EXTRA;
9940 arg[len++] = (int)KE_CSI;
9941 }
9942# endif
9943 }
9944 arg[len] = NUL;
9945 }
9946 }
9947 }
9948 }
9949#endif
9950
9951 /*
9952 * Save the current typeahead. This is required to allow using ":normal"
9953 * from an event handler and makes sure we don't hang when the argument
9954 * ends with half a command.
9955 */
9956 save_typeahead(&tabuf);
9957 if (tabuf.typebuf_valid)
9958 {
9959 /*
9960 * Repeat the :normal command for each line in the range. When no
9961 * range given, execute it just once, without positioning the cursor
9962 * first.
9963 */
9964 do
9965 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 if (eap->addr_count != 0)
9967 {
9968 curwin->w_cursor.lnum = eap->line1++;
9969 curwin->w_cursor.col = 0;
9970 }
9971
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009972 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973#ifdef FEAT_MBYTE
9974 arg != NULL ? arg :
9975#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009976 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977 }
9978 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9979 }
9980
9981 /* Might not return to the main loop when in an event handler. */
9982 update_topline_cursor();
9983
9984 /* Restore the previous typeahead. */
9985 restore_typeahead(&tabuf);
9986
9987 --ex_normal_busy;
9988 msg_scroll = save_msg_scroll;
9989 restart_edit = save_restart_edit;
9990 p_im = save_insertmode;
9991 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009992 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9994
9995 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +01009996 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +01009998#ifdef FEAT_MOUSE
9999 setmouse();
10000#endif
10001#ifdef CURSOR_SHAPE
10002 ui_cursor_shape(); /* may show different cursor shape */
10003#endif
10004
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005#ifdef FEAT_MBYTE
10006 vim_free(arg);
10007#endif
10008}
10009
10010/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010011 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 */
10013 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010014ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010016 if (eap->forceit)
10017 {
10018 coladvance((colnr_T)MAXCOL);
10019 curwin->w_curswant = MAXCOL;
10020 curwin->w_set_curswant = FALSE;
10021 }
10022
Bram Moolenaara40c5002005-01-09 21:16:21 +000010023 /* Ignore the command when already in Insert mode. Inserting an
10024 * expression register that invokes a function can do this. */
10025 if (State & INSERT)
10026 return;
10027
Bram Moolenaar64969662005-12-14 21:59:55 +000010028 if (eap->cmdidx == CMD_startinsert)
10029 restart_edit = 'a';
10030 else if (eap->cmdidx == CMD_startreplace)
10031 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010033 restart_edit = 'V';
10034
10035 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010037 if (eap->cmdidx == CMD_startinsert)
10038 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 curwin->w_curswant = 0; /* avoid MAXCOL */
10040 }
10041}
10042
10043/*
10044 * ":stopinsert"
10045 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010047ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048{
10049 restart_edit = 0;
10050 stop_insert_mode = TRUE;
10051}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010053/*
10054 * Execute normal mode command "cmd".
10055 * "remap" can be REMAP_NONE or REMAP_YES.
10056 */
10057 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010058exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010059{
Bram Moolenaar25281632016-01-21 23:32:32 +010010060 /* Stuff the argument into the typeahead buffer. */
10061 ins_typebuf(cmd, remap, 0, TRUE, silent);
10062 exec_normal(FALSE);
10063}
Bram Moolenaar25281632016-01-21 23:32:32 +010010064
Bram Moolenaar25281632016-01-21 23:32:32 +010010065/*
10066 * Execute normal_cmd() until there is no typeahead left.
10067 */
10068 void
10069exec_normal(int was_typed)
10070{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010071 oparg_T oa;
10072
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010073 clear_oparg(&oa);
10074 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010075 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10076 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010077 {
10078 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010079 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010080 }
10081}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010082
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083#ifdef FEAT_FIND_ID
10084 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010085ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086{
10087 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10088 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10089 (linenr_T)1, (linenr_T)MAXLNUM);
10090}
10091
10092#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
10093/*
10094 * ":psearch"
10095 */
10096 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010097ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098{
10099 g_do_tagpreview = p_pvh;
10100 ex_findpat(eap);
10101 g_do_tagpreview = 0;
10102}
10103#endif
10104
10105 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010106ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107{
10108 int whole = TRUE;
10109 long n;
10110 char_u *p;
10111 int action;
10112
10113 switch (cmdnames[eap->cmdidx].cmd_name[2])
10114 {
10115 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10116 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10117 action = ACTION_GOTO;
10118 else
10119 action = ACTION_SHOW;
10120 break;
10121 case 'i': /* ":ilist" and ":dlist" */
10122 action = ACTION_SHOW_ALL;
10123 break;
10124 case 'u': /* ":ijump" and ":djump" */
10125 action = ACTION_GOTO;
10126 break;
10127 default: /* ":isplit" and ":dsplit" */
10128 action = ACTION_SPLIT;
10129 break;
10130 }
10131
10132 n = 1;
10133 if (vim_isdigit(*eap->arg)) /* get count */
10134 {
10135 n = getdigits(&eap->arg);
10136 eap->arg = skipwhite(eap->arg);
10137 }
10138 if (*eap->arg == '/') /* Match regexp, not just whole words */
10139 {
10140 whole = FALSE;
10141 ++eap->arg;
10142 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10143 if (*p)
10144 {
10145 *p++ = NUL;
10146 p = skipwhite(p);
10147
10148 /* Check for trailing illegal characters */
10149 if (!ends_excmd(*p))
10150 eap->errmsg = e_trailing;
10151 else
10152 eap->nextcmd = check_nextcmd(p);
10153 }
10154 }
10155 if (!eap->skip)
10156 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10157 whole, !eap->forceit,
10158 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10159 n, action, eap->line1, eap->line2);
10160}
10161#endif
10162
10163#ifdef FEAT_WINDOWS
10164
10165# ifdef FEAT_QUICKFIX
10166/*
10167 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10168 */
10169 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010170ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010172 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10174}
10175
10176/*
10177 * ":pedit"
10178 */
10179 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010180ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181{
10182 win_T *curwin_save = curwin;
10183
10184 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010185 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 keep_help_flag = curwin_save->w_buffer->b_help;
10187 do_exedit(eap, NULL);
10188 keep_help_flag = FALSE;
10189 if (curwin != curwin_save && win_valid(curwin_save))
10190 {
10191 /* Return cursor to where we were */
10192 validate_cursor();
10193 redraw_later(VALID);
10194 win_enter(curwin_save, TRUE);
10195 }
10196 g_do_tagpreview = 0;
10197}
10198# endif
10199
10200/*
10201 * ":stag", ":stselect" and ":stjump".
10202 */
10203 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010204ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205{
10206 postponed_split = -1;
10207 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010208 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10210 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010211 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212}
10213#endif
10214
10215/*
10216 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10217 */
10218 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010219ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220{
10221 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10222}
10223
10224 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010225ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226{
10227 int cmd;
10228
10229 switch (name[1])
10230 {
10231 case 'j': cmd = DT_JUMP; /* ":tjump" */
10232 break;
10233 case 's': cmd = DT_SELECT; /* ":tselect" */
10234 break;
10235 case 'p': cmd = DT_PREV; /* ":tprevious" */
10236 break;
10237 case 'N': cmd = DT_PREV; /* ":tNext" */
10238 break;
10239 case 'n': cmd = DT_NEXT; /* ":tnext" */
10240 break;
10241 case 'o': cmd = DT_POP; /* ":pop" */
10242 break;
10243 case 'f': /* ":tfirst" */
10244 case 'r': cmd = DT_FIRST; /* ":trewind" */
10245 break;
10246 case 'l': cmd = DT_LAST; /* ":tlast" */
10247 break;
10248 default: /* ":tag" */
10249#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010250 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 {
10252 do_cstag(eap);
10253 return;
10254 }
10255#endif
10256 cmd = DT_TAG;
10257 break;
10258 }
10259
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010260 if (name[0] == 'l')
10261 {
10262#ifndef FEAT_QUICKFIX
10263 ex_ni(eap);
10264 return;
10265#else
10266 cmd = DT_LTAG;
10267#endif
10268 }
10269
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10271 eap->forceit, TRUE);
10272}
10273
10274/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010275 * Check "str" for starting with a special cmdline variable.
10276 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10277 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10278 */
10279 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010280find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010281{
10282 int len;
10283 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010284 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010285 "%",
10286#define SPEC_PERC 0
10287 "#",
10288#define SPEC_HASH 1
10289 "<cword>", /* cursor word */
10290#define SPEC_CWORD 2
10291 "<cWORD>", /* cursor WORD */
10292#define SPEC_CCWORD 3
10293 "<cfile>", /* cursor path name */
10294#define SPEC_CFILE 4
10295 "<sfile>", /* ":so" file name */
10296#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010297 "<slnum>", /* ":so" file line number */
10298#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010299#ifdef FEAT_AUTOCMD
10300 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010301# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010302 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010303# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010304 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010305# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010306#endif
10307#ifdef FEAT_CLIENTSERVER
10308 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010309# ifdef FEAT_AUTOCMD
10310# define SPEC_CLIENT 10
10311# else
10312# define SPEC_CLIENT 7
10313# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010314#endif
10315 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010316
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010317 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010318 {
10319 len = (int)STRLEN(spec_str[i]);
10320 if (STRNCMP(src, spec_str[i], len) == 0)
10321 {
10322 *usedlen = len;
10323 return i;
10324 }
10325 }
10326 return -1;
10327}
10328
10329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 * Evaluate cmdline variables.
10331 *
10332 * change '%' to curbuf->b_ffname
10333 * '#' to curwin->w_altfile
10334 * '<cword>' to word under the cursor
10335 * '<cWORD>' to WORD under the cursor
10336 * '<cfile>' to path name under the cursor
10337 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010338 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 * '<afile>' to file name for autocommand
10340 * '<abuf>' to buffer number for autocommand
10341 * '<amatch>' to matching name for autocommand
10342 *
10343 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10344 * "" for error without a message) and NULL is returned.
10345 * Returns an allocated string if a valid match was found.
10346 * Returns NULL if no match was found. "usedlen" then still contains the
10347 * number of characters to skip.
10348 */
10349 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010350eval_vars(
10351 char_u *src, /* pointer into commandline */
10352 char_u *srcstart, /* beginning of valid memory for src */
10353 int *usedlen, /* characters after src that are used */
10354 linenr_T *lnump, /* line number for :e command, or NULL */
10355 char_u **errormsg, /* pointer to error message */
10356 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010357 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358{
10359 int i;
10360 char_u *s;
10361 char_u *result;
10362 char_u *resultbuf = NULL;
10363 int resultlen;
10364 buf_T *buf;
10365 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10366 int spec_idx;
10367#ifdef FEAT_MODIFY_FNAME
10368 int skip_mod = FALSE;
10369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371
10372 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010373 if (escaped != NULL)
10374 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375
10376 /*
10377 * Check if there is something to do.
10378 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010379 spec_idx = find_cmdline_var(src, usedlen);
10380 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381 {
10382 *usedlen = 1;
10383 return NULL;
10384 }
10385
10386 /*
10387 * Skip when preceded with a backslash "\%" and "\#".
10388 * Note: In "\\%" the % is also not recognized!
10389 */
10390 if (src > srcstart && src[-1] == '\\')
10391 {
10392 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010393 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 return NULL;
10395 }
10396
10397 /*
10398 * word or WORD under cursor
10399 */
10400 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10401 {
10402 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10403 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10404 if (resultlen == 0)
10405 {
10406 *errormsg = (char_u *)"";
10407 return NULL;
10408 }
10409 }
10410
10411 /*
10412 * '#': Alternate file name
10413 * '%': Current file name
10414 * File name under the cursor
10415 * File name for autocommand
10416 * and following modifiers
10417 */
10418 else
10419 {
10420 switch (spec_idx)
10421 {
10422 case SPEC_PERC: /* '%': current file */
10423 if (curbuf->b_fname == NULL)
10424 {
10425 result = (char_u *)"";
10426 valid = 0; /* Must have ":p:h" to be valid */
10427 }
10428 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 break;
10431
10432 case SPEC_HASH: /* '#' or "#99": alternate file */
10433 if (src[1] == '#') /* "##": the argument list */
10434 {
10435 result = arg_all();
10436 resultbuf = result;
10437 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010438 if (escaped != NULL)
10439 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440#ifdef FEAT_MODIFY_FNAME
10441 skip_mod = TRUE;
10442#endif
10443 break;
10444 }
10445 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010446 if (*s == '<') /* "#<99" uses v:oldfiles */
10447 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448 i = (int)getdigits(&s);
10449 *usedlen = (int)(s - src); /* length of what we expand */
10450
Bram Moolenaard812df62008-11-09 12:46:09 +000010451 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010453 if (*usedlen < 2)
10454 {
10455 /* Should we give an error message for #<text? */
10456 *usedlen = 1;
10457 return NULL;
10458 }
10459#ifdef FEAT_EVAL
10460 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10461 (long)i);
10462 if (result == NULL)
10463 {
10464 *errormsg = (char_u *)"";
10465 return NULL;
10466 }
10467#else
10468 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471 }
10472 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010473 {
10474 buf = buflist_findnr(i);
10475 if (buf == NULL)
10476 {
10477 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10478 return NULL;
10479 }
10480 if (lnump != NULL)
10481 *lnump = ECMD_LAST;
10482 if (buf->b_fname == NULL)
10483 {
10484 result = (char_u *)"";
10485 valid = 0; /* Must have ":p:h" to be valid */
10486 }
10487 else
10488 result = buf->b_fname;
10489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490 break;
10491
10492#ifdef FEAT_SEARCHPATH
10493 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010494 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495 if (result == NULL)
10496 {
10497 *errormsg = (char_u *)"";
10498 return NULL;
10499 }
10500 resultbuf = result; /* remember allocated string */
10501 break;
10502#endif
10503
10504#ifdef FEAT_AUTOCMD
10505 case SPEC_AFILE: /* file name for autocommand */
10506 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010507 if (result != NULL && !autocmd_fname_full)
10508 {
10509 /* Still need to turn the fname into a full path. It is
10510 * postponed to avoid a delay when <afile> is not used. */
10511 autocmd_fname_full = TRUE;
10512 result = FullName_save(autocmd_fname, FALSE);
10513 vim_free(autocmd_fname);
10514 autocmd_fname = result;
10515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516 if (result == NULL)
10517 {
10518 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10519 return NULL;
10520 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010521 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522 break;
10523
10524 case SPEC_ABUF: /* buffer number for autocommand */
10525 if (autocmd_bufnr <= 0)
10526 {
10527 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10528 return NULL;
10529 }
10530 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10531 result = strbuf;
10532 break;
10533
10534 case SPEC_AMATCH: /* match name for autocommand */
10535 result = autocmd_match;
10536 if (result == NULL)
10537 {
10538 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10539 return NULL;
10540 }
10541 break;
10542
10543#endif
10544 case SPEC_SFILE: /* file name for ":so" command */
10545 result = sourcing_name;
10546 if (result == NULL)
10547 {
10548 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10549 return NULL;
10550 }
10551 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010552 case SPEC_SLNUM: /* line in file for ":so" command */
10553 if (sourcing_name == NULL || sourcing_lnum == 0)
10554 {
10555 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10556 return NULL;
10557 }
10558 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10559 result = strbuf;
10560 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561#if defined(FEAT_CLIENTSERVER)
10562 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010563 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10564 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565 result = strbuf;
10566 break;
10567#endif
10568 }
10569
10570 resultlen = (int)STRLEN(result); /* length of new string */
10571 if (src[*usedlen] == '<') /* remove the file name extension */
10572 {
10573 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575 resultlen = (int)(s - result);
10576 }
10577#ifdef FEAT_MODIFY_FNAME
10578 else if (!skip_mod)
10579 {
10580 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10581 &resultlen);
10582 if (result == NULL)
10583 {
10584 *errormsg = (char_u *)"";
10585 return NULL;
10586 }
10587 }
10588#endif
10589 }
10590
10591 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10592 {
10593 if (valid != VALID_HEAD + VALID_PATH)
10594 /* xgettext:no-c-format */
10595 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10596 else
10597 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10598 result = NULL;
10599 }
10600 else
10601 result = vim_strnsave(result, resultlen);
10602 vim_free(resultbuf);
10603 return result;
10604}
10605
10606/*
10607 * Concatenate all files in the argument list, separated by spaces, and return
10608 * it in one allocated string.
10609 * Spaces and backslashes in the file names are escaped with a backslash.
10610 * Returns NULL when out of memory.
10611 */
10612 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010613arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614{
10615 int len;
10616 int idx;
10617 char_u *retval = NULL;
10618 char_u *p;
10619
10620 /*
10621 * Do this loop two times:
10622 * first time: compute the total length
10623 * second time: concatenate the names
10624 */
10625 for (;;)
10626 {
10627 len = 0;
10628 for (idx = 0; idx < ARGCOUNT; ++idx)
10629 {
10630 p = alist_name(&ARGLIST[idx]);
10631 if (p != NULL)
10632 {
10633 if (len > 0)
10634 {
10635 /* insert a space in between names */
10636 if (retval != NULL)
10637 retval[len] = ' ';
10638 ++len;
10639 }
10640 for ( ; *p != NUL; ++p)
10641 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010642 if (*p == ' '
10643#ifndef BACKSLASH_IN_FILENAME
10644 || *p == '\\'
10645#endif
10646 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647 {
10648 /* insert a backslash */
10649 if (retval != NULL)
10650 retval[len] = '\\';
10651 ++len;
10652 }
10653 if (retval != NULL)
10654 retval[len] = *p;
10655 ++len;
10656 }
10657 }
10658 }
10659
10660 /* second time: break here */
10661 if (retval != NULL)
10662 {
10663 retval[len] = NUL;
10664 break;
10665 }
10666
10667 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010668 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669 if (retval == NULL)
10670 break;
10671 }
10672
10673 return retval;
10674}
10675
10676#if defined(FEAT_AUTOCMD) || defined(PROTO)
10677/*
10678 * Expand the <sfile> string in "arg".
10679 *
10680 * Returns an allocated string, or NULL for any error.
10681 */
10682 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010683expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684{
10685 char_u *errormsg;
10686 int len;
10687 char_u *result;
10688 char_u *newres;
10689 char_u *repl;
10690 int srclen;
10691 char_u *p;
10692
10693 result = vim_strsave(arg);
10694 if (result == NULL)
10695 return NULL;
10696
10697 for (p = result; *p; )
10698 {
10699 if (STRNCMP(p, "<sfile>", 7) != 0)
10700 ++p;
10701 else
10702 {
10703 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010704 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705 if (errormsg != NULL)
10706 {
10707 if (*errormsg)
10708 emsg(errormsg);
10709 vim_free(result);
10710 return NULL;
10711 }
10712 if (repl == NULL) /* no match (cannot happen) */
10713 {
10714 p += srclen;
10715 continue;
10716 }
10717 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10718 newres = alloc(len);
10719 if (newres == NULL)
10720 {
10721 vim_free(repl);
10722 vim_free(result);
10723 return NULL;
10724 }
10725 mch_memmove(newres, result, (size_t)(p - result));
10726 STRCPY(newres + (p - result), repl);
10727 len = (int)STRLEN(newres);
10728 STRCAT(newres, p + srclen);
10729 vim_free(repl);
10730 vim_free(result);
10731 result = newres;
10732 p = newres + len; /* continue after the match */
10733 }
10734 }
10735
10736 return result;
10737}
10738#endif
10739
10740#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010010741static int ses_winsizes(FILE *fd, int restore_size,
10742 win_T *tab_firstwin);
10743static int ses_win_rec(FILE *fd, frame_T *fr);
10744static frame_T *ses_skipframe(frame_T *fr);
10745static int ses_do_frame(frame_T *fr);
10746static int ses_do_win(win_T *wp);
10747static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
10748static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
10749static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750
10751/*
10752 * Write openfile commands for the current buffers to an .exrc file.
10753 * Return FAIL on error, OK otherwise.
10754 */
10755 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010756makeopens(
10757 FILE *fd,
10758 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759{
10760 buf_T *buf;
10761 int only_save_windows = TRUE;
10762 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763 int restore_size = TRUE;
10764 win_T *wp;
10765 char_u *sname;
10766 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010767 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010768 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010769 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010770 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010771 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010772 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773
10774 if (ssop_flags & SSOP_BUFFERS)
10775 only_save_windows = FALSE; /* Save ALL buffers */
10776
10777 /*
10778 * Begin by setting the this_session variable, and then other
10779 * sessionable variables.
10780 */
10781#ifdef FEAT_EVAL
10782 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10783 return FAIL;
10784 if (ssop_flags & SSOP_GLOBALS)
10785 if (store_session_globals(fd) == FAIL)
10786 return FAIL;
10787#endif
10788
10789 /*
10790 * Close all windows but one.
10791 */
10792 if (put_line(fd, "silent only") == FAIL)
10793 return FAIL;
10794
10795 /*
10796 * Now a :cd command to the session directory or the current directory
10797 */
10798 if (ssop_flags & SSOP_SESDIR)
10799 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010800 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10801 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802 return FAIL;
10803 }
10804 else if (ssop_flags & SSOP_CURDIR)
10805 {
10806 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10807 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010808 || fputs("cd ", fd) < 0
10809 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10810 || put_eol(fd) == FAIL)
10811 {
10812 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815 vim_free(sname);
10816 }
10817
10818 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010819 * If there is an empty, unnamed buffer we will wipe it out later.
10820 * Remember the buffer number.
10821 */
10822 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10823 return FAIL;
10824 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10825 return FAIL;
10826 if (put_line(fd, "endif") == FAIL)
10827 return FAIL;
10828
10829 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010830 * Now save the current files, current buffer first.
10831 */
10832 if (put_line(fd, "set shortmess=aoO") == FAIL)
10833 return FAIL;
10834
10835 /* Now put the other buffers into the buffer list */
10836 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10837 {
10838 if (!(only_save_windows && buf->b_nwindows == 0)
10839 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10840 && buf->b_fname != NULL
10841 && buf->b_p_bl)
10842 {
10843 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10844 : buf->b_wininfo->wi_fpos.lnum) < 0
10845 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10846 return FAIL;
10847 }
10848 }
10849
10850 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010851 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10853 return FAIL;
10854
10855 if (ssop_flags & SSOP_RESIZE)
10856 {
10857 /* Note: after the restore we still check it worked!*/
10858 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10859 || put_eol(fd) == FAIL)
10860 return FAIL;
10861 }
10862
10863#ifdef FEAT_GUI
10864 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10865 {
10866 int x, y;
10867
10868 if (gui_mch_get_winpos(&x, &y) == OK)
10869 {
10870 /* Note: after the restore we still check it worked!*/
10871 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10872 return FAIL;
10873 }
10874 }
10875#endif
10876
10877 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010878 * When there are two or more tabpages and 'showtabline' is 1 the tabline
10879 * will be displayed when creating the next tab. That resizes the windows
10880 * in the first tab, which may cause problems. Set 'showtabline' to 2
10881 * temporarily to avoid that.
10882 */
10883 if (p_stal == 1 && first_tabpage->tp_next != NULL)
10884 {
10885 if (put_line(fd, "set stal=2") == FAIL)
10886 return FAIL;
10887 restore_stal = TRUE;
10888 }
10889
10890 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010891 * May repeat putting Windows for each tab, when "tabpages" is in
10892 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010893 * Don't use goto_tabpage(), it may change directory and trigger
10894 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010896 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010897 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010898 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020010900 int need_tabnew = FALSE;
10901 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010902
Bram Moolenaar18144c82006-04-12 21:52:12 +000010903 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010905 tabpage_T *tp = find_tabpage(tabnr);
10906
10907 if (tp == NULL)
10908 break; /* done all tab pages */
10909 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010910 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010911 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010912 tab_topframe = topframe;
10913 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010914 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010915 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010916 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010917 tab_topframe = tp->tp_topframe;
10918 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010919 if (tabnr > 1)
10920 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922
Bram Moolenaar18144c82006-04-12 21:52:12 +000010923 /*
10924 * Before creating the window layout, try loading one file. If this
10925 * is aborted we don't end up with a number of useless windows.
10926 * This may have side effects! (e.g., compressed or network file).
10927 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010928 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010929 {
10930 if (ses_do_win(wp)
10931 && wp->w_buffer->b_ffname != NULL
10932 && !wp->w_buffer->b_help
10933#ifdef FEAT_QUICKFIX
10934 && !bt_nofile(wp->w_buffer)
10935#endif
10936 )
10937 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010938 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010939 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10940 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010941 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010942 if (!wp->w_arg_idx_invalid)
10943 edited_win = wp;
10944 break;
10945 }
10946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010948 /* If no file got edited create an empty tab page. */
10949 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10950 return FAIL;
10951
Bram Moolenaar18144c82006-04-12 21:52:12 +000010952 /*
10953 * Save current window layout.
10954 */
10955 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010957 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010959 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10960 return FAIL;
10961 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10962 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963
Bram Moolenaar18144c82006-04-12 21:52:12 +000010964 /*
10965 * Check if window sizes can be restored (no windows omitted).
10966 * Remember the window number of the current window after restoring.
10967 */
10968 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010969 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010970 {
10971 if (ses_do_win(wp))
10972 ++nr;
10973 else
10974 restore_size = FALSE;
10975 if (curwin == wp)
10976 cnr = nr;
10977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978
Bram Moolenaar18144c82006-04-12 21:52:12 +000010979 /* Go to the first window. */
10980 if (put_line(fd, "wincmd t") == FAIL)
10981 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010982
Bram Moolenaar18144c82006-04-12 21:52:12 +000010983 /*
10984 * If more than one window, see if sizes can be restored.
10985 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10986 * resized when moving between windows.
10987 * Do this before restoring the view, so that the topline and the
10988 * cursor can be set. This is done again below.
10989 */
10990 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10991 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010992 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010993 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994
Bram Moolenaar18144c82006-04-12 21:52:12 +000010995 /*
10996 * Restore the view of the window (options, file, cursor, etc.).
10997 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010998 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010999 {
11000 if (!ses_do_win(wp))
11001 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011002 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11003 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011004 return FAIL;
11005 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11006 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011007 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011008 }
11009
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011010 /* The argument index in the first tab page is zero, need to set it in
11011 * each window. For further tab pages it's the window where we do
11012 * "tabedit". */
11013 cur_arg_idx = next_arg_idx;
11014
Bram Moolenaar18144c82006-04-12 21:52:12 +000011015 /*
11016 * Restore cursor to the current window if it's not the first one.
11017 */
11018 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11019 || put_eol(fd) == FAIL))
11020 return FAIL;
11021
11022 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011023 * Restore window sizes again after jumping around in windows, because
11024 * the current window has a minimum size while others may not.
11025 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011026 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011027 return FAIL;
11028
Bram Moolenaar18144c82006-04-12 21:52:12 +000011029 /* Don't continue in another tab page when doing only the current one
11030 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011031 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011032 break;
11033 }
11034
11035 if (ssop_flags & SSOP_TABPAGES)
11036 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011037 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11038 || put_eol(fd) == FAIL)
11039 return FAIL;
11040 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011041 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11042 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011043
Bram Moolenaar9c102382006-05-03 21:26:49 +000011044 /*
11045 * Wipe out an empty unnamed buffer we started in.
11046 */
11047 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11048 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011049 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011050 return FAIL;
11051 if (put_line(fd, "endif") == FAIL)
11052 return FAIL;
11053 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11054 return FAIL;
11055
11056 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11057 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11058 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11059 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060
11061 /*
11062 * Lastly, execute the x.vim file if it exists.
11063 */
11064 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11065 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011066 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067 || put_line(fd, "endif") == FAIL)
11068 return FAIL;
11069
11070 return OK;
11071}
11072
11073 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011074ses_winsizes(
11075 FILE *fd,
11076 int restore_size,
11077 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078{
11079 int n = 0;
11080 win_T *wp;
11081
11082 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11083 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011084 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011085 {
11086 if (!ses_do_win(wp))
11087 continue;
11088 ++n;
11089
11090 /* restore height when not full height */
11091 if (wp->w_height + wp->w_status_height < topframe->fr_height
11092 && (fprintf(fd,
11093 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11094 n, (long)wp->w_height, Rows / 2, Rows) < 0
11095 || put_eol(fd) == FAIL))
11096 return FAIL;
11097
11098 /* restore width when not full width */
11099 if (wp->w_width < Columns && (fprintf(fd,
11100 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11101 n, (long)wp->w_width, Columns / 2, Columns) < 0
11102 || put_eol(fd) == FAIL))
11103 return FAIL;
11104 }
11105 }
11106 else
11107 {
11108 /* Just equalise window sizes */
11109 if (put_line(fd, "wincmd =") == FAIL)
11110 return FAIL;
11111 }
11112 return OK;
11113}
11114
11115/*
11116 * Write commands to "fd" to recursively create windows for frame "fr",
11117 * horizontally and vertically split.
11118 * After the commands the last window in the frame is the current window.
11119 * Returns FAIL when writing the commands to "fd" fails.
11120 */
11121 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011122ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123{
11124 frame_T *frc;
11125 int count = 0;
11126
11127 if (fr->fr_layout != FR_LEAF)
11128 {
11129 /* Find first frame that's not skipped and then create a window for
11130 * each following one (first frame is already there). */
11131 frc = ses_skipframe(fr->fr_child);
11132 if (frc != NULL)
11133 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11134 {
11135 /* Make window as big as possible so that we have lots of room
11136 * to split. */
11137 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11138 || put_line(fd, fr->fr_layout == FR_COL
11139 ? "split" : "vsplit") == FAIL)
11140 return FAIL;
11141 ++count;
11142 }
11143
11144 /* Go back to the first window. */
11145 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11146 ? "%dwincmd k" : "%dwincmd h", count) < 0
11147 || put_eol(fd) == FAIL))
11148 return FAIL;
11149
11150 /* Recursively create frames/windows in each window of this column or
11151 * row. */
11152 frc = ses_skipframe(fr->fr_child);
11153 while (frc != NULL)
11154 {
11155 ses_win_rec(fd, frc);
11156 frc = ses_skipframe(frc->fr_next);
11157 /* Go to next window. */
11158 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11159 return FAIL;
11160 }
11161 }
11162 return OK;
11163}
11164
11165/*
11166 * Skip frames that don't contain windows we want to save in the Session.
11167 * Returns NULL when there none.
11168 */
11169 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011170ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011171{
11172 frame_T *frc;
11173
11174 for (frc = fr; frc != NULL; frc = frc->fr_next)
11175 if (ses_do_frame(frc))
11176 break;
11177 return frc;
11178}
11179
11180/*
11181 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11182 * the Session.
11183 */
11184 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011185ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186{
11187 frame_T *frc;
11188
11189 if (fr->fr_layout == FR_LEAF)
11190 return ses_do_win(fr->fr_win);
11191 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11192 if (ses_do_frame(frc))
11193 return TRUE;
11194 return FALSE;
11195}
11196
11197/*
11198 * Return non-zero if window "wp" is to be stored in the Session.
11199 */
11200 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011201ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202{
11203 if (wp->w_buffer->b_fname == NULL
11204#ifdef FEAT_QUICKFIX
11205 /* When 'buftype' is "nofile" can't restore the window contents. */
11206 || bt_nofile(wp->w_buffer)
11207#endif
11208 )
11209 return (ssop_flags & SSOP_BLANK);
11210 if (wp->w_buffer->b_help)
11211 return (ssop_flags & SSOP_HELP);
11212 return TRUE;
11213}
11214
11215/*
11216 * Write commands to "fd" to restore the view of a window.
11217 * Caller must make sure 'scrolloff' is zero.
11218 */
11219 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011220put_view(
11221 FILE *fd,
11222 win_T *wp,
11223 int add_edit, /* add ":edit" command to view */
11224 unsigned *flagp, /* vop_flags or ssop_flags */
11225 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011226 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011227{
11228 win_T *save_curwin;
11229 int f;
11230 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011231 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011232
11233 /* Always restore cursor position for ":mksession". For ":mkview" only
11234 * when 'viewoptions' contains "cursor". */
11235 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11236
11237 /*
11238 * Local argument list.
11239 */
11240 if (wp->w_alist == &global_alist)
11241 {
11242 if (put_line(fd, "argglobal") == FAIL)
11243 return FAIL;
11244 }
11245 else
11246 {
11247 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11248 flagp == &vop_flags
11249 || !(*flagp & SSOP_CURDIR)
11250 || wp->w_localdir != NULL, flagp) == FAIL)
11251 return FAIL;
11252 }
11253
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011254 /* Only when part of a session: restore the argument index. Some
11255 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011256 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011257 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011259 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260 || put_eol(fd) == FAIL)
11261 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011262 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263 }
11264
11265 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011266 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267 {
11268 /*
11269 * Load the file.
11270 */
11271 if (wp->w_buffer->b_ffname != NULL
11272#ifdef FEAT_QUICKFIX
11273 && !bt_nofile(wp->w_buffer)
11274#endif
11275 )
11276 {
11277 /*
11278 * Editing a file in this buffer: use ":edit file".
11279 * This may have side effects! (e.g., compressed or network file).
11280 */
11281 if (fputs("edit ", fd) < 0
11282 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11283 return FAIL;
11284 }
11285 else
11286 {
11287 /* No file in this buffer, just make it empty. */
11288 if (put_line(fd, "enew") == FAIL)
11289 return FAIL;
11290#ifdef FEAT_QUICKFIX
11291 if (wp->w_buffer->b_ffname != NULL)
11292 {
11293 /* The buffer does have a name, but it's not a file name. */
11294 if (fputs("file ", fd) < 0
11295 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11296 return FAIL;
11297 }
11298#endif
11299 do_cursor = FALSE;
11300 }
11301 }
11302
11303 /*
11304 * Local mappings and abbreviations.
11305 */
11306 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11307 && makemap(fd, wp->w_buffer) == FAIL)
11308 return FAIL;
11309
11310 /*
11311 * Local options. Need to go to the window temporarily.
11312 * Store only local values when using ":mkview" and when ":mksession" is
11313 * used and 'sessionoptions' doesn't include "options".
11314 * Some folding options are always stored when "folds" is included,
11315 * otherwise the folds would not be restored correctly.
11316 */
11317 save_curwin = curwin;
11318 curwin = wp;
11319 curbuf = curwin->w_buffer;
11320 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11321 f = makeset(fd, OPT_LOCAL,
11322 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11323#ifdef FEAT_FOLDING
11324 else if (*flagp & SSOP_FOLDS)
11325 f = makefoldset(fd);
11326#endif
11327 else
11328 f = OK;
11329 curwin = save_curwin;
11330 curbuf = curwin->w_buffer;
11331 if (f == FAIL)
11332 return FAIL;
11333
11334#ifdef FEAT_FOLDING
11335 /*
11336 * Save Folds when 'buftype' is empty and for help files.
11337 */
11338 if ((*flagp & SSOP_FOLDS)
11339 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011340# ifdef FEAT_QUICKFIX
11341 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11342# endif
11343 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344 {
11345 if (put_folds(fd, wp) == FAIL)
11346 return FAIL;
11347 }
11348#endif
11349
11350 /*
11351 * Set the cursor after creating folds, since that moves the cursor.
11352 */
11353 if (do_cursor)
11354 {
11355
11356 /* Restore the cursor line in the file and relatively in the
11357 * window. Don't use "G", it changes the jumplist. */
11358 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11359 (long)wp->w_cursor.lnum,
11360 (long)(wp->w_cursor.lnum - wp->w_topline),
11361 (long)wp->w_height / 2, (long)wp->w_height) < 0
11362 || put_eol(fd) == FAIL
11363 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11364 || put_line(fd, "exe s:l") == FAIL
11365 || put_line(fd, "normal! zt") == FAIL
11366 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11367 || put_eol(fd) == FAIL)
11368 return FAIL;
11369 /* Restore the cursor column and left offset when not wrapping. */
11370 if (wp->w_cursor.col == 0)
11371 {
11372 if (put_line(fd, "normal! 0") == FAIL)
11373 return FAIL;
11374 }
11375 else
11376 {
11377 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11378 {
11379 if (fprintf(fd,
11380 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011381 (long)wp->w_virtcol + 1,
11382 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383 (long)wp->w_width / 2, (long)wp->w_width) < 0
11384 || put_eol(fd) == FAIL
11385 || put_line(fd, "if s:c > 0") == FAIL
11386 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011387 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11388 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389 || put_eol(fd) == FAIL
11390 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011391 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392 || put_eol(fd) == FAIL
11393 || put_line(fd, "endif") == FAIL)
11394 return FAIL;
11395 }
11396 else
11397 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011398 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399 || put_eol(fd) == FAIL)
11400 return FAIL;
11401 }
11402 }
11403 }
11404
11405 /*
11406 * Local directory.
11407 */
11408 if (wp->w_localdir != NULL)
11409 {
11410 if (fputs("lcd ", fd) < 0
11411 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11412 || put_eol(fd) == FAIL)
11413 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011414 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415 }
11416
11417 return OK;
11418}
11419
11420/*
11421 * Write an argument list to the session file.
11422 * Returns FAIL if writing fails.
11423 */
11424 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011425ses_arglist(
11426 FILE *fd,
11427 char *cmd,
11428 garray_T *gap,
11429 int fullname, /* TRUE: use full path name */
11430 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011431{
11432 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011433 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434 char_u *s;
11435
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011436 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11437 return FAIL;
11438 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439 return FAIL;
11440 for (i = 0; i < gap->ga_len; ++i)
11441 {
11442 /* NULL file names are skipped (only happens when out of memory). */
11443 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11444 if (s != NULL)
11445 {
11446 if (fullname)
11447 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011448 buf = alloc(MAXPATHL);
11449 if (buf != NULL)
11450 {
11451 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11452 s = buf;
11453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011455 if (fputs("argadd ", fd) < 0
11456 || ses_put_fname(fd, s, flagp) == FAIL
11457 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011458 {
11459 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011461 }
11462 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463 }
11464 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011465 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466}
11467
11468/*
11469 * Write a buffer name to the session file.
11470 * Also ends the line.
11471 * Returns FAIL if writing fails.
11472 */
11473 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011474ses_fname(FILE *fd, buf_T *buf, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475{
11476 char_u *name;
11477
11478 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011479 * the session file will be sourced.
11480 * Don't do this for ":mkview", we don't know the current directory.
11481 * Don't do this after ":lcd", we don't keep track of what the current
11482 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011483 if (buf->b_sfname != NULL
11484 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011485 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011486#ifdef FEAT_AUTOCHDIR
11487 && !p_acd
11488#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011489 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011490 name = buf->b_sfname;
11491 else
11492 name = buf->b_ffname;
11493 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11494 return FAIL;
11495 return OK;
11496}
11497
11498/*
11499 * Write a file name to the session file.
11500 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11501 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011502 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503 */
11504 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011505ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011506{
11507 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011508 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011509 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011510
11511 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011512 if (sname == NULL)
11513 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011514
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011515 if (*flagp & SSOP_SLASH)
11516 {
11517 /* change all backslashes to forward slashes */
11518 for (p = sname; *p != NUL; mb_ptr_adv(p))
11519 if (*p == '\\')
11520 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011522
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011523 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011524 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011525 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011526 if (p == NULL)
11527 return FAIL;
11528
11529 /* write the result */
11530 if (fputs((char *)p, fd) < 0)
11531 retval = FAIL;
11532
11533 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011534 return retval;
11535}
11536
11537/*
11538 * ":loadview [nr]"
11539 */
11540 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011541ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542{
11543 char_u *fname;
11544
11545 fname = get_view_file(*eap->arg);
11546 if (fname != NULL)
11547 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011548 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549 vim_free(fname);
11550 }
11551}
11552
11553/*
11554 * Get the name of the view file for the current buffer.
11555 */
11556 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011557get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011558{
11559 int len = 0;
11560 char_u *p, *s;
11561 char_u *retval;
11562 char_u *sname;
11563
11564 if (curbuf->b_ffname == NULL)
11565 {
11566 EMSG(_(e_noname));
11567 return NULL;
11568 }
11569 sname = home_replace_save(NULL, curbuf->b_ffname);
11570 if (sname == NULL)
11571 return NULL;
11572
11573 /*
11574 * We want a file name without separators, because we're not going to make
11575 * a directory.
11576 * "normal" path separator -> "=+"
11577 * "=" -> "=="
11578 * ":" path separator -> "=-"
11579 */
11580 for (p = sname; *p; ++p)
11581 if (*p == '=' || vim_ispathsep(*p))
11582 ++len;
11583 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11584 if (retval != NULL)
11585 {
11586 STRCPY(retval, p_vdir);
11587 add_pathsep(retval);
11588 s = retval + STRLEN(retval);
11589 for (p = sname; *p; ++p)
11590 {
11591 if (*p == '=')
11592 {
11593 *s++ = '=';
11594 *s++ = '=';
11595 }
11596 else if (vim_ispathsep(*p))
11597 {
11598 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011599#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600 if (*p == ':')
11601 *s++ = '-';
11602 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011604 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011605 }
11606 else
11607 *s++ = *p;
11608 }
11609 *s++ = '=';
11610 *s++ = c;
11611 STRCPY(s, ".vim");
11612 }
11613
11614 vim_free(sname);
11615 return retval;
11616}
11617
11618#endif /* FEAT_SESSION */
11619
11620/*
11621 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11622 * Return FAIL for a write error.
11623 */
11624 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011625put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011626{
11627 if (
11628#ifdef USE_CRNL
11629 (
11630# ifdef MKSESSION_NL
11631 !mksession_nl &&
11632# endif
11633 (putc('\r', fd) < 0)) ||
11634#endif
11635 (putc('\n', fd) < 0))
11636 return FAIL;
11637 return OK;
11638}
11639
11640/*
11641 * Write a line to "fd".
11642 * Return FAIL for a write error.
11643 */
11644 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011645put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011646{
11647 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11648 return FAIL;
11649 return OK;
11650}
11651
11652#ifdef FEAT_VIMINFO
11653/*
11654 * ":rviminfo" and ":wviminfo".
11655 */
11656 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011657ex_viminfo(
11658 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011659{
11660 char_u *save_viminfo;
11661
11662 save_viminfo = p_viminfo;
11663 if (*p_viminfo == NUL)
11664 p_viminfo = (char_u *)"'100";
11665 if (eap->cmdidx == CMD_rviminfo)
11666 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011667 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11668 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011669 EMSG(_("E195: Cannot open viminfo file for reading"));
11670 }
11671 else
11672 write_viminfo(eap->arg, eap->forceit);
11673 p_viminfo = save_viminfo;
11674}
11675#endif
11676
11677#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011678/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011679 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011680 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011681 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011682 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011683dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011684{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011685 if (fname == NULL)
11686 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011687 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688}
11689#endif
11690
11691/*
11692 * ":behave {mswin,xterm}"
11693 */
11694 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011695ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696{
11697 if (STRCMP(eap->arg, "mswin") == 0)
11698 {
11699 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11700 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11701 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11702 set_option_value((char_u *)"keymodel", 0L,
11703 (char_u *)"startsel,stopsel", 0);
11704 }
11705 else if (STRCMP(eap->arg, "xterm") == 0)
11706 {
11707 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11708 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11709 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11710 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11711 }
11712 else
11713 EMSG2(_(e_invarg2), eap->arg);
11714}
11715
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011716#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11717/*
11718 * Function given to ExpandGeneric() to obtain the possible arguments of the
11719 * ":behave {mswin,xterm}" command.
11720 */
11721 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011722get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011723{
11724 if (idx == 0)
11725 return (char_u *)"mswin";
11726 if (idx == 1)
11727 return (char_u *)"xterm";
11728 return NULL;
11729}
11730#endif
11731
Bram Moolenaar071d4272004-06-13 20:20:40 +000011732#ifdef FEAT_AUTOCMD
11733static int filetype_detect = FALSE;
11734static int filetype_plugin = FALSE;
11735static int filetype_indent = FALSE;
11736
11737/*
11738 * ":filetype [plugin] [indent] {on,off,detect}"
11739 * on: Load the filetype.vim file to install autocommands for file types.
11740 * off: Load the ftoff.vim file to remove all autocommands for file types.
11741 * plugin on: load filetype.vim and ftplugin.vim
11742 * plugin off: load ftplugof.vim
11743 * indent on: load filetype.vim and indent.vim
11744 * indent off: load indoff.vim
11745 */
11746 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011747ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011748{
11749 char_u *arg = eap->arg;
11750 int plugin = FALSE;
11751 int indent = FALSE;
11752
11753 if (*eap->arg == NUL)
11754 {
11755 /* Print current status. */
11756 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11757 filetype_detect ? "ON" : "OFF",
11758 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11759 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11760 return;
11761 }
11762
11763 /* Accept "plugin" and "indent" in any order. */
11764 for (;;)
11765 {
11766 if (STRNCMP(arg, "plugin", 6) == 0)
11767 {
11768 plugin = TRUE;
11769 arg = skipwhite(arg + 6);
11770 continue;
11771 }
11772 if (STRNCMP(arg, "indent", 6) == 0)
11773 {
11774 indent = TRUE;
11775 arg = skipwhite(arg + 6);
11776 continue;
11777 }
11778 break;
11779 }
11780 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11781 {
11782 if (*arg == 'o' || !filetype_detect)
11783 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011784 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011785 filetype_detect = TRUE;
11786 if (plugin)
11787 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011788 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011789 filetype_plugin = TRUE;
11790 }
11791 if (indent)
11792 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011793 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011794 filetype_indent = TRUE;
11795 }
11796 }
11797 if (*arg == 'd')
11798 {
11799 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011800 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011801 }
11802 }
11803 else if (STRCMP(arg, "off") == 0)
11804 {
11805 if (plugin || indent)
11806 {
11807 if (plugin)
11808 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011809 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810 filetype_plugin = FALSE;
11811 }
11812 if (indent)
11813 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011814 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011815 filetype_indent = FALSE;
11816 }
11817 }
11818 else
11819 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011820 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821 filetype_detect = FALSE;
11822 }
11823 }
11824 else
11825 EMSG2(_(e_invarg2), arg);
11826}
11827
11828/*
11829 * ":setfiletype {name}"
11830 */
11831 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011832ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011833{
11834 if (!did_filetype)
11835 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11836}
11837#endif
11838
Bram Moolenaar071d4272004-06-13 20:20:40 +000011839 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011840ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841{
11842#ifdef FEAT_DIGRAPHS
11843 if (*eap->arg != NUL)
11844 putdigraph(eap->arg);
11845 else
11846 listdigraphs();
11847#else
11848 EMSG(_("E196: No digraphs in this version"));
11849#endif
11850}
11851
11852 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011853ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854{
11855 int flags = 0;
11856
11857 if (eap->cmdidx == CMD_setlocal)
11858 flags = OPT_LOCAL;
11859 else if (eap->cmdidx == CMD_setglobal)
11860 flags = OPT_GLOBAL;
11861#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11862 if (cmdmod.browse && flags == 0)
11863 ex_options(eap);
11864 else
11865#endif
11866 (void)do_set(eap->arg, flags);
11867}
11868
11869#ifdef FEAT_SEARCH_EXTRA
11870/*
11871 * ":nohlsearch"
11872 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011874ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011875{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010011876 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011877 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011878}
11879
11880/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011881 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011882 * Sets nextcmd to the start of the next command, if any. Also called when
11883 * skipping commands to find the next command.
11884 */
11885 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011886ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011887{
11888 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011889 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011890 char_u *end;
11891 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011892 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011893
11894 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011895 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011896 else
11897 {
11898 EMSG(e_invcmd);
11899 return;
11900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011901
11902 /* First clear any old pattern. */
11903 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011904 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905
11906 if (ends_excmd(*eap->arg))
11907 end = eap->arg;
11908 else if ((STRNICMP(eap->arg, "none", 4) == 0
11909 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11910 end = eap->arg + 4;
11911 else
11912 {
11913 p = skiptowhite(eap->arg);
11914 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011915 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011916 p = skipwhite(p);
11917 if (*p == NUL)
11918 {
11919 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011920 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921 EMSG2(_(e_invarg2), eap->arg);
11922 return;
11923 }
11924 end = skip_regexp(p + 1, *p, TRUE, NULL);
11925 if (!eap->skip)
11926 {
11927 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11928 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011929 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011930 eap->errmsg = e_trailing;
11931 return;
11932 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011933 if (*end != *p)
11934 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011935 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011936 EMSG2(_(e_invarg2), p);
11937 return;
11938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011939
11940 c = *end;
11941 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020011942 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011943 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011944 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011945 }
11946 }
11947 eap->nextcmd = find_nextcmd(end);
11948}
11949#endif
11950
11951#ifdef FEAT_CRYPT
11952/*
11953 * ":X": Get crypt key
11954 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011955 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011956ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011957{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010011958 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020011959 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011960}
11961#endif
11962
11963#ifdef FEAT_FOLDING
11964 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011965ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011966{
11967 if (foldManualAllowed(TRUE))
11968 foldCreate(eap->line1, eap->line2);
11969}
11970
11971 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011972ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011973{
11974 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11975 eap->forceit, FALSE);
11976}
11977
11978 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011979ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011980{
11981 linenr_T lnum;
11982
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011983#ifdef FEAT_CLIPBOARD
11984 start_global_changes();
11985#endif
11986
Bram Moolenaar071d4272004-06-13 20:20:40 +000011987 /* First set the marks for all lines closed/open. */
11988 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11989 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11990 ml_setmarked(lnum);
11991
11992 /* Execute the command on the marked lines. */
11993 global_exe(eap->arg);
11994 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011995#ifdef FEAT_CLIPBOARD
11996 end_global_changes();
11997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011998}
11999#endif