blob: 1321b7fd66770dd87206e47febc432fe27059d46 [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);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100338static void ex_redrawstatus(exarg_T *eap);
339static void close_redir(void);
340static void ex_mkrc(exarg_T *eap);
341static void ex_mark(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342#ifdef FEAT_USR_CMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100343static char_u *uc_fun_cmd(void);
344static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100346static void ex_startinsert(exarg_T *eap);
347static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100349static void ex_checkpath(exarg_T *eap);
350static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351#else
352# define ex_findpat ex_ni
353# define ex_checkpath ex_ni
354#endif
355#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100356static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357#else
358# define ex_psearch ex_ni
359#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100360static void ex_tag(exarg_T *eap);
361static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362#ifndef FEAT_EVAL
363# define ex_scriptnames ex_ni
364# define ex_finish ex_ni
365# define ex_echo ex_ni
366# define ex_echohl ex_ni
367# define ex_execute ex_ni
368# define ex_call ex_ni
369# define ex_if ex_ni
370# define ex_endif ex_ni
371# define ex_else ex_ni
372# define ex_while ex_ni
373# define ex_continue ex_ni
374# define ex_break ex_ni
375# define ex_endwhile ex_ni
376# define ex_throw ex_ni
377# define ex_try ex_ni
378# define ex_catch ex_ni
379# define ex_finally ex_ni
380# define ex_endtry ex_ni
381# define ex_endfunction ex_ni
382# define ex_let ex_ni
383# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000384# define ex_lockvar ex_ni
385# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386# define ex_function ex_ni
387# define ex_delfunction ex_ni
388# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000389# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100391static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100393static int makeopens(FILE *fd, char_u *dirnow);
394static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
395static void ex_loadview(exarg_T *eap);
396static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000397static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398#else
399# define ex_loadview ex_ni
400#endif
401#ifndef FEAT_EVAL
402# define ex_compiler ex_ni
403#endif
404#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100405static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406#else
407# define ex_viminfo ex_ni
408#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100409static void ex_behave(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100411static void ex_filetype(exarg_T *eap);
412static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413#else
414# define ex_filetype ex_ni
415# define ex_setfiletype ex_ni
416#endif
417#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000418# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419# define ex_diffpatch ex_ni
420# define ex_diffgetput ex_ni
421# define ex_diffsplit ex_ni
422# define ex_diffthis ex_ni
423# define ex_diffupdate ex_ni
424#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100425static void ex_digraphs(exarg_T *eap);
426static void ex_set(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
428# define ex_options ex_ni
429#endif
430#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100431static void ex_nohlsearch(exarg_T *eap);
432static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433#else
434# define ex_nohlsearch ex_ni
435# define ex_match ex_ni
436#endif
437#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100438static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439#else
440# define ex_X ex_ni
441#endif
442#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100443static void ex_fold(exarg_T *eap);
444static void ex_foldopen(exarg_T *eap);
445static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446#else
447# define ex_fold ex_ni
448# define ex_foldopen ex_ni
449# define ex_folddo ex_ni
450#endif
451#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
452 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
453# define ex_language ex_ni
454#endif
455#ifndef FEAT_SIGNS
456# define ex_sign ex_ni
457#endif
458#ifndef FEAT_SUN_WORKSHOP
459# define ex_wsverb ex_ni
460#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000461#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200462# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000463# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200464# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466
467#ifndef FEAT_EVAL
468# define ex_debug ex_ni
469# define ex_breakadd ex_ni
470# define ex_debuggreedy ex_ni
471# define ex_breakdel ex_ni
472# define ex_breaklist ex_ni
473#endif
474
475#ifndef FEAT_CMDHIST
476# define ex_history ex_ni
477#endif
478#ifndef FEAT_JUMPLIST
479# define ex_jumps ex_ni
480# define ex_changes ex_ni
481#endif
482
Bram Moolenaar05159a02005-02-26 23:04:13 +0000483#ifndef FEAT_PROFILE
484# define ex_profile ex_ni
485#endif
486
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487/*
488 * Declare cmdnames[].
489 */
490#define DO_DECLARE_EXCMD
491#include "ex_cmds.h"
492
493/*
494 * Table used to quickly search for a command, based on its first character.
495 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000496static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497{
498 CMD_append,
499 CMD_buffer,
500 CMD_change,
501 CMD_delete,
502 CMD_edit,
503 CMD_file,
504 CMD_global,
505 CMD_help,
506 CMD_insert,
507 CMD_join,
508 CMD_k,
509 CMD_list,
510 CMD_move,
511 CMD_next,
512 CMD_open,
513 CMD_print,
514 CMD_quit,
515 CMD_read,
516 CMD_substitute,
517 CMD_t,
518 CMD_undo,
519 CMD_vglobal,
520 CMD_write,
521 CMD_xit,
522 CMD_yank,
523 CMD_z,
524 CMD_bang
525};
526
527static char_u dollar_command[2] = {'$', 0};
528
529
530#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000531/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532typedef struct
533{
534 char_u *line; /* command line */
535 linenr_T lnum; /* sourcing_lnum of the line */
536} wcmd_T;
537
538/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000539 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000541 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000543struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544{
545 garray_T *lines_gap; /* growarray with line info */
546 int current_line; /* last read line from growarray */
547 int repeating; /* TRUE when looping a second time */
548 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100549 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 void *cookie;
551};
552
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100553static char_u *get_loop_line(int c, void *cookie, int indent);
554static int store_loop_line(garray_T *gap, char_u *line);
555static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000556
557/* Struct to save a few things while debugging. Used in do_cmdline() only. */
558struct dbg_stuff
559{
560 int trylevel;
561 int force_abort;
562 except_T *caught_stack;
563 char_u *vv_exception;
564 char_u *vv_throwpoint;
565 int did_emsg;
566 int got_int;
567 int did_throw;
568 int need_rethrow;
569 int check_cstack;
570 except_T *current_exception;
571};
572
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100573static void save_dbg_stuff(struct dbg_stuff *dsp);
574static void restore_dbg_stuff(struct dbg_stuff *dsp);
Bram Moolenaared203462004-06-16 11:19:22 +0000575
576 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100577save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000578{
579 dsp->trylevel = trylevel; trylevel = 0;
580 dsp->force_abort = force_abort; force_abort = FALSE;
581 dsp->caught_stack = caught_stack; caught_stack = NULL;
582 dsp->vv_exception = v_exception(NULL);
583 dsp->vv_throwpoint = v_throwpoint(NULL);
584
585 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
586 dsp->did_emsg = did_emsg; did_emsg = FALSE;
587 dsp->got_int = got_int; got_int = FALSE;
588 dsp->did_throw = did_throw; did_throw = FALSE;
589 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
590 dsp->check_cstack = check_cstack; check_cstack = FALSE;
591 dsp->current_exception = current_exception; current_exception = NULL;
592}
593
594 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100595restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000596{
597 suppress_errthrow = FALSE;
598 trylevel = dsp->trylevel;
599 force_abort = dsp->force_abort;
600 caught_stack = dsp->caught_stack;
601 (void)v_exception(dsp->vv_exception);
602 (void)v_throwpoint(dsp->vv_throwpoint);
603 did_emsg = dsp->did_emsg;
604 got_int = dsp->got_int;
605 did_throw = dsp->did_throw;
606 need_rethrow = dsp->need_rethrow;
607 check_cstack = dsp->check_cstack;
608 current_exception = dsp->current_exception;
609}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610#endif
611
612
613/*
614 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
615 * command is given.
616 */
617 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100618do_exmode(
619 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620{
621 int save_msg_scroll;
622 int prev_msg_row;
623 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000624 int changedtick;
625
626 if (improved)
627 exmode_active = EXMODE_VIM;
628 else
629 exmode_active = EXMODE_NORMAL;
630 State = NORMAL;
631
632 /* When using ":global /pat/ visual" and then "Q" we return to continue
633 * the :global command. */
634 if (global_busy)
635 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636
637 save_msg_scroll = msg_scroll;
638 ++RedrawingDisabled; /* don't redisplay the window */
639 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640#ifdef FEAT_GUI
641 /* Ignore scrollbar and mouse events in Ex mode */
642 ++hold_gui_events;
643#endif
644#ifdef FEAT_SNIFF
645 want_sniff_request = 0; /* No K_SNIFF wanted */
646#endif
647
648 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
649 while (exmode_active)
650 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000651 /* Check for a ":normal" command and no more characters left. */
652 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
653 {
654 exmode_active = FALSE;
655 break;
656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 msg_scroll = TRUE;
658 need_wait_return = FALSE;
659 ex_pressedreturn = FALSE;
660 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000661 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 prev_msg_row = msg_row;
663 prev_line = curwin->w_cursor.lnum;
664#ifdef FEAT_SNIFF
665 ProcessSniffRequests();
666#endif
667 if (improved)
668 {
669 cmdline_row = msg_row;
670 do_cmdline(NULL, getexline, NULL, 0);
671 }
672 else
673 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
674 lines_left = Rows - 1;
675
Bram Moolenaardf177f62005-02-22 08:39:57 +0000676 if ((prev_line != curwin->w_cursor.lnum
677 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000679 if (curbuf->b_ml.ml_flags & ML_EMPTY)
680 EMSG(_(e_emptybuf));
681 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000683 if (ex_pressedreturn)
684 {
685 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000686 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000687 msg_row = prev_msg_row;
688 if (prev_msg_row == Rows - 1)
689 msg_row--;
690 }
691 msg_col = 0;
692 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
693 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000696 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
697 {
698 if (curbuf->b_ml.ml_flags & ML_EMPTY)
699 EMSG(_(e_emptybuf));
700 else
701 EMSG(_("E501: At end-of-file"));
702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 }
704
705#ifdef FEAT_GUI
706 --hold_gui_events;
707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 --RedrawingDisabled;
709 --no_wait_return;
710 update_screen(CLEAR);
711 need_wait_return = FALSE;
712 msg_scroll = save_msg_scroll;
713}
714
715/*
716 * Execute a simple command line. Used for translated commands like "*".
717 */
718 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100719do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720{
721 return do_cmdline(cmd, NULL, NULL,
722 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
723}
724
725/*
726 * do_cmdline(): execute one Ex command line
727 *
728 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100729 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 * 2. Split up in parts separated with '|'.
731 *
732 * This function can be called recursively!
733 *
734 * flags:
735 * DOCMD_VERBOSE - The command will be included in the error message.
736 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100737 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 * DOCMD_KEYTYPED - Don't reset KeyTyped.
739 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
740 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
741 *
742 * return FAIL if cmdline could not be executed, OK otherwise
743 */
744 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100745do_cmdline(
746 char_u *cmdline,
747 char_u *(*fgetline)(int, void *, int),
748 void *cookie, /* argument for fgetline() */
749 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750{
751 char_u *next_cmdline; /* next cmd to execute */
752 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100753 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 static int recursive = 0; /* recursive depth */
755 int msg_didout_before_start = 0;
756 int count = 0; /* line number count */
757 int did_inc = FALSE; /* incremented RedrawingDisabled */
758 int retval = OK;
759#ifdef FEAT_EVAL
760 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000761 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 int current_line = 0; /* active line in lines_ga */
763 char_u *fname = NULL; /* function or script name */
764 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
765 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000766 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 int initial_trylevel;
768 struct msglist **saved_msg_list = NULL;
769 struct msglist *private_msg_list;
770
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100771 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100772 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000774 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000776 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100778# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779# define cmd_cookie cookie
780#endif
781 static int call_depth = 0; /* recursiveness */
782
783#ifdef FEAT_EVAL
784 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
785 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000786 * This ensures that the do_errthrow() call in do_one_cmd() does not
787 * combine the messages stored by an earlier invocation of do_one_cmd()
788 * with the command name of the later one. This would happen when
789 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 saved_msg_list = msg_list;
791 msg_list = &private_msg_list;
792 private_msg_list = NULL;
793#endif
794
795 /* It's possible to create an endless loop with ":execute", catch that
796 * here. The value of 200 allows nested function calls, ":source", etc. */
797 if (call_depth == 200)
798 {
799 EMSG(_("E169: Command too recursive"));
800#ifdef FEAT_EVAL
801 /* When converting to an exception, we do not include the command name
802 * since this is not an error of the specific command. */
803 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
804 msg_list = saved_msg_list;
805#endif
806 return FAIL;
807 }
808 ++call_depth;
809
810#ifdef FEAT_EVAL
811 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000812 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 cstack.cs_trylevel = 0;
814 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000815 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
817
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100818 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819
820 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100821 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000822 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 ++ex_nesting_level;
824
825 /* Get the function or script name and the address where the next breakpoint
826 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000827 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 {
829 fname = func_name(real_cookie);
830 breakpoint = func_breakpoint(real_cookie);
831 dbg_tick = func_dbg_tick(real_cookie);
832 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100833 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 {
835 fname = sourcing_name;
836 breakpoint = source_breakpoint(real_cookie);
837 dbg_tick = source_dbg_tick(real_cookie);
838 }
839
840 /*
841 * Initialize "force_abort" and "suppress_errthrow" at the top level.
842 */
843 if (!recursive)
844 {
845 force_abort = FALSE;
846 suppress_errthrow = FALSE;
847 }
848
849 /*
850 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000851 * exception handling (used when debugging). Otherwise clear it to avoid
852 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000854 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000855 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000856 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200857 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858
859 initial_trylevel = trylevel;
860
861 /*
862 * "did_throw" will be set to TRUE when an exception is being thrown.
863 */
864 did_throw = FALSE;
865#endif
866 /*
867 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000868 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 * If force_abort is set, we cancel everything.
870 */
871 did_emsg = FALSE;
872
873 /*
874 * KeyTyped is only set when calling vgetc(). Reset it here when not
875 * calling vgetc() (sourced command lines).
876 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100877 if (!(flags & DOCMD_KEYTYPED)
878 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 KeyTyped = FALSE;
880
881 /*
882 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000883 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 * - for multiple commands on one line, separated with '|'
885 * - when repeating until there are no more lines (for ":source")
886 */
887 next_cmdline = cmdline;
888 do
889 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000890#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100891 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000892#endif
893
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000894 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 if (next_cmdline == NULL
896#ifdef FEAT_EVAL
897 && !force_abort
898 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000899 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900#endif
901 )
902 did_emsg = FALSE;
903
904 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000905 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100906 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 * 3. If a line is given: Make a copy, so we can mess with it.
908 */
909
910#ifdef FEAT_EVAL
911 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000912 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 {
914 /* Each '|' separated command is stored separately in lines_ga, to
915 * be able to jump to it. Don't use next_cmdline now. */
916 vim_free(cmdline_copy);
917 cmdline_copy = NULL;
918
919 /* Check if a function has returned or, unless it has an unclosed
920 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000921 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000923# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000924 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000925 func_line_end(real_cookie);
926# endif
927 if (func_has_ended(real_cookie))
928 {
929 retval = FAIL;
930 break;
931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000933#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000934 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100935 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000936 script_line_end();
937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938
939 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100940 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 {
942 retval = FAIL;
943 break;
944 }
945
946 /* If breakpoints have been added/deleted need to check for it. */
947 if (breakpoint != NULL && dbg_tick != NULL
948 && *dbg_tick != debug_tick)
949 {
950 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100951 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 fname, sourcing_lnum);
953 *dbg_tick = debug_tick;
954 }
955
956 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
957 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
958
959 /* Did we encounter a breakpoint? */
960 if (breakpoint != NULL && *breakpoint != 0
961 && *breakpoint <= sourcing_lnum)
962 {
963 dbg_breakpoint(fname, sourcing_lnum);
964 /* Find next breakpoint. */
965 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100966 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 fname, sourcing_lnum);
968 *dbg_tick = debug_tick;
969 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000970# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000971 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000972 {
973 if (getline_is_func)
974 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100975 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000976 script_line_start();
977 }
978# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 }
980
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000981 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000983 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100984 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 * below, so that it stores lines in or reads them from
986 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000987 * while/for loop. */
988 cmd_getline = get_loop_line;
989 cmd_cookie = (void *)&cmd_loop_cookie;
990 cmd_loop_cookie.lines_gap = &lines_ga;
991 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100992 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000993 cmd_loop_cookie.cookie = cookie;
994 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 }
996 else
997 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100998 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 cmd_cookie = cookie;
1000 }
1001#endif
1002
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001003 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 if (next_cmdline == NULL)
1005 {
1006 /*
1007 * Need to set msg_didout for the first line after an ":if",
1008 * otherwise the ":if" will be overwritten.
1009 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001010 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001012 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013#ifdef FEAT_EVAL
1014 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1015#else
1016 0
1017#endif
1018 )) == NULL)
1019 {
1020 /* Don't call wait_return for aborted command line. The NULL
1021 * returned for the end of a sourced file or executed function
1022 * doesn't do this. */
1023 if (KeyTyped && !(flags & DOCMD_REPEAT))
1024 need_wait_return = FALSE;
1025 retval = FAIL;
1026 break;
1027 }
1028 used_getline = TRUE;
1029
1030 /*
1031 * Keep the first typed line. Clear it when more lines are typed.
1032 */
1033 if (flags & DOCMD_KEEPLINE)
1034 {
1035 vim_free(repeat_cmdline);
1036 if (count == 0)
1037 repeat_cmdline = vim_strsave(next_cmdline);
1038 else
1039 repeat_cmdline = NULL;
1040 }
1041 }
1042
1043 /* 3. Make a copy of the command so we can mess with it. */
1044 else if (cmdline_copy == NULL)
1045 {
1046 next_cmdline = vim_strsave(next_cmdline);
1047 if (next_cmdline == NULL)
1048 {
1049 EMSG(_(e_outofmem));
1050 retval = FAIL;
1051 break;
1052 }
1053 }
1054 cmdline_copy = next_cmdline;
1055
1056#ifdef FEAT_EVAL
1057 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001058 * Save the current line when inside a ":while" or ":for", and when
1059 * the command looks like a ":while" or ":for", because we may need it
1060 * later. When there is a '|' and another command, it is stored
1061 * separately, because we need to be able to jump back to it from an
1062 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001064 if (current_line == lines_ga.ga_len
1065 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001067 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 {
1069 retval = FAIL;
1070 break;
1071 }
1072 }
1073 did_endif = FALSE;
1074#endif
1075
1076 if (count++ == 0)
1077 {
1078 /*
1079 * All output from the commands is put below each other, without
1080 * waiting for a return. Don't do this when executing commands
1081 * from a script or when being called recursive (e.g. for ":e
1082 * +command file").
1083 */
1084 if (!(flags & DOCMD_NOWAIT) && !recursive)
1085 {
1086 msg_didout_before_start = msg_didout;
1087 msg_didany = FALSE; /* no output yet */
1088 msg_start();
1089 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001090 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 ++RedrawingDisabled;
1092 did_inc = TRUE;
1093 }
1094 }
1095
1096 if (p_verbose >= 15 && sourcing_name != NULL)
1097 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001099 verbose_enter_scroll();
1100
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 smsg((char_u *)_("line %ld: %s"),
1102 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001103 if (msg_silent == 0)
1104 msg_puts((char_u *)"\n"); /* don't overwrite this */
1105
1106 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 --no_wait_return;
1108 }
1109
1110 /*
1111 * 2. Execute one '|' separated command.
1112 * do_one_cmd() will return NULL if there is no trailing '|'.
1113 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1114 */
1115 ++recursive;
1116 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1117#ifdef FEAT_EVAL
1118 &cstack,
1119#endif
1120 cmd_getline, cmd_cookie);
1121 --recursive;
1122
1123#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001124 if (cmd_cookie == (void *)&cmd_loop_cookie)
1125 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001127 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128#endif
1129
1130 if (next_cmdline == NULL)
1131 {
1132 vim_free(cmdline_copy);
1133 cmdline_copy = NULL;
1134#ifdef FEAT_CMDHIST
1135 /*
1136 * If the command was typed, remember it for the ':' register.
1137 * Do this AFTER executing the command to make :@: work.
1138 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001139 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 && new_last_cmdline != NULL)
1141 {
1142 vim_free(last_cmdline);
1143 last_cmdline = new_last_cmdline;
1144 new_last_cmdline = NULL;
1145 }
1146#endif
1147 }
1148 else
1149 {
1150 /* need to copy the command after the '|' to cmdline_copy, for the
1151 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001152 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 next_cmdline = cmdline_copy;
1154 }
1155
1156
1157#ifdef FEAT_EVAL
1158 /* reset did_emsg for a function that is not aborted by an error */
1159 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001160 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 && !func_has_abort(real_cookie))
1162 did_emsg = FALSE;
1163
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001164 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 {
1166 ++current_line;
1167
1168 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001169 * An ":endwhile", ":endfor" and ":continue" is handled here.
1170 * If we were executing commands, jump back to the ":while" or
1171 * ":for".
1172 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001174 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001176 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001178 /* Jump back to the matching ":while" or ":for". Be careful
1179 * not to use a cs_line[] from an entry that isn't a ":while"
1180 * or ":for": It would make "current_line" invalid and can
1181 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 if (!did_emsg && !got_int && !did_throw
1183 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001184 && (cstack.cs_flags[cstack.cs_idx]
1185 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 && cstack.cs_line[cstack.cs_idx] >= 0
1187 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1188 {
1189 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001190 /* remember we jumped there */
1191 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 line_breakcheck(); /* check if CTRL-C typed */
1193
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001194 /* Check for the next breakpoint at or after the ":while"
1195 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 if (breakpoint != NULL)
1197 {
1198 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001199 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 fname,
1201 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1202 *dbg_tick = debug_tick;
1203 }
1204 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001205 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001207 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001209 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1210 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 }
1212 }
1213
1214 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001215 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001217 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001219 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1221 }
1222 }
1223
1224 /*
1225 * When not inside any ":while" loop, clear remembered lines.
1226 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001227 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 {
1229 if (lines_ga.ga_len > 0)
1230 {
1231 sourcing_lnum =
1232 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1233 free_cmdlines(&lines_ga);
1234 }
1235 current_line = 0;
1236 }
1237
1238 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001239 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1240 * being restored at the ":endtry". Reset them here and set the
1241 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1242 * This includes the case where a missing ":endif", ":endwhile" or
1243 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001245 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001247 cstack.cs_lflags &= ~CSL_HAD_FINA;
1248 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1249 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 did_throw ? (void *)current_exception : NULL);
1251 did_emsg = got_int = did_throw = FALSE;
1252 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1253 }
1254
1255 /* Update global "trylevel" for recursive calls to do_cmdline() from
1256 * within this loop. */
1257 trylevel = initial_trylevel + cstack.cs_trylevel;
1258
1259 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001260 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 * files) is aborted because of an error, an interrupt, or an uncaught
1262 * exception, cancel everything. If it is left normally, reset
1263 * force_abort to get the non-EH compatible abortion behavior for
1264 * the rest of the script.
1265 */
1266 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1267 force_abort = FALSE;
1268
1269 /* Convert an interrupt to an exception if appropriate. */
1270 (void)do_intthrow(&cstack);
1271#endif /* FEAT_EVAL */
1272
1273 }
1274 /*
1275 * Continue executing command lines when:
1276 * - no CTRL-C typed, no aborting error, no exception thrown or try
1277 * conditionals need to be checked for executing finally clauses or
1278 * catching an interrupt exception
1279 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001280 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 * looping for ":source" command or function call.
1282 */
1283 while (!((got_int
1284#ifdef FEAT_EVAL
1285 || (did_emsg && force_abort) || did_throw
1286#endif
1287 )
1288#ifdef FEAT_EVAL
1289 && cstack.cs_trylevel == 0
1290#endif
1291 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001292 && !(did_emsg
1293#ifdef FEAT_EVAL
1294 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001295 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001296 * the :endtry to be missed. */
1297 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1298#endif
1299 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001300 && (getline_equal(fgetline, cookie, getexmodeline)
1301 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 && (next_cmdline != NULL
1303#ifdef FEAT_EVAL
1304 || cstack.cs_idx >= 0
1305#endif
1306 || (flags & DOCMD_REPEAT)));
1307
1308 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001309 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310#ifdef FEAT_EVAL
1311 free_cmdlines(&lines_ga);
1312 ga_clear(&lines_ga);
1313
1314 if (cstack.cs_idx >= 0)
1315 {
1316 /*
1317 * If a sourced file or executed function ran to its end, report the
1318 * unclosed conditional.
1319 */
1320 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001321 && ((getline_equal(fgetline, cookie, getsourceline)
1322 && !source_finished(fgetline, cookie))
1323 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 && !func_has_ended(real_cookie))))
1325 {
1326 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1327 EMSG(_(e_endtry));
1328 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1329 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001330 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1331 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 else
1333 EMSG(_(e_endif));
1334 }
1335
1336 /*
1337 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1338 * ":endtry" in a sourced file or executed function. If the try
1339 * conditional is in its finally clause, ignore anything pending.
1340 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001341 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 */
1343 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001344 {
1345 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1346
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001347 if (idx >= 0)
1348 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001349 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1350 &cstack.cs_looplevel);
1351 }
1352 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 trylevel = initial_trylevel;
1354 }
1355
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001356 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1357 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001359 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 ? (char_u *)"endfunction" : (char_u *)NULL);
1361
1362 if (trylevel == 0)
1363 {
1364 /*
1365 * When an exception is being thrown out of the outermost try
1366 * conditional, discard the uncaught exception, disable the conversion
1367 * of interrupts or errors to exceptions, and ensure that no more
1368 * commands are executed.
1369 */
1370 if (did_throw)
1371 {
1372 void *p = NULL;
1373 char_u *saved_sourcing_name;
1374 int saved_sourcing_lnum;
1375 struct msglist *messages = NULL, *next;
1376
1377 /*
1378 * If the uncaught exception is a user exception, report it as an
1379 * error. If it is an error exception, display the saved error
1380 * message now. For an interrupt exception, do nothing; the
1381 * interrupt message is given elsewhere.
1382 */
1383 switch (current_exception->type)
1384 {
1385 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001386 vim_snprintf((char *)IObuff, IOSIZE,
1387 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 current_exception->value);
1389 p = vim_strsave(IObuff);
1390 break;
1391 case ET_ERROR:
1392 messages = current_exception->messages;
1393 current_exception->messages = NULL;
1394 break;
1395 case ET_INTERRUPT:
1396 break;
1397 default:
1398 p = vim_strsave((char_u *)_(e_internal));
1399 }
1400
1401 saved_sourcing_name = sourcing_name;
1402 saved_sourcing_lnum = sourcing_lnum;
1403 sourcing_name = current_exception->throw_name;
1404 sourcing_lnum = current_exception->throw_lnum;
1405 current_exception->throw_name = NULL;
1406
1407 discard_current_exception(); /* uses IObuff if 'verbose' */
1408 suppress_errthrow = TRUE;
1409 force_abort = TRUE;
1410
1411 if (messages != NULL)
1412 {
1413 do
1414 {
1415 next = messages->next;
1416 emsg(messages->msg);
1417 vim_free(messages->msg);
1418 vim_free(messages);
1419 messages = next;
1420 }
1421 while (messages != NULL);
1422 }
1423 else if (p != NULL)
1424 {
1425 emsg(p);
1426 vim_free(p);
1427 }
1428 vim_free(sourcing_name);
1429 sourcing_name = saved_sourcing_name;
1430 sourcing_lnum = saved_sourcing_lnum;
1431 }
1432
1433 /*
1434 * On an interrupt or an aborting error not converted to an exception,
1435 * disable the conversion of errors to exceptions. (Interrupts are not
1436 * converted any more, here.) This enables also the interrupt message
1437 * when force_abort is set and did_emsg unset in case of an interrupt
1438 * from a finally clause after an error.
1439 */
1440 else if (got_int || (did_emsg && force_abort))
1441 suppress_errthrow = TRUE;
1442 }
1443
1444 /*
1445 * The current cstack will be freed when do_cmdline() returns. An uncaught
1446 * exception will have to be rethrown in the previous cstack. If a function
1447 * has just returned or a script file was just finished and the previous
1448 * cstack belongs to the same function or, respectively, script file, it
1449 * will have to be checked for finally clauses to be executed due to the
1450 * ":return" or ":finish". This is done in do_one_cmd().
1451 */
1452 if (did_throw)
1453 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001454 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001456 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 && ex_nesting_level > func_level(real_cookie) + 1))
1458 {
1459 if (!did_throw)
1460 check_cstack = TRUE;
1461 }
1462 else
1463 {
1464 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001465 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 --ex_nesting_level;
1467 /*
1468 * Go to debug mode when returning from a function in which we are
1469 * single-stepping.
1470 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001471 if ((getline_equal(fgetline, cookie, getsourceline)
1472 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001474 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 ? (char_u *)_("End of sourced file")
1476 : (char_u *)_("End of function"));
1477 }
1478
1479 /*
1480 * Restore the exception environment (done after returning from the
1481 * debugger).
1482 */
1483 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001484 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485
1486 msg_list = saved_msg_list;
1487#endif /* FEAT_EVAL */
1488
1489 /*
1490 * If there was too much output to fit on the command line, ask the user to
1491 * hit return before redrawing the screen. With the ":global" command we do
1492 * this only once after the command is finished.
1493 */
1494 if (did_inc)
1495 {
1496 --RedrawingDisabled;
1497 --no_wait_return;
1498 msg_scroll = FALSE;
1499
1500 /*
1501 * When just finished an ":if"-":else" which was typed, no need to
1502 * wait for hit-return. Also for an error situation.
1503 */
1504 if (retval == FAIL
1505#ifdef FEAT_EVAL
1506 || (did_endif && KeyTyped && !did_emsg)
1507#endif
1508 )
1509 {
1510 need_wait_return = FALSE;
1511 msg_didany = FALSE; /* don't wait when restarting edit */
1512 }
1513 else if (need_wait_return)
1514 {
1515 /*
1516 * The msg_start() above clears msg_didout. The wait_return we do
1517 * here should not overwrite the command that may be shown before
1518 * doing that.
1519 */
1520 msg_didout |= msg_didout_before_start;
1521 wait_return(FALSE);
1522 }
1523 }
1524
Bram Moolenaar2a942252012-11-28 23:03:07 +01001525#ifdef FEAT_EVAL
1526 did_endif = FALSE; /* in case do_cmdline used recursively */
1527#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 /*
1529 * Reset if_level, in case a sourced script file contains more ":if" than
1530 * ":endif" (could be ":if x | foo | endif").
1531 */
1532 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001533#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001534
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 --call_depth;
1536 return retval;
1537}
1538
1539#ifdef FEAT_EVAL
1540/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001541 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 */
1543 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001544get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001546 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 wcmd_T *wp;
1548 char_u *line;
1549
1550 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1551 {
1552 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001553 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001555 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 if (cp->getline == NULL)
1557 line = getcmdline(c, 0L, indent);
1558 else
1559 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001560 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 ++cp->current_line;
1562
1563 return line;
1564 }
1565
1566 KeyTyped = FALSE;
1567 ++cp->current_line;
1568 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1569 sourcing_lnum = wp->lnum;
1570 return vim_strsave(wp->line);
1571}
1572
1573/*
1574 * Store a line in "gap" so that a ":while" loop can execute it again.
1575 */
1576 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001577store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578{
1579 if (ga_grow(gap, 1) == FAIL)
1580 return FAIL;
1581 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1582 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1583 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 return OK;
1585}
1586
1587/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001588 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 */
1590 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001591free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592{
1593 while (gap->ga_len > 0)
1594 {
1595 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1596 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 }
1598}
1599#endif
1600
1601/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001602 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1603 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001606getline_equal(
1607 char_u *(*fgetline)(int, void *, int),
1608 void *cookie UNUSED, /* argument for fgetline() */
1609 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610{
1611#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001612 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001613 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614
Bram Moolenaar89d40322006-08-29 15:30:07 +00001615 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001616 * function that's originally used to obtain the lines. This may be
1617 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001618 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001619 cp = (struct loop_cookie *)cookie;
1620 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 {
1622 gp = cp->getline;
1623 cp = cp->cookie;
1624 }
1625 return gp == func;
1626#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001627 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628#endif
1629}
1630
1631#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1632/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001633 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 * getline function. Otherwise return "cookie".
1635 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001637getline_cookie(
1638 char_u *(*fgetline)(int, void *, int) UNUSED,
1639 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640{
1641# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001642 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001643 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644
Bram Moolenaar89d40322006-08-29 15:30:07 +00001645 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001646 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001648 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001649 cp = (struct loop_cookie *)cookie;
1650 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 {
1652 gp = cp->getline;
1653 cp = cp->cookie;
1654 }
1655 return cp;
1656# else
1657 return cookie;
1658# endif
1659}
1660#endif
1661
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001662
1663/*
1664 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1665 * ":bwipeout", etc.
1666 * Returns the buffer number.
1667 */
1668 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001669compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001670{
1671 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001672 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001673 int count = offset;
1674
1675 buf = firstbuf;
1676 while (buf->b_next != NULL && buf->b_fnum < lnum)
1677 buf = buf->b_next;
1678 while (count != 0)
1679 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001680 count += (offset < 0) ? 1 : -1;
1681 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1682 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001683 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001684 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001685 if (addr_type == ADDR_LOADED_BUFFERS)
1686 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001687 while (buf->b_ml.ml_mfp == NULL)
1688 {
1689 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1690 if (nextbuf == NULL)
1691 break;
1692 buf = nextbuf;
1693 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001694 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001695 /* we might have gone too far, last buffer is not loadedd */
1696 if (addr_type == ADDR_LOADED_BUFFERS)
1697 while (buf->b_ml.ml_mfp == NULL)
1698 {
1699 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1700 if (nextbuf == NULL)
1701 break;
1702 buf = nextbuf;
1703 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001704 return buf->b_fnum;
1705}
1706
Bram Moolenaarf240e182014-11-27 18:33:02 +01001707#ifdef FEAT_WINDOWS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001708static int current_win_nr(win_T *win);
1709static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001710
1711 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001712current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001713{
1714 win_T *wp;
1715 int nr = 0;
1716
1717 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1718 {
1719 ++nr;
1720 if (wp == win)
1721 break;
1722 }
1723 return nr;
1724}
1725
1726 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001727current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001728{
1729 tabpage_T *tp;
1730 int nr = 0;
1731
1732 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
1733 {
1734 ++nr;
1735 if (tp == tab)
1736 break;
1737 }
1738 return nr;
1739}
1740
1741# define CURRENT_WIN_NR current_win_nr(curwin)
1742# define LAST_WIN_NR current_win_nr(NULL)
1743# define CURRENT_TAB_NR current_tab_nr(curtab)
1744# define LAST_TAB_NR current_tab_nr(NULL)
1745#else
1746# define CURRENT_WIN_NR 1
1747# define LAST_WIN_NR 1
1748# define CURRENT_TAB_NR 1
1749# define LAST_TAB_NR 1
1750#endif
1751
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001752
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753/*
1754 * Execute one Ex command.
1755 *
1756 * If 'sourcing' is TRUE, the command will be included in the error message.
1757 *
1758 * 1. skip comment lines and leading space
1759 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001760 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001761 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001762 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001763 * 6. parse arguments
1764 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001766 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 *
1768 * This function may be called recursively!
1769 */
1770#if (_MSC_VER == 1200)
1771/*
Bram Moolenaared203462004-06-16 11:19:22 +00001772 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001774 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775#endif
1776 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001777do_one_cmd(
1778 char_u **cmdlinep,
1779 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001781 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001783 char_u *(*fgetline)(int, void *, int),
1784 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785{
1786 char_u *p;
1787 linenr_T lnum;
1788 long n;
1789 char_u *errormsg = NULL; /* error message */
1790 exarg_T ea; /* Ex command arguments */
1791 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001792 int save_msg_scroll = msg_scroll;
1793 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001795#ifdef HAVE_SANDBOX
1796 int did_sandbox = FALSE;
1797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 cmdmod_T save_cmdmod;
1799 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001800 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
1802 vim_memset(&ea, 0, sizeof(ea));
1803 ea.line1 = 1;
1804 ea.line2 = 1;
1805#ifdef FEAT_EVAL
1806 ++ex_nesting_level;
1807#endif
1808
Bram Moolenaar21691f82012-12-05 19:13:18 +01001809 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 if (quitmore
1811#ifdef FEAT_EVAL
1812 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001813 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001814#endif
1815#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001816 /* avoid that an autocommand, e.g. QuitPre, does this */
1817 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818#endif
1819 )
1820 --quitmore;
1821
1822 /*
1823 * Reset browse, confirm, etc.. They are restored when returning, for
1824 * recursive calls.
1825 */
1826 save_cmdmod = cmdmod;
1827 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1828
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001829 /* "#!anything" is handled like a comment. */
1830 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1831 goto doend;
1832
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 /*
1834 * Repeat until no more command modifiers are found.
1835 */
1836 ea.cmd = *cmdlinep;
1837 for (;;)
1838 {
1839/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001840 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 */
1842 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1843 ++ea.cmd;
1844
1845 /* in ex mode, an empty line works like :+ */
1846 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001847 && (getline_equal(fgetline, cookie, getexmodeline)
1848 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001849 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 {
1851 ea.cmd = (char_u *)"+";
1852 ex_pressedreturn = TRUE;
1853 }
1854
1855 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001856 if (*ea.cmd == '"')
1857 goto doend;
1858 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001859 {
1860 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863
1864/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001865 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 */
1867 p = ea.cmd;
1868 if (VIM_ISDIGIT(*ea.cmd))
1869 p = skipwhite(skipdigits(ea.cmd));
1870 switch (*p)
1871 {
1872 /* When adding an entry, also modify cmd_exists(). */
1873 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1874 break;
1875#ifdef FEAT_WINDOWS
1876 cmdmod.split |= WSP_ABOVE;
1877#endif
1878 continue;
1879
1880 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1881 {
1882#ifdef FEAT_WINDOWS
1883 cmdmod.split |= WSP_BELOW;
1884#endif
1885 continue;
1886 }
1887 if (checkforcmd(&ea.cmd, "browse", 3))
1888 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001889#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 cmdmod.browse = TRUE;
1891#endif
1892 continue;
1893 }
1894 if (!checkforcmd(&ea.cmd, "botright", 2))
1895 break;
1896#ifdef FEAT_WINDOWS
1897 cmdmod.split |= WSP_BOT;
1898#endif
1899 continue;
1900
1901 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1902 break;
1903#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1904 cmdmod.confirm = TRUE;
1905#endif
1906 continue;
1907
1908 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1909 {
1910 cmdmod.keepmarks = TRUE;
1911 continue;
1912 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001913 if (checkforcmd(&ea.cmd, "keepalt", 5))
1914 {
1915 cmdmod.keepalt = TRUE;
1916 continue;
1917 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001918 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1919 {
1920 cmdmod.keeppatterns = TRUE;
1921 continue;
1922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1924 break;
1925 cmdmod.keepjumps = TRUE;
1926 continue;
1927
1928 /* ":hide" and ":hide | cmd" are not modifiers */
1929 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1930 || *p == NUL || ends_excmd(*p))
1931 break;
1932 ea.cmd = p;
1933 cmdmod.hide = TRUE;
1934 continue;
1935
1936 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1937 {
1938 cmdmod.lockmarks = TRUE;
1939 continue;
1940 }
1941
1942 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1943 break;
1944#ifdef FEAT_WINDOWS
1945 cmdmod.split |= WSP_ABOVE;
1946#endif
1947 continue;
1948
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001949 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001950 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001951#ifdef FEAT_AUTOCMD
1952 if (cmdmod.save_ei == NULL)
1953 {
1954 /* Set 'eventignore' to "all". Restore the
1955 * existing option value later. */
1956 cmdmod.save_ei = vim_strsave(p_ei);
1957 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001958 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001959 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001960#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001961 continue;
1962 }
1963 if (!checkforcmd(&ea.cmd, "noswapfile", 6))
1964 break;
1965 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001966 continue;
1967
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1969 break;
1970#ifdef FEAT_WINDOWS
1971 cmdmod.split |= WSP_BELOW;
1972#endif
1973 continue;
1974
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001975 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1976 {
1977#ifdef HAVE_SANDBOX
1978 if (!did_sandbox)
1979 ++sandbox;
1980 did_sandbox = TRUE;
1981#endif
1982 continue;
1983 }
1984 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001986 if (save_msg_silent == -1)
1987 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1990 {
1991 /* ":silent!", but not "silent !cmd" */
1992 ea.cmd = skipwhite(ea.cmd + 1);
1993 ++emsg_silent;
1994 ++did_esilent;
1995 }
1996 continue;
1997
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001998 case 't': if (checkforcmd(&p, "tab", 3))
1999 {
2000#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002001 if (vim_isdigit(*ea.cmd))
2002 cmdmod.tab = atoi((char *)ea.cmd) + 1;
2003 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002004 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002005 ea.cmd = p;
2006#endif
2007 continue;
2008 }
2009 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 break;
2011#ifdef FEAT_WINDOWS
2012 cmdmod.split |= WSP_TOP;
2013#endif
2014 continue;
2015
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002016 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
2017 break;
2018 if (save_msg_silent == -1)
2019 save_msg_silent = msg_silent;
2020 msg_silent = 0;
2021 continue;
2022
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
2024 {
2025#ifdef FEAT_VERTSPLIT
2026 cmdmod.split |= WSP_VERT;
2027#endif
2028 continue;
2029 }
2030 if (!checkforcmd(&p, "verbose", 4))
2031 break;
2032 if (verbose_save < 0)
2033 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00002034 if (vim_isdigit(*ea.cmd))
2035 p_verbose = atoi((char *)ea.cmd);
2036 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 p_verbose = 1;
2038 ea.cmd = p;
2039 continue;
2040 }
2041 break;
2042 }
2043
2044#ifdef FEAT_EVAL
2045 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2046 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2047#else
2048 ea.skip = (if_level > 0);
2049#endif
2050
2051#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002052# ifdef FEAT_PROFILE
2053 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002054 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002055 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002056 if (getline_equal(fgetline, cookie, get_func_line))
2057 func_line_exec(getline_cookie(fgetline, cookie));
2058 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002059 script_line_exec();
2060 }
2061#endif
2062
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 /* May go to debug mode. If this happens and the ">quit" debug command is
2064 * used, throw an interrupt exception and skip the next command. */
2065 dbg_check_breakpoint(&ea);
2066 if (!ea.skip && got_int)
2067 {
2068 ea.skip = TRUE;
2069 (void)do_intthrow(cstack);
2070 }
2071#endif
2072
2073/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002074 * 3. Skip over the range to find the command. Let "p" point to after it.
2075 *
2076 * We need the command to know what kind of range it uses.
2077 */
2078 cmd = ea.cmd;
2079 ea.cmd = skip_range(ea.cmd, NULL);
2080 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2081 ea.cmd = skipwhite(ea.cmd + 1);
2082 p = find_command(&ea, NULL);
2083
2084/*
2085 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 *
2087 * where 'addr' is:
2088 *
2089 * % (entire file)
2090 * $ [+-NUM]
2091 * 'x [+-NUM] (where x denotes a currently defined mark)
2092 * . [+-NUM]
2093 * [+-NUM]..
2094 * NUM
2095 *
2096 * The ea.cmd pointer is updated to point to the first character following the
2097 * range spec. If an initial address is found, but no second, the upper bound
2098 * is equal to the lower.
2099 */
2100
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002101 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002102 if (!IS_USER_CMDIDX(ea.cmdidx))
2103 {
2104 if (ea.cmdidx != CMD_SIZE)
2105 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2106 else
2107 ea.addr_type = ADDR_LINES;
2108
2109#ifdef FEAT_WINDOWS
2110 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002111 if (ea.cmdidx == CMD_wincmd && p != NULL)
2112 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002113#endif
2114 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002115
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002117 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 for (;;)
2119 {
2120 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002121 switch (ea.addr_type)
2122 {
2123 case ADDR_LINES:
2124 /* default is current line number */
2125 ea.line2 = curwin->w_cursor.lnum;
2126 break;
2127 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002128 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002129 ea.line2 = lnum;
2130 break;
2131 case ADDR_ARGUMENTS:
2132 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002133 if (ea.line2 > ARGCOUNT)
2134 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002135 break;
2136 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002137 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002138 ea.line2 = curbuf->b_fnum;
2139 break;
2140 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01002141 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002142 ea.line2 = lnum;
2143 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002144#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002145 case ADDR_QUICKFIX:
2146 ea.line2 = qf_get_cur_valid_idx(&ea);
2147 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002148#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002151 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip, ea.addr_count == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 if (ea.cmd == NULL) /* error detected */
2153 goto doend;
2154 if (lnum == MAXLNUM)
2155 {
2156 if (*ea.cmd == '%') /* '%' - all lines */
2157 {
2158 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002159 switch (ea.addr_type)
2160 {
2161 case ADDR_LINES:
2162 ea.line1 = 1;
2163 ea.line2 = curbuf->b_ml.ml_line_count;
2164 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002165 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002166 {
2167 buf_T *buf = firstbuf;
2168
2169 while (buf->b_next != NULL
2170 && buf->b_ml.ml_mfp == NULL)
2171 buf = buf->b_next;
2172 ea.line1 = buf->b_fnum;
2173 buf = lastbuf;
2174 while (buf->b_prev != NULL
2175 && buf->b_ml.ml_mfp == NULL)
2176 buf = buf->b_prev;
2177 ea.line2 = buf->b_fnum;
2178 break;
2179 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002180 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002181 ea.line1 = firstbuf->b_fnum;
2182 ea.line2 = lastbuf->b_fnum;
2183 break;
2184 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002185 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002186 if (IS_USER_CMDIDX(ea.cmdidx))
2187 {
2188 ea.line1 = 1;
2189 ea.line2 = ea.addr_type == ADDR_WINDOWS
2190 ? LAST_WIN_NR : LAST_TAB_NR;
2191 }
2192 else
2193 {
2194 /* there is no Vim command which uses '%' and
2195 * ADDR_WINDOWS or ADDR_TABS */
2196 errormsg = (char_u *)_(e_invrange);
2197 goto doend;
2198 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002199 break;
2200 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002201 if (ARGCOUNT == 0)
2202 ea.line1 = ea.line2 = 0;
2203 else
2204 {
2205 ea.line1 = 1;
2206 ea.line2 = ARGCOUNT;
2207 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002208 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002209#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002210 case ADDR_QUICKFIX:
2211 ea.line1 = 1;
2212 ea.line2 = qf_get_size(&ea);
2213 if (ea.line2 == 0)
2214 ea.line2 = 1;
2215 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002216#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 ++ea.addr_count;
2219 }
2220 /* '*' - visual area */
2221 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2222 {
2223 pos_T *fp;
2224
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002225 if (ea.addr_type != ADDR_LINES)
2226 {
2227 errormsg = (char_u *)_(e_invrange);
2228 goto doend;
2229 }
2230
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 ++ea.cmd;
2232 if (!ea.skip)
2233 {
2234 fp = getmark('<', FALSE);
2235 if (check_mark(fp) == FAIL)
2236 goto doend;
2237 ea.line1 = fp->lnum;
2238 fp = getmark('>', FALSE);
2239 if (check_mark(fp) == FAIL)
2240 goto doend;
2241 ea.line2 = fp->lnum;
2242 ++ea.addr_count;
2243 }
2244 }
2245 }
2246 else
2247 ea.line2 = lnum;
2248 ea.addr_count++;
2249
2250 if (*ea.cmd == ';')
2251 {
2252 if (!ea.skip)
2253 curwin->w_cursor.lnum = ea.line2;
2254 }
2255 else if (*ea.cmd != ',')
2256 break;
2257 ++ea.cmd;
2258 }
2259
2260 /* One address given: set start and end lines */
2261 if (ea.addr_count == 1)
2262 {
2263 ea.line1 = ea.line2;
2264 /* ... but only implicit: really no address given */
2265 if (lnum == MAXLNUM)
2266 ea.addr_count = 0;
2267 }
2268
2269 /* Don't leave the cursor on an illegal line (caused by ';') */
2270 check_cursor_lnum();
2271
2272/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002273 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 */
2275
2276 /*
2277 * Skip ':' and any white space
2278 */
2279 ea.cmd = skipwhite(ea.cmd);
2280 while (*ea.cmd == ':')
2281 ea.cmd = skipwhite(ea.cmd + 1);
2282
2283 /*
2284 * If we got a line, but no command, then go to the line.
2285 * If we find a '|' or '\n' we set ea.nextcmd.
2286 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002287 if (*ea.cmd == NUL || *ea.cmd == '"'
2288 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 {
2290 /*
2291 * strange vi behaviour:
2292 * ":3" jumps to line 3
2293 * ":3|..." prints line 3
2294 * ":|" prints current line
2295 */
2296 if (ea.skip) /* skip this if inside :if */
2297 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002298 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 {
2300 ea.cmdidx = CMD_print;
2301 ea.argt = RANGE+COUNT+TRLBAR;
2302 if ((errormsg = invalid_range(&ea)) == NULL)
2303 {
2304 correct_range(&ea);
2305 ex_print(&ea);
2306 }
2307 }
2308 else if (ea.addr_count != 0)
2309 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002310 if (ea.line2 > curbuf->b_ml.ml_line_count)
2311 {
2312 /* With '-' in 'cpoptions' a line number past the file is an
2313 * error, otherwise put it at the end of the file. */
2314 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2315 ea.line2 = -1;
2316 else
2317 ea.line2 = curbuf->b_ml.ml_line_count;
2318 }
2319
2320 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002321 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 else
2323 {
2324 if (ea.line2 == 0)
2325 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 else
2327 curwin->w_cursor.lnum = ea.line2;
2328 beginline(BL_SOL | BL_FIX);
2329 }
2330 }
2331 goto doend;
2332 }
2333
Bram Moolenaard5005162014-08-22 23:05:54 +02002334#ifdef FEAT_AUTOCMD
2335 /* If this looks like an undefined user command and there are CmdUndefined
2336 * autocommands defined, trigger the matching autocommands. */
2337 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2338 && ASCII_ISUPPER(*ea.cmd)
2339 && has_cmdundefined())
2340 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002341 int ret;
2342
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002343 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002344 while (ASCII_ISALNUM(*p))
2345 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002346 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002347 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2348 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002349 /* If the autocommands did something and didn't cause an error, try
2350 * finding the command again. */
2351 p = (ret && !aborting()) ? find_command(&ea, NULL) : NULL;
Bram Moolenaard5005162014-08-22 23:05:54 +02002352 }
2353#endif
2354
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355#ifdef FEAT_USR_CMDS
2356 if (p == NULL)
2357 {
2358 if (!ea.skip)
2359 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2360 goto doend;
2361 }
2362 /* Check for wrong commands. */
2363 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2364 {
2365 errormsg = uc_fun_cmd();
2366 goto doend;
2367 }
2368#endif
2369 if (ea.cmdidx == CMD_SIZE)
2370 {
2371 if (!ea.skip)
2372 {
2373 STRCPY(IObuff, _("E492: Not an editor command"));
2374 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002375 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002377 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 }
2379 goto doend;
2380 }
2381
Bram Moolenaar958636c2014-10-21 20:01:58 +02002382 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2383 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002384#ifdef HAVE_EX_SCRIPT_NI
2385 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2386#endif
2387 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388
2389#ifndef FEAT_EVAL
2390 /*
2391 * When the expression evaluation is disabled, recognize the ":if" and
2392 * ":endif" commands and ignore everything in between it.
2393 */
2394 if (ea.cmdidx == CMD_if)
2395 ++if_level;
2396 if (if_level)
2397 {
2398 if (ea.cmdidx == CMD_endif)
2399 --if_level;
2400 goto doend;
2401 }
2402
2403#endif
2404
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002405 /* forced commands */
2406 if (*p == '!' && ea.cmdidx != CMD_substitute
2407 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 {
2409 ++p;
2410 ea.forceit = TRUE;
2411 }
2412 else
2413 ea.forceit = FALSE;
2414
2415/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002416 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002418 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002419 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420
2421 if (!ea.skip)
2422 {
2423#ifdef HAVE_SANDBOX
2424 if (sandbox != 0 && !(ea.argt & SBOXOK))
2425 {
2426 /* Command not allowed in sandbox. */
2427 errormsg = (char_u *)_(e_sandbox);
2428 goto doend;
2429 }
2430#endif
2431 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2432 {
2433 /* Command not allowed in non-'modifiable' buffer */
2434 errormsg = (char_u *)_(e_modifiable);
2435 goto doend;
2436 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002437
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002438 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002439 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002441 /* Command not allowed when editing the command line. */
2442#ifdef FEAT_CMDWIN
2443 if (cmdwin_type != 0)
2444 errormsg = (char_u *)_(e_cmdwin);
2445 else
2446#endif
2447 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 goto doend;
2449 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002450#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002451 /* Disallow editing another buffer when "curbuf_lock" is set.
2452 * Do allow ":edit" (check for argument later).
2453 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002454 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002455 && ea.cmdidx != CMD_edit
2456 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002457 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002458 && curbuf_locked())
2459 goto doend;
2460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461
2462 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2463 {
2464 /* no range allowed */
2465 errormsg = (char_u *)_(e_norange);
2466 goto doend;
2467 }
2468 }
2469
2470 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2471 {
2472 errormsg = (char_u *)_(e_nobang);
2473 goto doend;
2474 }
2475
2476 /*
2477 * Don't complain about the range if it is not used
2478 * (could happen if line_count is accidentally set to 0).
2479 */
2480 if (!ea.skip && !ni)
2481 {
2482 /*
2483 * If the range is backwards, ask for confirmation and, if given, swap
2484 * ea.line1 & ea.line2 so it's forwards again.
2485 * When global command is busy, don't ask, will fail below.
2486 */
2487 if (!global_busy && ea.line1 > ea.line2)
2488 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002489 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002491 if (sourcing || exmode_active)
2492 {
2493 errormsg = (char_u *)_("E493: Backwards range given");
2494 goto doend;
2495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 if (ask_yesno((char_u *)
2497 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002498 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 }
2500 lnum = ea.line1;
2501 ea.line1 = ea.line2;
2502 ea.line2 = lnum;
2503 }
2504 if ((errormsg = invalid_range(&ea)) != NULL)
2505 goto doend;
2506 }
2507
2508 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2509 ea.line2 = 1;
2510
2511 correct_range(&ea);
2512
2513#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002514 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2515 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 {
2517 /* Put the first line at the start of a closed fold, put the last line
2518 * at the end of a closed fold. */
2519 (void)hasFolding(ea.line1, &ea.line1, NULL);
2520 (void)hasFolding(ea.line2, NULL, &ea.line2);
2521 }
2522#endif
2523
2524#ifdef FEAT_QUICKFIX
2525 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002526 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 * option here, so things like % get expanded.
2528 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002529 p = replace_makeprg(&ea, p, cmdlinep);
2530 if (p == NULL)
2531 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532#endif
2533
2534 /*
2535 * Skip to start of argument.
2536 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2537 */
2538 if (ea.cmdidx == CMD_bang)
2539 ea.arg = p;
2540 else
2541 ea.arg = skipwhite(p);
2542
2543 /*
2544 * Check for "++opt=val" argument.
2545 * Must be first, allow ":w ++enc=utf8 !cmd"
2546 */
2547 if (ea.argt & ARGOPT)
2548 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2549 if (getargopt(&ea) == FAIL && !ni)
2550 {
2551 errormsg = (char_u *)_(e_invarg);
2552 goto doend;
2553 }
2554
2555 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2556 {
2557 if (*ea.arg == '>') /* append */
2558 {
2559 if (*++ea.arg != '>') /* typed wrong */
2560 {
2561 errormsg = (char_u *)_("E494: Use w or w>>");
2562 goto doend;
2563 }
2564 ea.arg = skipwhite(ea.arg + 1);
2565 ea.append = TRUE;
2566 }
2567 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2568 {
2569 ++ea.arg;
2570 ea.usefilter = TRUE;
2571 }
2572 }
2573
2574 if (ea.cmdidx == CMD_read)
2575 {
2576 if (ea.forceit)
2577 {
2578 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2579 ea.forceit = FALSE;
2580 }
2581 else if (*ea.arg == '!') /* :r !filter */
2582 {
2583 ++ea.arg;
2584 ea.usefilter = TRUE;
2585 }
2586 }
2587
2588 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2589 {
2590 ea.amount = 1;
2591 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2592 {
2593 ++ea.arg;
2594 ++ea.amount;
2595 }
2596 ea.arg = skipwhite(ea.arg);
2597 }
2598
2599 /*
2600 * Check for "+command" argument, before checking for next command.
2601 * Don't do this for ":read !cmd" and ":write !cmd".
2602 */
2603 if ((ea.argt & EDITCMD) && !ea.usefilter)
2604 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2605
2606 /*
2607 * Check for '|' to separate commands and '"' to start comments.
2608 * Don't do this for ":read !cmd" and ":write !cmd".
2609 */
2610 if ((ea.argt & TRLBAR) && !ea.usefilter)
2611 separate_nextcmd(&ea);
2612
2613 /*
2614 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002615 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2616 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002618 else if (ea.cmdidx == CMD_bang
2619 || ea.cmdidx == CMD_global
2620 || ea.cmdidx == CMD_vglobal
2621 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 {
2623 for (p = ea.arg; *p; ++p)
2624 {
2625 /* Remove one backslash before a newline, so that it's possible to
2626 * pass a newline to the shell and also a newline that is preceded
2627 * with a backslash. This makes it impossible to end a shell
2628 * command in a backslash, but that doesn't appear useful.
2629 * Halving the number of backslashes is incompatible with previous
2630 * versions. */
2631 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002632 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 else if (*p == '\n')
2634 {
2635 ea.nextcmd = p + 1;
2636 *p = NUL;
2637 break;
2638 }
2639 }
2640 }
2641
2642 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2643 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002644 buf_T *buf;
2645
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002647 switch (ea.addr_type)
2648 {
2649 case ADDR_LINES:
2650 ea.line2 = curbuf->b_ml.ml_line_count;
2651 break;
2652 case ADDR_LOADED_BUFFERS:
2653 buf = firstbuf;
2654 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2655 buf = buf->b_next;
2656 ea.line1 = buf->b_fnum;
2657 buf = lastbuf;
2658 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2659 buf = buf->b_prev;
2660 ea.line2 = buf->b_fnum;
2661 break;
2662 case ADDR_BUFFERS:
2663 ea.line1 = firstbuf->b_fnum;
2664 ea.line2 = lastbuf->b_fnum;
2665 break;
2666 case ADDR_WINDOWS:
2667 ea.line2 = LAST_WIN_NR;
2668 break;
2669 case ADDR_TABS:
2670 ea.line2 = LAST_TAB_NR;
2671 break;
2672 case ADDR_ARGUMENTS:
2673 if (ARGCOUNT == 0)
2674 ea.line1 = ea.line2 = 0;
2675 else
2676 ea.line2 = ARGCOUNT;
2677 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002678#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002679 case ADDR_QUICKFIX:
2680 ea.line2 = qf_get_size(&ea);
2681 if (ea.line2 == 0)
2682 ea.line2 = 1;
2683 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002684#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 }
2687
2688 /* accept numbered register only when no count allowed (:put) */
2689 if ( (ea.argt & REGSTR)
2690 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002691 /* Do not allow register = for user commands */
2692 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2694 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002695#ifndef FEAT_CLIPBOARD
2696 /* check these explicitly for a more specific error message */
2697 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002699 errormsg = (char_u *)_(e_invalidreg);
2700 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 }
2702#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002703 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2704 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002705 {
2706 ea.regname = *ea.arg++;
2707#ifdef FEAT_EVAL
2708 /* for '=' register: accept the rest of the line as an expression */
2709 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2710 {
2711 set_expr_line(vim_strsave(ea.arg));
2712 ea.arg += STRLEN(ea.arg);
2713 }
2714#endif
2715 ea.arg = skipwhite(ea.arg);
2716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 }
2718
2719 /*
2720 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2721 * count, it's a buffer name.
2722 */
2723 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2724 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2725 || vim_iswhite(*p)))
2726 {
2727 n = getdigits(&ea.arg);
2728 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002729 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 {
2731 errormsg = (char_u *)_(e_zerocount);
2732 goto doend;
2733 }
2734 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2735 {
2736 ea.line2 = n;
2737 if (ea.addr_count == 0)
2738 ea.addr_count = 1;
2739 }
2740 else
2741 {
2742 ea.line1 = ea.line2;
2743 ea.line2 += n - 1;
2744 ++ea.addr_count;
2745 /*
2746 * Be vi compatible: no error message for out of range.
2747 */
2748 if (ea.line2 > curbuf->b_ml.ml_line_count)
2749 ea.line2 = curbuf->b_ml.ml_line_count;
2750 }
2751 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002752
2753 /*
2754 * Check for flags: 'l', 'p' and '#'.
2755 */
2756 if (ea.argt & EXFLAGS)
2757 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002759 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002760 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 {
2762 errormsg = (char_u *)_(e_trailing);
2763 goto doend;
2764 }
2765
2766 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2767 {
2768 errormsg = (char_u *)_(e_argreq);
2769 goto doend;
2770 }
2771
2772#ifdef FEAT_EVAL
2773 /*
2774 * Skip the command when it's not going to be executed.
2775 * The commands like :if, :endif, etc. always need to be executed.
2776 * Also make an exception for commands that handle a trailing command
2777 * themselves.
2778 */
2779 if (ea.skip)
2780 {
2781 switch (ea.cmdidx)
2782 {
2783 /* commands that need evaluation */
2784 case CMD_while:
2785 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002786 case CMD_for:
2787 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 case CMD_if:
2789 case CMD_elseif:
2790 case CMD_else:
2791 case CMD_endif:
2792 case CMD_try:
2793 case CMD_catch:
2794 case CMD_finally:
2795 case CMD_endtry:
2796 case CMD_function:
2797 break;
2798
2799 /* Commands that handle '|' themselves. Check: A command should
2800 * either have the TRLBAR flag, appear in this list or appear in
2801 * the list at ":help :bar". */
2802 case CMD_aboveleft:
2803 case CMD_and:
2804 case CMD_belowright:
2805 case CMD_botright:
2806 case CMD_browse:
2807 case CMD_call:
2808 case CMD_confirm:
2809 case CMD_delfunction:
2810 case CMD_djump:
2811 case CMD_dlist:
2812 case CMD_dsearch:
2813 case CMD_dsplit:
2814 case CMD_echo:
2815 case CMD_echoerr:
2816 case CMD_echomsg:
2817 case CMD_echon:
2818 case CMD_execute:
2819 case CMD_help:
2820 case CMD_hide:
2821 case CMD_ijump:
2822 case CMD_ilist:
2823 case CMD_isearch:
2824 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002825 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 case CMD_keepjumps:
2827 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002828 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 case CMD_leftabove:
2830 case CMD_let:
2831 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002832 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002834 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002835 case CMD_noautocmd:
2836 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 case CMD_perl:
2838 case CMD_psearch:
2839 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002840 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002841 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 case CMD_return:
2843 case CMD_rightbelow:
2844 case CMD_ruby:
2845 case CMD_silent:
2846 case CMD_smagic:
2847 case CMD_snomagic:
2848 case CMD_substitute:
2849 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002850 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 case CMD_tcl:
2852 case CMD_throw:
2853 case CMD_tilde:
2854 case CMD_topleft:
2855 case CMD_unlet:
2856 case CMD_verbose:
2857 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002858 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 break;
2860
2861 default: goto doend;
2862 }
2863 }
2864#endif
2865
2866 if (ea.argt & XFILE)
2867 {
2868 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2869 goto doend;
2870 }
2871
2872#ifdef FEAT_LISTCMDS
2873 /*
2874 * Accept buffer name. Cannot be used at the same time with a buffer
2875 * number. Don't do this for a user command.
2876 */
2877 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002878 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 {
2880 /*
2881 * :bdelete, :bwipeout and :bunload take several arguments, separated
2882 * by spaces: find next space (skipping over escaped characters).
2883 * The others take one argument: ignore trailing spaces.
2884 */
2885 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2886 || ea.cmdidx == CMD_bunload)
2887 p = skiptowhite_esc(ea.arg);
2888 else
2889 {
2890 p = ea.arg + STRLEN(ea.arg);
2891 while (p > ea.arg && vim_iswhite(p[-1]))
2892 --p;
2893 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002894 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2895 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 if (ea.line2 < 0) /* failed */
2897 goto doend;
2898 ea.addr_count = 1;
2899 ea.arg = skipwhite(p);
2900 }
2901#endif
2902
2903/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002904 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 *
2906 * The "ea" structure holds the arguments that can be used.
2907 */
2908 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002909 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 ea.cookie = cookie;
2911#ifdef FEAT_EVAL
2912 ea.cstack = cstack;
2913#endif
2914
2915#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002916 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 {
2918 /*
2919 * Execute a user-defined command.
2920 */
2921 do_ucmd(&ea);
2922 }
2923 else
2924#endif
2925 {
2926 /*
2927 * Call the function to execute the command.
2928 */
2929 ea.errmsg = NULL;
2930 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2931 if (ea.errmsg != NULL)
2932 errormsg = (char_u *)_(ea.errmsg);
2933 }
2934
2935#ifdef FEAT_EVAL
2936 /*
2937 * If the command just executed called do_cmdline(), any throw or ":return"
2938 * or ":finish" encountered there must also check the cstack of the still
2939 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2940 * exception, or reanimate a returned function or finished script file and
2941 * return or finish it again.
2942 */
2943 if (need_rethrow)
2944 do_throw(cstack);
2945 else if (check_cstack)
2946 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002947 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002949 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 && current_func_returned())
2951 do_return(&ea, TRUE, FALSE, NULL);
2952 }
2953 need_rethrow = check_cstack = FALSE;
2954#endif
2955
2956doend:
2957 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2958 curwin->w_cursor.lnum = 1;
2959
2960 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2961 {
2962 if (sourcing)
2963 {
2964 if (errormsg != IObuff)
2965 {
2966 STRCPY(IObuff, errormsg);
2967 errormsg = IObuff;
2968 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002969 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 }
2971 emsg(errormsg);
2972 }
2973#ifdef FEAT_EVAL
2974 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002975 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2976 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977#endif
2978
2979 if (verbose_save >= 0)
2980 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002981#ifdef FEAT_AUTOCMD
2982 if (cmdmod.save_ei != NULL)
2983 {
2984 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002985 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2986 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002987 free_string_option(cmdmod.save_ei);
2988 }
2989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990
2991 cmdmod = save_cmdmod;
2992
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002993 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 {
2995 /* messages could be enabled for a serious error, need to check if the
2996 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002997 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002998 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 emsg_silent -= did_esilent;
3000 if (emsg_silent < 0)
3001 emsg_silent = 0;
3002 /* Restore msg_scroll, it's set by file I/O commands, even when no
3003 * message is actually displayed. */
3004 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003005
3006 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
3007 * somewhere in the line. Put it back in the first column. */
3008 if (redirecting())
3009 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 }
3011
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003012#ifdef HAVE_SANDBOX
3013 if (did_sandbox)
3014 --sandbox;
3015#endif
3016
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3018 ea.nextcmd = NULL;
3019
3020#ifdef FEAT_EVAL
3021 --ex_nesting_level;
3022#endif
3023
3024 return ea.nextcmd;
3025}
3026#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003027 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028#endif
3029
3030/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003031 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 * If there is a match advance "pp" to the argument and return TRUE.
3033 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003034 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003035checkforcmd(
3036 char_u **pp, /* start of command */
3037 char *cmd, /* name of command */
3038 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039{
3040 int i;
3041
3042 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003043 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 break;
3045 if (i >= len && !isalpha((*pp)[i]))
3046 {
3047 *pp = skipwhite(*pp + i);
3048 return TRUE;
3049 }
3050 return FALSE;
3051}
3052
3053/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003054 * Append "cmd" to the error message in IObuff.
3055 * Takes care of limiting the length and handling 0xa0, which would be
3056 * invisible otherwise.
3057 */
3058 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003059append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003060{
3061 char_u *s = cmd;
3062 char_u *d;
3063
3064 STRCAT(IObuff, ": ");
3065 d = IObuff + STRLEN(IObuff);
3066 while (*s != NUL && d - IObuff < IOSIZE - 7)
3067 {
3068 if (
3069#ifdef FEAT_MBYTE
3070 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3071#endif
3072 *s == 0xa0)
3073 {
3074 s +=
3075#ifdef FEAT_MBYTE
3076 enc_utf8 ? 2 :
3077#endif
3078 1;
3079 STRCPY(d, "<a0>");
3080 d += 4;
3081 }
3082 else
3083 MB_COPY_CHAR(s, d);
3084 }
3085 *d = NUL;
3086}
3087
3088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003090 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003092 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 * Returns NULL for an ambiguous user command.
3094 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003096find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097{
3098 int len;
3099 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003100 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101
3102 /*
3103 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003104 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 * - the 'k' command can directly be followed by any character.
3106 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003107 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003109 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 */
3111 p = eap->cmd;
3112 if (*p == 'k')
3113 {
3114 eap->cmdidx = CMD_k;
3115 ++p;
3116 }
3117 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003118 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3119 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 || p[1] == 'g'
3121 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3122 || p[1] == 'I'
3123 || (p[1] == 'r' && p[2] != 'e')))
3124 {
3125 eap->cmdidx = CMD_substitute;
3126 ++p;
3127 }
3128 else
3129 {
3130 while (ASCII_ISALPHA(*p))
3131 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003132 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003133 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003134 while (ASCII_ISALNUM(*p))
3135 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003136
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 /* check for non-alpha command */
3138 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3139 ++p;
3140 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003141 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3142 {
3143 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3144 * :delete with the 'l' flag. Same for 'p'. */
3145 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003146 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003147 break;
3148 if (i == len - 1)
3149 {
3150 --len;
3151 if (p[-1] == 'l')
3152 eap->flags |= EXFLAG_LIST;
3153 else
3154 eap->flags |= EXFLAG_PRINT;
3155 }
3156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157
3158 if (ASCII_ISLOWER(*eap->cmd))
3159 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
3160 else
3161 eap->cmdidx = cmdidxs[26];
3162
3163 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3164 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3165 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3166 (size_t)len) == 0)
3167 {
3168#ifdef FEAT_EVAL
3169 if (full != NULL
3170 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3171 *full = TRUE;
3172#endif
3173 break;
3174 }
3175
3176#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003177 /* Look for a user defined command as a last resort. Let ":Print" be
3178 * overruled by a user defined command. */
3179 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3180 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003182 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 while (ASCII_ISALNUM(*p))
3184 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003185 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 }
3187#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003188 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 eap->cmdidx = CMD_SIZE;
3190 }
3191
3192 return p;
3193}
3194
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003195#ifdef FEAT_USR_CMDS
3196/*
3197 * Search for a user command that matches "eap->cmd".
3198 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3199 * Return a pointer to just after the command.
3200 * Return NULL if there is no matching command.
3201 */
3202 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003203find_ucmd(
3204 exarg_T *eap,
3205 char_u *p, /* end of the command (possibly including count) */
3206 int *full, /* set to TRUE for a full match */
3207 expand_T *xp, /* used for completion, NULL otherwise */
3208 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003209{
3210 int len = (int)(p - eap->cmd);
3211 int j, k, matchlen = 0;
3212 ucmd_T *uc;
3213 int found = FALSE;
3214 int possible = FALSE;
3215 char_u *cp, *np; /* Point into typed cmd and test name */
3216 garray_T *gap;
3217 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3218 only full match global is accepted. */
3219
3220 /*
3221 * Look for buffer-local user commands first, then global ones.
3222 */
3223 gap = &curbuf->b_ucmds;
3224 for (;;)
3225 {
3226 for (j = 0; j < gap->ga_len; ++j)
3227 {
3228 uc = USER_CMD_GA(gap, j);
3229 cp = eap->cmd;
3230 np = uc->uc_name;
3231 k = 0;
3232 while (k < len && *np != NUL && *cp++ == *np++)
3233 k++;
3234 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3235 {
3236 /* If finding a second match, the command is ambiguous. But
3237 * not if a buffer-local command wasn't a full match and a
3238 * global command is a full match. */
3239 if (k == len && found && *np != NUL)
3240 {
3241 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003242 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003243 amb_local = TRUE;
3244 }
3245
3246 if (!found || (k == len && *np == NUL))
3247 {
3248 /* If we matched up to a digit, then there could
3249 * be another command including the digit that we
3250 * should use instead.
3251 */
3252 if (k == len)
3253 found = TRUE;
3254 else
3255 possible = TRUE;
3256
3257 if (gap == &ucmds)
3258 eap->cmdidx = CMD_USER;
3259 else
3260 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003261 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003262 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003263 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003264
3265# ifdef FEAT_CMDL_COMPL
3266 if (compl != NULL)
3267 *compl = uc->uc_compl;
3268# ifdef FEAT_EVAL
3269 if (xp != NULL)
3270 {
3271 xp->xp_arg = uc->uc_compl_arg;
3272 xp->xp_scriptID = uc->uc_scriptID;
3273 }
3274# endif
3275# endif
3276 /* Do not search for further abbreviations
3277 * if this is an exact match. */
3278 matchlen = k;
3279 if (k == len && *np == NUL)
3280 {
3281 if (full != NULL)
3282 *full = TRUE;
3283 amb_local = FALSE;
3284 break;
3285 }
3286 }
3287 }
3288 }
3289
3290 /* Stop if we found a full match or searched all. */
3291 if (j < gap->ga_len || gap == &ucmds)
3292 break;
3293 gap = &ucmds;
3294 }
3295
3296 /* Only found ambiguous matches. */
3297 if (amb_local)
3298 {
3299 if (xp != NULL)
3300 xp->xp_context = EXPAND_UNSUCCESSFUL;
3301 return NULL;
3302 }
3303
3304 /* The match we found may be followed immediately by a number. Move "p"
3305 * back to point to it. */
3306 if (found || possible)
3307 return p + (matchlen - len);
3308 return p;
3309}
3310#endif
3311
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003313static struct cmdmod
3314{
3315 char *name;
3316 int minlen;
3317 int has_count; /* :123verbose :3tab */
3318} cmdmods[] = {
3319 {"aboveleft", 3, FALSE},
3320 {"belowright", 3, FALSE},
3321 {"botright", 2, FALSE},
3322 {"browse", 3, FALSE},
3323 {"confirm", 4, FALSE},
3324 {"hide", 3, FALSE},
3325 {"keepalt", 5, FALSE},
3326 {"keepjumps", 5, FALSE},
3327 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003328 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003329 {"leftabove", 5, FALSE},
3330 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003331 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003332 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003333 {"rightbelow", 6, FALSE},
3334 {"sandbox", 3, FALSE},
3335 {"silent", 3, FALSE},
3336 {"tab", 3, TRUE},
3337 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003338 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003339 {"verbose", 4, TRUE},
3340 {"vertical", 4, FALSE},
3341};
3342
3343/*
3344 * Return length of a command modifier (including optional count).
3345 * Return zero when it's not a modifier.
3346 */
3347 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003348modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003349{
3350 int i, j;
3351 char_u *p = cmd;
3352
3353 if (VIM_ISDIGIT(*cmd))
3354 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003355 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003356 {
3357 for (j = 0; p[j] != NUL; ++j)
3358 if (p[j] != cmdmods[i].name[j])
3359 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003360 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003361 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003362 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003363 }
3364 return 0;
3365}
3366
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367/*
3368 * Return > 0 if an Ex command "name" exists.
3369 * Return 2 if there is an exact match.
3370 * Return 3 if there is an ambiguous match.
3371 */
3372 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003373cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374{
3375 exarg_T ea;
3376 int full = FALSE;
3377 int i;
3378 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003379 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380
3381 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003382 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 {
3384 for (j = 0; name[j] != NUL; ++j)
3385 if (name[j] != cmdmods[i].name[j])
3386 break;
3387 if (name[j] == NUL && j >= cmdmods[i].minlen)
3388 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3389 }
3390
Bram Moolenaara9587612006-05-04 21:47:50 +00003391 /* Check built-in commands and user defined commands.
3392 * For ":2match" and ":3match" we need to skip the number. */
3393 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003395 p = find_command(&ea, &full);
3396 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003398 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3399 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003400 if (*skipwhite(p) != NUL)
3401 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3403}
3404#endif
3405
3406/*
3407 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3408 * we don't need/want deleted. Maybe this could be done better if we didn't
3409 * repeat all this stuff. The only problem is that they may not stay
3410 * perfectly compatible with each other, but then the command line syntax
3411 * probably won't change that much -- webb.
3412 */
3413 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003414set_one_cmd_context(
3415 expand_T *xp,
3416 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417{
3418 char_u *p;
3419 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003420 int len = 0;
3421 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3423 int compl = EXPAND_NOTHING;
3424#endif
3425#ifdef FEAT_CMDL_COMPL
3426 int delim;
3427#endif
3428 int forceit = FALSE;
3429 int usefilter = FALSE; /* filter instead of file name */
3430
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003431 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 xp->xp_pattern = buff;
3433 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003434 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435
3436/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003437 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 */
3439 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3440 ;
3441 xp->xp_pattern = cmd;
3442
3443 if (*cmd == NUL)
3444 return NULL;
3445 if (*cmd == '"') /* ignore comment lines */
3446 {
3447 xp->xp_context = EXPAND_NOTHING;
3448 return NULL;
3449 }
3450
3451/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003452 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 */
3454 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 xp->xp_pattern = cmd;
3456 if (*cmd == NUL)
3457 return NULL;
3458 if (*cmd == '"')
3459 {
3460 xp->xp_context = EXPAND_NOTHING;
3461 return NULL;
3462 }
3463
3464 if (*cmd == '|' || *cmd == '\n')
3465 return cmd + 1; /* There's another command */
3466
3467 /*
3468 * Isolate the command and search for it in the command table.
3469 * Exceptions:
3470 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003471 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3473 */
3474 if (*cmd == 'k' && cmd[1] != 'e')
3475 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003476 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 p = cmd + 1;
3478 }
3479 else
3480 {
3481 p = cmd;
3482 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3483 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003484 /* a user command may contain digits */
3485 if (ASCII_ISUPPER(cmd[0]))
3486 while (ASCII_ISALNUM(*p) || *p == '*')
3487 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003488 /* for python 3.x: ":py3*" commands completion */
3489 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003490 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003491 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003492 while (ASCII_ISALPHA(*p) || *p == '*')
3493 ++p;
3494 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003495 /* check for non-alpha command */
3496 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3497 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003498 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003500 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 {
3502 xp->xp_context = EXPAND_UNSUCCESSFUL;
3503 return NULL;
3504 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003505 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003506 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3507 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3508 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 break;
3510
3511#ifdef FEAT_USR_CMDS
3512 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3514 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515#endif
3516 }
3517
3518 /*
3519 * If the cursor is touching the command, and it ends in an alpha-numeric
3520 * character, complete the command name.
3521 */
3522 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3523 return NULL;
3524
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003525 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 {
3527 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3528 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003529 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 p = cmd + 1;
3531 }
3532#ifdef FEAT_USR_CMDS
3533 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3534 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003535 ea.cmd = cmd;
3536 p = find_ucmd(&ea, p, NULL, xp,
3537# if defined(FEAT_CMDL_COMPL)
3538 &compl
3539# else
3540 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003542 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003543 if (p == NULL)
3544 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 }
3546#endif
3547 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003548 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 {
3550 /* Not still touching the command and it was an illegal one */
3551 xp->xp_context = EXPAND_UNSUCCESSFUL;
3552 return NULL;
3553 }
3554
3555 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3556
3557 if (*p == '!') /* forced commands */
3558 {
3559 forceit = TRUE;
3560 ++p;
3561 }
3562
3563/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003564 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003566 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003567 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568
3569 arg = skipwhite(p);
3570
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003571 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 {
3573 if (*arg == '>') /* append */
3574 {
3575 if (*++arg == '>')
3576 ++arg;
3577 arg = skipwhite(arg);
3578 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003579 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 {
3581 ++arg;
3582 usefilter = TRUE;
3583 }
3584 }
3585
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003586 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 {
3588 usefilter = forceit; /* :r! filter if forced */
3589 if (*arg == '!') /* :r !filter */
3590 {
3591 ++arg;
3592 usefilter = TRUE;
3593 }
3594 }
3595
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003596 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 {
3598 while (*arg == *cmd) /* allow any number of '>' or '<' */
3599 ++arg;
3600 arg = skipwhite(arg);
3601 }
3602
3603 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003604 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 {
3606 /* Check if we're in the +command */
3607 p = arg + 1;
3608 arg = skip_cmd_arg(arg, FALSE);
3609
3610 /* Still touching the command after '+'? */
3611 if (*arg == NUL)
3612 return p;
3613
3614 /* Skip space(s) after +command to get to the real argument */
3615 arg = skipwhite(arg);
3616 }
3617
3618 /*
3619 * Check for '|' to separate commands and '"' to start comments.
3620 * Don't do this for ":read !cmd" and ":write !cmd".
3621 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003622 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 {
3624 p = arg;
3625 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003626 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 p += 2;
3628 while (*p)
3629 {
3630 if (*p == Ctrl_V)
3631 {
3632 if (p[1] != NUL)
3633 ++p;
3634 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003635 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 || *p == '|' || *p == '\n')
3637 {
3638 if (*(p - 1) != '\\')
3639 {
3640 if (*p == '|' || *p == '\n')
3641 return p + 1;
3642 return NULL; /* It's a comment */
3643 }
3644 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003645 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 }
3647 }
3648
3649 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003650 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 vim_strchr((char_u *)"|\"", *arg) == NULL)
3652 return NULL;
3653
3654 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003655 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003657 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003658 while (*p && p < buff + len)
3659 {
3660 if (*p == ' ' || *p == TAB)
3661 {
3662 /* argument starts after a space */
3663 xp->xp_pattern = ++p;
3664 }
3665 else
3666 {
3667 if (*p == '\\' && *(p + 1) != NUL)
3668 ++p; /* skip over escaped character */
3669 mb_ptr_adv(p);
3670 }
3671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003673 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003675 int c;
3676 int in_quote = FALSE;
3677 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678
3679 /*
3680 * Allow spaces within back-quotes to count as part of the argument
3681 * being expanded.
3682 */
3683 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003684 p = xp->xp_pattern;
3685 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003687#ifdef FEAT_MBYTE
3688 if (has_mbyte)
3689 c = mb_ptr2char(p);
3690 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003692 c = *p;
3693 if (c == '\\' && p[1] != NUL)
3694 ++p;
3695 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 {
3697 if (!in_quote)
3698 {
3699 xp->xp_pattern = p;
3700 bow = p + 1;
3701 }
3702 in_quote = !in_quote;
3703 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003704 /* An argument can contain just about everything, except
3705 * characters that end the command and white space. */
3706 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003707#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003708 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003709#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003710 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003711 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003712 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003713 while (*p != NUL)
3714 {
3715#ifdef FEAT_MBYTE
3716 if (has_mbyte)
3717 c = mb_ptr2char(p);
3718 else
3719#endif
3720 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003721 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003722 break;
3723#ifdef FEAT_MBYTE
3724 if (has_mbyte)
3725 len = (*mb_ptr2len)(p);
3726 else
3727#endif
3728 len = 1;
3729 mb_ptr_adv(p);
3730 }
3731 if (in_quote)
3732 bow = p;
3733 else
3734 xp->xp_pattern = p;
3735 p -= len;
3736 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003737 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 }
3739
3740 /*
3741 * If we are still inside the quotes, and we passed a space, just
3742 * expand from there.
3743 */
3744 if (bow != NULL && in_quote)
3745 xp->xp_pattern = bow;
3746 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003747
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003748 /* For a shell command more chars need to be escaped. */
3749 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003750 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003751#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003752 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003753#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003754 /* When still after the command name expand executables. */
3755 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003756 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758
3759 /* Check for environment variable */
3760 if (*xp->xp_pattern == '$'
Bram Moolenaare7fedb62015-12-31 19:07:19 +01003761#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 || *xp->xp_pattern == '%'
3763#endif
3764 )
3765 {
3766 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3767 if (!vim_isIDc(*p))
3768 break;
3769 if (*p == NUL)
3770 {
3771 xp->xp_context = EXPAND_ENV_VARS;
3772 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003773#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3774 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003775 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003776 compl = EXPAND_ENV_VARS;
3777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 }
3779 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003780#if defined(FEAT_CMDL_COMPL)
3781 /* Check for user names */
3782 if (*xp->xp_pattern == '~')
3783 {
3784 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3785 ;
3786 /* Complete ~user only if it partially matches a user name.
3787 * A full match ~user<Tab> will be replaced by user's home
3788 * directory i.e. something like ~user<Tab> -> /home/user/ */
3789 if (*p == NUL && p > xp->xp_pattern + 1
3790 && match_user(xp->xp_pattern + 1) == 1)
3791 {
3792 xp->xp_context = EXPAND_USER;
3793 ++xp->xp_pattern;
3794 }
3795 }
3796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 }
3798
3799/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003800 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003802 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003804 case CMD_find:
3805 case CMD_sfind:
3806 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003807 if (xp->xp_context == EXPAND_FILES)
3808 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003809 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 case CMD_cd:
3811 case CMD_chdir:
3812 case CMD_lcd:
3813 case CMD_lchdir:
3814 if (xp->xp_context == EXPAND_FILES)
3815 xp->xp_context = EXPAND_DIRECTORIES;
3816 break;
3817 case CMD_help:
3818 xp->xp_context = EXPAND_HELP;
3819 xp->xp_pattern = arg;
3820 break;
3821
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003822 /* Command modifiers: return the argument.
3823 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003825 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 case CMD_belowright:
3827 case CMD_botright:
3828 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003829 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003830 case CMD_cdo:
3831 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003833 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 case CMD_folddoclosed:
3835 case CMD_folddoopen:
3836 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003837 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 case CMD_keepjumps:
3839 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003840 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003841 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003843 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003845 case CMD_noautocmd:
3846 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003848 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003850 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003851 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 case CMD_topleft:
3853 case CMD_verbose:
3854 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003855 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 return arg;
3857
Bram Moolenaar4f688582007-07-24 12:34:30 +00003858#ifdef FEAT_CMDL_COMPL
3859# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 case CMD_match:
3861 if (*arg == NUL || !ends_excmd(*arg))
3862 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003863 /* also complete "None" */
3864 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 arg = skipwhite(skiptowhite(arg));
3866 if (*arg != NUL)
3867 {
3868 xp->xp_context = EXPAND_NOTHING;
3869 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3870 }
3871 }
3872 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003873# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875/*
3876 * All completion for the +cmdline_compl feature goes here.
3877 */
3878
3879# ifdef FEAT_USR_CMDS
3880 case CMD_command:
3881 /* Check for attributes */
3882 while (*arg == '-')
3883 {
3884 arg++; /* Skip "-" */
3885 p = skiptowhite(arg);
3886 if (*p == NUL)
3887 {
3888 /* Cursor is still in the attribute */
3889 p = vim_strchr(arg, '=');
3890 if (p == NULL)
3891 {
3892 /* No "=", so complete attribute names */
3893 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3894 xp->xp_pattern = arg;
3895 return NULL;
3896 }
3897
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003898 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 * their arguments as well.
3900 */
3901 if (STRNICMP(arg, "complete", p - arg) == 0)
3902 {
3903 xp->xp_context = EXPAND_USER_COMPLETE;
3904 xp->xp_pattern = p + 1;
3905 return NULL;
3906 }
3907 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3908 {
3909 xp->xp_context = EXPAND_USER_NARGS;
3910 xp->xp_pattern = p + 1;
3911 return NULL;
3912 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003913 else if (STRNICMP(arg, "addr", p - arg) == 0)
3914 {
3915 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3916 xp->xp_pattern = p + 1;
3917 return NULL;
3918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 return NULL;
3920 }
3921 arg = skipwhite(p);
3922 }
3923
3924 /* After the attributes comes the new command name */
3925 p = skiptowhite(arg);
3926 if (*p == NUL)
3927 {
3928 xp->xp_context = EXPAND_USER_COMMANDS;
3929 xp->xp_pattern = arg;
3930 break;
3931 }
3932
3933 /* And finally comes a normal command */
3934 return skipwhite(p);
3935
3936 case CMD_delcommand:
3937 xp->xp_context = EXPAND_USER_COMMANDS;
3938 xp->xp_pattern = arg;
3939 break;
3940# endif
3941
3942 case CMD_global:
3943 case CMD_vglobal:
3944 delim = *arg; /* get the delimiter */
3945 if (delim)
3946 ++arg; /* skip delimiter if there is one */
3947
3948 while (arg[0] != NUL && arg[0] != delim)
3949 {
3950 if (arg[0] == '\\' && arg[1] != NUL)
3951 ++arg;
3952 ++arg;
3953 }
3954 if (arg[0] != NUL)
3955 return arg + 1;
3956 break;
3957 case CMD_and:
3958 case CMD_substitute:
3959 delim = *arg;
3960 if (delim)
3961 {
3962 /* skip "from" part */
3963 ++arg;
3964 arg = skip_regexp(arg, delim, p_magic, NULL);
3965 }
3966 /* skip "to" part */
3967 while (arg[0] != NUL && arg[0] != delim)
3968 {
3969 if (arg[0] == '\\' && arg[1] != NUL)
3970 ++arg;
3971 ++arg;
3972 }
3973 if (arg[0] != NUL) /* skip delimiter */
3974 ++arg;
3975 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3976 ++arg;
3977 if (arg[0] != NUL)
3978 return arg;
3979 break;
3980 case CMD_isearch:
3981 case CMD_dsearch:
3982 case CMD_ilist:
3983 case CMD_dlist:
3984 case CMD_ijump:
3985 case CMD_psearch:
3986 case CMD_djump:
3987 case CMD_isplit:
3988 case CMD_dsplit:
3989 arg = skipwhite(skipdigits(arg)); /* skip count */
3990 if (*arg == '/') /* Match regexp, not just whole words */
3991 {
3992 for (++arg; *arg && *arg != '/'; arg++)
3993 if (*arg == '\\' && arg[1] != NUL)
3994 arg++;
3995 if (*arg)
3996 {
3997 arg = skipwhite(arg + 1);
3998
3999 /* Check for trailing illegal characters */
4000 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4001 xp->xp_context = EXPAND_NOTHING;
4002 else
4003 return arg;
4004 }
4005 }
4006 break;
4007#ifdef FEAT_AUTOCMD
4008 case CMD_autocmd:
4009 return set_context_in_autocmd(xp, arg, FALSE);
4010
4011 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004012 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 return set_context_in_autocmd(xp, arg, TRUE);
4014#endif
4015 case CMD_set:
4016 set_context_in_set_cmd(xp, arg, 0);
4017 break;
4018 case CMD_setglobal:
4019 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4020 break;
4021 case CMD_setlocal:
4022 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4023 break;
4024 case CMD_tag:
4025 case CMD_stag:
4026 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004027 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 case CMD_tselect:
4029 case CMD_stselect:
4030 case CMD_ptselect:
4031 case CMD_tjump:
4032 case CMD_stjump:
4033 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004034 if (*p_wop != NUL)
4035 xp->xp_context = EXPAND_TAGS_LISTFILES;
4036 else
4037 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 xp->xp_pattern = arg;
4039 break;
4040 case CMD_augroup:
4041 xp->xp_context = EXPAND_AUGROUP;
4042 xp->xp_pattern = arg;
4043 break;
4044#ifdef FEAT_SYN_HL
4045 case CMD_syntax:
4046 set_context_in_syntax_cmd(xp, arg);
4047 break;
4048#endif
4049#ifdef FEAT_EVAL
4050 case CMD_let:
4051 case CMD_if:
4052 case CMD_elseif:
4053 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004054 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 case CMD_echo:
4056 case CMD_echon:
4057 case CMD_execute:
4058 case CMD_echomsg:
4059 case CMD_echoerr:
4060 case CMD_call:
4061 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004062 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 break;
4064
4065 case CMD_unlet:
4066 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4067 arg = xp->xp_pattern + 1;
4068 xp->xp_context = EXPAND_USER_VARS;
4069 xp->xp_pattern = arg;
4070 break;
4071
4072 case CMD_function:
4073 case CMD_delfunction:
4074 xp->xp_context = EXPAND_USER_FUNC;
4075 xp->xp_pattern = arg;
4076 break;
4077
4078 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004079 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 break;
4081#endif
4082 case CMD_highlight:
4083 set_context_in_highlight_cmd(xp, arg);
4084 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004085#ifdef FEAT_CSCOPE
4086 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004087 case CMD_lcscope:
4088 case CMD_scscope:
4089 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004090 break;
4091#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004092#ifdef FEAT_SIGNS
4093 case CMD_sign:
4094 set_context_in_sign_cmd(xp, arg);
4095 break;
4096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097#ifdef FEAT_LISTCMDS
4098 case CMD_bdelete:
4099 case CMD_bwipeout:
4100 case CMD_bunload:
4101 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4102 arg = xp->xp_pattern + 1;
4103 /*FALLTHROUGH*/
4104 case CMD_buffer:
4105 case CMD_sbuffer:
4106 case CMD_checktime:
4107 xp->xp_context = EXPAND_BUFFERS;
4108 xp->xp_pattern = arg;
4109 break;
4110#endif
4111#ifdef FEAT_USR_CMDS
4112 case CMD_USER:
4113 case CMD_USER_BUF:
4114 if (compl != EXPAND_NOTHING)
4115 {
4116 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004117 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 {
4119# ifdef FEAT_MENU
4120 if (compl == EXPAND_MENUS)
4121 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4122# endif
4123 if (compl == EXPAND_COMMANDS)
4124 return arg;
4125 if (compl == EXPAND_MAPPINGS)
4126 return set_context_in_map_cmd(xp, (char_u *)"map",
4127 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004128 /* Find start of last argument. */
4129 p = arg;
4130 while (*p)
4131 {
4132 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004133 /* argument starts after a space */
4134 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004135 else if (*p == '\\' && *(p + 1) != NUL)
4136 ++p; /* skip over escaped character */
4137 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 xp->xp_pattern = arg;
4140 }
4141 xp->xp_context = compl;
4142 }
4143 break;
4144#endif
4145 case CMD_map: case CMD_noremap:
4146 case CMD_nmap: case CMD_nnoremap:
4147 case CMD_vmap: case CMD_vnoremap:
4148 case CMD_omap: case CMD_onoremap:
4149 case CMD_imap: case CMD_inoremap:
4150 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004151 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004152 case CMD_smap: case CMD_snoremap:
4153 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004155 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 case CMD_unmap:
4157 case CMD_nunmap:
4158 case CMD_vunmap:
4159 case CMD_ounmap:
4160 case CMD_iunmap:
4161 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004162 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004163 case CMD_sunmap:
4164 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004166 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 case CMD_abbreviate: case CMD_noreabbrev:
4168 case CMD_cabbrev: case CMD_cnoreabbrev:
4169 case CMD_iabbrev: case CMD_inoreabbrev:
4170 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004171 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 case CMD_unabbreviate:
4173 case CMD_cunabbrev:
4174 case CMD_iunabbrev:
4175 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004176 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177#ifdef FEAT_MENU
4178 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4179 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4180 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4181 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4182 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4183 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4184 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4185 case CMD_tmenu: case CMD_tunmenu:
4186 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4187 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4188#endif
4189
4190 case CMD_colorscheme:
4191 xp->xp_context = EXPAND_COLORS;
4192 xp->xp_pattern = arg;
4193 break;
4194
4195 case CMD_compiler:
4196 xp->xp_context = EXPAND_COMPILER;
4197 xp->xp_pattern = arg;
4198 break;
4199
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004200 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004201 xp->xp_context = EXPAND_OWNSYNTAX;
4202 xp->xp_pattern = arg;
4203 break;
4204
4205 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004206 xp->xp_context = EXPAND_FILETYPE;
4207 xp->xp_pattern = arg;
4208 break;
4209
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4211 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4212 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004213 p = skiptowhite(arg);
4214 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 {
4216 xp->xp_context = EXPAND_LANGUAGE;
4217 xp->xp_pattern = arg;
4218 }
4219 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004220 {
4221 if ( STRNCMP(arg, "messages", p - arg) == 0
4222 || STRNCMP(arg, "ctype", p - arg) == 0
4223 || STRNCMP(arg, "time", p - arg) == 0)
4224 {
4225 xp->xp_context = EXPAND_LOCALES;
4226 xp->xp_pattern = skipwhite(p);
4227 }
4228 else
4229 xp->xp_context = EXPAND_NOTHING;
4230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 break;
4232#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004233#if defined(FEAT_PROFILE)
4234 case CMD_profile:
4235 set_context_in_profile_cmd(xp, arg);
4236 break;
4237#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004238 case CMD_behave:
4239 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004240 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004241 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004243#if defined(FEAT_CMDHIST)
4244 case CMD_history:
4245 xp->xp_context = EXPAND_HISTORY;
4246 xp->xp_pattern = arg;
4247 break;
4248#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004249#if defined(FEAT_PROFILE)
4250 case CMD_syntime:
4251 xp->xp_context = EXPAND_SYNTIME;
4252 xp->xp_pattern = arg;
4253 break;
4254#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004255
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256#endif /* FEAT_CMDL_COMPL */
4257
4258 default:
4259 break;
4260 }
4261 return NULL;
4262}
4263
4264/*
4265 * skip a range specifier of the form: addr [,addr] [;addr] ..
4266 *
4267 * Backslashed delimiters after / or ? will be skipped, and commands will
4268 * not be expanded between /'s and ?'s or after "'".
4269 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004270 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 * Returns the "cmd" pointer advanced to beyond the range.
4272 */
4273 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004274skip_range(
4275 char_u *cmd,
4276 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004278 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279
Bram Moolenaardf177f62005-02-22 08:39:57 +00004280 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 {
4282 if (*cmd == '\'')
4283 {
4284 if (*++cmd == NUL && ctx != NULL)
4285 *ctx = EXPAND_NOTHING;
4286 }
4287 else if (*cmd == '/' || *cmd == '?')
4288 {
4289 delim = *cmd++;
4290 while (*cmd != NUL && *cmd != delim)
4291 if (*cmd++ == '\\' && *cmd != NUL)
4292 ++cmd;
4293 if (*cmd == NUL && ctx != NULL)
4294 *ctx = EXPAND_NOTHING;
4295 }
4296 if (*cmd != NUL)
4297 ++cmd;
4298 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004299
4300 /* Skip ":" and white space. */
4301 while (*cmd == ':')
4302 cmd = skipwhite(cmd + 1);
4303
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 return cmd;
4305}
4306
4307/*
4308 * get a single EX address
4309 *
4310 * Set ptr to the next character after the part that was interpreted.
4311 * Set ptr to NULL when an error is encountered.
4312 *
4313 * Return MAXLNUM when no Ex address was found.
4314 */
4315 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004316get_address(
4317 exarg_T *eap UNUSED,
4318 char_u **ptr,
4319 int addr_type, /* flag: one of ADDR_LINES, ... */
4320 int skip, /* only skip the address, don't use it */
4321 int to_other_file) /* flag: may jump to other file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322{
4323 int c;
4324 int i;
4325 long n;
4326 char_u *cmd;
4327 pos_T pos;
4328 pos_T *fp;
4329 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004330 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331
4332 cmd = skipwhite(*ptr);
4333 lnum = MAXLNUM;
4334 do
4335 {
4336 switch (*cmd)
4337 {
4338 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004339 ++cmd;
4340 switch (addr_type)
4341 {
4342 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 lnum = curwin->w_cursor.lnum;
4344 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004345 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004346 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004347 break;
4348 case ADDR_ARGUMENTS:
4349 lnum = curwin->w_arg_idx + 1;
4350 break;
4351 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004352 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004353 lnum = curbuf->b_fnum;
4354 break;
4355 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004356 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004357 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004358#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004359 case ADDR_QUICKFIX:
4360 lnum = qf_get_cur_valid_idx(eap);
4361 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004362#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004363 }
4364 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365
4366 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004367 ++cmd;
4368 switch (addr_type)
4369 {
4370 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 lnum = curbuf->b_ml.ml_line_count;
4372 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004373 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004374 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004375 break;
4376 case ADDR_ARGUMENTS:
4377 lnum = ARGCOUNT;
4378 break;
4379 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004380 buf = lastbuf;
4381 while (buf->b_ml.ml_mfp == NULL)
4382 {
4383 if (buf->b_prev == NULL)
4384 break;
4385 buf = buf->b_prev;
4386 }
4387 lnum = buf->b_fnum;
4388 break;
4389 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004390 lnum = lastbuf->b_fnum;
4391 break;
4392 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004393 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004394 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004395#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004396 case ADDR_QUICKFIX:
4397 lnum = qf_get_size(eap);
4398 if (lnum == 0)
4399 lnum = 1;
4400 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004401#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004402 }
4403 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404
4405 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004406 if (*++cmd == NUL)
4407 {
4408 cmd = NULL;
4409 goto error;
4410 }
4411 if (addr_type != ADDR_LINES)
4412 {
4413 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004414 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004415 goto error;
4416 }
4417 if (skip)
4418 ++cmd;
4419 else
4420 {
4421 /* Only accept a mark in another file when it is
4422 * used by itself: ":'M". */
4423 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4424 ++cmd;
4425 if (fp == (pos_T *)-1)
4426 /* Jumped to another file. */
4427 lnum = curwin->w_cursor.lnum;
4428 else
4429 {
4430 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 {
4432 cmd = NULL;
4433 goto error;
4434 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004435 lnum = fp->lnum;
4436 }
4437 }
4438 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439
4440 case '/':
4441 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004442 c = *cmd++;
4443 if (addr_type != ADDR_LINES)
4444 {
4445 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004446 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004447 goto error;
4448 }
4449 if (skip) /* skip "/pat/" */
4450 {
4451 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4452 if (*cmd == c)
4453 ++cmd;
4454 }
4455 else
4456 {
4457 pos = curwin->w_cursor; /* save curwin->w_cursor */
4458 /*
4459 * When '/' or '?' follows another address, start
4460 * from there.
4461 */
4462 if (lnum != MAXLNUM)
4463 curwin->w_cursor.lnum = lnum;
4464 /*
4465 * Start a forward search at the end of the line.
4466 * Start a backward search at the start of the line.
4467 * This makes sure we never match in the current
4468 * line, and can match anywhere in the
4469 * next/previous line.
4470 */
4471 if (c == '/')
4472 curwin->w_cursor.col = MAXCOL;
4473 else
4474 curwin->w_cursor.col = 0;
4475 searchcmdlen = 0;
4476 if (!do_search(NULL, c, cmd, 1L,
4477 SEARCH_HIS | SEARCH_MSG, NULL))
4478 {
4479 curwin->w_cursor = pos;
4480 cmd = NULL;
4481 goto error;
4482 }
4483 lnum = curwin->w_cursor.lnum;
4484 curwin->w_cursor = pos;
4485 /* adjust command string pointer */
4486 cmd += searchcmdlen;
4487 }
4488 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489
4490 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004491 ++cmd;
4492 if (addr_type != ADDR_LINES)
4493 {
4494 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004495 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004496 goto error;
4497 }
4498 if (*cmd == '&')
4499 i = RE_SUBST;
4500 else if (*cmd == '?' || *cmd == '/')
4501 i = RE_SEARCH;
4502 else
4503 {
4504 EMSG(_(e_backslash));
4505 cmd = NULL;
4506 goto error;
4507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004509 if (!skip)
4510 {
4511 /*
4512 * When search follows another address, start from
4513 * there.
4514 */
4515 if (lnum != MAXLNUM)
4516 pos.lnum = lnum;
4517 else
4518 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004520 /*
4521 * Start the search just like for the above
4522 * do_search().
4523 */
4524 if (*cmd != '?')
4525 pos.col = MAXCOL;
4526 else
4527 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004528#ifdef FEAT_VIRTUALEDIT
4529 pos.coladd = 0;
4530#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004531 if (searchit(curwin, curbuf, &pos,
4532 *cmd == '?' ? BACKWARD : FORWARD,
4533 (char_u *)"", 1L, SEARCH_MSG,
4534 i, (linenr_T)0, NULL) != FAIL)
4535 lnum = pos.lnum;
4536 else
4537 {
4538 cmd = NULL;
4539 goto error;
4540 }
4541 }
4542 ++cmd;
4543 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544
4545 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004546 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4547 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 }
4549
4550 for (;;)
4551 {
4552 cmd = skipwhite(cmd);
4553 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4554 break;
4555
4556 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004557 {
4558 switch (addr_type)
4559 {
4560 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004561 /* "+1" is same as ".+1" */
4562 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004563 break;
4564 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004565 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004566 break;
4567 case ADDR_ARGUMENTS:
4568 lnum = curwin->w_arg_idx + 1;
4569 break;
4570 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004571 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004572 lnum = curbuf->b_fnum;
4573 break;
4574 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004575 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004576 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004577#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004578 case ADDR_QUICKFIX:
4579 lnum = qf_get_cur_valid_idx(eap);
4580 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004581#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004582 }
4583 }
4584
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 if (VIM_ISDIGIT(*cmd))
4586 i = '+'; /* "number" is same as "+number" */
4587 else
4588 i = *cmd++;
4589 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4590 n = 1;
4591 else
4592 n = getdigits(&cmd);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004593 if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004594 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004595 lnum = compute_buffer_local_count(
4596 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004597 else if (i == '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 lnum -= n;
4599 else
4600 lnum += n;
4601 }
4602 } while (*cmd == '/' || *cmd == '?');
4603
4604error:
4605 *ptr = cmd;
4606 return lnum;
4607}
4608
4609/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004610 * Get flags from an Ex command argument.
4611 */
4612 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004613get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004614{
4615 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4616 {
4617 if (*eap->arg == 'l')
4618 eap->flags |= EXFLAG_LIST;
4619 else if (*eap->arg == 'p')
4620 eap->flags |= EXFLAG_PRINT;
4621 else
4622 eap->flags |= EXFLAG_NR;
4623 eap->arg = skipwhite(eap->arg + 1);
4624 }
4625}
4626
4627/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 * Function called for command which is Not Implemented. NI!
4629 */
4630 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004631ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632{
4633 if (!eap->skip)
4634 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4635}
4636
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004637#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638/*
4639 * Function called for script command which is Not Implemented. NI!
4640 * Skips over ":perl <<EOF" constructs.
4641 */
4642 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004643ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644{
4645 if (!eap->skip)
4646 ex_ni(eap);
4647 else
4648 vim_free(script_get(eap, eap->arg));
4649}
4650#endif
4651
4652/*
4653 * Check range in Ex command for validity.
4654 * Return NULL when valid, error message when invalid.
4655 */
4656 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004657invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004659 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 if ( eap->line1 < 0
4661 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004662 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004664
4665 if (eap->argt & RANGE)
4666 {
4667 switch(eap->addr_type)
4668 {
4669 case ADDR_LINES:
4670 if (!(eap->argt & NOTADR)
4671 && eap->line2 > curbuf->b_ml.ml_line_count
4672#ifdef FEAT_DIFF
4673 + (eap->cmdidx == CMD_diffget)
4674#endif
4675 )
4676 return (char_u *)_(e_invrange);
4677 break;
4678 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004679 /* add 1 if ARGCOUNT is 0 */
4680 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004681 return (char_u *)_(e_invrange);
4682 break;
4683 case ADDR_BUFFERS:
4684 if (eap->line1 < firstbuf->b_fnum
4685 || eap->line2 > lastbuf->b_fnum)
4686 return (char_u *)_(e_invrange);
4687 break;
4688 case ADDR_LOADED_BUFFERS:
4689 buf = firstbuf;
4690 while (buf->b_ml.ml_mfp == NULL)
4691 {
4692 if (buf->b_next == NULL)
4693 return (char_u *)_(e_invrange);
4694 buf = buf->b_next;
4695 }
4696 if (eap->line1 < buf->b_fnum)
4697 return (char_u *)_(e_invrange);
4698 buf = lastbuf;
4699 while (buf->b_ml.ml_mfp == NULL)
4700 {
4701 if (buf->b_prev == NULL)
4702 return (char_u *)_(e_invrange);
4703 buf = buf->b_prev;
4704 }
4705 if (eap->line2 > buf->b_fnum)
4706 return (char_u *)_(e_invrange);
4707 break;
4708 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004709 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004710 return (char_u *)_(e_invrange);
4711 break;
4712 case ADDR_TABS:
4713 if (eap->line2 > LAST_TAB_NR)
4714 return (char_u *)_(e_invrange);
4715 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004716#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004717 case ADDR_QUICKFIX:
4718 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4719 return (char_u *)_(e_invrange);
4720 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004721#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004722 }
4723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 return NULL;
4725}
4726
4727/*
4728 * Correct the range for zero line number, if required.
4729 */
4730 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004731correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732{
4733 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4734 {
4735 if (eap->line1 == 0)
4736 eap->line1 = 1;
4737 if (eap->line2 == 0)
4738 eap->line2 = 1;
4739 }
4740}
4741
Bram Moolenaar748bf032005-02-02 23:04:36 +00004742#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004743static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004744
4745/*
4746 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4747 * pattern. Otherwise return eap->arg.
4748 */
4749 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004750skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004751{
4752 char_u *p = eap->arg;
4753
Bram Moolenaara37420f2006-02-04 22:37:47 +00004754 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4755 || eap->cmdidx == CMD_vimgrepadd
4756 || eap->cmdidx == CMD_lvimgrepadd
4757 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004758 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004759 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004760 if (p == NULL)
4761 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004762 }
4763 return p;
4764}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004765
4766/*
4767 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4768 * in the command line, so that things like % get expanded.
4769 */
4770 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004771replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004772{
4773 char_u *new_cmdline;
4774 char_u *program;
4775 char_u *pos;
4776 char_u *ptr;
4777 int len;
4778 int i;
4779
4780 /*
4781 * Don't do it when ":vimgrep" is used for ":grep".
4782 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004783 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4784 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4785 || eap->cmdidx == CMD_grepadd
4786 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004787 && !grep_internal(eap->cmdidx))
4788 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004789 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4790 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004791 {
4792 if (*curbuf->b_p_gp == NUL)
4793 program = p_gp;
4794 else
4795 program = curbuf->b_p_gp;
4796 }
4797 else
4798 {
4799 if (*curbuf->b_p_mp == NUL)
4800 program = p_mp;
4801 else
4802 program = curbuf->b_p_mp;
4803 }
4804
4805 p = skipwhite(p);
4806
4807 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4808 {
4809 /* replace $* by given arguments */
4810 i = 1;
4811 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4812 ++i;
4813 len = (int)STRLEN(p);
4814 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4815 if (new_cmdline == NULL)
4816 return NULL; /* out of memory */
4817 ptr = new_cmdline;
4818 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4819 {
4820 i = (int)(pos - program);
4821 STRNCPY(ptr, program, i);
4822 STRCPY(ptr += i, p);
4823 ptr += len;
4824 program = pos + 2;
4825 }
4826 STRCPY(ptr, program);
4827 }
4828 else
4829 {
4830 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4831 if (new_cmdline == NULL)
4832 return NULL; /* out of memory */
4833 STRCPY(new_cmdline, program);
4834 STRCAT(new_cmdline, " ");
4835 STRCAT(new_cmdline, p);
4836 }
4837 msg_make(p);
4838
4839 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4840 vim_free(*cmdlinep);
4841 *cmdlinep = new_cmdline;
4842 p = new_cmdline;
4843 }
4844 return p;
4845}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004846#endif
4847
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848/*
4849 * Expand file name in Ex command argument.
4850 * Return FAIL for failure, OK otherwise.
4851 */
4852 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004853expand_filename(
4854 exarg_T *eap,
4855 char_u **cmdlinep,
4856 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857{
4858 int has_wildcards; /* need to expand wildcards */
4859 char_u *repl;
4860 int srclen;
4861 char_u *p;
4862 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004863 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864
Bram Moolenaar748bf032005-02-02 23:04:36 +00004865#ifdef FEAT_QUICKFIX
4866 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4867 p = skip_grep_pat(eap);
4868#else
4869 p = eap->arg;
4870#endif
4871
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 /*
4873 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4874 * the file name contains a wildcard it should not cause expanding.
4875 * (it will be expanded anyway if there is a wildcard before replacing).
4876 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004877 has_wildcards = mch_has_wildcard(p);
4878 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004880#ifdef FEAT_EVAL
4881 /* Skip over `=expr`, wildcards in it are not expanded. */
4882 if (p[0] == '`' && p[1] == '=')
4883 {
4884 p += 2;
4885 (void)skip_expr(&p);
4886 if (*p == '`')
4887 ++p;
4888 continue;
4889 }
4890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 /*
4892 * Quick check if this cannot be the start of a special string.
4893 * Also removes backslash before '%', '#' and '<'.
4894 */
4895 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4896 {
4897 ++p;
4898 continue;
4899 }
4900
4901 /*
4902 * Try to find a match at this position.
4903 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004904 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4905 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 if (*errormsgp != NULL) /* error detected */
4907 return FAIL;
4908 if (repl == NULL) /* no match found */
4909 {
4910 p += srclen;
4911 continue;
4912 }
4913
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004914 /* Wildcards won't be expanded below, the replacement is taken
4915 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4916 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4917 {
4918 char_u *l = repl;
4919
4920 repl = expand_env_save(repl);
4921 vim_free(l);
4922 }
4923
Bram Moolenaar63b92542007-03-27 14:57:09 +00004924 /* Need to escape white space et al. with a backslash.
4925 * Don't do this for:
4926 * - replacement that already has been escaped: "##"
4927 * - shell commands (may have to use quotes instead).
4928 * - non-unix systems when there is a single argument (spaces don't
4929 * separate arguments then).
4930 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004932 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 && eap->cmdidx != CMD_bang
4934 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004935 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004937 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004939 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940#ifndef UNIX
4941 && !(eap->argt & NOSPC)
4942#endif
4943 )
4944 {
4945 char_u *l;
4946#ifdef BACKSLASH_IN_FILENAME
4947 /* Don't escape a backslash here, because rem_backslash() doesn't
4948 * remove it later. */
4949 static char_u *nobslash = (char_u *)" \t\"|";
4950# define ESCAPE_CHARS nobslash
4951#else
4952# define ESCAPE_CHARS escape_chars
4953#endif
4954
4955 for (l = repl; *l; ++l)
4956 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4957 {
4958 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4959 if (l != NULL)
4960 {
4961 vim_free(repl);
4962 repl = l;
4963 }
4964 break;
4965 }
4966 }
4967
4968 /* For a shell command a '!' must be escaped. */
4969 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004970 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 {
4972 char_u *l;
4973
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004974 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 if (l != NULL)
4976 {
4977 vim_free(repl);
4978 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 }
4980 }
4981
4982 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4983 vim_free(repl);
4984 if (p == NULL)
4985 return FAIL;
4986 }
4987
4988 /*
4989 * One file argument: Expand wildcards.
4990 * Don't do this with ":r !command" or ":w !command".
4991 */
4992 if ((eap->argt & NOSPC) && !eap->usefilter)
4993 {
4994 /*
4995 * May do this twice:
4996 * 1. Replace environment variables.
4997 * 2. Replace any other wildcards, remove backslashes.
4998 */
4999 for (n = 1; n <= 2; ++n)
5000 {
5001 if (n == 2)
5002 {
5003#ifdef UNIX
5004 /*
5005 * Only for Unix we check for more than one file name.
5006 * For other systems spaces are considered to be part
5007 * of the file name.
5008 * Only check here if there is no wildcard, otherwise
5009 * ExpandOne() will check for errors. This allows
5010 * ":e `ls ve*.c`" on Unix.
5011 */
5012 if (!has_wildcards)
5013 for (p = eap->arg; *p; ++p)
5014 {
5015 /* skip escaped characters */
5016 if (p[1] && (*p == '\\' || *p == Ctrl_V))
5017 ++p;
5018 else if (vim_iswhite(*p))
5019 {
5020 *errormsgp = (char_u *)_("E172: Only one file name allowed");
5021 return FAIL;
5022 }
5023 }
5024#endif
5025
5026 /*
5027 * Halve the number of backslashes (this is Vi compatible).
5028 * For Unix and OS/2, when wildcards are expanded, this is
5029 * done by ExpandOne() below.
5030 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005031#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 if (!has_wildcards)
5033#endif
5034 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 }
5036
5037 if (has_wildcards)
5038 {
5039 if (n == 1)
5040 {
5041 /*
5042 * First loop: May expand environment variables. This
5043 * can be done much faster with expand_env() than with
5044 * something else (e.g., calling a shell).
5045 * After expanding environment variables, check again
5046 * if there are still wildcards present.
5047 */
5048 if (vim_strchr(eap->arg, '$') != NULL
5049 || vim_strchr(eap->arg, '~') != NULL)
5050 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005051 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005052 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 has_wildcards = mch_has_wildcard(NameBuff);
5054 p = NameBuff;
5055 }
5056 else
5057 p = NULL;
5058 }
5059 else /* n == 2 */
5060 {
5061 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005062 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063
5064 ExpandInit(&xpc);
5065 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005066 if (p_wic)
5067 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005069 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 if (p == NULL)
5071 return FAIL;
5072 }
5073 if (p != NULL)
5074 {
5075 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5076 p, cmdlinep);
5077 if (n == 2) /* p came from ExpandOne() */
5078 vim_free(p);
5079 }
5080 }
5081 }
5082 }
5083 return OK;
5084}
5085
5086/*
5087 * Replace part of the command line, keeping eap->cmd, eap->arg and
5088 * eap->nextcmd correct.
5089 * "src" points to the part that is to be replaced, of length "srclen".
5090 * "repl" is the replacement string.
5091 * Returns a pointer to the character after the replaced string.
5092 * Returns NULL for failure.
5093 */
5094 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005095repl_cmdline(
5096 exarg_T *eap,
5097 char_u *src,
5098 int srclen,
5099 char_u *repl,
5100 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101{
5102 int len;
5103 int i;
5104 char_u *new_cmdline;
5105
5106 /*
5107 * The new command line is build in new_cmdline[].
5108 * First allocate it.
5109 * Careful: a "+cmd" argument may have been NUL terminated.
5110 */
5111 len = (int)STRLEN(repl);
5112 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005113 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5115 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5116 return NULL; /* out of memory! */
5117
5118 /*
5119 * Copy the stuff before the expanded part.
5120 * Copy the expanded stuff.
5121 * Copy what came after the expanded part.
5122 * Copy the next commands, if there are any.
5123 */
5124 i = (int)(src - *cmdlinep); /* length of part before match */
5125 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005126
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 mch_memmove(new_cmdline + i, repl, (size_t)len);
5128 i += len; /* remember the end of the string */
5129 STRCPY(new_cmdline + i, src + srclen);
5130 src = new_cmdline + i; /* remember where to continue */
5131
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005132 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 {
5134 i = (int)STRLEN(new_cmdline) + 1;
5135 STRCPY(new_cmdline + i, eap->nextcmd);
5136 eap->nextcmd = new_cmdline + i;
5137 }
5138 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5139 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5140 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5141 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5142 vim_free(*cmdlinep);
5143 *cmdlinep = new_cmdline;
5144
5145 return src;
5146}
5147
5148/*
5149 * Check for '|' to separate commands and '"' to start comments.
5150 */
5151 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005152separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153{
5154 char_u *p;
5155
Bram Moolenaar86b68352004-12-27 21:59:20 +00005156#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005157 p = skip_grep_pat(eap);
5158#else
5159 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005160#endif
5161
5162 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 {
5164 if (*p == Ctrl_V)
5165 {
5166 if (eap->argt & (USECTRLV | XFILE))
5167 ++p; /* skip CTRL-V and next char */
5168 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005169 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005170 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 if (*p == NUL) /* stop at NUL after CTRL-V */
5172 break;
5173 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005174
5175#ifdef FEAT_EVAL
5176 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005177 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005178 {
5179 p += 2;
5180 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005181 }
5182#endif
5183
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 /* Check for '"': start of comment or '|': next command */
5185 /* :@" and :*" do not start a comment!
5186 * :redir @" doesn't either. */
5187 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5188 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5189 || p != eap->arg)
5190 && (eap->cmdidx != CMD_redir
5191 || p != eap->arg + 1 || p[-1] != '@'))
5192 || *p == '|' || *p == '\n')
5193 {
5194 /*
5195 * We remove the '\' before the '|', unless USECTRLV is used
5196 * AND 'b' is present in 'cpoptions'.
5197 */
5198 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5199 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5200 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005201 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 --p;
5203 }
5204 else
5205 {
5206 eap->nextcmd = check_nextcmd(p);
5207 *p = NUL;
5208 break;
5209 }
5210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005212
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5214 del_trailing_spaces(eap->arg);
5215}
5216
5217/*
5218 * get + command from ex argument
5219 */
5220 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005221getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222{
5223 char_u *arg = *argp;
5224 char_u *command = NULL;
5225
5226 if (*arg == '+') /* +[command] */
5227 {
5228 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005229 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 command = dollar_command;
5231 else
5232 {
5233 command = arg;
5234 arg = skip_cmd_arg(command, TRUE);
5235 if (*arg != NUL)
5236 *arg++ = NUL; /* terminate command with NUL */
5237 }
5238
5239 arg = skipwhite(arg); /* skip over spaces */
5240 *argp = arg;
5241 }
5242 return command;
5243}
5244
5245/*
5246 * Find end of "+command" argument. Skip over "\ " and "\\".
5247 */
5248 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005249skip_cmd_arg(
5250 char_u *p,
5251 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252{
5253 while (*p && !vim_isspace(*p))
5254 {
5255 if (*p == '\\' && p[1] != NUL)
5256 {
5257 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005258 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 else
5260 ++p;
5261 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005262 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 }
5264 return p;
5265}
5266
5267/*
5268 * Get "++opt=arg" argument.
5269 * Return FAIL or OK.
5270 */
5271 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005272getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273{
5274 char_u *arg = eap->arg + 2;
5275 int *pp = NULL;
5276#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005277 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 char_u *p;
5279#endif
5280
5281 /* ":edit ++[no]bin[ary] file" */
5282 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5283 {
5284 if (*arg == 'n')
5285 {
5286 arg += 2;
5287 eap->force_bin = FORCE_NOBIN;
5288 }
5289 else
5290 eap->force_bin = FORCE_BIN;
5291 if (!checkforcmd(&arg, "binary", 3))
5292 return FAIL;
5293 eap->arg = skipwhite(arg);
5294 return OK;
5295 }
5296
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005297 /* ":read ++edit file" */
5298 if (STRNCMP(arg, "edit", 4) == 0)
5299 {
5300 eap->read_edit = TRUE;
5301 eap->arg = skipwhite(arg + 4);
5302 return OK;
5303 }
5304
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 if (STRNCMP(arg, "ff", 2) == 0)
5306 {
5307 arg += 2;
5308 pp = &eap->force_ff;
5309 }
5310 else if (STRNCMP(arg, "fileformat", 10) == 0)
5311 {
5312 arg += 10;
5313 pp = &eap->force_ff;
5314 }
5315#ifdef FEAT_MBYTE
5316 else if (STRNCMP(arg, "enc", 3) == 0)
5317 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005318 if (STRNCMP(arg, "encoding", 8) == 0)
5319 arg += 8;
5320 else
5321 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 pp = &eap->force_enc;
5323 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005324 else if (STRNCMP(arg, "bad", 3) == 0)
5325 {
5326 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005327 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329#endif
5330
5331 if (pp == NULL || *arg != '=')
5332 return FAIL;
5333
5334 ++arg;
5335 *pp = (int)(arg - eap->cmd);
5336 arg = skip_cmd_arg(arg, FALSE);
5337 eap->arg = skipwhite(arg);
5338 *arg = NUL;
5339
5340#ifdef FEAT_MBYTE
5341 if (pp == &eap->force_ff)
5342 {
5343#endif
5344 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5345 return FAIL;
5346#ifdef FEAT_MBYTE
5347 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005348 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 {
5350 /* Make 'fileencoding' lower case. */
5351 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5352 *p = TOLOWER_ASC(*p);
5353 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005354 else
5355 {
5356 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5357 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005358 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005359 if (STRICMP(p, "keep") == 0)
5360 eap->bad_char = BAD_KEEP;
5361 else if (STRICMP(p, "drop") == 0)
5362 eap->bad_char = BAD_DROP;
5363 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5364 eap->bad_char = *p;
5365 else
5366 return FAIL;
5367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368#endif
5369
5370 return OK;
5371}
5372
5373/*
5374 * ":abbreviate" and friends.
5375 */
5376 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005377ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378{
5379 do_exmap(eap, TRUE); /* almost the same as mapping */
5380}
5381
5382/*
5383 * ":map" and friends.
5384 */
5385 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005386ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387{
5388 /*
5389 * If we are sourcing .exrc or .vimrc in current directory we
5390 * print the mappings for security reasons.
5391 */
5392 if (secure)
5393 {
5394 secure = 2;
5395 msg_outtrans(eap->cmd);
5396 msg_putchar('\n');
5397 }
5398 do_exmap(eap, FALSE);
5399}
5400
5401/*
5402 * ":unmap" and friends.
5403 */
5404 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005405ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406{
5407 do_exmap(eap, FALSE);
5408}
5409
5410/*
5411 * ":mapclear" and friends.
5412 */
5413 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005414ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415{
5416 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5417}
5418
5419/*
5420 * ":abclear" and friends.
5421 */
5422 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005423ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424{
5425 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5426}
5427
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005428#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005430ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431{
5432 /*
5433 * Disallow auto commands from .exrc and .vimrc in current
5434 * directory for security reasons.
5435 */
5436 if (secure)
5437 {
5438 secure = 2;
5439 eap->errmsg = e_curdir;
5440 }
5441 else if (eap->cmdidx == CMD_autocmd)
5442 do_autocmd(eap->arg, eap->forceit);
5443 else
5444 do_augroup(eap->arg, eap->forceit);
5445}
5446
5447/*
5448 * ":doautocmd": Apply the automatic commands to the current buffer.
5449 */
5450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005451ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005453 char_u *arg = eap->arg;
5454 int call_do_modelines = check_nomodeline(&arg);
5455
5456 (void)do_doautocmd(arg, TRUE);
5457 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5458 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459}
5460#endif
5461
5462#ifdef FEAT_LISTCMDS
5463/*
5464 * :[N]bunload[!] [N] [bufname] unload buffer
5465 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5466 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5467 */
5468 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005469ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470{
5471 eap->errmsg = do_bufdel(
5472 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5473 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5474 : DOBUF_UNLOAD, eap->arg,
5475 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5476}
5477
5478/*
5479 * :[N]buffer [N] to buffer N
5480 * :[N]sbuffer [N] to buffer N
5481 */
5482 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005483ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484{
5485 if (*eap->arg)
5486 eap->errmsg = e_trailing;
5487 else
5488 {
5489 if (eap->addr_count == 0) /* default is current buffer */
5490 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5491 else
5492 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005493 if (eap->do_ecmd_cmd != NULL)
5494 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 }
5496}
5497
5498/*
5499 * :[N]bmodified [N] to next mod. buffer
5500 * :[N]sbmodified [N] to next mod. buffer
5501 */
5502 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005503ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504{
5505 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005506 if (eap->do_ecmd_cmd != NULL)
5507 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508}
5509
5510/*
5511 * :[N]bnext [N] to next buffer
5512 * :[N]sbnext [N] split and to next buffer
5513 */
5514 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005515ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516{
5517 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005518 if (eap->do_ecmd_cmd != NULL)
5519 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520}
5521
5522/*
5523 * :[N]bNext [N] to previous buffer
5524 * :[N]bprevious [N] to previous buffer
5525 * :[N]sbNext [N] split and to previous buffer
5526 * :[N]sbprevious [N] split and to previous buffer
5527 */
5528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005529ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530{
5531 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005532 if (eap->do_ecmd_cmd != NULL)
5533 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534}
5535
5536/*
5537 * :brewind to first buffer
5538 * :bfirst to first buffer
5539 * :sbrewind split and to first buffer
5540 * :sbfirst split and to first buffer
5541 */
5542 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005543ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544{
5545 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005546 if (eap->do_ecmd_cmd != NULL)
5547 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548}
5549
5550/*
5551 * :blast to last buffer
5552 * :sblast split and to last buffer
5553 */
5554 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005555ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556{
5557 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005558 if (eap->do_ecmd_cmd != NULL)
5559 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560}
5561#endif
5562
5563 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005564ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565{
5566 return (c == NUL || c == '|' || c == '"' || c == '\n');
5567}
5568
5569#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5570 || defined(PROTO)
5571/*
5572 * Return the next command, after the first '|' or '\n'.
5573 * Return NULL if not found.
5574 */
5575 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005576find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577{
5578 while (*p != '|' && *p != '\n')
5579 {
5580 if (*p == NUL)
5581 return NULL;
5582 ++p;
5583 }
5584 return (p + 1);
5585}
5586#endif
5587
5588/*
5589 * Check if *p is a separator between Ex commands.
5590 * Return NULL if it isn't, (p + 1) if it is.
5591 */
5592 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005593check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594{
5595 p = skipwhite(p);
5596 if (*p == '|' || *p == '\n')
5597 return (p + 1);
5598 else
5599 return NULL;
5600}
5601
5602/*
5603 * - if there are more files to edit
5604 * - and this is the last window
5605 * - and forceit not used
5606 * - and not repeated twice on a row
5607 * return FAIL and give error message if 'message' TRUE
5608 * return OK otherwise
5609 */
5610 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005611check_more(
5612 int message, /* when FALSE check only, no messages */
5613 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614{
5615 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5616
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005617 if (!forceit && only_one_window()
5618 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 {
5620 if (message)
5621 {
5622#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5623 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5624 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005625 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626
5627 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005628 vim_strncpy(buff,
5629 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005630 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005632 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 _("%d more files to edit. Quit anyway?"), n);
5634 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5635 return OK;
5636 return FAIL;
5637 }
5638#endif
5639 if (n == 1)
5640 EMSG(_("E173: 1 more file to edit"));
5641 else
5642 EMSGN(_("E173: %ld more files to edit"), n);
5643 quitmore = 2; /* next try to quit is allowed */
5644 }
5645 return FAIL;
5646 }
5647 return OK;
5648}
5649
5650#ifdef FEAT_CMDL_COMPL
5651/*
5652 * Function given to ExpandGeneric() to obtain the list of command names.
5653 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005655get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656{
5657 if (idx >= (int)CMD_SIZE)
5658# ifdef FEAT_USR_CMDS
5659 return get_user_command_name(idx);
5660# else
5661 return NULL;
5662# endif
5663 return cmdnames[idx].cmd_name;
5664}
5665#endif
5666
5667#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005668static int uc_add_command(char_u *name, size_t name_len, char_u *rep, long argt, long def, int flags, int compl, char_u *compl_arg, int addr_type, int force);
5669static void uc_list(char_u *name, size_t name_len);
5670static int uc_scan_attr(char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg, int* attr_type_arg);
5671static char_u *uc_split_args(char_u *arg, size_t *lenp);
5672static size_t uc_check_code(char_u *code, size_t len, char_u *buf, ucmd_T *cmd, exarg_T *eap, char_u **split_buf, size_t *split_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673
5674 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005675uc_add_command(
5676 char_u *name,
5677 size_t name_len,
5678 char_u *rep,
5679 long argt,
5680 long def,
5681 int flags,
5682 int compl,
5683 char_u *compl_arg,
5684 int addr_type,
5685 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686{
5687 ucmd_T *cmd = NULL;
5688 char_u *p;
5689 int i;
5690 int cmp = 1;
5691 char_u *rep_buf = NULL;
5692 garray_T *gap;
5693
Bram Moolenaar9c102382006-05-03 21:26:49 +00005694 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 if (rep_buf == NULL)
5696 {
5697 /* Can't replace termcodes - try using the string as is */
5698 rep_buf = vim_strsave(rep);
5699
5700 /* Give up if out of memory */
5701 if (rep_buf == NULL)
5702 return FAIL;
5703 }
5704
5705 /* get address of growarray: global or in curbuf */
5706 if (flags & UC_BUFFER)
5707 {
5708 gap = &curbuf->b_ucmds;
5709 if (gap->ga_itemsize == 0)
5710 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5711 }
5712 else
5713 gap = &ucmds;
5714
5715 /* Search for the command in the already defined commands. */
5716 for (i = 0; i < gap->ga_len; ++i)
5717 {
5718 size_t len;
5719
5720 cmd = USER_CMD_GA(gap, i);
5721 len = STRLEN(cmd->uc_name);
5722 cmp = STRNCMP(name, cmd->uc_name, name_len);
5723 if (cmp == 0)
5724 {
5725 if (name_len < len)
5726 cmp = -1;
5727 else if (name_len > len)
5728 cmp = 1;
5729 }
5730
5731 if (cmp == 0)
5732 {
5733 if (!force)
5734 {
5735 EMSG(_("E174: Command already exists: add ! to replace it"));
5736 goto fail;
5737 }
5738
5739 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005740 cmd->uc_rep = NULL;
5741#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5742 vim_free(cmd->uc_compl_arg);
5743 cmd->uc_compl_arg = NULL;
5744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 break;
5746 }
5747
5748 /* Stop as soon as we pass the name to add */
5749 if (cmp < 0)
5750 break;
5751 }
5752
5753 /* Extend the array unless we're replacing an existing command */
5754 if (cmp != 0)
5755 {
5756 if (ga_grow(gap, 1) != OK)
5757 goto fail;
5758 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5759 goto fail;
5760
5761 cmd = USER_CMD_GA(gap, i);
5762 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5763
5764 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765
5766 cmd->uc_name = p;
5767 }
5768
5769 cmd->uc_rep = rep_buf;
5770 cmd->uc_argt = argt;
5771 cmd->uc_def = def;
5772 cmd->uc_compl = compl;
5773#ifdef FEAT_EVAL
5774 cmd->uc_scriptID = current_SID;
5775# ifdef FEAT_CMDL_COMPL
5776 cmd->uc_compl_arg = compl_arg;
5777# endif
5778#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005779 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780
5781 return OK;
5782
5783fail:
5784 vim_free(rep_buf);
5785#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5786 vim_free(compl_arg);
5787#endif
5788 return FAIL;
5789}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005792#if defined(FEAT_USR_CMDS)
5793static struct
5794{
5795 int expand;
5796 char *name;
5797} addr_type_complete[] =
5798{
5799 {ADDR_ARGUMENTS, "arguments"},
5800 {ADDR_LINES, "lines"},
5801 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5802 {ADDR_TABS, "tabs"},
5803 {ADDR_BUFFERS, "buffers"},
5804 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005805 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005806 {-1, NULL}
5807};
5808#endif
5809
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005810#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811/*
5812 * List of names for completion for ":command" with the EXPAND_ flag.
5813 * Must be alphabetical for completion.
5814 */
5815static struct
5816{
5817 int expand;
5818 char *name;
5819} command_complete[] =
5820{
5821 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005822 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005824 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005826 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005827#if defined(FEAT_CSCOPE)
5828 {EXPAND_CSCOPE, "cscope"},
5829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5831 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005832 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833#endif
5834 {EXPAND_DIRECTORIES, "dir"},
5835 {EXPAND_ENV_VARS, "environment"},
5836 {EXPAND_EVENTS, "event"},
5837 {EXPAND_EXPRESSION, "expression"},
5838 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005839 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005840 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 {EXPAND_FUNCTIONS, "function"},
5842 {EXPAND_HELP, "help"},
5843 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005844#if defined(FEAT_CMDHIST)
5845 {EXPAND_HISTORY, "history"},
5846#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005847#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005848 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005849 {EXPAND_LOCALES, "locale"},
5850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 {EXPAND_MAPPINGS, "mapping"},
5852 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005853 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005854#if defined(FEAT_PROFILE)
5855 {EXPAND_SYNTIME, "syntime"},
5856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005858 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005859#if defined(FEAT_SIGNS)
5860 {EXPAND_SIGN, "sign"},
5861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 {EXPAND_TAGS, "tag"},
5863 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005864 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 {EXPAND_USER_VARS, "var"},
5866 {0, NULL}
5867};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005870#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005872uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873{
5874 int i, j;
5875 int found = FALSE;
5876 ucmd_T *cmd;
5877 int len;
5878 long a;
5879 garray_T *gap;
5880
5881 gap = &curbuf->b_ucmds;
5882 for (;;)
5883 {
5884 for (i = 0; i < gap->ga_len; ++i)
5885 {
5886 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005887 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888
5889 /* Skip commands which don't match the requested prefix */
5890 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5891 continue;
5892
5893 /* Put out the title first time */
5894 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005895 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 found = TRUE;
5897 msg_putchar('\n');
5898 if (got_int)
5899 break;
5900
5901 /* Special cases */
5902 msg_putchar(a & BANG ? '!' : ' ');
5903 msg_putchar(a & REGSTR ? '"' : ' ');
5904 msg_putchar(gap != &ucmds ? 'b' : ' ');
5905 msg_putchar(' ');
5906
5907 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5908 len = (int)STRLEN(cmd->uc_name) + 4;
5909
5910 do {
5911 msg_putchar(' ');
5912 ++len;
5913 } while (len < 16);
5914
5915 len = 0;
5916
5917 /* Arguments */
5918 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5919 {
5920 case 0: IObuff[len++] = '0'; break;
5921 case (EXTRA): IObuff[len++] = '*'; break;
5922 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5923 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5924 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5925 }
5926
5927 do {
5928 IObuff[len++] = ' ';
5929 } while (len < 5);
5930
5931 /* Range */
5932 if (a & (RANGE|COUNT))
5933 {
5934 if (a & COUNT)
5935 {
5936 /* -count=N */
5937 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5938 len += (int)STRLEN(IObuff + len);
5939 }
5940 else if (a & DFLALL)
5941 IObuff[len++] = '%';
5942 else if (cmd->uc_def >= 0)
5943 {
5944 /* -range=N */
5945 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5946 len += (int)STRLEN(IObuff + len);
5947 }
5948 else
5949 IObuff[len++] = '.';
5950 }
5951
5952 do {
5953 IObuff[len++] = ' ';
5954 } while (len < 11);
5955
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005956 /* Address Type */
5957 for (j = 0; addr_type_complete[j].expand != -1; ++j)
5958 if (addr_type_complete[j].expand != ADDR_LINES
5959 && addr_type_complete[j].expand == cmd->uc_addr_type)
5960 {
5961 STRCPY(IObuff + len, addr_type_complete[j].name);
5962 len += (int)STRLEN(IObuff + len);
5963 break;
5964 }
5965
5966 do {
5967 IObuff[len++] = ' ';
5968 } while (len < 21);
5969
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 /* Completion */
5971 for (j = 0; command_complete[j].expand != 0; ++j)
5972 if (command_complete[j].expand == cmd->uc_compl)
5973 {
5974 STRCPY(IObuff + len, command_complete[j].name);
5975 len += (int)STRLEN(IObuff + len);
5976 break;
5977 }
5978
5979 do {
5980 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005981 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982
5983 IObuff[len] = '\0';
5984 msg_outtrans(IObuff);
5985
5986 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005987#ifdef FEAT_EVAL
5988 if (p_verbose > 0)
5989 last_set_msg(cmd->uc_scriptID);
5990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 out_flush();
5992 ui_breakcheck();
5993 if (got_int)
5994 break;
5995 }
5996 if (gap == &ucmds || i < gap->ga_len)
5997 break;
5998 gap = &ucmds;
5999 }
6000
6001 if (!found)
6002 MSG(_("No user-defined commands found"));
6003}
6004
6005 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006006uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007{
6008 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6009 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6010 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6011 0xb9, 0x7f, 0};
6012 int i;
6013
6014 for (i = 0; fcmd[i]; ++i)
6015 IObuff[i] = fcmd[i] - 0x40;
6016 IObuff[i] = 0;
6017 return IObuff;
6018}
6019
6020 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006021uc_scan_attr(
6022 char_u *attr,
6023 size_t len,
6024 long *argt,
6025 long *def,
6026 int *flags,
6027 int *compl,
6028 char_u **compl_arg,
6029 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030{
6031 char_u *p;
6032
6033 if (len == 0)
6034 {
6035 EMSG(_("E175: No attribute specified"));
6036 return FAIL;
6037 }
6038
6039 /* First, try the simple attributes (no arguments) */
6040 if (STRNICMP(attr, "bang", len) == 0)
6041 *argt |= BANG;
6042 else if (STRNICMP(attr, "buffer", len) == 0)
6043 *flags |= UC_BUFFER;
6044 else if (STRNICMP(attr, "register", len) == 0)
6045 *argt |= REGSTR;
6046 else if (STRNICMP(attr, "bar", len) == 0)
6047 *argt |= TRLBAR;
6048 else
6049 {
6050 int i;
6051 char_u *val = NULL;
6052 size_t vallen = 0;
6053 size_t attrlen = len;
6054
6055 /* Look for the attribute name - which is the part before any '=' */
6056 for (i = 0; i < (int)len; ++i)
6057 {
6058 if (attr[i] == '=')
6059 {
6060 val = &attr[i + 1];
6061 vallen = len - i - 1;
6062 attrlen = i;
6063 break;
6064 }
6065 }
6066
6067 if (STRNICMP(attr, "nargs", attrlen) == 0)
6068 {
6069 if (vallen == 1)
6070 {
6071 if (*val == '0')
6072 /* Do nothing - this is the default */;
6073 else if (*val == '1')
6074 *argt |= (EXTRA | NOSPC | NEEDARG);
6075 else if (*val == '*')
6076 *argt |= EXTRA;
6077 else if (*val == '?')
6078 *argt |= (EXTRA | NOSPC);
6079 else if (*val == '+')
6080 *argt |= (EXTRA | NEEDARG);
6081 else
6082 goto wrong_nargs;
6083 }
6084 else
6085 {
6086wrong_nargs:
6087 EMSG(_("E176: Invalid number of arguments"));
6088 return FAIL;
6089 }
6090 }
6091 else if (STRNICMP(attr, "range", attrlen) == 0)
6092 {
6093 *argt |= RANGE;
6094 if (vallen == 1 && *val == '%')
6095 *argt |= DFLALL;
6096 else if (val != NULL)
6097 {
6098 p = val;
6099 if (*def >= 0)
6100 {
6101two_count:
6102 EMSG(_("E177: Count cannot be specified twice"));
6103 return FAIL;
6104 }
6105
6106 *def = getdigits(&p);
6107 *argt |= (ZEROR | NOTADR);
6108
6109 if (p != val + vallen || vallen == 0)
6110 {
6111invalid_count:
6112 EMSG(_("E178: Invalid default value for count"));
6113 return FAIL;
6114 }
6115 }
6116 }
6117 else if (STRNICMP(attr, "count", attrlen) == 0)
6118 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006119 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120
6121 if (val != NULL)
6122 {
6123 p = val;
6124 if (*def >= 0)
6125 goto two_count;
6126
6127 *def = getdigits(&p);
6128
6129 if (p != val + vallen)
6130 goto invalid_count;
6131 }
6132
6133 if (*def < 0)
6134 *def = 0;
6135 }
6136 else if (STRNICMP(attr, "complete", attrlen) == 0)
6137 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 if (val == NULL)
6139 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006140 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 return FAIL;
6142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006144 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6145 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006148 else if (STRNICMP(attr, "addr", attrlen) == 0)
6149 {
6150 *argt |= RANGE;
6151 if (val == NULL)
6152 {
6153 EMSG(_("E179: argument required for -addr"));
6154 return FAIL;
6155 }
6156 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6157 == FAIL)
6158 return FAIL;
6159 if (addr_type_arg != ADDR_LINES)
6160 *argt |= (ZEROR | NOTADR) ;
6161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 else
6163 {
6164 char_u ch = attr[len];
6165 attr[len] = '\0';
6166 EMSG2(_("E181: Invalid attribute: %s"), attr);
6167 attr[len] = ch;
6168 return FAIL;
6169 }
6170 }
6171
6172 return OK;
6173}
6174
Bram Moolenaara850a712009-01-28 14:42:59 +00006175/*
6176 * ":command ..."
6177 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006179ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180{
6181 char_u *name;
6182 char_u *end;
6183 char_u *p;
6184 long argt = 0;
6185 long def = -1;
6186 int flags = 0;
6187 int compl = EXPAND_NOTHING;
6188 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006189 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006191 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192
6193 p = eap->arg;
6194
6195 /* Check for attributes */
6196 while (*p == '-')
6197 {
6198 ++p;
6199 end = skiptowhite(p);
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006200 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 == FAIL)
6202 return;
6203 p = skipwhite(end);
6204 }
6205
6206 /* Get the name (if any) and skip to the following argument */
6207 name = p;
6208 if (ASCII_ISALPHA(*p))
6209 while (ASCII_ISALNUM(*p))
6210 ++p;
6211 if (!ends_excmd(*p) && !vim_iswhite(*p))
6212 {
6213 EMSG(_("E182: Invalid command name"));
6214 return;
6215 }
6216 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006217 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218
6219 /* If there is nothing after the name, and no attributes were specified,
6220 * we are listing commands
6221 */
6222 p = skipwhite(end);
6223 if (!has_attr && ends_excmd(*p))
6224 {
6225 uc_list(name, end - name);
6226 }
6227 else if (!ASCII_ISUPPER(*name))
6228 {
6229 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6230 return;
6231 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006232 else if ((name_len == 1 && *name == 'X')
6233 || (name_len <= 4
6234 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6235 {
6236 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6237 return;
6238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 else
6240 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006241 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242}
6243
6244/*
6245 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 * Clear all user commands, global and for current buffer.
6247 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006248 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006249ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250{
6251 uc_clear(&ucmds);
6252 uc_clear(&curbuf->b_ucmds);
6253}
6254
6255/*
6256 * Clear all user commands for "gap".
6257 */
6258 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006259uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260{
6261 int i;
6262 ucmd_T *cmd;
6263
6264 for (i = 0; i < gap->ga_len; ++i)
6265 {
6266 cmd = USER_CMD_GA(gap, i);
6267 vim_free(cmd->uc_name);
6268 vim_free(cmd->uc_rep);
6269# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6270 vim_free(cmd->uc_compl_arg);
6271# endif
6272 }
6273 ga_clear(gap);
6274}
6275
6276 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006277ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278{
6279 int i = 0;
6280 ucmd_T *cmd = NULL;
6281 int cmp = -1;
6282 garray_T *gap;
6283
6284 gap = &curbuf->b_ucmds;
6285 for (;;)
6286 {
6287 for (i = 0; i < gap->ga_len; ++i)
6288 {
6289 cmd = USER_CMD_GA(gap, i);
6290 cmp = STRCMP(eap->arg, cmd->uc_name);
6291 if (cmp <= 0)
6292 break;
6293 }
6294 if (gap == &ucmds || cmp == 0)
6295 break;
6296 gap = &ucmds;
6297 }
6298
6299 if (cmp != 0)
6300 {
6301 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6302 return;
6303 }
6304
6305 vim_free(cmd->uc_name);
6306 vim_free(cmd->uc_rep);
6307# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6308 vim_free(cmd->uc_compl_arg);
6309# endif
6310
6311 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312
6313 if (i < gap->ga_len)
6314 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6315}
6316
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006317/*
6318 * split and quote args for <f-args>
6319 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006321uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322{
6323 char_u *buf;
6324 char_u *p;
6325 char_u *q;
6326 int len;
6327
6328 /* Precalculate length */
6329 p = arg;
6330 len = 2; /* Initial and final quotes */
6331
6332 while (*p)
6333 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006334 if (p[0] == '\\' && p[1] == '\\')
6335 {
6336 len += 2;
6337 p += 2;
6338 }
6339 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 {
6341 len += 1;
6342 p += 2;
6343 }
6344 else if (*p == '\\' || *p == '"')
6345 {
6346 len += 2;
6347 p += 1;
6348 }
6349 else if (vim_iswhite(*p))
6350 {
6351 p = skipwhite(p);
6352 if (*p == NUL)
6353 break;
6354 len += 3; /* "," */
6355 }
6356 else
6357 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006358#ifdef FEAT_MBYTE
6359 int charlen = (*mb_ptr2len)(p);
6360 len += charlen;
6361 p += charlen;
6362#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 ++len;
6364 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 }
6367 }
6368
6369 buf = alloc(len + 1);
6370 if (buf == NULL)
6371 {
6372 *lenp = 0;
6373 return buf;
6374 }
6375
6376 p = arg;
6377 q = buf;
6378 *q++ = '"';
6379 while (*p)
6380 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006381 if (p[0] == '\\' && p[1] == '\\')
6382 {
6383 *q++ = '\\';
6384 *q++ = '\\';
6385 p += 2;
6386 }
6387 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 {
6389 *q++ = p[1];
6390 p += 2;
6391 }
6392 else if (*p == '\\' || *p == '"')
6393 {
6394 *q++ = '\\';
6395 *q++ = *p++;
6396 }
6397 else if (vim_iswhite(*p))
6398 {
6399 p = skipwhite(p);
6400 if (*p == NUL)
6401 break;
6402 *q++ = '"';
6403 *q++ = ',';
6404 *q++ = '"';
6405 }
6406 else
6407 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006408 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 }
6410 }
6411 *q++ = '"';
6412 *q = 0;
6413
6414 *lenp = len;
6415 return buf;
6416}
6417
6418/*
6419 * Check for a <> code in a user command.
6420 * "code" points to the '<'. "len" the length of the <> (inclusive).
6421 * "buf" is where the result is to be added.
6422 * "split_buf" points to a buffer used for splitting, caller should free it.
6423 * "split_len" is the length of what "split_buf" contains.
6424 * Returns the length of the replacement, which has been added to "buf".
6425 * Returns -1 if there was no match, and only the "<" has been copied.
6426 */
6427 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006428uc_check_code(
6429 char_u *code,
6430 size_t len,
6431 char_u *buf,
6432 ucmd_T *cmd, /* the user command we're expanding */
6433 exarg_T *eap, /* ex arguments */
6434 char_u **split_buf,
6435 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436{
6437 size_t result = 0;
6438 char_u *p = code + 1;
6439 size_t l = len - 2;
6440 int quote = 0;
6441 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
6442 ct_LT, ct_NONE } type = ct_NONE;
6443
6444 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6445 {
6446 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6447 p += 2;
6448 l -= 2;
6449 }
6450
Bram Moolenaar371d5402006-03-20 21:47:49 +00006451 ++l;
6452 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006454 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006456 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006458 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006460 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006462 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006464 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006466 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 type = ct_REGISTER;
6468
6469 switch (type)
6470 {
6471 case ct_ARGS:
6472 /* Simple case first */
6473 if (*eap->arg == NUL)
6474 {
6475 if (quote == 1)
6476 {
6477 result = 2;
6478 if (buf != NULL)
6479 STRCPY(buf, "''");
6480 }
6481 else
6482 result = 0;
6483 break;
6484 }
6485
6486 /* When specified there is a single argument don't split it.
6487 * Works for ":Cmd %" when % is "a b c". */
6488 if ((eap->argt & NOSPC) && quote == 2)
6489 quote = 1;
6490
6491 switch (quote)
6492 {
6493 case 0: /* No quoting, no splitting */
6494 result = STRLEN(eap->arg);
6495 if (buf != NULL)
6496 STRCPY(buf, eap->arg);
6497 break;
6498 case 1: /* Quote, but don't split */
6499 result = STRLEN(eap->arg) + 2;
6500 for (p = eap->arg; *p; ++p)
6501 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006502#ifdef FEAT_MBYTE
6503 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6504 /* DBCS can contain \ in a trail byte, skip the
6505 * double-byte character. */
6506 ++p;
6507 else
6508#endif
6509 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 ++result;
6511 }
6512
6513 if (buf != NULL)
6514 {
6515 *buf++ = '"';
6516 for (p = eap->arg; *p; ++p)
6517 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006518#ifdef FEAT_MBYTE
6519 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6520 /* DBCS can contain \ in a trail byte, copy the
6521 * double-byte character to avoid escaping. */
6522 *buf++ = *p++;
6523 else
6524#endif
6525 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 *buf++ = '\\';
6527 *buf++ = *p;
6528 }
6529 *buf = '"';
6530 }
6531
6532 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006533 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 /* This is hard, so only do it once, and cache the result */
6535 if (*split_buf == NULL)
6536 *split_buf = uc_split_args(eap->arg, split_len);
6537
6538 result = *split_len;
6539 if (buf != NULL && result != 0)
6540 STRCPY(buf, *split_buf);
6541
6542 break;
6543 }
6544 break;
6545
6546 case ct_BANG:
6547 result = eap->forceit ? 1 : 0;
6548 if (quote)
6549 result += 2;
6550 if (buf != NULL)
6551 {
6552 if (quote)
6553 *buf++ = '"';
6554 if (eap->forceit)
6555 *buf++ = '!';
6556 if (quote)
6557 *buf = '"';
6558 }
6559 break;
6560
6561 case ct_LINE1:
6562 case ct_LINE2:
6563 case ct_COUNT:
6564 {
6565 char num_buf[20];
6566 long num = (type == ct_LINE1) ? eap->line1 :
6567 (type == ct_LINE2) ? eap->line2 :
6568 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6569 size_t num_len;
6570
6571 sprintf(num_buf, "%ld", num);
6572 num_len = STRLEN(num_buf);
6573 result = num_len;
6574
6575 if (quote)
6576 result += 2;
6577
6578 if (buf != NULL)
6579 {
6580 if (quote)
6581 *buf++ = '"';
6582 STRCPY(buf, num_buf);
6583 buf += num_len;
6584 if (quote)
6585 *buf = '"';
6586 }
6587
6588 break;
6589 }
6590
6591 case ct_REGISTER:
6592 result = eap->regname ? 1 : 0;
6593 if (quote)
6594 result += 2;
6595 if (buf != NULL)
6596 {
6597 if (quote)
6598 *buf++ = '\'';
6599 if (eap->regname)
6600 *buf++ = eap->regname;
6601 if (quote)
6602 *buf = '\'';
6603 }
6604 break;
6605
6606 case ct_LT:
6607 result = 1;
6608 if (buf != NULL)
6609 *buf = '<';
6610 break;
6611
6612 default:
6613 /* Not recognized: just copy the '<' and return -1. */
6614 result = (size_t)-1;
6615 if (buf != NULL)
6616 *buf = '<';
6617 break;
6618 }
6619
6620 return result;
6621}
6622
6623 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006624do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625{
6626 char_u *buf;
6627 char_u *p;
6628 char_u *q;
6629
6630 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006631 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006632 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 size_t len, totlen;
6634
6635 size_t split_len = 0;
6636 char_u *split_buf = NULL;
6637 ucmd_T *cmd;
6638#ifdef FEAT_EVAL
6639 scid_T save_current_SID = current_SID;
6640#endif
6641
6642 if (eap->cmdidx == CMD_USER)
6643 cmd = USER_CMD(eap->useridx);
6644 else
6645 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6646
6647 /*
6648 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006649 * First round: "buf" is NULL, compute length, allocate "buf".
6650 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 */
6652 buf = NULL;
6653 for (;;)
6654 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006655 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006656 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006658
6659 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006661 start = vim_strchr(p, '<');
6662 if (start != NULL)
6663 end = vim_strchr(start + 1, '>');
6664 if (buf != NULL)
6665 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006666 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6667 ;
6668 if (*ksp == K_SPECIAL
6669 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006670 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6671# ifdef FEAT_GUI
6672 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6673# endif
6674 ))
6675 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006676 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006677 * KS_SPECIAL KE_FILLER, like for mappings, but
6678 * do_cmdline() doesn't handle that, so convert it back.
6679 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6680 len = ksp - p;
6681 if (len > 0)
6682 {
6683 mch_memmove(q, p, len);
6684 q += len;
6685 }
6686 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6687 p = ksp + 3;
6688 continue;
6689 }
6690 }
6691
6692 /* break if there no <item> is found */
6693 if (start == NULL || end == NULL)
6694 break;
6695
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 /* Include the '>' */
6697 ++end;
6698
6699 /* Take everything up to the '<' */
6700 len = start - p;
6701 if (buf == NULL)
6702 totlen += len;
6703 else
6704 {
6705 mch_memmove(q, p, len);
6706 q += len;
6707 }
6708
6709 len = uc_check_code(start, end - start, q, cmd, eap,
6710 &split_buf, &split_len);
6711 if (len == (size_t)-1)
6712 {
6713 /* no match, continue after '<' */
6714 p = start + 1;
6715 len = 1;
6716 }
6717 else
6718 p = end;
6719 if (buf == NULL)
6720 totlen += len;
6721 else
6722 q += len;
6723 }
6724 if (buf != NULL) /* second time here, finished */
6725 {
6726 STRCPY(q, p);
6727 break;
6728 }
6729
6730 totlen += STRLEN(p); /* Add on the trailing characters */
6731 buf = alloc((unsigned)(totlen + 1));
6732 if (buf == NULL)
6733 {
6734 vim_free(split_buf);
6735 return;
6736 }
6737 }
6738
6739#ifdef FEAT_EVAL
6740 current_SID = cmd->uc_scriptID;
6741#endif
6742 (void)do_cmdline(buf, eap->getline, eap->cookie,
6743 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6744#ifdef FEAT_EVAL
6745 current_SID = save_current_SID;
6746#endif
6747 vim_free(buf);
6748 vim_free(split_buf);
6749}
6750
6751# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6752 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006753get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754{
6755 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6756}
6757
6758/*
6759 * Function given to ExpandGeneric() to obtain the list of user command names.
6760 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006762get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763{
6764 if (idx < curbuf->b_ucmds.ga_len)
6765 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6766 idx -= curbuf->b_ucmds.ga_len;
6767 if (idx < ucmds.ga_len)
6768 return USER_CMD(idx)->uc_name;
6769 return NULL;
6770}
6771
6772/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006773 * Function given to ExpandGeneric() to obtain the list of user address type names.
6774 */
6775 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006776get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006777{
6778 return (char_u *)addr_type_complete[idx].name;
6779}
6780
6781/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 * Function given to ExpandGeneric() to obtain the list of user command
6783 * attributes.
6784 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006786get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787{
6788 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006789 {"addr", "bang", "bar", "buffer", "complete",
6790 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006792 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 return NULL;
6794 return (char_u *)user_cmd_flags[idx];
6795}
6796
6797/*
6798 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006801get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802{
6803 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6804
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006805 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 return NULL;
6807 return (char_u *)user_cmd_nargs[idx];
6808}
6809
6810/*
6811 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6812 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006814get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815{
6816 return (char_u *)command_complete[idx].name;
6817}
6818# endif /* FEAT_CMDL_COMPL */
6819
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006820/*
6821 * Parse address type argument
6822 */
6823 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006824parse_addr_type_arg(
6825 char_u *value,
6826 int vallen,
6827 long *argt,
6828 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006829{
6830 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01006831
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006832 for (i = 0; addr_type_complete[i].expand != -1; ++i)
6833 {
6834 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
6835 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
6836 if (a && b)
6837 {
6838 *addr_type_arg = addr_type_complete[i].expand;
6839 break;
6840 }
6841 }
6842
6843 if (addr_type_complete[i].expand == -1)
6844 {
6845 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01006846
6847 for (i = 0; err[i] != NUL && !vim_iswhite(err[i]); i++)
6848 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006849 err[i] = NUL;
6850 EMSG2(_("E180: Invalid address type value: %s"), err);
6851 return FAIL;
6852 }
6853
6854 if (*addr_type_arg != ADDR_LINES)
6855 *argt |= NOTADR;
6856
6857 return OK;
6858}
6859
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860#endif /* FEAT_USR_CMDS */
6861
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006862#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6863/*
6864 * Parse a completion argument "value[vallen]".
6865 * The detected completion goes in "*complp", argument type in "*argt".
6866 * When there is an argument, for function and user defined completion, it's
6867 * copied to allocated memory and stored in "*compl_arg".
6868 * Returns FAIL if something is wrong.
6869 */
6870 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006871parse_compl_arg(
6872 char_u *value,
6873 int vallen,
6874 int *complp,
6875 long *argt,
6876 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006877{
6878 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006879# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006880 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006881# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006882 int i;
6883 int valend = vallen;
6884
6885 /* Look for any argument part - which is the part after any ',' */
6886 for (i = 0; i < vallen; ++i)
6887 {
6888 if (value[i] == ',')
6889 {
6890 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006891# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006892 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006893# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006894 valend = i;
6895 break;
6896 }
6897 }
6898
6899 for (i = 0; command_complete[i].expand != 0; ++i)
6900 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006901 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006902 && STRNCMP(value, command_complete[i].name, valend) == 0)
6903 {
6904 *complp = command_complete[i].expand;
6905 if (command_complete[i].expand == EXPAND_BUFFERS)
6906 *argt |= BUFNAME;
6907 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6908 || command_complete[i].expand == EXPAND_FILES)
6909 *argt |= XFILE;
6910 break;
6911 }
6912 }
6913
6914 if (command_complete[i].expand == 0)
6915 {
6916 EMSG2(_("E180: Invalid complete value: %s"), value);
6917 return FAIL;
6918 }
6919
6920# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6921 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6922 && arg != NULL)
6923# else
6924 if (arg != NULL)
6925# endif
6926 {
6927 EMSG(_("E468: Completion argument only allowed for custom completion"));
6928 return FAIL;
6929 }
6930
6931# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6932 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6933 && arg == NULL)
6934 {
6935 EMSG(_("E467: Custom completion requires a function argument"));
6936 return FAIL;
6937 }
6938
6939 if (arg != NULL)
6940 *compl_arg = vim_strnsave(arg, (int)arglen);
6941# endif
6942 return OK;
6943}
6944#endif
6945
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006947ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948{
Bram Moolenaare6850792010-05-14 15:28:44 +02006949 if (*eap->arg == NUL)
6950 {
6951#ifdef FEAT_EVAL
6952 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6953 char_u *p = NULL;
6954
6955 if (expr != NULL)
6956 {
6957 ++emsg_off;
6958 p = eval_to_string(expr, NULL, FALSE);
6959 --emsg_off;
6960 vim_free(expr);
6961 }
6962 if (p != NULL)
6963 {
6964 MSG(p);
6965 vim_free(p);
6966 }
6967 else
6968 MSG("default");
6969#else
6970 MSG(_("unknown"));
6971#endif
6972 }
6973 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006974 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975}
6976
6977 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006978ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979{
6980 if (*eap->arg == NUL && eap->cmd[2] == '!')
6981 MSG(_("Greetings, Vim user!"));
6982 do_highlight(eap->arg, eap->forceit, FALSE);
6983}
6984
6985
6986/*
6987 * Call this function if we thought we were going to exit, but we won't
6988 * (because of an error). May need to restore the terminal mode.
6989 */
6990 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006991not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992{
6993 exiting = FALSE;
6994 settmode(TMODE_RAW);
6995}
6996
6997/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01006998 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 */
7000 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007001ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002{
Bram Moolenaarf240e182014-11-27 18:33:02 +01007003#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007004 win_T *wp;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007005#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007006
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007#ifdef FEAT_CMDWIN
7008 if (cmdwin_type != 0)
7009 {
7010 cmdwin_result = Ctrl_C;
7011 return;
7012 }
7013#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007014 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007015 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007016 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007017 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007018 return;
7019 }
Bram Moolenaarf240e182014-11-27 18:33:02 +01007020#ifdef FEAT_WINDOWS
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007021 if (eap->addr_count > 0)
7022 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007023 int wnr = eap->line2;
7024
7025 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7026 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007027 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007028 }
7029 else
Bram Moolenaarf240e182014-11-27 18:33:02 +01007030#endif
7031#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007032 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007033#endif
7034
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007035#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02007036 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007037 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02007038 * being closed (can only happen in autocommands). */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007039 if (curbuf_locked() || (wp->w_buffer->b_nwindows == 1
7040 && wp->w_buffer->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007041 return;
7042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043
7044#ifdef FEAT_NETBEANS_INTG
7045 netbeansForcedQuit = eap->forceit;
7046#endif
7047
7048 /*
7049 * If there are more files or windows we won't exit.
7050 */
7051 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7052 exiting = TRUE;
7053 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007054 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7055 | (eap->forceit ? CCGD_FORCEIT : 0)
7056 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007058 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 {
7060 not_exiting();
7061 }
7062 else
7063 {
7064#ifdef FEAT_WINDOWS
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007065 /* quit last window
7066 * Note: only_one_window() returns true, even so a help window is
7067 * still open. In that case only quit, if no address has been
7068 * specified. Example:
7069 * :h|wincmd w|1q - don't quit
7070 * :h|wincmd w|q - quit
7071 */
7072 if (only_one_window() && (firstwin == lastwin || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073#endif
7074 getout(0);
7075#ifdef FEAT_WINDOWS
7076# ifdef FEAT_GUI
7077 need_mouse_correct = TRUE;
7078# endif
7079 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007080 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081#endif
7082 }
7083}
7084
7085/*
7086 * ":cquit".
7087 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007089ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090{
7091 getout(1); /* this does not always pass on the exit code to the Manx
7092 compiler. why? */
7093}
7094
7095/*
7096 * ":qall": try to quit all windows
7097 */
7098 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007099ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100{
7101# ifdef FEAT_CMDWIN
7102 if (cmdwin_type != 0)
7103 {
7104 if (eap->forceit)
7105 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7106 else
7107 cmdwin_result = K_XF2;
7108 return;
7109 }
7110# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007111
7112 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007113 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007114 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007115 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007116 return;
7117 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007118#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007119 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7120 /* Refuse to quit when locked or when the buffer in the last window is
7121 * being closed (can only happen in autocommands). */
7122 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007123 return;
7124#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007125
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007127 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 getout(0);
7129 not_exiting();
7130}
7131
Bram Moolenaard9967712006-03-11 21:18:15 +00007132#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133/*
7134 * ":close": close current window, unless it is the last one
7135 */
7136 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007137ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007139 win_T *win;
7140 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141# ifdef FEAT_CMDWIN
7142 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007143 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 else
7145# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007146 if (!text_locked()
7147#ifdef FEAT_AUTOCMD
7148 && !curbuf_locked()
7149#endif
7150 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007151 {
7152 if (eap->addr_count == 0)
7153 ex_win_close(eap->forceit, curwin, NULL);
7154 else {
7155 for (win = firstwin; win != NULL; win = win->w_next)
7156 {
7157 winnr++;
7158 if (winnr == eap->line2)
7159 break;
7160 }
7161 if (win == NULL)
7162 win = lastwin;
7163 ex_win_close(eap->forceit, win, NULL);
7164 }
7165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166}
7167
Bram Moolenaard9967712006-03-11 21:18:15 +00007168# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007169/*
7170 * ":pclose": Close any preview window.
7171 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007173ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007174{
7175 win_T *win;
7176
7177 for (win = firstwin; win != NULL; win = win->w_next)
7178 if (win->w_p_pvw)
7179 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007180 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007181 break;
7182 }
7183}
Bram Moolenaard9967712006-03-11 21:18:15 +00007184# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007185
Bram Moolenaarf740b292006-02-16 22:11:02 +00007186/*
7187 * Close window "win" and take care of handling closing the last window for a
7188 * modified buffer.
7189 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007190 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007191ex_win_close(
7192 int forceit,
7193 win_T *win,
7194 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195{
7196 int need_hide;
7197 buf_T *buf = win->w_buffer;
7198
7199 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007200 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007202# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 if ((p_confirm || cmdmod.confirm) && p_write)
7204 {
7205 dialog_changed(buf, FALSE);
7206 if (buf_valid(buf) && bufIsChanged(buf))
7207 return;
7208 need_hide = FALSE;
7209 }
7210 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007211# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 {
7213 EMSG(_(e_nowrtmsg));
7214 return;
7215 }
7216 }
7217
Bram Moolenaard9967712006-03-11 21:18:15 +00007218# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007220# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007221
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007223 if (tp == NULL)
7224 win_close(win, !need_hide && !P_HID(buf));
7225 else
7226 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227}
7228
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007230 * ":tabclose": close current tab page, unless it is the last one.
7231 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 */
7233 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007234ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007236 tabpage_T *tp;
7237
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007238# ifdef FEAT_CMDWIN
7239 if (cmdwin_type != 0)
7240 cmdwin_result = K_IGNORE;
7241 else
7242# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007243 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007244 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007245 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007247 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007248 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007249 tp = find_tabpage((int)eap->line2);
7250 if (tp == NULL)
7251 {
7252 beep_flush();
7253 return;
7254 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007255 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007256 {
7257 tabpage_close_other(tp, eap->forceit);
7258 return;
7259 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007260 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007261 if (!text_locked()
7262#ifdef FEAT_AUTOCMD
7263 && !curbuf_locked()
7264#endif
7265 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00007266 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007268}
7269
7270/*
7271 * ":tabonly": close all tab pages except the current one
7272 */
7273 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007274ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007275{
7276 tabpage_T *tp;
7277 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007278
7279# ifdef FEAT_CMDWIN
7280 if (cmdwin_type != 0)
7281 cmdwin_result = K_IGNORE;
7282 else
7283# endif
7284 if (first_tabpage->tp_next == NULL)
7285 MSG(_("Already only one tab page"));
7286 else
7287 {
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007288 if (eap->addr_count > 0)
7289 goto_tabpage(eap->line2);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007290 /* Repeat this up to a 1000 times, because autocommands may mess
7291 * up the lists. */
7292 for (done = 0; done < 1000; ++done)
7293 {
7294 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7295 if (tp->tp_topframe != topframe)
7296 {
7297 tabpage_close_other(tp, eap->forceit);
7298 /* if we failed to close it quit */
7299 if (valid_tabpage(tp))
7300 done = 1000;
7301 /* start over, "tp" is now invalid */
7302 break;
7303 }
7304 if (first_tabpage->tp_next == NULL)
7305 break;
7306 }
7307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309
7310/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007311 * Close the current tab page.
7312 */
7313 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007314tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007315{
7316 /* First close all the windows but the current one. If that worked then
7317 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007318 if (lastwin != firstwin)
7319 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007320 if (lastwin == firstwin)
7321 ex_win_close(forceit, curwin, NULL);
7322# ifdef FEAT_GUI
7323 need_mouse_correct = TRUE;
7324# endif
7325}
7326
7327/*
7328 * Close tab page "tp", which is not the current tab page.
7329 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007330 * Also takes care of the tab pages line disappearing when closing the
7331 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007332 */
7333 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007334tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007335{
7336 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007337 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007338 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007339
7340 /* Limit to 1000 windows, autocommands may add a window while we close
7341 * one. OK, so I'm paranoid... */
7342 while (++done < 1000)
7343 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007344 wp = tp->tp_firstwin;
7345 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007346
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007347 /* Autocommands may delete the tab page under our fingers and we may
7348 * fail to close a window with a modified buffer. */
7349 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007350 break;
7351 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007352
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007353 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007354 if (h != tabline_height())
7355 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007356}
7357
7358/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 * ":only".
7360 */
7361 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007362ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007364 win_T *wp;
7365 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366# ifdef FEAT_GUI
7367 need_mouse_correct = TRUE;
7368# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007369 if (eap->addr_count > 0)
7370 {
7371 wnr = eap->line2;
7372 for (wp = firstwin; --wnr > 0; )
7373 {
7374 if (wp->w_next == NULL)
7375 break;
7376 else
7377 wp = wp->w_next;
7378 }
7379 win_goto(wp);
7380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 close_others(TRUE, eap->forceit);
7382}
7383
7384/*
7385 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007386 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007388 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007389ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390{
7391 if (eap->addr_count == 0)
7392 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007393 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394}
7395#endif /* FEAT_WINDOWS */
7396
7397 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007398ex_hide(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399{
7400 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
7401 eap->errmsg = e_invarg;
7402 else
7403 {
7404 /* ":hide" or ":hide | cmd": hide current window */
7405 eap->nextcmd = check_nextcmd(eap->arg);
7406#ifdef FEAT_WINDOWS
7407 if (!eap->skip)
7408 {
7409# ifdef FEAT_GUI
7410 need_mouse_correct = TRUE;
7411# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007412 if (eap->addr_count == 0)
7413 win_close(curwin, FALSE); /* don't free buffer */
Bram Moolenaarf240e182014-11-27 18:33:02 +01007414 else
7415 {
7416 int winnr = 0;
7417 win_T *win;
7418
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007419 for (win = firstwin; win != NULL; win = win->w_next)
7420 {
7421 winnr++;
7422 if (winnr == eap->line2)
7423 break;
7424 }
7425 if (win == NULL)
7426 win = lastwin;
7427 win_close(win, FALSE);
7428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 }
7430#endif
7431 }
7432}
7433
7434/*
7435 * ":stop" and ":suspend": Suspend Vim.
7436 */
7437 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007438ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439{
7440 /*
7441 * Disallow suspending for "rvim".
7442 */
7443 if (!check_restricted()
7444#ifdef WIN3264
7445 /*
7446 * Check if external commands are allowed now.
7447 */
7448 && can_end_termcap_mode(TRUE)
7449#endif
7450 )
7451 {
7452 if (!eap->forceit)
7453 autowrite_all();
7454 windgoto((int)Rows - 1, 0);
7455 out_char('\n');
7456 out_flush();
7457 stoptermcap();
7458 out_flush(); /* needed for SUN to restore xterm buffer */
7459#ifdef FEAT_TITLE
7460 mch_restore_title(3); /* restore window titles */
7461#endif
7462 ui_suspend(); /* call machine specific function */
7463#ifdef FEAT_TITLE
7464 maketitle();
7465 resettitle(); /* force updating the title */
7466#endif
7467 starttermcap();
7468 scroll_start(); /* scroll screen before redrawing */
7469 redraw_later_clear();
7470 shell_resized(); /* may have resized window */
7471 }
7472}
7473
7474/*
7475 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7476 */
7477 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007478ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479{
7480#ifdef FEAT_CMDWIN
7481 if (cmdwin_type != 0)
7482 {
7483 cmdwin_result = Ctrl_C;
7484 return;
7485 }
7486#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007487 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007488 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007489 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007490 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007491 return;
7492 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007493#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007494 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7495 /* Refuse to quit when locked or when the buffer in the last window is
7496 * being closed (can only happen in autocommands). */
7497 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007498 return;
7499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500
7501 /*
7502 * if more files or windows we won't exit
7503 */
7504 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7505 exiting = TRUE;
7506 if ( ((eap->cmdidx == CMD_wq
7507 || curbufIsChanged())
7508 && do_write(eap) == FAIL)
7509 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007510 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 {
7512 not_exiting();
7513 }
7514 else
7515 {
7516#ifdef FEAT_WINDOWS
7517 if (only_one_window()) /* quit last window, exit Vim */
7518#endif
7519 getout(0);
7520#ifdef FEAT_WINDOWS
7521# ifdef FEAT_GUI
7522 need_mouse_correct = TRUE;
7523# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007524 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 win_close(curwin, !P_HID(curwin->w_buffer));
7526#endif
7527 }
7528}
7529
7530/*
7531 * ":print", ":list", ":number".
7532 */
7533 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007534ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007536 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7537 EMSG(_(e_emptybuf));
7538 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007540 for ( ;!got_int; ui_breakcheck())
7541 {
7542 print_line(eap->line1,
7543 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7544 || (eap->flags & EXFLAG_NR)),
7545 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7546 if (++eap->line1 > eap->line2)
7547 break;
7548 out_flush(); /* show one line at a time */
7549 }
7550 setpcmark();
7551 /* put cursor at last line */
7552 curwin->w_cursor.lnum = eap->line2;
7553 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 }
7555
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557}
7558
7559#ifdef FEAT_BYTEOFF
7560 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007561ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562{
7563 goto_byte(eap->line2);
7564}
7565#endif
7566
7567/*
7568 * ":shell".
7569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007571ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572{
7573 do_shell(NULL, 0);
7574}
7575
7576#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7577 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007578 || defined(FEAT_GUI_MSWIN) \
7579 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 || defined(PROTO)
7581
7582/*
7583 * Handle a file drop. The code is here because a drop is *nearly* like an
7584 * :args command, but not quite (we have a list of exact filenames, so we
7585 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7586 * code is very similar to :args and hence needs access to a lot of the static
7587 * functions in this file.
7588 *
7589 * The list should be allocated using alloc(), as should each item in the
7590 * list. This function takes over responsibility for freeing the list.
7591 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007592 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7594 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7595 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7596 * file functionality is (currently) not in EMX this is not presently a
7597 * problem.
7598 */
7599 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007600handle_drop(
7601 int filec, /* the number of files dropped */
7602 char_u **filev, /* the list of files dropped */
7603 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604{
7605 exarg_T ea;
7606 int save_msg_scroll = msg_scroll;
7607
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007608 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007609 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007611#ifdef FEAT_AUTOCMD
7612 if (curbuf_locked())
7613 return;
7614#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007615 /* When the screen is being updated we should not change buffers and
7616 * windows structures, it may cause freed memory to be used. */
7617 if (updating_screen)
7618 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619
7620 /* Check whether the current buffer is changed. If so, we will need
7621 * to split the current window or data could be lost.
7622 * We don't need to check if the 'hidden' option is set, as in this
7623 * case the buffer won't be lost.
7624 */
7625 if (!P_HID(curbuf) && !split)
7626 {
7627 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007628 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 --emsg_off;
7630 }
7631 if (split)
7632 {
7633# ifdef FEAT_WINDOWS
7634 if (win_split(0, 0) == FAIL)
7635 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007636 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637
7638 /* When splitting the window, create a new alist. Otherwise the
7639 * existing one is overwritten. */
7640 alist_unlink(curwin->w_alist);
7641 alist_new();
7642# else
7643 return; /* can't split, always fail */
7644# endif
7645 }
7646
7647 /*
7648 * Set up the new argument list.
7649 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007650 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651
7652 /*
7653 * Move to the first file.
7654 */
7655 /* Fake up a minimal "next" command for do_argfile() */
7656 vim_memset(&ea, 0, sizeof(ea));
7657 ea.cmd = (char_u *)"next";
7658 do_argfile(&ea, 0);
7659
7660 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7661 * mode that is not needed here. */
7662 need_start_insertmode = FALSE;
7663
7664 /* Restore msg_scroll, otherwise a following command may cause scrolling
7665 * unexpectedly. The screen will be redrawn by the caller, thus
7666 * msg_scroll being set by displaying a message is irrelevant. */
7667 msg_scroll = save_msg_scroll;
7668}
7669#endif
7670
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671/*
7672 * Clear an argument list: free all file names and reset it to zero entries.
7673 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007674 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007675alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676{
7677 while (--al->al_ga.ga_len >= 0)
7678 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7679 ga_clear(&al->al_ga);
7680}
7681
7682/*
7683 * Init an argument list.
7684 */
7685 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007686alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687{
7688 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7689}
7690
7691#if defined(FEAT_WINDOWS) || defined(PROTO)
7692
7693/*
7694 * Remove a reference from an argument list.
7695 * Ignored when the argument list is the global one.
7696 * If the argument list is no longer used by any window, free it.
7697 */
7698 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007699alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700{
7701 if (al != &global_alist && --al->al_refcount <= 0)
7702 {
7703 alist_clear(al);
7704 vim_free(al);
7705 }
7706}
7707
7708# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7709/*
7710 * Create a new argument list and use it for the current window.
7711 */
7712 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007713alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714{
7715 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7716 if (curwin->w_alist == NULL)
7717 {
7718 curwin->w_alist = &global_alist;
7719 ++global_alist.al_refcount;
7720 }
7721 else
7722 {
7723 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007724 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 alist_init(curwin->w_alist);
7726 }
7727}
7728# endif
7729#endif
7730
Bram Moolenaar53076832015-12-31 19:53:21 +01007731#if (!defined(UNIX) && !defined(__EMX__)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732/*
7733 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007734 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7735 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 */
7737 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007738alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739{
7740 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007741 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 char_u **new_arg_files;
7743 int new_arg_file_count;
7744 char_u *save_p_su = p_su;
7745 int i;
7746
7747 /* Don't use 'suffixes' here. This should work like the shell did the
7748 * expansion. Also, the vimrc file isn't read yet, thus the user
7749 * can't set the options. */
7750 p_su = empty_option;
7751 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7752 if (old_arg_files != NULL)
7753 {
7754 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007755 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7756 old_arg_count = GARGCOUNT;
7757 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007759 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 && new_arg_file_count > 0)
7761 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007762 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7763 TRUE, fnum_list, fnum_len);
7764 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 }
7767 p_su = save_p_su;
7768}
7769#endif
7770
7771/*
7772 * Set the argument list for the current window.
7773 * Takes over the allocated files[] and the allocated fnames in it.
7774 */
7775 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007776alist_set(
7777 alist_T *al,
7778 int count,
7779 char_u **files,
7780 int use_curbuf,
7781 int *fnum_list,
7782 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783{
7784 int i;
7785
7786 alist_clear(al);
7787 if (ga_grow(&al->al_ga, count) == OK)
7788 {
7789 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007790 {
7791 if (got_int)
7792 {
7793 /* When adding many buffers this can take a long time. Allow
7794 * interrupting here. */
7795 while (i < count)
7796 vim_free(files[i++]);
7797 break;
7798 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007799
7800 /* May set buffer name of a buffer previously used for the
7801 * argument list, so that it's re-used by alist_add. */
7802 if (fnum_list != NULL && i < fnum_len)
7803 buf_set_name(fnum_list[i], files[i]);
7804
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007806 ui_breakcheck();
7807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 vim_free(files);
7809 }
7810 else
7811 FreeWild(count, files);
7812#ifdef FEAT_WINDOWS
7813 if (al == &global_alist)
7814#endif
7815 arg_had_last = FALSE;
7816}
7817
7818/*
7819 * Add file "fname" to argument list "al".
7820 * "fname" must have been allocated and "al" must have been checked for room.
7821 */
7822 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007823alist_add(
7824 alist_T *al,
7825 char_u *fname,
7826 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827{
7828 if (fname == NULL) /* don't add NULL file names */
7829 return;
7830#ifdef BACKSLASH_IN_FILENAME
7831 slash_adjust(fname);
7832#endif
7833 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7834 if (set_fnum > 0)
7835 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7836 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7837 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838}
7839
7840#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7841/*
7842 * Adjust slashes in file names. Called after 'shellslash' was set.
7843 */
7844 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007845alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846{
7847 int i;
7848# ifdef FEAT_WINDOWS
7849 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007850 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851# endif
7852
7853 for (i = 0; i < GARGCOUNT; ++i)
7854 if (GARGLIST[i].ae_fname != NULL)
7855 slash_adjust(GARGLIST[i].ae_fname);
7856# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007857 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 if (wp->w_alist != &global_alist)
7859 for (i = 0; i < WARGCOUNT(wp); ++i)
7860 if (WARGLIST(wp)[i].ae_fname != NULL)
7861 slash_adjust(WARGLIST(wp)[i].ae_fname);
7862# endif
7863}
7864#endif
7865
7866/*
7867 * ":preserve".
7868 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007870ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007872 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 ml_preserve(curbuf, TRUE);
7874}
7875
7876/*
7877 * ":recover".
7878 */
7879 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007880ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881{
7882 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7883 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007884 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7885 | CCGD_MULTWIN
7886 | (eap->forceit ? CCGD_FORCEIT : 0)
7887 | CCGD_EXCMD)
7888
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 && (*eap->arg == NUL
7890 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7891 ml_recover();
7892 recoverymode = FALSE;
7893}
7894
7895/*
7896 * Command modifier used in a wrong way.
7897 */
7898 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007899ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900{
7901 eap->errmsg = e_invcmd;
7902}
7903
7904#ifdef FEAT_WINDOWS
7905/*
7906 * :sview [+command] file split window with new file, read-only
7907 * :split [[+command] file] split window with current or new file
7908 * :vsplit [[+command] file] split window vertically with current or new file
7909 * :new [[+command] file] split window with no or new file
7910 * :vnew [[+command] file] split vertically window with no or new file
7911 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007912 *
7913 * :tabedit open new Tab page with empty window
7914 * :tabedit [+command] file open new Tab page and edit "file"
7915 * :tabnew [[+command] file] just like :tabedit
7916 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 */
7918 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007919ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007921 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007922# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007924# endif
7925# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007927# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007929# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7931 {
7932 ex_ni(eap);
7933 return;
7934 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007935# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007937# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007939# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007941# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007943 * quickfix windows. But it's OK when doing ":tab split". */
7944 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 {
7946 if (eap->cmdidx == CMD_split)
7947 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007948# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 if (eap->cmdidx == CMD_vsplit)
7950 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007951# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007953# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007955# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007956 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 {
7958 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7959 FNAME_MESS, TRUE, curbuf->b_ffname);
7960 if (fname == NULL)
7961 goto theend;
7962 eap->arg = fname;
7963 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007964# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007966# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007968# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007970# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007972# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 && eap->cmdidx != CMD_new)
7974 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007975# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007976 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007977# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007978 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007979# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007980 au_has_group((char_u *)"FileExplorer"))
7981 {
7982 /* No browsing supported but we do have the file explorer:
7983 * Edit the directory. */
7984 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7985 eap->arg = (char_u *)".";
7986 }
7987 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007988# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007989 {
7990 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007992 if (fname == NULL)
7993 goto theend;
7994 eap->arg = fname;
7995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 }
7997 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007998# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008000 /*
8001 * Either open new tab page or split the window.
8002 */
8003 if (eap->cmdidx == CMD_tabedit
8004 || eap->cmdidx == CMD_tabfind
8005 || eap->cmdidx == CMD_tabnew)
8006 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008007 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8008 : eap->addr_count == 0 ? 0
8009 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008010 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008011 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008012
8013 /* set the alternate buffer for the window we came from */
8014 if (curwin != old_curwin
8015 && win_valid(old_curwin)
8016 && old_curwin->w_buffer != curbuf
8017 && !cmdmod.keepalt)
8018 old_curwin->w_alt_fnum = curbuf->b_fnum;
8019 }
8020 }
8021 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8023 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008024# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 /* Reset 'scrollbind' when editing another file, but keep it when
8026 * doing ":split" without arguments. */
8027 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008028# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008030# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008032 {
8033 RESET_BINDING(curwin);
8034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 else
8036 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008037# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 do_exedit(eap, old_curwin);
8039 }
8040
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008041# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008043# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008045# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046theend:
8047 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008048# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008050
8051/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008052 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008053 */
8054 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008055tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008056{
8057 exarg_T ea;
8058
8059 vim_memset(&ea, 0, sizeof(ea));
8060 ea.cmdidx = CMD_tabnew;
8061 ea.cmd = (char_u *)"tabn";
8062 ea.arg = (char_u *)"";
8063 ex_splitview(&ea);
8064}
8065
8066/*
8067 * :tabnext command
8068 */
8069 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008070ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008071{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008072 switch (eap->cmdidx)
8073 {
8074 case CMD_tabfirst:
8075 case CMD_tabrewind:
8076 goto_tabpage(1);
8077 break;
8078 case CMD_tablast:
8079 goto_tabpage(9999);
8080 break;
8081 case CMD_tabprevious:
8082 case CMD_tabNext:
8083 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
8084 break;
8085 default: /* CMD_tabnext */
8086 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
8087 break;
8088 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008089}
8090
8091/*
8092 * :tabmove command
8093 */
8094 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008095ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008096{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008097 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008098
8099 if (eap->arg && *eap->arg != NUL)
8100 {
8101 char_u *p = eap->arg;
8102 int relative = 0; /* argument +N/-N means: move N places to the
8103 * right/left relative to the current position. */
8104
8105 if (*eap->arg == '-')
8106 {
8107 relative = -1;
8108 p = eap->arg + 1;
8109 }
8110 else if (*eap->arg == '+')
8111 {
8112 relative = 1;
8113 p = eap->arg + 1;
8114 }
8115 else
8116 p = eap->arg;
8117
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008118 if (relative == 0)
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008119 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008120 if (STRCMP(p, "$") == 0)
8121 tab_number = LAST_TAB_NR;
8122 else if (p == skipdigits(p))
8123 {
8124 /* No numbers as argument. */
8125 eap->errmsg = e_invarg;
8126 return;
8127 }
8128 else
8129 tab_number = getdigits(&p);
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008130 }
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008131 else
8132 {
8133 if (*p != NUL)
8134 tab_number = getdigits(&p);
8135 else
8136 tab_number = 1;
8137 tab_number = tab_number * relative + tabpage_index(curtab);
8138 if (relative == -1)
8139 --tab_number;
8140 }
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008141 }
8142 else if (eap->addr_count != 0)
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008143 {
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008144 tab_number = eap->line2;
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008145 if (**eap->cmdlinep == '-')
8146 --tab_number;
8147 }
8148 else
8149 tab_number = LAST_TAB_NR;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008150
8151 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008152}
8153
8154/*
8155 * :tabs command: List tabs and their contents.
8156 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008157 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008158ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008159{
8160 tabpage_T *tp;
8161 win_T *wp;
8162 int tabcount = 1;
8163
8164 msg_start();
8165 msg_scroll = TRUE;
8166 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8167 {
8168 msg_putchar('\n');
8169 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
8170 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
8171 out_flush(); /* output one line at a time */
8172 ui_breakcheck();
8173
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008174 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008175 wp = firstwin;
8176 else
8177 wp = tp->tp_firstwin;
8178 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8179 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008180 msg_putchar('\n');
8181 msg_putchar(wp == curwin ? '>' : ' ');
8182 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008183 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8184 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008185 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008186 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008187 else
8188 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8189 IObuff, IOSIZE, TRUE);
8190 msg_outtrans(IObuff);
8191 out_flush(); /* output one line at a time */
8192 ui_breakcheck();
8193 }
8194 }
8195}
8196
8197#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198
8199/*
8200 * ":mode": Set screen mode.
8201 * If no argument given, just get the screen size and redraw.
8202 */
8203 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008204ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205{
8206 if (*eap->arg == NUL)
8207 shell_resized();
8208 else
8209 mch_screenmode(eap->arg);
8210}
8211
8212#ifdef FEAT_WINDOWS
8213/*
8214 * ":resize".
8215 * set, increment or decrement current window height
8216 */
8217 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008218ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219{
8220 int n;
8221 win_T *wp = curwin;
8222
8223 if (eap->addr_count > 0)
8224 {
8225 n = eap->line2;
8226 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8227 ;
8228 }
8229
8230#ifdef FEAT_GUI
8231 need_mouse_correct = TRUE;
8232#endif
8233 n = atol((char *)eap->arg);
8234#ifdef FEAT_VERTSPLIT
8235 if (cmdmod.split & WSP_VERT)
8236 {
8237 if (*eap->arg == '-' || *eap->arg == '+')
8238 n += W_WIDTH(curwin);
8239 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8240 n = 9999;
8241 win_setwidth_win((int)n, wp);
8242 }
8243 else
8244#endif
8245 {
8246 if (*eap->arg == '-' || *eap->arg == '+')
8247 n += curwin->w_height;
8248 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8249 n = 9999;
8250 win_setheight_win((int)n, wp);
8251 }
8252}
8253#endif
8254
8255/*
8256 * ":find [+command] <file>" command.
8257 */
8258 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008259ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260{
8261#ifdef FEAT_SEARCHPATH
8262 char_u *fname;
8263 int count;
8264
8265 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8266 TRUE, curbuf->b_ffname);
8267 if (eap->addr_count > 0)
8268 {
8269 /* Repeat finding the file "count" times. This matters when it
8270 * appears several times in the path. */
8271 count = eap->line2;
8272 while (fname != NULL && --count > 0)
8273 {
8274 vim_free(fname);
8275 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8276 FALSE, curbuf->b_ffname);
8277 }
8278 }
8279
8280 if (fname != NULL)
8281 {
8282 eap->arg = fname;
8283#endif
8284 do_exedit(eap, NULL);
8285#ifdef FEAT_SEARCHPATH
8286 vim_free(fname);
8287 }
8288#endif
8289}
8290
8291/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008292 * ":open" simulation: for now just work like ":visual".
8293 */
8294 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008295ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008296{
8297 regmatch_T regmatch;
8298 char_u *p;
8299
8300 curwin->w_cursor.lnum = eap->line2;
8301 beginline(BL_SOL | BL_FIX);
8302 if (*eap->arg == '/')
8303 {
8304 /* ":open /pattern/": put cursor in column found with pattern */
8305 ++eap->arg;
8306 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8307 *p = NUL;
8308 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8309 if (regmatch.regprog != NULL)
8310 {
8311 regmatch.rm_ic = p_ic;
8312 p = ml_get_curline();
8313 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008314 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008315 else
8316 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008317 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008318 }
8319 /* Move to the NUL, ignore any other arguments. */
8320 eap->arg += STRLEN(eap->arg);
8321 }
8322 check_cursor();
8323
8324 eap->cmdidx = CMD_visual;
8325 do_exedit(eap, NULL);
8326}
8327
8328/*
8329 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 */
8331 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008332ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333{
8334 do_exedit(eap, NULL);
8335}
8336
8337/*
8338 * ":edit <file>" command and alikes.
8339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008341do_exedit(
8342 exarg_T *eap,
8343 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344{
8345 int n;
8346#ifdef FEAT_WINDOWS
8347 int need_hide;
8348#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008349 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350
8351 /*
8352 * ":vi" command ends Ex mode.
8353 */
8354 if (exmode_active && (eap->cmdidx == CMD_visual
8355 || eap->cmdidx == CMD_view))
8356 {
8357 exmode_active = FALSE;
8358 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008359 {
8360 /* Special case: ":global/pat/visual\NLvi-commands" */
8361 if (global_busy)
8362 {
8363 int rd = RedrawingDisabled;
8364 int nwr = no_wait_return;
8365 int ms = msg_scroll;
8366#ifdef FEAT_GUI
8367 int he = hold_gui_events;
8368#endif
8369
8370 if (eap->nextcmd != NULL)
8371 {
8372 stuffReadbuff(eap->nextcmd);
8373 eap->nextcmd = NULL;
8374 }
8375
8376 if (exmode_was != EXMODE_VIM)
8377 settmode(TMODE_RAW);
8378 RedrawingDisabled = 0;
8379 no_wait_return = 0;
8380 need_wait_return = FALSE;
8381 msg_scroll = 0;
8382#ifdef FEAT_GUI
8383 hold_gui_events = 0;
8384#endif
8385 must_redraw = CLEAR;
8386
8387 main_loop(FALSE, TRUE);
8388
8389 RedrawingDisabled = rd;
8390 no_wait_return = nwr;
8391 msg_scroll = ms;
8392#ifdef FEAT_GUI
8393 hold_gui_events = he;
8394#endif
8395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 }
8399
8400 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008401 || eap->cmdidx == CMD_tabnew
8402 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403#ifdef FEAT_VERTSPLIT
8404 || eap->cmdidx == CMD_vnew
8405#endif
8406 ) && *eap->arg == NUL)
8407 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008408 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 setpcmark();
8410 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008411 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8412 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 }
8414 else if ((eap->cmdidx != CMD_split
8415#ifdef FEAT_VERTSPLIT
8416 && eap->cmdidx != CMD_vsplit
8417#endif
8418 )
8419 || *eap->arg != NUL
8420#ifdef FEAT_BROWSE
8421 || cmdmod.browse
8422#endif
8423 )
8424 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008425#ifdef FEAT_AUTOCMD
8426 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8427 * can bring us here, others are stopped earlier. */
8428 if (*eap->arg != NUL && curbuf_locked())
8429 return;
8430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 n = readonlymode;
8432 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8433 readonlymode = TRUE;
8434 else if (eap->cmdidx == CMD_enew)
8435 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8436 empty buffer */
8437 setpcmark();
8438 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8439 NULL, eap,
8440 /* ":edit" goes to first line if Vi compatible */
8441 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8442 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8443 ? ECMD_ONE : eap->do_ecmd_lnum,
8444 (P_HID(curbuf) ? ECMD_HIDE : 0)
8445 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008446 /* after a split we can use an existing buffer */
8447 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448#ifdef FEAT_LISTCMDS
8449 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8450#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008451 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 {
8453 /* Editing the file failed. If the window was split, close it. */
8454#ifdef FEAT_WINDOWS
8455 if (old_curwin != NULL)
8456 {
8457 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8458 if (!need_hide || P_HID(curbuf))
8459 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008460# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8461 cleanup_T cs;
8462
8463 /* Reset the error/interrupt/exception state here so that
8464 * aborting() returns FALSE when closing a window. */
8465 enter_cleanup(&cs);
8466# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467# ifdef FEAT_GUI
8468 need_mouse_correct = TRUE;
8469# endif
8470 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008471
8472# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8473 /* Restore the error/interrupt/exception state if not
8474 * discarded by a new aborting error, interrupt, or
8475 * uncaught exception. */
8476 leave_cleanup(&cs);
8477# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 }
8479 }
8480#endif
8481 }
8482 else if (readonlymode && curbuf->b_nwindows == 1)
8483 {
8484 /* When editing an already visited buffer, 'readonly' won't be set
8485 * but the previous value is kept. With ":view" and ":sview" we
8486 * want the file to be readonly, except when another window is
8487 * editing the same buffer. */
8488 curbuf->b_p_ro = TRUE;
8489 }
8490 readonlymode = n;
8491 }
8492 else
8493 {
8494 if (eap->do_ecmd_cmd != NULL)
8495 do_cmdline_cmd(eap->do_ecmd_cmd);
8496#ifdef FEAT_TITLE
8497 n = curwin->w_arg_idx_invalid;
8498#endif
8499 check_arg_idx(curwin);
8500#ifdef FEAT_TITLE
8501 if (n != curwin->w_arg_idx_invalid)
8502 maketitle();
8503#endif
8504 }
8505
8506#ifdef FEAT_WINDOWS
8507 /*
8508 * if ":split file" worked, set alternate file name in old window to new
8509 * file
8510 */
8511 if (old_curwin != NULL
8512 && *eap->arg != NUL
8513 && curwin != old_curwin
8514 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008515 && old_curwin->w_buffer != curbuf
8516 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 old_curwin->w_alt_fnum = curbuf->b_fnum;
8518#endif
8519
8520 ex_no_reprint = TRUE;
8521}
8522
8523#ifndef FEAT_GUI
8524/*
8525 * ":gui" and ":gvim" when there is no GUI.
8526 */
8527 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008528ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529{
8530 eap->errmsg = e_nogvim;
8531}
8532#endif
8533
8534#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8535 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008536ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537{
8538 gui_make_tearoff(eap->arg);
8539}
8540#endif
8541
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008542#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008544ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008546 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547}
8548#endif
8549
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008551ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552{
8553 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8554 MSG(_("No swap file"));
8555 else
8556 msg(curbuf->b_ml.ml_mfp->mf_fname);
8557}
8558
8559/*
8560 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8561 * offset.
8562 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8563 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008565ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566{
8567#ifdef FEAT_SCROLLBIND
8568 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008569 win_T *save_curwin = curwin;
8570 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 long topline;
8572 long y;
8573 linenr_T old_linenr = curwin->w_cursor.lnum;
8574
8575 setpcmark();
8576
8577 /*
8578 * determine max topline
8579 */
8580 if (curwin->w_p_scb)
8581 {
8582 topline = curwin->w_topline;
8583 for (wp = firstwin; wp; wp = wp->w_next)
8584 {
8585 if (wp->w_p_scb && wp->w_buffer)
8586 {
8587 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8588 if (topline > y)
8589 topline = y;
8590 }
8591 }
8592 if (topline < 1)
8593 topline = 1;
8594 }
8595 else
8596 {
8597 topline = 1;
8598 }
8599
8600
8601 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008602 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8605 {
8606 if (curwin->w_p_scb)
8607 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008608 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 y = topline - curwin->w_topline;
8610 if (y > 0)
8611 scrollup(y, TRUE);
8612 else
8613 scrolldown(-y, TRUE);
8614 curwin->w_scbind_pos = topline;
8615 redraw_later(VALID);
8616 cursor_correct();
8617#ifdef FEAT_WINDOWS
8618 curwin->w_redr_status = TRUE;
8619#endif
8620 }
8621 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008622 curwin = save_curwin;
8623 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 if (curwin->w_p_scb)
8625 {
8626 did_syncbind = TRUE;
8627 checkpcmark();
8628 if (old_linenr != curwin->w_cursor.lnum)
8629 {
8630 char_u ctrl_o[2];
8631
8632 ctrl_o[0] = Ctrl_O;
8633 ctrl_o[1] = 0;
8634 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8635 }
8636 }
8637#endif
8638}
8639
8640
8641 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008642ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008644 int i;
8645 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8646 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647
8648 if (eap->usefilter) /* :r!cmd */
8649 do_bang(1, eap, FALSE, FALSE, TRUE);
8650 else
8651 {
8652 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8653 return;
8654
8655#ifdef FEAT_BROWSE
8656 if (cmdmod.browse)
8657 {
8658 char_u *browseFile;
8659
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008660 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 NULL, NULL, NULL, curbuf);
8662 if (browseFile != NULL)
8663 {
8664 i = readfile(browseFile, NULL,
8665 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8666 vim_free(browseFile);
8667 }
8668 else
8669 i = OK;
8670 }
8671 else
8672#endif
8673 if (*eap->arg == NUL)
8674 {
8675 if (check_fname() == FAIL) /* check for no file name */
8676 return;
8677 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8678 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8679 }
8680 else
8681 {
8682 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8683 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8684 i = readfile(eap->arg, NULL,
8685 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8686
8687 }
8688 if (i == FAIL)
8689 {
8690#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8691 if (!aborting())
8692#endif
8693 EMSG2(_(e_notopen), eap->arg);
8694 }
8695 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008696 {
8697 if (empty && exmode_active)
8698 {
8699 /* Delete the empty line that remains. Historically ex does
8700 * this but vi doesn't. */
8701 if (eap->line2 == 0)
8702 lnum = curbuf->b_ml.ml_line_count;
8703 else
8704 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008705 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008706 {
8707 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008708 if (curwin->w_cursor.lnum > 1
8709 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008710 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008711 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008712 }
8713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 }
8717}
8718
Bram Moolenaarea408852005-06-25 22:49:46 +00008719static char_u *prev_dir = NULL;
8720
8721#if defined(EXITFREE) || defined(PROTO)
8722 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008723free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00008724{
8725 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008726 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008727
8728 vim_free(globaldir);
8729 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008730}
8731#endif
8732
Bram Moolenaarf4258302013-06-02 18:20:17 +02008733/*
8734 * Deal with the side effects of changing the current directory.
8735 * When "local" is TRUE then this was after an ":lcd" command.
8736 */
8737 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008738post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008739{
8740 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008741 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008742 if (local)
8743 {
8744 /* If still in global directory, need to remember current
8745 * directory as global directory. */
8746 if (globaldir == NULL && prev_dir != NULL)
8747 globaldir = vim_strsave(prev_dir);
8748 /* Remember this local directory for the window. */
8749 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8750 curwin->w_localdir = vim_strsave(NameBuff);
8751 }
8752 else
8753 {
8754 /* We are now in the global directory, no need to remember its
8755 * name. */
8756 vim_free(globaldir);
8757 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008758 }
8759
8760 shorten_fnames(TRUE);
8761}
8762
Bram Moolenaarea408852005-06-25 22:49:46 +00008763
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764/*
8765 * ":cd", ":lcd", ":chdir" and ":lchdir".
8766 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008767 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008768ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 char_u *new_dir;
8771 char_u *tofree;
8772
8773 new_dir = eap->arg;
8774#if !defined(UNIX) && !defined(VMS)
8775 /* for non-UNIX ":cd" means: print current directory */
8776 if (*new_dir == NUL)
8777 ex_pwd(NULL);
8778 else
8779#endif
8780 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008781#ifdef FEAT_AUTOCMD
8782 if (allbuf_locked())
8783 return;
8784#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008785 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8786 && !eap->forceit)
8787 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008788 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008789 return;
8790 }
8791
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 /* ":cd -": Change to previous directory */
8793 if (STRCMP(new_dir, "-") == 0)
8794 {
8795 if (prev_dir == NULL)
8796 {
8797 EMSG(_("E186: No previous directory"));
8798 return;
8799 }
8800 new_dir = prev_dir;
8801 }
8802
8803 /* Save current directory for next ":cd -" */
8804 tofree = prev_dir;
8805 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8806 prev_dir = vim_strsave(NameBuff);
8807 else
8808 prev_dir = NULL;
8809
8810#if defined(UNIX) || defined(VMS)
8811 /* for UNIX ":cd" means: go to home directory */
8812 if (*new_dir == NUL)
8813 {
8814 /* use NameBuff for home directory name */
8815# ifdef VMS
8816 char_u *p;
8817
8818 p = mch_getenv((char_u *)"SYS$LOGIN");
8819 if (p == NULL || *p == NUL) /* empty is the same as not set */
8820 NameBuff[0] = NUL;
8821 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008822 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823# else
8824 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8825# endif
8826 new_dir = NameBuff;
8827 }
8828#endif
8829 if (new_dir == NULL || vim_chdir(new_dir))
8830 EMSG(_(e_failed));
8831 else
8832 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008833 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834
8835 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008836 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 ex_pwd(eap);
8838 }
8839 vim_free(tofree);
8840 }
8841}
8842
8843/*
8844 * ":pwd".
8845 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008847ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848{
8849 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8850 {
8851#ifdef BACKSLASH_IN_FILENAME
8852 slash_adjust(NameBuff);
8853#endif
8854 msg(NameBuff);
8855 }
8856 else
8857 EMSG(_("E187: Unknown"));
8858}
8859
8860/*
8861 * ":=".
8862 */
8863 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008864ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865{
8866 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008867 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868}
8869
8870 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008871ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008873 int n;
8874 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875
8876 if (cursor_valid())
8877 {
8878 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8879 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01008880 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008882
8883 len = eap->line2;
8884 switch (*eap->arg)
8885 {
8886 case 'm': break;
8887 case NUL: len *= 1000L; break;
8888 default: EMSG2(_(e_invarg2), eap->arg); return;
8889 }
8890 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891}
8892
8893/*
8894 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8895 */
8896 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008897do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898{
8899 long done;
8900
8901 cursor_on();
8902 out_flush();
8903 for (done = 0; !got_int && done < msec; done += 1000L)
8904 {
8905 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8906 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008907#ifdef MESSAGE_QUEUE
8908 /* Process the netbeans and clientserver messages that may have been
8909 * received in the call to ui_breakcheck() when the GUI is in use. This
8910 * may occur when running a test case. */
8911 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 }
8914}
8915
8916 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008917do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918{
8919 int mode;
8920 char_u *cmdp;
8921
8922 cmdp = eap->cmd;
8923 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8924
8925 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8926 eap->arg, mode, isabbrev))
8927 {
8928 case 1: EMSG(_(e_invarg));
8929 break;
8930 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8931 break;
8932 }
8933}
8934
8935/*
8936 * ":winsize" command (obsolete).
8937 */
8938 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008939ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940{
8941 int w, h;
8942 char_u *arg = eap->arg;
8943 char_u *p;
8944
8945 w = getdigits(&arg);
8946 arg = skipwhite(arg);
8947 p = arg;
8948 h = getdigits(&arg);
8949 if (*p != NUL && *arg == NUL)
8950 set_shellsize(w, h, TRUE);
8951 else
8952 EMSG(_("E465: :winsize requires two number arguments"));
8953}
8954
8955#ifdef FEAT_WINDOWS
8956 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008957ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958{
8959 int xchar = NUL;
8960 char_u *p;
8961
8962 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8963 {
8964 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8965 if (eap->arg[1] == NUL)
8966 {
8967 EMSG(_(e_invarg));
8968 return;
8969 }
8970 xchar = eap->arg[1];
8971 p = eap->arg + 2;
8972 }
8973 else
8974 p = eap->arg + 1;
8975
8976 eap->nextcmd = check_nextcmd(p);
8977 p = skipwhite(p);
8978 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8979 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008980 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 {
8982 /* Pass flags on for ":vertical wincmd ]". */
8983 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008984 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8986 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008987 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 }
8989}
8990#endif
8991
Bram Moolenaar843ee412004-06-30 16:16:41 +00008992#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993/*
8994 * ":winpos".
8995 */
8996 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008997ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998{
8999 int x, y;
9000 char_u *arg = eap->arg;
9001 char_u *p;
9002
9003 if (*arg == NUL)
9004 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009005# if defined(FEAT_GUI) || defined(MSWIN)
9006# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009008# else
9009 if (mch_get_winpos(&x, &y) != FAIL)
9010# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 {
9012 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9013 msg(IObuff);
9014 }
9015 else
9016# endif
9017 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9018 }
9019 else
9020 {
9021 x = getdigits(&arg);
9022 arg = skipwhite(arg);
9023 p = arg;
9024 y = getdigits(&arg);
9025 if (*p == NUL || *arg != NUL)
9026 {
9027 EMSG(_("E466: :winpos requires two number arguments"));
9028 return;
9029 }
9030# ifdef FEAT_GUI
9031 if (gui.in_use)
9032 gui_mch_set_winpos(x, y);
9033 else if (gui.starting)
9034 {
9035 /* Remember the coordinates for when the window is opened. */
9036 gui_win_x = x;
9037 gui_win_y = y;
9038 }
9039# ifdef HAVE_TGETENT
9040 else
9041# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009042# else
9043# ifdef MSWIN
9044 mch_set_winpos(x, y);
9045# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046# endif
9047# ifdef HAVE_TGETENT
9048 if (*T_CWP)
9049 term_set_winpos(x, y);
9050# endif
9051 }
9052}
9053#endif
9054
9055/*
9056 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9057 */
9058 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009059ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060{
9061 oparg_T oa;
9062
9063 clear_oparg(&oa);
9064 oa.regname = eap->regname;
9065 oa.start.lnum = eap->line1;
9066 oa.end.lnum = eap->line2;
9067 oa.line_count = eap->line2 - eap->line1 + 1;
9068 oa.motion_type = MLINE;
9069#ifdef FEAT_VIRTUALEDIT
9070 virtual_op = FALSE;
9071#endif
9072 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9073 {
9074 setpcmark();
9075 curwin->w_cursor.lnum = eap->line1;
9076 beginline(BL_SOL | BL_FIX);
9077 }
9078
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009079 if (VIsual_active)
9080 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009081
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 switch (eap->cmdidx)
9083 {
9084 case CMD_delete:
9085 oa.op_type = OP_DELETE;
9086 op_delete(&oa);
9087 break;
9088
9089 case CMD_yank:
9090 oa.op_type = OP_YANK;
9091 (void)op_yank(&oa, FALSE, TRUE);
9092 break;
9093
9094 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009095 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009097 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9098#else
9099 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009101 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102 oa.op_type = OP_RSHIFT;
9103 else
9104 oa.op_type = OP_LSHIFT;
9105 op_shift(&oa, FALSE, eap->amount);
9106 break;
9107 }
9108#ifdef FEAT_VIRTUALEDIT
9109 virtual_op = MAYBE;
9110#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009111 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112}
9113
9114/*
9115 * ":put".
9116 */
9117 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009118ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119{
9120 /* ":0put" works like ":1put!". */
9121 if (eap->line2 == 0)
9122 {
9123 eap->line2 = 1;
9124 eap->forceit = TRUE;
9125 }
9126 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009127 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9128 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129}
9130
9131/*
9132 * Handle ":copy" and ":move".
9133 */
9134 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009135ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136{
9137 long n;
9138
Bram Moolenaaraa23b372015-09-08 18:46:31 +02009139 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140 if (eap->arg == NULL) /* error detected */
9141 {
9142 eap->nextcmd = NULL;
9143 return;
9144 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009145 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146
9147 /*
9148 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9149 */
9150 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9151 {
9152 EMSG(_(e_invaddr));
9153 return;
9154 }
9155
9156 if (eap->cmdidx == CMD_move)
9157 {
9158 if (do_move(eap->line1, eap->line2, n) == FAIL)
9159 return;
9160 }
9161 else
9162 ex_copy(eap->line1, eap->line2, n);
9163 u_clearline();
9164 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009165 ex_may_print(eap);
9166}
9167
9168/*
9169 * Print the current line if flags were given to the Ex command.
9170 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009171 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009172ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009173{
9174 if (eap->flags != 0)
9175 {
9176 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9177 (eap->flags & EXFLAG_LIST));
9178 ex_no_reprint = TRUE;
9179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180}
9181
9182/*
9183 * ":smagic" and ":snomagic".
9184 */
9185 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009186ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187{
9188 int magic_save = p_magic;
9189
9190 p_magic = (eap->cmdidx == CMD_smagic);
9191 do_sub(eap);
9192 p_magic = magic_save;
9193}
9194
9195/*
9196 * ":join".
9197 */
9198 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009199ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200{
9201 curwin->w_cursor.lnum = eap->line1;
9202 if (eap->line1 == eap->line2)
9203 {
9204 if (eap->addr_count >= 2) /* :2,2join does nothing */
9205 return;
9206 if (eap->line2 == curbuf->b_ml.ml_line_count)
9207 {
9208 beep_flush();
9209 return;
9210 }
9211 ++eap->line2;
9212 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009213 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009215 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216}
9217
9218/*
9219 * ":[addr]@r" or ":[addr]*r": execute register
9220 */
9221 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009222ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223{
9224 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009225 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226
9227 curwin->w_cursor.lnum = eap->line2;
9228
9229#ifdef USE_ON_FLY_SCROLL
9230 dont_scroll = TRUE; /* disallow scrolling here */
9231#endif
9232
9233 /* get the register name. No name means to use the previous one */
9234 c = *eap->arg;
9235 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9236 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009237 /* Put the register in the typeahead buffer with the "silent" flag. */
9238 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9239 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009240 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 else
9244 {
9245 int save_efr = exec_from_reg;
9246
9247 exec_from_reg = TRUE;
9248
9249 /*
9250 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009251 * Continue until the stuff buffer is empty and all added characters
9252 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009254 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9256
9257 exec_from_reg = save_efr;
9258 }
9259}
9260
9261/*
9262 * ":!".
9263 */
9264 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009265ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266{
9267 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9268}
9269
9270/*
9271 * ":undo".
9272 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009274ex_undo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009276 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009277 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009278 else
9279 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280}
9281
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009282#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009283 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009284ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009285{
9286 char_u hash[UNDO_HASH_SIZE];
9287
9288 u_compute_hash(hash);
9289 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9290}
9291
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009292 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009293ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009294{
9295 char_u hash[UNDO_HASH_SIZE];
9296
9297 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009298 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009299}
9300#endif
9301
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302/*
9303 * ":redo".
9304 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009306ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307{
9308 u_redo(1);
9309}
9310
9311/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009312 * ":earlier" and ":later".
9313 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009314 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009315ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009316{
9317 long count = 0;
9318 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009319 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009320 char_u *p = eap->arg;
9321
9322 if (*p == NUL)
9323 count = 1;
9324 else if (isdigit(*p))
9325 {
9326 count = getdigits(&p);
9327 switch (*p)
9328 {
9329 case 's': ++p; sec = TRUE; break;
9330 case 'm': ++p; sec = TRUE; count *= 60; break;
9331 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009332 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9333 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009334 }
9335 }
9336
9337 if (*p != NUL)
9338 EMSG2(_(e_invarg2), eap->arg);
9339 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009340 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9341 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009342}
9343
9344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345 * ":redir": start/stop redirection.
9346 */
9347 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009348ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349{
9350 char *mode;
9351 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009352 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353
9354 if (STRICMP(eap->arg, "END") == 0)
9355 close_redir();
9356 else
9357 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009358 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009360 ++arg;
9361 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009363 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 mode = "a";
9365 }
9366 else
9367 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009368 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369
9370 close_redir();
9371
9372 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009373 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 if (fname == NULL)
9375 return;
9376#ifdef FEAT_BROWSE
9377 if (cmdmod.browse)
9378 {
9379 char_u *browseFile;
9380
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009381 browseFile = do_browse(BROWSE_SAVE,
9382 (char_u *)_("Save Redirection"),
9383 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 if (browseFile == NULL)
9385 return; /* operation cancelled */
9386 vim_free(fname);
9387 fname = browseFile;
9388 eap->forceit = TRUE; /* since dialog already asked */
9389 }
9390#endif
9391
9392 redir_fd = open_exfile(fname, eap->forceit, mode);
9393 vim_free(fname);
9394 }
9395#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009396 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 {
9398 /* redirect to a register a-z (resp. A-Z for appending) */
9399 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009400 ++arg;
9401 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009403 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009404 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009406 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009408 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009409 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009410 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009411 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009413 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009414 if (*arg == '>')
9415 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009416 /* Make register empty when not using @A-@Z and the
9417 * command is valid. */
9418 if (*arg == NUL && !isupper(redir_reg))
9419 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009421 }
9422 if (*arg != NUL)
9423 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009424 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009425 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009426 }
9427 }
9428 else if (*arg == '=' && arg[1] == '>')
9429 {
9430 int append;
9431
9432 /* redirect to a variable */
9433 close_redir();
9434 arg += 2;
9435
9436 if (*arg == '>')
9437 {
9438 ++arg;
9439 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440 }
9441 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009442 append = FALSE;
9443
9444 if (var_redir_start(skipwhite(arg), append) == OK)
9445 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 }
9447#endif
9448
9449 /* TODO: redirect to a buffer */
9450
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009452 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009454
9455 /* Make sure redirection is not off. Can happen for cmdline completion
9456 * that indirectly invokes a command to catch its output. */
9457 if (redir_fd != NULL
9458#ifdef FEAT_EVAL
9459 || redir_reg || redir_vname
9460#endif
9461 )
9462 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463}
9464
9465/*
9466 * ":redraw": force redraw
9467 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009468 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009469ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470{
9471 int r = RedrawingDisabled;
9472 int p = p_lz;
9473
9474 RedrawingDisabled = 0;
9475 p_lz = FALSE;
9476 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009477 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478#ifdef FEAT_TITLE
9479 if (need_maketitle)
9480 maketitle();
9481#endif
9482 RedrawingDisabled = r;
9483 p_lz = p;
9484
9485 /* Reset msg_didout, so that a message that's there is overwritten. */
9486 msg_didout = FALSE;
9487 msg_col = 0;
9488
Bram Moolenaar56667a52013-07-17 11:54:28 +02009489 /* No need to wait after an intentional redraw. */
9490 need_wait_return = FALSE;
9491
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 out_flush();
9493}
9494
9495/*
9496 * ":redrawstatus": force redraw of status line(s)
9497 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009499ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500{
9501#if defined(FEAT_WINDOWS)
9502 int r = RedrawingDisabled;
9503 int p = p_lz;
9504
9505 RedrawingDisabled = 0;
9506 p_lz = FALSE;
9507 if (eap->forceit)
9508 status_redraw_all();
9509 else
9510 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009511 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 RedrawingDisabled = r;
9513 p_lz = p;
9514 out_flush();
9515#endif
9516}
9517
9518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009519close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520{
9521 if (redir_fd != NULL)
9522 {
9523 fclose(redir_fd);
9524 redir_fd = NULL;
9525 }
9526#ifdef FEAT_EVAL
9527 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009528 if (redir_vname)
9529 {
9530 var_redir_stop();
9531 redir_vname = 0;
9532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533#endif
9534}
9535
9536#if defined(FEAT_SESSION) && defined(USE_CRNL)
9537# define MKSESSION_NL
9538static int mksession_nl = FALSE; /* use NL only in put_eol() */
9539#endif
9540
9541/*
9542 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9543 */
9544 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009545ex_mkrc(
9546 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547{
9548 FILE *fd;
9549 int failed = FALSE;
9550 char_u *fname;
9551#ifdef FEAT_BROWSE
9552 char_u *browseFile = NULL;
9553#endif
9554#ifdef FEAT_SESSION
9555 int view_session = FALSE;
9556 int using_vdir = FALSE; /* using 'viewdir'? */
9557 char_u *viewFile = NULL;
9558 unsigned *flagp;
9559#endif
9560
9561 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9562 {
9563#ifdef FEAT_SESSION
9564 view_session = TRUE;
9565#else
9566 ex_ni(eap);
9567 return;
9568#endif
9569 }
9570
9571#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009572 /* Use the short file name until ":lcd" is used. We also don't use the
9573 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009574 did_lcd = FALSE;
9575
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9577 if (eap->cmdidx == CMD_mkview
9578 && (*eap->arg == NUL
9579 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9580 {
9581 eap->forceit = TRUE;
9582 fname = get_view_file(*eap->arg);
9583 if (fname == NULL)
9584 return;
9585 viewFile = fname;
9586 using_vdir = TRUE;
9587 }
9588 else
9589#endif
9590 if (*eap->arg != NUL)
9591 fname = eap->arg;
9592 else if (eap->cmdidx == CMD_mkvimrc)
9593 fname = (char_u *)VIMRC_FILE;
9594#ifdef FEAT_SESSION
9595 else if (eap->cmdidx == CMD_mksession)
9596 fname = (char_u *)SESSION_FILE;
9597#endif
9598 else
9599 fname = (char_u *)EXRC_FILE;
9600
9601#ifdef FEAT_BROWSE
9602 if (cmdmod.browse)
9603 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009604 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605# ifdef FEAT_SESSION
9606 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9607 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9608# endif
9609 (char_u *)_("Save Setup"),
9610 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9611 if (browseFile == NULL)
9612 goto theend;
9613 fname = browseFile;
9614 eap->forceit = TRUE; /* since dialog already asked */
9615 }
9616#endif
9617
9618#if defined(FEAT_SESSION) && defined(vim_mkdir)
9619 /* When using 'viewdir' may have to create the directory. */
9620 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009621 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622#endif
9623
9624 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9625 if (fd != NULL)
9626 {
9627#ifdef FEAT_SESSION
9628 if (eap->cmdidx == CMD_mkview)
9629 flagp = &vop_flags;
9630 else
9631 flagp = &ssop_flags;
9632#endif
9633
9634#ifdef MKSESSION_NL
9635 /* "unix" in 'sessionoptions': use NL line separator */
9636 if (view_session && (*flagp & SSOP_UNIX))
9637 mksession_nl = TRUE;
9638#endif
9639
9640 /* Write the version command for :mkvimrc */
9641 if (eap->cmdidx == CMD_mkvimrc)
9642 (void)put_line(fd, "version 6.0");
9643
9644#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009645 if (eap->cmdidx == CMD_mksession)
9646 {
9647 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9648 failed = TRUE;
9649 }
9650
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 if (eap->cmdidx != CMD_mkview)
9652#endif
9653 {
9654 /* Write setting 'compatible' first, because it has side effects.
9655 * For that same reason only do it when needed. */
9656 if (p_cp)
9657 (void)put_line(fd, "if !&cp | set cp | endif");
9658 else
9659 (void)put_line(fd, "if &cp | set nocp | endif");
9660 }
9661
9662#ifdef FEAT_SESSION
9663 if (!view_session
9664 || (eap->cmdidx == CMD_mksession
9665 && (*flagp & SSOP_OPTIONS)))
9666#endif
9667 failed |= (makemap(fd, NULL) == FAIL
9668 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9669
9670#ifdef FEAT_SESSION
9671 if (!failed && view_session)
9672 {
9673 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9674 failed = TRUE;
9675 if (eap->cmdidx == CMD_mksession)
9676 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009677 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678
Bram Moolenaard9462e32011-04-11 21:35:11 +02009679 dirnow = alloc(MAXPATHL);
9680 if (dirnow == NULL)
9681 failed = TRUE;
9682 else
9683 {
9684 /*
9685 * Change to session file's dir.
9686 */
9687 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009689 *dirnow = NUL;
9690 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9691 {
9692 if (vim_chdirfile(fname) == OK)
9693 shorten_fnames(TRUE);
9694 }
9695 else if (*dirnow != NUL
9696 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9697 {
9698 if (mch_chdir((char *)globaldir) == 0)
9699 shorten_fnames(TRUE);
9700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701
Bram Moolenaard9462e32011-04-11 21:35:11 +02009702 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703
Bram Moolenaard9462e32011-04-11 21:35:11 +02009704 /* restore original dir */
9705 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009707 {
9708 if (mch_chdir((char *)dirnow) != 0)
9709 EMSG(_(e_prev_dir));
9710 shorten_fnames(TRUE);
9711 }
9712 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 }
9714 }
9715 else
9716 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009717 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9718 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 }
9720 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9721 == FAIL)
9722 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009723 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9724 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009725 if (eap->cmdidx == CMD_mksession)
9726 {
9727 if (put_line(fd, "unlet SessionLoad") == FAIL)
9728 failed = TRUE;
9729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 }
9731#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009732 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9733 failed = TRUE;
9734
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 failed |= fclose(fd);
9736
9737 if (failed)
9738 EMSG(_(e_write));
9739#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9740 else if (eap->cmdidx == CMD_mksession)
9741 {
9742 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009743 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744
Bram Moolenaard9462e32011-04-11 21:35:11 +02009745 tbuf = alloc(MAXPATHL);
9746 if (tbuf != NULL)
9747 {
9748 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9749 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9750 vim_free(tbuf);
9751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 }
9753#endif
9754#ifdef MKSESSION_NL
9755 mksession_nl = FALSE;
9756#endif
9757 }
9758
9759#ifdef FEAT_BROWSE
9760theend:
9761 vim_free(browseFile);
9762#endif
9763#ifdef FEAT_SESSION
9764 vim_free(viewFile);
9765#endif
9766}
9767
Bram Moolenaardf177f62005-02-22 08:39:57 +00009768#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9769 || defined(PROTO)
9770 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009771vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009772{
9773 if (vim_mkdir(name, prot) != 0)
9774 {
9775 EMSG2(_("E739: Cannot create directory: %s"), name);
9776 return FAIL;
9777 }
9778 return OK;
9779}
9780#endif
9781
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782/*
9783 * Open a file for writing for an Ex command, with some checks.
9784 * Return file descriptor, or NULL on failure.
9785 */
9786 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009787open_exfile(
9788 char_u *fname,
9789 int forceit,
9790 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791{
9792 FILE *fd;
9793
9794#ifdef UNIX
9795 /* with Unix it is possible to open a directory */
9796 if (mch_isdir(fname))
9797 {
9798 EMSG2(_(e_isadir2), fname);
9799 return NULL;
9800 }
9801#endif
9802 if (!forceit && *mode != 'a' && vim_fexists(fname))
9803 {
9804 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9805 return NULL;
9806 }
9807
9808 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9809 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9810
9811 return fd;
9812}
9813
9814/*
9815 * ":mark" and ":k".
9816 */
9817 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009818ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819{
9820 pos_T pos;
9821
9822 if (*eap->arg == NUL) /* No argument? */
9823 EMSG(_(e_argreq));
9824 else if (eap->arg[1] != NUL) /* more than one character? */
9825 EMSG(_(e_trailing));
9826 else
9827 {
9828 pos = curwin->w_cursor; /* save curwin->w_cursor */
9829 curwin->w_cursor.lnum = eap->line2;
9830 beginline(BL_WHITE | BL_FIX);
9831 if (setmark(*eap->arg) == FAIL) /* set mark */
9832 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9833 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9834 }
9835}
9836
9837/*
9838 * Update w_topline, w_leftcol and the cursor position.
9839 */
9840 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009841update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842{
9843 check_cursor(); /* put cursor on valid line */
9844 update_topline();
9845 if (!curwin->w_p_wrap)
9846 validate_cursor();
9847 update_curswant();
9848}
9849
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850/*
9851 * ":normal[!] {commands}": Execute normal mode commands.
9852 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01009853 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009854ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856 int save_msg_scroll = msg_scroll;
9857 int save_restart_edit = restart_edit;
9858 int save_msg_didout = msg_didout;
9859 int save_State = State;
9860 tasave_T tabuf;
9861 int save_insertmode = p_im;
9862 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009863 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864#ifdef FEAT_MBYTE
9865 char_u *arg = NULL;
9866 int l;
9867 char_u *p;
9868#endif
9869
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009870 if (ex_normal_lock > 0)
9871 {
9872 EMSG(_(e_secure));
9873 return;
9874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 if (ex_normal_busy >= p_mmd)
9876 {
9877 EMSG(_("E192: Recursive use of :normal too deep"));
9878 return;
9879 }
9880 ++ex_normal_busy;
9881
9882 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9883 restart_edit = 0; /* don't go to Insert mode */
9884 p_im = FALSE; /* don't use 'insertmode' */
9885
9886#ifdef FEAT_MBYTE
9887 /*
9888 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9889 * this for the K_SPECIAL leading byte, otherwise special keys will not
9890 * work.
9891 */
9892 if (has_mbyte)
9893 {
9894 int len = 0;
9895
9896 /* Count the number of characters to be escaped. */
9897 for (p = eap->arg; *p != NUL; ++p)
9898 {
9899# ifdef FEAT_GUI
9900 if (*p == CSI) /* leadbyte CSI */
9901 len += 2;
9902# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009903 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9905# ifdef FEAT_GUI
9906 || *p == CSI
9907# endif
9908 )
9909 len += 2;
9910 }
9911 if (len > 0)
9912 {
9913 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9914 if (arg != NULL)
9915 {
9916 len = 0;
9917 for (p = eap->arg; *p != NUL; ++p)
9918 {
9919 arg[len++] = *p;
9920# ifdef FEAT_GUI
9921 if (*p == CSI)
9922 {
9923 arg[len++] = KS_EXTRA;
9924 arg[len++] = (int)KE_CSI;
9925 }
9926# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009927 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928 {
9929 arg[len++] = *++p;
9930 if (*p == K_SPECIAL)
9931 {
9932 arg[len++] = KS_SPECIAL;
9933 arg[len++] = KE_FILLER;
9934 }
9935# ifdef FEAT_GUI
9936 else if (*p == CSI)
9937 {
9938 arg[len++] = KS_EXTRA;
9939 arg[len++] = (int)KE_CSI;
9940 }
9941# endif
9942 }
9943 arg[len] = NUL;
9944 }
9945 }
9946 }
9947 }
9948#endif
9949
9950 /*
9951 * Save the current typeahead. This is required to allow using ":normal"
9952 * from an event handler and makes sure we don't hang when the argument
9953 * ends with half a command.
9954 */
9955 save_typeahead(&tabuf);
9956 if (tabuf.typebuf_valid)
9957 {
9958 /*
9959 * Repeat the :normal command for each line in the range. When no
9960 * range given, execute it just once, without positioning the cursor
9961 * first.
9962 */
9963 do
9964 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965 if (eap->addr_count != 0)
9966 {
9967 curwin->w_cursor.lnum = eap->line1++;
9968 curwin->w_cursor.col = 0;
9969 }
9970
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009971 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972#ifdef FEAT_MBYTE
9973 arg != NULL ? arg :
9974#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009975 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 }
9977 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9978 }
9979
9980 /* Might not return to the main loop when in an event handler. */
9981 update_topline_cursor();
9982
9983 /* Restore the previous typeahead. */
9984 restore_typeahead(&tabuf);
9985
9986 --ex_normal_busy;
9987 msg_scroll = save_msg_scroll;
9988 restart_edit = save_restart_edit;
9989 p_im = save_insertmode;
9990 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009991 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9993
9994 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +01009995 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +01009997#ifdef FEAT_MOUSE
9998 setmouse();
9999#endif
10000#ifdef CURSOR_SHAPE
10001 ui_cursor_shape(); /* may show different cursor shape */
10002#endif
10003
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004#ifdef FEAT_MBYTE
10005 vim_free(arg);
10006#endif
10007}
10008
10009/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010010 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011 */
10012 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010013ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010015 if (eap->forceit)
10016 {
10017 coladvance((colnr_T)MAXCOL);
10018 curwin->w_curswant = MAXCOL;
10019 curwin->w_set_curswant = FALSE;
10020 }
10021
Bram Moolenaara40c5002005-01-09 21:16:21 +000010022 /* Ignore the command when already in Insert mode. Inserting an
10023 * expression register that invokes a function can do this. */
10024 if (State & INSERT)
10025 return;
10026
Bram Moolenaar64969662005-12-14 21:59:55 +000010027 if (eap->cmdidx == CMD_startinsert)
10028 restart_edit = 'a';
10029 else if (eap->cmdidx == CMD_startreplace)
10030 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010032 restart_edit = 'V';
10033
10034 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010036 if (eap->cmdidx == CMD_startinsert)
10037 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 curwin->w_curswant = 0; /* avoid MAXCOL */
10039 }
10040}
10041
10042/*
10043 * ":stopinsert"
10044 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010046ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047{
10048 restart_edit = 0;
10049 stop_insert_mode = TRUE;
10050}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010052/*
10053 * Execute normal mode command "cmd".
10054 * "remap" can be REMAP_NONE or REMAP_YES.
10055 */
10056 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010057exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010058{
Bram Moolenaar25281632016-01-21 23:32:32 +010010059 /* Stuff the argument into the typeahead buffer. */
10060 ins_typebuf(cmd, remap, 0, TRUE, silent);
10061 exec_normal(FALSE);
10062}
Bram Moolenaar25281632016-01-21 23:32:32 +010010063
Bram Moolenaar25281632016-01-21 23:32:32 +010010064/*
10065 * Execute normal_cmd() until there is no typeahead left.
10066 */
10067 void
10068exec_normal(int was_typed)
10069{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010070 oparg_T oa;
10071
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010072 clear_oparg(&oa);
10073 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010074 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10075 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010076 {
10077 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010078 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010079 }
10080}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010081
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082#ifdef FEAT_FIND_ID
10083 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010084ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085{
10086 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10087 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10088 (linenr_T)1, (linenr_T)MAXLNUM);
10089}
10090
10091#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
10092/*
10093 * ":psearch"
10094 */
10095 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010096ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097{
10098 g_do_tagpreview = p_pvh;
10099 ex_findpat(eap);
10100 g_do_tagpreview = 0;
10101}
10102#endif
10103
10104 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010105ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106{
10107 int whole = TRUE;
10108 long n;
10109 char_u *p;
10110 int action;
10111
10112 switch (cmdnames[eap->cmdidx].cmd_name[2])
10113 {
10114 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10115 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10116 action = ACTION_GOTO;
10117 else
10118 action = ACTION_SHOW;
10119 break;
10120 case 'i': /* ":ilist" and ":dlist" */
10121 action = ACTION_SHOW_ALL;
10122 break;
10123 case 'u': /* ":ijump" and ":djump" */
10124 action = ACTION_GOTO;
10125 break;
10126 default: /* ":isplit" and ":dsplit" */
10127 action = ACTION_SPLIT;
10128 break;
10129 }
10130
10131 n = 1;
10132 if (vim_isdigit(*eap->arg)) /* get count */
10133 {
10134 n = getdigits(&eap->arg);
10135 eap->arg = skipwhite(eap->arg);
10136 }
10137 if (*eap->arg == '/') /* Match regexp, not just whole words */
10138 {
10139 whole = FALSE;
10140 ++eap->arg;
10141 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10142 if (*p)
10143 {
10144 *p++ = NUL;
10145 p = skipwhite(p);
10146
10147 /* Check for trailing illegal characters */
10148 if (!ends_excmd(*p))
10149 eap->errmsg = e_trailing;
10150 else
10151 eap->nextcmd = check_nextcmd(p);
10152 }
10153 }
10154 if (!eap->skip)
10155 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10156 whole, !eap->forceit,
10157 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10158 n, action, eap->line1, eap->line2);
10159}
10160#endif
10161
10162#ifdef FEAT_WINDOWS
10163
10164# ifdef FEAT_QUICKFIX
10165/*
10166 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10167 */
10168 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010169ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010171 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10173}
10174
10175/*
10176 * ":pedit"
10177 */
10178 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010179ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180{
10181 win_T *curwin_save = curwin;
10182
10183 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010184 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185 keep_help_flag = curwin_save->w_buffer->b_help;
10186 do_exedit(eap, NULL);
10187 keep_help_flag = FALSE;
10188 if (curwin != curwin_save && win_valid(curwin_save))
10189 {
10190 /* Return cursor to where we were */
10191 validate_cursor();
10192 redraw_later(VALID);
10193 win_enter(curwin_save, TRUE);
10194 }
10195 g_do_tagpreview = 0;
10196}
10197# endif
10198
10199/*
10200 * ":stag", ":stselect" and ":stjump".
10201 */
10202 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010203ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204{
10205 postponed_split = -1;
10206 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010207 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10209 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010210 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211}
10212#endif
10213
10214/*
10215 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10216 */
10217 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010218ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219{
10220 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10221}
10222
10223 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010224ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225{
10226 int cmd;
10227
10228 switch (name[1])
10229 {
10230 case 'j': cmd = DT_JUMP; /* ":tjump" */
10231 break;
10232 case 's': cmd = DT_SELECT; /* ":tselect" */
10233 break;
10234 case 'p': cmd = DT_PREV; /* ":tprevious" */
10235 break;
10236 case 'N': cmd = DT_PREV; /* ":tNext" */
10237 break;
10238 case 'n': cmd = DT_NEXT; /* ":tnext" */
10239 break;
10240 case 'o': cmd = DT_POP; /* ":pop" */
10241 break;
10242 case 'f': /* ":tfirst" */
10243 case 'r': cmd = DT_FIRST; /* ":trewind" */
10244 break;
10245 case 'l': cmd = DT_LAST; /* ":tlast" */
10246 break;
10247 default: /* ":tag" */
10248#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010249 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 {
10251 do_cstag(eap);
10252 return;
10253 }
10254#endif
10255 cmd = DT_TAG;
10256 break;
10257 }
10258
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010259 if (name[0] == 'l')
10260 {
10261#ifndef FEAT_QUICKFIX
10262 ex_ni(eap);
10263 return;
10264#else
10265 cmd = DT_LTAG;
10266#endif
10267 }
10268
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10270 eap->forceit, TRUE);
10271}
10272
10273/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010274 * Check "str" for starting with a special cmdline variable.
10275 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10276 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10277 */
10278 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010279find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010280{
10281 int len;
10282 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010283 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010284 "%",
10285#define SPEC_PERC 0
10286 "#",
10287#define SPEC_HASH 1
10288 "<cword>", /* cursor word */
10289#define SPEC_CWORD 2
10290 "<cWORD>", /* cursor WORD */
10291#define SPEC_CCWORD 3
10292 "<cfile>", /* cursor path name */
10293#define SPEC_CFILE 4
10294 "<sfile>", /* ":so" file name */
10295#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010296 "<slnum>", /* ":so" file line number */
10297#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010298#ifdef FEAT_AUTOCMD
10299 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010300# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010301 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010302# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010303 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010304# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010305#endif
10306#ifdef FEAT_CLIENTSERVER
10307 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010308# ifdef FEAT_AUTOCMD
10309# define SPEC_CLIENT 10
10310# else
10311# define SPEC_CLIENT 7
10312# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010313#endif
10314 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010315
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010316 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010317 {
10318 len = (int)STRLEN(spec_str[i]);
10319 if (STRNCMP(src, spec_str[i], len) == 0)
10320 {
10321 *usedlen = len;
10322 return i;
10323 }
10324 }
10325 return -1;
10326}
10327
10328/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329 * Evaluate cmdline variables.
10330 *
10331 * change '%' to curbuf->b_ffname
10332 * '#' to curwin->w_altfile
10333 * '<cword>' to word under the cursor
10334 * '<cWORD>' to WORD under the cursor
10335 * '<cfile>' to path name under the cursor
10336 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010337 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338 * '<afile>' to file name for autocommand
10339 * '<abuf>' to buffer number for autocommand
10340 * '<amatch>' to matching name for autocommand
10341 *
10342 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10343 * "" for error without a message) and NULL is returned.
10344 * Returns an allocated string if a valid match was found.
10345 * Returns NULL if no match was found. "usedlen" then still contains the
10346 * number of characters to skip.
10347 */
10348 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010349eval_vars(
10350 char_u *src, /* pointer into commandline */
10351 char_u *srcstart, /* beginning of valid memory for src */
10352 int *usedlen, /* characters after src that are used */
10353 linenr_T *lnump, /* line number for :e command, or NULL */
10354 char_u **errormsg, /* pointer to error message */
10355 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010356 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357{
10358 int i;
10359 char_u *s;
10360 char_u *result;
10361 char_u *resultbuf = NULL;
10362 int resultlen;
10363 buf_T *buf;
10364 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10365 int spec_idx;
10366#ifdef FEAT_MODIFY_FNAME
10367 int skip_mod = FALSE;
10368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370
10371 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010372 if (escaped != NULL)
10373 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374
10375 /*
10376 * Check if there is something to do.
10377 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010378 spec_idx = find_cmdline_var(src, usedlen);
10379 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 {
10381 *usedlen = 1;
10382 return NULL;
10383 }
10384
10385 /*
10386 * Skip when preceded with a backslash "\%" and "\#".
10387 * Note: In "\\%" the % is also not recognized!
10388 */
10389 if (src > srcstart && src[-1] == '\\')
10390 {
10391 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010392 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 return NULL;
10394 }
10395
10396 /*
10397 * word or WORD under cursor
10398 */
10399 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10400 {
10401 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10402 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10403 if (resultlen == 0)
10404 {
10405 *errormsg = (char_u *)"";
10406 return NULL;
10407 }
10408 }
10409
10410 /*
10411 * '#': Alternate file name
10412 * '%': Current file name
10413 * File name under the cursor
10414 * File name for autocommand
10415 * and following modifiers
10416 */
10417 else
10418 {
10419 switch (spec_idx)
10420 {
10421 case SPEC_PERC: /* '%': current file */
10422 if (curbuf->b_fname == NULL)
10423 {
10424 result = (char_u *)"";
10425 valid = 0; /* Must have ":p:h" to be valid */
10426 }
10427 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429 break;
10430
10431 case SPEC_HASH: /* '#' or "#99": alternate file */
10432 if (src[1] == '#') /* "##": the argument list */
10433 {
10434 result = arg_all();
10435 resultbuf = result;
10436 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010437 if (escaped != NULL)
10438 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439#ifdef FEAT_MODIFY_FNAME
10440 skip_mod = TRUE;
10441#endif
10442 break;
10443 }
10444 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010445 if (*s == '<') /* "#<99" uses v:oldfiles */
10446 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 i = (int)getdigits(&s);
10448 *usedlen = (int)(s - src); /* length of what we expand */
10449
Bram Moolenaard812df62008-11-09 12:46:09 +000010450 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010452 if (*usedlen < 2)
10453 {
10454 /* Should we give an error message for #<text? */
10455 *usedlen = 1;
10456 return NULL;
10457 }
10458#ifdef FEAT_EVAL
10459 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10460 (long)i);
10461 if (result == NULL)
10462 {
10463 *errormsg = (char_u *)"";
10464 return NULL;
10465 }
10466#else
10467 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470 }
10471 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010472 {
10473 buf = buflist_findnr(i);
10474 if (buf == NULL)
10475 {
10476 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10477 return NULL;
10478 }
10479 if (lnump != NULL)
10480 *lnump = ECMD_LAST;
10481 if (buf->b_fname == NULL)
10482 {
10483 result = (char_u *)"";
10484 valid = 0; /* Must have ":p:h" to be valid */
10485 }
10486 else
10487 result = buf->b_fname;
10488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489 break;
10490
10491#ifdef FEAT_SEARCHPATH
10492 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010493 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494 if (result == NULL)
10495 {
10496 *errormsg = (char_u *)"";
10497 return NULL;
10498 }
10499 resultbuf = result; /* remember allocated string */
10500 break;
10501#endif
10502
10503#ifdef FEAT_AUTOCMD
10504 case SPEC_AFILE: /* file name for autocommand */
10505 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010506 if (result != NULL && !autocmd_fname_full)
10507 {
10508 /* Still need to turn the fname into a full path. It is
10509 * postponed to avoid a delay when <afile> is not used. */
10510 autocmd_fname_full = TRUE;
10511 result = FullName_save(autocmd_fname, FALSE);
10512 vim_free(autocmd_fname);
10513 autocmd_fname = result;
10514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 if (result == NULL)
10516 {
10517 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10518 return NULL;
10519 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010520 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 break;
10522
10523 case SPEC_ABUF: /* buffer number for autocommand */
10524 if (autocmd_bufnr <= 0)
10525 {
10526 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10527 return NULL;
10528 }
10529 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10530 result = strbuf;
10531 break;
10532
10533 case SPEC_AMATCH: /* match name for autocommand */
10534 result = autocmd_match;
10535 if (result == NULL)
10536 {
10537 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10538 return NULL;
10539 }
10540 break;
10541
10542#endif
10543 case SPEC_SFILE: /* file name for ":so" command */
10544 result = sourcing_name;
10545 if (result == NULL)
10546 {
10547 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10548 return NULL;
10549 }
10550 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010551 case SPEC_SLNUM: /* line in file for ":so" command */
10552 if (sourcing_name == NULL || sourcing_lnum == 0)
10553 {
10554 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10555 return NULL;
10556 }
10557 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10558 result = strbuf;
10559 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560#if defined(FEAT_CLIENTSERVER)
10561 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010562 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10563 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564 result = strbuf;
10565 break;
10566#endif
10567 }
10568
10569 resultlen = (int)STRLEN(result); /* length of new string */
10570 if (src[*usedlen] == '<') /* remove the file name extension */
10571 {
10572 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574 resultlen = (int)(s - result);
10575 }
10576#ifdef FEAT_MODIFY_FNAME
10577 else if (!skip_mod)
10578 {
10579 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10580 &resultlen);
10581 if (result == NULL)
10582 {
10583 *errormsg = (char_u *)"";
10584 return NULL;
10585 }
10586 }
10587#endif
10588 }
10589
10590 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10591 {
10592 if (valid != VALID_HEAD + VALID_PATH)
10593 /* xgettext:no-c-format */
10594 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10595 else
10596 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10597 result = NULL;
10598 }
10599 else
10600 result = vim_strnsave(result, resultlen);
10601 vim_free(resultbuf);
10602 return result;
10603}
10604
10605/*
10606 * Concatenate all files in the argument list, separated by spaces, and return
10607 * it in one allocated string.
10608 * Spaces and backslashes in the file names are escaped with a backslash.
10609 * Returns NULL when out of memory.
10610 */
10611 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010612arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613{
10614 int len;
10615 int idx;
10616 char_u *retval = NULL;
10617 char_u *p;
10618
10619 /*
10620 * Do this loop two times:
10621 * first time: compute the total length
10622 * second time: concatenate the names
10623 */
10624 for (;;)
10625 {
10626 len = 0;
10627 for (idx = 0; idx < ARGCOUNT; ++idx)
10628 {
10629 p = alist_name(&ARGLIST[idx]);
10630 if (p != NULL)
10631 {
10632 if (len > 0)
10633 {
10634 /* insert a space in between names */
10635 if (retval != NULL)
10636 retval[len] = ' ';
10637 ++len;
10638 }
10639 for ( ; *p != NUL; ++p)
10640 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010641 if (*p == ' '
10642#ifndef BACKSLASH_IN_FILENAME
10643 || *p == '\\'
10644#endif
10645 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646 {
10647 /* insert a backslash */
10648 if (retval != NULL)
10649 retval[len] = '\\';
10650 ++len;
10651 }
10652 if (retval != NULL)
10653 retval[len] = *p;
10654 ++len;
10655 }
10656 }
10657 }
10658
10659 /* second time: break here */
10660 if (retval != NULL)
10661 {
10662 retval[len] = NUL;
10663 break;
10664 }
10665
10666 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010667 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668 if (retval == NULL)
10669 break;
10670 }
10671
10672 return retval;
10673}
10674
10675#if defined(FEAT_AUTOCMD) || defined(PROTO)
10676/*
10677 * Expand the <sfile> string in "arg".
10678 *
10679 * Returns an allocated string, or NULL for any error.
10680 */
10681 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010682expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683{
10684 char_u *errormsg;
10685 int len;
10686 char_u *result;
10687 char_u *newres;
10688 char_u *repl;
10689 int srclen;
10690 char_u *p;
10691
10692 result = vim_strsave(arg);
10693 if (result == NULL)
10694 return NULL;
10695
10696 for (p = result; *p; )
10697 {
10698 if (STRNCMP(p, "<sfile>", 7) != 0)
10699 ++p;
10700 else
10701 {
10702 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010703 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704 if (errormsg != NULL)
10705 {
10706 if (*errormsg)
10707 emsg(errormsg);
10708 vim_free(result);
10709 return NULL;
10710 }
10711 if (repl == NULL) /* no match (cannot happen) */
10712 {
10713 p += srclen;
10714 continue;
10715 }
10716 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10717 newres = alloc(len);
10718 if (newres == NULL)
10719 {
10720 vim_free(repl);
10721 vim_free(result);
10722 return NULL;
10723 }
10724 mch_memmove(newres, result, (size_t)(p - result));
10725 STRCPY(newres + (p - result), repl);
10726 len = (int)STRLEN(newres);
10727 STRCAT(newres, p + srclen);
10728 vim_free(repl);
10729 vim_free(result);
10730 result = newres;
10731 p = newres + len; /* continue after the match */
10732 }
10733 }
10734
10735 return result;
10736}
10737#endif
10738
10739#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010010740static int ses_winsizes(FILE *fd, int restore_size,
10741 win_T *tab_firstwin);
10742static int ses_win_rec(FILE *fd, frame_T *fr);
10743static frame_T *ses_skipframe(frame_T *fr);
10744static int ses_do_frame(frame_T *fr);
10745static int ses_do_win(win_T *wp);
10746static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
10747static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
10748static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749
10750/*
10751 * Write openfile commands for the current buffers to an .exrc file.
10752 * Return FAIL on error, OK otherwise.
10753 */
10754 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010755makeopens(
10756 FILE *fd,
10757 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758{
10759 buf_T *buf;
10760 int only_save_windows = TRUE;
10761 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762 int restore_size = TRUE;
10763 win_T *wp;
10764 char_u *sname;
10765 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010766 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010767 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010768 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010769 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010770 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010771 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772
10773 if (ssop_flags & SSOP_BUFFERS)
10774 only_save_windows = FALSE; /* Save ALL buffers */
10775
10776 /*
10777 * Begin by setting the this_session variable, and then other
10778 * sessionable variables.
10779 */
10780#ifdef FEAT_EVAL
10781 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10782 return FAIL;
10783 if (ssop_flags & SSOP_GLOBALS)
10784 if (store_session_globals(fd) == FAIL)
10785 return FAIL;
10786#endif
10787
10788 /*
10789 * Close all windows but one.
10790 */
10791 if (put_line(fd, "silent only") == FAIL)
10792 return FAIL;
10793
10794 /*
10795 * Now a :cd command to the session directory or the current directory
10796 */
10797 if (ssop_flags & SSOP_SESDIR)
10798 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010799 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10800 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801 return FAIL;
10802 }
10803 else if (ssop_flags & SSOP_CURDIR)
10804 {
10805 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10806 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010807 || fputs("cd ", fd) < 0
10808 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10809 || put_eol(fd) == FAIL)
10810 {
10811 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814 vim_free(sname);
10815 }
10816
10817 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010818 * If there is an empty, unnamed buffer we will wipe it out later.
10819 * Remember the buffer number.
10820 */
10821 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10822 return FAIL;
10823 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10824 return FAIL;
10825 if (put_line(fd, "endif") == FAIL)
10826 return FAIL;
10827
10828 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829 * Now save the current files, current buffer first.
10830 */
10831 if (put_line(fd, "set shortmess=aoO") == FAIL)
10832 return FAIL;
10833
10834 /* Now put the other buffers into the buffer list */
10835 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10836 {
10837 if (!(only_save_windows && buf->b_nwindows == 0)
10838 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10839 && buf->b_fname != NULL
10840 && buf->b_p_bl)
10841 {
10842 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10843 : buf->b_wininfo->wi_fpos.lnum) < 0
10844 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10845 return FAIL;
10846 }
10847 }
10848
10849 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010850 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10852 return FAIL;
10853
10854 if (ssop_flags & SSOP_RESIZE)
10855 {
10856 /* Note: after the restore we still check it worked!*/
10857 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10858 || put_eol(fd) == FAIL)
10859 return FAIL;
10860 }
10861
10862#ifdef FEAT_GUI
10863 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10864 {
10865 int x, y;
10866
10867 if (gui_mch_get_winpos(&x, &y) == OK)
10868 {
10869 /* Note: after the restore we still check it worked!*/
10870 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10871 return FAIL;
10872 }
10873 }
10874#endif
10875
10876 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010877 * When there are two or more tabpages and 'showtabline' is 1 the tabline
10878 * will be displayed when creating the next tab. That resizes the windows
10879 * in the first tab, which may cause problems. Set 'showtabline' to 2
10880 * temporarily to avoid that.
10881 */
10882 if (p_stal == 1 && first_tabpage->tp_next != NULL)
10883 {
10884 if (put_line(fd, "set stal=2") == FAIL)
10885 return FAIL;
10886 restore_stal = TRUE;
10887 }
10888
10889 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010890 * May repeat putting Windows for each tab, when "tabpages" is in
10891 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010892 * Don't use goto_tabpage(), it may change directory and trigger
10893 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010895 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010896 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010897 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020010899 int need_tabnew = FALSE;
10900 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010901
Bram Moolenaar18144c82006-04-12 21:52:12 +000010902 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010904 tabpage_T *tp = find_tabpage(tabnr);
10905
10906 if (tp == NULL)
10907 break; /* done all tab pages */
10908 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010909 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010910 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010911 tab_topframe = topframe;
10912 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010913 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010914 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010915 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010916 tab_topframe = tp->tp_topframe;
10917 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010918 if (tabnr > 1)
10919 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921
Bram Moolenaar18144c82006-04-12 21:52:12 +000010922 /*
10923 * Before creating the window layout, try loading one file. If this
10924 * is aborted we don't end up with a number of useless windows.
10925 * This may have side effects! (e.g., compressed or network file).
10926 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010927 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010928 {
10929 if (ses_do_win(wp)
10930 && wp->w_buffer->b_ffname != NULL
10931 && !wp->w_buffer->b_help
10932#ifdef FEAT_QUICKFIX
10933 && !bt_nofile(wp->w_buffer)
10934#endif
10935 )
10936 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010937 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010938 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10939 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010940 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010941 if (!wp->w_arg_idx_invalid)
10942 edited_win = wp;
10943 break;
10944 }
10945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010947 /* If no file got edited create an empty tab page. */
10948 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10949 return FAIL;
10950
Bram Moolenaar18144c82006-04-12 21:52:12 +000010951 /*
10952 * Save current window layout.
10953 */
10954 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010956 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010958 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10959 return FAIL;
10960 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10961 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962
Bram Moolenaar18144c82006-04-12 21:52:12 +000010963 /*
10964 * Check if window sizes can be restored (no windows omitted).
10965 * Remember the window number of the current window after restoring.
10966 */
10967 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010968 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010969 {
10970 if (ses_do_win(wp))
10971 ++nr;
10972 else
10973 restore_size = FALSE;
10974 if (curwin == wp)
10975 cnr = nr;
10976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977
Bram Moolenaar18144c82006-04-12 21:52:12 +000010978 /* Go to the first window. */
10979 if (put_line(fd, "wincmd t") == FAIL)
10980 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010981
Bram Moolenaar18144c82006-04-12 21:52:12 +000010982 /*
10983 * If more than one window, see if sizes can be restored.
10984 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10985 * resized when moving between windows.
10986 * Do this before restoring the view, so that the topline and the
10987 * cursor can be set. This is done again below.
10988 */
10989 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10990 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010991 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010992 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993
Bram Moolenaar18144c82006-04-12 21:52:12 +000010994 /*
10995 * Restore the view of the window (options, file, cursor, etc.).
10996 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010997 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010998 {
10999 if (!ses_do_win(wp))
11000 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011001 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11002 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011003 return FAIL;
11004 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11005 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011006 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011007 }
11008
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011009 /* The argument index in the first tab page is zero, need to set it in
11010 * each window. For further tab pages it's the window where we do
11011 * "tabedit". */
11012 cur_arg_idx = next_arg_idx;
11013
Bram Moolenaar18144c82006-04-12 21:52:12 +000011014 /*
11015 * Restore cursor to the current window if it's not the first one.
11016 */
11017 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11018 || put_eol(fd) == FAIL))
11019 return FAIL;
11020
11021 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011022 * Restore window sizes again after jumping around in windows, because
11023 * the current window has a minimum size while others may not.
11024 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011025 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011026 return FAIL;
11027
Bram Moolenaar18144c82006-04-12 21:52:12 +000011028 /* Don't continue in another tab page when doing only the current one
11029 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011030 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011031 break;
11032 }
11033
11034 if (ssop_flags & SSOP_TABPAGES)
11035 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011036 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11037 || put_eol(fd) == FAIL)
11038 return FAIL;
11039 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011040 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11041 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011042
Bram Moolenaar9c102382006-05-03 21:26:49 +000011043 /*
11044 * Wipe out an empty unnamed buffer we started in.
11045 */
11046 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11047 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011048 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011049 return FAIL;
11050 if (put_line(fd, "endif") == FAIL)
11051 return FAIL;
11052 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11053 return FAIL;
11054
11055 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11056 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11057 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11058 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059
11060 /*
11061 * Lastly, execute the x.vim file if it exists.
11062 */
11063 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11064 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011065 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066 || put_line(fd, "endif") == FAIL)
11067 return FAIL;
11068
11069 return OK;
11070}
11071
11072 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011073ses_winsizes(
11074 FILE *fd,
11075 int restore_size,
11076 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077{
11078 int n = 0;
11079 win_T *wp;
11080
11081 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11082 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011083 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084 {
11085 if (!ses_do_win(wp))
11086 continue;
11087 ++n;
11088
11089 /* restore height when not full height */
11090 if (wp->w_height + wp->w_status_height < topframe->fr_height
11091 && (fprintf(fd,
11092 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11093 n, (long)wp->w_height, Rows / 2, Rows) < 0
11094 || put_eol(fd) == FAIL))
11095 return FAIL;
11096
11097 /* restore width when not full width */
11098 if (wp->w_width < Columns && (fprintf(fd,
11099 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11100 n, (long)wp->w_width, Columns / 2, Columns) < 0
11101 || put_eol(fd) == FAIL))
11102 return FAIL;
11103 }
11104 }
11105 else
11106 {
11107 /* Just equalise window sizes */
11108 if (put_line(fd, "wincmd =") == FAIL)
11109 return FAIL;
11110 }
11111 return OK;
11112}
11113
11114/*
11115 * Write commands to "fd" to recursively create windows for frame "fr",
11116 * horizontally and vertically split.
11117 * After the commands the last window in the frame is the current window.
11118 * Returns FAIL when writing the commands to "fd" fails.
11119 */
11120 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011121ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122{
11123 frame_T *frc;
11124 int count = 0;
11125
11126 if (fr->fr_layout != FR_LEAF)
11127 {
11128 /* Find first frame that's not skipped and then create a window for
11129 * each following one (first frame is already there). */
11130 frc = ses_skipframe(fr->fr_child);
11131 if (frc != NULL)
11132 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11133 {
11134 /* Make window as big as possible so that we have lots of room
11135 * to split. */
11136 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11137 || put_line(fd, fr->fr_layout == FR_COL
11138 ? "split" : "vsplit") == FAIL)
11139 return FAIL;
11140 ++count;
11141 }
11142
11143 /* Go back to the first window. */
11144 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11145 ? "%dwincmd k" : "%dwincmd h", count) < 0
11146 || put_eol(fd) == FAIL))
11147 return FAIL;
11148
11149 /* Recursively create frames/windows in each window of this column or
11150 * row. */
11151 frc = ses_skipframe(fr->fr_child);
11152 while (frc != NULL)
11153 {
11154 ses_win_rec(fd, frc);
11155 frc = ses_skipframe(frc->fr_next);
11156 /* Go to next window. */
11157 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11158 return FAIL;
11159 }
11160 }
11161 return OK;
11162}
11163
11164/*
11165 * Skip frames that don't contain windows we want to save in the Session.
11166 * Returns NULL when there none.
11167 */
11168 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011169ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011170{
11171 frame_T *frc;
11172
11173 for (frc = fr; frc != NULL; frc = frc->fr_next)
11174 if (ses_do_frame(frc))
11175 break;
11176 return frc;
11177}
11178
11179/*
11180 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11181 * the Session.
11182 */
11183 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011184ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185{
11186 frame_T *frc;
11187
11188 if (fr->fr_layout == FR_LEAF)
11189 return ses_do_win(fr->fr_win);
11190 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11191 if (ses_do_frame(frc))
11192 return TRUE;
11193 return FALSE;
11194}
11195
11196/*
11197 * Return non-zero if window "wp" is to be stored in the Session.
11198 */
11199 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011200ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011201{
11202 if (wp->w_buffer->b_fname == NULL
11203#ifdef FEAT_QUICKFIX
11204 /* When 'buftype' is "nofile" can't restore the window contents. */
11205 || bt_nofile(wp->w_buffer)
11206#endif
11207 )
11208 return (ssop_flags & SSOP_BLANK);
11209 if (wp->w_buffer->b_help)
11210 return (ssop_flags & SSOP_HELP);
11211 return TRUE;
11212}
11213
11214/*
11215 * Write commands to "fd" to restore the view of a window.
11216 * Caller must make sure 'scrolloff' is zero.
11217 */
11218 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011219put_view(
11220 FILE *fd,
11221 win_T *wp,
11222 int add_edit, /* add ":edit" command to view */
11223 unsigned *flagp, /* vop_flags or ssop_flags */
11224 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011225 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011226{
11227 win_T *save_curwin;
11228 int f;
11229 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011230 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011231
11232 /* Always restore cursor position for ":mksession". For ":mkview" only
11233 * when 'viewoptions' contains "cursor". */
11234 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11235
11236 /*
11237 * Local argument list.
11238 */
11239 if (wp->w_alist == &global_alist)
11240 {
11241 if (put_line(fd, "argglobal") == FAIL)
11242 return FAIL;
11243 }
11244 else
11245 {
11246 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11247 flagp == &vop_flags
11248 || !(*flagp & SSOP_CURDIR)
11249 || wp->w_localdir != NULL, flagp) == FAIL)
11250 return FAIL;
11251 }
11252
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011253 /* Only when part of a session: restore the argument index. Some
11254 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011255 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011256 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011258 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259 || put_eol(fd) == FAIL)
11260 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011261 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262 }
11263
11264 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011265 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266 {
11267 /*
11268 * Load the file.
11269 */
11270 if (wp->w_buffer->b_ffname != NULL
11271#ifdef FEAT_QUICKFIX
11272 && !bt_nofile(wp->w_buffer)
11273#endif
11274 )
11275 {
11276 /*
11277 * Editing a file in this buffer: use ":edit file".
11278 * This may have side effects! (e.g., compressed or network file).
11279 */
11280 if (fputs("edit ", fd) < 0
11281 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11282 return FAIL;
11283 }
11284 else
11285 {
11286 /* No file in this buffer, just make it empty. */
11287 if (put_line(fd, "enew") == FAIL)
11288 return FAIL;
11289#ifdef FEAT_QUICKFIX
11290 if (wp->w_buffer->b_ffname != NULL)
11291 {
11292 /* The buffer does have a name, but it's not a file name. */
11293 if (fputs("file ", fd) < 0
11294 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11295 return FAIL;
11296 }
11297#endif
11298 do_cursor = FALSE;
11299 }
11300 }
11301
11302 /*
11303 * Local mappings and abbreviations.
11304 */
11305 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11306 && makemap(fd, wp->w_buffer) == FAIL)
11307 return FAIL;
11308
11309 /*
11310 * Local options. Need to go to the window temporarily.
11311 * Store only local values when using ":mkview" and when ":mksession" is
11312 * used and 'sessionoptions' doesn't include "options".
11313 * Some folding options are always stored when "folds" is included,
11314 * otherwise the folds would not be restored correctly.
11315 */
11316 save_curwin = curwin;
11317 curwin = wp;
11318 curbuf = curwin->w_buffer;
11319 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11320 f = makeset(fd, OPT_LOCAL,
11321 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11322#ifdef FEAT_FOLDING
11323 else if (*flagp & SSOP_FOLDS)
11324 f = makefoldset(fd);
11325#endif
11326 else
11327 f = OK;
11328 curwin = save_curwin;
11329 curbuf = curwin->w_buffer;
11330 if (f == FAIL)
11331 return FAIL;
11332
11333#ifdef FEAT_FOLDING
11334 /*
11335 * Save Folds when 'buftype' is empty and for help files.
11336 */
11337 if ((*flagp & SSOP_FOLDS)
11338 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011339# ifdef FEAT_QUICKFIX
11340 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11341# endif
11342 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343 {
11344 if (put_folds(fd, wp) == FAIL)
11345 return FAIL;
11346 }
11347#endif
11348
11349 /*
11350 * Set the cursor after creating folds, since that moves the cursor.
11351 */
11352 if (do_cursor)
11353 {
11354
11355 /* Restore the cursor line in the file and relatively in the
11356 * window. Don't use "G", it changes the jumplist. */
11357 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11358 (long)wp->w_cursor.lnum,
11359 (long)(wp->w_cursor.lnum - wp->w_topline),
11360 (long)wp->w_height / 2, (long)wp->w_height) < 0
11361 || put_eol(fd) == FAIL
11362 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11363 || put_line(fd, "exe s:l") == FAIL
11364 || put_line(fd, "normal! zt") == FAIL
11365 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11366 || put_eol(fd) == FAIL)
11367 return FAIL;
11368 /* Restore the cursor column and left offset when not wrapping. */
11369 if (wp->w_cursor.col == 0)
11370 {
11371 if (put_line(fd, "normal! 0") == FAIL)
11372 return FAIL;
11373 }
11374 else
11375 {
11376 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11377 {
11378 if (fprintf(fd,
11379 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011380 (long)wp->w_virtcol + 1,
11381 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382 (long)wp->w_width / 2, (long)wp->w_width) < 0
11383 || put_eol(fd) == FAIL
11384 || put_line(fd, "if s:c > 0") == FAIL
11385 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011386 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11387 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388 || put_eol(fd) == FAIL
11389 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011390 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011391 || put_eol(fd) == FAIL
11392 || put_line(fd, "endif") == FAIL)
11393 return FAIL;
11394 }
11395 else
11396 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011397 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398 || put_eol(fd) == FAIL)
11399 return FAIL;
11400 }
11401 }
11402 }
11403
11404 /*
11405 * Local directory.
11406 */
11407 if (wp->w_localdir != NULL)
11408 {
11409 if (fputs("lcd ", fd) < 0
11410 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11411 || put_eol(fd) == FAIL)
11412 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011413 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414 }
11415
11416 return OK;
11417}
11418
11419/*
11420 * Write an argument list to the session file.
11421 * Returns FAIL if writing fails.
11422 */
11423 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011424ses_arglist(
11425 FILE *fd,
11426 char *cmd,
11427 garray_T *gap,
11428 int fullname, /* TRUE: use full path name */
11429 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430{
11431 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011432 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433 char_u *s;
11434
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011435 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11436 return FAIL;
11437 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011438 return FAIL;
11439 for (i = 0; i < gap->ga_len; ++i)
11440 {
11441 /* NULL file names are skipped (only happens when out of memory). */
11442 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11443 if (s != NULL)
11444 {
11445 if (fullname)
11446 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011447 buf = alloc(MAXPATHL);
11448 if (buf != NULL)
11449 {
11450 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11451 s = buf;
11452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011453 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011454 if (fputs("argadd ", fd) < 0
11455 || ses_put_fname(fd, s, flagp) == FAIL
11456 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011457 {
11458 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011459 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011460 }
11461 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011462 }
11463 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011464 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465}
11466
11467/*
11468 * Write a buffer name to the session file.
11469 * Also ends the line.
11470 * Returns FAIL if writing fails.
11471 */
11472 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011473ses_fname(FILE *fd, buf_T *buf, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474{
11475 char_u *name;
11476
11477 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011478 * the session file will be sourced.
11479 * Don't do this for ":mkview", we don't know the current directory.
11480 * Don't do this after ":lcd", we don't keep track of what the current
11481 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482 if (buf->b_sfname != NULL
11483 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011484 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011485#ifdef FEAT_AUTOCHDIR
11486 && !p_acd
11487#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011488 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011489 name = buf->b_sfname;
11490 else
11491 name = buf->b_ffname;
11492 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11493 return FAIL;
11494 return OK;
11495}
11496
11497/*
11498 * Write a file name to the session file.
11499 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11500 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011501 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502 */
11503 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011504ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505{
11506 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011507 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011508 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011509
11510 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011511 if (sname == NULL)
11512 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011513
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011514 if (*flagp & SSOP_SLASH)
11515 {
11516 /* change all backslashes to forward slashes */
11517 for (p = sname; *p != NUL; mb_ptr_adv(p))
11518 if (*p == '\\')
11519 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011520 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011521
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011522 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011523 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011525 if (p == NULL)
11526 return FAIL;
11527
11528 /* write the result */
11529 if (fputs((char *)p, fd) < 0)
11530 retval = FAIL;
11531
11532 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011533 return retval;
11534}
11535
11536/*
11537 * ":loadview [nr]"
11538 */
11539 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011540ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011541{
11542 char_u *fname;
11543
11544 fname = get_view_file(*eap->arg);
11545 if (fname != NULL)
11546 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011547 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011548 vim_free(fname);
11549 }
11550}
11551
11552/*
11553 * Get the name of the view file for the current buffer.
11554 */
11555 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011556get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011557{
11558 int len = 0;
11559 char_u *p, *s;
11560 char_u *retval;
11561 char_u *sname;
11562
11563 if (curbuf->b_ffname == NULL)
11564 {
11565 EMSG(_(e_noname));
11566 return NULL;
11567 }
11568 sname = home_replace_save(NULL, curbuf->b_ffname);
11569 if (sname == NULL)
11570 return NULL;
11571
11572 /*
11573 * We want a file name without separators, because we're not going to make
11574 * a directory.
11575 * "normal" path separator -> "=+"
11576 * "=" -> "=="
11577 * ":" path separator -> "=-"
11578 */
11579 for (p = sname; *p; ++p)
11580 if (*p == '=' || vim_ispathsep(*p))
11581 ++len;
11582 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11583 if (retval != NULL)
11584 {
11585 STRCPY(retval, p_vdir);
11586 add_pathsep(retval);
11587 s = retval + STRLEN(retval);
11588 for (p = sname; *p; ++p)
11589 {
11590 if (*p == '=')
11591 {
11592 *s++ = '=';
11593 *s++ = '=';
11594 }
11595 else if (vim_ispathsep(*p))
11596 {
11597 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011598#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011599 if (*p == ':')
11600 *s++ = '-';
11601 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011602#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011603 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604 }
11605 else
11606 *s++ = *p;
11607 }
11608 *s++ = '=';
11609 *s++ = c;
11610 STRCPY(s, ".vim");
11611 }
11612
11613 vim_free(sname);
11614 return retval;
11615}
11616
11617#endif /* FEAT_SESSION */
11618
11619/*
11620 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11621 * Return FAIL for a write error.
11622 */
11623 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011624put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625{
11626 if (
11627#ifdef USE_CRNL
11628 (
11629# ifdef MKSESSION_NL
11630 !mksession_nl &&
11631# endif
11632 (putc('\r', fd) < 0)) ||
11633#endif
11634 (putc('\n', fd) < 0))
11635 return FAIL;
11636 return OK;
11637}
11638
11639/*
11640 * Write a line to "fd".
11641 * Return FAIL for a write error.
11642 */
11643 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011644put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011645{
11646 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11647 return FAIL;
11648 return OK;
11649}
11650
11651#ifdef FEAT_VIMINFO
11652/*
11653 * ":rviminfo" and ":wviminfo".
11654 */
11655 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011656ex_viminfo(
11657 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011658{
11659 char_u *save_viminfo;
11660
11661 save_viminfo = p_viminfo;
11662 if (*p_viminfo == NUL)
11663 p_viminfo = (char_u *)"'100";
11664 if (eap->cmdidx == CMD_rviminfo)
11665 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011666 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11667 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011668 EMSG(_("E195: Cannot open viminfo file for reading"));
11669 }
11670 else
11671 write_viminfo(eap->arg, eap->forceit);
11672 p_viminfo = save_viminfo;
11673}
11674#endif
11675
11676#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011677/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011678 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011679 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011680 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011681 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011682dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011683{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011684 if (fname == NULL)
11685 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011686 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011687}
11688#endif
11689
11690/*
11691 * ":behave {mswin,xterm}"
11692 */
11693 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011694ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011695{
11696 if (STRCMP(eap->arg, "mswin") == 0)
11697 {
11698 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11699 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11700 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11701 set_option_value((char_u *)"keymodel", 0L,
11702 (char_u *)"startsel,stopsel", 0);
11703 }
11704 else if (STRCMP(eap->arg, "xterm") == 0)
11705 {
11706 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11707 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11708 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11709 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11710 }
11711 else
11712 EMSG2(_(e_invarg2), eap->arg);
11713}
11714
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011715#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11716/*
11717 * Function given to ExpandGeneric() to obtain the possible arguments of the
11718 * ":behave {mswin,xterm}" command.
11719 */
11720 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011721get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011722{
11723 if (idx == 0)
11724 return (char_u *)"mswin";
11725 if (idx == 1)
11726 return (char_u *)"xterm";
11727 return NULL;
11728}
11729#endif
11730
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731#ifdef FEAT_AUTOCMD
11732static int filetype_detect = FALSE;
11733static int filetype_plugin = FALSE;
11734static int filetype_indent = FALSE;
11735
11736/*
11737 * ":filetype [plugin] [indent] {on,off,detect}"
11738 * on: Load the filetype.vim file to install autocommands for file types.
11739 * off: Load the ftoff.vim file to remove all autocommands for file types.
11740 * plugin on: load filetype.vim and ftplugin.vim
11741 * plugin off: load ftplugof.vim
11742 * indent on: load filetype.vim and indent.vim
11743 * indent off: load indoff.vim
11744 */
11745 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011746ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011747{
11748 char_u *arg = eap->arg;
11749 int plugin = FALSE;
11750 int indent = FALSE;
11751
11752 if (*eap->arg == NUL)
11753 {
11754 /* Print current status. */
11755 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11756 filetype_detect ? "ON" : "OFF",
11757 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11758 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11759 return;
11760 }
11761
11762 /* Accept "plugin" and "indent" in any order. */
11763 for (;;)
11764 {
11765 if (STRNCMP(arg, "plugin", 6) == 0)
11766 {
11767 plugin = TRUE;
11768 arg = skipwhite(arg + 6);
11769 continue;
11770 }
11771 if (STRNCMP(arg, "indent", 6) == 0)
11772 {
11773 indent = TRUE;
11774 arg = skipwhite(arg + 6);
11775 continue;
11776 }
11777 break;
11778 }
11779 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11780 {
11781 if (*arg == 'o' || !filetype_detect)
11782 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011783 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011784 filetype_detect = TRUE;
11785 if (plugin)
11786 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011787 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011788 filetype_plugin = TRUE;
11789 }
11790 if (indent)
11791 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011792 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011793 filetype_indent = TRUE;
11794 }
11795 }
11796 if (*arg == 'd')
11797 {
11798 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011799 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011800 }
11801 }
11802 else if (STRCMP(arg, "off") == 0)
11803 {
11804 if (plugin || indent)
11805 {
11806 if (plugin)
11807 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011808 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809 filetype_plugin = FALSE;
11810 }
11811 if (indent)
11812 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011813 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011814 filetype_indent = FALSE;
11815 }
11816 }
11817 else
11818 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011819 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011820 filetype_detect = FALSE;
11821 }
11822 }
11823 else
11824 EMSG2(_(e_invarg2), arg);
11825}
11826
11827/*
11828 * ":setfiletype {name}"
11829 */
11830 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011831ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011832{
11833 if (!did_filetype)
11834 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11835}
11836#endif
11837
Bram Moolenaar071d4272004-06-13 20:20:40 +000011838 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011839ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011840{
11841#ifdef FEAT_DIGRAPHS
11842 if (*eap->arg != NUL)
11843 putdigraph(eap->arg);
11844 else
11845 listdigraphs();
11846#else
11847 EMSG(_("E196: No digraphs in this version"));
11848#endif
11849}
11850
11851 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011852ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011853{
11854 int flags = 0;
11855
11856 if (eap->cmdidx == CMD_setlocal)
11857 flags = OPT_LOCAL;
11858 else if (eap->cmdidx == CMD_setglobal)
11859 flags = OPT_GLOBAL;
11860#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11861 if (cmdmod.browse && flags == 0)
11862 ex_options(eap);
11863 else
11864#endif
11865 (void)do_set(eap->arg, flags);
11866}
11867
11868#ifdef FEAT_SEARCH_EXTRA
11869/*
11870 * ":nohlsearch"
11871 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011873ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011874{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010011875 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011876 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877}
11878
11879/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011880 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011881 * Sets nextcmd to the start of the next command, if any. Also called when
11882 * skipping commands to find the next command.
11883 */
11884 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011885ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011886{
11887 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011888 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011889 char_u *end;
11890 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011891 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011892
11893 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011894 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011895 else
11896 {
11897 EMSG(e_invcmd);
11898 return;
11899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011900
11901 /* First clear any old pattern. */
11902 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011903 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011904
11905 if (ends_excmd(*eap->arg))
11906 end = eap->arg;
11907 else if ((STRNICMP(eap->arg, "none", 4) == 0
11908 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11909 end = eap->arg + 4;
11910 else
11911 {
11912 p = skiptowhite(eap->arg);
11913 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011914 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011915 p = skipwhite(p);
11916 if (*p == NUL)
11917 {
11918 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011919 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011920 EMSG2(_(e_invarg2), eap->arg);
11921 return;
11922 }
11923 end = skip_regexp(p + 1, *p, TRUE, NULL);
11924 if (!eap->skip)
11925 {
11926 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11927 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011928 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011929 eap->errmsg = e_trailing;
11930 return;
11931 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011932 if (*end != *p)
11933 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011934 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011935 EMSG2(_(e_invarg2), p);
11936 return;
11937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938
11939 c = *end;
11940 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020011941 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011942 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011943 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011944 }
11945 }
11946 eap->nextcmd = find_nextcmd(end);
11947}
11948#endif
11949
11950#ifdef FEAT_CRYPT
11951/*
11952 * ":X": Get crypt key
11953 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011955ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011956{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010011957 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020011958 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011959}
11960#endif
11961
11962#ifdef FEAT_FOLDING
11963 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011964ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011965{
11966 if (foldManualAllowed(TRUE))
11967 foldCreate(eap->line1, eap->line2);
11968}
11969
11970 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011971ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011972{
11973 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11974 eap->forceit, FALSE);
11975}
11976
11977 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011978ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979{
11980 linenr_T lnum;
11981
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011982#ifdef FEAT_CLIPBOARD
11983 start_global_changes();
11984#endif
11985
Bram Moolenaar071d4272004-06-13 20:20:40 +000011986 /* First set the marks for all lines closed/open. */
11987 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11988 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11989 ml_setmarked(lnum);
11990
11991 /* Execute the command on the marked lines. */
11992 global_exe(eap->arg);
11993 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011994#ifdef FEAT_CLIPBOARD
11995 end_global_changes();
11996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011997}
11998#endif