blob: ad4ba7c4e547dca4b5324b899c01d244a8e1d774 [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
Bram Moolenaar2d358992016-06-12 21:20:54 +0200477# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478# define ex_changes ex_ni
479#endif
480
Bram Moolenaar05159a02005-02-26 23:04:13 +0000481#ifndef FEAT_PROFILE
482# define ex_profile ex_ni
483#endif
484
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485/*
486 * Declare cmdnames[].
487 */
488#define DO_DECLARE_EXCMD
489#include "ex_cmds.h"
490
491/*
492 * Table used to quickly search for a command, based on its first character.
493 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000494static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495{
496 CMD_append,
497 CMD_buffer,
498 CMD_change,
499 CMD_delete,
500 CMD_edit,
501 CMD_file,
502 CMD_global,
503 CMD_help,
504 CMD_insert,
505 CMD_join,
506 CMD_k,
507 CMD_list,
508 CMD_move,
509 CMD_next,
510 CMD_open,
511 CMD_print,
512 CMD_quit,
513 CMD_read,
514 CMD_substitute,
515 CMD_t,
516 CMD_undo,
517 CMD_vglobal,
518 CMD_write,
519 CMD_xit,
520 CMD_yank,
521 CMD_z,
522 CMD_bang
523};
524
525static char_u dollar_command[2] = {'$', 0};
526
527
528#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000529/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530typedef struct
531{
532 char_u *line; /* command line */
533 linenr_T lnum; /* sourcing_lnum of the line */
534} wcmd_T;
535
536/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000537 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000539 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000541struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542{
543 garray_T *lines_gap; /* growarray with line info */
544 int current_line; /* last read line from growarray */
545 int repeating; /* TRUE when looping a second time */
546 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100547 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 void *cookie;
549};
550
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100551static char_u *get_loop_line(int c, void *cookie, int indent);
552static int store_loop_line(garray_T *gap, char_u *line);
553static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000554
555/* Struct to save a few things while debugging. Used in do_cmdline() only. */
556struct dbg_stuff
557{
558 int trylevel;
559 int force_abort;
560 except_T *caught_stack;
561 char_u *vv_exception;
562 char_u *vv_throwpoint;
563 int did_emsg;
564 int got_int;
565 int did_throw;
566 int need_rethrow;
567 int check_cstack;
568 except_T *current_exception;
569};
570
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100571static void save_dbg_stuff(struct dbg_stuff *dsp);
572static void restore_dbg_stuff(struct dbg_stuff *dsp);
Bram Moolenaared203462004-06-16 11:19:22 +0000573
574 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100575save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000576{
577 dsp->trylevel = trylevel; trylevel = 0;
578 dsp->force_abort = force_abort; force_abort = FALSE;
579 dsp->caught_stack = caught_stack; caught_stack = NULL;
580 dsp->vv_exception = v_exception(NULL);
581 dsp->vv_throwpoint = v_throwpoint(NULL);
582
583 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
584 dsp->did_emsg = did_emsg; did_emsg = FALSE;
585 dsp->got_int = got_int; got_int = FALSE;
586 dsp->did_throw = did_throw; did_throw = FALSE;
587 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
588 dsp->check_cstack = check_cstack; check_cstack = FALSE;
589 dsp->current_exception = current_exception; current_exception = NULL;
590}
591
592 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100593restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000594{
595 suppress_errthrow = FALSE;
596 trylevel = dsp->trylevel;
597 force_abort = dsp->force_abort;
598 caught_stack = dsp->caught_stack;
599 (void)v_exception(dsp->vv_exception);
600 (void)v_throwpoint(dsp->vv_throwpoint);
601 did_emsg = dsp->did_emsg;
602 got_int = dsp->got_int;
603 did_throw = dsp->did_throw;
604 need_rethrow = dsp->need_rethrow;
605 check_cstack = dsp->check_cstack;
606 current_exception = dsp->current_exception;
607}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608#endif
609
610
611/*
612 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
613 * command is given.
614 */
615 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100616do_exmode(
617 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{
619 int save_msg_scroll;
620 int prev_msg_row;
621 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000622 int changedtick;
623
624 if (improved)
625 exmode_active = EXMODE_VIM;
626 else
627 exmode_active = EXMODE_NORMAL;
628 State = NORMAL;
629
630 /* When using ":global /pat/ visual" and then "Q" we return to continue
631 * the :global command. */
632 if (global_busy)
633 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634
635 save_msg_scroll = msg_scroll;
636 ++RedrawingDisabled; /* don't redisplay the window */
637 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638#ifdef FEAT_GUI
639 /* Ignore scrollbar and mouse events in Ex mode */
640 ++hold_gui_events;
641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642
643 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
644 while (exmode_active)
645 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000646 /* Check for a ":normal" command and no more characters left. */
647 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
648 {
649 exmode_active = FALSE;
650 break;
651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 msg_scroll = TRUE;
653 need_wait_return = FALSE;
654 ex_pressedreturn = FALSE;
655 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000656 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 prev_msg_row = msg_row;
658 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 if (improved)
660 {
661 cmdline_row = msg_row;
662 do_cmdline(NULL, getexline, NULL, 0);
663 }
664 else
665 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
666 lines_left = Rows - 1;
667
Bram Moolenaardf177f62005-02-22 08:39:57 +0000668 if ((prev_line != curwin->w_cursor.lnum
669 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000671 if (curbuf->b_ml.ml_flags & ML_EMPTY)
672 EMSG(_(e_emptybuf));
673 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000675 if (ex_pressedreturn)
676 {
677 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000678 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000679 msg_row = prev_msg_row;
680 if (prev_msg_row == Rows - 1)
681 msg_row--;
682 }
683 msg_col = 0;
684 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
685 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000688 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
689 {
690 if (curbuf->b_ml.ml_flags & ML_EMPTY)
691 EMSG(_(e_emptybuf));
692 else
693 EMSG(_("E501: At end-of-file"));
694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 }
696
697#ifdef FEAT_GUI
698 --hold_gui_events;
699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 --RedrawingDisabled;
701 --no_wait_return;
702 update_screen(CLEAR);
703 need_wait_return = FALSE;
704 msg_scroll = save_msg_scroll;
705}
706
707/*
708 * Execute a simple command line. Used for translated commands like "*".
709 */
710 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100711do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712{
713 return do_cmdline(cmd, NULL, NULL,
714 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
715}
716
717/*
718 * do_cmdline(): execute one Ex command line
719 *
720 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100721 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 * 2. Split up in parts separated with '|'.
723 *
724 * This function can be called recursively!
725 *
726 * flags:
727 * DOCMD_VERBOSE - The command will be included in the error message.
728 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100729 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 * DOCMD_KEYTYPED - Don't reset KeyTyped.
731 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
732 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
733 *
734 * return FAIL if cmdline could not be executed, OK otherwise
735 */
736 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100737do_cmdline(
738 char_u *cmdline,
739 char_u *(*fgetline)(int, void *, int),
740 void *cookie, /* argument for fgetline() */
741 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742{
743 char_u *next_cmdline; /* next cmd to execute */
744 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100745 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 static int recursive = 0; /* recursive depth */
747 int msg_didout_before_start = 0;
748 int count = 0; /* line number count */
749 int did_inc = FALSE; /* incremented RedrawingDisabled */
750 int retval = OK;
751#ifdef FEAT_EVAL
752 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000753 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 int current_line = 0; /* active line in lines_ga */
755 char_u *fname = NULL; /* function or script name */
756 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
757 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000758 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 int initial_trylevel;
760 struct msglist **saved_msg_list = NULL;
761 struct msglist *private_msg_list;
762
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100763 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100764 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000766 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000768 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100770# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771# define cmd_cookie cookie
772#endif
773 static int call_depth = 0; /* recursiveness */
774
775#ifdef FEAT_EVAL
776 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
777 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000778 * This ensures that the do_errthrow() call in do_one_cmd() does not
779 * combine the messages stored by an earlier invocation of do_one_cmd()
780 * with the command name of the later one. This would happen when
781 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 saved_msg_list = msg_list;
783 msg_list = &private_msg_list;
784 private_msg_list = NULL;
785#endif
786
787 /* It's possible to create an endless loop with ":execute", catch that
788 * here. The value of 200 allows nested function calls, ":source", etc. */
789 if (call_depth == 200)
790 {
791 EMSG(_("E169: Command too recursive"));
792#ifdef FEAT_EVAL
793 /* When converting to an exception, we do not include the command name
794 * since this is not an error of the specific command. */
795 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
796 msg_list = saved_msg_list;
797#endif
798 return FAIL;
799 }
800 ++call_depth;
801
802#ifdef FEAT_EVAL
803 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000804 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 cstack.cs_trylevel = 0;
806 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000807 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
809
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100810 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811
812 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100813 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000814 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 ++ex_nesting_level;
816
817 /* Get the function or script name and the address where the next breakpoint
818 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000819 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 {
821 fname = func_name(real_cookie);
822 breakpoint = func_breakpoint(real_cookie);
823 dbg_tick = func_dbg_tick(real_cookie);
824 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100825 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 {
827 fname = sourcing_name;
828 breakpoint = source_breakpoint(real_cookie);
829 dbg_tick = source_dbg_tick(real_cookie);
830 }
831
832 /*
833 * Initialize "force_abort" and "suppress_errthrow" at the top level.
834 */
835 if (!recursive)
836 {
837 force_abort = FALSE;
838 suppress_errthrow = FALSE;
839 }
840
841 /*
842 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000843 * exception handling (used when debugging). Otherwise clear it to avoid
844 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000846 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000847 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000848 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200849 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850
851 initial_trylevel = trylevel;
852
853 /*
854 * "did_throw" will be set to TRUE when an exception is being thrown.
855 */
856 did_throw = FALSE;
857#endif
858 /*
859 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000860 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 * If force_abort is set, we cancel everything.
862 */
863 did_emsg = FALSE;
864
865 /*
866 * KeyTyped is only set when calling vgetc(). Reset it here when not
867 * calling vgetc() (sourced command lines).
868 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100869 if (!(flags & DOCMD_KEYTYPED)
870 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 KeyTyped = FALSE;
872
873 /*
874 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000875 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 * - for multiple commands on one line, separated with '|'
877 * - when repeating until there are no more lines (for ":source")
878 */
879 next_cmdline = cmdline;
880 do
881 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000882#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100883 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000884#endif
885
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000886 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 if (next_cmdline == NULL
888#ifdef FEAT_EVAL
889 && !force_abort
890 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000891 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892#endif
893 )
894 did_emsg = FALSE;
895
896 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000897 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100898 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 * 3. If a line is given: Make a copy, so we can mess with it.
900 */
901
902#ifdef FEAT_EVAL
903 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000904 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 {
906 /* Each '|' separated command is stored separately in lines_ga, to
907 * be able to jump to it. Don't use next_cmdline now. */
908 vim_free(cmdline_copy);
909 cmdline_copy = NULL;
910
911 /* Check if a function has returned or, unless it has an unclosed
912 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000913 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000915# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000916 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000917 func_line_end(real_cookie);
918# endif
919 if (func_has_ended(real_cookie))
920 {
921 retval = FAIL;
922 break;
923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000925#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000926 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100927 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000928 script_line_end();
929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930
931 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100932 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 {
934 retval = FAIL;
935 break;
936 }
937
938 /* If breakpoints have been added/deleted need to check for it. */
939 if (breakpoint != NULL && dbg_tick != NULL
940 && *dbg_tick != debug_tick)
941 {
942 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100943 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 fname, sourcing_lnum);
945 *dbg_tick = debug_tick;
946 }
947
948 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
949 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
950
951 /* Did we encounter a breakpoint? */
952 if (breakpoint != NULL && *breakpoint != 0
953 && *breakpoint <= sourcing_lnum)
954 {
955 dbg_breakpoint(fname, sourcing_lnum);
956 /* Find next breakpoint. */
957 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100958 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 fname, sourcing_lnum);
960 *dbg_tick = debug_tick;
961 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000962# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000963 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000964 {
965 if (getline_is_func)
966 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100967 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000968 script_line_start();
969 }
970# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 }
972
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000973 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000975 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100976 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 * below, so that it stores lines in or reads them from
978 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000979 * while/for loop. */
980 cmd_getline = get_loop_line;
981 cmd_cookie = (void *)&cmd_loop_cookie;
982 cmd_loop_cookie.lines_gap = &lines_ga;
983 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100984 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000985 cmd_loop_cookie.cookie = cookie;
986 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 }
988 else
989 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100990 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 cmd_cookie = cookie;
992 }
993#endif
994
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100995 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 if (next_cmdline == NULL)
997 {
998 /*
999 * Need to set msg_didout for the first line after an ":if",
1000 * otherwise the ":if" will be overwritten.
1001 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001002 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001004 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005#ifdef FEAT_EVAL
1006 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1007#else
1008 0
1009#endif
1010 )) == NULL)
1011 {
1012 /* Don't call wait_return for aborted command line. The NULL
1013 * returned for the end of a sourced file or executed function
1014 * doesn't do this. */
1015 if (KeyTyped && !(flags & DOCMD_REPEAT))
1016 need_wait_return = FALSE;
1017 retval = FAIL;
1018 break;
1019 }
1020 used_getline = TRUE;
1021
1022 /*
1023 * Keep the first typed line. Clear it when more lines are typed.
1024 */
1025 if (flags & DOCMD_KEEPLINE)
1026 {
1027 vim_free(repeat_cmdline);
1028 if (count == 0)
1029 repeat_cmdline = vim_strsave(next_cmdline);
1030 else
1031 repeat_cmdline = NULL;
1032 }
1033 }
1034
1035 /* 3. Make a copy of the command so we can mess with it. */
1036 else if (cmdline_copy == NULL)
1037 {
1038 next_cmdline = vim_strsave(next_cmdline);
1039 if (next_cmdline == NULL)
1040 {
1041 EMSG(_(e_outofmem));
1042 retval = FAIL;
1043 break;
1044 }
1045 }
1046 cmdline_copy = next_cmdline;
1047
1048#ifdef FEAT_EVAL
1049 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001050 * Save the current line when inside a ":while" or ":for", and when
1051 * the command looks like a ":while" or ":for", because we may need it
1052 * later. When there is a '|' and another command, it is stored
1053 * separately, because we need to be able to jump back to it from an
1054 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001056 if (current_line == lines_ga.ga_len
1057 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001059 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 {
1061 retval = FAIL;
1062 break;
1063 }
1064 }
1065 did_endif = FALSE;
1066#endif
1067
1068 if (count++ == 0)
1069 {
1070 /*
1071 * All output from the commands is put below each other, without
1072 * waiting for a return. Don't do this when executing commands
1073 * from a script or when being called recursive (e.g. for ":e
1074 * +command file").
1075 */
1076 if (!(flags & DOCMD_NOWAIT) && !recursive)
1077 {
1078 msg_didout_before_start = msg_didout;
1079 msg_didany = FALSE; /* no output yet */
1080 msg_start();
1081 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001082 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 ++RedrawingDisabled;
1084 did_inc = TRUE;
1085 }
1086 }
1087
1088 if (p_verbose >= 15 && sourcing_name != NULL)
1089 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001091 verbose_enter_scroll();
1092
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 smsg((char_u *)_("line %ld: %s"),
1094 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001095 if (msg_silent == 0)
1096 msg_puts((char_u *)"\n"); /* don't overwrite this */
1097
1098 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 --no_wait_return;
1100 }
1101
1102 /*
1103 * 2. Execute one '|' separated command.
1104 * do_one_cmd() will return NULL if there is no trailing '|'.
1105 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1106 */
1107 ++recursive;
1108 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1109#ifdef FEAT_EVAL
1110 &cstack,
1111#endif
1112 cmd_getline, cmd_cookie);
1113 --recursive;
1114
1115#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001116 if (cmd_cookie == (void *)&cmd_loop_cookie)
1117 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001119 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120#endif
1121
1122 if (next_cmdline == NULL)
1123 {
1124 vim_free(cmdline_copy);
1125 cmdline_copy = NULL;
1126#ifdef FEAT_CMDHIST
1127 /*
1128 * If the command was typed, remember it for the ':' register.
1129 * Do this AFTER executing the command to make :@: work.
1130 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001131 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 && new_last_cmdline != NULL)
1133 {
1134 vim_free(last_cmdline);
1135 last_cmdline = new_last_cmdline;
1136 new_last_cmdline = NULL;
1137 }
1138#endif
1139 }
1140 else
1141 {
1142 /* need to copy the command after the '|' to cmdline_copy, for the
1143 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001144 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 next_cmdline = cmdline_copy;
1146 }
1147
1148
1149#ifdef FEAT_EVAL
1150 /* reset did_emsg for a function that is not aborted by an error */
1151 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001152 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 && !func_has_abort(real_cookie))
1154 did_emsg = FALSE;
1155
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001156 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 {
1158 ++current_line;
1159
1160 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001161 * An ":endwhile", ":endfor" and ":continue" is handled here.
1162 * If we were executing commands, jump back to the ":while" or
1163 * ":for".
1164 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001166 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001168 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001170 /* Jump back to the matching ":while" or ":for". Be careful
1171 * not to use a cs_line[] from an entry that isn't a ":while"
1172 * or ":for": It would make "current_line" invalid and can
1173 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 if (!did_emsg && !got_int && !did_throw
1175 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001176 && (cstack.cs_flags[cstack.cs_idx]
1177 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 && cstack.cs_line[cstack.cs_idx] >= 0
1179 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1180 {
1181 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001182 /* remember we jumped there */
1183 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 line_breakcheck(); /* check if CTRL-C typed */
1185
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001186 /* Check for the next breakpoint at or after the ":while"
1187 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 if (breakpoint != NULL)
1189 {
1190 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001191 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 fname,
1193 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1194 *dbg_tick = debug_tick;
1195 }
1196 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001197 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001199 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001201 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1202 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 }
1204 }
1205
1206 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001207 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001209 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001211 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1213 }
1214 }
1215
1216 /*
1217 * When not inside any ":while" loop, clear remembered lines.
1218 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001219 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 {
1221 if (lines_ga.ga_len > 0)
1222 {
1223 sourcing_lnum =
1224 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1225 free_cmdlines(&lines_ga);
1226 }
1227 current_line = 0;
1228 }
1229
1230 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001231 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1232 * being restored at the ":endtry". Reset them here and set the
1233 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1234 * This includes the case where a missing ":endif", ":endwhile" or
1235 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001237 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001239 cstack.cs_lflags &= ~CSL_HAD_FINA;
1240 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1241 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 did_throw ? (void *)current_exception : NULL);
1243 did_emsg = got_int = did_throw = FALSE;
1244 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1245 }
1246
1247 /* Update global "trylevel" for recursive calls to do_cmdline() from
1248 * within this loop. */
1249 trylevel = initial_trylevel + cstack.cs_trylevel;
1250
1251 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001252 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 * files) is aborted because of an error, an interrupt, or an uncaught
1254 * exception, cancel everything. If it is left normally, reset
1255 * force_abort to get the non-EH compatible abortion behavior for
1256 * the rest of the script.
1257 */
1258 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1259 force_abort = FALSE;
1260
1261 /* Convert an interrupt to an exception if appropriate. */
1262 (void)do_intthrow(&cstack);
1263#endif /* FEAT_EVAL */
1264
1265 }
1266 /*
1267 * Continue executing command lines when:
1268 * - no CTRL-C typed, no aborting error, no exception thrown or try
1269 * conditionals need to be checked for executing finally clauses or
1270 * catching an interrupt exception
1271 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001272 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 * looping for ":source" command or function call.
1274 */
1275 while (!((got_int
1276#ifdef FEAT_EVAL
1277 || (did_emsg && force_abort) || did_throw
1278#endif
1279 )
1280#ifdef FEAT_EVAL
1281 && cstack.cs_trylevel == 0
1282#endif
1283 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001284 && !(did_emsg
1285#ifdef FEAT_EVAL
1286 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001287 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001288 * the :endtry to be missed. */
1289 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1290#endif
1291 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001292 && (getline_equal(fgetline, cookie, getexmodeline)
1293 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 && (next_cmdline != NULL
1295#ifdef FEAT_EVAL
1296 || cstack.cs_idx >= 0
1297#endif
1298 || (flags & DOCMD_REPEAT)));
1299
1300 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001301 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302#ifdef FEAT_EVAL
1303 free_cmdlines(&lines_ga);
1304 ga_clear(&lines_ga);
1305
1306 if (cstack.cs_idx >= 0)
1307 {
1308 /*
1309 * If a sourced file or executed function ran to its end, report the
1310 * unclosed conditional.
1311 */
1312 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001313 && ((getline_equal(fgetline, cookie, getsourceline)
1314 && !source_finished(fgetline, cookie))
1315 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 && !func_has_ended(real_cookie))))
1317 {
1318 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1319 EMSG(_(e_endtry));
1320 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1321 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001322 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1323 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 else
1325 EMSG(_(e_endif));
1326 }
1327
1328 /*
1329 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1330 * ":endtry" in a sourced file or executed function. If the try
1331 * conditional is in its finally clause, ignore anything pending.
1332 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001333 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 */
1335 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001336 {
1337 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1338
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001339 if (idx >= 0)
1340 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001341 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1342 &cstack.cs_looplevel);
1343 }
1344 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 trylevel = initial_trylevel;
1346 }
1347
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001348 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1349 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001351 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 ? (char_u *)"endfunction" : (char_u *)NULL);
1353
1354 if (trylevel == 0)
1355 {
1356 /*
1357 * When an exception is being thrown out of the outermost try
1358 * conditional, discard the uncaught exception, disable the conversion
1359 * of interrupts or errors to exceptions, and ensure that no more
1360 * commands are executed.
1361 */
1362 if (did_throw)
1363 {
1364 void *p = NULL;
1365 char_u *saved_sourcing_name;
1366 int saved_sourcing_lnum;
1367 struct msglist *messages = NULL, *next;
1368
1369 /*
1370 * If the uncaught exception is a user exception, report it as an
1371 * error. If it is an error exception, display the saved error
1372 * message now. For an interrupt exception, do nothing; the
1373 * interrupt message is given elsewhere.
1374 */
1375 switch (current_exception->type)
1376 {
1377 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001378 vim_snprintf((char *)IObuff, IOSIZE,
1379 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 current_exception->value);
1381 p = vim_strsave(IObuff);
1382 break;
1383 case ET_ERROR:
1384 messages = current_exception->messages;
1385 current_exception->messages = NULL;
1386 break;
1387 case ET_INTERRUPT:
1388 break;
1389 default:
1390 p = vim_strsave((char_u *)_(e_internal));
1391 }
1392
1393 saved_sourcing_name = sourcing_name;
1394 saved_sourcing_lnum = sourcing_lnum;
1395 sourcing_name = current_exception->throw_name;
1396 sourcing_lnum = current_exception->throw_lnum;
1397 current_exception->throw_name = NULL;
1398
1399 discard_current_exception(); /* uses IObuff if 'verbose' */
1400 suppress_errthrow = TRUE;
1401 force_abort = TRUE;
1402
1403 if (messages != NULL)
1404 {
1405 do
1406 {
1407 next = messages->next;
1408 emsg(messages->msg);
1409 vim_free(messages->msg);
1410 vim_free(messages);
1411 messages = next;
1412 }
1413 while (messages != NULL);
1414 }
1415 else if (p != NULL)
1416 {
1417 emsg(p);
1418 vim_free(p);
1419 }
1420 vim_free(sourcing_name);
1421 sourcing_name = saved_sourcing_name;
1422 sourcing_lnum = saved_sourcing_lnum;
1423 }
1424
1425 /*
1426 * On an interrupt or an aborting error not converted to an exception,
1427 * disable the conversion of errors to exceptions. (Interrupts are not
1428 * converted any more, here.) This enables also the interrupt message
1429 * when force_abort is set and did_emsg unset in case of an interrupt
1430 * from a finally clause after an error.
1431 */
1432 else if (got_int || (did_emsg && force_abort))
1433 suppress_errthrow = TRUE;
1434 }
1435
1436 /*
1437 * The current cstack will be freed when do_cmdline() returns. An uncaught
1438 * exception will have to be rethrown in the previous cstack. If a function
1439 * has just returned or a script file was just finished and the previous
1440 * cstack belongs to the same function or, respectively, script file, it
1441 * will have to be checked for finally clauses to be executed due to the
1442 * ":return" or ":finish". This is done in do_one_cmd().
1443 */
1444 if (did_throw)
1445 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001446 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001448 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 && ex_nesting_level > func_level(real_cookie) + 1))
1450 {
1451 if (!did_throw)
1452 check_cstack = TRUE;
1453 }
1454 else
1455 {
1456 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001457 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 --ex_nesting_level;
1459 /*
1460 * Go to debug mode when returning from a function in which we are
1461 * single-stepping.
1462 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001463 if ((getline_equal(fgetline, cookie, getsourceline)
1464 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001466 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 ? (char_u *)_("End of sourced file")
1468 : (char_u *)_("End of function"));
1469 }
1470
1471 /*
1472 * Restore the exception environment (done after returning from the
1473 * debugger).
1474 */
1475 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001476 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477
1478 msg_list = saved_msg_list;
1479#endif /* FEAT_EVAL */
1480
1481 /*
1482 * If there was too much output to fit on the command line, ask the user to
1483 * hit return before redrawing the screen. With the ":global" command we do
1484 * this only once after the command is finished.
1485 */
1486 if (did_inc)
1487 {
1488 --RedrawingDisabled;
1489 --no_wait_return;
1490 msg_scroll = FALSE;
1491
1492 /*
1493 * When just finished an ":if"-":else" which was typed, no need to
1494 * wait for hit-return. Also for an error situation.
1495 */
1496 if (retval == FAIL
1497#ifdef FEAT_EVAL
1498 || (did_endif && KeyTyped && !did_emsg)
1499#endif
1500 )
1501 {
1502 need_wait_return = FALSE;
1503 msg_didany = FALSE; /* don't wait when restarting edit */
1504 }
1505 else if (need_wait_return)
1506 {
1507 /*
1508 * The msg_start() above clears msg_didout. The wait_return we do
1509 * here should not overwrite the command that may be shown before
1510 * doing that.
1511 */
1512 msg_didout |= msg_didout_before_start;
1513 wait_return(FALSE);
1514 }
1515 }
1516
Bram Moolenaar2a942252012-11-28 23:03:07 +01001517#ifdef FEAT_EVAL
1518 did_endif = FALSE; /* in case do_cmdline used recursively */
1519#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 /*
1521 * Reset if_level, in case a sourced script file contains more ":if" than
1522 * ":endif" (could be ":if x | foo | endif").
1523 */
1524 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001525#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001526
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 --call_depth;
1528 return retval;
1529}
1530
1531#ifdef FEAT_EVAL
1532/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001533 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 */
1535 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001536get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001538 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 wcmd_T *wp;
1540 char_u *line;
1541
1542 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1543 {
1544 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001545 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001547 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 if (cp->getline == NULL)
1549 line = getcmdline(c, 0L, indent);
1550 else
1551 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001552 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 ++cp->current_line;
1554
1555 return line;
1556 }
1557
1558 KeyTyped = FALSE;
1559 ++cp->current_line;
1560 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1561 sourcing_lnum = wp->lnum;
1562 return vim_strsave(wp->line);
1563}
1564
1565/*
1566 * Store a line in "gap" so that a ":while" loop can execute it again.
1567 */
1568 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001569store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570{
1571 if (ga_grow(gap, 1) == FAIL)
1572 return FAIL;
1573 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1574 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1575 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 return OK;
1577}
1578
1579/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001580 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 */
1582 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001583free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584{
1585 while (gap->ga_len > 0)
1586 {
1587 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1588 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 }
1590}
1591#endif
1592
1593/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001594 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1595 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001598getline_equal(
1599 char_u *(*fgetline)(int, void *, int),
1600 void *cookie UNUSED, /* argument for fgetline() */
1601 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602{
1603#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001604 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001605 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606
Bram Moolenaar89d40322006-08-29 15:30:07 +00001607 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001608 * function that's originally used to obtain the lines. This may be
1609 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001610 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001611 cp = (struct loop_cookie *)cookie;
1612 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 {
1614 gp = cp->getline;
1615 cp = cp->cookie;
1616 }
1617 return gp == func;
1618#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001619 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620#endif
1621}
1622
1623#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1624/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001625 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 * getline function. Otherwise return "cookie".
1627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001629getline_cookie(
1630 char_u *(*fgetline)(int, void *, int) UNUSED,
1631 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632{
1633# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001634 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001635 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
Bram Moolenaar89d40322006-08-29 15:30:07 +00001637 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001638 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001640 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001641 cp = (struct loop_cookie *)cookie;
1642 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 {
1644 gp = cp->getline;
1645 cp = cp->cookie;
1646 }
1647 return cp;
1648# else
1649 return cookie;
1650# endif
1651}
1652#endif
1653
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001654
1655/*
1656 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1657 * ":bwipeout", etc.
1658 * Returns the buffer number.
1659 */
1660 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001661compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001662{
1663 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001664 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001665 int count = offset;
1666
1667 buf = firstbuf;
1668 while (buf->b_next != NULL && buf->b_fnum < lnum)
1669 buf = buf->b_next;
1670 while (count != 0)
1671 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001672 count += (offset < 0) ? 1 : -1;
1673 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1674 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001675 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001676 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001677 if (addr_type == ADDR_LOADED_BUFFERS)
1678 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001679 while (buf->b_ml.ml_mfp == NULL)
1680 {
1681 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1682 if (nextbuf == NULL)
1683 break;
1684 buf = nextbuf;
1685 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001686 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001687 /* we might have gone too far, last buffer is not loadedd */
1688 if (addr_type == ADDR_LOADED_BUFFERS)
1689 while (buf->b_ml.ml_mfp == NULL)
1690 {
1691 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1692 if (nextbuf == NULL)
1693 break;
1694 buf = nextbuf;
1695 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001696 return buf->b_fnum;
1697}
1698
Bram Moolenaarf240e182014-11-27 18:33:02 +01001699#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001700static int current_win_nr(win_T *win);
1701static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001702
1703 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001704current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001705{
1706 win_T *wp;
1707 int nr = 0;
1708
1709 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1710 {
1711 ++nr;
1712 if (wp == win)
1713 break;
1714 }
1715 return nr;
1716}
1717
1718 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001719current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001720{
1721 tabpage_T *tp;
1722 int nr = 0;
1723
1724 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
1725 {
1726 ++nr;
1727 if (tp == tab)
1728 break;
1729 }
1730 return nr;
1731}
1732
1733# define CURRENT_WIN_NR current_win_nr(curwin)
1734# define LAST_WIN_NR current_win_nr(NULL)
1735# define CURRENT_TAB_NR current_tab_nr(curtab)
1736# define LAST_TAB_NR current_tab_nr(NULL)
1737#else
1738# define CURRENT_WIN_NR 1
1739# define LAST_WIN_NR 1
1740# define CURRENT_TAB_NR 1
1741# define LAST_TAB_NR 1
1742#endif
1743
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001744
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745/*
1746 * Execute one Ex command.
1747 *
1748 * If 'sourcing' is TRUE, the command will be included in the error message.
1749 *
1750 * 1. skip comment lines and leading space
1751 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001752 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001753 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001754 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001755 * 6. parse arguments
1756 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001758 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 *
1760 * This function may be called recursively!
1761 */
1762#if (_MSC_VER == 1200)
1763/*
Bram Moolenaared203462004-06-16 11:19:22 +00001764 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001766 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767#endif
1768 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001769do_one_cmd(
1770 char_u **cmdlinep,
1771 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001773 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001775 char_u *(*fgetline)(int, void *, int),
1776 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777{
1778 char_u *p;
1779 linenr_T lnum;
1780 long n;
1781 char_u *errormsg = NULL; /* error message */
1782 exarg_T ea; /* Ex command arguments */
1783 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001784 int save_msg_scroll = msg_scroll;
1785 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001787#ifdef HAVE_SANDBOX
1788 int did_sandbox = FALSE;
1789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 cmdmod_T save_cmdmod;
1791 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001792 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793
1794 vim_memset(&ea, 0, sizeof(ea));
1795 ea.line1 = 1;
1796 ea.line2 = 1;
1797#ifdef FEAT_EVAL
1798 ++ex_nesting_level;
1799#endif
1800
Bram Moolenaar21691f82012-12-05 19:13:18 +01001801 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 if (quitmore
1803#ifdef FEAT_EVAL
1804 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001805 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001806#endif
1807#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001808 /* avoid that an autocommand, e.g. QuitPre, does this */
1809 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810#endif
1811 )
1812 --quitmore;
1813
1814 /*
1815 * Reset browse, confirm, etc.. They are restored when returning, for
1816 * recursive calls.
1817 */
1818 save_cmdmod = cmdmod;
1819 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1820
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001821 /* "#!anything" is handled like a comment. */
1822 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1823 goto doend;
1824
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 /*
1826 * Repeat until no more command modifiers are found.
1827 */
1828 ea.cmd = *cmdlinep;
1829 for (;;)
1830 {
1831/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001832 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 */
1834 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1835 ++ea.cmd;
1836
1837 /* in ex mode, an empty line works like :+ */
1838 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001839 && (getline_equal(fgetline, cookie, getexmodeline)
1840 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001841 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 {
1843 ea.cmd = (char_u *)"+";
1844 ex_pressedreturn = TRUE;
1845 }
1846
1847 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001848 if (*ea.cmd == '"')
1849 goto doend;
1850 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001851 {
1852 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855
1856/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001857 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 */
1859 p = ea.cmd;
1860 if (VIM_ISDIGIT(*ea.cmd))
1861 p = skipwhite(skipdigits(ea.cmd));
1862 switch (*p)
1863 {
1864 /* When adding an entry, also modify cmd_exists(). */
1865 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1866 break;
1867#ifdef FEAT_WINDOWS
1868 cmdmod.split |= WSP_ABOVE;
1869#endif
1870 continue;
1871
1872 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1873 {
1874#ifdef FEAT_WINDOWS
1875 cmdmod.split |= WSP_BELOW;
1876#endif
1877 continue;
1878 }
1879 if (checkforcmd(&ea.cmd, "browse", 3))
1880 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001881#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 cmdmod.browse = TRUE;
1883#endif
1884 continue;
1885 }
1886 if (!checkforcmd(&ea.cmd, "botright", 2))
1887 break;
1888#ifdef FEAT_WINDOWS
1889 cmdmod.split |= WSP_BOT;
1890#endif
1891 continue;
1892
1893 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1894 break;
1895#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1896 cmdmod.confirm = TRUE;
1897#endif
1898 continue;
1899
1900 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1901 {
1902 cmdmod.keepmarks = TRUE;
1903 continue;
1904 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001905 if (checkforcmd(&ea.cmd, "keepalt", 5))
1906 {
1907 cmdmod.keepalt = TRUE;
1908 continue;
1909 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001910 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1911 {
1912 cmdmod.keeppatterns = TRUE;
1913 continue;
1914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1916 break;
1917 cmdmod.keepjumps = TRUE;
1918 continue;
1919
1920 /* ":hide" and ":hide | cmd" are not modifiers */
1921 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1922 || *p == NUL || ends_excmd(*p))
1923 break;
1924 ea.cmd = p;
1925 cmdmod.hide = TRUE;
1926 continue;
1927
1928 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1929 {
1930 cmdmod.lockmarks = TRUE;
1931 continue;
1932 }
1933
1934 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1935 break;
1936#ifdef FEAT_WINDOWS
1937 cmdmod.split |= WSP_ABOVE;
1938#endif
1939 continue;
1940
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001941 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001942 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001943#ifdef FEAT_AUTOCMD
1944 if (cmdmod.save_ei == NULL)
1945 {
1946 /* Set 'eventignore' to "all". Restore the
1947 * existing option value later. */
1948 cmdmod.save_ei = vim_strsave(p_ei);
1949 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001950 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001951 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001952#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001953 continue;
1954 }
1955 if (!checkforcmd(&ea.cmd, "noswapfile", 6))
1956 break;
1957 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001958 continue;
1959
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1961 break;
1962#ifdef FEAT_WINDOWS
1963 cmdmod.split |= WSP_BELOW;
1964#endif
1965 continue;
1966
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001967 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1968 {
1969#ifdef HAVE_SANDBOX
1970 if (!did_sandbox)
1971 ++sandbox;
1972 did_sandbox = TRUE;
1973#endif
1974 continue;
1975 }
1976 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001978 if (save_msg_silent == -1)
1979 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1982 {
1983 /* ":silent!", but not "silent !cmd" */
1984 ea.cmd = skipwhite(ea.cmd + 1);
1985 ++emsg_silent;
1986 ++did_esilent;
1987 }
1988 continue;
1989
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001990 case 't': if (checkforcmd(&p, "tab", 3))
1991 {
1992#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001993 if (vim_isdigit(*ea.cmd))
1994 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1995 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001996 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001997 ea.cmd = p;
1998#endif
1999 continue;
2000 }
2001 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 break;
2003#ifdef FEAT_WINDOWS
2004 cmdmod.split |= WSP_TOP;
2005#endif
2006 continue;
2007
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002008 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
2009 break;
2010 if (save_msg_silent == -1)
2011 save_msg_silent = msg_silent;
2012 msg_silent = 0;
2013 continue;
2014
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
2016 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002017#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 cmdmod.split |= WSP_VERT;
2019#endif
2020 continue;
2021 }
2022 if (!checkforcmd(&p, "verbose", 4))
2023 break;
2024 if (verbose_save < 0)
2025 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00002026 if (vim_isdigit(*ea.cmd))
2027 p_verbose = atoi((char *)ea.cmd);
2028 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 p_verbose = 1;
2030 ea.cmd = p;
2031 continue;
2032 }
2033 break;
2034 }
2035
2036#ifdef FEAT_EVAL
2037 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2038 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2039#else
2040 ea.skip = (if_level > 0);
2041#endif
2042
2043#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002044# ifdef FEAT_PROFILE
2045 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002046 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002047 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002048 if (getline_equal(fgetline, cookie, get_func_line))
2049 func_line_exec(getline_cookie(fgetline, cookie));
2050 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002051 script_line_exec();
2052 }
2053#endif
2054
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 /* May go to debug mode. If this happens and the ">quit" debug command is
2056 * used, throw an interrupt exception and skip the next command. */
2057 dbg_check_breakpoint(&ea);
2058 if (!ea.skip && got_int)
2059 {
2060 ea.skip = TRUE;
2061 (void)do_intthrow(cstack);
2062 }
2063#endif
2064
2065/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002066 * 3. Skip over the range to find the command. Let "p" point to after it.
2067 *
2068 * We need the command to know what kind of range it uses.
2069 */
2070 cmd = ea.cmd;
2071 ea.cmd = skip_range(ea.cmd, NULL);
2072 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2073 ea.cmd = skipwhite(ea.cmd + 1);
2074 p = find_command(&ea, NULL);
2075
2076/*
2077 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 *
2079 * where 'addr' is:
2080 *
2081 * % (entire file)
2082 * $ [+-NUM]
2083 * 'x [+-NUM] (where x denotes a currently defined mark)
2084 * . [+-NUM]
2085 * [+-NUM]..
2086 * NUM
2087 *
2088 * The ea.cmd pointer is updated to point to the first character following the
2089 * range spec. If an initial address is found, but no second, the upper bound
2090 * is equal to the lower.
2091 */
2092
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002093 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002094 if (!IS_USER_CMDIDX(ea.cmdidx))
2095 {
2096 if (ea.cmdidx != CMD_SIZE)
2097 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2098 else
2099 ea.addr_type = ADDR_LINES;
2100
2101#ifdef FEAT_WINDOWS
2102 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002103 if (ea.cmdidx == CMD_wincmd && p != NULL)
2104 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002105#endif
2106 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002107
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002109 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 for (;;)
2111 {
2112 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002113 switch (ea.addr_type)
2114 {
2115 case ADDR_LINES:
2116 /* default is current line number */
2117 ea.line2 = curwin->w_cursor.lnum;
2118 break;
2119 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002120 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002121 ea.line2 = lnum;
2122 break;
2123 case ADDR_ARGUMENTS:
2124 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002125 if (ea.line2 > ARGCOUNT)
2126 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002127 break;
2128 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002129 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002130 ea.line2 = curbuf->b_fnum;
2131 break;
2132 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002133 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002134 ea.line2 = lnum;
2135 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002136#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002137 case ADDR_QUICKFIX:
2138 ea.line2 = qf_get_cur_valid_idx(&ea);
2139 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002140#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002143 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip,
2144 ea.addr_count == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 if (ea.cmd == NULL) /* error detected */
2146 goto doend;
2147 if (lnum == MAXLNUM)
2148 {
2149 if (*ea.cmd == '%') /* '%' - all lines */
2150 {
2151 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002152 switch (ea.addr_type)
2153 {
2154 case ADDR_LINES:
2155 ea.line1 = 1;
2156 ea.line2 = curbuf->b_ml.ml_line_count;
2157 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002158 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002159 {
2160 buf_T *buf = firstbuf;
2161
2162 while (buf->b_next != NULL
2163 && buf->b_ml.ml_mfp == NULL)
2164 buf = buf->b_next;
2165 ea.line1 = buf->b_fnum;
2166 buf = lastbuf;
2167 while (buf->b_prev != NULL
2168 && buf->b_ml.ml_mfp == NULL)
2169 buf = buf->b_prev;
2170 ea.line2 = buf->b_fnum;
2171 break;
2172 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002173 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002174 ea.line1 = firstbuf->b_fnum;
2175 ea.line2 = lastbuf->b_fnum;
2176 break;
2177 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002178 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002179 if (IS_USER_CMDIDX(ea.cmdidx))
2180 {
2181 ea.line1 = 1;
2182 ea.line2 = ea.addr_type == ADDR_WINDOWS
2183 ? LAST_WIN_NR : LAST_TAB_NR;
2184 }
2185 else
2186 {
2187 /* there is no Vim command which uses '%' and
2188 * ADDR_WINDOWS or ADDR_TABS */
2189 errormsg = (char_u *)_(e_invrange);
2190 goto doend;
2191 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002192 break;
2193 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002194 if (ARGCOUNT == 0)
2195 ea.line1 = ea.line2 = 0;
2196 else
2197 {
2198 ea.line1 = 1;
2199 ea.line2 = ARGCOUNT;
2200 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002201 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002202#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002203 case ADDR_QUICKFIX:
2204 ea.line1 = 1;
2205 ea.line2 = qf_get_size(&ea);
2206 if (ea.line2 == 0)
2207 ea.line2 = 1;
2208 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002209#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 ++ea.addr_count;
2212 }
2213 /* '*' - visual area */
2214 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2215 {
2216 pos_T *fp;
2217
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002218 if (ea.addr_type != ADDR_LINES)
2219 {
2220 errormsg = (char_u *)_(e_invrange);
2221 goto doend;
2222 }
2223
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 ++ea.cmd;
2225 if (!ea.skip)
2226 {
2227 fp = getmark('<', FALSE);
2228 if (check_mark(fp) == FAIL)
2229 goto doend;
2230 ea.line1 = fp->lnum;
2231 fp = getmark('>', FALSE);
2232 if (check_mark(fp) == FAIL)
2233 goto doend;
2234 ea.line2 = fp->lnum;
2235 ++ea.addr_count;
2236 }
2237 }
2238 }
2239 else
2240 ea.line2 = lnum;
2241 ea.addr_count++;
2242
2243 if (*ea.cmd == ';')
2244 {
2245 if (!ea.skip)
2246 curwin->w_cursor.lnum = ea.line2;
2247 }
2248 else if (*ea.cmd != ',')
2249 break;
2250 ++ea.cmd;
2251 }
2252
2253 /* One address given: set start and end lines */
2254 if (ea.addr_count == 1)
2255 {
2256 ea.line1 = ea.line2;
2257 /* ... but only implicit: really no address given */
2258 if (lnum == MAXLNUM)
2259 ea.addr_count = 0;
2260 }
2261
2262 /* Don't leave the cursor on an illegal line (caused by ';') */
2263 check_cursor_lnum();
2264
2265/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002266 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 */
2268
2269 /*
2270 * Skip ':' and any white space
2271 */
2272 ea.cmd = skipwhite(ea.cmd);
2273 while (*ea.cmd == ':')
2274 ea.cmd = skipwhite(ea.cmd + 1);
2275
2276 /*
2277 * If we got a line, but no command, then go to the line.
2278 * If we find a '|' or '\n' we set ea.nextcmd.
2279 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002280 if (*ea.cmd == NUL || *ea.cmd == '"'
2281 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 {
2283 /*
2284 * strange vi behaviour:
2285 * ":3" jumps to line 3
2286 * ":3|..." prints line 3
2287 * ":|" prints current line
2288 */
2289 if (ea.skip) /* skip this if inside :if */
2290 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002291 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 {
2293 ea.cmdidx = CMD_print;
2294 ea.argt = RANGE+COUNT+TRLBAR;
2295 if ((errormsg = invalid_range(&ea)) == NULL)
2296 {
2297 correct_range(&ea);
2298 ex_print(&ea);
2299 }
2300 }
2301 else if (ea.addr_count != 0)
2302 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002303 if (ea.line2 > curbuf->b_ml.ml_line_count)
2304 {
2305 /* With '-' in 'cpoptions' a line number past the file is an
2306 * error, otherwise put it at the end of the file. */
2307 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2308 ea.line2 = -1;
2309 else
2310 ea.line2 = curbuf->b_ml.ml_line_count;
2311 }
2312
2313 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002314 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 else
2316 {
2317 if (ea.line2 == 0)
2318 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 else
2320 curwin->w_cursor.lnum = ea.line2;
2321 beginline(BL_SOL | BL_FIX);
2322 }
2323 }
2324 goto doend;
2325 }
2326
Bram Moolenaard5005162014-08-22 23:05:54 +02002327#ifdef FEAT_AUTOCMD
2328 /* If this looks like an undefined user command and there are CmdUndefined
2329 * autocommands defined, trigger the matching autocommands. */
2330 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2331 && ASCII_ISUPPER(*ea.cmd)
2332 && has_cmdundefined())
2333 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002334 int ret;
2335
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002336 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002337 while (ASCII_ISALNUM(*p))
2338 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002339 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002340 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2341 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002342 /* If the autocommands did something and didn't cause an error, try
2343 * finding the command again. */
2344 p = (ret && !aborting()) ? find_command(&ea, NULL) : NULL;
Bram Moolenaard5005162014-08-22 23:05:54 +02002345 }
2346#endif
2347
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348#ifdef FEAT_USR_CMDS
2349 if (p == NULL)
2350 {
2351 if (!ea.skip)
2352 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2353 goto doend;
2354 }
2355 /* Check for wrong commands. */
2356 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2357 {
2358 errormsg = uc_fun_cmd();
2359 goto doend;
2360 }
2361#endif
2362 if (ea.cmdidx == CMD_SIZE)
2363 {
2364 if (!ea.skip)
2365 {
2366 STRCPY(IObuff, _("E492: Not an editor command"));
2367 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002368 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002370 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 }
2372 goto doend;
2373 }
2374
Bram Moolenaar958636c2014-10-21 20:01:58 +02002375 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2376 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002377#ifdef HAVE_EX_SCRIPT_NI
2378 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2379#endif
2380 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381
2382#ifndef FEAT_EVAL
2383 /*
2384 * When the expression evaluation is disabled, recognize the ":if" and
2385 * ":endif" commands and ignore everything in between it.
2386 */
2387 if (ea.cmdidx == CMD_if)
2388 ++if_level;
2389 if (if_level)
2390 {
2391 if (ea.cmdidx == CMD_endif)
2392 --if_level;
2393 goto doend;
2394 }
2395
2396#endif
2397
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002398 /* forced commands */
2399 if (*p == '!' && ea.cmdidx != CMD_substitute
2400 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 {
2402 ++p;
2403 ea.forceit = TRUE;
2404 }
2405 else
2406 ea.forceit = FALSE;
2407
2408/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002409 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002411 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002412 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413
2414 if (!ea.skip)
2415 {
2416#ifdef HAVE_SANDBOX
2417 if (sandbox != 0 && !(ea.argt & SBOXOK))
2418 {
2419 /* Command not allowed in sandbox. */
2420 errormsg = (char_u *)_(e_sandbox);
2421 goto doend;
2422 }
2423#endif
2424 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2425 {
2426 /* Command not allowed in non-'modifiable' buffer */
2427 errormsg = (char_u *)_(e_modifiable);
2428 goto doend;
2429 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002430
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002431 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002432 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002434 /* Command not allowed when editing the command line. */
2435#ifdef FEAT_CMDWIN
2436 if (cmdwin_type != 0)
2437 errormsg = (char_u *)_(e_cmdwin);
2438 else
2439#endif
2440 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 goto doend;
2442 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002443#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002444 /* Disallow editing another buffer when "curbuf_lock" is set.
2445 * Do allow ":edit" (check for argument later).
2446 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002447 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002448 && ea.cmdidx != CMD_edit
2449 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002450 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002451 && curbuf_locked())
2452 goto doend;
2453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454
2455 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2456 {
2457 /* no range allowed */
2458 errormsg = (char_u *)_(e_norange);
2459 goto doend;
2460 }
2461 }
2462
2463 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2464 {
2465 errormsg = (char_u *)_(e_nobang);
2466 goto doend;
2467 }
2468
2469 /*
2470 * Don't complain about the range if it is not used
2471 * (could happen if line_count is accidentally set to 0).
2472 */
2473 if (!ea.skip && !ni)
2474 {
2475 /*
2476 * If the range is backwards, ask for confirmation and, if given, swap
2477 * ea.line1 & ea.line2 so it's forwards again.
2478 * When global command is busy, don't ask, will fail below.
2479 */
2480 if (!global_busy && ea.line1 > ea.line2)
2481 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002482 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002484 if (sourcing || exmode_active)
2485 {
2486 errormsg = (char_u *)_("E493: Backwards range given");
2487 goto doend;
2488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 if (ask_yesno((char_u *)
2490 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002491 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 }
2493 lnum = ea.line1;
2494 ea.line1 = ea.line2;
2495 ea.line2 = lnum;
2496 }
2497 if ((errormsg = invalid_range(&ea)) != NULL)
2498 goto doend;
2499 }
2500
2501 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2502 ea.line2 = 1;
2503
2504 correct_range(&ea);
2505
2506#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002507 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2508 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 {
2510 /* Put the first line at the start of a closed fold, put the last line
2511 * at the end of a closed fold. */
2512 (void)hasFolding(ea.line1, &ea.line1, NULL);
2513 (void)hasFolding(ea.line2, NULL, &ea.line2);
2514 }
2515#endif
2516
2517#ifdef FEAT_QUICKFIX
2518 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002519 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 * option here, so things like % get expanded.
2521 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002522 p = replace_makeprg(&ea, p, cmdlinep);
2523 if (p == NULL)
2524 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525#endif
2526
2527 /*
2528 * Skip to start of argument.
2529 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2530 */
2531 if (ea.cmdidx == CMD_bang)
2532 ea.arg = p;
2533 else
2534 ea.arg = skipwhite(p);
2535
2536 /*
2537 * Check for "++opt=val" argument.
2538 * Must be first, allow ":w ++enc=utf8 !cmd"
2539 */
2540 if (ea.argt & ARGOPT)
2541 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2542 if (getargopt(&ea) == FAIL && !ni)
2543 {
2544 errormsg = (char_u *)_(e_invarg);
2545 goto doend;
2546 }
2547
2548 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2549 {
2550 if (*ea.arg == '>') /* append */
2551 {
2552 if (*++ea.arg != '>') /* typed wrong */
2553 {
2554 errormsg = (char_u *)_("E494: Use w or w>>");
2555 goto doend;
2556 }
2557 ea.arg = skipwhite(ea.arg + 1);
2558 ea.append = TRUE;
2559 }
2560 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2561 {
2562 ++ea.arg;
2563 ea.usefilter = TRUE;
2564 }
2565 }
2566
2567 if (ea.cmdidx == CMD_read)
2568 {
2569 if (ea.forceit)
2570 {
2571 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2572 ea.forceit = FALSE;
2573 }
2574 else if (*ea.arg == '!') /* :r !filter */
2575 {
2576 ++ea.arg;
2577 ea.usefilter = TRUE;
2578 }
2579 }
2580
2581 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2582 {
2583 ea.amount = 1;
2584 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2585 {
2586 ++ea.arg;
2587 ++ea.amount;
2588 }
2589 ea.arg = skipwhite(ea.arg);
2590 }
2591
2592 /*
2593 * Check for "+command" argument, before checking for next command.
2594 * Don't do this for ":read !cmd" and ":write !cmd".
2595 */
2596 if ((ea.argt & EDITCMD) && !ea.usefilter)
2597 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2598
2599 /*
2600 * Check for '|' to separate commands and '"' to start comments.
2601 * Don't do this for ":read !cmd" and ":write !cmd".
2602 */
2603 if ((ea.argt & TRLBAR) && !ea.usefilter)
2604 separate_nextcmd(&ea);
2605
2606 /*
2607 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002608 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2609 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002611 else if (ea.cmdidx == CMD_bang
2612 || ea.cmdidx == CMD_global
2613 || ea.cmdidx == CMD_vglobal
2614 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 {
2616 for (p = ea.arg; *p; ++p)
2617 {
2618 /* Remove one backslash before a newline, so that it's possible to
2619 * pass a newline to the shell and also a newline that is preceded
2620 * with a backslash. This makes it impossible to end a shell
2621 * command in a backslash, but that doesn't appear useful.
2622 * Halving the number of backslashes is incompatible with previous
2623 * versions. */
2624 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002625 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 else if (*p == '\n')
2627 {
2628 ea.nextcmd = p + 1;
2629 *p = NUL;
2630 break;
2631 }
2632 }
2633 }
2634
2635 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2636 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002637 buf_T *buf;
2638
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002640 switch (ea.addr_type)
2641 {
2642 case ADDR_LINES:
2643 ea.line2 = curbuf->b_ml.ml_line_count;
2644 break;
2645 case ADDR_LOADED_BUFFERS:
2646 buf = firstbuf;
2647 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2648 buf = buf->b_next;
2649 ea.line1 = buf->b_fnum;
2650 buf = lastbuf;
2651 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2652 buf = buf->b_prev;
2653 ea.line2 = buf->b_fnum;
2654 break;
2655 case ADDR_BUFFERS:
2656 ea.line1 = firstbuf->b_fnum;
2657 ea.line2 = lastbuf->b_fnum;
2658 break;
2659 case ADDR_WINDOWS:
2660 ea.line2 = LAST_WIN_NR;
2661 break;
2662 case ADDR_TABS:
2663 ea.line2 = LAST_TAB_NR;
2664 break;
2665 case ADDR_ARGUMENTS:
2666 if (ARGCOUNT == 0)
2667 ea.line1 = ea.line2 = 0;
2668 else
2669 ea.line2 = ARGCOUNT;
2670 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002671#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002672 case ADDR_QUICKFIX:
2673 ea.line2 = qf_get_size(&ea);
2674 if (ea.line2 == 0)
2675 ea.line2 = 1;
2676 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002677#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 }
2680
2681 /* accept numbered register only when no count allowed (:put) */
2682 if ( (ea.argt & REGSTR)
2683 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002684 /* Do not allow register = for user commands */
2685 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2687 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002688#ifndef FEAT_CLIPBOARD
2689 /* check these explicitly for a more specific error message */
2690 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002692 errormsg = (char_u *)_(e_invalidreg);
2693 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 }
2695#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002696 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2697 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002698 {
2699 ea.regname = *ea.arg++;
2700#ifdef FEAT_EVAL
2701 /* for '=' register: accept the rest of the line as an expression */
2702 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2703 {
2704 set_expr_line(vim_strsave(ea.arg));
2705 ea.arg += STRLEN(ea.arg);
2706 }
2707#endif
2708 ea.arg = skipwhite(ea.arg);
2709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 }
2711
2712 /*
2713 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2714 * count, it's a buffer name.
2715 */
2716 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2717 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2718 || vim_iswhite(*p)))
2719 {
2720 n = getdigits(&ea.arg);
2721 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002722 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 {
2724 errormsg = (char_u *)_(e_zerocount);
2725 goto doend;
2726 }
2727 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2728 {
2729 ea.line2 = n;
2730 if (ea.addr_count == 0)
2731 ea.addr_count = 1;
2732 }
2733 else
2734 {
2735 ea.line1 = ea.line2;
2736 ea.line2 += n - 1;
2737 ++ea.addr_count;
2738 /*
2739 * Be vi compatible: no error message for out of range.
2740 */
2741 if (ea.line2 > curbuf->b_ml.ml_line_count)
2742 ea.line2 = curbuf->b_ml.ml_line_count;
2743 }
2744 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002745
2746 /*
2747 * Check for flags: 'l', 'p' and '#'.
2748 */
2749 if (ea.argt & EXFLAGS)
2750 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002752 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002753 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 {
2755 errormsg = (char_u *)_(e_trailing);
2756 goto doend;
2757 }
2758
2759 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2760 {
2761 errormsg = (char_u *)_(e_argreq);
2762 goto doend;
2763 }
2764
2765#ifdef FEAT_EVAL
2766 /*
2767 * Skip the command when it's not going to be executed.
2768 * The commands like :if, :endif, etc. always need to be executed.
2769 * Also make an exception for commands that handle a trailing command
2770 * themselves.
2771 */
2772 if (ea.skip)
2773 {
2774 switch (ea.cmdidx)
2775 {
2776 /* commands that need evaluation */
2777 case CMD_while:
2778 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002779 case CMD_for:
2780 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 case CMD_if:
2782 case CMD_elseif:
2783 case CMD_else:
2784 case CMD_endif:
2785 case CMD_try:
2786 case CMD_catch:
2787 case CMD_finally:
2788 case CMD_endtry:
2789 case CMD_function:
2790 break;
2791
2792 /* Commands that handle '|' themselves. Check: A command should
2793 * either have the TRLBAR flag, appear in this list or appear in
2794 * the list at ":help :bar". */
2795 case CMD_aboveleft:
2796 case CMD_and:
2797 case CMD_belowright:
2798 case CMD_botright:
2799 case CMD_browse:
2800 case CMD_call:
2801 case CMD_confirm:
2802 case CMD_delfunction:
2803 case CMD_djump:
2804 case CMD_dlist:
2805 case CMD_dsearch:
2806 case CMD_dsplit:
2807 case CMD_echo:
2808 case CMD_echoerr:
2809 case CMD_echomsg:
2810 case CMD_echon:
2811 case CMD_execute:
2812 case CMD_help:
2813 case CMD_hide:
2814 case CMD_ijump:
2815 case CMD_ilist:
2816 case CMD_isearch:
2817 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002818 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 case CMD_keepjumps:
2820 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002821 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 case CMD_leftabove:
2823 case CMD_let:
2824 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002825 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002827 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002828 case CMD_noautocmd:
2829 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 case CMD_perl:
2831 case CMD_psearch:
2832 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002833 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002834 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 case CMD_return:
2836 case CMD_rightbelow:
2837 case CMD_ruby:
2838 case CMD_silent:
2839 case CMD_smagic:
2840 case CMD_snomagic:
2841 case CMD_substitute:
2842 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002843 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 case CMD_tcl:
2845 case CMD_throw:
2846 case CMD_tilde:
2847 case CMD_topleft:
2848 case CMD_unlet:
2849 case CMD_verbose:
2850 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002851 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 break;
2853
2854 default: goto doend;
2855 }
2856 }
2857#endif
2858
2859 if (ea.argt & XFILE)
2860 {
2861 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2862 goto doend;
2863 }
2864
2865#ifdef FEAT_LISTCMDS
2866 /*
2867 * Accept buffer name. Cannot be used at the same time with a buffer
2868 * number. Don't do this for a user command.
2869 */
2870 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002871 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 {
2873 /*
2874 * :bdelete, :bwipeout and :bunload take several arguments, separated
2875 * by spaces: find next space (skipping over escaped characters).
2876 * The others take one argument: ignore trailing spaces.
2877 */
2878 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2879 || ea.cmdidx == CMD_bunload)
2880 p = skiptowhite_esc(ea.arg);
2881 else
2882 {
2883 p = ea.arg + STRLEN(ea.arg);
2884 while (p > ea.arg && vim_iswhite(p[-1]))
2885 --p;
2886 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002887 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2888 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 if (ea.line2 < 0) /* failed */
2890 goto doend;
2891 ea.addr_count = 1;
2892 ea.arg = skipwhite(p);
2893 }
2894#endif
2895
2896/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002897 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 *
2899 * The "ea" structure holds the arguments that can be used.
2900 */
2901 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002902 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 ea.cookie = cookie;
2904#ifdef FEAT_EVAL
2905 ea.cstack = cstack;
2906#endif
2907
2908#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002909 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 {
2911 /*
2912 * Execute a user-defined command.
2913 */
2914 do_ucmd(&ea);
2915 }
2916 else
2917#endif
2918 {
2919 /*
2920 * Call the function to execute the command.
2921 */
2922 ea.errmsg = NULL;
2923 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2924 if (ea.errmsg != NULL)
2925 errormsg = (char_u *)_(ea.errmsg);
2926 }
2927
2928#ifdef FEAT_EVAL
2929 /*
2930 * If the command just executed called do_cmdline(), any throw or ":return"
2931 * or ":finish" encountered there must also check the cstack of the still
2932 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2933 * exception, or reanimate a returned function or finished script file and
2934 * return or finish it again.
2935 */
2936 if (need_rethrow)
2937 do_throw(cstack);
2938 else if (check_cstack)
2939 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002940 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002942 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 && current_func_returned())
2944 do_return(&ea, TRUE, FALSE, NULL);
2945 }
2946 need_rethrow = check_cstack = FALSE;
2947#endif
2948
2949doend:
2950 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2951 curwin->w_cursor.lnum = 1;
2952
2953 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2954 {
2955 if (sourcing)
2956 {
2957 if (errormsg != IObuff)
2958 {
2959 STRCPY(IObuff, errormsg);
2960 errormsg = IObuff;
2961 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002962 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 }
2964 emsg(errormsg);
2965 }
2966#ifdef FEAT_EVAL
2967 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002968 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2969 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970#endif
2971
2972 if (verbose_save >= 0)
2973 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002974#ifdef FEAT_AUTOCMD
2975 if (cmdmod.save_ei != NULL)
2976 {
2977 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002978 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2979 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002980 free_string_option(cmdmod.save_ei);
2981 }
2982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983
2984 cmdmod = save_cmdmod;
2985
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002986 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 {
2988 /* messages could be enabled for a serious error, need to check if the
2989 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002990 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002991 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 emsg_silent -= did_esilent;
2993 if (emsg_silent < 0)
2994 emsg_silent = 0;
2995 /* Restore msg_scroll, it's set by file I/O commands, even when no
2996 * message is actually displayed. */
2997 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002998
2999 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
3000 * somewhere in the line. Put it back in the first column. */
3001 if (redirecting())
3002 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 }
3004
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003005#ifdef HAVE_SANDBOX
3006 if (did_sandbox)
3007 --sandbox;
3008#endif
3009
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3011 ea.nextcmd = NULL;
3012
3013#ifdef FEAT_EVAL
3014 --ex_nesting_level;
3015#endif
3016
3017 return ea.nextcmd;
3018}
3019#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003020 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021#endif
3022
3023/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003024 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 * If there is a match advance "pp" to the argument and return TRUE.
3026 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003027 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003028checkforcmd(
3029 char_u **pp, /* start of command */
3030 char *cmd, /* name of command */
3031 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032{
3033 int i;
3034
3035 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003036 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 break;
3038 if (i >= len && !isalpha((*pp)[i]))
3039 {
3040 *pp = skipwhite(*pp + i);
3041 return TRUE;
3042 }
3043 return FALSE;
3044}
3045
3046/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003047 * Append "cmd" to the error message in IObuff.
3048 * Takes care of limiting the length and handling 0xa0, which would be
3049 * invisible otherwise.
3050 */
3051 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003052append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003053{
3054 char_u *s = cmd;
3055 char_u *d;
3056
3057 STRCAT(IObuff, ": ");
3058 d = IObuff + STRLEN(IObuff);
3059 while (*s != NUL && d - IObuff < IOSIZE - 7)
3060 {
3061 if (
3062#ifdef FEAT_MBYTE
3063 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3064#endif
3065 *s == 0xa0)
3066 {
3067 s +=
3068#ifdef FEAT_MBYTE
3069 enc_utf8 ? 2 :
3070#endif
3071 1;
3072 STRCPY(d, "<a0>");
3073 d += 4;
3074 }
3075 else
3076 MB_COPY_CHAR(s, d);
3077 }
3078 *d = NUL;
3079}
3080
3081/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003083 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003085 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 * Returns NULL for an ambiguous user command.
3087 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003089find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090{
3091 int len;
3092 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003093 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094
3095 /*
3096 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003097 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 * - the 'k' command can directly be followed by any character.
3099 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003100 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003102 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 */
3104 p = eap->cmd;
3105 if (*p == 'k')
3106 {
3107 eap->cmdidx = CMD_k;
3108 ++p;
3109 }
3110 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003111 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3112 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 || p[1] == 'g'
3114 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3115 || p[1] == 'I'
3116 || (p[1] == 'r' && p[2] != 'e')))
3117 {
3118 eap->cmdidx = CMD_substitute;
3119 ++p;
3120 }
3121 else
3122 {
3123 while (ASCII_ISALPHA(*p))
3124 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003125 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003126 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003127 while (ASCII_ISALNUM(*p))
3128 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003129
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 /* check for non-alpha command */
3131 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3132 ++p;
3133 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003134 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3135 {
3136 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3137 * :delete with the 'l' flag. Same for 'p'. */
3138 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003139 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003140 break;
3141 if (i == len - 1)
3142 {
3143 --len;
3144 if (p[-1] == 'l')
3145 eap->flags |= EXFLAG_LIST;
3146 else
3147 eap->flags |= EXFLAG_PRINT;
3148 }
3149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150
3151 if (ASCII_ISLOWER(*eap->cmd))
3152 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
3153 else
3154 eap->cmdidx = cmdidxs[26];
3155
3156 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3157 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3158 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3159 (size_t)len) == 0)
3160 {
3161#ifdef FEAT_EVAL
3162 if (full != NULL
3163 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3164 *full = TRUE;
3165#endif
3166 break;
3167 }
3168
3169#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003170 /* Look for a user defined command as a last resort. Let ":Print" be
3171 * overruled by a user defined command. */
3172 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3173 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003175 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 while (ASCII_ISALNUM(*p))
3177 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003178 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 }
3180#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003181 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 eap->cmdidx = CMD_SIZE;
3183 }
3184
3185 return p;
3186}
3187
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003188#ifdef FEAT_USR_CMDS
3189/*
3190 * Search for a user command that matches "eap->cmd".
3191 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3192 * Return a pointer to just after the command.
3193 * Return NULL if there is no matching command.
3194 */
3195 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003196find_ucmd(
3197 exarg_T *eap,
3198 char_u *p, /* end of the command (possibly including count) */
3199 int *full, /* set to TRUE for a full match */
3200 expand_T *xp, /* used for completion, NULL otherwise */
3201 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003202{
3203 int len = (int)(p - eap->cmd);
3204 int j, k, matchlen = 0;
3205 ucmd_T *uc;
3206 int found = FALSE;
3207 int possible = FALSE;
3208 char_u *cp, *np; /* Point into typed cmd and test name */
3209 garray_T *gap;
3210 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3211 only full match global is accepted. */
3212
3213 /*
3214 * Look for buffer-local user commands first, then global ones.
3215 */
3216 gap = &curbuf->b_ucmds;
3217 for (;;)
3218 {
3219 for (j = 0; j < gap->ga_len; ++j)
3220 {
3221 uc = USER_CMD_GA(gap, j);
3222 cp = eap->cmd;
3223 np = uc->uc_name;
3224 k = 0;
3225 while (k < len && *np != NUL && *cp++ == *np++)
3226 k++;
3227 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3228 {
3229 /* If finding a second match, the command is ambiguous. But
3230 * not if a buffer-local command wasn't a full match and a
3231 * global command is a full match. */
3232 if (k == len && found && *np != NUL)
3233 {
3234 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003235 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003236 amb_local = TRUE;
3237 }
3238
3239 if (!found || (k == len && *np == NUL))
3240 {
3241 /* If we matched up to a digit, then there could
3242 * be another command including the digit that we
3243 * should use instead.
3244 */
3245 if (k == len)
3246 found = TRUE;
3247 else
3248 possible = TRUE;
3249
3250 if (gap == &ucmds)
3251 eap->cmdidx = CMD_USER;
3252 else
3253 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003254 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003255 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003256 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003257
3258# ifdef FEAT_CMDL_COMPL
3259 if (compl != NULL)
3260 *compl = uc->uc_compl;
3261# ifdef FEAT_EVAL
3262 if (xp != NULL)
3263 {
3264 xp->xp_arg = uc->uc_compl_arg;
3265 xp->xp_scriptID = uc->uc_scriptID;
3266 }
3267# endif
3268# endif
3269 /* Do not search for further abbreviations
3270 * if this is an exact match. */
3271 matchlen = k;
3272 if (k == len && *np == NUL)
3273 {
3274 if (full != NULL)
3275 *full = TRUE;
3276 amb_local = FALSE;
3277 break;
3278 }
3279 }
3280 }
3281 }
3282
3283 /* Stop if we found a full match or searched all. */
3284 if (j < gap->ga_len || gap == &ucmds)
3285 break;
3286 gap = &ucmds;
3287 }
3288
3289 /* Only found ambiguous matches. */
3290 if (amb_local)
3291 {
3292 if (xp != NULL)
3293 xp->xp_context = EXPAND_UNSUCCESSFUL;
3294 return NULL;
3295 }
3296
3297 /* The match we found may be followed immediately by a number. Move "p"
3298 * back to point to it. */
3299 if (found || possible)
3300 return p + (matchlen - len);
3301 return p;
3302}
3303#endif
3304
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003306static struct cmdmod
3307{
3308 char *name;
3309 int minlen;
3310 int has_count; /* :123verbose :3tab */
3311} cmdmods[] = {
3312 {"aboveleft", 3, FALSE},
3313 {"belowright", 3, FALSE},
3314 {"botright", 2, FALSE},
3315 {"browse", 3, FALSE},
3316 {"confirm", 4, FALSE},
3317 {"hide", 3, FALSE},
3318 {"keepalt", 5, FALSE},
3319 {"keepjumps", 5, FALSE},
3320 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003321 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003322 {"leftabove", 5, FALSE},
3323 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003324 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003325 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003326 {"rightbelow", 6, FALSE},
3327 {"sandbox", 3, FALSE},
3328 {"silent", 3, FALSE},
3329 {"tab", 3, TRUE},
3330 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003331 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003332 {"verbose", 4, TRUE},
3333 {"vertical", 4, FALSE},
3334};
3335
3336/*
3337 * Return length of a command modifier (including optional count).
3338 * Return zero when it's not a modifier.
3339 */
3340 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003341modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003342{
3343 int i, j;
3344 char_u *p = cmd;
3345
3346 if (VIM_ISDIGIT(*cmd))
3347 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003348 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003349 {
3350 for (j = 0; p[j] != NUL; ++j)
3351 if (p[j] != cmdmods[i].name[j])
3352 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003353 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003354 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003355 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003356 }
3357 return 0;
3358}
3359
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360/*
3361 * Return > 0 if an Ex command "name" exists.
3362 * Return 2 if there is an exact match.
3363 * Return 3 if there is an ambiguous match.
3364 */
3365 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003366cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367{
3368 exarg_T ea;
3369 int full = FALSE;
3370 int i;
3371 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003372 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373
3374 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003375 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 {
3377 for (j = 0; name[j] != NUL; ++j)
3378 if (name[j] != cmdmods[i].name[j])
3379 break;
3380 if (name[j] == NUL && j >= cmdmods[i].minlen)
3381 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3382 }
3383
Bram Moolenaara9587612006-05-04 21:47:50 +00003384 /* Check built-in commands and user defined commands.
3385 * For ":2match" and ":3match" we need to skip the number. */
3386 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003388 p = find_command(&ea, &full);
3389 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003391 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3392 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003393 if (*skipwhite(p) != NUL)
3394 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3396}
3397#endif
3398
3399/*
3400 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3401 * we don't need/want deleted. Maybe this could be done better if we didn't
3402 * repeat all this stuff. The only problem is that they may not stay
3403 * perfectly compatible with each other, but then the command line syntax
3404 * probably won't change that much -- webb.
3405 */
3406 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003407set_one_cmd_context(
3408 expand_T *xp,
3409 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410{
3411 char_u *p;
3412 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003413 int len = 0;
3414 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3416 int compl = EXPAND_NOTHING;
3417#endif
3418#ifdef FEAT_CMDL_COMPL
3419 int delim;
3420#endif
3421 int forceit = FALSE;
3422 int usefilter = FALSE; /* filter instead of file name */
3423
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003424 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 xp->xp_pattern = buff;
3426 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003427 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428
3429/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003430 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 */
3432 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3433 ;
3434 xp->xp_pattern = cmd;
3435
3436 if (*cmd == NUL)
3437 return NULL;
3438 if (*cmd == '"') /* ignore comment lines */
3439 {
3440 xp->xp_context = EXPAND_NOTHING;
3441 return NULL;
3442 }
3443
3444/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003445 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 */
3447 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 xp->xp_pattern = cmd;
3449 if (*cmd == NUL)
3450 return NULL;
3451 if (*cmd == '"')
3452 {
3453 xp->xp_context = EXPAND_NOTHING;
3454 return NULL;
3455 }
3456
3457 if (*cmd == '|' || *cmd == '\n')
3458 return cmd + 1; /* There's another command */
3459
3460 /*
3461 * Isolate the command and search for it in the command table.
3462 * Exceptions:
3463 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003464 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3466 */
3467 if (*cmd == 'k' && cmd[1] != 'e')
3468 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003469 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 p = cmd + 1;
3471 }
3472 else
3473 {
3474 p = cmd;
3475 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3476 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003477 /* a user command may contain digits */
3478 if (ASCII_ISUPPER(cmd[0]))
3479 while (ASCII_ISALNUM(*p) || *p == '*')
3480 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003481 /* for python 3.x: ":py3*" commands completion */
3482 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003483 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003484 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003485 while (ASCII_ISALPHA(*p) || *p == '*')
3486 ++p;
3487 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003488 /* check for non-alpha command */
3489 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3490 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003491 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003493 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 {
3495 xp->xp_context = EXPAND_UNSUCCESSFUL;
3496 return NULL;
3497 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003498 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003499 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3500 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3501 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 break;
3503
3504#ifdef FEAT_USR_CMDS
3505 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3507 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508#endif
3509 }
3510
3511 /*
3512 * If the cursor is touching the command, and it ends in an alpha-numeric
3513 * character, complete the command name.
3514 */
3515 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3516 return NULL;
3517
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003518 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 {
3520 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3521 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003522 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 p = cmd + 1;
3524 }
3525#ifdef FEAT_USR_CMDS
3526 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3527 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003528 ea.cmd = cmd;
3529 p = find_ucmd(&ea, p, NULL, xp,
3530# if defined(FEAT_CMDL_COMPL)
3531 &compl
3532# else
3533 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003535 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003536 if (p == NULL)
3537 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 }
3539#endif
3540 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003541 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 {
3543 /* Not still touching the command and it was an illegal one */
3544 xp->xp_context = EXPAND_UNSUCCESSFUL;
3545 return NULL;
3546 }
3547
3548 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3549
3550 if (*p == '!') /* forced commands */
3551 {
3552 forceit = TRUE;
3553 ++p;
3554 }
3555
3556/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003557 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003559 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003560 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561
3562 arg = skipwhite(p);
3563
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003564 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 {
3566 if (*arg == '>') /* append */
3567 {
3568 if (*++arg == '>')
3569 ++arg;
3570 arg = skipwhite(arg);
3571 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003572 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 {
3574 ++arg;
3575 usefilter = TRUE;
3576 }
3577 }
3578
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003579 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 {
3581 usefilter = forceit; /* :r! filter if forced */
3582 if (*arg == '!') /* :r !filter */
3583 {
3584 ++arg;
3585 usefilter = TRUE;
3586 }
3587 }
3588
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003589 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 {
3591 while (*arg == *cmd) /* allow any number of '>' or '<' */
3592 ++arg;
3593 arg = skipwhite(arg);
3594 }
3595
3596 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003597 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 {
3599 /* Check if we're in the +command */
3600 p = arg + 1;
3601 arg = skip_cmd_arg(arg, FALSE);
3602
3603 /* Still touching the command after '+'? */
3604 if (*arg == NUL)
3605 return p;
3606
3607 /* Skip space(s) after +command to get to the real argument */
3608 arg = skipwhite(arg);
3609 }
3610
3611 /*
3612 * Check for '|' to separate commands and '"' to start comments.
3613 * Don't do this for ":read !cmd" and ":write !cmd".
3614 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003615 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 {
3617 p = arg;
3618 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003619 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 p += 2;
3621 while (*p)
3622 {
3623 if (*p == Ctrl_V)
3624 {
3625 if (p[1] != NUL)
3626 ++p;
3627 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003628 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 || *p == '|' || *p == '\n')
3630 {
3631 if (*(p - 1) != '\\')
3632 {
3633 if (*p == '|' || *p == '\n')
3634 return p + 1;
3635 return NULL; /* It's a comment */
3636 }
3637 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003638 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 }
3640 }
3641
3642 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003643 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 vim_strchr((char_u *)"|\"", *arg) == NULL)
3645 return NULL;
3646
3647 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003648 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003650 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003651 while (*p && p < buff + len)
3652 {
3653 if (*p == ' ' || *p == TAB)
3654 {
3655 /* argument starts after a space */
3656 xp->xp_pattern = ++p;
3657 }
3658 else
3659 {
3660 if (*p == '\\' && *(p + 1) != NUL)
3661 ++p; /* skip over escaped character */
3662 mb_ptr_adv(p);
3663 }
3664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003666 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003668 int c;
3669 int in_quote = FALSE;
3670 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671
3672 /*
3673 * Allow spaces within back-quotes to count as part of the argument
3674 * being expanded.
3675 */
3676 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003677 p = xp->xp_pattern;
3678 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003680#ifdef FEAT_MBYTE
3681 if (has_mbyte)
3682 c = mb_ptr2char(p);
3683 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003685 c = *p;
3686 if (c == '\\' && p[1] != NUL)
3687 ++p;
3688 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 {
3690 if (!in_quote)
3691 {
3692 xp->xp_pattern = p;
3693 bow = p + 1;
3694 }
3695 in_quote = !in_quote;
3696 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003697 /* An argument can contain just about everything, except
3698 * characters that end the command and white space. */
3699 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003700#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003701 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003702#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003703 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003704 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003705 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003706 while (*p != NUL)
3707 {
3708#ifdef FEAT_MBYTE
3709 if (has_mbyte)
3710 c = mb_ptr2char(p);
3711 else
3712#endif
3713 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003714 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003715 break;
3716#ifdef FEAT_MBYTE
3717 if (has_mbyte)
3718 len = (*mb_ptr2len)(p);
3719 else
3720#endif
3721 len = 1;
3722 mb_ptr_adv(p);
3723 }
3724 if (in_quote)
3725 bow = p;
3726 else
3727 xp->xp_pattern = p;
3728 p -= len;
3729 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003730 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 }
3732
3733 /*
3734 * If we are still inside the quotes, and we passed a space, just
3735 * expand from there.
3736 */
3737 if (bow != NULL && in_quote)
3738 xp->xp_pattern = bow;
3739 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003740
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003741 /* For a shell command more chars need to be escaped. */
3742 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003743 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003744#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003745 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003746#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003747 /* When still after the command name expand executables. */
3748 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003749 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751
3752 /* Check for environment variable */
3753 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003754#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 || *xp->xp_pattern == '%'
3756#endif
3757 )
3758 {
3759 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3760 if (!vim_isIDc(*p))
3761 break;
3762 if (*p == NUL)
3763 {
3764 xp->xp_context = EXPAND_ENV_VARS;
3765 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003766#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3767 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003768 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003769 compl = EXPAND_ENV_VARS;
3770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 }
3772 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003773#if defined(FEAT_CMDL_COMPL)
3774 /* Check for user names */
3775 if (*xp->xp_pattern == '~')
3776 {
3777 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3778 ;
3779 /* Complete ~user only if it partially matches a user name.
3780 * A full match ~user<Tab> will be replaced by user's home
3781 * directory i.e. something like ~user<Tab> -> /home/user/ */
3782 if (*p == NUL && p > xp->xp_pattern + 1
3783 && match_user(xp->xp_pattern + 1) == 1)
3784 {
3785 xp->xp_context = EXPAND_USER;
3786 ++xp->xp_pattern;
3787 }
3788 }
3789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 }
3791
3792/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003793 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003795 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003797 case CMD_find:
3798 case CMD_sfind:
3799 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003800 if (xp->xp_context == EXPAND_FILES)
3801 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003802 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 case CMD_cd:
3804 case CMD_chdir:
3805 case CMD_lcd:
3806 case CMD_lchdir:
3807 if (xp->xp_context == EXPAND_FILES)
3808 xp->xp_context = EXPAND_DIRECTORIES;
3809 break;
3810 case CMD_help:
3811 xp->xp_context = EXPAND_HELP;
3812 xp->xp_pattern = arg;
3813 break;
3814
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003815 /* Command modifiers: return the argument.
3816 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003818 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 case CMD_belowright:
3820 case CMD_botright:
3821 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003822 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003823 case CMD_cdo:
3824 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003826 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 case CMD_folddoclosed:
3828 case CMD_folddoopen:
3829 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003830 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 case CMD_keepjumps:
3832 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003833 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003834 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003836 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003838 case CMD_noautocmd:
3839 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003841 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003843 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003844 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 case CMD_topleft:
3846 case CMD_verbose:
3847 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003848 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 return arg;
3850
Bram Moolenaar4f688582007-07-24 12:34:30 +00003851#ifdef FEAT_CMDL_COMPL
3852# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 case CMD_match:
3854 if (*arg == NUL || !ends_excmd(*arg))
3855 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003856 /* also complete "None" */
3857 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 arg = skipwhite(skiptowhite(arg));
3859 if (*arg != NUL)
3860 {
3861 xp->xp_context = EXPAND_NOTHING;
3862 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3863 }
3864 }
3865 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003866# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868/*
3869 * All completion for the +cmdline_compl feature goes here.
3870 */
3871
3872# ifdef FEAT_USR_CMDS
3873 case CMD_command:
3874 /* Check for attributes */
3875 while (*arg == '-')
3876 {
3877 arg++; /* Skip "-" */
3878 p = skiptowhite(arg);
3879 if (*p == NUL)
3880 {
3881 /* Cursor is still in the attribute */
3882 p = vim_strchr(arg, '=');
3883 if (p == NULL)
3884 {
3885 /* No "=", so complete attribute names */
3886 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3887 xp->xp_pattern = arg;
3888 return NULL;
3889 }
3890
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003891 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 * their arguments as well.
3893 */
3894 if (STRNICMP(arg, "complete", p - arg) == 0)
3895 {
3896 xp->xp_context = EXPAND_USER_COMPLETE;
3897 xp->xp_pattern = p + 1;
3898 return NULL;
3899 }
3900 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3901 {
3902 xp->xp_context = EXPAND_USER_NARGS;
3903 xp->xp_pattern = p + 1;
3904 return NULL;
3905 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003906 else if (STRNICMP(arg, "addr", p - arg) == 0)
3907 {
3908 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3909 xp->xp_pattern = p + 1;
3910 return NULL;
3911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 return NULL;
3913 }
3914 arg = skipwhite(p);
3915 }
3916
3917 /* After the attributes comes the new command name */
3918 p = skiptowhite(arg);
3919 if (*p == NUL)
3920 {
3921 xp->xp_context = EXPAND_USER_COMMANDS;
3922 xp->xp_pattern = arg;
3923 break;
3924 }
3925
3926 /* And finally comes a normal command */
3927 return skipwhite(p);
3928
3929 case CMD_delcommand:
3930 xp->xp_context = EXPAND_USER_COMMANDS;
3931 xp->xp_pattern = arg;
3932 break;
3933# endif
3934
3935 case CMD_global:
3936 case CMD_vglobal:
3937 delim = *arg; /* get the delimiter */
3938 if (delim)
3939 ++arg; /* skip delimiter if there is one */
3940
3941 while (arg[0] != NUL && arg[0] != delim)
3942 {
3943 if (arg[0] == '\\' && arg[1] != NUL)
3944 ++arg;
3945 ++arg;
3946 }
3947 if (arg[0] != NUL)
3948 return arg + 1;
3949 break;
3950 case CMD_and:
3951 case CMD_substitute:
3952 delim = *arg;
3953 if (delim)
3954 {
3955 /* skip "from" part */
3956 ++arg;
3957 arg = skip_regexp(arg, delim, p_magic, NULL);
3958 }
3959 /* skip "to" part */
3960 while (arg[0] != NUL && arg[0] != delim)
3961 {
3962 if (arg[0] == '\\' && arg[1] != NUL)
3963 ++arg;
3964 ++arg;
3965 }
3966 if (arg[0] != NUL) /* skip delimiter */
3967 ++arg;
3968 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3969 ++arg;
3970 if (arg[0] != NUL)
3971 return arg;
3972 break;
3973 case CMD_isearch:
3974 case CMD_dsearch:
3975 case CMD_ilist:
3976 case CMD_dlist:
3977 case CMD_ijump:
3978 case CMD_psearch:
3979 case CMD_djump:
3980 case CMD_isplit:
3981 case CMD_dsplit:
3982 arg = skipwhite(skipdigits(arg)); /* skip count */
3983 if (*arg == '/') /* Match regexp, not just whole words */
3984 {
3985 for (++arg; *arg && *arg != '/'; arg++)
3986 if (*arg == '\\' && arg[1] != NUL)
3987 arg++;
3988 if (*arg)
3989 {
3990 arg = skipwhite(arg + 1);
3991
3992 /* Check for trailing illegal characters */
3993 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3994 xp->xp_context = EXPAND_NOTHING;
3995 else
3996 return arg;
3997 }
3998 }
3999 break;
4000#ifdef FEAT_AUTOCMD
4001 case CMD_autocmd:
4002 return set_context_in_autocmd(xp, arg, FALSE);
4003
4004 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004005 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 return set_context_in_autocmd(xp, arg, TRUE);
4007#endif
4008 case CMD_set:
4009 set_context_in_set_cmd(xp, arg, 0);
4010 break;
4011 case CMD_setglobal:
4012 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4013 break;
4014 case CMD_setlocal:
4015 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4016 break;
4017 case CMD_tag:
4018 case CMD_stag:
4019 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004020 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 case CMD_tselect:
4022 case CMD_stselect:
4023 case CMD_ptselect:
4024 case CMD_tjump:
4025 case CMD_stjump:
4026 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004027 if (*p_wop != NUL)
4028 xp->xp_context = EXPAND_TAGS_LISTFILES;
4029 else
4030 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 xp->xp_pattern = arg;
4032 break;
4033 case CMD_augroup:
4034 xp->xp_context = EXPAND_AUGROUP;
4035 xp->xp_pattern = arg;
4036 break;
4037#ifdef FEAT_SYN_HL
4038 case CMD_syntax:
4039 set_context_in_syntax_cmd(xp, arg);
4040 break;
4041#endif
4042#ifdef FEAT_EVAL
4043 case CMD_let:
4044 case CMD_if:
4045 case CMD_elseif:
4046 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004047 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 case CMD_echo:
4049 case CMD_echon:
4050 case CMD_execute:
4051 case CMD_echomsg:
4052 case CMD_echoerr:
4053 case CMD_call:
4054 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004055 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 break;
4057
4058 case CMD_unlet:
4059 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4060 arg = xp->xp_pattern + 1;
4061 xp->xp_context = EXPAND_USER_VARS;
4062 xp->xp_pattern = arg;
4063 break;
4064
4065 case CMD_function:
4066 case CMD_delfunction:
4067 xp->xp_context = EXPAND_USER_FUNC;
4068 xp->xp_pattern = arg;
4069 break;
4070
4071 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004072 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 break;
4074#endif
4075 case CMD_highlight:
4076 set_context_in_highlight_cmd(xp, arg);
4077 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004078#ifdef FEAT_CSCOPE
4079 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004080 case CMD_lcscope:
4081 case CMD_scscope:
4082 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004083 break;
4084#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004085#ifdef FEAT_SIGNS
4086 case CMD_sign:
4087 set_context_in_sign_cmd(xp, arg);
4088 break;
4089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090#ifdef FEAT_LISTCMDS
4091 case CMD_bdelete:
4092 case CMD_bwipeout:
4093 case CMD_bunload:
4094 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4095 arg = xp->xp_pattern + 1;
4096 /*FALLTHROUGH*/
4097 case CMD_buffer:
4098 case CMD_sbuffer:
4099 case CMD_checktime:
4100 xp->xp_context = EXPAND_BUFFERS;
4101 xp->xp_pattern = arg;
4102 break;
4103#endif
4104#ifdef FEAT_USR_CMDS
4105 case CMD_USER:
4106 case CMD_USER_BUF:
4107 if (compl != EXPAND_NOTHING)
4108 {
4109 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004110 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 {
4112# ifdef FEAT_MENU
4113 if (compl == EXPAND_MENUS)
4114 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4115# endif
4116 if (compl == EXPAND_COMMANDS)
4117 return arg;
4118 if (compl == EXPAND_MAPPINGS)
4119 return set_context_in_map_cmd(xp, (char_u *)"map",
4120 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004121 /* Find start of last argument. */
4122 p = arg;
4123 while (*p)
4124 {
4125 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004126 /* argument starts after a space */
4127 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004128 else if (*p == '\\' && *(p + 1) != NUL)
4129 ++p; /* skip over escaped character */
4130 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 xp->xp_pattern = arg;
4133 }
4134 xp->xp_context = compl;
4135 }
4136 break;
4137#endif
4138 case CMD_map: case CMD_noremap:
4139 case CMD_nmap: case CMD_nnoremap:
4140 case CMD_vmap: case CMD_vnoremap:
4141 case CMD_omap: case CMD_onoremap:
4142 case CMD_imap: case CMD_inoremap:
4143 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004144 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004145 case CMD_smap: case CMD_snoremap:
4146 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004148 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 case CMD_unmap:
4150 case CMD_nunmap:
4151 case CMD_vunmap:
4152 case CMD_ounmap:
4153 case CMD_iunmap:
4154 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004155 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004156 case CMD_sunmap:
4157 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004159 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 case CMD_abbreviate: case CMD_noreabbrev:
4161 case CMD_cabbrev: case CMD_cnoreabbrev:
4162 case CMD_iabbrev: case CMD_inoreabbrev:
4163 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004164 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 case CMD_unabbreviate:
4166 case CMD_cunabbrev:
4167 case CMD_iunabbrev:
4168 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004169 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170#ifdef FEAT_MENU
4171 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4172 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4173 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4174 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4175 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4176 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4177 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4178 case CMD_tmenu: case CMD_tunmenu:
4179 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4180 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4181#endif
4182
4183 case CMD_colorscheme:
4184 xp->xp_context = EXPAND_COLORS;
4185 xp->xp_pattern = arg;
4186 break;
4187
4188 case CMD_compiler:
4189 xp->xp_context = EXPAND_COMPILER;
4190 xp->xp_pattern = arg;
4191 break;
4192
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004193 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004194 xp->xp_context = EXPAND_OWNSYNTAX;
4195 xp->xp_pattern = arg;
4196 break;
4197
4198 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004199 xp->xp_context = EXPAND_FILETYPE;
4200 xp->xp_pattern = arg;
4201 break;
4202
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004203 case CMD_packadd:
4204 xp->xp_context = EXPAND_PACKADD;
4205 xp->xp_pattern = arg;
4206 break;
4207
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4209 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4210 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004211 p = skiptowhite(arg);
4212 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 {
4214 xp->xp_context = EXPAND_LANGUAGE;
4215 xp->xp_pattern = arg;
4216 }
4217 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004218 {
4219 if ( STRNCMP(arg, "messages", p - arg) == 0
4220 || STRNCMP(arg, "ctype", p - arg) == 0
4221 || STRNCMP(arg, "time", p - arg) == 0)
4222 {
4223 xp->xp_context = EXPAND_LOCALES;
4224 xp->xp_pattern = skipwhite(p);
4225 }
4226 else
4227 xp->xp_context = EXPAND_NOTHING;
4228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 break;
4230#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004231#if defined(FEAT_PROFILE)
4232 case CMD_profile:
4233 set_context_in_profile_cmd(xp, arg);
4234 break;
4235#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004236 case CMD_behave:
4237 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004238 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004239 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004241#if defined(FEAT_CMDHIST)
4242 case CMD_history:
4243 xp->xp_context = EXPAND_HISTORY;
4244 xp->xp_pattern = arg;
4245 break;
4246#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004247#if defined(FEAT_PROFILE)
4248 case CMD_syntime:
4249 xp->xp_context = EXPAND_SYNTIME;
4250 xp->xp_pattern = arg;
4251 break;
4252#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004253
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254#endif /* FEAT_CMDL_COMPL */
4255
4256 default:
4257 break;
4258 }
4259 return NULL;
4260}
4261
4262/*
4263 * skip a range specifier of the form: addr [,addr] [;addr] ..
4264 *
4265 * Backslashed delimiters after / or ? will be skipped, and commands will
4266 * not be expanded between /'s and ?'s or after "'".
4267 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004268 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 * Returns the "cmd" pointer advanced to beyond the range.
4270 */
4271 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004272skip_range(
4273 char_u *cmd,
4274 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004276 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277
Bram Moolenaardf177f62005-02-22 08:39:57 +00004278 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 {
4280 if (*cmd == '\'')
4281 {
4282 if (*++cmd == NUL && ctx != NULL)
4283 *ctx = EXPAND_NOTHING;
4284 }
4285 else if (*cmd == '/' || *cmd == '?')
4286 {
4287 delim = *cmd++;
4288 while (*cmd != NUL && *cmd != delim)
4289 if (*cmd++ == '\\' && *cmd != NUL)
4290 ++cmd;
4291 if (*cmd == NUL && ctx != NULL)
4292 *ctx = EXPAND_NOTHING;
4293 }
4294 if (*cmd != NUL)
4295 ++cmd;
4296 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004297
4298 /* Skip ":" and white space. */
4299 while (*cmd == ':')
4300 cmd = skipwhite(cmd + 1);
4301
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 return cmd;
4303}
4304
4305/*
4306 * get a single EX address
4307 *
4308 * Set ptr to the next character after the part that was interpreted.
4309 * Set ptr to NULL when an error is encountered.
4310 *
4311 * Return MAXLNUM when no Ex address was found.
4312 */
4313 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004314get_address(
4315 exarg_T *eap UNUSED,
4316 char_u **ptr,
4317 int addr_type, /* flag: one of ADDR_LINES, ... */
4318 int skip, /* only skip the address, don't use it */
4319 int to_other_file) /* flag: may jump to other file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320{
4321 int c;
4322 int i;
4323 long n;
4324 char_u *cmd;
4325 pos_T pos;
4326 pos_T *fp;
4327 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004328 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329
4330 cmd = skipwhite(*ptr);
4331 lnum = MAXLNUM;
4332 do
4333 {
4334 switch (*cmd)
4335 {
4336 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004337 ++cmd;
4338 switch (addr_type)
4339 {
4340 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 lnum = curwin->w_cursor.lnum;
4342 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004343 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004344 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004345 break;
4346 case ADDR_ARGUMENTS:
4347 lnum = curwin->w_arg_idx + 1;
4348 break;
4349 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004350 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004351 lnum = curbuf->b_fnum;
4352 break;
4353 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004354 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004355 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004356#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004357 case ADDR_QUICKFIX:
4358 lnum = qf_get_cur_valid_idx(eap);
4359 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004360#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004361 }
4362 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363
4364 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004365 ++cmd;
4366 switch (addr_type)
4367 {
4368 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 lnum = curbuf->b_ml.ml_line_count;
4370 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004371 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004372 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004373 break;
4374 case ADDR_ARGUMENTS:
4375 lnum = ARGCOUNT;
4376 break;
4377 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004378 buf = lastbuf;
4379 while (buf->b_ml.ml_mfp == NULL)
4380 {
4381 if (buf->b_prev == NULL)
4382 break;
4383 buf = buf->b_prev;
4384 }
4385 lnum = buf->b_fnum;
4386 break;
4387 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004388 lnum = lastbuf->b_fnum;
4389 break;
4390 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004391 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004392 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004393#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004394 case ADDR_QUICKFIX:
4395 lnum = qf_get_size(eap);
4396 if (lnum == 0)
4397 lnum = 1;
4398 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004399#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004400 }
4401 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402
4403 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004404 if (*++cmd == NUL)
4405 {
4406 cmd = NULL;
4407 goto error;
4408 }
4409 if (addr_type != ADDR_LINES)
4410 {
4411 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004412 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004413 goto error;
4414 }
4415 if (skip)
4416 ++cmd;
4417 else
4418 {
4419 /* Only accept a mark in another file when it is
4420 * used by itself: ":'M". */
4421 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4422 ++cmd;
4423 if (fp == (pos_T *)-1)
4424 /* Jumped to another file. */
4425 lnum = curwin->w_cursor.lnum;
4426 else
4427 {
4428 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 {
4430 cmd = NULL;
4431 goto error;
4432 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004433 lnum = fp->lnum;
4434 }
4435 }
4436 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437
4438 case '/':
4439 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004440 c = *cmd++;
4441 if (addr_type != ADDR_LINES)
4442 {
4443 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004444 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004445 goto error;
4446 }
4447 if (skip) /* skip "/pat/" */
4448 {
4449 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4450 if (*cmd == c)
4451 ++cmd;
4452 }
4453 else
4454 {
4455 pos = curwin->w_cursor; /* save curwin->w_cursor */
4456 /*
4457 * When '/' or '?' follows another address, start
4458 * from there.
4459 */
4460 if (lnum != MAXLNUM)
4461 curwin->w_cursor.lnum = lnum;
4462 /*
4463 * Start a forward search at the end of the line.
4464 * Start a backward search at the start of the line.
4465 * This makes sure we never match in the current
4466 * line, and can match anywhere in the
4467 * next/previous line.
4468 */
4469 if (c == '/')
4470 curwin->w_cursor.col = MAXCOL;
4471 else
4472 curwin->w_cursor.col = 0;
4473 searchcmdlen = 0;
4474 if (!do_search(NULL, c, cmd, 1L,
4475 SEARCH_HIS | SEARCH_MSG, NULL))
4476 {
4477 curwin->w_cursor = pos;
4478 cmd = NULL;
4479 goto error;
4480 }
4481 lnum = curwin->w_cursor.lnum;
4482 curwin->w_cursor = pos;
4483 /* adjust command string pointer */
4484 cmd += searchcmdlen;
4485 }
4486 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487
4488 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004489 ++cmd;
4490 if (addr_type != ADDR_LINES)
4491 {
4492 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004493 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004494 goto error;
4495 }
4496 if (*cmd == '&')
4497 i = RE_SUBST;
4498 else if (*cmd == '?' || *cmd == '/')
4499 i = RE_SEARCH;
4500 else
4501 {
4502 EMSG(_(e_backslash));
4503 cmd = NULL;
4504 goto error;
4505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004507 if (!skip)
4508 {
4509 /*
4510 * When search follows another address, start from
4511 * there.
4512 */
4513 if (lnum != MAXLNUM)
4514 pos.lnum = lnum;
4515 else
4516 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004518 /*
4519 * Start the search just like for the above
4520 * do_search().
4521 */
4522 if (*cmd != '?')
4523 pos.col = MAXCOL;
4524 else
4525 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004526#ifdef FEAT_VIRTUALEDIT
4527 pos.coladd = 0;
4528#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004529 if (searchit(curwin, curbuf, &pos,
4530 *cmd == '?' ? BACKWARD : FORWARD,
4531 (char_u *)"", 1L, SEARCH_MSG,
4532 i, (linenr_T)0, NULL) != FAIL)
4533 lnum = pos.lnum;
4534 else
4535 {
4536 cmd = NULL;
4537 goto error;
4538 }
4539 }
4540 ++cmd;
4541 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542
4543 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004544 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4545 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 }
4547
4548 for (;;)
4549 {
4550 cmd = skipwhite(cmd);
4551 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4552 break;
4553
4554 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004555 {
4556 switch (addr_type)
4557 {
4558 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004559 /* "+1" is same as ".+1" */
4560 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004561 break;
4562 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004563 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004564 break;
4565 case ADDR_ARGUMENTS:
4566 lnum = curwin->w_arg_idx + 1;
4567 break;
4568 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004569 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004570 lnum = curbuf->b_fnum;
4571 break;
4572 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004573 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004574 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004575#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004576 case ADDR_QUICKFIX:
4577 lnum = qf_get_cur_valid_idx(eap);
4578 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004579#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004580 }
4581 }
4582
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 if (VIM_ISDIGIT(*cmd))
4584 i = '+'; /* "number" is same as "+number" */
4585 else
4586 i = *cmd++;
4587 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4588 n = 1;
4589 else
4590 n = getdigits(&cmd);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004591 if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004592 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004593 lnum = compute_buffer_local_count(
4594 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004595 else if (i == '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 lnum -= n;
4597 else
4598 lnum += n;
4599 }
4600 } while (*cmd == '/' || *cmd == '?');
4601
4602error:
4603 *ptr = cmd;
4604 return lnum;
4605}
4606
4607/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004608 * Get flags from an Ex command argument.
4609 */
4610 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004611get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004612{
4613 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4614 {
4615 if (*eap->arg == 'l')
4616 eap->flags |= EXFLAG_LIST;
4617 else if (*eap->arg == 'p')
4618 eap->flags |= EXFLAG_PRINT;
4619 else
4620 eap->flags |= EXFLAG_NR;
4621 eap->arg = skipwhite(eap->arg + 1);
4622 }
4623}
4624
4625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 * Function called for command which is Not Implemented. NI!
4627 */
4628 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004629ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630{
4631 if (!eap->skip)
4632 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4633}
4634
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004635#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636/*
4637 * Function called for script command which is Not Implemented. NI!
4638 * Skips over ":perl <<EOF" constructs.
4639 */
4640 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004641ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642{
4643 if (!eap->skip)
4644 ex_ni(eap);
4645 else
4646 vim_free(script_get(eap, eap->arg));
4647}
4648#endif
4649
4650/*
4651 * Check range in Ex command for validity.
4652 * Return NULL when valid, error message when invalid.
4653 */
4654 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004655invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004657 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 if ( eap->line1 < 0
4659 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004660 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004662
4663 if (eap->argt & RANGE)
4664 {
4665 switch(eap->addr_type)
4666 {
4667 case ADDR_LINES:
4668 if (!(eap->argt & NOTADR)
4669 && eap->line2 > curbuf->b_ml.ml_line_count
4670#ifdef FEAT_DIFF
4671 + (eap->cmdidx == CMD_diffget)
4672#endif
4673 )
4674 return (char_u *)_(e_invrange);
4675 break;
4676 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004677 /* add 1 if ARGCOUNT is 0 */
4678 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004679 return (char_u *)_(e_invrange);
4680 break;
4681 case ADDR_BUFFERS:
4682 if (eap->line1 < firstbuf->b_fnum
4683 || eap->line2 > lastbuf->b_fnum)
4684 return (char_u *)_(e_invrange);
4685 break;
4686 case ADDR_LOADED_BUFFERS:
4687 buf = firstbuf;
4688 while (buf->b_ml.ml_mfp == NULL)
4689 {
4690 if (buf->b_next == NULL)
4691 return (char_u *)_(e_invrange);
4692 buf = buf->b_next;
4693 }
4694 if (eap->line1 < buf->b_fnum)
4695 return (char_u *)_(e_invrange);
4696 buf = lastbuf;
4697 while (buf->b_ml.ml_mfp == NULL)
4698 {
4699 if (buf->b_prev == NULL)
4700 return (char_u *)_(e_invrange);
4701 buf = buf->b_prev;
4702 }
4703 if (eap->line2 > buf->b_fnum)
4704 return (char_u *)_(e_invrange);
4705 break;
4706 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004707 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004708 return (char_u *)_(e_invrange);
4709 break;
4710 case ADDR_TABS:
4711 if (eap->line2 > LAST_TAB_NR)
4712 return (char_u *)_(e_invrange);
4713 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004714#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004715 case ADDR_QUICKFIX:
4716 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4717 return (char_u *)_(e_invrange);
4718 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004719#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004720 }
4721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 return NULL;
4723}
4724
4725/*
4726 * Correct the range for zero line number, if required.
4727 */
4728 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004729correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730{
4731 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4732 {
4733 if (eap->line1 == 0)
4734 eap->line1 = 1;
4735 if (eap->line2 == 0)
4736 eap->line2 = 1;
4737 }
4738}
4739
Bram Moolenaar748bf032005-02-02 23:04:36 +00004740#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004741static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004742
4743/*
4744 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4745 * pattern. Otherwise return eap->arg.
4746 */
4747 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004748skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004749{
4750 char_u *p = eap->arg;
4751
Bram Moolenaara37420f2006-02-04 22:37:47 +00004752 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4753 || eap->cmdidx == CMD_vimgrepadd
4754 || eap->cmdidx == CMD_lvimgrepadd
4755 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004756 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004757 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004758 if (p == NULL)
4759 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004760 }
4761 return p;
4762}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004763
4764/*
4765 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4766 * in the command line, so that things like % get expanded.
4767 */
4768 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004769replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004770{
4771 char_u *new_cmdline;
4772 char_u *program;
4773 char_u *pos;
4774 char_u *ptr;
4775 int len;
4776 int i;
4777
4778 /*
4779 * Don't do it when ":vimgrep" is used for ":grep".
4780 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004781 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4782 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4783 || eap->cmdidx == CMD_grepadd
4784 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004785 && !grep_internal(eap->cmdidx))
4786 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004787 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4788 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004789 {
4790 if (*curbuf->b_p_gp == NUL)
4791 program = p_gp;
4792 else
4793 program = curbuf->b_p_gp;
4794 }
4795 else
4796 {
4797 if (*curbuf->b_p_mp == NUL)
4798 program = p_mp;
4799 else
4800 program = curbuf->b_p_mp;
4801 }
4802
4803 p = skipwhite(p);
4804
4805 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4806 {
4807 /* replace $* by given arguments */
4808 i = 1;
4809 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4810 ++i;
4811 len = (int)STRLEN(p);
4812 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4813 if (new_cmdline == NULL)
4814 return NULL; /* out of memory */
4815 ptr = new_cmdline;
4816 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4817 {
4818 i = (int)(pos - program);
4819 STRNCPY(ptr, program, i);
4820 STRCPY(ptr += i, p);
4821 ptr += len;
4822 program = pos + 2;
4823 }
4824 STRCPY(ptr, program);
4825 }
4826 else
4827 {
4828 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4829 if (new_cmdline == NULL)
4830 return NULL; /* out of memory */
4831 STRCPY(new_cmdline, program);
4832 STRCAT(new_cmdline, " ");
4833 STRCAT(new_cmdline, p);
4834 }
4835 msg_make(p);
4836
4837 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4838 vim_free(*cmdlinep);
4839 *cmdlinep = new_cmdline;
4840 p = new_cmdline;
4841 }
4842 return p;
4843}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004844#endif
4845
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846/*
4847 * Expand file name in Ex command argument.
4848 * Return FAIL for failure, OK otherwise.
4849 */
4850 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004851expand_filename(
4852 exarg_T *eap,
4853 char_u **cmdlinep,
4854 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855{
4856 int has_wildcards; /* need to expand wildcards */
4857 char_u *repl;
4858 int srclen;
4859 char_u *p;
4860 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004861 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862
Bram Moolenaar748bf032005-02-02 23:04:36 +00004863#ifdef FEAT_QUICKFIX
4864 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4865 p = skip_grep_pat(eap);
4866#else
4867 p = eap->arg;
4868#endif
4869
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 /*
4871 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4872 * the file name contains a wildcard it should not cause expanding.
4873 * (it will be expanded anyway if there is a wildcard before replacing).
4874 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004875 has_wildcards = mch_has_wildcard(p);
4876 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004878#ifdef FEAT_EVAL
4879 /* Skip over `=expr`, wildcards in it are not expanded. */
4880 if (p[0] == '`' && p[1] == '=')
4881 {
4882 p += 2;
4883 (void)skip_expr(&p);
4884 if (*p == '`')
4885 ++p;
4886 continue;
4887 }
4888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 /*
4890 * Quick check if this cannot be the start of a special string.
4891 * Also removes backslash before '%', '#' and '<'.
4892 */
4893 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4894 {
4895 ++p;
4896 continue;
4897 }
4898
4899 /*
4900 * Try to find a match at this position.
4901 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004902 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4903 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 if (*errormsgp != NULL) /* error detected */
4905 return FAIL;
4906 if (repl == NULL) /* no match found */
4907 {
4908 p += srclen;
4909 continue;
4910 }
4911
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004912 /* Wildcards won't be expanded below, the replacement is taken
4913 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4914 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4915 {
4916 char_u *l = repl;
4917
4918 repl = expand_env_save(repl);
4919 vim_free(l);
4920 }
4921
Bram Moolenaar63b92542007-03-27 14:57:09 +00004922 /* Need to escape white space et al. with a backslash.
4923 * Don't do this for:
4924 * - replacement that already has been escaped: "##"
4925 * - shell commands (may have to use quotes instead).
4926 * - non-unix systems when there is a single argument (spaces don't
4927 * separate arguments then).
4928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004930 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 && eap->cmdidx != CMD_bang
4932 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004933 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004935 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004937 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938#ifndef UNIX
4939 && !(eap->argt & NOSPC)
4940#endif
4941 )
4942 {
4943 char_u *l;
4944#ifdef BACKSLASH_IN_FILENAME
4945 /* Don't escape a backslash here, because rem_backslash() doesn't
4946 * remove it later. */
4947 static char_u *nobslash = (char_u *)" \t\"|";
4948# define ESCAPE_CHARS nobslash
4949#else
4950# define ESCAPE_CHARS escape_chars
4951#endif
4952
4953 for (l = repl; *l; ++l)
4954 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4955 {
4956 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4957 if (l != NULL)
4958 {
4959 vim_free(repl);
4960 repl = l;
4961 }
4962 break;
4963 }
4964 }
4965
4966 /* For a shell command a '!' must be escaped. */
4967 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004968 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 {
4970 char_u *l;
4971
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004972 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 if (l != NULL)
4974 {
4975 vim_free(repl);
4976 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 }
4978 }
4979
4980 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4981 vim_free(repl);
4982 if (p == NULL)
4983 return FAIL;
4984 }
4985
4986 /*
4987 * One file argument: Expand wildcards.
4988 * Don't do this with ":r !command" or ":w !command".
4989 */
4990 if ((eap->argt & NOSPC) && !eap->usefilter)
4991 {
4992 /*
4993 * May do this twice:
4994 * 1. Replace environment variables.
4995 * 2. Replace any other wildcards, remove backslashes.
4996 */
4997 for (n = 1; n <= 2; ++n)
4998 {
4999 if (n == 2)
5000 {
5001#ifdef UNIX
5002 /*
5003 * Only for Unix we check for more than one file name.
5004 * For other systems spaces are considered to be part
5005 * of the file name.
5006 * Only check here if there is no wildcard, otherwise
5007 * ExpandOne() will check for errors. This allows
5008 * ":e `ls ve*.c`" on Unix.
5009 */
5010 if (!has_wildcards)
5011 for (p = eap->arg; *p; ++p)
5012 {
5013 /* skip escaped characters */
5014 if (p[1] && (*p == '\\' || *p == Ctrl_V))
5015 ++p;
5016 else if (vim_iswhite(*p))
5017 {
5018 *errormsgp = (char_u *)_("E172: Only one file name allowed");
5019 return FAIL;
5020 }
5021 }
5022#endif
5023
5024 /*
5025 * Halve the number of backslashes (this is Vi compatible).
5026 * For Unix and OS/2, when wildcards are expanded, this is
5027 * done by ExpandOne() below.
5028 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005029#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 if (!has_wildcards)
5031#endif
5032 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 }
5034
5035 if (has_wildcards)
5036 {
5037 if (n == 1)
5038 {
5039 /*
5040 * First loop: May expand environment variables. This
5041 * can be done much faster with expand_env() than with
5042 * something else (e.g., calling a shell).
5043 * After expanding environment variables, check again
5044 * if there are still wildcards present.
5045 */
5046 if (vim_strchr(eap->arg, '$') != NULL
5047 || vim_strchr(eap->arg, '~') != NULL)
5048 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005049 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005050 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 has_wildcards = mch_has_wildcard(NameBuff);
5052 p = NameBuff;
5053 }
5054 else
5055 p = NULL;
5056 }
5057 else /* n == 2 */
5058 {
5059 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005060 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061
5062 ExpandInit(&xpc);
5063 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005064 if (p_wic)
5065 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005067 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 if (p == NULL)
5069 return FAIL;
5070 }
5071 if (p != NULL)
5072 {
5073 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5074 p, cmdlinep);
5075 if (n == 2) /* p came from ExpandOne() */
5076 vim_free(p);
5077 }
5078 }
5079 }
5080 }
5081 return OK;
5082}
5083
5084/*
5085 * Replace part of the command line, keeping eap->cmd, eap->arg and
5086 * eap->nextcmd correct.
5087 * "src" points to the part that is to be replaced, of length "srclen".
5088 * "repl" is the replacement string.
5089 * Returns a pointer to the character after the replaced string.
5090 * Returns NULL for failure.
5091 */
5092 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005093repl_cmdline(
5094 exarg_T *eap,
5095 char_u *src,
5096 int srclen,
5097 char_u *repl,
5098 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099{
5100 int len;
5101 int i;
5102 char_u *new_cmdline;
5103
5104 /*
5105 * The new command line is build in new_cmdline[].
5106 * First allocate it.
5107 * Careful: a "+cmd" argument may have been NUL terminated.
5108 */
5109 len = (int)STRLEN(repl);
5110 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005111 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5113 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5114 return NULL; /* out of memory! */
5115
5116 /*
5117 * Copy the stuff before the expanded part.
5118 * Copy the expanded stuff.
5119 * Copy what came after the expanded part.
5120 * Copy the next commands, if there are any.
5121 */
5122 i = (int)(src - *cmdlinep); /* length of part before match */
5123 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005124
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 mch_memmove(new_cmdline + i, repl, (size_t)len);
5126 i += len; /* remember the end of the string */
5127 STRCPY(new_cmdline + i, src + srclen);
5128 src = new_cmdline + i; /* remember where to continue */
5129
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005130 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 {
5132 i = (int)STRLEN(new_cmdline) + 1;
5133 STRCPY(new_cmdline + i, eap->nextcmd);
5134 eap->nextcmd = new_cmdline + i;
5135 }
5136 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5137 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5138 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5139 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5140 vim_free(*cmdlinep);
5141 *cmdlinep = new_cmdline;
5142
5143 return src;
5144}
5145
5146/*
5147 * Check for '|' to separate commands and '"' to start comments.
5148 */
5149 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005150separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151{
5152 char_u *p;
5153
Bram Moolenaar86b68352004-12-27 21:59:20 +00005154#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005155 p = skip_grep_pat(eap);
5156#else
5157 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005158#endif
5159
5160 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 {
5162 if (*p == Ctrl_V)
5163 {
5164 if (eap->argt & (USECTRLV | XFILE))
5165 ++p; /* skip CTRL-V and next char */
5166 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005167 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005168 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 if (*p == NUL) /* stop at NUL after CTRL-V */
5170 break;
5171 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005172
5173#ifdef FEAT_EVAL
5174 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005175 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005176 {
5177 p += 2;
5178 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005179 }
5180#endif
5181
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 /* Check for '"': start of comment or '|': next command */
5183 /* :@" and :*" do not start a comment!
5184 * :redir @" doesn't either. */
5185 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5186 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5187 || p != eap->arg)
5188 && (eap->cmdidx != CMD_redir
5189 || p != eap->arg + 1 || p[-1] != '@'))
5190 || *p == '|' || *p == '\n')
5191 {
5192 /*
5193 * We remove the '\' before the '|', unless USECTRLV is used
5194 * AND 'b' is present in 'cpoptions'.
5195 */
5196 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5197 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5198 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005199 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 --p;
5201 }
5202 else
5203 {
5204 eap->nextcmd = check_nextcmd(p);
5205 *p = NUL;
5206 break;
5207 }
5208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005210
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5212 del_trailing_spaces(eap->arg);
5213}
5214
5215/*
5216 * get + command from ex argument
5217 */
5218 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005219getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220{
5221 char_u *arg = *argp;
5222 char_u *command = NULL;
5223
5224 if (*arg == '+') /* +[command] */
5225 {
5226 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005227 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 command = dollar_command;
5229 else
5230 {
5231 command = arg;
5232 arg = skip_cmd_arg(command, TRUE);
5233 if (*arg != NUL)
5234 *arg++ = NUL; /* terminate command with NUL */
5235 }
5236
5237 arg = skipwhite(arg); /* skip over spaces */
5238 *argp = arg;
5239 }
5240 return command;
5241}
5242
5243/*
5244 * Find end of "+command" argument. Skip over "\ " and "\\".
5245 */
5246 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005247skip_cmd_arg(
5248 char_u *p,
5249 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250{
5251 while (*p && !vim_isspace(*p))
5252 {
5253 if (*p == '\\' && p[1] != NUL)
5254 {
5255 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005256 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 else
5258 ++p;
5259 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005260 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 }
5262 return p;
5263}
5264
5265/*
5266 * Get "++opt=arg" argument.
5267 * Return FAIL or OK.
5268 */
5269 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005270getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271{
5272 char_u *arg = eap->arg + 2;
5273 int *pp = NULL;
5274#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005275 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 char_u *p;
5277#endif
5278
5279 /* ":edit ++[no]bin[ary] file" */
5280 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5281 {
5282 if (*arg == 'n')
5283 {
5284 arg += 2;
5285 eap->force_bin = FORCE_NOBIN;
5286 }
5287 else
5288 eap->force_bin = FORCE_BIN;
5289 if (!checkforcmd(&arg, "binary", 3))
5290 return FAIL;
5291 eap->arg = skipwhite(arg);
5292 return OK;
5293 }
5294
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005295 /* ":read ++edit file" */
5296 if (STRNCMP(arg, "edit", 4) == 0)
5297 {
5298 eap->read_edit = TRUE;
5299 eap->arg = skipwhite(arg + 4);
5300 return OK;
5301 }
5302
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 if (STRNCMP(arg, "ff", 2) == 0)
5304 {
5305 arg += 2;
5306 pp = &eap->force_ff;
5307 }
5308 else if (STRNCMP(arg, "fileformat", 10) == 0)
5309 {
5310 arg += 10;
5311 pp = &eap->force_ff;
5312 }
5313#ifdef FEAT_MBYTE
5314 else if (STRNCMP(arg, "enc", 3) == 0)
5315 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005316 if (STRNCMP(arg, "encoding", 8) == 0)
5317 arg += 8;
5318 else
5319 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 pp = &eap->force_enc;
5321 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005322 else if (STRNCMP(arg, "bad", 3) == 0)
5323 {
5324 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005325 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327#endif
5328
5329 if (pp == NULL || *arg != '=')
5330 return FAIL;
5331
5332 ++arg;
5333 *pp = (int)(arg - eap->cmd);
5334 arg = skip_cmd_arg(arg, FALSE);
5335 eap->arg = skipwhite(arg);
5336 *arg = NUL;
5337
5338#ifdef FEAT_MBYTE
5339 if (pp == &eap->force_ff)
5340 {
5341#endif
5342 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5343 return FAIL;
5344#ifdef FEAT_MBYTE
5345 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005346 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 {
5348 /* Make 'fileencoding' lower case. */
5349 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5350 *p = TOLOWER_ASC(*p);
5351 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005352 else
5353 {
5354 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5355 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005356 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005357 if (STRICMP(p, "keep") == 0)
5358 eap->bad_char = BAD_KEEP;
5359 else if (STRICMP(p, "drop") == 0)
5360 eap->bad_char = BAD_DROP;
5361 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5362 eap->bad_char = *p;
5363 else
5364 return FAIL;
5365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366#endif
5367
5368 return OK;
5369}
5370
5371/*
5372 * ":abbreviate" and friends.
5373 */
5374 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005375ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376{
5377 do_exmap(eap, TRUE); /* almost the same as mapping */
5378}
5379
5380/*
5381 * ":map" and friends.
5382 */
5383 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005384ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385{
5386 /*
5387 * If we are sourcing .exrc or .vimrc in current directory we
5388 * print the mappings for security reasons.
5389 */
5390 if (secure)
5391 {
5392 secure = 2;
5393 msg_outtrans(eap->cmd);
5394 msg_putchar('\n');
5395 }
5396 do_exmap(eap, FALSE);
5397}
5398
5399/*
5400 * ":unmap" and friends.
5401 */
5402 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005403ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404{
5405 do_exmap(eap, FALSE);
5406}
5407
5408/*
5409 * ":mapclear" and friends.
5410 */
5411 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005412ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413{
5414 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5415}
5416
5417/*
5418 * ":abclear" and friends.
5419 */
5420 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005421ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422{
5423 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5424}
5425
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005426#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005428ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429{
5430 /*
5431 * Disallow auto commands from .exrc and .vimrc in current
5432 * directory for security reasons.
5433 */
5434 if (secure)
5435 {
5436 secure = 2;
5437 eap->errmsg = e_curdir;
5438 }
5439 else if (eap->cmdidx == CMD_autocmd)
5440 do_autocmd(eap->arg, eap->forceit);
5441 else
5442 do_augroup(eap->arg, eap->forceit);
5443}
5444
5445/*
5446 * ":doautocmd": Apply the automatic commands to the current buffer.
5447 */
5448 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005449ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005451 char_u *arg = eap->arg;
5452 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005453 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005454
Bram Moolenaar1610d052016-06-09 22:53:01 +02005455 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5456 /* Only when there is no <nomodeline>. */
5457 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005458 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459}
5460#endif
5461
5462#ifdef FEAT_LISTCMDS
5463/*
5464 * :[N]bunload[!] [N] [bufname] unload buffer
5465 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5466 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5467 */
5468 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005469ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470{
5471 eap->errmsg = do_bufdel(
5472 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5473 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5474 : DOBUF_UNLOAD, eap->arg,
5475 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5476}
5477
5478/*
5479 * :[N]buffer [N] to buffer N
5480 * :[N]sbuffer [N] to buffer N
5481 */
5482 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005483ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484{
5485 if (*eap->arg)
5486 eap->errmsg = e_trailing;
5487 else
5488 {
5489 if (eap->addr_count == 0) /* default is current buffer */
5490 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5491 else
5492 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005493 if (eap->do_ecmd_cmd != NULL)
5494 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 }
5496}
5497
5498/*
5499 * :[N]bmodified [N] to next mod. buffer
5500 * :[N]sbmodified [N] to next mod. buffer
5501 */
5502 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005503ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504{
5505 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005506 if (eap->do_ecmd_cmd != NULL)
5507 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508}
5509
5510/*
5511 * :[N]bnext [N] to next buffer
5512 * :[N]sbnext [N] split and to next buffer
5513 */
5514 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005515ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516{
5517 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005518 if (eap->do_ecmd_cmd != NULL)
5519 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520}
5521
5522/*
5523 * :[N]bNext [N] to previous buffer
5524 * :[N]bprevious [N] to previous buffer
5525 * :[N]sbNext [N] split and to previous buffer
5526 * :[N]sbprevious [N] split and to previous buffer
5527 */
5528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005529ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530{
5531 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005532 if (eap->do_ecmd_cmd != NULL)
5533 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534}
5535
5536/*
5537 * :brewind to first buffer
5538 * :bfirst to first buffer
5539 * :sbrewind split and to first buffer
5540 * :sbfirst split and to first buffer
5541 */
5542 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005543ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544{
5545 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005546 if (eap->do_ecmd_cmd != NULL)
5547 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548}
5549
5550/*
5551 * :blast to last buffer
5552 * :sblast split and to last buffer
5553 */
5554 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005555ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556{
5557 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005558 if (eap->do_ecmd_cmd != NULL)
5559 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560}
5561#endif
5562
5563 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005564ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565{
5566 return (c == NUL || c == '|' || c == '"' || c == '\n');
5567}
5568
5569#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5570 || defined(PROTO)
5571/*
5572 * Return the next command, after the first '|' or '\n'.
5573 * Return NULL if not found.
5574 */
5575 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005576find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577{
5578 while (*p != '|' && *p != '\n')
5579 {
5580 if (*p == NUL)
5581 return NULL;
5582 ++p;
5583 }
5584 return (p + 1);
5585}
5586#endif
5587
5588/*
5589 * Check if *p is a separator between Ex commands.
5590 * Return NULL if it isn't, (p + 1) if it is.
5591 */
5592 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005593check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594{
5595 p = skipwhite(p);
5596 if (*p == '|' || *p == '\n')
5597 return (p + 1);
5598 else
5599 return NULL;
5600}
5601
5602/*
5603 * - if there are more files to edit
5604 * - and this is the last window
5605 * - and forceit not used
5606 * - and not repeated twice on a row
5607 * return FAIL and give error message if 'message' TRUE
5608 * return OK otherwise
5609 */
5610 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005611check_more(
5612 int message, /* when FALSE check only, no messages */
5613 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614{
5615 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5616
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005617 if (!forceit && only_one_window()
5618 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 {
5620 if (message)
5621 {
5622#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5623 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5624 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005625 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626
5627 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005628 vim_strncpy(buff,
5629 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005630 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005632 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 _("%d more files to edit. Quit anyway?"), n);
5634 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5635 return OK;
5636 return FAIL;
5637 }
5638#endif
5639 if (n == 1)
5640 EMSG(_("E173: 1 more file to edit"));
5641 else
5642 EMSGN(_("E173: %ld more files to edit"), n);
5643 quitmore = 2; /* next try to quit is allowed */
5644 }
5645 return FAIL;
5646 }
5647 return OK;
5648}
5649
5650#ifdef FEAT_CMDL_COMPL
5651/*
5652 * Function given to ExpandGeneric() to obtain the list of command names.
5653 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005655get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656{
5657 if (idx >= (int)CMD_SIZE)
5658# ifdef FEAT_USR_CMDS
5659 return get_user_command_name(idx);
5660# else
5661 return NULL;
5662# endif
5663 return cmdnames[idx].cmd_name;
5664}
5665#endif
5666
5667#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005668static 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);
5669static void uc_list(char_u *name, size_t name_len);
5670static 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);
5671static char_u *uc_split_args(char_u *arg, size_t *lenp);
5672static 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 +00005673
5674 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005675uc_add_command(
5676 char_u *name,
5677 size_t name_len,
5678 char_u *rep,
5679 long argt,
5680 long def,
5681 int flags,
5682 int compl,
5683 char_u *compl_arg,
5684 int addr_type,
5685 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686{
5687 ucmd_T *cmd = NULL;
5688 char_u *p;
5689 int i;
5690 int cmp = 1;
5691 char_u *rep_buf = NULL;
5692 garray_T *gap;
5693
Bram Moolenaar9c102382006-05-03 21:26:49 +00005694 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 if (rep_buf == NULL)
5696 {
5697 /* Can't replace termcodes - try using the string as is */
5698 rep_buf = vim_strsave(rep);
5699
5700 /* Give up if out of memory */
5701 if (rep_buf == NULL)
5702 return FAIL;
5703 }
5704
5705 /* get address of growarray: global or in curbuf */
5706 if (flags & UC_BUFFER)
5707 {
5708 gap = &curbuf->b_ucmds;
5709 if (gap->ga_itemsize == 0)
5710 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5711 }
5712 else
5713 gap = &ucmds;
5714
5715 /* Search for the command in the already defined commands. */
5716 for (i = 0; i < gap->ga_len; ++i)
5717 {
5718 size_t len;
5719
5720 cmd = USER_CMD_GA(gap, i);
5721 len = STRLEN(cmd->uc_name);
5722 cmp = STRNCMP(name, cmd->uc_name, name_len);
5723 if (cmp == 0)
5724 {
5725 if (name_len < len)
5726 cmp = -1;
5727 else if (name_len > len)
5728 cmp = 1;
5729 }
5730
5731 if (cmp == 0)
5732 {
5733 if (!force)
5734 {
5735 EMSG(_("E174: Command already exists: add ! to replace it"));
5736 goto fail;
5737 }
5738
5739 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005740 cmd->uc_rep = NULL;
5741#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5742 vim_free(cmd->uc_compl_arg);
5743 cmd->uc_compl_arg = NULL;
5744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 break;
5746 }
5747
5748 /* Stop as soon as we pass the name to add */
5749 if (cmp < 0)
5750 break;
5751 }
5752
5753 /* Extend the array unless we're replacing an existing command */
5754 if (cmp != 0)
5755 {
5756 if (ga_grow(gap, 1) != OK)
5757 goto fail;
5758 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5759 goto fail;
5760
5761 cmd = USER_CMD_GA(gap, i);
5762 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5763
5764 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765
5766 cmd->uc_name = p;
5767 }
5768
5769 cmd->uc_rep = rep_buf;
5770 cmd->uc_argt = argt;
5771 cmd->uc_def = def;
5772 cmd->uc_compl = compl;
5773#ifdef FEAT_EVAL
5774 cmd->uc_scriptID = current_SID;
5775# ifdef FEAT_CMDL_COMPL
5776 cmd->uc_compl_arg = compl_arg;
5777# endif
5778#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005779 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780
5781 return OK;
5782
5783fail:
5784 vim_free(rep_buf);
5785#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5786 vim_free(compl_arg);
5787#endif
5788 return FAIL;
5789}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005792#if defined(FEAT_USR_CMDS)
5793static struct
5794{
5795 int expand;
5796 char *name;
5797} addr_type_complete[] =
5798{
5799 {ADDR_ARGUMENTS, "arguments"},
5800 {ADDR_LINES, "lines"},
5801 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5802 {ADDR_TABS, "tabs"},
5803 {ADDR_BUFFERS, "buffers"},
5804 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005805 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005806 {-1, NULL}
5807};
5808#endif
5809
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005810#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811/*
5812 * List of names for completion for ":command" with the EXPAND_ flag.
5813 * Must be alphabetical for completion.
5814 */
5815static struct
5816{
5817 int expand;
5818 char *name;
5819} command_complete[] =
5820{
5821 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005822 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005824 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005826 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005827#if defined(FEAT_CSCOPE)
5828 {EXPAND_CSCOPE, "cscope"},
5829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5831 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005832 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833#endif
5834 {EXPAND_DIRECTORIES, "dir"},
5835 {EXPAND_ENV_VARS, "environment"},
5836 {EXPAND_EVENTS, "event"},
5837 {EXPAND_EXPRESSION, "expression"},
5838 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005839 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005840 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 {EXPAND_FUNCTIONS, "function"},
5842 {EXPAND_HELP, "help"},
5843 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005844#if defined(FEAT_CMDHIST)
5845 {EXPAND_HISTORY, "history"},
5846#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005847#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005848 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005849 {EXPAND_LOCALES, "locale"},
5850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 {EXPAND_MAPPINGS, "mapping"},
5852 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005853 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005854#if defined(FEAT_PROFILE)
5855 {EXPAND_SYNTIME, "syntime"},
5856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005858 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005859 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005860#if defined(FEAT_SIGNS)
5861 {EXPAND_SIGN, "sign"},
5862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 {EXPAND_TAGS, "tag"},
5864 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005865 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 {EXPAND_USER_VARS, "var"},
5867 {0, NULL}
5868};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005871#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005873uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874{
5875 int i, j;
5876 int found = FALSE;
5877 ucmd_T *cmd;
5878 int len;
5879 long a;
5880 garray_T *gap;
5881
5882 gap = &curbuf->b_ucmds;
5883 for (;;)
5884 {
5885 for (i = 0; i < gap->ga_len; ++i)
5886 {
5887 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005888 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889
5890 /* Skip commands which don't match the requested prefix */
5891 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5892 continue;
5893
5894 /* Put out the title first time */
5895 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005896 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 found = TRUE;
5898 msg_putchar('\n');
5899 if (got_int)
5900 break;
5901
5902 /* Special cases */
5903 msg_putchar(a & BANG ? '!' : ' ');
5904 msg_putchar(a & REGSTR ? '"' : ' ');
5905 msg_putchar(gap != &ucmds ? 'b' : ' ');
5906 msg_putchar(' ');
5907
5908 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5909 len = (int)STRLEN(cmd->uc_name) + 4;
5910
5911 do {
5912 msg_putchar(' ');
5913 ++len;
5914 } while (len < 16);
5915
5916 len = 0;
5917
5918 /* Arguments */
5919 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5920 {
5921 case 0: IObuff[len++] = '0'; break;
5922 case (EXTRA): IObuff[len++] = '*'; break;
5923 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5924 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5925 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5926 }
5927
5928 do {
5929 IObuff[len++] = ' ';
5930 } while (len < 5);
5931
5932 /* Range */
5933 if (a & (RANGE|COUNT))
5934 {
5935 if (a & COUNT)
5936 {
5937 /* -count=N */
5938 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5939 len += (int)STRLEN(IObuff + len);
5940 }
5941 else if (a & DFLALL)
5942 IObuff[len++] = '%';
5943 else if (cmd->uc_def >= 0)
5944 {
5945 /* -range=N */
5946 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5947 len += (int)STRLEN(IObuff + len);
5948 }
5949 else
5950 IObuff[len++] = '.';
5951 }
5952
5953 do {
5954 IObuff[len++] = ' ';
5955 } while (len < 11);
5956
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005957 /* Address Type */
5958 for (j = 0; addr_type_complete[j].expand != -1; ++j)
5959 if (addr_type_complete[j].expand != ADDR_LINES
5960 && addr_type_complete[j].expand == cmd->uc_addr_type)
5961 {
5962 STRCPY(IObuff + len, addr_type_complete[j].name);
5963 len += (int)STRLEN(IObuff + len);
5964 break;
5965 }
5966
5967 do {
5968 IObuff[len++] = ' ';
5969 } while (len < 21);
5970
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 /* Completion */
5972 for (j = 0; command_complete[j].expand != 0; ++j)
5973 if (command_complete[j].expand == cmd->uc_compl)
5974 {
5975 STRCPY(IObuff + len, command_complete[j].name);
5976 len += (int)STRLEN(IObuff + len);
5977 break;
5978 }
5979
5980 do {
5981 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005982 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983
5984 IObuff[len] = '\0';
5985 msg_outtrans(IObuff);
5986
5987 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005988#ifdef FEAT_EVAL
5989 if (p_verbose > 0)
5990 last_set_msg(cmd->uc_scriptID);
5991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 out_flush();
5993 ui_breakcheck();
5994 if (got_int)
5995 break;
5996 }
5997 if (gap == &ucmds || i < gap->ga_len)
5998 break;
5999 gap = &ucmds;
6000 }
6001
6002 if (!found)
6003 MSG(_("No user-defined commands found"));
6004}
6005
6006 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006007uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008{
6009 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6010 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6011 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6012 0xb9, 0x7f, 0};
6013 int i;
6014
6015 for (i = 0; fcmd[i]; ++i)
6016 IObuff[i] = fcmd[i] - 0x40;
6017 IObuff[i] = 0;
6018 return IObuff;
6019}
6020
6021 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006022uc_scan_attr(
6023 char_u *attr,
6024 size_t len,
6025 long *argt,
6026 long *def,
6027 int *flags,
6028 int *compl,
6029 char_u **compl_arg,
6030 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031{
6032 char_u *p;
6033
6034 if (len == 0)
6035 {
6036 EMSG(_("E175: No attribute specified"));
6037 return FAIL;
6038 }
6039
6040 /* First, try the simple attributes (no arguments) */
6041 if (STRNICMP(attr, "bang", len) == 0)
6042 *argt |= BANG;
6043 else if (STRNICMP(attr, "buffer", len) == 0)
6044 *flags |= UC_BUFFER;
6045 else if (STRNICMP(attr, "register", len) == 0)
6046 *argt |= REGSTR;
6047 else if (STRNICMP(attr, "bar", len) == 0)
6048 *argt |= TRLBAR;
6049 else
6050 {
6051 int i;
6052 char_u *val = NULL;
6053 size_t vallen = 0;
6054 size_t attrlen = len;
6055
6056 /* Look for the attribute name - which is the part before any '=' */
6057 for (i = 0; i < (int)len; ++i)
6058 {
6059 if (attr[i] == '=')
6060 {
6061 val = &attr[i + 1];
6062 vallen = len - i - 1;
6063 attrlen = i;
6064 break;
6065 }
6066 }
6067
6068 if (STRNICMP(attr, "nargs", attrlen) == 0)
6069 {
6070 if (vallen == 1)
6071 {
6072 if (*val == '0')
6073 /* Do nothing - this is the default */;
6074 else if (*val == '1')
6075 *argt |= (EXTRA | NOSPC | NEEDARG);
6076 else if (*val == '*')
6077 *argt |= EXTRA;
6078 else if (*val == '?')
6079 *argt |= (EXTRA | NOSPC);
6080 else if (*val == '+')
6081 *argt |= (EXTRA | NEEDARG);
6082 else
6083 goto wrong_nargs;
6084 }
6085 else
6086 {
6087wrong_nargs:
6088 EMSG(_("E176: Invalid number of arguments"));
6089 return FAIL;
6090 }
6091 }
6092 else if (STRNICMP(attr, "range", attrlen) == 0)
6093 {
6094 *argt |= RANGE;
6095 if (vallen == 1 && *val == '%')
6096 *argt |= DFLALL;
6097 else if (val != NULL)
6098 {
6099 p = val;
6100 if (*def >= 0)
6101 {
6102two_count:
6103 EMSG(_("E177: Count cannot be specified twice"));
6104 return FAIL;
6105 }
6106
6107 *def = getdigits(&p);
6108 *argt |= (ZEROR | NOTADR);
6109
6110 if (p != val + vallen || vallen == 0)
6111 {
6112invalid_count:
6113 EMSG(_("E178: Invalid default value for count"));
6114 return FAIL;
6115 }
6116 }
6117 }
6118 else if (STRNICMP(attr, "count", attrlen) == 0)
6119 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006120 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121
6122 if (val != NULL)
6123 {
6124 p = val;
6125 if (*def >= 0)
6126 goto two_count;
6127
6128 *def = getdigits(&p);
6129
6130 if (p != val + vallen)
6131 goto invalid_count;
6132 }
6133
6134 if (*def < 0)
6135 *def = 0;
6136 }
6137 else if (STRNICMP(attr, "complete", attrlen) == 0)
6138 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 if (val == NULL)
6140 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006141 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 return FAIL;
6143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006145 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6146 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006149 else if (STRNICMP(attr, "addr", attrlen) == 0)
6150 {
6151 *argt |= RANGE;
6152 if (val == NULL)
6153 {
6154 EMSG(_("E179: argument required for -addr"));
6155 return FAIL;
6156 }
6157 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6158 == FAIL)
6159 return FAIL;
6160 if (addr_type_arg != ADDR_LINES)
6161 *argt |= (ZEROR | NOTADR) ;
6162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 else
6164 {
6165 char_u ch = attr[len];
6166 attr[len] = '\0';
6167 EMSG2(_("E181: Invalid attribute: %s"), attr);
6168 attr[len] = ch;
6169 return FAIL;
6170 }
6171 }
6172
6173 return OK;
6174}
6175
Bram Moolenaara850a712009-01-28 14:42:59 +00006176/*
6177 * ":command ..."
6178 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006180ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181{
6182 char_u *name;
6183 char_u *end;
6184 char_u *p;
6185 long argt = 0;
6186 long def = -1;
6187 int flags = 0;
6188 int compl = EXPAND_NOTHING;
6189 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006190 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006192 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193
6194 p = eap->arg;
6195
6196 /* Check for attributes */
6197 while (*p == '-')
6198 {
6199 ++p;
6200 end = skiptowhite(p);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006201 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 == FAIL)
6203 return;
6204 p = skipwhite(end);
6205 }
6206
6207 /* Get the name (if any) and skip to the following argument */
6208 name = p;
6209 if (ASCII_ISALPHA(*p))
6210 while (ASCII_ISALNUM(*p))
6211 ++p;
6212 if (!ends_excmd(*p) && !vim_iswhite(*p))
6213 {
6214 EMSG(_("E182: Invalid command name"));
6215 return;
6216 }
6217 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006218 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219
6220 /* If there is nothing after the name, and no attributes were specified,
6221 * we are listing commands
6222 */
6223 p = skipwhite(end);
6224 if (!has_attr && ends_excmd(*p))
6225 {
6226 uc_list(name, end - name);
6227 }
6228 else if (!ASCII_ISUPPER(*name))
6229 {
6230 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6231 return;
6232 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006233 else if ((name_len == 1 && *name == 'X')
6234 || (name_len <= 4
6235 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6236 {
6237 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6238 return;
6239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 else
6241 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006242 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243}
6244
6245/*
6246 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 * Clear all user commands, global and for current buffer.
6248 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006249 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006250ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251{
6252 uc_clear(&ucmds);
6253 uc_clear(&curbuf->b_ucmds);
6254}
6255
6256/*
6257 * Clear all user commands for "gap".
6258 */
6259 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006260uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261{
6262 int i;
6263 ucmd_T *cmd;
6264
6265 for (i = 0; i < gap->ga_len; ++i)
6266 {
6267 cmd = USER_CMD_GA(gap, i);
6268 vim_free(cmd->uc_name);
6269 vim_free(cmd->uc_rep);
6270# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6271 vim_free(cmd->uc_compl_arg);
6272# endif
6273 }
6274 ga_clear(gap);
6275}
6276
6277 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006278ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279{
6280 int i = 0;
6281 ucmd_T *cmd = NULL;
6282 int cmp = -1;
6283 garray_T *gap;
6284
6285 gap = &curbuf->b_ucmds;
6286 for (;;)
6287 {
6288 for (i = 0; i < gap->ga_len; ++i)
6289 {
6290 cmd = USER_CMD_GA(gap, i);
6291 cmp = STRCMP(eap->arg, cmd->uc_name);
6292 if (cmp <= 0)
6293 break;
6294 }
6295 if (gap == &ucmds || cmp == 0)
6296 break;
6297 gap = &ucmds;
6298 }
6299
6300 if (cmp != 0)
6301 {
6302 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6303 return;
6304 }
6305
6306 vim_free(cmd->uc_name);
6307 vim_free(cmd->uc_rep);
6308# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6309 vim_free(cmd->uc_compl_arg);
6310# endif
6311
6312 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313
6314 if (i < gap->ga_len)
6315 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6316}
6317
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006318/*
6319 * split and quote args for <f-args>
6320 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006322uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323{
6324 char_u *buf;
6325 char_u *p;
6326 char_u *q;
6327 int len;
6328
6329 /* Precalculate length */
6330 p = arg;
6331 len = 2; /* Initial and final quotes */
6332
6333 while (*p)
6334 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006335 if (p[0] == '\\' && p[1] == '\\')
6336 {
6337 len += 2;
6338 p += 2;
6339 }
6340 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 {
6342 len += 1;
6343 p += 2;
6344 }
6345 else if (*p == '\\' || *p == '"')
6346 {
6347 len += 2;
6348 p += 1;
6349 }
6350 else if (vim_iswhite(*p))
6351 {
6352 p = skipwhite(p);
6353 if (*p == NUL)
6354 break;
6355 len += 3; /* "," */
6356 }
6357 else
6358 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006359#ifdef FEAT_MBYTE
6360 int charlen = (*mb_ptr2len)(p);
6361 len += charlen;
6362 p += charlen;
6363#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 ++len;
6365 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 }
6368 }
6369
6370 buf = alloc(len + 1);
6371 if (buf == NULL)
6372 {
6373 *lenp = 0;
6374 return buf;
6375 }
6376
6377 p = arg;
6378 q = buf;
6379 *q++ = '"';
6380 while (*p)
6381 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006382 if (p[0] == '\\' && p[1] == '\\')
6383 {
6384 *q++ = '\\';
6385 *q++ = '\\';
6386 p += 2;
6387 }
6388 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 {
6390 *q++ = p[1];
6391 p += 2;
6392 }
6393 else if (*p == '\\' || *p == '"')
6394 {
6395 *q++ = '\\';
6396 *q++ = *p++;
6397 }
6398 else if (vim_iswhite(*p))
6399 {
6400 p = skipwhite(p);
6401 if (*p == NUL)
6402 break;
6403 *q++ = '"';
6404 *q++ = ',';
6405 *q++ = '"';
6406 }
6407 else
6408 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006409 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 }
6411 }
6412 *q++ = '"';
6413 *q = 0;
6414
6415 *lenp = len;
6416 return buf;
6417}
6418
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006419 static size_t
6420add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6421{
6422 size_t result;
6423
6424 result = STRLEN(mod_str);
6425 if (*multi_mods)
6426 result += 1;
6427 if (buf != NULL)
6428 {
6429 if (*multi_mods)
6430 STRCAT(buf, " ");
6431 STRCAT(buf, mod_str);
6432 }
6433
6434 *multi_mods = 1;
6435
6436 return result;
6437}
6438
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439/*
6440 * Check for a <> code in a user command.
6441 * "code" points to the '<'. "len" the length of the <> (inclusive).
6442 * "buf" is where the result is to be added.
6443 * "split_buf" points to a buffer used for splitting, caller should free it.
6444 * "split_len" is the length of what "split_buf" contains.
6445 * Returns the length of the replacement, which has been added to "buf".
6446 * Returns -1 if there was no match, and only the "<" has been copied.
6447 */
6448 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006449uc_check_code(
6450 char_u *code,
6451 size_t len,
6452 char_u *buf,
6453 ucmd_T *cmd, /* the user command we're expanding */
6454 exarg_T *eap, /* ex arguments */
6455 char_u **split_buf,
6456 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457{
6458 size_t result = 0;
6459 char_u *p = code + 1;
6460 size_t l = len - 2;
6461 int quote = 0;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006462 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_MODS,
6463 ct_REGISTER, ct_LT, ct_NONE } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464
6465 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6466 {
6467 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6468 p += 2;
6469 l -= 2;
6470 }
6471
Bram Moolenaar371d5402006-03-20 21:47:49 +00006472 ++l;
6473 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006475 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006477 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006479 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006481 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006483 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006485 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006487 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006489 else if (STRNICMP(p, "mods>", l) == 0)
6490 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491
6492 switch (type)
6493 {
6494 case ct_ARGS:
6495 /* Simple case first */
6496 if (*eap->arg == NUL)
6497 {
6498 if (quote == 1)
6499 {
6500 result = 2;
6501 if (buf != NULL)
6502 STRCPY(buf, "''");
6503 }
6504 else
6505 result = 0;
6506 break;
6507 }
6508
6509 /* When specified there is a single argument don't split it.
6510 * Works for ":Cmd %" when % is "a b c". */
6511 if ((eap->argt & NOSPC) && quote == 2)
6512 quote = 1;
6513
6514 switch (quote)
6515 {
6516 case 0: /* No quoting, no splitting */
6517 result = STRLEN(eap->arg);
6518 if (buf != NULL)
6519 STRCPY(buf, eap->arg);
6520 break;
6521 case 1: /* Quote, but don't split */
6522 result = STRLEN(eap->arg) + 2;
6523 for (p = eap->arg; *p; ++p)
6524 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006525#ifdef FEAT_MBYTE
6526 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6527 /* DBCS can contain \ in a trail byte, skip the
6528 * double-byte character. */
6529 ++p;
6530 else
6531#endif
6532 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 ++result;
6534 }
6535
6536 if (buf != NULL)
6537 {
6538 *buf++ = '"';
6539 for (p = eap->arg; *p; ++p)
6540 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006541#ifdef FEAT_MBYTE
6542 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6543 /* DBCS can contain \ in a trail byte, copy the
6544 * double-byte character to avoid escaping. */
6545 *buf++ = *p++;
6546 else
6547#endif
6548 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 *buf++ = '\\';
6550 *buf++ = *p;
6551 }
6552 *buf = '"';
6553 }
6554
6555 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006556 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 /* This is hard, so only do it once, and cache the result */
6558 if (*split_buf == NULL)
6559 *split_buf = uc_split_args(eap->arg, split_len);
6560
6561 result = *split_len;
6562 if (buf != NULL && result != 0)
6563 STRCPY(buf, *split_buf);
6564
6565 break;
6566 }
6567 break;
6568
6569 case ct_BANG:
6570 result = eap->forceit ? 1 : 0;
6571 if (quote)
6572 result += 2;
6573 if (buf != NULL)
6574 {
6575 if (quote)
6576 *buf++ = '"';
6577 if (eap->forceit)
6578 *buf++ = '!';
6579 if (quote)
6580 *buf = '"';
6581 }
6582 break;
6583
6584 case ct_LINE1:
6585 case ct_LINE2:
6586 case ct_COUNT:
6587 {
6588 char num_buf[20];
6589 long num = (type == ct_LINE1) ? eap->line1 :
6590 (type == ct_LINE2) ? eap->line2 :
6591 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6592 size_t num_len;
6593
6594 sprintf(num_buf, "%ld", num);
6595 num_len = STRLEN(num_buf);
6596 result = num_len;
6597
6598 if (quote)
6599 result += 2;
6600
6601 if (buf != NULL)
6602 {
6603 if (quote)
6604 *buf++ = '"';
6605 STRCPY(buf, num_buf);
6606 buf += num_len;
6607 if (quote)
6608 *buf = '"';
6609 }
6610
6611 break;
6612 }
6613
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006614 case ct_MODS:
6615 {
6616 int multi_mods = 0;
6617 typedef struct {
6618 int *varp;
6619 char *name;
6620 } mod_entry_T;
6621 static mod_entry_T mod_entries[] = {
6622#ifdef FEAT_BROWSE_CMD
6623 {&cmdmod.browse, "browse"},
6624#endif
6625#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6626 {&cmdmod.confirm, "confirm"},
6627#endif
6628 {&cmdmod.hide, "hide"},
6629 {&cmdmod.keepalt, "keepalt"},
6630 {&cmdmod.keepjumps, "keepjumps"},
6631 {&cmdmod.keepmarks, "keepmarks"},
6632 {&cmdmod.keeppatterns, "keeppatterns"},
6633 {&cmdmod.lockmarks, "lockmarks"},
6634 {&cmdmod.noswapfile, "noswapfile"},
6635 {NULL, NULL}
6636 };
6637 int i;
6638
6639 result = quote ? 2 : 0;
6640 if (buf != NULL)
6641 {
6642 if (quote)
6643 *buf++ = '"';
6644 *buf = '\0';
6645 }
6646
6647#ifdef FEAT_WINDOWS
6648 /* :aboveleft and :leftabove */
6649 if (cmdmod.split & WSP_ABOVE)
6650 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6651 /* :belowright and :rightbelow */
6652 if (cmdmod.split & WSP_BELOW)
6653 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6654 /* :botright */
6655 if (cmdmod.split & WSP_BOT)
6656 result += add_cmd_modifier(buf, "botright", &multi_mods);
6657#endif
6658
6659 /* the modifiers that are simple flags */
6660 for (i = 0; mod_entries[i].varp != NULL; ++i)
6661 if (*mod_entries[i].varp)
6662 result += add_cmd_modifier(buf, mod_entries[i].name,
6663 &multi_mods);
6664
6665 /* TODO: How to support :noautocmd? */
6666#ifdef HAVE_SANDBOX
6667 /* TODO: How to support :sandbox?*/
6668#endif
6669 /* :silent */
6670 if (msg_silent > 0)
6671 result += add_cmd_modifier(buf,
6672 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
6673#ifdef FEAT_WINDOWS
6674 /* :tab */
6675 if (cmdmod.tab > 0)
6676 result += add_cmd_modifier(buf, "tab", &multi_mods);
6677 /* :topleft */
6678 if (cmdmod.split & WSP_TOP)
6679 result += add_cmd_modifier(buf, "topleft", &multi_mods);
6680#endif
6681 /* TODO: How to support :unsilent?*/
6682 /* :verbose */
6683 if (p_verbose > 0)
6684 result += add_cmd_modifier(buf, "verbose", &multi_mods);
6685#ifdef FEAT_WINDOWS
6686 /* :vertical */
6687 if (cmdmod.split & WSP_VERT)
6688 result += add_cmd_modifier(buf, "vertical", &multi_mods);
6689#endif
6690 if (quote && buf != NULL)
6691 {
6692 buf += result - 2;
6693 *buf = '"';
6694 }
6695 break;
6696 }
6697
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 case ct_REGISTER:
6699 result = eap->regname ? 1 : 0;
6700 if (quote)
6701 result += 2;
6702 if (buf != NULL)
6703 {
6704 if (quote)
6705 *buf++ = '\'';
6706 if (eap->regname)
6707 *buf++ = eap->regname;
6708 if (quote)
6709 *buf = '\'';
6710 }
6711 break;
6712
6713 case ct_LT:
6714 result = 1;
6715 if (buf != NULL)
6716 *buf = '<';
6717 break;
6718
6719 default:
6720 /* Not recognized: just copy the '<' and return -1. */
6721 result = (size_t)-1;
6722 if (buf != NULL)
6723 *buf = '<';
6724 break;
6725 }
6726
6727 return result;
6728}
6729
6730 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006731do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732{
6733 char_u *buf;
6734 char_u *p;
6735 char_u *q;
6736
6737 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006738 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006739 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 size_t len, totlen;
6741
6742 size_t split_len = 0;
6743 char_u *split_buf = NULL;
6744 ucmd_T *cmd;
6745#ifdef FEAT_EVAL
6746 scid_T save_current_SID = current_SID;
6747#endif
6748
6749 if (eap->cmdidx == CMD_USER)
6750 cmd = USER_CMD(eap->useridx);
6751 else
6752 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6753
6754 /*
6755 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006756 * First round: "buf" is NULL, compute length, allocate "buf".
6757 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 */
6759 buf = NULL;
6760 for (;;)
6761 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006762 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006763 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006765
6766 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006768 start = vim_strchr(p, '<');
6769 if (start != NULL)
6770 end = vim_strchr(start + 1, '>');
6771 if (buf != NULL)
6772 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006773 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6774 ;
6775 if (*ksp == K_SPECIAL
6776 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006777 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6778# ifdef FEAT_GUI
6779 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6780# endif
6781 ))
6782 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006783 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006784 * KS_SPECIAL KE_FILLER, like for mappings, but
6785 * do_cmdline() doesn't handle that, so convert it back.
6786 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6787 len = ksp - p;
6788 if (len > 0)
6789 {
6790 mch_memmove(q, p, len);
6791 q += len;
6792 }
6793 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6794 p = ksp + 3;
6795 continue;
6796 }
6797 }
6798
6799 /* break if there no <item> is found */
6800 if (start == NULL || end == NULL)
6801 break;
6802
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 /* Include the '>' */
6804 ++end;
6805
6806 /* Take everything up to the '<' */
6807 len = start - p;
6808 if (buf == NULL)
6809 totlen += len;
6810 else
6811 {
6812 mch_memmove(q, p, len);
6813 q += len;
6814 }
6815
6816 len = uc_check_code(start, end - start, q, cmd, eap,
6817 &split_buf, &split_len);
6818 if (len == (size_t)-1)
6819 {
6820 /* no match, continue after '<' */
6821 p = start + 1;
6822 len = 1;
6823 }
6824 else
6825 p = end;
6826 if (buf == NULL)
6827 totlen += len;
6828 else
6829 q += len;
6830 }
6831 if (buf != NULL) /* second time here, finished */
6832 {
6833 STRCPY(q, p);
6834 break;
6835 }
6836
6837 totlen += STRLEN(p); /* Add on the trailing characters */
6838 buf = alloc((unsigned)(totlen + 1));
6839 if (buf == NULL)
6840 {
6841 vim_free(split_buf);
6842 return;
6843 }
6844 }
6845
6846#ifdef FEAT_EVAL
6847 current_SID = cmd->uc_scriptID;
6848#endif
6849 (void)do_cmdline(buf, eap->getline, eap->cookie,
6850 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6851#ifdef FEAT_EVAL
6852 current_SID = save_current_SID;
6853#endif
6854 vim_free(buf);
6855 vim_free(split_buf);
6856}
6857
6858# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6859 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006860get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861{
6862 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6863}
6864
6865/*
6866 * Function given to ExpandGeneric() to obtain the list of user command names.
6867 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006869get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870{
6871 if (idx < curbuf->b_ucmds.ga_len)
6872 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6873 idx -= curbuf->b_ucmds.ga_len;
6874 if (idx < ucmds.ga_len)
6875 return USER_CMD(idx)->uc_name;
6876 return NULL;
6877}
6878
6879/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006880 * Function given to ExpandGeneric() to obtain the list of user address type names.
6881 */
6882 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006883get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006884{
6885 return (char_u *)addr_type_complete[idx].name;
6886}
6887
6888/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 * Function given to ExpandGeneric() to obtain the list of user command
6890 * attributes.
6891 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006893get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894{
6895 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006896 {"addr", "bang", "bar", "buffer", "complete",
6897 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006899 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 return NULL;
6901 return (char_u *)user_cmd_flags[idx];
6902}
6903
6904/*
6905 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6906 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006908get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909{
6910 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6911
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006912 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 return NULL;
6914 return (char_u *)user_cmd_nargs[idx];
6915}
6916
6917/*
6918 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006921get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922{
6923 return (char_u *)command_complete[idx].name;
6924}
6925# endif /* FEAT_CMDL_COMPL */
6926
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006927/*
6928 * Parse address type argument
6929 */
6930 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006931parse_addr_type_arg(
6932 char_u *value,
6933 int vallen,
6934 long *argt,
6935 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006936{
6937 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01006938
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006939 for (i = 0; addr_type_complete[i].expand != -1; ++i)
6940 {
6941 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
6942 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
6943 if (a && b)
6944 {
6945 *addr_type_arg = addr_type_complete[i].expand;
6946 break;
6947 }
6948 }
6949
6950 if (addr_type_complete[i].expand == -1)
6951 {
6952 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01006953
6954 for (i = 0; err[i] != NUL && !vim_iswhite(err[i]); i++)
6955 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006956 err[i] = NUL;
6957 EMSG2(_("E180: Invalid address type value: %s"), err);
6958 return FAIL;
6959 }
6960
6961 if (*addr_type_arg != ADDR_LINES)
6962 *argt |= NOTADR;
6963
6964 return OK;
6965}
6966
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967#endif /* FEAT_USR_CMDS */
6968
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006969#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6970/*
6971 * Parse a completion argument "value[vallen]".
6972 * The detected completion goes in "*complp", argument type in "*argt".
6973 * When there is an argument, for function and user defined completion, it's
6974 * copied to allocated memory and stored in "*compl_arg".
6975 * Returns FAIL if something is wrong.
6976 */
6977 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006978parse_compl_arg(
6979 char_u *value,
6980 int vallen,
6981 int *complp,
6982 long *argt,
6983 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006984{
6985 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006986# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006987 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006988# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006989 int i;
6990 int valend = vallen;
6991
6992 /* Look for any argument part - which is the part after any ',' */
6993 for (i = 0; i < vallen; ++i)
6994 {
6995 if (value[i] == ',')
6996 {
6997 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006998# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006999 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007000# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007001 valend = i;
7002 break;
7003 }
7004 }
7005
7006 for (i = 0; command_complete[i].expand != 0; ++i)
7007 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007008 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007009 && STRNCMP(value, command_complete[i].name, valend) == 0)
7010 {
7011 *complp = command_complete[i].expand;
7012 if (command_complete[i].expand == EXPAND_BUFFERS)
7013 *argt |= BUFNAME;
7014 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7015 || command_complete[i].expand == EXPAND_FILES)
7016 *argt |= XFILE;
7017 break;
7018 }
7019 }
7020
7021 if (command_complete[i].expand == 0)
7022 {
7023 EMSG2(_("E180: Invalid complete value: %s"), value);
7024 return FAIL;
7025 }
7026
7027# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7028 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7029 && arg != NULL)
7030# else
7031 if (arg != NULL)
7032# endif
7033 {
7034 EMSG(_("E468: Completion argument only allowed for custom completion"));
7035 return FAIL;
7036 }
7037
7038# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7039 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7040 && arg == NULL)
7041 {
7042 EMSG(_("E467: Custom completion requires a function argument"));
7043 return FAIL;
7044 }
7045
7046 if (arg != NULL)
7047 *compl_arg = vim_strnsave(arg, (int)arglen);
7048# endif
7049 return OK;
7050}
7051#endif
7052
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007054ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055{
Bram Moolenaare6850792010-05-14 15:28:44 +02007056 if (*eap->arg == NUL)
7057 {
7058#ifdef FEAT_EVAL
7059 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7060 char_u *p = NULL;
7061
7062 if (expr != NULL)
7063 {
7064 ++emsg_off;
7065 p = eval_to_string(expr, NULL, FALSE);
7066 --emsg_off;
7067 vim_free(expr);
7068 }
7069 if (p != NULL)
7070 {
7071 MSG(p);
7072 vim_free(p);
7073 }
7074 else
7075 MSG("default");
7076#else
7077 MSG(_("unknown"));
7078#endif
7079 }
7080 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007081 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082}
7083
7084 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007085ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086{
7087 if (*eap->arg == NUL && eap->cmd[2] == '!')
7088 MSG(_("Greetings, Vim user!"));
7089 do_highlight(eap->arg, eap->forceit, FALSE);
7090}
7091
7092
7093/*
7094 * Call this function if we thought we were going to exit, but we won't
7095 * (because of an error). May need to restore the terminal mode.
7096 */
7097 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007098not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099{
7100 exiting = FALSE;
7101 settmode(TMODE_RAW);
7102}
7103
7104/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007105 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 */
7107 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007108ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109{
Bram Moolenaarf240e182014-11-27 18:33:02 +01007110#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007111 win_T *wp;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007112#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007113
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114#ifdef FEAT_CMDWIN
7115 if (cmdwin_type != 0)
7116 {
7117 cmdwin_result = Ctrl_C;
7118 return;
7119 }
7120#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007121 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007122 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007123 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007124 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007125 return;
7126 }
Bram Moolenaarf240e182014-11-27 18:33:02 +01007127#ifdef FEAT_WINDOWS
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007128 if (eap->addr_count > 0)
7129 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007130 int wnr = eap->line2;
7131
7132 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7133 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007134 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007135 }
7136 else
Bram Moolenaarf240e182014-11-27 18:33:02 +01007137#endif
7138#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007139 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007140#endif
7141
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007142#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02007143 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007144 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02007145 * being closed (can only happen in autocommands). */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007146 if (curbuf_locked() || (wp->w_buffer->b_nwindows == 1
7147 && wp->w_buffer->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007148 return;
7149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150
7151#ifdef FEAT_NETBEANS_INTG
7152 netbeansForcedQuit = eap->forceit;
7153#endif
7154
7155 /*
7156 * If there are more files or windows we won't exit.
7157 */
7158 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7159 exiting = TRUE;
7160 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007161 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7162 | (eap->forceit ? CCGD_FORCEIT : 0)
7163 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007165 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 {
7167 not_exiting();
7168 }
7169 else
7170 {
7171#ifdef FEAT_WINDOWS
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007172 /* quit last window
7173 * Note: only_one_window() returns true, even so a help window is
7174 * still open. In that case only quit, if no address has been
7175 * specified. Example:
7176 * :h|wincmd w|1q - don't quit
7177 * :h|wincmd w|q - quit
7178 */
7179 if (only_one_window() && (firstwin == lastwin || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180#endif
7181 getout(0);
7182#ifdef FEAT_WINDOWS
7183# ifdef FEAT_GUI
7184 need_mouse_correct = TRUE;
7185# endif
7186 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007187 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188#endif
7189 }
7190}
7191
7192/*
7193 * ":cquit".
7194 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007196ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197{
7198 getout(1); /* this does not always pass on the exit code to the Manx
7199 compiler. why? */
7200}
7201
7202/*
7203 * ":qall": try to quit all windows
7204 */
7205 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007206ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207{
7208# ifdef FEAT_CMDWIN
7209 if (cmdwin_type != 0)
7210 {
7211 if (eap->forceit)
7212 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7213 else
7214 cmdwin_result = K_XF2;
7215 return;
7216 }
7217# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007218
7219 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007220 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007221 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007222 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007223 return;
7224 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007225#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007226 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7227 /* Refuse to quit when locked or when the buffer in the last window is
7228 * being closed (can only happen in autocommands). */
7229 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007230 return;
7231#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007232
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007234 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 getout(0);
7236 not_exiting();
7237}
7238
Bram Moolenaard9967712006-03-11 21:18:15 +00007239#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240/*
7241 * ":close": close current window, unless it is the last one
7242 */
7243 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007244ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007246 win_T *win;
7247 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248# ifdef FEAT_CMDWIN
7249 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007250 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 else
7252# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007253 if (!text_locked()
7254#ifdef FEAT_AUTOCMD
7255 && !curbuf_locked()
7256#endif
7257 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007258 {
7259 if (eap->addr_count == 0)
7260 ex_win_close(eap->forceit, curwin, NULL);
7261 else {
7262 for (win = firstwin; win != NULL; win = win->w_next)
7263 {
7264 winnr++;
7265 if (winnr == eap->line2)
7266 break;
7267 }
7268 if (win == NULL)
7269 win = lastwin;
7270 ex_win_close(eap->forceit, win, NULL);
7271 }
7272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273}
7274
Bram Moolenaard9967712006-03-11 21:18:15 +00007275# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007276/*
7277 * ":pclose": Close any preview window.
7278 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007280ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007281{
7282 win_T *win;
7283
7284 for (win = firstwin; win != NULL; win = win->w_next)
7285 if (win->w_p_pvw)
7286 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007287 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007288 break;
7289 }
7290}
Bram Moolenaard9967712006-03-11 21:18:15 +00007291# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007292
Bram Moolenaarf740b292006-02-16 22:11:02 +00007293/*
7294 * Close window "win" and take care of handling closing the last window for a
7295 * modified buffer.
7296 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007297 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007298ex_win_close(
7299 int forceit,
7300 win_T *win,
7301 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302{
7303 int need_hide;
7304 buf_T *buf = win->w_buffer;
7305
7306 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007307 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007309# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 if ((p_confirm || cmdmod.confirm) && p_write)
7311 {
7312 dialog_changed(buf, FALSE);
7313 if (buf_valid(buf) && bufIsChanged(buf))
7314 return;
7315 need_hide = FALSE;
7316 }
7317 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007318# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 {
7320 EMSG(_(e_nowrtmsg));
7321 return;
7322 }
7323 }
7324
Bram Moolenaard9967712006-03-11 21:18:15 +00007325# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007327# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007328
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007330 if (tp == NULL)
7331 win_close(win, !need_hide && !P_HID(buf));
7332 else
7333 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334}
7335
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007337 * ":tabclose": close current tab page, unless it is the last one.
7338 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 */
7340 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007341ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007343 tabpage_T *tp;
7344
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007345# ifdef FEAT_CMDWIN
7346 if (cmdwin_type != 0)
7347 cmdwin_result = K_IGNORE;
7348 else
7349# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007350 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007351 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007352 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007354 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007355 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007356 tp = find_tabpage((int)eap->line2);
7357 if (tp == NULL)
7358 {
7359 beep_flush();
7360 return;
7361 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007362 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007363 {
7364 tabpage_close_other(tp, eap->forceit);
7365 return;
7366 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007367 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007368 if (!text_locked()
7369#ifdef FEAT_AUTOCMD
7370 && !curbuf_locked()
7371#endif
7372 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00007373 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007375}
7376
7377/*
7378 * ":tabonly": close all tab pages except the current one
7379 */
7380 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007381ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007382{
7383 tabpage_T *tp;
7384 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007385
7386# ifdef FEAT_CMDWIN
7387 if (cmdwin_type != 0)
7388 cmdwin_result = K_IGNORE;
7389 else
7390# endif
7391 if (first_tabpage->tp_next == NULL)
7392 MSG(_("Already only one tab page"));
7393 else
7394 {
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007395 if (eap->addr_count > 0)
7396 goto_tabpage(eap->line2);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007397 /* Repeat this up to a 1000 times, because autocommands may mess
7398 * up the lists. */
7399 for (done = 0; done < 1000; ++done)
7400 {
7401 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7402 if (tp->tp_topframe != topframe)
7403 {
7404 tabpage_close_other(tp, eap->forceit);
7405 /* if we failed to close it quit */
7406 if (valid_tabpage(tp))
7407 done = 1000;
7408 /* start over, "tp" is now invalid */
7409 break;
7410 }
7411 if (first_tabpage->tp_next == NULL)
7412 break;
7413 }
7414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416
7417/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007418 * Close the current tab page.
7419 */
7420 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007421tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007422{
7423 /* First close all the windows but the current one. If that worked then
7424 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007425 if (lastwin != firstwin)
7426 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007427 if (lastwin == firstwin)
7428 ex_win_close(forceit, curwin, NULL);
7429# ifdef FEAT_GUI
7430 need_mouse_correct = TRUE;
7431# endif
7432}
7433
7434/*
7435 * Close tab page "tp", which is not the current tab page.
7436 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007437 * Also takes care of the tab pages line disappearing when closing the
7438 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007439 */
7440 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007441tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007442{
7443 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007444 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007445 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007446
7447 /* Limit to 1000 windows, autocommands may add a window while we close
7448 * one. OK, so I'm paranoid... */
7449 while (++done < 1000)
7450 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007451 wp = tp->tp_firstwin;
7452 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007453
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007454 /* Autocommands may delete the tab page under our fingers and we may
7455 * fail to close a window with a modified buffer. */
7456 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007457 break;
7458 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007459
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007460 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007461 if (h != tabline_height())
7462 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007463}
7464
7465/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 * ":only".
7467 */
7468 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007469ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007471 win_T *wp;
7472 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473# ifdef FEAT_GUI
7474 need_mouse_correct = TRUE;
7475# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007476 if (eap->addr_count > 0)
7477 {
7478 wnr = eap->line2;
7479 for (wp = firstwin; --wnr > 0; )
7480 {
7481 if (wp->w_next == NULL)
7482 break;
7483 else
7484 wp = wp->w_next;
7485 }
7486 win_goto(wp);
7487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 close_others(TRUE, eap->forceit);
7489}
7490
7491/*
7492 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007493 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007495 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007496ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497{
7498 if (eap->addr_count == 0)
7499 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007500 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501}
7502#endif /* FEAT_WINDOWS */
7503
7504 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007505ex_hide(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506{
7507 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
7508 eap->errmsg = e_invarg;
7509 else
7510 {
7511 /* ":hide" or ":hide | cmd": hide current window */
7512 eap->nextcmd = check_nextcmd(eap->arg);
7513#ifdef FEAT_WINDOWS
7514 if (!eap->skip)
7515 {
7516# ifdef FEAT_GUI
7517 need_mouse_correct = TRUE;
7518# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007519 if (eap->addr_count == 0)
7520 win_close(curwin, FALSE); /* don't free buffer */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007521 else
7522 {
7523 int winnr = 0;
7524 win_T *win;
7525
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007526 for (win = firstwin; win != NULL; win = win->w_next)
7527 {
7528 winnr++;
7529 if (winnr == eap->line2)
7530 break;
7531 }
7532 if (win == NULL)
7533 win = lastwin;
7534 win_close(win, FALSE);
7535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 }
7537#endif
7538 }
7539}
7540
7541/*
7542 * ":stop" and ":suspend": Suspend Vim.
7543 */
7544 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007545ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546{
7547 /*
7548 * Disallow suspending for "rvim".
7549 */
7550 if (!check_restricted()
7551#ifdef WIN3264
7552 /*
7553 * Check if external commands are allowed now.
7554 */
7555 && can_end_termcap_mode(TRUE)
7556#endif
7557 )
7558 {
7559 if (!eap->forceit)
7560 autowrite_all();
7561 windgoto((int)Rows - 1, 0);
7562 out_char('\n');
7563 out_flush();
7564 stoptermcap();
7565 out_flush(); /* needed for SUN to restore xterm buffer */
7566#ifdef FEAT_TITLE
7567 mch_restore_title(3); /* restore window titles */
7568#endif
7569 ui_suspend(); /* call machine specific function */
7570#ifdef FEAT_TITLE
7571 maketitle();
7572 resettitle(); /* force updating the title */
7573#endif
7574 starttermcap();
7575 scroll_start(); /* scroll screen before redrawing */
7576 redraw_later_clear();
7577 shell_resized(); /* may have resized window */
7578 }
7579}
7580
7581/*
7582 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7583 */
7584 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007585ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586{
7587#ifdef FEAT_CMDWIN
7588 if (cmdwin_type != 0)
7589 {
7590 cmdwin_result = Ctrl_C;
7591 return;
7592 }
7593#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007594 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007595 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007596 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007597 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007598 return;
7599 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007600#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007601 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7602 /* Refuse to quit when locked or when the buffer in the last window is
7603 * being closed (can only happen in autocommands). */
7604 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007605 return;
7606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607
7608 /*
7609 * if more files or windows we won't exit
7610 */
7611 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7612 exiting = TRUE;
7613 if ( ((eap->cmdidx == CMD_wq
7614 || curbufIsChanged())
7615 && do_write(eap) == FAIL)
7616 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007617 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 {
7619 not_exiting();
7620 }
7621 else
7622 {
7623#ifdef FEAT_WINDOWS
7624 if (only_one_window()) /* quit last window, exit Vim */
7625#endif
7626 getout(0);
7627#ifdef FEAT_WINDOWS
7628# ifdef FEAT_GUI
7629 need_mouse_correct = TRUE;
7630# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007631 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 win_close(curwin, !P_HID(curwin->w_buffer));
7633#endif
7634 }
7635}
7636
7637/*
7638 * ":print", ":list", ":number".
7639 */
7640 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007641ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007643 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7644 EMSG(_(e_emptybuf));
7645 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007647 for ( ;!got_int; ui_breakcheck())
7648 {
7649 print_line(eap->line1,
7650 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7651 || (eap->flags & EXFLAG_NR)),
7652 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7653 if (++eap->line1 > eap->line2)
7654 break;
7655 out_flush(); /* show one line at a time */
7656 }
7657 setpcmark();
7658 /* put cursor at last line */
7659 curwin->w_cursor.lnum = eap->line2;
7660 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 }
7662
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664}
7665
7666#ifdef FEAT_BYTEOFF
7667 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007668ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669{
7670 goto_byte(eap->line2);
7671}
7672#endif
7673
7674/*
7675 * ":shell".
7676 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007678ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679{
7680 do_shell(NULL, 0);
7681}
7682
7683#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7684 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007685 || defined(FEAT_GUI_MSWIN) \
7686 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 || defined(PROTO)
7688
7689/*
7690 * Handle a file drop. The code is here because a drop is *nearly* like an
7691 * :args command, but not quite (we have a list of exact filenames, so we
7692 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7693 * code is very similar to :args and hence needs access to a lot of the static
7694 * functions in this file.
7695 *
7696 * The list should be allocated using alloc(), as should each item in the
7697 * list. This function takes over responsibility for freeing the list.
7698 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007699 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7701 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7702 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7703 * file functionality is (currently) not in EMX this is not presently a
7704 * problem.
7705 */
7706 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007707handle_drop(
7708 int filec, /* the number of files dropped */
7709 char_u **filev, /* the list of files dropped */
7710 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711{
7712 exarg_T ea;
7713 int save_msg_scroll = msg_scroll;
7714
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007715 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007716 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007718#ifdef FEAT_AUTOCMD
7719 if (curbuf_locked())
7720 return;
7721#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007722 /* When the screen is being updated we should not change buffers and
7723 * windows structures, it may cause freed memory to be used. */
7724 if (updating_screen)
7725 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726
7727 /* Check whether the current buffer is changed. If so, we will need
7728 * to split the current window or data could be lost.
7729 * We don't need to check if the 'hidden' option is set, as in this
7730 * case the buffer won't be lost.
7731 */
7732 if (!P_HID(curbuf) && !split)
7733 {
7734 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007735 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 --emsg_off;
7737 }
7738 if (split)
7739 {
7740# ifdef FEAT_WINDOWS
7741 if (win_split(0, 0) == FAIL)
7742 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007743 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744
7745 /* When splitting the window, create a new alist. Otherwise the
7746 * existing one is overwritten. */
7747 alist_unlink(curwin->w_alist);
7748 alist_new();
7749# else
7750 return; /* can't split, always fail */
7751# endif
7752 }
7753
7754 /*
7755 * Set up the new argument list.
7756 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007757 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758
7759 /*
7760 * Move to the first file.
7761 */
7762 /* Fake up a minimal "next" command for do_argfile() */
7763 vim_memset(&ea, 0, sizeof(ea));
7764 ea.cmd = (char_u *)"next";
7765 do_argfile(&ea, 0);
7766
7767 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7768 * mode that is not needed here. */
7769 need_start_insertmode = FALSE;
7770
7771 /* Restore msg_scroll, otherwise a following command may cause scrolling
7772 * unexpectedly. The screen will be redrawn by the caller, thus
7773 * msg_scroll being set by displaying a message is irrelevant. */
7774 msg_scroll = save_msg_scroll;
7775}
7776#endif
7777
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778/*
7779 * Clear an argument list: free all file names and reset it to zero entries.
7780 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007781 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007782alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783{
7784 while (--al->al_ga.ga_len >= 0)
7785 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7786 ga_clear(&al->al_ga);
7787}
7788
7789/*
7790 * Init an argument list.
7791 */
7792 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007793alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794{
7795 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7796}
7797
7798#if defined(FEAT_WINDOWS) || defined(PROTO)
7799
7800/*
7801 * Remove a reference from an argument list.
7802 * Ignored when the argument list is the global one.
7803 * If the argument list is no longer used by any window, free it.
7804 */
7805 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007806alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807{
7808 if (al != &global_alist && --al->al_refcount <= 0)
7809 {
7810 alist_clear(al);
7811 vim_free(al);
7812 }
7813}
7814
7815# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7816/*
7817 * Create a new argument list and use it for the current window.
7818 */
7819 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007820alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821{
7822 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7823 if (curwin->w_alist == NULL)
7824 {
7825 curwin->w_alist = &global_alist;
7826 ++global_alist.al_refcount;
7827 }
7828 else
7829 {
7830 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007831 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 alist_init(curwin->w_alist);
7833 }
7834}
7835# endif
7836#endif
7837
Bram Moolenaar53076832015-12-31 19:53:21 +01007838#if (!defined(UNIX) && !defined(__EMX__)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839/*
7840 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007841 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7842 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 */
7844 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007845alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846{
7847 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007848 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 char_u **new_arg_files;
7850 int new_arg_file_count;
7851 char_u *save_p_su = p_su;
7852 int i;
7853
7854 /* Don't use 'suffixes' here. This should work like the shell did the
7855 * expansion. Also, the vimrc file isn't read yet, thus the user
7856 * can't set the options. */
7857 p_su = empty_option;
7858 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7859 if (old_arg_files != NULL)
7860 {
7861 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007862 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7863 old_arg_count = GARGCOUNT;
7864 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007866 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 && new_arg_file_count > 0)
7868 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007869 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7870 TRUE, fnum_list, fnum_len);
7871 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 }
7874 p_su = save_p_su;
7875}
7876#endif
7877
7878/*
7879 * Set the argument list for the current window.
7880 * Takes over the allocated files[] and the allocated fnames in it.
7881 */
7882 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007883alist_set(
7884 alist_T *al,
7885 int count,
7886 char_u **files,
7887 int use_curbuf,
7888 int *fnum_list,
7889 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890{
7891 int i;
7892
7893 alist_clear(al);
7894 if (ga_grow(&al->al_ga, count) == OK)
7895 {
7896 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007897 {
7898 if (got_int)
7899 {
7900 /* When adding many buffers this can take a long time. Allow
7901 * interrupting here. */
7902 while (i < count)
7903 vim_free(files[i++]);
7904 break;
7905 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007906
7907 /* May set buffer name of a buffer previously used for the
7908 * argument list, so that it's re-used by alist_add. */
7909 if (fnum_list != NULL && i < fnum_len)
7910 buf_set_name(fnum_list[i], files[i]);
7911
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007913 ui_breakcheck();
7914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 vim_free(files);
7916 }
7917 else
7918 FreeWild(count, files);
7919#ifdef FEAT_WINDOWS
7920 if (al == &global_alist)
7921#endif
7922 arg_had_last = FALSE;
7923}
7924
7925/*
7926 * Add file "fname" to argument list "al".
7927 * "fname" must have been allocated and "al" must have been checked for room.
7928 */
7929 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007930alist_add(
7931 alist_T *al,
7932 char_u *fname,
7933 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934{
7935 if (fname == NULL) /* don't add NULL file names */
7936 return;
7937#ifdef BACKSLASH_IN_FILENAME
7938 slash_adjust(fname);
7939#endif
7940 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7941 if (set_fnum > 0)
7942 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7943 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7944 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945}
7946
7947#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7948/*
7949 * Adjust slashes in file names. Called after 'shellslash' was set.
7950 */
7951 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007952alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953{
7954 int i;
7955# ifdef FEAT_WINDOWS
7956 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007957 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958# endif
7959
7960 for (i = 0; i < GARGCOUNT; ++i)
7961 if (GARGLIST[i].ae_fname != NULL)
7962 slash_adjust(GARGLIST[i].ae_fname);
7963# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007964 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 if (wp->w_alist != &global_alist)
7966 for (i = 0; i < WARGCOUNT(wp); ++i)
7967 if (WARGLIST(wp)[i].ae_fname != NULL)
7968 slash_adjust(WARGLIST(wp)[i].ae_fname);
7969# endif
7970}
7971#endif
7972
7973/*
7974 * ":preserve".
7975 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007977ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007979 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 ml_preserve(curbuf, TRUE);
7981}
7982
7983/*
7984 * ":recover".
7985 */
7986 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007987ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988{
7989 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7990 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007991 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7992 | CCGD_MULTWIN
7993 | (eap->forceit ? CCGD_FORCEIT : 0)
7994 | CCGD_EXCMD)
7995
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 && (*eap->arg == NUL
7997 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7998 ml_recover();
7999 recoverymode = FALSE;
8000}
8001
8002/*
8003 * Command modifier used in a wrong way.
8004 */
8005 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008006ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007{
8008 eap->errmsg = e_invcmd;
8009}
8010
8011#ifdef FEAT_WINDOWS
8012/*
8013 * :sview [+command] file split window with new file, read-only
8014 * :split [[+command] file] split window with current or new file
8015 * :vsplit [[+command] file] split window vertically with current or new file
8016 * :new [[+command] file] split window with no or new file
8017 * :vnew [[+command] file] split vertically window with no or new file
8018 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008019 *
8020 * :tabedit open new Tab page with empty window
8021 * :tabedit [+command] file open new Tab page and edit "file"
8022 * :tabnew [[+command] file] just like :tabedit
8023 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 */
8025 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008026ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008028 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008029# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008031# endif
8032# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008034# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008036# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008038# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008040# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008042 * quickfix windows. But it's OK when doing ":tab split". */
8043 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 {
8045 if (eap->cmdidx == CMD_split)
8046 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 if (eap->cmdidx == CMD_vsplit)
8048 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008050# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008052# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008053 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 {
8055 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8056 FNAME_MESS, TRUE, curbuf->b_ffname);
8057 if (fname == NULL)
8058 goto theend;
8059 eap->arg = fname;
8060 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008061# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008063# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008065# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 && eap->cmdidx != CMD_new)
8069 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008070# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008071 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008072# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008073 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008074# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008075 au_has_group((char_u *)"FileExplorer"))
8076 {
8077 /* No browsing supported but we do have the file explorer:
8078 * Edit the directory. */
8079 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8080 eap->arg = (char_u *)".";
8081 }
8082 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008083# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008084 {
8085 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008087 if (fname == NULL)
8088 goto theend;
8089 eap->arg = fname;
8090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 }
8092 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008093# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008095 /*
8096 * Either open new tab page or split the window.
8097 */
8098 if (eap->cmdidx == CMD_tabedit
8099 || eap->cmdidx == CMD_tabfind
8100 || eap->cmdidx == CMD_tabnew)
8101 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008102 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8103 : eap->addr_count == 0 ? 0
8104 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008105 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008106 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008107
8108 /* set the alternate buffer for the window we came from */
8109 if (curwin != old_curwin
8110 && win_valid(old_curwin)
8111 && old_curwin->w_buffer != curbuf
8112 && !cmdmod.keepalt)
8113 old_curwin->w_alt_fnum = curbuf->b_fnum;
8114 }
8115 }
8116 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8118 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008119# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 /* Reset 'scrollbind' when editing another file, but keep it when
8121 * doing ":split" without arguments. */
8122 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008123# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008125# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008127 {
8128 RESET_BINDING(curwin);
8129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 else
8131 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008132# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 do_exedit(eap, old_curwin);
8134 }
8135
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008136# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008138# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008140# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141theend:
8142 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008143# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008145
8146/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008147 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008148 */
8149 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008150tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008151{
8152 exarg_T ea;
8153
8154 vim_memset(&ea, 0, sizeof(ea));
8155 ea.cmdidx = CMD_tabnew;
8156 ea.cmd = (char_u *)"tabn";
8157 ea.arg = (char_u *)"";
8158 ex_splitview(&ea);
8159}
8160
8161/*
8162 * :tabnext command
8163 */
8164 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008165ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008166{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008167 switch (eap->cmdidx)
8168 {
8169 case CMD_tabfirst:
8170 case CMD_tabrewind:
8171 goto_tabpage(1);
8172 break;
8173 case CMD_tablast:
8174 goto_tabpage(9999);
8175 break;
8176 case CMD_tabprevious:
8177 case CMD_tabNext:
8178 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
8179 break;
8180 default: /* CMD_tabnext */
8181 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
8182 break;
8183 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008184}
8185
8186/*
8187 * :tabmove command
8188 */
8189 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008190ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008191{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008192 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008193
8194 if (eap->arg && *eap->arg != NUL)
8195 {
8196 char_u *p = eap->arg;
8197 int relative = 0; /* argument +N/-N means: move N places to the
8198 * right/left relative to the current position. */
8199
8200 if (*eap->arg == '-')
8201 {
8202 relative = -1;
8203 p = eap->arg + 1;
8204 }
8205 else if (*eap->arg == '+')
8206 {
8207 relative = 1;
8208 p = eap->arg + 1;
8209 }
8210 else
8211 p = eap->arg;
8212
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008213 if (relative == 0)
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008214 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008215 if (STRCMP(p, "$") == 0)
8216 tab_number = LAST_TAB_NR;
8217 else if (p == skipdigits(p))
8218 {
8219 /* No numbers as argument. */
8220 eap->errmsg = e_invarg;
8221 return;
8222 }
8223 else
8224 tab_number = getdigits(&p);
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008225 }
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008226 else
8227 {
8228 if (*p != NUL)
8229 tab_number = getdigits(&p);
8230 else
8231 tab_number = 1;
8232 tab_number = tab_number * relative + tabpage_index(curtab);
8233 if (relative == -1)
8234 --tab_number;
8235 }
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008236 }
8237 else if (eap->addr_count != 0)
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008238 {
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008239 tab_number = eap->line2;
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008240 if (**eap->cmdlinep == '-')
8241 --tab_number;
8242 }
8243 else
8244 tab_number = LAST_TAB_NR;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008245
8246 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008247}
8248
8249/*
8250 * :tabs command: List tabs and their contents.
8251 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008252 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008253ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008254{
8255 tabpage_T *tp;
8256 win_T *wp;
8257 int tabcount = 1;
8258
8259 msg_start();
8260 msg_scroll = TRUE;
8261 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8262 {
8263 msg_putchar('\n');
8264 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
8265 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
8266 out_flush(); /* output one line at a time */
8267 ui_breakcheck();
8268
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008269 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008270 wp = firstwin;
8271 else
8272 wp = tp->tp_firstwin;
8273 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8274 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008275 msg_putchar('\n');
8276 msg_putchar(wp == curwin ? '>' : ' ');
8277 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008278 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8279 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008280 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008281 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008282 else
8283 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8284 IObuff, IOSIZE, TRUE);
8285 msg_outtrans(IObuff);
8286 out_flush(); /* output one line at a time */
8287 ui_breakcheck();
8288 }
8289 }
8290}
8291
8292#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293
8294/*
8295 * ":mode": Set screen mode.
8296 * If no argument given, just get the screen size and redraw.
8297 */
8298 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008299ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300{
8301 if (*eap->arg == NUL)
8302 shell_resized();
8303 else
8304 mch_screenmode(eap->arg);
8305}
8306
8307#ifdef FEAT_WINDOWS
8308/*
8309 * ":resize".
8310 * set, increment or decrement current window height
8311 */
8312 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008313ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314{
8315 int n;
8316 win_T *wp = curwin;
8317
8318 if (eap->addr_count > 0)
8319 {
8320 n = eap->line2;
8321 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8322 ;
8323 }
8324
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008325# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008327# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 if (cmdmod.split & WSP_VERT)
8330 {
8331 if (*eap->arg == '-' || *eap->arg == '+')
8332 n += W_WIDTH(curwin);
8333 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8334 n = 9999;
8335 win_setwidth_win((int)n, wp);
8336 }
8337 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 {
8339 if (*eap->arg == '-' || *eap->arg == '+')
8340 n += curwin->w_height;
8341 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8342 n = 9999;
8343 win_setheight_win((int)n, wp);
8344 }
8345}
8346#endif
8347
8348/*
8349 * ":find [+command] <file>" command.
8350 */
8351 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008352ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353{
8354#ifdef FEAT_SEARCHPATH
8355 char_u *fname;
8356 int count;
8357
8358 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8359 TRUE, curbuf->b_ffname);
8360 if (eap->addr_count > 0)
8361 {
8362 /* Repeat finding the file "count" times. This matters when it
8363 * appears several times in the path. */
8364 count = eap->line2;
8365 while (fname != NULL && --count > 0)
8366 {
8367 vim_free(fname);
8368 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8369 FALSE, curbuf->b_ffname);
8370 }
8371 }
8372
8373 if (fname != NULL)
8374 {
8375 eap->arg = fname;
8376#endif
8377 do_exedit(eap, NULL);
8378#ifdef FEAT_SEARCHPATH
8379 vim_free(fname);
8380 }
8381#endif
8382}
8383
8384/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008385 * ":open" simulation: for now just work like ":visual".
8386 */
8387 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008388ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008389{
8390 regmatch_T regmatch;
8391 char_u *p;
8392
8393 curwin->w_cursor.lnum = eap->line2;
8394 beginline(BL_SOL | BL_FIX);
8395 if (*eap->arg == '/')
8396 {
8397 /* ":open /pattern/": put cursor in column found with pattern */
8398 ++eap->arg;
8399 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8400 *p = NUL;
8401 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8402 if (regmatch.regprog != NULL)
8403 {
8404 regmatch.rm_ic = p_ic;
8405 p = ml_get_curline();
8406 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008407 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008408 else
8409 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008410 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008411 }
8412 /* Move to the NUL, ignore any other arguments. */
8413 eap->arg += STRLEN(eap->arg);
8414 }
8415 check_cursor();
8416
8417 eap->cmdidx = CMD_visual;
8418 do_exedit(eap, NULL);
8419}
8420
8421/*
8422 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 */
8424 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008425ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426{
8427 do_exedit(eap, NULL);
8428}
8429
8430/*
8431 * ":edit <file>" command and alikes.
8432 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008434do_exedit(
8435 exarg_T *eap,
8436 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437{
8438 int n;
8439#ifdef FEAT_WINDOWS
8440 int need_hide;
8441#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008442 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443
8444 /*
8445 * ":vi" command ends Ex mode.
8446 */
8447 if (exmode_active && (eap->cmdidx == CMD_visual
8448 || eap->cmdidx == CMD_view))
8449 {
8450 exmode_active = FALSE;
8451 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008452 {
8453 /* Special case: ":global/pat/visual\NLvi-commands" */
8454 if (global_busy)
8455 {
8456 int rd = RedrawingDisabled;
8457 int nwr = no_wait_return;
8458 int ms = msg_scroll;
8459#ifdef FEAT_GUI
8460 int he = hold_gui_events;
8461#endif
8462
8463 if (eap->nextcmd != NULL)
8464 {
8465 stuffReadbuff(eap->nextcmd);
8466 eap->nextcmd = NULL;
8467 }
8468
8469 if (exmode_was != EXMODE_VIM)
8470 settmode(TMODE_RAW);
8471 RedrawingDisabled = 0;
8472 no_wait_return = 0;
8473 need_wait_return = FALSE;
8474 msg_scroll = 0;
8475#ifdef FEAT_GUI
8476 hold_gui_events = 0;
8477#endif
8478 must_redraw = CLEAR;
8479
8480 main_loop(FALSE, TRUE);
8481
8482 RedrawingDisabled = rd;
8483 no_wait_return = nwr;
8484 msg_scroll = ms;
8485#ifdef FEAT_GUI
8486 hold_gui_events = he;
8487#endif
8488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 }
8492
8493 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008494 || eap->cmdidx == CMD_tabnew
8495 || eap->cmdidx == CMD_tabedit
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008496#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 || eap->cmdidx == CMD_vnew
8498#endif
8499 ) && *eap->arg == NUL)
8500 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008501 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 setpcmark();
8503 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008504 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8505 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 }
8507 else if ((eap->cmdidx != CMD_split
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008508#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 && eap->cmdidx != CMD_vsplit
8510#endif
8511 )
8512 || *eap->arg != NUL
8513#ifdef FEAT_BROWSE
8514 || cmdmod.browse
8515#endif
8516 )
8517 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008518#ifdef FEAT_AUTOCMD
8519 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8520 * can bring us here, others are stopped earlier. */
8521 if (*eap->arg != NUL && curbuf_locked())
8522 return;
8523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 n = readonlymode;
8525 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8526 readonlymode = TRUE;
8527 else if (eap->cmdidx == CMD_enew)
8528 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8529 empty buffer */
8530 setpcmark();
8531 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8532 NULL, eap,
8533 /* ":edit" goes to first line if Vi compatible */
8534 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8535 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8536 ? ECMD_ONE : eap->do_ecmd_lnum,
8537 (P_HID(curbuf) ? ECMD_HIDE : 0)
8538 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008539 /* after a split we can use an existing buffer */
8540 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541#ifdef FEAT_LISTCMDS
8542 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8543#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008544 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 {
8546 /* Editing the file failed. If the window was split, close it. */
8547#ifdef FEAT_WINDOWS
8548 if (old_curwin != NULL)
8549 {
8550 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8551 if (!need_hide || P_HID(curbuf))
8552 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008553# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8554 cleanup_T cs;
8555
8556 /* Reset the error/interrupt/exception state here so that
8557 * aborting() returns FALSE when closing a window. */
8558 enter_cleanup(&cs);
8559# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560# ifdef FEAT_GUI
8561 need_mouse_correct = TRUE;
8562# endif
8563 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008564
8565# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8566 /* Restore the error/interrupt/exception state if not
8567 * discarded by a new aborting error, interrupt, or
8568 * uncaught exception. */
8569 leave_cleanup(&cs);
8570# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 }
8572 }
8573#endif
8574 }
8575 else if (readonlymode && curbuf->b_nwindows == 1)
8576 {
8577 /* When editing an already visited buffer, 'readonly' won't be set
8578 * but the previous value is kept. With ":view" and ":sview" we
8579 * want the file to be readonly, except when another window is
8580 * editing the same buffer. */
8581 curbuf->b_p_ro = TRUE;
8582 }
8583 readonlymode = n;
8584 }
8585 else
8586 {
8587 if (eap->do_ecmd_cmd != NULL)
8588 do_cmdline_cmd(eap->do_ecmd_cmd);
8589#ifdef FEAT_TITLE
8590 n = curwin->w_arg_idx_invalid;
8591#endif
8592 check_arg_idx(curwin);
8593#ifdef FEAT_TITLE
8594 if (n != curwin->w_arg_idx_invalid)
8595 maketitle();
8596#endif
8597 }
8598
8599#ifdef FEAT_WINDOWS
8600 /*
8601 * if ":split file" worked, set alternate file name in old window to new
8602 * file
8603 */
8604 if (old_curwin != NULL
8605 && *eap->arg != NUL
8606 && curwin != old_curwin
8607 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008608 && old_curwin->w_buffer != curbuf
8609 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 old_curwin->w_alt_fnum = curbuf->b_fnum;
8611#endif
8612
8613 ex_no_reprint = TRUE;
8614}
8615
8616#ifndef FEAT_GUI
8617/*
8618 * ":gui" and ":gvim" when there is no GUI.
8619 */
8620 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008621ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622{
8623 eap->errmsg = e_nogvim;
8624}
8625#endif
8626
8627#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8628 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008629ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630{
8631 gui_make_tearoff(eap->arg);
8632}
8633#endif
8634
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008635#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008637ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008639 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640}
8641#endif
8642
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008644ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645{
8646 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8647 MSG(_("No swap file"));
8648 else
8649 msg(curbuf->b_ml.ml_mfp->mf_fname);
8650}
8651
8652/*
8653 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8654 * offset.
8655 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8656 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008658ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659{
8660#ifdef FEAT_SCROLLBIND
8661 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008662 win_T *save_curwin = curwin;
8663 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 long topline;
8665 long y;
8666 linenr_T old_linenr = curwin->w_cursor.lnum;
8667
8668 setpcmark();
8669
8670 /*
8671 * determine max topline
8672 */
8673 if (curwin->w_p_scb)
8674 {
8675 topline = curwin->w_topline;
8676 for (wp = firstwin; wp; wp = wp->w_next)
8677 {
8678 if (wp->w_p_scb && wp->w_buffer)
8679 {
8680 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8681 if (topline > y)
8682 topline = y;
8683 }
8684 }
8685 if (topline < 1)
8686 topline = 1;
8687 }
8688 else
8689 {
8690 topline = 1;
8691 }
8692
8693
8694 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008695 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8698 {
8699 if (curwin->w_p_scb)
8700 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008701 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 y = topline - curwin->w_topline;
8703 if (y > 0)
8704 scrollup(y, TRUE);
8705 else
8706 scrolldown(-y, TRUE);
8707 curwin->w_scbind_pos = topline;
8708 redraw_later(VALID);
8709 cursor_correct();
8710#ifdef FEAT_WINDOWS
8711 curwin->w_redr_status = TRUE;
8712#endif
8713 }
8714 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008715 curwin = save_curwin;
8716 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 if (curwin->w_p_scb)
8718 {
8719 did_syncbind = TRUE;
8720 checkpcmark();
8721 if (old_linenr != curwin->w_cursor.lnum)
8722 {
8723 char_u ctrl_o[2];
8724
8725 ctrl_o[0] = Ctrl_O;
8726 ctrl_o[1] = 0;
8727 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8728 }
8729 }
8730#endif
8731}
8732
8733
8734 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008735ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008737 int i;
8738 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8739 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740
8741 if (eap->usefilter) /* :r!cmd */
8742 do_bang(1, eap, FALSE, FALSE, TRUE);
8743 else
8744 {
8745 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8746 return;
8747
8748#ifdef FEAT_BROWSE
8749 if (cmdmod.browse)
8750 {
8751 char_u *browseFile;
8752
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008753 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 NULL, NULL, NULL, curbuf);
8755 if (browseFile != NULL)
8756 {
8757 i = readfile(browseFile, NULL,
8758 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8759 vim_free(browseFile);
8760 }
8761 else
8762 i = OK;
8763 }
8764 else
8765#endif
8766 if (*eap->arg == NUL)
8767 {
8768 if (check_fname() == FAIL) /* check for no file name */
8769 return;
8770 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8771 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8772 }
8773 else
8774 {
8775 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8776 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8777 i = readfile(eap->arg, NULL,
8778 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8779
8780 }
8781 if (i == FAIL)
8782 {
8783#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8784 if (!aborting())
8785#endif
8786 EMSG2(_(e_notopen), eap->arg);
8787 }
8788 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008789 {
8790 if (empty && exmode_active)
8791 {
8792 /* Delete the empty line that remains. Historically ex does
8793 * this but vi doesn't. */
8794 if (eap->line2 == 0)
8795 lnum = curbuf->b_ml.ml_line_count;
8796 else
8797 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008798 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008799 {
8800 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008801 if (curwin->w_cursor.lnum > 1
8802 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008803 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008804 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008805 }
8806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 }
8810}
8811
Bram Moolenaarea408852005-06-25 22:49:46 +00008812static char_u *prev_dir = NULL;
8813
8814#if defined(EXITFREE) || defined(PROTO)
8815 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008816free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00008817{
8818 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008819 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008820
8821 vim_free(globaldir);
8822 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008823}
8824#endif
8825
Bram Moolenaarf4258302013-06-02 18:20:17 +02008826/*
8827 * Deal with the side effects of changing the current directory.
8828 * When "local" is TRUE then this was after an ":lcd" command.
8829 */
8830 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008831post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008832{
8833 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008834 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008835 if (local)
8836 {
8837 /* If still in global directory, need to remember current
8838 * directory as global directory. */
8839 if (globaldir == NULL && prev_dir != NULL)
8840 globaldir = vim_strsave(prev_dir);
8841 /* Remember this local directory for the window. */
8842 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8843 curwin->w_localdir = vim_strsave(NameBuff);
8844 }
8845 else
8846 {
8847 /* We are now in the global directory, no need to remember its
8848 * name. */
8849 vim_free(globaldir);
8850 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008851 }
8852
8853 shorten_fnames(TRUE);
8854}
8855
Bram Moolenaarea408852005-06-25 22:49:46 +00008856
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857/*
8858 * ":cd", ":lcd", ":chdir" and ":lchdir".
8859 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008860 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008861ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 char_u *new_dir;
8864 char_u *tofree;
8865
8866 new_dir = eap->arg;
8867#if !defined(UNIX) && !defined(VMS)
8868 /* for non-UNIX ":cd" means: print current directory */
8869 if (*new_dir == NUL)
8870 ex_pwd(NULL);
8871 else
8872#endif
8873 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008874#ifdef FEAT_AUTOCMD
8875 if (allbuf_locked())
8876 return;
8877#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008878 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8879 && !eap->forceit)
8880 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008881 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008882 return;
8883 }
8884
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 /* ":cd -": Change to previous directory */
8886 if (STRCMP(new_dir, "-") == 0)
8887 {
8888 if (prev_dir == NULL)
8889 {
8890 EMSG(_("E186: No previous directory"));
8891 return;
8892 }
8893 new_dir = prev_dir;
8894 }
8895
8896 /* Save current directory for next ":cd -" */
8897 tofree = prev_dir;
8898 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8899 prev_dir = vim_strsave(NameBuff);
8900 else
8901 prev_dir = NULL;
8902
8903#if defined(UNIX) || defined(VMS)
8904 /* for UNIX ":cd" means: go to home directory */
8905 if (*new_dir == NUL)
8906 {
8907 /* use NameBuff for home directory name */
8908# ifdef VMS
8909 char_u *p;
8910
8911 p = mch_getenv((char_u *)"SYS$LOGIN");
8912 if (p == NULL || *p == NUL) /* empty is the same as not set */
8913 NameBuff[0] = NUL;
8914 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008915 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916# else
8917 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8918# endif
8919 new_dir = NameBuff;
8920 }
8921#endif
8922 if (new_dir == NULL || vim_chdir(new_dir))
8923 EMSG(_(e_failed));
8924 else
8925 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008926 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927
8928 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008929 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 ex_pwd(eap);
8931 }
8932 vim_free(tofree);
8933 }
8934}
8935
8936/*
8937 * ":pwd".
8938 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008940ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941{
8942 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8943 {
8944#ifdef BACKSLASH_IN_FILENAME
8945 slash_adjust(NameBuff);
8946#endif
8947 msg(NameBuff);
8948 }
8949 else
8950 EMSG(_("E187: Unknown"));
8951}
8952
8953/*
8954 * ":=".
8955 */
8956 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008957ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958{
8959 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008960 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961}
8962
8963 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008964ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008966 int n;
8967 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968
8969 if (cursor_valid())
8970 {
8971 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8972 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01008973 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008975
8976 len = eap->line2;
8977 switch (*eap->arg)
8978 {
8979 case 'm': break;
8980 case NUL: len *= 1000L; break;
8981 default: EMSG2(_(e_invarg2), eap->arg); return;
8982 }
8983 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984}
8985
8986/*
8987 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8988 */
8989 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008990do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991{
8992 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01008993 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994
8995 cursor_on();
8996 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01008997 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01008999 wait_now = msec - done > 1000L ? 1000L : msec - done;
9000#ifdef FEAT_TIMERS
9001 {
9002 long due_time = check_due_timer();
9003
9004 if (due_time > 0 && due_time < wait_now)
9005 wait_now = due_time;
9006 }
9007#endif
9008 ui_delay(wait_now, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009010#ifdef MESSAGE_QUEUE
9011 /* Process the netbeans and clientserver messages that may have been
9012 * received in the call to ui_breakcheck() when the GUI is in use. This
9013 * may occur when running a test case. */
9014 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 }
9017}
9018
9019 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009020do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021{
9022 int mode;
9023 char_u *cmdp;
9024
9025 cmdp = eap->cmd;
9026 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9027
9028 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9029 eap->arg, mode, isabbrev))
9030 {
9031 case 1: EMSG(_(e_invarg));
9032 break;
9033 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9034 break;
9035 }
9036}
9037
9038/*
9039 * ":winsize" command (obsolete).
9040 */
9041 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009042ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043{
9044 int w, h;
9045 char_u *arg = eap->arg;
9046 char_u *p;
9047
9048 w = getdigits(&arg);
9049 arg = skipwhite(arg);
9050 p = arg;
9051 h = getdigits(&arg);
9052 if (*p != NUL && *arg == NUL)
9053 set_shellsize(w, h, TRUE);
9054 else
9055 EMSG(_("E465: :winsize requires two number arguments"));
9056}
9057
9058#ifdef FEAT_WINDOWS
9059 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009060ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061{
9062 int xchar = NUL;
9063 char_u *p;
9064
9065 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9066 {
9067 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9068 if (eap->arg[1] == NUL)
9069 {
9070 EMSG(_(e_invarg));
9071 return;
9072 }
9073 xchar = eap->arg[1];
9074 p = eap->arg + 2;
9075 }
9076 else
9077 p = eap->arg + 1;
9078
9079 eap->nextcmd = check_nextcmd(p);
9080 p = skipwhite(p);
9081 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9082 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009083 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 {
9085 /* Pass flags on for ":vertical wincmd ]". */
9086 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009087 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9089 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009090 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 }
9092}
9093#endif
9094
Bram Moolenaar843ee412004-06-30 16:16:41 +00009095#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096/*
9097 * ":winpos".
9098 */
9099 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009100ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101{
9102 int x, y;
9103 char_u *arg = eap->arg;
9104 char_u *p;
9105
9106 if (*arg == NUL)
9107 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009108# if defined(FEAT_GUI) || defined(MSWIN)
9109# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009111# else
9112 if (mch_get_winpos(&x, &y) != FAIL)
9113# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 {
9115 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9116 msg(IObuff);
9117 }
9118 else
9119# endif
9120 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9121 }
9122 else
9123 {
9124 x = getdigits(&arg);
9125 arg = skipwhite(arg);
9126 p = arg;
9127 y = getdigits(&arg);
9128 if (*p == NUL || *arg != NUL)
9129 {
9130 EMSG(_("E466: :winpos requires two number arguments"));
9131 return;
9132 }
9133# ifdef FEAT_GUI
9134 if (gui.in_use)
9135 gui_mch_set_winpos(x, y);
9136 else if (gui.starting)
9137 {
9138 /* Remember the coordinates for when the window is opened. */
9139 gui_win_x = x;
9140 gui_win_y = y;
9141 }
9142# ifdef HAVE_TGETENT
9143 else
9144# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009145# else
9146# ifdef MSWIN
9147 mch_set_winpos(x, y);
9148# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149# endif
9150# ifdef HAVE_TGETENT
9151 if (*T_CWP)
9152 term_set_winpos(x, y);
9153# endif
9154 }
9155}
9156#endif
9157
9158/*
9159 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9160 */
9161 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009162ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163{
9164 oparg_T oa;
9165
9166 clear_oparg(&oa);
9167 oa.regname = eap->regname;
9168 oa.start.lnum = eap->line1;
9169 oa.end.lnum = eap->line2;
9170 oa.line_count = eap->line2 - eap->line1 + 1;
9171 oa.motion_type = MLINE;
9172#ifdef FEAT_VIRTUALEDIT
9173 virtual_op = FALSE;
9174#endif
9175 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9176 {
9177 setpcmark();
9178 curwin->w_cursor.lnum = eap->line1;
9179 beginline(BL_SOL | BL_FIX);
9180 }
9181
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009182 if (VIsual_active)
9183 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009184
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 switch (eap->cmdidx)
9186 {
9187 case CMD_delete:
9188 oa.op_type = OP_DELETE;
9189 op_delete(&oa);
9190 break;
9191
9192 case CMD_yank:
9193 oa.op_type = OP_YANK;
9194 (void)op_yank(&oa, FALSE, TRUE);
9195 break;
9196
9197 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009198 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009200 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9201#else
9202 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009204 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 oa.op_type = OP_RSHIFT;
9206 else
9207 oa.op_type = OP_LSHIFT;
9208 op_shift(&oa, FALSE, eap->amount);
9209 break;
9210 }
9211#ifdef FEAT_VIRTUALEDIT
9212 virtual_op = MAYBE;
9213#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009214 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215}
9216
9217/*
9218 * ":put".
9219 */
9220 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009221ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222{
9223 /* ":0put" works like ":1put!". */
9224 if (eap->line2 == 0)
9225 {
9226 eap->line2 = 1;
9227 eap->forceit = TRUE;
9228 }
9229 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009230 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9231 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232}
9233
9234/*
9235 * Handle ":copy" and ":move".
9236 */
9237 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009238ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239{
9240 long n;
9241
Bram Moolenaaraa23b372015-09-08 18:46:31 +02009242 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 if (eap->arg == NULL) /* error detected */
9244 {
9245 eap->nextcmd = NULL;
9246 return;
9247 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009248 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249
9250 /*
9251 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9252 */
9253 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9254 {
9255 EMSG(_(e_invaddr));
9256 return;
9257 }
9258
9259 if (eap->cmdidx == CMD_move)
9260 {
9261 if (do_move(eap->line1, eap->line2, n) == FAIL)
9262 return;
9263 }
9264 else
9265 ex_copy(eap->line1, eap->line2, n);
9266 u_clearline();
9267 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009268 ex_may_print(eap);
9269}
9270
9271/*
9272 * Print the current line if flags were given to the Ex command.
9273 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009274 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009275ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009276{
9277 if (eap->flags != 0)
9278 {
9279 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9280 (eap->flags & EXFLAG_LIST));
9281 ex_no_reprint = TRUE;
9282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283}
9284
9285/*
9286 * ":smagic" and ":snomagic".
9287 */
9288 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009289ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290{
9291 int magic_save = p_magic;
9292
9293 p_magic = (eap->cmdidx == CMD_smagic);
9294 do_sub(eap);
9295 p_magic = magic_save;
9296}
9297
9298/*
9299 * ":join".
9300 */
9301 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009302ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303{
9304 curwin->w_cursor.lnum = eap->line1;
9305 if (eap->line1 == eap->line2)
9306 {
9307 if (eap->addr_count >= 2) /* :2,2join does nothing */
9308 return;
9309 if (eap->line2 == curbuf->b_ml.ml_line_count)
9310 {
9311 beep_flush();
9312 return;
9313 }
9314 ++eap->line2;
9315 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009316 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009318 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319}
9320
9321/*
9322 * ":[addr]@r" or ":[addr]*r": execute register
9323 */
9324 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009325ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326{
9327 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009328 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329
9330 curwin->w_cursor.lnum = eap->line2;
9331
9332#ifdef USE_ON_FLY_SCROLL
9333 dont_scroll = TRUE; /* disallow scrolling here */
9334#endif
9335
9336 /* get the register name. No name means to use the previous one */
9337 c = *eap->arg;
9338 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9339 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009340 /* Put the register in the typeahead buffer with the "silent" flag. */
9341 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9342 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009343 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 else
9347 {
9348 int save_efr = exec_from_reg;
9349
9350 exec_from_reg = TRUE;
9351
9352 /*
9353 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009354 * Continue until the stuff buffer is empty and all added characters
9355 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009357 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9359
9360 exec_from_reg = save_efr;
9361 }
9362}
9363
9364/*
9365 * ":!".
9366 */
9367 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009368ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369{
9370 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9371}
9372
9373/*
9374 * ":undo".
9375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009377ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009379 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009380 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009381 else
9382 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383}
9384
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009385#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009386 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009387ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009388{
9389 char_u hash[UNDO_HASH_SIZE];
9390
9391 u_compute_hash(hash);
9392 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9393}
9394
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009395 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009396ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009397{
9398 char_u hash[UNDO_HASH_SIZE];
9399
9400 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009401 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009402}
9403#endif
9404
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405/*
9406 * ":redo".
9407 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009409ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410{
9411 u_redo(1);
9412}
9413
9414/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009415 * ":earlier" and ":later".
9416 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009417 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009418ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009419{
9420 long count = 0;
9421 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009422 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009423 char_u *p = eap->arg;
9424
9425 if (*p == NUL)
9426 count = 1;
9427 else if (isdigit(*p))
9428 {
9429 count = getdigits(&p);
9430 switch (*p)
9431 {
9432 case 's': ++p; sec = TRUE; break;
9433 case 'm': ++p; sec = TRUE; count *= 60; break;
9434 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009435 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9436 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009437 }
9438 }
9439
9440 if (*p != NUL)
9441 EMSG2(_(e_invarg2), eap->arg);
9442 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009443 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9444 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009445}
9446
9447/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 * ":redir": start/stop redirection.
9449 */
9450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009451ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452{
9453 char *mode;
9454 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009455 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456
9457 if (STRICMP(eap->arg, "END") == 0)
9458 close_redir();
9459 else
9460 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009461 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009463 ++arg;
9464 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009466 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 mode = "a";
9468 }
9469 else
9470 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009471 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472
9473 close_redir();
9474
9475 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009476 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 if (fname == NULL)
9478 return;
9479#ifdef FEAT_BROWSE
9480 if (cmdmod.browse)
9481 {
9482 char_u *browseFile;
9483
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009484 browseFile = do_browse(BROWSE_SAVE,
9485 (char_u *)_("Save Redirection"),
9486 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 if (browseFile == NULL)
9488 return; /* operation cancelled */
9489 vim_free(fname);
9490 fname = browseFile;
9491 eap->forceit = TRUE; /* since dialog already asked */
9492 }
9493#endif
9494
9495 redir_fd = open_exfile(fname, eap->forceit, mode);
9496 vim_free(fname);
9497 }
9498#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009499 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 {
9501 /* redirect to a register a-z (resp. A-Z for appending) */
9502 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009503 ++arg;
9504 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009506 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009507 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009509 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009511 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009512 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009513 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009514 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009516 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009517 if (*arg == '>')
9518 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009519 /* Make register empty when not using @A-@Z and the
9520 * command is valid. */
9521 if (*arg == NUL && !isupper(redir_reg))
9522 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009524 }
9525 if (*arg != NUL)
9526 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009527 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009528 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009529 }
9530 }
9531 else if (*arg == '=' && arg[1] == '>')
9532 {
9533 int append;
9534
9535 /* redirect to a variable */
9536 close_redir();
9537 arg += 2;
9538
9539 if (*arg == '>')
9540 {
9541 ++arg;
9542 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 }
9544 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009545 append = FALSE;
9546
9547 if (var_redir_start(skipwhite(arg), append) == OK)
9548 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549 }
9550#endif
9551
9552 /* TODO: redirect to a buffer */
9553
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009555 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009557
9558 /* Make sure redirection is not off. Can happen for cmdline completion
9559 * that indirectly invokes a command to catch its output. */
9560 if (redir_fd != NULL
9561#ifdef FEAT_EVAL
9562 || redir_reg || redir_vname
9563#endif
9564 )
9565 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566}
9567
9568/*
9569 * ":redraw": force redraw
9570 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009571 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009572ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573{
9574 int r = RedrawingDisabled;
9575 int p = p_lz;
9576
9577 RedrawingDisabled = 0;
9578 p_lz = FALSE;
9579 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009580 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581#ifdef FEAT_TITLE
9582 if (need_maketitle)
9583 maketitle();
9584#endif
9585 RedrawingDisabled = r;
9586 p_lz = p;
9587
9588 /* Reset msg_didout, so that a message that's there is overwritten. */
9589 msg_didout = FALSE;
9590 msg_col = 0;
9591
Bram Moolenaar56667a52013-07-17 11:54:28 +02009592 /* No need to wait after an intentional redraw. */
9593 need_wait_return = FALSE;
9594
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 out_flush();
9596}
9597
9598/*
9599 * ":redrawstatus": force redraw of status line(s)
9600 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009602ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603{
9604#if defined(FEAT_WINDOWS)
9605 int r = RedrawingDisabled;
9606 int p = p_lz;
9607
9608 RedrawingDisabled = 0;
9609 p_lz = FALSE;
9610 if (eap->forceit)
9611 status_redraw_all();
9612 else
9613 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009614 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 RedrawingDisabled = r;
9616 p_lz = p;
9617 out_flush();
9618#endif
9619}
9620
9621 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009622close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623{
9624 if (redir_fd != NULL)
9625 {
9626 fclose(redir_fd);
9627 redir_fd = NULL;
9628 }
9629#ifdef FEAT_EVAL
9630 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009631 if (redir_vname)
9632 {
9633 var_redir_stop();
9634 redir_vname = 0;
9635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636#endif
9637}
9638
9639#if defined(FEAT_SESSION) && defined(USE_CRNL)
9640# define MKSESSION_NL
9641static int mksession_nl = FALSE; /* use NL only in put_eol() */
9642#endif
9643
9644/*
9645 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9646 */
9647 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009648ex_mkrc(
9649 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650{
9651 FILE *fd;
9652 int failed = FALSE;
9653 char_u *fname;
9654#ifdef FEAT_BROWSE
9655 char_u *browseFile = NULL;
9656#endif
9657#ifdef FEAT_SESSION
9658 int view_session = FALSE;
9659 int using_vdir = FALSE; /* using 'viewdir'? */
9660 char_u *viewFile = NULL;
9661 unsigned *flagp;
9662#endif
9663
9664 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9665 {
9666#ifdef FEAT_SESSION
9667 view_session = TRUE;
9668#else
9669 ex_ni(eap);
9670 return;
9671#endif
9672 }
9673
9674#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009675 /* Use the short file name until ":lcd" is used. We also don't use the
9676 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009677 did_lcd = FALSE;
9678
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9680 if (eap->cmdidx == CMD_mkview
9681 && (*eap->arg == NUL
9682 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9683 {
9684 eap->forceit = TRUE;
9685 fname = get_view_file(*eap->arg);
9686 if (fname == NULL)
9687 return;
9688 viewFile = fname;
9689 using_vdir = TRUE;
9690 }
9691 else
9692#endif
9693 if (*eap->arg != NUL)
9694 fname = eap->arg;
9695 else if (eap->cmdidx == CMD_mkvimrc)
9696 fname = (char_u *)VIMRC_FILE;
9697#ifdef FEAT_SESSION
9698 else if (eap->cmdidx == CMD_mksession)
9699 fname = (char_u *)SESSION_FILE;
9700#endif
9701 else
9702 fname = (char_u *)EXRC_FILE;
9703
9704#ifdef FEAT_BROWSE
9705 if (cmdmod.browse)
9706 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009707 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708# ifdef FEAT_SESSION
9709 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9710 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9711# endif
9712 (char_u *)_("Save Setup"),
9713 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9714 if (browseFile == NULL)
9715 goto theend;
9716 fname = browseFile;
9717 eap->forceit = TRUE; /* since dialog already asked */
9718 }
9719#endif
9720
9721#if defined(FEAT_SESSION) && defined(vim_mkdir)
9722 /* When using 'viewdir' may have to create the directory. */
9723 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009724 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725#endif
9726
9727 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9728 if (fd != NULL)
9729 {
9730#ifdef FEAT_SESSION
9731 if (eap->cmdidx == CMD_mkview)
9732 flagp = &vop_flags;
9733 else
9734 flagp = &ssop_flags;
9735#endif
9736
9737#ifdef MKSESSION_NL
9738 /* "unix" in 'sessionoptions': use NL line separator */
9739 if (view_session && (*flagp & SSOP_UNIX))
9740 mksession_nl = TRUE;
9741#endif
9742
9743 /* Write the version command for :mkvimrc */
9744 if (eap->cmdidx == CMD_mkvimrc)
9745 (void)put_line(fd, "version 6.0");
9746
9747#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009748 if (eap->cmdidx == CMD_mksession)
9749 {
9750 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9751 failed = TRUE;
9752 }
9753
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 if (eap->cmdidx != CMD_mkview)
9755#endif
9756 {
9757 /* Write setting 'compatible' first, because it has side effects.
9758 * For that same reason only do it when needed. */
9759 if (p_cp)
9760 (void)put_line(fd, "if !&cp | set cp | endif");
9761 else
9762 (void)put_line(fd, "if &cp | set nocp | endif");
9763 }
9764
9765#ifdef FEAT_SESSION
9766 if (!view_session
9767 || (eap->cmdidx == CMD_mksession
9768 && (*flagp & SSOP_OPTIONS)))
9769#endif
9770 failed |= (makemap(fd, NULL) == FAIL
9771 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9772
9773#ifdef FEAT_SESSION
9774 if (!failed && view_session)
9775 {
9776 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9777 failed = TRUE;
9778 if (eap->cmdidx == CMD_mksession)
9779 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009780 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781
Bram Moolenaard9462e32011-04-11 21:35:11 +02009782 dirnow = alloc(MAXPATHL);
9783 if (dirnow == NULL)
9784 failed = TRUE;
9785 else
9786 {
9787 /*
9788 * Change to session file's dir.
9789 */
9790 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009792 *dirnow = NUL;
9793 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9794 {
9795 if (vim_chdirfile(fname) == OK)
9796 shorten_fnames(TRUE);
9797 }
9798 else if (*dirnow != NUL
9799 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9800 {
9801 if (mch_chdir((char *)globaldir) == 0)
9802 shorten_fnames(TRUE);
9803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804
Bram Moolenaard9462e32011-04-11 21:35:11 +02009805 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806
Bram Moolenaard9462e32011-04-11 21:35:11 +02009807 /* restore original dir */
9808 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009810 {
9811 if (mch_chdir((char *)dirnow) != 0)
9812 EMSG(_(e_prev_dir));
9813 shorten_fnames(TRUE);
9814 }
9815 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 }
9817 }
9818 else
9819 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009820 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9821 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822 }
9823 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9824 == FAIL)
9825 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009826 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9827 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009828 if (eap->cmdidx == CMD_mksession)
9829 {
9830 if (put_line(fd, "unlet SessionLoad") == FAIL)
9831 failed = TRUE;
9832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 }
9834#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009835 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9836 failed = TRUE;
9837
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 failed |= fclose(fd);
9839
9840 if (failed)
9841 EMSG(_(e_write));
9842#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9843 else if (eap->cmdidx == CMD_mksession)
9844 {
9845 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009846 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847
Bram Moolenaard9462e32011-04-11 21:35:11 +02009848 tbuf = alloc(MAXPATHL);
9849 if (tbuf != NULL)
9850 {
9851 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9852 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9853 vim_free(tbuf);
9854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 }
9856#endif
9857#ifdef MKSESSION_NL
9858 mksession_nl = FALSE;
9859#endif
9860 }
9861
9862#ifdef FEAT_BROWSE
9863theend:
9864 vim_free(browseFile);
9865#endif
9866#ifdef FEAT_SESSION
9867 vim_free(viewFile);
9868#endif
9869}
9870
Bram Moolenaardf177f62005-02-22 08:39:57 +00009871#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9872 || defined(PROTO)
9873 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009874vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009875{
9876 if (vim_mkdir(name, prot) != 0)
9877 {
9878 EMSG2(_("E739: Cannot create directory: %s"), name);
9879 return FAIL;
9880 }
9881 return OK;
9882}
9883#endif
9884
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885/*
9886 * Open a file for writing for an Ex command, with some checks.
9887 * Return file descriptor, or NULL on failure.
9888 */
9889 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009890open_exfile(
9891 char_u *fname,
9892 int forceit,
9893 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894{
9895 FILE *fd;
9896
9897#ifdef UNIX
9898 /* with Unix it is possible to open a directory */
9899 if (mch_isdir(fname))
9900 {
9901 EMSG2(_(e_isadir2), fname);
9902 return NULL;
9903 }
9904#endif
9905 if (!forceit && *mode != 'a' && vim_fexists(fname))
9906 {
9907 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9908 return NULL;
9909 }
9910
9911 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9912 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9913
9914 return fd;
9915}
9916
9917/*
9918 * ":mark" and ":k".
9919 */
9920 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009921ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922{
9923 pos_T pos;
9924
9925 if (*eap->arg == NUL) /* No argument? */
9926 EMSG(_(e_argreq));
9927 else if (eap->arg[1] != NUL) /* more than one character? */
9928 EMSG(_(e_trailing));
9929 else
9930 {
9931 pos = curwin->w_cursor; /* save curwin->w_cursor */
9932 curwin->w_cursor.lnum = eap->line2;
9933 beginline(BL_WHITE | BL_FIX);
9934 if (setmark(*eap->arg) == FAIL) /* set mark */
9935 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9936 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9937 }
9938}
9939
9940/*
9941 * Update w_topline, w_leftcol and the cursor position.
9942 */
9943 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009944update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945{
9946 check_cursor(); /* put cursor on valid line */
9947 update_topline();
9948 if (!curwin->w_p_wrap)
9949 validate_cursor();
9950 update_curswant();
9951}
9952
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953/*
9954 * ":normal[!] {commands}": Execute normal mode commands.
9955 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01009956 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009957ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959 int save_msg_scroll = msg_scroll;
9960 int save_restart_edit = restart_edit;
9961 int save_msg_didout = msg_didout;
9962 int save_State = State;
9963 tasave_T tabuf;
9964 int save_insertmode = p_im;
9965 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009966 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967#ifdef FEAT_MBYTE
9968 char_u *arg = NULL;
9969 int l;
9970 char_u *p;
9971#endif
9972
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009973 if (ex_normal_lock > 0)
9974 {
9975 EMSG(_(e_secure));
9976 return;
9977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978 if (ex_normal_busy >= p_mmd)
9979 {
9980 EMSG(_("E192: Recursive use of :normal too deep"));
9981 return;
9982 }
9983 ++ex_normal_busy;
9984
9985 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9986 restart_edit = 0; /* don't go to Insert mode */
9987 p_im = FALSE; /* don't use 'insertmode' */
9988
9989#ifdef FEAT_MBYTE
9990 /*
9991 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9992 * this for the K_SPECIAL leading byte, otherwise special keys will not
9993 * work.
9994 */
9995 if (has_mbyte)
9996 {
9997 int len = 0;
9998
9999 /* Count the number of characters to be escaped. */
10000 for (p = eap->arg; *p != NUL; ++p)
10001 {
10002# ifdef FEAT_GUI
10003 if (*p == CSI) /* leadbyte CSI */
10004 len += 2;
10005# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010006 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10008# ifdef FEAT_GUI
10009 || *p == CSI
10010# endif
10011 )
10012 len += 2;
10013 }
10014 if (len > 0)
10015 {
10016 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10017 if (arg != NULL)
10018 {
10019 len = 0;
10020 for (p = eap->arg; *p != NUL; ++p)
10021 {
10022 arg[len++] = *p;
10023# ifdef FEAT_GUI
10024 if (*p == CSI)
10025 {
10026 arg[len++] = KS_EXTRA;
10027 arg[len++] = (int)KE_CSI;
10028 }
10029# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010030 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 {
10032 arg[len++] = *++p;
10033 if (*p == K_SPECIAL)
10034 {
10035 arg[len++] = KS_SPECIAL;
10036 arg[len++] = KE_FILLER;
10037 }
10038# ifdef FEAT_GUI
10039 else if (*p == CSI)
10040 {
10041 arg[len++] = KS_EXTRA;
10042 arg[len++] = (int)KE_CSI;
10043 }
10044# endif
10045 }
10046 arg[len] = NUL;
10047 }
10048 }
10049 }
10050 }
10051#endif
10052
10053 /*
10054 * Save the current typeahead. This is required to allow using ":normal"
10055 * from an event handler and makes sure we don't hang when the argument
10056 * ends with half a command.
10057 */
10058 save_typeahead(&tabuf);
10059 if (tabuf.typebuf_valid)
10060 {
10061 /*
10062 * Repeat the :normal command for each line in the range. When no
10063 * range given, execute it just once, without positioning the cursor
10064 * first.
10065 */
10066 do
10067 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 if (eap->addr_count != 0)
10069 {
10070 curwin->w_cursor.lnum = eap->line1++;
10071 curwin->w_cursor.col = 0;
10072 }
10073
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010074 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075#ifdef FEAT_MBYTE
10076 arg != NULL ? arg :
10077#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010078 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 }
10080 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10081 }
10082
10083 /* Might not return to the main loop when in an event handler. */
10084 update_topline_cursor();
10085
10086 /* Restore the previous typeahead. */
10087 restore_typeahead(&tabuf);
10088
10089 --ex_normal_busy;
10090 msg_scroll = save_msg_scroll;
10091 restart_edit = save_restart_edit;
10092 p_im = save_insertmode;
10093 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010094 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
10096
10097 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +010010098 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +010010100#ifdef FEAT_MOUSE
10101 setmouse();
10102#endif
10103#ifdef CURSOR_SHAPE
10104 ui_cursor_shape(); /* may show different cursor shape */
10105#endif
10106
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107#ifdef FEAT_MBYTE
10108 vim_free(arg);
10109#endif
10110}
10111
10112/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010113 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 */
10115 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010116ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010118 if (eap->forceit)
10119 {
10120 coladvance((colnr_T)MAXCOL);
10121 curwin->w_curswant = MAXCOL;
10122 curwin->w_set_curswant = FALSE;
10123 }
10124
Bram Moolenaara40c5002005-01-09 21:16:21 +000010125 /* Ignore the command when already in Insert mode. Inserting an
10126 * expression register that invokes a function can do this. */
10127 if (State & INSERT)
10128 return;
10129
Bram Moolenaar64969662005-12-14 21:59:55 +000010130 if (eap->cmdidx == CMD_startinsert)
10131 restart_edit = 'a';
10132 else if (eap->cmdidx == CMD_startreplace)
10133 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010135 restart_edit = 'V';
10136
10137 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010139 if (eap->cmdidx == CMD_startinsert)
10140 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 curwin->w_curswant = 0; /* avoid MAXCOL */
10142 }
10143}
10144
10145/*
10146 * ":stopinsert"
10147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010149ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150{
10151 restart_edit = 0;
10152 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010153 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010156/*
10157 * Execute normal mode command "cmd".
10158 * "remap" can be REMAP_NONE or REMAP_YES.
10159 */
10160 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010161exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010162{
Bram Moolenaar25281632016-01-21 23:32:32 +010010163 /* Stuff the argument into the typeahead buffer. */
10164 ins_typebuf(cmd, remap, 0, TRUE, silent);
10165 exec_normal(FALSE);
10166}
Bram Moolenaar25281632016-01-21 23:32:32 +010010167
Bram Moolenaar25281632016-01-21 23:32:32 +010010168/*
10169 * Execute normal_cmd() until there is no typeahead left.
10170 */
10171 void
10172exec_normal(int was_typed)
10173{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010174 oparg_T oa;
10175
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010176 clear_oparg(&oa);
10177 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010178 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10179 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010180 {
10181 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010182 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010183 }
10184}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010185
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186#ifdef FEAT_FIND_ID
10187 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010188ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189{
10190 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10191 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10192 (linenr_T)1, (linenr_T)MAXLNUM);
10193}
10194
10195#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
10196/*
10197 * ":psearch"
10198 */
10199 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010200ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201{
10202 g_do_tagpreview = p_pvh;
10203 ex_findpat(eap);
10204 g_do_tagpreview = 0;
10205}
10206#endif
10207
10208 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010209ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210{
10211 int whole = TRUE;
10212 long n;
10213 char_u *p;
10214 int action;
10215
10216 switch (cmdnames[eap->cmdidx].cmd_name[2])
10217 {
10218 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10219 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10220 action = ACTION_GOTO;
10221 else
10222 action = ACTION_SHOW;
10223 break;
10224 case 'i': /* ":ilist" and ":dlist" */
10225 action = ACTION_SHOW_ALL;
10226 break;
10227 case 'u': /* ":ijump" and ":djump" */
10228 action = ACTION_GOTO;
10229 break;
10230 default: /* ":isplit" and ":dsplit" */
10231 action = ACTION_SPLIT;
10232 break;
10233 }
10234
10235 n = 1;
10236 if (vim_isdigit(*eap->arg)) /* get count */
10237 {
10238 n = getdigits(&eap->arg);
10239 eap->arg = skipwhite(eap->arg);
10240 }
10241 if (*eap->arg == '/') /* Match regexp, not just whole words */
10242 {
10243 whole = FALSE;
10244 ++eap->arg;
10245 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10246 if (*p)
10247 {
10248 *p++ = NUL;
10249 p = skipwhite(p);
10250
10251 /* Check for trailing illegal characters */
10252 if (!ends_excmd(*p))
10253 eap->errmsg = e_trailing;
10254 else
10255 eap->nextcmd = check_nextcmd(p);
10256 }
10257 }
10258 if (!eap->skip)
10259 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10260 whole, !eap->forceit,
10261 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10262 n, action, eap->line1, eap->line2);
10263}
10264#endif
10265
10266#ifdef FEAT_WINDOWS
10267
10268# ifdef FEAT_QUICKFIX
10269/*
10270 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10271 */
10272 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010273ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010275 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10277}
10278
10279/*
10280 * ":pedit"
10281 */
10282 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010283ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284{
10285 win_T *curwin_save = curwin;
10286
10287 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010288 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 keep_help_flag = curwin_save->w_buffer->b_help;
10290 do_exedit(eap, NULL);
10291 keep_help_flag = FALSE;
10292 if (curwin != curwin_save && win_valid(curwin_save))
10293 {
10294 /* Return cursor to where we were */
10295 validate_cursor();
10296 redraw_later(VALID);
10297 win_enter(curwin_save, TRUE);
10298 }
10299 g_do_tagpreview = 0;
10300}
10301# endif
10302
10303/*
10304 * ":stag", ":stselect" and ":stjump".
10305 */
10306 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010307ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308{
10309 postponed_split = -1;
10310 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010311 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10313 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010314 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315}
10316#endif
10317
10318/*
10319 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10320 */
10321 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010322ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323{
10324 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10325}
10326
10327 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010328ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329{
10330 int cmd;
10331
10332 switch (name[1])
10333 {
10334 case 'j': cmd = DT_JUMP; /* ":tjump" */
10335 break;
10336 case 's': cmd = DT_SELECT; /* ":tselect" */
10337 break;
10338 case 'p': cmd = DT_PREV; /* ":tprevious" */
10339 break;
10340 case 'N': cmd = DT_PREV; /* ":tNext" */
10341 break;
10342 case 'n': cmd = DT_NEXT; /* ":tnext" */
10343 break;
10344 case 'o': cmd = DT_POP; /* ":pop" */
10345 break;
10346 case 'f': /* ":tfirst" */
10347 case 'r': cmd = DT_FIRST; /* ":trewind" */
10348 break;
10349 case 'l': cmd = DT_LAST; /* ":tlast" */
10350 break;
10351 default: /* ":tag" */
10352#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010353 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 {
10355 do_cstag(eap);
10356 return;
10357 }
10358#endif
10359 cmd = DT_TAG;
10360 break;
10361 }
10362
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010363 if (name[0] == 'l')
10364 {
10365#ifndef FEAT_QUICKFIX
10366 ex_ni(eap);
10367 return;
10368#else
10369 cmd = DT_LTAG;
10370#endif
10371 }
10372
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10374 eap->forceit, TRUE);
10375}
10376
10377/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010378 * Check "str" for starting with a special cmdline variable.
10379 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10380 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10381 */
10382 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010383find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010384{
10385 int len;
10386 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010387 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010388 "%",
10389#define SPEC_PERC 0
10390 "#",
10391#define SPEC_HASH 1
10392 "<cword>", /* cursor word */
10393#define SPEC_CWORD 2
10394 "<cWORD>", /* cursor WORD */
10395#define SPEC_CCWORD 3
10396 "<cfile>", /* cursor path name */
10397#define SPEC_CFILE 4
10398 "<sfile>", /* ":so" file name */
10399#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010400 "<slnum>", /* ":so" file line number */
10401#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010402#ifdef FEAT_AUTOCMD
10403 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010404# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010405 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010406# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010407 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010408# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010409#endif
10410#ifdef FEAT_CLIENTSERVER
10411 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010412# ifdef FEAT_AUTOCMD
10413# define SPEC_CLIENT 10
10414# else
10415# define SPEC_CLIENT 7
10416# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010417#endif
10418 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010419
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010420 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010421 {
10422 len = (int)STRLEN(spec_str[i]);
10423 if (STRNCMP(src, spec_str[i], len) == 0)
10424 {
10425 *usedlen = len;
10426 return i;
10427 }
10428 }
10429 return -1;
10430}
10431
10432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433 * Evaluate cmdline variables.
10434 *
10435 * change '%' to curbuf->b_ffname
10436 * '#' to curwin->w_altfile
10437 * '<cword>' to word under the cursor
10438 * '<cWORD>' to WORD under the cursor
10439 * '<cfile>' to path name under the cursor
10440 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010441 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442 * '<afile>' to file name for autocommand
10443 * '<abuf>' to buffer number for autocommand
10444 * '<amatch>' to matching name for autocommand
10445 *
10446 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10447 * "" for error without a message) and NULL is returned.
10448 * Returns an allocated string if a valid match was found.
10449 * Returns NULL if no match was found. "usedlen" then still contains the
10450 * number of characters to skip.
10451 */
10452 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010453eval_vars(
10454 char_u *src, /* pointer into commandline */
10455 char_u *srcstart, /* beginning of valid memory for src */
10456 int *usedlen, /* characters after src that are used */
10457 linenr_T *lnump, /* line number for :e command, or NULL */
10458 char_u **errormsg, /* pointer to error message */
10459 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010460 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461{
10462 int i;
10463 char_u *s;
10464 char_u *result;
10465 char_u *resultbuf = NULL;
10466 int resultlen;
10467 buf_T *buf;
10468 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10469 int spec_idx;
10470#ifdef FEAT_MODIFY_FNAME
10471 int skip_mod = FALSE;
10472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474
10475 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010476 if (escaped != NULL)
10477 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478
10479 /*
10480 * Check if there is something to do.
10481 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010482 spec_idx = find_cmdline_var(src, usedlen);
10483 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484 {
10485 *usedlen = 1;
10486 return NULL;
10487 }
10488
10489 /*
10490 * Skip when preceded with a backslash "\%" and "\#".
10491 * Note: In "\\%" the % is also not recognized!
10492 */
10493 if (src > srcstart && src[-1] == '\\')
10494 {
10495 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010496 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497 return NULL;
10498 }
10499
10500 /*
10501 * word or WORD under cursor
10502 */
10503 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10504 {
10505 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10506 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10507 if (resultlen == 0)
10508 {
10509 *errormsg = (char_u *)"";
10510 return NULL;
10511 }
10512 }
10513
10514 /*
10515 * '#': Alternate file name
10516 * '%': Current file name
10517 * File name under the cursor
10518 * File name for autocommand
10519 * and following modifiers
10520 */
10521 else
10522 {
10523 switch (spec_idx)
10524 {
10525 case SPEC_PERC: /* '%': current file */
10526 if (curbuf->b_fname == NULL)
10527 {
10528 result = (char_u *)"";
10529 valid = 0; /* Must have ":p:h" to be valid */
10530 }
10531 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 break;
10534
10535 case SPEC_HASH: /* '#' or "#99": alternate file */
10536 if (src[1] == '#') /* "##": the argument list */
10537 {
10538 result = arg_all();
10539 resultbuf = result;
10540 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010541 if (escaped != NULL)
10542 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543#ifdef FEAT_MODIFY_FNAME
10544 skip_mod = TRUE;
10545#endif
10546 break;
10547 }
10548 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010549 if (*s == '<') /* "#<99" uses v:oldfiles */
10550 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551 i = (int)getdigits(&s);
10552 *usedlen = (int)(s - src); /* length of what we expand */
10553
Bram Moolenaard812df62008-11-09 12:46:09 +000010554 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010556 if (*usedlen < 2)
10557 {
10558 /* Should we give an error message for #<text? */
10559 *usedlen = 1;
10560 return NULL;
10561 }
10562#ifdef FEAT_EVAL
10563 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10564 (long)i);
10565 if (result == NULL)
10566 {
10567 *errormsg = (char_u *)"";
10568 return NULL;
10569 }
10570#else
10571 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574 }
10575 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010576 {
10577 buf = buflist_findnr(i);
10578 if (buf == NULL)
10579 {
10580 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10581 return NULL;
10582 }
10583 if (lnump != NULL)
10584 *lnump = ECMD_LAST;
10585 if (buf->b_fname == NULL)
10586 {
10587 result = (char_u *)"";
10588 valid = 0; /* Must have ":p:h" to be valid */
10589 }
10590 else
10591 result = buf->b_fname;
10592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593 break;
10594
10595#ifdef FEAT_SEARCHPATH
10596 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010597 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598 if (result == NULL)
10599 {
10600 *errormsg = (char_u *)"";
10601 return NULL;
10602 }
10603 resultbuf = result; /* remember allocated string */
10604 break;
10605#endif
10606
10607#ifdef FEAT_AUTOCMD
10608 case SPEC_AFILE: /* file name for autocommand */
10609 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010610 if (result != NULL && !autocmd_fname_full)
10611 {
10612 /* Still need to turn the fname into a full path. It is
10613 * postponed to avoid a delay when <afile> is not used. */
10614 autocmd_fname_full = TRUE;
10615 result = FullName_save(autocmd_fname, FALSE);
10616 vim_free(autocmd_fname);
10617 autocmd_fname = result;
10618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619 if (result == NULL)
10620 {
10621 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10622 return NULL;
10623 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010624 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625 break;
10626
10627 case SPEC_ABUF: /* buffer number for autocommand */
10628 if (autocmd_bufnr <= 0)
10629 {
10630 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10631 return NULL;
10632 }
10633 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10634 result = strbuf;
10635 break;
10636
10637 case SPEC_AMATCH: /* match name for autocommand */
10638 result = autocmd_match;
10639 if (result == NULL)
10640 {
10641 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10642 return NULL;
10643 }
10644 break;
10645
10646#endif
10647 case SPEC_SFILE: /* file name for ":so" command */
10648 result = sourcing_name;
10649 if (result == NULL)
10650 {
10651 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10652 return NULL;
10653 }
10654 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010655 case SPEC_SLNUM: /* line in file for ":so" command */
10656 if (sourcing_name == NULL || sourcing_lnum == 0)
10657 {
10658 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10659 return NULL;
10660 }
10661 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10662 result = strbuf;
10663 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664#if defined(FEAT_CLIENTSERVER)
10665 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010666 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10667 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668 result = strbuf;
10669 break;
10670#endif
10671 }
10672
10673 resultlen = (int)STRLEN(result); /* length of new string */
10674 if (src[*usedlen] == '<') /* remove the file name extension */
10675 {
10676 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678 resultlen = (int)(s - result);
10679 }
10680#ifdef FEAT_MODIFY_FNAME
10681 else if (!skip_mod)
10682 {
10683 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10684 &resultlen);
10685 if (result == NULL)
10686 {
10687 *errormsg = (char_u *)"";
10688 return NULL;
10689 }
10690 }
10691#endif
10692 }
10693
10694 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10695 {
10696 if (valid != VALID_HEAD + VALID_PATH)
10697 /* xgettext:no-c-format */
10698 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10699 else
10700 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10701 result = NULL;
10702 }
10703 else
10704 result = vim_strnsave(result, resultlen);
10705 vim_free(resultbuf);
10706 return result;
10707}
10708
10709/*
10710 * Concatenate all files in the argument list, separated by spaces, and return
10711 * it in one allocated string.
10712 * Spaces and backslashes in the file names are escaped with a backslash.
10713 * Returns NULL when out of memory.
10714 */
10715 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010716arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717{
10718 int len;
10719 int idx;
10720 char_u *retval = NULL;
10721 char_u *p;
10722
10723 /*
10724 * Do this loop two times:
10725 * first time: compute the total length
10726 * second time: concatenate the names
10727 */
10728 for (;;)
10729 {
10730 len = 0;
10731 for (idx = 0; idx < ARGCOUNT; ++idx)
10732 {
10733 p = alist_name(&ARGLIST[idx]);
10734 if (p != NULL)
10735 {
10736 if (len > 0)
10737 {
10738 /* insert a space in between names */
10739 if (retval != NULL)
10740 retval[len] = ' ';
10741 ++len;
10742 }
10743 for ( ; *p != NUL; ++p)
10744 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010745 if (*p == ' '
10746#ifndef BACKSLASH_IN_FILENAME
10747 || *p == '\\'
10748#endif
10749 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750 {
10751 /* insert a backslash */
10752 if (retval != NULL)
10753 retval[len] = '\\';
10754 ++len;
10755 }
10756 if (retval != NULL)
10757 retval[len] = *p;
10758 ++len;
10759 }
10760 }
10761 }
10762
10763 /* second time: break here */
10764 if (retval != NULL)
10765 {
10766 retval[len] = NUL;
10767 break;
10768 }
10769
10770 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010771 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772 if (retval == NULL)
10773 break;
10774 }
10775
10776 return retval;
10777}
10778
10779#if defined(FEAT_AUTOCMD) || defined(PROTO)
10780/*
10781 * Expand the <sfile> string in "arg".
10782 *
10783 * Returns an allocated string, or NULL for any error.
10784 */
10785 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010786expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787{
10788 char_u *errormsg;
10789 int len;
10790 char_u *result;
10791 char_u *newres;
10792 char_u *repl;
10793 int srclen;
10794 char_u *p;
10795
10796 result = vim_strsave(arg);
10797 if (result == NULL)
10798 return NULL;
10799
10800 for (p = result; *p; )
10801 {
10802 if (STRNCMP(p, "<sfile>", 7) != 0)
10803 ++p;
10804 else
10805 {
10806 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010807 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808 if (errormsg != NULL)
10809 {
10810 if (*errormsg)
10811 emsg(errormsg);
10812 vim_free(result);
10813 return NULL;
10814 }
10815 if (repl == NULL) /* no match (cannot happen) */
10816 {
10817 p += srclen;
10818 continue;
10819 }
10820 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10821 newres = alloc(len);
10822 if (newres == NULL)
10823 {
10824 vim_free(repl);
10825 vim_free(result);
10826 return NULL;
10827 }
10828 mch_memmove(newres, result, (size_t)(p - result));
10829 STRCPY(newres + (p - result), repl);
10830 len = (int)STRLEN(newres);
10831 STRCAT(newres, p + srclen);
10832 vim_free(repl);
10833 vim_free(result);
10834 result = newres;
10835 p = newres + len; /* continue after the match */
10836 }
10837 }
10838
10839 return result;
10840}
10841#endif
10842
10843#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010010844static int ses_winsizes(FILE *fd, int restore_size,
10845 win_T *tab_firstwin);
10846static int ses_win_rec(FILE *fd, frame_T *fr);
10847static frame_T *ses_skipframe(frame_T *fr);
10848static int ses_do_frame(frame_T *fr);
10849static int ses_do_win(win_T *wp);
10850static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
10851static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
10852static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853
10854/*
10855 * Write openfile commands for the current buffers to an .exrc file.
10856 * Return FAIL on error, OK otherwise.
10857 */
10858 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010859makeopens(
10860 FILE *fd,
10861 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862{
10863 buf_T *buf;
10864 int only_save_windows = TRUE;
10865 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866 int restore_size = TRUE;
10867 win_T *wp;
10868 char_u *sname;
10869 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010870 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010871 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010872 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010873 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010874 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010875 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876
10877 if (ssop_flags & SSOP_BUFFERS)
10878 only_save_windows = FALSE; /* Save ALL buffers */
10879
10880 /*
10881 * Begin by setting the this_session variable, and then other
10882 * sessionable variables.
10883 */
10884#ifdef FEAT_EVAL
10885 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10886 return FAIL;
10887 if (ssop_flags & SSOP_GLOBALS)
10888 if (store_session_globals(fd) == FAIL)
10889 return FAIL;
10890#endif
10891
10892 /*
10893 * Close all windows but one.
10894 */
10895 if (put_line(fd, "silent only") == FAIL)
10896 return FAIL;
10897
10898 /*
10899 * Now a :cd command to the session directory or the current directory
10900 */
10901 if (ssop_flags & SSOP_SESDIR)
10902 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010903 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10904 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 return FAIL;
10906 }
10907 else if (ssop_flags & SSOP_CURDIR)
10908 {
10909 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10910 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010911 || fputs("cd ", fd) < 0
10912 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10913 || put_eol(fd) == FAIL)
10914 {
10915 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918 vim_free(sname);
10919 }
10920
10921 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010922 * If there is an empty, unnamed buffer we will wipe it out later.
10923 * Remember the buffer number.
10924 */
10925 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10926 return FAIL;
10927 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10928 return FAIL;
10929 if (put_line(fd, "endif") == FAIL)
10930 return FAIL;
10931
10932 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 * Now save the current files, current buffer first.
10934 */
10935 if (put_line(fd, "set shortmess=aoO") == FAIL)
10936 return FAIL;
10937
10938 /* Now put the other buffers into the buffer list */
10939 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10940 {
10941 if (!(only_save_windows && buf->b_nwindows == 0)
10942 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10943 && buf->b_fname != NULL
10944 && buf->b_p_bl)
10945 {
10946 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10947 : buf->b_wininfo->wi_fpos.lnum) < 0
10948 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10949 return FAIL;
10950 }
10951 }
10952
10953 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010954 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10956 return FAIL;
10957
10958 if (ssop_flags & SSOP_RESIZE)
10959 {
10960 /* Note: after the restore we still check it worked!*/
10961 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10962 || put_eol(fd) == FAIL)
10963 return FAIL;
10964 }
10965
10966#ifdef FEAT_GUI
10967 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10968 {
10969 int x, y;
10970
10971 if (gui_mch_get_winpos(&x, &y) == OK)
10972 {
10973 /* Note: after the restore we still check it worked!*/
10974 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10975 return FAIL;
10976 }
10977 }
10978#endif
10979
10980 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010981 * When there are two or more tabpages and 'showtabline' is 1 the tabline
10982 * will be displayed when creating the next tab. That resizes the windows
10983 * in the first tab, which may cause problems. Set 'showtabline' to 2
10984 * temporarily to avoid that.
10985 */
10986 if (p_stal == 1 && first_tabpage->tp_next != NULL)
10987 {
10988 if (put_line(fd, "set stal=2") == FAIL)
10989 return FAIL;
10990 restore_stal = TRUE;
10991 }
10992
10993 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010994 * May repeat putting Windows for each tab, when "tabpages" is in
10995 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010996 * Don't use goto_tabpage(), it may change directory and trigger
10997 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010999 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011000 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011001 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011003 int need_tabnew = FALSE;
11004 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011005
Bram Moolenaar18144c82006-04-12 21:52:12 +000011006 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011008 tabpage_T *tp = find_tabpage(tabnr);
11009
11010 if (tp == NULL)
11011 break; /* done all tab pages */
11012 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011013 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011014 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011015 tab_topframe = topframe;
11016 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011017 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011018 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011019 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011020 tab_topframe = tp->tp_topframe;
11021 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011022 if (tabnr > 1)
11023 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025
Bram Moolenaar18144c82006-04-12 21:52:12 +000011026 /*
11027 * Before creating the window layout, try loading one file. If this
11028 * is aborted we don't end up with a number of useless windows.
11029 * This may have side effects! (e.g., compressed or network file).
11030 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011031 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011032 {
11033 if (ses_do_win(wp)
11034 && wp->w_buffer->b_ffname != NULL
11035 && !wp->w_buffer->b_help
11036#ifdef FEAT_QUICKFIX
11037 && !bt_nofile(wp->w_buffer)
11038#endif
11039 )
11040 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011041 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000011042 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
11043 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011044 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011045 if (!wp->w_arg_idx_invalid)
11046 edited_win = wp;
11047 break;
11048 }
11049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011050
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011051 /* If no file got edited create an empty tab page. */
11052 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11053 return FAIL;
11054
Bram Moolenaar18144c82006-04-12 21:52:12 +000011055 /*
11056 * Save current window layout.
11057 */
11058 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011060 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011062 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11063 return FAIL;
11064 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11065 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066
Bram Moolenaar18144c82006-04-12 21:52:12 +000011067 /*
11068 * Check if window sizes can be restored (no windows omitted).
11069 * Remember the window number of the current window after restoring.
11070 */
11071 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011072 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011073 {
11074 if (ses_do_win(wp))
11075 ++nr;
11076 else
11077 restore_size = FALSE;
11078 if (curwin == wp)
11079 cnr = nr;
11080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011081
Bram Moolenaar18144c82006-04-12 21:52:12 +000011082 /* Go to the first window. */
11083 if (put_line(fd, "wincmd t") == FAIL)
11084 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011085
Bram Moolenaar18144c82006-04-12 21:52:12 +000011086 /*
11087 * If more than one window, see if sizes can be restored.
11088 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11089 * resized when moving between windows.
11090 * Do this before restoring the view, so that the topline and the
11091 * cursor can be set. This is done again below.
11092 */
11093 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
11094 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011095 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011096 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097
Bram Moolenaar18144c82006-04-12 21:52:12 +000011098 /*
11099 * Restore the view of the window (options, file, cursor, etc.).
11100 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011101 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011102 {
11103 if (!ses_do_win(wp))
11104 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011105 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11106 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011107 return FAIL;
11108 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11109 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011110 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011111 }
11112
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011113 /* The argument index in the first tab page is zero, need to set it in
11114 * each window. For further tab pages it's the window where we do
11115 * "tabedit". */
11116 cur_arg_idx = next_arg_idx;
11117
Bram Moolenaar18144c82006-04-12 21:52:12 +000011118 /*
11119 * Restore cursor to the current window if it's not the first one.
11120 */
11121 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11122 || put_eol(fd) == FAIL))
11123 return FAIL;
11124
11125 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011126 * Restore window sizes again after jumping around in windows, because
11127 * the current window has a minimum size while others may not.
11128 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011129 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011130 return FAIL;
11131
Bram Moolenaar18144c82006-04-12 21:52:12 +000011132 /* Don't continue in another tab page when doing only the current one
11133 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011134 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011135 break;
11136 }
11137
11138 if (ssop_flags & SSOP_TABPAGES)
11139 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011140 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11141 || put_eol(fd) == FAIL)
11142 return FAIL;
11143 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011144 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11145 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011146
Bram Moolenaar9c102382006-05-03 21:26:49 +000011147 /*
11148 * Wipe out an empty unnamed buffer we started in.
11149 */
11150 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11151 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011152 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011153 return FAIL;
11154 if (put_line(fd, "endif") == FAIL)
11155 return FAIL;
11156 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11157 return FAIL;
11158
11159 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11160 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11161 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11162 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011163
11164 /*
11165 * Lastly, execute the x.vim file if it exists.
11166 */
11167 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11168 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011169 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011170 || put_line(fd, "endif") == FAIL)
11171 return FAIL;
11172
11173 return OK;
11174}
11175
11176 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011177ses_winsizes(
11178 FILE *fd,
11179 int restore_size,
11180 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181{
11182 int n = 0;
11183 win_T *wp;
11184
11185 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11186 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011187 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188 {
11189 if (!ses_do_win(wp))
11190 continue;
11191 ++n;
11192
11193 /* restore height when not full height */
11194 if (wp->w_height + wp->w_status_height < topframe->fr_height
11195 && (fprintf(fd,
11196 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11197 n, (long)wp->w_height, Rows / 2, Rows) < 0
11198 || put_eol(fd) == FAIL))
11199 return FAIL;
11200
11201 /* restore width when not full width */
11202 if (wp->w_width < Columns && (fprintf(fd,
11203 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11204 n, (long)wp->w_width, Columns / 2, Columns) < 0
11205 || put_eol(fd) == FAIL))
11206 return FAIL;
11207 }
11208 }
11209 else
11210 {
11211 /* Just equalise window sizes */
11212 if (put_line(fd, "wincmd =") == FAIL)
11213 return FAIL;
11214 }
11215 return OK;
11216}
11217
11218/*
11219 * Write commands to "fd" to recursively create windows for frame "fr",
11220 * horizontally and vertically split.
11221 * After the commands the last window in the frame is the current window.
11222 * Returns FAIL when writing the commands to "fd" fails.
11223 */
11224 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011225ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011226{
11227 frame_T *frc;
11228 int count = 0;
11229
11230 if (fr->fr_layout != FR_LEAF)
11231 {
11232 /* Find first frame that's not skipped and then create a window for
11233 * each following one (first frame is already there). */
11234 frc = ses_skipframe(fr->fr_child);
11235 if (frc != NULL)
11236 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11237 {
11238 /* Make window as big as possible so that we have lots of room
11239 * to split. */
11240 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11241 || put_line(fd, fr->fr_layout == FR_COL
11242 ? "split" : "vsplit") == FAIL)
11243 return FAIL;
11244 ++count;
11245 }
11246
11247 /* Go back to the first window. */
11248 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11249 ? "%dwincmd k" : "%dwincmd h", count) < 0
11250 || put_eol(fd) == FAIL))
11251 return FAIL;
11252
11253 /* Recursively create frames/windows in each window of this column or
11254 * row. */
11255 frc = ses_skipframe(fr->fr_child);
11256 while (frc != NULL)
11257 {
11258 ses_win_rec(fd, frc);
11259 frc = ses_skipframe(frc->fr_next);
11260 /* Go to next window. */
11261 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11262 return FAIL;
11263 }
11264 }
11265 return OK;
11266}
11267
11268/*
11269 * Skip frames that don't contain windows we want to save in the Session.
11270 * Returns NULL when there none.
11271 */
11272 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011273ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274{
11275 frame_T *frc;
11276
11277 for (frc = fr; frc != NULL; frc = frc->fr_next)
11278 if (ses_do_frame(frc))
11279 break;
11280 return frc;
11281}
11282
11283/*
11284 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11285 * the Session.
11286 */
11287 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011288ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289{
11290 frame_T *frc;
11291
11292 if (fr->fr_layout == FR_LEAF)
11293 return ses_do_win(fr->fr_win);
11294 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11295 if (ses_do_frame(frc))
11296 return TRUE;
11297 return FALSE;
11298}
11299
11300/*
11301 * Return non-zero if window "wp" is to be stored in the Session.
11302 */
11303 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011304ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305{
11306 if (wp->w_buffer->b_fname == NULL
11307#ifdef FEAT_QUICKFIX
11308 /* When 'buftype' is "nofile" can't restore the window contents. */
11309 || bt_nofile(wp->w_buffer)
11310#endif
11311 )
11312 return (ssop_flags & SSOP_BLANK);
11313 if (wp->w_buffer->b_help)
11314 return (ssop_flags & SSOP_HELP);
11315 return TRUE;
11316}
11317
11318/*
11319 * Write commands to "fd" to restore the view of a window.
11320 * Caller must make sure 'scrolloff' is zero.
11321 */
11322 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011323put_view(
11324 FILE *fd,
11325 win_T *wp,
11326 int add_edit, /* add ":edit" command to view */
11327 unsigned *flagp, /* vop_flags or ssop_flags */
11328 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011329 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330{
11331 win_T *save_curwin;
11332 int f;
11333 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011334 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335
11336 /* Always restore cursor position for ":mksession". For ":mkview" only
11337 * when 'viewoptions' contains "cursor". */
11338 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11339
11340 /*
11341 * Local argument list.
11342 */
11343 if (wp->w_alist == &global_alist)
11344 {
11345 if (put_line(fd, "argglobal") == FAIL)
11346 return FAIL;
11347 }
11348 else
11349 {
11350 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11351 flagp == &vop_flags
11352 || !(*flagp & SSOP_CURDIR)
11353 || wp->w_localdir != NULL, flagp) == FAIL)
11354 return FAIL;
11355 }
11356
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011357 /* Only when part of a session: restore the argument index. Some
11358 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011359 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011360 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011362 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363 || put_eol(fd) == FAIL)
11364 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011365 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011366 }
11367
11368 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011369 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370 {
11371 /*
11372 * Load the file.
11373 */
11374 if (wp->w_buffer->b_ffname != NULL
11375#ifdef FEAT_QUICKFIX
11376 && !bt_nofile(wp->w_buffer)
11377#endif
11378 )
11379 {
11380 /*
11381 * Editing a file in this buffer: use ":edit file".
11382 * This may have side effects! (e.g., compressed or network file).
11383 */
11384 if (fputs("edit ", fd) < 0
11385 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11386 return FAIL;
11387 }
11388 else
11389 {
11390 /* No file in this buffer, just make it empty. */
11391 if (put_line(fd, "enew") == FAIL)
11392 return FAIL;
11393#ifdef FEAT_QUICKFIX
11394 if (wp->w_buffer->b_ffname != NULL)
11395 {
11396 /* The buffer does have a name, but it's not a file name. */
11397 if (fputs("file ", fd) < 0
11398 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11399 return FAIL;
11400 }
11401#endif
11402 do_cursor = FALSE;
11403 }
11404 }
11405
11406 /*
11407 * Local mappings and abbreviations.
11408 */
11409 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11410 && makemap(fd, wp->w_buffer) == FAIL)
11411 return FAIL;
11412
11413 /*
11414 * Local options. Need to go to the window temporarily.
11415 * Store only local values when using ":mkview" and when ":mksession" is
11416 * used and 'sessionoptions' doesn't include "options".
11417 * Some folding options are always stored when "folds" is included,
11418 * otherwise the folds would not be restored correctly.
11419 */
11420 save_curwin = curwin;
11421 curwin = wp;
11422 curbuf = curwin->w_buffer;
11423 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11424 f = makeset(fd, OPT_LOCAL,
11425 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11426#ifdef FEAT_FOLDING
11427 else if (*flagp & SSOP_FOLDS)
11428 f = makefoldset(fd);
11429#endif
11430 else
11431 f = OK;
11432 curwin = save_curwin;
11433 curbuf = curwin->w_buffer;
11434 if (f == FAIL)
11435 return FAIL;
11436
11437#ifdef FEAT_FOLDING
11438 /*
11439 * Save Folds when 'buftype' is empty and for help files.
11440 */
11441 if ((*flagp & SSOP_FOLDS)
11442 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011443# ifdef FEAT_QUICKFIX
11444 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11445# endif
11446 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447 {
11448 if (put_folds(fd, wp) == FAIL)
11449 return FAIL;
11450 }
11451#endif
11452
11453 /*
11454 * Set the cursor after creating folds, since that moves the cursor.
11455 */
11456 if (do_cursor)
11457 {
11458
11459 /* Restore the cursor line in the file and relatively in the
11460 * window. Don't use "G", it changes the jumplist. */
11461 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11462 (long)wp->w_cursor.lnum,
11463 (long)(wp->w_cursor.lnum - wp->w_topline),
11464 (long)wp->w_height / 2, (long)wp->w_height) < 0
11465 || put_eol(fd) == FAIL
11466 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11467 || put_line(fd, "exe s:l") == FAIL
11468 || put_line(fd, "normal! zt") == FAIL
11469 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11470 || put_eol(fd) == FAIL)
11471 return FAIL;
11472 /* Restore the cursor column and left offset when not wrapping. */
11473 if (wp->w_cursor.col == 0)
11474 {
11475 if (put_line(fd, "normal! 0") == FAIL)
11476 return FAIL;
11477 }
11478 else
11479 {
11480 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11481 {
11482 if (fprintf(fd,
11483 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011484 (long)wp->w_virtcol + 1,
11485 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486 (long)wp->w_width / 2, (long)wp->w_width) < 0
11487 || put_eol(fd) == FAIL
11488 || put_line(fd, "if s:c > 0") == FAIL
11489 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011490 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11491 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011492 || put_eol(fd) == FAIL
11493 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011494 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495 || put_eol(fd) == FAIL
11496 || put_line(fd, "endif") == FAIL)
11497 return FAIL;
11498 }
11499 else
11500 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011501 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502 || put_eol(fd) == FAIL)
11503 return FAIL;
11504 }
11505 }
11506 }
11507
11508 /*
11509 * Local directory.
11510 */
11511 if (wp->w_localdir != NULL)
11512 {
11513 if (fputs("lcd ", fd) < 0
11514 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11515 || put_eol(fd) == FAIL)
11516 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011517 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518 }
11519
11520 return OK;
11521}
11522
11523/*
11524 * Write an argument list to the session file.
11525 * Returns FAIL if writing fails.
11526 */
11527 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011528ses_arglist(
11529 FILE *fd,
11530 char *cmd,
11531 garray_T *gap,
11532 int fullname, /* TRUE: use full path name */
11533 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011534{
11535 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011536 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537 char_u *s;
11538
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011539 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11540 return FAIL;
11541 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542 return FAIL;
11543 for (i = 0; i < gap->ga_len; ++i)
11544 {
11545 /* NULL file names are skipped (only happens when out of memory). */
11546 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11547 if (s != NULL)
11548 {
11549 if (fullname)
11550 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011551 buf = alloc(MAXPATHL);
11552 if (buf != NULL)
11553 {
11554 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11555 s = buf;
11556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011557 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011558 if (fputs("argadd ", fd) < 0
11559 || ses_put_fname(fd, s, flagp) == FAIL
11560 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011561 {
11562 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011563 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011564 }
11565 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566 }
11567 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011568 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011569}
11570
11571/*
11572 * Write a buffer name to the session file.
11573 * Also ends the line.
11574 * Returns FAIL if writing fails.
11575 */
11576 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011577ses_fname(FILE *fd, buf_T *buf, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011578{
11579 char_u *name;
11580
11581 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011582 * the session file will be sourced.
11583 * Don't do this for ":mkview", we don't know the current directory.
11584 * Don't do this after ":lcd", we don't keep track of what the current
11585 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586 if (buf->b_sfname != NULL
11587 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011588 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011589#ifdef FEAT_AUTOCHDIR
11590 && !p_acd
11591#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011592 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011593 name = buf->b_sfname;
11594 else
11595 name = buf->b_ffname;
11596 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11597 return FAIL;
11598 return OK;
11599}
11600
11601/*
11602 * Write a file name to the session file.
11603 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11604 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011605 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011606 */
11607 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011608ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011609{
11610 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011611 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011613
11614 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011615 if (sname == NULL)
11616 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011617
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011618 if (*flagp & SSOP_SLASH)
11619 {
11620 /* change all backslashes to forward slashes */
11621 for (p = sname; *p != NUL; mb_ptr_adv(p))
11622 if (*p == '\\')
11623 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011624 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011625
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011626 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011627 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011628 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011629 if (p == NULL)
11630 return FAIL;
11631
11632 /* write the result */
11633 if (fputs((char *)p, fd) < 0)
11634 retval = FAIL;
11635
11636 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011637 return retval;
11638}
11639
11640/*
11641 * ":loadview [nr]"
11642 */
11643 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011644ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011645{
11646 char_u *fname;
11647
11648 fname = get_view_file(*eap->arg);
11649 if (fname != NULL)
11650 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011651 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011652 vim_free(fname);
11653 }
11654}
11655
11656/*
11657 * Get the name of the view file for the current buffer.
11658 */
11659 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011660get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011661{
11662 int len = 0;
11663 char_u *p, *s;
11664 char_u *retval;
11665 char_u *sname;
11666
11667 if (curbuf->b_ffname == NULL)
11668 {
11669 EMSG(_(e_noname));
11670 return NULL;
11671 }
11672 sname = home_replace_save(NULL, curbuf->b_ffname);
11673 if (sname == NULL)
11674 return NULL;
11675
11676 /*
11677 * We want a file name without separators, because we're not going to make
11678 * a directory.
11679 * "normal" path separator -> "=+"
11680 * "=" -> "=="
11681 * ":" path separator -> "=-"
11682 */
11683 for (p = sname; *p; ++p)
11684 if (*p == '=' || vim_ispathsep(*p))
11685 ++len;
11686 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11687 if (retval != NULL)
11688 {
11689 STRCPY(retval, p_vdir);
11690 add_pathsep(retval);
11691 s = retval + STRLEN(retval);
11692 for (p = sname; *p; ++p)
11693 {
11694 if (*p == '=')
11695 {
11696 *s++ = '=';
11697 *s++ = '=';
11698 }
11699 else if (vim_ispathsep(*p))
11700 {
11701 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011702#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011703 if (*p == ':')
11704 *s++ = '-';
11705 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011706#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011707 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011708 }
11709 else
11710 *s++ = *p;
11711 }
11712 *s++ = '=';
11713 *s++ = c;
11714 STRCPY(s, ".vim");
11715 }
11716
11717 vim_free(sname);
11718 return retval;
11719}
11720
11721#endif /* FEAT_SESSION */
11722
11723/*
11724 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11725 * Return FAIL for a write error.
11726 */
11727 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011728put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011729{
11730 if (
11731#ifdef USE_CRNL
11732 (
11733# ifdef MKSESSION_NL
11734 !mksession_nl &&
11735# endif
11736 (putc('\r', fd) < 0)) ||
11737#endif
11738 (putc('\n', fd) < 0))
11739 return FAIL;
11740 return OK;
11741}
11742
11743/*
11744 * Write a line to "fd".
11745 * Return FAIL for a write error.
11746 */
11747 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011748put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011749{
11750 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11751 return FAIL;
11752 return OK;
11753}
11754
11755#ifdef FEAT_VIMINFO
11756/*
11757 * ":rviminfo" and ":wviminfo".
11758 */
11759 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011760ex_viminfo(
11761 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011762{
11763 char_u *save_viminfo;
11764
11765 save_viminfo = p_viminfo;
11766 if (*p_viminfo == NUL)
11767 p_viminfo = (char_u *)"'100";
11768 if (eap->cmdidx == CMD_rviminfo)
11769 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011770 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11771 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772 EMSG(_("E195: Cannot open viminfo file for reading"));
11773 }
11774 else
11775 write_viminfo(eap->arg, eap->forceit);
11776 p_viminfo = save_viminfo;
11777}
11778#endif
11779
11780#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011781/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011782 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011783 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011784 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011785 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011786dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011787{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011788 if (fname == NULL)
11789 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011790 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011791}
11792#endif
11793
11794/*
11795 * ":behave {mswin,xterm}"
11796 */
11797 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011798ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011799{
11800 if (STRCMP(eap->arg, "mswin") == 0)
11801 {
11802 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11803 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11804 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11805 set_option_value((char_u *)"keymodel", 0L,
11806 (char_u *)"startsel,stopsel", 0);
11807 }
11808 else if (STRCMP(eap->arg, "xterm") == 0)
11809 {
11810 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11811 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11812 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11813 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11814 }
11815 else
11816 EMSG2(_(e_invarg2), eap->arg);
11817}
11818
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011819#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11820/*
11821 * Function given to ExpandGeneric() to obtain the possible arguments of the
11822 * ":behave {mswin,xterm}" command.
11823 */
11824 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011825get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011826{
11827 if (idx == 0)
11828 return (char_u *)"mswin";
11829 if (idx == 1)
11830 return (char_u *)"xterm";
11831 return NULL;
11832}
11833#endif
11834
Bram Moolenaar071d4272004-06-13 20:20:40 +000011835#ifdef FEAT_AUTOCMD
11836static int filetype_detect = FALSE;
11837static int filetype_plugin = FALSE;
11838static int filetype_indent = FALSE;
11839
11840/*
11841 * ":filetype [plugin] [indent] {on,off,detect}"
11842 * on: Load the filetype.vim file to install autocommands for file types.
11843 * off: Load the ftoff.vim file to remove all autocommands for file types.
11844 * plugin on: load filetype.vim and ftplugin.vim
11845 * plugin off: load ftplugof.vim
11846 * indent on: load filetype.vim and indent.vim
11847 * indent off: load indoff.vim
11848 */
11849 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011850ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011851{
11852 char_u *arg = eap->arg;
11853 int plugin = FALSE;
11854 int indent = FALSE;
11855
11856 if (*eap->arg == NUL)
11857 {
11858 /* Print current status. */
11859 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11860 filetype_detect ? "ON" : "OFF",
11861 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11862 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11863 return;
11864 }
11865
11866 /* Accept "plugin" and "indent" in any order. */
11867 for (;;)
11868 {
11869 if (STRNCMP(arg, "plugin", 6) == 0)
11870 {
11871 plugin = TRUE;
11872 arg = skipwhite(arg + 6);
11873 continue;
11874 }
11875 if (STRNCMP(arg, "indent", 6) == 0)
11876 {
11877 indent = TRUE;
11878 arg = skipwhite(arg + 6);
11879 continue;
11880 }
11881 break;
11882 }
11883 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11884 {
11885 if (*arg == 'o' || !filetype_detect)
11886 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011887 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011888 filetype_detect = TRUE;
11889 if (plugin)
11890 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011891 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011892 filetype_plugin = TRUE;
11893 }
11894 if (indent)
11895 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011896 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011897 filetype_indent = TRUE;
11898 }
11899 }
11900 if (*arg == 'd')
11901 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020011902 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011903 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011904 }
11905 }
11906 else if (STRCMP(arg, "off") == 0)
11907 {
11908 if (plugin || indent)
11909 {
11910 if (plugin)
11911 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011912 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011913 filetype_plugin = FALSE;
11914 }
11915 if (indent)
11916 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011917 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011918 filetype_indent = FALSE;
11919 }
11920 }
11921 else
11922 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010011923 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924 filetype_detect = FALSE;
11925 }
11926 }
11927 else
11928 EMSG2(_(e_invarg2), arg);
11929}
11930
11931/*
11932 * ":setfiletype {name}"
11933 */
11934 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011935ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011936{
11937 if (!did_filetype)
11938 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11939}
11940#endif
11941
Bram Moolenaar071d4272004-06-13 20:20:40 +000011942 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011943ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011944{
11945#ifdef FEAT_DIGRAPHS
11946 if (*eap->arg != NUL)
11947 putdigraph(eap->arg);
11948 else
11949 listdigraphs();
11950#else
11951 EMSG(_("E196: No digraphs in this version"));
11952#endif
11953}
11954
11955 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011956ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011957{
11958 int flags = 0;
11959
11960 if (eap->cmdidx == CMD_setlocal)
11961 flags = OPT_LOCAL;
11962 else if (eap->cmdidx == CMD_setglobal)
11963 flags = OPT_GLOBAL;
11964#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11965 if (cmdmod.browse && flags == 0)
11966 ex_options(eap);
11967 else
11968#endif
11969 (void)do_set(eap->arg, flags);
11970}
11971
11972#ifdef FEAT_SEARCH_EXTRA
11973/*
11974 * ":nohlsearch"
11975 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011976 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011977ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011978{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010011979 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011980 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981}
11982
11983/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011984 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985 * Sets nextcmd to the start of the next command, if any. Also called when
11986 * skipping commands to find the next command.
11987 */
11988 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011989ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990{
11991 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011992 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011993 char_u *end;
11994 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011995 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011996
11997 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011998 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011999 else
12000 {
12001 EMSG(e_invcmd);
12002 return;
12003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004
12005 /* First clear any old pattern. */
12006 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012007 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008
12009 if (ends_excmd(*eap->arg))
12010 end = eap->arg;
12011 else if ((STRNICMP(eap->arg, "none", 4) == 0
12012 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
12013 end = eap->arg + 4;
12014 else
12015 {
12016 p = skiptowhite(eap->arg);
12017 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012018 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019 p = skipwhite(p);
12020 if (*p == NUL)
12021 {
12022 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012023 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024 EMSG2(_(e_invarg2), eap->arg);
12025 return;
12026 }
12027 end = skip_regexp(p + 1, *p, TRUE, NULL);
12028 if (!eap->skip)
12029 {
12030 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12031 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012032 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033 eap->errmsg = e_trailing;
12034 return;
12035 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012036 if (*end != *p)
12037 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012038 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012039 EMSG2(_(e_invarg2), p);
12040 return;
12041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012042
12043 c = *end;
12044 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012045 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012046 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012047 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012048 }
12049 }
12050 eap->nextcmd = find_nextcmd(end);
12051}
12052#endif
12053
12054#ifdef FEAT_CRYPT
12055/*
12056 * ":X": Get crypt key
12057 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012058 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012059ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012060{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012061 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012062 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012063}
12064#endif
12065
12066#ifdef FEAT_FOLDING
12067 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012068ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012069{
12070 if (foldManualAllowed(TRUE))
12071 foldCreate(eap->line1, eap->line2);
12072}
12073
12074 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012075ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012076{
12077 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12078 eap->forceit, FALSE);
12079}
12080
12081 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012082ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083{
12084 linenr_T lnum;
12085
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012086#ifdef FEAT_CLIPBOARD
12087 start_global_changes();
12088#endif
12089
Bram Moolenaar071d4272004-06-13 20:20:40 +000012090 /* First set the marks for all lines closed/open. */
12091 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12092 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12093 ml_setmarked(lnum);
12094
12095 /* Execute the command on the marked lines. */
12096 global_exe(eap->arg);
12097 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012098#ifdef FEAT_CLIPBOARD
12099 end_global_changes();
12100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101}
12102#endif