blob: 4f501aa45448748931a89b0fe6c2a61672189ce2 [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 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002016#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 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 Moolenaarfd89d7e2016-06-04 20:25:05 +02002142 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip,
2143 ea.addr_count == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 if (ea.cmd == NULL) /* error detected */
2145 goto doend;
2146 if (lnum == MAXLNUM)
2147 {
2148 if (*ea.cmd == '%') /* '%' - all lines */
2149 {
2150 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002151 switch (ea.addr_type)
2152 {
2153 case ADDR_LINES:
2154 ea.line1 = 1;
2155 ea.line2 = curbuf->b_ml.ml_line_count;
2156 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002157 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002158 {
2159 buf_T *buf = firstbuf;
2160
2161 while (buf->b_next != NULL
2162 && buf->b_ml.ml_mfp == NULL)
2163 buf = buf->b_next;
2164 ea.line1 = buf->b_fnum;
2165 buf = lastbuf;
2166 while (buf->b_prev != NULL
2167 && buf->b_ml.ml_mfp == NULL)
2168 buf = buf->b_prev;
2169 ea.line2 = buf->b_fnum;
2170 break;
2171 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002172 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002173 ea.line1 = firstbuf->b_fnum;
2174 ea.line2 = lastbuf->b_fnum;
2175 break;
2176 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002177 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002178 if (IS_USER_CMDIDX(ea.cmdidx))
2179 {
2180 ea.line1 = 1;
2181 ea.line2 = ea.addr_type == ADDR_WINDOWS
2182 ? LAST_WIN_NR : LAST_TAB_NR;
2183 }
2184 else
2185 {
2186 /* there is no Vim command which uses '%' and
2187 * ADDR_WINDOWS or ADDR_TABS */
2188 errormsg = (char_u *)_(e_invrange);
2189 goto doend;
2190 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002191 break;
2192 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002193 if (ARGCOUNT == 0)
2194 ea.line1 = ea.line2 = 0;
2195 else
2196 {
2197 ea.line1 = 1;
2198 ea.line2 = ARGCOUNT;
2199 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002200 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002201#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002202 case ADDR_QUICKFIX:
2203 ea.line1 = 1;
2204 ea.line2 = qf_get_size(&ea);
2205 if (ea.line2 == 0)
2206 ea.line2 = 1;
2207 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002208#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 ++ea.addr_count;
2211 }
2212 /* '*' - visual area */
2213 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2214 {
2215 pos_T *fp;
2216
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002217 if (ea.addr_type != ADDR_LINES)
2218 {
2219 errormsg = (char_u *)_(e_invrange);
2220 goto doend;
2221 }
2222
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 ++ea.cmd;
2224 if (!ea.skip)
2225 {
2226 fp = getmark('<', FALSE);
2227 if (check_mark(fp) == FAIL)
2228 goto doend;
2229 ea.line1 = fp->lnum;
2230 fp = getmark('>', FALSE);
2231 if (check_mark(fp) == FAIL)
2232 goto doend;
2233 ea.line2 = fp->lnum;
2234 ++ea.addr_count;
2235 }
2236 }
2237 }
2238 else
2239 ea.line2 = lnum;
2240 ea.addr_count++;
2241
2242 if (*ea.cmd == ';')
2243 {
2244 if (!ea.skip)
2245 curwin->w_cursor.lnum = ea.line2;
2246 }
2247 else if (*ea.cmd != ',')
2248 break;
2249 ++ea.cmd;
2250 }
2251
2252 /* One address given: set start and end lines */
2253 if (ea.addr_count == 1)
2254 {
2255 ea.line1 = ea.line2;
2256 /* ... but only implicit: really no address given */
2257 if (lnum == MAXLNUM)
2258 ea.addr_count = 0;
2259 }
2260
2261 /* Don't leave the cursor on an illegal line (caused by ';') */
2262 check_cursor_lnum();
2263
2264/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002265 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 */
2267
2268 /*
2269 * Skip ':' and any white space
2270 */
2271 ea.cmd = skipwhite(ea.cmd);
2272 while (*ea.cmd == ':')
2273 ea.cmd = skipwhite(ea.cmd + 1);
2274
2275 /*
2276 * If we got a line, but no command, then go to the line.
2277 * If we find a '|' or '\n' we set ea.nextcmd.
2278 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002279 if (*ea.cmd == NUL || *ea.cmd == '"'
2280 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 {
2282 /*
2283 * strange vi behaviour:
2284 * ":3" jumps to line 3
2285 * ":3|..." prints line 3
2286 * ":|" prints current line
2287 */
2288 if (ea.skip) /* skip this if inside :if */
2289 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002290 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 {
2292 ea.cmdidx = CMD_print;
2293 ea.argt = RANGE+COUNT+TRLBAR;
2294 if ((errormsg = invalid_range(&ea)) == NULL)
2295 {
2296 correct_range(&ea);
2297 ex_print(&ea);
2298 }
2299 }
2300 else if (ea.addr_count != 0)
2301 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002302 if (ea.line2 > curbuf->b_ml.ml_line_count)
2303 {
2304 /* With '-' in 'cpoptions' a line number past the file is an
2305 * error, otherwise put it at the end of the file. */
2306 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2307 ea.line2 = -1;
2308 else
2309 ea.line2 = curbuf->b_ml.ml_line_count;
2310 }
2311
2312 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002313 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 else
2315 {
2316 if (ea.line2 == 0)
2317 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 else
2319 curwin->w_cursor.lnum = ea.line2;
2320 beginline(BL_SOL | BL_FIX);
2321 }
2322 }
2323 goto doend;
2324 }
2325
Bram Moolenaard5005162014-08-22 23:05:54 +02002326#ifdef FEAT_AUTOCMD
2327 /* If this looks like an undefined user command and there are CmdUndefined
2328 * autocommands defined, trigger the matching autocommands. */
2329 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2330 && ASCII_ISUPPER(*ea.cmd)
2331 && has_cmdundefined())
2332 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002333 int ret;
2334
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002335 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002336 while (ASCII_ISALNUM(*p))
2337 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002338 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002339 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2340 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002341 /* If the autocommands did something and didn't cause an error, try
2342 * finding the command again. */
2343 p = (ret && !aborting()) ? find_command(&ea, NULL) : NULL;
Bram Moolenaard5005162014-08-22 23:05:54 +02002344 }
2345#endif
2346
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347#ifdef FEAT_USR_CMDS
2348 if (p == NULL)
2349 {
2350 if (!ea.skip)
2351 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2352 goto doend;
2353 }
2354 /* Check for wrong commands. */
2355 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2356 {
2357 errormsg = uc_fun_cmd();
2358 goto doend;
2359 }
2360#endif
2361 if (ea.cmdidx == CMD_SIZE)
2362 {
2363 if (!ea.skip)
2364 {
2365 STRCPY(IObuff, _("E492: Not an editor command"));
2366 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002367 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002369 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 }
2371 goto doend;
2372 }
2373
Bram Moolenaar958636c2014-10-21 20:01:58 +02002374 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2375 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002376#ifdef HAVE_EX_SCRIPT_NI
2377 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2378#endif
2379 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380
2381#ifndef FEAT_EVAL
2382 /*
2383 * When the expression evaluation is disabled, recognize the ":if" and
2384 * ":endif" commands and ignore everything in between it.
2385 */
2386 if (ea.cmdidx == CMD_if)
2387 ++if_level;
2388 if (if_level)
2389 {
2390 if (ea.cmdidx == CMD_endif)
2391 --if_level;
2392 goto doend;
2393 }
2394
2395#endif
2396
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002397 /* forced commands */
2398 if (*p == '!' && ea.cmdidx != CMD_substitute
2399 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 {
2401 ++p;
2402 ea.forceit = TRUE;
2403 }
2404 else
2405 ea.forceit = FALSE;
2406
2407/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002408 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002410 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002411 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412
2413 if (!ea.skip)
2414 {
2415#ifdef HAVE_SANDBOX
2416 if (sandbox != 0 && !(ea.argt & SBOXOK))
2417 {
2418 /* Command not allowed in sandbox. */
2419 errormsg = (char_u *)_(e_sandbox);
2420 goto doend;
2421 }
2422#endif
2423 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2424 {
2425 /* Command not allowed in non-'modifiable' buffer */
2426 errormsg = (char_u *)_(e_modifiable);
2427 goto doend;
2428 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002429
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002430 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002431 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002433 /* Command not allowed when editing the command line. */
2434#ifdef FEAT_CMDWIN
2435 if (cmdwin_type != 0)
2436 errormsg = (char_u *)_(e_cmdwin);
2437 else
2438#endif
2439 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 goto doend;
2441 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002442#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002443 /* Disallow editing another buffer when "curbuf_lock" is set.
2444 * Do allow ":edit" (check for argument later).
2445 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002446 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002447 && ea.cmdidx != CMD_edit
2448 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002449 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002450 && curbuf_locked())
2451 goto doend;
2452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453
2454 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2455 {
2456 /* no range allowed */
2457 errormsg = (char_u *)_(e_norange);
2458 goto doend;
2459 }
2460 }
2461
2462 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2463 {
2464 errormsg = (char_u *)_(e_nobang);
2465 goto doend;
2466 }
2467
2468 /*
2469 * Don't complain about the range if it is not used
2470 * (could happen if line_count is accidentally set to 0).
2471 */
2472 if (!ea.skip && !ni)
2473 {
2474 /*
2475 * If the range is backwards, ask for confirmation and, if given, swap
2476 * ea.line1 & ea.line2 so it's forwards again.
2477 * When global command is busy, don't ask, will fail below.
2478 */
2479 if (!global_busy && ea.line1 > ea.line2)
2480 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002481 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002483 if (sourcing || exmode_active)
2484 {
2485 errormsg = (char_u *)_("E493: Backwards range given");
2486 goto doend;
2487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 if (ask_yesno((char_u *)
2489 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002490 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 }
2492 lnum = ea.line1;
2493 ea.line1 = ea.line2;
2494 ea.line2 = lnum;
2495 }
2496 if ((errormsg = invalid_range(&ea)) != NULL)
2497 goto doend;
2498 }
2499
2500 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2501 ea.line2 = 1;
2502
2503 correct_range(&ea);
2504
2505#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002506 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2507 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 {
2509 /* Put the first line at the start of a closed fold, put the last line
2510 * at the end of a closed fold. */
2511 (void)hasFolding(ea.line1, &ea.line1, NULL);
2512 (void)hasFolding(ea.line2, NULL, &ea.line2);
2513 }
2514#endif
2515
2516#ifdef FEAT_QUICKFIX
2517 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002518 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 * option here, so things like % get expanded.
2520 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002521 p = replace_makeprg(&ea, p, cmdlinep);
2522 if (p == NULL)
2523 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524#endif
2525
2526 /*
2527 * Skip to start of argument.
2528 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2529 */
2530 if (ea.cmdidx == CMD_bang)
2531 ea.arg = p;
2532 else
2533 ea.arg = skipwhite(p);
2534
2535 /*
2536 * Check for "++opt=val" argument.
2537 * Must be first, allow ":w ++enc=utf8 !cmd"
2538 */
2539 if (ea.argt & ARGOPT)
2540 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2541 if (getargopt(&ea) == FAIL && !ni)
2542 {
2543 errormsg = (char_u *)_(e_invarg);
2544 goto doend;
2545 }
2546
2547 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2548 {
2549 if (*ea.arg == '>') /* append */
2550 {
2551 if (*++ea.arg != '>') /* typed wrong */
2552 {
2553 errormsg = (char_u *)_("E494: Use w or w>>");
2554 goto doend;
2555 }
2556 ea.arg = skipwhite(ea.arg + 1);
2557 ea.append = TRUE;
2558 }
2559 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2560 {
2561 ++ea.arg;
2562 ea.usefilter = TRUE;
2563 }
2564 }
2565
2566 if (ea.cmdidx == CMD_read)
2567 {
2568 if (ea.forceit)
2569 {
2570 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2571 ea.forceit = FALSE;
2572 }
2573 else if (*ea.arg == '!') /* :r !filter */
2574 {
2575 ++ea.arg;
2576 ea.usefilter = TRUE;
2577 }
2578 }
2579
2580 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2581 {
2582 ea.amount = 1;
2583 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2584 {
2585 ++ea.arg;
2586 ++ea.amount;
2587 }
2588 ea.arg = skipwhite(ea.arg);
2589 }
2590
2591 /*
2592 * Check for "+command" argument, before checking for next command.
2593 * Don't do this for ":read !cmd" and ":write !cmd".
2594 */
2595 if ((ea.argt & EDITCMD) && !ea.usefilter)
2596 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2597
2598 /*
2599 * Check for '|' to separate commands and '"' to start comments.
2600 * Don't do this for ":read !cmd" and ":write !cmd".
2601 */
2602 if ((ea.argt & TRLBAR) && !ea.usefilter)
2603 separate_nextcmd(&ea);
2604
2605 /*
2606 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002607 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2608 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002610 else if (ea.cmdidx == CMD_bang
2611 || ea.cmdidx == CMD_global
2612 || ea.cmdidx == CMD_vglobal
2613 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 {
2615 for (p = ea.arg; *p; ++p)
2616 {
2617 /* Remove one backslash before a newline, so that it's possible to
2618 * pass a newline to the shell and also a newline that is preceded
2619 * with a backslash. This makes it impossible to end a shell
2620 * command in a backslash, but that doesn't appear useful.
2621 * Halving the number of backslashes is incompatible with previous
2622 * versions. */
2623 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002624 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 else if (*p == '\n')
2626 {
2627 ea.nextcmd = p + 1;
2628 *p = NUL;
2629 break;
2630 }
2631 }
2632 }
2633
2634 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2635 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002636 buf_T *buf;
2637
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002639 switch (ea.addr_type)
2640 {
2641 case ADDR_LINES:
2642 ea.line2 = curbuf->b_ml.ml_line_count;
2643 break;
2644 case ADDR_LOADED_BUFFERS:
2645 buf = firstbuf;
2646 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2647 buf = buf->b_next;
2648 ea.line1 = buf->b_fnum;
2649 buf = lastbuf;
2650 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2651 buf = buf->b_prev;
2652 ea.line2 = buf->b_fnum;
2653 break;
2654 case ADDR_BUFFERS:
2655 ea.line1 = firstbuf->b_fnum;
2656 ea.line2 = lastbuf->b_fnum;
2657 break;
2658 case ADDR_WINDOWS:
2659 ea.line2 = LAST_WIN_NR;
2660 break;
2661 case ADDR_TABS:
2662 ea.line2 = LAST_TAB_NR;
2663 break;
2664 case ADDR_ARGUMENTS:
2665 if (ARGCOUNT == 0)
2666 ea.line1 = ea.line2 = 0;
2667 else
2668 ea.line2 = ARGCOUNT;
2669 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002670#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002671 case ADDR_QUICKFIX:
2672 ea.line2 = qf_get_size(&ea);
2673 if (ea.line2 == 0)
2674 ea.line2 = 1;
2675 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002676#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 }
2679
2680 /* accept numbered register only when no count allowed (:put) */
2681 if ( (ea.argt & REGSTR)
2682 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002683 /* Do not allow register = for user commands */
2684 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2686 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002687#ifndef FEAT_CLIPBOARD
2688 /* check these explicitly for a more specific error message */
2689 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002691 errormsg = (char_u *)_(e_invalidreg);
2692 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 }
2694#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002695 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2696 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002697 {
2698 ea.regname = *ea.arg++;
2699#ifdef FEAT_EVAL
2700 /* for '=' register: accept the rest of the line as an expression */
2701 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2702 {
2703 set_expr_line(vim_strsave(ea.arg));
2704 ea.arg += STRLEN(ea.arg);
2705 }
2706#endif
2707 ea.arg = skipwhite(ea.arg);
2708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 }
2710
2711 /*
2712 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2713 * count, it's a buffer name.
2714 */
2715 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2716 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2717 || vim_iswhite(*p)))
2718 {
2719 n = getdigits(&ea.arg);
2720 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002721 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 {
2723 errormsg = (char_u *)_(e_zerocount);
2724 goto doend;
2725 }
2726 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2727 {
2728 ea.line2 = n;
2729 if (ea.addr_count == 0)
2730 ea.addr_count = 1;
2731 }
2732 else
2733 {
2734 ea.line1 = ea.line2;
2735 ea.line2 += n - 1;
2736 ++ea.addr_count;
2737 /*
2738 * Be vi compatible: no error message for out of range.
2739 */
2740 if (ea.line2 > curbuf->b_ml.ml_line_count)
2741 ea.line2 = curbuf->b_ml.ml_line_count;
2742 }
2743 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002744
2745 /*
2746 * Check for flags: 'l', 'p' and '#'.
2747 */
2748 if (ea.argt & EXFLAGS)
2749 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002751 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002752 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 {
2754 errormsg = (char_u *)_(e_trailing);
2755 goto doend;
2756 }
2757
2758 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2759 {
2760 errormsg = (char_u *)_(e_argreq);
2761 goto doend;
2762 }
2763
2764#ifdef FEAT_EVAL
2765 /*
2766 * Skip the command when it's not going to be executed.
2767 * The commands like :if, :endif, etc. always need to be executed.
2768 * Also make an exception for commands that handle a trailing command
2769 * themselves.
2770 */
2771 if (ea.skip)
2772 {
2773 switch (ea.cmdidx)
2774 {
2775 /* commands that need evaluation */
2776 case CMD_while:
2777 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002778 case CMD_for:
2779 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 case CMD_if:
2781 case CMD_elseif:
2782 case CMD_else:
2783 case CMD_endif:
2784 case CMD_try:
2785 case CMD_catch:
2786 case CMD_finally:
2787 case CMD_endtry:
2788 case CMD_function:
2789 break;
2790
2791 /* Commands that handle '|' themselves. Check: A command should
2792 * either have the TRLBAR flag, appear in this list or appear in
2793 * the list at ":help :bar". */
2794 case CMD_aboveleft:
2795 case CMD_and:
2796 case CMD_belowright:
2797 case CMD_botright:
2798 case CMD_browse:
2799 case CMD_call:
2800 case CMD_confirm:
2801 case CMD_delfunction:
2802 case CMD_djump:
2803 case CMD_dlist:
2804 case CMD_dsearch:
2805 case CMD_dsplit:
2806 case CMD_echo:
2807 case CMD_echoerr:
2808 case CMD_echomsg:
2809 case CMD_echon:
2810 case CMD_execute:
2811 case CMD_help:
2812 case CMD_hide:
2813 case CMD_ijump:
2814 case CMD_ilist:
2815 case CMD_isearch:
2816 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002817 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 case CMD_keepjumps:
2819 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002820 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 case CMD_leftabove:
2822 case CMD_let:
2823 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002824 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002826 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002827 case CMD_noautocmd:
2828 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 case CMD_perl:
2830 case CMD_psearch:
2831 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002832 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002833 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 case CMD_return:
2835 case CMD_rightbelow:
2836 case CMD_ruby:
2837 case CMD_silent:
2838 case CMD_smagic:
2839 case CMD_snomagic:
2840 case CMD_substitute:
2841 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002842 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 case CMD_tcl:
2844 case CMD_throw:
2845 case CMD_tilde:
2846 case CMD_topleft:
2847 case CMD_unlet:
2848 case CMD_verbose:
2849 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002850 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 break;
2852
2853 default: goto doend;
2854 }
2855 }
2856#endif
2857
2858 if (ea.argt & XFILE)
2859 {
2860 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2861 goto doend;
2862 }
2863
2864#ifdef FEAT_LISTCMDS
2865 /*
2866 * Accept buffer name. Cannot be used at the same time with a buffer
2867 * number. Don't do this for a user command.
2868 */
2869 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002870 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 {
2872 /*
2873 * :bdelete, :bwipeout and :bunload take several arguments, separated
2874 * by spaces: find next space (skipping over escaped characters).
2875 * The others take one argument: ignore trailing spaces.
2876 */
2877 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2878 || ea.cmdidx == CMD_bunload)
2879 p = skiptowhite_esc(ea.arg);
2880 else
2881 {
2882 p = ea.arg + STRLEN(ea.arg);
2883 while (p > ea.arg && vim_iswhite(p[-1]))
2884 --p;
2885 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002886 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2887 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 if (ea.line2 < 0) /* failed */
2889 goto doend;
2890 ea.addr_count = 1;
2891 ea.arg = skipwhite(p);
2892 }
2893#endif
2894
2895/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002896 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 *
2898 * The "ea" structure holds the arguments that can be used.
2899 */
2900 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002901 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 ea.cookie = cookie;
2903#ifdef FEAT_EVAL
2904 ea.cstack = cstack;
2905#endif
2906
2907#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002908 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 {
2910 /*
2911 * Execute a user-defined command.
2912 */
2913 do_ucmd(&ea);
2914 }
2915 else
2916#endif
2917 {
2918 /*
2919 * Call the function to execute the command.
2920 */
2921 ea.errmsg = NULL;
2922 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2923 if (ea.errmsg != NULL)
2924 errormsg = (char_u *)_(ea.errmsg);
2925 }
2926
2927#ifdef FEAT_EVAL
2928 /*
2929 * If the command just executed called do_cmdline(), any throw or ":return"
2930 * or ":finish" encountered there must also check the cstack of the still
2931 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2932 * exception, or reanimate a returned function or finished script file and
2933 * return or finish it again.
2934 */
2935 if (need_rethrow)
2936 do_throw(cstack);
2937 else if (check_cstack)
2938 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002939 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002941 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 && current_func_returned())
2943 do_return(&ea, TRUE, FALSE, NULL);
2944 }
2945 need_rethrow = check_cstack = FALSE;
2946#endif
2947
2948doend:
2949 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2950 curwin->w_cursor.lnum = 1;
2951
2952 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2953 {
2954 if (sourcing)
2955 {
2956 if (errormsg != IObuff)
2957 {
2958 STRCPY(IObuff, errormsg);
2959 errormsg = IObuff;
2960 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002961 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 }
2963 emsg(errormsg);
2964 }
2965#ifdef FEAT_EVAL
2966 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002967 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2968 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969#endif
2970
2971 if (verbose_save >= 0)
2972 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002973#ifdef FEAT_AUTOCMD
2974 if (cmdmod.save_ei != NULL)
2975 {
2976 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002977 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2978 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002979 free_string_option(cmdmod.save_ei);
2980 }
2981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982
2983 cmdmod = save_cmdmod;
2984
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002985 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 {
2987 /* messages could be enabled for a serious error, need to check if the
2988 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002989 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002990 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 emsg_silent -= did_esilent;
2992 if (emsg_silent < 0)
2993 emsg_silent = 0;
2994 /* Restore msg_scroll, it's set by file I/O commands, even when no
2995 * message is actually displayed. */
2996 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002997
2998 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2999 * somewhere in the line. Put it back in the first column. */
3000 if (redirecting())
3001 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 }
3003
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003004#ifdef HAVE_SANDBOX
3005 if (did_sandbox)
3006 --sandbox;
3007#endif
3008
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3010 ea.nextcmd = NULL;
3011
3012#ifdef FEAT_EVAL
3013 --ex_nesting_level;
3014#endif
3015
3016 return ea.nextcmd;
3017}
3018#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003019 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020#endif
3021
3022/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003023 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 * If there is a match advance "pp" to the argument and return TRUE.
3025 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003026 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003027checkforcmd(
3028 char_u **pp, /* start of command */
3029 char *cmd, /* name of command */
3030 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031{
3032 int i;
3033
3034 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003035 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 break;
3037 if (i >= len && !isalpha((*pp)[i]))
3038 {
3039 *pp = skipwhite(*pp + i);
3040 return TRUE;
3041 }
3042 return FALSE;
3043}
3044
3045/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003046 * Append "cmd" to the error message in IObuff.
3047 * Takes care of limiting the length and handling 0xa0, which would be
3048 * invisible otherwise.
3049 */
3050 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003051append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003052{
3053 char_u *s = cmd;
3054 char_u *d;
3055
3056 STRCAT(IObuff, ": ");
3057 d = IObuff + STRLEN(IObuff);
3058 while (*s != NUL && d - IObuff < IOSIZE - 7)
3059 {
3060 if (
3061#ifdef FEAT_MBYTE
3062 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3063#endif
3064 *s == 0xa0)
3065 {
3066 s +=
3067#ifdef FEAT_MBYTE
3068 enc_utf8 ? 2 :
3069#endif
3070 1;
3071 STRCPY(d, "<a0>");
3072 d += 4;
3073 }
3074 else
3075 MB_COPY_CHAR(s, d);
3076 }
3077 *d = NUL;
3078}
3079
3080/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003082 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003084 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 * Returns NULL for an ambiguous user command.
3086 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003088find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089{
3090 int len;
3091 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003092 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093
3094 /*
3095 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003096 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 * - the 'k' command can directly be followed by any character.
3098 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003099 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003101 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 */
3103 p = eap->cmd;
3104 if (*p == 'k')
3105 {
3106 eap->cmdidx = CMD_k;
3107 ++p;
3108 }
3109 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003110 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3111 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 || p[1] == 'g'
3113 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3114 || p[1] == 'I'
3115 || (p[1] == 'r' && p[2] != 'e')))
3116 {
3117 eap->cmdidx = CMD_substitute;
3118 ++p;
3119 }
3120 else
3121 {
3122 while (ASCII_ISALPHA(*p))
3123 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003124 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003125 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003126 while (ASCII_ISALNUM(*p))
3127 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003128
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 /* check for non-alpha command */
3130 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3131 ++p;
3132 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003133 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3134 {
3135 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3136 * :delete with the 'l' flag. Same for 'p'. */
3137 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003138 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003139 break;
3140 if (i == len - 1)
3141 {
3142 --len;
3143 if (p[-1] == 'l')
3144 eap->flags |= EXFLAG_LIST;
3145 else
3146 eap->flags |= EXFLAG_PRINT;
3147 }
3148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149
3150 if (ASCII_ISLOWER(*eap->cmd))
3151 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
3152 else
3153 eap->cmdidx = cmdidxs[26];
3154
3155 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3156 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3157 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3158 (size_t)len) == 0)
3159 {
3160#ifdef FEAT_EVAL
3161 if (full != NULL
3162 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3163 *full = TRUE;
3164#endif
3165 break;
3166 }
3167
3168#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003169 /* Look for a user defined command as a last resort. Let ":Print" be
3170 * overruled by a user defined command. */
3171 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3172 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003174 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 while (ASCII_ISALNUM(*p))
3176 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003177 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 }
3179#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003180 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 eap->cmdidx = CMD_SIZE;
3182 }
3183
3184 return p;
3185}
3186
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003187#ifdef FEAT_USR_CMDS
3188/*
3189 * Search for a user command that matches "eap->cmd".
3190 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3191 * Return a pointer to just after the command.
3192 * Return NULL if there is no matching command.
3193 */
3194 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003195find_ucmd(
3196 exarg_T *eap,
3197 char_u *p, /* end of the command (possibly including count) */
3198 int *full, /* set to TRUE for a full match */
3199 expand_T *xp, /* used for completion, NULL otherwise */
3200 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003201{
3202 int len = (int)(p - eap->cmd);
3203 int j, k, matchlen = 0;
3204 ucmd_T *uc;
3205 int found = FALSE;
3206 int possible = FALSE;
3207 char_u *cp, *np; /* Point into typed cmd and test name */
3208 garray_T *gap;
3209 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3210 only full match global is accepted. */
3211
3212 /*
3213 * Look for buffer-local user commands first, then global ones.
3214 */
3215 gap = &curbuf->b_ucmds;
3216 for (;;)
3217 {
3218 for (j = 0; j < gap->ga_len; ++j)
3219 {
3220 uc = USER_CMD_GA(gap, j);
3221 cp = eap->cmd;
3222 np = uc->uc_name;
3223 k = 0;
3224 while (k < len && *np != NUL && *cp++ == *np++)
3225 k++;
3226 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3227 {
3228 /* If finding a second match, the command is ambiguous. But
3229 * not if a buffer-local command wasn't a full match and a
3230 * global command is a full match. */
3231 if (k == len && found && *np != NUL)
3232 {
3233 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003234 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003235 amb_local = TRUE;
3236 }
3237
3238 if (!found || (k == len && *np == NUL))
3239 {
3240 /* If we matched up to a digit, then there could
3241 * be another command including the digit that we
3242 * should use instead.
3243 */
3244 if (k == len)
3245 found = TRUE;
3246 else
3247 possible = TRUE;
3248
3249 if (gap == &ucmds)
3250 eap->cmdidx = CMD_USER;
3251 else
3252 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003253 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003254 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003255 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003256
3257# ifdef FEAT_CMDL_COMPL
3258 if (compl != NULL)
3259 *compl = uc->uc_compl;
3260# ifdef FEAT_EVAL
3261 if (xp != NULL)
3262 {
3263 xp->xp_arg = uc->uc_compl_arg;
3264 xp->xp_scriptID = uc->uc_scriptID;
3265 }
3266# endif
3267# endif
3268 /* Do not search for further abbreviations
3269 * if this is an exact match. */
3270 matchlen = k;
3271 if (k == len && *np == NUL)
3272 {
3273 if (full != NULL)
3274 *full = TRUE;
3275 amb_local = FALSE;
3276 break;
3277 }
3278 }
3279 }
3280 }
3281
3282 /* Stop if we found a full match or searched all. */
3283 if (j < gap->ga_len || gap == &ucmds)
3284 break;
3285 gap = &ucmds;
3286 }
3287
3288 /* Only found ambiguous matches. */
3289 if (amb_local)
3290 {
3291 if (xp != NULL)
3292 xp->xp_context = EXPAND_UNSUCCESSFUL;
3293 return NULL;
3294 }
3295
3296 /* The match we found may be followed immediately by a number. Move "p"
3297 * back to point to it. */
3298 if (found || possible)
3299 return p + (matchlen - len);
3300 return p;
3301}
3302#endif
3303
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003305static struct cmdmod
3306{
3307 char *name;
3308 int minlen;
3309 int has_count; /* :123verbose :3tab */
3310} cmdmods[] = {
3311 {"aboveleft", 3, FALSE},
3312 {"belowright", 3, FALSE},
3313 {"botright", 2, FALSE},
3314 {"browse", 3, FALSE},
3315 {"confirm", 4, FALSE},
3316 {"hide", 3, FALSE},
3317 {"keepalt", 5, FALSE},
3318 {"keepjumps", 5, FALSE},
3319 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003320 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003321 {"leftabove", 5, FALSE},
3322 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003323 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003324 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003325 {"rightbelow", 6, FALSE},
3326 {"sandbox", 3, FALSE},
3327 {"silent", 3, FALSE},
3328 {"tab", 3, TRUE},
3329 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003330 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003331 {"verbose", 4, TRUE},
3332 {"vertical", 4, FALSE},
3333};
3334
3335/*
3336 * Return length of a command modifier (including optional count).
3337 * Return zero when it's not a modifier.
3338 */
3339 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003340modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003341{
3342 int i, j;
3343 char_u *p = cmd;
3344
3345 if (VIM_ISDIGIT(*cmd))
3346 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003347 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003348 {
3349 for (j = 0; p[j] != NUL; ++j)
3350 if (p[j] != cmdmods[i].name[j])
3351 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003352 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003353 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003354 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003355 }
3356 return 0;
3357}
3358
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359/*
3360 * Return > 0 if an Ex command "name" exists.
3361 * Return 2 if there is an exact match.
3362 * Return 3 if there is an ambiguous match.
3363 */
3364 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003365cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366{
3367 exarg_T ea;
3368 int full = FALSE;
3369 int i;
3370 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003371 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372
3373 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003374 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 {
3376 for (j = 0; name[j] != NUL; ++j)
3377 if (name[j] != cmdmods[i].name[j])
3378 break;
3379 if (name[j] == NUL && j >= cmdmods[i].minlen)
3380 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3381 }
3382
Bram Moolenaara9587612006-05-04 21:47:50 +00003383 /* Check built-in commands and user defined commands.
3384 * For ":2match" and ":3match" we need to skip the number. */
3385 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003387 p = find_command(&ea, &full);
3388 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003390 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3391 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003392 if (*skipwhite(p) != NUL)
3393 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3395}
3396#endif
3397
3398/*
3399 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3400 * we don't need/want deleted. Maybe this could be done better if we didn't
3401 * repeat all this stuff. The only problem is that they may not stay
3402 * perfectly compatible with each other, but then the command line syntax
3403 * probably won't change that much -- webb.
3404 */
3405 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003406set_one_cmd_context(
3407 expand_T *xp,
3408 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409{
3410 char_u *p;
3411 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003412 int len = 0;
3413 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3415 int compl = EXPAND_NOTHING;
3416#endif
3417#ifdef FEAT_CMDL_COMPL
3418 int delim;
3419#endif
3420 int forceit = FALSE;
3421 int usefilter = FALSE; /* filter instead of file name */
3422
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003423 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 xp->xp_pattern = buff;
3425 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003426 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427
3428/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003429 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 */
3431 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3432 ;
3433 xp->xp_pattern = cmd;
3434
3435 if (*cmd == NUL)
3436 return NULL;
3437 if (*cmd == '"') /* ignore comment lines */
3438 {
3439 xp->xp_context = EXPAND_NOTHING;
3440 return NULL;
3441 }
3442
3443/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003444 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 */
3446 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 xp->xp_pattern = cmd;
3448 if (*cmd == NUL)
3449 return NULL;
3450 if (*cmd == '"')
3451 {
3452 xp->xp_context = EXPAND_NOTHING;
3453 return NULL;
3454 }
3455
3456 if (*cmd == '|' || *cmd == '\n')
3457 return cmd + 1; /* There's another command */
3458
3459 /*
3460 * Isolate the command and search for it in the command table.
3461 * Exceptions:
3462 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003463 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3465 */
3466 if (*cmd == 'k' && cmd[1] != 'e')
3467 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003468 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 p = cmd + 1;
3470 }
3471 else
3472 {
3473 p = cmd;
3474 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3475 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003476 /* a user command may contain digits */
3477 if (ASCII_ISUPPER(cmd[0]))
3478 while (ASCII_ISALNUM(*p) || *p == '*')
3479 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003480 /* for python 3.x: ":py3*" commands completion */
3481 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003482 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003483 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003484 while (ASCII_ISALPHA(*p) || *p == '*')
3485 ++p;
3486 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003487 /* check for non-alpha command */
3488 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3489 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003490 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003492 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 {
3494 xp->xp_context = EXPAND_UNSUCCESSFUL;
3495 return NULL;
3496 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003497 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003498 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3499 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3500 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 break;
3502
3503#ifdef FEAT_USR_CMDS
3504 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3506 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507#endif
3508 }
3509
3510 /*
3511 * If the cursor is touching the command, and it ends in an alpha-numeric
3512 * character, complete the command name.
3513 */
3514 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3515 return NULL;
3516
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003517 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 {
3519 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3520 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003521 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 p = cmd + 1;
3523 }
3524#ifdef FEAT_USR_CMDS
3525 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3526 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003527 ea.cmd = cmd;
3528 p = find_ucmd(&ea, p, NULL, xp,
3529# if defined(FEAT_CMDL_COMPL)
3530 &compl
3531# else
3532 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003534 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003535 if (p == NULL)
3536 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 }
3538#endif
3539 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003540 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 {
3542 /* Not still touching the command and it was an illegal one */
3543 xp->xp_context = EXPAND_UNSUCCESSFUL;
3544 return NULL;
3545 }
3546
3547 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3548
3549 if (*p == '!') /* forced commands */
3550 {
3551 forceit = TRUE;
3552 ++p;
3553 }
3554
3555/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003556 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003558 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003559 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560
3561 arg = skipwhite(p);
3562
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003563 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 {
3565 if (*arg == '>') /* append */
3566 {
3567 if (*++arg == '>')
3568 ++arg;
3569 arg = skipwhite(arg);
3570 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003571 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 {
3573 ++arg;
3574 usefilter = TRUE;
3575 }
3576 }
3577
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003578 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 {
3580 usefilter = forceit; /* :r! filter if forced */
3581 if (*arg == '!') /* :r !filter */
3582 {
3583 ++arg;
3584 usefilter = TRUE;
3585 }
3586 }
3587
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003588 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 {
3590 while (*arg == *cmd) /* allow any number of '>' or '<' */
3591 ++arg;
3592 arg = skipwhite(arg);
3593 }
3594
3595 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003596 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 {
3598 /* Check if we're in the +command */
3599 p = arg + 1;
3600 arg = skip_cmd_arg(arg, FALSE);
3601
3602 /* Still touching the command after '+'? */
3603 if (*arg == NUL)
3604 return p;
3605
3606 /* Skip space(s) after +command to get to the real argument */
3607 arg = skipwhite(arg);
3608 }
3609
3610 /*
3611 * Check for '|' to separate commands and '"' to start comments.
3612 * Don't do this for ":read !cmd" and ":write !cmd".
3613 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003614 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 {
3616 p = arg;
3617 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003618 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 p += 2;
3620 while (*p)
3621 {
3622 if (*p == Ctrl_V)
3623 {
3624 if (p[1] != NUL)
3625 ++p;
3626 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003627 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 || *p == '|' || *p == '\n')
3629 {
3630 if (*(p - 1) != '\\')
3631 {
3632 if (*p == '|' || *p == '\n')
3633 return p + 1;
3634 return NULL; /* It's a comment */
3635 }
3636 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003637 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 }
3639 }
3640
3641 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003642 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 vim_strchr((char_u *)"|\"", *arg) == NULL)
3644 return NULL;
3645
3646 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003647 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003649 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003650 while (*p && p < buff + len)
3651 {
3652 if (*p == ' ' || *p == TAB)
3653 {
3654 /* argument starts after a space */
3655 xp->xp_pattern = ++p;
3656 }
3657 else
3658 {
3659 if (*p == '\\' && *(p + 1) != NUL)
3660 ++p; /* skip over escaped character */
3661 mb_ptr_adv(p);
3662 }
3663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003665 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003667 int c;
3668 int in_quote = FALSE;
3669 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670
3671 /*
3672 * Allow spaces within back-quotes to count as part of the argument
3673 * being expanded.
3674 */
3675 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003676 p = xp->xp_pattern;
3677 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003679#ifdef FEAT_MBYTE
3680 if (has_mbyte)
3681 c = mb_ptr2char(p);
3682 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003684 c = *p;
3685 if (c == '\\' && p[1] != NUL)
3686 ++p;
3687 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 {
3689 if (!in_quote)
3690 {
3691 xp->xp_pattern = p;
3692 bow = p + 1;
3693 }
3694 in_quote = !in_quote;
3695 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003696 /* An argument can contain just about everything, except
3697 * characters that end the command and white space. */
3698 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003699#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003700 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003701#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003702 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003703 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003704 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003705 while (*p != NUL)
3706 {
3707#ifdef FEAT_MBYTE
3708 if (has_mbyte)
3709 c = mb_ptr2char(p);
3710 else
3711#endif
3712 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003713 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003714 break;
3715#ifdef FEAT_MBYTE
3716 if (has_mbyte)
3717 len = (*mb_ptr2len)(p);
3718 else
3719#endif
3720 len = 1;
3721 mb_ptr_adv(p);
3722 }
3723 if (in_quote)
3724 bow = p;
3725 else
3726 xp->xp_pattern = p;
3727 p -= len;
3728 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003729 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 }
3731
3732 /*
3733 * If we are still inside the quotes, and we passed a space, just
3734 * expand from there.
3735 */
3736 if (bow != NULL && in_quote)
3737 xp->xp_pattern = bow;
3738 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003739
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003740 /* For a shell command more chars need to be escaped. */
3741 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003742 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003743#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003744 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003745#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003746 /* When still after the command name expand executables. */
3747 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003748 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750
3751 /* Check for environment variable */
3752 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003753#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 || *xp->xp_pattern == '%'
3755#endif
3756 )
3757 {
3758 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3759 if (!vim_isIDc(*p))
3760 break;
3761 if (*p == NUL)
3762 {
3763 xp->xp_context = EXPAND_ENV_VARS;
3764 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003765#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3766 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003767 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003768 compl = EXPAND_ENV_VARS;
3769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 }
3771 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003772#if defined(FEAT_CMDL_COMPL)
3773 /* Check for user names */
3774 if (*xp->xp_pattern == '~')
3775 {
3776 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3777 ;
3778 /* Complete ~user only if it partially matches a user name.
3779 * A full match ~user<Tab> will be replaced by user's home
3780 * directory i.e. something like ~user<Tab> -> /home/user/ */
3781 if (*p == NUL && p > xp->xp_pattern + 1
3782 && match_user(xp->xp_pattern + 1) == 1)
3783 {
3784 xp->xp_context = EXPAND_USER;
3785 ++xp->xp_pattern;
3786 }
3787 }
3788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 }
3790
3791/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003792 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003794 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003796 case CMD_find:
3797 case CMD_sfind:
3798 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003799 if (xp->xp_context == EXPAND_FILES)
3800 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003801 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 case CMD_cd:
3803 case CMD_chdir:
3804 case CMD_lcd:
3805 case CMD_lchdir:
3806 if (xp->xp_context == EXPAND_FILES)
3807 xp->xp_context = EXPAND_DIRECTORIES;
3808 break;
3809 case CMD_help:
3810 xp->xp_context = EXPAND_HELP;
3811 xp->xp_pattern = arg;
3812 break;
3813
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003814 /* Command modifiers: return the argument.
3815 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003817 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 case CMD_belowright:
3819 case CMD_botright:
3820 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003821 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003822 case CMD_cdo:
3823 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003825 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 case CMD_folddoclosed:
3827 case CMD_folddoopen:
3828 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003829 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 case CMD_keepjumps:
3831 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003832 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003833 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003835 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003837 case CMD_noautocmd:
3838 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003840 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003842 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003843 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 case CMD_topleft:
3845 case CMD_verbose:
3846 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003847 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 return arg;
3849
Bram Moolenaar4f688582007-07-24 12:34:30 +00003850#ifdef FEAT_CMDL_COMPL
3851# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 case CMD_match:
3853 if (*arg == NUL || !ends_excmd(*arg))
3854 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003855 /* also complete "None" */
3856 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 arg = skipwhite(skiptowhite(arg));
3858 if (*arg != NUL)
3859 {
3860 xp->xp_context = EXPAND_NOTHING;
3861 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3862 }
3863 }
3864 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003865# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867/*
3868 * All completion for the +cmdline_compl feature goes here.
3869 */
3870
3871# ifdef FEAT_USR_CMDS
3872 case CMD_command:
3873 /* Check for attributes */
3874 while (*arg == '-')
3875 {
3876 arg++; /* Skip "-" */
3877 p = skiptowhite(arg);
3878 if (*p == NUL)
3879 {
3880 /* Cursor is still in the attribute */
3881 p = vim_strchr(arg, '=');
3882 if (p == NULL)
3883 {
3884 /* No "=", so complete attribute names */
3885 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3886 xp->xp_pattern = arg;
3887 return NULL;
3888 }
3889
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003890 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 * their arguments as well.
3892 */
3893 if (STRNICMP(arg, "complete", p - arg) == 0)
3894 {
3895 xp->xp_context = EXPAND_USER_COMPLETE;
3896 xp->xp_pattern = p + 1;
3897 return NULL;
3898 }
3899 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3900 {
3901 xp->xp_context = EXPAND_USER_NARGS;
3902 xp->xp_pattern = p + 1;
3903 return NULL;
3904 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003905 else if (STRNICMP(arg, "addr", p - arg) == 0)
3906 {
3907 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3908 xp->xp_pattern = p + 1;
3909 return NULL;
3910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 return NULL;
3912 }
3913 arg = skipwhite(p);
3914 }
3915
3916 /* After the attributes comes the new command name */
3917 p = skiptowhite(arg);
3918 if (*p == NUL)
3919 {
3920 xp->xp_context = EXPAND_USER_COMMANDS;
3921 xp->xp_pattern = arg;
3922 break;
3923 }
3924
3925 /* And finally comes a normal command */
3926 return skipwhite(p);
3927
3928 case CMD_delcommand:
3929 xp->xp_context = EXPAND_USER_COMMANDS;
3930 xp->xp_pattern = arg;
3931 break;
3932# endif
3933
3934 case CMD_global:
3935 case CMD_vglobal:
3936 delim = *arg; /* get the delimiter */
3937 if (delim)
3938 ++arg; /* skip delimiter if there is one */
3939
3940 while (arg[0] != NUL && arg[0] != delim)
3941 {
3942 if (arg[0] == '\\' && arg[1] != NUL)
3943 ++arg;
3944 ++arg;
3945 }
3946 if (arg[0] != NUL)
3947 return arg + 1;
3948 break;
3949 case CMD_and:
3950 case CMD_substitute:
3951 delim = *arg;
3952 if (delim)
3953 {
3954 /* skip "from" part */
3955 ++arg;
3956 arg = skip_regexp(arg, delim, p_magic, NULL);
3957 }
3958 /* skip "to" part */
3959 while (arg[0] != NUL && arg[0] != delim)
3960 {
3961 if (arg[0] == '\\' && arg[1] != NUL)
3962 ++arg;
3963 ++arg;
3964 }
3965 if (arg[0] != NUL) /* skip delimiter */
3966 ++arg;
3967 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3968 ++arg;
3969 if (arg[0] != NUL)
3970 return arg;
3971 break;
3972 case CMD_isearch:
3973 case CMD_dsearch:
3974 case CMD_ilist:
3975 case CMD_dlist:
3976 case CMD_ijump:
3977 case CMD_psearch:
3978 case CMD_djump:
3979 case CMD_isplit:
3980 case CMD_dsplit:
3981 arg = skipwhite(skipdigits(arg)); /* skip count */
3982 if (*arg == '/') /* Match regexp, not just whole words */
3983 {
3984 for (++arg; *arg && *arg != '/'; arg++)
3985 if (*arg == '\\' && arg[1] != NUL)
3986 arg++;
3987 if (*arg)
3988 {
3989 arg = skipwhite(arg + 1);
3990
3991 /* Check for trailing illegal characters */
3992 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3993 xp->xp_context = EXPAND_NOTHING;
3994 else
3995 return arg;
3996 }
3997 }
3998 break;
3999#ifdef FEAT_AUTOCMD
4000 case CMD_autocmd:
4001 return set_context_in_autocmd(xp, arg, FALSE);
4002
4003 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004004 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 return set_context_in_autocmd(xp, arg, TRUE);
4006#endif
4007 case CMD_set:
4008 set_context_in_set_cmd(xp, arg, 0);
4009 break;
4010 case CMD_setglobal:
4011 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4012 break;
4013 case CMD_setlocal:
4014 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4015 break;
4016 case CMD_tag:
4017 case CMD_stag:
4018 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004019 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 case CMD_tselect:
4021 case CMD_stselect:
4022 case CMD_ptselect:
4023 case CMD_tjump:
4024 case CMD_stjump:
4025 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004026 if (*p_wop != NUL)
4027 xp->xp_context = EXPAND_TAGS_LISTFILES;
4028 else
4029 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 xp->xp_pattern = arg;
4031 break;
4032 case CMD_augroup:
4033 xp->xp_context = EXPAND_AUGROUP;
4034 xp->xp_pattern = arg;
4035 break;
4036#ifdef FEAT_SYN_HL
4037 case CMD_syntax:
4038 set_context_in_syntax_cmd(xp, arg);
4039 break;
4040#endif
4041#ifdef FEAT_EVAL
4042 case CMD_let:
4043 case CMD_if:
4044 case CMD_elseif:
4045 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004046 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 case CMD_echo:
4048 case CMD_echon:
4049 case CMD_execute:
4050 case CMD_echomsg:
4051 case CMD_echoerr:
4052 case CMD_call:
4053 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004054 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 break;
4056
4057 case CMD_unlet:
4058 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4059 arg = xp->xp_pattern + 1;
4060 xp->xp_context = EXPAND_USER_VARS;
4061 xp->xp_pattern = arg;
4062 break;
4063
4064 case CMD_function:
4065 case CMD_delfunction:
4066 xp->xp_context = EXPAND_USER_FUNC;
4067 xp->xp_pattern = arg;
4068 break;
4069
4070 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004071 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 break;
4073#endif
4074 case CMD_highlight:
4075 set_context_in_highlight_cmd(xp, arg);
4076 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004077#ifdef FEAT_CSCOPE
4078 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004079 case CMD_lcscope:
4080 case CMD_scscope:
4081 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004082 break;
4083#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004084#ifdef FEAT_SIGNS
4085 case CMD_sign:
4086 set_context_in_sign_cmd(xp, arg);
4087 break;
4088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089#ifdef FEAT_LISTCMDS
4090 case CMD_bdelete:
4091 case CMD_bwipeout:
4092 case CMD_bunload:
4093 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4094 arg = xp->xp_pattern + 1;
4095 /*FALLTHROUGH*/
4096 case CMD_buffer:
4097 case CMD_sbuffer:
4098 case CMD_checktime:
4099 xp->xp_context = EXPAND_BUFFERS;
4100 xp->xp_pattern = arg;
4101 break;
4102#endif
4103#ifdef FEAT_USR_CMDS
4104 case CMD_USER:
4105 case CMD_USER_BUF:
4106 if (compl != EXPAND_NOTHING)
4107 {
4108 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004109 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 {
4111# ifdef FEAT_MENU
4112 if (compl == EXPAND_MENUS)
4113 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4114# endif
4115 if (compl == EXPAND_COMMANDS)
4116 return arg;
4117 if (compl == EXPAND_MAPPINGS)
4118 return set_context_in_map_cmd(xp, (char_u *)"map",
4119 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004120 /* Find start of last argument. */
4121 p = arg;
4122 while (*p)
4123 {
4124 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004125 /* argument starts after a space */
4126 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004127 else if (*p == '\\' && *(p + 1) != NUL)
4128 ++p; /* skip over escaped character */
4129 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 xp->xp_pattern = arg;
4132 }
4133 xp->xp_context = compl;
4134 }
4135 break;
4136#endif
4137 case CMD_map: case CMD_noremap:
4138 case CMD_nmap: case CMD_nnoremap:
4139 case CMD_vmap: case CMD_vnoremap:
4140 case CMD_omap: case CMD_onoremap:
4141 case CMD_imap: case CMD_inoremap:
4142 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004143 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004144 case CMD_smap: case CMD_snoremap:
4145 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004147 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 case CMD_unmap:
4149 case CMD_nunmap:
4150 case CMD_vunmap:
4151 case CMD_ounmap:
4152 case CMD_iunmap:
4153 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004154 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004155 case CMD_sunmap:
4156 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004158 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 case CMD_abbreviate: case CMD_noreabbrev:
4160 case CMD_cabbrev: case CMD_cnoreabbrev:
4161 case CMD_iabbrev: case CMD_inoreabbrev:
4162 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004163 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 case CMD_unabbreviate:
4165 case CMD_cunabbrev:
4166 case CMD_iunabbrev:
4167 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004168 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169#ifdef FEAT_MENU
4170 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4171 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4172 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4173 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4174 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4175 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4176 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4177 case CMD_tmenu: case CMD_tunmenu:
4178 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4179 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4180#endif
4181
4182 case CMD_colorscheme:
4183 xp->xp_context = EXPAND_COLORS;
4184 xp->xp_pattern = arg;
4185 break;
4186
4187 case CMD_compiler:
4188 xp->xp_context = EXPAND_COMPILER;
4189 xp->xp_pattern = arg;
4190 break;
4191
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004192 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004193 xp->xp_context = EXPAND_OWNSYNTAX;
4194 xp->xp_pattern = arg;
4195 break;
4196
4197 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004198 xp->xp_context = EXPAND_FILETYPE;
4199 xp->xp_pattern = arg;
4200 break;
4201
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004202 case CMD_packadd:
4203 xp->xp_context = EXPAND_PACKADD;
4204 xp->xp_pattern = arg;
4205 break;
4206
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4208 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4209 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004210 p = skiptowhite(arg);
4211 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 {
4213 xp->xp_context = EXPAND_LANGUAGE;
4214 xp->xp_pattern = arg;
4215 }
4216 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004217 {
4218 if ( STRNCMP(arg, "messages", p - arg) == 0
4219 || STRNCMP(arg, "ctype", p - arg) == 0
4220 || STRNCMP(arg, "time", p - arg) == 0)
4221 {
4222 xp->xp_context = EXPAND_LOCALES;
4223 xp->xp_pattern = skipwhite(p);
4224 }
4225 else
4226 xp->xp_context = EXPAND_NOTHING;
4227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 break;
4229#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004230#if defined(FEAT_PROFILE)
4231 case CMD_profile:
4232 set_context_in_profile_cmd(xp, arg);
4233 break;
4234#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004235 case CMD_behave:
4236 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004237 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004238 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004240#if defined(FEAT_CMDHIST)
4241 case CMD_history:
4242 xp->xp_context = EXPAND_HISTORY;
4243 xp->xp_pattern = arg;
4244 break;
4245#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004246#if defined(FEAT_PROFILE)
4247 case CMD_syntime:
4248 xp->xp_context = EXPAND_SYNTIME;
4249 xp->xp_pattern = arg;
4250 break;
4251#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004252
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253#endif /* FEAT_CMDL_COMPL */
4254
4255 default:
4256 break;
4257 }
4258 return NULL;
4259}
4260
4261/*
4262 * skip a range specifier of the form: addr [,addr] [;addr] ..
4263 *
4264 * Backslashed delimiters after / or ? will be skipped, and commands will
4265 * not be expanded between /'s and ?'s or after "'".
4266 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004267 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 * Returns the "cmd" pointer advanced to beyond the range.
4269 */
4270 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004271skip_range(
4272 char_u *cmd,
4273 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004275 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276
Bram Moolenaardf177f62005-02-22 08:39:57 +00004277 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 {
4279 if (*cmd == '\'')
4280 {
4281 if (*++cmd == NUL && ctx != NULL)
4282 *ctx = EXPAND_NOTHING;
4283 }
4284 else if (*cmd == '/' || *cmd == '?')
4285 {
4286 delim = *cmd++;
4287 while (*cmd != NUL && *cmd != delim)
4288 if (*cmd++ == '\\' && *cmd != NUL)
4289 ++cmd;
4290 if (*cmd == NUL && ctx != NULL)
4291 *ctx = EXPAND_NOTHING;
4292 }
4293 if (*cmd != NUL)
4294 ++cmd;
4295 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004296
4297 /* Skip ":" and white space. */
4298 while (*cmd == ':')
4299 cmd = skipwhite(cmd + 1);
4300
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 return cmd;
4302}
4303
4304/*
4305 * get a single EX address
4306 *
4307 * Set ptr to the next character after the part that was interpreted.
4308 * Set ptr to NULL when an error is encountered.
4309 *
4310 * Return MAXLNUM when no Ex address was found.
4311 */
4312 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004313get_address(
4314 exarg_T *eap UNUSED,
4315 char_u **ptr,
4316 int addr_type, /* flag: one of ADDR_LINES, ... */
4317 int skip, /* only skip the address, don't use it */
4318 int to_other_file) /* flag: may jump to other file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319{
4320 int c;
4321 int i;
4322 long n;
4323 char_u *cmd;
4324 pos_T pos;
4325 pos_T *fp;
4326 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004327 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328
4329 cmd = skipwhite(*ptr);
4330 lnum = MAXLNUM;
4331 do
4332 {
4333 switch (*cmd)
4334 {
4335 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004336 ++cmd;
4337 switch (addr_type)
4338 {
4339 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 lnum = curwin->w_cursor.lnum;
4341 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004342 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004343 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004344 break;
4345 case ADDR_ARGUMENTS:
4346 lnum = curwin->w_arg_idx + 1;
4347 break;
4348 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004349 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004350 lnum = curbuf->b_fnum;
4351 break;
4352 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004353 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004354 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004355#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004356 case ADDR_QUICKFIX:
4357 lnum = qf_get_cur_valid_idx(eap);
4358 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004359#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004360 }
4361 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362
4363 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004364 ++cmd;
4365 switch (addr_type)
4366 {
4367 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 lnum = curbuf->b_ml.ml_line_count;
4369 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004370 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004371 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004372 break;
4373 case ADDR_ARGUMENTS:
4374 lnum = ARGCOUNT;
4375 break;
4376 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004377 buf = lastbuf;
4378 while (buf->b_ml.ml_mfp == NULL)
4379 {
4380 if (buf->b_prev == NULL)
4381 break;
4382 buf = buf->b_prev;
4383 }
4384 lnum = buf->b_fnum;
4385 break;
4386 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004387 lnum = lastbuf->b_fnum;
4388 break;
4389 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004390 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004391 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004392#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004393 case ADDR_QUICKFIX:
4394 lnum = qf_get_size(eap);
4395 if (lnum == 0)
4396 lnum = 1;
4397 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004398#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004399 }
4400 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401
4402 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004403 if (*++cmd == NUL)
4404 {
4405 cmd = NULL;
4406 goto error;
4407 }
4408 if (addr_type != ADDR_LINES)
4409 {
4410 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004411 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004412 goto error;
4413 }
4414 if (skip)
4415 ++cmd;
4416 else
4417 {
4418 /* Only accept a mark in another file when it is
4419 * used by itself: ":'M". */
4420 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4421 ++cmd;
4422 if (fp == (pos_T *)-1)
4423 /* Jumped to another file. */
4424 lnum = curwin->w_cursor.lnum;
4425 else
4426 {
4427 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 {
4429 cmd = NULL;
4430 goto error;
4431 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004432 lnum = fp->lnum;
4433 }
4434 }
4435 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436
4437 case '/':
4438 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004439 c = *cmd++;
4440 if (addr_type != ADDR_LINES)
4441 {
4442 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004443 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004444 goto error;
4445 }
4446 if (skip) /* skip "/pat/" */
4447 {
4448 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4449 if (*cmd == c)
4450 ++cmd;
4451 }
4452 else
4453 {
4454 pos = curwin->w_cursor; /* save curwin->w_cursor */
4455 /*
4456 * When '/' or '?' follows another address, start
4457 * from there.
4458 */
4459 if (lnum != MAXLNUM)
4460 curwin->w_cursor.lnum = lnum;
4461 /*
4462 * Start a forward search at the end of the line.
4463 * Start a backward search at the start of the line.
4464 * This makes sure we never match in the current
4465 * line, and can match anywhere in the
4466 * next/previous line.
4467 */
4468 if (c == '/')
4469 curwin->w_cursor.col = MAXCOL;
4470 else
4471 curwin->w_cursor.col = 0;
4472 searchcmdlen = 0;
4473 if (!do_search(NULL, c, cmd, 1L,
4474 SEARCH_HIS | SEARCH_MSG, NULL))
4475 {
4476 curwin->w_cursor = pos;
4477 cmd = NULL;
4478 goto error;
4479 }
4480 lnum = curwin->w_cursor.lnum;
4481 curwin->w_cursor = pos;
4482 /* adjust command string pointer */
4483 cmd += searchcmdlen;
4484 }
4485 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486
4487 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004488 ++cmd;
4489 if (addr_type != ADDR_LINES)
4490 {
4491 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004492 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004493 goto error;
4494 }
4495 if (*cmd == '&')
4496 i = RE_SUBST;
4497 else if (*cmd == '?' || *cmd == '/')
4498 i = RE_SEARCH;
4499 else
4500 {
4501 EMSG(_(e_backslash));
4502 cmd = NULL;
4503 goto error;
4504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004506 if (!skip)
4507 {
4508 /*
4509 * When search follows another address, start from
4510 * there.
4511 */
4512 if (lnum != MAXLNUM)
4513 pos.lnum = lnum;
4514 else
4515 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004517 /*
4518 * Start the search just like for the above
4519 * do_search().
4520 */
4521 if (*cmd != '?')
4522 pos.col = MAXCOL;
4523 else
4524 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004525#ifdef FEAT_VIRTUALEDIT
4526 pos.coladd = 0;
4527#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004528 if (searchit(curwin, curbuf, &pos,
4529 *cmd == '?' ? BACKWARD : FORWARD,
4530 (char_u *)"", 1L, SEARCH_MSG,
4531 i, (linenr_T)0, NULL) != FAIL)
4532 lnum = pos.lnum;
4533 else
4534 {
4535 cmd = NULL;
4536 goto error;
4537 }
4538 }
4539 ++cmd;
4540 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541
4542 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004543 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4544 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 }
4546
4547 for (;;)
4548 {
4549 cmd = skipwhite(cmd);
4550 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4551 break;
4552
4553 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004554 {
4555 switch (addr_type)
4556 {
4557 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004558 /* "+1" is same as ".+1" */
4559 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004560 break;
4561 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004562 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004563 break;
4564 case ADDR_ARGUMENTS:
4565 lnum = curwin->w_arg_idx + 1;
4566 break;
4567 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004568 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004569 lnum = curbuf->b_fnum;
4570 break;
4571 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004572 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004573 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004574#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004575 case ADDR_QUICKFIX:
4576 lnum = qf_get_cur_valid_idx(eap);
4577 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004578#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004579 }
4580 }
4581
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 if (VIM_ISDIGIT(*cmd))
4583 i = '+'; /* "number" is same as "+number" */
4584 else
4585 i = *cmd++;
4586 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4587 n = 1;
4588 else
4589 n = getdigits(&cmd);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004590 if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004591 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004592 lnum = compute_buffer_local_count(
4593 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004594 else if (i == '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 lnum -= n;
4596 else
4597 lnum += n;
4598 }
4599 } while (*cmd == '/' || *cmd == '?');
4600
4601error:
4602 *ptr = cmd;
4603 return lnum;
4604}
4605
4606/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004607 * Get flags from an Ex command argument.
4608 */
4609 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004610get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004611{
4612 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4613 {
4614 if (*eap->arg == 'l')
4615 eap->flags |= EXFLAG_LIST;
4616 else if (*eap->arg == 'p')
4617 eap->flags |= EXFLAG_PRINT;
4618 else
4619 eap->flags |= EXFLAG_NR;
4620 eap->arg = skipwhite(eap->arg + 1);
4621 }
4622}
4623
4624/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 * Function called for command which is Not Implemented. NI!
4626 */
4627 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004628ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629{
4630 if (!eap->skip)
4631 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4632}
4633
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004634#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635/*
4636 * Function called for script command which is Not Implemented. NI!
4637 * Skips over ":perl <<EOF" constructs.
4638 */
4639 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004640ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641{
4642 if (!eap->skip)
4643 ex_ni(eap);
4644 else
4645 vim_free(script_get(eap, eap->arg));
4646}
4647#endif
4648
4649/*
4650 * Check range in Ex command for validity.
4651 * Return NULL when valid, error message when invalid.
4652 */
4653 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004654invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004656 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 if ( eap->line1 < 0
4658 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004659 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004661
4662 if (eap->argt & RANGE)
4663 {
4664 switch(eap->addr_type)
4665 {
4666 case ADDR_LINES:
4667 if (!(eap->argt & NOTADR)
4668 && eap->line2 > curbuf->b_ml.ml_line_count
4669#ifdef FEAT_DIFF
4670 + (eap->cmdidx == CMD_diffget)
4671#endif
4672 )
4673 return (char_u *)_(e_invrange);
4674 break;
4675 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004676 /* add 1 if ARGCOUNT is 0 */
4677 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004678 return (char_u *)_(e_invrange);
4679 break;
4680 case ADDR_BUFFERS:
4681 if (eap->line1 < firstbuf->b_fnum
4682 || eap->line2 > lastbuf->b_fnum)
4683 return (char_u *)_(e_invrange);
4684 break;
4685 case ADDR_LOADED_BUFFERS:
4686 buf = firstbuf;
4687 while (buf->b_ml.ml_mfp == NULL)
4688 {
4689 if (buf->b_next == NULL)
4690 return (char_u *)_(e_invrange);
4691 buf = buf->b_next;
4692 }
4693 if (eap->line1 < buf->b_fnum)
4694 return (char_u *)_(e_invrange);
4695 buf = lastbuf;
4696 while (buf->b_ml.ml_mfp == NULL)
4697 {
4698 if (buf->b_prev == NULL)
4699 return (char_u *)_(e_invrange);
4700 buf = buf->b_prev;
4701 }
4702 if (eap->line2 > buf->b_fnum)
4703 return (char_u *)_(e_invrange);
4704 break;
4705 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004706 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004707 return (char_u *)_(e_invrange);
4708 break;
4709 case ADDR_TABS:
4710 if (eap->line2 > LAST_TAB_NR)
4711 return (char_u *)_(e_invrange);
4712 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004713#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004714 case ADDR_QUICKFIX:
4715 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4716 return (char_u *)_(e_invrange);
4717 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004718#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004719 }
4720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 return NULL;
4722}
4723
4724/*
4725 * Correct the range for zero line number, if required.
4726 */
4727 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004728correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729{
4730 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4731 {
4732 if (eap->line1 == 0)
4733 eap->line1 = 1;
4734 if (eap->line2 == 0)
4735 eap->line2 = 1;
4736 }
4737}
4738
Bram Moolenaar748bf032005-02-02 23:04:36 +00004739#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004740static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004741
4742/*
4743 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4744 * pattern. Otherwise return eap->arg.
4745 */
4746 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004747skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004748{
4749 char_u *p = eap->arg;
4750
Bram Moolenaara37420f2006-02-04 22:37:47 +00004751 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4752 || eap->cmdidx == CMD_vimgrepadd
4753 || eap->cmdidx == CMD_lvimgrepadd
4754 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004755 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004756 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004757 if (p == NULL)
4758 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004759 }
4760 return p;
4761}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004762
4763/*
4764 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4765 * in the command line, so that things like % get expanded.
4766 */
4767 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004768replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004769{
4770 char_u *new_cmdline;
4771 char_u *program;
4772 char_u *pos;
4773 char_u *ptr;
4774 int len;
4775 int i;
4776
4777 /*
4778 * Don't do it when ":vimgrep" is used for ":grep".
4779 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004780 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4781 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4782 || eap->cmdidx == CMD_grepadd
4783 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004784 && !grep_internal(eap->cmdidx))
4785 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004786 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4787 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004788 {
4789 if (*curbuf->b_p_gp == NUL)
4790 program = p_gp;
4791 else
4792 program = curbuf->b_p_gp;
4793 }
4794 else
4795 {
4796 if (*curbuf->b_p_mp == NUL)
4797 program = p_mp;
4798 else
4799 program = curbuf->b_p_mp;
4800 }
4801
4802 p = skipwhite(p);
4803
4804 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4805 {
4806 /* replace $* by given arguments */
4807 i = 1;
4808 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4809 ++i;
4810 len = (int)STRLEN(p);
4811 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4812 if (new_cmdline == NULL)
4813 return NULL; /* out of memory */
4814 ptr = new_cmdline;
4815 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4816 {
4817 i = (int)(pos - program);
4818 STRNCPY(ptr, program, i);
4819 STRCPY(ptr += i, p);
4820 ptr += len;
4821 program = pos + 2;
4822 }
4823 STRCPY(ptr, program);
4824 }
4825 else
4826 {
4827 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4828 if (new_cmdline == NULL)
4829 return NULL; /* out of memory */
4830 STRCPY(new_cmdline, program);
4831 STRCAT(new_cmdline, " ");
4832 STRCAT(new_cmdline, p);
4833 }
4834 msg_make(p);
4835
4836 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4837 vim_free(*cmdlinep);
4838 *cmdlinep = new_cmdline;
4839 p = new_cmdline;
4840 }
4841 return p;
4842}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004843#endif
4844
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845/*
4846 * Expand file name in Ex command argument.
4847 * Return FAIL for failure, OK otherwise.
4848 */
4849 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004850expand_filename(
4851 exarg_T *eap,
4852 char_u **cmdlinep,
4853 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854{
4855 int has_wildcards; /* need to expand wildcards */
4856 char_u *repl;
4857 int srclen;
4858 char_u *p;
4859 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004860 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861
Bram Moolenaar748bf032005-02-02 23:04:36 +00004862#ifdef FEAT_QUICKFIX
4863 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4864 p = skip_grep_pat(eap);
4865#else
4866 p = eap->arg;
4867#endif
4868
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 /*
4870 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4871 * the file name contains a wildcard it should not cause expanding.
4872 * (it will be expanded anyway if there is a wildcard before replacing).
4873 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004874 has_wildcards = mch_has_wildcard(p);
4875 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004877#ifdef FEAT_EVAL
4878 /* Skip over `=expr`, wildcards in it are not expanded. */
4879 if (p[0] == '`' && p[1] == '=')
4880 {
4881 p += 2;
4882 (void)skip_expr(&p);
4883 if (*p == '`')
4884 ++p;
4885 continue;
4886 }
4887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 /*
4889 * Quick check if this cannot be the start of a special string.
4890 * Also removes backslash before '%', '#' and '<'.
4891 */
4892 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4893 {
4894 ++p;
4895 continue;
4896 }
4897
4898 /*
4899 * Try to find a match at this position.
4900 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004901 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4902 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 if (*errormsgp != NULL) /* error detected */
4904 return FAIL;
4905 if (repl == NULL) /* no match found */
4906 {
4907 p += srclen;
4908 continue;
4909 }
4910
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004911 /* Wildcards won't be expanded below, the replacement is taken
4912 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4913 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4914 {
4915 char_u *l = repl;
4916
4917 repl = expand_env_save(repl);
4918 vim_free(l);
4919 }
4920
Bram Moolenaar63b92542007-03-27 14:57:09 +00004921 /* Need to escape white space et al. with a backslash.
4922 * Don't do this for:
4923 * - replacement that already has been escaped: "##"
4924 * - shell commands (may have to use quotes instead).
4925 * - non-unix systems when there is a single argument (spaces don't
4926 * separate arguments then).
4927 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004929 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 && eap->cmdidx != CMD_bang
4931 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004932 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004934 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004936 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937#ifndef UNIX
4938 && !(eap->argt & NOSPC)
4939#endif
4940 )
4941 {
4942 char_u *l;
4943#ifdef BACKSLASH_IN_FILENAME
4944 /* Don't escape a backslash here, because rem_backslash() doesn't
4945 * remove it later. */
4946 static char_u *nobslash = (char_u *)" \t\"|";
4947# define ESCAPE_CHARS nobslash
4948#else
4949# define ESCAPE_CHARS escape_chars
4950#endif
4951
4952 for (l = repl; *l; ++l)
4953 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4954 {
4955 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4956 if (l != NULL)
4957 {
4958 vim_free(repl);
4959 repl = l;
4960 }
4961 break;
4962 }
4963 }
4964
4965 /* For a shell command a '!' must be escaped. */
4966 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004967 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 {
4969 char_u *l;
4970
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004971 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 if (l != NULL)
4973 {
4974 vim_free(repl);
4975 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 }
4977 }
4978
4979 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4980 vim_free(repl);
4981 if (p == NULL)
4982 return FAIL;
4983 }
4984
4985 /*
4986 * One file argument: Expand wildcards.
4987 * Don't do this with ":r !command" or ":w !command".
4988 */
4989 if ((eap->argt & NOSPC) && !eap->usefilter)
4990 {
4991 /*
4992 * May do this twice:
4993 * 1. Replace environment variables.
4994 * 2. Replace any other wildcards, remove backslashes.
4995 */
4996 for (n = 1; n <= 2; ++n)
4997 {
4998 if (n == 2)
4999 {
5000#ifdef UNIX
5001 /*
5002 * Only for Unix we check for more than one file name.
5003 * For other systems spaces are considered to be part
5004 * of the file name.
5005 * Only check here if there is no wildcard, otherwise
5006 * ExpandOne() will check for errors. This allows
5007 * ":e `ls ve*.c`" on Unix.
5008 */
5009 if (!has_wildcards)
5010 for (p = eap->arg; *p; ++p)
5011 {
5012 /* skip escaped characters */
5013 if (p[1] && (*p == '\\' || *p == Ctrl_V))
5014 ++p;
5015 else if (vim_iswhite(*p))
5016 {
5017 *errormsgp = (char_u *)_("E172: Only one file name allowed");
5018 return FAIL;
5019 }
5020 }
5021#endif
5022
5023 /*
5024 * Halve the number of backslashes (this is Vi compatible).
5025 * For Unix and OS/2, when wildcards are expanded, this is
5026 * done by ExpandOne() below.
5027 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005028#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 if (!has_wildcards)
5030#endif
5031 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 }
5033
5034 if (has_wildcards)
5035 {
5036 if (n == 1)
5037 {
5038 /*
5039 * First loop: May expand environment variables. This
5040 * can be done much faster with expand_env() than with
5041 * something else (e.g., calling a shell).
5042 * After expanding environment variables, check again
5043 * if there are still wildcards present.
5044 */
5045 if (vim_strchr(eap->arg, '$') != NULL
5046 || vim_strchr(eap->arg, '~') != NULL)
5047 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005048 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005049 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 has_wildcards = mch_has_wildcard(NameBuff);
5051 p = NameBuff;
5052 }
5053 else
5054 p = NULL;
5055 }
5056 else /* n == 2 */
5057 {
5058 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005059 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060
5061 ExpandInit(&xpc);
5062 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005063 if (p_wic)
5064 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005066 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 if (p == NULL)
5068 return FAIL;
5069 }
5070 if (p != NULL)
5071 {
5072 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5073 p, cmdlinep);
5074 if (n == 2) /* p came from ExpandOne() */
5075 vim_free(p);
5076 }
5077 }
5078 }
5079 }
5080 return OK;
5081}
5082
5083/*
5084 * Replace part of the command line, keeping eap->cmd, eap->arg and
5085 * eap->nextcmd correct.
5086 * "src" points to the part that is to be replaced, of length "srclen".
5087 * "repl" is the replacement string.
5088 * Returns a pointer to the character after the replaced string.
5089 * Returns NULL for failure.
5090 */
5091 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005092repl_cmdline(
5093 exarg_T *eap,
5094 char_u *src,
5095 int srclen,
5096 char_u *repl,
5097 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098{
5099 int len;
5100 int i;
5101 char_u *new_cmdline;
5102
5103 /*
5104 * The new command line is build in new_cmdline[].
5105 * First allocate it.
5106 * Careful: a "+cmd" argument may have been NUL terminated.
5107 */
5108 len = (int)STRLEN(repl);
5109 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005110 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5112 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5113 return NULL; /* out of memory! */
5114
5115 /*
5116 * Copy the stuff before the expanded part.
5117 * Copy the expanded stuff.
5118 * Copy what came after the expanded part.
5119 * Copy the next commands, if there are any.
5120 */
5121 i = (int)(src - *cmdlinep); /* length of part before match */
5122 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005123
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 mch_memmove(new_cmdline + i, repl, (size_t)len);
5125 i += len; /* remember the end of the string */
5126 STRCPY(new_cmdline + i, src + srclen);
5127 src = new_cmdline + i; /* remember where to continue */
5128
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005129 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 {
5131 i = (int)STRLEN(new_cmdline) + 1;
5132 STRCPY(new_cmdline + i, eap->nextcmd);
5133 eap->nextcmd = new_cmdline + i;
5134 }
5135 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5136 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5137 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5138 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5139 vim_free(*cmdlinep);
5140 *cmdlinep = new_cmdline;
5141
5142 return src;
5143}
5144
5145/*
5146 * Check for '|' to separate commands and '"' to start comments.
5147 */
5148 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005149separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150{
5151 char_u *p;
5152
Bram Moolenaar86b68352004-12-27 21:59:20 +00005153#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005154 p = skip_grep_pat(eap);
5155#else
5156 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005157#endif
5158
5159 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 {
5161 if (*p == Ctrl_V)
5162 {
5163 if (eap->argt & (USECTRLV | XFILE))
5164 ++p; /* skip CTRL-V and next char */
5165 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005166 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005167 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 if (*p == NUL) /* stop at NUL after CTRL-V */
5169 break;
5170 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005171
5172#ifdef FEAT_EVAL
5173 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005174 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005175 {
5176 p += 2;
5177 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005178 }
5179#endif
5180
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 /* Check for '"': start of comment or '|': next command */
5182 /* :@" and :*" do not start a comment!
5183 * :redir @" doesn't either. */
5184 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5185 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5186 || p != eap->arg)
5187 && (eap->cmdidx != CMD_redir
5188 || p != eap->arg + 1 || p[-1] != '@'))
5189 || *p == '|' || *p == '\n')
5190 {
5191 /*
5192 * We remove the '\' before the '|', unless USECTRLV is used
5193 * AND 'b' is present in 'cpoptions'.
5194 */
5195 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5196 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5197 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005198 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 --p;
5200 }
5201 else
5202 {
5203 eap->nextcmd = check_nextcmd(p);
5204 *p = NUL;
5205 break;
5206 }
5207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005209
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5211 del_trailing_spaces(eap->arg);
5212}
5213
5214/*
5215 * get + command from ex argument
5216 */
5217 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005218getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219{
5220 char_u *arg = *argp;
5221 char_u *command = NULL;
5222
5223 if (*arg == '+') /* +[command] */
5224 {
5225 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005226 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 command = dollar_command;
5228 else
5229 {
5230 command = arg;
5231 arg = skip_cmd_arg(command, TRUE);
5232 if (*arg != NUL)
5233 *arg++ = NUL; /* terminate command with NUL */
5234 }
5235
5236 arg = skipwhite(arg); /* skip over spaces */
5237 *argp = arg;
5238 }
5239 return command;
5240}
5241
5242/*
5243 * Find end of "+command" argument. Skip over "\ " and "\\".
5244 */
5245 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005246skip_cmd_arg(
5247 char_u *p,
5248 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249{
5250 while (*p && !vim_isspace(*p))
5251 {
5252 if (*p == '\\' && p[1] != NUL)
5253 {
5254 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005255 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 else
5257 ++p;
5258 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005259 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 }
5261 return p;
5262}
5263
5264/*
5265 * Get "++opt=arg" argument.
5266 * Return FAIL or OK.
5267 */
5268 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005269getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270{
5271 char_u *arg = eap->arg + 2;
5272 int *pp = NULL;
5273#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005274 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 char_u *p;
5276#endif
5277
5278 /* ":edit ++[no]bin[ary] file" */
5279 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5280 {
5281 if (*arg == 'n')
5282 {
5283 arg += 2;
5284 eap->force_bin = FORCE_NOBIN;
5285 }
5286 else
5287 eap->force_bin = FORCE_BIN;
5288 if (!checkforcmd(&arg, "binary", 3))
5289 return FAIL;
5290 eap->arg = skipwhite(arg);
5291 return OK;
5292 }
5293
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005294 /* ":read ++edit file" */
5295 if (STRNCMP(arg, "edit", 4) == 0)
5296 {
5297 eap->read_edit = TRUE;
5298 eap->arg = skipwhite(arg + 4);
5299 return OK;
5300 }
5301
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 if (STRNCMP(arg, "ff", 2) == 0)
5303 {
5304 arg += 2;
5305 pp = &eap->force_ff;
5306 }
5307 else if (STRNCMP(arg, "fileformat", 10) == 0)
5308 {
5309 arg += 10;
5310 pp = &eap->force_ff;
5311 }
5312#ifdef FEAT_MBYTE
5313 else if (STRNCMP(arg, "enc", 3) == 0)
5314 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005315 if (STRNCMP(arg, "encoding", 8) == 0)
5316 arg += 8;
5317 else
5318 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 pp = &eap->force_enc;
5320 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005321 else if (STRNCMP(arg, "bad", 3) == 0)
5322 {
5323 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005324 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326#endif
5327
5328 if (pp == NULL || *arg != '=')
5329 return FAIL;
5330
5331 ++arg;
5332 *pp = (int)(arg - eap->cmd);
5333 arg = skip_cmd_arg(arg, FALSE);
5334 eap->arg = skipwhite(arg);
5335 *arg = NUL;
5336
5337#ifdef FEAT_MBYTE
5338 if (pp == &eap->force_ff)
5339 {
5340#endif
5341 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5342 return FAIL;
5343#ifdef FEAT_MBYTE
5344 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005345 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 {
5347 /* Make 'fileencoding' lower case. */
5348 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5349 *p = TOLOWER_ASC(*p);
5350 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005351 else
5352 {
5353 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5354 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005355 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005356 if (STRICMP(p, "keep") == 0)
5357 eap->bad_char = BAD_KEEP;
5358 else if (STRICMP(p, "drop") == 0)
5359 eap->bad_char = BAD_DROP;
5360 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5361 eap->bad_char = *p;
5362 else
5363 return FAIL;
5364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365#endif
5366
5367 return OK;
5368}
5369
5370/*
5371 * ":abbreviate" and friends.
5372 */
5373 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005374ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375{
5376 do_exmap(eap, TRUE); /* almost the same as mapping */
5377}
5378
5379/*
5380 * ":map" and friends.
5381 */
5382 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005383ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384{
5385 /*
5386 * If we are sourcing .exrc or .vimrc in current directory we
5387 * print the mappings for security reasons.
5388 */
5389 if (secure)
5390 {
5391 secure = 2;
5392 msg_outtrans(eap->cmd);
5393 msg_putchar('\n');
5394 }
5395 do_exmap(eap, FALSE);
5396}
5397
5398/*
5399 * ":unmap" and friends.
5400 */
5401 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005402ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403{
5404 do_exmap(eap, FALSE);
5405}
5406
5407/*
5408 * ":mapclear" and friends.
5409 */
5410 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005411ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412{
5413 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5414}
5415
5416/*
5417 * ":abclear" and friends.
5418 */
5419 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005420ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421{
5422 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5423}
5424
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005425#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005427ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428{
5429 /*
5430 * Disallow auto commands from .exrc and .vimrc in current
5431 * directory for security reasons.
5432 */
5433 if (secure)
5434 {
5435 secure = 2;
5436 eap->errmsg = e_curdir;
5437 }
5438 else if (eap->cmdidx == CMD_autocmd)
5439 do_autocmd(eap->arg, eap->forceit);
5440 else
5441 do_augroup(eap->arg, eap->forceit);
5442}
5443
5444/*
5445 * ":doautocmd": Apply the automatic commands to the current buffer.
5446 */
5447 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005448ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005450 char_u *arg = eap->arg;
5451 int call_do_modelines = check_nomodeline(&arg);
5452
5453 (void)do_doautocmd(arg, TRUE);
5454 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5455 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456}
5457#endif
5458
5459#ifdef FEAT_LISTCMDS
5460/*
5461 * :[N]bunload[!] [N] [bufname] unload buffer
5462 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5463 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5464 */
5465 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005466ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467{
5468 eap->errmsg = do_bufdel(
5469 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5470 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5471 : DOBUF_UNLOAD, eap->arg,
5472 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5473}
5474
5475/*
5476 * :[N]buffer [N] to buffer N
5477 * :[N]sbuffer [N] to buffer N
5478 */
5479 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005480ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481{
5482 if (*eap->arg)
5483 eap->errmsg = e_trailing;
5484 else
5485 {
5486 if (eap->addr_count == 0) /* default is current buffer */
5487 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5488 else
5489 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005490 if (eap->do_ecmd_cmd != NULL)
5491 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 }
5493}
5494
5495/*
5496 * :[N]bmodified [N] to next mod. buffer
5497 * :[N]sbmodified [N] to next mod. buffer
5498 */
5499 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005500ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501{
5502 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005503 if (eap->do_ecmd_cmd != NULL)
5504 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505}
5506
5507/*
5508 * :[N]bnext [N] to next buffer
5509 * :[N]sbnext [N] split and to next buffer
5510 */
5511 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005512ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513{
5514 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005515 if (eap->do_ecmd_cmd != NULL)
5516 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517}
5518
5519/*
5520 * :[N]bNext [N] to previous buffer
5521 * :[N]bprevious [N] to previous buffer
5522 * :[N]sbNext [N] split and to previous buffer
5523 * :[N]sbprevious [N] split and to previous buffer
5524 */
5525 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005526ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527{
5528 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005529 if (eap->do_ecmd_cmd != NULL)
5530 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531}
5532
5533/*
5534 * :brewind to first buffer
5535 * :bfirst to first buffer
5536 * :sbrewind split and to first buffer
5537 * :sbfirst split and to first buffer
5538 */
5539 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005540ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541{
5542 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005543 if (eap->do_ecmd_cmd != NULL)
5544 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545}
5546
5547/*
5548 * :blast to last buffer
5549 * :sblast split and to last buffer
5550 */
5551 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005552ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553{
5554 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005555 if (eap->do_ecmd_cmd != NULL)
5556 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557}
5558#endif
5559
5560 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005561ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562{
5563 return (c == NUL || c == '|' || c == '"' || c == '\n');
5564}
5565
5566#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5567 || defined(PROTO)
5568/*
5569 * Return the next command, after the first '|' or '\n'.
5570 * Return NULL if not found.
5571 */
5572 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005573find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574{
5575 while (*p != '|' && *p != '\n')
5576 {
5577 if (*p == NUL)
5578 return NULL;
5579 ++p;
5580 }
5581 return (p + 1);
5582}
5583#endif
5584
5585/*
5586 * Check if *p is a separator between Ex commands.
5587 * Return NULL if it isn't, (p + 1) if it is.
5588 */
5589 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005590check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591{
5592 p = skipwhite(p);
5593 if (*p == '|' || *p == '\n')
5594 return (p + 1);
5595 else
5596 return NULL;
5597}
5598
5599/*
5600 * - if there are more files to edit
5601 * - and this is the last window
5602 * - and forceit not used
5603 * - and not repeated twice on a row
5604 * return FAIL and give error message if 'message' TRUE
5605 * return OK otherwise
5606 */
5607 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005608check_more(
5609 int message, /* when FALSE check only, no messages */
5610 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611{
5612 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5613
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005614 if (!forceit && only_one_window()
5615 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 {
5617 if (message)
5618 {
5619#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5620 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5621 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005622 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623
5624 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005625 vim_strncpy(buff,
5626 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005627 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005629 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 _("%d more files to edit. Quit anyway?"), n);
5631 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5632 return OK;
5633 return FAIL;
5634 }
5635#endif
5636 if (n == 1)
5637 EMSG(_("E173: 1 more file to edit"));
5638 else
5639 EMSGN(_("E173: %ld more files to edit"), n);
5640 quitmore = 2; /* next try to quit is allowed */
5641 }
5642 return FAIL;
5643 }
5644 return OK;
5645}
5646
5647#ifdef FEAT_CMDL_COMPL
5648/*
5649 * Function given to ExpandGeneric() to obtain the list of command names.
5650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005652get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653{
5654 if (idx >= (int)CMD_SIZE)
5655# ifdef FEAT_USR_CMDS
5656 return get_user_command_name(idx);
5657# else
5658 return NULL;
5659# endif
5660 return cmdnames[idx].cmd_name;
5661}
5662#endif
5663
5664#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005665static 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);
5666static void uc_list(char_u *name, size_t name_len);
5667static 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);
5668static char_u *uc_split_args(char_u *arg, size_t *lenp);
5669static 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 +00005670
5671 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005672uc_add_command(
5673 char_u *name,
5674 size_t name_len,
5675 char_u *rep,
5676 long argt,
5677 long def,
5678 int flags,
5679 int compl,
5680 char_u *compl_arg,
5681 int addr_type,
5682 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683{
5684 ucmd_T *cmd = NULL;
5685 char_u *p;
5686 int i;
5687 int cmp = 1;
5688 char_u *rep_buf = NULL;
5689 garray_T *gap;
5690
Bram Moolenaar9c102382006-05-03 21:26:49 +00005691 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 if (rep_buf == NULL)
5693 {
5694 /* Can't replace termcodes - try using the string as is */
5695 rep_buf = vim_strsave(rep);
5696
5697 /* Give up if out of memory */
5698 if (rep_buf == NULL)
5699 return FAIL;
5700 }
5701
5702 /* get address of growarray: global or in curbuf */
5703 if (flags & UC_BUFFER)
5704 {
5705 gap = &curbuf->b_ucmds;
5706 if (gap->ga_itemsize == 0)
5707 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5708 }
5709 else
5710 gap = &ucmds;
5711
5712 /* Search for the command in the already defined commands. */
5713 for (i = 0; i < gap->ga_len; ++i)
5714 {
5715 size_t len;
5716
5717 cmd = USER_CMD_GA(gap, i);
5718 len = STRLEN(cmd->uc_name);
5719 cmp = STRNCMP(name, cmd->uc_name, name_len);
5720 if (cmp == 0)
5721 {
5722 if (name_len < len)
5723 cmp = -1;
5724 else if (name_len > len)
5725 cmp = 1;
5726 }
5727
5728 if (cmp == 0)
5729 {
5730 if (!force)
5731 {
5732 EMSG(_("E174: Command already exists: add ! to replace it"));
5733 goto fail;
5734 }
5735
5736 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005737 cmd->uc_rep = NULL;
5738#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5739 vim_free(cmd->uc_compl_arg);
5740 cmd->uc_compl_arg = NULL;
5741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 break;
5743 }
5744
5745 /* Stop as soon as we pass the name to add */
5746 if (cmp < 0)
5747 break;
5748 }
5749
5750 /* Extend the array unless we're replacing an existing command */
5751 if (cmp != 0)
5752 {
5753 if (ga_grow(gap, 1) != OK)
5754 goto fail;
5755 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5756 goto fail;
5757
5758 cmd = USER_CMD_GA(gap, i);
5759 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5760
5761 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762
5763 cmd->uc_name = p;
5764 }
5765
5766 cmd->uc_rep = rep_buf;
5767 cmd->uc_argt = argt;
5768 cmd->uc_def = def;
5769 cmd->uc_compl = compl;
5770#ifdef FEAT_EVAL
5771 cmd->uc_scriptID = current_SID;
5772# ifdef FEAT_CMDL_COMPL
5773 cmd->uc_compl_arg = compl_arg;
5774# endif
5775#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005776 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777
5778 return OK;
5779
5780fail:
5781 vim_free(rep_buf);
5782#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5783 vim_free(compl_arg);
5784#endif
5785 return FAIL;
5786}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005789#if defined(FEAT_USR_CMDS)
5790static struct
5791{
5792 int expand;
5793 char *name;
5794} addr_type_complete[] =
5795{
5796 {ADDR_ARGUMENTS, "arguments"},
5797 {ADDR_LINES, "lines"},
5798 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5799 {ADDR_TABS, "tabs"},
5800 {ADDR_BUFFERS, "buffers"},
5801 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005802 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005803 {-1, NULL}
5804};
5805#endif
5806
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005807#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808/*
5809 * List of names for completion for ":command" with the EXPAND_ flag.
5810 * Must be alphabetical for completion.
5811 */
5812static struct
5813{
5814 int expand;
5815 char *name;
5816} command_complete[] =
5817{
5818 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005819 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005821 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005823 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005824#if defined(FEAT_CSCOPE)
5825 {EXPAND_CSCOPE, "cscope"},
5826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5828 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005829 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830#endif
5831 {EXPAND_DIRECTORIES, "dir"},
5832 {EXPAND_ENV_VARS, "environment"},
5833 {EXPAND_EVENTS, "event"},
5834 {EXPAND_EXPRESSION, "expression"},
5835 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005836 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005837 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 {EXPAND_FUNCTIONS, "function"},
5839 {EXPAND_HELP, "help"},
5840 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005841#if defined(FEAT_CMDHIST)
5842 {EXPAND_HISTORY, "history"},
5843#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005844#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005845 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005846 {EXPAND_LOCALES, "locale"},
5847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 {EXPAND_MAPPINGS, "mapping"},
5849 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005850 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005851#if defined(FEAT_PROFILE)
5852 {EXPAND_SYNTIME, "syntime"},
5853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005855 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005856 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005857#if defined(FEAT_SIGNS)
5858 {EXPAND_SIGN, "sign"},
5859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 {EXPAND_TAGS, "tag"},
5861 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005862 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 {EXPAND_USER_VARS, "var"},
5864 {0, NULL}
5865};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005868#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005870uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871{
5872 int i, j;
5873 int found = FALSE;
5874 ucmd_T *cmd;
5875 int len;
5876 long a;
5877 garray_T *gap;
5878
5879 gap = &curbuf->b_ucmds;
5880 for (;;)
5881 {
5882 for (i = 0; i < gap->ga_len; ++i)
5883 {
5884 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005885 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886
5887 /* Skip commands which don't match the requested prefix */
5888 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5889 continue;
5890
5891 /* Put out the title first time */
5892 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005893 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 found = TRUE;
5895 msg_putchar('\n');
5896 if (got_int)
5897 break;
5898
5899 /* Special cases */
5900 msg_putchar(a & BANG ? '!' : ' ');
5901 msg_putchar(a & REGSTR ? '"' : ' ');
5902 msg_putchar(gap != &ucmds ? 'b' : ' ');
5903 msg_putchar(' ');
5904
5905 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5906 len = (int)STRLEN(cmd->uc_name) + 4;
5907
5908 do {
5909 msg_putchar(' ');
5910 ++len;
5911 } while (len < 16);
5912
5913 len = 0;
5914
5915 /* Arguments */
5916 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5917 {
5918 case 0: IObuff[len++] = '0'; break;
5919 case (EXTRA): IObuff[len++] = '*'; break;
5920 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5921 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5922 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5923 }
5924
5925 do {
5926 IObuff[len++] = ' ';
5927 } while (len < 5);
5928
5929 /* Range */
5930 if (a & (RANGE|COUNT))
5931 {
5932 if (a & COUNT)
5933 {
5934 /* -count=N */
5935 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5936 len += (int)STRLEN(IObuff + len);
5937 }
5938 else if (a & DFLALL)
5939 IObuff[len++] = '%';
5940 else if (cmd->uc_def >= 0)
5941 {
5942 /* -range=N */
5943 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5944 len += (int)STRLEN(IObuff + len);
5945 }
5946 else
5947 IObuff[len++] = '.';
5948 }
5949
5950 do {
5951 IObuff[len++] = ' ';
5952 } while (len < 11);
5953
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005954 /* Address Type */
5955 for (j = 0; addr_type_complete[j].expand != -1; ++j)
5956 if (addr_type_complete[j].expand != ADDR_LINES
5957 && addr_type_complete[j].expand == cmd->uc_addr_type)
5958 {
5959 STRCPY(IObuff + len, addr_type_complete[j].name);
5960 len += (int)STRLEN(IObuff + len);
5961 break;
5962 }
5963
5964 do {
5965 IObuff[len++] = ' ';
5966 } while (len < 21);
5967
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 /* Completion */
5969 for (j = 0; command_complete[j].expand != 0; ++j)
5970 if (command_complete[j].expand == cmd->uc_compl)
5971 {
5972 STRCPY(IObuff + len, command_complete[j].name);
5973 len += (int)STRLEN(IObuff + len);
5974 break;
5975 }
5976
5977 do {
5978 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005979 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980
5981 IObuff[len] = '\0';
5982 msg_outtrans(IObuff);
5983
5984 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005985#ifdef FEAT_EVAL
5986 if (p_verbose > 0)
5987 last_set_msg(cmd->uc_scriptID);
5988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 out_flush();
5990 ui_breakcheck();
5991 if (got_int)
5992 break;
5993 }
5994 if (gap == &ucmds || i < gap->ga_len)
5995 break;
5996 gap = &ucmds;
5997 }
5998
5999 if (!found)
6000 MSG(_("No user-defined commands found"));
6001}
6002
6003 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006004uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005{
6006 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6007 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6008 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6009 0xb9, 0x7f, 0};
6010 int i;
6011
6012 for (i = 0; fcmd[i]; ++i)
6013 IObuff[i] = fcmd[i] - 0x40;
6014 IObuff[i] = 0;
6015 return IObuff;
6016}
6017
6018 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006019uc_scan_attr(
6020 char_u *attr,
6021 size_t len,
6022 long *argt,
6023 long *def,
6024 int *flags,
6025 int *compl,
6026 char_u **compl_arg,
6027 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028{
6029 char_u *p;
6030
6031 if (len == 0)
6032 {
6033 EMSG(_("E175: No attribute specified"));
6034 return FAIL;
6035 }
6036
6037 /* First, try the simple attributes (no arguments) */
6038 if (STRNICMP(attr, "bang", len) == 0)
6039 *argt |= BANG;
6040 else if (STRNICMP(attr, "buffer", len) == 0)
6041 *flags |= UC_BUFFER;
6042 else if (STRNICMP(attr, "register", len) == 0)
6043 *argt |= REGSTR;
6044 else if (STRNICMP(attr, "bar", len) == 0)
6045 *argt |= TRLBAR;
6046 else
6047 {
6048 int i;
6049 char_u *val = NULL;
6050 size_t vallen = 0;
6051 size_t attrlen = len;
6052
6053 /* Look for the attribute name - which is the part before any '=' */
6054 for (i = 0; i < (int)len; ++i)
6055 {
6056 if (attr[i] == '=')
6057 {
6058 val = &attr[i + 1];
6059 vallen = len - i - 1;
6060 attrlen = i;
6061 break;
6062 }
6063 }
6064
6065 if (STRNICMP(attr, "nargs", attrlen) == 0)
6066 {
6067 if (vallen == 1)
6068 {
6069 if (*val == '0')
6070 /* Do nothing - this is the default */;
6071 else if (*val == '1')
6072 *argt |= (EXTRA | NOSPC | NEEDARG);
6073 else if (*val == '*')
6074 *argt |= EXTRA;
6075 else if (*val == '?')
6076 *argt |= (EXTRA | NOSPC);
6077 else if (*val == '+')
6078 *argt |= (EXTRA | NEEDARG);
6079 else
6080 goto wrong_nargs;
6081 }
6082 else
6083 {
6084wrong_nargs:
6085 EMSG(_("E176: Invalid number of arguments"));
6086 return FAIL;
6087 }
6088 }
6089 else if (STRNICMP(attr, "range", attrlen) == 0)
6090 {
6091 *argt |= RANGE;
6092 if (vallen == 1 && *val == '%')
6093 *argt |= DFLALL;
6094 else if (val != NULL)
6095 {
6096 p = val;
6097 if (*def >= 0)
6098 {
6099two_count:
6100 EMSG(_("E177: Count cannot be specified twice"));
6101 return FAIL;
6102 }
6103
6104 *def = getdigits(&p);
6105 *argt |= (ZEROR | NOTADR);
6106
6107 if (p != val + vallen || vallen == 0)
6108 {
6109invalid_count:
6110 EMSG(_("E178: Invalid default value for count"));
6111 return FAIL;
6112 }
6113 }
6114 }
6115 else if (STRNICMP(attr, "count", attrlen) == 0)
6116 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006117 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118
6119 if (val != NULL)
6120 {
6121 p = val;
6122 if (*def >= 0)
6123 goto two_count;
6124
6125 *def = getdigits(&p);
6126
6127 if (p != val + vallen)
6128 goto invalid_count;
6129 }
6130
6131 if (*def < 0)
6132 *def = 0;
6133 }
6134 else if (STRNICMP(attr, "complete", attrlen) == 0)
6135 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 if (val == NULL)
6137 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006138 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 return FAIL;
6140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006142 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6143 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006146 else if (STRNICMP(attr, "addr", attrlen) == 0)
6147 {
6148 *argt |= RANGE;
6149 if (val == NULL)
6150 {
6151 EMSG(_("E179: argument required for -addr"));
6152 return FAIL;
6153 }
6154 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6155 == FAIL)
6156 return FAIL;
6157 if (addr_type_arg != ADDR_LINES)
6158 *argt |= (ZEROR | NOTADR) ;
6159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 else
6161 {
6162 char_u ch = attr[len];
6163 attr[len] = '\0';
6164 EMSG2(_("E181: Invalid attribute: %s"), attr);
6165 attr[len] = ch;
6166 return FAIL;
6167 }
6168 }
6169
6170 return OK;
6171}
6172
Bram Moolenaara850a712009-01-28 14:42:59 +00006173/*
6174 * ":command ..."
6175 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006177ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178{
6179 char_u *name;
6180 char_u *end;
6181 char_u *p;
6182 long argt = 0;
6183 long def = -1;
6184 int flags = 0;
6185 int compl = EXPAND_NOTHING;
6186 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006187 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006189 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190
6191 p = eap->arg;
6192
6193 /* Check for attributes */
6194 while (*p == '-')
6195 {
6196 ++p;
6197 end = skiptowhite(p);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006198 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 == FAIL)
6200 return;
6201 p = skipwhite(end);
6202 }
6203
6204 /* Get the name (if any) and skip to the following argument */
6205 name = p;
6206 if (ASCII_ISALPHA(*p))
6207 while (ASCII_ISALNUM(*p))
6208 ++p;
6209 if (!ends_excmd(*p) && !vim_iswhite(*p))
6210 {
6211 EMSG(_("E182: Invalid command name"));
6212 return;
6213 }
6214 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006215 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216
6217 /* If there is nothing after the name, and no attributes were specified,
6218 * we are listing commands
6219 */
6220 p = skipwhite(end);
6221 if (!has_attr && ends_excmd(*p))
6222 {
6223 uc_list(name, end - name);
6224 }
6225 else if (!ASCII_ISUPPER(*name))
6226 {
6227 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6228 return;
6229 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006230 else if ((name_len == 1 && *name == 'X')
6231 || (name_len <= 4
6232 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6233 {
6234 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6235 return;
6236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 else
6238 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006239 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240}
6241
6242/*
6243 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 * Clear all user commands, global and for current buffer.
6245 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006246 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006247ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248{
6249 uc_clear(&ucmds);
6250 uc_clear(&curbuf->b_ucmds);
6251}
6252
6253/*
6254 * Clear all user commands for "gap".
6255 */
6256 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006257uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258{
6259 int i;
6260 ucmd_T *cmd;
6261
6262 for (i = 0; i < gap->ga_len; ++i)
6263 {
6264 cmd = USER_CMD_GA(gap, i);
6265 vim_free(cmd->uc_name);
6266 vim_free(cmd->uc_rep);
6267# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6268 vim_free(cmd->uc_compl_arg);
6269# endif
6270 }
6271 ga_clear(gap);
6272}
6273
6274 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006275ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276{
6277 int i = 0;
6278 ucmd_T *cmd = NULL;
6279 int cmp = -1;
6280 garray_T *gap;
6281
6282 gap = &curbuf->b_ucmds;
6283 for (;;)
6284 {
6285 for (i = 0; i < gap->ga_len; ++i)
6286 {
6287 cmd = USER_CMD_GA(gap, i);
6288 cmp = STRCMP(eap->arg, cmd->uc_name);
6289 if (cmp <= 0)
6290 break;
6291 }
6292 if (gap == &ucmds || cmp == 0)
6293 break;
6294 gap = &ucmds;
6295 }
6296
6297 if (cmp != 0)
6298 {
6299 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6300 return;
6301 }
6302
6303 vim_free(cmd->uc_name);
6304 vim_free(cmd->uc_rep);
6305# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6306 vim_free(cmd->uc_compl_arg);
6307# endif
6308
6309 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310
6311 if (i < gap->ga_len)
6312 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6313}
6314
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006315/*
6316 * split and quote args for <f-args>
6317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006319uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320{
6321 char_u *buf;
6322 char_u *p;
6323 char_u *q;
6324 int len;
6325
6326 /* Precalculate length */
6327 p = arg;
6328 len = 2; /* Initial and final quotes */
6329
6330 while (*p)
6331 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006332 if (p[0] == '\\' && p[1] == '\\')
6333 {
6334 len += 2;
6335 p += 2;
6336 }
6337 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 {
6339 len += 1;
6340 p += 2;
6341 }
6342 else if (*p == '\\' || *p == '"')
6343 {
6344 len += 2;
6345 p += 1;
6346 }
6347 else if (vim_iswhite(*p))
6348 {
6349 p = skipwhite(p);
6350 if (*p == NUL)
6351 break;
6352 len += 3; /* "," */
6353 }
6354 else
6355 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006356#ifdef FEAT_MBYTE
6357 int charlen = (*mb_ptr2len)(p);
6358 len += charlen;
6359 p += charlen;
6360#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 ++len;
6362 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 }
6365 }
6366
6367 buf = alloc(len + 1);
6368 if (buf == NULL)
6369 {
6370 *lenp = 0;
6371 return buf;
6372 }
6373
6374 p = arg;
6375 q = buf;
6376 *q++ = '"';
6377 while (*p)
6378 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006379 if (p[0] == '\\' && p[1] == '\\')
6380 {
6381 *q++ = '\\';
6382 *q++ = '\\';
6383 p += 2;
6384 }
6385 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 {
6387 *q++ = p[1];
6388 p += 2;
6389 }
6390 else if (*p == '\\' || *p == '"')
6391 {
6392 *q++ = '\\';
6393 *q++ = *p++;
6394 }
6395 else if (vim_iswhite(*p))
6396 {
6397 p = skipwhite(p);
6398 if (*p == NUL)
6399 break;
6400 *q++ = '"';
6401 *q++ = ',';
6402 *q++ = '"';
6403 }
6404 else
6405 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006406 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 }
6408 }
6409 *q++ = '"';
6410 *q = 0;
6411
6412 *lenp = len;
6413 return buf;
6414}
6415
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006416 static size_t
6417add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6418{
6419 size_t result;
6420
6421 result = STRLEN(mod_str);
6422 if (*multi_mods)
6423 result += 1;
6424 if (buf != NULL)
6425 {
6426 if (*multi_mods)
6427 STRCAT(buf, " ");
6428 STRCAT(buf, mod_str);
6429 }
6430
6431 *multi_mods = 1;
6432
6433 return result;
6434}
6435
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436/*
6437 * Check for a <> code in a user command.
6438 * "code" points to the '<'. "len" the length of the <> (inclusive).
6439 * "buf" is where the result is to be added.
6440 * "split_buf" points to a buffer used for splitting, caller should free it.
6441 * "split_len" is the length of what "split_buf" contains.
6442 * Returns the length of the replacement, which has been added to "buf".
6443 * Returns -1 if there was no match, and only the "<" has been copied.
6444 */
6445 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006446uc_check_code(
6447 char_u *code,
6448 size_t len,
6449 char_u *buf,
6450 ucmd_T *cmd, /* the user command we're expanding */
6451 exarg_T *eap, /* ex arguments */
6452 char_u **split_buf,
6453 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454{
6455 size_t result = 0;
6456 char_u *p = code + 1;
6457 size_t l = len - 2;
6458 int quote = 0;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006459 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_MODS,
6460 ct_REGISTER, ct_LT, ct_NONE } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461
6462 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6463 {
6464 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6465 p += 2;
6466 l -= 2;
6467 }
6468
Bram Moolenaar371d5402006-03-20 21:47:49 +00006469 ++l;
6470 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006472 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006474 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006476 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006478 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006480 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006482 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006484 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006486 else if (STRNICMP(p, "mods>", l) == 0)
6487 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488
6489 switch (type)
6490 {
6491 case ct_ARGS:
6492 /* Simple case first */
6493 if (*eap->arg == NUL)
6494 {
6495 if (quote == 1)
6496 {
6497 result = 2;
6498 if (buf != NULL)
6499 STRCPY(buf, "''");
6500 }
6501 else
6502 result = 0;
6503 break;
6504 }
6505
6506 /* When specified there is a single argument don't split it.
6507 * Works for ":Cmd %" when % is "a b c". */
6508 if ((eap->argt & NOSPC) && quote == 2)
6509 quote = 1;
6510
6511 switch (quote)
6512 {
6513 case 0: /* No quoting, no splitting */
6514 result = STRLEN(eap->arg);
6515 if (buf != NULL)
6516 STRCPY(buf, eap->arg);
6517 break;
6518 case 1: /* Quote, but don't split */
6519 result = STRLEN(eap->arg) + 2;
6520 for (p = eap->arg; *p; ++p)
6521 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006522#ifdef FEAT_MBYTE
6523 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6524 /* DBCS can contain \ in a trail byte, skip the
6525 * double-byte character. */
6526 ++p;
6527 else
6528#endif
6529 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 ++result;
6531 }
6532
6533 if (buf != NULL)
6534 {
6535 *buf++ = '"';
6536 for (p = eap->arg; *p; ++p)
6537 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006538#ifdef FEAT_MBYTE
6539 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6540 /* DBCS can contain \ in a trail byte, copy the
6541 * double-byte character to avoid escaping. */
6542 *buf++ = *p++;
6543 else
6544#endif
6545 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 *buf++ = '\\';
6547 *buf++ = *p;
6548 }
6549 *buf = '"';
6550 }
6551
6552 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006553 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 /* This is hard, so only do it once, and cache the result */
6555 if (*split_buf == NULL)
6556 *split_buf = uc_split_args(eap->arg, split_len);
6557
6558 result = *split_len;
6559 if (buf != NULL && result != 0)
6560 STRCPY(buf, *split_buf);
6561
6562 break;
6563 }
6564 break;
6565
6566 case ct_BANG:
6567 result = eap->forceit ? 1 : 0;
6568 if (quote)
6569 result += 2;
6570 if (buf != NULL)
6571 {
6572 if (quote)
6573 *buf++ = '"';
6574 if (eap->forceit)
6575 *buf++ = '!';
6576 if (quote)
6577 *buf = '"';
6578 }
6579 break;
6580
6581 case ct_LINE1:
6582 case ct_LINE2:
6583 case ct_COUNT:
6584 {
6585 char num_buf[20];
6586 long num = (type == ct_LINE1) ? eap->line1 :
6587 (type == ct_LINE2) ? eap->line2 :
6588 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6589 size_t num_len;
6590
6591 sprintf(num_buf, "%ld", num);
6592 num_len = STRLEN(num_buf);
6593 result = num_len;
6594
6595 if (quote)
6596 result += 2;
6597
6598 if (buf != NULL)
6599 {
6600 if (quote)
6601 *buf++ = '"';
6602 STRCPY(buf, num_buf);
6603 buf += num_len;
6604 if (quote)
6605 *buf = '"';
6606 }
6607
6608 break;
6609 }
6610
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006611 case ct_MODS:
6612 {
6613 int multi_mods = 0;
6614 typedef struct {
6615 int *varp;
6616 char *name;
6617 } mod_entry_T;
6618 static mod_entry_T mod_entries[] = {
6619#ifdef FEAT_BROWSE_CMD
6620 {&cmdmod.browse, "browse"},
6621#endif
6622#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6623 {&cmdmod.confirm, "confirm"},
6624#endif
6625 {&cmdmod.hide, "hide"},
6626 {&cmdmod.keepalt, "keepalt"},
6627 {&cmdmod.keepjumps, "keepjumps"},
6628 {&cmdmod.keepmarks, "keepmarks"},
6629 {&cmdmod.keeppatterns, "keeppatterns"},
6630 {&cmdmod.lockmarks, "lockmarks"},
6631 {&cmdmod.noswapfile, "noswapfile"},
6632 {NULL, NULL}
6633 };
6634 int i;
6635
6636 result = quote ? 2 : 0;
6637 if (buf != NULL)
6638 {
6639 if (quote)
6640 *buf++ = '"';
6641 *buf = '\0';
6642 }
6643
6644#ifdef FEAT_WINDOWS
6645 /* :aboveleft and :leftabove */
6646 if (cmdmod.split & WSP_ABOVE)
6647 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6648 /* :belowright and :rightbelow */
6649 if (cmdmod.split & WSP_BELOW)
6650 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6651 /* :botright */
6652 if (cmdmod.split & WSP_BOT)
6653 result += add_cmd_modifier(buf, "botright", &multi_mods);
6654#endif
6655
6656 /* the modifiers that are simple flags */
6657 for (i = 0; mod_entries[i].varp != NULL; ++i)
6658 if (*mod_entries[i].varp)
6659 result += add_cmd_modifier(buf, mod_entries[i].name,
6660 &multi_mods);
6661
6662 /* TODO: How to support :noautocmd? */
6663#ifdef HAVE_SANDBOX
6664 /* TODO: How to support :sandbox?*/
6665#endif
6666 /* :silent */
6667 if (msg_silent > 0)
6668 result += add_cmd_modifier(buf,
6669 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
6670#ifdef FEAT_WINDOWS
6671 /* :tab */
6672 if (cmdmod.tab > 0)
6673 result += add_cmd_modifier(buf, "tab", &multi_mods);
6674 /* :topleft */
6675 if (cmdmod.split & WSP_TOP)
6676 result += add_cmd_modifier(buf, "topleft", &multi_mods);
6677#endif
6678 /* TODO: How to support :unsilent?*/
6679 /* :verbose */
6680 if (p_verbose > 0)
6681 result += add_cmd_modifier(buf, "verbose", &multi_mods);
6682#ifdef FEAT_WINDOWS
6683 /* :vertical */
6684 if (cmdmod.split & WSP_VERT)
6685 result += add_cmd_modifier(buf, "vertical", &multi_mods);
6686#endif
6687 if (quote && buf != NULL)
6688 {
6689 buf += result - 2;
6690 *buf = '"';
6691 }
6692 break;
6693 }
6694
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 case ct_REGISTER:
6696 result = eap->regname ? 1 : 0;
6697 if (quote)
6698 result += 2;
6699 if (buf != NULL)
6700 {
6701 if (quote)
6702 *buf++ = '\'';
6703 if (eap->regname)
6704 *buf++ = eap->regname;
6705 if (quote)
6706 *buf = '\'';
6707 }
6708 break;
6709
6710 case ct_LT:
6711 result = 1;
6712 if (buf != NULL)
6713 *buf = '<';
6714 break;
6715
6716 default:
6717 /* Not recognized: just copy the '<' and return -1. */
6718 result = (size_t)-1;
6719 if (buf != NULL)
6720 *buf = '<';
6721 break;
6722 }
6723
6724 return result;
6725}
6726
6727 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006728do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729{
6730 char_u *buf;
6731 char_u *p;
6732 char_u *q;
6733
6734 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006735 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006736 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 size_t len, totlen;
6738
6739 size_t split_len = 0;
6740 char_u *split_buf = NULL;
6741 ucmd_T *cmd;
6742#ifdef FEAT_EVAL
6743 scid_T save_current_SID = current_SID;
6744#endif
6745
6746 if (eap->cmdidx == CMD_USER)
6747 cmd = USER_CMD(eap->useridx);
6748 else
6749 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6750
6751 /*
6752 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006753 * First round: "buf" is NULL, compute length, allocate "buf".
6754 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 */
6756 buf = NULL;
6757 for (;;)
6758 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006759 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006760 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006762
6763 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006765 start = vim_strchr(p, '<');
6766 if (start != NULL)
6767 end = vim_strchr(start + 1, '>');
6768 if (buf != NULL)
6769 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006770 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6771 ;
6772 if (*ksp == K_SPECIAL
6773 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006774 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6775# ifdef FEAT_GUI
6776 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6777# endif
6778 ))
6779 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006780 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006781 * KS_SPECIAL KE_FILLER, like for mappings, but
6782 * do_cmdline() doesn't handle that, so convert it back.
6783 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6784 len = ksp - p;
6785 if (len > 0)
6786 {
6787 mch_memmove(q, p, len);
6788 q += len;
6789 }
6790 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6791 p = ksp + 3;
6792 continue;
6793 }
6794 }
6795
6796 /* break if there no <item> is found */
6797 if (start == NULL || end == NULL)
6798 break;
6799
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 /* Include the '>' */
6801 ++end;
6802
6803 /* Take everything up to the '<' */
6804 len = start - p;
6805 if (buf == NULL)
6806 totlen += len;
6807 else
6808 {
6809 mch_memmove(q, p, len);
6810 q += len;
6811 }
6812
6813 len = uc_check_code(start, end - start, q, cmd, eap,
6814 &split_buf, &split_len);
6815 if (len == (size_t)-1)
6816 {
6817 /* no match, continue after '<' */
6818 p = start + 1;
6819 len = 1;
6820 }
6821 else
6822 p = end;
6823 if (buf == NULL)
6824 totlen += len;
6825 else
6826 q += len;
6827 }
6828 if (buf != NULL) /* second time here, finished */
6829 {
6830 STRCPY(q, p);
6831 break;
6832 }
6833
6834 totlen += STRLEN(p); /* Add on the trailing characters */
6835 buf = alloc((unsigned)(totlen + 1));
6836 if (buf == NULL)
6837 {
6838 vim_free(split_buf);
6839 return;
6840 }
6841 }
6842
6843#ifdef FEAT_EVAL
6844 current_SID = cmd->uc_scriptID;
6845#endif
6846 (void)do_cmdline(buf, eap->getline, eap->cookie,
6847 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6848#ifdef FEAT_EVAL
6849 current_SID = save_current_SID;
6850#endif
6851 vim_free(buf);
6852 vim_free(split_buf);
6853}
6854
6855# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6856 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006857get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858{
6859 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6860}
6861
6862/*
6863 * Function given to ExpandGeneric() to obtain the list of user command names.
6864 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006866get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867{
6868 if (idx < curbuf->b_ucmds.ga_len)
6869 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6870 idx -= curbuf->b_ucmds.ga_len;
6871 if (idx < ucmds.ga_len)
6872 return USER_CMD(idx)->uc_name;
6873 return NULL;
6874}
6875
6876/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006877 * Function given to ExpandGeneric() to obtain the list of user address type names.
6878 */
6879 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006880get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006881{
6882 return (char_u *)addr_type_complete[idx].name;
6883}
6884
6885/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 * Function given to ExpandGeneric() to obtain the list of user command
6887 * attributes.
6888 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006890get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891{
6892 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006893 {"addr", "bang", "bar", "buffer", "complete",
6894 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006896 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 return NULL;
6898 return (char_u *)user_cmd_flags[idx];
6899}
6900
6901/*
6902 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6903 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006905get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906{
6907 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6908
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006909 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 return NULL;
6911 return (char_u *)user_cmd_nargs[idx];
6912}
6913
6914/*
6915 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6916 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006918get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919{
6920 return (char_u *)command_complete[idx].name;
6921}
6922# endif /* FEAT_CMDL_COMPL */
6923
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006924/*
6925 * Parse address type argument
6926 */
6927 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006928parse_addr_type_arg(
6929 char_u *value,
6930 int vallen,
6931 long *argt,
6932 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006933{
6934 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01006935
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006936 for (i = 0; addr_type_complete[i].expand != -1; ++i)
6937 {
6938 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
6939 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
6940 if (a && b)
6941 {
6942 *addr_type_arg = addr_type_complete[i].expand;
6943 break;
6944 }
6945 }
6946
6947 if (addr_type_complete[i].expand == -1)
6948 {
6949 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01006950
6951 for (i = 0; err[i] != NUL && !vim_iswhite(err[i]); i++)
6952 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006953 err[i] = NUL;
6954 EMSG2(_("E180: Invalid address type value: %s"), err);
6955 return FAIL;
6956 }
6957
6958 if (*addr_type_arg != ADDR_LINES)
6959 *argt |= NOTADR;
6960
6961 return OK;
6962}
6963
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964#endif /* FEAT_USR_CMDS */
6965
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006966#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6967/*
6968 * Parse a completion argument "value[vallen]".
6969 * The detected completion goes in "*complp", argument type in "*argt".
6970 * When there is an argument, for function and user defined completion, it's
6971 * copied to allocated memory and stored in "*compl_arg".
6972 * Returns FAIL if something is wrong.
6973 */
6974 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006975parse_compl_arg(
6976 char_u *value,
6977 int vallen,
6978 int *complp,
6979 long *argt,
6980 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006981{
6982 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006983# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006984 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006985# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006986 int i;
6987 int valend = vallen;
6988
6989 /* Look for any argument part - which is the part after any ',' */
6990 for (i = 0; i < vallen; ++i)
6991 {
6992 if (value[i] == ',')
6993 {
6994 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006995# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006996 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006997# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006998 valend = i;
6999 break;
7000 }
7001 }
7002
7003 for (i = 0; command_complete[i].expand != 0; ++i)
7004 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007005 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007006 && STRNCMP(value, command_complete[i].name, valend) == 0)
7007 {
7008 *complp = command_complete[i].expand;
7009 if (command_complete[i].expand == EXPAND_BUFFERS)
7010 *argt |= BUFNAME;
7011 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7012 || command_complete[i].expand == EXPAND_FILES)
7013 *argt |= XFILE;
7014 break;
7015 }
7016 }
7017
7018 if (command_complete[i].expand == 0)
7019 {
7020 EMSG2(_("E180: Invalid complete value: %s"), value);
7021 return FAIL;
7022 }
7023
7024# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7025 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7026 && arg != NULL)
7027# else
7028 if (arg != NULL)
7029# endif
7030 {
7031 EMSG(_("E468: Completion argument only allowed for custom completion"));
7032 return FAIL;
7033 }
7034
7035# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7036 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7037 && arg == NULL)
7038 {
7039 EMSG(_("E467: Custom completion requires a function argument"));
7040 return FAIL;
7041 }
7042
7043 if (arg != NULL)
7044 *compl_arg = vim_strnsave(arg, (int)arglen);
7045# endif
7046 return OK;
7047}
7048#endif
7049
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007051ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052{
Bram Moolenaare6850792010-05-14 15:28:44 +02007053 if (*eap->arg == NUL)
7054 {
7055#ifdef FEAT_EVAL
7056 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7057 char_u *p = NULL;
7058
7059 if (expr != NULL)
7060 {
7061 ++emsg_off;
7062 p = eval_to_string(expr, NULL, FALSE);
7063 --emsg_off;
7064 vim_free(expr);
7065 }
7066 if (p != NULL)
7067 {
7068 MSG(p);
7069 vim_free(p);
7070 }
7071 else
7072 MSG("default");
7073#else
7074 MSG(_("unknown"));
7075#endif
7076 }
7077 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007078 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079}
7080
7081 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007082ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083{
7084 if (*eap->arg == NUL && eap->cmd[2] == '!')
7085 MSG(_("Greetings, Vim user!"));
7086 do_highlight(eap->arg, eap->forceit, FALSE);
7087}
7088
7089
7090/*
7091 * Call this function if we thought we were going to exit, but we won't
7092 * (because of an error). May need to restore the terminal mode.
7093 */
7094 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007095not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096{
7097 exiting = FALSE;
7098 settmode(TMODE_RAW);
7099}
7100
7101/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007102 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 */
7104 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007105ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106{
Bram Moolenaarf240e182014-11-27 18:33:02 +01007107#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007108 win_T *wp;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007109#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007110
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111#ifdef FEAT_CMDWIN
7112 if (cmdwin_type != 0)
7113 {
7114 cmdwin_result = Ctrl_C;
7115 return;
7116 }
7117#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007118 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007119 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007120 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007121 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007122 return;
7123 }
Bram Moolenaarf240e182014-11-27 18:33:02 +01007124#ifdef FEAT_WINDOWS
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007125 if (eap->addr_count > 0)
7126 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007127 int wnr = eap->line2;
7128
7129 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7130 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007131 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007132 }
7133 else
Bram Moolenaarf240e182014-11-27 18:33:02 +01007134#endif
7135#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007136 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007137#endif
7138
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007139#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02007140 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007141 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02007142 * being closed (can only happen in autocommands). */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007143 if (curbuf_locked() || (wp->w_buffer->b_nwindows == 1
7144 && wp->w_buffer->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007145 return;
7146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147
7148#ifdef FEAT_NETBEANS_INTG
7149 netbeansForcedQuit = eap->forceit;
7150#endif
7151
7152 /*
7153 * If there are more files or windows we won't exit.
7154 */
7155 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7156 exiting = TRUE;
7157 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007158 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7159 | (eap->forceit ? CCGD_FORCEIT : 0)
7160 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007162 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 {
7164 not_exiting();
7165 }
7166 else
7167 {
7168#ifdef FEAT_WINDOWS
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007169 /* quit last window
7170 * Note: only_one_window() returns true, even so a help window is
7171 * still open. In that case only quit, if no address has been
7172 * specified. Example:
7173 * :h|wincmd w|1q - don't quit
7174 * :h|wincmd w|q - quit
7175 */
7176 if (only_one_window() && (firstwin == lastwin || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177#endif
7178 getout(0);
7179#ifdef FEAT_WINDOWS
7180# ifdef FEAT_GUI
7181 need_mouse_correct = TRUE;
7182# endif
7183 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007184 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185#endif
7186 }
7187}
7188
7189/*
7190 * ":cquit".
7191 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007193ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194{
7195 getout(1); /* this does not always pass on the exit code to the Manx
7196 compiler. why? */
7197}
7198
7199/*
7200 * ":qall": try to quit all windows
7201 */
7202 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007203ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204{
7205# ifdef FEAT_CMDWIN
7206 if (cmdwin_type != 0)
7207 {
7208 if (eap->forceit)
7209 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7210 else
7211 cmdwin_result = K_XF2;
7212 return;
7213 }
7214# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007215
7216 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007217 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007218 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007219 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007220 return;
7221 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007222#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007223 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7224 /* Refuse to quit when locked or when the buffer in the last window is
7225 * being closed (can only happen in autocommands). */
7226 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007227 return;
7228#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007229
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007231 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 getout(0);
7233 not_exiting();
7234}
7235
Bram Moolenaard9967712006-03-11 21:18:15 +00007236#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237/*
7238 * ":close": close current window, unless it is the last one
7239 */
7240 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007241ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007243 win_T *win;
7244 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245# ifdef FEAT_CMDWIN
7246 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007247 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 else
7249# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007250 if (!text_locked()
7251#ifdef FEAT_AUTOCMD
7252 && !curbuf_locked()
7253#endif
7254 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007255 {
7256 if (eap->addr_count == 0)
7257 ex_win_close(eap->forceit, curwin, NULL);
7258 else {
7259 for (win = firstwin; win != NULL; win = win->w_next)
7260 {
7261 winnr++;
7262 if (winnr == eap->line2)
7263 break;
7264 }
7265 if (win == NULL)
7266 win = lastwin;
7267 ex_win_close(eap->forceit, win, NULL);
7268 }
7269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270}
7271
Bram Moolenaard9967712006-03-11 21:18:15 +00007272# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007273/*
7274 * ":pclose": Close any preview window.
7275 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007277ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007278{
7279 win_T *win;
7280
7281 for (win = firstwin; win != NULL; win = win->w_next)
7282 if (win->w_p_pvw)
7283 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007284 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007285 break;
7286 }
7287}
Bram Moolenaard9967712006-03-11 21:18:15 +00007288# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007289
Bram Moolenaarf740b292006-02-16 22:11:02 +00007290/*
7291 * Close window "win" and take care of handling closing the last window for a
7292 * modified buffer.
7293 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007294 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007295ex_win_close(
7296 int forceit,
7297 win_T *win,
7298 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299{
7300 int need_hide;
7301 buf_T *buf = win->w_buffer;
7302
7303 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007304 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007306# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 if ((p_confirm || cmdmod.confirm) && p_write)
7308 {
7309 dialog_changed(buf, FALSE);
7310 if (buf_valid(buf) && bufIsChanged(buf))
7311 return;
7312 need_hide = FALSE;
7313 }
7314 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007315# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 {
7317 EMSG(_(e_nowrtmsg));
7318 return;
7319 }
7320 }
7321
Bram Moolenaard9967712006-03-11 21:18:15 +00007322# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007324# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007325
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007327 if (tp == NULL)
7328 win_close(win, !need_hide && !P_HID(buf));
7329 else
7330 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331}
7332
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007334 * ":tabclose": close current tab page, unless it is the last one.
7335 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 */
7337 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007338ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007340 tabpage_T *tp;
7341
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007342# ifdef FEAT_CMDWIN
7343 if (cmdwin_type != 0)
7344 cmdwin_result = K_IGNORE;
7345 else
7346# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007347 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007348 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007349 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007351 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007352 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007353 tp = find_tabpage((int)eap->line2);
7354 if (tp == NULL)
7355 {
7356 beep_flush();
7357 return;
7358 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007359 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007360 {
7361 tabpage_close_other(tp, eap->forceit);
7362 return;
7363 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007364 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007365 if (!text_locked()
7366#ifdef FEAT_AUTOCMD
7367 && !curbuf_locked()
7368#endif
7369 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00007370 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007372}
7373
7374/*
7375 * ":tabonly": close all tab pages except the current one
7376 */
7377 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007378ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007379{
7380 tabpage_T *tp;
7381 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007382
7383# ifdef FEAT_CMDWIN
7384 if (cmdwin_type != 0)
7385 cmdwin_result = K_IGNORE;
7386 else
7387# endif
7388 if (first_tabpage->tp_next == NULL)
7389 MSG(_("Already only one tab page"));
7390 else
7391 {
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007392 if (eap->addr_count > 0)
7393 goto_tabpage(eap->line2);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007394 /* Repeat this up to a 1000 times, because autocommands may mess
7395 * up the lists. */
7396 for (done = 0; done < 1000; ++done)
7397 {
7398 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7399 if (tp->tp_topframe != topframe)
7400 {
7401 tabpage_close_other(tp, eap->forceit);
7402 /* if we failed to close it quit */
7403 if (valid_tabpage(tp))
7404 done = 1000;
7405 /* start over, "tp" is now invalid */
7406 break;
7407 }
7408 if (first_tabpage->tp_next == NULL)
7409 break;
7410 }
7411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413
7414/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007415 * Close the current tab page.
7416 */
7417 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007418tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007419{
7420 /* First close all the windows but the current one. If that worked then
7421 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007422 if (lastwin != firstwin)
7423 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007424 if (lastwin == firstwin)
7425 ex_win_close(forceit, curwin, NULL);
7426# ifdef FEAT_GUI
7427 need_mouse_correct = TRUE;
7428# endif
7429}
7430
7431/*
7432 * Close tab page "tp", which is not the current tab page.
7433 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007434 * Also takes care of the tab pages line disappearing when closing the
7435 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007436 */
7437 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007438tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007439{
7440 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007441 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007442 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007443
7444 /* Limit to 1000 windows, autocommands may add a window while we close
7445 * one. OK, so I'm paranoid... */
7446 while (++done < 1000)
7447 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007448 wp = tp->tp_firstwin;
7449 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007450
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007451 /* Autocommands may delete the tab page under our fingers and we may
7452 * fail to close a window with a modified buffer. */
7453 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007454 break;
7455 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007456
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007457 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007458 if (h != tabline_height())
7459 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007460}
7461
7462/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 * ":only".
7464 */
7465 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007466ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007468 win_T *wp;
7469 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470# ifdef FEAT_GUI
7471 need_mouse_correct = TRUE;
7472# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007473 if (eap->addr_count > 0)
7474 {
7475 wnr = eap->line2;
7476 for (wp = firstwin; --wnr > 0; )
7477 {
7478 if (wp->w_next == NULL)
7479 break;
7480 else
7481 wp = wp->w_next;
7482 }
7483 win_goto(wp);
7484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 close_others(TRUE, eap->forceit);
7486}
7487
7488/*
7489 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007490 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007492 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007493ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494{
7495 if (eap->addr_count == 0)
7496 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007497 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498}
7499#endif /* FEAT_WINDOWS */
7500
7501 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007502ex_hide(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503{
7504 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
7505 eap->errmsg = e_invarg;
7506 else
7507 {
7508 /* ":hide" or ":hide | cmd": hide current window */
7509 eap->nextcmd = check_nextcmd(eap->arg);
7510#ifdef FEAT_WINDOWS
7511 if (!eap->skip)
7512 {
7513# ifdef FEAT_GUI
7514 need_mouse_correct = TRUE;
7515# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007516 if (eap->addr_count == 0)
7517 win_close(curwin, FALSE); /* don't free buffer */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007518 else
7519 {
7520 int winnr = 0;
7521 win_T *win;
7522
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007523 for (win = firstwin; win != NULL; win = win->w_next)
7524 {
7525 winnr++;
7526 if (winnr == eap->line2)
7527 break;
7528 }
7529 if (win == NULL)
7530 win = lastwin;
7531 win_close(win, FALSE);
7532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 }
7534#endif
7535 }
7536}
7537
7538/*
7539 * ":stop" and ":suspend": Suspend Vim.
7540 */
7541 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007542ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543{
7544 /*
7545 * Disallow suspending for "rvim".
7546 */
7547 if (!check_restricted()
7548#ifdef WIN3264
7549 /*
7550 * Check if external commands are allowed now.
7551 */
7552 && can_end_termcap_mode(TRUE)
7553#endif
7554 )
7555 {
7556 if (!eap->forceit)
7557 autowrite_all();
7558 windgoto((int)Rows - 1, 0);
7559 out_char('\n');
7560 out_flush();
7561 stoptermcap();
7562 out_flush(); /* needed for SUN to restore xterm buffer */
7563#ifdef FEAT_TITLE
7564 mch_restore_title(3); /* restore window titles */
7565#endif
7566 ui_suspend(); /* call machine specific function */
7567#ifdef FEAT_TITLE
7568 maketitle();
7569 resettitle(); /* force updating the title */
7570#endif
7571 starttermcap();
7572 scroll_start(); /* scroll screen before redrawing */
7573 redraw_later_clear();
7574 shell_resized(); /* may have resized window */
7575 }
7576}
7577
7578/*
7579 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7580 */
7581 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007582ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583{
7584#ifdef FEAT_CMDWIN
7585 if (cmdwin_type != 0)
7586 {
7587 cmdwin_result = Ctrl_C;
7588 return;
7589 }
7590#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007591 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007592 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007593 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007594 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007595 return;
7596 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007597#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007598 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7599 /* Refuse to quit when locked or when the buffer in the last window is
7600 * being closed (can only happen in autocommands). */
7601 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007602 return;
7603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604
7605 /*
7606 * if more files or windows we won't exit
7607 */
7608 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7609 exiting = TRUE;
7610 if ( ((eap->cmdidx == CMD_wq
7611 || curbufIsChanged())
7612 && do_write(eap) == FAIL)
7613 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007614 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 {
7616 not_exiting();
7617 }
7618 else
7619 {
7620#ifdef FEAT_WINDOWS
7621 if (only_one_window()) /* quit last window, exit Vim */
7622#endif
7623 getout(0);
7624#ifdef FEAT_WINDOWS
7625# ifdef FEAT_GUI
7626 need_mouse_correct = TRUE;
7627# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007628 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 win_close(curwin, !P_HID(curwin->w_buffer));
7630#endif
7631 }
7632}
7633
7634/*
7635 * ":print", ":list", ":number".
7636 */
7637 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007638ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007640 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7641 EMSG(_(e_emptybuf));
7642 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007644 for ( ;!got_int; ui_breakcheck())
7645 {
7646 print_line(eap->line1,
7647 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7648 || (eap->flags & EXFLAG_NR)),
7649 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7650 if (++eap->line1 > eap->line2)
7651 break;
7652 out_flush(); /* show one line at a time */
7653 }
7654 setpcmark();
7655 /* put cursor at last line */
7656 curwin->w_cursor.lnum = eap->line2;
7657 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 }
7659
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661}
7662
7663#ifdef FEAT_BYTEOFF
7664 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007665ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666{
7667 goto_byte(eap->line2);
7668}
7669#endif
7670
7671/*
7672 * ":shell".
7673 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007675ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676{
7677 do_shell(NULL, 0);
7678}
7679
7680#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7681 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007682 || defined(FEAT_GUI_MSWIN) \
7683 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 || defined(PROTO)
7685
7686/*
7687 * Handle a file drop. The code is here because a drop is *nearly* like an
7688 * :args command, but not quite (we have a list of exact filenames, so we
7689 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7690 * code is very similar to :args and hence needs access to a lot of the static
7691 * functions in this file.
7692 *
7693 * The list should be allocated using alloc(), as should each item in the
7694 * list. This function takes over responsibility for freeing the list.
7695 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007696 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7698 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7699 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7700 * file functionality is (currently) not in EMX this is not presently a
7701 * problem.
7702 */
7703 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007704handle_drop(
7705 int filec, /* the number of files dropped */
7706 char_u **filev, /* the list of files dropped */
7707 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708{
7709 exarg_T ea;
7710 int save_msg_scroll = msg_scroll;
7711
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007712 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007713 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007715#ifdef FEAT_AUTOCMD
7716 if (curbuf_locked())
7717 return;
7718#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007719 /* When the screen is being updated we should not change buffers and
7720 * windows structures, it may cause freed memory to be used. */
7721 if (updating_screen)
7722 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723
7724 /* Check whether the current buffer is changed. If so, we will need
7725 * to split the current window or data could be lost.
7726 * We don't need to check if the 'hidden' option is set, as in this
7727 * case the buffer won't be lost.
7728 */
7729 if (!P_HID(curbuf) && !split)
7730 {
7731 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007732 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 --emsg_off;
7734 }
7735 if (split)
7736 {
7737# ifdef FEAT_WINDOWS
7738 if (win_split(0, 0) == FAIL)
7739 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007740 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741
7742 /* When splitting the window, create a new alist. Otherwise the
7743 * existing one is overwritten. */
7744 alist_unlink(curwin->w_alist);
7745 alist_new();
7746# else
7747 return; /* can't split, always fail */
7748# endif
7749 }
7750
7751 /*
7752 * Set up the new argument list.
7753 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007754 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755
7756 /*
7757 * Move to the first file.
7758 */
7759 /* Fake up a minimal "next" command for do_argfile() */
7760 vim_memset(&ea, 0, sizeof(ea));
7761 ea.cmd = (char_u *)"next";
7762 do_argfile(&ea, 0);
7763
7764 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7765 * mode that is not needed here. */
7766 need_start_insertmode = FALSE;
7767
7768 /* Restore msg_scroll, otherwise a following command may cause scrolling
7769 * unexpectedly. The screen will be redrawn by the caller, thus
7770 * msg_scroll being set by displaying a message is irrelevant. */
7771 msg_scroll = save_msg_scroll;
7772}
7773#endif
7774
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775/*
7776 * Clear an argument list: free all file names and reset it to zero entries.
7777 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007778 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007779alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780{
7781 while (--al->al_ga.ga_len >= 0)
7782 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7783 ga_clear(&al->al_ga);
7784}
7785
7786/*
7787 * Init an argument list.
7788 */
7789 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007790alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791{
7792 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7793}
7794
7795#if defined(FEAT_WINDOWS) || defined(PROTO)
7796
7797/*
7798 * Remove a reference from an argument list.
7799 * Ignored when the argument list is the global one.
7800 * If the argument list is no longer used by any window, free it.
7801 */
7802 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007803alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804{
7805 if (al != &global_alist && --al->al_refcount <= 0)
7806 {
7807 alist_clear(al);
7808 vim_free(al);
7809 }
7810}
7811
7812# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7813/*
7814 * Create a new argument list and use it for the current window.
7815 */
7816 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007817alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818{
7819 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7820 if (curwin->w_alist == NULL)
7821 {
7822 curwin->w_alist = &global_alist;
7823 ++global_alist.al_refcount;
7824 }
7825 else
7826 {
7827 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007828 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 alist_init(curwin->w_alist);
7830 }
7831}
7832# endif
7833#endif
7834
Bram Moolenaar53076832015-12-31 19:53:21 +01007835#if (!defined(UNIX) && !defined(__EMX__)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836/*
7837 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007838 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7839 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 */
7841 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007842alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843{
7844 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007845 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 char_u **new_arg_files;
7847 int new_arg_file_count;
7848 char_u *save_p_su = p_su;
7849 int i;
7850
7851 /* Don't use 'suffixes' here. This should work like the shell did the
7852 * expansion. Also, the vimrc file isn't read yet, thus the user
7853 * can't set the options. */
7854 p_su = empty_option;
7855 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7856 if (old_arg_files != NULL)
7857 {
7858 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007859 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7860 old_arg_count = GARGCOUNT;
7861 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007863 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 && new_arg_file_count > 0)
7865 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007866 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7867 TRUE, fnum_list, fnum_len);
7868 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 }
7871 p_su = save_p_su;
7872}
7873#endif
7874
7875/*
7876 * Set the argument list for the current window.
7877 * Takes over the allocated files[] and the allocated fnames in it.
7878 */
7879 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007880alist_set(
7881 alist_T *al,
7882 int count,
7883 char_u **files,
7884 int use_curbuf,
7885 int *fnum_list,
7886 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887{
7888 int i;
7889
7890 alist_clear(al);
7891 if (ga_grow(&al->al_ga, count) == OK)
7892 {
7893 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007894 {
7895 if (got_int)
7896 {
7897 /* When adding many buffers this can take a long time. Allow
7898 * interrupting here. */
7899 while (i < count)
7900 vim_free(files[i++]);
7901 break;
7902 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007903
7904 /* May set buffer name of a buffer previously used for the
7905 * argument list, so that it's re-used by alist_add. */
7906 if (fnum_list != NULL && i < fnum_len)
7907 buf_set_name(fnum_list[i], files[i]);
7908
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007910 ui_breakcheck();
7911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 vim_free(files);
7913 }
7914 else
7915 FreeWild(count, files);
7916#ifdef FEAT_WINDOWS
7917 if (al == &global_alist)
7918#endif
7919 arg_had_last = FALSE;
7920}
7921
7922/*
7923 * Add file "fname" to argument list "al".
7924 * "fname" must have been allocated and "al" must have been checked for room.
7925 */
7926 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007927alist_add(
7928 alist_T *al,
7929 char_u *fname,
7930 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931{
7932 if (fname == NULL) /* don't add NULL file names */
7933 return;
7934#ifdef BACKSLASH_IN_FILENAME
7935 slash_adjust(fname);
7936#endif
7937 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7938 if (set_fnum > 0)
7939 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7940 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7941 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942}
7943
7944#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7945/*
7946 * Adjust slashes in file names. Called after 'shellslash' was set.
7947 */
7948 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007949alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950{
7951 int i;
7952# ifdef FEAT_WINDOWS
7953 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007954 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955# endif
7956
7957 for (i = 0; i < GARGCOUNT; ++i)
7958 if (GARGLIST[i].ae_fname != NULL)
7959 slash_adjust(GARGLIST[i].ae_fname);
7960# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007961 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 if (wp->w_alist != &global_alist)
7963 for (i = 0; i < WARGCOUNT(wp); ++i)
7964 if (WARGLIST(wp)[i].ae_fname != NULL)
7965 slash_adjust(WARGLIST(wp)[i].ae_fname);
7966# endif
7967}
7968#endif
7969
7970/*
7971 * ":preserve".
7972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007974ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007976 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 ml_preserve(curbuf, TRUE);
7978}
7979
7980/*
7981 * ":recover".
7982 */
7983 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007984ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985{
7986 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7987 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007988 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7989 | CCGD_MULTWIN
7990 | (eap->forceit ? CCGD_FORCEIT : 0)
7991 | CCGD_EXCMD)
7992
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 && (*eap->arg == NUL
7994 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7995 ml_recover();
7996 recoverymode = FALSE;
7997}
7998
7999/*
8000 * Command modifier used in a wrong way.
8001 */
8002 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008003ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004{
8005 eap->errmsg = e_invcmd;
8006}
8007
8008#ifdef FEAT_WINDOWS
8009/*
8010 * :sview [+command] file split window with new file, read-only
8011 * :split [[+command] file] split window with current or new file
8012 * :vsplit [[+command] file] split window vertically with current or new file
8013 * :new [[+command] file] split window with no or new file
8014 * :vnew [[+command] file] split vertically window with no or new file
8015 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008016 *
8017 * :tabedit open new Tab page with empty window
8018 * :tabedit [+command] file open new Tab page and edit "file"
8019 * :tabnew [[+command] file] just like :tabedit
8020 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 */
8022 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008023ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008025 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008026# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008028# endif
8029# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008031# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008033# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008035# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008037# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008039 * quickfix windows. But it's OK when doing ":tab split". */
8040 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 {
8042 if (eap->cmdidx == CMD_split)
8043 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 if (eap->cmdidx == CMD_vsplit)
8045 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008047# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008049# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008050 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 {
8052 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8053 FNAME_MESS, TRUE, curbuf->b_ffname);
8054 if (fname == NULL)
8055 goto theend;
8056 eap->arg = fname;
8057 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008058# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008060# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008062# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 && eap->cmdidx != CMD_new)
8066 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008067# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008068 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008069# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008070 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008071# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008072 au_has_group((char_u *)"FileExplorer"))
8073 {
8074 /* No browsing supported but we do have the file explorer:
8075 * Edit the directory. */
8076 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8077 eap->arg = (char_u *)".";
8078 }
8079 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008080# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008081 {
8082 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008084 if (fname == NULL)
8085 goto theend;
8086 eap->arg = fname;
8087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 }
8089 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008090# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008092 /*
8093 * Either open new tab page or split the window.
8094 */
8095 if (eap->cmdidx == CMD_tabedit
8096 || eap->cmdidx == CMD_tabfind
8097 || eap->cmdidx == CMD_tabnew)
8098 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008099 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8100 : eap->addr_count == 0 ? 0
8101 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008102 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008103 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008104
8105 /* set the alternate buffer for the window we came from */
8106 if (curwin != old_curwin
8107 && win_valid(old_curwin)
8108 && old_curwin->w_buffer != curbuf
8109 && !cmdmod.keepalt)
8110 old_curwin->w_alt_fnum = curbuf->b_fnum;
8111 }
8112 }
8113 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8115 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008116# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 /* Reset 'scrollbind' when editing another file, but keep it when
8118 * doing ":split" without arguments. */
8119 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008120# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008122# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008124 {
8125 RESET_BINDING(curwin);
8126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 else
8128 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008129# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 do_exedit(eap, old_curwin);
8131 }
8132
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008133# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008135# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008137# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138theend:
8139 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008140# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008142
8143/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008144 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008145 */
8146 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008147tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008148{
8149 exarg_T ea;
8150
8151 vim_memset(&ea, 0, sizeof(ea));
8152 ea.cmdidx = CMD_tabnew;
8153 ea.cmd = (char_u *)"tabn";
8154 ea.arg = (char_u *)"";
8155 ex_splitview(&ea);
8156}
8157
8158/*
8159 * :tabnext command
8160 */
8161 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008162ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008163{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008164 switch (eap->cmdidx)
8165 {
8166 case CMD_tabfirst:
8167 case CMD_tabrewind:
8168 goto_tabpage(1);
8169 break;
8170 case CMD_tablast:
8171 goto_tabpage(9999);
8172 break;
8173 case CMD_tabprevious:
8174 case CMD_tabNext:
8175 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
8176 break;
8177 default: /* CMD_tabnext */
8178 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
8179 break;
8180 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008181}
8182
8183/*
8184 * :tabmove command
8185 */
8186 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008187ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008188{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008189 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008190
8191 if (eap->arg && *eap->arg != NUL)
8192 {
8193 char_u *p = eap->arg;
8194 int relative = 0; /* argument +N/-N means: move N places to the
8195 * right/left relative to the current position. */
8196
8197 if (*eap->arg == '-')
8198 {
8199 relative = -1;
8200 p = eap->arg + 1;
8201 }
8202 else if (*eap->arg == '+')
8203 {
8204 relative = 1;
8205 p = eap->arg + 1;
8206 }
8207 else
8208 p = eap->arg;
8209
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008210 if (relative == 0)
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008211 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008212 if (STRCMP(p, "$") == 0)
8213 tab_number = LAST_TAB_NR;
8214 else if (p == skipdigits(p))
8215 {
8216 /* No numbers as argument. */
8217 eap->errmsg = e_invarg;
8218 return;
8219 }
8220 else
8221 tab_number = getdigits(&p);
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008222 }
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008223 else
8224 {
8225 if (*p != NUL)
8226 tab_number = getdigits(&p);
8227 else
8228 tab_number = 1;
8229 tab_number = tab_number * relative + tabpage_index(curtab);
8230 if (relative == -1)
8231 --tab_number;
8232 }
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008233 }
8234 else if (eap->addr_count != 0)
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008235 {
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008236 tab_number = eap->line2;
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008237 if (**eap->cmdlinep == '-')
8238 --tab_number;
8239 }
8240 else
8241 tab_number = LAST_TAB_NR;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008242
8243 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008244}
8245
8246/*
8247 * :tabs command: List tabs and their contents.
8248 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008249 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008250ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008251{
8252 tabpage_T *tp;
8253 win_T *wp;
8254 int tabcount = 1;
8255
8256 msg_start();
8257 msg_scroll = TRUE;
8258 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8259 {
8260 msg_putchar('\n');
8261 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
8262 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
8263 out_flush(); /* output one line at a time */
8264 ui_breakcheck();
8265
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008266 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008267 wp = firstwin;
8268 else
8269 wp = tp->tp_firstwin;
8270 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8271 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008272 msg_putchar('\n');
8273 msg_putchar(wp == curwin ? '>' : ' ');
8274 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008275 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8276 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008277 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008278 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008279 else
8280 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8281 IObuff, IOSIZE, TRUE);
8282 msg_outtrans(IObuff);
8283 out_flush(); /* output one line at a time */
8284 ui_breakcheck();
8285 }
8286 }
8287}
8288
8289#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290
8291/*
8292 * ":mode": Set screen mode.
8293 * If no argument given, just get the screen size and redraw.
8294 */
8295 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008296ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297{
8298 if (*eap->arg == NUL)
8299 shell_resized();
8300 else
8301 mch_screenmode(eap->arg);
8302}
8303
8304#ifdef FEAT_WINDOWS
8305/*
8306 * ":resize".
8307 * set, increment or decrement current window height
8308 */
8309 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008310ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311{
8312 int n;
8313 win_T *wp = curwin;
8314
8315 if (eap->addr_count > 0)
8316 {
8317 n = eap->line2;
8318 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8319 ;
8320 }
8321
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008322# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008324# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 if (cmdmod.split & WSP_VERT)
8327 {
8328 if (*eap->arg == '-' || *eap->arg == '+')
8329 n += W_WIDTH(curwin);
8330 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8331 n = 9999;
8332 win_setwidth_win((int)n, wp);
8333 }
8334 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 {
8336 if (*eap->arg == '-' || *eap->arg == '+')
8337 n += curwin->w_height;
8338 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8339 n = 9999;
8340 win_setheight_win((int)n, wp);
8341 }
8342}
8343#endif
8344
8345/*
8346 * ":find [+command] <file>" command.
8347 */
8348 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008349ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350{
8351#ifdef FEAT_SEARCHPATH
8352 char_u *fname;
8353 int count;
8354
8355 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8356 TRUE, curbuf->b_ffname);
8357 if (eap->addr_count > 0)
8358 {
8359 /* Repeat finding the file "count" times. This matters when it
8360 * appears several times in the path. */
8361 count = eap->line2;
8362 while (fname != NULL && --count > 0)
8363 {
8364 vim_free(fname);
8365 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8366 FALSE, curbuf->b_ffname);
8367 }
8368 }
8369
8370 if (fname != NULL)
8371 {
8372 eap->arg = fname;
8373#endif
8374 do_exedit(eap, NULL);
8375#ifdef FEAT_SEARCHPATH
8376 vim_free(fname);
8377 }
8378#endif
8379}
8380
8381/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008382 * ":open" simulation: for now just work like ":visual".
8383 */
8384 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008385ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008386{
8387 regmatch_T regmatch;
8388 char_u *p;
8389
8390 curwin->w_cursor.lnum = eap->line2;
8391 beginline(BL_SOL | BL_FIX);
8392 if (*eap->arg == '/')
8393 {
8394 /* ":open /pattern/": put cursor in column found with pattern */
8395 ++eap->arg;
8396 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8397 *p = NUL;
8398 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8399 if (regmatch.regprog != NULL)
8400 {
8401 regmatch.rm_ic = p_ic;
8402 p = ml_get_curline();
8403 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008404 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008405 else
8406 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008407 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008408 }
8409 /* Move to the NUL, ignore any other arguments. */
8410 eap->arg += STRLEN(eap->arg);
8411 }
8412 check_cursor();
8413
8414 eap->cmdidx = CMD_visual;
8415 do_exedit(eap, NULL);
8416}
8417
8418/*
8419 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 */
8421 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008422ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423{
8424 do_exedit(eap, NULL);
8425}
8426
8427/*
8428 * ":edit <file>" command and alikes.
8429 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008431do_exedit(
8432 exarg_T *eap,
8433 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434{
8435 int n;
8436#ifdef FEAT_WINDOWS
8437 int need_hide;
8438#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008439 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440
8441 /*
8442 * ":vi" command ends Ex mode.
8443 */
8444 if (exmode_active && (eap->cmdidx == CMD_visual
8445 || eap->cmdidx == CMD_view))
8446 {
8447 exmode_active = FALSE;
8448 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008449 {
8450 /* Special case: ":global/pat/visual\NLvi-commands" */
8451 if (global_busy)
8452 {
8453 int rd = RedrawingDisabled;
8454 int nwr = no_wait_return;
8455 int ms = msg_scroll;
8456#ifdef FEAT_GUI
8457 int he = hold_gui_events;
8458#endif
8459
8460 if (eap->nextcmd != NULL)
8461 {
8462 stuffReadbuff(eap->nextcmd);
8463 eap->nextcmd = NULL;
8464 }
8465
8466 if (exmode_was != EXMODE_VIM)
8467 settmode(TMODE_RAW);
8468 RedrawingDisabled = 0;
8469 no_wait_return = 0;
8470 need_wait_return = FALSE;
8471 msg_scroll = 0;
8472#ifdef FEAT_GUI
8473 hold_gui_events = 0;
8474#endif
8475 must_redraw = CLEAR;
8476
8477 main_loop(FALSE, TRUE);
8478
8479 RedrawingDisabled = rd;
8480 no_wait_return = nwr;
8481 msg_scroll = ms;
8482#ifdef FEAT_GUI
8483 hold_gui_events = he;
8484#endif
8485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 }
8489
8490 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008491 || eap->cmdidx == CMD_tabnew
8492 || eap->cmdidx == CMD_tabedit
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008493#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 || eap->cmdidx == CMD_vnew
8495#endif
8496 ) && *eap->arg == NUL)
8497 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008498 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 setpcmark();
8500 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008501 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8502 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 }
8504 else if ((eap->cmdidx != CMD_split
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008505#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 && eap->cmdidx != CMD_vsplit
8507#endif
8508 )
8509 || *eap->arg != NUL
8510#ifdef FEAT_BROWSE
8511 || cmdmod.browse
8512#endif
8513 )
8514 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008515#ifdef FEAT_AUTOCMD
8516 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8517 * can bring us here, others are stopped earlier. */
8518 if (*eap->arg != NUL && curbuf_locked())
8519 return;
8520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 n = readonlymode;
8522 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8523 readonlymode = TRUE;
8524 else if (eap->cmdidx == CMD_enew)
8525 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8526 empty buffer */
8527 setpcmark();
8528 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8529 NULL, eap,
8530 /* ":edit" goes to first line if Vi compatible */
8531 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8532 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8533 ? ECMD_ONE : eap->do_ecmd_lnum,
8534 (P_HID(curbuf) ? ECMD_HIDE : 0)
8535 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008536 /* after a split we can use an existing buffer */
8537 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538#ifdef FEAT_LISTCMDS
8539 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8540#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008541 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 {
8543 /* Editing the file failed. If the window was split, close it. */
8544#ifdef FEAT_WINDOWS
8545 if (old_curwin != NULL)
8546 {
8547 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8548 if (!need_hide || P_HID(curbuf))
8549 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008550# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8551 cleanup_T cs;
8552
8553 /* Reset the error/interrupt/exception state here so that
8554 * aborting() returns FALSE when closing a window. */
8555 enter_cleanup(&cs);
8556# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557# ifdef FEAT_GUI
8558 need_mouse_correct = TRUE;
8559# endif
8560 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008561
8562# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8563 /* Restore the error/interrupt/exception state if not
8564 * discarded by a new aborting error, interrupt, or
8565 * uncaught exception. */
8566 leave_cleanup(&cs);
8567# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 }
8569 }
8570#endif
8571 }
8572 else if (readonlymode && curbuf->b_nwindows == 1)
8573 {
8574 /* When editing an already visited buffer, 'readonly' won't be set
8575 * but the previous value is kept. With ":view" and ":sview" we
8576 * want the file to be readonly, except when another window is
8577 * editing the same buffer. */
8578 curbuf->b_p_ro = TRUE;
8579 }
8580 readonlymode = n;
8581 }
8582 else
8583 {
8584 if (eap->do_ecmd_cmd != NULL)
8585 do_cmdline_cmd(eap->do_ecmd_cmd);
8586#ifdef FEAT_TITLE
8587 n = curwin->w_arg_idx_invalid;
8588#endif
8589 check_arg_idx(curwin);
8590#ifdef FEAT_TITLE
8591 if (n != curwin->w_arg_idx_invalid)
8592 maketitle();
8593#endif
8594 }
8595
8596#ifdef FEAT_WINDOWS
8597 /*
8598 * if ":split file" worked, set alternate file name in old window to new
8599 * file
8600 */
8601 if (old_curwin != NULL
8602 && *eap->arg != NUL
8603 && curwin != old_curwin
8604 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008605 && old_curwin->w_buffer != curbuf
8606 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 old_curwin->w_alt_fnum = curbuf->b_fnum;
8608#endif
8609
8610 ex_no_reprint = TRUE;
8611}
8612
8613#ifndef FEAT_GUI
8614/*
8615 * ":gui" and ":gvim" when there is no GUI.
8616 */
8617 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008618ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619{
8620 eap->errmsg = e_nogvim;
8621}
8622#endif
8623
8624#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8625 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008626ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627{
8628 gui_make_tearoff(eap->arg);
8629}
8630#endif
8631
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008632#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008634ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008636 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637}
8638#endif
8639
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008641ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642{
8643 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8644 MSG(_("No swap file"));
8645 else
8646 msg(curbuf->b_ml.ml_mfp->mf_fname);
8647}
8648
8649/*
8650 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8651 * offset.
8652 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8653 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008655ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656{
8657#ifdef FEAT_SCROLLBIND
8658 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008659 win_T *save_curwin = curwin;
8660 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 long topline;
8662 long y;
8663 linenr_T old_linenr = curwin->w_cursor.lnum;
8664
8665 setpcmark();
8666
8667 /*
8668 * determine max topline
8669 */
8670 if (curwin->w_p_scb)
8671 {
8672 topline = curwin->w_topline;
8673 for (wp = firstwin; wp; wp = wp->w_next)
8674 {
8675 if (wp->w_p_scb && wp->w_buffer)
8676 {
8677 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8678 if (topline > y)
8679 topline = y;
8680 }
8681 }
8682 if (topline < 1)
8683 topline = 1;
8684 }
8685 else
8686 {
8687 topline = 1;
8688 }
8689
8690
8691 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008692 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8695 {
8696 if (curwin->w_p_scb)
8697 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008698 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 y = topline - curwin->w_topline;
8700 if (y > 0)
8701 scrollup(y, TRUE);
8702 else
8703 scrolldown(-y, TRUE);
8704 curwin->w_scbind_pos = topline;
8705 redraw_later(VALID);
8706 cursor_correct();
8707#ifdef FEAT_WINDOWS
8708 curwin->w_redr_status = TRUE;
8709#endif
8710 }
8711 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008712 curwin = save_curwin;
8713 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 if (curwin->w_p_scb)
8715 {
8716 did_syncbind = TRUE;
8717 checkpcmark();
8718 if (old_linenr != curwin->w_cursor.lnum)
8719 {
8720 char_u ctrl_o[2];
8721
8722 ctrl_o[0] = Ctrl_O;
8723 ctrl_o[1] = 0;
8724 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8725 }
8726 }
8727#endif
8728}
8729
8730
8731 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008732ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008734 int i;
8735 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8736 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737
8738 if (eap->usefilter) /* :r!cmd */
8739 do_bang(1, eap, FALSE, FALSE, TRUE);
8740 else
8741 {
8742 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8743 return;
8744
8745#ifdef FEAT_BROWSE
8746 if (cmdmod.browse)
8747 {
8748 char_u *browseFile;
8749
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008750 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 NULL, NULL, NULL, curbuf);
8752 if (browseFile != NULL)
8753 {
8754 i = readfile(browseFile, NULL,
8755 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8756 vim_free(browseFile);
8757 }
8758 else
8759 i = OK;
8760 }
8761 else
8762#endif
8763 if (*eap->arg == NUL)
8764 {
8765 if (check_fname() == FAIL) /* check for no file name */
8766 return;
8767 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8768 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8769 }
8770 else
8771 {
8772 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8773 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8774 i = readfile(eap->arg, NULL,
8775 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8776
8777 }
8778 if (i == FAIL)
8779 {
8780#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8781 if (!aborting())
8782#endif
8783 EMSG2(_(e_notopen), eap->arg);
8784 }
8785 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008786 {
8787 if (empty && exmode_active)
8788 {
8789 /* Delete the empty line that remains. Historically ex does
8790 * this but vi doesn't. */
8791 if (eap->line2 == 0)
8792 lnum = curbuf->b_ml.ml_line_count;
8793 else
8794 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008795 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008796 {
8797 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008798 if (curwin->w_cursor.lnum > 1
8799 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008800 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008801 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008802 }
8803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 }
8807}
8808
Bram Moolenaarea408852005-06-25 22:49:46 +00008809static char_u *prev_dir = NULL;
8810
8811#if defined(EXITFREE) || defined(PROTO)
8812 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008813free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00008814{
8815 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008816 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008817
8818 vim_free(globaldir);
8819 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008820}
8821#endif
8822
Bram Moolenaarf4258302013-06-02 18:20:17 +02008823/*
8824 * Deal with the side effects of changing the current directory.
8825 * When "local" is TRUE then this was after an ":lcd" command.
8826 */
8827 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008828post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008829{
8830 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008831 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008832 if (local)
8833 {
8834 /* If still in global directory, need to remember current
8835 * directory as global directory. */
8836 if (globaldir == NULL && prev_dir != NULL)
8837 globaldir = vim_strsave(prev_dir);
8838 /* Remember this local directory for the window. */
8839 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8840 curwin->w_localdir = vim_strsave(NameBuff);
8841 }
8842 else
8843 {
8844 /* We are now in the global directory, no need to remember its
8845 * name. */
8846 vim_free(globaldir);
8847 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008848 }
8849
8850 shorten_fnames(TRUE);
8851}
8852
Bram Moolenaarea408852005-06-25 22:49:46 +00008853
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854/*
8855 * ":cd", ":lcd", ":chdir" and ":lchdir".
8856 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008857 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008858ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 char_u *new_dir;
8861 char_u *tofree;
8862
8863 new_dir = eap->arg;
8864#if !defined(UNIX) && !defined(VMS)
8865 /* for non-UNIX ":cd" means: print current directory */
8866 if (*new_dir == NUL)
8867 ex_pwd(NULL);
8868 else
8869#endif
8870 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008871#ifdef FEAT_AUTOCMD
8872 if (allbuf_locked())
8873 return;
8874#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008875 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8876 && !eap->forceit)
8877 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008878 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008879 return;
8880 }
8881
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 /* ":cd -": Change to previous directory */
8883 if (STRCMP(new_dir, "-") == 0)
8884 {
8885 if (prev_dir == NULL)
8886 {
8887 EMSG(_("E186: No previous directory"));
8888 return;
8889 }
8890 new_dir = prev_dir;
8891 }
8892
8893 /* Save current directory for next ":cd -" */
8894 tofree = prev_dir;
8895 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8896 prev_dir = vim_strsave(NameBuff);
8897 else
8898 prev_dir = NULL;
8899
8900#if defined(UNIX) || defined(VMS)
8901 /* for UNIX ":cd" means: go to home directory */
8902 if (*new_dir == NUL)
8903 {
8904 /* use NameBuff for home directory name */
8905# ifdef VMS
8906 char_u *p;
8907
8908 p = mch_getenv((char_u *)"SYS$LOGIN");
8909 if (p == NULL || *p == NUL) /* empty is the same as not set */
8910 NameBuff[0] = NUL;
8911 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008912 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913# else
8914 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8915# endif
8916 new_dir = NameBuff;
8917 }
8918#endif
8919 if (new_dir == NULL || vim_chdir(new_dir))
8920 EMSG(_(e_failed));
8921 else
8922 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008923 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924
8925 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008926 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 ex_pwd(eap);
8928 }
8929 vim_free(tofree);
8930 }
8931}
8932
8933/*
8934 * ":pwd".
8935 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008937ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938{
8939 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8940 {
8941#ifdef BACKSLASH_IN_FILENAME
8942 slash_adjust(NameBuff);
8943#endif
8944 msg(NameBuff);
8945 }
8946 else
8947 EMSG(_("E187: Unknown"));
8948}
8949
8950/*
8951 * ":=".
8952 */
8953 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008954ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955{
8956 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008957 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958}
8959
8960 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008961ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008963 int n;
8964 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965
8966 if (cursor_valid())
8967 {
8968 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8969 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01008970 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008972
8973 len = eap->line2;
8974 switch (*eap->arg)
8975 {
8976 case 'm': break;
8977 case NUL: len *= 1000L; break;
8978 default: EMSG2(_(e_invarg2), eap->arg); return;
8979 }
8980 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981}
8982
8983/*
8984 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8985 */
8986 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008987do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988{
8989 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01008990 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991
8992 cursor_on();
8993 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01008994 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01008996 wait_now = msec - done > 1000L ? 1000L : msec - done;
8997#ifdef FEAT_TIMERS
8998 {
8999 long due_time = check_due_timer();
9000
9001 if (due_time > 0 && due_time < wait_now)
9002 wait_now = due_time;
9003 }
9004#endif
9005 ui_delay(wait_now, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009007#ifdef MESSAGE_QUEUE
9008 /* Process the netbeans and clientserver messages that may have been
9009 * received in the call to ui_breakcheck() when the GUI is in use. This
9010 * may occur when running a test case. */
9011 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 }
9014}
9015
9016 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009017do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018{
9019 int mode;
9020 char_u *cmdp;
9021
9022 cmdp = eap->cmd;
9023 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9024
9025 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9026 eap->arg, mode, isabbrev))
9027 {
9028 case 1: EMSG(_(e_invarg));
9029 break;
9030 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9031 break;
9032 }
9033}
9034
9035/*
9036 * ":winsize" command (obsolete).
9037 */
9038 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009039ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040{
9041 int w, h;
9042 char_u *arg = eap->arg;
9043 char_u *p;
9044
9045 w = getdigits(&arg);
9046 arg = skipwhite(arg);
9047 p = arg;
9048 h = getdigits(&arg);
9049 if (*p != NUL && *arg == NUL)
9050 set_shellsize(w, h, TRUE);
9051 else
9052 EMSG(_("E465: :winsize requires two number arguments"));
9053}
9054
9055#ifdef FEAT_WINDOWS
9056 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009057ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058{
9059 int xchar = NUL;
9060 char_u *p;
9061
9062 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9063 {
9064 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9065 if (eap->arg[1] == NUL)
9066 {
9067 EMSG(_(e_invarg));
9068 return;
9069 }
9070 xchar = eap->arg[1];
9071 p = eap->arg + 2;
9072 }
9073 else
9074 p = eap->arg + 1;
9075
9076 eap->nextcmd = check_nextcmd(p);
9077 p = skipwhite(p);
9078 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9079 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009080 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 {
9082 /* Pass flags on for ":vertical wincmd ]". */
9083 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009084 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9086 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009087 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 }
9089}
9090#endif
9091
Bram Moolenaar843ee412004-06-30 16:16:41 +00009092#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093/*
9094 * ":winpos".
9095 */
9096 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009097ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098{
9099 int x, y;
9100 char_u *arg = eap->arg;
9101 char_u *p;
9102
9103 if (*arg == NUL)
9104 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009105# if defined(FEAT_GUI) || defined(MSWIN)
9106# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009108# else
9109 if (mch_get_winpos(&x, &y) != FAIL)
9110# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 {
9112 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9113 msg(IObuff);
9114 }
9115 else
9116# endif
9117 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9118 }
9119 else
9120 {
9121 x = getdigits(&arg);
9122 arg = skipwhite(arg);
9123 p = arg;
9124 y = getdigits(&arg);
9125 if (*p == NUL || *arg != NUL)
9126 {
9127 EMSG(_("E466: :winpos requires two number arguments"));
9128 return;
9129 }
9130# ifdef FEAT_GUI
9131 if (gui.in_use)
9132 gui_mch_set_winpos(x, y);
9133 else if (gui.starting)
9134 {
9135 /* Remember the coordinates for when the window is opened. */
9136 gui_win_x = x;
9137 gui_win_y = y;
9138 }
9139# ifdef HAVE_TGETENT
9140 else
9141# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009142# else
9143# ifdef MSWIN
9144 mch_set_winpos(x, y);
9145# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146# endif
9147# ifdef HAVE_TGETENT
9148 if (*T_CWP)
9149 term_set_winpos(x, y);
9150# endif
9151 }
9152}
9153#endif
9154
9155/*
9156 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9157 */
9158 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009159ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160{
9161 oparg_T oa;
9162
9163 clear_oparg(&oa);
9164 oa.regname = eap->regname;
9165 oa.start.lnum = eap->line1;
9166 oa.end.lnum = eap->line2;
9167 oa.line_count = eap->line2 - eap->line1 + 1;
9168 oa.motion_type = MLINE;
9169#ifdef FEAT_VIRTUALEDIT
9170 virtual_op = FALSE;
9171#endif
9172 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9173 {
9174 setpcmark();
9175 curwin->w_cursor.lnum = eap->line1;
9176 beginline(BL_SOL | BL_FIX);
9177 }
9178
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009179 if (VIsual_active)
9180 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009181
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 switch (eap->cmdidx)
9183 {
9184 case CMD_delete:
9185 oa.op_type = OP_DELETE;
9186 op_delete(&oa);
9187 break;
9188
9189 case CMD_yank:
9190 oa.op_type = OP_YANK;
9191 (void)op_yank(&oa, FALSE, TRUE);
9192 break;
9193
9194 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009195 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009197 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9198#else
9199 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009201 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 oa.op_type = OP_RSHIFT;
9203 else
9204 oa.op_type = OP_LSHIFT;
9205 op_shift(&oa, FALSE, eap->amount);
9206 break;
9207 }
9208#ifdef FEAT_VIRTUALEDIT
9209 virtual_op = MAYBE;
9210#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009211 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212}
9213
9214/*
9215 * ":put".
9216 */
9217 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009218ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219{
9220 /* ":0put" works like ":1put!". */
9221 if (eap->line2 == 0)
9222 {
9223 eap->line2 = 1;
9224 eap->forceit = TRUE;
9225 }
9226 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009227 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9228 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229}
9230
9231/*
9232 * Handle ":copy" and ":move".
9233 */
9234 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009235ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236{
9237 long n;
9238
Bram Moolenaaraa23b372015-09-08 18:46:31 +02009239 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240 if (eap->arg == NULL) /* error detected */
9241 {
9242 eap->nextcmd = NULL;
9243 return;
9244 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009245 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246
9247 /*
9248 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9249 */
9250 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9251 {
9252 EMSG(_(e_invaddr));
9253 return;
9254 }
9255
9256 if (eap->cmdidx == CMD_move)
9257 {
9258 if (do_move(eap->line1, eap->line2, n) == FAIL)
9259 return;
9260 }
9261 else
9262 ex_copy(eap->line1, eap->line2, n);
9263 u_clearline();
9264 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009265 ex_may_print(eap);
9266}
9267
9268/*
9269 * Print the current line if flags were given to the Ex command.
9270 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009271 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009272ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009273{
9274 if (eap->flags != 0)
9275 {
9276 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9277 (eap->flags & EXFLAG_LIST));
9278 ex_no_reprint = TRUE;
9279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280}
9281
9282/*
9283 * ":smagic" and ":snomagic".
9284 */
9285 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009286ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287{
9288 int magic_save = p_magic;
9289
9290 p_magic = (eap->cmdidx == CMD_smagic);
9291 do_sub(eap);
9292 p_magic = magic_save;
9293}
9294
9295/*
9296 * ":join".
9297 */
9298 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009299ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300{
9301 curwin->w_cursor.lnum = eap->line1;
9302 if (eap->line1 == eap->line2)
9303 {
9304 if (eap->addr_count >= 2) /* :2,2join does nothing */
9305 return;
9306 if (eap->line2 == curbuf->b_ml.ml_line_count)
9307 {
9308 beep_flush();
9309 return;
9310 }
9311 ++eap->line2;
9312 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009313 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009315 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316}
9317
9318/*
9319 * ":[addr]@r" or ":[addr]*r": execute register
9320 */
9321 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009322ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323{
9324 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009325 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326
9327 curwin->w_cursor.lnum = eap->line2;
9328
9329#ifdef USE_ON_FLY_SCROLL
9330 dont_scroll = TRUE; /* disallow scrolling here */
9331#endif
9332
9333 /* get the register name. No name means to use the previous one */
9334 c = *eap->arg;
9335 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9336 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009337 /* Put the register in the typeahead buffer with the "silent" flag. */
9338 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9339 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009340 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 else
9344 {
9345 int save_efr = exec_from_reg;
9346
9347 exec_from_reg = TRUE;
9348
9349 /*
9350 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009351 * Continue until the stuff buffer is empty and all added characters
9352 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009354 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9356
9357 exec_from_reg = save_efr;
9358 }
9359}
9360
9361/*
9362 * ":!".
9363 */
9364 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009365ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366{
9367 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9368}
9369
9370/*
9371 * ":undo".
9372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009374ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009376 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009377 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009378 else
9379 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380}
9381
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009382#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009383 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009384ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009385{
9386 char_u hash[UNDO_HASH_SIZE];
9387
9388 u_compute_hash(hash);
9389 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9390}
9391
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009392 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009393ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009394{
9395 char_u hash[UNDO_HASH_SIZE];
9396
9397 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009398 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009399}
9400#endif
9401
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402/*
9403 * ":redo".
9404 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009406ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407{
9408 u_redo(1);
9409}
9410
9411/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009412 * ":earlier" and ":later".
9413 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009414 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009415ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009416{
9417 long count = 0;
9418 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009419 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009420 char_u *p = eap->arg;
9421
9422 if (*p == NUL)
9423 count = 1;
9424 else if (isdigit(*p))
9425 {
9426 count = getdigits(&p);
9427 switch (*p)
9428 {
9429 case 's': ++p; sec = TRUE; break;
9430 case 'm': ++p; sec = TRUE; count *= 60; break;
9431 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009432 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9433 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009434 }
9435 }
9436
9437 if (*p != NUL)
9438 EMSG2(_(e_invarg2), eap->arg);
9439 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009440 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9441 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009442}
9443
9444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445 * ":redir": start/stop redirection.
9446 */
9447 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009448ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449{
9450 char *mode;
9451 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009452 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453
9454 if (STRICMP(eap->arg, "END") == 0)
9455 close_redir();
9456 else
9457 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009458 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009460 ++arg;
9461 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009463 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 mode = "a";
9465 }
9466 else
9467 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009468 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469
9470 close_redir();
9471
9472 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009473 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 if (fname == NULL)
9475 return;
9476#ifdef FEAT_BROWSE
9477 if (cmdmod.browse)
9478 {
9479 char_u *browseFile;
9480
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009481 browseFile = do_browse(BROWSE_SAVE,
9482 (char_u *)_("Save Redirection"),
9483 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 if (browseFile == NULL)
9485 return; /* operation cancelled */
9486 vim_free(fname);
9487 fname = browseFile;
9488 eap->forceit = TRUE; /* since dialog already asked */
9489 }
9490#endif
9491
9492 redir_fd = open_exfile(fname, eap->forceit, mode);
9493 vim_free(fname);
9494 }
9495#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009496 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 {
9498 /* redirect to a register a-z (resp. A-Z for appending) */
9499 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009500 ++arg;
9501 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009503 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009504 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009506 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009508 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009509 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009510 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009511 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009513 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009514 if (*arg == '>')
9515 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009516 /* Make register empty when not using @A-@Z and the
9517 * command is valid. */
9518 if (*arg == NUL && !isupper(redir_reg))
9519 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009521 }
9522 if (*arg != NUL)
9523 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009524 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009525 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009526 }
9527 }
9528 else if (*arg == '=' && arg[1] == '>')
9529 {
9530 int append;
9531
9532 /* redirect to a variable */
9533 close_redir();
9534 arg += 2;
9535
9536 if (*arg == '>')
9537 {
9538 ++arg;
9539 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540 }
9541 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009542 append = FALSE;
9543
9544 if (var_redir_start(skipwhite(arg), append) == OK)
9545 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546 }
9547#endif
9548
9549 /* TODO: redirect to a buffer */
9550
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009552 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009554
9555 /* Make sure redirection is not off. Can happen for cmdline completion
9556 * that indirectly invokes a command to catch its output. */
9557 if (redir_fd != NULL
9558#ifdef FEAT_EVAL
9559 || redir_reg || redir_vname
9560#endif
9561 )
9562 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563}
9564
9565/*
9566 * ":redraw": force redraw
9567 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009568 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009569ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570{
9571 int r = RedrawingDisabled;
9572 int p = p_lz;
9573
9574 RedrawingDisabled = 0;
9575 p_lz = FALSE;
9576 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009577 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578#ifdef FEAT_TITLE
9579 if (need_maketitle)
9580 maketitle();
9581#endif
9582 RedrawingDisabled = r;
9583 p_lz = p;
9584
9585 /* Reset msg_didout, so that a message that's there is overwritten. */
9586 msg_didout = FALSE;
9587 msg_col = 0;
9588
Bram Moolenaar56667a52013-07-17 11:54:28 +02009589 /* No need to wait after an intentional redraw. */
9590 need_wait_return = FALSE;
9591
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 out_flush();
9593}
9594
9595/*
9596 * ":redrawstatus": force redraw of status line(s)
9597 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009599ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600{
9601#if defined(FEAT_WINDOWS)
9602 int r = RedrawingDisabled;
9603 int p = p_lz;
9604
9605 RedrawingDisabled = 0;
9606 p_lz = FALSE;
9607 if (eap->forceit)
9608 status_redraw_all();
9609 else
9610 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009611 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 RedrawingDisabled = r;
9613 p_lz = p;
9614 out_flush();
9615#endif
9616}
9617
9618 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009619close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620{
9621 if (redir_fd != NULL)
9622 {
9623 fclose(redir_fd);
9624 redir_fd = NULL;
9625 }
9626#ifdef FEAT_EVAL
9627 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009628 if (redir_vname)
9629 {
9630 var_redir_stop();
9631 redir_vname = 0;
9632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633#endif
9634}
9635
9636#if defined(FEAT_SESSION) && defined(USE_CRNL)
9637# define MKSESSION_NL
9638static int mksession_nl = FALSE; /* use NL only in put_eol() */
9639#endif
9640
9641/*
9642 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9643 */
9644 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009645ex_mkrc(
9646 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647{
9648 FILE *fd;
9649 int failed = FALSE;
9650 char_u *fname;
9651#ifdef FEAT_BROWSE
9652 char_u *browseFile = NULL;
9653#endif
9654#ifdef FEAT_SESSION
9655 int view_session = FALSE;
9656 int using_vdir = FALSE; /* using 'viewdir'? */
9657 char_u *viewFile = NULL;
9658 unsigned *flagp;
9659#endif
9660
9661 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9662 {
9663#ifdef FEAT_SESSION
9664 view_session = TRUE;
9665#else
9666 ex_ni(eap);
9667 return;
9668#endif
9669 }
9670
9671#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009672 /* Use the short file name until ":lcd" is used. We also don't use the
9673 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009674 did_lcd = FALSE;
9675
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9677 if (eap->cmdidx == CMD_mkview
9678 && (*eap->arg == NUL
9679 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9680 {
9681 eap->forceit = TRUE;
9682 fname = get_view_file(*eap->arg);
9683 if (fname == NULL)
9684 return;
9685 viewFile = fname;
9686 using_vdir = TRUE;
9687 }
9688 else
9689#endif
9690 if (*eap->arg != NUL)
9691 fname = eap->arg;
9692 else if (eap->cmdidx == CMD_mkvimrc)
9693 fname = (char_u *)VIMRC_FILE;
9694#ifdef FEAT_SESSION
9695 else if (eap->cmdidx == CMD_mksession)
9696 fname = (char_u *)SESSION_FILE;
9697#endif
9698 else
9699 fname = (char_u *)EXRC_FILE;
9700
9701#ifdef FEAT_BROWSE
9702 if (cmdmod.browse)
9703 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009704 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705# ifdef FEAT_SESSION
9706 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9707 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9708# endif
9709 (char_u *)_("Save Setup"),
9710 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9711 if (browseFile == NULL)
9712 goto theend;
9713 fname = browseFile;
9714 eap->forceit = TRUE; /* since dialog already asked */
9715 }
9716#endif
9717
9718#if defined(FEAT_SESSION) && defined(vim_mkdir)
9719 /* When using 'viewdir' may have to create the directory. */
9720 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009721 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722#endif
9723
9724 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9725 if (fd != NULL)
9726 {
9727#ifdef FEAT_SESSION
9728 if (eap->cmdidx == CMD_mkview)
9729 flagp = &vop_flags;
9730 else
9731 flagp = &ssop_flags;
9732#endif
9733
9734#ifdef MKSESSION_NL
9735 /* "unix" in 'sessionoptions': use NL line separator */
9736 if (view_session && (*flagp & SSOP_UNIX))
9737 mksession_nl = TRUE;
9738#endif
9739
9740 /* Write the version command for :mkvimrc */
9741 if (eap->cmdidx == CMD_mkvimrc)
9742 (void)put_line(fd, "version 6.0");
9743
9744#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009745 if (eap->cmdidx == CMD_mksession)
9746 {
9747 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9748 failed = TRUE;
9749 }
9750
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 if (eap->cmdidx != CMD_mkview)
9752#endif
9753 {
9754 /* Write setting 'compatible' first, because it has side effects.
9755 * For that same reason only do it when needed. */
9756 if (p_cp)
9757 (void)put_line(fd, "if !&cp | set cp | endif");
9758 else
9759 (void)put_line(fd, "if &cp | set nocp | endif");
9760 }
9761
9762#ifdef FEAT_SESSION
9763 if (!view_session
9764 || (eap->cmdidx == CMD_mksession
9765 && (*flagp & SSOP_OPTIONS)))
9766#endif
9767 failed |= (makemap(fd, NULL) == FAIL
9768 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9769
9770#ifdef FEAT_SESSION
9771 if (!failed && view_session)
9772 {
9773 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9774 failed = TRUE;
9775 if (eap->cmdidx == CMD_mksession)
9776 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009777 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778
Bram Moolenaard9462e32011-04-11 21:35:11 +02009779 dirnow = alloc(MAXPATHL);
9780 if (dirnow == NULL)
9781 failed = TRUE;
9782 else
9783 {
9784 /*
9785 * Change to session file's dir.
9786 */
9787 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009789 *dirnow = NUL;
9790 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9791 {
9792 if (vim_chdirfile(fname) == OK)
9793 shorten_fnames(TRUE);
9794 }
9795 else if (*dirnow != NUL
9796 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9797 {
9798 if (mch_chdir((char *)globaldir) == 0)
9799 shorten_fnames(TRUE);
9800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801
Bram Moolenaard9462e32011-04-11 21:35:11 +02009802 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803
Bram Moolenaard9462e32011-04-11 21:35:11 +02009804 /* restore original dir */
9805 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009807 {
9808 if (mch_chdir((char *)dirnow) != 0)
9809 EMSG(_(e_prev_dir));
9810 shorten_fnames(TRUE);
9811 }
9812 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 }
9814 }
9815 else
9816 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009817 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9818 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819 }
9820 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9821 == FAIL)
9822 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009823 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9824 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009825 if (eap->cmdidx == CMD_mksession)
9826 {
9827 if (put_line(fd, "unlet SessionLoad") == FAIL)
9828 failed = TRUE;
9829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 }
9831#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009832 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9833 failed = TRUE;
9834
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835 failed |= fclose(fd);
9836
9837 if (failed)
9838 EMSG(_(e_write));
9839#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9840 else if (eap->cmdidx == CMD_mksession)
9841 {
9842 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009843 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844
Bram Moolenaard9462e32011-04-11 21:35:11 +02009845 tbuf = alloc(MAXPATHL);
9846 if (tbuf != NULL)
9847 {
9848 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9849 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9850 vim_free(tbuf);
9851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 }
9853#endif
9854#ifdef MKSESSION_NL
9855 mksession_nl = FALSE;
9856#endif
9857 }
9858
9859#ifdef FEAT_BROWSE
9860theend:
9861 vim_free(browseFile);
9862#endif
9863#ifdef FEAT_SESSION
9864 vim_free(viewFile);
9865#endif
9866}
9867
Bram Moolenaardf177f62005-02-22 08:39:57 +00009868#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9869 || defined(PROTO)
9870 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009871vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009872{
9873 if (vim_mkdir(name, prot) != 0)
9874 {
9875 EMSG2(_("E739: Cannot create directory: %s"), name);
9876 return FAIL;
9877 }
9878 return OK;
9879}
9880#endif
9881
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882/*
9883 * Open a file for writing for an Ex command, with some checks.
9884 * Return file descriptor, or NULL on failure.
9885 */
9886 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009887open_exfile(
9888 char_u *fname,
9889 int forceit,
9890 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891{
9892 FILE *fd;
9893
9894#ifdef UNIX
9895 /* with Unix it is possible to open a directory */
9896 if (mch_isdir(fname))
9897 {
9898 EMSG2(_(e_isadir2), fname);
9899 return NULL;
9900 }
9901#endif
9902 if (!forceit && *mode != 'a' && vim_fexists(fname))
9903 {
9904 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9905 return NULL;
9906 }
9907
9908 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9909 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9910
9911 return fd;
9912}
9913
9914/*
9915 * ":mark" and ":k".
9916 */
9917 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009918ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919{
9920 pos_T pos;
9921
9922 if (*eap->arg == NUL) /* No argument? */
9923 EMSG(_(e_argreq));
9924 else if (eap->arg[1] != NUL) /* more than one character? */
9925 EMSG(_(e_trailing));
9926 else
9927 {
9928 pos = curwin->w_cursor; /* save curwin->w_cursor */
9929 curwin->w_cursor.lnum = eap->line2;
9930 beginline(BL_WHITE | BL_FIX);
9931 if (setmark(*eap->arg) == FAIL) /* set mark */
9932 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9933 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9934 }
9935}
9936
9937/*
9938 * Update w_topline, w_leftcol and the cursor position.
9939 */
9940 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009941update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942{
9943 check_cursor(); /* put cursor on valid line */
9944 update_topline();
9945 if (!curwin->w_p_wrap)
9946 validate_cursor();
9947 update_curswant();
9948}
9949
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950/*
9951 * ":normal[!] {commands}": Execute normal mode commands.
9952 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01009953 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009954ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 int save_msg_scroll = msg_scroll;
9957 int save_restart_edit = restart_edit;
9958 int save_msg_didout = msg_didout;
9959 int save_State = State;
9960 tasave_T tabuf;
9961 int save_insertmode = p_im;
9962 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009963 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964#ifdef FEAT_MBYTE
9965 char_u *arg = NULL;
9966 int l;
9967 char_u *p;
9968#endif
9969
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009970 if (ex_normal_lock > 0)
9971 {
9972 EMSG(_(e_secure));
9973 return;
9974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 if (ex_normal_busy >= p_mmd)
9976 {
9977 EMSG(_("E192: Recursive use of :normal too deep"));
9978 return;
9979 }
9980 ++ex_normal_busy;
9981
9982 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9983 restart_edit = 0; /* don't go to Insert mode */
9984 p_im = FALSE; /* don't use 'insertmode' */
9985
9986#ifdef FEAT_MBYTE
9987 /*
9988 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9989 * this for the K_SPECIAL leading byte, otherwise special keys will not
9990 * work.
9991 */
9992 if (has_mbyte)
9993 {
9994 int len = 0;
9995
9996 /* Count the number of characters to be escaped. */
9997 for (p = eap->arg; *p != NUL; ++p)
9998 {
9999# ifdef FEAT_GUI
10000 if (*p == CSI) /* leadbyte CSI */
10001 len += 2;
10002# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010003 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10005# ifdef FEAT_GUI
10006 || *p == CSI
10007# endif
10008 )
10009 len += 2;
10010 }
10011 if (len > 0)
10012 {
10013 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10014 if (arg != NULL)
10015 {
10016 len = 0;
10017 for (p = eap->arg; *p != NUL; ++p)
10018 {
10019 arg[len++] = *p;
10020# ifdef FEAT_GUI
10021 if (*p == CSI)
10022 {
10023 arg[len++] = KS_EXTRA;
10024 arg[len++] = (int)KE_CSI;
10025 }
10026# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010027 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028 {
10029 arg[len++] = *++p;
10030 if (*p == K_SPECIAL)
10031 {
10032 arg[len++] = KS_SPECIAL;
10033 arg[len++] = KE_FILLER;
10034 }
10035# ifdef FEAT_GUI
10036 else if (*p == CSI)
10037 {
10038 arg[len++] = KS_EXTRA;
10039 arg[len++] = (int)KE_CSI;
10040 }
10041# endif
10042 }
10043 arg[len] = NUL;
10044 }
10045 }
10046 }
10047 }
10048#endif
10049
10050 /*
10051 * Save the current typeahead. This is required to allow using ":normal"
10052 * from an event handler and makes sure we don't hang when the argument
10053 * ends with half a command.
10054 */
10055 save_typeahead(&tabuf);
10056 if (tabuf.typebuf_valid)
10057 {
10058 /*
10059 * Repeat the :normal command for each line in the range. When no
10060 * range given, execute it just once, without positioning the cursor
10061 * first.
10062 */
10063 do
10064 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 if (eap->addr_count != 0)
10066 {
10067 curwin->w_cursor.lnum = eap->line1++;
10068 curwin->w_cursor.col = 0;
10069 }
10070
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010071 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072#ifdef FEAT_MBYTE
10073 arg != NULL ? arg :
10074#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010075 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 }
10077 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10078 }
10079
10080 /* Might not return to the main loop when in an event handler. */
10081 update_topline_cursor();
10082
10083 /* Restore the previous typeahead. */
10084 restore_typeahead(&tabuf);
10085
10086 --ex_normal_busy;
10087 msg_scroll = save_msg_scroll;
10088 restart_edit = save_restart_edit;
10089 p_im = save_insertmode;
10090 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010091 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
10093
10094 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +010010095 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +010010097#ifdef FEAT_MOUSE
10098 setmouse();
10099#endif
10100#ifdef CURSOR_SHAPE
10101 ui_cursor_shape(); /* may show different cursor shape */
10102#endif
10103
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104#ifdef FEAT_MBYTE
10105 vim_free(arg);
10106#endif
10107}
10108
10109/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010110 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 */
10112 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010113ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010115 if (eap->forceit)
10116 {
10117 coladvance((colnr_T)MAXCOL);
10118 curwin->w_curswant = MAXCOL;
10119 curwin->w_set_curswant = FALSE;
10120 }
10121
Bram Moolenaara40c5002005-01-09 21:16:21 +000010122 /* Ignore the command when already in Insert mode. Inserting an
10123 * expression register that invokes a function can do this. */
10124 if (State & INSERT)
10125 return;
10126
Bram Moolenaar64969662005-12-14 21:59:55 +000010127 if (eap->cmdidx == CMD_startinsert)
10128 restart_edit = 'a';
10129 else if (eap->cmdidx == CMD_startreplace)
10130 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010132 restart_edit = 'V';
10133
10134 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010136 if (eap->cmdidx == CMD_startinsert)
10137 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 curwin->w_curswant = 0; /* avoid MAXCOL */
10139 }
10140}
10141
10142/*
10143 * ":stopinsert"
10144 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010146ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147{
10148 restart_edit = 0;
10149 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010150 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010153/*
10154 * Execute normal mode command "cmd".
10155 * "remap" can be REMAP_NONE or REMAP_YES.
10156 */
10157 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010158exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010159{
Bram Moolenaar25281632016-01-21 23:32:32 +010010160 /* Stuff the argument into the typeahead buffer. */
10161 ins_typebuf(cmd, remap, 0, TRUE, silent);
10162 exec_normal(FALSE);
10163}
Bram Moolenaar25281632016-01-21 23:32:32 +010010164
Bram Moolenaar25281632016-01-21 23:32:32 +010010165/*
10166 * Execute normal_cmd() until there is no typeahead left.
10167 */
10168 void
10169exec_normal(int was_typed)
10170{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010171 oparg_T oa;
10172
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010173 clear_oparg(&oa);
10174 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010175 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10176 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010177 {
10178 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010179 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010180 }
10181}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010182
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183#ifdef FEAT_FIND_ID
10184 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010185ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186{
10187 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10188 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10189 (linenr_T)1, (linenr_T)MAXLNUM);
10190}
10191
10192#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
10193/*
10194 * ":psearch"
10195 */
10196 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010197ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198{
10199 g_do_tagpreview = p_pvh;
10200 ex_findpat(eap);
10201 g_do_tagpreview = 0;
10202}
10203#endif
10204
10205 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010206ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207{
10208 int whole = TRUE;
10209 long n;
10210 char_u *p;
10211 int action;
10212
10213 switch (cmdnames[eap->cmdidx].cmd_name[2])
10214 {
10215 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10216 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10217 action = ACTION_GOTO;
10218 else
10219 action = ACTION_SHOW;
10220 break;
10221 case 'i': /* ":ilist" and ":dlist" */
10222 action = ACTION_SHOW_ALL;
10223 break;
10224 case 'u': /* ":ijump" and ":djump" */
10225 action = ACTION_GOTO;
10226 break;
10227 default: /* ":isplit" and ":dsplit" */
10228 action = ACTION_SPLIT;
10229 break;
10230 }
10231
10232 n = 1;
10233 if (vim_isdigit(*eap->arg)) /* get count */
10234 {
10235 n = getdigits(&eap->arg);
10236 eap->arg = skipwhite(eap->arg);
10237 }
10238 if (*eap->arg == '/') /* Match regexp, not just whole words */
10239 {
10240 whole = FALSE;
10241 ++eap->arg;
10242 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10243 if (*p)
10244 {
10245 *p++ = NUL;
10246 p = skipwhite(p);
10247
10248 /* Check for trailing illegal characters */
10249 if (!ends_excmd(*p))
10250 eap->errmsg = e_trailing;
10251 else
10252 eap->nextcmd = check_nextcmd(p);
10253 }
10254 }
10255 if (!eap->skip)
10256 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10257 whole, !eap->forceit,
10258 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10259 n, action, eap->line1, eap->line2);
10260}
10261#endif
10262
10263#ifdef FEAT_WINDOWS
10264
10265# ifdef FEAT_QUICKFIX
10266/*
10267 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10268 */
10269 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010270ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010272 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10274}
10275
10276/*
10277 * ":pedit"
10278 */
10279 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010280ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281{
10282 win_T *curwin_save = curwin;
10283
10284 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010285 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286 keep_help_flag = curwin_save->w_buffer->b_help;
10287 do_exedit(eap, NULL);
10288 keep_help_flag = FALSE;
10289 if (curwin != curwin_save && win_valid(curwin_save))
10290 {
10291 /* Return cursor to where we were */
10292 validate_cursor();
10293 redraw_later(VALID);
10294 win_enter(curwin_save, TRUE);
10295 }
10296 g_do_tagpreview = 0;
10297}
10298# endif
10299
10300/*
10301 * ":stag", ":stselect" and ":stjump".
10302 */
10303 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010304ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305{
10306 postponed_split = -1;
10307 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010308 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10310 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010311 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312}
10313#endif
10314
10315/*
10316 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10317 */
10318 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010319ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320{
10321 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10322}
10323
10324 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010325ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326{
10327 int cmd;
10328
10329 switch (name[1])
10330 {
10331 case 'j': cmd = DT_JUMP; /* ":tjump" */
10332 break;
10333 case 's': cmd = DT_SELECT; /* ":tselect" */
10334 break;
10335 case 'p': cmd = DT_PREV; /* ":tprevious" */
10336 break;
10337 case 'N': cmd = DT_PREV; /* ":tNext" */
10338 break;
10339 case 'n': cmd = DT_NEXT; /* ":tnext" */
10340 break;
10341 case 'o': cmd = DT_POP; /* ":pop" */
10342 break;
10343 case 'f': /* ":tfirst" */
10344 case 'r': cmd = DT_FIRST; /* ":trewind" */
10345 break;
10346 case 'l': cmd = DT_LAST; /* ":tlast" */
10347 break;
10348 default: /* ":tag" */
10349#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010350 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351 {
10352 do_cstag(eap);
10353 return;
10354 }
10355#endif
10356 cmd = DT_TAG;
10357 break;
10358 }
10359
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010360 if (name[0] == 'l')
10361 {
10362#ifndef FEAT_QUICKFIX
10363 ex_ni(eap);
10364 return;
10365#else
10366 cmd = DT_LTAG;
10367#endif
10368 }
10369
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10371 eap->forceit, TRUE);
10372}
10373
10374/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010375 * Check "str" for starting with a special cmdline variable.
10376 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10377 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10378 */
10379 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010380find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010381{
10382 int len;
10383 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010384 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010385 "%",
10386#define SPEC_PERC 0
10387 "#",
10388#define SPEC_HASH 1
10389 "<cword>", /* cursor word */
10390#define SPEC_CWORD 2
10391 "<cWORD>", /* cursor WORD */
10392#define SPEC_CCWORD 3
10393 "<cfile>", /* cursor path name */
10394#define SPEC_CFILE 4
10395 "<sfile>", /* ":so" file name */
10396#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010397 "<slnum>", /* ":so" file line number */
10398#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010399#ifdef FEAT_AUTOCMD
10400 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010401# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010402 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010403# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010404 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010405# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010406#endif
10407#ifdef FEAT_CLIENTSERVER
10408 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010409# ifdef FEAT_AUTOCMD
10410# define SPEC_CLIENT 10
10411# else
10412# define SPEC_CLIENT 7
10413# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010414#endif
10415 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010416
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010417 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010418 {
10419 len = (int)STRLEN(spec_str[i]);
10420 if (STRNCMP(src, spec_str[i], len) == 0)
10421 {
10422 *usedlen = len;
10423 return i;
10424 }
10425 }
10426 return -1;
10427}
10428
10429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 * Evaluate cmdline variables.
10431 *
10432 * change '%' to curbuf->b_ffname
10433 * '#' to curwin->w_altfile
10434 * '<cword>' to word under the cursor
10435 * '<cWORD>' to WORD under the cursor
10436 * '<cfile>' to path name under the cursor
10437 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010438 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439 * '<afile>' to file name for autocommand
10440 * '<abuf>' to buffer number for autocommand
10441 * '<amatch>' to matching name for autocommand
10442 *
10443 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10444 * "" for error without a message) and NULL is returned.
10445 * Returns an allocated string if a valid match was found.
10446 * Returns NULL if no match was found. "usedlen" then still contains the
10447 * number of characters to skip.
10448 */
10449 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010450eval_vars(
10451 char_u *src, /* pointer into commandline */
10452 char_u *srcstart, /* beginning of valid memory for src */
10453 int *usedlen, /* characters after src that are used */
10454 linenr_T *lnump, /* line number for :e command, or NULL */
10455 char_u **errormsg, /* pointer to error message */
10456 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010457 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458{
10459 int i;
10460 char_u *s;
10461 char_u *result;
10462 char_u *resultbuf = NULL;
10463 int resultlen;
10464 buf_T *buf;
10465 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10466 int spec_idx;
10467#ifdef FEAT_MODIFY_FNAME
10468 int skip_mod = FALSE;
10469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471
10472 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010473 if (escaped != NULL)
10474 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475
10476 /*
10477 * Check if there is something to do.
10478 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010479 spec_idx = find_cmdline_var(src, usedlen);
10480 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481 {
10482 *usedlen = 1;
10483 return NULL;
10484 }
10485
10486 /*
10487 * Skip when preceded with a backslash "\%" and "\#".
10488 * Note: In "\\%" the % is also not recognized!
10489 */
10490 if (src > srcstart && src[-1] == '\\')
10491 {
10492 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010493 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494 return NULL;
10495 }
10496
10497 /*
10498 * word or WORD under cursor
10499 */
10500 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10501 {
10502 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10503 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10504 if (resultlen == 0)
10505 {
10506 *errormsg = (char_u *)"";
10507 return NULL;
10508 }
10509 }
10510
10511 /*
10512 * '#': Alternate file name
10513 * '%': Current file name
10514 * File name under the cursor
10515 * File name for autocommand
10516 * and following modifiers
10517 */
10518 else
10519 {
10520 switch (spec_idx)
10521 {
10522 case SPEC_PERC: /* '%': current file */
10523 if (curbuf->b_fname == NULL)
10524 {
10525 result = (char_u *)"";
10526 valid = 0; /* Must have ":p:h" to be valid */
10527 }
10528 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530 break;
10531
10532 case SPEC_HASH: /* '#' or "#99": alternate file */
10533 if (src[1] == '#') /* "##": the argument list */
10534 {
10535 result = arg_all();
10536 resultbuf = result;
10537 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010538 if (escaped != NULL)
10539 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540#ifdef FEAT_MODIFY_FNAME
10541 skip_mod = TRUE;
10542#endif
10543 break;
10544 }
10545 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010546 if (*s == '<') /* "#<99" uses v:oldfiles */
10547 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548 i = (int)getdigits(&s);
10549 *usedlen = (int)(s - src); /* length of what we expand */
10550
Bram Moolenaard812df62008-11-09 12:46:09 +000010551 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010553 if (*usedlen < 2)
10554 {
10555 /* Should we give an error message for #<text? */
10556 *usedlen = 1;
10557 return NULL;
10558 }
10559#ifdef FEAT_EVAL
10560 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10561 (long)i);
10562 if (result == NULL)
10563 {
10564 *errormsg = (char_u *)"";
10565 return NULL;
10566 }
10567#else
10568 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010571 }
10572 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010573 {
10574 buf = buflist_findnr(i);
10575 if (buf == NULL)
10576 {
10577 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10578 return NULL;
10579 }
10580 if (lnump != NULL)
10581 *lnump = ECMD_LAST;
10582 if (buf->b_fname == NULL)
10583 {
10584 result = (char_u *)"";
10585 valid = 0; /* Must have ":p:h" to be valid */
10586 }
10587 else
10588 result = buf->b_fname;
10589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590 break;
10591
10592#ifdef FEAT_SEARCHPATH
10593 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010594 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595 if (result == NULL)
10596 {
10597 *errormsg = (char_u *)"";
10598 return NULL;
10599 }
10600 resultbuf = result; /* remember allocated string */
10601 break;
10602#endif
10603
10604#ifdef FEAT_AUTOCMD
10605 case SPEC_AFILE: /* file name for autocommand */
10606 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010607 if (result != NULL && !autocmd_fname_full)
10608 {
10609 /* Still need to turn the fname into a full path. It is
10610 * postponed to avoid a delay when <afile> is not used. */
10611 autocmd_fname_full = TRUE;
10612 result = FullName_save(autocmd_fname, FALSE);
10613 vim_free(autocmd_fname);
10614 autocmd_fname = result;
10615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616 if (result == NULL)
10617 {
10618 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10619 return NULL;
10620 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010621 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622 break;
10623
10624 case SPEC_ABUF: /* buffer number for autocommand */
10625 if (autocmd_bufnr <= 0)
10626 {
10627 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10628 return NULL;
10629 }
10630 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10631 result = strbuf;
10632 break;
10633
10634 case SPEC_AMATCH: /* match name for autocommand */
10635 result = autocmd_match;
10636 if (result == NULL)
10637 {
10638 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10639 return NULL;
10640 }
10641 break;
10642
10643#endif
10644 case SPEC_SFILE: /* file name for ":so" command */
10645 result = sourcing_name;
10646 if (result == NULL)
10647 {
10648 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10649 return NULL;
10650 }
10651 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010652 case SPEC_SLNUM: /* line in file for ":so" command */
10653 if (sourcing_name == NULL || sourcing_lnum == 0)
10654 {
10655 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10656 return NULL;
10657 }
10658 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10659 result = strbuf;
10660 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661#if defined(FEAT_CLIENTSERVER)
10662 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010663 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10664 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665 result = strbuf;
10666 break;
10667#endif
10668 }
10669
10670 resultlen = (int)STRLEN(result); /* length of new string */
10671 if (src[*usedlen] == '<') /* remove the file name extension */
10672 {
10673 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 resultlen = (int)(s - result);
10676 }
10677#ifdef FEAT_MODIFY_FNAME
10678 else if (!skip_mod)
10679 {
10680 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10681 &resultlen);
10682 if (result == NULL)
10683 {
10684 *errormsg = (char_u *)"";
10685 return NULL;
10686 }
10687 }
10688#endif
10689 }
10690
10691 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10692 {
10693 if (valid != VALID_HEAD + VALID_PATH)
10694 /* xgettext:no-c-format */
10695 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10696 else
10697 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10698 result = NULL;
10699 }
10700 else
10701 result = vim_strnsave(result, resultlen);
10702 vim_free(resultbuf);
10703 return result;
10704}
10705
10706/*
10707 * Concatenate all files in the argument list, separated by spaces, and return
10708 * it in one allocated string.
10709 * Spaces and backslashes in the file names are escaped with a backslash.
10710 * Returns NULL when out of memory.
10711 */
10712 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010713arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714{
10715 int len;
10716 int idx;
10717 char_u *retval = NULL;
10718 char_u *p;
10719
10720 /*
10721 * Do this loop two times:
10722 * first time: compute the total length
10723 * second time: concatenate the names
10724 */
10725 for (;;)
10726 {
10727 len = 0;
10728 for (idx = 0; idx < ARGCOUNT; ++idx)
10729 {
10730 p = alist_name(&ARGLIST[idx]);
10731 if (p != NULL)
10732 {
10733 if (len > 0)
10734 {
10735 /* insert a space in between names */
10736 if (retval != NULL)
10737 retval[len] = ' ';
10738 ++len;
10739 }
10740 for ( ; *p != NUL; ++p)
10741 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010742 if (*p == ' '
10743#ifndef BACKSLASH_IN_FILENAME
10744 || *p == '\\'
10745#endif
10746 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747 {
10748 /* insert a backslash */
10749 if (retval != NULL)
10750 retval[len] = '\\';
10751 ++len;
10752 }
10753 if (retval != NULL)
10754 retval[len] = *p;
10755 ++len;
10756 }
10757 }
10758 }
10759
10760 /* second time: break here */
10761 if (retval != NULL)
10762 {
10763 retval[len] = NUL;
10764 break;
10765 }
10766
10767 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010768 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769 if (retval == NULL)
10770 break;
10771 }
10772
10773 return retval;
10774}
10775
10776#if defined(FEAT_AUTOCMD) || defined(PROTO)
10777/*
10778 * Expand the <sfile> string in "arg".
10779 *
10780 * Returns an allocated string, or NULL for any error.
10781 */
10782 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010783expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010784{
10785 char_u *errormsg;
10786 int len;
10787 char_u *result;
10788 char_u *newres;
10789 char_u *repl;
10790 int srclen;
10791 char_u *p;
10792
10793 result = vim_strsave(arg);
10794 if (result == NULL)
10795 return NULL;
10796
10797 for (p = result; *p; )
10798 {
10799 if (STRNCMP(p, "<sfile>", 7) != 0)
10800 ++p;
10801 else
10802 {
10803 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010804 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805 if (errormsg != NULL)
10806 {
10807 if (*errormsg)
10808 emsg(errormsg);
10809 vim_free(result);
10810 return NULL;
10811 }
10812 if (repl == NULL) /* no match (cannot happen) */
10813 {
10814 p += srclen;
10815 continue;
10816 }
10817 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10818 newres = alloc(len);
10819 if (newres == NULL)
10820 {
10821 vim_free(repl);
10822 vim_free(result);
10823 return NULL;
10824 }
10825 mch_memmove(newres, result, (size_t)(p - result));
10826 STRCPY(newres + (p - result), repl);
10827 len = (int)STRLEN(newres);
10828 STRCAT(newres, p + srclen);
10829 vim_free(repl);
10830 vim_free(result);
10831 result = newres;
10832 p = newres + len; /* continue after the match */
10833 }
10834 }
10835
10836 return result;
10837}
10838#endif
10839
10840#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010010841static int ses_winsizes(FILE *fd, int restore_size,
10842 win_T *tab_firstwin);
10843static int ses_win_rec(FILE *fd, frame_T *fr);
10844static frame_T *ses_skipframe(frame_T *fr);
10845static int ses_do_frame(frame_T *fr);
10846static int ses_do_win(win_T *wp);
10847static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
10848static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
10849static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850
10851/*
10852 * Write openfile commands for the current buffers to an .exrc file.
10853 * Return FAIL on error, OK otherwise.
10854 */
10855 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010856makeopens(
10857 FILE *fd,
10858 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859{
10860 buf_T *buf;
10861 int only_save_windows = TRUE;
10862 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863 int restore_size = TRUE;
10864 win_T *wp;
10865 char_u *sname;
10866 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010867 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010868 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010869 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010870 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010871 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010872 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873
10874 if (ssop_flags & SSOP_BUFFERS)
10875 only_save_windows = FALSE; /* Save ALL buffers */
10876
10877 /*
10878 * Begin by setting the this_session variable, and then other
10879 * sessionable variables.
10880 */
10881#ifdef FEAT_EVAL
10882 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10883 return FAIL;
10884 if (ssop_flags & SSOP_GLOBALS)
10885 if (store_session_globals(fd) == FAIL)
10886 return FAIL;
10887#endif
10888
10889 /*
10890 * Close all windows but one.
10891 */
10892 if (put_line(fd, "silent only") == FAIL)
10893 return FAIL;
10894
10895 /*
10896 * Now a :cd command to the session directory or the current directory
10897 */
10898 if (ssop_flags & SSOP_SESDIR)
10899 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010900 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10901 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902 return FAIL;
10903 }
10904 else if (ssop_flags & SSOP_CURDIR)
10905 {
10906 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10907 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010908 || fputs("cd ", fd) < 0
10909 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10910 || put_eol(fd) == FAIL)
10911 {
10912 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915 vim_free(sname);
10916 }
10917
10918 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010919 * If there is an empty, unnamed buffer we will wipe it out later.
10920 * Remember the buffer number.
10921 */
10922 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10923 return FAIL;
10924 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10925 return FAIL;
10926 if (put_line(fd, "endif") == FAIL)
10927 return FAIL;
10928
10929 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930 * Now save the current files, current buffer first.
10931 */
10932 if (put_line(fd, "set shortmess=aoO") == FAIL)
10933 return FAIL;
10934
10935 /* Now put the other buffers into the buffer list */
10936 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10937 {
10938 if (!(only_save_windows && buf->b_nwindows == 0)
10939 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10940 && buf->b_fname != NULL
10941 && buf->b_p_bl)
10942 {
10943 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10944 : buf->b_wininfo->wi_fpos.lnum) < 0
10945 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10946 return FAIL;
10947 }
10948 }
10949
10950 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010951 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10953 return FAIL;
10954
10955 if (ssop_flags & SSOP_RESIZE)
10956 {
10957 /* Note: after the restore we still check it worked!*/
10958 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10959 || put_eol(fd) == FAIL)
10960 return FAIL;
10961 }
10962
10963#ifdef FEAT_GUI
10964 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10965 {
10966 int x, y;
10967
10968 if (gui_mch_get_winpos(&x, &y) == OK)
10969 {
10970 /* Note: after the restore we still check it worked!*/
10971 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10972 return FAIL;
10973 }
10974 }
10975#endif
10976
10977 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010978 * When there are two or more tabpages and 'showtabline' is 1 the tabline
10979 * will be displayed when creating the next tab. That resizes the windows
10980 * in the first tab, which may cause problems. Set 'showtabline' to 2
10981 * temporarily to avoid that.
10982 */
10983 if (p_stal == 1 && first_tabpage->tp_next != NULL)
10984 {
10985 if (put_line(fd, "set stal=2") == FAIL)
10986 return FAIL;
10987 restore_stal = TRUE;
10988 }
10989
10990 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010991 * May repeat putting Windows for each tab, when "tabpages" is in
10992 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010993 * Don't use goto_tabpage(), it may change directory and trigger
10994 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010996 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010997 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010998 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011000 int need_tabnew = FALSE;
11001 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011002
Bram Moolenaar18144c82006-04-12 21:52:12 +000011003 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011004 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011005 tabpage_T *tp = find_tabpage(tabnr);
11006
11007 if (tp == NULL)
11008 break; /* done all tab pages */
11009 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011010 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011011 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011012 tab_topframe = topframe;
11013 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011014 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011015 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011016 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011017 tab_topframe = tp->tp_topframe;
11018 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011019 if (tabnr > 1)
11020 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022
Bram Moolenaar18144c82006-04-12 21:52:12 +000011023 /*
11024 * Before creating the window layout, try loading one file. If this
11025 * is aborted we don't end up with a number of useless windows.
11026 * This may have side effects! (e.g., compressed or network file).
11027 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011028 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011029 {
11030 if (ses_do_win(wp)
11031 && wp->w_buffer->b_ffname != NULL
11032 && !wp->w_buffer->b_help
11033#ifdef FEAT_QUICKFIX
11034 && !bt_nofile(wp->w_buffer)
11035#endif
11036 )
11037 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011038 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000011039 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
11040 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011041 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011042 if (!wp->w_arg_idx_invalid)
11043 edited_win = wp;
11044 break;
11045 }
11046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011048 /* If no file got edited create an empty tab page. */
11049 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11050 return FAIL;
11051
Bram Moolenaar18144c82006-04-12 21:52:12 +000011052 /*
11053 * Save current window layout.
11054 */
11055 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011057 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011059 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11060 return FAIL;
11061 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11062 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011063
Bram Moolenaar18144c82006-04-12 21:52:12 +000011064 /*
11065 * Check if window sizes can be restored (no windows omitted).
11066 * Remember the window number of the current window after restoring.
11067 */
11068 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011069 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011070 {
11071 if (ses_do_win(wp))
11072 ++nr;
11073 else
11074 restore_size = FALSE;
11075 if (curwin == wp)
11076 cnr = nr;
11077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078
Bram Moolenaar18144c82006-04-12 21:52:12 +000011079 /* Go to the first window. */
11080 if (put_line(fd, "wincmd t") == FAIL)
11081 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011082
Bram Moolenaar18144c82006-04-12 21:52:12 +000011083 /*
11084 * If more than one window, see if sizes can be restored.
11085 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11086 * resized when moving between windows.
11087 * Do this before restoring the view, so that the topline and the
11088 * cursor can be set. This is done again below.
11089 */
11090 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
11091 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011092 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011093 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094
Bram Moolenaar18144c82006-04-12 21:52:12 +000011095 /*
11096 * Restore the view of the window (options, file, cursor, etc.).
11097 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011098 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011099 {
11100 if (!ses_do_win(wp))
11101 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011102 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11103 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011104 return FAIL;
11105 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11106 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011107 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011108 }
11109
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011110 /* The argument index in the first tab page is zero, need to set it in
11111 * each window. For further tab pages it's the window where we do
11112 * "tabedit". */
11113 cur_arg_idx = next_arg_idx;
11114
Bram Moolenaar18144c82006-04-12 21:52:12 +000011115 /*
11116 * Restore cursor to the current window if it's not the first one.
11117 */
11118 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11119 || put_eol(fd) == FAIL))
11120 return FAIL;
11121
11122 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011123 * Restore window sizes again after jumping around in windows, because
11124 * the current window has a minimum size while others may not.
11125 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011126 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011127 return FAIL;
11128
Bram Moolenaar18144c82006-04-12 21:52:12 +000011129 /* Don't continue in another tab page when doing only the current one
11130 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011131 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011132 break;
11133 }
11134
11135 if (ssop_flags & SSOP_TABPAGES)
11136 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011137 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11138 || put_eol(fd) == FAIL)
11139 return FAIL;
11140 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011141 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11142 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011143
Bram Moolenaar9c102382006-05-03 21:26:49 +000011144 /*
11145 * Wipe out an empty unnamed buffer we started in.
11146 */
11147 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11148 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011149 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011150 return FAIL;
11151 if (put_line(fd, "endif") == FAIL)
11152 return FAIL;
11153 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11154 return FAIL;
11155
11156 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11157 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11158 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11159 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160
11161 /*
11162 * Lastly, execute the x.vim file if it exists.
11163 */
11164 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11165 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011166 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167 || put_line(fd, "endif") == FAIL)
11168 return FAIL;
11169
11170 return OK;
11171}
11172
11173 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011174ses_winsizes(
11175 FILE *fd,
11176 int restore_size,
11177 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178{
11179 int n = 0;
11180 win_T *wp;
11181
11182 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11183 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011184 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185 {
11186 if (!ses_do_win(wp))
11187 continue;
11188 ++n;
11189
11190 /* restore height when not full height */
11191 if (wp->w_height + wp->w_status_height < topframe->fr_height
11192 && (fprintf(fd,
11193 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11194 n, (long)wp->w_height, Rows / 2, Rows) < 0
11195 || put_eol(fd) == FAIL))
11196 return FAIL;
11197
11198 /* restore width when not full width */
11199 if (wp->w_width < Columns && (fprintf(fd,
11200 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11201 n, (long)wp->w_width, Columns / 2, Columns) < 0
11202 || put_eol(fd) == FAIL))
11203 return FAIL;
11204 }
11205 }
11206 else
11207 {
11208 /* Just equalise window sizes */
11209 if (put_line(fd, "wincmd =") == FAIL)
11210 return FAIL;
11211 }
11212 return OK;
11213}
11214
11215/*
11216 * Write commands to "fd" to recursively create windows for frame "fr",
11217 * horizontally and vertically split.
11218 * After the commands the last window in the frame is the current window.
11219 * Returns FAIL when writing the commands to "fd" fails.
11220 */
11221 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011222ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011223{
11224 frame_T *frc;
11225 int count = 0;
11226
11227 if (fr->fr_layout != FR_LEAF)
11228 {
11229 /* Find first frame that's not skipped and then create a window for
11230 * each following one (first frame is already there). */
11231 frc = ses_skipframe(fr->fr_child);
11232 if (frc != NULL)
11233 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11234 {
11235 /* Make window as big as possible so that we have lots of room
11236 * to split. */
11237 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11238 || put_line(fd, fr->fr_layout == FR_COL
11239 ? "split" : "vsplit") == FAIL)
11240 return FAIL;
11241 ++count;
11242 }
11243
11244 /* Go back to the first window. */
11245 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11246 ? "%dwincmd k" : "%dwincmd h", count) < 0
11247 || put_eol(fd) == FAIL))
11248 return FAIL;
11249
11250 /* Recursively create frames/windows in each window of this column or
11251 * row. */
11252 frc = ses_skipframe(fr->fr_child);
11253 while (frc != NULL)
11254 {
11255 ses_win_rec(fd, frc);
11256 frc = ses_skipframe(frc->fr_next);
11257 /* Go to next window. */
11258 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11259 return FAIL;
11260 }
11261 }
11262 return OK;
11263}
11264
11265/*
11266 * Skip frames that don't contain windows we want to save in the Session.
11267 * Returns NULL when there none.
11268 */
11269 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011270ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271{
11272 frame_T *frc;
11273
11274 for (frc = fr; frc != NULL; frc = frc->fr_next)
11275 if (ses_do_frame(frc))
11276 break;
11277 return frc;
11278}
11279
11280/*
11281 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11282 * the Session.
11283 */
11284 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011285ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286{
11287 frame_T *frc;
11288
11289 if (fr->fr_layout == FR_LEAF)
11290 return ses_do_win(fr->fr_win);
11291 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11292 if (ses_do_frame(frc))
11293 return TRUE;
11294 return FALSE;
11295}
11296
11297/*
11298 * Return non-zero if window "wp" is to be stored in the Session.
11299 */
11300 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011301ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302{
11303 if (wp->w_buffer->b_fname == NULL
11304#ifdef FEAT_QUICKFIX
11305 /* When 'buftype' is "nofile" can't restore the window contents. */
11306 || bt_nofile(wp->w_buffer)
11307#endif
11308 )
11309 return (ssop_flags & SSOP_BLANK);
11310 if (wp->w_buffer->b_help)
11311 return (ssop_flags & SSOP_HELP);
11312 return TRUE;
11313}
11314
11315/*
11316 * Write commands to "fd" to restore the view of a window.
11317 * Caller must make sure 'scrolloff' is zero.
11318 */
11319 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011320put_view(
11321 FILE *fd,
11322 win_T *wp,
11323 int add_edit, /* add ":edit" command to view */
11324 unsigned *flagp, /* vop_flags or ssop_flags */
11325 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011326 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327{
11328 win_T *save_curwin;
11329 int f;
11330 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011331 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332
11333 /* Always restore cursor position for ":mksession". For ":mkview" only
11334 * when 'viewoptions' contains "cursor". */
11335 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11336
11337 /*
11338 * Local argument list.
11339 */
11340 if (wp->w_alist == &global_alist)
11341 {
11342 if (put_line(fd, "argglobal") == FAIL)
11343 return FAIL;
11344 }
11345 else
11346 {
11347 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11348 flagp == &vop_flags
11349 || !(*flagp & SSOP_CURDIR)
11350 || wp->w_localdir != NULL, flagp) == FAIL)
11351 return FAIL;
11352 }
11353
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011354 /* Only when part of a session: restore the argument index. Some
11355 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011356 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011357 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011359 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360 || put_eol(fd) == FAIL)
11361 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011362 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363 }
11364
11365 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011366 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367 {
11368 /*
11369 * Load the file.
11370 */
11371 if (wp->w_buffer->b_ffname != NULL
11372#ifdef FEAT_QUICKFIX
11373 && !bt_nofile(wp->w_buffer)
11374#endif
11375 )
11376 {
11377 /*
11378 * Editing a file in this buffer: use ":edit file".
11379 * This may have side effects! (e.g., compressed or network file).
11380 */
11381 if (fputs("edit ", fd) < 0
11382 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11383 return FAIL;
11384 }
11385 else
11386 {
11387 /* No file in this buffer, just make it empty. */
11388 if (put_line(fd, "enew") == FAIL)
11389 return FAIL;
11390#ifdef FEAT_QUICKFIX
11391 if (wp->w_buffer->b_ffname != NULL)
11392 {
11393 /* The buffer does have a name, but it's not a file name. */
11394 if (fputs("file ", fd) < 0
11395 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11396 return FAIL;
11397 }
11398#endif
11399 do_cursor = FALSE;
11400 }
11401 }
11402
11403 /*
11404 * Local mappings and abbreviations.
11405 */
11406 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11407 && makemap(fd, wp->w_buffer) == FAIL)
11408 return FAIL;
11409
11410 /*
11411 * Local options. Need to go to the window temporarily.
11412 * Store only local values when using ":mkview" and when ":mksession" is
11413 * used and 'sessionoptions' doesn't include "options".
11414 * Some folding options are always stored when "folds" is included,
11415 * otherwise the folds would not be restored correctly.
11416 */
11417 save_curwin = curwin;
11418 curwin = wp;
11419 curbuf = curwin->w_buffer;
11420 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11421 f = makeset(fd, OPT_LOCAL,
11422 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11423#ifdef FEAT_FOLDING
11424 else if (*flagp & SSOP_FOLDS)
11425 f = makefoldset(fd);
11426#endif
11427 else
11428 f = OK;
11429 curwin = save_curwin;
11430 curbuf = curwin->w_buffer;
11431 if (f == FAIL)
11432 return FAIL;
11433
11434#ifdef FEAT_FOLDING
11435 /*
11436 * Save Folds when 'buftype' is empty and for help files.
11437 */
11438 if ((*flagp & SSOP_FOLDS)
11439 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011440# ifdef FEAT_QUICKFIX
11441 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11442# endif
11443 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444 {
11445 if (put_folds(fd, wp) == FAIL)
11446 return FAIL;
11447 }
11448#endif
11449
11450 /*
11451 * Set the cursor after creating folds, since that moves the cursor.
11452 */
11453 if (do_cursor)
11454 {
11455
11456 /* Restore the cursor line in the file and relatively in the
11457 * window. Don't use "G", it changes the jumplist. */
11458 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11459 (long)wp->w_cursor.lnum,
11460 (long)(wp->w_cursor.lnum - wp->w_topline),
11461 (long)wp->w_height / 2, (long)wp->w_height) < 0
11462 || put_eol(fd) == FAIL
11463 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11464 || put_line(fd, "exe s:l") == FAIL
11465 || put_line(fd, "normal! zt") == FAIL
11466 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11467 || put_eol(fd) == FAIL)
11468 return FAIL;
11469 /* Restore the cursor column and left offset when not wrapping. */
11470 if (wp->w_cursor.col == 0)
11471 {
11472 if (put_line(fd, "normal! 0") == FAIL)
11473 return FAIL;
11474 }
11475 else
11476 {
11477 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11478 {
11479 if (fprintf(fd,
11480 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011481 (long)wp->w_virtcol + 1,
11482 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011483 (long)wp->w_width / 2, (long)wp->w_width) < 0
11484 || put_eol(fd) == FAIL
11485 || put_line(fd, "if s:c > 0") == FAIL
11486 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011487 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11488 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011489 || put_eol(fd) == FAIL
11490 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011491 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011492 || put_eol(fd) == FAIL
11493 || put_line(fd, "endif") == FAIL)
11494 return FAIL;
11495 }
11496 else
11497 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011498 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011499 || put_eol(fd) == FAIL)
11500 return FAIL;
11501 }
11502 }
11503 }
11504
11505 /*
11506 * Local directory.
11507 */
11508 if (wp->w_localdir != NULL)
11509 {
11510 if (fputs("lcd ", fd) < 0
11511 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11512 || put_eol(fd) == FAIL)
11513 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011514 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515 }
11516
11517 return OK;
11518}
11519
11520/*
11521 * Write an argument list to the session file.
11522 * Returns FAIL if writing fails.
11523 */
11524 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011525ses_arglist(
11526 FILE *fd,
11527 char *cmd,
11528 garray_T *gap,
11529 int fullname, /* TRUE: use full path name */
11530 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011531{
11532 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011533 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011534 char_u *s;
11535
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011536 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11537 return FAIL;
11538 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011539 return FAIL;
11540 for (i = 0; i < gap->ga_len; ++i)
11541 {
11542 /* NULL file names are skipped (only happens when out of memory). */
11543 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11544 if (s != NULL)
11545 {
11546 if (fullname)
11547 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011548 buf = alloc(MAXPATHL);
11549 if (buf != NULL)
11550 {
11551 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11552 s = buf;
11553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011555 if (fputs("argadd ", fd) < 0
11556 || ses_put_fname(fd, s, flagp) == FAIL
11557 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011558 {
11559 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011560 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011561 }
11562 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011563 }
11564 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011565 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566}
11567
11568/*
11569 * Write a buffer name to the session file.
11570 * Also ends the line.
11571 * Returns FAIL if writing fails.
11572 */
11573 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011574ses_fname(FILE *fd, buf_T *buf, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011575{
11576 char_u *name;
11577
11578 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011579 * the session file will be sourced.
11580 * Don't do this for ":mkview", we don't know the current directory.
11581 * Don't do this after ":lcd", we don't keep track of what the current
11582 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011583 if (buf->b_sfname != NULL
11584 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011585 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011586#ifdef FEAT_AUTOCHDIR
11587 && !p_acd
11588#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011589 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590 name = buf->b_sfname;
11591 else
11592 name = buf->b_ffname;
11593 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11594 return FAIL;
11595 return OK;
11596}
11597
11598/*
11599 * Write a file name to the session file.
11600 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11601 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011602 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603 */
11604 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011605ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011606{
11607 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011608 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011609 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011610
11611 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011612 if (sname == NULL)
11613 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011614
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011615 if (*flagp & SSOP_SLASH)
11616 {
11617 /* change all backslashes to forward slashes */
11618 for (p = sname; *p != NUL; mb_ptr_adv(p))
11619 if (*p == '\\')
11620 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011621 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011622
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011623 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011624 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011626 if (p == NULL)
11627 return FAIL;
11628
11629 /* write the result */
11630 if (fputs((char *)p, fd) < 0)
11631 retval = FAIL;
11632
11633 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011634 return retval;
11635}
11636
11637/*
11638 * ":loadview [nr]"
11639 */
11640 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011641ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011642{
11643 char_u *fname;
11644
11645 fname = get_view_file(*eap->arg);
11646 if (fname != NULL)
11647 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011648 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011649 vim_free(fname);
11650 }
11651}
11652
11653/*
11654 * Get the name of the view file for the current buffer.
11655 */
11656 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011657get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011658{
11659 int len = 0;
11660 char_u *p, *s;
11661 char_u *retval;
11662 char_u *sname;
11663
11664 if (curbuf->b_ffname == NULL)
11665 {
11666 EMSG(_(e_noname));
11667 return NULL;
11668 }
11669 sname = home_replace_save(NULL, curbuf->b_ffname);
11670 if (sname == NULL)
11671 return NULL;
11672
11673 /*
11674 * We want a file name without separators, because we're not going to make
11675 * a directory.
11676 * "normal" path separator -> "=+"
11677 * "=" -> "=="
11678 * ":" path separator -> "=-"
11679 */
11680 for (p = sname; *p; ++p)
11681 if (*p == '=' || vim_ispathsep(*p))
11682 ++len;
11683 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11684 if (retval != NULL)
11685 {
11686 STRCPY(retval, p_vdir);
11687 add_pathsep(retval);
11688 s = retval + STRLEN(retval);
11689 for (p = sname; *p; ++p)
11690 {
11691 if (*p == '=')
11692 {
11693 *s++ = '=';
11694 *s++ = '=';
11695 }
11696 else if (vim_ispathsep(*p))
11697 {
11698 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011699#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011700 if (*p == ':')
11701 *s++ = '-';
11702 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011703#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011704 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011705 }
11706 else
11707 *s++ = *p;
11708 }
11709 *s++ = '=';
11710 *s++ = c;
11711 STRCPY(s, ".vim");
11712 }
11713
11714 vim_free(sname);
11715 return retval;
11716}
11717
11718#endif /* FEAT_SESSION */
11719
11720/*
11721 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11722 * Return FAIL for a write error.
11723 */
11724 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011725put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011726{
11727 if (
11728#ifdef USE_CRNL
11729 (
11730# ifdef MKSESSION_NL
11731 !mksession_nl &&
11732# endif
11733 (putc('\r', fd) < 0)) ||
11734#endif
11735 (putc('\n', fd) < 0))
11736 return FAIL;
11737 return OK;
11738}
11739
11740/*
11741 * Write a line to "fd".
11742 * Return FAIL for a write error.
11743 */
11744 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011745put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011746{
11747 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11748 return FAIL;
11749 return OK;
11750}
11751
11752#ifdef FEAT_VIMINFO
11753/*
11754 * ":rviminfo" and ":wviminfo".
11755 */
11756 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011757ex_viminfo(
11758 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011759{
11760 char_u *save_viminfo;
11761
11762 save_viminfo = p_viminfo;
11763 if (*p_viminfo == NUL)
11764 p_viminfo = (char_u *)"'100";
11765 if (eap->cmdidx == CMD_rviminfo)
11766 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011767 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11768 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011769 EMSG(_("E195: Cannot open viminfo file for reading"));
11770 }
11771 else
11772 write_viminfo(eap->arg, eap->forceit);
11773 p_viminfo = save_viminfo;
11774}
11775#endif
11776
11777#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011778/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011779 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011780 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011781 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011782 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011783dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011784{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011785 if (fname == NULL)
11786 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011787 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011788}
11789#endif
11790
11791/*
11792 * ":behave {mswin,xterm}"
11793 */
11794 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011795ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011796{
11797 if (STRCMP(eap->arg, "mswin") == 0)
11798 {
11799 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11800 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11801 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11802 set_option_value((char_u *)"keymodel", 0L,
11803 (char_u *)"startsel,stopsel", 0);
11804 }
11805 else if (STRCMP(eap->arg, "xterm") == 0)
11806 {
11807 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11808 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11809 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11810 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11811 }
11812 else
11813 EMSG2(_(e_invarg2), eap->arg);
11814}
11815
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011816#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11817/*
11818 * Function given to ExpandGeneric() to obtain the possible arguments of the
11819 * ":behave {mswin,xterm}" command.
11820 */
11821 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011822get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011823{
11824 if (idx == 0)
11825 return (char_u *)"mswin";
11826 if (idx == 1)
11827 return (char_u *)"xterm";
11828 return NULL;
11829}
11830#endif
11831
Bram Moolenaar071d4272004-06-13 20:20:40 +000011832#ifdef FEAT_AUTOCMD
11833static int filetype_detect = FALSE;
11834static int filetype_plugin = FALSE;
11835static int filetype_indent = FALSE;
11836
11837/*
11838 * ":filetype [plugin] [indent] {on,off,detect}"
11839 * on: Load the filetype.vim file to install autocommands for file types.
11840 * off: Load the ftoff.vim file to remove all autocommands for file types.
11841 * plugin on: load filetype.vim and ftplugin.vim
11842 * plugin off: load ftplugof.vim
11843 * indent on: load filetype.vim and indent.vim
11844 * indent off: load indoff.vim
11845 */
11846 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011847ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011848{
11849 char_u *arg = eap->arg;
11850 int plugin = FALSE;
11851 int indent = FALSE;
11852
11853 if (*eap->arg == NUL)
11854 {
11855 /* Print current status. */
11856 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11857 filetype_detect ? "ON" : "OFF",
11858 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11859 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11860 return;
11861 }
11862
11863 /* Accept "plugin" and "indent" in any order. */
11864 for (;;)
11865 {
11866 if (STRNCMP(arg, "plugin", 6) == 0)
11867 {
11868 plugin = TRUE;
11869 arg = skipwhite(arg + 6);
11870 continue;
11871 }
11872 if (STRNCMP(arg, "indent", 6) == 0)
11873 {
11874 indent = TRUE;
11875 arg = skipwhite(arg + 6);
11876 continue;
11877 }
11878 break;
11879 }
11880 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11881 {
11882 if (*arg == 'o' || !filetype_detect)
11883 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011884 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011885 filetype_detect = TRUE;
11886 if (plugin)
11887 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011888 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011889 filetype_plugin = TRUE;
11890 }
11891 if (indent)
11892 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011893 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011894 filetype_indent = TRUE;
11895 }
11896 }
11897 if (*arg == 'd')
11898 {
11899 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011900 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011901 }
11902 }
11903 else if (STRCMP(arg, "off") == 0)
11904 {
11905 if (plugin || indent)
11906 {
11907 if (plugin)
11908 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011909 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011910 filetype_plugin = FALSE;
11911 }
11912 if (indent)
11913 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011914 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011915 filetype_indent = FALSE;
11916 }
11917 }
11918 else
11919 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011920 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921 filetype_detect = FALSE;
11922 }
11923 }
11924 else
11925 EMSG2(_(e_invarg2), arg);
11926}
11927
11928/*
11929 * ":setfiletype {name}"
11930 */
11931 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011932ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011933{
11934 if (!did_filetype)
11935 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11936}
11937#endif
11938
Bram Moolenaar071d4272004-06-13 20:20:40 +000011939 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011940ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011941{
11942#ifdef FEAT_DIGRAPHS
11943 if (*eap->arg != NUL)
11944 putdigraph(eap->arg);
11945 else
11946 listdigraphs();
11947#else
11948 EMSG(_("E196: No digraphs in this version"));
11949#endif
11950}
11951
11952 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011953ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954{
11955 int flags = 0;
11956
11957 if (eap->cmdidx == CMD_setlocal)
11958 flags = OPT_LOCAL;
11959 else if (eap->cmdidx == CMD_setglobal)
11960 flags = OPT_GLOBAL;
11961#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11962 if (cmdmod.browse && flags == 0)
11963 ex_options(eap);
11964 else
11965#endif
11966 (void)do_set(eap->arg, flags);
11967}
11968
11969#ifdef FEAT_SEARCH_EXTRA
11970/*
11971 * ":nohlsearch"
11972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011973 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011974ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010011976 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011977 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011978}
11979
11980/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011981 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011982 * Sets nextcmd to the start of the next command, if any. Also called when
11983 * skipping commands to find the next command.
11984 */
11985 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011986ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011987{
11988 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011989 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990 char_u *end;
11991 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011992 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011993
11994 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011995 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011996 else
11997 {
11998 EMSG(e_invcmd);
11999 return;
12000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012001
12002 /* First clear any old pattern. */
12003 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012004 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005
12006 if (ends_excmd(*eap->arg))
12007 end = eap->arg;
12008 else if ((STRNICMP(eap->arg, "none", 4) == 0
12009 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
12010 end = eap->arg + 4;
12011 else
12012 {
12013 p = skiptowhite(eap->arg);
12014 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012015 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012016 p = skipwhite(p);
12017 if (*p == NUL)
12018 {
12019 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012020 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012021 EMSG2(_(e_invarg2), eap->arg);
12022 return;
12023 }
12024 end = skip_regexp(p + 1, *p, TRUE, NULL);
12025 if (!eap->skip)
12026 {
12027 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12028 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012029 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012030 eap->errmsg = e_trailing;
12031 return;
12032 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012033 if (*end != *p)
12034 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012035 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012036 EMSG2(_(e_invarg2), p);
12037 return;
12038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012039
12040 c = *end;
12041 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012042 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012043 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012044 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012045 }
12046 }
12047 eap->nextcmd = find_nextcmd(end);
12048}
12049#endif
12050
12051#ifdef FEAT_CRYPT
12052/*
12053 * ":X": Get crypt key
12054 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012055 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012056ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012058 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012059 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012060}
12061#endif
12062
12063#ifdef FEAT_FOLDING
12064 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012065ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012066{
12067 if (foldManualAllowed(TRUE))
12068 foldCreate(eap->line1, eap->line2);
12069}
12070
12071 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012072ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073{
12074 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12075 eap->forceit, FALSE);
12076}
12077
12078 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012079ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080{
12081 linenr_T lnum;
12082
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012083#ifdef FEAT_CLIPBOARD
12084 start_global_changes();
12085#endif
12086
Bram Moolenaar071d4272004-06-13 20:20:40 +000012087 /* First set the marks for all lines closed/open. */
12088 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12089 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12090 ml_setmarked(lnum);
12091
12092 /* Execute the command on the marked lines. */
12093 global_exe(eap->arg);
12094 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012095#ifdef FEAT_CLIPBOARD
12096 end_global_changes();
12097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098}
12099#endif