blob: 4aa7f5df736638c6b76c4255b64e1b87fae95e15 [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
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299#ifndef FEAT_KEYMAP
300# define ex_loadkeymap ex_ni
301#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100302static void ex_swapname(exarg_T *eap);
303static void ex_syncbind(exarg_T *eap);
304static void ex_read(exarg_T *eap);
305static void ex_pwd(exarg_T *eap);
306static void ex_equal(exarg_T *eap);
307static void ex_sleep(exarg_T *eap);
308static void do_exmap(exarg_T *eap, int isabbrev);
309static void ex_winsize(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100311static void ex_wincmd(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312#else
313# define ex_wincmd ex_ni
314#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000315#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100316static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317#else
318# define ex_winpos ex_ni
319#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100320static void ex_operators(exarg_T *eap);
321static void ex_put(exarg_T *eap);
322static void ex_copymove(exarg_T *eap);
323static void ex_submagic(exarg_T *eap);
324static void ex_join(exarg_T *eap);
325static void ex_at(exarg_T *eap);
326static void ex_bang(exarg_T *eap);
327static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200328#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100329static void ex_wundo(exarg_T *eap);
330static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200331#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100332static void ex_redo(exarg_T *eap);
333static void ex_later(exarg_T *eap);
334static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100335static void ex_redrawstatus(exarg_T *eap);
336static void close_redir(void);
337static void ex_mkrc(exarg_T *eap);
338static void ex_mark(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339#ifdef FEAT_USR_CMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100340static char_u *uc_fun_cmd(void);
341static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100343static void ex_startinsert(exarg_T *eap);
344static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100346static void ex_checkpath(exarg_T *eap);
347static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348#else
349# define ex_findpat ex_ni
350# define ex_checkpath ex_ni
351#endif
352#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100353static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354#else
355# define ex_psearch ex_ni
356#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100357static void ex_tag(exarg_T *eap);
358static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359#ifndef FEAT_EVAL
360# define ex_scriptnames ex_ni
361# define ex_finish ex_ni
362# define ex_echo ex_ni
363# define ex_echohl ex_ni
364# define ex_execute ex_ni
365# define ex_call ex_ni
366# define ex_if ex_ni
367# define ex_endif ex_ni
368# define ex_else ex_ni
369# define ex_while ex_ni
370# define ex_continue ex_ni
371# define ex_break ex_ni
372# define ex_endwhile ex_ni
373# define ex_throw ex_ni
374# define ex_try ex_ni
375# define ex_catch ex_ni
376# define ex_finally ex_ni
377# define ex_endtry ex_ni
378# define ex_endfunction ex_ni
379# define ex_let ex_ni
380# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000381# define ex_lockvar ex_ni
382# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383# define ex_function ex_ni
384# define ex_delfunction ex_ni
385# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000386# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100388static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100390static int makeopens(FILE *fd, char_u *dirnow);
391static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
392static void ex_loadview(exarg_T *eap);
393static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000394static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395#else
396# define ex_loadview ex_ni
397#endif
398#ifndef FEAT_EVAL
399# define ex_compiler ex_ni
400#endif
401#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100402static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403#else
404# define ex_viminfo ex_ni
405#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100406static void ex_behave(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100408static void ex_filetype(exarg_T *eap);
409static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410#else
411# define ex_filetype ex_ni
412# define ex_setfiletype ex_ni
413#endif
414#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000415# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416# define ex_diffpatch ex_ni
417# define ex_diffgetput ex_ni
418# define ex_diffsplit ex_ni
419# define ex_diffthis ex_ni
420# define ex_diffupdate ex_ni
421#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100422static void ex_digraphs(exarg_T *eap);
423static void ex_set(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
425# define ex_options ex_ni
426#endif
427#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100428static void ex_nohlsearch(exarg_T *eap);
429static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430#else
431# define ex_nohlsearch ex_ni
432# define ex_match ex_ni
433#endif
434#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100435static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436#else
437# define ex_X ex_ni
438#endif
439#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100440static void ex_fold(exarg_T *eap);
441static void ex_foldopen(exarg_T *eap);
442static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443#else
444# define ex_fold ex_ni
445# define ex_foldopen ex_ni
446# define ex_folddo ex_ni
447#endif
448#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
449 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
450# define ex_language ex_ni
451#endif
452#ifndef FEAT_SIGNS
453# define ex_sign ex_ni
454#endif
455#ifndef FEAT_SUN_WORKSHOP
456# define ex_wsverb ex_ni
457#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000458#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200459# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000460# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200461# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463
464#ifndef FEAT_EVAL
465# define ex_debug ex_ni
466# define ex_breakadd ex_ni
467# define ex_debuggreedy ex_ni
468# define ex_breakdel ex_ni
469# define ex_breaklist ex_ni
470#endif
471
472#ifndef FEAT_CMDHIST
473# define ex_history ex_ni
474#endif
475#ifndef FEAT_JUMPLIST
476# define ex_jumps ex_ni
477# define ex_changes ex_ni
478#endif
479
Bram Moolenaar05159a02005-02-26 23:04:13 +0000480#ifndef FEAT_PROFILE
481# define ex_profile ex_ni
482#endif
483
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484/*
485 * Declare cmdnames[].
486 */
487#define DO_DECLARE_EXCMD
488#include "ex_cmds.h"
489
490/*
491 * Table used to quickly search for a command, based on its first character.
492 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000493static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494{
495 CMD_append,
496 CMD_buffer,
497 CMD_change,
498 CMD_delete,
499 CMD_edit,
500 CMD_file,
501 CMD_global,
502 CMD_help,
503 CMD_insert,
504 CMD_join,
505 CMD_k,
506 CMD_list,
507 CMD_move,
508 CMD_next,
509 CMD_open,
510 CMD_print,
511 CMD_quit,
512 CMD_read,
513 CMD_substitute,
514 CMD_t,
515 CMD_undo,
516 CMD_vglobal,
517 CMD_write,
518 CMD_xit,
519 CMD_yank,
520 CMD_z,
521 CMD_bang
522};
523
524static char_u dollar_command[2] = {'$', 0};
525
526
527#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000528/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529typedef struct
530{
531 char_u *line; /* command line */
532 linenr_T lnum; /* sourcing_lnum of the line */
533} wcmd_T;
534
535/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000536 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000538 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000540struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541{
542 garray_T *lines_gap; /* growarray with line info */
543 int current_line; /* last read line from growarray */
544 int repeating; /* TRUE when looping a second time */
545 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100546 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 void *cookie;
548};
549
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100550static char_u *get_loop_line(int c, void *cookie, int indent);
551static int store_loop_line(garray_T *gap, char_u *line);
552static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000553
554/* Struct to save a few things while debugging. Used in do_cmdline() only. */
555struct dbg_stuff
556{
557 int trylevel;
558 int force_abort;
559 except_T *caught_stack;
560 char_u *vv_exception;
561 char_u *vv_throwpoint;
562 int did_emsg;
563 int got_int;
564 int did_throw;
565 int need_rethrow;
566 int check_cstack;
567 except_T *current_exception;
568};
569
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100570static void save_dbg_stuff(struct dbg_stuff *dsp);
571static void restore_dbg_stuff(struct dbg_stuff *dsp);
Bram Moolenaared203462004-06-16 11:19:22 +0000572
573 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100574save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000575{
576 dsp->trylevel = trylevel; trylevel = 0;
577 dsp->force_abort = force_abort; force_abort = FALSE;
578 dsp->caught_stack = caught_stack; caught_stack = NULL;
579 dsp->vv_exception = v_exception(NULL);
580 dsp->vv_throwpoint = v_throwpoint(NULL);
581
582 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
583 dsp->did_emsg = did_emsg; did_emsg = FALSE;
584 dsp->got_int = got_int; got_int = FALSE;
585 dsp->did_throw = did_throw; did_throw = FALSE;
586 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
587 dsp->check_cstack = check_cstack; check_cstack = FALSE;
588 dsp->current_exception = current_exception; current_exception = NULL;
589}
590
591 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100592restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000593{
594 suppress_errthrow = FALSE;
595 trylevel = dsp->trylevel;
596 force_abort = dsp->force_abort;
597 caught_stack = dsp->caught_stack;
598 (void)v_exception(dsp->vv_exception);
599 (void)v_throwpoint(dsp->vv_throwpoint);
600 did_emsg = dsp->did_emsg;
601 got_int = dsp->got_int;
602 did_throw = dsp->did_throw;
603 need_rethrow = dsp->need_rethrow;
604 check_cstack = dsp->check_cstack;
605 current_exception = dsp->current_exception;
606}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607#endif
608
609
610/*
611 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
612 * command is given.
613 */
614 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100615do_exmode(
616 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617{
618 int save_msg_scroll;
619 int prev_msg_row;
620 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000621 int changedtick;
622
623 if (improved)
624 exmode_active = EXMODE_VIM;
625 else
626 exmode_active = EXMODE_NORMAL;
627 State = NORMAL;
628
629 /* When using ":global /pat/ visual" and then "Q" we return to continue
630 * the :global command. */
631 if (global_busy)
632 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633
634 save_msg_scroll = msg_scroll;
635 ++RedrawingDisabled; /* don't redisplay the window */
636 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637#ifdef FEAT_GUI
638 /* Ignore scrollbar and mouse events in Ex mode */
639 ++hold_gui_events;
640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641
642 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
643 while (exmode_active)
644 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000645 /* Check for a ":normal" command and no more characters left. */
646 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
647 {
648 exmode_active = FALSE;
649 break;
650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 msg_scroll = TRUE;
652 need_wait_return = FALSE;
653 ex_pressedreturn = FALSE;
654 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000655 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 prev_msg_row = msg_row;
657 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 if (improved)
659 {
660 cmdline_row = msg_row;
661 do_cmdline(NULL, getexline, NULL, 0);
662 }
663 else
664 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
665 lines_left = Rows - 1;
666
Bram Moolenaardf177f62005-02-22 08:39:57 +0000667 if ((prev_line != curwin->w_cursor.lnum
668 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000670 if (curbuf->b_ml.ml_flags & ML_EMPTY)
671 EMSG(_(e_emptybuf));
672 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000674 if (ex_pressedreturn)
675 {
676 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000677 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000678 msg_row = prev_msg_row;
679 if (prev_msg_row == Rows - 1)
680 msg_row--;
681 }
682 msg_col = 0;
683 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
684 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000687 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
688 {
689 if (curbuf->b_ml.ml_flags & ML_EMPTY)
690 EMSG(_(e_emptybuf));
691 else
692 EMSG(_("E501: At end-of-file"));
693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 }
695
696#ifdef FEAT_GUI
697 --hold_gui_events;
698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 --RedrawingDisabled;
700 --no_wait_return;
701 update_screen(CLEAR);
702 need_wait_return = FALSE;
703 msg_scroll = save_msg_scroll;
704}
705
706/*
707 * Execute a simple command line. Used for translated commands like "*".
708 */
709 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100710do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711{
712 return do_cmdline(cmd, NULL, NULL,
713 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
714}
715
716/*
717 * do_cmdline(): execute one Ex command line
718 *
719 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100720 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 * 2. Split up in parts separated with '|'.
722 *
723 * This function can be called recursively!
724 *
725 * flags:
726 * DOCMD_VERBOSE - The command will be included in the error message.
727 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100728 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 * DOCMD_KEYTYPED - Don't reset KeyTyped.
730 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
731 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
732 *
733 * return FAIL if cmdline could not be executed, OK otherwise
734 */
735 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100736do_cmdline(
737 char_u *cmdline,
738 char_u *(*fgetline)(int, void *, int),
739 void *cookie, /* argument for fgetline() */
740 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741{
742 char_u *next_cmdline; /* next cmd to execute */
743 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100744 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 static int recursive = 0; /* recursive depth */
746 int msg_didout_before_start = 0;
747 int count = 0; /* line number count */
748 int did_inc = FALSE; /* incremented RedrawingDisabled */
749 int retval = OK;
750#ifdef FEAT_EVAL
751 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000752 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 int current_line = 0; /* active line in lines_ga */
754 char_u *fname = NULL; /* function or script name */
755 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
756 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000757 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 int initial_trylevel;
759 struct msglist **saved_msg_list = NULL;
760 struct msglist *private_msg_list;
761
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100762 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100763 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000765 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000767 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100769# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770# define cmd_cookie cookie
771#endif
772 static int call_depth = 0; /* recursiveness */
773
774#ifdef FEAT_EVAL
775 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
776 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000777 * This ensures that the do_errthrow() call in do_one_cmd() does not
778 * combine the messages stored by an earlier invocation of do_one_cmd()
779 * with the command name of the later one. This would happen when
780 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 saved_msg_list = msg_list;
782 msg_list = &private_msg_list;
783 private_msg_list = NULL;
784#endif
785
786 /* It's possible to create an endless loop with ":execute", catch that
787 * here. The value of 200 allows nested function calls, ":source", etc. */
788 if (call_depth == 200)
789 {
790 EMSG(_("E169: Command too recursive"));
791#ifdef FEAT_EVAL
792 /* When converting to an exception, we do not include the command name
793 * since this is not an error of the specific command. */
794 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
795 msg_list = saved_msg_list;
796#endif
797 return FAIL;
798 }
799 ++call_depth;
800
801#ifdef FEAT_EVAL
802 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000803 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 cstack.cs_trylevel = 0;
805 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000806 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
808
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100809 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810
811 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100812 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000813 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 ++ex_nesting_level;
815
816 /* Get the function or script name and the address where the next breakpoint
817 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000818 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 {
820 fname = func_name(real_cookie);
821 breakpoint = func_breakpoint(real_cookie);
822 dbg_tick = func_dbg_tick(real_cookie);
823 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100824 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 {
826 fname = sourcing_name;
827 breakpoint = source_breakpoint(real_cookie);
828 dbg_tick = source_dbg_tick(real_cookie);
829 }
830
831 /*
832 * Initialize "force_abort" and "suppress_errthrow" at the top level.
833 */
834 if (!recursive)
835 {
836 force_abort = FALSE;
837 suppress_errthrow = FALSE;
838 }
839
840 /*
841 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000842 * exception handling (used when debugging). Otherwise clear it to avoid
843 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000845 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000846 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000847 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200848 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849
850 initial_trylevel = trylevel;
851
852 /*
853 * "did_throw" will be set to TRUE when an exception is being thrown.
854 */
855 did_throw = FALSE;
856#endif
857 /*
858 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000859 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 * If force_abort is set, we cancel everything.
861 */
862 did_emsg = FALSE;
863
864 /*
865 * KeyTyped is only set when calling vgetc(). Reset it here when not
866 * calling vgetc() (sourced command lines).
867 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100868 if (!(flags & DOCMD_KEYTYPED)
869 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 KeyTyped = FALSE;
871
872 /*
873 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000874 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 * - for multiple commands on one line, separated with '|'
876 * - when repeating until there are no more lines (for ":source")
877 */
878 next_cmdline = cmdline;
879 do
880 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000881#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100882 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000883#endif
884
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000885 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 if (next_cmdline == NULL
887#ifdef FEAT_EVAL
888 && !force_abort
889 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000890 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891#endif
892 )
893 did_emsg = FALSE;
894
895 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000896 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100897 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 * 3. If a line is given: Make a copy, so we can mess with it.
899 */
900
901#ifdef FEAT_EVAL
902 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000903 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 {
905 /* Each '|' separated command is stored separately in lines_ga, to
906 * be able to jump to it. Don't use next_cmdline now. */
907 vim_free(cmdline_copy);
908 cmdline_copy = NULL;
909
910 /* Check if a function has returned or, unless it has an unclosed
911 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000912 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000914# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000915 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000916 func_line_end(real_cookie);
917# endif
918 if (func_has_ended(real_cookie))
919 {
920 retval = FAIL;
921 break;
922 }
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 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100926 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000927 script_line_end();
928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929
930 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100931 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 {
933 retval = FAIL;
934 break;
935 }
936
937 /* If breakpoints have been added/deleted need to check for it. */
938 if (breakpoint != NULL && dbg_tick != NULL
939 && *dbg_tick != debug_tick)
940 {
941 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100942 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 fname, sourcing_lnum);
944 *dbg_tick = debug_tick;
945 }
946
947 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
948 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
949
950 /* Did we encounter a breakpoint? */
951 if (breakpoint != NULL && *breakpoint != 0
952 && *breakpoint <= sourcing_lnum)
953 {
954 dbg_breakpoint(fname, sourcing_lnum);
955 /* Find next breakpoint. */
956 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100957 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 fname, sourcing_lnum);
959 *dbg_tick = debug_tick;
960 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000961# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000962 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000963 {
964 if (getline_is_func)
965 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100966 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000967 script_line_start();
968 }
969# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 }
971
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000972 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000974 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100975 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 * below, so that it stores lines in or reads them from
977 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000978 * while/for loop. */
979 cmd_getline = get_loop_line;
980 cmd_cookie = (void *)&cmd_loop_cookie;
981 cmd_loop_cookie.lines_gap = &lines_ga;
982 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100983 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000984 cmd_loop_cookie.cookie = cookie;
985 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 }
987 else
988 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100989 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 cmd_cookie = cookie;
991 }
992#endif
993
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100994 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 if (next_cmdline == NULL)
996 {
997 /*
998 * Need to set msg_didout for the first line after an ":if",
999 * otherwise the ":if" will be overwritten.
1000 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001001 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001003 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004#ifdef FEAT_EVAL
1005 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1006#else
1007 0
1008#endif
1009 )) == NULL)
1010 {
1011 /* Don't call wait_return for aborted command line. The NULL
1012 * returned for the end of a sourced file or executed function
1013 * doesn't do this. */
1014 if (KeyTyped && !(flags & DOCMD_REPEAT))
1015 need_wait_return = FALSE;
1016 retval = FAIL;
1017 break;
1018 }
1019 used_getline = TRUE;
1020
1021 /*
1022 * Keep the first typed line. Clear it when more lines are typed.
1023 */
1024 if (flags & DOCMD_KEEPLINE)
1025 {
1026 vim_free(repeat_cmdline);
1027 if (count == 0)
1028 repeat_cmdline = vim_strsave(next_cmdline);
1029 else
1030 repeat_cmdline = NULL;
1031 }
1032 }
1033
1034 /* 3. Make a copy of the command so we can mess with it. */
1035 else if (cmdline_copy == NULL)
1036 {
1037 next_cmdline = vim_strsave(next_cmdline);
1038 if (next_cmdline == NULL)
1039 {
1040 EMSG(_(e_outofmem));
1041 retval = FAIL;
1042 break;
1043 }
1044 }
1045 cmdline_copy = next_cmdline;
1046
1047#ifdef FEAT_EVAL
1048 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001049 * Save the current line when inside a ":while" or ":for", and when
1050 * the command looks like a ":while" or ":for", because we may need it
1051 * later. When there is a '|' and another command, it is stored
1052 * separately, because we need to be able to jump back to it from an
1053 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001055 if (current_line == lines_ga.ga_len
1056 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001058 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 {
1060 retval = FAIL;
1061 break;
1062 }
1063 }
1064 did_endif = FALSE;
1065#endif
1066
1067 if (count++ == 0)
1068 {
1069 /*
1070 * All output from the commands is put below each other, without
1071 * waiting for a return. Don't do this when executing commands
1072 * from a script or when being called recursive (e.g. for ":e
1073 * +command file").
1074 */
1075 if (!(flags & DOCMD_NOWAIT) && !recursive)
1076 {
1077 msg_didout_before_start = msg_didout;
1078 msg_didany = FALSE; /* no output yet */
1079 msg_start();
1080 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001081 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 ++RedrawingDisabled;
1083 did_inc = TRUE;
1084 }
1085 }
1086
1087 if (p_verbose >= 15 && sourcing_name != NULL)
1088 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001090 verbose_enter_scroll();
1091
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 smsg((char_u *)_("line %ld: %s"),
1093 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001094 if (msg_silent == 0)
1095 msg_puts((char_u *)"\n"); /* don't overwrite this */
1096
1097 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 --no_wait_return;
1099 }
1100
1101 /*
1102 * 2. Execute one '|' separated command.
1103 * do_one_cmd() will return NULL if there is no trailing '|'.
1104 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1105 */
1106 ++recursive;
1107 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1108#ifdef FEAT_EVAL
1109 &cstack,
1110#endif
1111 cmd_getline, cmd_cookie);
1112 --recursive;
1113
1114#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001115 if (cmd_cookie == (void *)&cmd_loop_cookie)
1116 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001118 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119#endif
1120
1121 if (next_cmdline == NULL)
1122 {
1123 vim_free(cmdline_copy);
1124 cmdline_copy = NULL;
1125#ifdef FEAT_CMDHIST
1126 /*
1127 * If the command was typed, remember it for the ':' register.
1128 * Do this AFTER executing the command to make :@: work.
1129 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001130 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 && new_last_cmdline != NULL)
1132 {
1133 vim_free(last_cmdline);
1134 last_cmdline = new_last_cmdline;
1135 new_last_cmdline = NULL;
1136 }
1137#endif
1138 }
1139 else
1140 {
1141 /* need to copy the command after the '|' to cmdline_copy, for the
1142 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001143 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 next_cmdline = cmdline_copy;
1145 }
1146
1147
1148#ifdef FEAT_EVAL
1149 /* reset did_emsg for a function that is not aborted by an error */
1150 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001151 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 && !func_has_abort(real_cookie))
1153 did_emsg = FALSE;
1154
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001155 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 {
1157 ++current_line;
1158
1159 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001160 * An ":endwhile", ":endfor" and ":continue" is handled here.
1161 * If we were executing commands, jump back to the ":while" or
1162 * ":for".
1163 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001165 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001167 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001169 /* Jump back to the matching ":while" or ":for". Be careful
1170 * not to use a cs_line[] from an entry that isn't a ":while"
1171 * or ":for": It would make "current_line" invalid and can
1172 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 if (!did_emsg && !got_int && !did_throw
1174 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001175 && (cstack.cs_flags[cstack.cs_idx]
1176 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 && cstack.cs_line[cstack.cs_idx] >= 0
1178 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1179 {
1180 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001181 /* remember we jumped there */
1182 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 line_breakcheck(); /* check if CTRL-C typed */
1184
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001185 /* Check for the next breakpoint at or after the ":while"
1186 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 if (breakpoint != NULL)
1188 {
1189 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001190 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 fname,
1192 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1193 *dbg_tick = debug_tick;
1194 }
1195 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001196 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001198 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001200 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1201 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 }
1203 }
1204
1205 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001206 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001208 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001210 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1212 }
1213 }
1214
1215 /*
1216 * When not inside any ":while" loop, clear remembered lines.
1217 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001218 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 {
1220 if (lines_ga.ga_len > 0)
1221 {
1222 sourcing_lnum =
1223 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1224 free_cmdlines(&lines_ga);
1225 }
1226 current_line = 0;
1227 }
1228
1229 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001230 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1231 * being restored at the ":endtry". Reset them here and set the
1232 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1233 * This includes the case where a missing ":endif", ":endwhile" or
1234 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001236 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001238 cstack.cs_lflags &= ~CSL_HAD_FINA;
1239 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1240 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 did_throw ? (void *)current_exception : NULL);
1242 did_emsg = got_int = did_throw = FALSE;
1243 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1244 }
1245
1246 /* Update global "trylevel" for recursive calls to do_cmdline() from
1247 * within this loop. */
1248 trylevel = initial_trylevel + cstack.cs_trylevel;
1249
1250 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001251 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 * files) is aborted because of an error, an interrupt, or an uncaught
1253 * exception, cancel everything. If it is left normally, reset
1254 * force_abort to get the non-EH compatible abortion behavior for
1255 * the rest of the script.
1256 */
1257 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1258 force_abort = FALSE;
1259
1260 /* Convert an interrupt to an exception if appropriate. */
1261 (void)do_intthrow(&cstack);
1262#endif /* FEAT_EVAL */
1263
1264 }
1265 /*
1266 * Continue executing command lines when:
1267 * - no CTRL-C typed, no aborting error, no exception thrown or try
1268 * conditionals need to be checked for executing finally clauses or
1269 * catching an interrupt exception
1270 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001271 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 * looping for ":source" command or function call.
1273 */
1274 while (!((got_int
1275#ifdef FEAT_EVAL
1276 || (did_emsg && force_abort) || did_throw
1277#endif
1278 )
1279#ifdef FEAT_EVAL
1280 && cstack.cs_trylevel == 0
1281#endif
1282 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001283 && !(did_emsg
1284#ifdef FEAT_EVAL
1285 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001286 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001287 * the :endtry to be missed. */
1288 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1289#endif
1290 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001291 && (getline_equal(fgetline, cookie, getexmodeline)
1292 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 && (next_cmdline != NULL
1294#ifdef FEAT_EVAL
1295 || cstack.cs_idx >= 0
1296#endif
1297 || (flags & DOCMD_REPEAT)));
1298
1299 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001300 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301#ifdef FEAT_EVAL
1302 free_cmdlines(&lines_ga);
1303 ga_clear(&lines_ga);
1304
1305 if (cstack.cs_idx >= 0)
1306 {
1307 /*
1308 * If a sourced file or executed function ran to its end, report the
1309 * unclosed conditional.
1310 */
1311 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001312 && ((getline_equal(fgetline, cookie, getsourceline)
1313 && !source_finished(fgetline, cookie))
1314 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 && !func_has_ended(real_cookie))))
1316 {
1317 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1318 EMSG(_(e_endtry));
1319 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1320 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001321 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1322 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 else
1324 EMSG(_(e_endif));
1325 }
1326
1327 /*
1328 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1329 * ":endtry" in a sourced file or executed function. If the try
1330 * conditional is in its finally clause, ignore anything pending.
1331 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001332 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 */
1334 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001335 {
1336 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1337
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001338 if (idx >= 0)
1339 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001340 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1341 &cstack.cs_looplevel);
1342 }
1343 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 trylevel = initial_trylevel;
1345 }
1346
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001347 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1348 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001350 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 ? (char_u *)"endfunction" : (char_u *)NULL);
1352
1353 if (trylevel == 0)
1354 {
1355 /*
1356 * When an exception is being thrown out of the outermost try
1357 * conditional, discard the uncaught exception, disable the conversion
1358 * of interrupts or errors to exceptions, and ensure that no more
1359 * commands are executed.
1360 */
1361 if (did_throw)
1362 {
1363 void *p = NULL;
1364 char_u *saved_sourcing_name;
1365 int saved_sourcing_lnum;
1366 struct msglist *messages = NULL, *next;
1367
1368 /*
1369 * If the uncaught exception is a user exception, report it as an
1370 * error. If it is an error exception, display the saved error
1371 * message now. For an interrupt exception, do nothing; the
1372 * interrupt message is given elsewhere.
1373 */
1374 switch (current_exception->type)
1375 {
1376 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001377 vim_snprintf((char *)IObuff, IOSIZE,
1378 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 current_exception->value);
1380 p = vim_strsave(IObuff);
1381 break;
1382 case ET_ERROR:
1383 messages = current_exception->messages;
1384 current_exception->messages = NULL;
1385 break;
1386 case ET_INTERRUPT:
1387 break;
1388 default:
1389 p = vim_strsave((char_u *)_(e_internal));
1390 }
1391
1392 saved_sourcing_name = sourcing_name;
1393 saved_sourcing_lnum = sourcing_lnum;
1394 sourcing_name = current_exception->throw_name;
1395 sourcing_lnum = current_exception->throw_lnum;
1396 current_exception->throw_name = NULL;
1397
1398 discard_current_exception(); /* uses IObuff if 'verbose' */
1399 suppress_errthrow = TRUE;
1400 force_abort = TRUE;
1401
1402 if (messages != NULL)
1403 {
1404 do
1405 {
1406 next = messages->next;
1407 emsg(messages->msg);
1408 vim_free(messages->msg);
1409 vim_free(messages);
1410 messages = next;
1411 }
1412 while (messages != NULL);
1413 }
1414 else if (p != NULL)
1415 {
1416 emsg(p);
1417 vim_free(p);
1418 }
1419 vim_free(sourcing_name);
1420 sourcing_name = saved_sourcing_name;
1421 sourcing_lnum = saved_sourcing_lnum;
1422 }
1423
1424 /*
1425 * On an interrupt or an aborting error not converted to an exception,
1426 * disable the conversion of errors to exceptions. (Interrupts are not
1427 * converted any more, here.) This enables also the interrupt message
1428 * when force_abort is set and did_emsg unset in case of an interrupt
1429 * from a finally clause after an error.
1430 */
1431 else if (got_int || (did_emsg && force_abort))
1432 suppress_errthrow = TRUE;
1433 }
1434
1435 /*
1436 * The current cstack will be freed when do_cmdline() returns. An uncaught
1437 * exception will have to be rethrown in the previous cstack. If a function
1438 * has just returned or a script file was just finished and the previous
1439 * cstack belongs to the same function or, respectively, script file, it
1440 * will have to be checked for finally clauses to be executed due to the
1441 * ":return" or ":finish". This is done in do_one_cmd().
1442 */
1443 if (did_throw)
1444 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001445 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001447 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 && ex_nesting_level > func_level(real_cookie) + 1))
1449 {
1450 if (!did_throw)
1451 check_cstack = TRUE;
1452 }
1453 else
1454 {
1455 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001456 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 --ex_nesting_level;
1458 /*
1459 * Go to debug mode when returning from a function in which we are
1460 * single-stepping.
1461 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001462 if ((getline_equal(fgetline, cookie, getsourceline)
1463 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001465 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 ? (char_u *)_("End of sourced file")
1467 : (char_u *)_("End of function"));
1468 }
1469
1470 /*
1471 * Restore the exception environment (done after returning from the
1472 * debugger).
1473 */
1474 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001475 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476
1477 msg_list = saved_msg_list;
1478#endif /* FEAT_EVAL */
1479
1480 /*
1481 * If there was too much output to fit on the command line, ask the user to
1482 * hit return before redrawing the screen. With the ":global" command we do
1483 * this only once after the command is finished.
1484 */
1485 if (did_inc)
1486 {
1487 --RedrawingDisabled;
1488 --no_wait_return;
1489 msg_scroll = FALSE;
1490
1491 /*
1492 * When just finished an ":if"-":else" which was typed, no need to
1493 * wait for hit-return. Also for an error situation.
1494 */
1495 if (retval == FAIL
1496#ifdef FEAT_EVAL
1497 || (did_endif && KeyTyped && !did_emsg)
1498#endif
1499 )
1500 {
1501 need_wait_return = FALSE;
1502 msg_didany = FALSE; /* don't wait when restarting edit */
1503 }
1504 else if (need_wait_return)
1505 {
1506 /*
1507 * The msg_start() above clears msg_didout. The wait_return we do
1508 * here should not overwrite the command that may be shown before
1509 * doing that.
1510 */
1511 msg_didout |= msg_didout_before_start;
1512 wait_return(FALSE);
1513 }
1514 }
1515
Bram Moolenaar2a942252012-11-28 23:03:07 +01001516#ifdef FEAT_EVAL
1517 did_endif = FALSE; /* in case do_cmdline used recursively */
1518#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 /*
1520 * Reset if_level, in case a sourced script file contains more ":if" than
1521 * ":endif" (could be ":if x | foo | endif").
1522 */
1523 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001524#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001525
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 --call_depth;
1527 return retval;
1528}
1529
1530#ifdef FEAT_EVAL
1531/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001532 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 */
1534 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001535get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001537 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 wcmd_T *wp;
1539 char_u *line;
1540
1541 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1542 {
1543 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001544 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001546 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 if (cp->getline == NULL)
1548 line = getcmdline(c, 0L, indent);
1549 else
1550 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001551 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 ++cp->current_line;
1553
1554 return line;
1555 }
1556
1557 KeyTyped = FALSE;
1558 ++cp->current_line;
1559 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1560 sourcing_lnum = wp->lnum;
1561 return vim_strsave(wp->line);
1562}
1563
1564/*
1565 * Store a line in "gap" so that a ":while" loop can execute it again.
1566 */
1567 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001568store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569{
1570 if (ga_grow(gap, 1) == FAIL)
1571 return FAIL;
1572 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1573 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1574 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 return OK;
1576}
1577
1578/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001579 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 */
1581 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001582free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583{
1584 while (gap->ga_len > 0)
1585 {
1586 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1587 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 }
1589}
1590#endif
1591
1592/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001593 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1594 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001597getline_equal(
1598 char_u *(*fgetline)(int, void *, int),
1599 void *cookie UNUSED, /* argument for fgetline() */
1600 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601{
1602#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001603 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001604 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605
Bram Moolenaar89d40322006-08-29 15:30:07 +00001606 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001607 * function that's originally used to obtain the lines. This may be
1608 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001609 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001610 cp = (struct loop_cookie *)cookie;
1611 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 {
1613 gp = cp->getline;
1614 cp = cp->cookie;
1615 }
1616 return gp == func;
1617#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001618 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619#endif
1620}
1621
1622#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1623/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001624 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 * getline function. Otherwise return "cookie".
1626 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001628getline_cookie(
1629 char_u *(*fgetline)(int, void *, int) UNUSED,
1630 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631{
1632# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001633 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001634 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635
Bram Moolenaar89d40322006-08-29 15:30:07 +00001636 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001637 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001639 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001640 cp = (struct loop_cookie *)cookie;
1641 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 {
1643 gp = cp->getline;
1644 cp = cp->cookie;
1645 }
1646 return cp;
1647# else
1648 return cookie;
1649# endif
1650}
1651#endif
1652
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001653
1654/*
1655 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1656 * ":bwipeout", etc.
1657 * Returns the buffer number.
1658 */
1659 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001660compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001661{
1662 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001663 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001664 int count = offset;
1665
1666 buf = firstbuf;
1667 while (buf->b_next != NULL && buf->b_fnum < lnum)
1668 buf = buf->b_next;
1669 while (count != 0)
1670 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001671 count += (offset < 0) ? 1 : -1;
1672 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1673 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001674 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001675 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001676 if (addr_type == ADDR_LOADED_BUFFERS)
1677 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001678 while (buf->b_ml.ml_mfp == NULL)
1679 {
1680 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1681 if (nextbuf == NULL)
1682 break;
1683 buf = nextbuf;
1684 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001685 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001686 /* we might have gone too far, last buffer is not loadedd */
1687 if (addr_type == ADDR_LOADED_BUFFERS)
1688 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 return buf->b_fnum;
1696}
1697
Bram Moolenaarf240e182014-11-27 18:33:02 +01001698#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001699static int current_win_nr(win_T *win);
1700static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001701
1702 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001703current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001704{
1705 win_T *wp;
1706 int nr = 0;
1707
1708 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1709 {
1710 ++nr;
1711 if (wp == win)
1712 break;
1713 }
1714 return nr;
1715}
1716
1717 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001718current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001719{
1720 tabpage_T *tp;
1721 int nr = 0;
1722
1723 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
1724 {
1725 ++nr;
1726 if (tp == tab)
1727 break;
1728 }
1729 return nr;
1730}
1731
1732# define CURRENT_WIN_NR current_win_nr(curwin)
1733# define LAST_WIN_NR current_win_nr(NULL)
1734# define CURRENT_TAB_NR current_tab_nr(curtab)
1735# define LAST_TAB_NR current_tab_nr(NULL)
1736#else
1737# define CURRENT_WIN_NR 1
1738# define LAST_WIN_NR 1
1739# define CURRENT_TAB_NR 1
1740# define LAST_TAB_NR 1
1741#endif
1742
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001743
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744/*
1745 * Execute one Ex command.
1746 *
1747 * If 'sourcing' is TRUE, the command will be included in the error message.
1748 *
1749 * 1. skip comment lines and leading space
1750 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001751 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001752 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001753 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001754 * 6. parse arguments
1755 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001757 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 *
1759 * This function may be called recursively!
1760 */
1761#if (_MSC_VER == 1200)
1762/*
Bram Moolenaared203462004-06-16 11:19:22 +00001763 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001765 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766#endif
1767 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001768do_one_cmd(
1769 char_u **cmdlinep,
1770 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001772 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001774 char_u *(*fgetline)(int, void *, int),
1775 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776{
1777 char_u *p;
1778 linenr_T lnum;
1779 long n;
1780 char_u *errormsg = NULL; /* error message */
1781 exarg_T ea; /* Ex command arguments */
1782 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001783 int save_msg_scroll = msg_scroll;
1784 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001786#ifdef HAVE_SANDBOX
1787 int did_sandbox = FALSE;
1788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 cmdmod_T save_cmdmod;
1790 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001791 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792
1793 vim_memset(&ea, 0, sizeof(ea));
1794 ea.line1 = 1;
1795 ea.line2 = 1;
1796#ifdef FEAT_EVAL
1797 ++ex_nesting_level;
1798#endif
1799
Bram Moolenaar21691f82012-12-05 19:13:18 +01001800 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 if (quitmore
1802#ifdef FEAT_EVAL
1803 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001804 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001805#endif
1806#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001807 /* avoid that an autocommand, e.g. QuitPre, does this */
1808 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809#endif
1810 )
1811 --quitmore;
1812
1813 /*
1814 * Reset browse, confirm, etc.. They are restored when returning, for
1815 * recursive calls.
1816 */
1817 save_cmdmod = cmdmod;
1818 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1819
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001820 /* "#!anything" is handled like a comment. */
1821 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1822 goto doend;
1823
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 /*
1825 * Repeat until no more command modifiers are found.
1826 */
1827 ea.cmd = *cmdlinep;
1828 for (;;)
1829 {
1830/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001831 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 */
1833 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1834 ++ea.cmd;
1835
1836 /* in ex mode, an empty line works like :+ */
1837 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001838 && (getline_equal(fgetline, cookie, getexmodeline)
1839 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001840 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 {
1842 ea.cmd = (char_u *)"+";
1843 ex_pressedreturn = TRUE;
1844 }
1845
1846 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001847 if (*ea.cmd == '"')
1848 goto doend;
1849 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001850 {
1851 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854
1855/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001856 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 */
1858 p = ea.cmd;
1859 if (VIM_ISDIGIT(*ea.cmd))
1860 p = skipwhite(skipdigits(ea.cmd));
1861 switch (*p)
1862 {
1863 /* When adding an entry, also modify cmd_exists(). */
1864 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1865 break;
1866#ifdef FEAT_WINDOWS
1867 cmdmod.split |= WSP_ABOVE;
1868#endif
1869 continue;
1870
1871 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1872 {
1873#ifdef FEAT_WINDOWS
1874 cmdmod.split |= WSP_BELOW;
1875#endif
1876 continue;
1877 }
1878 if (checkforcmd(&ea.cmd, "browse", 3))
1879 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001880#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 cmdmod.browse = TRUE;
1882#endif
1883 continue;
1884 }
1885 if (!checkforcmd(&ea.cmd, "botright", 2))
1886 break;
1887#ifdef FEAT_WINDOWS
1888 cmdmod.split |= WSP_BOT;
1889#endif
1890 continue;
1891
1892 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1893 break;
1894#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1895 cmdmod.confirm = TRUE;
1896#endif
1897 continue;
1898
1899 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1900 {
1901 cmdmod.keepmarks = TRUE;
1902 continue;
1903 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001904 if (checkforcmd(&ea.cmd, "keepalt", 5))
1905 {
1906 cmdmod.keepalt = TRUE;
1907 continue;
1908 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001909 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1910 {
1911 cmdmod.keeppatterns = TRUE;
1912 continue;
1913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1915 break;
1916 cmdmod.keepjumps = TRUE;
1917 continue;
1918
1919 /* ":hide" and ":hide | cmd" are not modifiers */
1920 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1921 || *p == NUL || ends_excmd(*p))
1922 break;
1923 ea.cmd = p;
1924 cmdmod.hide = TRUE;
1925 continue;
1926
1927 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1928 {
1929 cmdmod.lockmarks = TRUE;
1930 continue;
1931 }
1932
1933 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1934 break;
1935#ifdef FEAT_WINDOWS
1936 cmdmod.split |= WSP_ABOVE;
1937#endif
1938 continue;
1939
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001940 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001941 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001942#ifdef FEAT_AUTOCMD
1943 if (cmdmod.save_ei == NULL)
1944 {
1945 /* Set 'eventignore' to "all". Restore the
1946 * existing option value later. */
1947 cmdmod.save_ei = vim_strsave(p_ei);
1948 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001949 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001950 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001951#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001952 continue;
1953 }
1954 if (!checkforcmd(&ea.cmd, "noswapfile", 6))
1955 break;
1956 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001957 continue;
1958
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1960 break;
1961#ifdef FEAT_WINDOWS
1962 cmdmod.split |= WSP_BELOW;
1963#endif
1964 continue;
1965
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001966 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1967 {
1968#ifdef HAVE_SANDBOX
1969 if (!did_sandbox)
1970 ++sandbox;
1971 did_sandbox = TRUE;
1972#endif
1973 continue;
1974 }
1975 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001977 if (save_msg_silent == -1)
1978 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1981 {
1982 /* ":silent!", but not "silent !cmd" */
1983 ea.cmd = skipwhite(ea.cmd + 1);
1984 ++emsg_silent;
1985 ++did_esilent;
1986 }
1987 continue;
1988
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001989 case 't': if (checkforcmd(&p, "tab", 3))
1990 {
1991#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001992 if (vim_isdigit(*ea.cmd))
1993 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1994 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001995 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001996 ea.cmd = p;
1997#endif
1998 continue;
1999 }
2000 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 break;
2002#ifdef FEAT_WINDOWS
2003 cmdmod.split |= WSP_TOP;
2004#endif
2005 continue;
2006
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002007 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
2008 break;
2009 if (save_msg_silent == -1)
2010 save_msg_silent = msg_silent;
2011 msg_silent = 0;
2012 continue;
2013
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
2015 {
2016#ifdef FEAT_VERTSPLIT
2017 cmdmod.split |= WSP_VERT;
2018#endif
2019 continue;
2020 }
2021 if (!checkforcmd(&p, "verbose", 4))
2022 break;
2023 if (verbose_save < 0)
2024 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00002025 if (vim_isdigit(*ea.cmd))
2026 p_verbose = atoi((char *)ea.cmd);
2027 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 p_verbose = 1;
2029 ea.cmd = p;
2030 continue;
2031 }
2032 break;
2033 }
2034
2035#ifdef FEAT_EVAL
2036 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2037 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2038#else
2039 ea.skip = (if_level > 0);
2040#endif
2041
2042#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002043# ifdef FEAT_PROFILE
2044 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002045 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002046 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002047 if (getline_equal(fgetline, cookie, get_func_line))
2048 func_line_exec(getline_cookie(fgetline, cookie));
2049 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002050 script_line_exec();
2051 }
2052#endif
2053
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 /* May go to debug mode. If this happens and the ">quit" debug command is
2055 * used, throw an interrupt exception and skip the next command. */
2056 dbg_check_breakpoint(&ea);
2057 if (!ea.skip && got_int)
2058 {
2059 ea.skip = TRUE;
2060 (void)do_intthrow(cstack);
2061 }
2062#endif
2063
2064/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002065 * 3. Skip over the range to find the command. Let "p" point to after it.
2066 *
2067 * We need the command to know what kind of range it uses.
2068 */
2069 cmd = ea.cmd;
2070 ea.cmd = skip_range(ea.cmd, NULL);
2071 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2072 ea.cmd = skipwhite(ea.cmd + 1);
2073 p = find_command(&ea, NULL);
2074
2075/*
2076 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 *
2078 * where 'addr' is:
2079 *
2080 * % (entire file)
2081 * $ [+-NUM]
2082 * 'x [+-NUM] (where x denotes a currently defined mark)
2083 * . [+-NUM]
2084 * [+-NUM]..
2085 * NUM
2086 *
2087 * The ea.cmd pointer is updated to point to the first character following the
2088 * range spec. If an initial address is found, but no second, the upper bound
2089 * is equal to the lower.
2090 */
2091
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002092 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002093 if (!IS_USER_CMDIDX(ea.cmdidx))
2094 {
2095 if (ea.cmdidx != CMD_SIZE)
2096 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2097 else
2098 ea.addr_type = ADDR_LINES;
2099
2100#ifdef FEAT_WINDOWS
2101 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002102 if (ea.cmdidx == CMD_wincmd && p != NULL)
2103 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002104#endif
2105 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002106
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002108 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 for (;;)
2110 {
2111 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002112 switch (ea.addr_type)
2113 {
2114 case ADDR_LINES:
2115 /* default is current line number */
2116 ea.line2 = curwin->w_cursor.lnum;
2117 break;
2118 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002119 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002120 ea.line2 = lnum;
2121 break;
2122 case ADDR_ARGUMENTS:
2123 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002124 if (ea.line2 > ARGCOUNT)
2125 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002126 break;
2127 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002128 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002129 ea.line2 = curbuf->b_fnum;
2130 break;
2131 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002132 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002133 ea.line2 = lnum;
2134 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002135#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002136 case ADDR_QUICKFIX:
2137 ea.line2 = qf_get_cur_valid_idx(&ea);
2138 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002139#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002142 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip, ea.addr_count == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 if (ea.cmd == NULL) /* error detected */
2144 goto doend;
2145 if (lnum == MAXLNUM)
2146 {
2147 if (*ea.cmd == '%') /* '%' - all lines */
2148 {
2149 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002150 switch (ea.addr_type)
2151 {
2152 case ADDR_LINES:
2153 ea.line1 = 1;
2154 ea.line2 = curbuf->b_ml.ml_line_count;
2155 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002156 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002157 {
2158 buf_T *buf = firstbuf;
2159
2160 while (buf->b_next != NULL
2161 && buf->b_ml.ml_mfp == NULL)
2162 buf = buf->b_next;
2163 ea.line1 = buf->b_fnum;
2164 buf = lastbuf;
2165 while (buf->b_prev != NULL
2166 && buf->b_ml.ml_mfp == NULL)
2167 buf = buf->b_prev;
2168 ea.line2 = buf->b_fnum;
2169 break;
2170 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002171 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002172 ea.line1 = firstbuf->b_fnum;
2173 ea.line2 = lastbuf->b_fnum;
2174 break;
2175 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002176 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002177 if (IS_USER_CMDIDX(ea.cmdidx))
2178 {
2179 ea.line1 = 1;
2180 ea.line2 = ea.addr_type == ADDR_WINDOWS
2181 ? LAST_WIN_NR : LAST_TAB_NR;
2182 }
2183 else
2184 {
2185 /* there is no Vim command which uses '%' and
2186 * ADDR_WINDOWS or ADDR_TABS */
2187 errormsg = (char_u *)_(e_invrange);
2188 goto doend;
2189 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002190 break;
2191 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002192 if (ARGCOUNT == 0)
2193 ea.line1 = ea.line2 = 0;
2194 else
2195 {
2196 ea.line1 = 1;
2197 ea.line2 = ARGCOUNT;
2198 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002199 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002200#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002201 case ADDR_QUICKFIX:
2202 ea.line1 = 1;
2203 ea.line2 = qf_get_size(&ea);
2204 if (ea.line2 == 0)
2205 ea.line2 = 1;
2206 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002207#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 ++ea.addr_count;
2210 }
2211 /* '*' - visual area */
2212 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2213 {
2214 pos_T *fp;
2215
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002216 if (ea.addr_type != ADDR_LINES)
2217 {
2218 errormsg = (char_u *)_(e_invrange);
2219 goto doend;
2220 }
2221
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 ++ea.cmd;
2223 if (!ea.skip)
2224 {
2225 fp = getmark('<', FALSE);
2226 if (check_mark(fp) == FAIL)
2227 goto doend;
2228 ea.line1 = fp->lnum;
2229 fp = getmark('>', FALSE);
2230 if (check_mark(fp) == FAIL)
2231 goto doend;
2232 ea.line2 = fp->lnum;
2233 ++ea.addr_count;
2234 }
2235 }
2236 }
2237 else
2238 ea.line2 = lnum;
2239 ea.addr_count++;
2240
2241 if (*ea.cmd == ';')
2242 {
2243 if (!ea.skip)
2244 curwin->w_cursor.lnum = ea.line2;
2245 }
2246 else if (*ea.cmd != ',')
2247 break;
2248 ++ea.cmd;
2249 }
2250
2251 /* One address given: set start and end lines */
2252 if (ea.addr_count == 1)
2253 {
2254 ea.line1 = ea.line2;
2255 /* ... but only implicit: really no address given */
2256 if (lnum == MAXLNUM)
2257 ea.addr_count = 0;
2258 }
2259
2260 /* Don't leave the cursor on an illegal line (caused by ';') */
2261 check_cursor_lnum();
2262
2263/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002264 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 */
2266
2267 /*
2268 * Skip ':' and any white space
2269 */
2270 ea.cmd = skipwhite(ea.cmd);
2271 while (*ea.cmd == ':')
2272 ea.cmd = skipwhite(ea.cmd + 1);
2273
2274 /*
2275 * If we got a line, but no command, then go to the line.
2276 * If we find a '|' or '\n' we set ea.nextcmd.
2277 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002278 if (*ea.cmd == NUL || *ea.cmd == '"'
2279 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 {
2281 /*
2282 * strange vi behaviour:
2283 * ":3" jumps to line 3
2284 * ":3|..." prints line 3
2285 * ":|" prints current line
2286 */
2287 if (ea.skip) /* skip this if inside :if */
2288 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002289 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 {
2291 ea.cmdidx = CMD_print;
2292 ea.argt = RANGE+COUNT+TRLBAR;
2293 if ((errormsg = invalid_range(&ea)) == NULL)
2294 {
2295 correct_range(&ea);
2296 ex_print(&ea);
2297 }
2298 }
2299 else if (ea.addr_count != 0)
2300 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002301 if (ea.line2 > curbuf->b_ml.ml_line_count)
2302 {
2303 /* With '-' in 'cpoptions' a line number past the file is an
2304 * error, otherwise put it at the end of the file. */
2305 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2306 ea.line2 = -1;
2307 else
2308 ea.line2 = curbuf->b_ml.ml_line_count;
2309 }
2310
2311 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002312 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 else
2314 {
2315 if (ea.line2 == 0)
2316 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 else
2318 curwin->w_cursor.lnum = ea.line2;
2319 beginline(BL_SOL | BL_FIX);
2320 }
2321 }
2322 goto doend;
2323 }
2324
Bram Moolenaard5005162014-08-22 23:05:54 +02002325#ifdef FEAT_AUTOCMD
2326 /* If this looks like an undefined user command and there are CmdUndefined
2327 * autocommands defined, trigger the matching autocommands. */
2328 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2329 && ASCII_ISUPPER(*ea.cmd)
2330 && has_cmdundefined())
2331 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002332 int ret;
2333
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002334 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002335 while (ASCII_ISALNUM(*p))
2336 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002337 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002338 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2339 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002340 /* If the autocommands did something and didn't cause an error, try
2341 * finding the command again. */
2342 p = (ret && !aborting()) ? find_command(&ea, NULL) : NULL;
Bram Moolenaard5005162014-08-22 23:05:54 +02002343 }
2344#endif
2345
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346#ifdef FEAT_USR_CMDS
2347 if (p == NULL)
2348 {
2349 if (!ea.skip)
2350 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2351 goto doend;
2352 }
2353 /* Check for wrong commands. */
2354 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2355 {
2356 errormsg = uc_fun_cmd();
2357 goto doend;
2358 }
2359#endif
2360 if (ea.cmdidx == CMD_SIZE)
2361 {
2362 if (!ea.skip)
2363 {
2364 STRCPY(IObuff, _("E492: Not an editor command"));
2365 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002366 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002368 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 }
2370 goto doend;
2371 }
2372
Bram Moolenaar958636c2014-10-21 20:01:58 +02002373 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2374 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002375#ifdef HAVE_EX_SCRIPT_NI
2376 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2377#endif
2378 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379
2380#ifndef FEAT_EVAL
2381 /*
2382 * When the expression evaluation is disabled, recognize the ":if" and
2383 * ":endif" commands and ignore everything in between it.
2384 */
2385 if (ea.cmdidx == CMD_if)
2386 ++if_level;
2387 if (if_level)
2388 {
2389 if (ea.cmdidx == CMD_endif)
2390 --if_level;
2391 goto doend;
2392 }
2393
2394#endif
2395
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002396 /* forced commands */
2397 if (*p == '!' && ea.cmdidx != CMD_substitute
2398 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 {
2400 ++p;
2401 ea.forceit = TRUE;
2402 }
2403 else
2404 ea.forceit = FALSE;
2405
2406/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002407 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002409 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002410 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411
2412 if (!ea.skip)
2413 {
2414#ifdef HAVE_SANDBOX
2415 if (sandbox != 0 && !(ea.argt & SBOXOK))
2416 {
2417 /* Command not allowed in sandbox. */
2418 errormsg = (char_u *)_(e_sandbox);
2419 goto doend;
2420 }
2421#endif
2422 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2423 {
2424 /* Command not allowed in non-'modifiable' buffer */
2425 errormsg = (char_u *)_(e_modifiable);
2426 goto doend;
2427 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002428
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002429 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002430 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002432 /* Command not allowed when editing the command line. */
2433#ifdef FEAT_CMDWIN
2434 if (cmdwin_type != 0)
2435 errormsg = (char_u *)_(e_cmdwin);
2436 else
2437#endif
2438 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 goto doend;
2440 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002441#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002442 /* Disallow editing another buffer when "curbuf_lock" is set.
2443 * Do allow ":edit" (check for argument later).
2444 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002445 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002446 && ea.cmdidx != CMD_edit
2447 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002448 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002449 && curbuf_locked())
2450 goto doend;
2451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452
2453 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2454 {
2455 /* no range allowed */
2456 errormsg = (char_u *)_(e_norange);
2457 goto doend;
2458 }
2459 }
2460
2461 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2462 {
2463 errormsg = (char_u *)_(e_nobang);
2464 goto doend;
2465 }
2466
2467 /*
2468 * Don't complain about the range if it is not used
2469 * (could happen if line_count is accidentally set to 0).
2470 */
2471 if (!ea.skip && !ni)
2472 {
2473 /*
2474 * If the range is backwards, ask for confirmation and, if given, swap
2475 * ea.line1 & ea.line2 so it's forwards again.
2476 * When global command is busy, don't ask, will fail below.
2477 */
2478 if (!global_busy && ea.line1 > ea.line2)
2479 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002480 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002482 if (sourcing || exmode_active)
2483 {
2484 errormsg = (char_u *)_("E493: Backwards range given");
2485 goto doend;
2486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 if (ask_yesno((char_u *)
2488 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002489 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 }
2491 lnum = ea.line1;
2492 ea.line1 = ea.line2;
2493 ea.line2 = lnum;
2494 }
2495 if ((errormsg = invalid_range(&ea)) != NULL)
2496 goto doend;
2497 }
2498
2499 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2500 ea.line2 = 1;
2501
2502 correct_range(&ea);
2503
2504#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002505 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2506 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 {
2508 /* Put the first line at the start of a closed fold, put the last line
2509 * at the end of a closed fold. */
2510 (void)hasFolding(ea.line1, &ea.line1, NULL);
2511 (void)hasFolding(ea.line2, NULL, &ea.line2);
2512 }
2513#endif
2514
2515#ifdef FEAT_QUICKFIX
2516 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002517 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 * option here, so things like % get expanded.
2519 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002520 p = replace_makeprg(&ea, p, cmdlinep);
2521 if (p == NULL)
2522 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523#endif
2524
2525 /*
2526 * Skip to start of argument.
2527 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2528 */
2529 if (ea.cmdidx == CMD_bang)
2530 ea.arg = p;
2531 else
2532 ea.arg = skipwhite(p);
2533
2534 /*
2535 * Check for "++opt=val" argument.
2536 * Must be first, allow ":w ++enc=utf8 !cmd"
2537 */
2538 if (ea.argt & ARGOPT)
2539 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2540 if (getargopt(&ea) == FAIL && !ni)
2541 {
2542 errormsg = (char_u *)_(e_invarg);
2543 goto doend;
2544 }
2545
2546 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2547 {
2548 if (*ea.arg == '>') /* append */
2549 {
2550 if (*++ea.arg != '>') /* typed wrong */
2551 {
2552 errormsg = (char_u *)_("E494: Use w or w>>");
2553 goto doend;
2554 }
2555 ea.arg = skipwhite(ea.arg + 1);
2556 ea.append = TRUE;
2557 }
2558 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2559 {
2560 ++ea.arg;
2561 ea.usefilter = TRUE;
2562 }
2563 }
2564
2565 if (ea.cmdidx == CMD_read)
2566 {
2567 if (ea.forceit)
2568 {
2569 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2570 ea.forceit = FALSE;
2571 }
2572 else if (*ea.arg == '!') /* :r !filter */
2573 {
2574 ++ea.arg;
2575 ea.usefilter = TRUE;
2576 }
2577 }
2578
2579 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2580 {
2581 ea.amount = 1;
2582 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2583 {
2584 ++ea.arg;
2585 ++ea.amount;
2586 }
2587 ea.arg = skipwhite(ea.arg);
2588 }
2589
2590 /*
2591 * Check for "+command" argument, before checking for next command.
2592 * Don't do this for ":read !cmd" and ":write !cmd".
2593 */
2594 if ((ea.argt & EDITCMD) && !ea.usefilter)
2595 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2596
2597 /*
2598 * Check for '|' to separate commands and '"' to start comments.
2599 * Don't do this for ":read !cmd" and ":write !cmd".
2600 */
2601 if ((ea.argt & TRLBAR) && !ea.usefilter)
2602 separate_nextcmd(&ea);
2603
2604 /*
2605 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002606 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2607 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002609 else if (ea.cmdidx == CMD_bang
2610 || ea.cmdidx == CMD_global
2611 || ea.cmdidx == CMD_vglobal
2612 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 {
2614 for (p = ea.arg; *p; ++p)
2615 {
2616 /* Remove one backslash before a newline, so that it's possible to
2617 * pass a newline to the shell and also a newline that is preceded
2618 * with a backslash. This makes it impossible to end a shell
2619 * command in a backslash, but that doesn't appear useful.
2620 * Halving the number of backslashes is incompatible with previous
2621 * versions. */
2622 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002623 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 else if (*p == '\n')
2625 {
2626 ea.nextcmd = p + 1;
2627 *p = NUL;
2628 break;
2629 }
2630 }
2631 }
2632
2633 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2634 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002635 buf_T *buf;
2636
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002638 switch (ea.addr_type)
2639 {
2640 case ADDR_LINES:
2641 ea.line2 = curbuf->b_ml.ml_line_count;
2642 break;
2643 case ADDR_LOADED_BUFFERS:
2644 buf = firstbuf;
2645 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2646 buf = buf->b_next;
2647 ea.line1 = buf->b_fnum;
2648 buf = lastbuf;
2649 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2650 buf = buf->b_prev;
2651 ea.line2 = buf->b_fnum;
2652 break;
2653 case ADDR_BUFFERS:
2654 ea.line1 = firstbuf->b_fnum;
2655 ea.line2 = lastbuf->b_fnum;
2656 break;
2657 case ADDR_WINDOWS:
2658 ea.line2 = LAST_WIN_NR;
2659 break;
2660 case ADDR_TABS:
2661 ea.line2 = LAST_TAB_NR;
2662 break;
2663 case ADDR_ARGUMENTS:
2664 if (ARGCOUNT == 0)
2665 ea.line1 = ea.line2 = 0;
2666 else
2667 ea.line2 = ARGCOUNT;
2668 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002669#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002670 case ADDR_QUICKFIX:
2671 ea.line2 = qf_get_size(&ea);
2672 if (ea.line2 == 0)
2673 ea.line2 = 1;
2674 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002675#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 }
2678
2679 /* accept numbered register only when no count allowed (:put) */
2680 if ( (ea.argt & REGSTR)
2681 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002682 /* Do not allow register = for user commands */
2683 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2685 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002686#ifndef FEAT_CLIPBOARD
2687 /* check these explicitly for a more specific error message */
2688 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002690 errormsg = (char_u *)_(e_invalidreg);
2691 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 }
2693#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002694 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2695 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002696 {
2697 ea.regname = *ea.arg++;
2698#ifdef FEAT_EVAL
2699 /* for '=' register: accept the rest of the line as an expression */
2700 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2701 {
2702 set_expr_line(vim_strsave(ea.arg));
2703 ea.arg += STRLEN(ea.arg);
2704 }
2705#endif
2706 ea.arg = skipwhite(ea.arg);
2707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 }
2709
2710 /*
2711 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2712 * count, it's a buffer name.
2713 */
2714 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2715 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2716 || vim_iswhite(*p)))
2717 {
2718 n = getdigits(&ea.arg);
2719 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002720 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 {
2722 errormsg = (char_u *)_(e_zerocount);
2723 goto doend;
2724 }
2725 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2726 {
2727 ea.line2 = n;
2728 if (ea.addr_count == 0)
2729 ea.addr_count = 1;
2730 }
2731 else
2732 {
2733 ea.line1 = ea.line2;
2734 ea.line2 += n - 1;
2735 ++ea.addr_count;
2736 /*
2737 * Be vi compatible: no error message for out of range.
2738 */
2739 if (ea.line2 > curbuf->b_ml.ml_line_count)
2740 ea.line2 = curbuf->b_ml.ml_line_count;
2741 }
2742 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002743
2744 /*
2745 * Check for flags: 'l', 'p' and '#'.
2746 */
2747 if (ea.argt & EXFLAGS)
2748 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002750 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002751 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 {
2753 errormsg = (char_u *)_(e_trailing);
2754 goto doend;
2755 }
2756
2757 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2758 {
2759 errormsg = (char_u *)_(e_argreq);
2760 goto doend;
2761 }
2762
2763#ifdef FEAT_EVAL
2764 /*
2765 * Skip the command when it's not going to be executed.
2766 * The commands like :if, :endif, etc. always need to be executed.
2767 * Also make an exception for commands that handle a trailing command
2768 * themselves.
2769 */
2770 if (ea.skip)
2771 {
2772 switch (ea.cmdidx)
2773 {
2774 /* commands that need evaluation */
2775 case CMD_while:
2776 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002777 case CMD_for:
2778 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 case CMD_if:
2780 case CMD_elseif:
2781 case CMD_else:
2782 case CMD_endif:
2783 case CMD_try:
2784 case CMD_catch:
2785 case CMD_finally:
2786 case CMD_endtry:
2787 case CMD_function:
2788 break;
2789
2790 /* Commands that handle '|' themselves. Check: A command should
2791 * either have the TRLBAR flag, appear in this list or appear in
2792 * the list at ":help :bar". */
2793 case CMD_aboveleft:
2794 case CMD_and:
2795 case CMD_belowright:
2796 case CMD_botright:
2797 case CMD_browse:
2798 case CMD_call:
2799 case CMD_confirm:
2800 case CMD_delfunction:
2801 case CMD_djump:
2802 case CMD_dlist:
2803 case CMD_dsearch:
2804 case CMD_dsplit:
2805 case CMD_echo:
2806 case CMD_echoerr:
2807 case CMD_echomsg:
2808 case CMD_echon:
2809 case CMD_execute:
2810 case CMD_help:
2811 case CMD_hide:
2812 case CMD_ijump:
2813 case CMD_ilist:
2814 case CMD_isearch:
2815 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002816 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 case CMD_keepjumps:
2818 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002819 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 case CMD_leftabove:
2821 case CMD_let:
2822 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002823 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002825 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002826 case CMD_noautocmd:
2827 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 case CMD_perl:
2829 case CMD_psearch:
2830 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002831 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002832 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 case CMD_return:
2834 case CMD_rightbelow:
2835 case CMD_ruby:
2836 case CMD_silent:
2837 case CMD_smagic:
2838 case CMD_snomagic:
2839 case CMD_substitute:
2840 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002841 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 case CMD_tcl:
2843 case CMD_throw:
2844 case CMD_tilde:
2845 case CMD_topleft:
2846 case CMD_unlet:
2847 case CMD_verbose:
2848 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002849 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 break;
2851
2852 default: goto doend;
2853 }
2854 }
2855#endif
2856
2857 if (ea.argt & XFILE)
2858 {
2859 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2860 goto doend;
2861 }
2862
2863#ifdef FEAT_LISTCMDS
2864 /*
2865 * Accept buffer name. Cannot be used at the same time with a buffer
2866 * number. Don't do this for a user command.
2867 */
2868 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002869 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 {
2871 /*
2872 * :bdelete, :bwipeout and :bunload take several arguments, separated
2873 * by spaces: find next space (skipping over escaped characters).
2874 * The others take one argument: ignore trailing spaces.
2875 */
2876 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2877 || ea.cmdidx == CMD_bunload)
2878 p = skiptowhite_esc(ea.arg);
2879 else
2880 {
2881 p = ea.arg + STRLEN(ea.arg);
2882 while (p > ea.arg && vim_iswhite(p[-1]))
2883 --p;
2884 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002885 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2886 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 if (ea.line2 < 0) /* failed */
2888 goto doend;
2889 ea.addr_count = 1;
2890 ea.arg = skipwhite(p);
2891 }
2892#endif
2893
2894/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002895 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 *
2897 * The "ea" structure holds the arguments that can be used.
2898 */
2899 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002900 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 ea.cookie = cookie;
2902#ifdef FEAT_EVAL
2903 ea.cstack = cstack;
2904#endif
2905
2906#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002907 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 {
2909 /*
2910 * Execute a user-defined command.
2911 */
2912 do_ucmd(&ea);
2913 }
2914 else
2915#endif
2916 {
2917 /*
2918 * Call the function to execute the command.
2919 */
2920 ea.errmsg = NULL;
2921 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2922 if (ea.errmsg != NULL)
2923 errormsg = (char_u *)_(ea.errmsg);
2924 }
2925
2926#ifdef FEAT_EVAL
2927 /*
2928 * If the command just executed called do_cmdline(), any throw or ":return"
2929 * or ":finish" encountered there must also check the cstack of the still
2930 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2931 * exception, or reanimate a returned function or finished script file and
2932 * return or finish it again.
2933 */
2934 if (need_rethrow)
2935 do_throw(cstack);
2936 else if (check_cstack)
2937 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002938 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002940 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 && current_func_returned())
2942 do_return(&ea, TRUE, FALSE, NULL);
2943 }
2944 need_rethrow = check_cstack = FALSE;
2945#endif
2946
2947doend:
2948 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2949 curwin->w_cursor.lnum = 1;
2950
2951 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2952 {
2953 if (sourcing)
2954 {
2955 if (errormsg != IObuff)
2956 {
2957 STRCPY(IObuff, errormsg);
2958 errormsg = IObuff;
2959 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002960 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 }
2962 emsg(errormsg);
2963 }
2964#ifdef FEAT_EVAL
2965 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002966 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2967 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968#endif
2969
2970 if (verbose_save >= 0)
2971 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002972#ifdef FEAT_AUTOCMD
2973 if (cmdmod.save_ei != NULL)
2974 {
2975 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002976 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2977 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002978 free_string_option(cmdmod.save_ei);
2979 }
2980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981
2982 cmdmod = save_cmdmod;
2983
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002984 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 {
2986 /* messages could be enabled for a serious error, need to check if the
2987 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002988 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002989 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 emsg_silent -= did_esilent;
2991 if (emsg_silent < 0)
2992 emsg_silent = 0;
2993 /* Restore msg_scroll, it's set by file I/O commands, even when no
2994 * message is actually displayed. */
2995 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002996
2997 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2998 * somewhere in the line. Put it back in the first column. */
2999 if (redirecting())
3000 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 }
3002
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003003#ifdef HAVE_SANDBOX
3004 if (did_sandbox)
3005 --sandbox;
3006#endif
3007
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3009 ea.nextcmd = NULL;
3010
3011#ifdef FEAT_EVAL
3012 --ex_nesting_level;
3013#endif
3014
3015 return ea.nextcmd;
3016}
3017#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003018 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019#endif
3020
3021/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003022 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 * If there is a match advance "pp" to the argument and return TRUE.
3024 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003025 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003026checkforcmd(
3027 char_u **pp, /* start of command */
3028 char *cmd, /* name of command */
3029 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030{
3031 int i;
3032
3033 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003034 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 break;
3036 if (i >= len && !isalpha((*pp)[i]))
3037 {
3038 *pp = skipwhite(*pp + i);
3039 return TRUE;
3040 }
3041 return FALSE;
3042}
3043
3044/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003045 * Append "cmd" to the error message in IObuff.
3046 * Takes care of limiting the length and handling 0xa0, which would be
3047 * invisible otherwise.
3048 */
3049 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003050append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003051{
3052 char_u *s = cmd;
3053 char_u *d;
3054
3055 STRCAT(IObuff, ": ");
3056 d = IObuff + STRLEN(IObuff);
3057 while (*s != NUL && d - IObuff < IOSIZE - 7)
3058 {
3059 if (
3060#ifdef FEAT_MBYTE
3061 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3062#endif
3063 *s == 0xa0)
3064 {
3065 s +=
3066#ifdef FEAT_MBYTE
3067 enc_utf8 ? 2 :
3068#endif
3069 1;
3070 STRCPY(d, "<a0>");
3071 d += 4;
3072 }
3073 else
3074 MB_COPY_CHAR(s, d);
3075 }
3076 *d = NUL;
3077}
3078
3079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003081 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003083 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 * Returns NULL for an ambiguous user command.
3085 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003087find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088{
3089 int len;
3090 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003091 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092
3093 /*
3094 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003095 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 * - the 'k' command can directly be followed by any character.
3097 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003098 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003100 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 */
3102 p = eap->cmd;
3103 if (*p == 'k')
3104 {
3105 eap->cmdidx = CMD_k;
3106 ++p;
3107 }
3108 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003109 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3110 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 || p[1] == 'g'
3112 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3113 || p[1] == 'I'
3114 || (p[1] == 'r' && p[2] != 'e')))
3115 {
3116 eap->cmdidx = CMD_substitute;
3117 ++p;
3118 }
3119 else
3120 {
3121 while (ASCII_ISALPHA(*p))
3122 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003123 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003124 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003125 while (ASCII_ISALNUM(*p))
3126 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003127
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 /* check for non-alpha command */
3129 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3130 ++p;
3131 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003132 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3133 {
3134 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3135 * :delete with the 'l' flag. Same for 'p'. */
3136 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003137 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003138 break;
3139 if (i == len - 1)
3140 {
3141 --len;
3142 if (p[-1] == 'l')
3143 eap->flags |= EXFLAG_LIST;
3144 else
3145 eap->flags |= EXFLAG_PRINT;
3146 }
3147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148
3149 if (ASCII_ISLOWER(*eap->cmd))
3150 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
3151 else
3152 eap->cmdidx = cmdidxs[26];
3153
3154 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3155 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3156 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3157 (size_t)len) == 0)
3158 {
3159#ifdef FEAT_EVAL
3160 if (full != NULL
3161 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3162 *full = TRUE;
3163#endif
3164 break;
3165 }
3166
3167#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003168 /* Look for a user defined command as a last resort. Let ":Print" be
3169 * overruled by a user defined command. */
3170 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3171 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003173 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 while (ASCII_ISALNUM(*p))
3175 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003176 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 }
3178#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003179 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 eap->cmdidx = CMD_SIZE;
3181 }
3182
3183 return p;
3184}
3185
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003186#ifdef FEAT_USR_CMDS
3187/*
3188 * Search for a user command that matches "eap->cmd".
3189 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3190 * Return a pointer to just after the command.
3191 * Return NULL if there is no matching command.
3192 */
3193 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003194find_ucmd(
3195 exarg_T *eap,
3196 char_u *p, /* end of the command (possibly including count) */
3197 int *full, /* set to TRUE for a full match */
3198 expand_T *xp, /* used for completion, NULL otherwise */
3199 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003200{
3201 int len = (int)(p - eap->cmd);
3202 int j, k, matchlen = 0;
3203 ucmd_T *uc;
3204 int found = FALSE;
3205 int possible = FALSE;
3206 char_u *cp, *np; /* Point into typed cmd and test name */
3207 garray_T *gap;
3208 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3209 only full match global is accepted. */
3210
3211 /*
3212 * Look for buffer-local user commands first, then global ones.
3213 */
3214 gap = &curbuf->b_ucmds;
3215 for (;;)
3216 {
3217 for (j = 0; j < gap->ga_len; ++j)
3218 {
3219 uc = USER_CMD_GA(gap, j);
3220 cp = eap->cmd;
3221 np = uc->uc_name;
3222 k = 0;
3223 while (k < len && *np != NUL && *cp++ == *np++)
3224 k++;
3225 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3226 {
3227 /* If finding a second match, the command is ambiguous. But
3228 * not if a buffer-local command wasn't a full match and a
3229 * global command is a full match. */
3230 if (k == len && found && *np != NUL)
3231 {
3232 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003233 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003234 amb_local = TRUE;
3235 }
3236
3237 if (!found || (k == len && *np == NUL))
3238 {
3239 /* If we matched up to a digit, then there could
3240 * be another command including the digit that we
3241 * should use instead.
3242 */
3243 if (k == len)
3244 found = TRUE;
3245 else
3246 possible = TRUE;
3247
3248 if (gap == &ucmds)
3249 eap->cmdidx = CMD_USER;
3250 else
3251 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003252 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003253 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003254 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003255
3256# ifdef FEAT_CMDL_COMPL
3257 if (compl != NULL)
3258 *compl = uc->uc_compl;
3259# ifdef FEAT_EVAL
3260 if (xp != NULL)
3261 {
3262 xp->xp_arg = uc->uc_compl_arg;
3263 xp->xp_scriptID = uc->uc_scriptID;
3264 }
3265# endif
3266# endif
3267 /* Do not search for further abbreviations
3268 * if this is an exact match. */
3269 matchlen = k;
3270 if (k == len && *np == NUL)
3271 {
3272 if (full != NULL)
3273 *full = TRUE;
3274 amb_local = FALSE;
3275 break;
3276 }
3277 }
3278 }
3279 }
3280
3281 /* Stop if we found a full match or searched all. */
3282 if (j < gap->ga_len || gap == &ucmds)
3283 break;
3284 gap = &ucmds;
3285 }
3286
3287 /* Only found ambiguous matches. */
3288 if (amb_local)
3289 {
3290 if (xp != NULL)
3291 xp->xp_context = EXPAND_UNSUCCESSFUL;
3292 return NULL;
3293 }
3294
3295 /* The match we found may be followed immediately by a number. Move "p"
3296 * back to point to it. */
3297 if (found || possible)
3298 return p + (matchlen - len);
3299 return p;
3300}
3301#endif
3302
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003304static struct cmdmod
3305{
3306 char *name;
3307 int minlen;
3308 int has_count; /* :123verbose :3tab */
3309} cmdmods[] = {
3310 {"aboveleft", 3, FALSE},
3311 {"belowright", 3, FALSE},
3312 {"botright", 2, FALSE},
3313 {"browse", 3, FALSE},
3314 {"confirm", 4, FALSE},
3315 {"hide", 3, FALSE},
3316 {"keepalt", 5, FALSE},
3317 {"keepjumps", 5, FALSE},
3318 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003319 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003320 {"leftabove", 5, FALSE},
3321 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003322 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003323 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003324 {"rightbelow", 6, FALSE},
3325 {"sandbox", 3, FALSE},
3326 {"silent", 3, FALSE},
3327 {"tab", 3, TRUE},
3328 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003329 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003330 {"verbose", 4, TRUE},
3331 {"vertical", 4, FALSE},
3332};
3333
3334/*
3335 * Return length of a command modifier (including optional count).
3336 * Return zero when it's not a modifier.
3337 */
3338 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003339modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003340{
3341 int i, j;
3342 char_u *p = cmd;
3343
3344 if (VIM_ISDIGIT(*cmd))
3345 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003346 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003347 {
3348 for (j = 0; p[j] != NUL; ++j)
3349 if (p[j] != cmdmods[i].name[j])
3350 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003351 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003352 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003353 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003354 }
3355 return 0;
3356}
3357
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358/*
3359 * Return > 0 if an Ex command "name" exists.
3360 * Return 2 if there is an exact match.
3361 * Return 3 if there is an ambiguous match.
3362 */
3363 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003364cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365{
3366 exarg_T ea;
3367 int full = FALSE;
3368 int i;
3369 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003370 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371
3372 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003373 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 {
3375 for (j = 0; name[j] != NUL; ++j)
3376 if (name[j] != cmdmods[i].name[j])
3377 break;
3378 if (name[j] == NUL && j >= cmdmods[i].minlen)
3379 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3380 }
3381
Bram Moolenaara9587612006-05-04 21:47:50 +00003382 /* Check built-in commands and user defined commands.
3383 * For ":2match" and ":3match" we need to skip the number. */
3384 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003386 p = find_command(&ea, &full);
3387 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003389 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3390 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003391 if (*skipwhite(p) != NUL)
3392 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3394}
3395#endif
3396
3397/*
3398 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3399 * we don't need/want deleted. Maybe this could be done better if we didn't
3400 * repeat all this stuff. The only problem is that they may not stay
3401 * perfectly compatible with each other, but then the command line syntax
3402 * probably won't change that much -- webb.
3403 */
3404 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003405set_one_cmd_context(
3406 expand_T *xp,
3407 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408{
3409 char_u *p;
3410 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003411 int len = 0;
3412 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3414 int compl = EXPAND_NOTHING;
3415#endif
3416#ifdef FEAT_CMDL_COMPL
3417 int delim;
3418#endif
3419 int forceit = FALSE;
3420 int usefilter = FALSE; /* filter instead of file name */
3421
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003422 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 xp->xp_pattern = buff;
3424 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003425 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426
3427/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003428 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 */
3430 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3431 ;
3432 xp->xp_pattern = cmd;
3433
3434 if (*cmd == NUL)
3435 return NULL;
3436 if (*cmd == '"') /* ignore comment lines */
3437 {
3438 xp->xp_context = EXPAND_NOTHING;
3439 return NULL;
3440 }
3441
3442/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003443 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 */
3445 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 xp->xp_pattern = cmd;
3447 if (*cmd == NUL)
3448 return NULL;
3449 if (*cmd == '"')
3450 {
3451 xp->xp_context = EXPAND_NOTHING;
3452 return NULL;
3453 }
3454
3455 if (*cmd == '|' || *cmd == '\n')
3456 return cmd + 1; /* There's another command */
3457
3458 /*
3459 * Isolate the command and search for it in the command table.
3460 * Exceptions:
3461 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003462 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3464 */
3465 if (*cmd == 'k' && cmd[1] != 'e')
3466 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003467 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 p = cmd + 1;
3469 }
3470 else
3471 {
3472 p = cmd;
3473 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3474 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003475 /* a user command may contain digits */
3476 if (ASCII_ISUPPER(cmd[0]))
3477 while (ASCII_ISALNUM(*p) || *p == '*')
3478 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003479 /* for python 3.x: ":py3*" commands completion */
3480 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003481 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003482 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003483 while (ASCII_ISALPHA(*p) || *p == '*')
3484 ++p;
3485 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003486 /* check for non-alpha command */
3487 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3488 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003489 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003491 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 {
3493 xp->xp_context = EXPAND_UNSUCCESSFUL;
3494 return NULL;
3495 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003496 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003497 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3498 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3499 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 break;
3501
3502#ifdef FEAT_USR_CMDS
3503 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3505 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506#endif
3507 }
3508
3509 /*
3510 * If the cursor is touching the command, and it ends in an alpha-numeric
3511 * character, complete the command name.
3512 */
3513 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3514 return NULL;
3515
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003516 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 {
3518 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3519 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003520 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 p = cmd + 1;
3522 }
3523#ifdef FEAT_USR_CMDS
3524 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3525 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003526 ea.cmd = cmd;
3527 p = find_ucmd(&ea, p, NULL, xp,
3528# if defined(FEAT_CMDL_COMPL)
3529 &compl
3530# else
3531 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003533 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003534 if (p == NULL)
3535 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 }
3537#endif
3538 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003539 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 {
3541 /* Not still touching the command and it was an illegal one */
3542 xp->xp_context = EXPAND_UNSUCCESSFUL;
3543 return NULL;
3544 }
3545
3546 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3547
3548 if (*p == '!') /* forced commands */
3549 {
3550 forceit = TRUE;
3551 ++p;
3552 }
3553
3554/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003555 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003557 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003558 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559
3560 arg = skipwhite(p);
3561
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003562 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 {
3564 if (*arg == '>') /* append */
3565 {
3566 if (*++arg == '>')
3567 ++arg;
3568 arg = skipwhite(arg);
3569 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003570 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
3572 ++arg;
3573 usefilter = TRUE;
3574 }
3575 }
3576
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003577 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 {
3579 usefilter = forceit; /* :r! filter if forced */
3580 if (*arg == '!') /* :r !filter */
3581 {
3582 ++arg;
3583 usefilter = TRUE;
3584 }
3585 }
3586
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003587 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 {
3589 while (*arg == *cmd) /* allow any number of '>' or '<' */
3590 ++arg;
3591 arg = skipwhite(arg);
3592 }
3593
3594 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003595 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 {
3597 /* Check if we're in the +command */
3598 p = arg + 1;
3599 arg = skip_cmd_arg(arg, FALSE);
3600
3601 /* Still touching the command after '+'? */
3602 if (*arg == NUL)
3603 return p;
3604
3605 /* Skip space(s) after +command to get to the real argument */
3606 arg = skipwhite(arg);
3607 }
3608
3609 /*
3610 * Check for '|' to separate commands and '"' to start comments.
3611 * Don't do this for ":read !cmd" and ":write !cmd".
3612 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003613 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 {
3615 p = arg;
3616 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003617 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 p += 2;
3619 while (*p)
3620 {
3621 if (*p == Ctrl_V)
3622 {
3623 if (p[1] != NUL)
3624 ++p;
3625 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003626 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 || *p == '|' || *p == '\n')
3628 {
3629 if (*(p - 1) != '\\')
3630 {
3631 if (*p == '|' || *p == '\n')
3632 return p + 1;
3633 return NULL; /* It's a comment */
3634 }
3635 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003636 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 }
3638 }
3639
3640 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003641 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 vim_strchr((char_u *)"|\"", *arg) == NULL)
3643 return NULL;
3644
3645 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003646 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003648 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003649 while (*p && p < buff + len)
3650 {
3651 if (*p == ' ' || *p == TAB)
3652 {
3653 /* argument starts after a space */
3654 xp->xp_pattern = ++p;
3655 }
3656 else
3657 {
3658 if (*p == '\\' && *(p + 1) != NUL)
3659 ++p; /* skip over escaped character */
3660 mb_ptr_adv(p);
3661 }
3662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003664 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003666 int c;
3667 int in_quote = FALSE;
3668 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669
3670 /*
3671 * Allow spaces within back-quotes to count as part of the argument
3672 * being expanded.
3673 */
3674 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003675 p = xp->xp_pattern;
3676 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003678#ifdef FEAT_MBYTE
3679 if (has_mbyte)
3680 c = mb_ptr2char(p);
3681 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003683 c = *p;
3684 if (c == '\\' && p[1] != NUL)
3685 ++p;
3686 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 {
3688 if (!in_quote)
3689 {
3690 xp->xp_pattern = p;
3691 bow = p + 1;
3692 }
3693 in_quote = !in_quote;
3694 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003695 /* An argument can contain just about everything, except
3696 * characters that end the command and white space. */
3697 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003698#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003699 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003700#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003701 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003702 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003703 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003704 while (*p != NUL)
3705 {
3706#ifdef FEAT_MBYTE
3707 if (has_mbyte)
3708 c = mb_ptr2char(p);
3709 else
3710#endif
3711 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003712 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003713 break;
3714#ifdef FEAT_MBYTE
3715 if (has_mbyte)
3716 len = (*mb_ptr2len)(p);
3717 else
3718#endif
3719 len = 1;
3720 mb_ptr_adv(p);
3721 }
3722 if (in_quote)
3723 bow = p;
3724 else
3725 xp->xp_pattern = p;
3726 p -= len;
3727 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003728 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 }
3730
3731 /*
3732 * If we are still inside the quotes, and we passed a space, just
3733 * expand from there.
3734 */
3735 if (bow != NULL && in_quote)
3736 xp->xp_pattern = bow;
3737 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003738
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003739 /* For a shell command more chars need to be escaped. */
3740 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003741 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003742#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003743 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003744#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003745 /* When still after the command name expand executables. */
3746 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003747 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749
3750 /* Check for environment variable */
3751 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003752#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 || *xp->xp_pattern == '%'
3754#endif
3755 )
3756 {
3757 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3758 if (!vim_isIDc(*p))
3759 break;
3760 if (*p == NUL)
3761 {
3762 xp->xp_context = EXPAND_ENV_VARS;
3763 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003764#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3765 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003766 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003767 compl = EXPAND_ENV_VARS;
3768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 }
3770 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003771#if defined(FEAT_CMDL_COMPL)
3772 /* Check for user names */
3773 if (*xp->xp_pattern == '~')
3774 {
3775 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3776 ;
3777 /* Complete ~user only if it partially matches a user name.
3778 * A full match ~user<Tab> will be replaced by user's home
3779 * directory i.e. something like ~user<Tab> -> /home/user/ */
3780 if (*p == NUL && p > xp->xp_pattern + 1
3781 && match_user(xp->xp_pattern + 1) == 1)
3782 {
3783 xp->xp_context = EXPAND_USER;
3784 ++xp->xp_pattern;
3785 }
3786 }
3787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 }
3789
3790/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003791 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003793 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003795 case CMD_find:
3796 case CMD_sfind:
3797 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003798 if (xp->xp_context == EXPAND_FILES)
3799 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003800 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 case CMD_cd:
3802 case CMD_chdir:
3803 case CMD_lcd:
3804 case CMD_lchdir:
3805 if (xp->xp_context == EXPAND_FILES)
3806 xp->xp_context = EXPAND_DIRECTORIES;
3807 break;
3808 case CMD_help:
3809 xp->xp_context = EXPAND_HELP;
3810 xp->xp_pattern = arg;
3811 break;
3812
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003813 /* Command modifiers: return the argument.
3814 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003816 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 case CMD_belowright:
3818 case CMD_botright:
3819 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003820 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003821 case CMD_cdo:
3822 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003824 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 case CMD_folddoclosed:
3826 case CMD_folddoopen:
3827 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003828 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 case CMD_keepjumps:
3830 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003831 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003832 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003834 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003836 case CMD_noautocmd:
3837 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003839 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003841 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003842 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 case CMD_topleft:
3844 case CMD_verbose:
3845 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003846 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 return arg;
3848
Bram Moolenaar4f688582007-07-24 12:34:30 +00003849#ifdef FEAT_CMDL_COMPL
3850# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 case CMD_match:
3852 if (*arg == NUL || !ends_excmd(*arg))
3853 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003854 /* also complete "None" */
3855 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 arg = skipwhite(skiptowhite(arg));
3857 if (*arg != NUL)
3858 {
3859 xp->xp_context = EXPAND_NOTHING;
3860 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3861 }
3862 }
3863 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003864# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866/*
3867 * All completion for the +cmdline_compl feature goes here.
3868 */
3869
3870# ifdef FEAT_USR_CMDS
3871 case CMD_command:
3872 /* Check for attributes */
3873 while (*arg == '-')
3874 {
3875 arg++; /* Skip "-" */
3876 p = skiptowhite(arg);
3877 if (*p == NUL)
3878 {
3879 /* Cursor is still in the attribute */
3880 p = vim_strchr(arg, '=');
3881 if (p == NULL)
3882 {
3883 /* No "=", so complete attribute names */
3884 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3885 xp->xp_pattern = arg;
3886 return NULL;
3887 }
3888
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003889 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 * their arguments as well.
3891 */
3892 if (STRNICMP(arg, "complete", p - arg) == 0)
3893 {
3894 xp->xp_context = EXPAND_USER_COMPLETE;
3895 xp->xp_pattern = p + 1;
3896 return NULL;
3897 }
3898 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3899 {
3900 xp->xp_context = EXPAND_USER_NARGS;
3901 xp->xp_pattern = p + 1;
3902 return NULL;
3903 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003904 else if (STRNICMP(arg, "addr", p - arg) == 0)
3905 {
3906 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3907 xp->xp_pattern = p + 1;
3908 return NULL;
3909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 return NULL;
3911 }
3912 arg = skipwhite(p);
3913 }
3914
3915 /* After the attributes comes the new command name */
3916 p = skiptowhite(arg);
3917 if (*p == NUL)
3918 {
3919 xp->xp_context = EXPAND_USER_COMMANDS;
3920 xp->xp_pattern = arg;
3921 break;
3922 }
3923
3924 /* And finally comes a normal command */
3925 return skipwhite(p);
3926
3927 case CMD_delcommand:
3928 xp->xp_context = EXPAND_USER_COMMANDS;
3929 xp->xp_pattern = arg;
3930 break;
3931# endif
3932
3933 case CMD_global:
3934 case CMD_vglobal:
3935 delim = *arg; /* get the delimiter */
3936 if (delim)
3937 ++arg; /* skip delimiter if there is one */
3938
3939 while (arg[0] != NUL && arg[0] != delim)
3940 {
3941 if (arg[0] == '\\' && arg[1] != NUL)
3942 ++arg;
3943 ++arg;
3944 }
3945 if (arg[0] != NUL)
3946 return arg + 1;
3947 break;
3948 case CMD_and:
3949 case CMD_substitute:
3950 delim = *arg;
3951 if (delim)
3952 {
3953 /* skip "from" part */
3954 ++arg;
3955 arg = skip_regexp(arg, delim, p_magic, NULL);
3956 }
3957 /* skip "to" part */
3958 while (arg[0] != NUL && arg[0] != delim)
3959 {
3960 if (arg[0] == '\\' && arg[1] != NUL)
3961 ++arg;
3962 ++arg;
3963 }
3964 if (arg[0] != NUL) /* skip delimiter */
3965 ++arg;
3966 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3967 ++arg;
3968 if (arg[0] != NUL)
3969 return arg;
3970 break;
3971 case CMD_isearch:
3972 case CMD_dsearch:
3973 case CMD_ilist:
3974 case CMD_dlist:
3975 case CMD_ijump:
3976 case CMD_psearch:
3977 case CMD_djump:
3978 case CMD_isplit:
3979 case CMD_dsplit:
3980 arg = skipwhite(skipdigits(arg)); /* skip count */
3981 if (*arg == '/') /* Match regexp, not just whole words */
3982 {
3983 for (++arg; *arg && *arg != '/'; arg++)
3984 if (*arg == '\\' && arg[1] != NUL)
3985 arg++;
3986 if (*arg)
3987 {
3988 arg = skipwhite(arg + 1);
3989
3990 /* Check for trailing illegal characters */
3991 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3992 xp->xp_context = EXPAND_NOTHING;
3993 else
3994 return arg;
3995 }
3996 }
3997 break;
3998#ifdef FEAT_AUTOCMD
3999 case CMD_autocmd:
4000 return set_context_in_autocmd(xp, arg, FALSE);
4001
4002 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004003 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 return set_context_in_autocmd(xp, arg, TRUE);
4005#endif
4006 case CMD_set:
4007 set_context_in_set_cmd(xp, arg, 0);
4008 break;
4009 case CMD_setglobal:
4010 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4011 break;
4012 case CMD_setlocal:
4013 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4014 break;
4015 case CMD_tag:
4016 case CMD_stag:
4017 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004018 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 case CMD_tselect:
4020 case CMD_stselect:
4021 case CMD_ptselect:
4022 case CMD_tjump:
4023 case CMD_stjump:
4024 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004025 if (*p_wop != NUL)
4026 xp->xp_context = EXPAND_TAGS_LISTFILES;
4027 else
4028 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 xp->xp_pattern = arg;
4030 break;
4031 case CMD_augroup:
4032 xp->xp_context = EXPAND_AUGROUP;
4033 xp->xp_pattern = arg;
4034 break;
4035#ifdef FEAT_SYN_HL
4036 case CMD_syntax:
4037 set_context_in_syntax_cmd(xp, arg);
4038 break;
4039#endif
4040#ifdef FEAT_EVAL
4041 case CMD_let:
4042 case CMD_if:
4043 case CMD_elseif:
4044 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004045 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 case CMD_echo:
4047 case CMD_echon:
4048 case CMD_execute:
4049 case CMD_echomsg:
4050 case CMD_echoerr:
4051 case CMD_call:
4052 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004053 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 break;
4055
4056 case CMD_unlet:
4057 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4058 arg = xp->xp_pattern + 1;
4059 xp->xp_context = EXPAND_USER_VARS;
4060 xp->xp_pattern = arg;
4061 break;
4062
4063 case CMD_function:
4064 case CMD_delfunction:
4065 xp->xp_context = EXPAND_USER_FUNC;
4066 xp->xp_pattern = arg;
4067 break;
4068
4069 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004070 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 break;
4072#endif
4073 case CMD_highlight:
4074 set_context_in_highlight_cmd(xp, arg);
4075 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004076#ifdef FEAT_CSCOPE
4077 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004078 case CMD_lcscope:
4079 case CMD_scscope:
4080 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004081 break;
4082#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004083#ifdef FEAT_SIGNS
4084 case CMD_sign:
4085 set_context_in_sign_cmd(xp, arg);
4086 break;
4087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088#ifdef FEAT_LISTCMDS
4089 case CMD_bdelete:
4090 case CMD_bwipeout:
4091 case CMD_bunload:
4092 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4093 arg = xp->xp_pattern + 1;
4094 /*FALLTHROUGH*/
4095 case CMD_buffer:
4096 case CMD_sbuffer:
4097 case CMD_checktime:
4098 xp->xp_context = EXPAND_BUFFERS;
4099 xp->xp_pattern = arg;
4100 break;
4101#endif
4102#ifdef FEAT_USR_CMDS
4103 case CMD_USER:
4104 case CMD_USER_BUF:
4105 if (compl != EXPAND_NOTHING)
4106 {
4107 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004108 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 {
4110# ifdef FEAT_MENU
4111 if (compl == EXPAND_MENUS)
4112 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4113# endif
4114 if (compl == EXPAND_COMMANDS)
4115 return arg;
4116 if (compl == EXPAND_MAPPINGS)
4117 return set_context_in_map_cmd(xp, (char_u *)"map",
4118 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004119 /* Find start of last argument. */
4120 p = arg;
4121 while (*p)
4122 {
4123 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004124 /* argument starts after a space */
4125 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004126 else if (*p == '\\' && *(p + 1) != NUL)
4127 ++p; /* skip over escaped character */
4128 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 xp->xp_pattern = arg;
4131 }
4132 xp->xp_context = compl;
4133 }
4134 break;
4135#endif
4136 case CMD_map: case CMD_noremap:
4137 case CMD_nmap: case CMD_nnoremap:
4138 case CMD_vmap: case CMD_vnoremap:
4139 case CMD_omap: case CMD_onoremap:
4140 case CMD_imap: case CMD_inoremap:
4141 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004142 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004143 case CMD_smap: case CMD_snoremap:
4144 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004146 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 case CMD_unmap:
4148 case CMD_nunmap:
4149 case CMD_vunmap:
4150 case CMD_ounmap:
4151 case CMD_iunmap:
4152 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004153 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004154 case CMD_sunmap:
4155 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004157 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 case CMD_abbreviate: case CMD_noreabbrev:
4159 case CMD_cabbrev: case CMD_cnoreabbrev:
4160 case CMD_iabbrev: case CMD_inoreabbrev:
4161 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004162 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 case CMD_unabbreviate:
4164 case CMD_cunabbrev:
4165 case CMD_iunabbrev:
4166 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004167 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168#ifdef FEAT_MENU
4169 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4170 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4171 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4172 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4173 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4174 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4175 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4176 case CMD_tmenu: case CMD_tunmenu:
4177 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4178 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4179#endif
4180
4181 case CMD_colorscheme:
4182 xp->xp_context = EXPAND_COLORS;
4183 xp->xp_pattern = arg;
4184 break;
4185
4186 case CMD_compiler:
4187 xp->xp_context = EXPAND_COMPILER;
4188 xp->xp_pattern = arg;
4189 break;
4190
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004191 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004192 xp->xp_context = EXPAND_OWNSYNTAX;
4193 xp->xp_pattern = arg;
4194 break;
4195
4196 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004197 xp->xp_context = EXPAND_FILETYPE;
4198 xp->xp_pattern = arg;
4199 break;
4200
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4202 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4203 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004204 p = skiptowhite(arg);
4205 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 {
4207 xp->xp_context = EXPAND_LANGUAGE;
4208 xp->xp_pattern = arg;
4209 }
4210 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004211 {
4212 if ( STRNCMP(arg, "messages", p - arg) == 0
4213 || STRNCMP(arg, "ctype", p - arg) == 0
4214 || STRNCMP(arg, "time", p - arg) == 0)
4215 {
4216 xp->xp_context = EXPAND_LOCALES;
4217 xp->xp_pattern = skipwhite(p);
4218 }
4219 else
4220 xp->xp_context = EXPAND_NOTHING;
4221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 break;
4223#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004224#if defined(FEAT_PROFILE)
4225 case CMD_profile:
4226 set_context_in_profile_cmd(xp, arg);
4227 break;
4228#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004229 case CMD_behave:
4230 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004231 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004232 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004234#if defined(FEAT_CMDHIST)
4235 case CMD_history:
4236 xp->xp_context = EXPAND_HISTORY;
4237 xp->xp_pattern = arg;
4238 break;
4239#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004240#if defined(FEAT_PROFILE)
4241 case CMD_syntime:
4242 xp->xp_context = EXPAND_SYNTIME;
4243 xp->xp_pattern = arg;
4244 break;
4245#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004246
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247#endif /* FEAT_CMDL_COMPL */
4248
4249 default:
4250 break;
4251 }
4252 return NULL;
4253}
4254
4255/*
4256 * skip a range specifier of the form: addr [,addr] [;addr] ..
4257 *
4258 * Backslashed delimiters after / or ? will be skipped, and commands will
4259 * not be expanded between /'s and ?'s or after "'".
4260 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004261 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 * Returns the "cmd" pointer advanced to beyond the range.
4263 */
4264 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004265skip_range(
4266 char_u *cmd,
4267 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004269 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270
Bram Moolenaardf177f62005-02-22 08:39:57 +00004271 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 {
4273 if (*cmd == '\'')
4274 {
4275 if (*++cmd == NUL && ctx != NULL)
4276 *ctx = EXPAND_NOTHING;
4277 }
4278 else if (*cmd == '/' || *cmd == '?')
4279 {
4280 delim = *cmd++;
4281 while (*cmd != NUL && *cmd != delim)
4282 if (*cmd++ == '\\' && *cmd != NUL)
4283 ++cmd;
4284 if (*cmd == NUL && ctx != NULL)
4285 *ctx = EXPAND_NOTHING;
4286 }
4287 if (*cmd != NUL)
4288 ++cmd;
4289 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004290
4291 /* Skip ":" and white space. */
4292 while (*cmd == ':')
4293 cmd = skipwhite(cmd + 1);
4294
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 return cmd;
4296}
4297
4298/*
4299 * get a single EX address
4300 *
4301 * Set ptr to the next character after the part that was interpreted.
4302 * Set ptr to NULL when an error is encountered.
4303 *
4304 * Return MAXLNUM when no Ex address was found.
4305 */
4306 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004307get_address(
4308 exarg_T *eap UNUSED,
4309 char_u **ptr,
4310 int addr_type, /* flag: one of ADDR_LINES, ... */
4311 int skip, /* only skip the address, don't use it */
4312 int to_other_file) /* flag: may jump to other file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313{
4314 int c;
4315 int i;
4316 long n;
4317 char_u *cmd;
4318 pos_T pos;
4319 pos_T *fp;
4320 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004321 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
4323 cmd = skipwhite(*ptr);
4324 lnum = MAXLNUM;
4325 do
4326 {
4327 switch (*cmd)
4328 {
4329 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004330 ++cmd;
4331 switch (addr_type)
4332 {
4333 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 lnum = curwin->w_cursor.lnum;
4335 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004336 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004337 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004338 break;
4339 case ADDR_ARGUMENTS:
4340 lnum = curwin->w_arg_idx + 1;
4341 break;
4342 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004343 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004344 lnum = curbuf->b_fnum;
4345 break;
4346 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004347 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004348 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004349#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004350 case ADDR_QUICKFIX:
4351 lnum = qf_get_cur_valid_idx(eap);
4352 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004353#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004354 }
4355 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356
4357 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004358 ++cmd;
4359 switch (addr_type)
4360 {
4361 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 lnum = curbuf->b_ml.ml_line_count;
4363 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004364 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004365 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004366 break;
4367 case ADDR_ARGUMENTS:
4368 lnum = ARGCOUNT;
4369 break;
4370 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004371 buf = lastbuf;
4372 while (buf->b_ml.ml_mfp == NULL)
4373 {
4374 if (buf->b_prev == NULL)
4375 break;
4376 buf = buf->b_prev;
4377 }
4378 lnum = buf->b_fnum;
4379 break;
4380 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004381 lnum = lastbuf->b_fnum;
4382 break;
4383 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004384 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004385 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004386#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004387 case ADDR_QUICKFIX:
4388 lnum = qf_get_size(eap);
4389 if (lnum == 0)
4390 lnum = 1;
4391 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004392#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004393 }
4394 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395
4396 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004397 if (*++cmd == NUL)
4398 {
4399 cmd = NULL;
4400 goto error;
4401 }
4402 if (addr_type != ADDR_LINES)
4403 {
4404 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004405 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004406 goto error;
4407 }
4408 if (skip)
4409 ++cmd;
4410 else
4411 {
4412 /* Only accept a mark in another file when it is
4413 * used by itself: ":'M". */
4414 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4415 ++cmd;
4416 if (fp == (pos_T *)-1)
4417 /* Jumped to another file. */
4418 lnum = curwin->w_cursor.lnum;
4419 else
4420 {
4421 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 {
4423 cmd = NULL;
4424 goto error;
4425 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004426 lnum = fp->lnum;
4427 }
4428 }
4429 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430
4431 case '/':
4432 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004433 c = *cmd++;
4434 if (addr_type != ADDR_LINES)
4435 {
4436 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004437 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004438 goto error;
4439 }
4440 if (skip) /* skip "/pat/" */
4441 {
4442 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4443 if (*cmd == c)
4444 ++cmd;
4445 }
4446 else
4447 {
4448 pos = curwin->w_cursor; /* save curwin->w_cursor */
4449 /*
4450 * When '/' or '?' follows another address, start
4451 * from there.
4452 */
4453 if (lnum != MAXLNUM)
4454 curwin->w_cursor.lnum = lnum;
4455 /*
4456 * Start a forward search at the end of the line.
4457 * Start a backward search at the start of the line.
4458 * This makes sure we never match in the current
4459 * line, and can match anywhere in the
4460 * next/previous line.
4461 */
4462 if (c == '/')
4463 curwin->w_cursor.col = MAXCOL;
4464 else
4465 curwin->w_cursor.col = 0;
4466 searchcmdlen = 0;
4467 if (!do_search(NULL, c, cmd, 1L,
4468 SEARCH_HIS | SEARCH_MSG, NULL))
4469 {
4470 curwin->w_cursor = pos;
4471 cmd = NULL;
4472 goto error;
4473 }
4474 lnum = curwin->w_cursor.lnum;
4475 curwin->w_cursor = pos;
4476 /* adjust command string pointer */
4477 cmd += searchcmdlen;
4478 }
4479 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480
4481 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004482 ++cmd;
4483 if (addr_type != ADDR_LINES)
4484 {
4485 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004486 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004487 goto error;
4488 }
4489 if (*cmd == '&')
4490 i = RE_SUBST;
4491 else if (*cmd == '?' || *cmd == '/')
4492 i = RE_SEARCH;
4493 else
4494 {
4495 EMSG(_(e_backslash));
4496 cmd = NULL;
4497 goto error;
4498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004500 if (!skip)
4501 {
4502 /*
4503 * When search follows another address, start from
4504 * there.
4505 */
4506 if (lnum != MAXLNUM)
4507 pos.lnum = lnum;
4508 else
4509 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004511 /*
4512 * Start the search just like for the above
4513 * do_search().
4514 */
4515 if (*cmd != '?')
4516 pos.col = MAXCOL;
4517 else
4518 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004519#ifdef FEAT_VIRTUALEDIT
4520 pos.coladd = 0;
4521#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004522 if (searchit(curwin, curbuf, &pos,
4523 *cmd == '?' ? BACKWARD : FORWARD,
4524 (char_u *)"", 1L, SEARCH_MSG,
4525 i, (linenr_T)0, NULL) != FAIL)
4526 lnum = pos.lnum;
4527 else
4528 {
4529 cmd = NULL;
4530 goto error;
4531 }
4532 }
4533 ++cmd;
4534 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535
4536 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004537 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4538 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 }
4540
4541 for (;;)
4542 {
4543 cmd = skipwhite(cmd);
4544 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4545 break;
4546
4547 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004548 {
4549 switch (addr_type)
4550 {
4551 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004552 /* "+1" is same as ".+1" */
4553 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004554 break;
4555 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004556 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004557 break;
4558 case ADDR_ARGUMENTS:
4559 lnum = curwin->w_arg_idx + 1;
4560 break;
4561 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004562 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004563 lnum = curbuf->b_fnum;
4564 break;
4565 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004566 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004567 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004568#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004569 case ADDR_QUICKFIX:
4570 lnum = qf_get_cur_valid_idx(eap);
4571 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004572#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004573 }
4574 }
4575
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 if (VIM_ISDIGIT(*cmd))
4577 i = '+'; /* "number" is same as "+number" */
4578 else
4579 i = *cmd++;
4580 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4581 n = 1;
4582 else
4583 n = getdigits(&cmd);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004584 if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004585 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004586 lnum = compute_buffer_local_count(
4587 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004588 else if (i == '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 lnum -= n;
4590 else
4591 lnum += n;
4592 }
4593 } while (*cmd == '/' || *cmd == '?');
4594
4595error:
4596 *ptr = cmd;
4597 return lnum;
4598}
4599
4600/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004601 * Get flags from an Ex command argument.
4602 */
4603 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004604get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004605{
4606 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4607 {
4608 if (*eap->arg == 'l')
4609 eap->flags |= EXFLAG_LIST;
4610 else if (*eap->arg == 'p')
4611 eap->flags |= EXFLAG_PRINT;
4612 else
4613 eap->flags |= EXFLAG_NR;
4614 eap->arg = skipwhite(eap->arg + 1);
4615 }
4616}
4617
4618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 * Function called for command which is Not Implemented. NI!
4620 */
4621 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004622ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623{
4624 if (!eap->skip)
4625 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4626}
4627
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004628#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629/*
4630 * Function called for script command which is Not Implemented. NI!
4631 * Skips over ":perl <<EOF" constructs.
4632 */
4633 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004634ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635{
4636 if (!eap->skip)
4637 ex_ni(eap);
4638 else
4639 vim_free(script_get(eap, eap->arg));
4640}
4641#endif
4642
4643/*
4644 * Check range in Ex command for validity.
4645 * Return NULL when valid, error message when invalid.
4646 */
4647 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004648invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004650 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 if ( eap->line1 < 0
4652 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004653 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004655
4656 if (eap->argt & RANGE)
4657 {
4658 switch(eap->addr_type)
4659 {
4660 case ADDR_LINES:
4661 if (!(eap->argt & NOTADR)
4662 && eap->line2 > curbuf->b_ml.ml_line_count
4663#ifdef FEAT_DIFF
4664 + (eap->cmdidx == CMD_diffget)
4665#endif
4666 )
4667 return (char_u *)_(e_invrange);
4668 break;
4669 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004670 /* add 1 if ARGCOUNT is 0 */
4671 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004672 return (char_u *)_(e_invrange);
4673 break;
4674 case ADDR_BUFFERS:
4675 if (eap->line1 < firstbuf->b_fnum
4676 || eap->line2 > lastbuf->b_fnum)
4677 return (char_u *)_(e_invrange);
4678 break;
4679 case ADDR_LOADED_BUFFERS:
4680 buf = firstbuf;
4681 while (buf->b_ml.ml_mfp == NULL)
4682 {
4683 if (buf->b_next == NULL)
4684 return (char_u *)_(e_invrange);
4685 buf = buf->b_next;
4686 }
4687 if (eap->line1 < buf->b_fnum)
4688 return (char_u *)_(e_invrange);
4689 buf = lastbuf;
4690 while (buf->b_ml.ml_mfp == NULL)
4691 {
4692 if (buf->b_prev == NULL)
4693 return (char_u *)_(e_invrange);
4694 buf = buf->b_prev;
4695 }
4696 if (eap->line2 > buf->b_fnum)
4697 return (char_u *)_(e_invrange);
4698 break;
4699 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004700 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004701 return (char_u *)_(e_invrange);
4702 break;
4703 case ADDR_TABS:
4704 if (eap->line2 > LAST_TAB_NR)
4705 return (char_u *)_(e_invrange);
4706 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004707#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004708 case ADDR_QUICKFIX:
4709 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4710 return (char_u *)_(e_invrange);
4711 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004712#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004713 }
4714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 return NULL;
4716}
4717
4718/*
4719 * Correct the range for zero line number, if required.
4720 */
4721 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004722correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723{
4724 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4725 {
4726 if (eap->line1 == 0)
4727 eap->line1 = 1;
4728 if (eap->line2 == 0)
4729 eap->line2 = 1;
4730 }
4731}
4732
Bram Moolenaar748bf032005-02-02 23:04:36 +00004733#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004734static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004735
4736/*
4737 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4738 * pattern. Otherwise return eap->arg.
4739 */
4740 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004741skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004742{
4743 char_u *p = eap->arg;
4744
Bram Moolenaara37420f2006-02-04 22:37:47 +00004745 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4746 || eap->cmdidx == CMD_vimgrepadd
4747 || eap->cmdidx == CMD_lvimgrepadd
4748 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004749 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004750 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004751 if (p == NULL)
4752 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004753 }
4754 return p;
4755}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004756
4757/*
4758 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4759 * in the command line, so that things like % get expanded.
4760 */
4761 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004762replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004763{
4764 char_u *new_cmdline;
4765 char_u *program;
4766 char_u *pos;
4767 char_u *ptr;
4768 int len;
4769 int i;
4770
4771 /*
4772 * Don't do it when ":vimgrep" is used for ":grep".
4773 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004774 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4775 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4776 || eap->cmdidx == CMD_grepadd
4777 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004778 && !grep_internal(eap->cmdidx))
4779 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004780 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4781 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004782 {
4783 if (*curbuf->b_p_gp == NUL)
4784 program = p_gp;
4785 else
4786 program = curbuf->b_p_gp;
4787 }
4788 else
4789 {
4790 if (*curbuf->b_p_mp == NUL)
4791 program = p_mp;
4792 else
4793 program = curbuf->b_p_mp;
4794 }
4795
4796 p = skipwhite(p);
4797
4798 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4799 {
4800 /* replace $* by given arguments */
4801 i = 1;
4802 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4803 ++i;
4804 len = (int)STRLEN(p);
4805 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4806 if (new_cmdline == NULL)
4807 return NULL; /* out of memory */
4808 ptr = new_cmdline;
4809 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4810 {
4811 i = (int)(pos - program);
4812 STRNCPY(ptr, program, i);
4813 STRCPY(ptr += i, p);
4814 ptr += len;
4815 program = pos + 2;
4816 }
4817 STRCPY(ptr, program);
4818 }
4819 else
4820 {
4821 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4822 if (new_cmdline == NULL)
4823 return NULL; /* out of memory */
4824 STRCPY(new_cmdline, program);
4825 STRCAT(new_cmdline, " ");
4826 STRCAT(new_cmdline, p);
4827 }
4828 msg_make(p);
4829
4830 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4831 vim_free(*cmdlinep);
4832 *cmdlinep = new_cmdline;
4833 p = new_cmdline;
4834 }
4835 return p;
4836}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004837#endif
4838
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839/*
4840 * Expand file name in Ex command argument.
4841 * Return FAIL for failure, OK otherwise.
4842 */
4843 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004844expand_filename(
4845 exarg_T *eap,
4846 char_u **cmdlinep,
4847 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848{
4849 int has_wildcards; /* need to expand wildcards */
4850 char_u *repl;
4851 int srclen;
4852 char_u *p;
4853 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004854 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855
Bram Moolenaar748bf032005-02-02 23:04:36 +00004856#ifdef FEAT_QUICKFIX
4857 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4858 p = skip_grep_pat(eap);
4859#else
4860 p = eap->arg;
4861#endif
4862
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 /*
4864 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4865 * the file name contains a wildcard it should not cause expanding.
4866 * (it will be expanded anyway if there is a wildcard before replacing).
4867 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004868 has_wildcards = mch_has_wildcard(p);
4869 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004871#ifdef FEAT_EVAL
4872 /* Skip over `=expr`, wildcards in it are not expanded. */
4873 if (p[0] == '`' && p[1] == '=')
4874 {
4875 p += 2;
4876 (void)skip_expr(&p);
4877 if (*p == '`')
4878 ++p;
4879 continue;
4880 }
4881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 /*
4883 * Quick check if this cannot be the start of a special string.
4884 * Also removes backslash before '%', '#' and '<'.
4885 */
4886 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4887 {
4888 ++p;
4889 continue;
4890 }
4891
4892 /*
4893 * Try to find a match at this position.
4894 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004895 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4896 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 if (*errormsgp != NULL) /* error detected */
4898 return FAIL;
4899 if (repl == NULL) /* no match found */
4900 {
4901 p += srclen;
4902 continue;
4903 }
4904
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004905 /* Wildcards won't be expanded below, the replacement is taken
4906 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4907 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4908 {
4909 char_u *l = repl;
4910
4911 repl = expand_env_save(repl);
4912 vim_free(l);
4913 }
4914
Bram Moolenaar63b92542007-03-27 14:57:09 +00004915 /* Need to escape white space et al. with a backslash.
4916 * Don't do this for:
4917 * - replacement that already has been escaped: "##"
4918 * - shell commands (may have to use quotes instead).
4919 * - non-unix systems when there is a single argument (spaces don't
4920 * separate arguments then).
4921 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004923 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 && eap->cmdidx != CMD_bang
4925 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004926 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004928 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004930 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931#ifndef UNIX
4932 && !(eap->argt & NOSPC)
4933#endif
4934 )
4935 {
4936 char_u *l;
4937#ifdef BACKSLASH_IN_FILENAME
4938 /* Don't escape a backslash here, because rem_backslash() doesn't
4939 * remove it later. */
4940 static char_u *nobslash = (char_u *)" \t\"|";
4941# define ESCAPE_CHARS nobslash
4942#else
4943# define ESCAPE_CHARS escape_chars
4944#endif
4945
4946 for (l = repl; *l; ++l)
4947 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4948 {
4949 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4950 if (l != NULL)
4951 {
4952 vim_free(repl);
4953 repl = l;
4954 }
4955 break;
4956 }
4957 }
4958
4959 /* For a shell command a '!' must be escaped. */
4960 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004961 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 {
4963 char_u *l;
4964
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004965 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 if (l != NULL)
4967 {
4968 vim_free(repl);
4969 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 }
4971 }
4972
4973 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4974 vim_free(repl);
4975 if (p == NULL)
4976 return FAIL;
4977 }
4978
4979 /*
4980 * One file argument: Expand wildcards.
4981 * Don't do this with ":r !command" or ":w !command".
4982 */
4983 if ((eap->argt & NOSPC) && !eap->usefilter)
4984 {
4985 /*
4986 * May do this twice:
4987 * 1. Replace environment variables.
4988 * 2. Replace any other wildcards, remove backslashes.
4989 */
4990 for (n = 1; n <= 2; ++n)
4991 {
4992 if (n == 2)
4993 {
4994#ifdef UNIX
4995 /*
4996 * Only for Unix we check for more than one file name.
4997 * For other systems spaces are considered to be part
4998 * of the file name.
4999 * Only check here if there is no wildcard, otherwise
5000 * ExpandOne() will check for errors. This allows
5001 * ":e `ls ve*.c`" on Unix.
5002 */
5003 if (!has_wildcards)
5004 for (p = eap->arg; *p; ++p)
5005 {
5006 /* skip escaped characters */
5007 if (p[1] && (*p == '\\' || *p == Ctrl_V))
5008 ++p;
5009 else if (vim_iswhite(*p))
5010 {
5011 *errormsgp = (char_u *)_("E172: Only one file name allowed");
5012 return FAIL;
5013 }
5014 }
5015#endif
5016
5017 /*
5018 * Halve the number of backslashes (this is Vi compatible).
5019 * For Unix and OS/2, when wildcards are expanded, this is
5020 * done by ExpandOne() below.
5021 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005022#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 if (!has_wildcards)
5024#endif
5025 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 }
5027
5028 if (has_wildcards)
5029 {
5030 if (n == 1)
5031 {
5032 /*
5033 * First loop: May expand environment variables. This
5034 * can be done much faster with expand_env() than with
5035 * something else (e.g., calling a shell).
5036 * After expanding environment variables, check again
5037 * if there are still wildcards present.
5038 */
5039 if (vim_strchr(eap->arg, '$') != NULL
5040 || vim_strchr(eap->arg, '~') != NULL)
5041 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005042 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005043 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 has_wildcards = mch_has_wildcard(NameBuff);
5045 p = NameBuff;
5046 }
5047 else
5048 p = NULL;
5049 }
5050 else /* n == 2 */
5051 {
5052 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005053 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054
5055 ExpandInit(&xpc);
5056 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005057 if (p_wic)
5058 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005060 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 if (p == NULL)
5062 return FAIL;
5063 }
5064 if (p != NULL)
5065 {
5066 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5067 p, cmdlinep);
5068 if (n == 2) /* p came from ExpandOne() */
5069 vim_free(p);
5070 }
5071 }
5072 }
5073 }
5074 return OK;
5075}
5076
5077/*
5078 * Replace part of the command line, keeping eap->cmd, eap->arg and
5079 * eap->nextcmd correct.
5080 * "src" points to the part that is to be replaced, of length "srclen".
5081 * "repl" is the replacement string.
5082 * Returns a pointer to the character after the replaced string.
5083 * Returns NULL for failure.
5084 */
5085 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005086repl_cmdline(
5087 exarg_T *eap,
5088 char_u *src,
5089 int srclen,
5090 char_u *repl,
5091 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092{
5093 int len;
5094 int i;
5095 char_u *new_cmdline;
5096
5097 /*
5098 * The new command line is build in new_cmdline[].
5099 * First allocate it.
5100 * Careful: a "+cmd" argument may have been NUL terminated.
5101 */
5102 len = (int)STRLEN(repl);
5103 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005104 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5106 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5107 return NULL; /* out of memory! */
5108
5109 /*
5110 * Copy the stuff before the expanded part.
5111 * Copy the expanded stuff.
5112 * Copy what came after the expanded part.
5113 * Copy the next commands, if there are any.
5114 */
5115 i = (int)(src - *cmdlinep); /* length of part before match */
5116 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005117
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 mch_memmove(new_cmdline + i, repl, (size_t)len);
5119 i += len; /* remember the end of the string */
5120 STRCPY(new_cmdline + i, src + srclen);
5121 src = new_cmdline + i; /* remember where to continue */
5122
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005123 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 {
5125 i = (int)STRLEN(new_cmdline) + 1;
5126 STRCPY(new_cmdline + i, eap->nextcmd);
5127 eap->nextcmd = new_cmdline + i;
5128 }
5129 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5130 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5131 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5132 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5133 vim_free(*cmdlinep);
5134 *cmdlinep = new_cmdline;
5135
5136 return src;
5137}
5138
5139/*
5140 * Check for '|' to separate commands and '"' to start comments.
5141 */
5142 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005143separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144{
5145 char_u *p;
5146
Bram Moolenaar86b68352004-12-27 21:59:20 +00005147#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005148 p = skip_grep_pat(eap);
5149#else
5150 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005151#endif
5152
5153 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 {
5155 if (*p == Ctrl_V)
5156 {
5157 if (eap->argt & (USECTRLV | XFILE))
5158 ++p; /* skip CTRL-V and next char */
5159 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005160 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005161 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 if (*p == NUL) /* stop at NUL after CTRL-V */
5163 break;
5164 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005165
5166#ifdef FEAT_EVAL
5167 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005168 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005169 {
5170 p += 2;
5171 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005172 }
5173#endif
5174
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 /* Check for '"': start of comment or '|': next command */
5176 /* :@" and :*" do not start a comment!
5177 * :redir @" doesn't either. */
5178 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5179 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5180 || p != eap->arg)
5181 && (eap->cmdidx != CMD_redir
5182 || p != eap->arg + 1 || p[-1] != '@'))
5183 || *p == '|' || *p == '\n')
5184 {
5185 /*
5186 * We remove the '\' before the '|', unless USECTRLV is used
5187 * AND 'b' is present in 'cpoptions'.
5188 */
5189 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5190 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5191 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005192 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 --p;
5194 }
5195 else
5196 {
5197 eap->nextcmd = check_nextcmd(p);
5198 *p = NUL;
5199 break;
5200 }
5201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005203
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5205 del_trailing_spaces(eap->arg);
5206}
5207
5208/*
5209 * get + command from ex argument
5210 */
5211 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005212getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213{
5214 char_u *arg = *argp;
5215 char_u *command = NULL;
5216
5217 if (*arg == '+') /* +[command] */
5218 {
5219 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005220 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 command = dollar_command;
5222 else
5223 {
5224 command = arg;
5225 arg = skip_cmd_arg(command, TRUE);
5226 if (*arg != NUL)
5227 *arg++ = NUL; /* terminate command with NUL */
5228 }
5229
5230 arg = skipwhite(arg); /* skip over spaces */
5231 *argp = arg;
5232 }
5233 return command;
5234}
5235
5236/*
5237 * Find end of "+command" argument. Skip over "\ " and "\\".
5238 */
5239 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005240skip_cmd_arg(
5241 char_u *p,
5242 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243{
5244 while (*p && !vim_isspace(*p))
5245 {
5246 if (*p == '\\' && p[1] != NUL)
5247 {
5248 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005249 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 else
5251 ++p;
5252 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005253 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 }
5255 return p;
5256}
5257
5258/*
5259 * Get "++opt=arg" argument.
5260 * Return FAIL or OK.
5261 */
5262 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005263getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264{
5265 char_u *arg = eap->arg + 2;
5266 int *pp = NULL;
5267#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005268 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 char_u *p;
5270#endif
5271
5272 /* ":edit ++[no]bin[ary] file" */
5273 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5274 {
5275 if (*arg == 'n')
5276 {
5277 arg += 2;
5278 eap->force_bin = FORCE_NOBIN;
5279 }
5280 else
5281 eap->force_bin = FORCE_BIN;
5282 if (!checkforcmd(&arg, "binary", 3))
5283 return FAIL;
5284 eap->arg = skipwhite(arg);
5285 return OK;
5286 }
5287
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005288 /* ":read ++edit file" */
5289 if (STRNCMP(arg, "edit", 4) == 0)
5290 {
5291 eap->read_edit = TRUE;
5292 eap->arg = skipwhite(arg + 4);
5293 return OK;
5294 }
5295
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 if (STRNCMP(arg, "ff", 2) == 0)
5297 {
5298 arg += 2;
5299 pp = &eap->force_ff;
5300 }
5301 else if (STRNCMP(arg, "fileformat", 10) == 0)
5302 {
5303 arg += 10;
5304 pp = &eap->force_ff;
5305 }
5306#ifdef FEAT_MBYTE
5307 else if (STRNCMP(arg, "enc", 3) == 0)
5308 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005309 if (STRNCMP(arg, "encoding", 8) == 0)
5310 arg += 8;
5311 else
5312 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 pp = &eap->force_enc;
5314 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005315 else if (STRNCMP(arg, "bad", 3) == 0)
5316 {
5317 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005318 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320#endif
5321
5322 if (pp == NULL || *arg != '=')
5323 return FAIL;
5324
5325 ++arg;
5326 *pp = (int)(arg - eap->cmd);
5327 arg = skip_cmd_arg(arg, FALSE);
5328 eap->arg = skipwhite(arg);
5329 *arg = NUL;
5330
5331#ifdef FEAT_MBYTE
5332 if (pp == &eap->force_ff)
5333 {
5334#endif
5335 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5336 return FAIL;
5337#ifdef FEAT_MBYTE
5338 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005339 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 {
5341 /* Make 'fileencoding' lower case. */
5342 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5343 *p = TOLOWER_ASC(*p);
5344 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005345 else
5346 {
5347 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5348 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005349 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005350 if (STRICMP(p, "keep") == 0)
5351 eap->bad_char = BAD_KEEP;
5352 else if (STRICMP(p, "drop") == 0)
5353 eap->bad_char = BAD_DROP;
5354 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5355 eap->bad_char = *p;
5356 else
5357 return FAIL;
5358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359#endif
5360
5361 return OK;
5362}
5363
5364/*
5365 * ":abbreviate" and friends.
5366 */
5367 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005368ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369{
5370 do_exmap(eap, TRUE); /* almost the same as mapping */
5371}
5372
5373/*
5374 * ":map" and friends.
5375 */
5376 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005377ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378{
5379 /*
5380 * If we are sourcing .exrc or .vimrc in current directory we
5381 * print the mappings for security reasons.
5382 */
5383 if (secure)
5384 {
5385 secure = 2;
5386 msg_outtrans(eap->cmd);
5387 msg_putchar('\n');
5388 }
5389 do_exmap(eap, FALSE);
5390}
5391
5392/*
5393 * ":unmap" and friends.
5394 */
5395 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005396ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397{
5398 do_exmap(eap, FALSE);
5399}
5400
5401/*
5402 * ":mapclear" and friends.
5403 */
5404 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005405ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406{
5407 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5408}
5409
5410/*
5411 * ":abclear" and friends.
5412 */
5413 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005414ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415{
5416 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5417}
5418
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005419#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005421ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422{
5423 /*
5424 * Disallow auto commands from .exrc and .vimrc in current
5425 * directory for security reasons.
5426 */
5427 if (secure)
5428 {
5429 secure = 2;
5430 eap->errmsg = e_curdir;
5431 }
5432 else if (eap->cmdidx == CMD_autocmd)
5433 do_autocmd(eap->arg, eap->forceit);
5434 else
5435 do_augroup(eap->arg, eap->forceit);
5436}
5437
5438/*
5439 * ":doautocmd": Apply the automatic commands to the current buffer.
5440 */
5441 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005442ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005444 char_u *arg = eap->arg;
5445 int call_do_modelines = check_nomodeline(&arg);
5446
5447 (void)do_doautocmd(arg, TRUE);
5448 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5449 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450}
5451#endif
5452
5453#ifdef FEAT_LISTCMDS
5454/*
5455 * :[N]bunload[!] [N] [bufname] unload buffer
5456 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5457 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5458 */
5459 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005460ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461{
5462 eap->errmsg = do_bufdel(
5463 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5464 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5465 : DOBUF_UNLOAD, eap->arg,
5466 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5467}
5468
5469/*
5470 * :[N]buffer [N] to buffer N
5471 * :[N]sbuffer [N] to buffer N
5472 */
5473 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005474ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475{
5476 if (*eap->arg)
5477 eap->errmsg = e_trailing;
5478 else
5479 {
5480 if (eap->addr_count == 0) /* default is current buffer */
5481 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5482 else
5483 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005484 if (eap->do_ecmd_cmd != NULL)
5485 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 }
5487}
5488
5489/*
5490 * :[N]bmodified [N] to next mod. buffer
5491 * :[N]sbmodified [N] to next mod. buffer
5492 */
5493 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005494ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495{
5496 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005497 if (eap->do_ecmd_cmd != NULL)
5498 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499}
5500
5501/*
5502 * :[N]bnext [N] to next buffer
5503 * :[N]sbnext [N] split and to next buffer
5504 */
5505 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005506ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507{
5508 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005509 if (eap->do_ecmd_cmd != NULL)
5510 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511}
5512
5513/*
5514 * :[N]bNext [N] to previous buffer
5515 * :[N]bprevious [N] to previous buffer
5516 * :[N]sbNext [N] split and to previous buffer
5517 * :[N]sbprevious [N] split and to previous buffer
5518 */
5519 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005520ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521{
5522 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005523 if (eap->do_ecmd_cmd != NULL)
5524 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525}
5526
5527/*
5528 * :brewind to first buffer
5529 * :bfirst to first buffer
5530 * :sbrewind split and to first buffer
5531 * :sbfirst split and to first buffer
5532 */
5533 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005534ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535{
5536 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005537 if (eap->do_ecmd_cmd != NULL)
5538 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539}
5540
5541/*
5542 * :blast to last buffer
5543 * :sblast split and to last buffer
5544 */
5545 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005546ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547{
5548 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005549 if (eap->do_ecmd_cmd != NULL)
5550 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551}
5552#endif
5553
5554 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005555ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556{
5557 return (c == NUL || c == '|' || c == '"' || c == '\n');
5558}
5559
5560#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5561 || defined(PROTO)
5562/*
5563 * Return the next command, after the first '|' or '\n'.
5564 * Return NULL if not found.
5565 */
5566 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005567find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568{
5569 while (*p != '|' && *p != '\n')
5570 {
5571 if (*p == NUL)
5572 return NULL;
5573 ++p;
5574 }
5575 return (p + 1);
5576}
5577#endif
5578
5579/*
5580 * Check if *p is a separator between Ex commands.
5581 * Return NULL if it isn't, (p + 1) if it is.
5582 */
5583 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005584check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585{
5586 p = skipwhite(p);
5587 if (*p == '|' || *p == '\n')
5588 return (p + 1);
5589 else
5590 return NULL;
5591}
5592
5593/*
5594 * - if there are more files to edit
5595 * - and this is the last window
5596 * - and forceit not used
5597 * - and not repeated twice on a row
5598 * return FAIL and give error message if 'message' TRUE
5599 * return OK otherwise
5600 */
5601 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005602check_more(
5603 int message, /* when FALSE check only, no messages */
5604 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605{
5606 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5607
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005608 if (!forceit && only_one_window()
5609 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 {
5611 if (message)
5612 {
5613#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5614 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5615 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005616 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617
5618 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005619 vim_strncpy(buff,
5620 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005621 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005623 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 _("%d more files to edit. Quit anyway?"), n);
5625 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5626 return OK;
5627 return FAIL;
5628 }
5629#endif
5630 if (n == 1)
5631 EMSG(_("E173: 1 more file to edit"));
5632 else
5633 EMSGN(_("E173: %ld more files to edit"), n);
5634 quitmore = 2; /* next try to quit is allowed */
5635 }
5636 return FAIL;
5637 }
5638 return OK;
5639}
5640
5641#ifdef FEAT_CMDL_COMPL
5642/*
5643 * Function given to ExpandGeneric() to obtain the list of command names.
5644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005646get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647{
5648 if (idx >= (int)CMD_SIZE)
5649# ifdef FEAT_USR_CMDS
5650 return get_user_command_name(idx);
5651# else
5652 return NULL;
5653# endif
5654 return cmdnames[idx].cmd_name;
5655}
5656#endif
5657
5658#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005659static 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);
5660static void uc_list(char_u *name, size_t name_len);
5661static 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);
5662static char_u *uc_split_args(char_u *arg, size_t *lenp);
5663static 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 +00005664
5665 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005666uc_add_command(
5667 char_u *name,
5668 size_t name_len,
5669 char_u *rep,
5670 long argt,
5671 long def,
5672 int flags,
5673 int compl,
5674 char_u *compl_arg,
5675 int addr_type,
5676 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677{
5678 ucmd_T *cmd = NULL;
5679 char_u *p;
5680 int i;
5681 int cmp = 1;
5682 char_u *rep_buf = NULL;
5683 garray_T *gap;
5684
Bram Moolenaar9c102382006-05-03 21:26:49 +00005685 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 if (rep_buf == NULL)
5687 {
5688 /* Can't replace termcodes - try using the string as is */
5689 rep_buf = vim_strsave(rep);
5690
5691 /* Give up if out of memory */
5692 if (rep_buf == NULL)
5693 return FAIL;
5694 }
5695
5696 /* get address of growarray: global or in curbuf */
5697 if (flags & UC_BUFFER)
5698 {
5699 gap = &curbuf->b_ucmds;
5700 if (gap->ga_itemsize == 0)
5701 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5702 }
5703 else
5704 gap = &ucmds;
5705
5706 /* Search for the command in the already defined commands. */
5707 for (i = 0; i < gap->ga_len; ++i)
5708 {
5709 size_t len;
5710
5711 cmd = USER_CMD_GA(gap, i);
5712 len = STRLEN(cmd->uc_name);
5713 cmp = STRNCMP(name, cmd->uc_name, name_len);
5714 if (cmp == 0)
5715 {
5716 if (name_len < len)
5717 cmp = -1;
5718 else if (name_len > len)
5719 cmp = 1;
5720 }
5721
5722 if (cmp == 0)
5723 {
5724 if (!force)
5725 {
5726 EMSG(_("E174: Command already exists: add ! to replace it"));
5727 goto fail;
5728 }
5729
5730 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005731 cmd->uc_rep = NULL;
5732#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5733 vim_free(cmd->uc_compl_arg);
5734 cmd->uc_compl_arg = NULL;
5735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 break;
5737 }
5738
5739 /* Stop as soon as we pass the name to add */
5740 if (cmp < 0)
5741 break;
5742 }
5743
5744 /* Extend the array unless we're replacing an existing command */
5745 if (cmp != 0)
5746 {
5747 if (ga_grow(gap, 1) != OK)
5748 goto fail;
5749 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5750 goto fail;
5751
5752 cmd = USER_CMD_GA(gap, i);
5753 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5754
5755 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756
5757 cmd->uc_name = p;
5758 }
5759
5760 cmd->uc_rep = rep_buf;
5761 cmd->uc_argt = argt;
5762 cmd->uc_def = def;
5763 cmd->uc_compl = compl;
5764#ifdef FEAT_EVAL
5765 cmd->uc_scriptID = current_SID;
5766# ifdef FEAT_CMDL_COMPL
5767 cmd->uc_compl_arg = compl_arg;
5768# endif
5769#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005770 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771
5772 return OK;
5773
5774fail:
5775 vim_free(rep_buf);
5776#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5777 vim_free(compl_arg);
5778#endif
5779 return FAIL;
5780}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005783#if defined(FEAT_USR_CMDS)
5784static struct
5785{
5786 int expand;
5787 char *name;
5788} addr_type_complete[] =
5789{
5790 {ADDR_ARGUMENTS, "arguments"},
5791 {ADDR_LINES, "lines"},
5792 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5793 {ADDR_TABS, "tabs"},
5794 {ADDR_BUFFERS, "buffers"},
5795 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005796 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005797 {-1, NULL}
5798};
5799#endif
5800
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005801#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802/*
5803 * List of names for completion for ":command" with the EXPAND_ flag.
5804 * Must be alphabetical for completion.
5805 */
5806static struct
5807{
5808 int expand;
5809 char *name;
5810} command_complete[] =
5811{
5812 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005813 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005815 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005817 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005818#if defined(FEAT_CSCOPE)
5819 {EXPAND_CSCOPE, "cscope"},
5820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5822 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005823 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824#endif
5825 {EXPAND_DIRECTORIES, "dir"},
5826 {EXPAND_ENV_VARS, "environment"},
5827 {EXPAND_EVENTS, "event"},
5828 {EXPAND_EXPRESSION, "expression"},
5829 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005830 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005831 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 {EXPAND_FUNCTIONS, "function"},
5833 {EXPAND_HELP, "help"},
5834 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005835#if defined(FEAT_CMDHIST)
5836 {EXPAND_HISTORY, "history"},
5837#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005838#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005839 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005840 {EXPAND_LOCALES, "locale"},
5841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 {EXPAND_MAPPINGS, "mapping"},
5843 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005844 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005845#if defined(FEAT_PROFILE)
5846 {EXPAND_SYNTIME, "syntime"},
5847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005849 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005850#if defined(FEAT_SIGNS)
5851 {EXPAND_SIGN, "sign"},
5852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 {EXPAND_TAGS, "tag"},
5854 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005855 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 {EXPAND_USER_VARS, "var"},
5857 {0, NULL}
5858};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005861#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005863uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864{
5865 int i, j;
5866 int found = FALSE;
5867 ucmd_T *cmd;
5868 int len;
5869 long a;
5870 garray_T *gap;
5871
5872 gap = &curbuf->b_ucmds;
5873 for (;;)
5874 {
5875 for (i = 0; i < gap->ga_len; ++i)
5876 {
5877 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005878 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879
5880 /* Skip commands which don't match the requested prefix */
5881 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5882 continue;
5883
5884 /* Put out the title first time */
5885 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005886 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 found = TRUE;
5888 msg_putchar('\n');
5889 if (got_int)
5890 break;
5891
5892 /* Special cases */
5893 msg_putchar(a & BANG ? '!' : ' ');
5894 msg_putchar(a & REGSTR ? '"' : ' ');
5895 msg_putchar(gap != &ucmds ? 'b' : ' ');
5896 msg_putchar(' ');
5897
5898 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5899 len = (int)STRLEN(cmd->uc_name) + 4;
5900
5901 do {
5902 msg_putchar(' ');
5903 ++len;
5904 } while (len < 16);
5905
5906 len = 0;
5907
5908 /* Arguments */
5909 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5910 {
5911 case 0: IObuff[len++] = '0'; break;
5912 case (EXTRA): IObuff[len++] = '*'; break;
5913 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5914 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5915 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5916 }
5917
5918 do {
5919 IObuff[len++] = ' ';
5920 } while (len < 5);
5921
5922 /* Range */
5923 if (a & (RANGE|COUNT))
5924 {
5925 if (a & COUNT)
5926 {
5927 /* -count=N */
5928 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5929 len += (int)STRLEN(IObuff + len);
5930 }
5931 else if (a & DFLALL)
5932 IObuff[len++] = '%';
5933 else if (cmd->uc_def >= 0)
5934 {
5935 /* -range=N */
5936 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5937 len += (int)STRLEN(IObuff + len);
5938 }
5939 else
5940 IObuff[len++] = '.';
5941 }
5942
5943 do {
5944 IObuff[len++] = ' ';
5945 } while (len < 11);
5946
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005947 /* Address Type */
5948 for (j = 0; addr_type_complete[j].expand != -1; ++j)
5949 if (addr_type_complete[j].expand != ADDR_LINES
5950 && addr_type_complete[j].expand == cmd->uc_addr_type)
5951 {
5952 STRCPY(IObuff + len, addr_type_complete[j].name);
5953 len += (int)STRLEN(IObuff + len);
5954 break;
5955 }
5956
5957 do {
5958 IObuff[len++] = ' ';
5959 } while (len < 21);
5960
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 /* Completion */
5962 for (j = 0; command_complete[j].expand != 0; ++j)
5963 if (command_complete[j].expand == cmd->uc_compl)
5964 {
5965 STRCPY(IObuff + len, command_complete[j].name);
5966 len += (int)STRLEN(IObuff + len);
5967 break;
5968 }
5969
5970 do {
5971 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005972 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973
5974 IObuff[len] = '\0';
5975 msg_outtrans(IObuff);
5976
5977 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005978#ifdef FEAT_EVAL
5979 if (p_verbose > 0)
5980 last_set_msg(cmd->uc_scriptID);
5981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 out_flush();
5983 ui_breakcheck();
5984 if (got_int)
5985 break;
5986 }
5987 if (gap == &ucmds || i < gap->ga_len)
5988 break;
5989 gap = &ucmds;
5990 }
5991
5992 if (!found)
5993 MSG(_("No user-defined commands found"));
5994}
5995
5996 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005997uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998{
5999 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6000 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6001 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6002 0xb9, 0x7f, 0};
6003 int i;
6004
6005 for (i = 0; fcmd[i]; ++i)
6006 IObuff[i] = fcmd[i] - 0x40;
6007 IObuff[i] = 0;
6008 return IObuff;
6009}
6010
6011 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006012uc_scan_attr(
6013 char_u *attr,
6014 size_t len,
6015 long *argt,
6016 long *def,
6017 int *flags,
6018 int *compl,
6019 char_u **compl_arg,
6020 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021{
6022 char_u *p;
6023
6024 if (len == 0)
6025 {
6026 EMSG(_("E175: No attribute specified"));
6027 return FAIL;
6028 }
6029
6030 /* First, try the simple attributes (no arguments) */
6031 if (STRNICMP(attr, "bang", len) == 0)
6032 *argt |= BANG;
6033 else if (STRNICMP(attr, "buffer", len) == 0)
6034 *flags |= UC_BUFFER;
6035 else if (STRNICMP(attr, "register", len) == 0)
6036 *argt |= REGSTR;
6037 else if (STRNICMP(attr, "bar", len) == 0)
6038 *argt |= TRLBAR;
6039 else
6040 {
6041 int i;
6042 char_u *val = NULL;
6043 size_t vallen = 0;
6044 size_t attrlen = len;
6045
6046 /* Look for the attribute name - which is the part before any '=' */
6047 for (i = 0; i < (int)len; ++i)
6048 {
6049 if (attr[i] == '=')
6050 {
6051 val = &attr[i + 1];
6052 vallen = len - i - 1;
6053 attrlen = i;
6054 break;
6055 }
6056 }
6057
6058 if (STRNICMP(attr, "nargs", attrlen) == 0)
6059 {
6060 if (vallen == 1)
6061 {
6062 if (*val == '0')
6063 /* Do nothing - this is the default */;
6064 else if (*val == '1')
6065 *argt |= (EXTRA | NOSPC | NEEDARG);
6066 else if (*val == '*')
6067 *argt |= EXTRA;
6068 else if (*val == '?')
6069 *argt |= (EXTRA | NOSPC);
6070 else if (*val == '+')
6071 *argt |= (EXTRA | NEEDARG);
6072 else
6073 goto wrong_nargs;
6074 }
6075 else
6076 {
6077wrong_nargs:
6078 EMSG(_("E176: Invalid number of arguments"));
6079 return FAIL;
6080 }
6081 }
6082 else if (STRNICMP(attr, "range", attrlen) == 0)
6083 {
6084 *argt |= RANGE;
6085 if (vallen == 1 && *val == '%')
6086 *argt |= DFLALL;
6087 else if (val != NULL)
6088 {
6089 p = val;
6090 if (*def >= 0)
6091 {
6092two_count:
6093 EMSG(_("E177: Count cannot be specified twice"));
6094 return FAIL;
6095 }
6096
6097 *def = getdigits(&p);
6098 *argt |= (ZEROR | NOTADR);
6099
6100 if (p != val + vallen || vallen == 0)
6101 {
6102invalid_count:
6103 EMSG(_("E178: Invalid default value for count"));
6104 return FAIL;
6105 }
6106 }
6107 }
6108 else if (STRNICMP(attr, "count", attrlen) == 0)
6109 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006110 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111
6112 if (val != NULL)
6113 {
6114 p = val;
6115 if (*def >= 0)
6116 goto two_count;
6117
6118 *def = getdigits(&p);
6119
6120 if (p != val + vallen)
6121 goto invalid_count;
6122 }
6123
6124 if (*def < 0)
6125 *def = 0;
6126 }
6127 else if (STRNICMP(attr, "complete", attrlen) == 0)
6128 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 if (val == NULL)
6130 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006131 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 return FAIL;
6133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006135 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6136 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006139 else if (STRNICMP(attr, "addr", attrlen) == 0)
6140 {
6141 *argt |= RANGE;
6142 if (val == NULL)
6143 {
6144 EMSG(_("E179: argument required for -addr"));
6145 return FAIL;
6146 }
6147 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6148 == FAIL)
6149 return FAIL;
6150 if (addr_type_arg != ADDR_LINES)
6151 *argt |= (ZEROR | NOTADR) ;
6152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 else
6154 {
6155 char_u ch = attr[len];
6156 attr[len] = '\0';
6157 EMSG2(_("E181: Invalid attribute: %s"), attr);
6158 attr[len] = ch;
6159 return FAIL;
6160 }
6161 }
6162
6163 return OK;
6164}
6165
Bram Moolenaara850a712009-01-28 14:42:59 +00006166/*
6167 * ":command ..."
6168 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006170ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171{
6172 char_u *name;
6173 char_u *end;
6174 char_u *p;
6175 long argt = 0;
6176 long def = -1;
6177 int flags = 0;
6178 int compl = EXPAND_NOTHING;
6179 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006180 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006182 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183
6184 p = eap->arg;
6185
6186 /* Check for attributes */
6187 while (*p == '-')
6188 {
6189 ++p;
6190 end = skiptowhite(p);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006191 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 == FAIL)
6193 return;
6194 p = skipwhite(end);
6195 }
6196
6197 /* Get the name (if any) and skip to the following argument */
6198 name = p;
6199 if (ASCII_ISALPHA(*p))
6200 while (ASCII_ISALNUM(*p))
6201 ++p;
6202 if (!ends_excmd(*p) && !vim_iswhite(*p))
6203 {
6204 EMSG(_("E182: Invalid command name"));
6205 return;
6206 }
6207 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006208 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209
6210 /* If there is nothing after the name, and no attributes were specified,
6211 * we are listing commands
6212 */
6213 p = skipwhite(end);
6214 if (!has_attr && ends_excmd(*p))
6215 {
6216 uc_list(name, end - name);
6217 }
6218 else if (!ASCII_ISUPPER(*name))
6219 {
6220 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6221 return;
6222 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006223 else if ((name_len == 1 && *name == 'X')
6224 || (name_len <= 4
6225 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6226 {
6227 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6228 return;
6229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 else
6231 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006232 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233}
6234
6235/*
6236 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 * Clear all user commands, global and for current buffer.
6238 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006239 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006240ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241{
6242 uc_clear(&ucmds);
6243 uc_clear(&curbuf->b_ucmds);
6244}
6245
6246/*
6247 * Clear all user commands for "gap".
6248 */
6249 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006250uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251{
6252 int i;
6253 ucmd_T *cmd;
6254
6255 for (i = 0; i < gap->ga_len; ++i)
6256 {
6257 cmd = USER_CMD_GA(gap, i);
6258 vim_free(cmd->uc_name);
6259 vim_free(cmd->uc_rep);
6260# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6261 vim_free(cmd->uc_compl_arg);
6262# endif
6263 }
6264 ga_clear(gap);
6265}
6266
6267 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006268ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269{
6270 int i = 0;
6271 ucmd_T *cmd = NULL;
6272 int cmp = -1;
6273 garray_T *gap;
6274
6275 gap = &curbuf->b_ucmds;
6276 for (;;)
6277 {
6278 for (i = 0; i < gap->ga_len; ++i)
6279 {
6280 cmd = USER_CMD_GA(gap, i);
6281 cmp = STRCMP(eap->arg, cmd->uc_name);
6282 if (cmp <= 0)
6283 break;
6284 }
6285 if (gap == &ucmds || cmp == 0)
6286 break;
6287 gap = &ucmds;
6288 }
6289
6290 if (cmp != 0)
6291 {
6292 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6293 return;
6294 }
6295
6296 vim_free(cmd->uc_name);
6297 vim_free(cmd->uc_rep);
6298# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6299 vim_free(cmd->uc_compl_arg);
6300# endif
6301
6302 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303
6304 if (i < gap->ga_len)
6305 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6306}
6307
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006308/*
6309 * split and quote args for <f-args>
6310 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006312uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313{
6314 char_u *buf;
6315 char_u *p;
6316 char_u *q;
6317 int len;
6318
6319 /* Precalculate length */
6320 p = arg;
6321 len = 2; /* Initial and final quotes */
6322
6323 while (*p)
6324 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006325 if (p[0] == '\\' && p[1] == '\\')
6326 {
6327 len += 2;
6328 p += 2;
6329 }
6330 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 {
6332 len += 1;
6333 p += 2;
6334 }
6335 else if (*p == '\\' || *p == '"')
6336 {
6337 len += 2;
6338 p += 1;
6339 }
6340 else if (vim_iswhite(*p))
6341 {
6342 p = skipwhite(p);
6343 if (*p == NUL)
6344 break;
6345 len += 3; /* "," */
6346 }
6347 else
6348 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006349#ifdef FEAT_MBYTE
6350 int charlen = (*mb_ptr2len)(p);
6351 len += charlen;
6352 p += charlen;
6353#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 ++len;
6355 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 }
6358 }
6359
6360 buf = alloc(len + 1);
6361 if (buf == NULL)
6362 {
6363 *lenp = 0;
6364 return buf;
6365 }
6366
6367 p = arg;
6368 q = buf;
6369 *q++ = '"';
6370 while (*p)
6371 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006372 if (p[0] == '\\' && p[1] == '\\')
6373 {
6374 *q++ = '\\';
6375 *q++ = '\\';
6376 p += 2;
6377 }
6378 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 {
6380 *q++ = p[1];
6381 p += 2;
6382 }
6383 else if (*p == '\\' || *p == '"')
6384 {
6385 *q++ = '\\';
6386 *q++ = *p++;
6387 }
6388 else if (vim_iswhite(*p))
6389 {
6390 p = skipwhite(p);
6391 if (*p == NUL)
6392 break;
6393 *q++ = '"';
6394 *q++ = ',';
6395 *q++ = '"';
6396 }
6397 else
6398 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006399 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 }
6401 }
6402 *q++ = '"';
6403 *q = 0;
6404
6405 *lenp = len;
6406 return buf;
6407}
6408
6409/*
6410 * Check for a <> code in a user command.
6411 * "code" points to the '<'. "len" the length of the <> (inclusive).
6412 * "buf" is where the result is to be added.
6413 * "split_buf" points to a buffer used for splitting, caller should free it.
6414 * "split_len" is the length of what "split_buf" contains.
6415 * Returns the length of the replacement, which has been added to "buf".
6416 * Returns -1 if there was no match, and only the "<" has been copied.
6417 */
6418 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006419uc_check_code(
6420 char_u *code,
6421 size_t len,
6422 char_u *buf,
6423 ucmd_T *cmd, /* the user command we're expanding */
6424 exarg_T *eap, /* ex arguments */
6425 char_u **split_buf,
6426 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427{
6428 size_t result = 0;
6429 char_u *p = code + 1;
6430 size_t l = len - 2;
6431 int quote = 0;
6432 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
6433 ct_LT, ct_NONE } type = ct_NONE;
6434
6435 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6436 {
6437 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6438 p += 2;
6439 l -= 2;
6440 }
6441
Bram Moolenaar371d5402006-03-20 21:47:49 +00006442 ++l;
6443 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006445 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006447 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006449 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006451 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006453 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006455 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006457 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 type = ct_REGISTER;
6459
6460 switch (type)
6461 {
6462 case ct_ARGS:
6463 /* Simple case first */
6464 if (*eap->arg == NUL)
6465 {
6466 if (quote == 1)
6467 {
6468 result = 2;
6469 if (buf != NULL)
6470 STRCPY(buf, "''");
6471 }
6472 else
6473 result = 0;
6474 break;
6475 }
6476
6477 /* When specified there is a single argument don't split it.
6478 * Works for ":Cmd %" when % is "a b c". */
6479 if ((eap->argt & NOSPC) && quote == 2)
6480 quote = 1;
6481
6482 switch (quote)
6483 {
6484 case 0: /* No quoting, no splitting */
6485 result = STRLEN(eap->arg);
6486 if (buf != NULL)
6487 STRCPY(buf, eap->arg);
6488 break;
6489 case 1: /* Quote, but don't split */
6490 result = STRLEN(eap->arg) + 2;
6491 for (p = eap->arg; *p; ++p)
6492 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006493#ifdef FEAT_MBYTE
6494 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6495 /* DBCS can contain \ in a trail byte, skip the
6496 * double-byte character. */
6497 ++p;
6498 else
6499#endif
6500 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 ++result;
6502 }
6503
6504 if (buf != NULL)
6505 {
6506 *buf++ = '"';
6507 for (p = eap->arg; *p; ++p)
6508 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006509#ifdef FEAT_MBYTE
6510 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6511 /* DBCS can contain \ in a trail byte, copy the
6512 * double-byte character to avoid escaping. */
6513 *buf++ = *p++;
6514 else
6515#endif
6516 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 *buf++ = '\\';
6518 *buf++ = *p;
6519 }
6520 *buf = '"';
6521 }
6522
6523 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006524 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 /* This is hard, so only do it once, and cache the result */
6526 if (*split_buf == NULL)
6527 *split_buf = uc_split_args(eap->arg, split_len);
6528
6529 result = *split_len;
6530 if (buf != NULL && result != 0)
6531 STRCPY(buf, *split_buf);
6532
6533 break;
6534 }
6535 break;
6536
6537 case ct_BANG:
6538 result = eap->forceit ? 1 : 0;
6539 if (quote)
6540 result += 2;
6541 if (buf != NULL)
6542 {
6543 if (quote)
6544 *buf++ = '"';
6545 if (eap->forceit)
6546 *buf++ = '!';
6547 if (quote)
6548 *buf = '"';
6549 }
6550 break;
6551
6552 case ct_LINE1:
6553 case ct_LINE2:
6554 case ct_COUNT:
6555 {
6556 char num_buf[20];
6557 long num = (type == ct_LINE1) ? eap->line1 :
6558 (type == ct_LINE2) ? eap->line2 :
6559 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6560 size_t num_len;
6561
6562 sprintf(num_buf, "%ld", num);
6563 num_len = STRLEN(num_buf);
6564 result = num_len;
6565
6566 if (quote)
6567 result += 2;
6568
6569 if (buf != NULL)
6570 {
6571 if (quote)
6572 *buf++ = '"';
6573 STRCPY(buf, num_buf);
6574 buf += num_len;
6575 if (quote)
6576 *buf = '"';
6577 }
6578
6579 break;
6580 }
6581
6582 case ct_REGISTER:
6583 result = eap->regname ? 1 : 0;
6584 if (quote)
6585 result += 2;
6586 if (buf != NULL)
6587 {
6588 if (quote)
6589 *buf++ = '\'';
6590 if (eap->regname)
6591 *buf++ = eap->regname;
6592 if (quote)
6593 *buf = '\'';
6594 }
6595 break;
6596
6597 case ct_LT:
6598 result = 1;
6599 if (buf != NULL)
6600 *buf = '<';
6601 break;
6602
6603 default:
6604 /* Not recognized: just copy the '<' and return -1. */
6605 result = (size_t)-1;
6606 if (buf != NULL)
6607 *buf = '<';
6608 break;
6609 }
6610
6611 return result;
6612}
6613
6614 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006615do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616{
6617 char_u *buf;
6618 char_u *p;
6619 char_u *q;
6620
6621 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006622 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006623 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 size_t len, totlen;
6625
6626 size_t split_len = 0;
6627 char_u *split_buf = NULL;
6628 ucmd_T *cmd;
6629#ifdef FEAT_EVAL
6630 scid_T save_current_SID = current_SID;
6631#endif
6632
6633 if (eap->cmdidx == CMD_USER)
6634 cmd = USER_CMD(eap->useridx);
6635 else
6636 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6637
6638 /*
6639 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006640 * First round: "buf" is NULL, compute length, allocate "buf".
6641 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 */
6643 buf = NULL;
6644 for (;;)
6645 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006646 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006647 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006649
6650 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006652 start = vim_strchr(p, '<');
6653 if (start != NULL)
6654 end = vim_strchr(start + 1, '>');
6655 if (buf != NULL)
6656 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006657 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6658 ;
6659 if (*ksp == K_SPECIAL
6660 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006661 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6662# ifdef FEAT_GUI
6663 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6664# endif
6665 ))
6666 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006667 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006668 * KS_SPECIAL KE_FILLER, like for mappings, but
6669 * do_cmdline() doesn't handle that, so convert it back.
6670 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6671 len = ksp - p;
6672 if (len > 0)
6673 {
6674 mch_memmove(q, p, len);
6675 q += len;
6676 }
6677 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6678 p = ksp + 3;
6679 continue;
6680 }
6681 }
6682
6683 /* break if there no <item> is found */
6684 if (start == NULL || end == NULL)
6685 break;
6686
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 /* Include the '>' */
6688 ++end;
6689
6690 /* Take everything up to the '<' */
6691 len = start - p;
6692 if (buf == NULL)
6693 totlen += len;
6694 else
6695 {
6696 mch_memmove(q, p, len);
6697 q += len;
6698 }
6699
6700 len = uc_check_code(start, end - start, q, cmd, eap,
6701 &split_buf, &split_len);
6702 if (len == (size_t)-1)
6703 {
6704 /* no match, continue after '<' */
6705 p = start + 1;
6706 len = 1;
6707 }
6708 else
6709 p = end;
6710 if (buf == NULL)
6711 totlen += len;
6712 else
6713 q += len;
6714 }
6715 if (buf != NULL) /* second time here, finished */
6716 {
6717 STRCPY(q, p);
6718 break;
6719 }
6720
6721 totlen += STRLEN(p); /* Add on the trailing characters */
6722 buf = alloc((unsigned)(totlen + 1));
6723 if (buf == NULL)
6724 {
6725 vim_free(split_buf);
6726 return;
6727 }
6728 }
6729
6730#ifdef FEAT_EVAL
6731 current_SID = cmd->uc_scriptID;
6732#endif
6733 (void)do_cmdline(buf, eap->getline, eap->cookie,
6734 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6735#ifdef FEAT_EVAL
6736 current_SID = save_current_SID;
6737#endif
6738 vim_free(buf);
6739 vim_free(split_buf);
6740}
6741
6742# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6743 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006744get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745{
6746 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6747}
6748
6749/*
6750 * Function given to ExpandGeneric() to obtain the list of user command names.
6751 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006753get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754{
6755 if (idx < curbuf->b_ucmds.ga_len)
6756 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6757 idx -= curbuf->b_ucmds.ga_len;
6758 if (idx < ucmds.ga_len)
6759 return USER_CMD(idx)->uc_name;
6760 return NULL;
6761}
6762
6763/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006764 * Function given to ExpandGeneric() to obtain the list of user address type names.
6765 */
6766 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006767get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006768{
6769 return (char_u *)addr_type_complete[idx].name;
6770}
6771
6772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 * Function given to ExpandGeneric() to obtain the list of user command
6774 * attributes.
6775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006777get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778{
6779 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006780 {"addr", "bang", "bar", "buffer", "complete",
6781 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006783 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 return NULL;
6785 return (char_u *)user_cmd_flags[idx];
6786}
6787
6788/*
6789 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6790 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006792get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793{
6794 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6795
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006796 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 return NULL;
6798 return (char_u *)user_cmd_nargs[idx];
6799}
6800
6801/*
6802 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6803 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006805get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806{
6807 return (char_u *)command_complete[idx].name;
6808}
6809# endif /* FEAT_CMDL_COMPL */
6810
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006811/*
6812 * Parse address type argument
6813 */
6814 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006815parse_addr_type_arg(
6816 char_u *value,
6817 int vallen,
6818 long *argt,
6819 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006820{
6821 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01006822
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006823 for (i = 0; addr_type_complete[i].expand != -1; ++i)
6824 {
6825 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
6826 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
6827 if (a && b)
6828 {
6829 *addr_type_arg = addr_type_complete[i].expand;
6830 break;
6831 }
6832 }
6833
6834 if (addr_type_complete[i].expand == -1)
6835 {
6836 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01006837
6838 for (i = 0; err[i] != NUL && !vim_iswhite(err[i]); i++)
6839 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006840 err[i] = NUL;
6841 EMSG2(_("E180: Invalid address type value: %s"), err);
6842 return FAIL;
6843 }
6844
6845 if (*addr_type_arg != ADDR_LINES)
6846 *argt |= NOTADR;
6847
6848 return OK;
6849}
6850
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851#endif /* FEAT_USR_CMDS */
6852
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006853#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6854/*
6855 * Parse a completion argument "value[vallen]".
6856 * The detected completion goes in "*complp", argument type in "*argt".
6857 * When there is an argument, for function and user defined completion, it's
6858 * copied to allocated memory and stored in "*compl_arg".
6859 * Returns FAIL if something is wrong.
6860 */
6861 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006862parse_compl_arg(
6863 char_u *value,
6864 int vallen,
6865 int *complp,
6866 long *argt,
6867 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006868{
6869 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006870# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006871 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006872# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006873 int i;
6874 int valend = vallen;
6875
6876 /* Look for any argument part - which is the part after any ',' */
6877 for (i = 0; i < vallen; ++i)
6878 {
6879 if (value[i] == ',')
6880 {
6881 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006882# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006883 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006884# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006885 valend = i;
6886 break;
6887 }
6888 }
6889
6890 for (i = 0; command_complete[i].expand != 0; ++i)
6891 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006892 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006893 && STRNCMP(value, command_complete[i].name, valend) == 0)
6894 {
6895 *complp = command_complete[i].expand;
6896 if (command_complete[i].expand == EXPAND_BUFFERS)
6897 *argt |= BUFNAME;
6898 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6899 || command_complete[i].expand == EXPAND_FILES)
6900 *argt |= XFILE;
6901 break;
6902 }
6903 }
6904
6905 if (command_complete[i].expand == 0)
6906 {
6907 EMSG2(_("E180: Invalid complete value: %s"), value);
6908 return FAIL;
6909 }
6910
6911# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6912 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6913 && arg != NULL)
6914# else
6915 if (arg != NULL)
6916# endif
6917 {
6918 EMSG(_("E468: Completion argument only allowed for custom completion"));
6919 return FAIL;
6920 }
6921
6922# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6923 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6924 && arg == NULL)
6925 {
6926 EMSG(_("E467: Custom completion requires a function argument"));
6927 return FAIL;
6928 }
6929
6930 if (arg != NULL)
6931 *compl_arg = vim_strnsave(arg, (int)arglen);
6932# endif
6933 return OK;
6934}
6935#endif
6936
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006938ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939{
Bram Moolenaare6850792010-05-14 15:28:44 +02006940 if (*eap->arg == NUL)
6941 {
6942#ifdef FEAT_EVAL
6943 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6944 char_u *p = NULL;
6945
6946 if (expr != NULL)
6947 {
6948 ++emsg_off;
6949 p = eval_to_string(expr, NULL, FALSE);
6950 --emsg_off;
6951 vim_free(expr);
6952 }
6953 if (p != NULL)
6954 {
6955 MSG(p);
6956 vim_free(p);
6957 }
6958 else
6959 MSG("default");
6960#else
6961 MSG(_("unknown"));
6962#endif
6963 }
6964 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006965 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966}
6967
6968 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006969ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970{
6971 if (*eap->arg == NUL && eap->cmd[2] == '!')
6972 MSG(_("Greetings, Vim user!"));
6973 do_highlight(eap->arg, eap->forceit, FALSE);
6974}
6975
6976
6977/*
6978 * Call this function if we thought we were going to exit, but we won't
6979 * (because of an error). May need to restore the terminal mode.
6980 */
6981 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006982not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983{
6984 exiting = FALSE;
6985 settmode(TMODE_RAW);
6986}
6987
6988/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01006989 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 */
6991 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006992ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993{
Bram Moolenaarf240e182014-11-27 18:33:02 +01006994#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006995 win_T *wp;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006996#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006997
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998#ifdef FEAT_CMDWIN
6999 if (cmdwin_type != 0)
7000 {
7001 cmdwin_result = Ctrl_C;
7002 return;
7003 }
7004#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007005 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007006 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007007 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007008 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007009 return;
7010 }
Bram Moolenaarf240e182014-11-27 18:33:02 +01007011#ifdef FEAT_WINDOWS
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007012 if (eap->addr_count > 0)
7013 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007014 int wnr = eap->line2;
7015
7016 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7017 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007018 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007019 }
7020 else
Bram Moolenaarf240e182014-11-27 18:33:02 +01007021#endif
7022#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007023 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007024#endif
7025
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007026#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02007027 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007028 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02007029 * being closed (can only happen in autocommands). */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007030 if (curbuf_locked() || (wp->w_buffer->b_nwindows == 1
7031 && wp->w_buffer->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007032 return;
7033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034
7035#ifdef FEAT_NETBEANS_INTG
7036 netbeansForcedQuit = eap->forceit;
7037#endif
7038
7039 /*
7040 * If there are more files or windows we won't exit.
7041 */
7042 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7043 exiting = TRUE;
7044 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007045 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7046 | (eap->forceit ? CCGD_FORCEIT : 0)
7047 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007049 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 {
7051 not_exiting();
7052 }
7053 else
7054 {
7055#ifdef FEAT_WINDOWS
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007056 /* quit last window
7057 * Note: only_one_window() returns true, even so a help window is
7058 * still open. In that case only quit, if no address has been
7059 * specified. Example:
7060 * :h|wincmd w|1q - don't quit
7061 * :h|wincmd w|q - quit
7062 */
7063 if (only_one_window() && (firstwin == lastwin || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064#endif
7065 getout(0);
7066#ifdef FEAT_WINDOWS
7067# ifdef FEAT_GUI
7068 need_mouse_correct = TRUE;
7069# endif
7070 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007071 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072#endif
7073 }
7074}
7075
7076/*
7077 * ":cquit".
7078 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007080ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081{
7082 getout(1); /* this does not always pass on the exit code to the Manx
7083 compiler. why? */
7084}
7085
7086/*
7087 * ":qall": try to quit all windows
7088 */
7089 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007090ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091{
7092# ifdef FEAT_CMDWIN
7093 if (cmdwin_type != 0)
7094 {
7095 if (eap->forceit)
7096 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7097 else
7098 cmdwin_result = K_XF2;
7099 return;
7100 }
7101# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007102
7103 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007104 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007105 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007106 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007107 return;
7108 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007109#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007110 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7111 /* Refuse to quit when locked or when the buffer in the last window is
7112 * being closed (can only happen in autocommands). */
7113 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007114 return;
7115#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007116
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007118 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 getout(0);
7120 not_exiting();
7121}
7122
Bram Moolenaard9967712006-03-11 21:18:15 +00007123#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124/*
7125 * ":close": close current window, unless it is the last one
7126 */
7127 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007128ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007130 win_T *win;
7131 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132# ifdef FEAT_CMDWIN
7133 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007134 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 else
7136# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007137 if (!text_locked()
7138#ifdef FEAT_AUTOCMD
7139 && !curbuf_locked()
7140#endif
7141 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007142 {
7143 if (eap->addr_count == 0)
7144 ex_win_close(eap->forceit, curwin, NULL);
7145 else {
7146 for (win = firstwin; win != NULL; win = win->w_next)
7147 {
7148 winnr++;
7149 if (winnr == eap->line2)
7150 break;
7151 }
7152 if (win == NULL)
7153 win = lastwin;
7154 ex_win_close(eap->forceit, win, NULL);
7155 }
7156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157}
7158
Bram Moolenaard9967712006-03-11 21:18:15 +00007159# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007160/*
7161 * ":pclose": Close any preview window.
7162 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007164ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007165{
7166 win_T *win;
7167
7168 for (win = firstwin; win != NULL; win = win->w_next)
7169 if (win->w_p_pvw)
7170 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007171 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007172 break;
7173 }
7174}
Bram Moolenaard9967712006-03-11 21:18:15 +00007175# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007176
Bram Moolenaarf740b292006-02-16 22:11:02 +00007177/*
7178 * Close window "win" and take care of handling closing the last window for a
7179 * modified buffer.
7180 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007181 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007182ex_win_close(
7183 int forceit,
7184 win_T *win,
7185 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186{
7187 int need_hide;
7188 buf_T *buf = win->w_buffer;
7189
7190 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007191 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007193# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 if ((p_confirm || cmdmod.confirm) && p_write)
7195 {
7196 dialog_changed(buf, FALSE);
7197 if (buf_valid(buf) && bufIsChanged(buf))
7198 return;
7199 need_hide = FALSE;
7200 }
7201 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007202# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 {
7204 EMSG(_(e_nowrtmsg));
7205 return;
7206 }
7207 }
7208
Bram Moolenaard9967712006-03-11 21:18:15 +00007209# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007211# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007212
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007214 if (tp == NULL)
7215 win_close(win, !need_hide && !P_HID(buf));
7216 else
7217 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218}
7219
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007221 * ":tabclose": close current tab page, unless it is the last one.
7222 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 */
7224 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007225ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007227 tabpage_T *tp;
7228
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007229# ifdef FEAT_CMDWIN
7230 if (cmdwin_type != 0)
7231 cmdwin_result = K_IGNORE;
7232 else
7233# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007234 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007235 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007236 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007238 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007239 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007240 tp = find_tabpage((int)eap->line2);
7241 if (tp == NULL)
7242 {
7243 beep_flush();
7244 return;
7245 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007246 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007247 {
7248 tabpage_close_other(tp, eap->forceit);
7249 return;
7250 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007251 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007252 if (!text_locked()
7253#ifdef FEAT_AUTOCMD
7254 && !curbuf_locked()
7255#endif
7256 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00007257 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007259}
7260
7261/*
7262 * ":tabonly": close all tab pages except the current one
7263 */
7264 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007265ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007266{
7267 tabpage_T *tp;
7268 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007269
7270# ifdef FEAT_CMDWIN
7271 if (cmdwin_type != 0)
7272 cmdwin_result = K_IGNORE;
7273 else
7274# endif
7275 if (first_tabpage->tp_next == NULL)
7276 MSG(_("Already only one tab page"));
7277 else
7278 {
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007279 if (eap->addr_count > 0)
7280 goto_tabpage(eap->line2);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007281 /* Repeat this up to a 1000 times, because autocommands may mess
7282 * up the lists. */
7283 for (done = 0; done < 1000; ++done)
7284 {
7285 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7286 if (tp->tp_topframe != topframe)
7287 {
7288 tabpage_close_other(tp, eap->forceit);
7289 /* if we failed to close it quit */
7290 if (valid_tabpage(tp))
7291 done = 1000;
7292 /* start over, "tp" is now invalid */
7293 break;
7294 }
7295 if (first_tabpage->tp_next == NULL)
7296 break;
7297 }
7298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300
7301/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007302 * Close the current tab page.
7303 */
7304 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007305tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007306{
7307 /* First close all the windows but the current one. If that worked then
7308 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007309 if (lastwin != firstwin)
7310 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007311 if (lastwin == firstwin)
7312 ex_win_close(forceit, curwin, NULL);
7313# ifdef FEAT_GUI
7314 need_mouse_correct = TRUE;
7315# endif
7316}
7317
7318/*
7319 * Close tab page "tp", which is not the current tab page.
7320 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007321 * Also takes care of the tab pages line disappearing when closing the
7322 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007323 */
7324 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007325tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007326{
7327 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007328 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007329 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007330
7331 /* Limit to 1000 windows, autocommands may add a window while we close
7332 * one. OK, so I'm paranoid... */
7333 while (++done < 1000)
7334 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007335 wp = tp->tp_firstwin;
7336 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007337
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007338 /* Autocommands may delete the tab page under our fingers and we may
7339 * fail to close a window with a modified buffer. */
7340 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007341 break;
7342 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007343
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007344 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007345 if (h != tabline_height())
7346 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007347}
7348
7349/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 * ":only".
7351 */
7352 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007353ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007355 win_T *wp;
7356 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357# ifdef FEAT_GUI
7358 need_mouse_correct = TRUE;
7359# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007360 if (eap->addr_count > 0)
7361 {
7362 wnr = eap->line2;
7363 for (wp = firstwin; --wnr > 0; )
7364 {
7365 if (wp->w_next == NULL)
7366 break;
7367 else
7368 wp = wp->w_next;
7369 }
7370 win_goto(wp);
7371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 close_others(TRUE, eap->forceit);
7373}
7374
7375/*
7376 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007377 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007379 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007380ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381{
7382 if (eap->addr_count == 0)
7383 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007384 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385}
7386#endif /* FEAT_WINDOWS */
7387
7388 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007389ex_hide(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390{
7391 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
7392 eap->errmsg = e_invarg;
7393 else
7394 {
7395 /* ":hide" or ":hide | cmd": hide current window */
7396 eap->nextcmd = check_nextcmd(eap->arg);
7397#ifdef FEAT_WINDOWS
7398 if (!eap->skip)
7399 {
7400# ifdef FEAT_GUI
7401 need_mouse_correct = TRUE;
7402# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007403 if (eap->addr_count == 0)
7404 win_close(curwin, FALSE); /* don't free buffer */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007405 else
7406 {
7407 int winnr = 0;
7408 win_T *win;
7409
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007410 for (win = firstwin; win != NULL; win = win->w_next)
7411 {
7412 winnr++;
7413 if (winnr == eap->line2)
7414 break;
7415 }
7416 if (win == NULL)
7417 win = lastwin;
7418 win_close(win, FALSE);
7419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 }
7421#endif
7422 }
7423}
7424
7425/*
7426 * ":stop" and ":suspend": Suspend Vim.
7427 */
7428 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007429ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430{
7431 /*
7432 * Disallow suspending for "rvim".
7433 */
7434 if (!check_restricted()
7435#ifdef WIN3264
7436 /*
7437 * Check if external commands are allowed now.
7438 */
7439 && can_end_termcap_mode(TRUE)
7440#endif
7441 )
7442 {
7443 if (!eap->forceit)
7444 autowrite_all();
7445 windgoto((int)Rows - 1, 0);
7446 out_char('\n');
7447 out_flush();
7448 stoptermcap();
7449 out_flush(); /* needed for SUN to restore xterm buffer */
7450#ifdef FEAT_TITLE
7451 mch_restore_title(3); /* restore window titles */
7452#endif
7453 ui_suspend(); /* call machine specific function */
7454#ifdef FEAT_TITLE
7455 maketitle();
7456 resettitle(); /* force updating the title */
7457#endif
7458 starttermcap();
7459 scroll_start(); /* scroll screen before redrawing */
7460 redraw_later_clear();
7461 shell_resized(); /* may have resized window */
7462 }
7463}
7464
7465/*
7466 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7467 */
7468 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007469ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470{
7471#ifdef FEAT_CMDWIN
7472 if (cmdwin_type != 0)
7473 {
7474 cmdwin_result = Ctrl_C;
7475 return;
7476 }
7477#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007478 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007479 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007480 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007481 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007482 return;
7483 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007484#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007485 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7486 /* Refuse to quit when locked or when the buffer in the last window is
7487 * being closed (can only happen in autocommands). */
7488 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007489 return;
7490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491
7492 /*
7493 * if more files or windows we won't exit
7494 */
7495 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7496 exiting = TRUE;
7497 if ( ((eap->cmdidx == CMD_wq
7498 || curbufIsChanged())
7499 && do_write(eap) == FAIL)
7500 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007501 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 {
7503 not_exiting();
7504 }
7505 else
7506 {
7507#ifdef FEAT_WINDOWS
7508 if (only_one_window()) /* quit last window, exit Vim */
7509#endif
7510 getout(0);
7511#ifdef FEAT_WINDOWS
7512# ifdef FEAT_GUI
7513 need_mouse_correct = TRUE;
7514# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007515 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 win_close(curwin, !P_HID(curwin->w_buffer));
7517#endif
7518 }
7519}
7520
7521/*
7522 * ":print", ":list", ":number".
7523 */
7524 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007525ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007527 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7528 EMSG(_(e_emptybuf));
7529 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007531 for ( ;!got_int; ui_breakcheck())
7532 {
7533 print_line(eap->line1,
7534 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7535 || (eap->flags & EXFLAG_NR)),
7536 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7537 if (++eap->line1 > eap->line2)
7538 break;
7539 out_flush(); /* show one line at a time */
7540 }
7541 setpcmark();
7542 /* put cursor at last line */
7543 curwin->w_cursor.lnum = eap->line2;
7544 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 }
7546
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548}
7549
7550#ifdef FEAT_BYTEOFF
7551 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007552ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553{
7554 goto_byte(eap->line2);
7555}
7556#endif
7557
7558/*
7559 * ":shell".
7560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007562ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563{
7564 do_shell(NULL, 0);
7565}
7566
7567#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7568 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007569 || defined(FEAT_GUI_MSWIN) \
7570 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 || defined(PROTO)
7572
7573/*
7574 * Handle a file drop. The code is here because a drop is *nearly* like an
7575 * :args command, but not quite (we have a list of exact filenames, so we
7576 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7577 * code is very similar to :args and hence needs access to a lot of the static
7578 * functions in this file.
7579 *
7580 * The list should be allocated using alloc(), as should each item in the
7581 * list. This function takes over responsibility for freeing the list.
7582 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007583 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7585 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7586 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7587 * file functionality is (currently) not in EMX this is not presently a
7588 * problem.
7589 */
7590 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007591handle_drop(
7592 int filec, /* the number of files dropped */
7593 char_u **filev, /* the list of files dropped */
7594 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595{
7596 exarg_T ea;
7597 int save_msg_scroll = msg_scroll;
7598
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007599 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007600 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007602#ifdef FEAT_AUTOCMD
7603 if (curbuf_locked())
7604 return;
7605#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007606 /* When the screen is being updated we should not change buffers and
7607 * windows structures, it may cause freed memory to be used. */
7608 if (updating_screen)
7609 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610
7611 /* Check whether the current buffer is changed. If so, we will need
7612 * to split the current window or data could be lost.
7613 * We don't need to check if the 'hidden' option is set, as in this
7614 * case the buffer won't be lost.
7615 */
7616 if (!P_HID(curbuf) && !split)
7617 {
7618 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007619 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 --emsg_off;
7621 }
7622 if (split)
7623 {
7624# ifdef FEAT_WINDOWS
7625 if (win_split(0, 0) == FAIL)
7626 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007627 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628
7629 /* When splitting the window, create a new alist. Otherwise the
7630 * existing one is overwritten. */
7631 alist_unlink(curwin->w_alist);
7632 alist_new();
7633# else
7634 return; /* can't split, always fail */
7635# endif
7636 }
7637
7638 /*
7639 * Set up the new argument list.
7640 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007641 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642
7643 /*
7644 * Move to the first file.
7645 */
7646 /* Fake up a minimal "next" command for do_argfile() */
7647 vim_memset(&ea, 0, sizeof(ea));
7648 ea.cmd = (char_u *)"next";
7649 do_argfile(&ea, 0);
7650
7651 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7652 * mode that is not needed here. */
7653 need_start_insertmode = FALSE;
7654
7655 /* Restore msg_scroll, otherwise a following command may cause scrolling
7656 * unexpectedly. The screen will be redrawn by the caller, thus
7657 * msg_scroll being set by displaying a message is irrelevant. */
7658 msg_scroll = save_msg_scroll;
7659}
7660#endif
7661
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662/*
7663 * Clear an argument list: free all file names and reset it to zero entries.
7664 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007665 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007666alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667{
7668 while (--al->al_ga.ga_len >= 0)
7669 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7670 ga_clear(&al->al_ga);
7671}
7672
7673/*
7674 * Init an argument list.
7675 */
7676 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007677alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678{
7679 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7680}
7681
7682#if defined(FEAT_WINDOWS) || defined(PROTO)
7683
7684/*
7685 * Remove a reference from an argument list.
7686 * Ignored when the argument list is the global one.
7687 * If the argument list is no longer used by any window, free it.
7688 */
7689 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007690alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691{
7692 if (al != &global_alist && --al->al_refcount <= 0)
7693 {
7694 alist_clear(al);
7695 vim_free(al);
7696 }
7697}
7698
7699# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7700/*
7701 * Create a new argument list and use it for the current window.
7702 */
7703 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007704alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705{
7706 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7707 if (curwin->w_alist == NULL)
7708 {
7709 curwin->w_alist = &global_alist;
7710 ++global_alist.al_refcount;
7711 }
7712 else
7713 {
7714 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007715 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 alist_init(curwin->w_alist);
7717 }
7718}
7719# endif
7720#endif
7721
Bram Moolenaar53076832015-12-31 19:53:21 +01007722#if (!defined(UNIX) && !defined(__EMX__)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723/*
7724 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007725 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7726 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 */
7728 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007729alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730{
7731 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007732 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 char_u **new_arg_files;
7734 int new_arg_file_count;
7735 char_u *save_p_su = p_su;
7736 int i;
7737
7738 /* Don't use 'suffixes' here. This should work like the shell did the
7739 * expansion. Also, the vimrc file isn't read yet, thus the user
7740 * can't set the options. */
7741 p_su = empty_option;
7742 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7743 if (old_arg_files != NULL)
7744 {
7745 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007746 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7747 old_arg_count = GARGCOUNT;
7748 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007750 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 && new_arg_file_count > 0)
7752 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007753 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7754 TRUE, fnum_list, fnum_len);
7755 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 }
7758 p_su = save_p_su;
7759}
7760#endif
7761
7762/*
7763 * Set the argument list for the current window.
7764 * Takes over the allocated files[] and the allocated fnames in it.
7765 */
7766 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007767alist_set(
7768 alist_T *al,
7769 int count,
7770 char_u **files,
7771 int use_curbuf,
7772 int *fnum_list,
7773 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774{
7775 int i;
7776
7777 alist_clear(al);
7778 if (ga_grow(&al->al_ga, count) == OK)
7779 {
7780 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007781 {
7782 if (got_int)
7783 {
7784 /* When adding many buffers this can take a long time. Allow
7785 * interrupting here. */
7786 while (i < count)
7787 vim_free(files[i++]);
7788 break;
7789 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007790
7791 /* May set buffer name of a buffer previously used for the
7792 * argument list, so that it's re-used by alist_add. */
7793 if (fnum_list != NULL && i < fnum_len)
7794 buf_set_name(fnum_list[i], files[i]);
7795
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007797 ui_breakcheck();
7798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 vim_free(files);
7800 }
7801 else
7802 FreeWild(count, files);
7803#ifdef FEAT_WINDOWS
7804 if (al == &global_alist)
7805#endif
7806 arg_had_last = FALSE;
7807}
7808
7809/*
7810 * Add file "fname" to argument list "al".
7811 * "fname" must have been allocated and "al" must have been checked for room.
7812 */
7813 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007814alist_add(
7815 alist_T *al,
7816 char_u *fname,
7817 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818{
7819 if (fname == NULL) /* don't add NULL file names */
7820 return;
7821#ifdef BACKSLASH_IN_FILENAME
7822 slash_adjust(fname);
7823#endif
7824 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7825 if (set_fnum > 0)
7826 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7827 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7828 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829}
7830
7831#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7832/*
7833 * Adjust slashes in file names. Called after 'shellslash' was set.
7834 */
7835 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007836alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837{
7838 int i;
7839# ifdef FEAT_WINDOWS
7840 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007841 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842# endif
7843
7844 for (i = 0; i < GARGCOUNT; ++i)
7845 if (GARGLIST[i].ae_fname != NULL)
7846 slash_adjust(GARGLIST[i].ae_fname);
7847# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007848 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 if (wp->w_alist != &global_alist)
7850 for (i = 0; i < WARGCOUNT(wp); ++i)
7851 if (WARGLIST(wp)[i].ae_fname != NULL)
7852 slash_adjust(WARGLIST(wp)[i].ae_fname);
7853# endif
7854}
7855#endif
7856
7857/*
7858 * ":preserve".
7859 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007861ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007863 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 ml_preserve(curbuf, TRUE);
7865}
7866
7867/*
7868 * ":recover".
7869 */
7870 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007871ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872{
7873 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7874 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007875 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7876 | CCGD_MULTWIN
7877 | (eap->forceit ? CCGD_FORCEIT : 0)
7878 | CCGD_EXCMD)
7879
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 && (*eap->arg == NUL
7881 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7882 ml_recover();
7883 recoverymode = FALSE;
7884}
7885
7886/*
7887 * Command modifier used in a wrong way.
7888 */
7889 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007890ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891{
7892 eap->errmsg = e_invcmd;
7893}
7894
7895#ifdef FEAT_WINDOWS
7896/*
7897 * :sview [+command] file split window with new file, read-only
7898 * :split [[+command] file] split window with current or new file
7899 * :vsplit [[+command] file] split window vertically with current or new file
7900 * :new [[+command] file] split window with no or new file
7901 * :vnew [[+command] file] split vertically window with no or new file
7902 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007903 *
7904 * :tabedit open new Tab page with empty window
7905 * :tabedit [+command] file open new Tab page and edit "file"
7906 * :tabnew [[+command] file] just like :tabedit
7907 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 */
7909 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007910ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007912 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007913# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007915# endif
7916# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007918# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007920# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7922 {
7923 ex_ni(eap);
7924 return;
7925 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007926# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007928# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007930# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007932# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007934 * quickfix windows. But it's OK when doing ":tab split". */
7935 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 {
7937 if (eap->cmdidx == CMD_split)
7938 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007939# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 if (eap->cmdidx == CMD_vsplit)
7941 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007942# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007944# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007946# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007947 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 {
7949 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7950 FNAME_MESS, TRUE, curbuf->b_ffname);
7951 if (fname == NULL)
7952 goto theend;
7953 eap->arg = fname;
7954 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007955# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007957# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007959# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007961# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007963# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 && eap->cmdidx != CMD_new)
7965 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007966# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007967 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007968# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007969 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007970# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007971 au_has_group((char_u *)"FileExplorer"))
7972 {
7973 /* No browsing supported but we do have the file explorer:
7974 * Edit the directory. */
7975 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7976 eap->arg = (char_u *)".";
7977 }
7978 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007979# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007980 {
7981 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007983 if (fname == NULL)
7984 goto theend;
7985 eap->arg = fname;
7986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 }
7988 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007989# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007991 /*
7992 * Either open new tab page or split the window.
7993 */
7994 if (eap->cmdidx == CMD_tabedit
7995 || eap->cmdidx == CMD_tabfind
7996 || eap->cmdidx == CMD_tabnew)
7997 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007998 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7999 : eap->addr_count == 0 ? 0
8000 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008001 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008002 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008003
8004 /* set the alternate buffer for the window we came from */
8005 if (curwin != old_curwin
8006 && win_valid(old_curwin)
8007 && old_curwin->w_buffer != curbuf
8008 && !cmdmod.keepalt)
8009 old_curwin->w_alt_fnum = curbuf->b_fnum;
8010 }
8011 }
8012 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8014 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008015# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 /* Reset 'scrollbind' when editing another file, but keep it when
8017 * doing ":split" without arguments. */
8018 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008019# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008021# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008023 {
8024 RESET_BINDING(curwin);
8025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 else
8027 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008028# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 do_exedit(eap, old_curwin);
8030 }
8031
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008032# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008034# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008036# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037theend:
8038 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008039# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008041
8042/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008043 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008044 */
8045 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008046tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008047{
8048 exarg_T ea;
8049
8050 vim_memset(&ea, 0, sizeof(ea));
8051 ea.cmdidx = CMD_tabnew;
8052 ea.cmd = (char_u *)"tabn";
8053 ea.arg = (char_u *)"";
8054 ex_splitview(&ea);
8055}
8056
8057/*
8058 * :tabnext command
8059 */
8060 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008061ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008062{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008063 switch (eap->cmdidx)
8064 {
8065 case CMD_tabfirst:
8066 case CMD_tabrewind:
8067 goto_tabpage(1);
8068 break;
8069 case CMD_tablast:
8070 goto_tabpage(9999);
8071 break;
8072 case CMD_tabprevious:
8073 case CMD_tabNext:
8074 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
8075 break;
8076 default: /* CMD_tabnext */
8077 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
8078 break;
8079 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008080}
8081
8082/*
8083 * :tabmove command
8084 */
8085 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008086ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008087{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008088 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008089
8090 if (eap->arg && *eap->arg != NUL)
8091 {
8092 char_u *p = eap->arg;
8093 int relative = 0; /* argument +N/-N means: move N places to the
8094 * right/left relative to the current position. */
8095
8096 if (*eap->arg == '-')
8097 {
8098 relative = -1;
8099 p = eap->arg + 1;
8100 }
8101 else if (*eap->arg == '+')
8102 {
8103 relative = 1;
8104 p = eap->arg + 1;
8105 }
8106 else
8107 p = eap->arg;
8108
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008109 if (relative == 0)
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008110 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008111 if (STRCMP(p, "$") == 0)
8112 tab_number = LAST_TAB_NR;
8113 else if (p == skipdigits(p))
8114 {
8115 /* No numbers as argument. */
8116 eap->errmsg = e_invarg;
8117 return;
8118 }
8119 else
8120 tab_number = getdigits(&p);
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008121 }
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008122 else
8123 {
8124 if (*p != NUL)
8125 tab_number = getdigits(&p);
8126 else
8127 tab_number = 1;
8128 tab_number = tab_number * relative + tabpage_index(curtab);
8129 if (relative == -1)
8130 --tab_number;
8131 }
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008132 }
8133 else if (eap->addr_count != 0)
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008134 {
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008135 tab_number = eap->line2;
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008136 if (**eap->cmdlinep == '-')
8137 --tab_number;
8138 }
8139 else
8140 tab_number = LAST_TAB_NR;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008141
8142 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008143}
8144
8145/*
8146 * :tabs command: List tabs and their contents.
8147 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008148 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008149ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008150{
8151 tabpage_T *tp;
8152 win_T *wp;
8153 int tabcount = 1;
8154
8155 msg_start();
8156 msg_scroll = TRUE;
8157 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8158 {
8159 msg_putchar('\n');
8160 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
8161 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
8162 out_flush(); /* output one line at a time */
8163 ui_breakcheck();
8164
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008165 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008166 wp = firstwin;
8167 else
8168 wp = tp->tp_firstwin;
8169 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8170 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008171 msg_putchar('\n');
8172 msg_putchar(wp == curwin ? '>' : ' ');
8173 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008174 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8175 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008176 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008177 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008178 else
8179 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8180 IObuff, IOSIZE, TRUE);
8181 msg_outtrans(IObuff);
8182 out_flush(); /* output one line at a time */
8183 ui_breakcheck();
8184 }
8185 }
8186}
8187
8188#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189
8190/*
8191 * ":mode": Set screen mode.
8192 * If no argument given, just get the screen size and redraw.
8193 */
8194 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008195ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196{
8197 if (*eap->arg == NUL)
8198 shell_resized();
8199 else
8200 mch_screenmode(eap->arg);
8201}
8202
8203#ifdef FEAT_WINDOWS
8204/*
8205 * ":resize".
8206 * set, increment or decrement current window height
8207 */
8208 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008209ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210{
8211 int n;
8212 win_T *wp = curwin;
8213
8214 if (eap->addr_count > 0)
8215 {
8216 n = eap->line2;
8217 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8218 ;
8219 }
8220
8221#ifdef FEAT_GUI
8222 need_mouse_correct = TRUE;
8223#endif
8224 n = atol((char *)eap->arg);
8225#ifdef FEAT_VERTSPLIT
8226 if (cmdmod.split & WSP_VERT)
8227 {
8228 if (*eap->arg == '-' || *eap->arg == '+')
8229 n += W_WIDTH(curwin);
8230 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8231 n = 9999;
8232 win_setwidth_win((int)n, wp);
8233 }
8234 else
8235#endif
8236 {
8237 if (*eap->arg == '-' || *eap->arg == '+')
8238 n += curwin->w_height;
8239 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8240 n = 9999;
8241 win_setheight_win((int)n, wp);
8242 }
8243}
8244#endif
8245
8246/*
8247 * ":find [+command] <file>" command.
8248 */
8249 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008250ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251{
8252#ifdef FEAT_SEARCHPATH
8253 char_u *fname;
8254 int count;
8255
8256 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8257 TRUE, curbuf->b_ffname);
8258 if (eap->addr_count > 0)
8259 {
8260 /* Repeat finding the file "count" times. This matters when it
8261 * appears several times in the path. */
8262 count = eap->line2;
8263 while (fname != NULL && --count > 0)
8264 {
8265 vim_free(fname);
8266 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8267 FALSE, curbuf->b_ffname);
8268 }
8269 }
8270
8271 if (fname != NULL)
8272 {
8273 eap->arg = fname;
8274#endif
8275 do_exedit(eap, NULL);
8276#ifdef FEAT_SEARCHPATH
8277 vim_free(fname);
8278 }
8279#endif
8280}
8281
8282/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008283 * ":open" simulation: for now just work like ":visual".
8284 */
8285 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008286ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008287{
8288 regmatch_T regmatch;
8289 char_u *p;
8290
8291 curwin->w_cursor.lnum = eap->line2;
8292 beginline(BL_SOL | BL_FIX);
8293 if (*eap->arg == '/')
8294 {
8295 /* ":open /pattern/": put cursor in column found with pattern */
8296 ++eap->arg;
8297 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8298 *p = NUL;
8299 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8300 if (regmatch.regprog != NULL)
8301 {
8302 regmatch.rm_ic = p_ic;
8303 p = ml_get_curline();
8304 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008305 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008306 else
8307 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008308 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008309 }
8310 /* Move to the NUL, ignore any other arguments. */
8311 eap->arg += STRLEN(eap->arg);
8312 }
8313 check_cursor();
8314
8315 eap->cmdidx = CMD_visual;
8316 do_exedit(eap, NULL);
8317}
8318
8319/*
8320 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 */
8322 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008323ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324{
8325 do_exedit(eap, NULL);
8326}
8327
8328/*
8329 * ":edit <file>" command and alikes.
8330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008332do_exedit(
8333 exarg_T *eap,
8334 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335{
8336 int n;
8337#ifdef FEAT_WINDOWS
8338 int need_hide;
8339#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008340 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341
8342 /*
8343 * ":vi" command ends Ex mode.
8344 */
8345 if (exmode_active && (eap->cmdidx == CMD_visual
8346 || eap->cmdidx == CMD_view))
8347 {
8348 exmode_active = FALSE;
8349 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008350 {
8351 /* Special case: ":global/pat/visual\NLvi-commands" */
8352 if (global_busy)
8353 {
8354 int rd = RedrawingDisabled;
8355 int nwr = no_wait_return;
8356 int ms = msg_scroll;
8357#ifdef FEAT_GUI
8358 int he = hold_gui_events;
8359#endif
8360
8361 if (eap->nextcmd != NULL)
8362 {
8363 stuffReadbuff(eap->nextcmd);
8364 eap->nextcmd = NULL;
8365 }
8366
8367 if (exmode_was != EXMODE_VIM)
8368 settmode(TMODE_RAW);
8369 RedrawingDisabled = 0;
8370 no_wait_return = 0;
8371 need_wait_return = FALSE;
8372 msg_scroll = 0;
8373#ifdef FEAT_GUI
8374 hold_gui_events = 0;
8375#endif
8376 must_redraw = CLEAR;
8377
8378 main_loop(FALSE, TRUE);
8379
8380 RedrawingDisabled = rd;
8381 no_wait_return = nwr;
8382 msg_scroll = ms;
8383#ifdef FEAT_GUI
8384 hold_gui_events = he;
8385#endif
8386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 }
8390
8391 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008392 || eap->cmdidx == CMD_tabnew
8393 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394#ifdef FEAT_VERTSPLIT
8395 || eap->cmdidx == CMD_vnew
8396#endif
8397 ) && *eap->arg == NUL)
8398 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008399 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 setpcmark();
8401 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008402 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8403 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 }
8405 else if ((eap->cmdidx != CMD_split
8406#ifdef FEAT_VERTSPLIT
8407 && eap->cmdidx != CMD_vsplit
8408#endif
8409 )
8410 || *eap->arg != NUL
8411#ifdef FEAT_BROWSE
8412 || cmdmod.browse
8413#endif
8414 )
8415 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008416#ifdef FEAT_AUTOCMD
8417 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8418 * can bring us here, others are stopped earlier. */
8419 if (*eap->arg != NUL && curbuf_locked())
8420 return;
8421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 n = readonlymode;
8423 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8424 readonlymode = TRUE;
8425 else if (eap->cmdidx == CMD_enew)
8426 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8427 empty buffer */
8428 setpcmark();
8429 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8430 NULL, eap,
8431 /* ":edit" goes to first line if Vi compatible */
8432 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8433 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8434 ? ECMD_ONE : eap->do_ecmd_lnum,
8435 (P_HID(curbuf) ? ECMD_HIDE : 0)
8436 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008437 /* after a split we can use an existing buffer */
8438 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439#ifdef FEAT_LISTCMDS
8440 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8441#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008442 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 {
8444 /* Editing the file failed. If the window was split, close it. */
8445#ifdef FEAT_WINDOWS
8446 if (old_curwin != NULL)
8447 {
8448 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8449 if (!need_hide || P_HID(curbuf))
8450 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008451# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8452 cleanup_T cs;
8453
8454 /* Reset the error/interrupt/exception state here so that
8455 * aborting() returns FALSE when closing a window. */
8456 enter_cleanup(&cs);
8457# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458# ifdef FEAT_GUI
8459 need_mouse_correct = TRUE;
8460# endif
8461 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008462
8463# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8464 /* Restore the error/interrupt/exception state if not
8465 * discarded by a new aborting error, interrupt, or
8466 * uncaught exception. */
8467 leave_cleanup(&cs);
8468# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 }
8470 }
8471#endif
8472 }
8473 else if (readonlymode && curbuf->b_nwindows == 1)
8474 {
8475 /* When editing an already visited buffer, 'readonly' won't be set
8476 * but the previous value is kept. With ":view" and ":sview" we
8477 * want the file to be readonly, except when another window is
8478 * editing the same buffer. */
8479 curbuf->b_p_ro = TRUE;
8480 }
8481 readonlymode = n;
8482 }
8483 else
8484 {
8485 if (eap->do_ecmd_cmd != NULL)
8486 do_cmdline_cmd(eap->do_ecmd_cmd);
8487#ifdef FEAT_TITLE
8488 n = curwin->w_arg_idx_invalid;
8489#endif
8490 check_arg_idx(curwin);
8491#ifdef FEAT_TITLE
8492 if (n != curwin->w_arg_idx_invalid)
8493 maketitle();
8494#endif
8495 }
8496
8497#ifdef FEAT_WINDOWS
8498 /*
8499 * if ":split file" worked, set alternate file name in old window to new
8500 * file
8501 */
8502 if (old_curwin != NULL
8503 && *eap->arg != NUL
8504 && curwin != old_curwin
8505 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008506 && old_curwin->w_buffer != curbuf
8507 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 old_curwin->w_alt_fnum = curbuf->b_fnum;
8509#endif
8510
8511 ex_no_reprint = TRUE;
8512}
8513
8514#ifndef FEAT_GUI
8515/*
8516 * ":gui" and ":gvim" when there is no GUI.
8517 */
8518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008519ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520{
8521 eap->errmsg = e_nogvim;
8522}
8523#endif
8524
8525#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8526 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008527ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528{
8529 gui_make_tearoff(eap->arg);
8530}
8531#endif
8532
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008533#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008535ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008537 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538}
8539#endif
8540
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008542ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543{
8544 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8545 MSG(_("No swap file"));
8546 else
8547 msg(curbuf->b_ml.ml_mfp->mf_fname);
8548}
8549
8550/*
8551 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8552 * offset.
8553 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8554 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008556ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557{
8558#ifdef FEAT_SCROLLBIND
8559 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008560 win_T *save_curwin = curwin;
8561 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 long topline;
8563 long y;
8564 linenr_T old_linenr = curwin->w_cursor.lnum;
8565
8566 setpcmark();
8567
8568 /*
8569 * determine max topline
8570 */
8571 if (curwin->w_p_scb)
8572 {
8573 topline = curwin->w_topline;
8574 for (wp = firstwin; wp; wp = wp->w_next)
8575 {
8576 if (wp->w_p_scb && wp->w_buffer)
8577 {
8578 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8579 if (topline > y)
8580 topline = y;
8581 }
8582 }
8583 if (topline < 1)
8584 topline = 1;
8585 }
8586 else
8587 {
8588 topline = 1;
8589 }
8590
8591
8592 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008593 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8596 {
8597 if (curwin->w_p_scb)
8598 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008599 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 y = topline - curwin->w_topline;
8601 if (y > 0)
8602 scrollup(y, TRUE);
8603 else
8604 scrolldown(-y, TRUE);
8605 curwin->w_scbind_pos = topline;
8606 redraw_later(VALID);
8607 cursor_correct();
8608#ifdef FEAT_WINDOWS
8609 curwin->w_redr_status = TRUE;
8610#endif
8611 }
8612 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008613 curwin = save_curwin;
8614 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 if (curwin->w_p_scb)
8616 {
8617 did_syncbind = TRUE;
8618 checkpcmark();
8619 if (old_linenr != curwin->w_cursor.lnum)
8620 {
8621 char_u ctrl_o[2];
8622
8623 ctrl_o[0] = Ctrl_O;
8624 ctrl_o[1] = 0;
8625 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8626 }
8627 }
8628#endif
8629}
8630
8631
8632 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008633ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008635 int i;
8636 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8637 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638
8639 if (eap->usefilter) /* :r!cmd */
8640 do_bang(1, eap, FALSE, FALSE, TRUE);
8641 else
8642 {
8643 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8644 return;
8645
8646#ifdef FEAT_BROWSE
8647 if (cmdmod.browse)
8648 {
8649 char_u *browseFile;
8650
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008651 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 NULL, NULL, NULL, curbuf);
8653 if (browseFile != NULL)
8654 {
8655 i = readfile(browseFile, NULL,
8656 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8657 vim_free(browseFile);
8658 }
8659 else
8660 i = OK;
8661 }
8662 else
8663#endif
8664 if (*eap->arg == NUL)
8665 {
8666 if (check_fname() == FAIL) /* check for no file name */
8667 return;
8668 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8669 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8670 }
8671 else
8672 {
8673 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8674 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8675 i = readfile(eap->arg, NULL,
8676 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8677
8678 }
8679 if (i == FAIL)
8680 {
8681#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8682 if (!aborting())
8683#endif
8684 EMSG2(_(e_notopen), eap->arg);
8685 }
8686 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008687 {
8688 if (empty && exmode_active)
8689 {
8690 /* Delete the empty line that remains. Historically ex does
8691 * this but vi doesn't. */
8692 if (eap->line2 == 0)
8693 lnum = curbuf->b_ml.ml_line_count;
8694 else
8695 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008696 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008697 {
8698 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008699 if (curwin->w_cursor.lnum > 1
8700 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008701 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008702 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008703 }
8704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 }
8708}
8709
Bram Moolenaarea408852005-06-25 22:49:46 +00008710static char_u *prev_dir = NULL;
8711
8712#if defined(EXITFREE) || defined(PROTO)
8713 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008714free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00008715{
8716 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008717 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008718
8719 vim_free(globaldir);
8720 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008721}
8722#endif
8723
Bram Moolenaarf4258302013-06-02 18:20:17 +02008724/*
8725 * Deal with the side effects of changing the current directory.
8726 * When "local" is TRUE then this was after an ":lcd" command.
8727 */
8728 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008729post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008730{
8731 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008732 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008733 if (local)
8734 {
8735 /* If still in global directory, need to remember current
8736 * directory as global directory. */
8737 if (globaldir == NULL && prev_dir != NULL)
8738 globaldir = vim_strsave(prev_dir);
8739 /* Remember this local directory for the window. */
8740 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8741 curwin->w_localdir = vim_strsave(NameBuff);
8742 }
8743 else
8744 {
8745 /* We are now in the global directory, no need to remember its
8746 * name. */
8747 vim_free(globaldir);
8748 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008749 }
8750
8751 shorten_fnames(TRUE);
8752}
8753
Bram Moolenaarea408852005-06-25 22:49:46 +00008754
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755/*
8756 * ":cd", ":lcd", ":chdir" and ":lchdir".
8757 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008758 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008759ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 char_u *new_dir;
8762 char_u *tofree;
8763
8764 new_dir = eap->arg;
8765#if !defined(UNIX) && !defined(VMS)
8766 /* for non-UNIX ":cd" means: print current directory */
8767 if (*new_dir == NUL)
8768 ex_pwd(NULL);
8769 else
8770#endif
8771 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008772#ifdef FEAT_AUTOCMD
8773 if (allbuf_locked())
8774 return;
8775#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008776 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8777 && !eap->forceit)
8778 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008779 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008780 return;
8781 }
8782
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 /* ":cd -": Change to previous directory */
8784 if (STRCMP(new_dir, "-") == 0)
8785 {
8786 if (prev_dir == NULL)
8787 {
8788 EMSG(_("E186: No previous directory"));
8789 return;
8790 }
8791 new_dir = prev_dir;
8792 }
8793
8794 /* Save current directory for next ":cd -" */
8795 tofree = prev_dir;
8796 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8797 prev_dir = vim_strsave(NameBuff);
8798 else
8799 prev_dir = NULL;
8800
8801#if defined(UNIX) || defined(VMS)
8802 /* for UNIX ":cd" means: go to home directory */
8803 if (*new_dir == NUL)
8804 {
8805 /* use NameBuff for home directory name */
8806# ifdef VMS
8807 char_u *p;
8808
8809 p = mch_getenv((char_u *)"SYS$LOGIN");
8810 if (p == NULL || *p == NUL) /* empty is the same as not set */
8811 NameBuff[0] = NUL;
8812 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008813 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814# else
8815 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8816# endif
8817 new_dir = NameBuff;
8818 }
8819#endif
8820 if (new_dir == NULL || vim_chdir(new_dir))
8821 EMSG(_(e_failed));
8822 else
8823 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008824 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825
8826 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008827 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 ex_pwd(eap);
8829 }
8830 vim_free(tofree);
8831 }
8832}
8833
8834/*
8835 * ":pwd".
8836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008838ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839{
8840 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8841 {
8842#ifdef BACKSLASH_IN_FILENAME
8843 slash_adjust(NameBuff);
8844#endif
8845 msg(NameBuff);
8846 }
8847 else
8848 EMSG(_("E187: Unknown"));
8849}
8850
8851/*
8852 * ":=".
8853 */
8854 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008855ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856{
8857 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008858 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859}
8860
8861 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008862ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008864 int n;
8865 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866
8867 if (cursor_valid())
8868 {
8869 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8870 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01008871 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008873
8874 len = eap->line2;
8875 switch (*eap->arg)
8876 {
8877 case 'm': break;
8878 case NUL: len *= 1000L; break;
8879 default: EMSG2(_(e_invarg2), eap->arg); return;
8880 }
8881 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882}
8883
8884/*
8885 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8886 */
8887 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008888do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889{
8890 long done;
8891
8892 cursor_on();
8893 out_flush();
8894 for (done = 0; !got_int && done < msec; done += 1000L)
8895 {
8896 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8897 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008898#ifdef MESSAGE_QUEUE
8899 /* Process the netbeans and clientserver messages that may have been
8900 * received in the call to ui_breakcheck() when the GUI is in use. This
8901 * may occur when running a test case. */
8902 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 }
8905}
8906
8907 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008908do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909{
8910 int mode;
8911 char_u *cmdp;
8912
8913 cmdp = eap->cmd;
8914 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8915
8916 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8917 eap->arg, mode, isabbrev))
8918 {
8919 case 1: EMSG(_(e_invarg));
8920 break;
8921 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8922 break;
8923 }
8924}
8925
8926/*
8927 * ":winsize" command (obsolete).
8928 */
8929 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008930ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931{
8932 int w, h;
8933 char_u *arg = eap->arg;
8934 char_u *p;
8935
8936 w = getdigits(&arg);
8937 arg = skipwhite(arg);
8938 p = arg;
8939 h = getdigits(&arg);
8940 if (*p != NUL && *arg == NUL)
8941 set_shellsize(w, h, TRUE);
8942 else
8943 EMSG(_("E465: :winsize requires two number arguments"));
8944}
8945
8946#ifdef FEAT_WINDOWS
8947 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008948ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949{
8950 int xchar = NUL;
8951 char_u *p;
8952
8953 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8954 {
8955 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8956 if (eap->arg[1] == NUL)
8957 {
8958 EMSG(_(e_invarg));
8959 return;
8960 }
8961 xchar = eap->arg[1];
8962 p = eap->arg + 2;
8963 }
8964 else
8965 p = eap->arg + 1;
8966
8967 eap->nextcmd = check_nextcmd(p);
8968 p = skipwhite(p);
8969 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8970 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008971 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 {
8973 /* Pass flags on for ":vertical wincmd ]". */
8974 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008975 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8977 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008978 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 }
8980}
8981#endif
8982
Bram Moolenaar843ee412004-06-30 16:16:41 +00008983#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984/*
8985 * ":winpos".
8986 */
8987 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008988ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989{
8990 int x, y;
8991 char_u *arg = eap->arg;
8992 char_u *p;
8993
8994 if (*arg == NUL)
8995 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008996# if defined(FEAT_GUI) || defined(MSWIN)
8997# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008999# else
9000 if (mch_get_winpos(&x, &y) != FAIL)
9001# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 {
9003 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9004 msg(IObuff);
9005 }
9006 else
9007# endif
9008 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9009 }
9010 else
9011 {
9012 x = getdigits(&arg);
9013 arg = skipwhite(arg);
9014 p = arg;
9015 y = getdigits(&arg);
9016 if (*p == NUL || *arg != NUL)
9017 {
9018 EMSG(_("E466: :winpos requires two number arguments"));
9019 return;
9020 }
9021# ifdef FEAT_GUI
9022 if (gui.in_use)
9023 gui_mch_set_winpos(x, y);
9024 else if (gui.starting)
9025 {
9026 /* Remember the coordinates for when the window is opened. */
9027 gui_win_x = x;
9028 gui_win_y = y;
9029 }
9030# ifdef HAVE_TGETENT
9031 else
9032# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009033# else
9034# ifdef MSWIN
9035 mch_set_winpos(x, y);
9036# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037# endif
9038# ifdef HAVE_TGETENT
9039 if (*T_CWP)
9040 term_set_winpos(x, y);
9041# endif
9042 }
9043}
9044#endif
9045
9046/*
9047 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9048 */
9049 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009050ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051{
9052 oparg_T oa;
9053
9054 clear_oparg(&oa);
9055 oa.regname = eap->regname;
9056 oa.start.lnum = eap->line1;
9057 oa.end.lnum = eap->line2;
9058 oa.line_count = eap->line2 - eap->line1 + 1;
9059 oa.motion_type = MLINE;
9060#ifdef FEAT_VIRTUALEDIT
9061 virtual_op = FALSE;
9062#endif
9063 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9064 {
9065 setpcmark();
9066 curwin->w_cursor.lnum = eap->line1;
9067 beginline(BL_SOL | BL_FIX);
9068 }
9069
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009070 if (VIsual_active)
9071 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009072
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 switch (eap->cmdidx)
9074 {
9075 case CMD_delete:
9076 oa.op_type = OP_DELETE;
9077 op_delete(&oa);
9078 break;
9079
9080 case CMD_yank:
9081 oa.op_type = OP_YANK;
9082 (void)op_yank(&oa, FALSE, TRUE);
9083 break;
9084
9085 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009086 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009088 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9089#else
9090 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009092 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 oa.op_type = OP_RSHIFT;
9094 else
9095 oa.op_type = OP_LSHIFT;
9096 op_shift(&oa, FALSE, eap->amount);
9097 break;
9098 }
9099#ifdef FEAT_VIRTUALEDIT
9100 virtual_op = MAYBE;
9101#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009102 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103}
9104
9105/*
9106 * ":put".
9107 */
9108 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009109ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110{
9111 /* ":0put" works like ":1put!". */
9112 if (eap->line2 == 0)
9113 {
9114 eap->line2 = 1;
9115 eap->forceit = TRUE;
9116 }
9117 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009118 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9119 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120}
9121
9122/*
9123 * Handle ":copy" and ":move".
9124 */
9125 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009126ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127{
9128 long n;
9129
Bram Moolenaaraa23b372015-09-08 18:46:31 +02009130 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 if (eap->arg == NULL) /* error detected */
9132 {
9133 eap->nextcmd = NULL;
9134 return;
9135 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009136 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137
9138 /*
9139 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9140 */
9141 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9142 {
9143 EMSG(_(e_invaddr));
9144 return;
9145 }
9146
9147 if (eap->cmdidx == CMD_move)
9148 {
9149 if (do_move(eap->line1, eap->line2, n) == FAIL)
9150 return;
9151 }
9152 else
9153 ex_copy(eap->line1, eap->line2, n);
9154 u_clearline();
9155 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009156 ex_may_print(eap);
9157}
9158
9159/*
9160 * Print the current line if flags were given to the Ex command.
9161 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009162 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009163ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009164{
9165 if (eap->flags != 0)
9166 {
9167 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9168 (eap->flags & EXFLAG_LIST));
9169 ex_no_reprint = TRUE;
9170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171}
9172
9173/*
9174 * ":smagic" and ":snomagic".
9175 */
9176 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009177ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178{
9179 int magic_save = p_magic;
9180
9181 p_magic = (eap->cmdidx == CMD_smagic);
9182 do_sub(eap);
9183 p_magic = magic_save;
9184}
9185
9186/*
9187 * ":join".
9188 */
9189 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009190ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191{
9192 curwin->w_cursor.lnum = eap->line1;
9193 if (eap->line1 == eap->line2)
9194 {
9195 if (eap->addr_count >= 2) /* :2,2join does nothing */
9196 return;
9197 if (eap->line2 == curbuf->b_ml.ml_line_count)
9198 {
9199 beep_flush();
9200 return;
9201 }
9202 ++eap->line2;
9203 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009204 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009206 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207}
9208
9209/*
9210 * ":[addr]@r" or ":[addr]*r": execute register
9211 */
9212 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009213ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214{
9215 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009216 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217
9218 curwin->w_cursor.lnum = eap->line2;
9219
9220#ifdef USE_ON_FLY_SCROLL
9221 dont_scroll = TRUE; /* disallow scrolling here */
9222#endif
9223
9224 /* get the register name. No name means to use the previous one */
9225 c = *eap->arg;
9226 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9227 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009228 /* Put the register in the typeahead buffer with the "silent" flag. */
9229 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9230 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009231 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 else
9235 {
9236 int save_efr = exec_from_reg;
9237
9238 exec_from_reg = TRUE;
9239
9240 /*
9241 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009242 * Continue until the stuff buffer is empty and all added characters
9243 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009245 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9247
9248 exec_from_reg = save_efr;
9249 }
9250}
9251
9252/*
9253 * ":!".
9254 */
9255 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009256ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257{
9258 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9259}
9260
9261/*
9262 * ":undo".
9263 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009265ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009267 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009268 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009269 else
9270 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271}
9272
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009273#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009274 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009275ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009276{
9277 char_u hash[UNDO_HASH_SIZE];
9278
9279 u_compute_hash(hash);
9280 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9281}
9282
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009283 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009284ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009285{
9286 char_u hash[UNDO_HASH_SIZE];
9287
9288 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009289 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009290}
9291#endif
9292
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293/*
9294 * ":redo".
9295 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009297ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298{
9299 u_redo(1);
9300}
9301
9302/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009303 * ":earlier" and ":later".
9304 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009305 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009306ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009307{
9308 long count = 0;
9309 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009310 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009311 char_u *p = eap->arg;
9312
9313 if (*p == NUL)
9314 count = 1;
9315 else if (isdigit(*p))
9316 {
9317 count = getdigits(&p);
9318 switch (*p)
9319 {
9320 case 's': ++p; sec = TRUE; break;
9321 case 'm': ++p; sec = TRUE; count *= 60; break;
9322 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009323 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9324 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009325 }
9326 }
9327
9328 if (*p != NUL)
9329 EMSG2(_(e_invarg2), eap->arg);
9330 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009331 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9332 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009333}
9334
9335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 * ":redir": start/stop redirection.
9337 */
9338 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009339ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340{
9341 char *mode;
9342 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009343 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344
9345 if (STRICMP(eap->arg, "END") == 0)
9346 close_redir();
9347 else
9348 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009349 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009351 ++arg;
9352 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009354 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 mode = "a";
9356 }
9357 else
9358 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009359 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360
9361 close_redir();
9362
9363 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009364 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 if (fname == NULL)
9366 return;
9367#ifdef FEAT_BROWSE
9368 if (cmdmod.browse)
9369 {
9370 char_u *browseFile;
9371
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009372 browseFile = do_browse(BROWSE_SAVE,
9373 (char_u *)_("Save Redirection"),
9374 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 if (browseFile == NULL)
9376 return; /* operation cancelled */
9377 vim_free(fname);
9378 fname = browseFile;
9379 eap->forceit = TRUE; /* since dialog already asked */
9380 }
9381#endif
9382
9383 redir_fd = open_exfile(fname, eap->forceit, mode);
9384 vim_free(fname);
9385 }
9386#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009387 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 {
9389 /* redirect to a register a-z (resp. A-Z for appending) */
9390 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009391 ++arg;
9392 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009394 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009395 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009397 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009399 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009400 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009401 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009402 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009404 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009405 if (*arg == '>')
9406 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009407 /* Make register empty when not using @A-@Z and the
9408 * command is valid. */
9409 if (*arg == NUL && !isupper(redir_reg))
9410 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009412 }
9413 if (*arg != NUL)
9414 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009415 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009416 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009417 }
9418 }
9419 else if (*arg == '=' && arg[1] == '>')
9420 {
9421 int append;
9422
9423 /* redirect to a variable */
9424 close_redir();
9425 arg += 2;
9426
9427 if (*arg == '>')
9428 {
9429 ++arg;
9430 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 }
9432 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009433 append = FALSE;
9434
9435 if (var_redir_start(skipwhite(arg), append) == OK)
9436 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437 }
9438#endif
9439
9440 /* TODO: redirect to a buffer */
9441
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009443 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009445
9446 /* Make sure redirection is not off. Can happen for cmdline completion
9447 * that indirectly invokes a command to catch its output. */
9448 if (redir_fd != NULL
9449#ifdef FEAT_EVAL
9450 || redir_reg || redir_vname
9451#endif
9452 )
9453 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454}
9455
9456/*
9457 * ":redraw": force redraw
9458 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009459 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009460ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461{
9462 int r = RedrawingDisabled;
9463 int p = p_lz;
9464
9465 RedrawingDisabled = 0;
9466 p_lz = FALSE;
9467 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009468 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469#ifdef FEAT_TITLE
9470 if (need_maketitle)
9471 maketitle();
9472#endif
9473 RedrawingDisabled = r;
9474 p_lz = p;
9475
9476 /* Reset msg_didout, so that a message that's there is overwritten. */
9477 msg_didout = FALSE;
9478 msg_col = 0;
9479
Bram Moolenaar56667a52013-07-17 11:54:28 +02009480 /* No need to wait after an intentional redraw. */
9481 need_wait_return = FALSE;
9482
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 out_flush();
9484}
9485
9486/*
9487 * ":redrawstatus": force redraw of status line(s)
9488 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009490ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491{
9492#if defined(FEAT_WINDOWS)
9493 int r = RedrawingDisabled;
9494 int p = p_lz;
9495
9496 RedrawingDisabled = 0;
9497 p_lz = FALSE;
9498 if (eap->forceit)
9499 status_redraw_all();
9500 else
9501 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009502 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 RedrawingDisabled = r;
9504 p_lz = p;
9505 out_flush();
9506#endif
9507}
9508
9509 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009510close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511{
9512 if (redir_fd != NULL)
9513 {
9514 fclose(redir_fd);
9515 redir_fd = NULL;
9516 }
9517#ifdef FEAT_EVAL
9518 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009519 if (redir_vname)
9520 {
9521 var_redir_stop();
9522 redir_vname = 0;
9523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524#endif
9525}
9526
9527#if defined(FEAT_SESSION) && defined(USE_CRNL)
9528# define MKSESSION_NL
9529static int mksession_nl = FALSE; /* use NL only in put_eol() */
9530#endif
9531
9532/*
9533 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9534 */
9535 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009536ex_mkrc(
9537 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538{
9539 FILE *fd;
9540 int failed = FALSE;
9541 char_u *fname;
9542#ifdef FEAT_BROWSE
9543 char_u *browseFile = NULL;
9544#endif
9545#ifdef FEAT_SESSION
9546 int view_session = FALSE;
9547 int using_vdir = FALSE; /* using 'viewdir'? */
9548 char_u *viewFile = NULL;
9549 unsigned *flagp;
9550#endif
9551
9552 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9553 {
9554#ifdef FEAT_SESSION
9555 view_session = TRUE;
9556#else
9557 ex_ni(eap);
9558 return;
9559#endif
9560 }
9561
9562#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009563 /* Use the short file name until ":lcd" is used. We also don't use the
9564 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009565 did_lcd = FALSE;
9566
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9568 if (eap->cmdidx == CMD_mkview
9569 && (*eap->arg == NUL
9570 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9571 {
9572 eap->forceit = TRUE;
9573 fname = get_view_file(*eap->arg);
9574 if (fname == NULL)
9575 return;
9576 viewFile = fname;
9577 using_vdir = TRUE;
9578 }
9579 else
9580#endif
9581 if (*eap->arg != NUL)
9582 fname = eap->arg;
9583 else if (eap->cmdidx == CMD_mkvimrc)
9584 fname = (char_u *)VIMRC_FILE;
9585#ifdef FEAT_SESSION
9586 else if (eap->cmdidx == CMD_mksession)
9587 fname = (char_u *)SESSION_FILE;
9588#endif
9589 else
9590 fname = (char_u *)EXRC_FILE;
9591
9592#ifdef FEAT_BROWSE
9593 if (cmdmod.browse)
9594 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009595 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596# ifdef FEAT_SESSION
9597 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9598 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9599# endif
9600 (char_u *)_("Save Setup"),
9601 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9602 if (browseFile == NULL)
9603 goto theend;
9604 fname = browseFile;
9605 eap->forceit = TRUE; /* since dialog already asked */
9606 }
9607#endif
9608
9609#if defined(FEAT_SESSION) && defined(vim_mkdir)
9610 /* When using 'viewdir' may have to create the directory. */
9611 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009612 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613#endif
9614
9615 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9616 if (fd != NULL)
9617 {
9618#ifdef FEAT_SESSION
9619 if (eap->cmdidx == CMD_mkview)
9620 flagp = &vop_flags;
9621 else
9622 flagp = &ssop_flags;
9623#endif
9624
9625#ifdef MKSESSION_NL
9626 /* "unix" in 'sessionoptions': use NL line separator */
9627 if (view_session && (*flagp & SSOP_UNIX))
9628 mksession_nl = TRUE;
9629#endif
9630
9631 /* Write the version command for :mkvimrc */
9632 if (eap->cmdidx == CMD_mkvimrc)
9633 (void)put_line(fd, "version 6.0");
9634
9635#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009636 if (eap->cmdidx == CMD_mksession)
9637 {
9638 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9639 failed = TRUE;
9640 }
9641
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642 if (eap->cmdidx != CMD_mkview)
9643#endif
9644 {
9645 /* Write setting 'compatible' first, because it has side effects.
9646 * For that same reason only do it when needed. */
9647 if (p_cp)
9648 (void)put_line(fd, "if !&cp | set cp | endif");
9649 else
9650 (void)put_line(fd, "if &cp | set nocp | endif");
9651 }
9652
9653#ifdef FEAT_SESSION
9654 if (!view_session
9655 || (eap->cmdidx == CMD_mksession
9656 && (*flagp & SSOP_OPTIONS)))
9657#endif
9658 failed |= (makemap(fd, NULL) == FAIL
9659 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9660
9661#ifdef FEAT_SESSION
9662 if (!failed && view_session)
9663 {
9664 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9665 failed = TRUE;
9666 if (eap->cmdidx == CMD_mksession)
9667 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009668 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669
Bram Moolenaard9462e32011-04-11 21:35:11 +02009670 dirnow = alloc(MAXPATHL);
9671 if (dirnow == NULL)
9672 failed = TRUE;
9673 else
9674 {
9675 /*
9676 * Change to session file's dir.
9677 */
9678 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009680 *dirnow = NUL;
9681 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9682 {
9683 if (vim_chdirfile(fname) == OK)
9684 shorten_fnames(TRUE);
9685 }
9686 else if (*dirnow != NUL
9687 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9688 {
9689 if (mch_chdir((char *)globaldir) == 0)
9690 shorten_fnames(TRUE);
9691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692
Bram Moolenaard9462e32011-04-11 21:35:11 +02009693 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694
Bram Moolenaard9462e32011-04-11 21:35:11 +02009695 /* restore original dir */
9696 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009698 {
9699 if (mch_chdir((char *)dirnow) != 0)
9700 EMSG(_(e_prev_dir));
9701 shorten_fnames(TRUE);
9702 }
9703 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 }
9705 }
9706 else
9707 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009708 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9709 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 }
9711 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9712 == FAIL)
9713 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009714 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9715 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009716 if (eap->cmdidx == CMD_mksession)
9717 {
9718 if (put_line(fd, "unlet SessionLoad") == FAIL)
9719 failed = TRUE;
9720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 }
9722#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009723 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9724 failed = TRUE;
9725
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 failed |= fclose(fd);
9727
9728 if (failed)
9729 EMSG(_(e_write));
9730#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9731 else if (eap->cmdidx == CMD_mksession)
9732 {
9733 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009734 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735
Bram Moolenaard9462e32011-04-11 21:35:11 +02009736 tbuf = alloc(MAXPATHL);
9737 if (tbuf != NULL)
9738 {
9739 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9740 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9741 vim_free(tbuf);
9742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 }
9744#endif
9745#ifdef MKSESSION_NL
9746 mksession_nl = FALSE;
9747#endif
9748 }
9749
9750#ifdef FEAT_BROWSE
9751theend:
9752 vim_free(browseFile);
9753#endif
9754#ifdef FEAT_SESSION
9755 vim_free(viewFile);
9756#endif
9757}
9758
Bram Moolenaardf177f62005-02-22 08:39:57 +00009759#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9760 || defined(PROTO)
9761 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009762vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009763{
9764 if (vim_mkdir(name, prot) != 0)
9765 {
9766 EMSG2(_("E739: Cannot create directory: %s"), name);
9767 return FAIL;
9768 }
9769 return OK;
9770}
9771#endif
9772
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773/*
9774 * Open a file for writing for an Ex command, with some checks.
9775 * Return file descriptor, or NULL on failure.
9776 */
9777 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009778open_exfile(
9779 char_u *fname,
9780 int forceit,
9781 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782{
9783 FILE *fd;
9784
9785#ifdef UNIX
9786 /* with Unix it is possible to open a directory */
9787 if (mch_isdir(fname))
9788 {
9789 EMSG2(_(e_isadir2), fname);
9790 return NULL;
9791 }
9792#endif
9793 if (!forceit && *mode != 'a' && vim_fexists(fname))
9794 {
9795 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9796 return NULL;
9797 }
9798
9799 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9800 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9801
9802 return fd;
9803}
9804
9805/*
9806 * ":mark" and ":k".
9807 */
9808 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009809ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810{
9811 pos_T pos;
9812
9813 if (*eap->arg == NUL) /* No argument? */
9814 EMSG(_(e_argreq));
9815 else if (eap->arg[1] != NUL) /* more than one character? */
9816 EMSG(_(e_trailing));
9817 else
9818 {
9819 pos = curwin->w_cursor; /* save curwin->w_cursor */
9820 curwin->w_cursor.lnum = eap->line2;
9821 beginline(BL_WHITE | BL_FIX);
9822 if (setmark(*eap->arg) == FAIL) /* set mark */
9823 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9824 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9825 }
9826}
9827
9828/*
9829 * Update w_topline, w_leftcol and the cursor position.
9830 */
9831 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009832update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833{
9834 check_cursor(); /* put cursor on valid line */
9835 update_topline();
9836 if (!curwin->w_p_wrap)
9837 validate_cursor();
9838 update_curswant();
9839}
9840
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841/*
9842 * ":normal[!] {commands}": Execute normal mode commands.
9843 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01009844 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009845ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847 int save_msg_scroll = msg_scroll;
9848 int save_restart_edit = restart_edit;
9849 int save_msg_didout = msg_didout;
9850 int save_State = State;
9851 tasave_T tabuf;
9852 int save_insertmode = p_im;
9853 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009854 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855#ifdef FEAT_MBYTE
9856 char_u *arg = NULL;
9857 int l;
9858 char_u *p;
9859#endif
9860
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009861 if (ex_normal_lock > 0)
9862 {
9863 EMSG(_(e_secure));
9864 return;
9865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 if (ex_normal_busy >= p_mmd)
9867 {
9868 EMSG(_("E192: Recursive use of :normal too deep"));
9869 return;
9870 }
9871 ++ex_normal_busy;
9872
9873 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9874 restart_edit = 0; /* don't go to Insert mode */
9875 p_im = FALSE; /* don't use 'insertmode' */
9876
9877#ifdef FEAT_MBYTE
9878 /*
9879 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9880 * this for the K_SPECIAL leading byte, otherwise special keys will not
9881 * work.
9882 */
9883 if (has_mbyte)
9884 {
9885 int len = 0;
9886
9887 /* Count the number of characters to be escaped. */
9888 for (p = eap->arg; *p != NUL; ++p)
9889 {
9890# ifdef FEAT_GUI
9891 if (*p == CSI) /* leadbyte CSI */
9892 len += 2;
9893# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009894 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9896# ifdef FEAT_GUI
9897 || *p == CSI
9898# endif
9899 )
9900 len += 2;
9901 }
9902 if (len > 0)
9903 {
9904 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9905 if (arg != NULL)
9906 {
9907 len = 0;
9908 for (p = eap->arg; *p != NUL; ++p)
9909 {
9910 arg[len++] = *p;
9911# ifdef FEAT_GUI
9912 if (*p == CSI)
9913 {
9914 arg[len++] = KS_EXTRA;
9915 arg[len++] = (int)KE_CSI;
9916 }
9917# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009918 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 {
9920 arg[len++] = *++p;
9921 if (*p == K_SPECIAL)
9922 {
9923 arg[len++] = KS_SPECIAL;
9924 arg[len++] = KE_FILLER;
9925 }
9926# ifdef FEAT_GUI
9927 else if (*p == CSI)
9928 {
9929 arg[len++] = KS_EXTRA;
9930 arg[len++] = (int)KE_CSI;
9931 }
9932# endif
9933 }
9934 arg[len] = NUL;
9935 }
9936 }
9937 }
9938 }
9939#endif
9940
9941 /*
9942 * Save the current typeahead. This is required to allow using ":normal"
9943 * from an event handler and makes sure we don't hang when the argument
9944 * ends with half a command.
9945 */
9946 save_typeahead(&tabuf);
9947 if (tabuf.typebuf_valid)
9948 {
9949 /*
9950 * Repeat the :normal command for each line in the range. When no
9951 * range given, execute it just once, without positioning the cursor
9952 * first.
9953 */
9954 do
9955 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 if (eap->addr_count != 0)
9957 {
9958 curwin->w_cursor.lnum = eap->line1++;
9959 curwin->w_cursor.col = 0;
9960 }
9961
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009962 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963#ifdef FEAT_MBYTE
9964 arg != NULL ? arg :
9965#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009966 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 }
9968 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9969 }
9970
9971 /* Might not return to the main loop when in an event handler. */
9972 update_topline_cursor();
9973
9974 /* Restore the previous typeahead. */
9975 restore_typeahead(&tabuf);
9976
9977 --ex_normal_busy;
9978 msg_scroll = save_msg_scroll;
9979 restart_edit = save_restart_edit;
9980 p_im = save_insertmode;
9981 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009982 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9984
9985 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +01009986 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +01009988#ifdef FEAT_MOUSE
9989 setmouse();
9990#endif
9991#ifdef CURSOR_SHAPE
9992 ui_cursor_shape(); /* may show different cursor shape */
9993#endif
9994
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995#ifdef FEAT_MBYTE
9996 vim_free(arg);
9997#endif
9998}
9999
10000/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010001 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 */
10003 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010004ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010006 if (eap->forceit)
10007 {
10008 coladvance((colnr_T)MAXCOL);
10009 curwin->w_curswant = MAXCOL;
10010 curwin->w_set_curswant = FALSE;
10011 }
10012
Bram Moolenaara40c5002005-01-09 21:16:21 +000010013 /* Ignore the command when already in Insert mode. Inserting an
10014 * expression register that invokes a function can do this. */
10015 if (State & INSERT)
10016 return;
10017
Bram Moolenaar64969662005-12-14 21:59:55 +000010018 if (eap->cmdidx == CMD_startinsert)
10019 restart_edit = 'a';
10020 else if (eap->cmdidx == CMD_startreplace)
10021 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010023 restart_edit = 'V';
10024
10025 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010027 if (eap->cmdidx == CMD_startinsert)
10028 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 curwin->w_curswant = 0; /* avoid MAXCOL */
10030 }
10031}
10032
10033/*
10034 * ":stopinsert"
10035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010037ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038{
10039 restart_edit = 0;
10040 stop_insert_mode = TRUE;
10041}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010043/*
10044 * Execute normal mode command "cmd".
10045 * "remap" can be REMAP_NONE or REMAP_YES.
10046 */
10047 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010048exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010049{
Bram Moolenaar25281632016-01-21 23:32:32 +010010050 /* Stuff the argument into the typeahead buffer. */
10051 ins_typebuf(cmd, remap, 0, TRUE, silent);
10052 exec_normal(FALSE);
10053}
Bram Moolenaar25281632016-01-21 23:32:32 +010010054
Bram Moolenaar25281632016-01-21 23:32:32 +010010055/*
10056 * Execute normal_cmd() until there is no typeahead left.
10057 */
10058 void
10059exec_normal(int was_typed)
10060{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010061 oparg_T oa;
10062
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010063 clear_oparg(&oa);
10064 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010065 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10066 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010067 {
10068 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010069 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010070 }
10071}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010072
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073#ifdef FEAT_FIND_ID
10074 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010075ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076{
10077 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10078 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10079 (linenr_T)1, (linenr_T)MAXLNUM);
10080}
10081
10082#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
10083/*
10084 * ":psearch"
10085 */
10086 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010087ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088{
10089 g_do_tagpreview = p_pvh;
10090 ex_findpat(eap);
10091 g_do_tagpreview = 0;
10092}
10093#endif
10094
10095 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010096ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097{
10098 int whole = TRUE;
10099 long n;
10100 char_u *p;
10101 int action;
10102
10103 switch (cmdnames[eap->cmdidx].cmd_name[2])
10104 {
10105 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10106 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10107 action = ACTION_GOTO;
10108 else
10109 action = ACTION_SHOW;
10110 break;
10111 case 'i': /* ":ilist" and ":dlist" */
10112 action = ACTION_SHOW_ALL;
10113 break;
10114 case 'u': /* ":ijump" and ":djump" */
10115 action = ACTION_GOTO;
10116 break;
10117 default: /* ":isplit" and ":dsplit" */
10118 action = ACTION_SPLIT;
10119 break;
10120 }
10121
10122 n = 1;
10123 if (vim_isdigit(*eap->arg)) /* get count */
10124 {
10125 n = getdigits(&eap->arg);
10126 eap->arg = skipwhite(eap->arg);
10127 }
10128 if (*eap->arg == '/') /* Match regexp, not just whole words */
10129 {
10130 whole = FALSE;
10131 ++eap->arg;
10132 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10133 if (*p)
10134 {
10135 *p++ = NUL;
10136 p = skipwhite(p);
10137
10138 /* Check for trailing illegal characters */
10139 if (!ends_excmd(*p))
10140 eap->errmsg = e_trailing;
10141 else
10142 eap->nextcmd = check_nextcmd(p);
10143 }
10144 }
10145 if (!eap->skip)
10146 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10147 whole, !eap->forceit,
10148 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10149 n, action, eap->line1, eap->line2);
10150}
10151#endif
10152
10153#ifdef FEAT_WINDOWS
10154
10155# ifdef FEAT_QUICKFIX
10156/*
10157 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10158 */
10159 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010160ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010162 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10164}
10165
10166/*
10167 * ":pedit"
10168 */
10169 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010170ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171{
10172 win_T *curwin_save = curwin;
10173
10174 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010175 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176 keep_help_flag = curwin_save->w_buffer->b_help;
10177 do_exedit(eap, NULL);
10178 keep_help_flag = FALSE;
10179 if (curwin != curwin_save && win_valid(curwin_save))
10180 {
10181 /* Return cursor to where we were */
10182 validate_cursor();
10183 redraw_later(VALID);
10184 win_enter(curwin_save, TRUE);
10185 }
10186 g_do_tagpreview = 0;
10187}
10188# endif
10189
10190/*
10191 * ":stag", ":stselect" and ":stjump".
10192 */
10193 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010194ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195{
10196 postponed_split = -1;
10197 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010198 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10200 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010201 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202}
10203#endif
10204
10205/*
10206 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10207 */
10208 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010209ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210{
10211 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10212}
10213
10214 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010215ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216{
10217 int cmd;
10218
10219 switch (name[1])
10220 {
10221 case 'j': cmd = DT_JUMP; /* ":tjump" */
10222 break;
10223 case 's': cmd = DT_SELECT; /* ":tselect" */
10224 break;
10225 case 'p': cmd = DT_PREV; /* ":tprevious" */
10226 break;
10227 case 'N': cmd = DT_PREV; /* ":tNext" */
10228 break;
10229 case 'n': cmd = DT_NEXT; /* ":tnext" */
10230 break;
10231 case 'o': cmd = DT_POP; /* ":pop" */
10232 break;
10233 case 'f': /* ":tfirst" */
10234 case 'r': cmd = DT_FIRST; /* ":trewind" */
10235 break;
10236 case 'l': cmd = DT_LAST; /* ":tlast" */
10237 break;
10238 default: /* ":tag" */
10239#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010240 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 {
10242 do_cstag(eap);
10243 return;
10244 }
10245#endif
10246 cmd = DT_TAG;
10247 break;
10248 }
10249
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010250 if (name[0] == 'l')
10251 {
10252#ifndef FEAT_QUICKFIX
10253 ex_ni(eap);
10254 return;
10255#else
10256 cmd = DT_LTAG;
10257#endif
10258 }
10259
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10261 eap->forceit, TRUE);
10262}
10263
10264/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010265 * Check "str" for starting with a special cmdline variable.
10266 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10267 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10268 */
10269 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010270find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010271{
10272 int len;
10273 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010274 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010275 "%",
10276#define SPEC_PERC 0
10277 "#",
10278#define SPEC_HASH 1
10279 "<cword>", /* cursor word */
10280#define SPEC_CWORD 2
10281 "<cWORD>", /* cursor WORD */
10282#define SPEC_CCWORD 3
10283 "<cfile>", /* cursor path name */
10284#define SPEC_CFILE 4
10285 "<sfile>", /* ":so" file name */
10286#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010287 "<slnum>", /* ":so" file line number */
10288#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010289#ifdef FEAT_AUTOCMD
10290 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010291# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010292 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010293# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010294 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010295# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010296#endif
10297#ifdef FEAT_CLIENTSERVER
10298 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010299# ifdef FEAT_AUTOCMD
10300# define SPEC_CLIENT 10
10301# else
10302# define SPEC_CLIENT 7
10303# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010304#endif
10305 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010306
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010307 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010308 {
10309 len = (int)STRLEN(spec_str[i]);
10310 if (STRNCMP(src, spec_str[i], len) == 0)
10311 {
10312 *usedlen = len;
10313 return i;
10314 }
10315 }
10316 return -1;
10317}
10318
10319/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 * Evaluate cmdline variables.
10321 *
10322 * change '%' to curbuf->b_ffname
10323 * '#' to curwin->w_altfile
10324 * '<cword>' to word under the cursor
10325 * '<cWORD>' to WORD under the cursor
10326 * '<cfile>' to path name under the cursor
10327 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010328 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329 * '<afile>' to file name for autocommand
10330 * '<abuf>' to buffer number for autocommand
10331 * '<amatch>' to matching name for autocommand
10332 *
10333 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10334 * "" for error without a message) and NULL is returned.
10335 * Returns an allocated string if a valid match was found.
10336 * Returns NULL if no match was found. "usedlen" then still contains the
10337 * number of characters to skip.
10338 */
10339 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010340eval_vars(
10341 char_u *src, /* pointer into commandline */
10342 char_u *srcstart, /* beginning of valid memory for src */
10343 int *usedlen, /* characters after src that are used */
10344 linenr_T *lnump, /* line number for :e command, or NULL */
10345 char_u **errormsg, /* pointer to error message */
10346 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010347 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348{
10349 int i;
10350 char_u *s;
10351 char_u *result;
10352 char_u *resultbuf = NULL;
10353 int resultlen;
10354 buf_T *buf;
10355 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10356 int spec_idx;
10357#ifdef FEAT_MODIFY_FNAME
10358 int skip_mod = FALSE;
10359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361
10362 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010363 if (escaped != NULL)
10364 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365
10366 /*
10367 * Check if there is something to do.
10368 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010369 spec_idx = find_cmdline_var(src, usedlen);
10370 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371 {
10372 *usedlen = 1;
10373 return NULL;
10374 }
10375
10376 /*
10377 * Skip when preceded with a backslash "\%" and "\#".
10378 * Note: In "\\%" the % is also not recognized!
10379 */
10380 if (src > srcstart && src[-1] == '\\')
10381 {
10382 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010383 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 return NULL;
10385 }
10386
10387 /*
10388 * word or WORD under cursor
10389 */
10390 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10391 {
10392 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10393 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10394 if (resultlen == 0)
10395 {
10396 *errormsg = (char_u *)"";
10397 return NULL;
10398 }
10399 }
10400
10401 /*
10402 * '#': Alternate file name
10403 * '%': Current file name
10404 * File name under the cursor
10405 * File name for autocommand
10406 * and following modifiers
10407 */
10408 else
10409 {
10410 switch (spec_idx)
10411 {
10412 case SPEC_PERC: /* '%': current file */
10413 if (curbuf->b_fname == NULL)
10414 {
10415 result = (char_u *)"";
10416 valid = 0; /* Must have ":p:h" to be valid */
10417 }
10418 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420 break;
10421
10422 case SPEC_HASH: /* '#' or "#99": alternate file */
10423 if (src[1] == '#') /* "##": the argument list */
10424 {
10425 result = arg_all();
10426 resultbuf = result;
10427 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010428 if (escaped != NULL)
10429 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430#ifdef FEAT_MODIFY_FNAME
10431 skip_mod = TRUE;
10432#endif
10433 break;
10434 }
10435 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010436 if (*s == '<') /* "#<99" uses v:oldfiles */
10437 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438 i = (int)getdigits(&s);
10439 *usedlen = (int)(s - src); /* length of what we expand */
10440
Bram Moolenaard812df62008-11-09 12:46:09 +000010441 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010443 if (*usedlen < 2)
10444 {
10445 /* Should we give an error message for #<text? */
10446 *usedlen = 1;
10447 return NULL;
10448 }
10449#ifdef FEAT_EVAL
10450 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10451 (long)i);
10452 if (result == NULL)
10453 {
10454 *errormsg = (char_u *)"";
10455 return NULL;
10456 }
10457#else
10458 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 }
10462 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010463 {
10464 buf = buflist_findnr(i);
10465 if (buf == NULL)
10466 {
10467 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10468 return NULL;
10469 }
10470 if (lnump != NULL)
10471 *lnump = ECMD_LAST;
10472 if (buf->b_fname == NULL)
10473 {
10474 result = (char_u *)"";
10475 valid = 0; /* Must have ":p:h" to be valid */
10476 }
10477 else
10478 result = buf->b_fname;
10479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480 break;
10481
10482#ifdef FEAT_SEARCHPATH
10483 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010484 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485 if (result == NULL)
10486 {
10487 *errormsg = (char_u *)"";
10488 return NULL;
10489 }
10490 resultbuf = result; /* remember allocated string */
10491 break;
10492#endif
10493
10494#ifdef FEAT_AUTOCMD
10495 case SPEC_AFILE: /* file name for autocommand */
10496 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010497 if (result != NULL && !autocmd_fname_full)
10498 {
10499 /* Still need to turn the fname into a full path. It is
10500 * postponed to avoid a delay when <afile> is not used. */
10501 autocmd_fname_full = TRUE;
10502 result = FullName_save(autocmd_fname, FALSE);
10503 vim_free(autocmd_fname);
10504 autocmd_fname = result;
10505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506 if (result == NULL)
10507 {
10508 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10509 return NULL;
10510 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010511 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512 break;
10513
10514 case SPEC_ABUF: /* buffer number for autocommand */
10515 if (autocmd_bufnr <= 0)
10516 {
10517 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10518 return NULL;
10519 }
10520 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10521 result = strbuf;
10522 break;
10523
10524 case SPEC_AMATCH: /* match name for autocommand */
10525 result = autocmd_match;
10526 if (result == NULL)
10527 {
10528 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10529 return NULL;
10530 }
10531 break;
10532
10533#endif
10534 case SPEC_SFILE: /* file name for ":so" command */
10535 result = sourcing_name;
10536 if (result == NULL)
10537 {
10538 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10539 return NULL;
10540 }
10541 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010542 case SPEC_SLNUM: /* line in file for ":so" command */
10543 if (sourcing_name == NULL || sourcing_lnum == 0)
10544 {
10545 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10546 return NULL;
10547 }
10548 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10549 result = strbuf;
10550 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551#if defined(FEAT_CLIENTSERVER)
10552 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010553 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10554 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555 result = strbuf;
10556 break;
10557#endif
10558 }
10559
10560 resultlen = (int)STRLEN(result); /* length of new string */
10561 if (src[*usedlen] == '<') /* remove the file name extension */
10562 {
10563 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565 resultlen = (int)(s - result);
10566 }
10567#ifdef FEAT_MODIFY_FNAME
10568 else if (!skip_mod)
10569 {
10570 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10571 &resultlen);
10572 if (result == NULL)
10573 {
10574 *errormsg = (char_u *)"";
10575 return NULL;
10576 }
10577 }
10578#endif
10579 }
10580
10581 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10582 {
10583 if (valid != VALID_HEAD + VALID_PATH)
10584 /* xgettext:no-c-format */
10585 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10586 else
10587 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10588 result = NULL;
10589 }
10590 else
10591 result = vim_strnsave(result, resultlen);
10592 vim_free(resultbuf);
10593 return result;
10594}
10595
10596/*
10597 * Concatenate all files in the argument list, separated by spaces, and return
10598 * it in one allocated string.
10599 * Spaces and backslashes in the file names are escaped with a backslash.
10600 * Returns NULL when out of memory.
10601 */
10602 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010603arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604{
10605 int len;
10606 int idx;
10607 char_u *retval = NULL;
10608 char_u *p;
10609
10610 /*
10611 * Do this loop two times:
10612 * first time: compute the total length
10613 * second time: concatenate the names
10614 */
10615 for (;;)
10616 {
10617 len = 0;
10618 for (idx = 0; idx < ARGCOUNT; ++idx)
10619 {
10620 p = alist_name(&ARGLIST[idx]);
10621 if (p != NULL)
10622 {
10623 if (len > 0)
10624 {
10625 /* insert a space in between names */
10626 if (retval != NULL)
10627 retval[len] = ' ';
10628 ++len;
10629 }
10630 for ( ; *p != NUL; ++p)
10631 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010632 if (*p == ' '
10633#ifndef BACKSLASH_IN_FILENAME
10634 || *p == '\\'
10635#endif
10636 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637 {
10638 /* insert a backslash */
10639 if (retval != NULL)
10640 retval[len] = '\\';
10641 ++len;
10642 }
10643 if (retval != NULL)
10644 retval[len] = *p;
10645 ++len;
10646 }
10647 }
10648 }
10649
10650 /* second time: break here */
10651 if (retval != NULL)
10652 {
10653 retval[len] = NUL;
10654 break;
10655 }
10656
10657 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010658 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659 if (retval == NULL)
10660 break;
10661 }
10662
10663 return retval;
10664}
10665
10666#if defined(FEAT_AUTOCMD) || defined(PROTO)
10667/*
10668 * Expand the <sfile> string in "arg".
10669 *
10670 * Returns an allocated string, or NULL for any error.
10671 */
10672 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010673expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674{
10675 char_u *errormsg;
10676 int len;
10677 char_u *result;
10678 char_u *newres;
10679 char_u *repl;
10680 int srclen;
10681 char_u *p;
10682
10683 result = vim_strsave(arg);
10684 if (result == NULL)
10685 return NULL;
10686
10687 for (p = result; *p; )
10688 {
10689 if (STRNCMP(p, "<sfile>", 7) != 0)
10690 ++p;
10691 else
10692 {
10693 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010694 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695 if (errormsg != NULL)
10696 {
10697 if (*errormsg)
10698 emsg(errormsg);
10699 vim_free(result);
10700 return NULL;
10701 }
10702 if (repl == NULL) /* no match (cannot happen) */
10703 {
10704 p += srclen;
10705 continue;
10706 }
10707 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10708 newres = alloc(len);
10709 if (newres == NULL)
10710 {
10711 vim_free(repl);
10712 vim_free(result);
10713 return NULL;
10714 }
10715 mch_memmove(newres, result, (size_t)(p - result));
10716 STRCPY(newres + (p - result), repl);
10717 len = (int)STRLEN(newres);
10718 STRCAT(newres, p + srclen);
10719 vim_free(repl);
10720 vim_free(result);
10721 result = newres;
10722 p = newres + len; /* continue after the match */
10723 }
10724 }
10725
10726 return result;
10727}
10728#endif
10729
10730#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010010731static int ses_winsizes(FILE *fd, int restore_size,
10732 win_T *tab_firstwin);
10733static int ses_win_rec(FILE *fd, frame_T *fr);
10734static frame_T *ses_skipframe(frame_T *fr);
10735static int ses_do_frame(frame_T *fr);
10736static int ses_do_win(win_T *wp);
10737static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
10738static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
10739static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740
10741/*
10742 * Write openfile commands for the current buffers to an .exrc file.
10743 * Return FAIL on error, OK otherwise.
10744 */
10745 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010746makeopens(
10747 FILE *fd,
10748 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749{
10750 buf_T *buf;
10751 int only_save_windows = TRUE;
10752 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 int restore_size = TRUE;
10754 win_T *wp;
10755 char_u *sname;
10756 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010757 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010758 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010759 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010760 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010761 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010762 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763
10764 if (ssop_flags & SSOP_BUFFERS)
10765 only_save_windows = FALSE; /* Save ALL buffers */
10766
10767 /*
10768 * Begin by setting the this_session variable, and then other
10769 * sessionable variables.
10770 */
10771#ifdef FEAT_EVAL
10772 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10773 return FAIL;
10774 if (ssop_flags & SSOP_GLOBALS)
10775 if (store_session_globals(fd) == FAIL)
10776 return FAIL;
10777#endif
10778
10779 /*
10780 * Close all windows but one.
10781 */
10782 if (put_line(fd, "silent only") == FAIL)
10783 return FAIL;
10784
10785 /*
10786 * Now a :cd command to the session directory or the current directory
10787 */
10788 if (ssop_flags & SSOP_SESDIR)
10789 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010790 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10791 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792 return FAIL;
10793 }
10794 else if (ssop_flags & SSOP_CURDIR)
10795 {
10796 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10797 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010798 || fputs("cd ", fd) < 0
10799 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10800 || put_eol(fd) == FAIL)
10801 {
10802 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805 vim_free(sname);
10806 }
10807
10808 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010809 * If there is an empty, unnamed buffer we will wipe it out later.
10810 * Remember the buffer number.
10811 */
10812 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10813 return FAIL;
10814 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10815 return FAIL;
10816 if (put_line(fd, "endif") == FAIL)
10817 return FAIL;
10818
10819 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 * Now save the current files, current buffer first.
10821 */
10822 if (put_line(fd, "set shortmess=aoO") == FAIL)
10823 return FAIL;
10824
10825 /* Now put the other buffers into the buffer list */
10826 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10827 {
10828 if (!(only_save_windows && buf->b_nwindows == 0)
10829 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10830 && buf->b_fname != NULL
10831 && buf->b_p_bl)
10832 {
10833 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10834 : buf->b_wininfo->wi_fpos.lnum) < 0
10835 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10836 return FAIL;
10837 }
10838 }
10839
10840 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010841 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10843 return FAIL;
10844
10845 if (ssop_flags & SSOP_RESIZE)
10846 {
10847 /* Note: after the restore we still check it worked!*/
10848 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10849 || put_eol(fd) == FAIL)
10850 return FAIL;
10851 }
10852
10853#ifdef FEAT_GUI
10854 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10855 {
10856 int x, y;
10857
10858 if (gui_mch_get_winpos(&x, &y) == OK)
10859 {
10860 /* Note: after the restore we still check it worked!*/
10861 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10862 return FAIL;
10863 }
10864 }
10865#endif
10866
10867 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010868 * When there are two or more tabpages and 'showtabline' is 1 the tabline
10869 * will be displayed when creating the next tab. That resizes the windows
10870 * in the first tab, which may cause problems. Set 'showtabline' to 2
10871 * temporarily to avoid that.
10872 */
10873 if (p_stal == 1 && first_tabpage->tp_next != NULL)
10874 {
10875 if (put_line(fd, "set stal=2") == FAIL)
10876 return FAIL;
10877 restore_stal = TRUE;
10878 }
10879
10880 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010881 * May repeat putting Windows for each tab, when "tabpages" is in
10882 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010883 * Don't use goto_tabpage(), it may change directory and trigger
10884 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010886 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010887 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010888 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020010890 int need_tabnew = FALSE;
10891 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010892
Bram Moolenaar18144c82006-04-12 21:52:12 +000010893 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010895 tabpage_T *tp = find_tabpage(tabnr);
10896
10897 if (tp == NULL)
10898 break; /* done all tab pages */
10899 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010900 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010901 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010902 tab_topframe = topframe;
10903 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010904 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010905 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010906 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010907 tab_topframe = tp->tp_topframe;
10908 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010909 if (tabnr > 1)
10910 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912
Bram Moolenaar18144c82006-04-12 21:52:12 +000010913 /*
10914 * Before creating the window layout, try loading one file. If this
10915 * is aborted we don't end up with a number of useless windows.
10916 * This may have side effects! (e.g., compressed or network file).
10917 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010918 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010919 {
10920 if (ses_do_win(wp)
10921 && wp->w_buffer->b_ffname != NULL
10922 && !wp->w_buffer->b_help
10923#ifdef FEAT_QUICKFIX
10924 && !bt_nofile(wp->w_buffer)
10925#endif
10926 )
10927 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010928 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010929 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10930 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010931 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010932 if (!wp->w_arg_idx_invalid)
10933 edited_win = wp;
10934 break;
10935 }
10936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010938 /* If no file got edited create an empty tab page. */
10939 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10940 return FAIL;
10941
Bram Moolenaar18144c82006-04-12 21:52:12 +000010942 /*
10943 * Save current window layout.
10944 */
10945 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010947 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010949 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10950 return FAIL;
10951 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10952 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953
Bram Moolenaar18144c82006-04-12 21:52:12 +000010954 /*
10955 * Check if window sizes can be restored (no windows omitted).
10956 * Remember the window number of the current window after restoring.
10957 */
10958 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010959 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010960 {
10961 if (ses_do_win(wp))
10962 ++nr;
10963 else
10964 restore_size = FALSE;
10965 if (curwin == wp)
10966 cnr = nr;
10967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968
Bram Moolenaar18144c82006-04-12 21:52:12 +000010969 /* Go to the first window. */
10970 if (put_line(fd, "wincmd t") == FAIL)
10971 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010972
Bram Moolenaar18144c82006-04-12 21:52:12 +000010973 /*
10974 * If more than one window, see if sizes can be restored.
10975 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10976 * resized when moving between windows.
10977 * Do this before restoring the view, so that the topline and the
10978 * cursor can be set. This is done again below.
10979 */
10980 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10981 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010982 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010983 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984
Bram Moolenaar18144c82006-04-12 21:52:12 +000010985 /*
10986 * Restore the view of the window (options, file, cursor, etc.).
10987 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010988 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010989 {
10990 if (!ses_do_win(wp))
10991 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010992 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10993 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010994 return FAIL;
10995 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10996 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010997 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010998 }
10999
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011000 /* The argument index in the first tab page is zero, need to set it in
11001 * each window. For further tab pages it's the window where we do
11002 * "tabedit". */
11003 cur_arg_idx = next_arg_idx;
11004
Bram Moolenaar18144c82006-04-12 21:52:12 +000011005 /*
11006 * Restore cursor to the current window if it's not the first one.
11007 */
11008 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11009 || put_eol(fd) == FAIL))
11010 return FAIL;
11011
11012 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011013 * Restore window sizes again after jumping around in windows, because
11014 * the current window has a minimum size while others may not.
11015 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011016 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011017 return FAIL;
11018
Bram Moolenaar18144c82006-04-12 21:52:12 +000011019 /* Don't continue in another tab page when doing only the current one
11020 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011021 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011022 break;
11023 }
11024
11025 if (ssop_flags & SSOP_TABPAGES)
11026 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011027 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11028 || put_eol(fd) == FAIL)
11029 return FAIL;
11030 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011031 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11032 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011033
Bram Moolenaar9c102382006-05-03 21:26:49 +000011034 /*
11035 * Wipe out an empty unnamed buffer we started in.
11036 */
11037 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11038 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011039 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011040 return FAIL;
11041 if (put_line(fd, "endif") == FAIL)
11042 return FAIL;
11043 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11044 return FAIL;
11045
11046 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11047 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11048 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11049 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011050
11051 /*
11052 * Lastly, execute the x.vim file if it exists.
11053 */
11054 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11055 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011056 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057 || put_line(fd, "endif") == FAIL)
11058 return FAIL;
11059
11060 return OK;
11061}
11062
11063 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011064ses_winsizes(
11065 FILE *fd,
11066 int restore_size,
11067 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068{
11069 int n = 0;
11070 win_T *wp;
11071
11072 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11073 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011074 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075 {
11076 if (!ses_do_win(wp))
11077 continue;
11078 ++n;
11079
11080 /* restore height when not full height */
11081 if (wp->w_height + wp->w_status_height < topframe->fr_height
11082 && (fprintf(fd,
11083 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11084 n, (long)wp->w_height, Rows / 2, Rows) < 0
11085 || put_eol(fd) == FAIL))
11086 return FAIL;
11087
11088 /* restore width when not full width */
11089 if (wp->w_width < Columns && (fprintf(fd,
11090 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11091 n, (long)wp->w_width, Columns / 2, Columns) < 0
11092 || put_eol(fd) == FAIL))
11093 return FAIL;
11094 }
11095 }
11096 else
11097 {
11098 /* Just equalise window sizes */
11099 if (put_line(fd, "wincmd =") == FAIL)
11100 return FAIL;
11101 }
11102 return OK;
11103}
11104
11105/*
11106 * Write commands to "fd" to recursively create windows for frame "fr",
11107 * horizontally and vertically split.
11108 * After the commands the last window in the frame is the current window.
11109 * Returns FAIL when writing the commands to "fd" fails.
11110 */
11111 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011112ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113{
11114 frame_T *frc;
11115 int count = 0;
11116
11117 if (fr->fr_layout != FR_LEAF)
11118 {
11119 /* Find first frame that's not skipped and then create a window for
11120 * each following one (first frame is already there). */
11121 frc = ses_skipframe(fr->fr_child);
11122 if (frc != NULL)
11123 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11124 {
11125 /* Make window as big as possible so that we have lots of room
11126 * to split. */
11127 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11128 || put_line(fd, fr->fr_layout == FR_COL
11129 ? "split" : "vsplit") == FAIL)
11130 return FAIL;
11131 ++count;
11132 }
11133
11134 /* Go back to the first window. */
11135 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11136 ? "%dwincmd k" : "%dwincmd h", count) < 0
11137 || put_eol(fd) == FAIL))
11138 return FAIL;
11139
11140 /* Recursively create frames/windows in each window of this column or
11141 * row. */
11142 frc = ses_skipframe(fr->fr_child);
11143 while (frc != NULL)
11144 {
11145 ses_win_rec(fd, frc);
11146 frc = ses_skipframe(frc->fr_next);
11147 /* Go to next window. */
11148 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11149 return FAIL;
11150 }
11151 }
11152 return OK;
11153}
11154
11155/*
11156 * Skip frames that don't contain windows we want to save in the Session.
11157 * Returns NULL when there none.
11158 */
11159 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011160ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161{
11162 frame_T *frc;
11163
11164 for (frc = fr; frc != NULL; frc = frc->fr_next)
11165 if (ses_do_frame(frc))
11166 break;
11167 return frc;
11168}
11169
11170/*
11171 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11172 * the Session.
11173 */
11174 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011175ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176{
11177 frame_T *frc;
11178
11179 if (fr->fr_layout == FR_LEAF)
11180 return ses_do_win(fr->fr_win);
11181 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11182 if (ses_do_frame(frc))
11183 return TRUE;
11184 return FALSE;
11185}
11186
11187/*
11188 * Return non-zero if window "wp" is to be stored in the Session.
11189 */
11190 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011191ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192{
11193 if (wp->w_buffer->b_fname == NULL
11194#ifdef FEAT_QUICKFIX
11195 /* When 'buftype' is "nofile" can't restore the window contents. */
11196 || bt_nofile(wp->w_buffer)
11197#endif
11198 )
11199 return (ssop_flags & SSOP_BLANK);
11200 if (wp->w_buffer->b_help)
11201 return (ssop_flags & SSOP_HELP);
11202 return TRUE;
11203}
11204
11205/*
11206 * Write commands to "fd" to restore the view of a window.
11207 * Caller must make sure 'scrolloff' is zero.
11208 */
11209 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011210put_view(
11211 FILE *fd,
11212 win_T *wp,
11213 int add_edit, /* add ":edit" command to view */
11214 unsigned *flagp, /* vop_flags or ssop_flags */
11215 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011216 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217{
11218 win_T *save_curwin;
11219 int f;
11220 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011221 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222
11223 /* Always restore cursor position for ":mksession". For ":mkview" only
11224 * when 'viewoptions' contains "cursor". */
11225 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11226
11227 /*
11228 * Local argument list.
11229 */
11230 if (wp->w_alist == &global_alist)
11231 {
11232 if (put_line(fd, "argglobal") == FAIL)
11233 return FAIL;
11234 }
11235 else
11236 {
11237 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11238 flagp == &vop_flags
11239 || !(*flagp & SSOP_CURDIR)
11240 || wp->w_localdir != NULL, flagp) == FAIL)
11241 return FAIL;
11242 }
11243
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011244 /* Only when part of a session: restore the argument index. Some
11245 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011246 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011247 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011249 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250 || put_eol(fd) == FAIL)
11251 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011252 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253 }
11254
11255 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011256 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 {
11258 /*
11259 * Load the file.
11260 */
11261 if (wp->w_buffer->b_ffname != NULL
11262#ifdef FEAT_QUICKFIX
11263 && !bt_nofile(wp->w_buffer)
11264#endif
11265 )
11266 {
11267 /*
11268 * Editing a file in this buffer: use ":edit file".
11269 * This may have side effects! (e.g., compressed or network file).
11270 */
11271 if (fputs("edit ", fd) < 0
11272 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11273 return FAIL;
11274 }
11275 else
11276 {
11277 /* No file in this buffer, just make it empty. */
11278 if (put_line(fd, "enew") == FAIL)
11279 return FAIL;
11280#ifdef FEAT_QUICKFIX
11281 if (wp->w_buffer->b_ffname != NULL)
11282 {
11283 /* The buffer does have a name, but it's not a file name. */
11284 if (fputs("file ", fd) < 0
11285 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11286 return FAIL;
11287 }
11288#endif
11289 do_cursor = FALSE;
11290 }
11291 }
11292
11293 /*
11294 * Local mappings and abbreviations.
11295 */
11296 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11297 && makemap(fd, wp->w_buffer) == FAIL)
11298 return FAIL;
11299
11300 /*
11301 * Local options. Need to go to the window temporarily.
11302 * Store only local values when using ":mkview" and when ":mksession" is
11303 * used and 'sessionoptions' doesn't include "options".
11304 * Some folding options are always stored when "folds" is included,
11305 * otherwise the folds would not be restored correctly.
11306 */
11307 save_curwin = curwin;
11308 curwin = wp;
11309 curbuf = curwin->w_buffer;
11310 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11311 f = makeset(fd, OPT_LOCAL,
11312 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11313#ifdef FEAT_FOLDING
11314 else if (*flagp & SSOP_FOLDS)
11315 f = makefoldset(fd);
11316#endif
11317 else
11318 f = OK;
11319 curwin = save_curwin;
11320 curbuf = curwin->w_buffer;
11321 if (f == FAIL)
11322 return FAIL;
11323
11324#ifdef FEAT_FOLDING
11325 /*
11326 * Save Folds when 'buftype' is empty and for help files.
11327 */
11328 if ((*flagp & SSOP_FOLDS)
11329 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011330# ifdef FEAT_QUICKFIX
11331 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11332# endif
11333 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334 {
11335 if (put_folds(fd, wp) == FAIL)
11336 return FAIL;
11337 }
11338#endif
11339
11340 /*
11341 * Set the cursor after creating folds, since that moves the cursor.
11342 */
11343 if (do_cursor)
11344 {
11345
11346 /* Restore the cursor line in the file and relatively in the
11347 * window. Don't use "G", it changes the jumplist. */
11348 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11349 (long)wp->w_cursor.lnum,
11350 (long)(wp->w_cursor.lnum - wp->w_topline),
11351 (long)wp->w_height / 2, (long)wp->w_height) < 0
11352 || put_eol(fd) == FAIL
11353 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11354 || put_line(fd, "exe s:l") == FAIL
11355 || put_line(fd, "normal! zt") == FAIL
11356 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11357 || put_eol(fd) == FAIL)
11358 return FAIL;
11359 /* Restore the cursor column and left offset when not wrapping. */
11360 if (wp->w_cursor.col == 0)
11361 {
11362 if (put_line(fd, "normal! 0") == FAIL)
11363 return FAIL;
11364 }
11365 else
11366 {
11367 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11368 {
11369 if (fprintf(fd,
11370 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011371 (long)wp->w_virtcol + 1,
11372 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373 (long)wp->w_width / 2, (long)wp->w_width) < 0
11374 || put_eol(fd) == FAIL
11375 || put_line(fd, "if s:c > 0") == FAIL
11376 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011377 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11378 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379 || put_eol(fd) == FAIL
11380 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011381 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382 || put_eol(fd) == FAIL
11383 || put_line(fd, "endif") == FAIL)
11384 return FAIL;
11385 }
11386 else
11387 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011388 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389 || put_eol(fd) == FAIL)
11390 return FAIL;
11391 }
11392 }
11393 }
11394
11395 /*
11396 * Local directory.
11397 */
11398 if (wp->w_localdir != NULL)
11399 {
11400 if (fputs("lcd ", fd) < 0
11401 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11402 || put_eol(fd) == FAIL)
11403 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011404 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011405 }
11406
11407 return OK;
11408}
11409
11410/*
11411 * Write an argument list to the session file.
11412 * Returns FAIL if writing fails.
11413 */
11414 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011415ses_arglist(
11416 FILE *fd,
11417 char *cmd,
11418 garray_T *gap,
11419 int fullname, /* TRUE: use full path name */
11420 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421{
11422 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011423 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011424 char_u *s;
11425
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011426 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11427 return FAIL;
11428 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429 return FAIL;
11430 for (i = 0; i < gap->ga_len; ++i)
11431 {
11432 /* NULL file names are skipped (only happens when out of memory). */
11433 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11434 if (s != NULL)
11435 {
11436 if (fullname)
11437 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011438 buf = alloc(MAXPATHL);
11439 if (buf != NULL)
11440 {
11441 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11442 s = buf;
11443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011445 if (fputs("argadd ", fd) < 0
11446 || ses_put_fname(fd, s, flagp) == FAIL
11447 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011448 {
11449 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011451 }
11452 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011453 }
11454 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011455 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011456}
11457
11458/*
11459 * Write a buffer name to the session file.
11460 * Also ends the line.
11461 * Returns FAIL if writing fails.
11462 */
11463 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011464ses_fname(FILE *fd, buf_T *buf, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465{
11466 char_u *name;
11467
11468 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011469 * the session file will be sourced.
11470 * Don't do this for ":mkview", we don't know the current directory.
11471 * Don't do this after ":lcd", we don't keep track of what the current
11472 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473 if (buf->b_sfname != NULL
11474 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011475 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011476#ifdef FEAT_AUTOCHDIR
11477 && !p_acd
11478#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011479 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480 name = buf->b_sfname;
11481 else
11482 name = buf->b_ffname;
11483 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11484 return FAIL;
11485 return OK;
11486}
11487
11488/*
11489 * Write a file name to the session file.
11490 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11491 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011492 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493 */
11494 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011495ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011496{
11497 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011498 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011499 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011500
11501 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011502 if (sname == NULL)
11503 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011504
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011505 if (*flagp & SSOP_SLASH)
11506 {
11507 /* change all backslashes to forward slashes */
11508 for (p = sname; *p != NUL; mb_ptr_adv(p))
11509 if (*p == '\\')
11510 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011511 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011512
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011513 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011514 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011516 if (p == NULL)
11517 return FAIL;
11518
11519 /* write the result */
11520 if (fputs((char *)p, fd) < 0)
11521 retval = FAIL;
11522
11523 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524 return retval;
11525}
11526
11527/*
11528 * ":loadview [nr]"
11529 */
11530 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011531ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011532{
11533 char_u *fname;
11534
11535 fname = get_view_file(*eap->arg);
11536 if (fname != NULL)
11537 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011538 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011539 vim_free(fname);
11540 }
11541}
11542
11543/*
11544 * Get the name of the view file for the current buffer.
11545 */
11546 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011547get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011548{
11549 int len = 0;
11550 char_u *p, *s;
11551 char_u *retval;
11552 char_u *sname;
11553
11554 if (curbuf->b_ffname == NULL)
11555 {
11556 EMSG(_(e_noname));
11557 return NULL;
11558 }
11559 sname = home_replace_save(NULL, curbuf->b_ffname);
11560 if (sname == NULL)
11561 return NULL;
11562
11563 /*
11564 * We want a file name without separators, because we're not going to make
11565 * a directory.
11566 * "normal" path separator -> "=+"
11567 * "=" -> "=="
11568 * ":" path separator -> "=-"
11569 */
11570 for (p = sname; *p; ++p)
11571 if (*p == '=' || vim_ispathsep(*p))
11572 ++len;
11573 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11574 if (retval != NULL)
11575 {
11576 STRCPY(retval, p_vdir);
11577 add_pathsep(retval);
11578 s = retval + STRLEN(retval);
11579 for (p = sname; *p; ++p)
11580 {
11581 if (*p == '=')
11582 {
11583 *s++ = '=';
11584 *s++ = '=';
11585 }
11586 else if (vim_ispathsep(*p))
11587 {
11588 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011589#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590 if (*p == ':')
11591 *s++ = '-';
11592 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011593#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011594 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011595 }
11596 else
11597 *s++ = *p;
11598 }
11599 *s++ = '=';
11600 *s++ = c;
11601 STRCPY(s, ".vim");
11602 }
11603
11604 vim_free(sname);
11605 return retval;
11606}
11607
11608#endif /* FEAT_SESSION */
11609
11610/*
11611 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11612 * Return FAIL for a write error.
11613 */
11614 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011615put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011616{
11617 if (
11618#ifdef USE_CRNL
11619 (
11620# ifdef MKSESSION_NL
11621 !mksession_nl &&
11622# endif
11623 (putc('\r', fd) < 0)) ||
11624#endif
11625 (putc('\n', fd) < 0))
11626 return FAIL;
11627 return OK;
11628}
11629
11630/*
11631 * Write a line to "fd".
11632 * Return FAIL for a write error.
11633 */
11634 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011635put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011636{
11637 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11638 return FAIL;
11639 return OK;
11640}
11641
11642#ifdef FEAT_VIMINFO
11643/*
11644 * ":rviminfo" and ":wviminfo".
11645 */
11646 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011647ex_viminfo(
11648 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011649{
11650 char_u *save_viminfo;
11651
11652 save_viminfo = p_viminfo;
11653 if (*p_viminfo == NUL)
11654 p_viminfo = (char_u *)"'100";
11655 if (eap->cmdidx == CMD_rviminfo)
11656 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011657 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11658 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011659 EMSG(_("E195: Cannot open viminfo file for reading"));
11660 }
11661 else
11662 write_viminfo(eap->arg, eap->forceit);
11663 p_viminfo = save_viminfo;
11664}
11665#endif
11666
11667#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011668/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011669 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011670 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011671 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011672 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011673dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011675 if (fname == NULL)
11676 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011677 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011678}
11679#endif
11680
11681/*
11682 * ":behave {mswin,xterm}"
11683 */
11684 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011685ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011686{
11687 if (STRCMP(eap->arg, "mswin") == 0)
11688 {
11689 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11690 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11691 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11692 set_option_value((char_u *)"keymodel", 0L,
11693 (char_u *)"startsel,stopsel", 0);
11694 }
11695 else if (STRCMP(eap->arg, "xterm") == 0)
11696 {
11697 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11698 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11699 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11700 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11701 }
11702 else
11703 EMSG2(_(e_invarg2), eap->arg);
11704}
11705
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011706#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11707/*
11708 * Function given to ExpandGeneric() to obtain the possible arguments of the
11709 * ":behave {mswin,xterm}" command.
11710 */
11711 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011712get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011713{
11714 if (idx == 0)
11715 return (char_u *)"mswin";
11716 if (idx == 1)
11717 return (char_u *)"xterm";
11718 return NULL;
11719}
11720#endif
11721
Bram Moolenaar071d4272004-06-13 20:20:40 +000011722#ifdef FEAT_AUTOCMD
11723static int filetype_detect = FALSE;
11724static int filetype_plugin = FALSE;
11725static int filetype_indent = FALSE;
11726
11727/*
11728 * ":filetype [plugin] [indent] {on,off,detect}"
11729 * on: Load the filetype.vim file to install autocommands for file types.
11730 * off: Load the ftoff.vim file to remove all autocommands for file types.
11731 * plugin on: load filetype.vim and ftplugin.vim
11732 * plugin off: load ftplugof.vim
11733 * indent on: load filetype.vim and indent.vim
11734 * indent off: load indoff.vim
11735 */
11736 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011737ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738{
11739 char_u *arg = eap->arg;
11740 int plugin = FALSE;
11741 int indent = FALSE;
11742
11743 if (*eap->arg == NUL)
11744 {
11745 /* Print current status. */
11746 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11747 filetype_detect ? "ON" : "OFF",
11748 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11749 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11750 return;
11751 }
11752
11753 /* Accept "plugin" and "indent" in any order. */
11754 for (;;)
11755 {
11756 if (STRNCMP(arg, "plugin", 6) == 0)
11757 {
11758 plugin = TRUE;
11759 arg = skipwhite(arg + 6);
11760 continue;
11761 }
11762 if (STRNCMP(arg, "indent", 6) == 0)
11763 {
11764 indent = TRUE;
11765 arg = skipwhite(arg + 6);
11766 continue;
11767 }
11768 break;
11769 }
11770 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11771 {
11772 if (*arg == 'o' || !filetype_detect)
11773 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011774 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011775 filetype_detect = TRUE;
11776 if (plugin)
11777 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011778 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011779 filetype_plugin = TRUE;
11780 }
11781 if (indent)
11782 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011783 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011784 filetype_indent = TRUE;
11785 }
11786 }
11787 if (*arg == 'd')
11788 {
11789 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011790 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011791 }
11792 }
11793 else if (STRCMP(arg, "off") == 0)
11794 {
11795 if (plugin || indent)
11796 {
11797 if (plugin)
11798 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011799 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011800 filetype_plugin = FALSE;
11801 }
11802 if (indent)
11803 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011804 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011805 filetype_indent = FALSE;
11806 }
11807 }
11808 else
11809 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011810 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811 filetype_detect = FALSE;
11812 }
11813 }
11814 else
11815 EMSG2(_(e_invarg2), arg);
11816}
11817
11818/*
11819 * ":setfiletype {name}"
11820 */
11821 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011822ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011823{
11824 if (!did_filetype)
11825 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11826}
11827#endif
11828
Bram Moolenaar071d4272004-06-13 20:20:40 +000011829 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011830ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011831{
11832#ifdef FEAT_DIGRAPHS
11833 if (*eap->arg != NUL)
11834 putdigraph(eap->arg);
11835 else
11836 listdigraphs();
11837#else
11838 EMSG(_("E196: No digraphs in this version"));
11839#endif
11840}
11841
11842 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011843ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011844{
11845 int flags = 0;
11846
11847 if (eap->cmdidx == CMD_setlocal)
11848 flags = OPT_LOCAL;
11849 else if (eap->cmdidx == CMD_setglobal)
11850 flags = OPT_GLOBAL;
11851#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11852 if (cmdmod.browse && flags == 0)
11853 ex_options(eap);
11854 else
11855#endif
11856 (void)do_set(eap->arg, flags);
11857}
11858
11859#ifdef FEAT_SEARCH_EXTRA
11860/*
11861 * ":nohlsearch"
11862 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011864ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010011866 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011867 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868}
11869
11870/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011871 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872 * Sets nextcmd to the start of the next command, if any. Also called when
11873 * skipping commands to find the next command.
11874 */
11875 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011876ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877{
11878 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011879 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011880 char_u *end;
11881 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011882 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011883
11884 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011885 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011886 else
11887 {
11888 EMSG(e_invcmd);
11889 return;
11890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011891
11892 /* First clear any old pattern. */
11893 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011894 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011895
11896 if (ends_excmd(*eap->arg))
11897 end = eap->arg;
11898 else if ((STRNICMP(eap->arg, "none", 4) == 0
11899 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11900 end = eap->arg + 4;
11901 else
11902 {
11903 p = skiptowhite(eap->arg);
11904 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011905 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011906 p = skipwhite(p);
11907 if (*p == NUL)
11908 {
11909 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011910 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011911 EMSG2(_(e_invarg2), eap->arg);
11912 return;
11913 }
11914 end = skip_regexp(p + 1, *p, TRUE, NULL);
11915 if (!eap->skip)
11916 {
11917 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11918 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011919 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011920 eap->errmsg = e_trailing;
11921 return;
11922 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011923 if (*end != *p)
11924 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011925 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011926 EMSG2(_(e_invarg2), p);
11927 return;
11928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011929
11930 c = *end;
11931 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020011932 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011933 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011934 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011935 }
11936 }
11937 eap->nextcmd = find_nextcmd(end);
11938}
11939#endif
11940
11941#ifdef FEAT_CRYPT
11942/*
11943 * ":X": Get crypt key
11944 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011945 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011946ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011947{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010011948 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020011949 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950}
11951#endif
11952
11953#ifdef FEAT_FOLDING
11954 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011955ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011956{
11957 if (foldManualAllowed(TRUE))
11958 foldCreate(eap->line1, eap->line2);
11959}
11960
11961 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011962ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963{
11964 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11965 eap->forceit, FALSE);
11966}
11967
11968 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011969ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011970{
11971 linenr_T lnum;
11972
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011973#ifdef FEAT_CLIPBOARD
11974 start_global_changes();
11975#endif
11976
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977 /* First set the marks for all lines closed/open. */
11978 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11979 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11980 ml_setmarked(lnum);
11981
11982 /* Execute the command on the marked lines. */
11983 global_exe(eap->arg);
11984 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011985#ifdef FEAT_CLIPBOARD
11986 end_global_changes();
11987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988}
11989#endif