blob: 24df1d17bf3c09f6ed5a0d18aec8e5c79e4a9acf [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
22#ifdef FEAT_USR_CMDS
23typedef struct ucmd
24{
25 char_u *uc_name; /* The command name */
26 long_u uc_argt; /* The argument type */
27 char_u *uc_rep; /* The command's replacement string */
28 long uc_def; /* The default value for a range/count */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int uc_compl; /* completion type */
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +010030 int uc_addr_type; /* The command's address type */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010031# ifdef FEAT_EVAL
32 scid_T uc_scriptID; /* SID where the command was defined */
33# ifdef FEAT_CMDL_COMPL
Bram Moolenaar071d4272004-06-13 20:20:40 +000034 char_u *uc_compl_arg; /* completion argument if any */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010035# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000036# endif
37} ucmd_T;
38
39#define UC_BUFFER 1 /* -buffer: local to current buffer */
40
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000041static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
43#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
44#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
45
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010046static void do_ucmd(exarg_T *eap);
47static void ex_command(exarg_T *eap);
48static void ex_delcommand(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# ifdef FEAT_CMDL_COMPL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010050static char_u *get_user_command_name(int idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051# endif
52
Bram Moolenaar958636c2014-10-21 20:01:58 +020053/* Wether a command index indicates a user command. */
54# define IS_USER_CMDIDX(idx) ((int)(idx) < 0)
55
Bram Moolenaar071d4272004-06-13 20:20:40 +000056#else
57# define ex_command ex_ni
58# define ex_comclear ex_ni
59# define ex_delcommand ex_ni
Bram Moolenaar958636c2014-10-21 20:01:58 +020060/* Wether a command index indicates a user command. */
61# define IS_USER_CMDIDX(idx) (FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000062#endif
63
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010064static int compute_buffer_local_count(int addr_type, int lnum, int local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000065#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010066static char_u *do_one_cmd(char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000067#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010068static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000069static int if_level = 0; /* depth in :if */
70#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010071static void append_command(char_u *cmd);
72static char_u *find_command(exarg_T *eap, int *full);
Bram Moolenaar071d4272004-06-13 20:20:40 +000073
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010074static void ex_abbreviate(exarg_T *eap);
75static void ex_map(exarg_T *eap);
76static void ex_unmap(exarg_T *eap);
77static void ex_mapclear(exarg_T *eap);
78static void ex_abclear(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#ifndef FEAT_MENU
80# define ex_emenu ex_ni
81# define ex_menu ex_ni
82# define ex_menutranslate ex_ni
83#endif
84#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static void ex_autocmd(exarg_T *eap);
86static void ex_doautocmd(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#else
88# define ex_autocmd ex_ni
89# define ex_doautocmd ex_ni
90# define ex_doautoall ex_ni
91#endif
92#ifdef FEAT_LISTCMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010093static void ex_bunload(exarg_T *eap);
94static void ex_buffer(exarg_T *eap);
95static void ex_bmodified(exarg_T *eap);
96static void ex_bnext(exarg_T *eap);
97static void ex_bprevious(exarg_T *eap);
98static void ex_brewind(exarg_T *eap);
99static void ex_blast(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#else
101# define ex_bunload ex_ni
102# define ex_buffer ex_ni
103# define ex_bmodified ex_ni
104# define ex_bnext ex_ni
105# define ex_bprevious ex_ni
106# define ex_brewind ex_ni
107# define ex_blast ex_ni
108# define buflist_list ex_ni
109# define ex_checktime ex_ni
110#endif
111#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
112# define ex_buffer_all ex_ni
113#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100114static char_u *getargcmd(char_u **);
115static char_u *skip_cmd_arg(char_u *p, int rembs);
116static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#ifndef FEAT_QUICKFIX
118# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000119# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120# define ex_cc ex_ni
121# define ex_cnext ex_ni
122# define ex_cfile ex_ni
123# define qf_list ex_ni
124# define qf_age ex_ni
125# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000126# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127#endif
128#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
129# define ex_cclose ex_ni
130# define ex_copen ex_ni
131# define ex_cwindow ex_ni
132#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000133#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
134# define ex_cexpr ex_ni
135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100137static int check_more(int, int);
138static linenr_T get_address(exarg_T *, char_u **, int addr_type, int skip, int to_other_file);
139static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200140#if !defined(FEAT_PERL) \
141 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
142 || !defined(FEAT_TCL) \
143 || !defined(FEAT_RUBY) \
144 || !defined(FEAT_LUA) \
145 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000146# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100147static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100149static char_u *invalid_range(exarg_T *eap);
150static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000151#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100152static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000153#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100154static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
155static void ex_highlight(exarg_T *eap);
156static void ex_colorscheme(exarg_T *eap);
157static void ex_quit(exarg_T *eap);
158static void ex_cquit(exarg_T *eap);
159static void ex_quit_all(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100161static void ex_close(exarg_T *eap);
162static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
163static void ex_only(exarg_T *eap);
164static void ex_resize(exarg_T *eap);
165static void ex_stag(exarg_T *eap);
166static void ex_tabclose(exarg_T *eap);
167static void ex_tabonly(exarg_T *eap);
168static void ex_tabnext(exarg_T *eap);
169static void ex_tabmove(exarg_T *eap);
170static void ex_tabs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171#else
172# define ex_close ex_ni
173# define ex_only ex_ni
174# define ex_all ex_ni
175# define ex_resize ex_ni
176# define ex_splitview ex_ni
177# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000178# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000179# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000180# define ex_tabs ex_ni
181# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000182# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183#endif
184#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100185static void ex_pclose(exarg_T *eap);
186static void ex_ptag(exarg_T *eap);
187static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188#else
189# define ex_pclose ex_ni
190# define ex_ptag ex_ni
191# define ex_pedit ex_ni
192#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100193static void ex_hide(exarg_T *eap);
194static void ex_stop(exarg_T *eap);
195static void ex_exit(exarg_T *eap);
196static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100198static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199#else
200# define ex_goto ex_ni
201#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100202static void ex_shell(exarg_T *eap);
203static void ex_preserve(exarg_T *eap);
204static void ex_recover(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205#ifndef FEAT_LISTCMDS
206# define ex_argedit ex_ni
207# define ex_argadd ex_ni
208# define ex_argdelete ex_ni
209# define ex_listdo ex_ni
210#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100211static void ex_mode(exarg_T *eap);
212static void ex_wrongmodifier(exarg_T *eap);
213static void ex_find(exarg_T *eap);
214static void ex_open(exarg_T *eap);
215static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
217# define ex_drop ex_ni
218#endif
219#ifndef FEAT_GUI
220# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100221static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222#endif
223#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100224static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225#else
226# define ex_tearoff ex_ni
227#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000228#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100229static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230#else
231# define ex_popup ex_ni
232#endif
233#ifndef FEAT_GUI_MSWIN
234# define ex_simalt ex_ni
235#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000236#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237# define gui_mch_find_dialog ex_ni
238# define gui_mch_replace_dialog ex_ni
239#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000240#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241# define ex_helpfind ex_ni
242#endif
243#ifndef FEAT_CSCOPE
244# define do_cscope ex_ni
245# define do_scscope ex_ni
246# define do_cstag ex_ni
247#endif
248#ifndef FEAT_SYN_HL
249# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200250# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000251#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200252#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200253# define ex_syntime ex_ni
254#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000255#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000256# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000257# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000258# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000259# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000260# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000261#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200262#ifndef FEAT_PERSISTENT_UNDO
263# define ex_rundo ex_ni
264# define ex_wundo ex_ni
265#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200266#ifndef FEAT_LUA
267# define ex_lua ex_script_ni
268# define ex_luado ex_ni
269# define ex_luafile ex_ni
270#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000271#ifndef FEAT_MZSCHEME
272# define ex_mzscheme ex_script_ni
273# define ex_mzfile ex_ni
274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275#ifndef FEAT_PERL
276# define ex_perl ex_script_ni
277# define ex_perldo ex_ni
278#endif
279#ifndef FEAT_PYTHON
280# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200281# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282# define ex_pyfile ex_ni
283#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200284#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200285# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200286# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200287# define ex_py3file ex_ni
288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289#ifndef FEAT_TCL
290# define ex_tcl ex_script_ni
291# define ex_tcldo ex_ni
292# define ex_tclfile ex_ni
293#endif
294#ifndef FEAT_RUBY
295# define ex_ruby ex_script_ni
296# define ex_rubydo ex_ni
297# define ex_rubyfile ex_ni
298#endif
299#ifndef FEAT_SNIFF
300# define ex_sniff ex_ni
301#endif
302#ifndef FEAT_KEYMAP
303# define ex_loadkeymap ex_ni
304#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100305static void ex_swapname(exarg_T *eap);
306static void ex_syncbind(exarg_T *eap);
307static void ex_read(exarg_T *eap);
308static void ex_pwd(exarg_T *eap);
309static void ex_equal(exarg_T *eap);
310static void ex_sleep(exarg_T *eap);
311static void do_exmap(exarg_T *eap, int isabbrev);
312static void ex_winsize(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100314static void ex_wincmd(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315#else
316# define ex_wincmd ex_ni
317#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000318#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100319static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320#else
321# define ex_winpos ex_ni
322#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100323static void ex_operators(exarg_T *eap);
324static void ex_put(exarg_T *eap);
325static void ex_copymove(exarg_T *eap);
326static void ex_submagic(exarg_T *eap);
327static void ex_join(exarg_T *eap);
328static void ex_at(exarg_T *eap);
329static void ex_bang(exarg_T *eap);
330static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200331#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100332static void ex_wundo(exarg_T *eap);
333static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200334#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100335static void ex_redo(exarg_T *eap);
336static void ex_later(exarg_T *eap);
337static void ex_redir(exarg_T *eap);
338static void ex_redraw(exarg_T *eap);
339static void ex_redrawstatus(exarg_T *eap);
340static void close_redir(void);
341static void ex_mkrc(exarg_T *eap);
342static void ex_mark(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343#ifdef FEAT_USR_CMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100344static char_u *uc_fun_cmd(void);
345static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346#endif
347#ifdef FEAT_EX_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100348static void ex_startinsert(exarg_T *eap);
349static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350#else
351# define ex_normal ex_ni
352# define ex_align ex_ni
353# define ex_retab ex_ni
354# define ex_startinsert ex_ni
355# define ex_stopinsert ex_ni
356# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000357# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#endif
359#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100360static void ex_checkpath(exarg_T *eap);
361static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362#else
363# define ex_findpat ex_ni
364# define ex_checkpath ex_ni
365#endif
366#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100367static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368#else
369# define ex_psearch ex_ni
370#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100371static void ex_tag(exarg_T *eap);
372static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373#ifndef FEAT_EVAL
374# define ex_scriptnames ex_ni
375# define ex_finish ex_ni
376# define ex_echo ex_ni
377# define ex_echohl ex_ni
378# define ex_execute ex_ni
379# define ex_call ex_ni
380# define ex_if ex_ni
381# define ex_endif ex_ni
382# define ex_else ex_ni
383# define ex_while ex_ni
384# define ex_continue ex_ni
385# define ex_break ex_ni
386# define ex_endwhile ex_ni
387# define ex_throw ex_ni
388# define ex_try ex_ni
389# define ex_catch ex_ni
390# define ex_finally ex_ni
391# define ex_endtry ex_ni
392# define ex_endfunction ex_ni
393# define ex_let ex_ni
394# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000395# define ex_lockvar ex_ni
396# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397# define ex_function ex_ni
398# define ex_delfunction ex_ni
399# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000400# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100402static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100404static int makeopens(FILE *fd, char_u *dirnow);
405static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
406static void ex_loadview(exarg_T *eap);
407static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000408static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409#else
410# define ex_loadview ex_ni
411#endif
412#ifndef FEAT_EVAL
413# define ex_compiler ex_ni
414#endif
415#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100416static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417#else
418# define ex_viminfo ex_ni
419#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100420static void ex_behave(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100422static void ex_filetype(exarg_T *eap);
423static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424#else
425# define ex_filetype ex_ni
426# define ex_setfiletype ex_ni
427#endif
428#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000429# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430# define ex_diffpatch ex_ni
431# define ex_diffgetput ex_ni
432# define ex_diffsplit ex_ni
433# define ex_diffthis ex_ni
434# define ex_diffupdate ex_ni
435#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100436static void ex_digraphs(exarg_T *eap);
437static void ex_set(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
439# define ex_options ex_ni
440#endif
441#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100442static void ex_nohlsearch(exarg_T *eap);
443static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444#else
445# define ex_nohlsearch ex_ni
446# define ex_match ex_ni
447#endif
448#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100449static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450#else
451# define ex_X ex_ni
452#endif
453#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100454static void ex_fold(exarg_T *eap);
455static void ex_foldopen(exarg_T *eap);
456static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457#else
458# define ex_fold ex_ni
459# define ex_foldopen ex_ni
460# define ex_folddo ex_ni
461#endif
462#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
463 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
464# define ex_language ex_ni
465#endif
466#ifndef FEAT_SIGNS
467# define ex_sign ex_ni
468#endif
469#ifndef FEAT_SUN_WORKSHOP
470# define ex_wsverb ex_ni
471#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000472#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200473# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000474# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200475# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477
478#ifndef FEAT_EVAL
479# define ex_debug ex_ni
480# define ex_breakadd ex_ni
481# define ex_debuggreedy ex_ni
482# define ex_breakdel ex_ni
483# define ex_breaklist ex_ni
484#endif
485
486#ifndef FEAT_CMDHIST
487# define ex_history ex_ni
488#endif
489#ifndef FEAT_JUMPLIST
490# define ex_jumps ex_ni
491# define ex_changes ex_ni
492#endif
493
Bram Moolenaar05159a02005-02-26 23:04:13 +0000494#ifndef FEAT_PROFILE
495# define ex_profile ex_ni
496#endif
497
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498/*
499 * Declare cmdnames[].
500 */
501#define DO_DECLARE_EXCMD
502#include "ex_cmds.h"
503
504/*
505 * Table used to quickly search for a command, based on its first character.
506 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000507static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508{
509 CMD_append,
510 CMD_buffer,
511 CMD_change,
512 CMD_delete,
513 CMD_edit,
514 CMD_file,
515 CMD_global,
516 CMD_help,
517 CMD_insert,
518 CMD_join,
519 CMD_k,
520 CMD_list,
521 CMD_move,
522 CMD_next,
523 CMD_open,
524 CMD_print,
525 CMD_quit,
526 CMD_read,
527 CMD_substitute,
528 CMD_t,
529 CMD_undo,
530 CMD_vglobal,
531 CMD_write,
532 CMD_xit,
533 CMD_yank,
534 CMD_z,
535 CMD_bang
536};
537
538static char_u dollar_command[2] = {'$', 0};
539
540
541#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000542/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543typedef struct
544{
545 char_u *line; /* command line */
546 linenr_T lnum; /* sourcing_lnum of the line */
547} wcmd_T;
548
549/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000550 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000552 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000554struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555{
556 garray_T *lines_gap; /* growarray with line info */
557 int current_line; /* last read line from growarray */
558 int repeating; /* TRUE when looping a second time */
559 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100560 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 void *cookie;
562};
563
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100564static char_u *get_loop_line(int c, void *cookie, int indent);
565static int store_loop_line(garray_T *gap, char_u *line);
566static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000567
568/* Struct to save a few things while debugging. Used in do_cmdline() only. */
569struct dbg_stuff
570{
571 int trylevel;
572 int force_abort;
573 except_T *caught_stack;
574 char_u *vv_exception;
575 char_u *vv_throwpoint;
576 int did_emsg;
577 int got_int;
578 int did_throw;
579 int need_rethrow;
580 int check_cstack;
581 except_T *current_exception;
582};
583
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100584static void save_dbg_stuff(struct dbg_stuff *dsp);
585static void restore_dbg_stuff(struct dbg_stuff *dsp);
Bram Moolenaared203462004-06-16 11:19:22 +0000586
587 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100588save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000589{
590 dsp->trylevel = trylevel; trylevel = 0;
591 dsp->force_abort = force_abort; force_abort = FALSE;
592 dsp->caught_stack = caught_stack; caught_stack = NULL;
593 dsp->vv_exception = v_exception(NULL);
594 dsp->vv_throwpoint = v_throwpoint(NULL);
595
596 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
597 dsp->did_emsg = did_emsg; did_emsg = FALSE;
598 dsp->got_int = got_int; got_int = FALSE;
599 dsp->did_throw = did_throw; did_throw = FALSE;
600 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
601 dsp->check_cstack = check_cstack; check_cstack = FALSE;
602 dsp->current_exception = current_exception; current_exception = NULL;
603}
604
605 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100606restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000607{
608 suppress_errthrow = FALSE;
609 trylevel = dsp->trylevel;
610 force_abort = dsp->force_abort;
611 caught_stack = dsp->caught_stack;
612 (void)v_exception(dsp->vv_exception);
613 (void)v_throwpoint(dsp->vv_throwpoint);
614 did_emsg = dsp->did_emsg;
615 got_int = dsp->got_int;
616 did_throw = dsp->did_throw;
617 need_rethrow = dsp->need_rethrow;
618 check_cstack = dsp->check_cstack;
619 current_exception = dsp->current_exception;
620}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621#endif
622
623
624/*
625 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
626 * command is given.
627 */
628 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100629do_exmode(
630 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631{
632 int save_msg_scroll;
633 int prev_msg_row;
634 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000635 int changedtick;
636
637 if (improved)
638 exmode_active = EXMODE_VIM;
639 else
640 exmode_active = EXMODE_NORMAL;
641 State = NORMAL;
642
643 /* When using ":global /pat/ visual" and then "Q" we return to continue
644 * the :global command. */
645 if (global_busy)
646 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647
648 save_msg_scroll = msg_scroll;
649 ++RedrawingDisabled; /* don't redisplay the window */
650 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651#ifdef FEAT_GUI
652 /* Ignore scrollbar and mouse events in Ex mode */
653 ++hold_gui_events;
654#endif
655#ifdef FEAT_SNIFF
656 want_sniff_request = 0; /* No K_SNIFF wanted */
657#endif
658
659 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
660 while (exmode_active)
661 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000662#ifdef FEAT_EX_EXTRA
663 /* Check for a ":normal" command and no more characters left. */
664 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
665 {
666 exmode_active = FALSE;
667 break;
668 }
669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 msg_scroll = TRUE;
671 need_wait_return = FALSE;
672 ex_pressedreturn = FALSE;
673 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000674 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 prev_msg_row = msg_row;
676 prev_line = curwin->w_cursor.lnum;
677#ifdef FEAT_SNIFF
678 ProcessSniffRequests();
679#endif
680 if (improved)
681 {
682 cmdline_row = msg_row;
683 do_cmdline(NULL, getexline, NULL, 0);
684 }
685 else
686 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
687 lines_left = Rows - 1;
688
Bram Moolenaardf177f62005-02-22 08:39:57 +0000689 if ((prev_line != curwin->w_cursor.lnum
690 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000692 if (curbuf->b_ml.ml_flags & ML_EMPTY)
693 EMSG(_(e_emptybuf));
694 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000696 if (ex_pressedreturn)
697 {
698 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000699 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000700 msg_row = prev_msg_row;
701 if (prev_msg_row == Rows - 1)
702 msg_row--;
703 }
704 msg_col = 0;
705 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
706 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000709 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
710 {
711 if (curbuf->b_ml.ml_flags & ML_EMPTY)
712 EMSG(_(e_emptybuf));
713 else
714 EMSG(_("E501: At end-of-file"));
715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 }
717
718#ifdef FEAT_GUI
719 --hold_gui_events;
720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 --RedrawingDisabled;
722 --no_wait_return;
723 update_screen(CLEAR);
724 need_wait_return = FALSE;
725 msg_scroll = save_msg_scroll;
726}
727
728/*
729 * Execute a simple command line. Used for translated commands like "*".
730 */
731 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100732do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733{
734 return do_cmdline(cmd, NULL, NULL,
735 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
736}
737
738/*
739 * do_cmdline(): execute one Ex command line
740 *
741 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100742 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 * 2. Split up in parts separated with '|'.
744 *
745 * This function can be called recursively!
746 *
747 * flags:
748 * DOCMD_VERBOSE - The command will be included in the error message.
749 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100750 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 * DOCMD_KEYTYPED - Don't reset KeyTyped.
752 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
753 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
754 *
755 * return FAIL if cmdline could not be executed, OK otherwise
756 */
757 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100758do_cmdline(
759 char_u *cmdline,
760 char_u *(*fgetline)(int, void *, int),
761 void *cookie, /* argument for fgetline() */
762 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763{
764 char_u *next_cmdline; /* next cmd to execute */
765 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100766 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 static int recursive = 0; /* recursive depth */
768 int msg_didout_before_start = 0;
769 int count = 0; /* line number count */
770 int did_inc = FALSE; /* incremented RedrawingDisabled */
771 int retval = OK;
772#ifdef FEAT_EVAL
773 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000774 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 int current_line = 0; /* active line in lines_ga */
776 char_u *fname = NULL; /* function or script name */
777 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
778 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000779 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 int initial_trylevel;
781 struct msglist **saved_msg_list = NULL;
782 struct msglist *private_msg_list;
783
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100784 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100785 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000787 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000789 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100791# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792# define cmd_cookie cookie
793#endif
794 static int call_depth = 0; /* recursiveness */
795
796#ifdef FEAT_EVAL
797 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
798 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000799 * This ensures that the do_errthrow() call in do_one_cmd() does not
800 * combine the messages stored by an earlier invocation of do_one_cmd()
801 * with the command name of the later one. This would happen when
802 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 saved_msg_list = msg_list;
804 msg_list = &private_msg_list;
805 private_msg_list = NULL;
806#endif
807
808 /* It's possible to create an endless loop with ":execute", catch that
809 * here. The value of 200 allows nested function calls, ":source", etc. */
810 if (call_depth == 200)
811 {
812 EMSG(_("E169: Command too recursive"));
813#ifdef FEAT_EVAL
814 /* When converting to an exception, we do not include the command name
815 * since this is not an error of the specific command. */
816 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
817 msg_list = saved_msg_list;
818#endif
819 return FAIL;
820 }
821 ++call_depth;
822
823#ifdef FEAT_EVAL
824 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000825 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 cstack.cs_trylevel = 0;
827 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000828 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
830
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100831 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832
833 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100834 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000835 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 ++ex_nesting_level;
837
838 /* Get the function or script name and the address where the next breakpoint
839 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000840 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 {
842 fname = func_name(real_cookie);
843 breakpoint = func_breakpoint(real_cookie);
844 dbg_tick = func_dbg_tick(real_cookie);
845 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100846 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 {
848 fname = sourcing_name;
849 breakpoint = source_breakpoint(real_cookie);
850 dbg_tick = source_dbg_tick(real_cookie);
851 }
852
853 /*
854 * Initialize "force_abort" and "suppress_errthrow" at the top level.
855 */
856 if (!recursive)
857 {
858 force_abort = FALSE;
859 suppress_errthrow = FALSE;
860 }
861
862 /*
863 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000864 * exception handling (used when debugging). Otherwise clear it to avoid
865 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000867 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000868 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000869 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200870 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871
872 initial_trylevel = trylevel;
873
874 /*
875 * "did_throw" will be set to TRUE when an exception is being thrown.
876 */
877 did_throw = FALSE;
878#endif
879 /*
880 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000881 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 * If force_abort is set, we cancel everything.
883 */
884 did_emsg = FALSE;
885
886 /*
887 * KeyTyped is only set when calling vgetc(). Reset it here when not
888 * calling vgetc() (sourced command lines).
889 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100890 if (!(flags & DOCMD_KEYTYPED)
891 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 KeyTyped = FALSE;
893
894 /*
895 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000896 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 * - for multiple commands on one line, separated with '|'
898 * - when repeating until there are no more lines (for ":source")
899 */
900 next_cmdline = cmdline;
901 do
902 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000903#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100904 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000905#endif
906
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000907 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 if (next_cmdline == NULL
909#ifdef FEAT_EVAL
910 && !force_abort
911 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000912 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913#endif
914 )
915 did_emsg = FALSE;
916
917 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000918 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100919 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 * 3. If a line is given: Make a copy, so we can mess with it.
921 */
922
923#ifdef FEAT_EVAL
924 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000925 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 {
927 /* Each '|' separated command is stored separately in lines_ga, to
928 * be able to jump to it. Don't use next_cmdline now. */
929 vim_free(cmdline_copy);
930 cmdline_copy = NULL;
931
932 /* Check if a function has returned or, unless it has an unclosed
933 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000934 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000936# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000937 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000938 func_line_end(real_cookie);
939# endif
940 if (func_has_ended(real_cookie))
941 {
942 retval = FAIL;
943 break;
944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000946#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000947 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100948 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000949 script_line_end();
950#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951
952 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100953 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 {
955 retval = FAIL;
956 break;
957 }
958
959 /* If breakpoints have been added/deleted need to check for it. */
960 if (breakpoint != NULL && dbg_tick != NULL
961 && *dbg_tick != debug_tick)
962 {
963 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100964 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 fname, sourcing_lnum);
966 *dbg_tick = debug_tick;
967 }
968
969 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
970 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
971
972 /* Did we encounter a breakpoint? */
973 if (breakpoint != NULL && *breakpoint != 0
974 && *breakpoint <= sourcing_lnum)
975 {
976 dbg_breakpoint(fname, sourcing_lnum);
977 /* Find next breakpoint. */
978 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100979 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 fname, sourcing_lnum);
981 *dbg_tick = debug_tick;
982 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000983# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000984 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000985 {
986 if (getline_is_func)
987 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100988 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000989 script_line_start();
990 }
991# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 }
993
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000994 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000996 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100997 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 * below, so that it stores lines in or reads them from
999 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001000 * while/for loop. */
1001 cmd_getline = get_loop_line;
1002 cmd_cookie = (void *)&cmd_loop_cookie;
1003 cmd_loop_cookie.lines_gap = &lines_ga;
1004 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001005 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001006 cmd_loop_cookie.cookie = cookie;
1007 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 }
1009 else
1010 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001011 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 cmd_cookie = cookie;
1013 }
1014#endif
1015
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001016 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 if (next_cmdline == NULL)
1018 {
1019 /*
1020 * Need to set msg_didout for the first line after an ":if",
1021 * otherwise the ":if" will be overwritten.
1022 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001023 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001025 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026#ifdef FEAT_EVAL
1027 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1028#else
1029 0
1030#endif
1031 )) == NULL)
1032 {
1033 /* Don't call wait_return for aborted command line. The NULL
1034 * returned for the end of a sourced file or executed function
1035 * doesn't do this. */
1036 if (KeyTyped && !(flags & DOCMD_REPEAT))
1037 need_wait_return = FALSE;
1038 retval = FAIL;
1039 break;
1040 }
1041 used_getline = TRUE;
1042
1043 /*
1044 * Keep the first typed line. Clear it when more lines are typed.
1045 */
1046 if (flags & DOCMD_KEEPLINE)
1047 {
1048 vim_free(repeat_cmdline);
1049 if (count == 0)
1050 repeat_cmdline = vim_strsave(next_cmdline);
1051 else
1052 repeat_cmdline = NULL;
1053 }
1054 }
1055
1056 /* 3. Make a copy of the command so we can mess with it. */
1057 else if (cmdline_copy == NULL)
1058 {
1059 next_cmdline = vim_strsave(next_cmdline);
1060 if (next_cmdline == NULL)
1061 {
1062 EMSG(_(e_outofmem));
1063 retval = FAIL;
1064 break;
1065 }
1066 }
1067 cmdline_copy = next_cmdline;
1068
1069#ifdef FEAT_EVAL
1070 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001071 * Save the current line when inside a ":while" or ":for", and when
1072 * the command looks like a ":while" or ":for", because we may need it
1073 * later. When there is a '|' and another command, it is stored
1074 * separately, because we need to be able to jump back to it from an
1075 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001077 if (current_line == lines_ga.ga_len
1078 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001080 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 {
1082 retval = FAIL;
1083 break;
1084 }
1085 }
1086 did_endif = FALSE;
1087#endif
1088
1089 if (count++ == 0)
1090 {
1091 /*
1092 * All output from the commands is put below each other, without
1093 * waiting for a return. Don't do this when executing commands
1094 * from a script or when being called recursive (e.g. for ":e
1095 * +command file").
1096 */
1097 if (!(flags & DOCMD_NOWAIT) && !recursive)
1098 {
1099 msg_didout_before_start = msg_didout;
1100 msg_didany = FALSE; /* no output yet */
1101 msg_start();
1102 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001103 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 ++RedrawingDisabled;
1105 did_inc = TRUE;
1106 }
1107 }
1108
1109 if (p_verbose >= 15 && sourcing_name != NULL)
1110 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001112 verbose_enter_scroll();
1113
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 smsg((char_u *)_("line %ld: %s"),
1115 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001116 if (msg_silent == 0)
1117 msg_puts((char_u *)"\n"); /* don't overwrite this */
1118
1119 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 --no_wait_return;
1121 }
1122
1123 /*
1124 * 2. Execute one '|' separated command.
1125 * do_one_cmd() will return NULL if there is no trailing '|'.
1126 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1127 */
1128 ++recursive;
1129 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1130#ifdef FEAT_EVAL
1131 &cstack,
1132#endif
1133 cmd_getline, cmd_cookie);
1134 --recursive;
1135
1136#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001137 if (cmd_cookie == (void *)&cmd_loop_cookie)
1138 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001140 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141#endif
1142
1143 if (next_cmdline == NULL)
1144 {
1145 vim_free(cmdline_copy);
1146 cmdline_copy = NULL;
1147#ifdef FEAT_CMDHIST
1148 /*
1149 * If the command was typed, remember it for the ':' register.
1150 * Do this AFTER executing the command to make :@: work.
1151 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001152 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 && new_last_cmdline != NULL)
1154 {
1155 vim_free(last_cmdline);
1156 last_cmdline = new_last_cmdline;
1157 new_last_cmdline = NULL;
1158 }
1159#endif
1160 }
1161 else
1162 {
1163 /* need to copy the command after the '|' to cmdline_copy, for the
1164 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001165 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 next_cmdline = cmdline_copy;
1167 }
1168
1169
1170#ifdef FEAT_EVAL
1171 /* reset did_emsg for a function that is not aborted by an error */
1172 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001173 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 && !func_has_abort(real_cookie))
1175 did_emsg = FALSE;
1176
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001177 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 {
1179 ++current_line;
1180
1181 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001182 * An ":endwhile", ":endfor" and ":continue" is handled here.
1183 * If we were executing commands, jump back to the ":while" or
1184 * ":for".
1185 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001187 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001189 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001191 /* Jump back to the matching ":while" or ":for". Be careful
1192 * not to use a cs_line[] from an entry that isn't a ":while"
1193 * or ":for": It would make "current_line" invalid and can
1194 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 if (!did_emsg && !got_int && !did_throw
1196 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001197 && (cstack.cs_flags[cstack.cs_idx]
1198 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 && cstack.cs_line[cstack.cs_idx] >= 0
1200 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1201 {
1202 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001203 /* remember we jumped there */
1204 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 line_breakcheck(); /* check if CTRL-C typed */
1206
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001207 /* Check for the next breakpoint at or after the ":while"
1208 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 if (breakpoint != NULL)
1210 {
1211 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001212 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 fname,
1214 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1215 *dbg_tick = debug_tick;
1216 }
1217 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001218 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001220 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001222 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1223 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 }
1225 }
1226
1227 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001228 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001230 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001232 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1234 }
1235 }
1236
1237 /*
1238 * When not inside any ":while" loop, clear remembered lines.
1239 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001240 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 {
1242 if (lines_ga.ga_len > 0)
1243 {
1244 sourcing_lnum =
1245 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1246 free_cmdlines(&lines_ga);
1247 }
1248 current_line = 0;
1249 }
1250
1251 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001252 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1253 * being restored at the ":endtry". Reset them here and set the
1254 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1255 * This includes the case where a missing ":endif", ":endwhile" or
1256 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001258 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001260 cstack.cs_lflags &= ~CSL_HAD_FINA;
1261 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1262 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 did_throw ? (void *)current_exception : NULL);
1264 did_emsg = got_int = did_throw = FALSE;
1265 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1266 }
1267
1268 /* Update global "trylevel" for recursive calls to do_cmdline() from
1269 * within this loop. */
1270 trylevel = initial_trylevel + cstack.cs_trylevel;
1271
1272 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001273 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 * files) is aborted because of an error, an interrupt, or an uncaught
1275 * exception, cancel everything. If it is left normally, reset
1276 * force_abort to get the non-EH compatible abortion behavior for
1277 * the rest of the script.
1278 */
1279 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1280 force_abort = FALSE;
1281
1282 /* Convert an interrupt to an exception if appropriate. */
1283 (void)do_intthrow(&cstack);
1284#endif /* FEAT_EVAL */
1285
1286 }
1287 /*
1288 * Continue executing command lines when:
1289 * - no CTRL-C typed, no aborting error, no exception thrown or try
1290 * conditionals need to be checked for executing finally clauses or
1291 * catching an interrupt exception
1292 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001293 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 * looping for ":source" command or function call.
1295 */
1296 while (!((got_int
1297#ifdef FEAT_EVAL
1298 || (did_emsg && force_abort) || did_throw
1299#endif
1300 )
1301#ifdef FEAT_EVAL
1302 && cstack.cs_trylevel == 0
1303#endif
1304 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001305 && !(did_emsg
1306#ifdef FEAT_EVAL
1307 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001308 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001309 * the :endtry to be missed. */
1310 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1311#endif
1312 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001313 && (getline_equal(fgetline, cookie, getexmodeline)
1314 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 && (next_cmdline != NULL
1316#ifdef FEAT_EVAL
1317 || cstack.cs_idx >= 0
1318#endif
1319 || (flags & DOCMD_REPEAT)));
1320
1321 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001322 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323#ifdef FEAT_EVAL
1324 free_cmdlines(&lines_ga);
1325 ga_clear(&lines_ga);
1326
1327 if (cstack.cs_idx >= 0)
1328 {
1329 /*
1330 * If a sourced file or executed function ran to its end, report the
1331 * unclosed conditional.
1332 */
1333 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001334 && ((getline_equal(fgetline, cookie, getsourceline)
1335 && !source_finished(fgetline, cookie))
1336 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 && !func_has_ended(real_cookie))))
1338 {
1339 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1340 EMSG(_(e_endtry));
1341 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1342 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001343 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1344 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 else
1346 EMSG(_(e_endif));
1347 }
1348
1349 /*
1350 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1351 * ":endtry" in a sourced file or executed function. If the try
1352 * conditional is in its finally clause, ignore anything pending.
1353 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001354 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 */
1356 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001357 {
1358 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1359
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001360 if (idx >= 0)
1361 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001362 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1363 &cstack.cs_looplevel);
1364 }
1365 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 trylevel = initial_trylevel;
1367 }
1368
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001369 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1370 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001372 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 ? (char_u *)"endfunction" : (char_u *)NULL);
1374
1375 if (trylevel == 0)
1376 {
1377 /*
1378 * When an exception is being thrown out of the outermost try
1379 * conditional, discard the uncaught exception, disable the conversion
1380 * of interrupts or errors to exceptions, and ensure that no more
1381 * commands are executed.
1382 */
1383 if (did_throw)
1384 {
1385 void *p = NULL;
1386 char_u *saved_sourcing_name;
1387 int saved_sourcing_lnum;
1388 struct msglist *messages = NULL, *next;
1389
1390 /*
1391 * If the uncaught exception is a user exception, report it as an
1392 * error. If it is an error exception, display the saved error
1393 * message now. For an interrupt exception, do nothing; the
1394 * interrupt message is given elsewhere.
1395 */
1396 switch (current_exception->type)
1397 {
1398 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001399 vim_snprintf((char *)IObuff, IOSIZE,
1400 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 current_exception->value);
1402 p = vim_strsave(IObuff);
1403 break;
1404 case ET_ERROR:
1405 messages = current_exception->messages;
1406 current_exception->messages = NULL;
1407 break;
1408 case ET_INTERRUPT:
1409 break;
1410 default:
1411 p = vim_strsave((char_u *)_(e_internal));
1412 }
1413
1414 saved_sourcing_name = sourcing_name;
1415 saved_sourcing_lnum = sourcing_lnum;
1416 sourcing_name = current_exception->throw_name;
1417 sourcing_lnum = current_exception->throw_lnum;
1418 current_exception->throw_name = NULL;
1419
1420 discard_current_exception(); /* uses IObuff if 'verbose' */
1421 suppress_errthrow = TRUE;
1422 force_abort = TRUE;
1423
1424 if (messages != NULL)
1425 {
1426 do
1427 {
1428 next = messages->next;
1429 emsg(messages->msg);
1430 vim_free(messages->msg);
1431 vim_free(messages);
1432 messages = next;
1433 }
1434 while (messages != NULL);
1435 }
1436 else if (p != NULL)
1437 {
1438 emsg(p);
1439 vim_free(p);
1440 }
1441 vim_free(sourcing_name);
1442 sourcing_name = saved_sourcing_name;
1443 sourcing_lnum = saved_sourcing_lnum;
1444 }
1445
1446 /*
1447 * On an interrupt or an aborting error not converted to an exception,
1448 * disable the conversion of errors to exceptions. (Interrupts are not
1449 * converted any more, here.) This enables also the interrupt message
1450 * when force_abort is set and did_emsg unset in case of an interrupt
1451 * from a finally clause after an error.
1452 */
1453 else if (got_int || (did_emsg && force_abort))
1454 suppress_errthrow = TRUE;
1455 }
1456
1457 /*
1458 * The current cstack will be freed when do_cmdline() returns. An uncaught
1459 * exception will have to be rethrown in the previous cstack. If a function
1460 * has just returned or a script file was just finished and the previous
1461 * cstack belongs to the same function or, respectively, script file, it
1462 * will have to be checked for finally clauses to be executed due to the
1463 * ":return" or ":finish". This is done in do_one_cmd().
1464 */
1465 if (did_throw)
1466 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001467 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001469 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 && ex_nesting_level > func_level(real_cookie) + 1))
1471 {
1472 if (!did_throw)
1473 check_cstack = TRUE;
1474 }
1475 else
1476 {
1477 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001478 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 --ex_nesting_level;
1480 /*
1481 * Go to debug mode when returning from a function in which we are
1482 * single-stepping.
1483 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001484 if ((getline_equal(fgetline, cookie, getsourceline)
1485 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001487 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 ? (char_u *)_("End of sourced file")
1489 : (char_u *)_("End of function"));
1490 }
1491
1492 /*
1493 * Restore the exception environment (done after returning from the
1494 * debugger).
1495 */
1496 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001497 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498
1499 msg_list = saved_msg_list;
1500#endif /* FEAT_EVAL */
1501
1502 /*
1503 * If there was too much output to fit on the command line, ask the user to
1504 * hit return before redrawing the screen. With the ":global" command we do
1505 * this only once after the command is finished.
1506 */
1507 if (did_inc)
1508 {
1509 --RedrawingDisabled;
1510 --no_wait_return;
1511 msg_scroll = FALSE;
1512
1513 /*
1514 * When just finished an ":if"-":else" which was typed, no need to
1515 * wait for hit-return. Also for an error situation.
1516 */
1517 if (retval == FAIL
1518#ifdef FEAT_EVAL
1519 || (did_endif && KeyTyped && !did_emsg)
1520#endif
1521 )
1522 {
1523 need_wait_return = FALSE;
1524 msg_didany = FALSE; /* don't wait when restarting edit */
1525 }
1526 else if (need_wait_return)
1527 {
1528 /*
1529 * The msg_start() above clears msg_didout. The wait_return we do
1530 * here should not overwrite the command that may be shown before
1531 * doing that.
1532 */
1533 msg_didout |= msg_didout_before_start;
1534 wait_return(FALSE);
1535 }
1536 }
1537
Bram Moolenaar2a942252012-11-28 23:03:07 +01001538#ifdef FEAT_EVAL
1539 did_endif = FALSE; /* in case do_cmdline used recursively */
1540#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 /*
1542 * Reset if_level, in case a sourced script file contains more ":if" than
1543 * ":endif" (could be ":if x | foo | endif").
1544 */
1545 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001546#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001547
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 --call_depth;
1549 return retval;
1550}
1551
1552#ifdef FEAT_EVAL
1553/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001554 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 */
1556 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001557get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001559 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 wcmd_T *wp;
1561 char_u *line;
1562
1563 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1564 {
1565 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001566 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001568 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 if (cp->getline == NULL)
1570 line = getcmdline(c, 0L, indent);
1571 else
1572 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001573 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 ++cp->current_line;
1575
1576 return line;
1577 }
1578
1579 KeyTyped = FALSE;
1580 ++cp->current_line;
1581 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1582 sourcing_lnum = wp->lnum;
1583 return vim_strsave(wp->line);
1584}
1585
1586/*
1587 * Store a line in "gap" so that a ":while" loop can execute it again.
1588 */
1589 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001590store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591{
1592 if (ga_grow(gap, 1) == FAIL)
1593 return FAIL;
1594 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1595 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1596 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 return OK;
1598}
1599
1600/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001601 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 */
1603 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001604free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605{
1606 while (gap->ga_len > 0)
1607 {
1608 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1609 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 }
1611}
1612#endif
1613
1614/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001615 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1616 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001619getline_equal(
1620 char_u *(*fgetline)(int, void *, int),
1621 void *cookie UNUSED, /* argument for fgetline() */
1622 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623{
1624#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001625 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001626 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627
Bram Moolenaar89d40322006-08-29 15:30:07 +00001628 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001629 * function that's originally used to obtain the lines. This may be
1630 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001631 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001632 cp = (struct loop_cookie *)cookie;
1633 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 {
1635 gp = cp->getline;
1636 cp = cp->cookie;
1637 }
1638 return gp == func;
1639#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001640 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641#endif
1642}
1643
1644#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1645/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001646 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 * getline function. Otherwise return "cookie".
1648 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001650getline_cookie(
1651 char_u *(*fgetline)(int, void *, int) UNUSED,
1652 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653{
1654# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001655 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001656 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657
Bram Moolenaar89d40322006-08-29 15:30:07 +00001658 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001659 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001661 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001662 cp = (struct loop_cookie *)cookie;
1663 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 {
1665 gp = cp->getline;
1666 cp = cp->cookie;
1667 }
1668 return cp;
1669# else
1670 return cookie;
1671# endif
1672}
1673#endif
1674
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001675
1676/*
1677 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1678 * ":bwipeout", etc.
1679 * Returns the buffer number.
1680 */
1681 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001682compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001683{
1684 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001685 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001686 int count = offset;
1687
1688 buf = firstbuf;
1689 while (buf->b_next != NULL && buf->b_fnum < lnum)
1690 buf = buf->b_next;
1691 while (count != 0)
1692 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001693 count += (offset < 0) ? 1 : -1;
1694 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1695 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001696 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001697 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001698 if (addr_type == ADDR_LOADED_BUFFERS)
1699 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001700 while (buf->b_ml.ml_mfp == NULL)
1701 {
1702 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1703 if (nextbuf == NULL)
1704 break;
1705 buf = nextbuf;
1706 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001707 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001708 /* we might have gone too far, last buffer is not loadedd */
1709 if (addr_type == ADDR_LOADED_BUFFERS)
1710 while (buf->b_ml.ml_mfp == NULL)
1711 {
1712 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1713 if (nextbuf == NULL)
1714 break;
1715 buf = nextbuf;
1716 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001717 return buf->b_fnum;
1718}
1719
Bram Moolenaarf240e182014-11-27 18:33:02 +01001720#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001721static int current_win_nr(win_T *win);
1722static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001723
1724 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001725current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001726{
1727 win_T *wp;
1728 int nr = 0;
1729
1730 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1731 {
1732 ++nr;
1733 if (wp == win)
1734 break;
1735 }
1736 return nr;
1737}
1738
1739 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001740current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001741{
1742 tabpage_T *tp;
1743 int nr = 0;
1744
1745 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
1746 {
1747 ++nr;
1748 if (tp == tab)
1749 break;
1750 }
1751 return nr;
1752}
1753
1754# define CURRENT_WIN_NR current_win_nr(curwin)
1755# define LAST_WIN_NR current_win_nr(NULL)
1756# define CURRENT_TAB_NR current_tab_nr(curtab)
1757# define LAST_TAB_NR current_tab_nr(NULL)
1758#else
1759# define CURRENT_WIN_NR 1
1760# define LAST_WIN_NR 1
1761# define CURRENT_TAB_NR 1
1762# define LAST_TAB_NR 1
1763#endif
1764
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001765
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766/*
1767 * Execute one Ex command.
1768 *
1769 * If 'sourcing' is TRUE, the command will be included in the error message.
1770 *
1771 * 1. skip comment lines and leading space
1772 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001773 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001774 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001775 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001776 * 6. parse arguments
1777 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001779 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 *
1781 * This function may be called recursively!
1782 */
1783#if (_MSC_VER == 1200)
1784/*
Bram Moolenaared203462004-06-16 11:19:22 +00001785 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001787 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788#endif
1789 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001790do_one_cmd(
1791 char_u **cmdlinep,
1792 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001794 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001796 char_u *(*fgetline)(int, void *, int),
1797 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798{
1799 char_u *p;
1800 linenr_T lnum;
1801 long n;
1802 char_u *errormsg = NULL; /* error message */
1803 exarg_T ea; /* Ex command arguments */
1804 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001805 int save_msg_scroll = msg_scroll;
1806 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001808#ifdef HAVE_SANDBOX
1809 int did_sandbox = FALSE;
1810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 cmdmod_T save_cmdmod;
1812 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001813 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814
1815 vim_memset(&ea, 0, sizeof(ea));
1816 ea.line1 = 1;
1817 ea.line2 = 1;
1818#ifdef FEAT_EVAL
1819 ++ex_nesting_level;
1820#endif
1821
Bram Moolenaar21691f82012-12-05 19:13:18 +01001822 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 if (quitmore
1824#ifdef FEAT_EVAL
1825 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001826 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001827#endif
1828#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001829 /* avoid that an autocommand, e.g. QuitPre, does this */
1830 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831#endif
1832 )
1833 --quitmore;
1834
1835 /*
1836 * Reset browse, confirm, etc.. They are restored when returning, for
1837 * recursive calls.
1838 */
1839 save_cmdmod = cmdmod;
1840 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1841
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001842 /* "#!anything" is handled like a comment. */
1843 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1844 goto doend;
1845
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 /*
1847 * Repeat until no more command modifiers are found.
1848 */
1849 ea.cmd = *cmdlinep;
1850 for (;;)
1851 {
1852/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001853 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 */
1855 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1856 ++ea.cmd;
1857
1858 /* in ex mode, an empty line works like :+ */
1859 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001860 && (getline_equal(fgetline, cookie, getexmodeline)
1861 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001862 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 {
1864 ea.cmd = (char_u *)"+";
1865 ex_pressedreturn = TRUE;
1866 }
1867
1868 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001869 if (*ea.cmd == '"')
1870 goto doend;
1871 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001872 {
1873 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876
1877/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001878 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 */
1880 p = ea.cmd;
1881 if (VIM_ISDIGIT(*ea.cmd))
1882 p = skipwhite(skipdigits(ea.cmd));
1883 switch (*p)
1884 {
1885 /* When adding an entry, also modify cmd_exists(). */
1886 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1887 break;
1888#ifdef FEAT_WINDOWS
1889 cmdmod.split |= WSP_ABOVE;
1890#endif
1891 continue;
1892
1893 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1894 {
1895#ifdef FEAT_WINDOWS
1896 cmdmod.split |= WSP_BELOW;
1897#endif
1898 continue;
1899 }
1900 if (checkforcmd(&ea.cmd, "browse", 3))
1901 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001902#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 cmdmod.browse = TRUE;
1904#endif
1905 continue;
1906 }
1907 if (!checkforcmd(&ea.cmd, "botright", 2))
1908 break;
1909#ifdef FEAT_WINDOWS
1910 cmdmod.split |= WSP_BOT;
1911#endif
1912 continue;
1913
1914 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1915 break;
1916#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1917 cmdmod.confirm = TRUE;
1918#endif
1919 continue;
1920
1921 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1922 {
1923 cmdmod.keepmarks = TRUE;
1924 continue;
1925 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001926 if (checkforcmd(&ea.cmd, "keepalt", 5))
1927 {
1928 cmdmod.keepalt = TRUE;
1929 continue;
1930 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001931 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1932 {
1933 cmdmod.keeppatterns = TRUE;
1934 continue;
1935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1937 break;
1938 cmdmod.keepjumps = TRUE;
1939 continue;
1940
1941 /* ":hide" and ":hide | cmd" are not modifiers */
1942 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1943 || *p == NUL || ends_excmd(*p))
1944 break;
1945 ea.cmd = p;
1946 cmdmod.hide = TRUE;
1947 continue;
1948
1949 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1950 {
1951 cmdmod.lockmarks = TRUE;
1952 continue;
1953 }
1954
1955 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1956 break;
1957#ifdef FEAT_WINDOWS
1958 cmdmod.split |= WSP_ABOVE;
1959#endif
1960 continue;
1961
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001962 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001963 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001964#ifdef FEAT_AUTOCMD
1965 if (cmdmod.save_ei == NULL)
1966 {
1967 /* Set 'eventignore' to "all". Restore the
1968 * existing option value later. */
1969 cmdmod.save_ei = vim_strsave(p_ei);
1970 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001971 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001972 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001973#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001974 continue;
1975 }
1976 if (!checkforcmd(&ea.cmd, "noswapfile", 6))
1977 break;
1978 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001979 continue;
1980
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1982 break;
1983#ifdef FEAT_WINDOWS
1984 cmdmod.split |= WSP_BELOW;
1985#endif
1986 continue;
1987
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001988 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1989 {
1990#ifdef HAVE_SANDBOX
1991 if (!did_sandbox)
1992 ++sandbox;
1993 did_sandbox = TRUE;
1994#endif
1995 continue;
1996 }
1997 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001999 if (save_msg_silent == -1)
2000 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
2003 {
2004 /* ":silent!", but not "silent !cmd" */
2005 ea.cmd = skipwhite(ea.cmd + 1);
2006 ++emsg_silent;
2007 ++did_esilent;
2008 }
2009 continue;
2010
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002011 case 't': if (checkforcmd(&p, "tab", 3))
2012 {
2013#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002014 if (vim_isdigit(*ea.cmd))
2015 cmdmod.tab = atoi((char *)ea.cmd) + 1;
2016 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002017 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002018 ea.cmd = p;
2019#endif
2020 continue;
2021 }
2022 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 break;
2024#ifdef FEAT_WINDOWS
2025 cmdmod.split |= WSP_TOP;
2026#endif
2027 continue;
2028
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002029 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
2030 break;
2031 if (save_msg_silent == -1)
2032 save_msg_silent = msg_silent;
2033 msg_silent = 0;
2034 continue;
2035
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
2037 {
2038#ifdef FEAT_VERTSPLIT
2039 cmdmod.split |= WSP_VERT;
2040#endif
2041 continue;
2042 }
2043 if (!checkforcmd(&p, "verbose", 4))
2044 break;
2045 if (verbose_save < 0)
2046 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00002047 if (vim_isdigit(*ea.cmd))
2048 p_verbose = atoi((char *)ea.cmd);
2049 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 p_verbose = 1;
2051 ea.cmd = p;
2052 continue;
2053 }
2054 break;
2055 }
2056
2057#ifdef FEAT_EVAL
2058 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2059 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2060#else
2061 ea.skip = (if_level > 0);
2062#endif
2063
2064#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002065# ifdef FEAT_PROFILE
2066 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002067 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002068 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002069 if (getline_equal(fgetline, cookie, get_func_line))
2070 func_line_exec(getline_cookie(fgetline, cookie));
2071 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002072 script_line_exec();
2073 }
2074#endif
2075
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 /* May go to debug mode. If this happens and the ">quit" debug command is
2077 * used, throw an interrupt exception and skip the next command. */
2078 dbg_check_breakpoint(&ea);
2079 if (!ea.skip && got_int)
2080 {
2081 ea.skip = TRUE;
2082 (void)do_intthrow(cstack);
2083 }
2084#endif
2085
2086/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002087 * 3. Skip over the range to find the command. Let "p" point to after it.
2088 *
2089 * We need the command to know what kind of range it uses.
2090 */
2091 cmd = ea.cmd;
2092 ea.cmd = skip_range(ea.cmd, NULL);
2093 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2094 ea.cmd = skipwhite(ea.cmd + 1);
2095 p = find_command(&ea, NULL);
2096
2097/*
2098 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 *
2100 * where 'addr' is:
2101 *
2102 * % (entire file)
2103 * $ [+-NUM]
2104 * 'x [+-NUM] (where x denotes a currently defined mark)
2105 * . [+-NUM]
2106 * [+-NUM]..
2107 * NUM
2108 *
2109 * The ea.cmd pointer is updated to point to the first character following the
2110 * range spec. If an initial address is found, but no second, the upper bound
2111 * is equal to the lower.
2112 */
2113
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002114 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002115 if (!IS_USER_CMDIDX(ea.cmdidx))
2116 {
2117 if (ea.cmdidx != CMD_SIZE)
2118 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2119 else
2120 ea.addr_type = ADDR_LINES;
2121
2122#ifdef FEAT_WINDOWS
2123 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002124 if (ea.cmdidx == CMD_wincmd && p != NULL)
2125 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002126#endif
2127 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002128
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002130 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 for (;;)
2132 {
2133 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002134 switch (ea.addr_type)
2135 {
2136 case ADDR_LINES:
2137 /* default is current line number */
2138 ea.line2 = curwin->w_cursor.lnum;
2139 break;
2140 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002141 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002142 ea.line2 = lnum;
2143 break;
2144 case ADDR_ARGUMENTS:
2145 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002146 if (ea.line2 > ARGCOUNT)
2147 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002148 break;
2149 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002150 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002151 ea.line2 = curbuf->b_fnum;
2152 break;
2153 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002154 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002155 ea.line2 = lnum;
2156 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002157#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002158 case ADDR_QUICKFIX:
2159 ea.line2 = qf_get_cur_valid_idx(&ea);
2160 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002161#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002164 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip, ea.addr_count == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 if (ea.cmd == NULL) /* error detected */
2166 goto doend;
2167 if (lnum == MAXLNUM)
2168 {
2169 if (*ea.cmd == '%') /* '%' - all lines */
2170 {
2171 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002172 switch (ea.addr_type)
2173 {
2174 case ADDR_LINES:
2175 ea.line1 = 1;
2176 ea.line2 = curbuf->b_ml.ml_line_count;
2177 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002178 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002179 {
2180 buf_T *buf = firstbuf;
2181
2182 while (buf->b_next != NULL
2183 && buf->b_ml.ml_mfp == NULL)
2184 buf = buf->b_next;
2185 ea.line1 = buf->b_fnum;
2186 buf = lastbuf;
2187 while (buf->b_prev != NULL
2188 && buf->b_ml.ml_mfp == NULL)
2189 buf = buf->b_prev;
2190 ea.line2 = buf->b_fnum;
2191 break;
2192 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002193 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002194 ea.line1 = firstbuf->b_fnum;
2195 ea.line2 = lastbuf->b_fnum;
2196 break;
2197 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002198 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002199 if (IS_USER_CMDIDX(ea.cmdidx))
2200 {
2201 ea.line1 = 1;
2202 ea.line2 = ea.addr_type == ADDR_WINDOWS
2203 ? LAST_WIN_NR : LAST_TAB_NR;
2204 }
2205 else
2206 {
2207 /* there is no Vim command which uses '%' and
2208 * ADDR_WINDOWS or ADDR_TABS */
2209 errormsg = (char_u *)_(e_invrange);
2210 goto doend;
2211 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002212 break;
2213 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002214 if (ARGCOUNT == 0)
2215 ea.line1 = ea.line2 = 0;
2216 else
2217 {
2218 ea.line1 = 1;
2219 ea.line2 = ARGCOUNT;
2220 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002221 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002222#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002223 case ADDR_QUICKFIX:
2224 ea.line1 = 1;
2225 ea.line2 = qf_get_size(&ea);
2226 if (ea.line2 == 0)
2227 ea.line2 = 1;
2228 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002229#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 ++ea.addr_count;
2232 }
2233 /* '*' - visual area */
2234 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2235 {
2236 pos_T *fp;
2237
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002238 if (ea.addr_type != ADDR_LINES)
2239 {
2240 errormsg = (char_u *)_(e_invrange);
2241 goto doend;
2242 }
2243
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 ++ea.cmd;
2245 if (!ea.skip)
2246 {
2247 fp = getmark('<', FALSE);
2248 if (check_mark(fp) == FAIL)
2249 goto doend;
2250 ea.line1 = fp->lnum;
2251 fp = getmark('>', FALSE);
2252 if (check_mark(fp) == FAIL)
2253 goto doend;
2254 ea.line2 = fp->lnum;
2255 ++ea.addr_count;
2256 }
2257 }
2258 }
2259 else
2260 ea.line2 = lnum;
2261 ea.addr_count++;
2262
2263 if (*ea.cmd == ';')
2264 {
2265 if (!ea.skip)
2266 curwin->w_cursor.lnum = ea.line2;
2267 }
2268 else if (*ea.cmd != ',')
2269 break;
2270 ++ea.cmd;
2271 }
2272
2273 /* One address given: set start and end lines */
2274 if (ea.addr_count == 1)
2275 {
2276 ea.line1 = ea.line2;
2277 /* ... but only implicit: really no address given */
2278 if (lnum == MAXLNUM)
2279 ea.addr_count = 0;
2280 }
2281
2282 /* Don't leave the cursor on an illegal line (caused by ';') */
2283 check_cursor_lnum();
2284
2285/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002286 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 */
2288
2289 /*
2290 * Skip ':' and any white space
2291 */
2292 ea.cmd = skipwhite(ea.cmd);
2293 while (*ea.cmd == ':')
2294 ea.cmd = skipwhite(ea.cmd + 1);
2295
2296 /*
2297 * If we got a line, but no command, then go to the line.
2298 * If we find a '|' or '\n' we set ea.nextcmd.
2299 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002300 if (*ea.cmd == NUL || *ea.cmd == '"'
2301 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 {
2303 /*
2304 * strange vi behaviour:
2305 * ":3" jumps to line 3
2306 * ":3|..." prints line 3
2307 * ":|" prints current line
2308 */
2309 if (ea.skip) /* skip this if inside :if */
2310 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002311 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 {
2313 ea.cmdidx = CMD_print;
2314 ea.argt = RANGE+COUNT+TRLBAR;
2315 if ((errormsg = invalid_range(&ea)) == NULL)
2316 {
2317 correct_range(&ea);
2318 ex_print(&ea);
2319 }
2320 }
2321 else if (ea.addr_count != 0)
2322 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002323 if (ea.line2 > curbuf->b_ml.ml_line_count)
2324 {
2325 /* With '-' in 'cpoptions' a line number past the file is an
2326 * error, otherwise put it at the end of the file. */
2327 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2328 ea.line2 = -1;
2329 else
2330 ea.line2 = curbuf->b_ml.ml_line_count;
2331 }
2332
2333 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002334 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 else
2336 {
2337 if (ea.line2 == 0)
2338 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 else
2340 curwin->w_cursor.lnum = ea.line2;
2341 beginline(BL_SOL | BL_FIX);
2342 }
2343 }
2344 goto doend;
2345 }
2346
Bram Moolenaard5005162014-08-22 23:05:54 +02002347#ifdef FEAT_AUTOCMD
2348 /* If this looks like an undefined user command and there are CmdUndefined
2349 * autocommands defined, trigger the matching autocommands. */
2350 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2351 && ASCII_ISUPPER(*ea.cmd)
2352 && has_cmdundefined())
2353 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002354 int ret;
2355
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002356 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002357 while (ASCII_ISALNUM(*p))
2358 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002359 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002360 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2361 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002362 /* If the autocommands did something and didn't cause an error, try
2363 * finding the command again. */
2364 p = (ret && !aborting()) ? find_command(&ea, NULL) : NULL;
Bram Moolenaard5005162014-08-22 23:05:54 +02002365 }
2366#endif
2367
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368#ifdef FEAT_USR_CMDS
2369 if (p == NULL)
2370 {
2371 if (!ea.skip)
2372 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2373 goto doend;
2374 }
2375 /* Check for wrong commands. */
2376 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2377 {
2378 errormsg = uc_fun_cmd();
2379 goto doend;
2380 }
2381#endif
2382 if (ea.cmdidx == CMD_SIZE)
2383 {
2384 if (!ea.skip)
2385 {
2386 STRCPY(IObuff, _("E492: Not an editor command"));
2387 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002388 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002390 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 }
2392 goto doend;
2393 }
2394
Bram Moolenaar958636c2014-10-21 20:01:58 +02002395 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2396 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002397#ifdef HAVE_EX_SCRIPT_NI
2398 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2399#endif
2400 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401
2402#ifndef FEAT_EVAL
2403 /*
2404 * When the expression evaluation is disabled, recognize the ":if" and
2405 * ":endif" commands and ignore everything in between it.
2406 */
2407 if (ea.cmdidx == CMD_if)
2408 ++if_level;
2409 if (if_level)
2410 {
2411 if (ea.cmdidx == CMD_endif)
2412 --if_level;
2413 goto doend;
2414 }
2415
2416#endif
2417
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002418 /* forced commands */
2419 if (*p == '!' && ea.cmdidx != CMD_substitute
2420 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 {
2422 ++p;
2423 ea.forceit = TRUE;
2424 }
2425 else
2426 ea.forceit = FALSE;
2427
2428/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002429 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002431 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002432 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433
2434 if (!ea.skip)
2435 {
2436#ifdef HAVE_SANDBOX
2437 if (sandbox != 0 && !(ea.argt & SBOXOK))
2438 {
2439 /* Command not allowed in sandbox. */
2440 errormsg = (char_u *)_(e_sandbox);
2441 goto doend;
2442 }
2443#endif
2444 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2445 {
2446 /* Command not allowed in non-'modifiable' buffer */
2447 errormsg = (char_u *)_(e_modifiable);
2448 goto doend;
2449 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002450
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002451 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002452 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002454 /* Command not allowed when editing the command line. */
2455#ifdef FEAT_CMDWIN
2456 if (cmdwin_type != 0)
2457 errormsg = (char_u *)_(e_cmdwin);
2458 else
2459#endif
2460 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 goto doend;
2462 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002463#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002464 /* Disallow editing another buffer when "curbuf_lock" is set.
2465 * Do allow ":edit" (check for argument later).
2466 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002467 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002468 && ea.cmdidx != CMD_edit
2469 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002470 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002471 && curbuf_locked())
2472 goto doend;
2473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474
2475 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2476 {
2477 /* no range allowed */
2478 errormsg = (char_u *)_(e_norange);
2479 goto doend;
2480 }
2481 }
2482
2483 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2484 {
2485 errormsg = (char_u *)_(e_nobang);
2486 goto doend;
2487 }
2488
2489 /*
2490 * Don't complain about the range if it is not used
2491 * (could happen if line_count is accidentally set to 0).
2492 */
2493 if (!ea.skip && !ni)
2494 {
2495 /*
2496 * If the range is backwards, ask for confirmation and, if given, swap
2497 * ea.line1 & ea.line2 so it's forwards again.
2498 * When global command is busy, don't ask, will fail below.
2499 */
2500 if (!global_busy && ea.line1 > ea.line2)
2501 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002502 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002504 if (sourcing || exmode_active)
2505 {
2506 errormsg = (char_u *)_("E493: Backwards range given");
2507 goto doend;
2508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 if (ask_yesno((char_u *)
2510 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002511 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 }
2513 lnum = ea.line1;
2514 ea.line1 = ea.line2;
2515 ea.line2 = lnum;
2516 }
2517 if ((errormsg = invalid_range(&ea)) != NULL)
2518 goto doend;
2519 }
2520
2521 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2522 ea.line2 = 1;
2523
2524 correct_range(&ea);
2525
2526#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002527 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2528 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 {
2530 /* Put the first line at the start of a closed fold, put the last line
2531 * at the end of a closed fold. */
2532 (void)hasFolding(ea.line1, &ea.line1, NULL);
2533 (void)hasFolding(ea.line2, NULL, &ea.line2);
2534 }
2535#endif
2536
2537#ifdef FEAT_QUICKFIX
2538 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002539 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 * option here, so things like % get expanded.
2541 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002542 p = replace_makeprg(&ea, p, cmdlinep);
2543 if (p == NULL)
2544 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545#endif
2546
2547 /*
2548 * Skip to start of argument.
2549 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2550 */
2551 if (ea.cmdidx == CMD_bang)
2552 ea.arg = p;
2553 else
2554 ea.arg = skipwhite(p);
2555
2556 /*
2557 * Check for "++opt=val" argument.
2558 * Must be first, allow ":w ++enc=utf8 !cmd"
2559 */
2560 if (ea.argt & ARGOPT)
2561 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2562 if (getargopt(&ea) == FAIL && !ni)
2563 {
2564 errormsg = (char_u *)_(e_invarg);
2565 goto doend;
2566 }
2567
2568 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2569 {
2570 if (*ea.arg == '>') /* append */
2571 {
2572 if (*++ea.arg != '>') /* typed wrong */
2573 {
2574 errormsg = (char_u *)_("E494: Use w or w>>");
2575 goto doend;
2576 }
2577 ea.arg = skipwhite(ea.arg + 1);
2578 ea.append = TRUE;
2579 }
2580 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2581 {
2582 ++ea.arg;
2583 ea.usefilter = TRUE;
2584 }
2585 }
2586
2587 if (ea.cmdidx == CMD_read)
2588 {
2589 if (ea.forceit)
2590 {
2591 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2592 ea.forceit = FALSE;
2593 }
2594 else if (*ea.arg == '!') /* :r !filter */
2595 {
2596 ++ea.arg;
2597 ea.usefilter = TRUE;
2598 }
2599 }
2600
2601 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2602 {
2603 ea.amount = 1;
2604 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2605 {
2606 ++ea.arg;
2607 ++ea.amount;
2608 }
2609 ea.arg = skipwhite(ea.arg);
2610 }
2611
2612 /*
2613 * Check for "+command" argument, before checking for next command.
2614 * Don't do this for ":read !cmd" and ":write !cmd".
2615 */
2616 if ((ea.argt & EDITCMD) && !ea.usefilter)
2617 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2618
2619 /*
2620 * Check for '|' to separate commands and '"' to start comments.
2621 * Don't do this for ":read !cmd" and ":write !cmd".
2622 */
2623 if ((ea.argt & TRLBAR) && !ea.usefilter)
2624 separate_nextcmd(&ea);
2625
2626 /*
2627 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002628 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2629 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002631 else if (ea.cmdidx == CMD_bang
2632 || ea.cmdidx == CMD_global
2633 || ea.cmdidx == CMD_vglobal
2634 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 {
2636 for (p = ea.arg; *p; ++p)
2637 {
2638 /* Remove one backslash before a newline, so that it's possible to
2639 * pass a newline to the shell and also a newline that is preceded
2640 * with a backslash. This makes it impossible to end a shell
2641 * command in a backslash, but that doesn't appear useful.
2642 * Halving the number of backslashes is incompatible with previous
2643 * versions. */
2644 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002645 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 else if (*p == '\n')
2647 {
2648 ea.nextcmd = p + 1;
2649 *p = NUL;
2650 break;
2651 }
2652 }
2653 }
2654
2655 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2656 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002657 buf_T *buf;
2658
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002660 switch (ea.addr_type)
2661 {
2662 case ADDR_LINES:
2663 ea.line2 = curbuf->b_ml.ml_line_count;
2664 break;
2665 case ADDR_LOADED_BUFFERS:
2666 buf = firstbuf;
2667 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2668 buf = buf->b_next;
2669 ea.line1 = buf->b_fnum;
2670 buf = lastbuf;
2671 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2672 buf = buf->b_prev;
2673 ea.line2 = buf->b_fnum;
2674 break;
2675 case ADDR_BUFFERS:
2676 ea.line1 = firstbuf->b_fnum;
2677 ea.line2 = lastbuf->b_fnum;
2678 break;
2679 case ADDR_WINDOWS:
2680 ea.line2 = LAST_WIN_NR;
2681 break;
2682 case ADDR_TABS:
2683 ea.line2 = LAST_TAB_NR;
2684 break;
2685 case ADDR_ARGUMENTS:
2686 if (ARGCOUNT == 0)
2687 ea.line1 = ea.line2 = 0;
2688 else
2689 ea.line2 = ARGCOUNT;
2690 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002691#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002692 case ADDR_QUICKFIX:
2693 ea.line2 = qf_get_size(&ea);
2694 if (ea.line2 == 0)
2695 ea.line2 = 1;
2696 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002697#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 }
2700
2701 /* accept numbered register only when no count allowed (:put) */
2702 if ( (ea.argt & REGSTR)
2703 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002704 /* Do not allow register = for user commands */
2705 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2707 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002708#ifndef FEAT_CLIPBOARD
2709 /* check these explicitly for a more specific error message */
2710 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002712 errormsg = (char_u *)_(e_invalidreg);
2713 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 }
2715#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002716 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2717 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002718 {
2719 ea.regname = *ea.arg++;
2720#ifdef FEAT_EVAL
2721 /* for '=' register: accept the rest of the line as an expression */
2722 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2723 {
2724 set_expr_line(vim_strsave(ea.arg));
2725 ea.arg += STRLEN(ea.arg);
2726 }
2727#endif
2728 ea.arg = skipwhite(ea.arg);
2729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 }
2731
2732 /*
2733 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2734 * count, it's a buffer name.
2735 */
2736 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2737 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2738 || vim_iswhite(*p)))
2739 {
2740 n = getdigits(&ea.arg);
2741 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002742 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 {
2744 errormsg = (char_u *)_(e_zerocount);
2745 goto doend;
2746 }
2747 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2748 {
2749 ea.line2 = n;
2750 if (ea.addr_count == 0)
2751 ea.addr_count = 1;
2752 }
2753 else
2754 {
2755 ea.line1 = ea.line2;
2756 ea.line2 += n - 1;
2757 ++ea.addr_count;
2758 /*
2759 * Be vi compatible: no error message for out of range.
2760 */
2761 if (ea.line2 > curbuf->b_ml.ml_line_count)
2762 ea.line2 = curbuf->b_ml.ml_line_count;
2763 }
2764 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002765
2766 /*
2767 * Check for flags: 'l', 'p' and '#'.
2768 */
2769 if (ea.argt & EXFLAGS)
2770 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002772 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002773 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 {
2775 errormsg = (char_u *)_(e_trailing);
2776 goto doend;
2777 }
2778
2779 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2780 {
2781 errormsg = (char_u *)_(e_argreq);
2782 goto doend;
2783 }
2784
2785#ifdef FEAT_EVAL
2786 /*
2787 * Skip the command when it's not going to be executed.
2788 * The commands like :if, :endif, etc. always need to be executed.
2789 * Also make an exception for commands that handle a trailing command
2790 * themselves.
2791 */
2792 if (ea.skip)
2793 {
2794 switch (ea.cmdidx)
2795 {
2796 /* commands that need evaluation */
2797 case CMD_while:
2798 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002799 case CMD_for:
2800 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 case CMD_if:
2802 case CMD_elseif:
2803 case CMD_else:
2804 case CMD_endif:
2805 case CMD_try:
2806 case CMD_catch:
2807 case CMD_finally:
2808 case CMD_endtry:
2809 case CMD_function:
2810 break;
2811
2812 /* Commands that handle '|' themselves. Check: A command should
2813 * either have the TRLBAR flag, appear in this list or appear in
2814 * the list at ":help :bar". */
2815 case CMD_aboveleft:
2816 case CMD_and:
2817 case CMD_belowright:
2818 case CMD_botright:
2819 case CMD_browse:
2820 case CMD_call:
2821 case CMD_confirm:
2822 case CMD_delfunction:
2823 case CMD_djump:
2824 case CMD_dlist:
2825 case CMD_dsearch:
2826 case CMD_dsplit:
2827 case CMD_echo:
2828 case CMD_echoerr:
2829 case CMD_echomsg:
2830 case CMD_echon:
2831 case CMD_execute:
2832 case CMD_help:
2833 case CMD_hide:
2834 case CMD_ijump:
2835 case CMD_ilist:
2836 case CMD_isearch:
2837 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002838 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 case CMD_keepjumps:
2840 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002841 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 case CMD_leftabove:
2843 case CMD_let:
2844 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002845 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002847 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002848 case CMD_noautocmd:
2849 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 case CMD_perl:
2851 case CMD_psearch:
2852 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002853 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002854 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 case CMD_return:
2856 case CMD_rightbelow:
2857 case CMD_ruby:
2858 case CMD_silent:
2859 case CMD_smagic:
2860 case CMD_snomagic:
2861 case CMD_substitute:
2862 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002863 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 case CMD_tcl:
2865 case CMD_throw:
2866 case CMD_tilde:
2867 case CMD_topleft:
2868 case CMD_unlet:
2869 case CMD_verbose:
2870 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002871 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 break;
2873
2874 default: goto doend;
2875 }
2876 }
2877#endif
2878
2879 if (ea.argt & XFILE)
2880 {
2881 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2882 goto doend;
2883 }
2884
2885#ifdef FEAT_LISTCMDS
2886 /*
2887 * Accept buffer name. Cannot be used at the same time with a buffer
2888 * number. Don't do this for a user command.
2889 */
2890 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002891 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 {
2893 /*
2894 * :bdelete, :bwipeout and :bunload take several arguments, separated
2895 * by spaces: find next space (skipping over escaped characters).
2896 * The others take one argument: ignore trailing spaces.
2897 */
2898 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2899 || ea.cmdidx == CMD_bunload)
2900 p = skiptowhite_esc(ea.arg);
2901 else
2902 {
2903 p = ea.arg + STRLEN(ea.arg);
2904 while (p > ea.arg && vim_iswhite(p[-1]))
2905 --p;
2906 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002907 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2908 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 if (ea.line2 < 0) /* failed */
2910 goto doend;
2911 ea.addr_count = 1;
2912 ea.arg = skipwhite(p);
2913 }
2914#endif
2915
2916/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002917 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 *
2919 * The "ea" structure holds the arguments that can be used.
2920 */
2921 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002922 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 ea.cookie = cookie;
2924#ifdef FEAT_EVAL
2925 ea.cstack = cstack;
2926#endif
2927
2928#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002929 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 {
2931 /*
2932 * Execute a user-defined command.
2933 */
2934 do_ucmd(&ea);
2935 }
2936 else
2937#endif
2938 {
2939 /*
2940 * Call the function to execute the command.
2941 */
2942 ea.errmsg = NULL;
2943 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2944 if (ea.errmsg != NULL)
2945 errormsg = (char_u *)_(ea.errmsg);
2946 }
2947
2948#ifdef FEAT_EVAL
2949 /*
2950 * If the command just executed called do_cmdline(), any throw or ":return"
2951 * or ":finish" encountered there must also check the cstack of the still
2952 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2953 * exception, or reanimate a returned function or finished script file and
2954 * return or finish it again.
2955 */
2956 if (need_rethrow)
2957 do_throw(cstack);
2958 else if (check_cstack)
2959 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002960 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002962 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 && current_func_returned())
2964 do_return(&ea, TRUE, FALSE, NULL);
2965 }
2966 need_rethrow = check_cstack = FALSE;
2967#endif
2968
2969doend:
2970 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2971 curwin->w_cursor.lnum = 1;
2972
2973 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2974 {
2975 if (sourcing)
2976 {
2977 if (errormsg != IObuff)
2978 {
2979 STRCPY(IObuff, errormsg);
2980 errormsg = IObuff;
2981 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002982 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 }
2984 emsg(errormsg);
2985 }
2986#ifdef FEAT_EVAL
2987 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002988 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2989 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990#endif
2991
2992 if (verbose_save >= 0)
2993 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002994#ifdef FEAT_AUTOCMD
2995 if (cmdmod.save_ei != NULL)
2996 {
2997 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002998 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2999 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00003000 free_string_option(cmdmod.save_ei);
3001 }
3002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003
3004 cmdmod = save_cmdmod;
3005
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003006 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 {
3008 /* messages could be enabled for a serious error, need to check if the
3009 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00003010 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003011 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 emsg_silent -= did_esilent;
3013 if (emsg_silent < 0)
3014 emsg_silent = 0;
3015 /* Restore msg_scroll, it's set by file I/O commands, even when no
3016 * message is actually displayed. */
3017 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003018
3019 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
3020 * somewhere in the line. Put it back in the first column. */
3021 if (redirecting())
3022 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 }
3024
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003025#ifdef HAVE_SANDBOX
3026 if (did_sandbox)
3027 --sandbox;
3028#endif
3029
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3031 ea.nextcmd = NULL;
3032
3033#ifdef FEAT_EVAL
3034 --ex_nesting_level;
3035#endif
3036
3037 return ea.nextcmd;
3038}
3039#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003040 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041#endif
3042
3043/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003044 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 * If there is a match advance "pp" to the argument and return TRUE.
3046 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003047 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003048checkforcmd(
3049 char_u **pp, /* start of command */
3050 char *cmd, /* name of command */
3051 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052{
3053 int i;
3054
3055 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003056 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 break;
3058 if (i >= len && !isalpha((*pp)[i]))
3059 {
3060 *pp = skipwhite(*pp + i);
3061 return TRUE;
3062 }
3063 return FALSE;
3064}
3065
3066/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003067 * Append "cmd" to the error message in IObuff.
3068 * Takes care of limiting the length and handling 0xa0, which would be
3069 * invisible otherwise.
3070 */
3071 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003072append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003073{
3074 char_u *s = cmd;
3075 char_u *d;
3076
3077 STRCAT(IObuff, ": ");
3078 d = IObuff + STRLEN(IObuff);
3079 while (*s != NUL && d - IObuff < IOSIZE - 7)
3080 {
3081 if (
3082#ifdef FEAT_MBYTE
3083 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3084#endif
3085 *s == 0xa0)
3086 {
3087 s +=
3088#ifdef FEAT_MBYTE
3089 enc_utf8 ? 2 :
3090#endif
3091 1;
3092 STRCPY(d, "<a0>");
3093 d += 4;
3094 }
3095 else
3096 MB_COPY_CHAR(s, d);
3097 }
3098 *d = NUL;
3099}
3100
3101/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003103 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003105 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 * Returns NULL for an ambiguous user command.
3107 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003109find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110{
3111 int len;
3112 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003113 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114
3115 /*
3116 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003117 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 * - the 'k' command can directly be followed by any character.
3119 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003120 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003122 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 */
3124 p = eap->cmd;
3125 if (*p == 'k')
3126 {
3127 eap->cmdidx = CMD_k;
3128 ++p;
3129 }
3130 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003131 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3132 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 || p[1] == 'g'
3134 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3135 || p[1] == 'I'
3136 || (p[1] == 'r' && p[2] != 'e')))
3137 {
3138 eap->cmdidx = CMD_substitute;
3139 ++p;
3140 }
3141 else
3142 {
3143 while (ASCII_ISALPHA(*p))
3144 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003145 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003146 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003147 while (ASCII_ISALNUM(*p))
3148 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003149
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 /* check for non-alpha command */
3151 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3152 ++p;
3153 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003154 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3155 {
3156 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3157 * :delete with the 'l' flag. Same for 'p'. */
3158 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003159 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003160 break;
3161 if (i == len - 1)
3162 {
3163 --len;
3164 if (p[-1] == 'l')
3165 eap->flags |= EXFLAG_LIST;
3166 else
3167 eap->flags |= EXFLAG_PRINT;
3168 }
3169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170
3171 if (ASCII_ISLOWER(*eap->cmd))
3172 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
3173 else
3174 eap->cmdidx = cmdidxs[26];
3175
3176 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3177 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3178 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3179 (size_t)len) == 0)
3180 {
3181#ifdef FEAT_EVAL
3182 if (full != NULL
3183 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3184 *full = TRUE;
3185#endif
3186 break;
3187 }
3188
3189#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003190 /* Look for a user defined command as a last resort. Let ":Print" be
3191 * overruled by a user defined command. */
3192 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3193 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003195 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 while (ASCII_ISALNUM(*p))
3197 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003198 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 }
3200#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003201 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 eap->cmdidx = CMD_SIZE;
3203 }
3204
3205 return p;
3206}
3207
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003208#ifdef FEAT_USR_CMDS
3209/*
3210 * Search for a user command that matches "eap->cmd".
3211 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3212 * Return a pointer to just after the command.
3213 * Return NULL if there is no matching command.
3214 */
3215 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003216find_ucmd(
3217 exarg_T *eap,
3218 char_u *p, /* end of the command (possibly including count) */
3219 int *full, /* set to TRUE for a full match */
3220 expand_T *xp, /* used for completion, NULL otherwise */
3221 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003222{
3223 int len = (int)(p - eap->cmd);
3224 int j, k, matchlen = 0;
3225 ucmd_T *uc;
3226 int found = FALSE;
3227 int possible = FALSE;
3228 char_u *cp, *np; /* Point into typed cmd and test name */
3229 garray_T *gap;
3230 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3231 only full match global is accepted. */
3232
3233 /*
3234 * Look for buffer-local user commands first, then global ones.
3235 */
3236 gap = &curbuf->b_ucmds;
3237 for (;;)
3238 {
3239 for (j = 0; j < gap->ga_len; ++j)
3240 {
3241 uc = USER_CMD_GA(gap, j);
3242 cp = eap->cmd;
3243 np = uc->uc_name;
3244 k = 0;
3245 while (k < len && *np != NUL && *cp++ == *np++)
3246 k++;
3247 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3248 {
3249 /* If finding a second match, the command is ambiguous. But
3250 * not if a buffer-local command wasn't a full match and a
3251 * global command is a full match. */
3252 if (k == len && found && *np != NUL)
3253 {
3254 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003255 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003256 amb_local = TRUE;
3257 }
3258
3259 if (!found || (k == len && *np == NUL))
3260 {
3261 /* If we matched up to a digit, then there could
3262 * be another command including the digit that we
3263 * should use instead.
3264 */
3265 if (k == len)
3266 found = TRUE;
3267 else
3268 possible = TRUE;
3269
3270 if (gap == &ucmds)
3271 eap->cmdidx = CMD_USER;
3272 else
3273 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003274 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003275 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003276 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003277
3278# ifdef FEAT_CMDL_COMPL
3279 if (compl != NULL)
3280 *compl = uc->uc_compl;
3281# ifdef FEAT_EVAL
3282 if (xp != NULL)
3283 {
3284 xp->xp_arg = uc->uc_compl_arg;
3285 xp->xp_scriptID = uc->uc_scriptID;
3286 }
3287# endif
3288# endif
3289 /* Do not search for further abbreviations
3290 * if this is an exact match. */
3291 matchlen = k;
3292 if (k == len && *np == NUL)
3293 {
3294 if (full != NULL)
3295 *full = TRUE;
3296 amb_local = FALSE;
3297 break;
3298 }
3299 }
3300 }
3301 }
3302
3303 /* Stop if we found a full match or searched all. */
3304 if (j < gap->ga_len || gap == &ucmds)
3305 break;
3306 gap = &ucmds;
3307 }
3308
3309 /* Only found ambiguous matches. */
3310 if (amb_local)
3311 {
3312 if (xp != NULL)
3313 xp->xp_context = EXPAND_UNSUCCESSFUL;
3314 return NULL;
3315 }
3316
3317 /* The match we found may be followed immediately by a number. Move "p"
3318 * back to point to it. */
3319 if (found || possible)
3320 return p + (matchlen - len);
3321 return p;
3322}
3323#endif
3324
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003326static struct cmdmod
3327{
3328 char *name;
3329 int minlen;
3330 int has_count; /* :123verbose :3tab */
3331} cmdmods[] = {
3332 {"aboveleft", 3, FALSE},
3333 {"belowright", 3, FALSE},
3334 {"botright", 2, FALSE},
3335 {"browse", 3, FALSE},
3336 {"confirm", 4, FALSE},
3337 {"hide", 3, FALSE},
3338 {"keepalt", 5, FALSE},
3339 {"keepjumps", 5, FALSE},
3340 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003341 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003342 {"leftabove", 5, FALSE},
3343 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003344 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003345 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003346 {"rightbelow", 6, FALSE},
3347 {"sandbox", 3, FALSE},
3348 {"silent", 3, FALSE},
3349 {"tab", 3, TRUE},
3350 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003351 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003352 {"verbose", 4, TRUE},
3353 {"vertical", 4, FALSE},
3354};
3355
3356/*
3357 * Return length of a command modifier (including optional count).
3358 * Return zero when it's not a modifier.
3359 */
3360 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003361modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003362{
3363 int i, j;
3364 char_u *p = cmd;
3365
3366 if (VIM_ISDIGIT(*cmd))
3367 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003368 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003369 {
3370 for (j = 0; p[j] != NUL; ++j)
3371 if (p[j] != cmdmods[i].name[j])
3372 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003373 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003374 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003375 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003376 }
3377 return 0;
3378}
3379
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380/*
3381 * Return > 0 if an Ex command "name" exists.
3382 * Return 2 if there is an exact match.
3383 * Return 3 if there is an ambiguous match.
3384 */
3385 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003386cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387{
3388 exarg_T ea;
3389 int full = FALSE;
3390 int i;
3391 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003392 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393
3394 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003395 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 {
3397 for (j = 0; name[j] != NUL; ++j)
3398 if (name[j] != cmdmods[i].name[j])
3399 break;
3400 if (name[j] == NUL && j >= cmdmods[i].minlen)
3401 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3402 }
3403
Bram Moolenaara9587612006-05-04 21:47:50 +00003404 /* Check built-in commands and user defined commands.
3405 * For ":2match" and ":3match" we need to skip the number. */
3406 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003408 p = find_command(&ea, &full);
3409 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003411 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3412 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003413 if (*skipwhite(p) != NUL)
3414 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3416}
3417#endif
3418
3419/*
3420 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3421 * we don't need/want deleted. Maybe this could be done better if we didn't
3422 * repeat all this stuff. The only problem is that they may not stay
3423 * perfectly compatible with each other, but then the command line syntax
3424 * probably won't change that much -- webb.
3425 */
3426 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003427set_one_cmd_context(
3428 expand_T *xp,
3429 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430{
3431 char_u *p;
3432 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003433 int len = 0;
3434 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3436 int compl = EXPAND_NOTHING;
3437#endif
3438#ifdef FEAT_CMDL_COMPL
3439 int delim;
3440#endif
3441 int forceit = FALSE;
3442 int usefilter = FALSE; /* filter instead of file name */
3443
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003444 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 xp->xp_pattern = buff;
3446 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003447 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448
3449/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003450 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 */
3452 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3453 ;
3454 xp->xp_pattern = cmd;
3455
3456 if (*cmd == NUL)
3457 return NULL;
3458 if (*cmd == '"') /* ignore comment lines */
3459 {
3460 xp->xp_context = EXPAND_NOTHING;
3461 return NULL;
3462 }
3463
3464/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003465 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 */
3467 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 xp->xp_pattern = cmd;
3469 if (*cmd == NUL)
3470 return NULL;
3471 if (*cmd == '"')
3472 {
3473 xp->xp_context = EXPAND_NOTHING;
3474 return NULL;
3475 }
3476
3477 if (*cmd == '|' || *cmd == '\n')
3478 return cmd + 1; /* There's another command */
3479
3480 /*
3481 * Isolate the command and search for it in the command table.
3482 * Exceptions:
3483 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003484 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3486 */
3487 if (*cmd == 'k' && cmd[1] != 'e')
3488 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003489 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 p = cmd + 1;
3491 }
3492 else
3493 {
3494 p = cmd;
3495 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3496 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003497 /* a user command may contain digits */
3498 if (ASCII_ISUPPER(cmd[0]))
3499 while (ASCII_ISALNUM(*p) || *p == '*')
3500 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003501 /* for python 3.x: ":py3*" commands completion */
3502 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003503 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003504 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003505 while (ASCII_ISALPHA(*p) || *p == '*')
3506 ++p;
3507 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003508 /* check for non-alpha command */
3509 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3510 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003511 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003513 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 {
3515 xp->xp_context = EXPAND_UNSUCCESSFUL;
3516 return NULL;
3517 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003518 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003519 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3520 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3521 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 break;
3523
3524#ifdef FEAT_USR_CMDS
3525 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3527 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528#endif
3529 }
3530
3531 /*
3532 * If the cursor is touching the command, and it ends in an alpha-numeric
3533 * character, complete the command name.
3534 */
3535 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3536 return NULL;
3537
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003538 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 {
3540 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3541 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003542 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 p = cmd + 1;
3544 }
3545#ifdef FEAT_USR_CMDS
3546 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3547 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003548 ea.cmd = cmd;
3549 p = find_ucmd(&ea, p, NULL, xp,
3550# if defined(FEAT_CMDL_COMPL)
3551 &compl
3552# else
3553 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003555 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003556 if (p == NULL)
3557 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 }
3559#endif
3560 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003561 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 {
3563 /* Not still touching the command and it was an illegal one */
3564 xp->xp_context = EXPAND_UNSUCCESSFUL;
3565 return NULL;
3566 }
3567
3568 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3569
3570 if (*p == '!') /* forced commands */
3571 {
3572 forceit = TRUE;
3573 ++p;
3574 }
3575
3576/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003577 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003579 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003580 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581
3582 arg = skipwhite(p);
3583
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003584 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 {
3586 if (*arg == '>') /* append */
3587 {
3588 if (*++arg == '>')
3589 ++arg;
3590 arg = skipwhite(arg);
3591 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003592 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 {
3594 ++arg;
3595 usefilter = TRUE;
3596 }
3597 }
3598
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003599 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 {
3601 usefilter = forceit; /* :r! filter if forced */
3602 if (*arg == '!') /* :r !filter */
3603 {
3604 ++arg;
3605 usefilter = TRUE;
3606 }
3607 }
3608
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003609 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 {
3611 while (*arg == *cmd) /* allow any number of '>' or '<' */
3612 ++arg;
3613 arg = skipwhite(arg);
3614 }
3615
3616 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003617 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 {
3619 /* Check if we're in the +command */
3620 p = arg + 1;
3621 arg = skip_cmd_arg(arg, FALSE);
3622
3623 /* Still touching the command after '+'? */
3624 if (*arg == NUL)
3625 return p;
3626
3627 /* Skip space(s) after +command to get to the real argument */
3628 arg = skipwhite(arg);
3629 }
3630
3631 /*
3632 * Check for '|' to separate commands and '"' to start comments.
3633 * Don't do this for ":read !cmd" and ":write !cmd".
3634 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003635 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 {
3637 p = arg;
3638 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003639 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 p += 2;
3641 while (*p)
3642 {
3643 if (*p == Ctrl_V)
3644 {
3645 if (p[1] != NUL)
3646 ++p;
3647 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003648 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 || *p == '|' || *p == '\n')
3650 {
3651 if (*(p - 1) != '\\')
3652 {
3653 if (*p == '|' || *p == '\n')
3654 return p + 1;
3655 return NULL; /* It's a comment */
3656 }
3657 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003658 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 }
3660 }
3661
3662 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003663 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 vim_strchr((char_u *)"|\"", *arg) == NULL)
3665 return NULL;
3666
3667 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003668 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003670 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003671 while (*p && p < buff + len)
3672 {
3673 if (*p == ' ' || *p == TAB)
3674 {
3675 /* argument starts after a space */
3676 xp->xp_pattern = ++p;
3677 }
3678 else
3679 {
3680 if (*p == '\\' && *(p + 1) != NUL)
3681 ++p; /* skip over escaped character */
3682 mb_ptr_adv(p);
3683 }
3684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003686 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003688 int c;
3689 int in_quote = FALSE;
3690 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691
3692 /*
3693 * Allow spaces within back-quotes to count as part of the argument
3694 * being expanded.
3695 */
3696 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003697 p = xp->xp_pattern;
3698 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003700#ifdef FEAT_MBYTE
3701 if (has_mbyte)
3702 c = mb_ptr2char(p);
3703 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003705 c = *p;
3706 if (c == '\\' && p[1] != NUL)
3707 ++p;
3708 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 {
3710 if (!in_quote)
3711 {
3712 xp->xp_pattern = p;
3713 bow = p + 1;
3714 }
3715 in_quote = !in_quote;
3716 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003717 /* An argument can contain just about everything, except
3718 * characters that end the command and white space. */
3719 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003720#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003721 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003722#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003723 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003724 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003725 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003726 while (*p != NUL)
3727 {
3728#ifdef FEAT_MBYTE
3729 if (has_mbyte)
3730 c = mb_ptr2char(p);
3731 else
3732#endif
3733 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003734 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003735 break;
3736#ifdef FEAT_MBYTE
3737 if (has_mbyte)
3738 len = (*mb_ptr2len)(p);
3739 else
3740#endif
3741 len = 1;
3742 mb_ptr_adv(p);
3743 }
3744 if (in_quote)
3745 bow = p;
3746 else
3747 xp->xp_pattern = p;
3748 p -= len;
3749 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003750 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 }
3752
3753 /*
3754 * If we are still inside the quotes, and we passed a space, just
3755 * expand from there.
3756 */
3757 if (bow != NULL && in_quote)
3758 xp->xp_pattern = bow;
3759 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003760
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003761 /* For a shell command more chars need to be escaped. */
3762 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003763 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003764#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003765 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003766#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003767 /* When still after the command name expand executables. */
3768 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003769 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771
3772 /* Check for environment variable */
3773 if (*xp->xp_pattern == '$'
Bram Moolenaare7fedb62015-12-31 19:07:19 +01003774#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 || *xp->xp_pattern == '%'
3776#endif
3777 )
3778 {
3779 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3780 if (!vim_isIDc(*p))
3781 break;
3782 if (*p == NUL)
3783 {
3784 xp->xp_context = EXPAND_ENV_VARS;
3785 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003786#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3787 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003788 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003789 compl = EXPAND_ENV_VARS;
3790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 }
3792 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003793#if defined(FEAT_CMDL_COMPL)
3794 /* Check for user names */
3795 if (*xp->xp_pattern == '~')
3796 {
3797 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3798 ;
3799 /* Complete ~user only if it partially matches a user name.
3800 * A full match ~user<Tab> will be replaced by user's home
3801 * directory i.e. something like ~user<Tab> -> /home/user/ */
3802 if (*p == NUL && p > xp->xp_pattern + 1
3803 && match_user(xp->xp_pattern + 1) == 1)
3804 {
3805 xp->xp_context = EXPAND_USER;
3806 ++xp->xp_pattern;
3807 }
3808 }
3809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 }
3811
3812/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003813 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003815 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003817 case CMD_find:
3818 case CMD_sfind:
3819 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003820 if (xp->xp_context == EXPAND_FILES)
3821 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003822 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 case CMD_cd:
3824 case CMD_chdir:
3825 case CMD_lcd:
3826 case CMD_lchdir:
3827 if (xp->xp_context == EXPAND_FILES)
3828 xp->xp_context = EXPAND_DIRECTORIES;
3829 break;
3830 case CMD_help:
3831 xp->xp_context = EXPAND_HELP;
3832 xp->xp_pattern = arg;
3833 break;
3834
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003835 /* Command modifiers: return the argument.
3836 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003838 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 case CMD_belowright:
3840 case CMD_botright:
3841 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003842 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003843 case CMD_cdo:
3844 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003846 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 case CMD_folddoclosed:
3848 case CMD_folddoopen:
3849 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003850 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 case CMD_keepjumps:
3852 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003853 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003854 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003856 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003858 case CMD_noautocmd:
3859 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003861 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003863 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003864 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 case CMD_topleft:
3866 case CMD_verbose:
3867 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003868 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 return arg;
3870
Bram Moolenaar4f688582007-07-24 12:34:30 +00003871#ifdef FEAT_CMDL_COMPL
3872# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 case CMD_match:
3874 if (*arg == NUL || !ends_excmd(*arg))
3875 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003876 /* also complete "None" */
3877 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 arg = skipwhite(skiptowhite(arg));
3879 if (*arg != NUL)
3880 {
3881 xp->xp_context = EXPAND_NOTHING;
3882 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3883 }
3884 }
3885 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003886# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888/*
3889 * All completion for the +cmdline_compl feature goes here.
3890 */
3891
3892# ifdef FEAT_USR_CMDS
3893 case CMD_command:
3894 /* Check for attributes */
3895 while (*arg == '-')
3896 {
3897 arg++; /* Skip "-" */
3898 p = skiptowhite(arg);
3899 if (*p == NUL)
3900 {
3901 /* Cursor is still in the attribute */
3902 p = vim_strchr(arg, '=');
3903 if (p == NULL)
3904 {
3905 /* No "=", so complete attribute names */
3906 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3907 xp->xp_pattern = arg;
3908 return NULL;
3909 }
3910
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003911 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 * their arguments as well.
3913 */
3914 if (STRNICMP(arg, "complete", p - arg) == 0)
3915 {
3916 xp->xp_context = EXPAND_USER_COMPLETE;
3917 xp->xp_pattern = p + 1;
3918 return NULL;
3919 }
3920 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3921 {
3922 xp->xp_context = EXPAND_USER_NARGS;
3923 xp->xp_pattern = p + 1;
3924 return NULL;
3925 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003926 else if (STRNICMP(arg, "addr", p - arg) == 0)
3927 {
3928 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3929 xp->xp_pattern = p + 1;
3930 return NULL;
3931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 return NULL;
3933 }
3934 arg = skipwhite(p);
3935 }
3936
3937 /* After the attributes comes the new command name */
3938 p = skiptowhite(arg);
3939 if (*p == NUL)
3940 {
3941 xp->xp_context = EXPAND_USER_COMMANDS;
3942 xp->xp_pattern = arg;
3943 break;
3944 }
3945
3946 /* And finally comes a normal command */
3947 return skipwhite(p);
3948
3949 case CMD_delcommand:
3950 xp->xp_context = EXPAND_USER_COMMANDS;
3951 xp->xp_pattern = arg;
3952 break;
3953# endif
3954
3955 case CMD_global:
3956 case CMD_vglobal:
3957 delim = *arg; /* get the delimiter */
3958 if (delim)
3959 ++arg; /* skip delimiter if there is one */
3960
3961 while (arg[0] != NUL && arg[0] != delim)
3962 {
3963 if (arg[0] == '\\' && arg[1] != NUL)
3964 ++arg;
3965 ++arg;
3966 }
3967 if (arg[0] != NUL)
3968 return arg + 1;
3969 break;
3970 case CMD_and:
3971 case CMD_substitute:
3972 delim = *arg;
3973 if (delim)
3974 {
3975 /* skip "from" part */
3976 ++arg;
3977 arg = skip_regexp(arg, delim, p_magic, NULL);
3978 }
3979 /* skip "to" part */
3980 while (arg[0] != NUL && arg[0] != delim)
3981 {
3982 if (arg[0] == '\\' && arg[1] != NUL)
3983 ++arg;
3984 ++arg;
3985 }
3986 if (arg[0] != NUL) /* skip delimiter */
3987 ++arg;
3988 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3989 ++arg;
3990 if (arg[0] != NUL)
3991 return arg;
3992 break;
3993 case CMD_isearch:
3994 case CMD_dsearch:
3995 case CMD_ilist:
3996 case CMD_dlist:
3997 case CMD_ijump:
3998 case CMD_psearch:
3999 case CMD_djump:
4000 case CMD_isplit:
4001 case CMD_dsplit:
4002 arg = skipwhite(skipdigits(arg)); /* skip count */
4003 if (*arg == '/') /* Match regexp, not just whole words */
4004 {
4005 for (++arg; *arg && *arg != '/'; arg++)
4006 if (*arg == '\\' && arg[1] != NUL)
4007 arg++;
4008 if (*arg)
4009 {
4010 arg = skipwhite(arg + 1);
4011
4012 /* Check for trailing illegal characters */
4013 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4014 xp->xp_context = EXPAND_NOTHING;
4015 else
4016 return arg;
4017 }
4018 }
4019 break;
4020#ifdef FEAT_AUTOCMD
4021 case CMD_autocmd:
4022 return set_context_in_autocmd(xp, arg, FALSE);
4023
4024 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004025 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 return set_context_in_autocmd(xp, arg, TRUE);
4027#endif
4028 case CMD_set:
4029 set_context_in_set_cmd(xp, arg, 0);
4030 break;
4031 case CMD_setglobal:
4032 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4033 break;
4034 case CMD_setlocal:
4035 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4036 break;
4037 case CMD_tag:
4038 case CMD_stag:
4039 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004040 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 case CMD_tselect:
4042 case CMD_stselect:
4043 case CMD_ptselect:
4044 case CMD_tjump:
4045 case CMD_stjump:
4046 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004047 if (*p_wop != NUL)
4048 xp->xp_context = EXPAND_TAGS_LISTFILES;
4049 else
4050 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 xp->xp_pattern = arg;
4052 break;
4053 case CMD_augroup:
4054 xp->xp_context = EXPAND_AUGROUP;
4055 xp->xp_pattern = arg;
4056 break;
4057#ifdef FEAT_SYN_HL
4058 case CMD_syntax:
4059 set_context_in_syntax_cmd(xp, arg);
4060 break;
4061#endif
4062#ifdef FEAT_EVAL
4063 case CMD_let:
4064 case CMD_if:
4065 case CMD_elseif:
4066 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004067 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 case CMD_echo:
4069 case CMD_echon:
4070 case CMD_execute:
4071 case CMD_echomsg:
4072 case CMD_echoerr:
4073 case CMD_call:
4074 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004075 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 break;
4077
4078 case CMD_unlet:
4079 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4080 arg = xp->xp_pattern + 1;
4081 xp->xp_context = EXPAND_USER_VARS;
4082 xp->xp_pattern = arg;
4083 break;
4084
4085 case CMD_function:
4086 case CMD_delfunction:
4087 xp->xp_context = EXPAND_USER_FUNC;
4088 xp->xp_pattern = arg;
4089 break;
4090
4091 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004092 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 break;
4094#endif
4095 case CMD_highlight:
4096 set_context_in_highlight_cmd(xp, arg);
4097 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004098#ifdef FEAT_CSCOPE
4099 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004100 case CMD_lcscope:
4101 case CMD_scscope:
4102 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004103 break;
4104#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004105#ifdef FEAT_SIGNS
4106 case CMD_sign:
4107 set_context_in_sign_cmd(xp, arg);
4108 break;
4109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110#ifdef FEAT_LISTCMDS
4111 case CMD_bdelete:
4112 case CMD_bwipeout:
4113 case CMD_bunload:
4114 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4115 arg = xp->xp_pattern + 1;
4116 /*FALLTHROUGH*/
4117 case CMD_buffer:
4118 case CMD_sbuffer:
4119 case CMD_checktime:
4120 xp->xp_context = EXPAND_BUFFERS;
4121 xp->xp_pattern = arg;
4122 break;
4123#endif
4124#ifdef FEAT_USR_CMDS
4125 case CMD_USER:
4126 case CMD_USER_BUF:
4127 if (compl != EXPAND_NOTHING)
4128 {
4129 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004130 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 {
4132# ifdef FEAT_MENU
4133 if (compl == EXPAND_MENUS)
4134 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4135# endif
4136 if (compl == EXPAND_COMMANDS)
4137 return arg;
4138 if (compl == EXPAND_MAPPINGS)
4139 return set_context_in_map_cmd(xp, (char_u *)"map",
4140 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004141 /* Find start of last argument. */
4142 p = arg;
4143 while (*p)
4144 {
4145 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004146 /* argument starts after a space */
4147 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004148 else if (*p == '\\' && *(p + 1) != NUL)
4149 ++p; /* skip over escaped character */
4150 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 xp->xp_pattern = arg;
4153 }
4154 xp->xp_context = compl;
4155 }
4156 break;
4157#endif
4158 case CMD_map: case CMD_noremap:
4159 case CMD_nmap: case CMD_nnoremap:
4160 case CMD_vmap: case CMD_vnoremap:
4161 case CMD_omap: case CMD_onoremap:
4162 case CMD_imap: case CMD_inoremap:
4163 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004164 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004165 case CMD_smap: case CMD_snoremap:
4166 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004168 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 case CMD_unmap:
4170 case CMD_nunmap:
4171 case CMD_vunmap:
4172 case CMD_ounmap:
4173 case CMD_iunmap:
4174 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004175 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004176 case CMD_sunmap:
4177 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004179 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 case CMD_abbreviate: case CMD_noreabbrev:
4181 case CMD_cabbrev: case CMD_cnoreabbrev:
4182 case CMD_iabbrev: case CMD_inoreabbrev:
4183 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004184 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 case CMD_unabbreviate:
4186 case CMD_cunabbrev:
4187 case CMD_iunabbrev:
4188 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004189 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190#ifdef FEAT_MENU
4191 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4192 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4193 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4194 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4195 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4196 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4197 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4198 case CMD_tmenu: case CMD_tunmenu:
4199 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4200 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4201#endif
4202
4203 case CMD_colorscheme:
4204 xp->xp_context = EXPAND_COLORS;
4205 xp->xp_pattern = arg;
4206 break;
4207
4208 case CMD_compiler:
4209 xp->xp_context = EXPAND_COMPILER;
4210 xp->xp_pattern = arg;
4211 break;
4212
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004213 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004214 xp->xp_context = EXPAND_OWNSYNTAX;
4215 xp->xp_pattern = arg;
4216 break;
4217
4218 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004219 xp->xp_context = EXPAND_FILETYPE;
4220 xp->xp_pattern = arg;
4221 break;
4222
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4224 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4225 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004226 p = skiptowhite(arg);
4227 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 {
4229 xp->xp_context = EXPAND_LANGUAGE;
4230 xp->xp_pattern = arg;
4231 }
4232 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004233 {
4234 if ( STRNCMP(arg, "messages", p - arg) == 0
4235 || STRNCMP(arg, "ctype", p - arg) == 0
4236 || STRNCMP(arg, "time", p - arg) == 0)
4237 {
4238 xp->xp_context = EXPAND_LOCALES;
4239 xp->xp_pattern = skipwhite(p);
4240 }
4241 else
4242 xp->xp_context = EXPAND_NOTHING;
4243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 break;
4245#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004246#if defined(FEAT_PROFILE)
4247 case CMD_profile:
4248 set_context_in_profile_cmd(xp, arg);
4249 break;
4250#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004251 case CMD_behave:
4252 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004253 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004254 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004256#if defined(FEAT_CMDHIST)
4257 case CMD_history:
4258 xp->xp_context = EXPAND_HISTORY;
4259 xp->xp_pattern = arg;
4260 break;
4261#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004262#if defined(FEAT_PROFILE)
4263 case CMD_syntime:
4264 xp->xp_context = EXPAND_SYNTIME;
4265 xp->xp_pattern = arg;
4266 break;
4267#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004268
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269#endif /* FEAT_CMDL_COMPL */
4270
4271 default:
4272 break;
4273 }
4274 return NULL;
4275}
4276
4277/*
4278 * skip a range specifier of the form: addr [,addr] [;addr] ..
4279 *
4280 * Backslashed delimiters after / or ? will be skipped, and commands will
4281 * not be expanded between /'s and ?'s or after "'".
4282 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004283 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 * Returns the "cmd" pointer advanced to beyond the range.
4285 */
4286 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004287skip_range(
4288 char_u *cmd,
4289 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004291 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292
Bram Moolenaardf177f62005-02-22 08:39:57 +00004293 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 {
4295 if (*cmd == '\'')
4296 {
4297 if (*++cmd == NUL && ctx != NULL)
4298 *ctx = EXPAND_NOTHING;
4299 }
4300 else if (*cmd == '/' || *cmd == '?')
4301 {
4302 delim = *cmd++;
4303 while (*cmd != NUL && *cmd != delim)
4304 if (*cmd++ == '\\' && *cmd != NUL)
4305 ++cmd;
4306 if (*cmd == NUL && ctx != NULL)
4307 *ctx = EXPAND_NOTHING;
4308 }
4309 if (*cmd != NUL)
4310 ++cmd;
4311 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004312
4313 /* Skip ":" and white space. */
4314 while (*cmd == ':')
4315 cmd = skipwhite(cmd + 1);
4316
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 return cmd;
4318}
4319
4320/*
4321 * get a single EX address
4322 *
4323 * Set ptr to the next character after the part that was interpreted.
4324 * Set ptr to NULL when an error is encountered.
4325 *
4326 * Return MAXLNUM when no Ex address was found.
4327 */
4328 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004329get_address(
4330 exarg_T *eap UNUSED,
4331 char_u **ptr,
4332 int addr_type, /* flag: one of ADDR_LINES, ... */
4333 int skip, /* only skip the address, don't use it */
4334 int to_other_file) /* flag: may jump to other file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335{
4336 int c;
4337 int i;
4338 long n;
4339 char_u *cmd;
4340 pos_T pos;
4341 pos_T *fp;
4342 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004343 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344
4345 cmd = skipwhite(*ptr);
4346 lnum = MAXLNUM;
4347 do
4348 {
4349 switch (*cmd)
4350 {
4351 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004352 ++cmd;
4353 switch (addr_type)
4354 {
4355 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 lnum = curwin->w_cursor.lnum;
4357 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004358 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004359 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004360 break;
4361 case ADDR_ARGUMENTS:
4362 lnum = curwin->w_arg_idx + 1;
4363 break;
4364 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004365 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004366 lnum = curbuf->b_fnum;
4367 break;
4368 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004369 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004370 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004371#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004372 case ADDR_QUICKFIX:
4373 lnum = qf_get_cur_valid_idx(eap);
4374 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004375#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004376 }
4377 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378
4379 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004380 ++cmd;
4381 switch (addr_type)
4382 {
4383 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 lnum = curbuf->b_ml.ml_line_count;
4385 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004386 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004387 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004388 break;
4389 case ADDR_ARGUMENTS:
4390 lnum = ARGCOUNT;
4391 break;
4392 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004393 buf = lastbuf;
4394 while (buf->b_ml.ml_mfp == NULL)
4395 {
4396 if (buf->b_prev == NULL)
4397 break;
4398 buf = buf->b_prev;
4399 }
4400 lnum = buf->b_fnum;
4401 break;
4402 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004403 lnum = lastbuf->b_fnum;
4404 break;
4405 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004406 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004407 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004408#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004409 case ADDR_QUICKFIX:
4410 lnum = qf_get_size(eap);
4411 if (lnum == 0)
4412 lnum = 1;
4413 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004414#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004415 }
4416 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417
4418 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004419 if (*++cmd == NUL)
4420 {
4421 cmd = NULL;
4422 goto error;
4423 }
4424 if (addr_type != ADDR_LINES)
4425 {
4426 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004427 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004428 goto error;
4429 }
4430 if (skip)
4431 ++cmd;
4432 else
4433 {
4434 /* Only accept a mark in another file when it is
4435 * used by itself: ":'M". */
4436 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4437 ++cmd;
4438 if (fp == (pos_T *)-1)
4439 /* Jumped to another file. */
4440 lnum = curwin->w_cursor.lnum;
4441 else
4442 {
4443 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 {
4445 cmd = NULL;
4446 goto error;
4447 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004448 lnum = fp->lnum;
4449 }
4450 }
4451 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452
4453 case '/':
4454 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004455 c = *cmd++;
4456 if (addr_type != ADDR_LINES)
4457 {
4458 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004459 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004460 goto error;
4461 }
4462 if (skip) /* skip "/pat/" */
4463 {
4464 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4465 if (*cmd == c)
4466 ++cmd;
4467 }
4468 else
4469 {
4470 pos = curwin->w_cursor; /* save curwin->w_cursor */
4471 /*
4472 * When '/' or '?' follows another address, start
4473 * from there.
4474 */
4475 if (lnum != MAXLNUM)
4476 curwin->w_cursor.lnum = lnum;
4477 /*
4478 * Start a forward search at the end of the line.
4479 * Start a backward search at the start of the line.
4480 * This makes sure we never match in the current
4481 * line, and can match anywhere in the
4482 * next/previous line.
4483 */
4484 if (c == '/')
4485 curwin->w_cursor.col = MAXCOL;
4486 else
4487 curwin->w_cursor.col = 0;
4488 searchcmdlen = 0;
4489 if (!do_search(NULL, c, cmd, 1L,
4490 SEARCH_HIS | SEARCH_MSG, NULL))
4491 {
4492 curwin->w_cursor = pos;
4493 cmd = NULL;
4494 goto error;
4495 }
4496 lnum = curwin->w_cursor.lnum;
4497 curwin->w_cursor = pos;
4498 /* adjust command string pointer */
4499 cmd += searchcmdlen;
4500 }
4501 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502
4503 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004504 ++cmd;
4505 if (addr_type != ADDR_LINES)
4506 {
4507 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004508 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004509 goto error;
4510 }
4511 if (*cmd == '&')
4512 i = RE_SUBST;
4513 else if (*cmd == '?' || *cmd == '/')
4514 i = RE_SEARCH;
4515 else
4516 {
4517 EMSG(_(e_backslash));
4518 cmd = NULL;
4519 goto error;
4520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004522 if (!skip)
4523 {
4524 /*
4525 * When search follows another address, start from
4526 * there.
4527 */
4528 if (lnum != MAXLNUM)
4529 pos.lnum = lnum;
4530 else
4531 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004533 /*
4534 * Start the search just like for the above
4535 * do_search().
4536 */
4537 if (*cmd != '?')
4538 pos.col = MAXCOL;
4539 else
4540 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004541#ifdef FEAT_VIRTUALEDIT
4542 pos.coladd = 0;
4543#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004544 if (searchit(curwin, curbuf, &pos,
4545 *cmd == '?' ? BACKWARD : FORWARD,
4546 (char_u *)"", 1L, SEARCH_MSG,
4547 i, (linenr_T)0, NULL) != FAIL)
4548 lnum = pos.lnum;
4549 else
4550 {
4551 cmd = NULL;
4552 goto error;
4553 }
4554 }
4555 ++cmd;
4556 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557
4558 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004559 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4560 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 }
4562
4563 for (;;)
4564 {
4565 cmd = skipwhite(cmd);
4566 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4567 break;
4568
4569 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004570 {
4571 switch (addr_type)
4572 {
4573 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004574 /* "+1" is same as ".+1" */
4575 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004576 break;
4577 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004578 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004579 break;
4580 case ADDR_ARGUMENTS:
4581 lnum = curwin->w_arg_idx + 1;
4582 break;
4583 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004584 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004585 lnum = curbuf->b_fnum;
4586 break;
4587 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004588 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004589 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004590#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004591 case ADDR_QUICKFIX:
4592 lnum = qf_get_cur_valid_idx(eap);
4593 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004594#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004595 }
4596 }
4597
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 if (VIM_ISDIGIT(*cmd))
4599 i = '+'; /* "number" is same as "+number" */
4600 else
4601 i = *cmd++;
4602 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4603 n = 1;
4604 else
4605 n = getdigits(&cmd);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004606 if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004607 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004608 lnum = compute_buffer_local_count(
4609 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004610 else if (i == '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 lnum -= n;
4612 else
4613 lnum += n;
4614 }
4615 } while (*cmd == '/' || *cmd == '?');
4616
4617error:
4618 *ptr = cmd;
4619 return lnum;
4620}
4621
4622/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004623 * Get flags from an Ex command argument.
4624 */
4625 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004626get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004627{
4628 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4629 {
4630 if (*eap->arg == 'l')
4631 eap->flags |= EXFLAG_LIST;
4632 else if (*eap->arg == 'p')
4633 eap->flags |= EXFLAG_PRINT;
4634 else
4635 eap->flags |= EXFLAG_NR;
4636 eap->arg = skipwhite(eap->arg + 1);
4637 }
4638}
4639
4640/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 * Function called for command which is Not Implemented. NI!
4642 */
4643 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004644ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645{
4646 if (!eap->skip)
4647 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4648}
4649
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004650#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651/*
4652 * Function called for script command which is Not Implemented. NI!
4653 * Skips over ":perl <<EOF" constructs.
4654 */
4655 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004656ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657{
4658 if (!eap->skip)
4659 ex_ni(eap);
4660 else
4661 vim_free(script_get(eap, eap->arg));
4662}
4663#endif
4664
4665/*
4666 * Check range in Ex command for validity.
4667 * Return NULL when valid, error message when invalid.
4668 */
4669 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004670invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004672 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 if ( eap->line1 < 0
4674 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004675 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004677
4678 if (eap->argt & RANGE)
4679 {
4680 switch(eap->addr_type)
4681 {
4682 case ADDR_LINES:
4683 if (!(eap->argt & NOTADR)
4684 && eap->line2 > curbuf->b_ml.ml_line_count
4685#ifdef FEAT_DIFF
4686 + (eap->cmdidx == CMD_diffget)
4687#endif
4688 )
4689 return (char_u *)_(e_invrange);
4690 break;
4691 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004692 /* add 1 if ARGCOUNT is 0 */
4693 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004694 return (char_u *)_(e_invrange);
4695 break;
4696 case ADDR_BUFFERS:
4697 if (eap->line1 < firstbuf->b_fnum
4698 || eap->line2 > lastbuf->b_fnum)
4699 return (char_u *)_(e_invrange);
4700 break;
4701 case ADDR_LOADED_BUFFERS:
4702 buf = firstbuf;
4703 while (buf->b_ml.ml_mfp == NULL)
4704 {
4705 if (buf->b_next == NULL)
4706 return (char_u *)_(e_invrange);
4707 buf = buf->b_next;
4708 }
4709 if (eap->line1 < buf->b_fnum)
4710 return (char_u *)_(e_invrange);
4711 buf = lastbuf;
4712 while (buf->b_ml.ml_mfp == NULL)
4713 {
4714 if (buf->b_prev == NULL)
4715 return (char_u *)_(e_invrange);
4716 buf = buf->b_prev;
4717 }
4718 if (eap->line2 > buf->b_fnum)
4719 return (char_u *)_(e_invrange);
4720 break;
4721 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004722 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004723 return (char_u *)_(e_invrange);
4724 break;
4725 case ADDR_TABS:
4726 if (eap->line2 > LAST_TAB_NR)
4727 return (char_u *)_(e_invrange);
4728 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004729#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004730 case ADDR_QUICKFIX:
4731 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4732 return (char_u *)_(e_invrange);
4733 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004734#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004735 }
4736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 return NULL;
4738}
4739
4740/*
4741 * Correct the range for zero line number, if required.
4742 */
4743 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004744correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745{
4746 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4747 {
4748 if (eap->line1 == 0)
4749 eap->line1 = 1;
4750 if (eap->line2 == 0)
4751 eap->line2 = 1;
4752 }
4753}
4754
Bram Moolenaar748bf032005-02-02 23:04:36 +00004755#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004756static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004757
4758/*
4759 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4760 * pattern. Otherwise return eap->arg.
4761 */
4762 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004763skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004764{
4765 char_u *p = eap->arg;
4766
Bram Moolenaara37420f2006-02-04 22:37:47 +00004767 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4768 || eap->cmdidx == CMD_vimgrepadd
4769 || eap->cmdidx == CMD_lvimgrepadd
4770 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004771 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004772 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004773 if (p == NULL)
4774 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004775 }
4776 return p;
4777}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004778
4779/*
4780 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4781 * in the command line, so that things like % get expanded.
4782 */
4783 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004784replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004785{
4786 char_u *new_cmdline;
4787 char_u *program;
4788 char_u *pos;
4789 char_u *ptr;
4790 int len;
4791 int i;
4792
4793 /*
4794 * Don't do it when ":vimgrep" is used for ":grep".
4795 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004796 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4797 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4798 || eap->cmdidx == CMD_grepadd
4799 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004800 && !grep_internal(eap->cmdidx))
4801 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004802 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4803 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004804 {
4805 if (*curbuf->b_p_gp == NUL)
4806 program = p_gp;
4807 else
4808 program = curbuf->b_p_gp;
4809 }
4810 else
4811 {
4812 if (*curbuf->b_p_mp == NUL)
4813 program = p_mp;
4814 else
4815 program = curbuf->b_p_mp;
4816 }
4817
4818 p = skipwhite(p);
4819
4820 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4821 {
4822 /* replace $* by given arguments */
4823 i = 1;
4824 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4825 ++i;
4826 len = (int)STRLEN(p);
4827 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4828 if (new_cmdline == NULL)
4829 return NULL; /* out of memory */
4830 ptr = new_cmdline;
4831 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4832 {
4833 i = (int)(pos - program);
4834 STRNCPY(ptr, program, i);
4835 STRCPY(ptr += i, p);
4836 ptr += len;
4837 program = pos + 2;
4838 }
4839 STRCPY(ptr, program);
4840 }
4841 else
4842 {
4843 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4844 if (new_cmdline == NULL)
4845 return NULL; /* out of memory */
4846 STRCPY(new_cmdline, program);
4847 STRCAT(new_cmdline, " ");
4848 STRCAT(new_cmdline, p);
4849 }
4850 msg_make(p);
4851
4852 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4853 vim_free(*cmdlinep);
4854 *cmdlinep = new_cmdline;
4855 p = new_cmdline;
4856 }
4857 return p;
4858}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004859#endif
4860
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861/*
4862 * Expand file name in Ex command argument.
4863 * Return FAIL for failure, OK otherwise.
4864 */
4865 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004866expand_filename(
4867 exarg_T *eap,
4868 char_u **cmdlinep,
4869 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870{
4871 int has_wildcards; /* need to expand wildcards */
4872 char_u *repl;
4873 int srclen;
4874 char_u *p;
4875 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004876 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877
Bram Moolenaar748bf032005-02-02 23:04:36 +00004878#ifdef FEAT_QUICKFIX
4879 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4880 p = skip_grep_pat(eap);
4881#else
4882 p = eap->arg;
4883#endif
4884
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 /*
4886 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4887 * the file name contains a wildcard it should not cause expanding.
4888 * (it will be expanded anyway if there is a wildcard before replacing).
4889 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004890 has_wildcards = mch_has_wildcard(p);
4891 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004893#ifdef FEAT_EVAL
4894 /* Skip over `=expr`, wildcards in it are not expanded. */
4895 if (p[0] == '`' && p[1] == '=')
4896 {
4897 p += 2;
4898 (void)skip_expr(&p);
4899 if (*p == '`')
4900 ++p;
4901 continue;
4902 }
4903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 /*
4905 * Quick check if this cannot be the start of a special string.
4906 * Also removes backslash before '%', '#' and '<'.
4907 */
4908 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4909 {
4910 ++p;
4911 continue;
4912 }
4913
4914 /*
4915 * Try to find a match at this position.
4916 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004917 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4918 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 if (*errormsgp != NULL) /* error detected */
4920 return FAIL;
4921 if (repl == NULL) /* no match found */
4922 {
4923 p += srclen;
4924 continue;
4925 }
4926
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004927 /* Wildcards won't be expanded below, the replacement is taken
4928 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4929 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4930 {
4931 char_u *l = repl;
4932
4933 repl = expand_env_save(repl);
4934 vim_free(l);
4935 }
4936
Bram Moolenaar63b92542007-03-27 14:57:09 +00004937 /* Need to escape white space et al. with a backslash.
4938 * Don't do this for:
4939 * - replacement that already has been escaped: "##"
4940 * - shell commands (may have to use quotes instead).
4941 * - non-unix systems when there is a single argument (spaces don't
4942 * separate arguments then).
4943 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004945 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 && eap->cmdidx != CMD_bang
4947 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004948 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004950 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004952 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953#ifndef UNIX
4954 && !(eap->argt & NOSPC)
4955#endif
4956 )
4957 {
4958 char_u *l;
4959#ifdef BACKSLASH_IN_FILENAME
4960 /* Don't escape a backslash here, because rem_backslash() doesn't
4961 * remove it later. */
4962 static char_u *nobslash = (char_u *)" \t\"|";
4963# define ESCAPE_CHARS nobslash
4964#else
4965# define ESCAPE_CHARS escape_chars
4966#endif
4967
4968 for (l = repl; *l; ++l)
4969 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4970 {
4971 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4972 if (l != NULL)
4973 {
4974 vim_free(repl);
4975 repl = l;
4976 }
4977 break;
4978 }
4979 }
4980
4981 /* For a shell command a '!' must be escaped. */
4982 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004983 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 {
4985 char_u *l;
4986
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004987 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 if (l != NULL)
4989 {
4990 vim_free(repl);
4991 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 }
4993 }
4994
4995 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4996 vim_free(repl);
4997 if (p == NULL)
4998 return FAIL;
4999 }
5000
5001 /*
5002 * One file argument: Expand wildcards.
5003 * Don't do this with ":r !command" or ":w !command".
5004 */
5005 if ((eap->argt & NOSPC) && !eap->usefilter)
5006 {
5007 /*
5008 * May do this twice:
5009 * 1. Replace environment variables.
5010 * 2. Replace any other wildcards, remove backslashes.
5011 */
5012 for (n = 1; n <= 2; ++n)
5013 {
5014 if (n == 2)
5015 {
5016#ifdef UNIX
5017 /*
5018 * Only for Unix we check for more than one file name.
5019 * For other systems spaces are considered to be part
5020 * of the file name.
5021 * Only check here if there is no wildcard, otherwise
5022 * ExpandOne() will check for errors. This allows
5023 * ":e `ls ve*.c`" on Unix.
5024 */
5025 if (!has_wildcards)
5026 for (p = eap->arg; *p; ++p)
5027 {
5028 /* skip escaped characters */
5029 if (p[1] && (*p == '\\' || *p == Ctrl_V))
5030 ++p;
5031 else if (vim_iswhite(*p))
5032 {
5033 *errormsgp = (char_u *)_("E172: Only one file name allowed");
5034 return FAIL;
5035 }
5036 }
5037#endif
5038
5039 /*
5040 * Halve the number of backslashes (this is Vi compatible).
5041 * For Unix and OS/2, when wildcards are expanded, this is
5042 * done by ExpandOne() below.
5043 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005044#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 if (!has_wildcards)
5046#endif
5047 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 }
5049
5050 if (has_wildcards)
5051 {
5052 if (n == 1)
5053 {
5054 /*
5055 * First loop: May expand environment variables. This
5056 * can be done much faster with expand_env() than with
5057 * something else (e.g., calling a shell).
5058 * After expanding environment variables, check again
5059 * if there are still wildcards present.
5060 */
5061 if (vim_strchr(eap->arg, '$') != NULL
5062 || vim_strchr(eap->arg, '~') != NULL)
5063 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005064 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005065 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 has_wildcards = mch_has_wildcard(NameBuff);
5067 p = NameBuff;
5068 }
5069 else
5070 p = NULL;
5071 }
5072 else /* n == 2 */
5073 {
5074 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005075 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076
5077 ExpandInit(&xpc);
5078 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005079 if (p_wic)
5080 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005082 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 if (p == NULL)
5084 return FAIL;
5085 }
5086 if (p != NULL)
5087 {
5088 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5089 p, cmdlinep);
5090 if (n == 2) /* p came from ExpandOne() */
5091 vim_free(p);
5092 }
5093 }
5094 }
5095 }
5096 return OK;
5097}
5098
5099/*
5100 * Replace part of the command line, keeping eap->cmd, eap->arg and
5101 * eap->nextcmd correct.
5102 * "src" points to the part that is to be replaced, of length "srclen".
5103 * "repl" is the replacement string.
5104 * Returns a pointer to the character after the replaced string.
5105 * Returns NULL for failure.
5106 */
5107 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005108repl_cmdline(
5109 exarg_T *eap,
5110 char_u *src,
5111 int srclen,
5112 char_u *repl,
5113 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114{
5115 int len;
5116 int i;
5117 char_u *new_cmdline;
5118
5119 /*
5120 * The new command line is build in new_cmdline[].
5121 * First allocate it.
5122 * Careful: a "+cmd" argument may have been NUL terminated.
5123 */
5124 len = (int)STRLEN(repl);
5125 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005126 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5128 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5129 return NULL; /* out of memory! */
5130
5131 /*
5132 * Copy the stuff before the expanded part.
5133 * Copy the expanded stuff.
5134 * Copy what came after the expanded part.
5135 * Copy the next commands, if there are any.
5136 */
5137 i = (int)(src - *cmdlinep); /* length of part before match */
5138 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005139
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 mch_memmove(new_cmdline + i, repl, (size_t)len);
5141 i += len; /* remember the end of the string */
5142 STRCPY(new_cmdline + i, src + srclen);
5143 src = new_cmdline + i; /* remember where to continue */
5144
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005145 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 {
5147 i = (int)STRLEN(new_cmdline) + 1;
5148 STRCPY(new_cmdline + i, eap->nextcmd);
5149 eap->nextcmd = new_cmdline + i;
5150 }
5151 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5152 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5153 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5154 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5155 vim_free(*cmdlinep);
5156 *cmdlinep = new_cmdline;
5157
5158 return src;
5159}
5160
5161/*
5162 * Check for '|' to separate commands and '"' to start comments.
5163 */
5164 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005165separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166{
5167 char_u *p;
5168
Bram Moolenaar86b68352004-12-27 21:59:20 +00005169#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005170 p = skip_grep_pat(eap);
5171#else
5172 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005173#endif
5174
5175 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 {
5177 if (*p == Ctrl_V)
5178 {
5179 if (eap->argt & (USECTRLV | XFILE))
5180 ++p; /* skip CTRL-V and next char */
5181 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005182 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005183 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 if (*p == NUL) /* stop at NUL after CTRL-V */
5185 break;
5186 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005187
5188#ifdef FEAT_EVAL
5189 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005190 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005191 {
5192 p += 2;
5193 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005194 }
5195#endif
5196
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 /* Check for '"': start of comment or '|': next command */
5198 /* :@" and :*" do not start a comment!
5199 * :redir @" doesn't either. */
5200 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5201 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5202 || p != eap->arg)
5203 && (eap->cmdidx != CMD_redir
5204 || p != eap->arg + 1 || p[-1] != '@'))
5205 || *p == '|' || *p == '\n')
5206 {
5207 /*
5208 * We remove the '\' before the '|', unless USECTRLV is used
5209 * AND 'b' is present in 'cpoptions'.
5210 */
5211 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5212 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5213 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005214 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 --p;
5216 }
5217 else
5218 {
5219 eap->nextcmd = check_nextcmd(p);
5220 *p = NUL;
5221 break;
5222 }
5223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005225
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5227 del_trailing_spaces(eap->arg);
5228}
5229
5230/*
5231 * get + command from ex argument
5232 */
5233 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005234getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235{
5236 char_u *arg = *argp;
5237 char_u *command = NULL;
5238
5239 if (*arg == '+') /* +[command] */
5240 {
5241 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005242 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 command = dollar_command;
5244 else
5245 {
5246 command = arg;
5247 arg = skip_cmd_arg(command, TRUE);
5248 if (*arg != NUL)
5249 *arg++ = NUL; /* terminate command with NUL */
5250 }
5251
5252 arg = skipwhite(arg); /* skip over spaces */
5253 *argp = arg;
5254 }
5255 return command;
5256}
5257
5258/*
5259 * Find end of "+command" argument. Skip over "\ " and "\\".
5260 */
5261 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005262skip_cmd_arg(
5263 char_u *p,
5264 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265{
5266 while (*p && !vim_isspace(*p))
5267 {
5268 if (*p == '\\' && p[1] != NUL)
5269 {
5270 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005271 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 else
5273 ++p;
5274 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005275 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 }
5277 return p;
5278}
5279
5280/*
5281 * Get "++opt=arg" argument.
5282 * Return FAIL or OK.
5283 */
5284 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005285getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286{
5287 char_u *arg = eap->arg + 2;
5288 int *pp = NULL;
5289#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005290 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 char_u *p;
5292#endif
5293
5294 /* ":edit ++[no]bin[ary] file" */
5295 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5296 {
5297 if (*arg == 'n')
5298 {
5299 arg += 2;
5300 eap->force_bin = FORCE_NOBIN;
5301 }
5302 else
5303 eap->force_bin = FORCE_BIN;
5304 if (!checkforcmd(&arg, "binary", 3))
5305 return FAIL;
5306 eap->arg = skipwhite(arg);
5307 return OK;
5308 }
5309
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005310 /* ":read ++edit file" */
5311 if (STRNCMP(arg, "edit", 4) == 0)
5312 {
5313 eap->read_edit = TRUE;
5314 eap->arg = skipwhite(arg + 4);
5315 return OK;
5316 }
5317
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 if (STRNCMP(arg, "ff", 2) == 0)
5319 {
5320 arg += 2;
5321 pp = &eap->force_ff;
5322 }
5323 else if (STRNCMP(arg, "fileformat", 10) == 0)
5324 {
5325 arg += 10;
5326 pp = &eap->force_ff;
5327 }
5328#ifdef FEAT_MBYTE
5329 else if (STRNCMP(arg, "enc", 3) == 0)
5330 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005331 if (STRNCMP(arg, "encoding", 8) == 0)
5332 arg += 8;
5333 else
5334 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 pp = &eap->force_enc;
5336 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005337 else if (STRNCMP(arg, "bad", 3) == 0)
5338 {
5339 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005340 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342#endif
5343
5344 if (pp == NULL || *arg != '=')
5345 return FAIL;
5346
5347 ++arg;
5348 *pp = (int)(arg - eap->cmd);
5349 arg = skip_cmd_arg(arg, FALSE);
5350 eap->arg = skipwhite(arg);
5351 *arg = NUL;
5352
5353#ifdef FEAT_MBYTE
5354 if (pp == &eap->force_ff)
5355 {
5356#endif
5357 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5358 return FAIL;
5359#ifdef FEAT_MBYTE
5360 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005361 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 {
5363 /* Make 'fileencoding' lower case. */
5364 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5365 *p = TOLOWER_ASC(*p);
5366 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005367 else
5368 {
5369 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5370 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005371 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005372 if (STRICMP(p, "keep") == 0)
5373 eap->bad_char = BAD_KEEP;
5374 else if (STRICMP(p, "drop") == 0)
5375 eap->bad_char = BAD_DROP;
5376 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5377 eap->bad_char = *p;
5378 else
5379 return FAIL;
5380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381#endif
5382
5383 return OK;
5384}
5385
5386/*
5387 * ":abbreviate" and friends.
5388 */
5389 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005390ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391{
5392 do_exmap(eap, TRUE); /* almost the same as mapping */
5393}
5394
5395/*
5396 * ":map" and friends.
5397 */
5398 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005399ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400{
5401 /*
5402 * If we are sourcing .exrc or .vimrc in current directory we
5403 * print the mappings for security reasons.
5404 */
5405 if (secure)
5406 {
5407 secure = 2;
5408 msg_outtrans(eap->cmd);
5409 msg_putchar('\n');
5410 }
5411 do_exmap(eap, FALSE);
5412}
5413
5414/*
5415 * ":unmap" and friends.
5416 */
5417 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005418ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419{
5420 do_exmap(eap, FALSE);
5421}
5422
5423/*
5424 * ":mapclear" and friends.
5425 */
5426 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005427ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428{
5429 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5430}
5431
5432/*
5433 * ":abclear" and friends.
5434 */
5435 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005436ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437{
5438 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5439}
5440
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005441#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005443ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444{
5445 /*
5446 * Disallow auto commands from .exrc and .vimrc in current
5447 * directory for security reasons.
5448 */
5449 if (secure)
5450 {
5451 secure = 2;
5452 eap->errmsg = e_curdir;
5453 }
5454 else if (eap->cmdidx == CMD_autocmd)
5455 do_autocmd(eap->arg, eap->forceit);
5456 else
5457 do_augroup(eap->arg, eap->forceit);
5458}
5459
5460/*
5461 * ":doautocmd": Apply the automatic commands to the current buffer.
5462 */
5463 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005464ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005466 char_u *arg = eap->arg;
5467 int call_do_modelines = check_nomodeline(&arg);
5468
5469 (void)do_doautocmd(arg, TRUE);
5470 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5471 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472}
5473#endif
5474
5475#ifdef FEAT_LISTCMDS
5476/*
5477 * :[N]bunload[!] [N] [bufname] unload buffer
5478 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5479 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5480 */
5481 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005482ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483{
5484 eap->errmsg = do_bufdel(
5485 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5486 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5487 : DOBUF_UNLOAD, eap->arg,
5488 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5489}
5490
5491/*
5492 * :[N]buffer [N] to buffer N
5493 * :[N]sbuffer [N] to buffer N
5494 */
5495 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005496ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497{
5498 if (*eap->arg)
5499 eap->errmsg = e_trailing;
5500 else
5501 {
5502 if (eap->addr_count == 0) /* default is current buffer */
5503 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5504 else
5505 goto_buffer(eap, DOBUF_FIRST, 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/*
5512 * :[N]bmodified [N] to next mod. buffer
5513 * :[N]sbmodified [N] to next mod. buffer
5514 */
5515 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005516ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517{
5518 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005519 if (eap->do_ecmd_cmd != NULL)
5520 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521}
5522
5523/*
5524 * :[N]bnext [N] to next buffer
5525 * :[N]sbnext [N] split and to next buffer
5526 */
5527 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005528ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529{
5530 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005531 if (eap->do_ecmd_cmd != NULL)
5532 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533}
5534
5535/*
5536 * :[N]bNext [N] to previous buffer
5537 * :[N]bprevious [N] to previous buffer
5538 * :[N]sbNext [N] split and to previous buffer
5539 * :[N]sbprevious [N] split and to previous buffer
5540 */
5541 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005542ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543{
5544 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005545 if (eap->do_ecmd_cmd != NULL)
5546 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547}
5548
5549/*
5550 * :brewind to first buffer
5551 * :bfirst to first buffer
5552 * :sbrewind split and to first buffer
5553 * :sbfirst split and to first buffer
5554 */
5555 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005556ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557{
5558 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005559 if (eap->do_ecmd_cmd != NULL)
5560 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561}
5562
5563/*
5564 * :blast to last buffer
5565 * :sblast split and to last buffer
5566 */
5567 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005568ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569{
5570 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005571 if (eap->do_ecmd_cmd != NULL)
5572 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573}
5574#endif
5575
5576 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005577ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578{
5579 return (c == NUL || c == '|' || c == '"' || c == '\n');
5580}
5581
5582#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5583 || defined(PROTO)
5584/*
5585 * Return the next command, after the first '|' or '\n'.
5586 * Return NULL if not found.
5587 */
5588 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005589find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590{
5591 while (*p != '|' && *p != '\n')
5592 {
5593 if (*p == NUL)
5594 return NULL;
5595 ++p;
5596 }
5597 return (p + 1);
5598}
5599#endif
5600
5601/*
5602 * Check if *p is a separator between Ex commands.
5603 * Return NULL if it isn't, (p + 1) if it is.
5604 */
5605 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005606check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607{
5608 p = skipwhite(p);
5609 if (*p == '|' || *p == '\n')
5610 return (p + 1);
5611 else
5612 return NULL;
5613}
5614
5615/*
5616 * - if there are more files to edit
5617 * - and this is the last window
5618 * - and forceit not used
5619 * - and not repeated twice on a row
5620 * return FAIL and give error message if 'message' TRUE
5621 * return OK otherwise
5622 */
5623 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005624check_more(
5625 int message, /* when FALSE check only, no messages */
5626 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627{
5628 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5629
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005630 if (!forceit && only_one_window()
5631 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 {
5633 if (message)
5634 {
5635#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5636 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5637 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005638 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639
5640 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005641 vim_strncpy(buff,
5642 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005643 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005645 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 _("%d more files to edit. Quit anyway?"), n);
5647 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5648 return OK;
5649 return FAIL;
5650 }
5651#endif
5652 if (n == 1)
5653 EMSG(_("E173: 1 more file to edit"));
5654 else
5655 EMSGN(_("E173: %ld more files to edit"), n);
5656 quitmore = 2; /* next try to quit is allowed */
5657 }
5658 return FAIL;
5659 }
5660 return OK;
5661}
5662
5663#ifdef FEAT_CMDL_COMPL
5664/*
5665 * Function given to ExpandGeneric() to obtain the list of command names.
5666 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005668get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669{
5670 if (idx >= (int)CMD_SIZE)
5671# ifdef FEAT_USR_CMDS
5672 return get_user_command_name(idx);
5673# else
5674 return NULL;
5675# endif
5676 return cmdnames[idx].cmd_name;
5677}
5678#endif
5679
5680#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005681static 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);
5682static void uc_list(char_u *name, size_t name_len);
5683static 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);
5684static char_u *uc_split_args(char_u *arg, size_t *lenp);
5685static 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 +00005686
5687 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005688uc_add_command(
5689 char_u *name,
5690 size_t name_len,
5691 char_u *rep,
5692 long argt,
5693 long def,
5694 int flags,
5695 int compl,
5696 char_u *compl_arg,
5697 int addr_type,
5698 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699{
5700 ucmd_T *cmd = NULL;
5701 char_u *p;
5702 int i;
5703 int cmp = 1;
5704 char_u *rep_buf = NULL;
5705 garray_T *gap;
5706
Bram Moolenaar9c102382006-05-03 21:26:49 +00005707 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 if (rep_buf == NULL)
5709 {
5710 /* Can't replace termcodes - try using the string as is */
5711 rep_buf = vim_strsave(rep);
5712
5713 /* Give up if out of memory */
5714 if (rep_buf == NULL)
5715 return FAIL;
5716 }
5717
5718 /* get address of growarray: global or in curbuf */
5719 if (flags & UC_BUFFER)
5720 {
5721 gap = &curbuf->b_ucmds;
5722 if (gap->ga_itemsize == 0)
5723 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5724 }
5725 else
5726 gap = &ucmds;
5727
5728 /* Search for the command in the already defined commands. */
5729 for (i = 0; i < gap->ga_len; ++i)
5730 {
5731 size_t len;
5732
5733 cmd = USER_CMD_GA(gap, i);
5734 len = STRLEN(cmd->uc_name);
5735 cmp = STRNCMP(name, cmd->uc_name, name_len);
5736 if (cmp == 0)
5737 {
5738 if (name_len < len)
5739 cmp = -1;
5740 else if (name_len > len)
5741 cmp = 1;
5742 }
5743
5744 if (cmp == 0)
5745 {
5746 if (!force)
5747 {
5748 EMSG(_("E174: Command already exists: add ! to replace it"));
5749 goto fail;
5750 }
5751
5752 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005753 cmd->uc_rep = NULL;
5754#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5755 vim_free(cmd->uc_compl_arg);
5756 cmd->uc_compl_arg = NULL;
5757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 break;
5759 }
5760
5761 /* Stop as soon as we pass the name to add */
5762 if (cmp < 0)
5763 break;
5764 }
5765
5766 /* Extend the array unless we're replacing an existing command */
5767 if (cmp != 0)
5768 {
5769 if (ga_grow(gap, 1) != OK)
5770 goto fail;
5771 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5772 goto fail;
5773
5774 cmd = USER_CMD_GA(gap, i);
5775 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5776
5777 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778
5779 cmd->uc_name = p;
5780 }
5781
5782 cmd->uc_rep = rep_buf;
5783 cmd->uc_argt = argt;
5784 cmd->uc_def = def;
5785 cmd->uc_compl = compl;
5786#ifdef FEAT_EVAL
5787 cmd->uc_scriptID = current_SID;
5788# ifdef FEAT_CMDL_COMPL
5789 cmd->uc_compl_arg = compl_arg;
5790# endif
5791#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005792 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793
5794 return OK;
5795
5796fail:
5797 vim_free(rep_buf);
5798#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5799 vim_free(compl_arg);
5800#endif
5801 return FAIL;
5802}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005805#if defined(FEAT_USR_CMDS)
5806static struct
5807{
5808 int expand;
5809 char *name;
5810} addr_type_complete[] =
5811{
5812 {ADDR_ARGUMENTS, "arguments"},
5813 {ADDR_LINES, "lines"},
5814 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5815 {ADDR_TABS, "tabs"},
5816 {ADDR_BUFFERS, "buffers"},
5817 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005818 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005819 {-1, NULL}
5820};
5821#endif
5822
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005823#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824/*
5825 * List of names for completion for ":command" with the EXPAND_ flag.
5826 * Must be alphabetical for completion.
5827 */
5828static struct
5829{
5830 int expand;
5831 char *name;
5832} command_complete[] =
5833{
5834 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005835 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005837 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005839 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005840#if defined(FEAT_CSCOPE)
5841 {EXPAND_CSCOPE, "cscope"},
5842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5844 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005845 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846#endif
5847 {EXPAND_DIRECTORIES, "dir"},
5848 {EXPAND_ENV_VARS, "environment"},
5849 {EXPAND_EVENTS, "event"},
5850 {EXPAND_EXPRESSION, "expression"},
5851 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005852 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005853 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 {EXPAND_FUNCTIONS, "function"},
5855 {EXPAND_HELP, "help"},
5856 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005857#if defined(FEAT_CMDHIST)
5858 {EXPAND_HISTORY, "history"},
5859#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005860#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005861 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005862 {EXPAND_LOCALES, "locale"},
5863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 {EXPAND_MAPPINGS, "mapping"},
5865 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005866 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005867#if defined(FEAT_PROFILE)
5868 {EXPAND_SYNTIME, "syntime"},
5869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005871 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005872#if defined(FEAT_SIGNS)
5873 {EXPAND_SIGN, "sign"},
5874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 {EXPAND_TAGS, "tag"},
5876 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005877 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 {EXPAND_USER_VARS, "var"},
5879 {0, NULL}
5880};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005883#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005885uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886{
5887 int i, j;
5888 int found = FALSE;
5889 ucmd_T *cmd;
5890 int len;
5891 long a;
5892 garray_T *gap;
5893
5894 gap = &curbuf->b_ucmds;
5895 for (;;)
5896 {
5897 for (i = 0; i < gap->ga_len; ++i)
5898 {
5899 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005900 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901
5902 /* Skip commands which don't match the requested prefix */
5903 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5904 continue;
5905
5906 /* Put out the title first time */
5907 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005908 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 found = TRUE;
5910 msg_putchar('\n');
5911 if (got_int)
5912 break;
5913
5914 /* Special cases */
5915 msg_putchar(a & BANG ? '!' : ' ');
5916 msg_putchar(a & REGSTR ? '"' : ' ');
5917 msg_putchar(gap != &ucmds ? 'b' : ' ');
5918 msg_putchar(' ');
5919
5920 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5921 len = (int)STRLEN(cmd->uc_name) + 4;
5922
5923 do {
5924 msg_putchar(' ');
5925 ++len;
5926 } while (len < 16);
5927
5928 len = 0;
5929
5930 /* Arguments */
5931 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5932 {
5933 case 0: IObuff[len++] = '0'; break;
5934 case (EXTRA): IObuff[len++] = '*'; break;
5935 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5936 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5937 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5938 }
5939
5940 do {
5941 IObuff[len++] = ' ';
5942 } while (len < 5);
5943
5944 /* Range */
5945 if (a & (RANGE|COUNT))
5946 {
5947 if (a & COUNT)
5948 {
5949 /* -count=N */
5950 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5951 len += (int)STRLEN(IObuff + len);
5952 }
5953 else if (a & DFLALL)
5954 IObuff[len++] = '%';
5955 else if (cmd->uc_def >= 0)
5956 {
5957 /* -range=N */
5958 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5959 len += (int)STRLEN(IObuff + len);
5960 }
5961 else
5962 IObuff[len++] = '.';
5963 }
5964
5965 do {
5966 IObuff[len++] = ' ';
5967 } while (len < 11);
5968
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005969 /* Address Type */
5970 for (j = 0; addr_type_complete[j].expand != -1; ++j)
5971 if (addr_type_complete[j].expand != ADDR_LINES
5972 && addr_type_complete[j].expand == cmd->uc_addr_type)
5973 {
5974 STRCPY(IObuff + len, addr_type_complete[j].name);
5975 len += (int)STRLEN(IObuff + len);
5976 break;
5977 }
5978
5979 do {
5980 IObuff[len++] = ' ';
5981 } while (len < 21);
5982
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 /* Completion */
5984 for (j = 0; command_complete[j].expand != 0; ++j)
5985 if (command_complete[j].expand == cmd->uc_compl)
5986 {
5987 STRCPY(IObuff + len, command_complete[j].name);
5988 len += (int)STRLEN(IObuff + len);
5989 break;
5990 }
5991
5992 do {
5993 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005994 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995
5996 IObuff[len] = '\0';
5997 msg_outtrans(IObuff);
5998
5999 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006000#ifdef FEAT_EVAL
6001 if (p_verbose > 0)
6002 last_set_msg(cmd->uc_scriptID);
6003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 out_flush();
6005 ui_breakcheck();
6006 if (got_int)
6007 break;
6008 }
6009 if (gap == &ucmds || i < gap->ga_len)
6010 break;
6011 gap = &ucmds;
6012 }
6013
6014 if (!found)
6015 MSG(_("No user-defined commands found"));
6016}
6017
6018 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006019uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020{
6021 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6022 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6023 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6024 0xb9, 0x7f, 0};
6025 int i;
6026
6027 for (i = 0; fcmd[i]; ++i)
6028 IObuff[i] = fcmd[i] - 0x40;
6029 IObuff[i] = 0;
6030 return IObuff;
6031}
6032
6033 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006034uc_scan_attr(
6035 char_u *attr,
6036 size_t len,
6037 long *argt,
6038 long *def,
6039 int *flags,
6040 int *compl,
6041 char_u **compl_arg,
6042 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043{
6044 char_u *p;
6045
6046 if (len == 0)
6047 {
6048 EMSG(_("E175: No attribute specified"));
6049 return FAIL;
6050 }
6051
6052 /* First, try the simple attributes (no arguments) */
6053 if (STRNICMP(attr, "bang", len) == 0)
6054 *argt |= BANG;
6055 else if (STRNICMP(attr, "buffer", len) == 0)
6056 *flags |= UC_BUFFER;
6057 else if (STRNICMP(attr, "register", len) == 0)
6058 *argt |= REGSTR;
6059 else if (STRNICMP(attr, "bar", len) == 0)
6060 *argt |= TRLBAR;
6061 else
6062 {
6063 int i;
6064 char_u *val = NULL;
6065 size_t vallen = 0;
6066 size_t attrlen = len;
6067
6068 /* Look for the attribute name - which is the part before any '=' */
6069 for (i = 0; i < (int)len; ++i)
6070 {
6071 if (attr[i] == '=')
6072 {
6073 val = &attr[i + 1];
6074 vallen = len - i - 1;
6075 attrlen = i;
6076 break;
6077 }
6078 }
6079
6080 if (STRNICMP(attr, "nargs", attrlen) == 0)
6081 {
6082 if (vallen == 1)
6083 {
6084 if (*val == '0')
6085 /* Do nothing - this is the default */;
6086 else if (*val == '1')
6087 *argt |= (EXTRA | NOSPC | NEEDARG);
6088 else if (*val == '*')
6089 *argt |= EXTRA;
6090 else if (*val == '?')
6091 *argt |= (EXTRA | NOSPC);
6092 else if (*val == '+')
6093 *argt |= (EXTRA | NEEDARG);
6094 else
6095 goto wrong_nargs;
6096 }
6097 else
6098 {
6099wrong_nargs:
6100 EMSG(_("E176: Invalid number of arguments"));
6101 return FAIL;
6102 }
6103 }
6104 else if (STRNICMP(attr, "range", attrlen) == 0)
6105 {
6106 *argt |= RANGE;
6107 if (vallen == 1 && *val == '%')
6108 *argt |= DFLALL;
6109 else if (val != NULL)
6110 {
6111 p = val;
6112 if (*def >= 0)
6113 {
6114two_count:
6115 EMSG(_("E177: Count cannot be specified twice"));
6116 return FAIL;
6117 }
6118
6119 *def = getdigits(&p);
6120 *argt |= (ZEROR | NOTADR);
6121
6122 if (p != val + vallen || vallen == 0)
6123 {
6124invalid_count:
6125 EMSG(_("E178: Invalid default value for count"));
6126 return FAIL;
6127 }
6128 }
6129 }
6130 else if (STRNICMP(attr, "count", attrlen) == 0)
6131 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006132 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133
6134 if (val != NULL)
6135 {
6136 p = val;
6137 if (*def >= 0)
6138 goto two_count;
6139
6140 *def = getdigits(&p);
6141
6142 if (p != val + vallen)
6143 goto invalid_count;
6144 }
6145
6146 if (*def < 0)
6147 *def = 0;
6148 }
6149 else if (STRNICMP(attr, "complete", attrlen) == 0)
6150 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 if (val == NULL)
6152 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006153 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 return FAIL;
6155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006157 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6158 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006161 else if (STRNICMP(attr, "addr", attrlen) == 0)
6162 {
6163 *argt |= RANGE;
6164 if (val == NULL)
6165 {
6166 EMSG(_("E179: argument required for -addr"));
6167 return FAIL;
6168 }
6169 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6170 == FAIL)
6171 return FAIL;
6172 if (addr_type_arg != ADDR_LINES)
6173 *argt |= (ZEROR | NOTADR) ;
6174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 else
6176 {
6177 char_u ch = attr[len];
6178 attr[len] = '\0';
6179 EMSG2(_("E181: Invalid attribute: %s"), attr);
6180 attr[len] = ch;
6181 return FAIL;
6182 }
6183 }
6184
6185 return OK;
6186}
6187
Bram Moolenaara850a712009-01-28 14:42:59 +00006188/*
6189 * ":command ..."
6190 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006192ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193{
6194 char_u *name;
6195 char_u *end;
6196 char_u *p;
6197 long argt = 0;
6198 long def = -1;
6199 int flags = 0;
6200 int compl = EXPAND_NOTHING;
6201 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006202 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006204 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205
6206 p = eap->arg;
6207
6208 /* Check for attributes */
6209 while (*p == '-')
6210 {
6211 ++p;
6212 end = skiptowhite(p);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006213 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 == FAIL)
6215 return;
6216 p = skipwhite(end);
6217 }
6218
6219 /* Get the name (if any) and skip to the following argument */
6220 name = p;
6221 if (ASCII_ISALPHA(*p))
6222 while (ASCII_ISALNUM(*p))
6223 ++p;
6224 if (!ends_excmd(*p) && !vim_iswhite(*p))
6225 {
6226 EMSG(_("E182: Invalid command name"));
6227 return;
6228 }
6229 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006230 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231
6232 /* If there is nothing after the name, and no attributes were specified,
6233 * we are listing commands
6234 */
6235 p = skipwhite(end);
6236 if (!has_attr && ends_excmd(*p))
6237 {
6238 uc_list(name, end - name);
6239 }
6240 else if (!ASCII_ISUPPER(*name))
6241 {
6242 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6243 return;
6244 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006245 else if ((name_len == 1 && *name == 'X')
6246 || (name_len <= 4
6247 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6248 {
6249 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6250 return;
6251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 else
6253 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006254 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255}
6256
6257/*
6258 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 * Clear all user commands, global and for current buffer.
6260 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006261 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006262ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263{
6264 uc_clear(&ucmds);
6265 uc_clear(&curbuf->b_ucmds);
6266}
6267
6268/*
6269 * Clear all user commands for "gap".
6270 */
6271 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006272uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273{
6274 int i;
6275 ucmd_T *cmd;
6276
6277 for (i = 0; i < gap->ga_len; ++i)
6278 {
6279 cmd = USER_CMD_GA(gap, i);
6280 vim_free(cmd->uc_name);
6281 vim_free(cmd->uc_rep);
6282# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6283 vim_free(cmd->uc_compl_arg);
6284# endif
6285 }
6286 ga_clear(gap);
6287}
6288
6289 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006290ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291{
6292 int i = 0;
6293 ucmd_T *cmd = NULL;
6294 int cmp = -1;
6295 garray_T *gap;
6296
6297 gap = &curbuf->b_ucmds;
6298 for (;;)
6299 {
6300 for (i = 0; i < gap->ga_len; ++i)
6301 {
6302 cmd = USER_CMD_GA(gap, i);
6303 cmp = STRCMP(eap->arg, cmd->uc_name);
6304 if (cmp <= 0)
6305 break;
6306 }
6307 if (gap == &ucmds || cmp == 0)
6308 break;
6309 gap = &ucmds;
6310 }
6311
6312 if (cmp != 0)
6313 {
6314 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6315 return;
6316 }
6317
6318 vim_free(cmd->uc_name);
6319 vim_free(cmd->uc_rep);
6320# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6321 vim_free(cmd->uc_compl_arg);
6322# endif
6323
6324 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325
6326 if (i < gap->ga_len)
6327 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6328}
6329
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006330/*
6331 * split and quote args for <f-args>
6332 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006334uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335{
6336 char_u *buf;
6337 char_u *p;
6338 char_u *q;
6339 int len;
6340
6341 /* Precalculate length */
6342 p = arg;
6343 len = 2; /* Initial and final quotes */
6344
6345 while (*p)
6346 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006347 if (p[0] == '\\' && p[1] == '\\')
6348 {
6349 len += 2;
6350 p += 2;
6351 }
6352 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 {
6354 len += 1;
6355 p += 2;
6356 }
6357 else if (*p == '\\' || *p == '"')
6358 {
6359 len += 2;
6360 p += 1;
6361 }
6362 else if (vim_iswhite(*p))
6363 {
6364 p = skipwhite(p);
6365 if (*p == NUL)
6366 break;
6367 len += 3; /* "," */
6368 }
6369 else
6370 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006371#ifdef FEAT_MBYTE
6372 int charlen = (*mb_ptr2len)(p);
6373 len += charlen;
6374 p += charlen;
6375#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 ++len;
6377 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 }
6380 }
6381
6382 buf = alloc(len + 1);
6383 if (buf == NULL)
6384 {
6385 *lenp = 0;
6386 return buf;
6387 }
6388
6389 p = arg;
6390 q = buf;
6391 *q++ = '"';
6392 while (*p)
6393 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006394 if (p[0] == '\\' && p[1] == '\\')
6395 {
6396 *q++ = '\\';
6397 *q++ = '\\';
6398 p += 2;
6399 }
6400 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 {
6402 *q++ = p[1];
6403 p += 2;
6404 }
6405 else if (*p == '\\' || *p == '"')
6406 {
6407 *q++ = '\\';
6408 *q++ = *p++;
6409 }
6410 else if (vim_iswhite(*p))
6411 {
6412 p = skipwhite(p);
6413 if (*p == NUL)
6414 break;
6415 *q++ = '"';
6416 *q++ = ',';
6417 *q++ = '"';
6418 }
6419 else
6420 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006421 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 }
6423 }
6424 *q++ = '"';
6425 *q = 0;
6426
6427 *lenp = len;
6428 return buf;
6429}
6430
6431/*
6432 * Check for a <> code in a user command.
6433 * "code" points to the '<'. "len" the length of the <> (inclusive).
6434 * "buf" is where the result is to be added.
6435 * "split_buf" points to a buffer used for splitting, caller should free it.
6436 * "split_len" is the length of what "split_buf" contains.
6437 * Returns the length of the replacement, which has been added to "buf".
6438 * Returns -1 if there was no match, and only the "<" has been copied.
6439 */
6440 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006441uc_check_code(
6442 char_u *code,
6443 size_t len,
6444 char_u *buf,
6445 ucmd_T *cmd, /* the user command we're expanding */
6446 exarg_T *eap, /* ex arguments */
6447 char_u **split_buf,
6448 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449{
6450 size_t result = 0;
6451 char_u *p = code + 1;
6452 size_t l = len - 2;
6453 int quote = 0;
6454 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
6455 ct_LT, ct_NONE } type = ct_NONE;
6456
6457 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6458 {
6459 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6460 p += 2;
6461 l -= 2;
6462 }
6463
Bram Moolenaar371d5402006-03-20 21:47:49 +00006464 ++l;
6465 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006467 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006469 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006471 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006473 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006475 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006477 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006479 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 type = ct_REGISTER;
6481
6482 switch (type)
6483 {
6484 case ct_ARGS:
6485 /* Simple case first */
6486 if (*eap->arg == NUL)
6487 {
6488 if (quote == 1)
6489 {
6490 result = 2;
6491 if (buf != NULL)
6492 STRCPY(buf, "''");
6493 }
6494 else
6495 result = 0;
6496 break;
6497 }
6498
6499 /* When specified there is a single argument don't split it.
6500 * Works for ":Cmd %" when % is "a b c". */
6501 if ((eap->argt & NOSPC) && quote == 2)
6502 quote = 1;
6503
6504 switch (quote)
6505 {
6506 case 0: /* No quoting, no splitting */
6507 result = STRLEN(eap->arg);
6508 if (buf != NULL)
6509 STRCPY(buf, eap->arg);
6510 break;
6511 case 1: /* Quote, but don't split */
6512 result = STRLEN(eap->arg) + 2;
6513 for (p = eap->arg; *p; ++p)
6514 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006515#ifdef FEAT_MBYTE
6516 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6517 /* DBCS can contain \ in a trail byte, skip the
6518 * double-byte character. */
6519 ++p;
6520 else
6521#endif
6522 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 ++result;
6524 }
6525
6526 if (buf != NULL)
6527 {
6528 *buf++ = '"';
6529 for (p = eap->arg; *p; ++p)
6530 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006531#ifdef FEAT_MBYTE
6532 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6533 /* DBCS can contain \ in a trail byte, copy the
6534 * double-byte character to avoid escaping. */
6535 *buf++ = *p++;
6536 else
6537#endif
6538 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 *buf++ = '\\';
6540 *buf++ = *p;
6541 }
6542 *buf = '"';
6543 }
6544
6545 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006546 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 /* This is hard, so only do it once, and cache the result */
6548 if (*split_buf == NULL)
6549 *split_buf = uc_split_args(eap->arg, split_len);
6550
6551 result = *split_len;
6552 if (buf != NULL && result != 0)
6553 STRCPY(buf, *split_buf);
6554
6555 break;
6556 }
6557 break;
6558
6559 case ct_BANG:
6560 result = eap->forceit ? 1 : 0;
6561 if (quote)
6562 result += 2;
6563 if (buf != NULL)
6564 {
6565 if (quote)
6566 *buf++ = '"';
6567 if (eap->forceit)
6568 *buf++ = '!';
6569 if (quote)
6570 *buf = '"';
6571 }
6572 break;
6573
6574 case ct_LINE1:
6575 case ct_LINE2:
6576 case ct_COUNT:
6577 {
6578 char num_buf[20];
6579 long num = (type == ct_LINE1) ? eap->line1 :
6580 (type == ct_LINE2) ? eap->line2 :
6581 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6582 size_t num_len;
6583
6584 sprintf(num_buf, "%ld", num);
6585 num_len = STRLEN(num_buf);
6586 result = num_len;
6587
6588 if (quote)
6589 result += 2;
6590
6591 if (buf != NULL)
6592 {
6593 if (quote)
6594 *buf++ = '"';
6595 STRCPY(buf, num_buf);
6596 buf += num_len;
6597 if (quote)
6598 *buf = '"';
6599 }
6600
6601 break;
6602 }
6603
6604 case ct_REGISTER:
6605 result = eap->regname ? 1 : 0;
6606 if (quote)
6607 result += 2;
6608 if (buf != NULL)
6609 {
6610 if (quote)
6611 *buf++ = '\'';
6612 if (eap->regname)
6613 *buf++ = eap->regname;
6614 if (quote)
6615 *buf = '\'';
6616 }
6617 break;
6618
6619 case ct_LT:
6620 result = 1;
6621 if (buf != NULL)
6622 *buf = '<';
6623 break;
6624
6625 default:
6626 /* Not recognized: just copy the '<' and return -1. */
6627 result = (size_t)-1;
6628 if (buf != NULL)
6629 *buf = '<';
6630 break;
6631 }
6632
6633 return result;
6634}
6635
6636 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006637do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638{
6639 char_u *buf;
6640 char_u *p;
6641 char_u *q;
6642
6643 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006644 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006645 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 size_t len, totlen;
6647
6648 size_t split_len = 0;
6649 char_u *split_buf = NULL;
6650 ucmd_T *cmd;
6651#ifdef FEAT_EVAL
6652 scid_T save_current_SID = current_SID;
6653#endif
6654
6655 if (eap->cmdidx == CMD_USER)
6656 cmd = USER_CMD(eap->useridx);
6657 else
6658 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6659
6660 /*
6661 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006662 * First round: "buf" is NULL, compute length, allocate "buf".
6663 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 */
6665 buf = NULL;
6666 for (;;)
6667 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006668 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006669 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006671
6672 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006674 start = vim_strchr(p, '<');
6675 if (start != NULL)
6676 end = vim_strchr(start + 1, '>');
6677 if (buf != NULL)
6678 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006679 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6680 ;
6681 if (*ksp == K_SPECIAL
6682 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006683 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6684# ifdef FEAT_GUI
6685 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6686# endif
6687 ))
6688 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006689 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006690 * KS_SPECIAL KE_FILLER, like for mappings, but
6691 * do_cmdline() doesn't handle that, so convert it back.
6692 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6693 len = ksp - p;
6694 if (len > 0)
6695 {
6696 mch_memmove(q, p, len);
6697 q += len;
6698 }
6699 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6700 p = ksp + 3;
6701 continue;
6702 }
6703 }
6704
6705 /* break if there no <item> is found */
6706 if (start == NULL || end == NULL)
6707 break;
6708
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 /* Include the '>' */
6710 ++end;
6711
6712 /* Take everything up to the '<' */
6713 len = start - p;
6714 if (buf == NULL)
6715 totlen += len;
6716 else
6717 {
6718 mch_memmove(q, p, len);
6719 q += len;
6720 }
6721
6722 len = uc_check_code(start, end - start, q, cmd, eap,
6723 &split_buf, &split_len);
6724 if (len == (size_t)-1)
6725 {
6726 /* no match, continue after '<' */
6727 p = start + 1;
6728 len = 1;
6729 }
6730 else
6731 p = end;
6732 if (buf == NULL)
6733 totlen += len;
6734 else
6735 q += len;
6736 }
6737 if (buf != NULL) /* second time here, finished */
6738 {
6739 STRCPY(q, p);
6740 break;
6741 }
6742
6743 totlen += STRLEN(p); /* Add on the trailing characters */
6744 buf = alloc((unsigned)(totlen + 1));
6745 if (buf == NULL)
6746 {
6747 vim_free(split_buf);
6748 return;
6749 }
6750 }
6751
6752#ifdef FEAT_EVAL
6753 current_SID = cmd->uc_scriptID;
6754#endif
6755 (void)do_cmdline(buf, eap->getline, eap->cookie,
6756 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6757#ifdef FEAT_EVAL
6758 current_SID = save_current_SID;
6759#endif
6760 vim_free(buf);
6761 vim_free(split_buf);
6762}
6763
6764# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6765 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006766get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767{
6768 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6769}
6770
6771/*
6772 * Function given to ExpandGeneric() to obtain the list of user command names.
6773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006775get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776{
6777 if (idx < curbuf->b_ucmds.ga_len)
6778 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6779 idx -= curbuf->b_ucmds.ga_len;
6780 if (idx < ucmds.ga_len)
6781 return USER_CMD(idx)->uc_name;
6782 return NULL;
6783}
6784
6785/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006786 * Function given to ExpandGeneric() to obtain the list of user address type names.
6787 */
6788 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006789get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006790{
6791 return (char_u *)addr_type_complete[idx].name;
6792}
6793
6794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 * Function given to ExpandGeneric() to obtain the list of user command
6796 * attributes.
6797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006799get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800{
6801 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006802 {"addr", "bang", "bar", "buffer", "complete",
6803 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006805 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 return NULL;
6807 return (char_u *)user_cmd_flags[idx];
6808}
6809
6810/*
6811 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6812 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006814get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815{
6816 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6817
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006818 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 return NULL;
6820 return (char_u *)user_cmd_nargs[idx];
6821}
6822
6823/*
6824 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6825 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006827get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828{
6829 return (char_u *)command_complete[idx].name;
6830}
6831# endif /* FEAT_CMDL_COMPL */
6832
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006833/*
6834 * Parse address type argument
6835 */
6836 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006837parse_addr_type_arg(
6838 char_u *value,
6839 int vallen,
6840 long *argt,
6841 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006842{
6843 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01006844
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006845 for (i = 0; addr_type_complete[i].expand != -1; ++i)
6846 {
6847 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
6848 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
6849 if (a && b)
6850 {
6851 *addr_type_arg = addr_type_complete[i].expand;
6852 break;
6853 }
6854 }
6855
6856 if (addr_type_complete[i].expand == -1)
6857 {
6858 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01006859
6860 for (i = 0; err[i] != NUL && !vim_iswhite(err[i]); i++)
6861 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006862 err[i] = NUL;
6863 EMSG2(_("E180: Invalid address type value: %s"), err);
6864 return FAIL;
6865 }
6866
6867 if (*addr_type_arg != ADDR_LINES)
6868 *argt |= NOTADR;
6869
6870 return OK;
6871}
6872
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873#endif /* FEAT_USR_CMDS */
6874
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006875#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6876/*
6877 * Parse a completion argument "value[vallen]".
6878 * The detected completion goes in "*complp", argument type in "*argt".
6879 * When there is an argument, for function and user defined completion, it's
6880 * copied to allocated memory and stored in "*compl_arg".
6881 * Returns FAIL if something is wrong.
6882 */
6883 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006884parse_compl_arg(
6885 char_u *value,
6886 int vallen,
6887 int *complp,
6888 long *argt,
6889 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006890{
6891 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006892# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006893 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006894# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006895 int i;
6896 int valend = vallen;
6897
6898 /* Look for any argument part - which is the part after any ',' */
6899 for (i = 0; i < vallen; ++i)
6900 {
6901 if (value[i] == ',')
6902 {
6903 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006904# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006905 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006906# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006907 valend = i;
6908 break;
6909 }
6910 }
6911
6912 for (i = 0; command_complete[i].expand != 0; ++i)
6913 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006914 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006915 && STRNCMP(value, command_complete[i].name, valend) == 0)
6916 {
6917 *complp = command_complete[i].expand;
6918 if (command_complete[i].expand == EXPAND_BUFFERS)
6919 *argt |= BUFNAME;
6920 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6921 || command_complete[i].expand == EXPAND_FILES)
6922 *argt |= XFILE;
6923 break;
6924 }
6925 }
6926
6927 if (command_complete[i].expand == 0)
6928 {
6929 EMSG2(_("E180: Invalid complete value: %s"), value);
6930 return FAIL;
6931 }
6932
6933# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6934 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6935 && arg != NULL)
6936# else
6937 if (arg != NULL)
6938# endif
6939 {
6940 EMSG(_("E468: Completion argument only allowed for custom completion"));
6941 return FAIL;
6942 }
6943
6944# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6945 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6946 && arg == NULL)
6947 {
6948 EMSG(_("E467: Custom completion requires a function argument"));
6949 return FAIL;
6950 }
6951
6952 if (arg != NULL)
6953 *compl_arg = vim_strnsave(arg, (int)arglen);
6954# endif
6955 return OK;
6956}
6957#endif
6958
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006960ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961{
Bram Moolenaare6850792010-05-14 15:28:44 +02006962 if (*eap->arg == NUL)
6963 {
6964#ifdef FEAT_EVAL
6965 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6966 char_u *p = NULL;
6967
6968 if (expr != NULL)
6969 {
6970 ++emsg_off;
6971 p = eval_to_string(expr, NULL, FALSE);
6972 --emsg_off;
6973 vim_free(expr);
6974 }
6975 if (p != NULL)
6976 {
6977 MSG(p);
6978 vim_free(p);
6979 }
6980 else
6981 MSG("default");
6982#else
6983 MSG(_("unknown"));
6984#endif
6985 }
6986 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006987 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988}
6989
6990 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006991ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992{
6993 if (*eap->arg == NUL && eap->cmd[2] == '!')
6994 MSG(_("Greetings, Vim user!"));
6995 do_highlight(eap->arg, eap->forceit, FALSE);
6996}
6997
6998
6999/*
7000 * Call this function if we thought we were going to exit, but we won't
7001 * (because of an error). May need to restore the terminal mode.
7002 */
7003 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007004not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005{
7006 exiting = FALSE;
7007 settmode(TMODE_RAW);
7008}
7009
7010/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007011 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 */
7013 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007014ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015{
Bram Moolenaarf240e182014-11-27 18:33:02 +01007016#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007017 win_T *wp;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007018#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007019
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020#ifdef FEAT_CMDWIN
7021 if (cmdwin_type != 0)
7022 {
7023 cmdwin_result = Ctrl_C;
7024 return;
7025 }
7026#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007027 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007028 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007029 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007030 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007031 return;
7032 }
Bram Moolenaarf240e182014-11-27 18:33:02 +01007033#ifdef FEAT_WINDOWS
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007034 if (eap->addr_count > 0)
7035 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007036 int wnr = eap->line2;
7037
7038 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7039 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007040 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007041 }
7042 else
Bram Moolenaarf240e182014-11-27 18:33:02 +01007043#endif
7044#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007045 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007046#endif
7047
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007048#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02007049 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007050 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02007051 * being closed (can only happen in autocommands). */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007052 if (curbuf_locked() || (wp->w_buffer->b_nwindows == 1
7053 && wp->w_buffer->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007054 return;
7055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056
7057#ifdef FEAT_NETBEANS_INTG
7058 netbeansForcedQuit = eap->forceit;
7059#endif
7060
7061 /*
7062 * If there are more files or windows we won't exit.
7063 */
7064 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7065 exiting = TRUE;
7066 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007067 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7068 | (eap->forceit ? CCGD_FORCEIT : 0)
7069 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007071 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 {
7073 not_exiting();
7074 }
7075 else
7076 {
7077#ifdef FEAT_WINDOWS
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007078 /* quit last window
7079 * Note: only_one_window() returns true, even so a help window is
7080 * still open. In that case only quit, if no address has been
7081 * specified. Example:
7082 * :h|wincmd w|1q - don't quit
7083 * :h|wincmd w|q - quit
7084 */
7085 if (only_one_window() && (firstwin == lastwin || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086#endif
7087 getout(0);
7088#ifdef FEAT_WINDOWS
7089# ifdef FEAT_GUI
7090 need_mouse_correct = TRUE;
7091# endif
7092 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007093 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094#endif
7095 }
7096}
7097
7098/*
7099 * ":cquit".
7100 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007102ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103{
7104 getout(1); /* this does not always pass on the exit code to the Manx
7105 compiler. why? */
7106}
7107
7108/*
7109 * ":qall": try to quit all windows
7110 */
7111 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007112ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113{
7114# ifdef FEAT_CMDWIN
7115 if (cmdwin_type != 0)
7116 {
7117 if (eap->forceit)
7118 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7119 else
7120 cmdwin_result = K_XF2;
7121 return;
7122 }
7123# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007124
7125 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007126 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007127 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007128 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007129 return;
7130 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007131#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007132 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7133 /* Refuse to quit when locked or when the buffer in the last window is
7134 * being closed (can only happen in autocommands). */
7135 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007136 return;
7137#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007138
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007140 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 getout(0);
7142 not_exiting();
7143}
7144
Bram Moolenaard9967712006-03-11 21:18:15 +00007145#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146/*
7147 * ":close": close current window, unless it is the last one
7148 */
7149 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007150ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007152 win_T *win;
7153 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154# ifdef FEAT_CMDWIN
7155 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007156 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 else
7158# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007159 if (!text_locked()
7160#ifdef FEAT_AUTOCMD
7161 && !curbuf_locked()
7162#endif
7163 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007164 {
7165 if (eap->addr_count == 0)
7166 ex_win_close(eap->forceit, curwin, NULL);
7167 else {
7168 for (win = firstwin; win != NULL; win = win->w_next)
7169 {
7170 winnr++;
7171 if (winnr == eap->line2)
7172 break;
7173 }
7174 if (win == NULL)
7175 win = lastwin;
7176 ex_win_close(eap->forceit, win, NULL);
7177 }
7178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179}
7180
Bram Moolenaard9967712006-03-11 21:18:15 +00007181# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007182/*
7183 * ":pclose": Close any preview window.
7184 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007186ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007187{
7188 win_T *win;
7189
7190 for (win = firstwin; win != NULL; win = win->w_next)
7191 if (win->w_p_pvw)
7192 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007193 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007194 break;
7195 }
7196}
Bram Moolenaard9967712006-03-11 21:18:15 +00007197# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007198
Bram Moolenaarf740b292006-02-16 22:11:02 +00007199/*
7200 * Close window "win" and take care of handling closing the last window for a
7201 * modified buffer.
7202 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007203 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007204ex_win_close(
7205 int forceit,
7206 win_T *win,
7207 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208{
7209 int need_hide;
7210 buf_T *buf = win->w_buffer;
7211
7212 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007213 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007215# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 if ((p_confirm || cmdmod.confirm) && p_write)
7217 {
7218 dialog_changed(buf, FALSE);
7219 if (buf_valid(buf) && bufIsChanged(buf))
7220 return;
7221 need_hide = FALSE;
7222 }
7223 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007224# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 {
7226 EMSG(_(e_nowrtmsg));
7227 return;
7228 }
7229 }
7230
Bram Moolenaard9967712006-03-11 21:18:15 +00007231# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007233# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007234
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007236 if (tp == NULL)
7237 win_close(win, !need_hide && !P_HID(buf));
7238 else
7239 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240}
7241
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007243 * ":tabclose": close current tab page, unless it is the last one.
7244 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 */
7246 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007247ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007249 tabpage_T *tp;
7250
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007251# ifdef FEAT_CMDWIN
7252 if (cmdwin_type != 0)
7253 cmdwin_result = K_IGNORE;
7254 else
7255# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007256 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007257 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007258 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007260 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007261 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007262 tp = find_tabpage((int)eap->line2);
7263 if (tp == NULL)
7264 {
7265 beep_flush();
7266 return;
7267 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007268 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007269 {
7270 tabpage_close_other(tp, eap->forceit);
7271 return;
7272 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007273 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007274 if (!text_locked()
7275#ifdef FEAT_AUTOCMD
7276 && !curbuf_locked()
7277#endif
7278 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00007279 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007281}
7282
7283/*
7284 * ":tabonly": close all tab pages except the current one
7285 */
7286 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007287ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007288{
7289 tabpage_T *tp;
7290 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007291
7292# ifdef FEAT_CMDWIN
7293 if (cmdwin_type != 0)
7294 cmdwin_result = K_IGNORE;
7295 else
7296# endif
7297 if (first_tabpage->tp_next == NULL)
7298 MSG(_("Already only one tab page"));
7299 else
7300 {
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007301 if (eap->addr_count > 0)
7302 goto_tabpage(eap->line2);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007303 /* Repeat this up to a 1000 times, because autocommands may mess
7304 * up the lists. */
7305 for (done = 0; done < 1000; ++done)
7306 {
7307 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7308 if (tp->tp_topframe != topframe)
7309 {
7310 tabpage_close_other(tp, eap->forceit);
7311 /* if we failed to close it quit */
7312 if (valid_tabpage(tp))
7313 done = 1000;
7314 /* start over, "tp" is now invalid */
7315 break;
7316 }
7317 if (first_tabpage->tp_next == NULL)
7318 break;
7319 }
7320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322
7323/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007324 * Close the current tab page.
7325 */
7326 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007327tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007328{
7329 /* First close all the windows but the current one. If that worked then
7330 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007331 if (lastwin != firstwin)
7332 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007333 if (lastwin == firstwin)
7334 ex_win_close(forceit, curwin, NULL);
7335# ifdef FEAT_GUI
7336 need_mouse_correct = TRUE;
7337# endif
7338}
7339
7340/*
7341 * Close tab page "tp", which is not the current tab page.
7342 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007343 * Also takes care of the tab pages line disappearing when closing the
7344 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007345 */
7346 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007347tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007348{
7349 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007350 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007351 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007352
7353 /* Limit to 1000 windows, autocommands may add a window while we close
7354 * one. OK, so I'm paranoid... */
7355 while (++done < 1000)
7356 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007357 wp = tp->tp_firstwin;
7358 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007359
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007360 /* Autocommands may delete the tab page under our fingers and we may
7361 * fail to close a window with a modified buffer. */
7362 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007363 break;
7364 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007365
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007366 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007367 if (h != tabline_height())
7368 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007369}
7370
7371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 * ":only".
7373 */
7374 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007375ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007377 win_T *wp;
7378 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379# ifdef FEAT_GUI
7380 need_mouse_correct = TRUE;
7381# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007382 if (eap->addr_count > 0)
7383 {
7384 wnr = eap->line2;
7385 for (wp = firstwin; --wnr > 0; )
7386 {
7387 if (wp->w_next == NULL)
7388 break;
7389 else
7390 wp = wp->w_next;
7391 }
7392 win_goto(wp);
7393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 close_others(TRUE, eap->forceit);
7395}
7396
7397/*
7398 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007399 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007401 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007402ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403{
7404 if (eap->addr_count == 0)
7405 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007406 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407}
7408#endif /* FEAT_WINDOWS */
7409
7410 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007411ex_hide(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412{
7413 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
7414 eap->errmsg = e_invarg;
7415 else
7416 {
7417 /* ":hide" or ":hide | cmd": hide current window */
7418 eap->nextcmd = check_nextcmd(eap->arg);
7419#ifdef FEAT_WINDOWS
7420 if (!eap->skip)
7421 {
7422# ifdef FEAT_GUI
7423 need_mouse_correct = TRUE;
7424# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007425 if (eap->addr_count == 0)
7426 win_close(curwin, FALSE); /* don't free buffer */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007427 else
7428 {
7429 int winnr = 0;
7430 win_T *win;
7431
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007432 for (win = firstwin; win != NULL; win = win->w_next)
7433 {
7434 winnr++;
7435 if (winnr == eap->line2)
7436 break;
7437 }
7438 if (win == NULL)
7439 win = lastwin;
7440 win_close(win, FALSE);
7441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 }
7443#endif
7444 }
7445}
7446
7447/*
7448 * ":stop" and ":suspend": Suspend Vim.
7449 */
7450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007451ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452{
7453 /*
7454 * Disallow suspending for "rvim".
7455 */
7456 if (!check_restricted()
7457#ifdef WIN3264
7458 /*
7459 * Check if external commands are allowed now.
7460 */
7461 && can_end_termcap_mode(TRUE)
7462#endif
7463 )
7464 {
7465 if (!eap->forceit)
7466 autowrite_all();
7467 windgoto((int)Rows - 1, 0);
7468 out_char('\n');
7469 out_flush();
7470 stoptermcap();
7471 out_flush(); /* needed for SUN to restore xterm buffer */
7472#ifdef FEAT_TITLE
7473 mch_restore_title(3); /* restore window titles */
7474#endif
7475 ui_suspend(); /* call machine specific function */
7476#ifdef FEAT_TITLE
7477 maketitle();
7478 resettitle(); /* force updating the title */
7479#endif
7480 starttermcap();
7481 scroll_start(); /* scroll screen before redrawing */
7482 redraw_later_clear();
7483 shell_resized(); /* may have resized window */
7484 }
7485}
7486
7487/*
7488 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7489 */
7490 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007491ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492{
7493#ifdef FEAT_CMDWIN
7494 if (cmdwin_type != 0)
7495 {
7496 cmdwin_result = Ctrl_C;
7497 return;
7498 }
7499#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007500 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007501 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007502 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007503 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007504 return;
7505 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007506#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007507 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7508 /* Refuse to quit when locked or when the buffer in the last window is
7509 * being closed (can only happen in autocommands). */
7510 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007511 return;
7512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513
7514 /*
7515 * if more files or windows we won't exit
7516 */
7517 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7518 exiting = TRUE;
7519 if ( ((eap->cmdidx == CMD_wq
7520 || curbufIsChanged())
7521 && do_write(eap) == FAIL)
7522 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007523 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 {
7525 not_exiting();
7526 }
7527 else
7528 {
7529#ifdef FEAT_WINDOWS
7530 if (only_one_window()) /* quit last window, exit Vim */
7531#endif
7532 getout(0);
7533#ifdef FEAT_WINDOWS
7534# ifdef FEAT_GUI
7535 need_mouse_correct = TRUE;
7536# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007537 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 win_close(curwin, !P_HID(curwin->w_buffer));
7539#endif
7540 }
7541}
7542
7543/*
7544 * ":print", ":list", ":number".
7545 */
7546 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007547ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007549 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7550 EMSG(_(e_emptybuf));
7551 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007553 for ( ;!got_int; ui_breakcheck())
7554 {
7555 print_line(eap->line1,
7556 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7557 || (eap->flags & EXFLAG_NR)),
7558 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7559 if (++eap->line1 > eap->line2)
7560 break;
7561 out_flush(); /* show one line at a time */
7562 }
7563 setpcmark();
7564 /* put cursor at last line */
7565 curwin->w_cursor.lnum = eap->line2;
7566 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 }
7568
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570}
7571
7572#ifdef FEAT_BYTEOFF
7573 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007574ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575{
7576 goto_byte(eap->line2);
7577}
7578#endif
7579
7580/*
7581 * ":shell".
7582 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007584ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585{
7586 do_shell(NULL, 0);
7587}
7588
7589#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7590 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007591 || defined(FEAT_GUI_MSWIN) \
7592 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 || defined(PROTO)
7594
7595/*
7596 * Handle a file drop. The code is here because a drop is *nearly* like an
7597 * :args command, but not quite (we have a list of exact filenames, so we
7598 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7599 * code is very similar to :args and hence needs access to a lot of the static
7600 * functions in this file.
7601 *
7602 * The list should be allocated using alloc(), as should each item in the
7603 * list. This function takes over responsibility for freeing the list.
7604 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007605 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7607 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7608 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7609 * file functionality is (currently) not in EMX this is not presently a
7610 * problem.
7611 */
7612 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007613handle_drop(
7614 int filec, /* the number of files dropped */
7615 char_u **filev, /* the list of files dropped */
7616 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617{
7618 exarg_T ea;
7619 int save_msg_scroll = msg_scroll;
7620
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007621 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007622 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007624#ifdef FEAT_AUTOCMD
7625 if (curbuf_locked())
7626 return;
7627#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007628 /* When the screen is being updated we should not change buffers and
7629 * windows structures, it may cause freed memory to be used. */
7630 if (updating_screen)
7631 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632
7633 /* Check whether the current buffer is changed. If so, we will need
7634 * to split the current window or data could be lost.
7635 * We don't need to check if the 'hidden' option is set, as in this
7636 * case the buffer won't be lost.
7637 */
7638 if (!P_HID(curbuf) && !split)
7639 {
7640 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007641 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 --emsg_off;
7643 }
7644 if (split)
7645 {
7646# ifdef FEAT_WINDOWS
7647 if (win_split(0, 0) == FAIL)
7648 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007649 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650
7651 /* When splitting the window, create a new alist. Otherwise the
7652 * existing one is overwritten. */
7653 alist_unlink(curwin->w_alist);
7654 alist_new();
7655# else
7656 return; /* can't split, always fail */
7657# endif
7658 }
7659
7660 /*
7661 * Set up the new argument list.
7662 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007663 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664
7665 /*
7666 * Move to the first file.
7667 */
7668 /* Fake up a minimal "next" command for do_argfile() */
7669 vim_memset(&ea, 0, sizeof(ea));
7670 ea.cmd = (char_u *)"next";
7671 do_argfile(&ea, 0);
7672
7673 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7674 * mode that is not needed here. */
7675 need_start_insertmode = FALSE;
7676
7677 /* Restore msg_scroll, otherwise a following command may cause scrolling
7678 * unexpectedly. The screen will be redrawn by the caller, thus
7679 * msg_scroll being set by displaying a message is irrelevant. */
7680 msg_scroll = save_msg_scroll;
7681}
7682#endif
7683
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684/*
7685 * Clear an argument list: free all file names and reset it to zero entries.
7686 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007687 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007688alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689{
7690 while (--al->al_ga.ga_len >= 0)
7691 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7692 ga_clear(&al->al_ga);
7693}
7694
7695/*
7696 * Init an argument list.
7697 */
7698 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007699alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700{
7701 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7702}
7703
7704#if defined(FEAT_WINDOWS) || defined(PROTO)
7705
7706/*
7707 * Remove a reference from an argument list.
7708 * Ignored when the argument list is the global one.
7709 * If the argument list is no longer used by any window, free it.
7710 */
7711 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007712alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713{
7714 if (al != &global_alist && --al->al_refcount <= 0)
7715 {
7716 alist_clear(al);
7717 vim_free(al);
7718 }
7719}
7720
7721# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7722/*
7723 * Create a new argument list and use it for the current window.
7724 */
7725 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007726alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727{
7728 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7729 if (curwin->w_alist == NULL)
7730 {
7731 curwin->w_alist = &global_alist;
7732 ++global_alist.al_refcount;
7733 }
7734 else
7735 {
7736 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007737 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 alist_init(curwin->w_alist);
7739 }
7740}
7741# endif
7742#endif
7743
Bram Moolenaar53076832015-12-31 19:53:21 +01007744#if (!defined(UNIX) && !defined(__EMX__)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745/*
7746 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007747 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7748 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 */
7750 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007751alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752{
7753 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007754 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 char_u **new_arg_files;
7756 int new_arg_file_count;
7757 char_u *save_p_su = p_su;
7758 int i;
7759
7760 /* Don't use 'suffixes' here. This should work like the shell did the
7761 * expansion. Also, the vimrc file isn't read yet, thus the user
7762 * can't set the options. */
7763 p_su = empty_option;
7764 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7765 if (old_arg_files != NULL)
7766 {
7767 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007768 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7769 old_arg_count = GARGCOUNT;
7770 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007772 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 && new_arg_file_count > 0)
7774 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007775 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7776 TRUE, fnum_list, fnum_len);
7777 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 }
7780 p_su = save_p_su;
7781}
7782#endif
7783
7784/*
7785 * Set the argument list for the current window.
7786 * Takes over the allocated files[] and the allocated fnames in it.
7787 */
7788 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007789alist_set(
7790 alist_T *al,
7791 int count,
7792 char_u **files,
7793 int use_curbuf,
7794 int *fnum_list,
7795 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796{
7797 int i;
7798
7799 alist_clear(al);
7800 if (ga_grow(&al->al_ga, count) == OK)
7801 {
7802 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007803 {
7804 if (got_int)
7805 {
7806 /* When adding many buffers this can take a long time. Allow
7807 * interrupting here. */
7808 while (i < count)
7809 vim_free(files[i++]);
7810 break;
7811 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007812
7813 /* May set buffer name of a buffer previously used for the
7814 * argument list, so that it's re-used by alist_add. */
7815 if (fnum_list != NULL && i < fnum_len)
7816 buf_set_name(fnum_list[i], files[i]);
7817
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007819 ui_breakcheck();
7820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 vim_free(files);
7822 }
7823 else
7824 FreeWild(count, files);
7825#ifdef FEAT_WINDOWS
7826 if (al == &global_alist)
7827#endif
7828 arg_had_last = FALSE;
7829}
7830
7831/*
7832 * Add file "fname" to argument list "al".
7833 * "fname" must have been allocated and "al" must have been checked for room.
7834 */
7835 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007836alist_add(
7837 alist_T *al,
7838 char_u *fname,
7839 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840{
7841 if (fname == NULL) /* don't add NULL file names */
7842 return;
7843#ifdef BACKSLASH_IN_FILENAME
7844 slash_adjust(fname);
7845#endif
7846 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7847 if (set_fnum > 0)
7848 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7849 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7850 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851}
7852
7853#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7854/*
7855 * Adjust slashes in file names. Called after 'shellslash' was set.
7856 */
7857 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007858alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859{
7860 int i;
7861# ifdef FEAT_WINDOWS
7862 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007863 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864# endif
7865
7866 for (i = 0; i < GARGCOUNT; ++i)
7867 if (GARGLIST[i].ae_fname != NULL)
7868 slash_adjust(GARGLIST[i].ae_fname);
7869# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007870 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 if (wp->w_alist != &global_alist)
7872 for (i = 0; i < WARGCOUNT(wp); ++i)
7873 if (WARGLIST(wp)[i].ae_fname != NULL)
7874 slash_adjust(WARGLIST(wp)[i].ae_fname);
7875# endif
7876}
7877#endif
7878
7879/*
7880 * ":preserve".
7881 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007883ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007885 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 ml_preserve(curbuf, TRUE);
7887}
7888
7889/*
7890 * ":recover".
7891 */
7892 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007893ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894{
7895 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7896 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007897 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7898 | CCGD_MULTWIN
7899 | (eap->forceit ? CCGD_FORCEIT : 0)
7900 | CCGD_EXCMD)
7901
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 && (*eap->arg == NUL
7903 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7904 ml_recover();
7905 recoverymode = FALSE;
7906}
7907
7908/*
7909 * Command modifier used in a wrong way.
7910 */
7911 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007912ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913{
7914 eap->errmsg = e_invcmd;
7915}
7916
7917#ifdef FEAT_WINDOWS
7918/*
7919 * :sview [+command] file split window with new file, read-only
7920 * :split [[+command] file] split window with current or new file
7921 * :vsplit [[+command] file] split window vertically with current or new file
7922 * :new [[+command] file] split window with no or new file
7923 * :vnew [[+command] file] split vertically window with no or new file
7924 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007925 *
7926 * :tabedit open new Tab page with empty window
7927 * :tabedit [+command] file open new Tab page and edit "file"
7928 * :tabnew [[+command] file] just like :tabedit
7929 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 */
7931 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007932ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007934 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007935# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007937# endif
7938# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007940# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007942# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7944 {
7945 ex_ni(eap);
7946 return;
7947 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007948# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007950# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007952# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007954# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007956 * quickfix windows. But it's OK when doing ":tab split". */
7957 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 {
7959 if (eap->cmdidx == CMD_split)
7960 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007961# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 if (eap->cmdidx == CMD_vsplit)
7963 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007964# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007966# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007968# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007969 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 {
7971 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7972 FNAME_MESS, TRUE, curbuf->b_ffname);
7973 if (fname == NULL)
7974 goto theend;
7975 eap->arg = fname;
7976 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007977# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007979# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007981# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007983# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007985# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 && eap->cmdidx != CMD_new)
7987 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007988# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007989 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007990# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007991 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007992# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007993 au_has_group((char_u *)"FileExplorer"))
7994 {
7995 /* No browsing supported but we do have the file explorer:
7996 * Edit the directory. */
7997 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7998 eap->arg = (char_u *)".";
7999 }
8000 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008001# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008002 {
8003 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008005 if (fname == NULL)
8006 goto theend;
8007 eap->arg = fname;
8008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 }
8010 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008011# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008013 /*
8014 * Either open new tab page or split the window.
8015 */
8016 if (eap->cmdidx == CMD_tabedit
8017 || eap->cmdidx == CMD_tabfind
8018 || eap->cmdidx == CMD_tabnew)
8019 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008020 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8021 : eap->addr_count == 0 ? 0
8022 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008023 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008024 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008025
8026 /* set the alternate buffer for the window we came from */
8027 if (curwin != old_curwin
8028 && win_valid(old_curwin)
8029 && old_curwin->w_buffer != curbuf
8030 && !cmdmod.keepalt)
8031 old_curwin->w_alt_fnum = curbuf->b_fnum;
8032 }
8033 }
8034 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8036 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008037# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 /* Reset 'scrollbind' when editing another file, but keep it when
8039 * doing ":split" without arguments. */
8040 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008041# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008043# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008045 {
8046 RESET_BINDING(curwin);
8047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 else
8049 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008050# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 do_exedit(eap, old_curwin);
8052 }
8053
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008054# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008056# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008058# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059theend:
8060 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008061# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008063
8064/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008065 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008066 */
8067 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008068tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008069{
8070 exarg_T ea;
8071
8072 vim_memset(&ea, 0, sizeof(ea));
8073 ea.cmdidx = CMD_tabnew;
8074 ea.cmd = (char_u *)"tabn";
8075 ea.arg = (char_u *)"";
8076 ex_splitview(&ea);
8077}
8078
8079/*
8080 * :tabnext command
8081 */
8082 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008083ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008084{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008085 switch (eap->cmdidx)
8086 {
8087 case CMD_tabfirst:
8088 case CMD_tabrewind:
8089 goto_tabpage(1);
8090 break;
8091 case CMD_tablast:
8092 goto_tabpage(9999);
8093 break;
8094 case CMD_tabprevious:
8095 case CMD_tabNext:
8096 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
8097 break;
8098 default: /* CMD_tabnext */
8099 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
8100 break;
8101 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008102}
8103
8104/*
8105 * :tabmove command
8106 */
8107 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008108ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008109{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008110 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008111
8112 if (eap->arg && *eap->arg != NUL)
8113 {
8114 char_u *p = eap->arg;
8115 int relative = 0; /* argument +N/-N means: move N places to the
8116 * right/left relative to the current position. */
8117
8118 if (*eap->arg == '-')
8119 {
8120 relative = -1;
8121 p = eap->arg + 1;
8122 }
8123 else if (*eap->arg == '+')
8124 {
8125 relative = 1;
8126 p = eap->arg + 1;
8127 }
8128 else
8129 p = eap->arg;
8130
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008131 if (relative == 0)
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008132 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008133 if (STRCMP(p, "$") == 0)
8134 tab_number = LAST_TAB_NR;
8135 else if (p == skipdigits(p))
8136 {
8137 /* No numbers as argument. */
8138 eap->errmsg = e_invarg;
8139 return;
8140 }
8141 else
8142 tab_number = getdigits(&p);
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008143 }
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008144 else
8145 {
8146 if (*p != NUL)
8147 tab_number = getdigits(&p);
8148 else
8149 tab_number = 1;
8150 tab_number = tab_number * relative + tabpage_index(curtab);
8151 if (relative == -1)
8152 --tab_number;
8153 }
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008154 }
8155 else if (eap->addr_count != 0)
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008156 {
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008157 tab_number = eap->line2;
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008158 if (**eap->cmdlinep == '-')
8159 --tab_number;
8160 }
8161 else
8162 tab_number = LAST_TAB_NR;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008163
8164 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008165}
8166
8167/*
8168 * :tabs command: List tabs and their contents.
8169 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008170 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008171ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008172{
8173 tabpage_T *tp;
8174 win_T *wp;
8175 int tabcount = 1;
8176
8177 msg_start();
8178 msg_scroll = TRUE;
8179 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8180 {
8181 msg_putchar('\n');
8182 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
8183 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
8184 out_flush(); /* output one line at a time */
8185 ui_breakcheck();
8186
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008187 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008188 wp = firstwin;
8189 else
8190 wp = tp->tp_firstwin;
8191 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8192 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008193 msg_putchar('\n');
8194 msg_putchar(wp == curwin ? '>' : ' ');
8195 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008196 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8197 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008198 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008199 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008200 else
8201 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8202 IObuff, IOSIZE, TRUE);
8203 msg_outtrans(IObuff);
8204 out_flush(); /* output one line at a time */
8205 ui_breakcheck();
8206 }
8207 }
8208}
8209
8210#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211
8212/*
8213 * ":mode": Set screen mode.
8214 * If no argument given, just get the screen size and redraw.
8215 */
8216 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008217ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218{
8219 if (*eap->arg == NUL)
8220 shell_resized();
8221 else
8222 mch_screenmode(eap->arg);
8223}
8224
8225#ifdef FEAT_WINDOWS
8226/*
8227 * ":resize".
8228 * set, increment or decrement current window height
8229 */
8230 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008231ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232{
8233 int n;
8234 win_T *wp = curwin;
8235
8236 if (eap->addr_count > 0)
8237 {
8238 n = eap->line2;
8239 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8240 ;
8241 }
8242
8243#ifdef FEAT_GUI
8244 need_mouse_correct = TRUE;
8245#endif
8246 n = atol((char *)eap->arg);
8247#ifdef FEAT_VERTSPLIT
8248 if (cmdmod.split & WSP_VERT)
8249 {
8250 if (*eap->arg == '-' || *eap->arg == '+')
8251 n += W_WIDTH(curwin);
8252 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8253 n = 9999;
8254 win_setwidth_win((int)n, wp);
8255 }
8256 else
8257#endif
8258 {
8259 if (*eap->arg == '-' || *eap->arg == '+')
8260 n += curwin->w_height;
8261 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8262 n = 9999;
8263 win_setheight_win((int)n, wp);
8264 }
8265}
8266#endif
8267
8268/*
8269 * ":find [+command] <file>" command.
8270 */
8271 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008272ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273{
8274#ifdef FEAT_SEARCHPATH
8275 char_u *fname;
8276 int count;
8277
8278 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8279 TRUE, curbuf->b_ffname);
8280 if (eap->addr_count > 0)
8281 {
8282 /* Repeat finding the file "count" times. This matters when it
8283 * appears several times in the path. */
8284 count = eap->line2;
8285 while (fname != NULL && --count > 0)
8286 {
8287 vim_free(fname);
8288 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8289 FALSE, curbuf->b_ffname);
8290 }
8291 }
8292
8293 if (fname != NULL)
8294 {
8295 eap->arg = fname;
8296#endif
8297 do_exedit(eap, NULL);
8298#ifdef FEAT_SEARCHPATH
8299 vim_free(fname);
8300 }
8301#endif
8302}
8303
8304/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008305 * ":open" simulation: for now just work like ":visual".
8306 */
8307 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008308ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008309{
8310 regmatch_T regmatch;
8311 char_u *p;
8312
8313 curwin->w_cursor.lnum = eap->line2;
8314 beginline(BL_SOL | BL_FIX);
8315 if (*eap->arg == '/')
8316 {
8317 /* ":open /pattern/": put cursor in column found with pattern */
8318 ++eap->arg;
8319 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8320 *p = NUL;
8321 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8322 if (regmatch.regprog != NULL)
8323 {
8324 regmatch.rm_ic = p_ic;
8325 p = ml_get_curline();
8326 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008327 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008328 else
8329 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008330 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008331 }
8332 /* Move to the NUL, ignore any other arguments. */
8333 eap->arg += STRLEN(eap->arg);
8334 }
8335 check_cursor();
8336
8337 eap->cmdidx = CMD_visual;
8338 do_exedit(eap, NULL);
8339}
8340
8341/*
8342 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 */
8344 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008345ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346{
8347 do_exedit(eap, NULL);
8348}
8349
8350/*
8351 * ":edit <file>" command and alikes.
8352 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008354do_exedit(
8355 exarg_T *eap,
8356 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357{
8358 int n;
8359#ifdef FEAT_WINDOWS
8360 int need_hide;
8361#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008362 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363
8364 /*
8365 * ":vi" command ends Ex mode.
8366 */
8367 if (exmode_active && (eap->cmdidx == CMD_visual
8368 || eap->cmdidx == CMD_view))
8369 {
8370 exmode_active = FALSE;
8371 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008372 {
8373 /* Special case: ":global/pat/visual\NLvi-commands" */
8374 if (global_busy)
8375 {
8376 int rd = RedrawingDisabled;
8377 int nwr = no_wait_return;
8378 int ms = msg_scroll;
8379#ifdef FEAT_GUI
8380 int he = hold_gui_events;
8381#endif
8382
8383 if (eap->nextcmd != NULL)
8384 {
8385 stuffReadbuff(eap->nextcmd);
8386 eap->nextcmd = NULL;
8387 }
8388
8389 if (exmode_was != EXMODE_VIM)
8390 settmode(TMODE_RAW);
8391 RedrawingDisabled = 0;
8392 no_wait_return = 0;
8393 need_wait_return = FALSE;
8394 msg_scroll = 0;
8395#ifdef FEAT_GUI
8396 hold_gui_events = 0;
8397#endif
8398 must_redraw = CLEAR;
8399
8400 main_loop(FALSE, TRUE);
8401
8402 RedrawingDisabled = rd;
8403 no_wait_return = nwr;
8404 msg_scroll = ms;
8405#ifdef FEAT_GUI
8406 hold_gui_events = he;
8407#endif
8408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 }
8412
8413 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008414 || eap->cmdidx == CMD_tabnew
8415 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416#ifdef FEAT_VERTSPLIT
8417 || eap->cmdidx == CMD_vnew
8418#endif
8419 ) && *eap->arg == NUL)
8420 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008421 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 setpcmark();
8423 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008424 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8425 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 }
8427 else if ((eap->cmdidx != CMD_split
8428#ifdef FEAT_VERTSPLIT
8429 && eap->cmdidx != CMD_vsplit
8430#endif
8431 )
8432 || *eap->arg != NUL
8433#ifdef FEAT_BROWSE
8434 || cmdmod.browse
8435#endif
8436 )
8437 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008438#ifdef FEAT_AUTOCMD
8439 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8440 * can bring us here, others are stopped earlier. */
8441 if (*eap->arg != NUL && curbuf_locked())
8442 return;
8443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 n = readonlymode;
8445 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8446 readonlymode = TRUE;
8447 else if (eap->cmdidx == CMD_enew)
8448 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8449 empty buffer */
8450 setpcmark();
8451 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8452 NULL, eap,
8453 /* ":edit" goes to first line if Vi compatible */
8454 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8455 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8456 ? ECMD_ONE : eap->do_ecmd_lnum,
8457 (P_HID(curbuf) ? ECMD_HIDE : 0)
8458 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008459 /* after a split we can use an existing buffer */
8460 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461#ifdef FEAT_LISTCMDS
8462 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8463#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008464 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 {
8466 /* Editing the file failed. If the window was split, close it. */
8467#ifdef FEAT_WINDOWS
8468 if (old_curwin != NULL)
8469 {
8470 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8471 if (!need_hide || P_HID(curbuf))
8472 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008473# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8474 cleanup_T cs;
8475
8476 /* Reset the error/interrupt/exception state here so that
8477 * aborting() returns FALSE when closing a window. */
8478 enter_cleanup(&cs);
8479# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480# ifdef FEAT_GUI
8481 need_mouse_correct = TRUE;
8482# endif
8483 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008484
8485# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8486 /* Restore the error/interrupt/exception state if not
8487 * discarded by a new aborting error, interrupt, or
8488 * uncaught exception. */
8489 leave_cleanup(&cs);
8490# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 }
8492 }
8493#endif
8494 }
8495 else if (readonlymode && curbuf->b_nwindows == 1)
8496 {
8497 /* When editing an already visited buffer, 'readonly' won't be set
8498 * but the previous value is kept. With ":view" and ":sview" we
8499 * want the file to be readonly, except when another window is
8500 * editing the same buffer. */
8501 curbuf->b_p_ro = TRUE;
8502 }
8503 readonlymode = n;
8504 }
8505 else
8506 {
8507 if (eap->do_ecmd_cmd != NULL)
8508 do_cmdline_cmd(eap->do_ecmd_cmd);
8509#ifdef FEAT_TITLE
8510 n = curwin->w_arg_idx_invalid;
8511#endif
8512 check_arg_idx(curwin);
8513#ifdef FEAT_TITLE
8514 if (n != curwin->w_arg_idx_invalid)
8515 maketitle();
8516#endif
8517 }
8518
8519#ifdef FEAT_WINDOWS
8520 /*
8521 * if ":split file" worked, set alternate file name in old window to new
8522 * file
8523 */
8524 if (old_curwin != NULL
8525 && *eap->arg != NUL
8526 && curwin != old_curwin
8527 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008528 && old_curwin->w_buffer != curbuf
8529 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 old_curwin->w_alt_fnum = curbuf->b_fnum;
8531#endif
8532
8533 ex_no_reprint = TRUE;
8534}
8535
8536#ifndef FEAT_GUI
8537/*
8538 * ":gui" and ":gvim" when there is no GUI.
8539 */
8540 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008541ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542{
8543 eap->errmsg = e_nogvim;
8544}
8545#endif
8546
8547#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8548 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008549ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550{
8551 gui_make_tearoff(eap->arg);
8552}
8553#endif
8554
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008555#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008557ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008559 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560}
8561#endif
8562
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008564ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565{
8566 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8567 MSG(_("No swap file"));
8568 else
8569 msg(curbuf->b_ml.ml_mfp->mf_fname);
8570}
8571
8572/*
8573 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8574 * offset.
8575 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8576 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008578ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579{
8580#ifdef FEAT_SCROLLBIND
8581 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008582 win_T *save_curwin = curwin;
8583 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 long topline;
8585 long y;
8586 linenr_T old_linenr = curwin->w_cursor.lnum;
8587
8588 setpcmark();
8589
8590 /*
8591 * determine max topline
8592 */
8593 if (curwin->w_p_scb)
8594 {
8595 topline = curwin->w_topline;
8596 for (wp = firstwin; wp; wp = wp->w_next)
8597 {
8598 if (wp->w_p_scb && wp->w_buffer)
8599 {
8600 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8601 if (topline > y)
8602 topline = y;
8603 }
8604 }
8605 if (topline < 1)
8606 topline = 1;
8607 }
8608 else
8609 {
8610 topline = 1;
8611 }
8612
8613
8614 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008615 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8618 {
8619 if (curwin->w_p_scb)
8620 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008621 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 y = topline - curwin->w_topline;
8623 if (y > 0)
8624 scrollup(y, TRUE);
8625 else
8626 scrolldown(-y, TRUE);
8627 curwin->w_scbind_pos = topline;
8628 redraw_later(VALID);
8629 cursor_correct();
8630#ifdef FEAT_WINDOWS
8631 curwin->w_redr_status = TRUE;
8632#endif
8633 }
8634 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008635 curwin = save_curwin;
8636 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 if (curwin->w_p_scb)
8638 {
8639 did_syncbind = TRUE;
8640 checkpcmark();
8641 if (old_linenr != curwin->w_cursor.lnum)
8642 {
8643 char_u ctrl_o[2];
8644
8645 ctrl_o[0] = Ctrl_O;
8646 ctrl_o[1] = 0;
8647 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8648 }
8649 }
8650#endif
8651}
8652
8653
8654 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008655ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008657 int i;
8658 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8659 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660
8661 if (eap->usefilter) /* :r!cmd */
8662 do_bang(1, eap, FALSE, FALSE, TRUE);
8663 else
8664 {
8665 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8666 return;
8667
8668#ifdef FEAT_BROWSE
8669 if (cmdmod.browse)
8670 {
8671 char_u *browseFile;
8672
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008673 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 NULL, NULL, NULL, curbuf);
8675 if (browseFile != NULL)
8676 {
8677 i = readfile(browseFile, NULL,
8678 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8679 vim_free(browseFile);
8680 }
8681 else
8682 i = OK;
8683 }
8684 else
8685#endif
8686 if (*eap->arg == NUL)
8687 {
8688 if (check_fname() == FAIL) /* check for no file name */
8689 return;
8690 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8691 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8692 }
8693 else
8694 {
8695 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8696 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8697 i = readfile(eap->arg, NULL,
8698 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8699
8700 }
8701 if (i == FAIL)
8702 {
8703#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8704 if (!aborting())
8705#endif
8706 EMSG2(_(e_notopen), eap->arg);
8707 }
8708 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008709 {
8710 if (empty && exmode_active)
8711 {
8712 /* Delete the empty line that remains. Historically ex does
8713 * this but vi doesn't. */
8714 if (eap->line2 == 0)
8715 lnum = curbuf->b_ml.ml_line_count;
8716 else
8717 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008718 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008719 {
8720 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008721 if (curwin->w_cursor.lnum > 1
8722 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008723 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008724 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008725 }
8726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 }
8730}
8731
Bram Moolenaarea408852005-06-25 22:49:46 +00008732static char_u *prev_dir = NULL;
8733
8734#if defined(EXITFREE) || defined(PROTO)
8735 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008736free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00008737{
8738 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008739 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008740
8741 vim_free(globaldir);
8742 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008743}
8744#endif
8745
Bram Moolenaarf4258302013-06-02 18:20:17 +02008746/*
8747 * Deal with the side effects of changing the current directory.
8748 * When "local" is TRUE then this was after an ":lcd" command.
8749 */
8750 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008751post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008752{
8753 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008754 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008755 if (local)
8756 {
8757 /* If still in global directory, need to remember current
8758 * directory as global directory. */
8759 if (globaldir == NULL && prev_dir != NULL)
8760 globaldir = vim_strsave(prev_dir);
8761 /* Remember this local directory for the window. */
8762 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8763 curwin->w_localdir = vim_strsave(NameBuff);
8764 }
8765 else
8766 {
8767 /* We are now in the global directory, no need to remember its
8768 * name. */
8769 vim_free(globaldir);
8770 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008771 }
8772
8773 shorten_fnames(TRUE);
8774}
8775
Bram Moolenaarea408852005-06-25 22:49:46 +00008776
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777/*
8778 * ":cd", ":lcd", ":chdir" and ":lchdir".
8779 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008780 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008781ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 char_u *new_dir;
8784 char_u *tofree;
8785
8786 new_dir = eap->arg;
8787#if !defined(UNIX) && !defined(VMS)
8788 /* for non-UNIX ":cd" means: print current directory */
8789 if (*new_dir == NUL)
8790 ex_pwd(NULL);
8791 else
8792#endif
8793 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008794#ifdef FEAT_AUTOCMD
8795 if (allbuf_locked())
8796 return;
8797#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008798 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8799 && !eap->forceit)
8800 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008801 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008802 return;
8803 }
8804
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 /* ":cd -": Change to previous directory */
8806 if (STRCMP(new_dir, "-") == 0)
8807 {
8808 if (prev_dir == NULL)
8809 {
8810 EMSG(_("E186: No previous directory"));
8811 return;
8812 }
8813 new_dir = prev_dir;
8814 }
8815
8816 /* Save current directory for next ":cd -" */
8817 tofree = prev_dir;
8818 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8819 prev_dir = vim_strsave(NameBuff);
8820 else
8821 prev_dir = NULL;
8822
8823#if defined(UNIX) || defined(VMS)
8824 /* for UNIX ":cd" means: go to home directory */
8825 if (*new_dir == NUL)
8826 {
8827 /* use NameBuff for home directory name */
8828# ifdef VMS
8829 char_u *p;
8830
8831 p = mch_getenv((char_u *)"SYS$LOGIN");
8832 if (p == NULL || *p == NUL) /* empty is the same as not set */
8833 NameBuff[0] = NUL;
8834 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008835 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836# else
8837 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8838# endif
8839 new_dir = NameBuff;
8840 }
8841#endif
8842 if (new_dir == NULL || vim_chdir(new_dir))
8843 EMSG(_(e_failed));
8844 else
8845 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008846 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847
8848 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008849 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 ex_pwd(eap);
8851 }
8852 vim_free(tofree);
8853 }
8854}
8855
8856/*
8857 * ":pwd".
8858 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008860ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861{
8862 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8863 {
8864#ifdef BACKSLASH_IN_FILENAME
8865 slash_adjust(NameBuff);
8866#endif
8867 msg(NameBuff);
8868 }
8869 else
8870 EMSG(_("E187: Unknown"));
8871}
8872
8873/*
8874 * ":=".
8875 */
8876 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008877ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878{
8879 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008880 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881}
8882
8883 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008884ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008886 int n;
8887 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888
8889 if (cursor_valid())
8890 {
8891 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8892 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01008893 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008895
8896 len = eap->line2;
8897 switch (*eap->arg)
8898 {
8899 case 'm': break;
8900 case NUL: len *= 1000L; break;
8901 default: EMSG2(_(e_invarg2), eap->arg); return;
8902 }
8903 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904}
8905
8906/*
8907 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8908 */
8909 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008910do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911{
8912 long done;
8913
8914 cursor_on();
8915 out_flush();
8916 for (done = 0; !got_int && done < msec; done += 1000L)
8917 {
8918 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8919 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008920#ifdef MESSAGE_QUEUE
8921 /* Process the netbeans and clientserver messages that may have been
8922 * received in the call to ui_breakcheck() when the GUI is in use. This
8923 * may occur when running a test case. */
8924 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 }
8927}
8928
8929 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008930do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931{
8932 int mode;
8933 char_u *cmdp;
8934
8935 cmdp = eap->cmd;
8936 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8937
8938 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8939 eap->arg, mode, isabbrev))
8940 {
8941 case 1: EMSG(_(e_invarg));
8942 break;
8943 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8944 break;
8945 }
8946}
8947
8948/*
8949 * ":winsize" command (obsolete).
8950 */
8951 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008952ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953{
8954 int w, h;
8955 char_u *arg = eap->arg;
8956 char_u *p;
8957
8958 w = getdigits(&arg);
8959 arg = skipwhite(arg);
8960 p = arg;
8961 h = getdigits(&arg);
8962 if (*p != NUL && *arg == NUL)
8963 set_shellsize(w, h, TRUE);
8964 else
8965 EMSG(_("E465: :winsize requires two number arguments"));
8966}
8967
8968#ifdef FEAT_WINDOWS
8969 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008970ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971{
8972 int xchar = NUL;
8973 char_u *p;
8974
8975 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8976 {
8977 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8978 if (eap->arg[1] == NUL)
8979 {
8980 EMSG(_(e_invarg));
8981 return;
8982 }
8983 xchar = eap->arg[1];
8984 p = eap->arg + 2;
8985 }
8986 else
8987 p = eap->arg + 1;
8988
8989 eap->nextcmd = check_nextcmd(p);
8990 p = skipwhite(p);
8991 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8992 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008993 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 {
8995 /* Pass flags on for ":vertical wincmd ]". */
8996 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008997 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8999 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009000 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 }
9002}
9003#endif
9004
Bram Moolenaar843ee412004-06-30 16:16:41 +00009005#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006/*
9007 * ":winpos".
9008 */
9009 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009010ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011{
9012 int x, y;
9013 char_u *arg = eap->arg;
9014 char_u *p;
9015
9016 if (*arg == NUL)
9017 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009018# if defined(FEAT_GUI) || defined(MSWIN)
9019# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009021# else
9022 if (mch_get_winpos(&x, &y) != FAIL)
9023# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024 {
9025 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9026 msg(IObuff);
9027 }
9028 else
9029# endif
9030 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9031 }
9032 else
9033 {
9034 x = getdigits(&arg);
9035 arg = skipwhite(arg);
9036 p = arg;
9037 y = getdigits(&arg);
9038 if (*p == NUL || *arg != NUL)
9039 {
9040 EMSG(_("E466: :winpos requires two number arguments"));
9041 return;
9042 }
9043# ifdef FEAT_GUI
9044 if (gui.in_use)
9045 gui_mch_set_winpos(x, y);
9046 else if (gui.starting)
9047 {
9048 /* Remember the coordinates for when the window is opened. */
9049 gui_win_x = x;
9050 gui_win_y = y;
9051 }
9052# ifdef HAVE_TGETENT
9053 else
9054# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009055# else
9056# ifdef MSWIN
9057 mch_set_winpos(x, y);
9058# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059# endif
9060# ifdef HAVE_TGETENT
9061 if (*T_CWP)
9062 term_set_winpos(x, y);
9063# endif
9064 }
9065}
9066#endif
9067
9068/*
9069 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9070 */
9071 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009072ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073{
9074 oparg_T oa;
9075
9076 clear_oparg(&oa);
9077 oa.regname = eap->regname;
9078 oa.start.lnum = eap->line1;
9079 oa.end.lnum = eap->line2;
9080 oa.line_count = eap->line2 - eap->line1 + 1;
9081 oa.motion_type = MLINE;
9082#ifdef FEAT_VIRTUALEDIT
9083 virtual_op = FALSE;
9084#endif
9085 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9086 {
9087 setpcmark();
9088 curwin->w_cursor.lnum = eap->line1;
9089 beginline(BL_SOL | BL_FIX);
9090 }
9091
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009092 if (VIsual_active)
9093 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009094
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 switch (eap->cmdidx)
9096 {
9097 case CMD_delete:
9098 oa.op_type = OP_DELETE;
9099 op_delete(&oa);
9100 break;
9101
9102 case CMD_yank:
9103 oa.op_type = OP_YANK;
9104 (void)op_yank(&oa, FALSE, TRUE);
9105 break;
9106
9107 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009108 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009110 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9111#else
9112 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009114 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 oa.op_type = OP_RSHIFT;
9116 else
9117 oa.op_type = OP_LSHIFT;
9118 op_shift(&oa, FALSE, eap->amount);
9119 break;
9120 }
9121#ifdef FEAT_VIRTUALEDIT
9122 virtual_op = MAYBE;
9123#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009124 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125}
9126
9127/*
9128 * ":put".
9129 */
9130 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009131ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132{
9133 /* ":0put" works like ":1put!". */
9134 if (eap->line2 == 0)
9135 {
9136 eap->line2 = 1;
9137 eap->forceit = TRUE;
9138 }
9139 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009140 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9141 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142}
9143
9144/*
9145 * Handle ":copy" and ":move".
9146 */
9147 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009148ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149{
9150 long n;
9151
Bram Moolenaaraa23b372015-09-08 18:46:31 +02009152 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 if (eap->arg == NULL) /* error detected */
9154 {
9155 eap->nextcmd = NULL;
9156 return;
9157 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009158 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159
9160 /*
9161 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9162 */
9163 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9164 {
9165 EMSG(_(e_invaddr));
9166 return;
9167 }
9168
9169 if (eap->cmdidx == CMD_move)
9170 {
9171 if (do_move(eap->line1, eap->line2, n) == FAIL)
9172 return;
9173 }
9174 else
9175 ex_copy(eap->line1, eap->line2, n);
9176 u_clearline();
9177 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009178 ex_may_print(eap);
9179}
9180
9181/*
9182 * Print the current line if flags were given to the Ex command.
9183 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009184 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009185ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009186{
9187 if (eap->flags != 0)
9188 {
9189 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9190 (eap->flags & EXFLAG_LIST));
9191 ex_no_reprint = TRUE;
9192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193}
9194
9195/*
9196 * ":smagic" and ":snomagic".
9197 */
9198 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009199ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200{
9201 int magic_save = p_magic;
9202
9203 p_magic = (eap->cmdidx == CMD_smagic);
9204 do_sub(eap);
9205 p_magic = magic_save;
9206}
9207
9208/*
9209 * ":join".
9210 */
9211 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009212ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213{
9214 curwin->w_cursor.lnum = eap->line1;
9215 if (eap->line1 == eap->line2)
9216 {
9217 if (eap->addr_count >= 2) /* :2,2join does nothing */
9218 return;
9219 if (eap->line2 == curbuf->b_ml.ml_line_count)
9220 {
9221 beep_flush();
9222 return;
9223 }
9224 ++eap->line2;
9225 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009226 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009228 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229}
9230
9231/*
9232 * ":[addr]@r" or ":[addr]*r": execute register
9233 */
9234 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009235ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236{
9237 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009238 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239
9240 curwin->w_cursor.lnum = eap->line2;
9241
9242#ifdef USE_ON_FLY_SCROLL
9243 dont_scroll = TRUE; /* disallow scrolling here */
9244#endif
9245
9246 /* get the register name. No name means to use the previous one */
9247 c = *eap->arg;
9248 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9249 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009250 /* Put the register in the typeahead buffer with the "silent" flag. */
9251 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9252 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009253 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 else
9257 {
9258 int save_efr = exec_from_reg;
9259
9260 exec_from_reg = TRUE;
9261
9262 /*
9263 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009264 * Continue until the stuff buffer is empty and all added characters
9265 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009267 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9269
9270 exec_from_reg = save_efr;
9271 }
9272}
9273
9274/*
9275 * ":!".
9276 */
9277 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009278ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279{
9280 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9281}
9282
9283/*
9284 * ":undo".
9285 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009287ex_undo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009289 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009290 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009291 else
9292 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293}
9294
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009295#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009296 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009297ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009298{
9299 char_u hash[UNDO_HASH_SIZE];
9300
9301 u_compute_hash(hash);
9302 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9303}
9304
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009305 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009306ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009307{
9308 char_u hash[UNDO_HASH_SIZE];
9309
9310 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009311 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009312}
9313#endif
9314
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315/*
9316 * ":redo".
9317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009319ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320{
9321 u_redo(1);
9322}
9323
9324/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009325 * ":earlier" and ":later".
9326 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009327 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009328ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009329{
9330 long count = 0;
9331 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009332 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009333 char_u *p = eap->arg;
9334
9335 if (*p == NUL)
9336 count = 1;
9337 else if (isdigit(*p))
9338 {
9339 count = getdigits(&p);
9340 switch (*p)
9341 {
9342 case 's': ++p; sec = TRUE; break;
9343 case 'm': ++p; sec = TRUE; count *= 60; break;
9344 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009345 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9346 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009347 }
9348 }
9349
9350 if (*p != NUL)
9351 EMSG2(_(e_invarg2), eap->arg);
9352 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009353 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9354 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009355}
9356
9357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 * ":redir": start/stop redirection.
9359 */
9360 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009361ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362{
9363 char *mode;
9364 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009365 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366
9367 if (STRICMP(eap->arg, "END") == 0)
9368 close_redir();
9369 else
9370 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009371 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009373 ++arg;
9374 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009376 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 mode = "a";
9378 }
9379 else
9380 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009381 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382
9383 close_redir();
9384
9385 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009386 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 if (fname == NULL)
9388 return;
9389#ifdef FEAT_BROWSE
9390 if (cmdmod.browse)
9391 {
9392 char_u *browseFile;
9393
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009394 browseFile = do_browse(BROWSE_SAVE,
9395 (char_u *)_("Save Redirection"),
9396 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 if (browseFile == NULL)
9398 return; /* operation cancelled */
9399 vim_free(fname);
9400 fname = browseFile;
9401 eap->forceit = TRUE; /* since dialog already asked */
9402 }
9403#endif
9404
9405 redir_fd = open_exfile(fname, eap->forceit, mode);
9406 vim_free(fname);
9407 }
9408#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009409 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 {
9411 /* redirect to a register a-z (resp. A-Z for appending) */
9412 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009413 ++arg;
9414 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009416 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009417 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009419 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009421 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009422 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009423 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009424 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009426 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009427 if (*arg == '>')
9428 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009429 /* Make register empty when not using @A-@Z and the
9430 * command is valid. */
9431 if (*arg == NUL && !isupper(redir_reg))
9432 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009434 }
9435 if (*arg != NUL)
9436 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009437 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009438 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009439 }
9440 }
9441 else if (*arg == '=' && arg[1] == '>')
9442 {
9443 int append;
9444
9445 /* redirect to a variable */
9446 close_redir();
9447 arg += 2;
9448
9449 if (*arg == '>')
9450 {
9451 ++arg;
9452 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 }
9454 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009455 append = FALSE;
9456
9457 if (var_redir_start(skipwhite(arg), append) == OK)
9458 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 }
9460#endif
9461
9462 /* TODO: redirect to a buffer */
9463
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009465 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009467
9468 /* Make sure redirection is not off. Can happen for cmdline completion
9469 * that indirectly invokes a command to catch its output. */
9470 if (redir_fd != NULL
9471#ifdef FEAT_EVAL
9472 || redir_reg || redir_vname
9473#endif
9474 )
9475 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476}
9477
9478/*
9479 * ":redraw": force redraw
9480 */
9481 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009482ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483{
9484 int r = RedrawingDisabled;
9485 int p = p_lz;
9486
9487 RedrawingDisabled = 0;
9488 p_lz = FALSE;
9489 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009490 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491#ifdef FEAT_TITLE
9492 if (need_maketitle)
9493 maketitle();
9494#endif
9495 RedrawingDisabled = r;
9496 p_lz = p;
9497
9498 /* Reset msg_didout, so that a message that's there is overwritten. */
9499 msg_didout = FALSE;
9500 msg_col = 0;
9501
Bram Moolenaar56667a52013-07-17 11:54:28 +02009502 /* No need to wait after an intentional redraw. */
9503 need_wait_return = FALSE;
9504
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 out_flush();
9506}
9507
9508/*
9509 * ":redrawstatus": force redraw of status line(s)
9510 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009512ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513{
9514#if defined(FEAT_WINDOWS)
9515 int r = RedrawingDisabled;
9516 int p = p_lz;
9517
9518 RedrawingDisabled = 0;
9519 p_lz = FALSE;
9520 if (eap->forceit)
9521 status_redraw_all();
9522 else
9523 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009524 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525 RedrawingDisabled = r;
9526 p_lz = p;
9527 out_flush();
9528#endif
9529}
9530
9531 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009532close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533{
9534 if (redir_fd != NULL)
9535 {
9536 fclose(redir_fd);
9537 redir_fd = NULL;
9538 }
9539#ifdef FEAT_EVAL
9540 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009541 if (redir_vname)
9542 {
9543 var_redir_stop();
9544 redir_vname = 0;
9545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546#endif
9547}
9548
9549#if defined(FEAT_SESSION) && defined(USE_CRNL)
9550# define MKSESSION_NL
9551static int mksession_nl = FALSE; /* use NL only in put_eol() */
9552#endif
9553
9554/*
9555 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9556 */
9557 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009558ex_mkrc(
9559 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560{
9561 FILE *fd;
9562 int failed = FALSE;
9563 char_u *fname;
9564#ifdef FEAT_BROWSE
9565 char_u *browseFile = NULL;
9566#endif
9567#ifdef FEAT_SESSION
9568 int view_session = FALSE;
9569 int using_vdir = FALSE; /* using 'viewdir'? */
9570 char_u *viewFile = NULL;
9571 unsigned *flagp;
9572#endif
9573
9574 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9575 {
9576#ifdef FEAT_SESSION
9577 view_session = TRUE;
9578#else
9579 ex_ni(eap);
9580 return;
9581#endif
9582 }
9583
9584#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009585 /* Use the short file name until ":lcd" is used. We also don't use the
9586 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009587 did_lcd = FALSE;
9588
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9590 if (eap->cmdidx == CMD_mkview
9591 && (*eap->arg == NUL
9592 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9593 {
9594 eap->forceit = TRUE;
9595 fname = get_view_file(*eap->arg);
9596 if (fname == NULL)
9597 return;
9598 viewFile = fname;
9599 using_vdir = TRUE;
9600 }
9601 else
9602#endif
9603 if (*eap->arg != NUL)
9604 fname = eap->arg;
9605 else if (eap->cmdidx == CMD_mkvimrc)
9606 fname = (char_u *)VIMRC_FILE;
9607#ifdef FEAT_SESSION
9608 else if (eap->cmdidx == CMD_mksession)
9609 fname = (char_u *)SESSION_FILE;
9610#endif
9611 else
9612 fname = (char_u *)EXRC_FILE;
9613
9614#ifdef FEAT_BROWSE
9615 if (cmdmod.browse)
9616 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009617 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618# ifdef FEAT_SESSION
9619 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9620 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9621# endif
9622 (char_u *)_("Save Setup"),
9623 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9624 if (browseFile == NULL)
9625 goto theend;
9626 fname = browseFile;
9627 eap->forceit = TRUE; /* since dialog already asked */
9628 }
9629#endif
9630
9631#if defined(FEAT_SESSION) && defined(vim_mkdir)
9632 /* When using 'viewdir' may have to create the directory. */
9633 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009634 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635#endif
9636
9637 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9638 if (fd != NULL)
9639 {
9640#ifdef FEAT_SESSION
9641 if (eap->cmdidx == CMD_mkview)
9642 flagp = &vop_flags;
9643 else
9644 flagp = &ssop_flags;
9645#endif
9646
9647#ifdef MKSESSION_NL
9648 /* "unix" in 'sessionoptions': use NL line separator */
9649 if (view_session && (*flagp & SSOP_UNIX))
9650 mksession_nl = TRUE;
9651#endif
9652
9653 /* Write the version command for :mkvimrc */
9654 if (eap->cmdidx == CMD_mkvimrc)
9655 (void)put_line(fd, "version 6.0");
9656
9657#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009658 if (eap->cmdidx == CMD_mksession)
9659 {
9660 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9661 failed = TRUE;
9662 }
9663
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 if (eap->cmdidx != CMD_mkview)
9665#endif
9666 {
9667 /* Write setting 'compatible' first, because it has side effects.
9668 * For that same reason only do it when needed. */
9669 if (p_cp)
9670 (void)put_line(fd, "if !&cp | set cp | endif");
9671 else
9672 (void)put_line(fd, "if &cp | set nocp | endif");
9673 }
9674
9675#ifdef FEAT_SESSION
9676 if (!view_session
9677 || (eap->cmdidx == CMD_mksession
9678 && (*flagp & SSOP_OPTIONS)))
9679#endif
9680 failed |= (makemap(fd, NULL) == FAIL
9681 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9682
9683#ifdef FEAT_SESSION
9684 if (!failed && view_session)
9685 {
9686 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9687 failed = TRUE;
9688 if (eap->cmdidx == CMD_mksession)
9689 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009690 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691
Bram Moolenaard9462e32011-04-11 21:35:11 +02009692 dirnow = alloc(MAXPATHL);
9693 if (dirnow == NULL)
9694 failed = TRUE;
9695 else
9696 {
9697 /*
9698 * Change to session file's dir.
9699 */
9700 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009702 *dirnow = NUL;
9703 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9704 {
9705 if (vim_chdirfile(fname) == OK)
9706 shorten_fnames(TRUE);
9707 }
9708 else if (*dirnow != NUL
9709 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9710 {
9711 if (mch_chdir((char *)globaldir) == 0)
9712 shorten_fnames(TRUE);
9713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714
Bram Moolenaard9462e32011-04-11 21:35:11 +02009715 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716
Bram Moolenaard9462e32011-04-11 21:35:11 +02009717 /* restore original dir */
9718 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009720 {
9721 if (mch_chdir((char *)dirnow) != 0)
9722 EMSG(_(e_prev_dir));
9723 shorten_fnames(TRUE);
9724 }
9725 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 }
9727 }
9728 else
9729 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009730 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9731 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 }
9733 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9734 == FAIL)
9735 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009736 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9737 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009738 if (eap->cmdidx == CMD_mksession)
9739 {
9740 if (put_line(fd, "unlet SessionLoad") == FAIL)
9741 failed = TRUE;
9742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 }
9744#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009745 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9746 failed = TRUE;
9747
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 failed |= fclose(fd);
9749
9750 if (failed)
9751 EMSG(_(e_write));
9752#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9753 else if (eap->cmdidx == CMD_mksession)
9754 {
9755 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009756 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757
Bram Moolenaard9462e32011-04-11 21:35:11 +02009758 tbuf = alloc(MAXPATHL);
9759 if (tbuf != NULL)
9760 {
9761 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9762 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9763 vim_free(tbuf);
9764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 }
9766#endif
9767#ifdef MKSESSION_NL
9768 mksession_nl = FALSE;
9769#endif
9770 }
9771
9772#ifdef FEAT_BROWSE
9773theend:
9774 vim_free(browseFile);
9775#endif
9776#ifdef FEAT_SESSION
9777 vim_free(viewFile);
9778#endif
9779}
9780
Bram Moolenaardf177f62005-02-22 08:39:57 +00009781#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9782 || defined(PROTO)
9783 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009784vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009785{
9786 if (vim_mkdir(name, prot) != 0)
9787 {
9788 EMSG2(_("E739: Cannot create directory: %s"), name);
9789 return FAIL;
9790 }
9791 return OK;
9792}
9793#endif
9794
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795/*
9796 * Open a file for writing for an Ex command, with some checks.
9797 * Return file descriptor, or NULL on failure.
9798 */
9799 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009800open_exfile(
9801 char_u *fname,
9802 int forceit,
9803 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804{
9805 FILE *fd;
9806
9807#ifdef UNIX
9808 /* with Unix it is possible to open a directory */
9809 if (mch_isdir(fname))
9810 {
9811 EMSG2(_(e_isadir2), fname);
9812 return NULL;
9813 }
9814#endif
9815 if (!forceit && *mode != 'a' && vim_fexists(fname))
9816 {
9817 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9818 return NULL;
9819 }
9820
9821 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9822 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9823
9824 return fd;
9825}
9826
9827/*
9828 * ":mark" and ":k".
9829 */
9830 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009831ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832{
9833 pos_T pos;
9834
9835 if (*eap->arg == NUL) /* No argument? */
9836 EMSG(_(e_argreq));
9837 else if (eap->arg[1] != NUL) /* more than one character? */
9838 EMSG(_(e_trailing));
9839 else
9840 {
9841 pos = curwin->w_cursor; /* save curwin->w_cursor */
9842 curwin->w_cursor.lnum = eap->line2;
9843 beginline(BL_WHITE | BL_FIX);
9844 if (setmark(*eap->arg) == FAIL) /* set mark */
9845 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9846 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9847 }
9848}
9849
9850/*
9851 * Update w_topline, w_leftcol and the cursor position.
9852 */
9853 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009854update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855{
9856 check_cursor(); /* put cursor on valid line */
9857 update_topline();
9858 if (!curwin->w_p_wrap)
9859 validate_cursor();
9860 update_curswant();
9861}
9862
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01009863#if defined(FEAT_EX_EXTRA) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864/*
9865 * ":normal[!] {commands}": Execute normal mode commands.
9866 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01009867 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009868ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 int save_msg_scroll = msg_scroll;
9871 int save_restart_edit = restart_edit;
9872 int save_msg_didout = msg_didout;
9873 int save_State = State;
9874 tasave_T tabuf;
9875 int save_insertmode = p_im;
9876 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009877 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878#ifdef FEAT_MBYTE
9879 char_u *arg = NULL;
9880 int l;
9881 char_u *p;
9882#endif
9883
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009884 if (ex_normal_lock > 0)
9885 {
9886 EMSG(_(e_secure));
9887 return;
9888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 if (ex_normal_busy >= p_mmd)
9890 {
9891 EMSG(_("E192: Recursive use of :normal too deep"));
9892 return;
9893 }
9894 ++ex_normal_busy;
9895
9896 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9897 restart_edit = 0; /* don't go to Insert mode */
9898 p_im = FALSE; /* don't use 'insertmode' */
9899
9900#ifdef FEAT_MBYTE
9901 /*
9902 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9903 * this for the K_SPECIAL leading byte, otherwise special keys will not
9904 * work.
9905 */
9906 if (has_mbyte)
9907 {
9908 int len = 0;
9909
9910 /* Count the number of characters to be escaped. */
9911 for (p = eap->arg; *p != NUL; ++p)
9912 {
9913# ifdef FEAT_GUI
9914 if (*p == CSI) /* leadbyte CSI */
9915 len += 2;
9916# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009917 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9919# ifdef FEAT_GUI
9920 || *p == CSI
9921# endif
9922 )
9923 len += 2;
9924 }
9925 if (len > 0)
9926 {
9927 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9928 if (arg != NULL)
9929 {
9930 len = 0;
9931 for (p = eap->arg; *p != NUL; ++p)
9932 {
9933 arg[len++] = *p;
9934# ifdef FEAT_GUI
9935 if (*p == CSI)
9936 {
9937 arg[len++] = KS_EXTRA;
9938 arg[len++] = (int)KE_CSI;
9939 }
9940# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009941 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942 {
9943 arg[len++] = *++p;
9944 if (*p == K_SPECIAL)
9945 {
9946 arg[len++] = KS_SPECIAL;
9947 arg[len++] = KE_FILLER;
9948 }
9949# ifdef FEAT_GUI
9950 else if (*p == CSI)
9951 {
9952 arg[len++] = KS_EXTRA;
9953 arg[len++] = (int)KE_CSI;
9954 }
9955# endif
9956 }
9957 arg[len] = NUL;
9958 }
9959 }
9960 }
9961 }
9962#endif
9963
9964 /*
9965 * Save the current typeahead. This is required to allow using ":normal"
9966 * from an event handler and makes sure we don't hang when the argument
9967 * ends with half a command.
9968 */
9969 save_typeahead(&tabuf);
9970 if (tabuf.typebuf_valid)
9971 {
9972 /*
9973 * Repeat the :normal command for each line in the range. When no
9974 * range given, execute it just once, without positioning the cursor
9975 * first.
9976 */
9977 do
9978 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 if (eap->addr_count != 0)
9980 {
9981 curwin->w_cursor.lnum = eap->line1++;
9982 curwin->w_cursor.col = 0;
9983 }
9984
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009985 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986#ifdef FEAT_MBYTE
9987 arg != NULL ? arg :
9988#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009989 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 }
9991 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9992 }
9993
9994 /* Might not return to the main loop when in an event handler. */
9995 update_topline_cursor();
9996
9997 /* Restore the previous typeahead. */
9998 restore_typeahead(&tabuf);
9999
10000 --ex_normal_busy;
10001 msg_scroll = save_msg_scroll;
10002 restart_edit = save_restart_edit;
10003 p_im = save_insertmode;
10004 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +000010005 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
10007
10008 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +010010009 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +010010011#ifdef FEAT_MOUSE
10012 setmouse();
10013#endif
10014#ifdef CURSOR_SHAPE
10015 ui_cursor_shape(); /* may show different cursor shape */
10016#endif
10017
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018#ifdef FEAT_MBYTE
10019 vim_free(arg);
10020#endif
10021}
10022
10023/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010024 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025 */
10026 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010027ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010029 if (eap->forceit)
10030 {
10031 coladvance((colnr_T)MAXCOL);
10032 curwin->w_curswant = MAXCOL;
10033 curwin->w_set_curswant = FALSE;
10034 }
10035
Bram Moolenaara40c5002005-01-09 21:16:21 +000010036 /* Ignore the command when already in Insert mode. Inserting an
10037 * expression register that invokes a function can do this. */
10038 if (State & INSERT)
10039 return;
10040
Bram Moolenaar64969662005-12-14 21:59:55 +000010041 if (eap->cmdidx == CMD_startinsert)
10042 restart_edit = 'a';
10043 else if (eap->cmdidx == CMD_startreplace)
10044 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010046 restart_edit = 'V';
10047
10048 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010050 if (eap->cmdidx == CMD_startinsert)
10051 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 curwin->w_curswant = 0; /* avoid MAXCOL */
10053 }
10054}
10055
10056/*
10057 * ":stopinsert"
10058 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010060ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061{
10062 restart_edit = 0;
10063 stop_insert_mode = TRUE;
10064}
10065#endif
10066
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010067#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
10068/*
10069 * Execute normal mode command "cmd".
10070 * "remap" can be REMAP_NONE or REMAP_YES.
10071 */
10072 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010073exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010074{
Bram Moolenaar25281632016-01-21 23:32:32 +010010075 /* Stuff the argument into the typeahead buffer. */
10076 ins_typebuf(cmd, remap, 0, TRUE, silent);
10077 exec_normal(FALSE);
10078}
10079#endif
10080
10081#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(FEAT_EVAL) \
10082 || defined(PROTO)
10083/*
10084 * Execute normal_cmd() until there is no typeahead left.
10085 */
10086 void
10087exec_normal(int was_typed)
10088{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010089 oparg_T oa;
10090
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010091 clear_oparg(&oa);
10092 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010093 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10094 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010095 {
10096 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010097 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010098 }
10099}
10100#endif
10101
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102#ifdef FEAT_FIND_ID
10103 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010104ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105{
10106 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10107 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10108 (linenr_T)1, (linenr_T)MAXLNUM);
10109}
10110
10111#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
10112/*
10113 * ":psearch"
10114 */
10115 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010116ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117{
10118 g_do_tagpreview = p_pvh;
10119 ex_findpat(eap);
10120 g_do_tagpreview = 0;
10121}
10122#endif
10123
10124 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010125ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126{
10127 int whole = TRUE;
10128 long n;
10129 char_u *p;
10130 int action;
10131
10132 switch (cmdnames[eap->cmdidx].cmd_name[2])
10133 {
10134 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10135 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10136 action = ACTION_GOTO;
10137 else
10138 action = ACTION_SHOW;
10139 break;
10140 case 'i': /* ":ilist" and ":dlist" */
10141 action = ACTION_SHOW_ALL;
10142 break;
10143 case 'u': /* ":ijump" and ":djump" */
10144 action = ACTION_GOTO;
10145 break;
10146 default: /* ":isplit" and ":dsplit" */
10147 action = ACTION_SPLIT;
10148 break;
10149 }
10150
10151 n = 1;
10152 if (vim_isdigit(*eap->arg)) /* get count */
10153 {
10154 n = getdigits(&eap->arg);
10155 eap->arg = skipwhite(eap->arg);
10156 }
10157 if (*eap->arg == '/') /* Match regexp, not just whole words */
10158 {
10159 whole = FALSE;
10160 ++eap->arg;
10161 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10162 if (*p)
10163 {
10164 *p++ = NUL;
10165 p = skipwhite(p);
10166
10167 /* Check for trailing illegal characters */
10168 if (!ends_excmd(*p))
10169 eap->errmsg = e_trailing;
10170 else
10171 eap->nextcmd = check_nextcmd(p);
10172 }
10173 }
10174 if (!eap->skip)
10175 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10176 whole, !eap->forceit,
10177 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10178 n, action, eap->line1, eap->line2);
10179}
10180#endif
10181
10182#ifdef FEAT_WINDOWS
10183
10184# ifdef FEAT_QUICKFIX
10185/*
10186 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10187 */
10188 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010189ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010191 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10193}
10194
10195/*
10196 * ":pedit"
10197 */
10198 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010199ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200{
10201 win_T *curwin_save = curwin;
10202
10203 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010204 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205 keep_help_flag = curwin_save->w_buffer->b_help;
10206 do_exedit(eap, NULL);
10207 keep_help_flag = FALSE;
10208 if (curwin != curwin_save && win_valid(curwin_save))
10209 {
10210 /* Return cursor to where we were */
10211 validate_cursor();
10212 redraw_later(VALID);
10213 win_enter(curwin_save, TRUE);
10214 }
10215 g_do_tagpreview = 0;
10216}
10217# endif
10218
10219/*
10220 * ":stag", ":stselect" and ":stjump".
10221 */
10222 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010223ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224{
10225 postponed_split = -1;
10226 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010227 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10229 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010230 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231}
10232#endif
10233
10234/*
10235 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10236 */
10237 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010238ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239{
10240 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10241}
10242
10243 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010244ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245{
10246 int cmd;
10247
10248 switch (name[1])
10249 {
10250 case 'j': cmd = DT_JUMP; /* ":tjump" */
10251 break;
10252 case 's': cmd = DT_SELECT; /* ":tselect" */
10253 break;
10254 case 'p': cmd = DT_PREV; /* ":tprevious" */
10255 break;
10256 case 'N': cmd = DT_PREV; /* ":tNext" */
10257 break;
10258 case 'n': cmd = DT_NEXT; /* ":tnext" */
10259 break;
10260 case 'o': cmd = DT_POP; /* ":pop" */
10261 break;
10262 case 'f': /* ":tfirst" */
10263 case 'r': cmd = DT_FIRST; /* ":trewind" */
10264 break;
10265 case 'l': cmd = DT_LAST; /* ":tlast" */
10266 break;
10267 default: /* ":tag" */
10268#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010269 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270 {
10271 do_cstag(eap);
10272 return;
10273 }
10274#endif
10275 cmd = DT_TAG;
10276 break;
10277 }
10278
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010279 if (name[0] == 'l')
10280 {
10281#ifndef FEAT_QUICKFIX
10282 ex_ni(eap);
10283 return;
10284#else
10285 cmd = DT_LTAG;
10286#endif
10287 }
10288
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10290 eap->forceit, TRUE);
10291}
10292
10293/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010294 * Check "str" for starting with a special cmdline variable.
10295 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10296 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10297 */
10298 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010299find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010300{
10301 int len;
10302 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010303 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010304 "%",
10305#define SPEC_PERC 0
10306 "#",
10307#define SPEC_HASH 1
10308 "<cword>", /* cursor word */
10309#define SPEC_CWORD 2
10310 "<cWORD>", /* cursor WORD */
10311#define SPEC_CCWORD 3
10312 "<cfile>", /* cursor path name */
10313#define SPEC_CFILE 4
10314 "<sfile>", /* ":so" file name */
10315#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010316 "<slnum>", /* ":so" file line number */
10317#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010318#ifdef FEAT_AUTOCMD
10319 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010320# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010321 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010322# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010323 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010324# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010325#endif
10326#ifdef FEAT_CLIENTSERVER
10327 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010328# ifdef FEAT_AUTOCMD
10329# define SPEC_CLIENT 10
10330# else
10331# define SPEC_CLIENT 7
10332# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010333#endif
10334 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010335
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010336 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010337 {
10338 len = (int)STRLEN(spec_str[i]);
10339 if (STRNCMP(src, spec_str[i], len) == 0)
10340 {
10341 *usedlen = len;
10342 return i;
10343 }
10344 }
10345 return -1;
10346}
10347
10348/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 * Evaluate cmdline variables.
10350 *
10351 * change '%' to curbuf->b_ffname
10352 * '#' to curwin->w_altfile
10353 * '<cword>' to word under the cursor
10354 * '<cWORD>' to WORD under the cursor
10355 * '<cfile>' to path name under the cursor
10356 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010357 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 * '<afile>' to file name for autocommand
10359 * '<abuf>' to buffer number for autocommand
10360 * '<amatch>' to matching name for autocommand
10361 *
10362 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10363 * "" for error without a message) and NULL is returned.
10364 * Returns an allocated string if a valid match was found.
10365 * Returns NULL if no match was found. "usedlen" then still contains the
10366 * number of characters to skip.
10367 */
10368 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010369eval_vars(
10370 char_u *src, /* pointer into commandline */
10371 char_u *srcstart, /* beginning of valid memory for src */
10372 int *usedlen, /* characters after src that are used */
10373 linenr_T *lnump, /* line number for :e command, or NULL */
10374 char_u **errormsg, /* pointer to error message */
10375 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010376 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377{
10378 int i;
10379 char_u *s;
10380 char_u *result;
10381 char_u *resultbuf = NULL;
10382 int resultlen;
10383 buf_T *buf;
10384 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10385 int spec_idx;
10386#ifdef FEAT_MODIFY_FNAME
10387 int skip_mod = FALSE;
10388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390
10391 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010392 if (escaped != NULL)
10393 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394
10395 /*
10396 * Check if there is something to do.
10397 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010398 spec_idx = find_cmdline_var(src, usedlen);
10399 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400 {
10401 *usedlen = 1;
10402 return NULL;
10403 }
10404
10405 /*
10406 * Skip when preceded with a backslash "\%" and "\#".
10407 * Note: In "\\%" the % is also not recognized!
10408 */
10409 if (src > srcstart && src[-1] == '\\')
10410 {
10411 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010412 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413 return NULL;
10414 }
10415
10416 /*
10417 * word or WORD under cursor
10418 */
10419 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10420 {
10421 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10422 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10423 if (resultlen == 0)
10424 {
10425 *errormsg = (char_u *)"";
10426 return NULL;
10427 }
10428 }
10429
10430 /*
10431 * '#': Alternate file name
10432 * '%': Current file name
10433 * File name under the cursor
10434 * File name for autocommand
10435 * and following modifiers
10436 */
10437 else
10438 {
10439 switch (spec_idx)
10440 {
10441 case SPEC_PERC: /* '%': current file */
10442 if (curbuf->b_fname == NULL)
10443 {
10444 result = (char_u *)"";
10445 valid = 0; /* Must have ":p:h" to be valid */
10446 }
10447 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 break;
10450
10451 case SPEC_HASH: /* '#' or "#99": alternate file */
10452 if (src[1] == '#') /* "##": the argument list */
10453 {
10454 result = arg_all();
10455 resultbuf = result;
10456 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010457 if (escaped != NULL)
10458 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459#ifdef FEAT_MODIFY_FNAME
10460 skip_mod = TRUE;
10461#endif
10462 break;
10463 }
10464 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010465 if (*s == '<') /* "#<99" uses v:oldfiles */
10466 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 i = (int)getdigits(&s);
10468 *usedlen = (int)(s - src); /* length of what we expand */
10469
Bram Moolenaard812df62008-11-09 12:46:09 +000010470 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010472 if (*usedlen < 2)
10473 {
10474 /* Should we give an error message for #<text? */
10475 *usedlen = 1;
10476 return NULL;
10477 }
10478#ifdef FEAT_EVAL
10479 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10480 (long)i);
10481 if (result == NULL)
10482 {
10483 *errormsg = (char_u *)"";
10484 return NULL;
10485 }
10486#else
10487 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490 }
10491 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010492 {
10493 buf = buflist_findnr(i);
10494 if (buf == NULL)
10495 {
10496 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10497 return NULL;
10498 }
10499 if (lnump != NULL)
10500 *lnump = ECMD_LAST;
10501 if (buf->b_fname == NULL)
10502 {
10503 result = (char_u *)"";
10504 valid = 0; /* Must have ":p:h" to be valid */
10505 }
10506 else
10507 result = buf->b_fname;
10508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 break;
10510
10511#ifdef FEAT_SEARCHPATH
10512 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010513 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514 if (result == NULL)
10515 {
10516 *errormsg = (char_u *)"";
10517 return NULL;
10518 }
10519 resultbuf = result; /* remember allocated string */
10520 break;
10521#endif
10522
10523#ifdef FEAT_AUTOCMD
10524 case SPEC_AFILE: /* file name for autocommand */
10525 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010526 if (result != NULL && !autocmd_fname_full)
10527 {
10528 /* Still need to turn the fname into a full path. It is
10529 * postponed to avoid a delay when <afile> is not used. */
10530 autocmd_fname_full = TRUE;
10531 result = FullName_save(autocmd_fname, FALSE);
10532 vim_free(autocmd_fname);
10533 autocmd_fname = result;
10534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535 if (result == NULL)
10536 {
10537 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10538 return NULL;
10539 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010540 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 break;
10542
10543 case SPEC_ABUF: /* buffer number for autocommand */
10544 if (autocmd_bufnr <= 0)
10545 {
10546 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10547 return NULL;
10548 }
10549 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10550 result = strbuf;
10551 break;
10552
10553 case SPEC_AMATCH: /* match name for autocommand */
10554 result = autocmd_match;
10555 if (result == NULL)
10556 {
10557 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10558 return NULL;
10559 }
10560 break;
10561
10562#endif
10563 case SPEC_SFILE: /* file name for ":so" command */
10564 result = sourcing_name;
10565 if (result == NULL)
10566 {
10567 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10568 return NULL;
10569 }
10570 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010571 case SPEC_SLNUM: /* line in file for ":so" command */
10572 if (sourcing_name == NULL || sourcing_lnum == 0)
10573 {
10574 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10575 return NULL;
10576 }
10577 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10578 result = strbuf;
10579 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580#if defined(FEAT_CLIENTSERVER)
10581 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010582 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10583 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010584 result = strbuf;
10585 break;
10586#endif
10587 }
10588
10589 resultlen = (int)STRLEN(result); /* length of new string */
10590 if (src[*usedlen] == '<') /* remove the file name extension */
10591 {
10592 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594 resultlen = (int)(s - result);
10595 }
10596#ifdef FEAT_MODIFY_FNAME
10597 else if (!skip_mod)
10598 {
10599 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10600 &resultlen);
10601 if (result == NULL)
10602 {
10603 *errormsg = (char_u *)"";
10604 return NULL;
10605 }
10606 }
10607#endif
10608 }
10609
10610 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10611 {
10612 if (valid != VALID_HEAD + VALID_PATH)
10613 /* xgettext:no-c-format */
10614 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10615 else
10616 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10617 result = NULL;
10618 }
10619 else
10620 result = vim_strnsave(result, resultlen);
10621 vim_free(resultbuf);
10622 return result;
10623}
10624
10625/*
10626 * Concatenate all files in the argument list, separated by spaces, and return
10627 * it in one allocated string.
10628 * Spaces and backslashes in the file names are escaped with a backslash.
10629 * Returns NULL when out of memory.
10630 */
10631 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010632arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633{
10634 int len;
10635 int idx;
10636 char_u *retval = NULL;
10637 char_u *p;
10638
10639 /*
10640 * Do this loop two times:
10641 * first time: compute the total length
10642 * second time: concatenate the names
10643 */
10644 for (;;)
10645 {
10646 len = 0;
10647 for (idx = 0; idx < ARGCOUNT; ++idx)
10648 {
10649 p = alist_name(&ARGLIST[idx]);
10650 if (p != NULL)
10651 {
10652 if (len > 0)
10653 {
10654 /* insert a space in between names */
10655 if (retval != NULL)
10656 retval[len] = ' ';
10657 ++len;
10658 }
10659 for ( ; *p != NUL; ++p)
10660 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010661 if (*p == ' '
10662#ifndef BACKSLASH_IN_FILENAME
10663 || *p == '\\'
10664#endif
10665 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 {
10667 /* insert a backslash */
10668 if (retval != NULL)
10669 retval[len] = '\\';
10670 ++len;
10671 }
10672 if (retval != NULL)
10673 retval[len] = *p;
10674 ++len;
10675 }
10676 }
10677 }
10678
10679 /* second time: break here */
10680 if (retval != NULL)
10681 {
10682 retval[len] = NUL;
10683 break;
10684 }
10685
10686 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010687 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688 if (retval == NULL)
10689 break;
10690 }
10691
10692 return retval;
10693}
10694
10695#if defined(FEAT_AUTOCMD) || defined(PROTO)
10696/*
10697 * Expand the <sfile> string in "arg".
10698 *
10699 * Returns an allocated string, or NULL for any error.
10700 */
10701 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010702expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703{
10704 char_u *errormsg;
10705 int len;
10706 char_u *result;
10707 char_u *newres;
10708 char_u *repl;
10709 int srclen;
10710 char_u *p;
10711
10712 result = vim_strsave(arg);
10713 if (result == NULL)
10714 return NULL;
10715
10716 for (p = result; *p; )
10717 {
10718 if (STRNCMP(p, "<sfile>", 7) != 0)
10719 ++p;
10720 else
10721 {
10722 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010723 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 if (errormsg != NULL)
10725 {
10726 if (*errormsg)
10727 emsg(errormsg);
10728 vim_free(result);
10729 return NULL;
10730 }
10731 if (repl == NULL) /* no match (cannot happen) */
10732 {
10733 p += srclen;
10734 continue;
10735 }
10736 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10737 newres = alloc(len);
10738 if (newres == NULL)
10739 {
10740 vim_free(repl);
10741 vim_free(result);
10742 return NULL;
10743 }
10744 mch_memmove(newres, result, (size_t)(p - result));
10745 STRCPY(newres + (p - result), repl);
10746 len = (int)STRLEN(newres);
10747 STRCAT(newres, p + srclen);
10748 vim_free(repl);
10749 vim_free(result);
10750 result = newres;
10751 p = newres + len; /* continue after the match */
10752 }
10753 }
10754
10755 return result;
10756}
10757#endif
10758
10759#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010010760static int ses_winsizes(FILE *fd, int restore_size,
10761 win_T *tab_firstwin);
10762static int ses_win_rec(FILE *fd, frame_T *fr);
10763static frame_T *ses_skipframe(frame_T *fr);
10764static int ses_do_frame(frame_T *fr);
10765static int ses_do_win(win_T *wp);
10766static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
10767static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
10768static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769
10770/*
10771 * Write openfile commands for the current buffers to an .exrc file.
10772 * Return FAIL on error, OK otherwise.
10773 */
10774 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010775makeopens(
10776 FILE *fd,
10777 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778{
10779 buf_T *buf;
10780 int only_save_windows = TRUE;
10781 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782 int restore_size = TRUE;
10783 win_T *wp;
10784 char_u *sname;
10785 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010786 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010787 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010788 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010789 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010790 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010791 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792
10793 if (ssop_flags & SSOP_BUFFERS)
10794 only_save_windows = FALSE; /* Save ALL buffers */
10795
10796 /*
10797 * Begin by setting the this_session variable, and then other
10798 * sessionable variables.
10799 */
10800#ifdef FEAT_EVAL
10801 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10802 return FAIL;
10803 if (ssop_flags & SSOP_GLOBALS)
10804 if (store_session_globals(fd) == FAIL)
10805 return FAIL;
10806#endif
10807
10808 /*
10809 * Close all windows but one.
10810 */
10811 if (put_line(fd, "silent only") == FAIL)
10812 return FAIL;
10813
10814 /*
10815 * Now a :cd command to the session directory or the current directory
10816 */
10817 if (ssop_flags & SSOP_SESDIR)
10818 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010819 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10820 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010821 return FAIL;
10822 }
10823 else if (ssop_flags & SSOP_CURDIR)
10824 {
10825 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10826 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010827 || fputs("cd ", fd) < 0
10828 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10829 || put_eol(fd) == FAIL)
10830 {
10831 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 vim_free(sname);
10835 }
10836
10837 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010838 * If there is an empty, unnamed buffer we will wipe it out later.
10839 * Remember the buffer number.
10840 */
10841 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10842 return FAIL;
10843 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10844 return FAIL;
10845 if (put_line(fd, "endif") == FAIL)
10846 return FAIL;
10847
10848 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849 * Now save the current files, current buffer first.
10850 */
10851 if (put_line(fd, "set shortmess=aoO") == FAIL)
10852 return FAIL;
10853
10854 /* Now put the other buffers into the buffer list */
10855 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10856 {
10857 if (!(only_save_windows && buf->b_nwindows == 0)
10858 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10859 && buf->b_fname != NULL
10860 && buf->b_p_bl)
10861 {
10862 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10863 : buf->b_wininfo->wi_fpos.lnum) < 0
10864 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10865 return FAIL;
10866 }
10867 }
10868
10869 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010870 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10872 return FAIL;
10873
10874 if (ssop_flags & SSOP_RESIZE)
10875 {
10876 /* Note: after the restore we still check it worked!*/
10877 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10878 || put_eol(fd) == FAIL)
10879 return FAIL;
10880 }
10881
10882#ifdef FEAT_GUI
10883 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10884 {
10885 int x, y;
10886
10887 if (gui_mch_get_winpos(&x, &y) == OK)
10888 {
10889 /* Note: after the restore we still check it worked!*/
10890 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10891 return FAIL;
10892 }
10893 }
10894#endif
10895
10896 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010897 * When there are two or more tabpages and 'showtabline' is 1 the tabline
10898 * will be displayed when creating the next tab. That resizes the windows
10899 * in the first tab, which may cause problems. Set 'showtabline' to 2
10900 * temporarily to avoid that.
10901 */
10902 if (p_stal == 1 && first_tabpage->tp_next != NULL)
10903 {
10904 if (put_line(fd, "set stal=2") == FAIL)
10905 return FAIL;
10906 restore_stal = TRUE;
10907 }
10908
10909 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010910 * May repeat putting Windows for each tab, when "tabpages" is in
10911 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010912 * Don't use goto_tabpage(), it may change directory and trigger
10913 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010915 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010916 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010917 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020010919 int need_tabnew = FALSE;
10920 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010921
Bram Moolenaar18144c82006-04-12 21:52:12 +000010922 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010924 tabpage_T *tp = find_tabpage(tabnr);
10925
10926 if (tp == NULL)
10927 break; /* done all tab pages */
10928 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010929 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010930 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010931 tab_topframe = topframe;
10932 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010933 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010934 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010935 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010936 tab_topframe = tp->tp_topframe;
10937 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010938 if (tabnr > 1)
10939 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941
Bram Moolenaar18144c82006-04-12 21:52:12 +000010942 /*
10943 * Before creating the window layout, try loading one file. If this
10944 * is aborted we don't end up with a number of useless windows.
10945 * This may have side effects! (e.g., compressed or network file).
10946 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010947 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010948 {
10949 if (ses_do_win(wp)
10950 && wp->w_buffer->b_ffname != NULL
10951 && !wp->w_buffer->b_help
10952#ifdef FEAT_QUICKFIX
10953 && !bt_nofile(wp->w_buffer)
10954#endif
10955 )
10956 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010957 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010958 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10959 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010960 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010961 if (!wp->w_arg_idx_invalid)
10962 edited_win = wp;
10963 break;
10964 }
10965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010967 /* If no file got edited create an empty tab page. */
10968 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10969 return FAIL;
10970
Bram Moolenaar18144c82006-04-12 21:52:12 +000010971 /*
10972 * Save current window layout.
10973 */
10974 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010976 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010978 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10979 return FAIL;
10980 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10981 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982
Bram Moolenaar18144c82006-04-12 21:52:12 +000010983 /*
10984 * Check if window sizes can be restored (no windows omitted).
10985 * Remember the window number of the current window after restoring.
10986 */
10987 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010988 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010989 {
10990 if (ses_do_win(wp))
10991 ++nr;
10992 else
10993 restore_size = FALSE;
10994 if (curwin == wp)
10995 cnr = nr;
10996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997
Bram Moolenaar18144c82006-04-12 21:52:12 +000010998 /* Go to the first window. */
10999 if (put_line(fd, "wincmd t") == FAIL)
11000 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011001
Bram Moolenaar18144c82006-04-12 21:52:12 +000011002 /*
11003 * If more than one window, see if sizes can be restored.
11004 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11005 * resized when moving between windows.
11006 * Do this before restoring the view, so that the topline and the
11007 * cursor can be set. This is done again below.
11008 */
11009 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
11010 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011011 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011012 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013
Bram Moolenaar18144c82006-04-12 21:52:12 +000011014 /*
11015 * Restore the view of the window (options, file, cursor, etc.).
11016 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011017 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011018 {
11019 if (!ses_do_win(wp))
11020 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011021 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11022 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011023 return FAIL;
11024 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11025 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011026 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011027 }
11028
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011029 /* The argument index in the first tab page is zero, need to set it in
11030 * each window. For further tab pages it's the window where we do
11031 * "tabedit". */
11032 cur_arg_idx = next_arg_idx;
11033
Bram Moolenaar18144c82006-04-12 21:52:12 +000011034 /*
11035 * Restore cursor to the current window if it's not the first one.
11036 */
11037 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11038 || put_eol(fd) == FAIL))
11039 return FAIL;
11040
11041 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011042 * Restore window sizes again after jumping around in windows, because
11043 * the current window has a minimum size while others may not.
11044 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011045 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011046 return FAIL;
11047
Bram Moolenaar18144c82006-04-12 21:52:12 +000011048 /* Don't continue in another tab page when doing only the current one
11049 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011050 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011051 break;
11052 }
11053
11054 if (ssop_flags & SSOP_TABPAGES)
11055 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011056 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11057 || put_eol(fd) == FAIL)
11058 return FAIL;
11059 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011060 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11061 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011062
Bram Moolenaar9c102382006-05-03 21:26:49 +000011063 /*
11064 * Wipe out an empty unnamed buffer we started in.
11065 */
11066 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11067 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011068 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011069 return FAIL;
11070 if (put_line(fd, "endif") == FAIL)
11071 return FAIL;
11072 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11073 return FAIL;
11074
11075 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11076 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11077 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11078 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079
11080 /*
11081 * Lastly, execute the x.vim file if it exists.
11082 */
11083 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11084 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011085 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086 || put_line(fd, "endif") == FAIL)
11087 return FAIL;
11088
11089 return OK;
11090}
11091
11092 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011093ses_winsizes(
11094 FILE *fd,
11095 int restore_size,
11096 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097{
11098 int n = 0;
11099 win_T *wp;
11100
11101 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11102 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011103 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104 {
11105 if (!ses_do_win(wp))
11106 continue;
11107 ++n;
11108
11109 /* restore height when not full height */
11110 if (wp->w_height + wp->w_status_height < topframe->fr_height
11111 && (fprintf(fd,
11112 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11113 n, (long)wp->w_height, Rows / 2, Rows) < 0
11114 || put_eol(fd) == FAIL))
11115 return FAIL;
11116
11117 /* restore width when not full width */
11118 if (wp->w_width < Columns && (fprintf(fd,
11119 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11120 n, (long)wp->w_width, Columns / 2, Columns) < 0
11121 || put_eol(fd) == FAIL))
11122 return FAIL;
11123 }
11124 }
11125 else
11126 {
11127 /* Just equalise window sizes */
11128 if (put_line(fd, "wincmd =") == FAIL)
11129 return FAIL;
11130 }
11131 return OK;
11132}
11133
11134/*
11135 * Write commands to "fd" to recursively create windows for frame "fr",
11136 * horizontally and vertically split.
11137 * After the commands the last window in the frame is the current window.
11138 * Returns FAIL when writing the commands to "fd" fails.
11139 */
11140 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011141ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142{
11143 frame_T *frc;
11144 int count = 0;
11145
11146 if (fr->fr_layout != FR_LEAF)
11147 {
11148 /* Find first frame that's not skipped and then create a window for
11149 * each following one (first frame is already there). */
11150 frc = ses_skipframe(fr->fr_child);
11151 if (frc != NULL)
11152 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11153 {
11154 /* Make window as big as possible so that we have lots of room
11155 * to split. */
11156 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11157 || put_line(fd, fr->fr_layout == FR_COL
11158 ? "split" : "vsplit") == FAIL)
11159 return FAIL;
11160 ++count;
11161 }
11162
11163 /* Go back to the first window. */
11164 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11165 ? "%dwincmd k" : "%dwincmd h", count) < 0
11166 || put_eol(fd) == FAIL))
11167 return FAIL;
11168
11169 /* Recursively create frames/windows in each window of this column or
11170 * row. */
11171 frc = ses_skipframe(fr->fr_child);
11172 while (frc != NULL)
11173 {
11174 ses_win_rec(fd, frc);
11175 frc = ses_skipframe(frc->fr_next);
11176 /* Go to next window. */
11177 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11178 return FAIL;
11179 }
11180 }
11181 return OK;
11182}
11183
11184/*
11185 * Skip frames that don't contain windows we want to save in the Session.
11186 * Returns NULL when there none.
11187 */
11188 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011189ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011190{
11191 frame_T *frc;
11192
11193 for (frc = fr; frc != NULL; frc = frc->fr_next)
11194 if (ses_do_frame(frc))
11195 break;
11196 return frc;
11197}
11198
11199/*
11200 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11201 * the Session.
11202 */
11203 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011204ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205{
11206 frame_T *frc;
11207
11208 if (fr->fr_layout == FR_LEAF)
11209 return ses_do_win(fr->fr_win);
11210 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11211 if (ses_do_frame(frc))
11212 return TRUE;
11213 return FALSE;
11214}
11215
11216/*
11217 * Return non-zero if window "wp" is to be stored in the Session.
11218 */
11219 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011220ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221{
11222 if (wp->w_buffer->b_fname == NULL
11223#ifdef FEAT_QUICKFIX
11224 /* When 'buftype' is "nofile" can't restore the window contents. */
11225 || bt_nofile(wp->w_buffer)
11226#endif
11227 )
11228 return (ssop_flags & SSOP_BLANK);
11229 if (wp->w_buffer->b_help)
11230 return (ssop_flags & SSOP_HELP);
11231 return TRUE;
11232}
11233
11234/*
11235 * Write commands to "fd" to restore the view of a window.
11236 * Caller must make sure 'scrolloff' is zero.
11237 */
11238 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011239put_view(
11240 FILE *fd,
11241 win_T *wp,
11242 int add_edit, /* add ":edit" command to view */
11243 unsigned *flagp, /* vop_flags or ssop_flags */
11244 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011245 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246{
11247 win_T *save_curwin;
11248 int f;
11249 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011250 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251
11252 /* Always restore cursor position for ":mksession". For ":mkview" only
11253 * when 'viewoptions' contains "cursor". */
11254 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11255
11256 /*
11257 * Local argument list.
11258 */
11259 if (wp->w_alist == &global_alist)
11260 {
11261 if (put_line(fd, "argglobal") == FAIL)
11262 return FAIL;
11263 }
11264 else
11265 {
11266 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11267 flagp == &vop_flags
11268 || !(*flagp & SSOP_CURDIR)
11269 || wp->w_localdir != NULL, flagp) == FAIL)
11270 return FAIL;
11271 }
11272
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011273 /* Only when part of a session: restore the argument index. Some
11274 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011275 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011276 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011278 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011279 || put_eol(fd) == FAIL)
11280 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011281 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282 }
11283
11284 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011285 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286 {
11287 /*
11288 * Load the file.
11289 */
11290 if (wp->w_buffer->b_ffname != NULL
11291#ifdef FEAT_QUICKFIX
11292 && !bt_nofile(wp->w_buffer)
11293#endif
11294 )
11295 {
11296 /*
11297 * Editing a file in this buffer: use ":edit file".
11298 * This may have side effects! (e.g., compressed or network file).
11299 */
11300 if (fputs("edit ", fd) < 0
11301 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11302 return FAIL;
11303 }
11304 else
11305 {
11306 /* No file in this buffer, just make it empty. */
11307 if (put_line(fd, "enew") == FAIL)
11308 return FAIL;
11309#ifdef FEAT_QUICKFIX
11310 if (wp->w_buffer->b_ffname != NULL)
11311 {
11312 /* The buffer does have a name, but it's not a file name. */
11313 if (fputs("file ", fd) < 0
11314 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11315 return FAIL;
11316 }
11317#endif
11318 do_cursor = FALSE;
11319 }
11320 }
11321
11322 /*
11323 * Local mappings and abbreviations.
11324 */
11325 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11326 && makemap(fd, wp->w_buffer) == FAIL)
11327 return FAIL;
11328
11329 /*
11330 * Local options. Need to go to the window temporarily.
11331 * Store only local values when using ":mkview" and when ":mksession" is
11332 * used and 'sessionoptions' doesn't include "options".
11333 * Some folding options are always stored when "folds" is included,
11334 * otherwise the folds would not be restored correctly.
11335 */
11336 save_curwin = curwin;
11337 curwin = wp;
11338 curbuf = curwin->w_buffer;
11339 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11340 f = makeset(fd, OPT_LOCAL,
11341 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11342#ifdef FEAT_FOLDING
11343 else if (*flagp & SSOP_FOLDS)
11344 f = makefoldset(fd);
11345#endif
11346 else
11347 f = OK;
11348 curwin = save_curwin;
11349 curbuf = curwin->w_buffer;
11350 if (f == FAIL)
11351 return FAIL;
11352
11353#ifdef FEAT_FOLDING
11354 /*
11355 * Save Folds when 'buftype' is empty and for help files.
11356 */
11357 if ((*flagp & SSOP_FOLDS)
11358 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011359# ifdef FEAT_QUICKFIX
11360 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11361# endif
11362 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363 {
11364 if (put_folds(fd, wp) == FAIL)
11365 return FAIL;
11366 }
11367#endif
11368
11369 /*
11370 * Set the cursor after creating folds, since that moves the cursor.
11371 */
11372 if (do_cursor)
11373 {
11374
11375 /* Restore the cursor line in the file and relatively in the
11376 * window. Don't use "G", it changes the jumplist. */
11377 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11378 (long)wp->w_cursor.lnum,
11379 (long)(wp->w_cursor.lnum - wp->w_topline),
11380 (long)wp->w_height / 2, (long)wp->w_height) < 0
11381 || put_eol(fd) == FAIL
11382 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11383 || put_line(fd, "exe s:l") == FAIL
11384 || put_line(fd, "normal! zt") == FAIL
11385 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11386 || put_eol(fd) == FAIL)
11387 return FAIL;
11388 /* Restore the cursor column and left offset when not wrapping. */
11389 if (wp->w_cursor.col == 0)
11390 {
11391 if (put_line(fd, "normal! 0") == FAIL)
11392 return FAIL;
11393 }
11394 else
11395 {
11396 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11397 {
11398 if (fprintf(fd,
11399 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011400 (long)wp->w_virtcol + 1,
11401 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402 (long)wp->w_width / 2, (long)wp->w_width) < 0
11403 || put_eol(fd) == FAIL
11404 || put_line(fd, "if s:c > 0") == FAIL
11405 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011406 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11407 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408 || put_eol(fd) == FAIL
11409 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011410 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011411 || put_eol(fd) == FAIL
11412 || put_line(fd, "endif") == FAIL)
11413 return FAIL;
11414 }
11415 else
11416 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011417 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011418 || put_eol(fd) == FAIL)
11419 return FAIL;
11420 }
11421 }
11422 }
11423
11424 /*
11425 * Local directory.
11426 */
11427 if (wp->w_localdir != NULL)
11428 {
11429 if (fputs("lcd ", fd) < 0
11430 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11431 || put_eol(fd) == FAIL)
11432 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011433 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434 }
11435
11436 return OK;
11437}
11438
11439/*
11440 * Write an argument list to the session file.
11441 * Returns FAIL if writing fails.
11442 */
11443 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011444ses_arglist(
11445 FILE *fd,
11446 char *cmd,
11447 garray_T *gap,
11448 int fullname, /* TRUE: use full path name */
11449 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450{
11451 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011452 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011453 char_u *s;
11454
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011455 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11456 return FAIL;
11457 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011458 return FAIL;
11459 for (i = 0; i < gap->ga_len; ++i)
11460 {
11461 /* NULL file names are skipped (only happens when out of memory). */
11462 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11463 if (s != NULL)
11464 {
11465 if (fullname)
11466 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011467 buf = alloc(MAXPATHL);
11468 if (buf != NULL)
11469 {
11470 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11471 s = buf;
11472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011474 if (fputs("argadd ", fd) < 0
11475 || ses_put_fname(fd, s, flagp) == FAIL
11476 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011477 {
11478 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011480 }
11481 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482 }
11483 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011484 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485}
11486
11487/*
11488 * Write a buffer name to the session file.
11489 * Also ends the line.
11490 * Returns FAIL if writing fails.
11491 */
11492 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011493ses_fname(FILE *fd, buf_T *buf, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011494{
11495 char_u *name;
11496
11497 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011498 * the session file will be sourced.
11499 * Don't do this for ":mkview", we don't know the current directory.
11500 * Don't do this after ":lcd", we don't keep track of what the current
11501 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502 if (buf->b_sfname != NULL
11503 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011504 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011505#ifdef FEAT_AUTOCHDIR
11506 && !p_acd
11507#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011508 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011509 name = buf->b_sfname;
11510 else
11511 name = buf->b_ffname;
11512 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11513 return FAIL;
11514 return OK;
11515}
11516
11517/*
11518 * Write a file name to the session file.
11519 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11520 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011521 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011522 */
11523 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011524ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011525{
11526 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011527 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011528 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011529
11530 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011531 if (sname == NULL)
11532 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011533
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011534 if (*flagp & SSOP_SLASH)
11535 {
11536 /* change all backslashes to forward slashes */
11537 for (p = sname; *p != NUL; mb_ptr_adv(p))
11538 if (*p == '\\')
11539 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011540 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011541
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011542 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011543 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011544 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011545 if (p == NULL)
11546 return FAIL;
11547
11548 /* write the result */
11549 if (fputs((char *)p, fd) < 0)
11550 retval = FAIL;
11551
11552 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553 return retval;
11554}
11555
11556/*
11557 * ":loadview [nr]"
11558 */
11559 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011560ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011561{
11562 char_u *fname;
11563
11564 fname = get_view_file(*eap->arg);
11565 if (fname != NULL)
11566 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011567 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011568 vim_free(fname);
11569 }
11570}
11571
11572/*
11573 * Get the name of the view file for the current buffer.
11574 */
11575 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011576get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011577{
11578 int len = 0;
11579 char_u *p, *s;
11580 char_u *retval;
11581 char_u *sname;
11582
11583 if (curbuf->b_ffname == NULL)
11584 {
11585 EMSG(_(e_noname));
11586 return NULL;
11587 }
11588 sname = home_replace_save(NULL, curbuf->b_ffname);
11589 if (sname == NULL)
11590 return NULL;
11591
11592 /*
11593 * We want a file name without separators, because we're not going to make
11594 * a directory.
11595 * "normal" path separator -> "=+"
11596 * "=" -> "=="
11597 * ":" path separator -> "=-"
11598 */
11599 for (p = sname; *p; ++p)
11600 if (*p == '=' || vim_ispathsep(*p))
11601 ++len;
11602 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11603 if (retval != NULL)
11604 {
11605 STRCPY(retval, p_vdir);
11606 add_pathsep(retval);
11607 s = retval + STRLEN(retval);
11608 for (p = sname; *p; ++p)
11609 {
11610 if (*p == '=')
11611 {
11612 *s++ = '=';
11613 *s++ = '=';
11614 }
11615 else if (vim_ispathsep(*p))
11616 {
11617 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011618#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011619 if (*p == ':')
11620 *s++ = '-';
11621 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011622#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011623 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011624 }
11625 else
11626 *s++ = *p;
11627 }
11628 *s++ = '=';
11629 *s++ = c;
11630 STRCPY(s, ".vim");
11631 }
11632
11633 vim_free(sname);
11634 return retval;
11635}
11636
11637#endif /* FEAT_SESSION */
11638
11639/*
11640 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11641 * Return FAIL for a write error.
11642 */
11643 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011644put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011645{
11646 if (
11647#ifdef USE_CRNL
11648 (
11649# ifdef MKSESSION_NL
11650 !mksession_nl &&
11651# endif
11652 (putc('\r', fd) < 0)) ||
11653#endif
11654 (putc('\n', fd) < 0))
11655 return FAIL;
11656 return OK;
11657}
11658
11659/*
11660 * Write a line to "fd".
11661 * Return FAIL for a write error.
11662 */
11663 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011664put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011665{
11666 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11667 return FAIL;
11668 return OK;
11669}
11670
11671#ifdef FEAT_VIMINFO
11672/*
11673 * ":rviminfo" and ":wviminfo".
11674 */
11675 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011676ex_viminfo(
11677 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011678{
11679 char_u *save_viminfo;
11680
11681 save_viminfo = p_viminfo;
11682 if (*p_viminfo == NUL)
11683 p_viminfo = (char_u *)"'100";
11684 if (eap->cmdidx == CMD_rviminfo)
11685 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011686 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11687 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688 EMSG(_("E195: Cannot open viminfo file for reading"));
11689 }
11690 else
11691 write_viminfo(eap->arg, eap->forceit);
11692 p_viminfo = save_viminfo;
11693}
11694#endif
11695
11696#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011697/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011698 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011699 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011700 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011701 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011702dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011703{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011704 if (fname == NULL)
11705 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011706 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011707}
11708#endif
11709
11710/*
11711 * ":behave {mswin,xterm}"
11712 */
11713 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011714ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011715{
11716 if (STRCMP(eap->arg, "mswin") == 0)
11717 {
11718 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11719 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11720 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11721 set_option_value((char_u *)"keymodel", 0L,
11722 (char_u *)"startsel,stopsel", 0);
11723 }
11724 else if (STRCMP(eap->arg, "xterm") == 0)
11725 {
11726 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11727 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11728 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11729 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11730 }
11731 else
11732 EMSG2(_(e_invarg2), eap->arg);
11733}
11734
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011735#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11736/*
11737 * Function given to ExpandGeneric() to obtain the possible arguments of the
11738 * ":behave {mswin,xterm}" command.
11739 */
11740 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011741get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011742{
11743 if (idx == 0)
11744 return (char_u *)"mswin";
11745 if (idx == 1)
11746 return (char_u *)"xterm";
11747 return NULL;
11748}
11749#endif
11750
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751#ifdef FEAT_AUTOCMD
11752static int filetype_detect = FALSE;
11753static int filetype_plugin = FALSE;
11754static int filetype_indent = FALSE;
11755
11756/*
11757 * ":filetype [plugin] [indent] {on,off,detect}"
11758 * on: Load the filetype.vim file to install autocommands for file types.
11759 * off: Load the ftoff.vim file to remove all autocommands for file types.
11760 * plugin on: load filetype.vim and ftplugin.vim
11761 * plugin off: load ftplugof.vim
11762 * indent on: load filetype.vim and indent.vim
11763 * indent off: load indoff.vim
11764 */
11765 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011766ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011767{
11768 char_u *arg = eap->arg;
11769 int plugin = FALSE;
11770 int indent = FALSE;
11771
11772 if (*eap->arg == NUL)
11773 {
11774 /* Print current status. */
11775 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11776 filetype_detect ? "ON" : "OFF",
11777 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11778 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11779 return;
11780 }
11781
11782 /* Accept "plugin" and "indent" in any order. */
11783 for (;;)
11784 {
11785 if (STRNCMP(arg, "plugin", 6) == 0)
11786 {
11787 plugin = TRUE;
11788 arg = skipwhite(arg + 6);
11789 continue;
11790 }
11791 if (STRNCMP(arg, "indent", 6) == 0)
11792 {
11793 indent = TRUE;
11794 arg = skipwhite(arg + 6);
11795 continue;
11796 }
11797 break;
11798 }
11799 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11800 {
11801 if (*arg == 'o' || !filetype_detect)
11802 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011803 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011804 filetype_detect = TRUE;
11805 if (plugin)
11806 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011807 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011808 filetype_plugin = TRUE;
11809 }
11810 if (indent)
11811 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011812 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011813 filetype_indent = TRUE;
11814 }
11815 }
11816 if (*arg == 'd')
11817 {
11818 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011819 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011820 }
11821 }
11822 else if (STRCMP(arg, "off") == 0)
11823 {
11824 if (plugin || indent)
11825 {
11826 if (plugin)
11827 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011828 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011829 filetype_plugin = FALSE;
11830 }
11831 if (indent)
11832 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011833 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011834 filetype_indent = FALSE;
11835 }
11836 }
11837 else
11838 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011839 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011840 filetype_detect = FALSE;
11841 }
11842 }
11843 else
11844 EMSG2(_(e_invarg2), arg);
11845}
11846
11847/*
11848 * ":setfiletype {name}"
11849 */
11850 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011851ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852{
11853 if (!did_filetype)
11854 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11855}
11856#endif
11857
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011859ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860{
11861#ifdef FEAT_DIGRAPHS
11862 if (*eap->arg != NUL)
11863 putdigraph(eap->arg);
11864 else
11865 listdigraphs();
11866#else
11867 EMSG(_("E196: No digraphs in this version"));
11868#endif
11869}
11870
11871 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011872ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873{
11874 int flags = 0;
11875
11876 if (eap->cmdidx == CMD_setlocal)
11877 flags = OPT_LOCAL;
11878 else if (eap->cmdidx == CMD_setglobal)
11879 flags = OPT_GLOBAL;
11880#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11881 if (cmdmod.browse && flags == 0)
11882 ex_options(eap);
11883 else
11884#endif
11885 (void)do_set(eap->arg, flags);
11886}
11887
11888#ifdef FEAT_SEARCH_EXTRA
11889/*
11890 * ":nohlsearch"
11891 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011892 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011893ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011894{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010011895 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011896 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011897}
11898
11899/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011900 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011901 * Sets nextcmd to the start of the next command, if any. Also called when
11902 * skipping commands to find the next command.
11903 */
11904 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011905ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011906{
11907 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011908 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011909 char_u *end;
11910 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011911 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011912
11913 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011914 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011915 else
11916 {
11917 EMSG(e_invcmd);
11918 return;
11919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011920
11921 /* First clear any old pattern. */
11922 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011923 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924
11925 if (ends_excmd(*eap->arg))
11926 end = eap->arg;
11927 else if ((STRNICMP(eap->arg, "none", 4) == 0
11928 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11929 end = eap->arg + 4;
11930 else
11931 {
11932 p = skiptowhite(eap->arg);
11933 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011934 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011935 p = skipwhite(p);
11936 if (*p == NUL)
11937 {
11938 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011939 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011940 EMSG2(_(e_invarg2), eap->arg);
11941 return;
11942 }
11943 end = skip_regexp(p + 1, *p, TRUE, NULL);
11944 if (!eap->skip)
11945 {
11946 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11947 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011948 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011949 eap->errmsg = e_trailing;
11950 return;
11951 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011952 if (*end != *p)
11953 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011954 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011955 EMSG2(_(e_invarg2), p);
11956 return;
11957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011958
11959 c = *end;
11960 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020011961 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011962 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011963 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011964 }
11965 }
11966 eap->nextcmd = find_nextcmd(end);
11967}
11968#endif
11969
11970#ifdef FEAT_CRYPT
11971/*
11972 * ":X": Get crypt key
11973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011974 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011975ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011976{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010011977 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020011978 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979}
11980#endif
11981
11982#ifdef FEAT_FOLDING
11983 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011984ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985{
11986 if (foldManualAllowed(TRUE))
11987 foldCreate(eap->line1, eap->line2);
11988}
11989
11990 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011991ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992{
11993 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11994 eap->forceit, FALSE);
11995}
11996
11997 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011998ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999{
12000 linenr_T lnum;
12001
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012002#ifdef FEAT_CLIPBOARD
12003 start_global_changes();
12004#endif
12005
Bram Moolenaar071d4272004-06-13 20:20:40 +000012006 /* First set the marks for all lines closed/open. */
12007 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12008 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12009 ml_setmarked(lnum);
12010
12011 /* Execute the command on the marked lines. */
12012 global_exe(eap->arg);
12013 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012014#ifdef FEAT_CLIPBOARD
12015 end_global_changes();
12016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012017}
12018#endif